1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 89, 92-97, 1998, 1999 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern struct obstack
*current_obstack
;
40 extern tree abort_fndecl
;
44 /* Obstack used for remembering decision points of breadth-first. */
46 static struct obstack search_obstack
;
48 /* Methods for pushing and popping objects to and from obstacks. */
51 push_stack_level (obstack
, tp
, size
)
52 struct obstack
*obstack
;
53 char *tp
; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
56 struct stack_level
*stack
;
57 obstack_grow (obstack
, tp
, size
);
58 stack
= (struct stack_level
*) ((char*)obstack_next_free (obstack
) - size
);
59 obstack_finish (obstack
);
60 stack
->obstack
= obstack
;
61 stack
->first
= (tree
*) obstack_base (obstack
);
62 stack
->limit
= obstack_room (obstack
) / sizeof (tree
*);
67 pop_stack_level (stack
)
68 struct stack_level
*stack
;
70 struct stack_level
*tem
= stack
;
71 struct obstack
*obstack
= tem
->obstack
;
73 obstack_free (obstack
, tem
);
77 #define search_level stack_level
78 static struct search_level
*search_stack
;
80 static tree get_abstract_virtuals_1
PROTO((tree
, int, tree
));
81 static tree get_vbase_1
PROTO((tree
, tree
, unsigned int *));
82 static tree convert_pointer_to_vbase
PROTO((tree
, tree
));
83 static tree lookup_field_1
PROTO((tree
, tree
));
84 static tree convert_pointer_to_single_level
PROTO((tree
, tree
));
85 static int lookup_fnfields_here
PROTO((tree
, tree
));
86 static int is_subobject_of_p
PROTO((tree
, tree
));
87 static int hides
PROTO((tree
, tree
));
88 static tree virtual_context
PROTO((tree
, tree
, tree
));
89 static tree dfs_check_overlap
PROTO((tree
, void *));
90 static tree dfs_no_overlap_yet
PROTO((tree
, void *));
91 static void envelope_add_decl
PROTO((tree
, tree
, tree
*));
92 static int get_base_distance_recursive
93 PROTO((tree
, int, int, int, int *, tree
*, tree
,
94 int, int *, int, int));
95 static void expand_upcast_fixups
96 PROTO((tree
, tree
, tree
, tree
, tree
, tree
, tree
*));
97 static void fixup_virtual_upcast_offsets
98 PROTO((tree
, tree
, int, int, tree
, tree
, tree
, tree
,
100 static tree unmarkedp
PROTO((tree
, void *));
101 static tree marked_vtable_pathp
PROTO((tree
, void *));
102 static tree unmarked_vtable_pathp
PROTO((tree
, void *));
103 static tree marked_new_vtablep
PROTO((tree
, void *));
104 static tree unmarked_new_vtablep
PROTO((tree
, void *));
105 static tree marked_pushdecls_p
PROTO((tree
, void *));
106 static tree unmarked_pushdecls_p
PROTO((tree
, void *));
107 static tree dfs_debug_unmarkedp
PROTO((tree
, void *));
108 static tree dfs_debug_mark
PROTO((tree
, void *));
109 static tree dfs_find_vbases
PROTO((tree
, void *));
110 static tree dfs_clear_vbase_slots
PROTO((tree
, void *));
111 static tree dfs_init_vbase_pointers
PROTO((tree
, void *));
112 static tree dfs_get_vbase_types
PROTO((tree
, void *));
113 static tree dfs_pushdecls
PROTO((tree
, void *));
114 static tree dfs_compress_decls
PROTO((tree
, void *));
115 static tree dfs_unuse_fields
PROTO((tree
, void *));
116 static tree add_conversions
PROTO((tree
, void *));
117 static tree get_virtuals_named_this
PROTO((tree
, tree
));
118 static tree get_virtual_destructor
PROTO((tree
, void *));
119 static tree tree_has_any_destructor_p
PROTO((tree
, void *));
120 static int covariant_return_p
PROTO((tree
, tree
));
121 static struct search_level
*push_search_level
122 PROTO((struct stack_level
*, struct obstack
*));
123 static struct search_level
*pop_search_level
124 PROTO((struct stack_level
*));
126 PROTO((tree
, tree (*) (tree
, void *), tree (*) (tree
, void *),
128 static tree lookup_field_queue_p
PROTO((tree
, void *));
129 static tree lookup_field_r
PROTO((tree
, void *));
130 static tree dfs_walk_real
PROTO ((tree
,
131 tree (*) (tree
, void *),
132 tree (*) (tree
, void *),
133 tree (*) (tree
, void *),
135 static tree dfs_bfv_queue_p
PROTO ((tree
, void *));
136 static tree dfs_bfv_helper
PROTO ((tree
, void *));
137 static tree get_virtuals_named_this_r
PROTO ((tree
, void *));
138 static tree context_for_name_lookup
PROTO ((tree
));
139 static tree canonical_binfo
PROTO ((tree
));
140 static tree shared_marked_p
PROTO ((tree
, void *));
141 static tree shared_unmarked_p
PROTO ((tree
, void *));
142 static int dependent_base_p
PROTO ((tree
));
143 static tree dfs_accessible_queue_p
PROTO ((tree
, void *));
144 static tree dfs_accessible_p
PROTO ((tree
, void *));
145 static tree dfs_access_in_type
PROTO ((tree
, void *));
146 static tree access_in_type
PROTO ((tree
, tree
));
148 /* Allocate a level of searching. */
150 static struct search_level
*
151 push_search_level (stack
, obstack
)
152 struct stack_level
*stack
;
153 struct obstack
*obstack
;
155 struct search_level tem
;
158 return push_stack_level (obstack
, (char *)&tem
, sizeof (tem
));
161 /* Discard a level of search allocation. */
163 static struct search_level
*
164 pop_search_level (obstack
)
165 struct stack_level
*obstack
;
167 register struct search_level
*stack
= pop_stack_level (obstack
);
172 static tree _vptr_name
;
174 /* Variables for gathering statistics. */
175 #ifdef GATHER_STATISTICS
176 static int n_fields_searched
;
177 static int n_calls_lookup_field
, n_calls_lookup_field_1
;
178 static int n_calls_lookup_fnfields
, n_calls_lookup_fnfields_1
;
179 static int n_calls_get_base_type
;
180 static int n_outer_fields_searched
;
181 static int n_contexts_saved
;
182 #endif /* GATHER_STATISTICS */
185 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
186 the same type as the type given in PARENT. To be optimal, we want
187 the first one that is found by going through the least number of
190 This uses a clever algorithm that updates *depth when we find the vbase,
191 and cuts off other paths of search when they reach that depth. */
194 get_vbase_1 (parent
, binfo
, depth
)
200 tree rval
= NULL_TREE
;
202 if (BINFO_TYPE (binfo
) == parent
&& TREE_VIA_VIRTUAL (binfo
))
210 binfos
= BINFO_BASETYPES (binfo
);
211 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
213 /* Process base types. */
214 for (i
= 0; i
< n_baselinks
; i
++)
216 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
222 nrval
= get_vbase_1 (parent
, base_binfo
, depth
);
230 /* Return the shortest path to vbase PARENT within BINFO, ignoring
231 access and ambiguity. */
234 get_vbase (parent
, binfo
)
238 unsigned int d
= (unsigned int)-1;
239 return get_vbase_1 (parent
, binfo
, &d
);
242 /* Convert EXPR to a virtual base class of type TYPE. We know that
243 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
244 the type of what expr points to has a virtual base of type TYPE. */
247 convert_pointer_to_vbase (type
, expr
)
251 tree vb
= get_vbase (type
, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr
))));
252 return convert_pointer_to_real (vb
, expr
);
255 /* Check whether the type given in BINFO is derived from PARENT. If
256 it isn't, return 0. If it is, but the derivation is MI-ambiguous
257 AND protect != 0, emit an error message and return error_mark_node.
259 Otherwise, if TYPE is derived from PARENT, return the actual base
260 information, unless a one of the protection violations below
261 occurs, in which case emit an error message and return error_mark_node.
263 If PROTECT is 1, then check if access to a public field of PARENT
264 would be private. Also check for ambiguity. */
267 get_binfo (parent
, binfo
, protect
)
268 register tree parent
, binfo
;
271 tree type
= NULL_TREE
;
273 tree rval
= NULL_TREE
;
275 if (TREE_CODE (parent
) == TREE_VEC
)
276 parent
= BINFO_TYPE (parent
);
277 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent
)))
278 my_friendly_abort (89);
280 if (TREE_CODE (binfo
) == TREE_VEC
)
281 type
= BINFO_TYPE (binfo
);
282 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
285 my_friendly_abort (90);
287 dist
= get_base_distance (parent
, binfo
, protect
, &rval
);
291 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
293 return error_mark_node
;
295 else if (dist
== -2 && protect
)
297 cp_error ("type `%T' is ambiguous base class for type `%T'", parent
,
299 return error_mark_node
;
305 /* This is the newer depth first get_base_distance routine. */
308 get_base_distance_recursive (binfo
, depth
, is_private
, rval
,
309 rval_private_ptr
, new_binfo_ptr
, parent
,
310 protect
, via_virtual_ptr
, via_virtual
,
311 current_scope_in_chain
)
313 int depth
, is_private
, rval
;
314 int *rval_private_ptr
;
315 tree
*new_binfo_ptr
, parent
;
316 int protect
, *via_virtual_ptr
, via_virtual
;
317 int current_scope_in_chain
;
323 && !current_scope_in_chain
324 && is_friend (BINFO_TYPE (binfo
), current_scope ()))
325 current_scope_in_chain
= 1;
327 if (BINFO_TYPE (binfo
) == parent
|| binfo
== parent
)
332 /* This is the first time we've found parent. */
334 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr
),
335 BINFO_OFFSET (binfo
))
336 && *via_virtual_ptr
&& via_virtual
)
338 /* A new path to the same vbase. If this one has better
339 access or is shorter, take it. */
342 better
= *rval_private_ptr
- is_private
;
344 better
= rval
- depth
;
348 /* Ambiguous base class. */
351 /* If we get an ambiguity between virtual and non-virtual base
352 class, return the non-virtual in case we are ignoring
354 better
= *via_virtual_ptr
- via_virtual
;
360 *rval_private_ptr
= is_private
;
361 *new_binfo_ptr
= binfo
;
362 *via_virtual_ptr
= via_virtual
;
368 binfos
= BINFO_BASETYPES (binfo
);
369 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
372 /* Process base types. */
373 for (i
= 0; i
< n_baselinks
; i
++)
375 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
380 || (!TREE_VIA_PUBLIC (base_binfo
)
381 && !(TREE_VIA_PROTECTED (base_binfo
)
382 && current_scope_in_chain
)
383 && !is_friend (BINFO_TYPE (binfo
), current_scope ()))));
384 int this_virtual
= via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
);
386 rval
= get_base_distance_recursive (base_binfo
, depth
, via_private
,
387 rval
, rval_private_ptr
,
388 new_binfo_ptr
, parent
,
389 protect
, via_virtual_ptr
,
391 current_scope_in_chain
);
393 /* If we've found a non-virtual, ambiguous base class, we don't need
394 to keep searching. */
395 if (rval
== -2 && *via_virtual_ptr
== 0)
402 /* Return the number of levels between type PARENT and the type given
403 in BINFO, following the leftmost path to PARENT not found along a
404 virtual path, if there are no real PARENTs (all come from virtual
405 base classes), then follow the shortest public path to PARENT.
407 Return -1 if TYPE is not derived from PARENT.
408 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
410 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
412 If PATH_PTR is non-NULL, then also build the list of types
413 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
416 PARENT can also be a binfo, in which case that exact parent is found
417 and no other. convert_pointer_to_real uses this functionality.
419 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
422 get_base_distance (parent
, binfo
, protect
, path_ptr
)
423 register tree parent
, binfo
;
428 int rval_private
= 0;
429 tree type
= NULL_TREE
;
430 tree new_binfo
= NULL_TREE
;
432 int watch_access
= protect
;
434 /* Should we be completing types here? */
435 if (TREE_CODE (parent
) != TREE_VEC
)
436 parent
= complete_type (TYPE_MAIN_VARIANT (parent
));
438 complete_type (TREE_TYPE (parent
));
440 if (TREE_CODE (binfo
) == TREE_VEC
)
441 type
= BINFO_TYPE (binfo
);
442 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
444 type
= complete_type (binfo
);
445 binfo
= TYPE_BINFO (type
);
448 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo
) == NULL_TREE
,
452 my_friendly_abort (92);
454 if (parent
== type
|| parent
== binfo
)
456 /* If the distance is 0, then we don't really need
457 a path pointer, but we shouldn't let garbage go back. */
466 rval
= get_base_distance_recursive (binfo
, 0, 0, -1,
467 &rval_private
, &new_binfo
, parent
,
468 watch_access
, &via_virtual
, 0,
471 /* Access restrictions don't count if we found an ambiguous basetype. */
472 if (rval
== -2 && protect
>= 0)
475 if (rval
&& protect
&& rval_private
)
478 /* If they gave us the real vbase binfo, which isn't in the main binfo
479 tree, deal with it. This happens when we are called from
480 expand_upcast_fixups. */
481 if (rval
== -1 && TREE_CODE (parent
) == TREE_VEC
482 && parent
== binfo_member (BINFO_TYPE (parent
),
483 CLASSTYPE_VBASECLASSES (type
)))
485 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent
) == binfo
, 980827);
491 *path_ptr
= new_binfo
;
495 /* Search for a member with name NAME in a multiple inheritance lattice
496 specified by TYPE. If it does not exist, return NULL_TREE.
497 If the member is ambiguously referenced, return `error_mark_node'.
498 Otherwise, return the FIELD_DECL. */
500 /* Do a 1-level search for NAME as a member of TYPE. The caller must
501 figure out whether it can access this field. (Since it is only one
502 level, this is reasonable.) */
505 lookup_field_1 (type
, name
)
510 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
511 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
512 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
513 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
514 the code often worked even when we treated the index as a list
518 field
= TYPE_FIELDS (type
);
520 #ifdef GATHER_STATISTICS
521 n_calls_lookup_field_1
++;
522 #endif /* GATHER_STATISTICS */
525 #ifdef GATHER_STATISTICS
527 #endif /* GATHER_STATISTICS */
528 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field
)) == 'd', 0);
529 if (DECL_NAME (field
) == NULL_TREE
530 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
532 tree temp
= lookup_field_1 (TREE_TYPE (field
), name
);
536 if (TREE_CODE (field
) == USING_DECL
)
537 /* For now, we're just treating member using declarations as
538 old ARM-style access declarations. Thus, there's no reason
539 to return a USING_DECL, and the rest of the compiler can't
540 handle it. Once the class is defined, these are purged
541 from TYPE_FIELDS anyhow; see handle_using_decl. */
543 else if (DECL_NAME (field
) == name
)
545 if ((TREE_CODE(field
) == VAR_DECL
|| TREE_CODE(field
) == CONST_DECL
)
546 && DECL_ASSEMBLER_NAME (field
) != NULL
)
547 GNU_xref_ref(current_function_decl
,
548 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field
)));
551 field
= TREE_CHAIN (field
);
554 if (name
== _vptr_name
)
556 /* Give the user what s/he thinks s/he wants. */
557 if (TYPE_VIRTUAL_P (type
))
558 return CLASSTYPE_VFIELD (type
);
563 /* There are a number of cases we need to be aware of here:
564 current_class_type current_function_decl
571 Those last two make life interesting. If we're in a function which is
572 itself inside a class, we need decls to go into the fn's decls (our
573 second case below). But if we're in a class and the class itself is
574 inside a function, we need decls to go into the decls for the class. To
575 achieve this last goal, we must see if, when both current_class_ptr and
576 current_function_decl are set, the class was declared inside that
577 function. If so, we know to put the decls into the class's scope. */
582 if (current_function_decl
== NULL_TREE
)
583 return current_class_type
;
584 if (current_class_type
== NULL_TREE
)
585 return current_function_decl
;
586 if (DECL_CLASS_CONTEXT (current_function_decl
) == current_class_type
)
587 return current_function_decl
;
589 return current_class_type
;
592 /* Return the scope of DECL, as appropriate when doing name-lookup. */
595 context_for_name_lookup (decl
)
600 For the purposes of name lookup, after the anonymous union
601 definition, the members of the anonymous union are considered to
602 have been defined in the scope in which teh anonymous union is
604 tree context
= DECL_REAL_CONTEXT (decl
);
606 while (TYPE_P (context
) && ANON_UNION_TYPE_P (context
))
607 context
= TYPE_CONTEXT (context
);
609 context
= global_namespace
;
614 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
618 canonical_binfo (binfo
)
621 return (TREE_VIA_VIRTUAL (binfo
)
622 ? TYPE_BINFO (BINFO_TYPE (binfo
)) : binfo
);
625 /* If BINFO is marked, return a canonical version of BINFO.
626 Otherwise, return NULL_TREE. */
629 shared_marked_p (binfo
, data
)
633 binfo
= canonical_binfo (binfo
);
634 return markedp (binfo
, data
) ? binfo
: NULL_TREE
;
637 /* If BINFO is not marked, return a canonical version of BINFO.
638 Otherwise, return NULL_TREE. */
641 shared_unmarked_p (binfo
, data
)
645 binfo
= canonical_binfo (binfo
);
646 return unmarkedp (binfo
, data
) ? binfo
: NULL_TREE
;
649 /* Called from access_in_type via dfs_walk. Calculate the access to
650 DATA (which is really a DECL) in BINFO. */
653 dfs_access_in_type (binfo
, data
)
657 tree decl
= (tree
) data
;
658 tree type
= BINFO_TYPE (binfo
);
659 tree access
= NULL_TREE
;
661 if (context_for_name_lookup (decl
) == type
)
663 /* If we have desceneded to the scope of DECL, just note the
664 appropriate access. */
665 if (TREE_PRIVATE (decl
))
666 access
= access_private_node
;
667 else if (TREE_PROTECTED (decl
))
668 access
= access_protected_node
;
670 access
= access_public_node
;
674 /* First, check for an access-declaration that gives us more
675 access to the DECL. The CONST_DECL for an enumeration
676 constant will not have DECL_LANG_SPECIFIC, and thus no
678 if (DECL_LANG_SPECIFIC (decl
))
680 access
= purpose_member (type
, DECL_ACCESS (decl
));
682 access
= TREE_VALUE (access
);
691 /* Otherwise, scan our baseclasses, and pick the most favorable
693 binfos
= BINFO_BASETYPES (binfo
);
694 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
695 for (i
= 0; i
< n_baselinks
; ++i
)
697 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
698 tree base_access
= TREE_CHAIN (canonical_binfo (base_binfo
));
700 if (!base_access
|| base_access
== access_private_node
)
701 /* If it was not accessible in the base, or only
702 accessible as a private member, we can't access it
704 base_access
= NULL_TREE
;
705 else if (TREE_VIA_PROTECTED (base_binfo
))
706 /* Public and protected members in the base are
708 base_access
= access_protected_node
;
709 else if (!TREE_VIA_PUBLIC (base_binfo
))
710 /* Public and protected members in the base are
712 base_access
= access_private_node
;
714 /* See if the new access, via this base, gives more
715 access than our previous best access. */
717 (base_access
== access_public_node
718 || (base_access
== access_protected_node
719 && access
!= access_public_node
)
720 || (base_access
== access_private_node
723 access
= base_access
;
725 /* If the new access is public, we can't do better. */
726 if (access
== access_public_node
)
733 /* Note the access to DECL in TYPE. */
734 TREE_CHAIN (binfo
) = access
;
736 /* Mark TYPE as visited so that if we reach it again we do not
737 duplicate our efforts here. */
738 SET_BINFO_MARKED (binfo
);
743 /* Return the access to DECL in TYPE. */
746 access_in_type (type
, decl
)
750 tree binfo
= TYPE_BINFO (type
);
752 /* We must take into account
756 If a name can be reached by several paths through a multiple
757 inheritance graph, the access is that of the path that gives
760 The algorithm we use is to make a post-order depth-first traversal
761 of the base-class hierarchy. As we come up the tree, we annotate
762 each node with the most lenient access. */
763 dfs_walk_real (binfo
, 0, dfs_access_in_type
, shared_unmarked_p
, decl
);
764 dfs_walk (binfo
, dfs_unmark
, shared_marked_p
, 0);
766 return TREE_CHAIN (binfo
);
769 /* Called from dfs_accessible_p via dfs_walk. */
772 dfs_accessible_queue_p (binfo
, data
)
774 void *data ATTRIBUTE_UNUSED
;
776 if (BINFO_MARKED (binfo
))
779 /* If this class is inherited via private or protected inheritance,
780 then we can't see it, unless we are a friend of the subclass. */
781 if (!TREE_VIA_PUBLIC (binfo
)
782 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
786 return canonical_binfo (binfo
);
789 /* Called from dfs_accessible_p via dfs_walk. */
792 dfs_accessible_p (binfo
, data
)
796 int protected_ok
= data
!= 0;
799 /* We marked the binfos while computing the access in each type.
800 So, we unmark as we go now. */
801 SET_BINFO_MARKED (binfo
);
803 access
= TREE_CHAIN (binfo
);
804 if (access
== access_public_node
805 || (access
== access_protected_node
&& protected_ok
))
807 else if (access
&& is_friend (BINFO_TYPE (binfo
), current_scope ()))
813 /* DECL is a declaration from a base class of TYPE, which was the
814 classs used to name DECL. Return non-zero if, in the current
815 context, DECL is accessible. If TYPE is actually a BINFO node,
816 then the most derived class along the path indicated by BINFO is
817 the one used to name the DECL. */
820 accessible_p (type
, decl
)
829 /* Non-zero if it's OK to access DECL if it has protected
830 accessibility in TYPE. */
831 int protected_ok
= 0;
833 /* If we're not checking access, everything is accessible. */
834 if (!flag_access_control
)
837 /* If this declaration is in a block or namespace scope, there's no
839 if (!TYPE_P (context_for_name_lookup (decl
)))
842 /* We don't do access control for types yet. */
843 if (TREE_CODE (decl
) == TYPE_DECL
)
849 type
= BINFO_TYPE (type
);
852 binfo
= TYPE_BINFO (type
);
854 /* [class.access.base]
856 A member m is accessible when named in class N if
858 --m as a member of N is public, or
860 --m as a member of N is private, and the reference occurs in a
861 member or friend of class N, or
863 --m as a member of N is protected, and the reference occurs in a
864 member or friend of class N, or in a member or friend of a
865 class P derived from N, where m as a member of P is private or
868 --there exists a base class B of N that is accessible at the point
869 of reference, and m is accessible when named in class B.
871 We walk the base class hierarchy, checking these conditions. */
873 /* Figure out where the reference is occurring. Check to see if
874 DECL is private or protected in this scope, since that will
875 determine whether protected access in TYPE allowed. */
876 if (current_class_type
877 && DERIVED_FROM_P (type
, current_class_type
))
879 tree access
= access_in_type (current_class_type
, decl
);
880 if (same_type_p (current_class_type
, type
)
881 && access
== access_private_node
)
883 else if (access
&& (access
== access_private_node
884 || access
== access_protected_node
))
888 /* Now, loop through the classes of which SCOPE is a friend. */
889 if (!protected_ok
&& scope
)
891 /* FIXME: Implement this. Right now, we have no way of knowing
892 which classes befriend a particular function or class. */
897 When a friend or a member function of a derived class references
898 a protected nonstatic member of a base class, an access check
899 applies in addition to those described earlier in clause
900 _class.access_.4) Except when forming a pointer to member
901 (_expr.unary.op_), the access must be through a pointer to,
902 reference to, or object of the derived class itself (or any class
903 derived from that class) (_expr.ref_). If the access is to form
904 a pointer to member, the nested-name-specifier shall name the
905 derived class (or any class derived from that class). */
906 if (protected_ok
&& DECL_NONSTATIC_MEMBER_P (decl
))
908 /* We can tell through what the reference is occurring by
909 chasing BINFO up to the root. */
911 while (BINFO_INHERITANCE_CHAIN (t
))
912 t
= BINFO_INHERITANCE_CHAIN (t
);
914 if (!DERIVED_FROM_P (current_class_type
, BINFO_TYPE (t
)))
918 /* Standardize on the same that will access_in_type will use. We
919 don't need to know what path was chosen from this point onwards. */
920 binfo
= TYPE_BINFO (type
);
922 /* Compute the accessibility of DECL in the class hierarchy
923 dominated by type. */
924 access_in_type (type
, decl
);
925 /* Walk the hierarchy again, looking for a base class that allows
927 t
= dfs_walk (binfo
, dfs_accessible_p
,
928 dfs_accessible_queue_p
,
929 protected_ok
? &protected_ok
: 0);
930 /* Clear any mark bits. */
931 dfs_walk (binfo
, dfs_unmark
, shared_marked_p
, 0);
933 return t
!= NULL_TREE
;
936 /* Routine to see if the sub-object denoted by the binfo PARENT can be
937 found as a base class and sub-object of the object denoted by
938 BINFO. This routine relies upon binfos not being shared, except
939 for binfos for virtual bases. */
942 is_subobject_of_p (parent
, binfo
)
945 tree binfos
= BINFO_BASETYPES (binfo
);
946 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
948 if (TREE_VIA_VIRTUAL (parent
))
949 parent
= TYPE_BINFO (TREE_TYPE (parent
));
950 if (TREE_VIA_VIRTUAL (binfo
))
951 binfo
= TYPE_BINFO (TREE_TYPE (binfo
));
956 /* Process and/or queue base types. */
957 for (i
= 0; i
< n_baselinks
; i
++)
959 tree base_binfo
= canonical_binfo (TREE_VEC_ELT (binfos
, i
));
960 if (is_subobject_of_p (parent
, base_binfo
))
966 /* See if a one FIELD_DECL hides another. This routine is meant to
967 correspond to ANSI working paper Sept 17, 1992 10p4. The two
968 binfos given are the binfos corresponding to the particular places
969 the FIELD_DECLs are found. This routine relies upon binfos not
970 being shared, except for virtual bases. */
973 hides (hider_binfo
, hidee_binfo
)
974 tree hider_binfo
, hidee_binfo
;
976 /* hider hides hidee, if hider has hidee as a base class and
977 the instance of hidee is a sub-object of hider. The first
978 part is always true is the second part is true.
980 When hider and hidee are the same (two ways to get to the exact
981 same member) we consider either one as hiding the other. */
982 return is_subobject_of_p (hidee_binfo
, hider_binfo
);
985 /* Very similar to lookup_fnfields_1 but it ensures that at least one
986 function was declared inside the class given by TYPE. It really should
987 only return functions that match the given TYPE. */
990 lookup_fnfields_here (type
, name
)
993 int idx
= lookup_fnfields_1 (type
, name
);
996 /* ctors and dtors are always only in the right class. */
999 fndecls
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1002 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls
)))
1003 == TYPE_MAIN_VARIANT (type
))
1005 fndecls
= OVL_CHAIN (fndecls
);
1010 struct lookup_field_info
{
1011 /* The type in which we're looking. */
1013 /* The name of the field for which we're looking. */
1015 /* If non-NULL, the current result of the lookup. */
1017 /* The path to RVAL. */
1019 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1022 /* If non-zero, we are looking for types, not data members. */
1024 /* If non-zero, RVAL was found by looking through a dependent base. */
1025 int from_dep_base_p
;
1026 /* If something went wrong, a message indicating what. */
1030 /* Returns non-zero if BINFO is not hidden by the value found by the
1031 lookup so far. If BINFO is hidden, then there's no need to look in
1032 it. DATA is really a struct lookup_field_info. Called from
1033 lookup_field via breadth_first_search. */
1036 lookup_field_queue_p (binfo
, data
)
1040 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1042 /* Don't look for constructors or destructors in base classes. */
1043 if (lfi
->name
== ctor_identifier
|| lfi
->name
== dtor_identifier
)
1046 /* If this base class is hidden by the best-known value so far, we
1047 don't need to look. */
1048 if (!lfi
->from_dep_base_p
&& lfi
->rval_binfo
1049 && hides (lfi
->rval_binfo
, binfo
))
1052 if (TREE_VIA_VIRTUAL (binfo
))
1053 return binfo_member (BINFO_TYPE (binfo
),
1054 CLASSTYPE_VBASECLASSES (lfi
->type
));
1059 /* DATA is really a struct lookup_field_info. Look for a field with
1060 the name indicated there in BINFO. If this function returns a
1061 non-NULL value it is the result of the lookup. Called from
1062 lookup_field via breadth_first_search. */
1065 lookup_field_r (binfo
, data
)
1069 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1070 tree type
= BINFO_TYPE (binfo
);
1073 int from_dep_base_p
;
1075 /* First, look for a function. There can't be a function and a data
1076 member with the same name, and if there's a function and a type
1077 with the same name, the type is hidden by the function. */
1078 idx
= lookup_fnfields_here (type
, lfi
->name
);
1080 nval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1082 /* Look for a data member or type. */
1083 nval
= lookup_field_1 (type
, lfi
->name
);
1085 /* If there is no declaration with the indicated name in this type,
1086 then there's nothing to do. */
1090 from_dep_base_p
= dependent_base_p (binfo
);
1091 if (lfi
->from_dep_base_p
&& !from_dep_base_p
)
1093 /* If the new declaration is not found via a dependent base, and
1094 the old one was, then we must prefer the new one. We weren't
1095 really supposed to be able to find the old one, so we don't
1096 want to be affected by a specialization. Consider:
1098 struct B { typedef int I; };
1099 template <typename T> struct D1 : virtual public B {};
1100 template <typename T> struct D :
1101 public D1, virtual pubic B { I i; };
1103 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1104 D1 is specialized. */
1105 lfi
->from_dep_base_p
= 0;
1106 lfi
->rval
= NULL_TREE
;
1107 lfi
->rval_binfo
= NULL_TREE
;
1108 lfi
->ambiguous
= NULL_TREE
;
1111 else if (lfi
->rval_binfo
&& !lfi
->from_dep_base_p
&& from_dep_base_p
)
1112 /* Similarly, if the old declaration was not found via a dependent
1113 base, and the new one is, ignore the new one. */
1116 /* If the lookup already found a match, and the new value doesn't
1117 hide the old one, we might have an ambiguity. */
1118 if (lfi
->rval_binfo
&& !hides (binfo
, lfi
->rval_binfo
))
1120 if (nval
== lfi
->rval
&& SHARED_MEMBER_P (nval
))
1121 /* The two things are really the same. */
1123 else if (hides (lfi
->rval_binfo
, binfo
))
1124 /* The previous value hides the new one. */
1128 /* We have a real ambiguity. We keep a chain of all the
1130 if (!lfi
->ambiguous
&& lfi
->rval
)
1131 /* This is the first time we noticed an ambiguity. Add
1132 what we previously thought was a reasonable candidate
1134 lfi
->ambiguous
= scratch_tree_cons (NULL_TREE
, lfi
->rval
,
1136 /* Add the new value. */
1137 lfi
->ambiguous
= scratch_tree_cons (NULL_TREE
, nval
,
1139 lfi
->errstr
= "request for member `%D' is ambiguous";
1144 /* The new lookup is the best we've got so far. Verify that
1145 it's the kind of thing we're looking for. */
1146 if (lfi
->want_type
&& TREE_CODE (nval
) != TYPE_DECL
)
1148 nval
= purpose_member (lfi
->name
, CLASSTYPE_TAGS (type
));
1150 nval
= TYPE_MAIN_DECL (TREE_VALUE (nval
));
1155 /* If the thing we're looking for is a virtual base class,
1156 then we know we've got what we want at this point;
1157 there's no way to get an ambiguity. */
1158 if (VBASE_NAME_P (lfi
->name
))
1164 if (from_dep_base_p
&& TREE_CODE (nval
) != TYPE_DECL
1165 /* We need to return a member template class so we can
1166 define partial specializations. Is there a better
1168 && !DECL_CLASS_TEMPLATE_P (nval
))
1169 /* The thing we're looking for isn't a type, so the implicit
1170 typename extension doesn't apply, so we just pretend we
1171 didn't find anything. */
1176 lfi
->from_dep_base_p
= from_dep_base_p
;
1177 lfi
->rval_binfo
= binfo
;
1183 /* Look for a memer named NAME in an inheritance lattice dominated by
1184 XBASETYPE. PROTECT is zero if we can avoid computing access
1185 information, otherwise it is 1. WANT_TYPE is 1 when we should only
1186 return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
1188 It was not clear what should happen if WANT_TYPE is set, and an
1189 ambiguity is found. At least one use (lookup_name) to not see
1193 lookup_member (xbasetype
, name
, protect
, want_type
)
1194 register tree xbasetype
, name
;
1195 int protect
, want_type
;
1197 tree rval
, rval_binfo
= NULL_TREE
;
1198 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1199 struct lookup_field_info lfi
;
1201 /* rval_binfo is the binfo associated with the found member, note,
1202 this can be set with useful information, even when rval is not
1203 set, because it must deal with ALL members, not just non-function
1204 members. It is used for ambiguity checking and the hidden
1205 checks. Whereas rval is only set if a proper (not hidden)
1206 non-function member is found. */
1208 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1209 hiding checks, as virtual base classes may not be shared. The strategy
1210 is we always go into the binfo hierarchy owned by TYPE_BINFO of
1211 virtual base classes, as we cross virtual base class lines. This way
1212 we know that binfo of a virtual base class will always == itself when
1213 found along any line. (mrs) */
1217 if (xbasetype
== current_class_type
&& TYPE_BEING_DEFINED (xbasetype
)
1218 && IDENTIFIER_CLASS_VALUE (name
))
1220 tree field
= IDENTIFIER_CLASS_VALUE (name
);
1221 if (TREE_CODE (field
) != FUNCTION_DECL
1222 && ! (want_type
&& TREE_CODE (field
) != TYPE_DECL
))
1226 if (TREE_CODE (xbasetype
) == TREE_VEC
)
1228 type
= BINFO_TYPE (xbasetype
);
1229 basetype_path
= xbasetype
;
1231 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype
)))
1234 basetype_path
= TYPE_BINFO (type
);
1235 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
,
1239 my_friendly_abort (97);
1241 complete_type (type
);
1243 #ifdef GATHER_STATISTICS
1244 n_calls_lookup_field
++;
1245 #endif /* GATHER_STATISTICS */
1247 bzero (&lfi
, sizeof (lfi
));
1250 lfi
.want_type
= want_type
;
1251 bfs_walk (basetype_path
, &lookup_field_r
, &lookup_field_queue_p
, &lfi
);
1253 rval_binfo
= lfi
.rval_binfo
;
1255 type
= BINFO_TYPE (rval_binfo
);
1256 errstr
= lfi
.errstr
;
1258 /* If we are not interested in ambiguities, don't report them;
1259 just return NULL_TREE. */
1260 if (!protect
&& lfi
.ambiguous
)
1265 In the case of overloaded function names, access control is
1266 applied to the function selected by overloaded resolution. */
1267 if (rval
&& protect
&& !is_overloaded_fn (rval
)
1268 && !IS_SIGNATURE_POINTER (DECL_REAL_CONTEXT (rval
))
1269 && !IS_SIGNATURE_REFERENCE (DECL_REAL_CONTEXT (rval
))
1270 && !enforce_access (xbasetype
, rval
))
1271 return error_mark_node
;
1273 if (errstr
&& protect
)
1275 cp_error (errstr
, name
, type
);
1277 print_candidates (lfi
.ambiguous
);
1278 rval
= error_mark_node
;
1281 /* If the thing we found was found via the implicit typename
1282 extension, build the typename type. */
1283 if (rval
&& lfi
.from_dep_base_p
&& !DECL_CLASS_TEMPLATE_P (rval
))
1284 rval
= TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path
),
1288 if (rval
&& is_overloaded_fn (rval
))
1289 rval
= scratch_tree_cons (basetype_path
, rval
, NULL_TREE
);
1294 /* Like lookup_member, except that if we find a function member we
1295 return NULL_TREE. */
1298 lookup_field (xbasetype
, name
, protect
, want_type
)
1299 register tree xbasetype
, name
;
1300 int protect
, want_type
;
1302 tree rval
= lookup_member (xbasetype
, name
, protect
, want_type
);
1304 /* Ignore functions. */
1305 if (rval
&& TREE_CODE (rval
) == TREE_LIST
)
1311 /* Like lookup_member, except that if we find a non-function member we
1312 return NULL_TREE. */
1315 lookup_fnfields (xbasetype
, name
, protect
)
1316 register tree xbasetype
, name
;
1319 tree rval
= lookup_member (xbasetype
, name
, protect
, /*want_type=*/0);
1321 /* Ignore non-functions. */
1322 if (rval
&& TREE_CODE (rval
) != TREE_LIST
)
1328 /* Try to find NAME inside a nested class. */
1331 lookup_nested_field (name
, complain
)
1337 tree id
= NULL_TREE
;
1338 if (TYPE_MAIN_DECL (current_class_type
))
1340 /* Climb our way up the nested ladder, seeing if we're trying to
1341 modify a field in an enclosing class. If so, we should only
1342 be able to modify if it's static. */
1343 for (t
= TYPE_MAIN_DECL (current_class_type
);
1344 t
&& DECL_CONTEXT (t
);
1345 t
= TYPE_MAIN_DECL (DECL_CONTEXT (t
)))
1347 if (TREE_CODE (DECL_CONTEXT (t
)) != RECORD_TYPE
)
1350 /* N.B.: lookup_field will do the access checking for us */
1351 id
= lookup_field (DECL_CONTEXT (t
), name
, complain
, 0);
1352 if (id
== error_mark_node
)
1358 if (id
!= NULL_TREE
)
1360 if (TREE_CODE (id
) == FIELD_DECL
1361 && ! TREE_STATIC (id
)
1362 && TREE_TYPE (id
) != error_mark_node
)
1366 /* At parse time, we don't want to give this error, since
1367 we won't have enough state to make this kind of
1368 decision properly. But there are times (e.g., with
1369 enums in nested classes) when we do need to call
1370 this fn at parse time. So, in those cases, we pass
1371 complain as a 0 and just return a NULL_TREE. */
1372 cp_error ("assignment to non-static member `%D' of enclosing class `%T'",
1373 id
, DECL_CONTEXT (t
));
1374 /* Mark this for do_identifier(). It would otherwise
1375 claim that the variable was undeclared. */
1376 TREE_TYPE (id
) = error_mark_node
;
1392 /* TYPE is a class type. Return the index of the fields within
1393 the method vector with name NAME, or -1 is no such field exists. */
1396 lookup_fnfields_1 (type
, name
)
1399 register tree method_vec
1400 = CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
1402 if (method_vec
!= 0)
1404 register tree
*methods
= &TREE_VEC_ELT (method_vec
, 0);
1405 register tree
*end
= TREE_VEC_END (method_vec
);
1407 #ifdef GATHER_STATISTICS
1408 n_calls_lookup_fnfields_1
++;
1409 #endif /* GATHER_STATISTICS */
1411 /* Constructors are first... */
1412 if (*methods
&& name
== ctor_identifier
)
1415 /* and destructors are second. */
1416 if (*++methods
&& name
== dtor_identifier
)
1419 while (++methods
!= end
&& *methods
)
1421 #ifdef GATHER_STATISTICS
1422 n_outer_fields_searched
++;
1423 #endif /* GATHER_STATISTICS */
1424 if (DECL_NAME (OVL_CURRENT (*methods
)) == name
)
1428 /* If we didn't find it, it might have been a template
1429 conversion operator. (Note that we don't look for this case
1430 above so that we will always find specializations first.) */
1431 if ((methods
== end
|| !*methods
)
1432 && IDENTIFIER_TYPENAME_P (name
))
1434 methods
= &TREE_VEC_ELT (method_vec
, 0) + 1;
1436 while (++methods
!= end
&& *methods
)
1438 tree method_name
= DECL_NAME (OVL_CURRENT (*methods
));
1440 if (!IDENTIFIER_TYPENAME_P (method_name
))
1442 /* Since all conversion operators come first, we know
1443 there is no such operator. */
1447 else if (TREE_CODE (OVL_CURRENT (*methods
)) == TEMPLATE_DECL
)
1452 if (methods
!= end
&& *methods
)
1453 return methods
- &TREE_VEC_ELT (method_vec
, 0);
1459 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1460 type in the hierarchy, in a breadth-first preorder traversal. .
1461 If it ever returns a non-NULL value, that value is immediately
1462 returned and the walk is terminated. At each node FN, is passed a
1463 BINFO indicating the path from the curently visited base-class to
1464 TYPE. The TREE_CHAINs of the BINFOs may be used for scratch space;
1465 they are otherwise unused. Before each base-class is walked QFN is
1466 called. If the value returned is non-zero, the base-class is
1467 walked; otherwise it is not. If QFN is NULL, it is treated as a
1468 function which always returns 1. Both FN and QFN are passed the
1469 DATA whenever they are called. */
1472 bfs_walk (binfo
, fn
, qfn
, data
)
1474 tree (*fn
) PROTO((tree
, void *));
1475 tree (*qfn
) PROTO((tree
, void *));
1480 tree rval
= NULL_TREE
;
1481 /* An array of the base classes of BINFO. These will be built up in
1482 breadth-first order, except where QFN prunes the search. */
1483 varray_type bfs_bases
;
1485 /* Start with enough room for ten base classes. That will be enough
1486 for most hierarchies. */
1487 VARRAY_TREE_INIT (bfs_bases
, 10, "search_stack");
1489 /* Put the first type into the stack. */
1490 VARRAY_TREE (bfs_bases
, 0) = binfo
;
1493 for (head
= 0; head
< tail
; ++head
)
1499 /* Pull the next type out of the queue. */
1500 binfo
= VARRAY_TREE (bfs_bases
, head
);
1502 /* If this is the one we're looking for, we're done. */
1503 rval
= (*fn
) (binfo
, data
);
1507 /* Queue up the base types. */
1508 binfos
= BINFO_BASETYPES (binfo
);
1509 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1510 for (i
= 0; i
< n_baselinks
; i
++)
1512 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1515 base_binfo
= (*qfn
) (base_binfo
, data
);
1519 if (tail
== VARRAY_SIZE (bfs_bases
))
1520 VARRAY_GROW (bfs_bases
, 2 * VARRAY_SIZE (bfs_bases
));
1521 VARRAY_TREE (bfs_bases
, tail
) = base_binfo
;
1528 VARRAY_FREE (bfs_bases
);
1533 /* Exactly like bfs_walk, except that a depth-first traversal is
1534 performed, and PREFN is called in preorder, while POSTFN is called
1538 dfs_walk_real (binfo
, prefn
, postfn
, qfn
, data
)
1540 tree (*prefn
) PROTO((tree
, void *));
1541 tree (*postfn
) PROTO((tree
, void *));
1542 tree (*qfn
) PROTO((tree
, void *));
1548 tree rval
= NULL_TREE
;
1550 /* Call the pre-order walking function. */
1553 rval
= (*prefn
) (binfo
, data
);
1558 /* Process the basetypes. */
1559 binfos
= BINFO_BASETYPES (binfo
);
1560 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1561 for (i
= 0; i
< n_baselinks
; i
++)
1563 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1566 base_binfo
= (*qfn
) (base_binfo
, data
);
1570 rval
= dfs_walk_real (base_binfo
, prefn
, postfn
, qfn
, data
);
1576 /* Call the post-order walking function. */
1578 rval
= (*postfn
) (binfo
, data
);
1583 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1587 dfs_walk (binfo
, fn
, qfn
, data
)
1589 tree (*fn
) PROTO((tree
, void *));
1590 tree (*qfn
) PROTO((tree
, void *));
1593 return dfs_walk_real (binfo
, 0, fn
, qfn
, data
);
1598 /* The name of the function we are looking for. */
1600 /* The overloaded functions we have found. */
1604 /* Called from get_virtuals_named_this via bfs_walk. */
1607 get_virtuals_named_this_r (binfo
, data
)
1611 struct gvnt_info
*gvnti
= (struct gvnt_info
*) data
;
1612 tree type
= BINFO_TYPE (binfo
);
1615 idx
= lookup_fnfields_here (BINFO_TYPE (binfo
), gvnti
->name
);
1618 = scratch_tree_cons (binfo
,
1619 TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
),
1626 /* Return the virtual functions with the indicated NAME in the type
1627 indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE
1628 indicates the base class from which the TREE_VALUE (an OVERLOAD or
1629 just a FUNCTION_DECL) originated. */
1632 get_virtuals_named_this (binfo
, name
)
1636 struct gvnt_info gvnti
;
1640 gvnti
.fields
= NULL_TREE
;
1642 bfs_walk (binfo
, get_virtuals_named_this_r
, 0, &gvnti
);
1644 /* Get to the function decls, and return the first virtual function
1645 with this name, if there is one. */
1646 for (fields
= gvnti
.fields
; fields
; fields
= next_baselink (fields
))
1650 for (fndecl
= TREE_VALUE (fields
); fndecl
; fndecl
= OVL_NEXT (fndecl
))
1651 if (DECL_VINDEX (OVL_CURRENT (fndecl
)))
1658 get_virtual_destructor (binfo
, data
)
1660 void *data ATTRIBUTE_UNUSED
;
1662 tree type
= BINFO_TYPE (binfo
);
1663 if (TYPE_HAS_DESTRUCTOR (type
)
1664 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1)))
1665 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1);
1670 tree_has_any_destructor_p (binfo
, data
)
1672 void *data ATTRIBUTE_UNUSED
;
1674 tree type
= BINFO_TYPE (binfo
);
1675 return TYPE_NEEDS_DESTRUCTOR (type
) ? binfo
: NULL_TREE
;
1678 /* Returns > 0 if a function with type DRETTYPE overriding a function
1679 with type BRETTYPE is covariant, as defined in [class.virtual].
1681 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1682 adjustment), or -1 if pedantically invalid covariance. */
1685 covariant_return_p (brettype
, drettype
)
1686 tree brettype
, drettype
;
1690 if (TREE_CODE (brettype
) == FUNCTION_DECL
1691 || TREE_CODE (brettype
) == THUNK_DECL
)
1693 brettype
= TREE_TYPE (TREE_TYPE (brettype
));
1694 drettype
= TREE_TYPE (TREE_TYPE (drettype
));
1696 else if (TREE_CODE (brettype
) == METHOD_TYPE
)
1698 brettype
= TREE_TYPE (brettype
);
1699 drettype
= TREE_TYPE (drettype
);
1702 if (same_type_p (brettype
, drettype
))
1705 if (! (TREE_CODE (brettype
) == TREE_CODE (drettype
)
1706 && (TREE_CODE (brettype
) == POINTER_TYPE
1707 || TREE_CODE (brettype
) == REFERENCE_TYPE
)
1708 && TYPE_QUALS (brettype
) == TYPE_QUALS (drettype
)))
1711 if (! can_convert (brettype
, drettype
))
1714 brettype
= TREE_TYPE (brettype
);
1715 drettype
= TREE_TYPE (drettype
);
1717 /* If not pedantic, allow any standard pointer conversion. */
1718 if (! IS_AGGR_TYPE (drettype
) || ! IS_AGGR_TYPE (brettype
))
1721 binfo
= get_binfo (brettype
, drettype
, 1);
1723 /* If we get an error_mark_node from get_binfo, it already complained,
1724 so let's just succeed. */
1725 if (binfo
== error_mark_node
)
1728 if (! BINFO_OFFSET_ZEROP (binfo
) || TREE_VIA_VIRTUAL (binfo
))
1733 /* Given a class type TYPE, and a function decl FNDECL, look for a
1734 virtual function in TYPE's hierarchy which FNDECL could match as a
1735 virtual function. It doesn't matter which one we find.
1737 DTORP is nonzero if we are looking for a destructor. Destructors
1738 need special treatment because they do not match by name. */
1741 get_matching_virtual (binfo
, fndecl
, dtorp
)
1745 tree tmp
= NULL_TREE
;
1748 if (TREE_CODE (fndecl
) == TEMPLATE_DECL
)
1749 /* In [temp.mem] we have:
1751 A specialization of a member function template does not
1752 override a virtual function from a base class. */
1755 /* Breadth first search routines start searching basetypes
1756 of TYPE, so we must perform first ply of search here. */
1758 return bfs_walk (binfo
, get_virtual_destructor
,
1759 tree_has_any_destructor_p
, 0);
1762 tree drettype
, dtypes
, btypes
, instptr_type
;
1763 tree basetype
= DECL_CLASS_CONTEXT (fndecl
);
1764 tree baselink
, best
= NULL_TREE
;
1765 tree name
= DECL_ASSEMBLER_NAME (fndecl
);
1766 tree declarator
= DECL_NAME (fndecl
);
1767 if (IDENTIFIER_VIRTUAL_P (declarator
) == 0)
1770 baselink
= get_virtuals_named_this (binfo
, declarator
);
1771 if (baselink
== NULL_TREE
)
1774 drettype
= TREE_TYPE (TREE_TYPE (fndecl
));
1775 dtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1776 if (DECL_STATIC_FUNCTION_P (fndecl
))
1777 instptr_type
= NULL_TREE
;
1779 instptr_type
= TREE_TYPE (TREE_VALUE (dtypes
));
1781 for (; baselink
; baselink
= next_baselink (baselink
))
1784 for (tmps
= TREE_VALUE (baselink
); tmps
; tmps
= OVL_NEXT (tmps
))
1786 tmp
= OVL_CURRENT (tmps
);
1787 if (! DECL_VINDEX (tmp
))
1790 btypes
= TYPE_ARG_TYPES (TREE_TYPE (tmp
));
1791 if (instptr_type
== NULL_TREE
)
1793 if (compparms (TREE_CHAIN (btypes
), dtypes
))
1794 /* Caller knows to give error in this case. */
1799 if (/* The first parameter is the `this' parameter,
1800 which has POINTER_TYPE, and we can therefore
1801 safely use TYPE_QUALS, rather than
1803 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes
)))
1804 == TYPE_QUALS (instptr_type
))
1805 && compparms (TREE_CHAIN (btypes
), TREE_CHAIN (dtypes
)))
1807 tree brettype
= TREE_TYPE (TREE_TYPE (tmp
));
1808 if (same_type_p (brettype
, drettype
))
1810 else if ((i
= covariant_return_p (brettype
, drettype
)))
1813 sorry ("adjusting pointers for covariant returns");
1815 if (pedantic
&& i
== -1)
1817 cp_pedwarn_at ("invalid covariant return type for `%#D' (must be pointer or reference to class)", fndecl
);
1818 cp_pedwarn_at (" overriding `%#D'", tmp
);
1821 else if (IS_AGGR_TYPE_2 (brettype
, drettype
)
1822 && same_or_base_type_p (brettype
, drettype
))
1824 error ("invalid covariant return type (must use pointer or reference)");
1825 cp_error_at (" overriding `%#D'", tmp
);
1826 cp_error_at (" with `%#D'", fndecl
);
1828 else if (IDENTIFIER_ERROR_LOCUS (name
) == NULL_TREE
)
1830 cp_error_at ("conflicting return type specified for virtual function `%#D'", fndecl
);
1831 cp_error_at (" overriding definition as `%#D'", tmp
);
1832 SET_IDENTIFIER_ERROR_LOCUS (name
, basetype
);
1835 /* FNDECL overrides this function. We continue to
1836 check all the other functions in order to catch
1837 errors; it might be that in some other baseclass
1838 a virtual function was declared with the same
1839 parameter types, but a different return type. */
1849 /* Return the list of virtual functions which are abstract in type
1850 TYPE that come from non virtual base classes. See
1851 expand_direct_vtbls_init for the style of search we do. */
1854 get_abstract_virtuals_1 (binfo
, do_self
, abstract_virtuals
)
1857 tree abstract_virtuals
;
1859 tree binfos
= BINFO_BASETYPES (binfo
);
1860 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1862 for (i
= 0; i
< n_baselinks
; i
++)
1864 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1865 int is_not_base_vtable
1866 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
1867 if (! TREE_VIA_VIRTUAL (base_binfo
))
1869 = get_abstract_virtuals_1 (base_binfo
, is_not_base_vtable
,
1872 /* Should we use something besides CLASSTYPE_VFIELDS? */
1873 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
1875 tree virtuals
= BINFO_VIRTUALS (binfo
);
1877 skip_rtti_stuff (&virtuals
);
1881 tree base_pfn
= FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals
));
1882 tree base_fndecl
= TREE_OPERAND (base_pfn
, 0);
1883 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl
))
1884 abstract_virtuals
= tree_cons (NULL_TREE
, base_fndecl
, abstract_virtuals
);
1885 virtuals
= TREE_CHAIN (virtuals
);
1888 return abstract_virtuals
;
1891 /* Return the list of virtual functions which are abstract in type TYPE.
1892 This information is cached, and so must be built on a
1893 non-temporary obstack. */
1896 get_abstract_virtuals (type
)
1900 tree abstract_virtuals
= NULL
;
1902 /* First get all from non-virtual bases. */
1904 = get_abstract_virtuals_1 (TYPE_BINFO (type
), 1, abstract_virtuals
);
1906 for (vbases
= CLASSTYPE_VBASECLASSES (type
); vbases
; vbases
= TREE_CHAIN (vbases
))
1908 tree virtuals
= BINFO_VIRTUALS (vbases
);
1910 skip_rtti_stuff (&virtuals
);
1914 tree base_pfn
= FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals
));
1915 tree base_fndecl
= TREE_OPERAND (base_pfn
, 0);
1916 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl
))
1917 cp_error ("`%#D' needs a final overrider", base_fndecl
);
1918 else if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl
))
1919 abstract_virtuals
= tree_cons (NULL_TREE
, base_fndecl
, abstract_virtuals
);
1920 virtuals
= TREE_CHAIN (virtuals
);
1923 return nreverse (abstract_virtuals
);
1926 /* For the type TYPE, return a list of member functions available from
1927 base classes with name NAME. The TREE_VALUE of the list is a chain of
1928 member functions with name NAME. The TREE_PURPOSE of the list is a
1929 basetype, or a list of base types (in reverse order) which were
1930 traversed to reach the chain of member functions. If we reach a base
1931 type which provides a member function of name NAME, and which has at
1932 most one base type itself, then we can terminate the search. */
1935 get_baselinks (type_as_binfo_list
, type
, name
)
1936 tree type_as_binfo_list
;
1939 int head
= 0, tail
= 0, idx
;
1940 tree rval
= 0, nval
= 0;
1941 tree basetypes
= type_as_binfo_list
;
1942 tree binfo
= TYPE_BINFO (type
);
1944 search_stack
= push_search_level (search_stack
, &search_obstack
);
1948 tree binfos
= BINFO_BASETYPES (binfo
);
1949 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1951 /* Process and/or queue base types. */
1952 for (i
= 0; i
< n_baselinks
; i
++)
1954 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1957 btypes
= hash_tree_cons (TREE_VIA_PUBLIC (base_binfo
),
1958 TREE_VIA_VIRTUAL (base_binfo
),
1959 TREE_VIA_PROTECTED (base_binfo
),
1960 NULL_TREE
, base_binfo
,
1962 obstack_ptr_grow (&search_obstack
, btypes
);
1963 search_stack
->first
= (tree
*)obstack_base (&search_obstack
);
1968 /* Process head of queue, if one exists. */
1972 basetypes
= search_stack
->first
[head
++];
1973 binfo
= TREE_VALUE (basetypes
);
1974 type
= BINFO_TYPE (binfo
);
1975 idx
= lookup_fnfields_1 (type
, name
);
1978 nval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1979 rval
= hash_tree_cons (0, 0, 0, basetypes
, nval
, rval
);
1980 if (TYPE_BINFO_BASETYPES (type
) == 0)
1982 else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type
)) == 1)
1984 if (CLASSTYPE_BASELINK_VEC (type
))
1985 TREE_TYPE (rval
) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type
), idx
);
1992 search_stack
= pop_search_level (search_stack
);
1997 next_baselink (baselink
)
2000 tree tmp
= TREE_TYPE (baselink
);
2001 baselink
= TREE_CHAIN (baselink
);
2004 /* @@ does not yet add previous base types. */
2005 baselink
= tree_cons (TREE_PURPOSE (tmp
), TREE_VALUE (tmp
),
2007 TREE_TYPE (baselink
) = TREE_TYPE (tmp
);
2008 tmp
= TREE_CHAIN (tmp
);
2013 /* DEPTH-FIRST SEARCH ROUTINES. */
2015 /* This routine converts a pointer to be a pointer of an immediate
2016 base class. The normal convert_pointer_to routine would diagnose
2017 the conversion as ambiguous, under MI code that has the base class
2018 as an ambiguous base class. */
2021 convert_pointer_to_single_level (to_type
, expr
)
2024 tree binfo_of_derived
;
2027 binfo_of_derived
= TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr
)));
2028 last
= get_binfo (to_type
, TREE_TYPE (TREE_TYPE (expr
)), 0);
2029 my_friendly_assert (BINFO_INHERITANCE_CHAIN (last
) == binfo_of_derived
,
2031 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived
) == NULL_TREE
,
2033 return build_vbase_path (PLUS_EXPR
, build_pointer_type (to_type
), expr
,
2037 /* Like dfs_walk, but only walk until fn returns something, and return
2038 that. We also use the real vbase binfos instead of the placeholders
2039 in the normal binfo hierarchy. START is the most-derived type for this
2040 hierarchy, so that we can find the vbase binfos. */
2043 dfs_search (binfo
, fn
, start
)
2045 tree (*fn
) PROTO((tree
));
2047 tree binfos
= BINFO_BASETYPES (binfo
);
2048 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2051 for (i
= 0; i
< n_baselinks
; i
++)
2053 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2055 if (TREE_CODE (BINFO_TYPE (base_binfo
)) == TEMPLATE_TYPE_PARM
2056 || TREE_CODE (BINFO_TYPE (base_binfo
)) == TEMPLATE_TEMPLATE_PARM
)
2060 if (TREE_VIA_VIRTUAL (base_binfo
) && start
)
2061 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
),
2062 CLASSTYPE_VBASECLASSES (start
));
2063 retval
= dfs_search (base_binfo
, fn
, start
);
2072 tree
markedp (binfo
, data
)
2074 void *data ATTRIBUTE_UNUSED
;
2076 return BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2080 unmarkedp (binfo
, data
)
2082 void *data ATTRIBUTE_UNUSED
;
2084 return !BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2088 marked_vtable_pathp (binfo
, data
)
2090 void *data ATTRIBUTE_UNUSED
;
2092 return BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2096 unmarked_vtable_pathp (binfo
, data
)
2098 void *data ATTRIBUTE_UNUSED
;
2100 return !BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2104 marked_new_vtablep (binfo
, data
)
2106 void *data ATTRIBUTE_UNUSED
;
2108 return BINFO_NEW_VTABLE_MARKED (binfo
) ? binfo
: NULL_TREE
;
2112 unmarked_new_vtablep (binfo
, data
)
2114 void *data ATTRIBUTE_UNUSED
;
2116 return !BINFO_NEW_VTABLE_MARKED (binfo
) ? binfo
: NULL_TREE
;
2120 marked_pushdecls_p (binfo
, data
)
2122 void *data ATTRIBUTE_UNUSED
;
2124 return BINFO_PUSHDECLS_MARKED (binfo
) ? binfo
: NULL_TREE
;
2128 unmarked_pushdecls_p (binfo
, data
)
2130 void *data ATTRIBUTE_UNUSED
;
2132 return !BINFO_PUSHDECLS_MARKED (binfo
) ? binfo
: NULL_TREE
;
2136 static int dfs_search_slot_nonempty_p (binfo
) tree binfo
;
2137 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) != 0; }
2141 dfs_debug_unmarkedp (binfo
, data
)
2143 void *data ATTRIBUTE_UNUSED
;
2145 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo
))
2146 ? binfo
: NULL_TREE
);
2149 /* The worker functions for `dfs_walk'. These do not need to
2150 test anything (vis a vis marking) if they are paired with
2151 a predicate function (above). */
2155 dfs_mark (binfo
) tree binfo
;
2156 { SET_BINFO_MARKED (binfo
); }
2160 dfs_unmark (binfo
, data
)
2162 void *data ATTRIBUTE_UNUSED
;
2164 CLEAR_BINFO_MARKED (binfo
);
2170 dfs_mark_vtable_path (binfo
) tree binfo
;
2171 { SET_BINFO_VTABLE_PATH_MARKED (binfo
); }
2174 dfs_unmark_vtable_path (binfo
) tree binfo
;
2175 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
); }
2178 dfs_mark_new_vtable (binfo
) tree binfo
;
2179 { SET_BINFO_NEW_VTABLE_MARKED (binfo
); }
2182 dfs_unmark_new_vtable (binfo
) tree binfo
;
2183 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
); }
2186 dfs_clear_search_slot (binfo
) tree binfo
;
2187 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) = 0; }
2191 dfs_debug_mark (binfo
, data
)
2193 void *data ATTRIBUTE_UNUSED
;
2195 tree t
= BINFO_TYPE (binfo
);
2197 /* Use heuristic that if there are virtual functions,
2198 ignore until we see a non-inline virtual function. */
2199 tree methods
= CLASSTYPE_METHOD_VEC (t
);
2201 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2206 /* If interface info is known, either we've already emitted the debug
2207 info or we don't need to. */
2208 if (CLASSTYPE_INTERFACE_KNOWN (t
))
2211 /* If debug info is requested from this context for this type, supply it.
2212 If debug info is requested from another context for this type,
2213 see if some third context can supply it. */
2214 if (current_function_decl
== NULL_TREE
2215 || DECL_CLASS_CONTEXT (current_function_decl
) != t
)
2217 if (TREE_VEC_ELT (methods
, 1))
2218 methods
= TREE_VEC_ELT (methods
, 1);
2219 else if (TREE_VEC_ELT (methods
, 0))
2220 methods
= TREE_VEC_ELT (methods
, 0);
2222 methods
= TREE_VEC_ELT (methods
, 2);
2223 methods
= OVL_CURRENT (methods
);
2226 if (DECL_VINDEX (methods
)
2227 && DECL_THIS_INLINE (methods
) == 0
2228 && DECL_ABSTRACT_VIRTUAL_P (methods
) == 0)
2230 /* Somebody, somewhere is going to have to define this
2231 virtual function. When they do, they will provide
2232 the debugging info. */
2235 methods
= TREE_CHAIN (methods
);
2238 /* We cannot rely on some alien method to solve our problems,
2239 so we must write out the debug info ourselves. */
2240 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = 0;
2241 rest_of_type_compilation (t
, toplevel_bindings_p ());
2253 /* Attach to the type of the virtual base class, the pointer to the
2254 virtual base class. */
2257 dfs_find_vbases (binfo
, data
)
2261 struct vbase_info
*vi
= (struct vbase_info
*) data
;
2262 tree binfos
= BINFO_BASETYPES (binfo
);
2263 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2265 for (i
= n_baselinks
-1; i
>= 0; i
--)
2267 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2269 if (TREE_VIA_VIRTUAL (base_binfo
)
2270 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo
)) == 0)
2272 tree vbase
= BINFO_TYPE (base_binfo
);
2273 tree binfo
= binfo_member (vbase
, vi
->vbase_types
);
2275 CLASSTYPE_SEARCH_SLOT (vbase
)
2276 = build (PLUS_EXPR
, build_pointer_type (vbase
),
2277 vi
->decl_ptr
, BINFO_OFFSET (binfo
));
2280 SET_BINFO_VTABLE_PATH_MARKED (binfo
);
2281 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
2287 dfs_init_vbase_pointers (binfo
, data
)
2291 struct vbase_info
*vi
= (struct vbase_info
*) data
;
2292 tree type
= BINFO_TYPE (binfo
);
2293 tree fields
= TYPE_FIELDS (type
);
2294 tree this_vbase_ptr
;
2296 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2299 /* See finish_struct_1 for when we can enable this. */
2300 /* If we have a vtable pointer first, skip it. */
2301 if (VFIELD_NAME_P (DECL_NAME (fields
)))
2302 fields
= TREE_CHAIN (fields
);
2305 if (BINFO_INHERITANCE_CHAIN (binfo
))
2307 this_vbase_ptr
= TREE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo
));
2308 if (TREE_VIA_VIRTUAL (binfo
))
2309 this_vbase_ptr
= CLASSTYPE_SEARCH_SLOT (type
);
2311 this_vbase_ptr
= convert_pointer_to_single_level (type
,
2313 TREE_CHAIN (binfo
) = this_vbase_ptr
;
2316 this_vbase_ptr
= TREE_CHAIN (binfo
);
2318 if (fields
== NULL_TREE
2319 || DECL_NAME (fields
) == NULL_TREE
2320 || ! VBASE_NAME_P (DECL_NAME (fields
)))
2323 if (build_pointer_type (type
)
2324 != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr
)))
2325 my_friendly_abort (125);
2327 while (fields
&& DECL_NAME (fields
) && VBASE_NAME_P (DECL_NAME (fields
)))
2329 tree ref
= build (COMPONENT_REF
, TREE_TYPE (fields
),
2330 build_indirect_ref (this_vbase_ptr
, NULL_PTR
), fields
);
2331 tree init
= CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields
)));
2332 vi
->inits
= tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields
)),
2334 build_modify_expr (ref
, NOP_EXPR
, init
),
2336 fields
= TREE_CHAIN (fields
);
2342 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2343 times, just NEW_VTABLE, but optimizer should make both with equal
2344 efficiency (though it does not currently). */
2347 dfs_clear_vbase_slots (binfo
, data
)
2349 void *data ATTRIBUTE_UNUSED
;
2351 tree type
= BINFO_TYPE (binfo
);
2352 CLASSTYPE_SEARCH_SLOT (type
) = 0;
2353 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2354 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2359 init_vbase_pointers (type
, decl_ptr
)
2363 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
2365 struct vbase_info vi
;
2366 int old_flag
= flag_this_is_variable
;
2367 tree binfo
= TYPE_BINFO (type
);
2368 flag_this_is_variable
= -2;
2370 /* Find all the virtual base classes, marking them for later
2372 vi
.decl_ptr
= decl_ptr
;
2373 vi
.vbase_types
= CLASSTYPE_VBASECLASSES (type
);
2374 vi
.inits
= NULL_TREE
;
2376 dfs_walk (binfo
, dfs_find_vbases
, unmarked_vtable_pathp
, &vi
);
2378 /* Build up a list of the initializers. */
2379 TREE_CHAIN (binfo
) = decl_ptr
;
2380 dfs_walk_real (binfo
,
2381 dfs_init_vbase_pointers
, 0,
2382 marked_vtable_pathp
,
2385 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
, 0);
2386 flag_this_is_variable
= old_flag
;
2392 /* get the virtual context (the vbase that directly contains the
2393 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2394 or NULL_TREE if there is none.
2396 FNDECL must come from a virtual table from a virtual base to ensure that
2397 there is only one possible DECL_CLASS_CONTEXT.
2399 We know that if there is more than one place (binfo) the fndecl that the
2400 declared, they all refer to the same binfo. See get_class_offset_1 for
2401 the check that ensures this. */
2404 virtual_context (fndecl
, t
, vbase
)
2405 tree fndecl
, t
, vbase
;
2408 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl
), t
, 0, &path
) < 0)
2410 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2411 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl
), vbase
, 0, &path
) >= 0)
2415 /* Not sure if checking path == vbase is necessary here, but just in
2417 if (TREE_VIA_VIRTUAL (path
) || path
== vbase
)
2418 return binfo_member (BINFO_TYPE (path
), CLASSTYPE_VBASECLASSES (t
));
2419 path
= BINFO_INHERITANCE_CHAIN (path
);
2422 /* This shouldn't happen, I don't want errors! */
2423 warning ("recoverable compiler error, fixups for virtual function");
2428 if (TREE_VIA_VIRTUAL (path
))
2429 return binfo_member (BINFO_TYPE (path
), CLASSTYPE_VBASECLASSES (t
));
2430 path
= BINFO_INHERITANCE_CHAIN (path
);
2435 /* Fixups upcast offsets for one vtable.
2436 Entries may stay within the VBASE given, or
2437 they may upcast into a direct base, or
2438 they may upcast into a different vbase.
2440 We only need to do fixups in case 2 and 3. In case 2, we add in
2441 the virtual base offset to effect an upcast, in case 3, we add in
2442 the virtual base offset to effect an upcast, then subtract out the
2443 offset for the other virtual base, to effect a downcast into it.
2445 This routine mirrors fixup_vtable_deltas in functionality, though
2446 this one is runtime based, and the other is compile time based.
2447 Conceivably that routine could be removed entirely, and all fixups
2450 VBASE_OFFSETS is an association list of virtual bases that contains
2451 offset information for the virtual bases, so the offsets are only
2452 calculated once. The offsets are computed by where we think the
2453 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2454 the vbase really is. */
2457 expand_upcast_fixups (binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
,
2459 tree binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
, *vbase_offsets
;
2461 tree virtuals
= BINFO_VIRTUALS (binfo
);
2464 unsigned HOST_WIDE_INT n
;
2466 delta
= purpose_member (vbase
, *vbase_offsets
);
2469 delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase
));
2470 delta
= build (MINUS_EXPR
, ptrdiff_type_node
, delta
, vbase_addr
);
2471 delta
= save_expr (delta
);
2472 delta
= tree_cons (vbase
, delta
, *vbase_offsets
);
2473 *vbase_offsets
= delta
;
2476 n
= skip_rtti_stuff (&virtuals
);
2480 tree current_fndecl
= TREE_VALUE (virtuals
);
2481 current_fndecl
= FNADDR_FROM_VTABLE_ENTRY (current_fndecl
);
2482 current_fndecl
= TREE_OPERAND (current_fndecl
, 0);
2484 && current_fndecl
!= abort_fndecl
2485 && (vc
=virtual_context (current_fndecl
, t
, vbase
)) != vbase
)
2487 /* This may in fact need a runtime fixup. */
2488 tree idx
= build_int_2 (n
, 0);
2489 tree vtbl
= BINFO_VTABLE (binfo
);
2490 tree nvtbl
= lookup_name (DECL_NAME (vtbl
), 0);
2491 tree aref
, ref
, naref
;
2492 tree old_delta
, new_delta
;
2495 if (nvtbl
== NULL_TREE
2496 || nvtbl
== IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl
)))
2498 /* Dup it if it isn't in local scope yet. */
2500 (VAR_DECL
, DECL_NAME (vtbl
),
2501 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl
)));
2502 DECL_ALIGN (nvtbl
) = MAX (TYPE_ALIGN (double_type_node
),
2503 DECL_ALIGN (nvtbl
));
2504 TREE_READONLY (nvtbl
) = 0;
2505 DECL_ARTIFICIAL (nvtbl
) = 1;
2506 nvtbl
= pushdecl (nvtbl
);
2508 cp_finish_decl (nvtbl
, init
, NULL_TREE
, 0,
2509 LOOKUP_ONLYCONVERTING
);
2511 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2512 because they wouldn't be useful; everything that wants to
2513 look at the vtable will look at the decl for the normal
2514 vtable. Setting DECL_CONTEXT also screws up
2515 decl_function_context. */
2517 init
= build (MODIFY_EXPR
, TREE_TYPE (nvtbl
),
2519 TREE_SIDE_EFFECTS (init
) = 1;
2520 expand_expr_stmt (init
);
2521 /* Update the vtable pointers as necessary. */
2522 ref
= build_vfield_ref
2523 (build_indirect_ref (addr
, NULL_PTR
),
2524 DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo
))));
2526 (build_modify_expr (ref
, NOP_EXPR
, nvtbl
));
2528 assemble_external (vtbl
);
2529 aref
= build_array_ref (vtbl
, idx
);
2530 naref
= build_array_ref (nvtbl
, idx
);
2531 old_delta
= build_component_ref (aref
, delta_identifier
,
2533 new_delta
= build_component_ref (naref
, delta_identifier
,
2536 /* This is a upcast, so we have to add the offset for the
2538 old_delta
= build_binary_op (PLUS_EXPR
, old_delta
,
2539 TREE_VALUE (delta
), 0);
2542 /* If this is set, we need to subtract out the delta
2543 adjustments for the other virtual base that we
2545 tree vc_delta
= purpose_member (vc
, *vbase_offsets
);
2548 tree vc_addr
= convert_pointer_to_real (vc
, orig_addr
);
2549 vc_delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc
));
2550 vc_delta
= build (MINUS_EXPR
, ptrdiff_type_node
,
2552 vc_delta
= save_expr (vc_delta
);
2553 *vbase_offsets
= tree_cons (vc
, vc_delta
, *vbase_offsets
);
2556 vc_delta
= TREE_VALUE (vc_delta
);
2558 /* This is a downcast, so we have to subtract the offset
2559 for the virtual base. */
2560 old_delta
= build_binary_op (MINUS_EXPR
, old_delta
, vc_delta
, 0);
2563 TREE_READONLY (new_delta
) = 0;
2564 TREE_TYPE (new_delta
) =
2565 cp_build_qualified_type (TREE_TYPE (new_delta
),
2566 CP_TYPE_QUALS (TREE_TYPE (new_delta
))
2567 & ~TYPE_QUAL_CONST
);
2568 expand_expr_stmt (build_modify_expr (new_delta
, NOP_EXPR
,
2572 virtuals
= TREE_CHAIN (virtuals
);
2576 /* Fixup upcast offsets for all direct vtables. Patterned after
2577 expand_direct_vtbls_init. */
2580 fixup_virtual_upcast_offsets (real_binfo
, binfo
, init_self
, can_elide
, addr
, orig_addr
, type
, vbase
, vbase_offsets
)
2581 tree real_binfo
, binfo
;
2582 int init_self
, can_elide
;
2583 tree addr
, orig_addr
, type
, vbase
, *vbase_offsets
;
2585 tree real_binfos
= BINFO_BASETYPES (real_binfo
);
2586 tree binfos
= BINFO_BASETYPES (binfo
);
2587 int i
, n_baselinks
= real_binfos
? TREE_VEC_LENGTH (real_binfos
) : 0;
2589 for (i
= 0; i
< n_baselinks
; i
++)
2591 tree real_base_binfo
= TREE_VEC_ELT (real_binfos
, i
);
2592 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2593 int is_not_base_vtable
2594 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo
));
2595 if (! TREE_VIA_VIRTUAL (real_base_binfo
))
2596 fixup_virtual_upcast_offsets (real_base_binfo
, base_binfo
,
2597 is_not_base_vtable
, can_elide
, addr
,
2598 orig_addr
, type
, vbase
, vbase_offsets
);
2601 /* Before turning this on, make sure it is correct. */
2602 if (can_elide
&& ! BINFO_MODIFIED (binfo
))
2605 /* Should we use something besides CLASSTYPE_VFIELDS? */
2606 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo
)))
2608 tree new_addr
= convert_pointer_to_real (binfo
, addr
);
2609 expand_upcast_fixups (real_binfo
, new_addr
, orig_addr
, vbase
, addr
,
2610 type
, vbase_offsets
);
2614 /* Build a COMPOUND_EXPR which when expanded will generate the code
2615 needed to initialize all the virtual function table slots of all
2616 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2617 the virtual baseclasses to use; TYPE is the type of the object to
2618 which the initialization applies. TRUE_EXP is the true object we
2619 are initializing, and DECL_PTR is the pointer to the sub-object we
2622 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
2623 object was laid out by a top-level constructor and the computed
2624 offsets are valid to store vtables. When zero, we must store new
2625 vtables through virtual baseclass pointers. */
2628 expand_indirect_vtbls_init (binfo
, true_exp
, decl_ptr
)
2630 tree true_exp
, decl_ptr
;
2632 tree type
= BINFO_TYPE (binfo
);
2634 /* This function executes during the finish_function() segment,
2635 AFTER the auto variables and temporary stack space has been marked
2636 unused...If space is needed for the virtual function tables,
2637 some of them might fit within what the compiler now thinks
2638 are available stack slots... These values are actually initialized at
2639 the beginnning of the function, so when the automatics use their space,
2640 they will overwrite the values that are placed here. Marking all
2641 temporary space as unavailable prevents this from happening. */
2643 mark_all_temps_used();
2645 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
2647 rtx fixup_insns
= NULL_RTX
;
2648 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
2649 struct vbase_info vi
;
2650 vi
.decl_ptr
= (true_exp
? build_unary_op (ADDR_EXPR
, true_exp
, 0)
2652 vi
.vbase_types
= vbases
;
2654 dfs_walk (binfo
, dfs_find_vbases
, unmarked_new_vtablep
, &vi
);
2656 /* Initialized with vtables of type TYPE. */
2657 for (; vbases
; vbases
= TREE_CHAIN (vbases
))
2661 addr
= convert_pointer_to_vbase (TREE_TYPE (vbases
), vi
.decl_ptr
);
2663 /* Do all vtables from this virtual base. */
2664 /* This assumes that virtual bases can never serve as parent
2665 binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */
2666 expand_direct_vtbls_init (vbases
, TYPE_BINFO (BINFO_TYPE (vbases
)),
2669 /* Now we adjust the offsets for virtual functions that
2670 cross virtual boundaries on an implicit upcast on vf call
2671 so that the layout of the most complete type is used,
2672 instead of assuming the layout of the virtual bases from
2673 our current type. */
2675 if (flag_vtable_thunks
)
2677 /* We don't have dynamic thunks yet!
2678 So for now, just fail silently. */
2682 tree vbase_offsets
= NULL_TREE
;
2683 push_to_sequence (fixup_insns
);
2684 fixup_virtual_upcast_offsets (vbases
,
2685 TYPE_BINFO (BINFO_TYPE (vbases
)),
2686 1, 0, addr
, vi
.decl_ptr
,
2687 type
, vbases
, &vbase_offsets
);
2688 fixup_insns
= get_insns ();
2695 extern tree in_charge_identifier
;
2696 tree in_charge_node
= lookup_name (in_charge_identifier
, 0);
2697 if (! in_charge_node
)
2699 warning ("recoverable internal compiler error, nobody's in charge!");
2700 in_charge_node
= integer_zero_node
;
2702 in_charge_node
= build_binary_op (EQ_EXPR
, in_charge_node
, integer_zero_node
, 1);
2703 expand_start_cond (in_charge_node
, 0);
2704 emit_insns (fixup_insns
);
2708 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
, 0);
2712 /* get virtual base class types.
2713 This adds type to the vbase_types list in reverse dfs order.
2714 Ordering is very important, so don't change it. */
2717 dfs_get_vbase_types (binfo
, data
)
2721 tree
*vbase_types
= (tree
*) data
;
2723 if (TREE_VIA_VIRTUAL (binfo
) && ! BINFO_VBASE_MARKED (binfo
))
2725 tree new_vbase
= make_binfo (integer_zero_node
, binfo
,
2726 BINFO_VTABLE (binfo
),
2727 BINFO_VIRTUALS (binfo
));
2728 TREE_CHAIN (new_vbase
) = *vbase_types
;
2729 TREE_VIA_VIRTUAL (new_vbase
) = 1;
2730 *vbase_types
= new_vbase
;
2731 SET_BINFO_VBASE_MARKED (binfo
);
2733 SET_BINFO_MARKED (binfo
);
2737 /* Return a list of binfos for the virtual base classes for TYPE, in
2738 depth-first search order. The list is freshly allocated, so
2739 no modification is made to the current binfo hierarchy. */
2742 get_vbase_types (type
)
2749 binfo
= TYPE_BINFO (type
);
2750 vbase_types
= NULL_TREE
;
2751 dfs_walk (binfo
, dfs_get_vbase_types
, unmarkedp
, &vbase_types
);
2752 dfs_walk (binfo
, dfs_unmark
, markedp
, 0);
2753 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2754 reverse it so that we get normal dfs ordering. */
2755 vbase_types
= nreverse (vbase_types
);
2757 /* unmark marked vbases */
2758 for (vbases
= vbase_types
; vbases
; vbases
= TREE_CHAIN (vbases
))
2759 CLEAR_BINFO_VBASE_MARKED (vbases
);
2764 /* If we want debug info for a type TYPE, make sure all its base types
2765 are also marked as being potentially interesting. This avoids
2766 the problem of not writing any debug info for intermediate basetypes
2767 that have abstract virtual functions. Also mark member types. */
2770 note_debug_info_needed (type
)
2775 if (current_template_parms
)
2778 if (TYPE_BEING_DEFINED (type
))
2779 /* We can't go looking for the base types and fields just yet. */
2782 /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2783 does not support name references between translation units. Well, we
2784 could, but that would mean putting global labels in the debug output
2785 before each exported type and each of its functions and static data
2787 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== DWARF2_DEBUG
)
2790 dfs_walk (TYPE_BINFO (type
), dfs_debug_mark
, dfs_debug_unmarkedp
, 0);
2791 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2794 if (TREE_CODE (field
) == FIELD_DECL
2795 && IS_AGGR_TYPE (ttype
= target_type (TREE_TYPE (field
)))
2796 && dfs_debug_unmarkedp (TYPE_BINFO (ttype
), 0))
2797 note_debug_info_needed (ttype
);
2801 /* Subroutines of push_class_decls (). */
2803 /* Add in a decl to the envelope. */
2805 envelope_add_decl (type
, decl
, values
)
2806 tree type
, decl
, *values
;
2809 tree name
= DECL_NAME (decl
);
2812 /* Yet Another Implicit Typename Kludge: Since we don't tsubst
2813 the members for partial instantiations, DECL_CONTEXT (decl) is wrong.
2814 But pretend it's right for this function. */
2815 if (processing_template_decl
)
2816 type
= DECL_REAL_CONTEXT (decl
);
2818 /* virtual base names are always unique. */
2819 if (VBASE_NAME_P (name
))
2820 *values
= NULL_TREE
;
2822 /* Possible ambiguity. If its defining type(s)
2823 is (are all) derived from us, no problem. */
2824 else if (*values
&& TREE_CODE (*values
) != TREE_LIST
)
2826 tree value
= *values
;
2827 /* Only complain if we shadow something we can access. */
2828 if (warn_shadow
&& TREE_CODE (decl
) == FUNCTION_DECL
2829 && ((DECL_LANG_SPECIFIC (*values
)
2830 && DECL_CLASS_CONTEXT (value
) == current_class_type
)
2831 || ! TREE_PRIVATE (value
)))
2832 /* Should figure out access control more accurately. */
2834 cp_warning_at ("member `%#D' is shadowed", value
);
2835 cp_warning_at ("by member function `%#D'", decl
);
2836 warning ("in this context");
2839 context
= DECL_REAL_CONTEXT (value
);
2841 if (context
== type
)
2843 if (TREE_CODE (value
) == TYPE_DECL
2844 && DECL_ARTIFICIAL (value
))
2845 *values
= NULL_TREE
;
2849 else if (type
== current_class_type
2850 || DERIVED_FROM_P (context
, type
))
2852 /* Don't add in *values to list */
2853 *values
= NULL_TREE
;
2856 *values
= build_tree_list (NULL_TREE
, value
);
2859 for (tmp
= values
; *tmp
;)
2861 tree value
= TREE_VALUE (*tmp
);
2862 my_friendly_assert (TREE_CODE (value
) != TREE_LIST
, 999);
2863 context
= (TREE_CODE (value
) == FUNCTION_DECL
2864 && DECL_VIRTUAL_P (value
))
2865 ? DECL_CLASS_CONTEXT (value
)
2866 : DECL_CONTEXT (value
);
2868 if (type
== current_class_type
2869 || DERIVED_FROM_P (context
, type
))
2871 /* remove *tmp from list */
2872 *tmp
= TREE_CHAIN (*tmp
);
2875 tmp
= &TREE_CHAIN (*tmp
);
2880 /* Put the new contents in our envelope. */
2881 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2883 *values
= tree_cons (name
, decl
, *values
);
2884 TREE_NONLOCAL_FLAG (*values
) = 1;
2885 TREE_TYPE (*values
) = unknown_type_node
;
2891 *values
= tree_cons (NULL_TREE
, decl
, *values
);
2892 /* Mark this as a potentially ambiguous member. */
2893 /* Leaving TREE_TYPE blank is intentional.
2894 We cannot use `error_mark_node' (lookup_name)
2895 or `unknown_type_node' (all member functions use this). */
2896 TREE_NONLOCAL_FLAG (*values
) = 1;
2904 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2905 because it (or one of the intermediate bases) depends on template parms. */
2908 dependent_base_p (binfo
)
2911 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2913 if (currently_open_class (TREE_TYPE (binfo
)))
2915 if (uses_template_parms (TREE_TYPE (binfo
)))
2921 /* Add the instance variables which this class contributed to the
2922 current class binding contour. When a redefinition occurs, if the
2923 redefinition is strictly within a single inheritance path, we just
2924 overwrite the old declaration with the new. If the fields are not
2925 within a single inheritance path, we must cons them.
2927 In order to know what decls are new (stemming from the current
2928 invocation of push_class_decls) we enclose them in an "envelope",
2929 which is a TREE_LIST node where the TREE_PURPOSE slot contains the
2930 new decl (or possibly a list of competing ones), the TREE_VALUE slot
2931 points to the old value and the TREE_CHAIN slot chains together all
2932 envelopes which needs to be "opened" in push_class_decls. Opening an
2933 envelope means: push the old value onto the class_shadowed list,
2934 install the new one and if it's a TYPE_DECL do the same to the
2935 IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that
2936 the TREE_PURPOSE slot is non-null, and that it is not an identifier.
2937 Because if it is, it could be a set of overloaded methods from an
2941 dfs_pushdecls (binfo
, data
)
2945 tree
*closed_envelopes
= (tree
*) data
;
2946 tree type
= BINFO_TYPE (binfo
);
2951 /* Only record types if we're a template base. */
2952 if (processing_template_decl
&& type
!= current_class_type
2953 && dependent_base_p (binfo
))
2956 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
2958 if (dummy
&& TREE_CODE (fields
) != TYPE_DECL
)
2961 /* Unmark so that if we are in a constructor, and then find that
2962 this field was initialized by a base initializer,
2963 we can emit an error message. */
2964 if (TREE_CODE (fields
) == FIELD_DECL
)
2965 TREE_USED (fields
) = 0;
2967 /* Recurse into anonymous unions. */
2968 if (DECL_NAME (fields
) == NULL_TREE
2969 && TREE_CODE (TREE_TYPE (fields
)) == UNION_TYPE
)
2971 dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields
)), data
);
2975 if (DECL_NAME (fields
))
2977 tree name
= DECL_NAME (fields
);
2978 tree class_value
= IDENTIFIER_CLASS_VALUE (name
);
2980 /* If the class value is not an envelope of the kind described in
2981 the comment above, we create a new envelope. */
2982 maybe_push_cache_obstack ();
2983 if (class_value
== NULL_TREE
|| TREE_CODE (class_value
) != TREE_LIST
2984 || TREE_PURPOSE (class_value
) == NULL_TREE
2985 || TREE_CODE (TREE_PURPOSE (class_value
)) == IDENTIFIER_NODE
)
2987 /* See comment above for a description of envelopes. */
2988 *closed_envelopes
= tree_cons (NULL_TREE
, class_value
,
2990 IDENTIFIER_CLASS_VALUE (name
) = *closed_envelopes
;
2991 class_value
= IDENTIFIER_CLASS_VALUE (name
);
2994 envelope_add_decl (type
, fields
, &TREE_PURPOSE (class_value
));
2999 method_vec
= CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
3000 if (method_vec
&& ! dummy
)
3005 /* Farm out constructors and destructors. */
3006 end
= TREE_VEC_END (method_vec
);
3008 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
3009 *methods
&& methods
!= end
;
3012 /* This will cause lookup_name to return a pointer
3013 to the tree_list of possible methods of this name. */
3018 name
= DECL_NAME (OVL_CURRENT (*methods
));
3019 class_value
= IDENTIFIER_CLASS_VALUE (name
);
3021 maybe_push_cache_obstack ();
3023 /* If the class value is not an envelope of the kind described in
3024 the comment above, we create a new envelope. */
3025 if (class_value
== NULL_TREE
|| TREE_CODE (class_value
) != TREE_LIST
3026 || TREE_PURPOSE (class_value
) == NULL_TREE
3027 || TREE_CODE (TREE_PURPOSE (class_value
)) == IDENTIFIER_NODE
)
3029 /* See comment above for a description of envelopes. */
3030 *closed_envelopes
= tree_cons (NULL_TREE
, class_value
,
3032 IDENTIFIER_CLASS_VALUE (name
) = *closed_envelopes
;
3033 class_value
= IDENTIFIER_CLASS_VALUE (name
);
3036 /* Here we try to rule out possible ambiguities.
3037 If we can't do that, keep a TREE_LIST with possibly ambiguous
3039 /* Arbitrarily choose the first function in the list. This is OK
3040 because this is only used for initial lookup; anything that
3041 actually uses the function will look it up again. */
3042 envelope_add_decl (type
, OVL_CURRENT (*methods
),
3043 &TREE_PURPOSE (class_value
));
3048 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3049 DERIVED_FROM_P, which calls get_base_distance. */
3050 SET_BINFO_PUSHDECLS_MARKED (binfo
);
3055 /* Consolidate unique (by name) member functions. */
3058 dfs_compress_decls (binfo
, data
)
3060 void *data ATTRIBUTE_UNUSED
;
3062 tree type
= BINFO_TYPE (binfo
);
3064 = CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
3066 if (processing_template_decl
&& type
!= current_class_type
3067 && dependent_base_p (binfo
))
3068 /* We only record types if we're a template base. */;
3069 else if (method_vec
!= 0)
3071 /* Farm out constructors and destructors. */
3073 tree
*end
= TREE_VEC_END (method_vec
);
3075 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
3076 methods
!= end
&& *methods
; methods
++)
3078 /* This is known to be an envelope of the kind described before
3081 IDENTIFIER_CLASS_VALUE (DECL_NAME (OVL_CURRENT (*methods
)));
3082 tree tmp
= TREE_PURPOSE (class_value
);
3084 /* This was replaced in scope by somebody else. Just leave it
3086 if (TREE_CODE (tmp
) != TREE_LIST
)
3089 if (TREE_CHAIN (tmp
) == NULL_TREE
3091 && OVL_NEXT (TREE_VALUE (tmp
)) == NULL_TREE
)
3093 TREE_PURPOSE (class_value
) = TREE_VALUE (tmp
);
3097 CLEAR_BINFO_PUSHDECLS_MARKED (binfo
);
3102 /* When entering the scope of a class, we cache all of the
3103 fields that that class provides within its inheritance
3104 lattice. Where ambiguities result, we mark them
3105 with `error_mark_node' so that if they are encountered
3106 without explicit qualification, we can emit an error
3110 push_class_decls (type
)
3113 struct obstack
*ambient_obstack
= current_obstack
;
3114 tree closed_envelopes
= NULL_TREE
;
3115 search_stack
= push_search_level (search_stack
, &search_obstack
);
3117 /* Build up all the relevant bindings and such on the cache
3118 obstack. That way no memory is wasted when we throw away the
3120 maybe_push_cache_obstack ();
3122 /* Push class fields into CLASS_VALUE scope, and mark. */
3123 dfs_walk (TYPE_BINFO (type
), dfs_pushdecls
, unmarked_pushdecls_p
,
3126 /* Compress fields which have only a single entry
3127 by a given name, and unmark. */
3128 dfs_walk (TYPE_BINFO (type
), dfs_compress_decls
, marked_pushdecls_p
,
3131 /* Open up all the closed envelopes and push the contained decls into
3133 while (closed_envelopes
)
3135 tree
new = TREE_PURPOSE (closed_envelopes
);
3138 /* This is messy because the class value may be a *_DECL, or a
3139 TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
3140 *_DECLs. The name is stored at different places in these three
3142 if (TREE_CODE (new) == TREE_LIST
)
3144 if (TREE_PURPOSE (new) != NULL_TREE
)
3145 id
= TREE_PURPOSE (new);
3148 tree node
= TREE_VALUE (new);
3150 if (TREE_CODE (node
) == TYPE_DECL
3151 && DECL_ARTIFICIAL (node
)
3152 && IS_AGGR_TYPE (TREE_TYPE (node
))
3153 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (node
)))
3155 tree t
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (node
));
3158 for (; n
; n
= TREE_CHAIN (n
))
3160 tree d
= TREE_VALUE (n
);
3161 if (TREE_CODE (d
) == TYPE_DECL
3162 && DECL_ARTIFICIAL (node
)
3163 && IS_AGGR_TYPE (TREE_TYPE (d
))
3164 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d
))
3165 && CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d
)) == t
)
3174 else while (TREE_CODE (node
) == TREE_LIST
)
3175 node
= TREE_VALUE (node
);
3176 id
= DECL_NAME (node
);
3180 id
= DECL_NAME (new);
3182 /* Install the original class value in order to make
3183 pushdecl_class_level work correctly. */
3184 IDENTIFIER_CLASS_VALUE (id
) = TREE_VALUE (closed_envelopes
);
3185 if (TREE_CODE (new) == TREE_LIST
)
3186 push_class_level_binding (id
, new);
3188 pushdecl_class_level (new);
3189 closed_envelopes
= TREE_CHAIN (closed_envelopes
);
3192 /* Undo the call to maybe_push_cache_obstack above. */
3195 current_obstack
= ambient_obstack
;
3198 /* Here's a subroutine we need because C lacks lambdas. */
3201 dfs_unuse_fields (binfo
, data
)
3203 void *data ATTRIBUTE_UNUSED
;
3205 tree type
= TREE_TYPE (binfo
);
3208 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3210 if (TREE_CODE (fields
) != FIELD_DECL
)
3213 TREE_USED (fields
) = 0;
3214 if (DECL_NAME (fields
) == NULL_TREE
3215 && TREE_CODE (TREE_TYPE (fields
)) == UNION_TYPE
)
3216 unuse_fields (TREE_TYPE (fields
));
3226 dfs_walk (TYPE_BINFO (type
), dfs_unuse_fields
, unmarkedp
, 0);
3232 /* We haven't pushed a search level when dealing with cached classes,
3233 so we'd better not try to pop it. */
3235 search_stack
= pop_search_level (search_stack
);
3239 print_search_statistics ()
3241 #ifdef GATHER_STATISTICS
3242 fprintf (stderr
, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3243 n_fields_searched
, n_calls_lookup_field
, n_calls_lookup_field_1
);
3244 fprintf (stderr
, "%d fnfields searched in %d calls to lookup_fnfields\n",
3245 n_outer_fields_searched
, n_calls_lookup_fnfields
);
3246 fprintf (stderr
, "%d calls to get_base_type\n", n_calls_get_base_type
);
3247 #else /* GATHER_STATISTICS */
3248 fprintf (stderr
, "no search statistics\n");
3249 #endif /* GATHER_STATISTICS */
3253 init_search_processing ()
3255 gcc_obstack_init (&search_obstack
);
3256 _vptr_name
= get_identifier ("_vptr");
3260 reinit_search_statistics ()
3262 #ifdef GATHER_STATISTICS
3263 n_fields_searched
= 0;
3264 n_calls_lookup_field
= 0, n_calls_lookup_field_1
= 0;
3265 n_calls_lookup_fnfields
= 0, n_calls_lookup_fnfields_1
= 0;
3266 n_calls_get_base_type
= 0;
3267 n_outer_fields_searched
= 0;
3268 n_contexts_saved
= 0;
3269 #endif /* GATHER_STATISTICS */
3272 #define scratch_tree_cons expr_tree_cons
3275 add_conversions (binfo
, data
)
3280 tree method_vec
= CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo
));
3281 tree
*conversions
= (tree
*) data
;
3283 for (i
= 2; i
< TREE_VEC_LENGTH (method_vec
); ++i
)
3285 tree tmp
= TREE_VEC_ELT (method_vec
, i
);
3288 if (!tmp
|| ! DECL_CONV_FN_P (OVL_CURRENT (tmp
)))
3291 name
= DECL_NAME (OVL_CURRENT (tmp
));
3293 /* Make sure we don't already have this conversion. */
3294 if (! IDENTIFIER_MARKED (name
))
3296 *conversions
= scratch_tree_cons (binfo
, tmp
, *conversions
);
3297 IDENTIFIER_MARKED (name
) = 1;
3304 lookup_conversions (type
)
3308 tree conversions
= NULL_TREE
;
3310 if (TYPE_SIZE (type
))
3311 bfs_walk (TYPE_BINFO (type
), add_conversions
, 0, &conversions
);
3313 for (t
= conversions
; t
; t
= TREE_CHAIN (t
))
3314 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t
)))) = 0;
3325 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3326 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3329 dfs_check_overlap (empty_binfo
, data
)
3333 struct overlap_info
*oi
= (struct overlap_info
*) data
;
3335 for (binfo
= TYPE_BINFO (oi
->compare_type
);
3337 binfo
= BINFO_BASETYPE (binfo
, 0))
3339 if (BINFO_TYPE (binfo
) == BINFO_TYPE (empty_binfo
))
3341 oi
->found_overlap
= 1;
3344 else if (BINFO_BASETYPES (binfo
) == NULL_TREE
)
3351 /* Trivial function to stop base traversal when we find something. */
3354 dfs_no_overlap_yet (binfo
, data
)
3358 struct overlap_info
*oi
= (struct overlap_info
*) data
;
3359 return !oi
->found_overlap
? binfo
: NULL_TREE
;
3362 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3363 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3366 types_overlap_p (empty_type
, next_type
)
3367 tree empty_type
, next_type
;
3369 struct overlap_info oi
;
3371 if (! IS_AGGR_TYPE (next_type
))
3373 oi
.compare_type
= next_type
;
3374 oi
.found_overlap
= 0;
3375 dfs_walk (TYPE_BINFO (empty_type
), dfs_check_overlap
,
3376 dfs_no_overlap_yet
, &oi
);
3377 return oi
.found_overlap
;
3386 dfs_bfv_queue_p (binfo
, data
)
3390 struct bfv_info
*bfvi
= (struct bfv_info
*) data
;
3392 /* Use the real virtual base class objects, not the placeholders in
3393 the usual hierarchy. */
3394 if (TREE_VIA_VIRTUAL (binfo
))
3395 return binfo_member (BINFO_TYPE (binfo
), bfvi
->vbases
);
3400 /* Passed to dfs_search by binfo_for_vtable; determine if bvtable comes
3404 dfs_bfv_helper (binfo
, data
)
3408 struct bfv_info
*bfvi
= (struct bfv_info
*) data
;
3410 if (BINFO_VTABLE (binfo
) == bfvi
->var
)
3415 /* Given a vtable VAR, determine which binfo it comes from. */
3418 binfo_for_vtable (var
)
3422 struct bfv_info bfvi
;
3424 type
= DECL_CONTEXT (var
);
3425 bfvi
.vbases
= CLASSTYPE_VBASECLASSES (type
);
3426 return dfs_walk_real (TYPE_BINFO (type
),
3427 0, dfs_bfv_helper
, dfs_bfv_queue_p
, &bfvi
);