1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 89, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern struct obstack
*current_obstack
;
43 /* Obstack used for remembering decision points of breadth-first. */
45 static struct obstack search_obstack
;
47 /* Methods for pushing and popping objects to and from obstacks. */
50 push_stack_level (obstack
, tp
, size
)
51 struct obstack
*obstack
;
52 char *tp
; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
55 struct stack_level
*stack
;
56 obstack_grow (obstack
, tp
, size
);
57 stack
= (struct stack_level
*) ((char*)obstack_next_free (obstack
) - size
);
58 obstack_finish (obstack
);
59 stack
->obstack
= obstack
;
60 stack
->first
= (tree
*) obstack_base (obstack
);
61 stack
->limit
= obstack_room (obstack
) / sizeof (tree
*);
66 pop_stack_level (stack
)
67 struct stack_level
*stack
;
69 struct stack_level
*tem
= stack
;
70 struct obstack
*obstack
= tem
->obstack
;
72 obstack_free (obstack
, tem
);
76 #define search_level stack_level
77 static struct search_level
*search_stack
;
79 static tree next_baselink
PARAMS ((tree
));
80 static tree get_vbase_1
PARAMS ((tree
, tree
, unsigned int *));
81 static tree lookup_field_1
PARAMS ((tree
, tree
));
82 static tree convert_pointer_to_single_level
PARAMS ((tree
, tree
));
83 static int lookup_fnfields_here
PARAMS ((tree
, tree
));
84 static int is_subobject_of_p
PARAMS ((tree
, tree
));
85 static int hides
PARAMS ((tree
, tree
));
86 static tree virtual_context
PARAMS ((tree
, tree
, tree
));
87 static tree dfs_check_overlap
PARAMS ((tree
, void *));
88 static tree dfs_no_overlap_yet
PARAMS ((tree
, void *));
89 static int get_base_distance_recursive
90 PARAMS ((tree
, int, int, int, int *, tree
*, tree
,
91 int, int *, int, int));
92 static int dynamic_cast_base_recurse
PARAMS ((tree
, tree
, int, tree
*));
93 static void expand_upcast_fixups
94 PARAMS ((tree
, tree
, tree
, tree
, tree
, tree
, tree
*));
95 static void fixup_virtual_upcast_offsets
96 PARAMS ((tree
, tree
, int, int, tree
, tree
, tree
, tree
,
98 static tree marked_vtable_pathp
PARAMS ((tree
, void *));
99 static tree unmarked_vtable_pathp
PARAMS ((tree
, void *));
100 static tree marked_new_vtablep
PARAMS ((tree
, void *));
101 static tree unmarked_new_vtablep
PARAMS ((tree
, void *));
102 static tree marked_pushdecls_p
PARAMS ((tree
, void *));
103 static tree unmarked_pushdecls_p
PARAMS ((tree
, void *));
105 static tree dfs_debug_unmarkedp
PARAMS ((tree
, void *));
106 static tree dfs_debug_mark
PARAMS ((tree
, void *));
108 static tree dfs_find_vbases
PARAMS ((tree
, void *));
109 static tree dfs_clear_vbase_slots
PARAMS ((tree
, void *));
110 static tree dfs_init_vbase_pointers
PARAMS ((tree
, void *));
111 static tree dfs_get_vbase_types
PARAMS ((tree
, void *));
112 static tree dfs_push_type_decls
PARAMS ((tree
, void *));
113 static tree dfs_push_decls
PARAMS ((tree
, void *));
114 static tree dfs_unuse_fields
PARAMS ((tree
, void *));
115 static tree add_conversions
PARAMS ((tree
, void *));
116 static tree get_virtuals_named_this
PARAMS ((tree
, tree
));
117 static tree get_virtual_destructor
PARAMS ((tree
, void *));
118 static tree tree_has_any_destructor_p
PARAMS ((tree
, void *));
119 static int covariant_return_p
PARAMS ((tree
, tree
));
120 static int check_final_overrider
PARAMS ((tree
, tree
));
121 static struct search_level
*push_search_level
122 PARAMS ((struct stack_level
*, struct obstack
*));
123 static struct search_level
*pop_search_level
124 PARAMS ((struct stack_level
*));
126 PARAMS ((tree
, tree (*) (tree
, void *), tree (*) (tree
, void *),
128 static tree lookup_field_queue_p
PARAMS ((tree
, void *));
129 static tree lookup_field_r
PARAMS ((tree
, void *));
130 static tree get_virtuals_named_this_r
PARAMS ((tree
, void *));
131 static tree context_for_name_lookup
PARAMS ((tree
));
132 static tree canonical_binfo
PARAMS ((tree
));
133 static tree shared_marked_p
PARAMS ((tree
, void *));
134 static tree shared_unmarked_p
PARAMS ((tree
, void *));
135 static int dependent_base_p
PARAMS ((tree
));
136 static tree dfs_accessible_queue_p
PARAMS ((tree
, void *));
137 static tree dfs_accessible_p
PARAMS ((tree
, void *));
138 static tree dfs_access_in_type
PARAMS ((tree
, void *));
139 static tree access_in_type
PARAMS ((tree
, tree
));
140 static tree dfs_canonical_queue
PARAMS ((tree
, void *));
141 static tree dfs_assert_unmarked_p
PARAMS ((tree
, void *));
142 static void assert_canonical_unmarked
PARAMS ((tree
));
143 static int protected_accessible_p
PARAMS ((tree
, tree
, tree
, tree
));
144 static int friend_accessible_p
PARAMS ((tree
, tree
, tree
, tree
));
145 static void setup_class_bindings
PARAMS ((tree
, int));
146 static int template_self_reference_p
PARAMS ((tree
, tree
));
147 static void fixup_all_virtual_upcast_offsets
PARAMS ((tree
, tree
));
148 static tree dfs_mark_primary_bases
PARAMS ((tree
, void *));
149 static tree get_shared_vbase_if_not_primary
PARAMS ((tree
, void *));
150 static tree dfs_find_vbase_instance
PARAMS ((tree
, void *));
151 static tree dfs_get_pure_virtuals
PARAMS ((tree
, void *));
153 /* Allocate a level of searching. */
155 static struct search_level
*
156 push_search_level (stack
, obstack
)
157 struct stack_level
*stack
;
158 struct obstack
*obstack
;
160 struct search_level tem
;
163 return push_stack_level (obstack
, (char *)&tem
, sizeof (tem
));
166 /* Discard a level of search allocation. */
168 static struct search_level
*
169 pop_search_level (obstack
)
170 struct stack_level
*obstack
;
172 register struct search_level
*stack
= pop_stack_level (obstack
);
177 /* Variables for gathering statistics. */
178 #ifdef GATHER_STATISTICS
179 static int n_fields_searched
;
180 static int n_calls_lookup_field
, n_calls_lookup_field_1
;
181 static int n_calls_lookup_fnfields
, n_calls_lookup_fnfields_1
;
182 static int n_calls_get_base_type
;
183 static int n_outer_fields_searched
;
184 static int n_contexts_saved
;
185 #endif /* GATHER_STATISTICS */
188 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
189 the same type as the type given in PARENT. To be optimal, we want
190 the first one that is found by going through the least number of
193 This uses a clever algorithm that updates *depth when we find the vbase,
194 and cuts off other paths of search when they reach that depth. */
197 get_vbase_1 (parent
, binfo
, depth
)
203 tree rval
= NULL_TREE
;
205 if (BINFO_TYPE (binfo
) == parent
&& TREE_VIA_VIRTUAL (binfo
))
213 binfos
= BINFO_BASETYPES (binfo
);
214 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
216 /* Process base types. */
217 for (i
= 0; i
< n_baselinks
; i
++)
219 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
225 nrval
= get_vbase_1 (parent
, base_binfo
, depth
);
233 /* Return the shortest path to vbase PARENT within BINFO, ignoring
234 access and ambiguity. */
237 get_vbase (parent
, binfo
)
241 unsigned int d
= (unsigned int)-1;
242 return get_vbase_1 (parent
, binfo
, &d
);
245 /* Convert EXPR to a virtual base class of type TYPE. We know that
246 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
247 the type of what expr points to has a virtual base of type TYPE. */
250 convert_pointer_to_vbase (type
, expr
)
254 tree vb
= get_vbase (type
, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr
))));
255 return convert_pointer_to_real (vb
, expr
);
258 /* Check whether the type given in BINFO is derived from PARENT. If
259 it isn't, return 0. If it is, but the derivation is MI-ambiguous
260 AND protect != 0, emit an error message and return error_mark_node.
262 Otherwise, if TYPE is derived from PARENT, return the actual base
263 information, unless a one of the protection violations below
264 occurs, in which case emit an error message and return error_mark_node.
266 If PROTECT is 1, then check if access to a public field of PARENT
267 would be private. Also check for ambiguity. */
270 get_binfo (parent
, binfo
, protect
)
271 register tree parent
, binfo
;
274 tree type
= NULL_TREE
;
276 tree rval
= NULL_TREE
;
278 if (TREE_CODE (parent
) == TREE_VEC
)
279 parent
= BINFO_TYPE (parent
);
280 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent
)))
281 my_friendly_abort (89);
283 if (TREE_CODE (binfo
) == TREE_VEC
)
284 type
= BINFO_TYPE (binfo
);
285 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
288 my_friendly_abort (90);
290 dist
= get_base_distance (parent
, binfo
, protect
, &rval
);
294 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
296 return error_mark_node
;
298 else if (dist
== -2 && protect
)
300 cp_error ("type `%T' is ambiguous base class for type `%T'", parent
,
302 return error_mark_node
;
308 /* This is the newer depth first get_base_distance routine. */
311 get_base_distance_recursive (binfo
, depth
, is_private
, rval
,
312 rval_private_ptr
, new_binfo_ptr
, parent
,
313 protect
, via_virtual_ptr
, via_virtual
,
314 current_scope_in_chain
)
316 int depth
, is_private
, rval
;
317 int *rval_private_ptr
;
318 tree
*new_binfo_ptr
, parent
;
319 int protect
, *via_virtual_ptr
, via_virtual
;
320 int current_scope_in_chain
;
326 && !current_scope_in_chain
327 && is_friend (BINFO_TYPE (binfo
), current_scope ()))
328 current_scope_in_chain
= 1;
330 if (BINFO_TYPE (binfo
) == parent
|| binfo
== parent
)
335 /* This is the first time we've found parent. */
337 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr
),
338 BINFO_OFFSET (binfo
))
339 && *via_virtual_ptr
&& via_virtual
)
341 /* A new path to the same vbase. If this one has better
342 access or is shorter, take it. */
345 better
= *rval_private_ptr
- is_private
;
347 better
= rval
- depth
;
351 /* Ambiguous base class. */
354 /* If we get an ambiguity between virtual and non-virtual base
355 class, return the non-virtual in case we are ignoring
357 better
= *via_virtual_ptr
- via_virtual
;
363 *rval_private_ptr
= is_private
;
364 *new_binfo_ptr
= binfo
;
365 *via_virtual_ptr
= via_virtual
;
371 binfos
= BINFO_BASETYPES (binfo
);
372 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
375 /* Process base types. */
376 for (i
= 0; i
< n_baselinks
; i
++)
378 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
383 || (!TREE_VIA_PUBLIC (base_binfo
)
384 && !(TREE_VIA_PROTECTED (base_binfo
)
385 && current_scope_in_chain
)
386 && !is_friend (BINFO_TYPE (binfo
), current_scope ()))));
387 int this_virtual
= via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
);
389 rval
= get_base_distance_recursive (base_binfo
, depth
, via_private
,
390 rval
, rval_private_ptr
,
391 new_binfo_ptr
, parent
,
392 protect
, via_virtual_ptr
,
394 current_scope_in_chain
);
396 /* If we've found a non-virtual, ambiguous base class, we don't need
397 to keep searching. */
398 if (rval
== -2 && *via_virtual_ptr
== 0)
405 /* Return the number of levels between type PARENT and the type given
406 in BINFO, following the leftmost path to PARENT not found along a
407 virtual path, if there are no real PARENTs (all come from virtual
408 base classes), then follow the shortest public path to PARENT.
410 Return -1 if TYPE is not derived from PARENT.
411 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
413 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
415 If PATH_PTR is non-NULL, then also build the list of types
416 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
419 PARENT can also be a binfo, in which case that exact parent is found
420 and no other. convert_pointer_to_real uses this functionality.
422 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
425 get_base_distance (parent
, binfo
, protect
, path_ptr
)
426 register tree parent
, binfo
;
431 int rval_private
= 0;
432 tree type
= NULL_TREE
;
433 tree new_binfo
= NULL_TREE
;
435 int watch_access
= protect
;
437 /* Should we be completing types here? */
438 if (TREE_CODE (parent
) != TREE_VEC
)
439 parent
= complete_type (TYPE_MAIN_VARIANT (parent
));
441 complete_type (TREE_TYPE (parent
));
443 if (TREE_CODE (binfo
) == TREE_VEC
)
444 type
= BINFO_TYPE (binfo
);
445 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
447 type
= complete_type (binfo
);
448 binfo
= TYPE_BINFO (type
);
451 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo
) == NULL_TREE
,
455 my_friendly_abort (92);
457 if (parent
== type
|| parent
== binfo
)
459 /* If the distance is 0, then we don't really need
460 a path pointer, but we shouldn't let garbage go back. */
469 rval
= get_base_distance_recursive (binfo
, 0, 0, -1,
470 &rval_private
, &new_binfo
, parent
,
471 watch_access
, &via_virtual
, 0,
474 /* Access restrictions don't count if we found an ambiguous basetype. */
475 if (rval
== -2 && protect
>= 0)
478 if (rval
&& protect
&& rval_private
)
481 /* If they gave us the real vbase binfo, which isn't in the main binfo
482 tree, deal with it. This happens when we are called from
483 expand_upcast_fixups. */
484 if (rval
== -1 && TREE_CODE (parent
) == TREE_VEC
485 && parent
== BINFO_FOR_VBASE (BINFO_TYPE (parent
), type
))
487 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent
) == binfo
, 980827);
493 *path_ptr
= new_binfo
;
497 /* Worker function for get_dynamic_cast_base_type. */
500 dynamic_cast_base_recurse (subtype
, binfo
, via_virtual
, offset_ptr
)
510 if (BINFO_TYPE (binfo
) == subtype
)
516 *offset_ptr
= BINFO_OFFSET (binfo
);
521 binfos
= BINFO_BASETYPES (binfo
);
522 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
523 for (i
= 0; i
< n_baselinks
; i
++)
525 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
528 if (!TREE_VIA_PUBLIC (base_binfo
))
530 rval
= dynamic_cast_base_recurse
531 (subtype
, base_binfo
,
532 via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
), offset_ptr
);
536 worst
= worst
>= 0 ? -3 : worst
;
539 else if (rval
== -3 && worst
!= -1)
545 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
546 started from is related to the required TARGET type, in order to optimize
547 the inheritance graph search. This information is independant of the
548 current context, and ignores private paths, hence get_base_distance is
549 inappropriate. Return a TREE specifying the base offset, BOFF.
550 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
551 and there are no public virtual SUBTYPE bases.
552 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
553 BOFF == -2, SUBTYPE is not a public base.
554 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
557 get_dynamic_cast_base_type (subtype
, target
)
561 tree offset
= NULL_TREE
;
562 int boff
= dynamic_cast_base_recurse (subtype
, TYPE_BINFO (target
),
567 return build_int_2 (boff
, -1);
570 /* Search for a member with name NAME in a multiple inheritance lattice
571 specified by TYPE. If it does not exist, return NULL_TREE.
572 If the member is ambiguously referenced, return `error_mark_node'.
573 Otherwise, return the FIELD_DECL. */
575 /* Do a 1-level search for NAME as a member of TYPE. The caller must
576 figure out whether it can access this field. (Since it is only one
577 level, this is reasonable.) */
580 lookup_field_1 (type
, name
)
585 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
586 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
587 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
588 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
589 the code often worked even when we treated the index as a list
594 && DECL_LANG_SPECIFIC (TYPE_NAME (type
))
595 && DECL_SORTED_FIELDS (TYPE_NAME (type
)))
597 tree
*fields
= &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type
)), 0);
598 int lo
= 0, hi
= TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type
)));
605 #ifdef GATHER_STATISTICS
607 #endif /* GATHER_STATISTICS */
609 if (DECL_NAME (fields
[i
]) > name
)
611 else if (DECL_NAME (fields
[i
]) < name
)
615 /* We might have a nested class and a field with the
616 same name; we sorted them appropriately via
617 field_decl_cmp, so just look for the last field with
620 && DECL_NAME (fields
[i
+1]) == name
)
628 field
= TYPE_FIELDS (type
);
630 #ifdef GATHER_STATISTICS
631 n_calls_lookup_field_1
++;
632 #endif /* GATHER_STATISTICS */
635 #ifdef GATHER_STATISTICS
637 #endif /* GATHER_STATISTICS */
638 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field
)) == 'd', 0);
639 if (DECL_NAME (field
) == NULL_TREE
640 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
642 tree temp
= lookup_field_1 (TREE_TYPE (field
), name
);
646 if (TREE_CODE (field
) == USING_DECL
)
647 /* For now, we're just treating member using declarations as
648 old ARM-style access declarations. Thus, there's no reason
649 to return a USING_DECL, and the rest of the compiler can't
650 handle it. Once the class is defined, these are purged
651 from TYPE_FIELDS anyhow; see handle_using_decl. */
653 else if (DECL_NAME (field
) == name
)
655 if ((TREE_CODE(field
) == VAR_DECL
|| TREE_CODE(field
) == CONST_DECL
)
656 && DECL_ASSEMBLER_NAME (field
) != NULL
)
657 GNU_xref_ref(current_function_decl
,
658 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field
)));
661 field
= TREE_CHAIN (field
);
664 if (name
== vptr_identifier
)
666 /* Give the user what s/he thinks s/he wants. */
667 if (TYPE_POLYMORPHIC_P (type
))
668 return TYPE_VFIELD (type
);
673 /* There are a number of cases we need to be aware of here:
674 current_class_type current_function_decl
681 Those last two make life interesting. If we're in a function which is
682 itself inside a class, we need decls to go into the fn's decls (our
683 second case below). But if we're in a class and the class itself is
684 inside a function, we need decls to go into the decls for the class. To
685 achieve this last goal, we must see if, when both current_class_ptr and
686 current_function_decl are set, the class was declared inside that
687 function. If so, we know to put the decls into the class's scope. */
692 if (current_function_decl
== NULL_TREE
)
693 return current_class_type
;
694 if (current_class_type
== NULL_TREE
)
695 return current_function_decl
;
696 if ((DECL_FUNCTION_MEMBER_P (current_function_decl
)
697 && same_type_p (DECL_CONTEXT (current_function_decl
),
699 || (DECL_FRIEND_CONTEXT (current_function_decl
)
700 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl
),
701 current_class_type
)))
702 return current_function_decl
;
704 return current_class_type
;
707 /* Returns non-zero if we are currently in a function scope. Note
708 that this function returns zero if we are within a local class, but
709 not within a member function body of the local class. */
712 at_function_scope_p ()
714 tree cs
= current_scope ();
715 return cs
&& TREE_CODE (cs
) == FUNCTION_DECL
;
718 /* Return the scope of DECL, as appropriate when doing name-lookup. */
721 context_for_name_lookup (decl
)
726 For the purposes of name lookup, after the anonymous union
727 definition, the members of the anonymous union are considered to
728 have been defined in the scope in which teh anonymous union is
730 tree context
= CP_DECL_CONTEXT (decl
);
732 while (TYPE_P (context
) && ANON_AGGR_TYPE_P (context
))
733 context
= TYPE_CONTEXT (context
);
735 context
= global_namespace
;
740 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
744 canonical_binfo (binfo
)
747 return (TREE_VIA_VIRTUAL (binfo
)
748 ? TYPE_BINFO (BINFO_TYPE (binfo
)) : binfo
);
751 /* A queue function that simply ensures that we walk into the
752 canonical versions of virtual bases. */
755 dfs_canonical_queue (binfo
, data
)
757 void *data ATTRIBUTE_UNUSED
;
759 return canonical_binfo (binfo
);
762 /* Called via dfs_walk from assert_canonical_unmarked. */
765 dfs_assert_unmarked_p (binfo
, data
)
767 void *data ATTRIBUTE_UNUSED
;
769 my_friendly_assert (!BINFO_MARKED (binfo
), 0);
773 /* Asserts that all the nodes below BINFO (using the canonical
774 versions of virtual bases) are unmarked. */
777 assert_canonical_unmarked (binfo
)
780 dfs_walk (binfo
, dfs_assert_unmarked_p
, dfs_canonical_queue
, 0);
783 /* If BINFO is marked, return a canonical version of BINFO.
784 Otherwise, return NULL_TREE. */
787 shared_marked_p (binfo
, data
)
791 binfo
= canonical_binfo (binfo
);
792 return markedp (binfo
, data
);
795 /* If BINFO is not marked, return a canonical version of BINFO.
796 Otherwise, return NULL_TREE. */
799 shared_unmarked_p (binfo
, data
)
803 binfo
= canonical_binfo (binfo
);
804 return unmarkedp (binfo
, data
);
807 /* Called from access_in_type via dfs_walk. Calculate the access to
808 DATA (which is really a DECL) in BINFO. */
811 dfs_access_in_type (binfo
, data
)
815 tree decl
= (tree
) data
;
816 tree type
= BINFO_TYPE (binfo
);
817 tree access
= NULL_TREE
;
819 if (context_for_name_lookup (decl
) == type
)
821 /* If we have desceneded to the scope of DECL, just note the
822 appropriate access. */
823 if (TREE_PRIVATE (decl
))
824 access
= access_private_node
;
825 else if (TREE_PROTECTED (decl
))
826 access
= access_protected_node
;
828 access
= access_public_node
;
832 /* First, check for an access-declaration that gives us more
833 access to the DECL. The CONST_DECL for an enumeration
834 constant will not have DECL_LANG_SPECIFIC, and thus no
836 if (DECL_LANG_SPECIFIC (decl
))
838 access
= purpose_member (type
, DECL_ACCESS (decl
));
840 access
= TREE_VALUE (access
);
849 /* Otherwise, scan our baseclasses, and pick the most favorable
851 binfos
= BINFO_BASETYPES (binfo
);
852 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
853 for (i
= 0; i
< n_baselinks
; ++i
)
855 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
856 tree base_access
= TREE_CHAIN (canonical_binfo (base_binfo
));
858 if (!base_access
|| base_access
== access_private_node
)
859 /* If it was not accessible in the base, or only
860 accessible as a private member, we can't access it
862 base_access
= NULL_TREE
;
863 else if (TREE_VIA_PROTECTED (base_binfo
))
864 /* Public and protected members in the base are
866 base_access
= access_protected_node
;
867 else if (!TREE_VIA_PUBLIC (base_binfo
))
868 /* Public and protected members in the base are
870 base_access
= access_private_node
;
872 /* See if the new access, via this base, gives more
873 access than our previous best access. */
875 (base_access
== access_public_node
876 || (base_access
== access_protected_node
877 && access
!= access_public_node
)
878 || (base_access
== access_private_node
881 access
= base_access
;
883 /* If the new access is public, we can't do better. */
884 if (access
== access_public_node
)
891 /* Note the access to DECL in TYPE. */
892 TREE_CHAIN (binfo
) = access
;
894 /* Mark TYPE as visited so that if we reach it again we do not
895 duplicate our efforts here. */
896 SET_BINFO_MARKED (binfo
);
901 /* Return the access to DECL in TYPE. */
904 access_in_type (type
, decl
)
908 tree binfo
= TYPE_BINFO (type
);
910 /* We must take into account
914 If a name can be reached by several paths through a multiple
915 inheritance graph, the access is that of the path that gives
918 The algorithm we use is to make a post-order depth-first traversal
919 of the base-class hierarchy. As we come up the tree, we annotate
920 each node with the most lenient access. */
921 dfs_walk_real (binfo
, 0, dfs_access_in_type
, shared_unmarked_p
, decl
);
922 dfs_walk (binfo
, dfs_unmark
, shared_marked_p
, 0);
923 assert_canonical_unmarked (binfo
);
925 return TREE_CHAIN (binfo
);
928 /* Called from dfs_accessible_p via dfs_walk. */
931 dfs_accessible_queue_p (binfo
, data
)
933 void *data ATTRIBUTE_UNUSED
;
935 if (BINFO_MARKED (binfo
))
938 /* If this class is inherited via private or protected inheritance,
939 then we can't see it, unless we are a friend of the subclass. */
940 if (!TREE_VIA_PUBLIC (binfo
)
941 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
945 return canonical_binfo (binfo
);
948 /* Called from dfs_accessible_p via dfs_walk. */
951 dfs_accessible_p (binfo
, data
)
955 int protected_ok
= data
!= 0;
958 /* We marked the binfos while computing the access in each type.
959 So, we unmark as we go now. */
960 SET_BINFO_MARKED (binfo
);
962 access
= TREE_CHAIN (binfo
);
963 if (access
== access_public_node
964 || (access
== access_protected_node
&& protected_ok
))
966 else if (access
&& is_friend (BINFO_TYPE (binfo
), current_scope ()))
972 /* Returns non-zero if it is OK to access DECL when named in TYPE
973 through an object indiated by BINFO in the context of DERIVED. */
976 protected_accessible_p (type
, decl
, derived
, binfo
)
984 /* We're checking this clause from [class.access.base]
986 m as a member of N is protected, and the reference occurs in a
987 member or friend of class N, or in a member or friend of a
988 class P derived from N, where m as a member of P is private or
991 If DERIVED isn't derived from TYPE, then it certainly does not
993 if (!DERIVED_FROM_P (type
, derived
))
996 access
= access_in_type (derived
, decl
);
997 if (same_type_p (derived
, type
))
999 if (access
!= access_private_node
)
1002 else if (access
!= access_private_node
1003 && access
!= access_protected_node
)
1006 /* [class.protected]
1008 When a friend or a member function of a derived class references
1009 a protected nonstatic member of a base class, an access check
1010 applies in addition to those described earlier in clause
1011 _class.access_.4) Except when forming a pointer to member
1012 (_expr.unary.op_), the access must be through a pointer to,
1013 reference to, or object of the derived class itself (or any class
1014 derived from that class) (_expr.ref_). If the access is to form
1015 a pointer to member, the nested-name-specifier shall name the
1016 derived class (or any class derived from that class). */
1017 if (DECL_NONSTATIC_MEMBER_P (decl
))
1019 /* We can tell through what the reference is occurring by
1020 chasing BINFO up to the root. */
1022 while (BINFO_INHERITANCE_CHAIN (t
))
1023 t
= BINFO_INHERITANCE_CHAIN (t
);
1025 if (!DERIVED_FROM_P (derived
, BINFO_TYPE (t
)))
1032 /* Returns non-zero if SCOPE is a friend of a type which would be able
1033 to acces DECL, named in TYPE, through the object indicated by
1037 friend_accessible_p (scope
, type
, decl
, binfo
)
1043 tree befriending_classes
;
1049 if (TREE_CODE (scope
) == FUNCTION_DECL
1050 || DECL_FUNCTION_TEMPLATE_P (scope
))
1051 befriending_classes
= DECL_BEFRIENDING_CLASSES (scope
);
1052 else if (TYPE_P (scope
))
1053 befriending_classes
= CLASSTYPE_BEFRIENDING_CLASSES (scope
);
1057 for (t
= befriending_classes
; t
; t
= TREE_CHAIN (t
))
1058 if (protected_accessible_p (type
, decl
, TREE_VALUE (t
), binfo
))
1061 /* Nested classes are implicitly friends of their enclosing types, as
1062 per core issue 45 (this is a change from the standard). */
1064 for (t
= TYPE_CONTEXT (scope
); t
&& TYPE_P (t
); t
= TYPE_CONTEXT (t
))
1065 if (protected_accessible_p (type
, decl
, t
, binfo
))
1068 if (TREE_CODE (scope
) == FUNCTION_DECL
1069 || DECL_FUNCTION_TEMPLATE_P (scope
))
1071 /* Perhaps this SCOPE is a member of a class which is a
1073 if (DECL_CLASS_SCOPE_P (decl
)
1074 && friend_accessible_p (DECL_CONTEXT (scope
), type
,
1078 /* Or an instantiation of something which is a friend. */
1079 if (DECL_TEMPLATE_INFO (scope
))
1080 return friend_accessible_p (DECL_TI_TEMPLATE (scope
),
1083 else if (CLASSTYPE_TEMPLATE_INFO (scope
))
1084 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope
),
1090 /* Perform access control on TYPE_DECL VAL, which was looked up in TYPE.
1091 This is fairly complex, so here's the design:
1093 The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
1094 start to process a top-level declaration.
1095 As we process the decl-specifier-seq for the declaration, any types we
1096 see that might need access control are passed to type_access_control,
1097 which defers checking by adding them to type_lookups.
1098 When we are done with the decl-specifier-seq, we record the lookups we've
1099 seen in the lookups field of the typed_declspecs nonterminal.
1100 When we process the first declarator, either in parse_decl or
1101 begin_function_definition, we call save_type_access_control,
1102 which stores the lookups from the decl-specifier-seq in
1103 current_type_lookups.
1104 As we finish with each declarator, we process everything in type_lookups
1105 via decl_type_access_control, which resets type_lookups to the value of
1106 current_type_lookups for subsequent declarators.
1107 When we enter a function, we set type_lookups to error_mark_node, so all
1108 lookups are processed immediately. */
1111 type_access_control (type
, val
)
1114 if (val
== NULL_TREE
|| TREE_CODE (val
) != TYPE_DECL
1115 || ! DECL_CLASS_SCOPE_P (val
))
1118 if (type_lookups
== error_mark_node
)
1119 enforce_access (type
, val
);
1120 else if (! accessible_p (type
, val
))
1121 type_lookups
= tree_cons (type
, val
, type_lookups
);
1124 /* DECL is a declaration from a base class of TYPE, which was the
1125 class used to name DECL. Return non-zero if, in the current
1126 context, DECL is accessible. If TYPE is actually a BINFO node,
1127 then we can tell in what context the access is occurring by looking
1128 at the most derived class along the path indicated by BINFO. */
1131 accessible_p (type
, decl
)
1139 /* Non-zero if it's OK to access DECL if it has protected
1140 accessibility in TYPE. */
1141 int protected_ok
= 0;
1143 /* If we're not checking access, everything is accessible. */
1144 if (!flag_access_control
)
1147 /* If this declaration is in a block or namespace scope, there's no
1149 if (!TYPE_P (context_for_name_lookup (decl
)))
1155 type
= BINFO_TYPE (type
);
1158 binfo
= TYPE_BINFO (type
);
1160 /* [class.access.base]
1162 A member m is accessible when named in class N if
1164 --m as a member of N is public, or
1166 --m as a member of N is private, and the reference occurs in a
1167 member or friend of class N, or
1169 --m as a member of N is protected, and the reference occurs in a
1170 member or friend of class N, or in a member or friend of a
1171 class P derived from N, where m as a member of P is private or
1174 --there exists a base class B of N that is accessible at the point
1175 of reference, and m is accessible when named in class B.
1177 We walk the base class hierarchy, checking these conditions. */
1179 /* Figure out where the reference is occurring. Check to see if
1180 DECL is private or protected in this scope, since that will
1181 determine whether protected access in TYPE allowed. */
1182 if (current_class_type
)
1184 = protected_accessible_p (type
, decl
, current_class_type
,
1187 /* Now, loop through the classes of which we are a friend. */
1189 protected_ok
= friend_accessible_p (current_scope (),
1192 /* Standardize the binfo that access_in_type will use. We don't
1193 need to know what path was chosen from this point onwards. */
1194 binfo
= TYPE_BINFO (type
);
1196 /* Compute the accessibility of DECL in the class hierarchy
1197 dominated by type. */
1198 access_in_type (type
, decl
);
1199 /* Walk the hierarchy again, looking for a base class that allows
1201 t
= dfs_walk (binfo
, dfs_accessible_p
,
1202 dfs_accessible_queue_p
,
1203 protected_ok
? &protected_ok
: 0);
1204 /* Clear any mark bits. Note that we have to walk the whole tree
1205 here, since we have aborted the previous walk from some point
1206 deep in the tree. */
1207 dfs_walk (binfo
, dfs_unmark
, dfs_canonical_queue
, 0);
1208 assert_canonical_unmarked (binfo
);
1210 return t
!= NULL_TREE
;
1213 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1214 found as a base class and sub-object of the object denoted by
1215 BINFO. This routine relies upon binfos not being shared, except
1216 for binfos for virtual bases. */
1219 is_subobject_of_p (parent
, binfo
)
1225 /* We want to canonicalize for comparison purposes. But, when we
1226 iterate through basetypes later, we want the binfos from the
1227 original hierarchy. That's why we have to calculate BINFOS
1228 first, and then canonicalize. */
1229 binfos
= BINFO_BASETYPES (binfo
);
1230 parent
= canonical_binfo (parent
);
1231 binfo
= canonical_binfo (binfo
);
1233 if (parent
== binfo
)
1236 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1238 /* Process and/or queue base types. */
1239 for (i
= 0; i
< n_baselinks
; i
++)
1241 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1242 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo
)))
1243 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1244 class there's no way to descend into it. */
1247 if (is_subobject_of_p (parent
, base_binfo
))
1253 /* See if a one FIELD_DECL hides another. This routine is meant to
1254 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1255 binfos given are the binfos corresponding to the particular places
1256 the FIELD_DECLs are found. This routine relies upon binfos not
1257 being shared, except for virtual bases. */
1260 hides (hider_binfo
, hidee_binfo
)
1261 tree hider_binfo
, hidee_binfo
;
1263 /* hider hides hidee, if hider has hidee as a base class and
1264 the instance of hidee is a sub-object of hider. The first
1265 part is always true is the second part is true.
1267 When hider and hidee are the same (two ways to get to the exact
1268 same member) we consider either one as hiding the other. */
1269 return is_subobject_of_p (hidee_binfo
, hider_binfo
);
1272 /* Very similar to lookup_fnfields_1 but it ensures that at least one
1273 function was declared inside the class given by TYPE. It really should
1274 only return functions that match the given TYPE. */
1277 lookup_fnfields_here (type
, name
)
1280 int idx
= lookup_fnfields_1 (type
, name
);
1283 /* ctors and dtors are always only in the right class. */
1286 fndecls
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1289 if (TYPE_MAIN_VARIANT (DECL_CONTEXT (OVL_CURRENT (fndecls
)))
1290 == TYPE_MAIN_VARIANT (type
))
1292 fndecls
= OVL_CHAIN (fndecls
);
1297 struct lookup_field_info
{
1298 /* The type in which we're looking. */
1300 /* The name of the field for which we're looking. */
1302 /* If non-NULL, the current result of the lookup. */
1304 /* The path to RVAL. */
1306 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1309 /* If non-zero, we are looking for types, not data members. */
1311 /* If non-zero, RVAL was found by looking through a dependent base. */
1312 int from_dep_base_p
;
1313 /* If something went wrong, a message indicating what. */
1317 /* Returns non-zero if BINFO is not hidden by the value found by the
1318 lookup so far. If BINFO is hidden, then there's no need to look in
1319 it. DATA is really a struct lookup_field_info. Called from
1320 lookup_field via breadth_first_search. */
1323 lookup_field_queue_p (binfo
, data
)
1327 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1329 /* Don't look for constructors or destructors in base classes. */
1330 if (lfi
->name
== ctor_identifier
|| lfi
->name
== dtor_identifier
)
1333 /* If this base class is hidden by the best-known value so far, we
1334 don't need to look. */
1335 if (!lfi
->from_dep_base_p
&& lfi
->rval_binfo
1336 && hides (lfi
->rval_binfo
, binfo
))
1339 if (TREE_VIA_VIRTUAL (binfo
))
1340 return BINFO_FOR_VBASE (BINFO_TYPE (binfo
), lfi
->type
);
1345 /* Within the scope of a template class, you can refer to the to the
1346 current specialization with the name of the template itself. For
1349 template <typename T> struct S { S* sp; }
1351 Returns non-zero if DECL is such a declaration in a class TYPE. */
1354 template_self_reference_p (type
, decl
)
1358 return (CLASSTYPE_USE_TEMPLATE (type
)
1359 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
1360 && TREE_CODE (decl
) == TYPE_DECL
1361 && DECL_ARTIFICIAL (decl
)
1362 && DECL_NAME (decl
) == constructor_name (type
));
1365 /* DATA is really a struct lookup_field_info. Look for a field with
1366 the name indicated there in BINFO. If this function returns a
1367 non-NULL value it is the result of the lookup. Called from
1368 lookup_field via breadth_first_search. */
1371 lookup_field_r (binfo
, data
)
1375 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1376 tree type
= BINFO_TYPE (binfo
);
1377 tree nval
= NULL_TREE
;
1378 int from_dep_base_p
;
1380 /* First, look for a function. There can't be a function and a data
1381 member with the same name, and if there's a function and a type
1382 with the same name, the type is hidden by the function. */
1383 if (!lfi
->want_type
)
1385 int idx
= lookup_fnfields_here (type
, lfi
->name
);
1387 nval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1391 /* Look for a data member or type. */
1392 nval
= lookup_field_1 (type
, lfi
->name
);
1394 /* If there is no declaration with the indicated name in this type,
1395 then there's nothing to do. */
1399 /* If we're looking up a type (as with an elaborated type specifier)
1400 we ignore all non-types we find. */
1401 if (lfi
->want_type
&& TREE_CODE (nval
) != TYPE_DECL
)
1403 nval
= purpose_member (lfi
->name
, CLASSTYPE_TAGS (type
));
1405 nval
= TYPE_MAIN_DECL (TREE_VALUE (nval
));
1410 /* You must name a template base class with a template-id. */
1411 if (!same_type_p (type
, lfi
->type
)
1412 && template_self_reference_p (type
, nval
))
1415 from_dep_base_p
= dependent_base_p (binfo
);
1416 if (lfi
->from_dep_base_p
&& !from_dep_base_p
)
1418 /* If the new declaration is not found via a dependent base, and
1419 the old one was, then we must prefer the new one. We weren't
1420 really supposed to be able to find the old one, so we don't
1421 want to be affected by a specialization. Consider:
1423 struct B { typedef int I; };
1424 template <typename T> struct D1 : virtual public B {};
1425 template <typename T> struct D :
1426 public D1, virtual pubic B { I i; };
1428 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1429 D1 is specialized. */
1430 lfi
->from_dep_base_p
= 0;
1431 lfi
->rval
= NULL_TREE
;
1432 lfi
->rval_binfo
= NULL_TREE
;
1433 lfi
->ambiguous
= NULL_TREE
;
1436 else if (lfi
->rval_binfo
&& !lfi
->from_dep_base_p
&& from_dep_base_p
)
1437 /* Similarly, if the old declaration was not found via a dependent
1438 base, and the new one is, ignore the new one. */
1441 /* If the lookup already found a match, and the new value doesn't
1442 hide the old one, we might have an ambiguity. */
1443 if (lfi
->rval_binfo
&& !hides (binfo
, lfi
->rval_binfo
))
1445 if (nval
== lfi
->rval
&& SHARED_MEMBER_P (nval
))
1446 /* The two things are really the same. */
1448 else if (hides (lfi
->rval_binfo
, binfo
))
1449 /* The previous value hides the new one. */
1453 /* We have a real ambiguity. We keep a chain of all the
1455 if (!lfi
->ambiguous
&& lfi
->rval
)
1457 /* This is the first time we noticed an ambiguity. Add
1458 what we previously thought was a reasonable candidate
1460 lfi
->ambiguous
= tree_cons (NULL_TREE
, lfi
->rval
, NULL_TREE
);
1461 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1464 /* Add the new value. */
1465 lfi
->ambiguous
= tree_cons (NULL_TREE
, nval
, lfi
->ambiguous
);
1466 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1467 lfi
->errstr
= "request for member `%D' is ambiguous";
1472 /* If the thing we're looking for is a virtual base class, then
1473 we know we've got what we want at this point; there's no way
1474 to get an ambiguity. */
1475 if (VBASE_NAME_P (lfi
->name
))
1481 if (from_dep_base_p
&& TREE_CODE (nval
) != TYPE_DECL
1482 /* We need to return a member template class so we can
1483 define partial specializations. Is there a better
1485 && !DECL_CLASS_TEMPLATE_P (nval
))
1486 /* The thing we're looking for isn't a type, so the implicit
1487 typename extension doesn't apply, so we just pretend we
1488 didn't find anything. */
1492 lfi
->from_dep_base_p
= from_dep_base_p
;
1493 lfi
->rval_binfo
= binfo
;
1499 /* Look for a memer named NAME in an inheritance lattice dominated by
1500 XBASETYPE. PROTECT is 0 or two, we do not check access. If it is
1501 1, we enforce accessibility. If PROTECT is zero, then, for an
1502 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
1503 error message. If PROTECT is 2, we return a TREE_LIST whose
1504 TREE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
1505 ambiguous candidates.
1507 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1508 TYPE_DECL can be found return NULL_TREE. */
1511 lookup_member (xbasetype
, name
, protect
, want_type
)
1512 register tree xbasetype
, name
;
1513 int protect
, want_type
;
1515 tree rval
, rval_binfo
= NULL_TREE
;
1516 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1517 struct lookup_field_info lfi
;
1519 /* rval_binfo is the binfo associated with the found member, note,
1520 this can be set with useful information, even when rval is not
1521 set, because it must deal with ALL members, not just non-function
1522 members. It is used for ambiguity checking and the hidden
1523 checks. Whereas rval is only set if a proper (not hidden)
1524 non-function member is found. */
1526 const char *errstr
= 0;
1528 if (xbasetype
== current_class_type
&& TYPE_BEING_DEFINED (xbasetype
)
1529 && IDENTIFIER_CLASS_VALUE (name
))
1531 tree field
= IDENTIFIER_CLASS_VALUE (name
);
1532 if (TREE_CODE (field
) != FUNCTION_DECL
1533 && ! (want_type
&& TREE_CODE (field
) != TYPE_DECL
))
1534 /* We're in the scope of this class, and the value has already
1535 been looked up. Just return the cached value. */
1539 if (TREE_CODE (xbasetype
) == TREE_VEC
)
1541 type
= BINFO_TYPE (xbasetype
);
1542 basetype_path
= xbasetype
;
1544 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype
)))
1547 basetype_path
= TYPE_BINFO (type
);
1548 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
,
1552 my_friendly_abort (97);
1554 complete_type (type
);
1556 #ifdef GATHER_STATISTICS
1557 n_calls_lookup_field
++;
1558 #endif /* GATHER_STATISTICS */
1560 bzero ((PTR
) &lfi
, sizeof (lfi
));
1563 lfi
.want_type
= want_type
;
1564 bfs_walk (basetype_path
, &lookup_field_r
, &lookup_field_queue_p
, &lfi
);
1566 rval_binfo
= lfi
.rval_binfo
;
1568 type
= BINFO_TYPE (rval_binfo
);
1569 errstr
= lfi
.errstr
;
1571 /* If we are not interested in ambiguities, don't report them;
1572 just return NULL_TREE. */
1573 if (!protect
&& lfi
.ambiguous
)
1579 return lfi
.ambiguous
;
1586 In the case of overloaded function names, access control is
1587 applied to the function selected by overloaded resolution. */
1588 if (rval
&& protect
&& !is_overloaded_fn (rval
)
1589 && !enforce_access (xbasetype
, rval
))
1590 return error_mark_node
;
1592 if (errstr
&& protect
)
1594 cp_error (errstr
, name
, type
);
1596 print_candidates (lfi
.ambiguous
);
1597 rval
= error_mark_node
;
1600 /* If the thing we found was found via the implicit typename
1601 extension, build the typename type. */
1602 if (rval
&& lfi
.from_dep_base_p
&& !DECL_CLASS_TEMPLATE_P (rval
))
1603 rval
= TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path
),
1607 if (rval
&& is_overloaded_fn (rval
))
1609 rval
= tree_cons (basetype_path
, rval
, NULL_TREE
);
1610 SET_BASELINK_P (rval
);
1616 /* Like lookup_member, except that if we find a function member we
1617 return NULL_TREE. */
1620 lookup_field (xbasetype
, name
, protect
, want_type
)
1621 register tree xbasetype
, name
;
1622 int protect
, want_type
;
1624 tree rval
= lookup_member (xbasetype
, name
, protect
, want_type
);
1626 /* Ignore functions. */
1627 if (rval
&& TREE_CODE (rval
) == TREE_LIST
)
1633 /* Like lookup_member, except that if we find a non-function member we
1634 return NULL_TREE. */
1637 lookup_fnfields (xbasetype
, name
, protect
)
1638 register tree xbasetype
, name
;
1641 tree rval
= lookup_member (xbasetype
, name
, protect
, /*want_type=*/0);
1643 /* Ignore non-functions. */
1644 if (rval
&& TREE_CODE (rval
) != TREE_LIST
)
1650 /* TYPE is a class type. Return the index of the fields within
1651 the method vector with name NAME, or -1 is no such field exists. */
1654 lookup_fnfields_1 (type
, name
)
1658 = CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
1660 if (method_vec
!= 0)
1663 register tree
*methods
= &TREE_VEC_ELT (method_vec
, 0);
1664 int len
= TREE_VEC_LENGTH (method_vec
);
1667 #ifdef GATHER_STATISTICS
1668 n_calls_lookup_fnfields_1
++;
1669 #endif /* GATHER_STATISTICS */
1671 /* Constructors are first... */
1672 if (name
== ctor_identifier
)
1673 return methods
[0] ? 0 : -1;
1675 /* and destructors are second. */
1676 if (name
== dtor_identifier
)
1677 return methods
[1] ? 1 : -1;
1679 for (i
= 2; i
< len
&& methods
[i
]; ++i
)
1681 #ifdef GATHER_STATISTICS
1682 n_outer_fields_searched
++;
1683 #endif /* GATHER_STATISTICS */
1685 tmp
= OVL_CURRENT (methods
[i
]);
1686 if (DECL_NAME (tmp
) == name
)
1689 /* If the type is complete and we're past the conversion ops,
1690 switch to binary search. */
1691 if (! DECL_CONV_FN_P (tmp
)
1692 && TYPE_SIZE (type
))
1694 int lo
= i
+ 1, hi
= len
;
1700 #ifdef GATHER_STATISTICS
1701 n_outer_fields_searched
++;
1702 #endif /* GATHER_STATISTICS */
1704 tmp
= DECL_NAME (OVL_CURRENT (methods
[i
]));
1708 else if (tmp
< name
)
1717 /* If we didn't find it, it might have been a template
1718 conversion operator. (Note that we don't look for this case
1719 above so that we will always find specializations first.) */
1720 if (IDENTIFIER_TYPENAME_P (name
))
1722 for (i
= 2; i
< len
&& methods
[i
]; ++i
)
1724 tmp
= OVL_CURRENT (methods
[i
]);
1725 if (! DECL_CONV_FN_P (tmp
))
1727 /* Since all conversion operators come first, we know
1728 there is no such operator. */
1731 else if (TREE_CODE (tmp
) == TEMPLATE_DECL
)
1740 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1741 type in the hierarchy, in a breadth-first preorder traversal. .
1742 If it ever returns a non-NULL value, that value is immediately
1743 returned and the walk is terminated. At each node FN, is passed a
1744 BINFO indicating the path from the curently visited base-class to
1745 TYPE. The TREE_CHAINs of the BINFOs may be used for scratch space;
1746 they are otherwise unused. Before each base-class is walked QFN is
1747 called. If the value returned is non-zero, the base-class is
1748 walked; otherwise it is not. If QFN is NULL, it is treated as a
1749 function which always returns 1. Both FN and QFN are passed the
1750 DATA whenever they are called. */
1753 bfs_walk (binfo
, fn
, qfn
, data
)
1755 tree (*fn
) PARAMS ((tree
, void *));
1756 tree (*qfn
) PARAMS ((tree
, void *));
1761 tree rval
= NULL_TREE
;
1762 /* An array of the base classes of BINFO. These will be built up in
1763 breadth-first order, except where QFN prunes the search. */
1764 varray_type bfs_bases
;
1766 /* Start with enough room for ten base classes. That will be enough
1767 for most hierarchies. */
1768 VARRAY_TREE_INIT (bfs_bases
, 10, "search_stack");
1770 /* Put the first type into the stack. */
1771 VARRAY_TREE (bfs_bases
, 0) = binfo
;
1774 for (head
= 0; head
< tail
; ++head
)
1780 /* Pull the next type out of the queue. */
1781 binfo
= VARRAY_TREE (bfs_bases
, head
);
1783 /* If this is the one we're looking for, we're done. */
1784 rval
= (*fn
) (binfo
, data
);
1788 /* Queue up the base types. */
1789 binfos
= BINFO_BASETYPES (binfo
);
1790 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1791 for (i
= 0; i
< n_baselinks
; i
++)
1793 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1796 base_binfo
= (*qfn
) (base_binfo
, data
);
1800 if (tail
== VARRAY_SIZE (bfs_bases
))
1801 VARRAY_GROW (bfs_bases
, 2 * VARRAY_SIZE (bfs_bases
));
1802 VARRAY_TREE (bfs_bases
, tail
) = base_binfo
;
1809 VARRAY_FREE (bfs_bases
);
1814 /* Exactly like bfs_walk, except that a depth-first traversal is
1815 performed, and PREFN is called in preorder, while POSTFN is called
1819 dfs_walk_real (binfo
, prefn
, postfn
, qfn
, data
)
1821 tree (*prefn
) PARAMS ((tree
, void *));
1822 tree (*postfn
) PARAMS ((tree
, void *));
1823 tree (*qfn
) PARAMS ((tree
, void *));
1829 tree rval
= NULL_TREE
;
1831 /* Call the pre-order walking function. */
1834 rval
= (*prefn
) (binfo
, data
);
1839 /* Process the basetypes. */
1840 binfos
= BINFO_BASETYPES (binfo
);
1841 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1842 for (i
= 0; i
< n_baselinks
; i
++)
1844 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1847 base_binfo
= (*qfn
) (base_binfo
, data
);
1851 rval
= dfs_walk_real (base_binfo
, prefn
, postfn
, qfn
, data
);
1857 /* Call the post-order walking function. */
1859 rval
= (*postfn
) (binfo
, data
);
1864 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1868 dfs_walk (binfo
, fn
, qfn
, data
)
1870 tree (*fn
) PARAMS ((tree
, void *));
1871 tree (*qfn
) PARAMS ((tree
, void *));
1874 return dfs_walk_real (binfo
, 0, fn
, qfn
, data
);
1879 /* The name of the function we are looking for. */
1881 /* The overloaded functions we have found. */
1885 /* Called from get_virtuals_named_this via bfs_walk. */
1888 get_virtuals_named_this_r (binfo
, data
)
1892 struct gvnt_info
*gvnti
= (struct gvnt_info
*) data
;
1893 tree type
= BINFO_TYPE (binfo
);
1896 idx
= lookup_fnfields_here (BINFO_TYPE (binfo
), gvnti
->name
);
1900 TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
),
1906 /* Return the virtual functions with the indicated NAME in the type
1907 indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE
1908 indicates the base class from which the TREE_VALUE (an OVERLOAD or
1909 just a FUNCTION_DECL) originated. */
1912 get_virtuals_named_this (binfo
, name
)
1916 struct gvnt_info gvnti
;
1920 gvnti
.fields
= NULL_TREE
;
1922 bfs_walk (binfo
, get_virtuals_named_this_r
, 0, &gvnti
);
1924 /* Get to the function decls, and return the first virtual function
1925 with this name, if there is one. */
1926 for (fields
= gvnti
.fields
; fields
; fields
= next_baselink (fields
))
1930 for (fndecl
= TREE_VALUE (fields
); fndecl
; fndecl
= OVL_NEXT (fndecl
))
1931 if (DECL_VINDEX (OVL_CURRENT (fndecl
)))
1938 get_virtual_destructor (binfo
, data
)
1940 void *data ATTRIBUTE_UNUSED
;
1942 tree type
= BINFO_TYPE (binfo
);
1943 if (TYPE_HAS_DESTRUCTOR (type
)
1944 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1)))
1945 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1);
1950 tree_has_any_destructor_p (binfo
, data
)
1952 void *data ATTRIBUTE_UNUSED
;
1954 tree type
= BINFO_TYPE (binfo
);
1955 return TYPE_NEEDS_DESTRUCTOR (type
) ? binfo
: NULL_TREE
;
1958 /* Returns > 0 if a function with type DRETTYPE overriding a function
1959 with type BRETTYPE is covariant, as defined in [class.virtual].
1961 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1962 adjustment), or -1 if pedantically invalid covariance. */
1965 covariant_return_p (brettype
, drettype
)
1966 tree brettype
, drettype
;
1970 if (TREE_CODE (brettype
) == FUNCTION_DECL
1971 || TREE_CODE (brettype
) == THUNK_DECL
)
1973 brettype
= TREE_TYPE (TREE_TYPE (brettype
));
1974 drettype
= TREE_TYPE (TREE_TYPE (drettype
));
1976 else if (TREE_CODE (brettype
) == METHOD_TYPE
)
1978 brettype
= TREE_TYPE (brettype
);
1979 drettype
= TREE_TYPE (drettype
);
1982 if (same_type_p (brettype
, drettype
))
1985 if (! (TREE_CODE (brettype
) == TREE_CODE (drettype
)
1986 && (TREE_CODE (brettype
) == POINTER_TYPE
1987 || TREE_CODE (brettype
) == REFERENCE_TYPE
)
1988 && TYPE_QUALS (brettype
) == TYPE_QUALS (drettype
)))
1991 if (! can_convert (brettype
, drettype
))
1994 brettype
= TREE_TYPE (brettype
);
1995 drettype
= TREE_TYPE (drettype
);
1997 /* If not pedantic, allow any standard pointer conversion. */
1998 if (! IS_AGGR_TYPE (drettype
) || ! IS_AGGR_TYPE (brettype
))
2001 binfo
= get_binfo (brettype
, drettype
, 1);
2003 /* If we get an error_mark_node from get_binfo, it already complained,
2004 so let's just succeed. */
2005 if (binfo
== error_mark_node
)
2008 if (! BINFO_OFFSET_ZEROP (binfo
) || TREE_VIA_VIRTUAL (binfo
))
2013 /* Check that virtual overrider OVERRIDER is acceptable for base function
2014 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2017 check_final_overrider (overrider
, basefn
)
2018 tree overrider
, basefn
;
2020 tree over_type
= TREE_TYPE (overrider
);
2021 tree base_type
= TREE_TYPE (basefn
);
2022 tree over_return
= TREE_TYPE (over_type
);
2023 tree base_return
= TREE_TYPE (base_type
);
2024 tree over_throw
= TYPE_RAISES_EXCEPTIONS (over_type
);
2025 tree base_throw
= TYPE_RAISES_EXCEPTIONS (base_type
);
2028 if (same_type_p (base_return
, over_return
))
2030 else if ((i
= covariant_return_p (base_return
, over_return
)))
2033 sorry ("adjusting pointers for covariant returns");
2035 if (pedantic
&& i
== -1)
2037 cp_pedwarn_at ("invalid covariant return type for `virtual %#D'", overrider
);
2038 cp_pedwarn_at (" overriding `virtual %#D' (must be pointer or reference to class)", basefn
);
2041 else if (IS_AGGR_TYPE_2 (base_return
, over_return
)
2042 && same_or_base_type_p (base_return
, over_return
))
2044 cp_error_at ("invalid covariant return type for `virtual %#D'", overrider
);
2045 cp_error_at (" overriding `virtual %#D' (must use pointer or reference)", basefn
);
2048 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
)) == NULL_TREE
)
2050 cp_error_at ("conflicting return type specified for `virtual %#D'", overrider
);
2051 cp_error_at (" overriding `virtual %#D'", basefn
);
2052 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
),
2053 DECL_CONTEXT (overrider
));
2057 /* Check throw specifier is subset. */
2058 /* XXX At the moment, punt on an overriding artificial function. We
2059 don't generate its exception specifier, so can't check it properly. */
2060 if (! DECL_ARTIFICIAL (overrider
)
2061 && !comp_except_specs (base_throw
, over_throw
, 0))
2063 cp_error_at ("looser throw specifier for `virtual %#F'", overrider
);
2064 cp_error_at (" overriding `virtual %#F'", basefn
);
2070 /* Given a class type TYPE, and a function decl FNDECL, look for a
2071 virtual function in TYPE's hierarchy which FNDECL could match as a
2072 virtual function. It doesn't matter which one we find.
2074 DTORP is nonzero if we are looking for a destructor. Destructors
2075 need special treatment because they do not match by name. */
2078 get_matching_virtual (binfo
, fndecl
, dtorp
)
2082 tree tmp
= NULL_TREE
;
2084 if (TREE_CODE (fndecl
) == TEMPLATE_DECL
)
2085 /* In [temp.mem] we have:
2087 A specialization of a member function template does not
2088 override a virtual function from a base class. */
2091 /* Breadth first search routines start searching basetypes
2092 of TYPE, so we must perform first ply of search here. */
2094 return bfs_walk (binfo
, get_virtual_destructor
,
2095 tree_has_any_destructor_p
, 0);
2098 tree drettype
, dtypes
, btypes
, instptr_type
;
2099 tree baselink
, best
= NULL_TREE
;
2100 tree declarator
= DECL_NAME (fndecl
);
2101 if (IDENTIFIER_VIRTUAL_P (declarator
) == 0)
2104 baselink
= get_virtuals_named_this (binfo
, declarator
);
2105 if (baselink
== NULL_TREE
)
2108 drettype
= TREE_TYPE (TREE_TYPE (fndecl
));
2109 dtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2110 if (DECL_STATIC_FUNCTION_P (fndecl
))
2111 instptr_type
= NULL_TREE
;
2113 instptr_type
= TREE_TYPE (TREE_VALUE (dtypes
));
2115 for (; baselink
; baselink
= next_baselink (baselink
))
2118 for (tmps
= TREE_VALUE (baselink
); tmps
; tmps
= OVL_NEXT (tmps
))
2120 tmp
= OVL_CURRENT (tmps
);
2121 if (! DECL_VINDEX (tmp
))
2124 btypes
= TYPE_ARG_TYPES (TREE_TYPE (tmp
));
2125 if (instptr_type
== NULL_TREE
)
2127 if (compparms (TREE_CHAIN (btypes
), dtypes
))
2128 /* Caller knows to give error in this case. */
2133 if (/* The first parameter is the `this' parameter,
2134 which has POINTER_TYPE, and we can therefore
2135 safely use TYPE_QUALS, rather than
2137 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes
)))
2138 == TYPE_QUALS (instptr_type
))
2139 && compparms (TREE_CHAIN (btypes
), TREE_CHAIN (dtypes
)))
2141 check_final_overrider (fndecl
, tmp
);
2143 /* FNDECL overrides this function. We continue to
2144 check all the other functions in order to catch
2145 errors; it might be that in some other baseclass
2146 a virtual function was declared with the same
2147 parameter types, but a different return type. */
2157 /* A queue function for dfs_walk that skips any nonprimary virtual
2158 bases and any already marked bases. */
2161 dfs_skip_nonprimary_vbases_unmarkedp (binfo
, data
)
2163 void *data ATTRIBUTE_UNUSED
;
2165 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
2166 /* This is a non-primary virtual base. SKip it. */
2169 return unmarkedp (binfo
, NULL
);
2172 /* A queue function for dfs_walk that skips any nonprimary virtual
2173 bases and any unmarked bases. */
2176 dfs_skip_nonprimary_vbases_markedp (binfo
, data
)
2178 void *data ATTRIBUTE_UNUSED
;
2180 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
2181 /* This is a non-primary virtual base. SKip it. */
2184 return markedp (binfo
, NULL
);
2187 /* Called via dfs_walk from mark_primary_bases. */
2190 dfs_mark_primary_bases (binfo
, data
)
2197 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo
)))
2200 i
= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2201 base_binfo
= BINFO_BASETYPE (binfo
, i
);
2203 if (!TREE_VIA_VIRTUAL (base_binfo
))
2204 /* Non-virtual base classes are easy. */
2205 BINFO_PRIMARY_MARKED_P (base_binfo
) = 1;
2211 = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo
), (tree
) data
);
2213 /* If this virtual base is not already primary somewhere else in
2214 the hiearchy, then we'll be using this copy. */
2215 if (!BINFO_VBASE_PRIMARY_P (shared_binfo
)
2216 && !BINFO_VBASE_MARKED (shared_binfo
))
2218 BINFO_VBASE_PRIMARY_P (shared_binfo
) = 1;
2219 BINFO_PRIMARY_MARKED_P (base_binfo
) = 1;
2226 /* Set BINFO_PRIMARY_MARKED_P for all binfos in the hierarchy
2227 dominated by BINFO that are primary bases. */
2230 mark_primary_bases (type
)
2235 /* Mark the TYPE_BINFO hierarchy. We need to mark primary bases in
2236 pre-order to deal with primary virtual bases. (The virtual base
2237 would be skipped if it were not marked as primary, and that
2238 requires getting to dfs_mark_primary_bases before
2239 dfs_skip_nonprimary_vbases_unmarkedp has a chance to skip the
2241 dfs_walk_real (TYPE_BINFO (type
), dfs_mark_primary_bases
, NULL
,
2242 dfs_skip_nonprimary_vbases_unmarkedp
, type
);
2244 /* Now go through the virtual base classes. Any that are not
2245 already primary will need to be allocated in TYPE, and so we need
2246 to mark their primary bases. */
2247 for (vbase
= CLASSTYPE_VBASECLASSES (type
);
2249 vbase
= TREE_CHAIN (vbase
))
2251 if (BINFO_VBASE_PRIMARY_P (vbase
))
2252 /* This virtual base was already included in the hierarchy, so
2253 there's nothing to do here. */
2256 /* Temporarily pretend that VBASE is primary so that its bases
2257 will be walked; this is the real copy of VBASE. */
2258 BINFO_PRIMARY_MARKED_P (vbase
) = 1;
2260 /* Now, walk its bases. */
2261 dfs_walk (vbase
, dfs_mark_primary_bases
,
2262 dfs_skip_nonprimary_vbases_unmarkedp
, type
);
2264 /* VBASE wasn't really primary. */
2265 BINFO_PRIMARY_MARKED_P (vbase
) = 0;
2266 /* And we don't want to allow it to *become* primary if it is a
2267 base of some subsequent base class. */
2268 SET_BINFO_VBASE_MARKED (vbase
);
2271 /* Clear the VBASE_MARKED bits we set above. */
2272 for (vbase
= CLASSTYPE_VBASECLASSES (type
);
2274 vbase
= TREE_CHAIN (vbase
))
2275 CLEAR_BINFO_VBASE_MARKED (vbase
);
2278 /* If BINFO is a non-primary virtual baseclass (in the hierarchy
2279 dominated by TYPE), and no primary copy appears anywhere in the
2280 hierarchy, return the shared copy. If a primary copy appears
2281 elsewhere, return NULL_TREE. Otherwise, return BINFO itself; it is
2282 either a non-virtual base or a primary virtual base. */
2285 get_shared_vbase_if_not_primary (binfo
, data
)
2289 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
2291 tree type
= (tree
) data
;
2293 if (TREE_CODE (type
) == TREE_LIST
)
2294 type
= TREE_PURPOSE (type
);
2296 /* This is a non-primary virtual base. If there is no primary
2297 version, get the shared version. */
2298 binfo
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), type
);
2299 if (BINFO_VBASE_PRIMARY_P (binfo
))
2306 /* A queue function to use with dfs_walk that prevents travel into any
2307 nonprimary virtual base, or its baseclasses. DATA should be the
2308 type of the complete object, or a TREE_LIST whose TREE_PURPOSE is
2309 the type of the complete object. By using this function as a queue
2310 function, you will walk over exactly those BINFOs that actually
2311 exist in the complete object, including those for virtual base
2312 classes. If you SET_BINFO_MARKED for each binfo you process, you
2313 are further guaranteed that you will walk into each virtual base
2314 class exactly once. */
2317 dfs_unmarked_real_bases_queue_p (binfo
, data
)
2321 binfo
= get_shared_vbase_if_not_primary (binfo
, data
);
2322 return binfo
? unmarkedp (binfo
, NULL
) : NULL_TREE
;
2325 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2326 that are marked, rather than unmarked. */
2329 dfs_marked_real_bases_queue_p (binfo
, data
)
2333 binfo
= get_shared_vbase_if_not_primary (binfo
, data
);
2334 return binfo
? markedp (binfo
, NULL
) : NULL_TREE
;
2337 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2338 that are not BINFO_VTABLE_PATH_MARKED. */
2341 dfs_vtable_path_unmarked_real_bases_queue_p (binfo
, data
)
2345 binfo
= get_shared_vbase_if_not_primary (binfo
, data
);
2346 return binfo
? unmarked_vtable_pathp (binfo
, NULL
): NULL_TREE
;
2349 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2350 that are BINFO_VTABLE_PATH_MARKED. */
2353 dfs_vtable_path_marked_real_bases_queue_p (binfo
, data
)
2357 binfo
= get_shared_vbase_if_not_primary (binfo
, data
);
2358 return binfo
? marked_vtable_pathp (binfo
, NULL
): NULL_TREE
;
2361 /* A queue function that skips all virtual bases (and their
2365 dfs_skip_vbases (binfo
, data
)
2367 void *data ATTRIBUTE_UNUSED
;
2369 if (TREE_VIA_VIRTUAL (binfo
))
2375 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2378 dfs_get_pure_virtuals (binfo
, data
)
2382 tree type
= (tree
) data
;
2384 /* We're not interested in primary base classes; the derived class
2385 of which they are a primary base will contain the information we
2387 if (!BINFO_PRIMARY_MARKED_P (binfo
))
2391 for (virtuals
= skip_rtti_stuff (binfo
,
2395 virtuals
= TREE_CHAIN (virtuals
))
2396 if (DECL_PURE_VIRTUAL_P (TREE_VALUE (virtuals
)))
2397 CLASSTYPE_PURE_VIRTUALS (type
)
2398 = tree_cons (NULL_TREE
, TREE_VALUE (virtuals
),
2399 CLASSTYPE_PURE_VIRTUALS (type
));
2402 SET_BINFO_MARKED (binfo
);
2407 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2410 get_pure_virtuals (type
)
2415 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2416 is going to be overridden. */
2417 CLASSTYPE_PURE_VIRTUALS (type
) = NULL_TREE
;
2418 /* Now, run through all the bases which are not primary bases, and
2419 collect the pure virtual functions. We look at the vtable in
2420 each class to determine what pure virtual functions are present.
2421 (A primary base is not interesting because the derived class of
2422 which it is a primary base will contain vtable entries for the
2423 pure virtuals in the base class. */
2424 dfs_walk (TYPE_BINFO (type
), dfs_get_pure_virtuals
,
2425 dfs_unmarked_real_bases_queue_p
, type
);
2426 dfs_walk (TYPE_BINFO (type
), dfs_unmark
,
2427 dfs_marked_real_bases_queue_p
, type
);
2429 /* Put the pure virtuals in dfs order. */
2430 CLASSTYPE_PURE_VIRTUALS (type
) = nreverse (CLASSTYPE_PURE_VIRTUALS (type
));
2432 for (vbases
= CLASSTYPE_VBASECLASSES (type
);
2434 vbases
= TREE_CHAIN (vbases
))
2438 for (virtuals
= skip_rtti_stuff (vbases
, BINFO_TYPE (vbases
), NULL
);
2440 virtuals
= TREE_CHAIN (virtuals
))
2442 tree base_fndecl
= TREE_VALUE (virtuals
);
2443 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl
))
2444 cp_error ("`%#D' needs a final overrider", base_fndecl
);
2450 next_baselink (baselink
)
2453 tree tmp
= TREE_TYPE (baselink
);
2454 baselink
= TREE_CHAIN (baselink
);
2457 /* @@ does not yet add previous base types. */
2458 baselink
= tree_cons (TREE_PURPOSE (tmp
), TREE_VALUE (tmp
),
2460 TREE_TYPE (baselink
) = TREE_TYPE (tmp
);
2461 tmp
= TREE_CHAIN (tmp
);
2466 /* DEPTH-FIRST SEARCH ROUTINES. */
2468 /* This routine converts a pointer to be a pointer of an immediate
2469 base class. The normal convert_pointer_to routine would diagnose
2470 the conversion as ambiguous, under MI code that has the base class
2471 as an ambiguous base class. */
2474 convert_pointer_to_single_level (to_type
, expr
)
2478 tree binfo_of_derived
;
2481 derived
= TREE_TYPE (TREE_TYPE (expr
));
2482 binfo_of_derived
= TYPE_BINFO (derived
);
2483 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived
) == NULL_TREE
,
2485 for (i
= CLASSTYPE_N_BASECLASSES (derived
) - 1; i
>= 0; --i
)
2487 tree binfo
= BINFO_BASETYPE (binfo_of_derived
, i
);
2488 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo
) == binfo_of_derived
,
2490 if (same_type_p (BINFO_TYPE (binfo
), to_type
))
2491 return build_vbase_path (PLUS_EXPR
,
2492 build_pointer_type (to_type
),
2496 my_friendly_abort (19990607);
2503 markedp (binfo
, data
)
2505 void *data ATTRIBUTE_UNUSED
;
2507 return BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2511 unmarkedp (binfo
, data
)
2513 void *data ATTRIBUTE_UNUSED
;
2515 return !BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2519 marked_vtable_pathp (binfo
, data
)
2521 void *data ATTRIBUTE_UNUSED
;
2523 return BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2527 unmarked_vtable_pathp (binfo
, data
)
2529 void *data ATTRIBUTE_UNUSED
;
2531 return !BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2535 marked_new_vtablep (binfo
, data
)
2537 void *data ATTRIBUTE_UNUSED
;
2539 return BINFO_NEW_VTABLE_MARKED (binfo
) ? binfo
: NULL_TREE
;
2543 unmarked_new_vtablep (binfo
, data
)
2545 void *data ATTRIBUTE_UNUSED
;
2547 return !BINFO_NEW_VTABLE_MARKED (binfo
) ? binfo
: NULL_TREE
;
2551 marked_pushdecls_p (binfo
, data
)
2553 void *data ATTRIBUTE_UNUSED
;
2555 return (CLASS_TYPE_P (BINFO_TYPE (binfo
))
2556 && BINFO_PUSHDECLS_MARKED (binfo
)) ? binfo
: NULL_TREE
;
2560 unmarked_pushdecls_p (binfo
, data
)
2562 void *data ATTRIBUTE_UNUSED
;
2564 return (CLASS_TYPE_P (BINFO_TYPE (binfo
))
2565 && !BINFO_PUSHDECLS_MARKED (binfo
)) ? binfo
: NULL_TREE
;
2569 static int dfs_search_slot_nonempty_p (binfo
) tree binfo
;
2570 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) != 0; }
2573 dfs_debug_unmarkedp (binfo
, data
)
2575 void *data ATTRIBUTE_UNUSED
;
2577 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo
))
2578 ? binfo
: NULL_TREE
);
2582 /* The worker functions for `dfs_walk'. These do not need to
2583 test anything (vis a vis marking) if they are paired with
2584 a predicate function (above). */
2588 dfs_mark (binfo
) tree binfo
;
2589 { SET_BINFO_MARKED (binfo
); }
2593 dfs_unmark (binfo
, data
)
2595 void *data ATTRIBUTE_UNUSED
;
2597 CLEAR_BINFO_MARKED (binfo
);
2601 /* Clear both BINFO_MARKED and BINFO_VBASE_MARKED. */
2604 dfs_vbase_unmark (binfo
, data
)
2606 void *data ATTRIBUTE_UNUSED
;
2608 CLEAR_BINFO_VBASE_MARKED (binfo
);
2609 return dfs_unmark (binfo
, data
);
2612 /* Clear BINFO_VTABLE_PATH_MARKED. */
2615 dfs_vtable_path_unmark (binfo
, data
)
2617 void *data ATTRIBUTE_UNUSED
;
2619 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2625 dfs_mark_vtable_path (binfo
) tree binfo
;
2626 { SET_BINFO_VTABLE_PATH_MARKED (binfo
); }
2629 dfs_mark_new_vtable (binfo
) tree binfo
;
2630 { SET_BINFO_NEW_VTABLE_MARKED (binfo
); }
2633 dfs_unmark_new_vtable (binfo
) tree binfo
;
2634 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
); }
2637 dfs_clear_search_slot (binfo
) tree binfo
;
2638 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) = 0; }
2640 /* Keep this code around in case we later want to control debug info
2641 based on whether a type is "used". Currently, we only suppress debug
2642 info if we can emit it with the vtable. jason 1999-11-11) */
2644 dfs_debug_mark (binfo
, data
)
2646 void *data ATTRIBUTE_UNUSED
;
2648 tree t
= BINFO_TYPE (binfo
);
2650 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2652 /* If interface info is known, either we've already emitted the debug
2653 info or we don't need to. */
2654 if (CLASSTYPE_INTERFACE_KNOWN (t
))
2657 /* If the class has virtual functions, we'll emit the debug info
2659 if (TYPE_POLYMORPHIC_P (t
))
2662 /* We cannot rely on some alien method to solve our problems,
2663 so we must write out the debug info ourselves. */
2664 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = 0;
2665 rest_of_type_compilation (t
, toplevel_bindings_p ());
2678 /* Attach to the type of the virtual base class, the pointer to the
2679 virtual base class. */
2682 dfs_find_vbases (binfo
, data
)
2686 struct vbase_info
*vi
= (struct vbase_info
*) data
;
2687 tree binfos
= BINFO_BASETYPES (binfo
);
2688 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2690 for (i
= n_baselinks
-1; i
>= 0; i
--)
2692 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2694 if (TREE_VIA_VIRTUAL (base_binfo
)
2695 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo
)) == 0)
2697 tree vbase
= BINFO_TYPE (base_binfo
);
2698 tree binfo
= binfo_member (vbase
, vi
->vbase_types
);
2700 CLASSTYPE_SEARCH_SLOT (vbase
)
2701 = build (PLUS_EXPR
, build_pointer_type (vbase
),
2702 vi
->decl_ptr
, BINFO_OFFSET (binfo
));
2705 SET_BINFO_VTABLE_PATH_MARKED (binfo
);
2706 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
2712 dfs_init_vbase_pointers (binfo
, data
)
2716 struct vbase_info
*vi
= (struct vbase_info
*) data
;
2717 tree type
= BINFO_TYPE (binfo
);
2719 tree this_vbase_ptr
;
2721 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2723 if (BINFO_INHERITANCE_CHAIN (binfo
))
2725 this_vbase_ptr
= TREE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo
));
2726 if (TREE_VIA_VIRTUAL (binfo
))
2727 this_vbase_ptr
= CLASSTYPE_SEARCH_SLOT (type
);
2729 this_vbase_ptr
= convert_pointer_to_single_level (type
,
2731 TREE_CHAIN (binfo
) = this_vbase_ptr
;
2734 this_vbase_ptr
= TREE_CHAIN (binfo
);
2736 /* We're going to iterate through all the pointers to virtual
2737 base-classes. They come at the beginning of the class. */
2738 fields
= TYPE_FIELDS (type
);
2739 if (fields
== TYPE_VFIELD (type
))
2740 /* If the first field is the vtbl pointer (as happens in the new
2742 fields
= TREE_CHAIN (fields
);
2744 if (fields
== NULL_TREE
2745 || DECL_NAME (fields
) == NULL_TREE
2746 || ! VBASE_NAME_P (DECL_NAME (fields
)))
2749 if (build_pointer_type (type
)
2750 != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr
)))
2751 my_friendly_abort (125);
2753 while (fields
&& DECL_NAME (fields
) && VBASE_NAME_P (DECL_NAME (fields
)))
2755 tree ref
= build (COMPONENT_REF
, TREE_TYPE (fields
),
2756 build_indirect_ref (this_vbase_ptr
, NULL_PTR
), fields
);
2757 tree init
= CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields
)));
2758 vi
->inits
= tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields
)),
2760 build_modify_expr (ref
, NOP_EXPR
, init
),
2762 fields
= TREE_CHAIN (fields
);
2768 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2769 times, just NEW_VTABLE, but optimizer should make both with equal
2770 efficiency (though it does not currently). */
2773 dfs_clear_vbase_slots (binfo
, data
)
2775 void *data ATTRIBUTE_UNUSED
;
2777 tree type
= BINFO_TYPE (binfo
);
2778 CLASSTYPE_SEARCH_SLOT (type
) = 0;
2779 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2780 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2785 init_vbase_pointers (type
, decl_ptr
)
2789 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
2791 struct vbase_info vi
;
2792 int old_flag
= flag_this_is_variable
;
2793 tree binfo
= TYPE_BINFO (type
);
2794 flag_this_is_variable
= -2;
2796 /* Find all the virtual base classes, marking them for later
2798 vi
.decl_ptr
= decl_ptr
;
2799 vi
.vbase_types
= CLASSTYPE_VBASECLASSES (type
);
2800 vi
.inits
= NULL_TREE
;
2802 dfs_walk (binfo
, dfs_find_vbases
, unmarked_vtable_pathp
, &vi
);
2804 /* Build up a list of the initializers. */
2805 TREE_CHAIN (binfo
) = decl_ptr
;
2806 dfs_walk_real (binfo
,
2807 dfs_init_vbase_pointers
, 0,
2808 marked_vtable_pathp
,
2811 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
, 0);
2812 flag_this_is_variable
= old_flag
;
2818 /* get the virtual context (the vbase that directly contains the
2819 DECL_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2820 or NULL_TREE if there is none.
2822 FNDECL must come from a virtual table from a virtual base to ensure
2823 that there is only one possible DECL_CONTEXT.
2825 We know that if there is more than one place (binfo) the fndecl that the
2826 declared, they all refer to the same binfo. See get_class_offset_1 for
2827 the check that ensures this. */
2830 virtual_context (fndecl
, t
, vbase
)
2831 tree fndecl
, t
, vbase
;
2834 if (get_base_distance (DECL_CONTEXT (fndecl
), t
, 0, &path
) < 0)
2836 /* DECL_CONTEXT can be ambiguous in t. */
2837 if (get_base_distance (DECL_CONTEXT (fndecl
), vbase
, 0, &path
) >= 0)
2841 /* Not sure if checking path == vbase is necessary here, but just in
2843 if (TREE_VIA_VIRTUAL (path
) || path
== vbase
)
2844 return BINFO_FOR_VBASE (BINFO_TYPE (path
), t
);
2845 path
= BINFO_INHERITANCE_CHAIN (path
);
2848 /* This shouldn't happen, I don't want errors! */
2849 warning ("recoverable compiler error, fixups for virtual function");
2854 if (TREE_VIA_VIRTUAL (path
))
2855 return binfo_member (BINFO_TYPE (path
), CLASSTYPE_VBASECLASSES (t
));
2856 path
= BINFO_INHERITANCE_CHAIN (path
);
2861 /* Fixups upcast offsets for one vtable.
2862 Entries may stay within the VBASE given, or
2863 they may upcast into a direct base, or
2864 they may upcast into a different vbase.
2866 We only need to do fixups in case 2 and 3. In case 2, we add in
2867 the virtual base offset to effect an upcast, in case 3, we add in
2868 the virtual base offset to effect an upcast, then subtract out the
2869 offset for the other virtual base, to effect a downcast into it.
2871 This routine mirrors fixup_vtable_deltas in functionality, though
2872 this one is runtime based, and the other is compile time based.
2873 Conceivably that routine could be removed entirely, and all fixups
2876 VBASE_OFFSETS is an association list of virtual bases that contains
2877 offset information for the virtual bases, so the offsets are only
2878 calculated once. The offsets are computed by where we think the
2879 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2880 the vbase really is. */
2883 expand_upcast_fixups (binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
,
2885 tree binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
, *vbase_offsets
;
2890 unsigned HOST_WIDE_INT n
;
2892 while (BINFO_PRIMARY_MARKED_P (binfo
))
2894 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
2895 if (TREE_VIA_VIRTUAL (binfo
))
2899 delta
= purpose_member (vbase
, *vbase_offsets
);
2902 delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase
));
2903 delta
= build (MINUS_EXPR
, ptrdiff_type_node
, delta
, vbase_addr
);
2904 delta
= save_expr (delta
);
2905 delta
= tree_cons (vbase
, delta
, *vbase_offsets
);
2906 *vbase_offsets
= delta
;
2909 virtuals
= skip_rtti_stuff (binfo
, BINFO_TYPE (binfo
), &n
);
2913 tree current_fndecl
= TREE_VALUE (virtuals
);
2916 && current_fndecl
!= abort_fndecl
2917 && (vc
=virtual_context (current_fndecl
, t
, vbase
)) != vbase
)
2919 /* This may in fact need a runtime fixup. */
2920 tree idx
= build_int_2 (n
, 0);
2921 tree vtbl
= BINFO_VTABLE (binfo
);
2922 tree nvtbl
= lookup_name (DECL_NAME (vtbl
), 0);
2923 tree aref
, ref
, naref
;
2924 tree old_delta
, new_delta
;
2927 if (nvtbl
== NULL_TREE
2928 || nvtbl
== IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl
)))
2930 /* Dup it if it isn't in local scope yet. */
2932 (VAR_DECL
, DECL_NAME (vtbl
),
2933 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl
)));
2934 DECL_ALIGN (nvtbl
) = MAX (TYPE_ALIGN (double_type_node
),
2935 DECL_ALIGN (nvtbl
));
2936 TREE_READONLY (nvtbl
) = 0;
2937 DECL_ARTIFICIAL (nvtbl
) = 1;
2938 nvtbl
= pushdecl (nvtbl
);
2940 cp_finish_decl (nvtbl
, init
, NULL_TREE
,
2941 LOOKUP_ONLYCONVERTING
);
2943 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2944 because they wouldn't be useful; everything that wants to
2945 look at the vtable will look at the decl for the normal
2946 vtable. Setting DECL_CONTEXT also screws up
2947 decl_function_context. */
2949 init
= build (MODIFY_EXPR
, TREE_TYPE (nvtbl
),
2951 finish_expr_stmt (init
);
2952 /* Update the vtable pointers as necessary. */
2953 ref
= build_vfield_ref
2954 (build_indirect_ref (addr
, NULL_PTR
),
2955 DECL_CONTEXT (TYPE_VFIELD (BINFO_TYPE (binfo
))));
2957 (build_modify_expr (ref
, NOP_EXPR
, nvtbl
));
2959 assemble_external (vtbl
);
2960 aref
= build_array_ref (vtbl
, idx
);
2961 naref
= build_array_ref (nvtbl
, idx
);
2962 old_delta
= build_component_ref (aref
, delta_identifier
,
2964 new_delta
= build_component_ref (naref
, delta_identifier
,
2967 /* This is a upcast, so we have to add the offset for the
2969 old_delta
= build_binary_op (PLUS_EXPR
, old_delta
,
2970 TREE_VALUE (delta
));
2973 /* If this is set, we need to subtract out the delta
2974 adjustments for the other virtual base that we
2976 tree vc_delta
= purpose_member (vc
, *vbase_offsets
);
2979 tree vc_addr
= convert_pointer_to_real (vc
, orig_addr
);
2980 vc_delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc
));
2981 vc_delta
= build (MINUS_EXPR
, ptrdiff_type_node
,
2983 vc_delta
= save_expr (vc_delta
);
2984 *vbase_offsets
= tree_cons (vc
, vc_delta
, *vbase_offsets
);
2987 vc_delta
= TREE_VALUE (vc_delta
);
2989 /* This is a downcast, so we have to subtract the offset
2990 for the virtual base. */
2991 old_delta
= build_binary_op (MINUS_EXPR
, old_delta
, vc_delta
);
2994 TREE_READONLY (new_delta
) = 0;
2995 TREE_TYPE (new_delta
) =
2996 cp_build_qualified_type (TREE_TYPE (new_delta
),
2997 CP_TYPE_QUALS (TREE_TYPE (new_delta
))
2998 & ~TYPE_QUAL_CONST
);
2999 finish_expr_stmt (build_modify_expr (new_delta
, NOP_EXPR
,
3003 virtuals
= TREE_CHAIN (virtuals
);
3007 /* Fixup upcast offsets for all direct vtables. Patterned after
3008 expand_direct_vtbls_init. */
3011 fixup_virtual_upcast_offsets (real_binfo
, binfo
, init_self
, can_elide
, addr
, orig_addr
, type
, vbase
, vbase_offsets
)
3012 tree real_binfo
, binfo
;
3013 int init_self
, can_elide
;
3014 tree addr
, orig_addr
, type
, vbase
, *vbase_offsets
;
3016 tree real_binfos
= BINFO_BASETYPES (real_binfo
);
3017 tree binfos
= BINFO_BASETYPES (binfo
);
3018 int i
, n_baselinks
= real_binfos
? TREE_VEC_LENGTH (real_binfos
) : 0;
3020 for (i
= 0; i
< n_baselinks
; i
++)
3022 tree real_base_binfo
= TREE_VEC_ELT (real_binfos
, i
);
3023 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3024 int is_not_base_vtable
3025 = !BINFO_PRIMARY_MARKED_P (real_base_binfo
);
3026 if (! TREE_VIA_VIRTUAL (real_base_binfo
))
3027 fixup_virtual_upcast_offsets (real_base_binfo
, base_binfo
,
3028 is_not_base_vtable
, can_elide
, addr
,
3029 orig_addr
, type
, vbase
, vbase_offsets
);
3032 /* Before turning this on, make sure it is correct. */
3033 if (can_elide
&& ! BINFO_MODIFIED (binfo
))
3036 /* Should we use something besides CLASSTYPE_VFIELDS? */
3037 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo
)))
3039 tree new_addr
= convert_pointer_to_real (binfo
, addr
);
3040 expand_upcast_fixups (real_binfo
, new_addr
, orig_addr
, vbase
, addr
,
3041 type
, vbase_offsets
);
3045 /* Fixup all the virtual upcast offsets for TYPE. DECL_PTR is the
3046 address of the sub-object being initialized. */
3049 fixup_all_virtual_upcast_offsets (type
, decl_ptr
)
3054 tree in_charge_node
;
3057 /* Only tweak the vtables if we're in charge. */
3058 in_charge_node
= current_in_charge_parm
;
3059 if (!in_charge_node
)
3060 /* There's no need for any fixups in this case. */
3062 in_charge_node
= build_binary_op (EQ_EXPR
,
3063 in_charge_node
, integer_zero_node
);
3064 if_stmt
= begin_if_stmt ();
3065 finish_if_stmt_cond (in_charge_node
, if_stmt
);
3067 /* Iterate through the virtual bases, fixing up the upcast offset
3069 for (vbases
= CLASSTYPE_VBASECLASSES (type
);
3071 vbases
= TREE_CHAIN (vbases
))
3073 if (flag_vtable_thunks
)
3074 /* We don't have dynamic thunks yet! So for now, just fail
3083 vbase
= find_vbase_instance (BINFO_TYPE (vbases
), type
);
3084 vbase_offsets
= NULL_TREE
;
3085 addr
= convert_pointer_to_vbase (BINFO_TYPE (vbases
), decl_ptr
);
3086 fixup_virtual_upcast_offsets (vbase
,
3087 TYPE_BINFO (BINFO_TYPE (vbases
)),
3088 1, 0, addr
, decl_ptr
,
3089 type
, vbase
, &vbase_offsets
);
3093 /* Close out the if-statement. */
3094 finish_then_clause (if_stmt
);
3098 /* Generate the code needed to initialize all the virtual function
3099 table slots of all the virtual baseclasses. BINFO is the binfo
3100 which determines the virtual baseclasses to use. TRUE_EXP is the
3101 true object we are initializing, and DECL_PTR is the pointer to the
3102 sub-object we are initializing. */
3105 expand_indirect_vtbls_init (binfo
, decl_ptr
)
3109 tree type
= BINFO_TYPE (binfo
);
3111 /* This function executes during the finish_function() segment,
3112 AFTER the auto variables and temporary stack space has been marked
3113 unused...If space is needed for the virtual function tables,
3114 some of them might fit within what the compiler now thinks
3115 are available stack slots... These values are actually initialized at
3116 the beginnning of the function, so when the automatics use their space,
3117 they will overwrite the values that are placed here. Marking all
3118 temporary space as unavailable prevents this from happening. */
3120 mark_all_temps_used();
3122 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
3124 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
3125 struct vbase_info vi
;
3126 vi
.decl_ptr
= decl_ptr
;
3127 vi
.vbase_types
= vbases
;
3129 dfs_walk (binfo
, dfs_find_vbases
, unmarked_new_vtablep
, &vi
);
3130 fixup_all_virtual_upcast_offsets (type
, vi
.decl_ptr
);
3131 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
, 0);
3135 /* get virtual base class types.
3136 This adds type to the vbase_types list in reverse dfs order.
3137 Ordering is very important, so don't change it. */
3140 dfs_get_vbase_types (binfo
, data
)
3144 tree type
= (tree
) data
;
3146 if (TREE_VIA_VIRTUAL (binfo
) && ! BINFO_VBASE_MARKED (binfo
))
3148 tree new_vbase
= make_binfo (integer_zero_node
,
3150 BINFO_VTABLE (binfo
),
3151 BINFO_VIRTUALS (binfo
));
3152 unshare_base_binfos (new_vbase
);
3153 TREE_VIA_VIRTUAL (new_vbase
) = 1;
3154 BINFO_INHERITANCE_CHAIN (new_vbase
) = TYPE_BINFO (type
);
3155 TREE_CHAIN (new_vbase
) = CLASSTYPE_VBASECLASSES (type
);
3156 CLASSTYPE_VBASECLASSES (type
) = new_vbase
;
3157 SET_BINFO_VBASE_MARKED (binfo
);
3159 SET_BINFO_MARKED (binfo
);
3163 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
3166 get_vbase_types (type
)
3169 CLASSTYPE_VBASECLASSES (type
) = NULL_TREE
;
3170 dfs_walk (TYPE_BINFO (type
), dfs_get_vbase_types
, unmarkedp
, type
);
3171 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
3172 reverse it so that we get normal dfs ordering. */
3173 CLASSTYPE_VBASECLASSES (type
) = nreverse (CLASSTYPE_VBASECLASSES (type
));
3174 dfs_walk (TYPE_BINFO (type
), dfs_vbase_unmark
, markedp
, 0);
3177 /* Called from find_vbase_instance via dfs_walk. */
3180 dfs_find_vbase_instance (binfo
, data
)
3184 tree base
= TREE_VALUE ((tree
) data
);
3186 if (BINFO_PRIMARY_MARKED_P (binfo
)
3187 && same_type_p (BINFO_TYPE (binfo
), base
))
3193 /* Find the real occurrence of the virtual BASE (a class type) in the
3194 hierarchy dominated by TYPE. */
3197 find_vbase_instance (base
, type
)
3203 instance
= BINFO_FOR_VBASE (base
, type
);
3204 if (!BINFO_VBASE_PRIMARY_P (instance
))
3207 return dfs_walk (TYPE_BINFO (type
),
3208 dfs_find_vbase_instance
,
3210 build_tree_list (type
, base
));
3214 /* Debug info for C++ classes can get very large; try to avoid
3215 emitting it everywhere.
3217 Note that this optimization wins even when the target supports
3218 BINCL (if only slightly), and reduces the amount of work for the
3222 maybe_suppress_debug_info (t
)
3225 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
3226 does not support name references between translation units. It supports
3227 symbolic references between translation units, but only within a single
3228 executable or shared library.
3230 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
3231 that the type was never defined, so we only get the members we
3233 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== NO_DEBUG
)
3236 /* We might have set this earlier in cp_finish_decl. */
3237 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 0;
3239 /* If we already know how we're handling this class, handle debug info
3241 if (CLASSTYPE_INTERFACE_ONLY (t
))
3242 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
3243 else if (CLASSTYPE_INTERFACE_KNOWN (t
))
3244 /* Don't set it. */;
3245 /* If the class has a vtable, write out the debug info along with
3247 else if (TYPE_CONTAINS_VPTR_P (t
))
3248 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
3250 /* Otherwise, just emit the debug info normally. */
3254 /* Keep this code around in case we later want to control debug info
3255 based on whether a type is "used". Currently, we only suppress debug
3256 info if we can emit it with the vtable. jason 1999-11-11) */
3258 /* If we want debug info for a type TYPE, make sure all its base types
3259 are also marked as being potentially interesting. This avoids
3260 the problem of not writing any debug info for intermediate basetypes
3261 that have abstract virtual functions. Also mark member types. */
3264 note_debug_info_needed (type
)
3269 if (current_template_parms
)
3272 if (TYPE_BEING_DEFINED (type
))
3273 /* We can't go looking for the base types and fields just yet. */
3276 /* See the comment in maybe_suppress_debug_info. */
3277 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== NO_DEBUG
)
3280 dfs_walk (TYPE_BINFO (type
), dfs_debug_mark
, dfs_debug_unmarkedp
, 0);
3281 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3284 if (TREE_CODE (field
) == FIELD_DECL
3285 && IS_AGGR_TYPE (ttype
= target_type (TREE_TYPE (field
)))
3286 && dfs_debug_unmarkedp (TYPE_BINFO (ttype
), 0))
3287 note_debug_info_needed (ttype
);
3292 /* Subroutines of push_class_decls (). */
3294 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
3295 because it (or one of the intermediate bases) depends on template parms. */
3298 dependent_base_p (binfo
)
3301 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
3303 if (currently_open_class (TREE_TYPE (binfo
)))
3305 if (uses_template_parms (TREE_TYPE (binfo
)))
3312 setup_class_bindings (name
, type_binding_p
)
3316 tree type_binding
= NULL_TREE
;
3319 /* If we've already done the lookup for this declaration, we're
3321 if (IDENTIFIER_CLASS_VALUE (name
))
3324 /* First, deal with the type binding. */
3327 type_binding
= lookup_member (current_class_type
, name
,
3330 if (TREE_CODE (type_binding
) == TREE_LIST
3331 && TREE_TYPE (type_binding
) == error_mark_node
)
3332 /* NAME is ambiguous. */
3333 push_class_level_binding (name
, type_binding
);
3335 pushdecl_class_level (type_binding
);
3338 /* Now, do the value binding. */
3339 value_binding
= lookup_member (current_class_type
, name
,
3344 && (TREE_CODE (value_binding
) == TYPE_DECL
3345 || (TREE_CODE (value_binding
) == TREE_LIST
3346 && TREE_TYPE (value_binding
) == error_mark_node
3347 && (TREE_CODE (TREE_VALUE (value_binding
))
3349 /* We found a type-binding, even when looking for a non-type
3350 binding. This means that we already processed this binding
3352 my_friendly_assert (type_binding_p
, 19990401);
3353 else if (value_binding
)
3355 if (TREE_CODE (value_binding
) == TREE_LIST
3356 && TREE_TYPE (value_binding
) == error_mark_node
)
3357 /* NAME is ambiguous. */
3358 push_class_level_binding (name
, value_binding
);
3361 if (BASELINK_P (value_binding
))
3362 /* NAME is some overloaded functions. */
3363 value_binding
= TREE_VALUE (value_binding
);
3364 pushdecl_class_level (value_binding
);
3369 /* Push class-level declarations for any names appearing in BINFO that
3373 dfs_push_type_decls (binfo
, data
)
3375 void *data ATTRIBUTE_UNUSED
;
3380 type
= BINFO_TYPE (binfo
);
3381 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3382 if (DECL_NAME (fields
) && TREE_CODE (fields
) == TYPE_DECL
3383 && !(!same_type_p (type
, current_class_type
)
3384 && template_self_reference_p (type
, fields
)))
3385 setup_class_bindings (DECL_NAME (fields
), /*type_binding_p=*/1);
3387 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3388 DERIVED_FROM_P, which calls get_base_distance. */
3389 SET_BINFO_PUSHDECLS_MARKED (binfo
);
3394 /* Push class-level declarations for any names appearing in BINFO that
3395 are not TYPE_DECLS. */
3398 dfs_push_decls (binfo
, data
)
3406 type
= BINFO_TYPE (binfo
);
3407 dep_base_p
= (processing_template_decl
&& type
!= current_class_type
3408 && dependent_base_p (binfo
));
3412 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3413 if (DECL_NAME (fields
)
3414 && TREE_CODE (fields
) != TYPE_DECL
3415 && TREE_CODE (fields
) != USING_DECL
)
3416 setup_class_bindings (DECL_NAME (fields
), /*type_binding_p=*/0);
3417 else if (TREE_CODE (fields
) == FIELD_DECL
3418 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
3419 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields
)), data
);
3421 method_vec
= (CLASS_TYPE_P (type
)
3422 ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
);
3428 /* Farm out constructors and destructors. */
3429 end
= TREE_VEC_END (method_vec
);
3431 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
3432 *methods
&& methods
!= end
;
3434 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods
)),
3435 /*type_binding_p=*/0);
3439 CLEAR_BINFO_PUSHDECLS_MARKED (binfo
);
3444 /* When entering the scope of a class, we cache all of the
3445 fields that that class provides within its inheritance
3446 lattice. Where ambiguities result, we mark them
3447 with `error_mark_node' so that if they are encountered
3448 without explicit qualification, we can emit an error
3452 push_class_decls (type
)
3455 search_stack
= push_search_level (search_stack
, &search_obstack
);
3457 /* Enter type declarations and mark. */
3458 dfs_walk (TYPE_BINFO (type
), dfs_push_type_decls
, unmarked_pushdecls_p
, 0);
3460 /* Enter non-type declarations and unmark. */
3461 dfs_walk (TYPE_BINFO (type
), dfs_push_decls
, marked_pushdecls_p
, 0);
3464 /* Here's a subroutine we need because C lacks lambdas. */
3467 dfs_unuse_fields (binfo
, data
)
3469 void *data ATTRIBUTE_UNUSED
;
3471 tree type
= TREE_TYPE (binfo
);
3474 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3476 if (TREE_CODE (fields
) != FIELD_DECL
)
3479 TREE_USED (fields
) = 0;
3480 if (DECL_NAME (fields
) == NULL_TREE
3481 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
3482 unuse_fields (TREE_TYPE (fields
));
3492 dfs_walk (TYPE_BINFO (type
), dfs_unuse_fields
, unmarkedp
, 0);
3498 /* We haven't pushed a search level when dealing with cached classes,
3499 so we'd better not try to pop it. */
3501 search_stack
= pop_search_level (search_stack
);
3505 print_search_statistics ()
3507 #ifdef GATHER_STATISTICS
3508 fprintf (stderr
, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3509 n_fields_searched
, n_calls_lookup_field
, n_calls_lookup_field_1
);
3510 fprintf (stderr
, "%d fnfields searched in %d calls to lookup_fnfields\n",
3511 n_outer_fields_searched
, n_calls_lookup_fnfields
);
3512 fprintf (stderr
, "%d calls to get_base_type\n", n_calls_get_base_type
);
3513 #else /* GATHER_STATISTICS */
3514 fprintf (stderr
, "no search statistics\n");
3515 #endif /* GATHER_STATISTICS */
3519 init_search_processing ()
3521 gcc_obstack_init (&search_obstack
);
3522 vptr_identifier
= get_identifier ("_vptr");
3526 reinit_search_statistics ()
3528 #ifdef GATHER_STATISTICS
3529 n_fields_searched
= 0;
3530 n_calls_lookup_field
= 0, n_calls_lookup_field_1
= 0;
3531 n_calls_lookup_fnfields
= 0, n_calls_lookup_fnfields_1
= 0;
3532 n_calls_get_base_type
= 0;
3533 n_outer_fields_searched
= 0;
3534 n_contexts_saved
= 0;
3535 #endif /* GATHER_STATISTICS */
3539 add_conversions (binfo
, data
)
3544 tree method_vec
= CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo
));
3545 tree
*conversions
= (tree
*) data
;
3547 /* Some builtin types have no method vector, not even an empty one. */
3551 for (i
= 2; i
< TREE_VEC_LENGTH (method_vec
); ++i
)
3553 tree tmp
= TREE_VEC_ELT (method_vec
, i
);
3556 if (!tmp
|| ! DECL_CONV_FN_P (OVL_CURRENT (tmp
)))
3559 name
= DECL_NAME (OVL_CURRENT (tmp
));
3561 /* Make sure we don't already have this conversion. */
3562 if (! IDENTIFIER_MARKED (name
))
3564 *conversions
= tree_cons (binfo
, tmp
, *conversions
);
3565 IDENTIFIER_MARKED (name
) = 1;
3571 /* Return a TREE_LIST containing all the non-hidden user-defined
3572 conversion functions for TYPE (and its base-classes). The
3573 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
3574 containing the conversion functions. The TREE_PURPOSE is the BINFO
3575 from which the conversion functions in this node were selected. */
3578 lookup_conversions (type
)
3582 tree conversions
= NULL_TREE
;
3584 if (TYPE_SIZE (type
))
3585 bfs_walk (TYPE_BINFO (type
), add_conversions
, 0, &conversions
);
3587 for (t
= conversions
; t
; t
= TREE_CHAIN (t
))
3588 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t
)))) = 0;
3599 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3600 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3603 dfs_check_overlap (empty_binfo
, data
)
3607 struct overlap_info
*oi
= (struct overlap_info
*) data
;
3609 for (binfo
= TYPE_BINFO (oi
->compare_type
);
3611 binfo
= BINFO_BASETYPE (binfo
, 0))
3613 if (BINFO_TYPE (binfo
) == BINFO_TYPE (empty_binfo
))
3615 oi
->found_overlap
= 1;
3618 else if (BINFO_BASETYPES (binfo
) == NULL_TREE
)
3625 /* Trivial function to stop base traversal when we find something. */
3628 dfs_no_overlap_yet (binfo
, data
)
3632 struct overlap_info
*oi
= (struct overlap_info
*) data
;
3633 return !oi
->found_overlap
? binfo
: NULL_TREE
;
3636 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3637 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3640 types_overlap_p (empty_type
, next_type
)
3641 tree empty_type
, next_type
;
3643 struct overlap_info oi
;
3645 if (! IS_AGGR_TYPE (next_type
))
3647 oi
.compare_type
= next_type
;
3648 oi
.found_overlap
= 0;
3649 dfs_walk (TYPE_BINFO (empty_type
), dfs_check_overlap
,
3650 dfs_no_overlap_yet
, &oi
);
3651 return oi
.found_overlap
;
3654 /* Given a vtable VAR, determine which binfo it comes from.
3656 FIXME What about secondary vtables? */
3659 binfo_for_vtable (var
)
3662 tree binfo
= TYPE_BINFO (DECL_CONTEXT (var
));
3668 binfos
= BINFO_BASETYPES (binfo
);
3669 if (binfos
== NULL_TREE
)
3672 i
= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
3676 binfo
= TREE_VEC_ELT (binfos
, i
);
3682 /* Returns 1 iff BINFO is from a direct or indirect virtual base. */
3685 binfo_from_vbase (binfo
)
3688 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
3690 if (TREE_VIA_VIRTUAL (binfo
))