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, 2000 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. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 extern struct obstack
*current_obstack
;
42 /* Obstack used for remembering decision points of breadth-first. */
44 static struct obstack search_obstack
;
46 /* Methods for pushing and popping objects to and from obstacks. */
49 push_stack_level (obstack
, tp
, size
)
50 struct obstack
*obstack
;
51 char *tp
; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
54 struct stack_level
*stack
;
55 obstack_grow (obstack
, tp
, size
);
56 stack
= (struct stack_level
*) ((char*)obstack_next_free (obstack
) - size
);
57 obstack_finish (obstack
);
58 stack
->obstack
= obstack
;
59 stack
->first
= (tree
*) obstack_base (obstack
);
60 stack
->limit
= obstack_room (obstack
) / sizeof (tree
*);
65 pop_stack_level (stack
)
66 struct stack_level
*stack
;
68 struct stack_level
*tem
= stack
;
69 struct obstack
*obstack
= tem
->obstack
;
71 obstack_free (obstack
, tem
);
75 #define search_level stack_level
76 static struct search_level
*search_stack
;
78 static tree next_baselink
PROTO((tree
));
79 static tree get_vbase_1
PROTO((tree
, tree
, unsigned int *));
80 static tree lookup_field_1
PROTO((tree
, tree
));
81 static tree convert_pointer_to_single_level
PROTO((tree
, tree
));
82 static int lookup_fnfields_here
PROTO((tree
, tree
));
83 static int is_subobject_of_p
PROTO((tree
, tree
));
84 static int hides
PROTO((tree
, tree
));
85 static tree virtual_context
PROTO((tree
, tree
, tree
));
86 static tree dfs_check_overlap
PROTO((tree
, void *));
87 static tree dfs_no_overlap_yet
PROTO((tree
, void *));
88 static int get_base_distance_recursive
89 PROTO((tree
, int, int, int, int *, tree
*, tree
,
90 int, int *, int, int));
91 static int dynamic_cast_base_recurse
PROTO((tree
, tree
, int, tree
*));
92 static void expand_upcast_fixups
93 PROTO((tree
, tree
, tree
, tree
, tree
, tree
, tree
*));
94 static void fixup_virtual_upcast_offsets
95 PROTO((tree
, tree
, int, int, tree
, tree
, tree
, tree
,
97 static tree marked_vtable_pathp
PROTO((tree
, void *));
98 static tree unmarked_vtable_pathp
PROTO((tree
, void *));
99 static tree marked_new_vtablep
PROTO((tree
, void *));
100 static tree unmarked_new_vtablep
PROTO((tree
, void *));
101 static tree marked_pushdecls_p
PROTO((tree
, void *));
102 static tree unmarked_pushdecls_p
PROTO((tree
, void *));
104 static tree dfs_debug_unmarkedp
PROTO((tree
, void *));
105 static tree dfs_debug_mark
PROTO((tree
, void *));
107 static tree dfs_find_vbases
PROTO((tree
, void *));
108 static tree dfs_clear_vbase_slots
PROTO((tree
, void *));
109 static tree dfs_init_vbase_pointers
PROTO((tree
, void *));
110 static tree dfs_get_vbase_types
PROTO((tree
, void *));
111 static tree dfs_push_type_decls
PROTO((tree
, void *));
112 static tree dfs_push_decls
PROTO((tree
, void *));
113 static tree dfs_unuse_fields
PROTO((tree
, void *));
114 static tree add_conversions
PROTO((tree
, void *));
115 static tree get_virtuals_named_this
PROTO((tree
, tree
));
116 static tree get_virtual_destructor
PROTO((tree
, void *));
117 static tree tree_has_any_destructor_p
PROTO((tree
, void *));
118 static int covariant_return_p
PROTO((tree
, tree
));
119 static int check_final_overrider
PROTO((tree
, tree
));
120 static struct search_level
*push_search_level
121 PROTO((struct stack_level
*, struct obstack
*));
122 static struct search_level
*pop_search_level
123 PROTO((struct stack_level
*));
125 PROTO((tree
, tree (*) (tree
, void *), tree (*) (tree
, void *),
127 static tree lookup_field_queue_p
PROTO((tree
, void *));
128 static tree lookup_field_r
PROTO((tree
, void *));
129 static tree dfs_walk_real
PROTO ((tree
,
130 tree (*) (tree
, void *),
131 tree (*) (tree
, void *),
132 tree (*) (tree
, void *),
134 static tree get_virtuals_named_this_r
PROTO ((tree
, void *));
135 static tree context_for_name_lookup
PROTO ((tree
));
136 static tree canonical_binfo
PROTO ((tree
));
137 static tree shared_marked_p
PROTO ((tree
, void *));
138 static tree shared_unmarked_p
PROTO ((tree
, void *));
139 static int dependent_base_p
PROTO ((tree
));
140 static tree dfs_accessible_queue_p
PROTO ((tree
, void *));
141 static tree dfs_accessible_p
PROTO ((tree
, void *));
142 static tree dfs_access_in_type
PROTO ((tree
, void *));
143 static tree access_in_type
PROTO ((tree
, tree
));
144 static tree dfs_canonical_queue
PROTO ((tree
, void *));
145 static tree dfs_assert_unmarked_p
PROTO ((tree
, void *));
146 static void assert_canonical_unmarked
PROTO ((tree
));
147 static int protected_accessible_p
PROTO ((tree
, tree
, tree
, tree
));
148 static int friend_accessible_p
PROTO ((tree
, tree
, tree
, tree
));
149 static void setup_class_bindings
PROTO ((tree
, int));
150 static int template_self_reference_p
PROTO ((tree
, tree
));
151 static void fixup_all_virtual_upcast_offsets
PROTO ((tree
, tree
));
152 static tree dfs_mark_primary_bases
PROTO((tree
, void *));
153 static tree get_shared_vbase_if_not_primary
PROTO((tree
, tree
));
155 /* Allocate a level of searching. */
157 static struct search_level
*
158 push_search_level (stack
, obstack
)
159 struct stack_level
*stack
;
160 struct obstack
*obstack
;
162 struct search_level tem
;
165 return push_stack_level (obstack
, (char *)&tem
, sizeof (tem
));
168 /* Discard a level of search allocation. */
170 static struct search_level
*
171 pop_search_level (obstack
)
172 struct stack_level
*obstack
;
174 register struct search_level
*stack
= pop_stack_level (obstack
);
179 /* Variables for gathering statistics. */
180 #ifdef GATHER_STATISTICS
181 static int n_fields_searched
;
182 static int n_calls_lookup_field
, n_calls_lookup_field_1
;
183 static int n_calls_lookup_fnfields
, n_calls_lookup_fnfields_1
;
184 static int n_calls_get_base_type
;
185 static int n_outer_fields_searched
;
186 static int n_contexts_saved
;
187 #endif /* GATHER_STATISTICS */
190 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
191 the same type as the type given in PARENT. To be optimal, we want
192 the first one that is found by going through the least number of
195 This uses a clever algorithm that updates *depth when we find the vbase,
196 and cuts off other paths of search when they reach that depth. */
199 get_vbase_1 (parent
, binfo
, depth
)
205 tree rval
= NULL_TREE
;
207 if (BINFO_TYPE (binfo
) == parent
&& TREE_VIA_VIRTUAL (binfo
))
215 binfos
= BINFO_BASETYPES (binfo
);
216 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
218 /* Process base types. */
219 for (i
= 0; i
< n_baselinks
; i
++)
221 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
227 nrval
= get_vbase_1 (parent
, base_binfo
, depth
);
235 /* Return the shortest path to vbase PARENT within BINFO, ignoring
236 access and ambiguity. */
239 get_vbase (parent
, binfo
)
243 unsigned int d
= (unsigned int)-1;
244 return get_vbase_1 (parent
, binfo
, &d
);
247 /* Convert EXPR to a virtual base class of type TYPE. We know that
248 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
249 the type of what expr points to has a virtual base of type TYPE. */
252 convert_pointer_to_vbase (type
, expr
)
256 tree vb
= get_vbase (type
, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr
))));
257 return convert_pointer_to_real (vb
, expr
);
260 /* Check whether the type given in BINFO is derived from PARENT. If
261 it isn't, return 0. If it is, but the derivation is MI-ambiguous
262 AND protect != 0, emit an error message and return error_mark_node.
264 Otherwise, if TYPE is derived from PARENT, return the actual base
265 information, unless a one of the protection violations below
266 occurs, in which case emit an error message and return error_mark_node.
268 If PROTECT is 1, then check if access to a public field of PARENT
269 would be private. Also check for ambiguity. */
272 get_binfo (parent
, binfo
, protect
)
273 register tree parent
, binfo
;
276 tree type
= NULL_TREE
;
278 tree rval
= NULL_TREE
;
280 if (TREE_CODE (parent
) == TREE_VEC
)
281 parent
= BINFO_TYPE (parent
);
282 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent
)))
283 my_friendly_abort (89);
285 if (TREE_CODE (binfo
) == TREE_VEC
)
286 type
= BINFO_TYPE (binfo
);
287 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
290 my_friendly_abort (90);
292 dist
= get_base_distance (parent
, binfo
, protect
, &rval
);
296 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
298 return error_mark_node
;
300 else if (dist
== -2 && protect
)
302 cp_error ("type `%T' is ambiguous base class for type `%T'", parent
,
304 return error_mark_node
;
310 /* This is the newer depth first get_base_distance routine. */
313 get_base_distance_recursive (binfo
, depth
, is_private
, rval
,
314 rval_private_ptr
, new_binfo_ptr
, parent
,
315 protect
, via_virtual_ptr
, via_virtual
,
316 current_scope_in_chain
)
318 int depth
, is_private
, rval
;
319 int *rval_private_ptr
;
320 tree
*new_binfo_ptr
, parent
;
321 int protect
, *via_virtual_ptr
, via_virtual
;
322 int current_scope_in_chain
;
328 && !current_scope_in_chain
329 && is_friend (BINFO_TYPE (binfo
), current_scope ()))
330 current_scope_in_chain
= 1;
332 if (BINFO_TYPE (binfo
) == parent
|| binfo
== parent
)
337 /* This is the first time we've found parent. */
339 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr
),
340 BINFO_OFFSET (binfo
))
341 && *via_virtual_ptr
&& via_virtual
)
343 /* A new path to the same vbase. If this one has better
344 access or is shorter, take it. */
347 better
= *rval_private_ptr
- is_private
;
349 better
= rval
- depth
;
353 /* Ambiguous base class. */
356 /* If we get an ambiguity between virtual and non-virtual base
357 class, return the non-virtual in case we are ignoring
359 better
= *via_virtual_ptr
- via_virtual
;
365 *rval_private_ptr
= is_private
;
366 *new_binfo_ptr
= binfo
;
367 *via_virtual_ptr
= via_virtual
;
373 binfos
= BINFO_BASETYPES (binfo
);
374 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
377 /* Process base types. */
378 for (i
= 0; i
< n_baselinks
; i
++)
380 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
385 || (!TREE_VIA_PUBLIC (base_binfo
)
386 && !(TREE_VIA_PROTECTED (base_binfo
)
387 && current_scope_in_chain
)
388 && !is_friend (BINFO_TYPE (binfo
), current_scope ()))));
389 int this_virtual
= via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
);
391 rval
= get_base_distance_recursive (base_binfo
, depth
, via_private
,
392 rval
, rval_private_ptr
,
393 new_binfo_ptr
, parent
,
394 protect
, via_virtual_ptr
,
396 current_scope_in_chain
);
398 /* If we've found a non-virtual, ambiguous base class, we don't need
399 to keep searching. */
400 if (rval
== -2 && *via_virtual_ptr
== 0)
407 /* Return the number of levels between type PARENT and the type given
408 in BINFO, following the leftmost path to PARENT not found along a
409 virtual path, if there are no real PARENTs (all come from virtual
410 base classes), then follow the shortest public path to PARENT.
412 Return -1 if TYPE is not derived from PARENT.
413 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
415 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
417 If PATH_PTR is non-NULL, then also build the list of types
418 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
421 PARENT can also be a binfo, in which case that exact parent is found
422 and no other. convert_pointer_to_real uses this functionality.
424 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
427 get_base_distance (parent
, binfo
, protect
, path_ptr
)
428 register tree parent
, binfo
;
433 int rval_private
= 0;
434 tree type
= NULL_TREE
;
435 tree new_binfo
= NULL_TREE
;
437 int watch_access
= protect
;
439 /* Should we be completing types here? */
440 if (TREE_CODE (parent
) != TREE_VEC
)
441 parent
= complete_type (TYPE_MAIN_VARIANT (parent
));
443 complete_type (TREE_TYPE (parent
));
445 if (TREE_CODE (binfo
) == TREE_VEC
)
446 type
= BINFO_TYPE (binfo
);
447 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
449 type
= complete_type (binfo
);
450 binfo
= TYPE_BINFO (type
);
453 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo
) == NULL_TREE
,
457 my_friendly_abort (92);
459 if (parent
== type
|| parent
== binfo
)
461 /* If the distance is 0, then we don't really need
462 a path pointer, but we shouldn't let garbage go back. */
471 rval
= get_base_distance_recursive (binfo
, 0, 0, -1,
472 &rval_private
, &new_binfo
, parent
,
473 watch_access
, &via_virtual
, 0,
476 /* Access restrictions don't count if we found an ambiguous basetype. */
477 if (rval
== -2 && protect
>= 0)
480 if (rval
&& protect
&& rval_private
)
483 /* If they gave us the real vbase binfo, which isn't in the main binfo
484 tree, deal with it. This happens when we are called from
485 expand_upcast_fixups. */
486 if (rval
== -1 && TREE_CODE (parent
) == TREE_VEC
487 && parent
== BINFO_FOR_VBASE (BINFO_TYPE (parent
), type
))
489 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent
) == binfo
, 980827);
495 *path_ptr
= new_binfo
;
499 /* Worker function for get_dynamic_cast_base_type. */
502 dynamic_cast_base_recurse (subtype
, binfo
, via_virtual
, offset_ptr
)
512 if (BINFO_TYPE (binfo
) == subtype
)
518 *offset_ptr
= BINFO_OFFSET (binfo
);
523 binfos
= BINFO_BASETYPES (binfo
);
524 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
525 for (i
= 0; i
< n_baselinks
; i
++)
527 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
530 if (!TREE_VIA_PUBLIC (base_binfo
))
532 rval
= dynamic_cast_base_recurse
533 (subtype
, base_binfo
,
534 via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
), offset_ptr
);
538 worst
= worst
>= 0 ? -1 : worst
;
540 worst
= worst
< rval
? worst
: rval
;
545 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type started
546 from is related to the required TARGET type, in order to optimize the
547 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 non-virtual bases.
553 BOFF == -2, SUBTYPE occurs as multiple public virtual or non-virtual bases.
554 BOFF == -3, SUBTYPE is not a public base. */
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_CLASS_CONTEXT (current_function_decl
) == current_class_type
)
697 return current_function_decl
;
699 return current_class_type
;
702 /* Returns non-zero if we are currently in a function scope. Note
703 that this function returns zero if we are within a local class, but
704 not within a member function body of the local class. */
707 at_function_scope_p ()
709 tree cs
= current_scope ();
710 return cs
&& TREE_CODE (cs
) == FUNCTION_DECL
;
713 /* Return the scope of DECL, as appropriate when doing name-lookup. */
716 context_for_name_lookup (decl
)
721 For the purposes of name lookup, after the anonymous union
722 definition, the members of the anonymous union are considered to
723 have been defined in the scope in which teh anonymous union is
725 tree context
= DECL_REAL_CONTEXT (decl
);
727 while (TYPE_P (context
) && ANON_AGGR_TYPE_P (context
))
728 context
= TYPE_CONTEXT (context
);
730 context
= global_namespace
;
735 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
739 canonical_binfo (binfo
)
742 return (TREE_VIA_VIRTUAL (binfo
)
743 ? TYPE_BINFO (BINFO_TYPE (binfo
)) : binfo
);
746 /* A queue function that simply ensures that we walk into the
747 canonical versions of virtual bases. */
750 dfs_canonical_queue (binfo
, data
)
752 void *data ATTRIBUTE_UNUSED
;
754 return canonical_binfo (binfo
);
757 /* Called via dfs_walk from assert_canonical_unmarked. */
760 dfs_assert_unmarked_p (binfo
, data
)
762 void *data ATTRIBUTE_UNUSED
;
764 my_friendly_assert (!BINFO_MARKED (binfo
), 0);
768 /* Asserts that all the nodes below BINFO (using the canonical
769 versions of virtual bases) are unmarked. */
772 assert_canonical_unmarked (binfo
)
775 dfs_walk (binfo
, dfs_assert_unmarked_p
, dfs_canonical_queue
, 0);
778 /* If BINFO is marked, return a canonical version of BINFO.
779 Otherwise, return NULL_TREE. */
782 shared_marked_p (binfo
, data
)
786 binfo
= canonical_binfo (binfo
);
787 return markedp (binfo
, data
);
790 /* If BINFO is not marked, return a canonical version of BINFO.
791 Otherwise, return NULL_TREE. */
794 shared_unmarked_p (binfo
, data
)
798 binfo
= canonical_binfo (binfo
);
799 return unmarkedp (binfo
, data
);
802 /* Called from access_in_type via dfs_walk. Calculate the access to
803 DATA (which is really a DECL) in BINFO. */
806 dfs_access_in_type (binfo
, data
)
810 tree decl
= (tree
) data
;
811 tree type
= BINFO_TYPE (binfo
);
812 tree access
= NULL_TREE
;
814 if (context_for_name_lookup (decl
) == type
)
816 /* If we have desceneded to the scope of DECL, just note the
817 appropriate access. */
818 if (TREE_PRIVATE (decl
))
819 access
= access_private_node
;
820 else if (TREE_PROTECTED (decl
))
821 access
= access_protected_node
;
823 access
= access_public_node
;
827 /* First, check for an access-declaration that gives us more
828 access to the DECL. The CONST_DECL for an enumeration
829 constant will not have DECL_LANG_SPECIFIC, and thus no
831 if (DECL_LANG_SPECIFIC (decl
))
833 access
= purpose_member (type
, DECL_ACCESS (decl
));
835 access
= TREE_VALUE (access
);
844 /* Otherwise, scan our baseclasses, and pick the most favorable
846 binfos
= BINFO_BASETYPES (binfo
);
847 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
848 for (i
= 0; i
< n_baselinks
; ++i
)
850 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
851 tree base_access
= TREE_CHAIN (canonical_binfo (base_binfo
));
853 if (!base_access
|| base_access
== access_private_node
)
854 /* If it was not accessible in the base, or only
855 accessible as a private member, we can't access it
857 base_access
= NULL_TREE
;
858 else if (TREE_VIA_PROTECTED (base_binfo
))
859 /* Public and protected members in the base are
861 base_access
= access_protected_node
;
862 else if (!TREE_VIA_PUBLIC (base_binfo
))
863 /* Public and protected members in the base are
865 base_access
= access_private_node
;
867 /* See if the new access, via this base, gives more
868 access than our previous best access. */
870 (base_access
== access_public_node
871 || (base_access
== access_protected_node
872 && access
!= access_public_node
)
873 || (base_access
== access_private_node
876 access
= base_access
;
878 /* If the new access is public, we can't do better. */
879 if (access
== access_public_node
)
886 /* Note the access to DECL in TYPE. */
887 TREE_CHAIN (binfo
) = access
;
889 /* Mark TYPE as visited so that if we reach it again we do not
890 duplicate our efforts here. */
891 SET_BINFO_MARKED (binfo
);
896 /* Return the access to DECL in TYPE. */
899 access_in_type (type
, decl
)
903 tree binfo
= TYPE_BINFO (type
);
905 /* We must take into account
909 If a name can be reached by several paths through a multiple
910 inheritance graph, the access is that of the path that gives
913 The algorithm we use is to make a post-order depth-first traversal
914 of the base-class hierarchy. As we come up the tree, we annotate
915 each node with the most lenient access. */
916 dfs_walk_real (binfo
, 0, dfs_access_in_type
, shared_unmarked_p
, decl
);
917 dfs_walk (binfo
, dfs_unmark
, shared_marked_p
, 0);
918 assert_canonical_unmarked (binfo
);
920 return TREE_CHAIN (binfo
);
923 /* Called from dfs_accessible_p via dfs_walk. */
926 dfs_accessible_queue_p (binfo
, data
)
928 void *data ATTRIBUTE_UNUSED
;
930 if (BINFO_MARKED (binfo
))
933 /* If this class is inherited via private or protected inheritance,
934 then we can't see it, unless we are a friend of the subclass. */
935 if (!TREE_VIA_PUBLIC (binfo
)
936 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
940 return canonical_binfo (binfo
);
943 /* Called from dfs_accessible_p via dfs_walk. */
946 dfs_accessible_p (binfo
, data
)
950 int protected_ok
= data
!= 0;
953 /* We marked the binfos while computing the access in each type.
954 So, we unmark as we go now. */
955 SET_BINFO_MARKED (binfo
);
957 access
= TREE_CHAIN (binfo
);
958 if (access
== access_public_node
959 || (access
== access_protected_node
&& protected_ok
))
961 else if (access
&& is_friend (BINFO_TYPE (binfo
), current_scope ()))
967 /* Returns non-zero if it is OK to access DECL when named in TYPE
968 through an object indiated by BINFO in the context of DERIVED. */
971 protected_accessible_p (type
, decl
, derived
, binfo
)
979 /* We're checking this clause from [class.access.base]
981 m as a member of N is protected, and the reference occurs in a
982 member or friend of class N, or in a member or friend of a
983 class P derived from N, where m as a member of P is private or
986 If DERIVED isn't derived from TYPE, then it certainly does not
988 if (!DERIVED_FROM_P (type
, derived
))
991 access
= access_in_type (derived
, decl
);
992 if (same_type_p (derived
, type
))
994 if (access
!= access_private_node
)
997 else if (access
!= access_private_node
998 && access
!= access_protected_node
)
1001 /* [class.protected]
1003 When a friend or a member function of a derived class references
1004 a protected nonstatic member of a base class, an access check
1005 applies in addition to those described earlier in clause
1006 _class.access_.4) Except when forming a pointer to member
1007 (_expr.unary.op_), the access must be through a pointer to,
1008 reference to, or object of the derived class itself (or any class
1009 derived from that class) (_expr.ref_). If the access is to form
1010 a pointer to member, the nested-name-specifier shall name the
1011 derived class (or any class derived from that class). */
1012 if (DECL_NONSTATIC_MEMBER_P (decl
))
1014 /* We can tell through what the reference is occurring by
1015 chasing BINFO up to the root. */
1017 while (BINFO_INHERITANCE_CHAIN (t
))
1018 t
= BINFO_INHERITANCE_CHAIN (t
);
1020 if (!DERIVED_FROM_P (derived
, BINFO_TYPE (t
)))
1027 /* Returns non-zero if SCOPE is a friend of a type which would be able
1028 to acces DECL, named in TYPE, through the object indicated by
1032 friend_accessible_p (scope
, type
, decl
, binfo
)
1038 tree befriending_classes
;
1044 if (TREE_CODE (scope
) == FUNCTION_DECL
1045 || DECL_FUNCTION_TEMPLATE_P (scope
))
1046 befriending_classes
= DECL_BEFRIENDING_CLASSES (scope
);
1047 else if (TYPE_P (scope
))
1048 befriending_classes
= CLASSTYPE_BEFRIENDING_CLASSES (scope
);
1052 for (t
= befriending_classes
; t
; t
= TREE_CHAIN (t
))
1053 if (protected_accessible_p (type
, decl
, TREE_VALUE (t
), binfo
))
1056 if (TREE_CODE (scope
) == FUNCTION_DECL
1057 || DECL_FUNCTION_TEMPLATE_P (scope
))
1059 /* Perhaps this SCOPE is a member of a class which is a
1061 if (friend_accessible_p (DECL_CLASS_CONTEXT (scope
), type
,
1065 /* Or an instantiation of something which is a friend. */
1066 if (DECL_TEMPLATE_INFO (scope
))
1067 return friend_accessible_p (DECL_TI_TEMPLATE (scope
),
1070 else if (CLASSTYPE_TEMPLATE_INFO (scope
))
1071 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope
),
1077 /* DECL is a declaration from a base class of TYPE, which was the
1078 classs used to name DECL. Return non-zero if, in the current
1079 context, DECL is accessible. If TYPE is actually a BINFO node,
1080 then we can tell in what context the access is occurring by looking
1081 at the most derived class along the path indicated by BINFO. */
1084 accessible_p (type
, decl
)
1092 /* Non-zero if it's OK to access DECL if it has protected
1093 accessibility in TYPE. */
1094 int protected_ok
= 0;
1096 /* If we're not checking access, everything is accessible. */
1097 if (!flag_access_control
)
1100 /* If this declaration is in a block or namespace scope, there's no
1102 if (!TYPE_P (context_for_name_lookup (decl
)))
1105 /* We don't do access control for types yet. */
1106 if (TREE_CODE (decl
) == TYPE_DECL
)
1112 type
= BINFO_TYPE (type
);
1115 binfo
= TYPE_BINFO (type
);
1117 /* [class.access.base]
1119 A member m is accessible when named in class N if
1121 --m as a member of N is public, or
1123 --m as a member of N is private, and the reference occurs in a
1124 member or friend of class N, or
1126 --m as a member of N is protected, and the reference occurs in a
1127 member or friend of class N, or in a member or friend of a
1128 class P derived from N, where m as a member of P is private or
1131 --there exists a base class B of N that is accessible at the point
1132 of reference, and m is accessible when named in class B.
1134 We walk the base class hierarchy, checking these conditions. */
1136 /* Figure out where the reference is occurring. Check to see if
1137 DECL is private or protected in this scope, since that will
1138 determine whether protected access in TYPE allowed. */
1139 if (current_class_type
)
1141 = protected_accessible_p (type
, decl
, current_class_type
,
1144 /* Now, loop through the classes of which we are a friend. */
1146 protected_ok
= friend_accessible_p (current_scope (),
1149 /* Standardize on the same that will access_in_type will use. We
1150 don't need to know what path was chosen from this point onwards. */
1151 binfo
= TYPE_BINFO (type
);
1153 /* Compute the accessibility of DECL in the class hierarchy
1154 dominated by type. */
1155 access_in_type (type
, decl
);
1156 /* Walk the hierarchy again, looking for a base class that allows
1158 t
= dfs_walk (binfo
, dfs_accessible_p
,
1159 dfs_accessible_queue_p
,
1160 protected_ok
? &protected_ok
: 0);
1161 /* Clear any mark bits. Note that we have to walk the whole tree
1162 here, since we have aborted the previous walk from some point
1163 deep in the tree. */
1164 dfs_walk (binfo
, dfs_unmark
, dfs_canonical_queue
, 0);
1165 assert_canonical_unmarked (binfo
);
1167 return t
!= NULL_TREE
;
1170 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1171 found as a base class and sub-object of the object denoted by
1172 BINFO. This routine relies upon binfos not being shared, except
1173 for binfos for virtual bases. */
1176 is_subobject_of_p (parent
, binfo
)
1182 /* We want to canonicalize for comparison purposes. But, when we
1183 iterate through basetypes later, we want the binfos from the
1184 original hierarchy. That's why we have to calculate BINFOS
1185 first, and then canonicalize. */
1186 binfos
= BINFO_BASETYPES (binfo
);
1187 parent
= canonical_binfo (parent
);
1188 binfo
= canonical_binfo (binfo
);
1190 if (parent
== binfo
)
1193 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1195 /* Process and/or queue base types. */
1196 for (i
= 0; i
< n_baselinks
; i
++)
1198 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1199 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo
)))
1200 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1201 class there's no way to descend into it. */
1204 if (is_subobject_of_p (parent
, base_binfo
))
1210 /* See if a one FIELD_DECL hides another. This routine is meant to
1211 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1212 binfos given are the binfos corresponding to the particular places
1213 the FIELD_DECLs are found. This routine relies upon binfos not
1214 being shared, except for virtual bases. */
1217 hides (hider_binfo
, hidee_binfo
)
1218 tree hider_binfo
, hidee_binfo
;
1220 /* hider hides hidee, if hider has hidee as a base class and
1221 the instance of hidee is a sub-object of hider. The first
1222 part is always true is the second part is true.
1224 When hider and hidee are the same (two ways to get to the exact
1225 same member) we consider either one as hiding the other. */
1226 return is_subobject_of_p (hidee_binfo
, hider_binfo
);
1229 /* Very similar to lookup_fnfields_1 but it ensures that at least one
1230 function was declared inside the class given by TYPE. It really should
1231 only return functions that match the given TYPE. */
1234 lookup_fnfields_here (type
, name
)
1237 int idx
= lookup_fnfields_1 (type
, name
);
1240 /* ctors and dtors are always only in the right class. */
1243 fndecls
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1246 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls
)))
1247 == TYPE_MAIN_VARIANT (type
))
1249 fndecls
= OVL_CHAIN (fndecls
);
1254 struct lookup_field_info
{
1255 /* The type in which we're looking. */
1257 /* The name of the field for which we're looking. */
1259 /* If non-NULL, the current result of the lookup. */
1261 /* The path to RVAL. */
1263 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1266 /* If non-zero, we are looking for types, not data members. */
1268 /* If non-zero, RVAL was found by looking through a dependent base. */
1269 int from_dep_base_p
;
1270 /* If something went wrong, a message indicating what. */
1274 /* Returns non-zero if BINFO is not hidden by the value found by the
1275 lookup so far. If BINFO is hidden, then there's no need to look in
1276 it. DATA is really a struct lookup_field_info. Called from
1277 lookup_field via breadth_first_search. */
1280 lookup_field_queue_p (binfo
, data
)
1284 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1286 /* Don't look for constructors or destructors in base classes. */
1287 if (lfi
->name
== ctor_identifier
|| lfi
->name
== dtor_identifier
)
1290 /* If this base class is hidden by the best-known value so far, we
1291 don't need to look. */
1292 if (!lfi
->from_dep_base_p
&& lfi
->rval_binfo
1293 && hides (lfi
->rval_binfo
, binfo
))
1296 if (TREE_VIA_VIRTUAL (binfo
))
1297 return BINFO_FOR_VBASE (BINFO_TYPE (binfo
), lfi
->type
);
1302 /* Within the scope of a template class, you can refer to the to the
1303 current specialization with the name of the template itself. For
1306 template <typename T> struct S { S* sp; }
1308 Returns non-zero if DECL is such a declaration in a class TYPE. */
1311 template_self_reference_p (type
, decl
)
1315 return (CLASSTYPE_USE_TEMPLATE (type
)
1316 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
1317 && TREE_CODE (decl
) == TYPE_DECL
1318 && DECL_ARTIFICIAL (decl
)
1319 && DECL_NAME (decl
) == constructor_name (type
));
1322 /* DATA is really a struct lookup_field_info. Look for a field with
1323 the name indicated there in BINFO. If this function returns a
1324 non-NULL value it is the result of the lookup. Called from
1325 lookup_field via breadth_first_search. */
1328 lookup_field_r (binfo
, data
)
1332 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1333 tree type
= BINFO_TYPE (binfo
);
1334 tree nval
= NULL_TREE
;
1335 int from_dep_base_p
;
1337 /* First, look for a function. There can't be a function and a data
1338 member with the same name, and if there's a function and a type
1339 with the same name, the type is hidden by the function. */
1340 if (!lfi
->want_type
)
1342 int idx
= lookup_fnfields_here (type
, lfi
->name
);
1344 nval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1348 /* Look for a data member or type. */
1349 nval
= lookup_field_1 (type
, lfi
->name
);
1351 /* If there is no declaration with the indicated name in this type,
1352 then there's nothing to do. */
1356 /* If we're looking up a type (as with an elaborated type specifier)
1357 we ignore all non-types we find. */
1358 if (lfi
->want_type
&& TREE_CODE (nval
) != TYPE_DECL
)
1360 nval
= purpose_member (lfi
->name
, CLASSTYPE_TAGS (type
));
1362 nval
= TYPE_MAIN_DECL (TREE_VALUE (nval
));
1367 /* You must name a template base class with a template-id. */
1368 if (!same_type_p (type
, lfi
->type
)
1369 && template_self_reference_p (type
, nval
))
1372 from_dep_base_p
= dependent_base_p (binfo
);
1373 if (lfi
->from_dep_base_p
&& !from_dep_base_p
)
1375 /* If the new declaration is not found via a dependent base, and
1376 the old one was, then we must prefer the new one. We weren't
1377 really supposed to be able to find the old one, so we don't
1378 want to be affected by a specialization. Consider:
1380 struct B { typedef int I; };
1381 template <typename T> struct D1 : virtual public B {};
1382 template <typename T> struct D :
1383 public D1, virtual pubic B { I i; };
1385 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1386 D1 is specialized. */
1387 lfi
->from_dep_base_p
= 0;
1388 lfi
->rval
= NULL_TREE
;
1389 lfi
->rval_binfo
= NULL_TREE
;
1390 lfi
->ambiguous
= NULL_TREE
;
1393 else if (lfi
->rval_binfo
&& !lfi
->from_dep_base_p
&& from_dep_base_p
)
1394 /* Similarly, if the old declaration was not found via a dependent
1395 base, and the new one is, ignore the new one. */
1398 /* If the lookup already found a match, and the new value doesn't
1399 hide the old one, we might have an ambiguity. */
1400 if (lfi
->rval_binfo
&& !hides (binfo
, lfi
->rval_binfo
))
1402 if (nval
== lfi
->rval
&& SHARED_MEMBER_P (nval
))
1403 /* The two things are really the same. */
1405 else if (hides (lfi
->rval_binfo
, binfo
))
1406 /* The previous value hides the new one. */
1410 /* We have a real ambiguity. We keep a chain of all the
1412 if (!lfi
->ambiguous
&& lfi
->rval
)
1414 /* This is the first time we noticed an ambiguity. Add
1415 what we previously thought was a reasonable candidate
1417 lfi
->ambiguous
= tree_cons (NULL_TREE
, lfi
->rval
, NULL_TREE
);
1418 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1421 /* Add the new value. */
1422 lfi
->ambiguous
= tree_cons (NULL_TREE
, nval
, lfi
->ambiguous
);
1423 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1424 lfi
->errstr
= "request for member `%D' is ambiguous";
1429 /* If the thing we're looking for is a virtual base class, then
1430 we know we've got what we want at this point; there's no way
1431 to get an ambiguity. */
1432 if (VBASE_NAME_P (lfi
->name
))
1438 if (from_dep_base_p
&& TREE_CODE (nval
) != TYPE_DECL
1439 /* We need to return a member template class so we can
1440 define partial specializations. Is there a better
1442 && !DECL_CLASS_TEMPLATE_P (nval
))
1443 /* The thing we're looking for isn't a type, so the implicit
1444 typename extension doesn't apply, so we just pretend we
1445 didn't find anything. */
1449 lfi
->from_dep_base_p
= from_dep_base_p
;
1450 lfi
->rval_binfo
= binfo
;
1456 /* Look for a memer named NAME in an inheritance lattice dominated by
1457 XBASETYPE. PROTECT is 0 or two, we do not check access. If it is
1458 1, we enforce accessibility. If PROTECT is zero, then, for an
1459 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
1460 error message. If PROTECT is 2, we return a TREE_LIST whose
1461 TREEE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
1462 ambiguous candidates.
1464 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1465 TYPE_DECL can be found return NULL_TREE. */
1468 lookup_member (xbasetype
, name
, protect
, want_type
)
1469 register tree xbasetype
, name
;
1470 int protect
, want_type
;
1472 tree rval
, rval_binfo
= NULL_TREE
;
1473 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1474 struct lookup_field_info lfi
;
1476 /* rval_binfo is the binfo associated with the found member, note,
1477 this can be set with useful information, even when rval is not
1478 set, because it must deal with ALL members, not just non-function
1479 members. It is used for ambiguity checking and the hidden
1480 checks. Whereas rval is only set if a proper (not hidden)
1481 non-function member is found. */
1483 const char *errstr
= 0;
1485 if (xbasetype
== current_class_type
&& TYPE_BEING_DEFINED (xbasetype
)
1486 && IDENTIFIER_CLASS_VALUE (name
))
1488 tree field
= IDENTIFIER_CLASS_VALUE (name
);
1489 if (TREE_CODE (field
) != FUNCTION_DECL
1490 && ! (want_type
&& TREE_CODE (field
) != TYPE_DECL
))
1491 /* We're in the scope of this class, and the value has already
1492 been looked up. Just return the cached value. */
1496 if (TREE_CODE (xbasetype
) == TREE_VEC
)
1498 type
= BINFO_TYPE (xbasetype
);
1499 basetype_path
= xbasetype
;
1501 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype
)))
1504 basetype_path
= TYPE_BINFO (type
);
1505 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
,
1509 my_friendly_abort (97);
1511 complete_type (type
);
1513 #ifdef GATHER_STATISTICS
1514 n_calls_lookup_field
++;
1515 #endif /* GATHER_STATISTICS */
1517 bzero ((PTR
) &lfi
, sizeof (lfi
));
1520 lfi
.want_type
= want_type
;
1521 bfs_walk (basetype_path
, &lookup_field_r
, &lookup_field_queue_p
, &lfi
);
1523 rval_binfo
= lfi
.rval_binfo
;
1525 type
= BINFO_TYPE (rval_binfo
);
1526 errstr
= lfi
.errstr
;
1528 /* If we are not interested in ambiguities, don't report them;
1529 just return NULL_TREE. */
1530 if (!protect
&& lfi
.ambiguous
)
1536 return lfi
.ambiguous
;
1543 In the case of overloaded function names, access control is
1544 applied to the function selected by overloaded resolution. */
1545 if (rval
&& protect
&& !is_overloaded_fn (rval
)
1546 && !enforce_access (xbasetype
, rval
))
1547 return error_mark_node
;
1549 if (errstr
&& protect
)
1551 cp_error (errstr
, name
, type
);
1553 print_candidates (lfi
.ambiguous
);
1554 rval
= error_mark_node
;
1557 /* If the thing we found was found via the implicit typename
1558 extension, build the typename type. */
1559 if (rval
&& lfi
.from_dep_base_p
&& !DECL_CLASS_TEMPLATE_P (rval
))
1560 rval
= TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path
),
1564 if (rval
&& is_overloaded_fn (rval
))
1566 rval
= tree_cons (basetype_path
, rval
, NULL_TREE
);
1567 SET_BASELINK_P (rval
);
1573 /* Like lookup_member, except that if we find a function member we
1574 return NULL_TREE. */
1577 lookup_field (xbasetype
, name
, protect
, want_type
)
1578 register tree xbasetype
, name
;
1579 int protect
, want_type
;
1581 tree rval
= lookup_member (xbasetype
, name
, protect
, want_type
);
1583 /* Ignore functions. */
1584 if (rval
&& TREE_CODE (rval
) == TREE_LIST
)
1590 /* Like lookup_member, except that if we find a non-function member we
1591 return NULL_TREE. */
1594 lookup_fnfields (xbasetype
, name
, protect
)
1595 register tree xbasetype
, name
;
1598 tree rval
= lookup_member (xbasetype
, name
, protect
, /*want_type=*/0);
1600 /* Ignore non-functions. */
1601 if (rval
&& TREE_CODE (rval
) != TREE_LIST
)
1607 /* TYPE is a class type. Return the index of the fields within
1608 the method vector with name NAME, or -1 is no such field exists. */
1611 lookup_fnfields_1 (type
, name
)
1615 = CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
1617 if (method_vec
!= 0)
1620 register tree
*methods
= &TREE_VEC_ELT (method_vec
, 0);
1621 int len
= TREE_VEC_LENGTH (method_vec
);
1624 #ifdef GATHER_STATISTICS
1625 n_calls_lookup_fnfields_1
++;
1626 #endif /* GATHER_STATISTICS */
1628 /* Constructors are first... */
1629 if (name
== ctor_identifier
)
1630 return methods
[0] ? 0 : -1;
1632 /* and destructors are second. */
1633 if (name
== dtor_identifier
)
1634 return methods
[1] ? 1 : -1;
1636 for (i
= 2; i
< len
&& methods
[i
]; ++i
)
1638 #ifdef GATHER_STATISTICS
1639 n_outer_fields_searched
++;
1640 #endif /* GATHER_STATISTICS */
1642 tmp
= OVL_CURRENT (methods
[i
]);
1643 if (DECL_NAME (tmp
) == name
)
1646 /* If the type is complete and we're past the conversion ops,
1647 switch to binary search. */
1648 if (! DECL_CONV_FN_P (tmp
)
1649 && TYPE_SIZE (type
))
1651 int lo
= i
+ 1, hi
= len
;
1657 #ifdef GATHER_STATISTICS
1658 n_outer_fields_searched
++;
1659 #endif /* GATHER_STATISTICS */
1661 tmp
= DECL_NAME (OVL_CURRENT (methods
[i
]));
1665 else if (tmp
< name
)
1674 /* If we didn't find it, it might have been a template
1675 conversion operator. (Note that we don't look for this case
1676 above so that we will always find specializations first.) */
1677 if (IDENTIFIER_TYPENAME_P (name
))
1679 for (i
= 2; i
< len
&& methods
[i
]; ++i
)
1681 tmp
= OVL_CURRENT (methods
[i
]);
1682 if (! DECL_CONV_FN_P (tmp
))
1684 /* Since all conversion operators come first, we know
1685 there is no such operator. */
1688 else if (TREE_CODE (tmp
) == TEMPLATE_DECL
)
1697 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1698 type in the hierarchy, in a breadth-first preorder traversal. .
1699 If it ever returns a non-NULL value, that value is immediately
1700 returned and the walk is terminated. At each node FN, is passed a
1701 BINFO indicating the path from the curently visited base-class to
1702 TYPE. The TREE_CHAINs of the BINFOs may be used for scratch space;
1703 they are otherwise unused. Before each base-class is walked QFN is
1704 called. If the value returned is non-zero, the base-class is
1705 walked; otherwise it is not. If QFN is NULL, it is treated as a
1706 function which always returns 1. Both FN and QFN are passed the
1707 DATA whenever they are called. */
1710 bfs_walk (binfo
, fn
, qfn
, data
)
1712 tree (*fn
) PROTO((tree
, void *));
1713 tree (*qfn
) PROTO((tree
, void *));
1718 tree rval
= NULL_TREE
;
1719 /* An array of the base classes of BINFO. These will be built up in
1720 breadth-first order, except where QFN prunes the search. */
1721 varray_type bfs_bases
;
1723 /* Start with enough room for ten base classes. That will be enough
1724 for most hierarchies. */
1725 VARRAY_TREE_INIT (bfs_bases
, 10, "search_stack");
1727 /* Put the first type into the stack. */
1728 VARRAY_TREE (bfs_bases
, 0) = binfo
;
1731 for (head
= 0; head
< tail
; ++head
)
1737 /* Pull the next type out of the queue. */
1738 binfo
= VARRAY_TREE (bfs_bases
, head
);
1740 /* If this is the one we're looking for, we're done. */
1741 rval
= (*fn
) (binfo
, data
);
1745 /* Queue up the base types. */
1746 binfos
= BINFO_BASETYPES (binfo
);
1747 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1748 for (i
= 0; i
< n_baselinks
; i
++)
1750 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1753 base_binfo
= (*qfn
) (base_binfo
, data
);
1757 if (tail
== VARRAY_SIZE (bfs_bases
))
1758 VARRAY_GROW (bfs_bases
, 2 * VARRAY_SIZE (bfs_bases
));
1759 VARRAY_TREE (bfs_bases
, tail
) = base_binfo
;
1766 VARRAY_FREE (bfs_bases
);
1771 /* Exactly like bfs_walk, except that a depth-first traversal is
1772 performed, and PREFN is called in preorder, while POSTFN is called
1776 dfs_walk_real (binfo
, prefn
, postfn
, qfn
, data
)
1778 tree (*prefn
) PROTO((tree
, void *));
1779 tree (*postfn
) PROTO((tree
, void *));
1780 tree (*qfn
) PROTO((tree
, void *));
1786 tree rval
= NULL_TREE
;
1788 /* Call the pre-order walking function. */
1791 rval
= (*prefn
) (binfo
, data
);
1796 /* Process the basetypes. */
1797 binfos
= BINFO_BASETYPES (binfo
);
1798 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1799 for (i
= 0; i
< n_baselinks
; i
++)
1801 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1804 base_binfo
= (*qfn
) (base_binfo
, data
);
1808 rval
= dfs_walk_real (base_binfo
, prefn
, postfn
, qfn
, data
);
1814 /* Call the post-order walking function. */
1816 rval
= (*postfn
) (binfo
, data
);
1821 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1825 dfs_walk (binfo
, fn
, qfn
, data
)
1827 tree (*fn
) PROTO((tree
, void *));
1828 tree (*qfn
) PROTO((tree
, void *));
1831 return dfs_walk_real (binfo
, 0, fn
, qfn
, data
);
1836 /* The name of the function we are looking for. */
1838 /* The overloaded functions we have found. */
1842 /* Called from get_virtuals_named_this via bfs_walk. */
1845 get_virtuals_named_this_r (binfo
, data
)
1849 struct gvnt_info
*gvnti
= (struct gvnt_info
*) data
;
1850 tree type
= BINFO_TYPE (binfo
);
1853 idx
= lookup_fnfields_here (BINFO_TYPE (binfo
), gvnti
->name
);
1857 TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
),
1863 /* Return the virtual functions with the indicated NAME in the type
1864 indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE
1865 indicates the base class from which the TREE_VALUE (an OVERLOAD or
1866 just a FUNCTION_DECL) originated. */
1869 get_virtuals_named_this (binfo
, name
)
1873 struct gvnt_info gvnti
;
1877 gvnti
.fields
= NULL_TREE
;
1879 bfs_walk (binfo
, get_virtuals_named_this_r
, 0, &gvnti
);
1881 /* Get to the function decls, and return the first virtual function
1882 with this name, if there is one. */
1883 for (fields
= gvnti
.fields
; fields
; fields
= next_baselink (fields
))
1887 for (fndecl
= TREE_VALUE (fields
); fndecl
; fndecl
= OVL_NEXT (fndecl
))
1888 if (DECL_VINDEX (OVL_CURRENT (fndecl
)))
1895 get_virtual_destructor (binfo
, data
)
1897 void *data ATTRIBUTE_UNUSED
;
1899 tree type
= BINFO_TYPE (binfo
);
1900 if (TYPE_HAS_DESTRUCTOR (type
)
1901 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1)))
1902 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1);
1907 tree_has_any_destructor_p (binfo
, data
)
1909 void *data ATTRIBUTE_UNUSED
;
1911 tree type
= BINFO_TYPE (binfo
);
1912 return TYPE_NEEDS_DESTRUCTOR (type
) ? binfo
: NULL_TREE
;
1915 /* Returns > 0 if a function with type DRETTYPE overriding a function
1916 with type BRETTYPE is covariant, as defined in [class.virtual].
1918 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1919 adjustment), or -1 if pedantically invalid covariance. */
1922 covariant_return_p (brettype
, drettype
)
1923 tree brettype
, drettype
;
1927 if (TREE_CODE (brettype
) == FUNCTION_DECL
1928 || TREE_CODE (brettype
) == THUNK_DECL
)
1930 brettype
= TREE_TYPE (TREE_TYPE (brettype
));
1931 drettype
= TREE_TYPE (TREE_TYPE (drettype
));
1933 else if (TREE_CODE (brettype
) == METHOD_TYPE
)
1935 brettype
= TREE_TYPE (brettype
);
1936 drettype
= TREE_TYPE (drettype
);
1939 if (same_type_p (brettype
, drettype
))
1942 if (! (TREE_CODE (brettype
) == TREE_CODE (drettype
)
1943 && (TREE_CODE (brettype
) == POINTER_TYPE
1944 || TREE_CODE (brettype
) == REFERENCE_TYPE
)
1945 && TYPE_QUALS (brettype
) == TYPE_QUALS (drettype
)))
1948 if (! can_convert (brettype
, drettype
))
1951 brettype
= TREE_TYPE (brettype
);
1952 drettype
= TREE_TYPE (drettype
);
1954 /* If not pedantic, allow any standard pointer conversion. */
1955 if (! IS_AGGR_TYPE (drettype
) || ! IS_AGGR_TYPE (brettype
))
1958 binfo
= get_binfo (brettype
, drettype
, 1);
1960 /* If we get an error_mark_node from get_binfo, it already complained,
1961 so let's just succeed. */
1962 if (binfo
== error_mark_node
)
1965 if (! BINFO_OFFSET_ZEROP (binfo
) || TREE_VIA_VIRTUAL (binfo
))
1970 /* Check that virtual overrider OVERRIDER is acceptable for base function
1971 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1974 check_final_overrider (overrider
, basefn
)
1975 tree overrider
, basefn
;
1977 tree over_type
= TREE_TYPE (overrider
);
1978 tree base_type
= TREE_TYPE (basefn
);
1979 tree over_return
= TREE_TYPE (over_type
);
1980 tree base_return
= TREE_TYPE (base_type
);
1981 tree over_throw
= TYPE_RAISES_EXCEPTIONS (over_type
);
1982 tree base_throw
= TYPE_RAISES_EXCEPTIONS (base_type
);
1985 if (same_type_p (base_return
, over_return
))
1987 else if ((i
= covariant_return_p (base_return
, over_return
)))
1990 sorry ("adjusting pointers for covariant returns");
1992 if (pedantic
&& i
== -1)
1994 cp_pedwarn_at ("invalid covariant return type for `virtual %#D'", overrider
);
1995 cp_pedwarn_at (" overriding `virtual %#D' (must be pointer or reference to class)", basefn
);
1998 else if (IS_AGGR_TYPE_2 (base_return
, over_return
)
1999 && same_or_base_type_p (base_return
, over_return
))
2001 cp_error_at ("invalid covariant return type for `virtual %#D'", overrider
);
2002 cp_error_at (" overriding `virtual %#D' (must use pointer or reference)", basefn
);
2005 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
)) == NULL_TREE
)
2007 cp_error_at ("conflicting return type specified for `virtual %#D'", overrider
);
2008 cp_error_at (" overriding `virtual %#D'", basefn
);
2009 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
),
2010 DECL_CLASS_CONTEXT (overrider
));
2014 /* Check throw specifier is subset. */
2015 /* XXX At the moment, punt on an overriding artificial function. We
2016 don't generate its exception specifier, so can't check it properly. */
2017 if (! DECL_ARTIFICIAL (overrider
)
2018 && !comp_except_specs (base_throw
, over_throw
, 0))
2020 cp_error_at ("looser throw specifier for `virtual %#F'", overrider
);
2021 cp_error_at (" overriding `virtual %#F'", basefn
);
2027 /* Given a class type TYPE, and a function decl FNDECL, look for a
2028 virtual function in TYPE's hierarchy which FNDECL could match as a
2029 virtual function. It doesn't matter which one we find.
2031 DTORP is nonzero if we are looking for a destructor. Destructors
2032 need special treatment because they do not match by name. */
2035 get_matching_virtual (binfo
, fndecl
, dtorp
)
2039 tree tmp
= NULL_TREE
;
2041 if (TREE_CODE (fndecl
) == TEMPLATE_DECL
)
2042 /* In [temp.mem] we have:
2044 A specialization of a member function template does not
2045 override a virtual function from a base class. */
2048 /* Breadth first search routines start searching basetypes
2049 of TYPE, so we must perform first ply of search here. */
2051 return bfs_walk (binfo
, get_virtual_destructor
,
2052 tree_has_any_destructor_p
, 0);
2055 tree drettype
, dtypes
, btypes
, instptr_type
;
2056 tree baselink
, best
= NULL_TREE
;
2057 tree declarator
= DECL_NAME (fndecl
);
2058 if (IDENTIFIER_VIRTUAL_P (declarator
) == 0)
2061 baselink
= get_virtuals_named_this (binfo
, declarator
);
2062 if (baselink
== NULL_TREE
)
2065 drettype
= TREE_TYPE (TREE_TYPE (fndecl
));
2066 dtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2067 if (DECL_STATIC_FUNCTION_P (fndecl
))
2068 instptr_type
= NULL_TREE
;
2070 instptr_type
= TREE_TYPE (TREE_VALUE (dtypes
));
2072 for (; baselink
; baselink
= next_baselink (baselink
))
2075 for (tmps
= TREE_VALUE (baselink
); tmps
; tmps
= OVL_NEXT (tmps
))
2077 tmp
= OVL_CURRENT (tmps
);
2078 if (! DECL_VINDEX (tmp
))
2081 btypes
= TYPE_ARG_TYPES (TREE_TYPE (tmp
));
2082 if (instptr_type
== NULL_TREE
)
2084 if (compparms (TREE_CHAIN (btypes
), dtypes
))
2085 /* Caller knows to give error in this case. */
2090 if (/* The first parameter is the `this' parameter,
2091 which has POINTER_TYPE, and we can therefore
2092 safely use TYPE_QUALS, rather than
2094 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes
)))
2095 == TYPE_QUALS (instptr_type
))
2096 && compparms (TREE_CHAIN (btypes
), TREE_CHAIN (dtypes
)))
2098 check_final_overrider (fndecl
, tmp
);
2100 /* FNDECL overrides this function. We continue to
2101 check all the other functions in order to catch
2102 errors; it might be that in some other baseclass
2103 a virtual function was declared with the same
2104 parameter types, but a different return type. */
2114 /* A queue function for dfs_walk that skips any nonprimary virtual
2115 bases and any already marked bases. */
2118 dfs_skip_nonprimary_vbases_unmarkedp (binfo
, data
)
2120 void *data ATTRIBUTE_UNUSED
;
2122 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
2123 /* This is a non-primary virtual base. SKip it. */
2126 return unmarkedp (binfo
, NULL
);
2129 /* A queue function for dfs_walk that skips any nonprimary virtual
2130 bases and any unmarked bases. */
2133 dfs_skip_nonprimary_vbases_markedp (binfo
, data
)
2135 void *data ATTRIBUTE_UNUSED
;
2137 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
2138 /* This is a non-primary virtual base. SKip it. */
2141 return markedp (binfo
, NULL
);
2144 /* Called via dfs_walk from mark_primary_bases. */
2147 dfs_mark_primary_bases (binfo
, data
)
2154 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo
)))
2157 i
= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2158 base_binfo
= BINFO_BASETYPE (binfo
, i
);
2160 if (!TREE_VIA_VIRTUAL (base_binfo
))
2161 /* Non-virtual base classes are easy. */
2162 BINFO_PRIMARY_MARKED_P (base_binfo
) = 1;
2168 = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo
), (tree
) data
);
2170 /* If this virtual base is not already primary somewhere else in
2171 the hiearchy, then we'll be using this copy. */
2172 if (!BINFO_VBASE_PRIMARY_P (shared_binfo
)
2173 && !BINFO_VBASE_MARKED (shared_binfo
))
2175 BINFO_VBASE_PRIMARY_P (shared_binfo
) = 1;
2176 BINFO_PRIMARY_MARKED_P (base_binfo
) = 1;
2183 /* Set BINFO_PRIMARY_MARKED_P for all binfos in the hierarchy
2184 dominated by BINFO that are primary bases. */
2187 mark_primary_bases (type
)
2192 /* Mark the TYPE_BINFO hierarchy. */
2193 dfs_walk (TYPE_BINFO (type
), dfs_mark_primary_bases
,
2194 dfs_skip_nonprimary_vbases_unmarkedp
, type
);
2196 /* Now go through the virtual base classes. Any that are not
2197 already primary will need to be allocated in TYPE, and so we need
2198 to mark their primary bases. */
2199 for (vbase
= CLASSTYPE_VBASECLASSES (type
);
2201 vbase
= TREE_CHAIN (vbase
))
2203 if (BINFO_VBASE_PRIMARY_P (vbase
))
2204 /* This virtual base was already included in the hierarchy, so
2205 there's nothing to do here. */
2208 /* Temporarily pretend that VBASE is primary so that its bases
2209 will be walked; this is the real copy of VBASE. */
2210 BINFO_PRIMARY_MARKED_P (vbase
) = 1;
2212 /* Now, walk its bases. */
2213 dfs_walk (vbase
, dfs_mark_primary_bases
,
2214 dfs_skip_nonprimary_vbases_unmarkedp
, type
);
2216 /* VBASE wasn't really primary. */
2217 BINFO_PRIMARY_MARKED_P (vbase
) = 0;
2218 /* And we don't want to allow it to *become* primary if it is a
2219 base of some subsequent base class. */
2220 SET_BINFO_VBASE_MARKED (vbase
);
2223 /* Clear the VBASE_MARKED bits we set above. */
2224 for (vbase
= CLASSTYPE_VBASECLASSES (type
);
2226 vbase
= TREE_CHAIN (vbase
))
2227 CLEAR_BINFO_VBASE_MARKED (vbase
);
2230 /* If BINFO is a non-primary virtual baseclass (in the hierarchy
2231 dominated by TYPE), and no primary copy appears anywhere in the
2232 hierarchy, return the shared copy. If a primary copy appears
2233 elsewhere, return NULL_TREE. Otherwise, return BINFO itself; it is
2234 either a non-virtual base or a primary virtual base. */
2237 get_shared_vbase_if_not_primary (binfo
, type
)
2241 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
2243 /* This is a non-primary virtual base. If there is no primary
2244 version, get the shared version. */
2245 binfo
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), type
);
2246 if (BINFO_VBASE_PRIMARY_P (binfo
))
2253 /* A queue function to use with dfs_walk that prevents travel into any
2254 nonprimary virtual base, or its baseclasses. DATA should be the
2255 type of the complete object, or a TREE_LIST whose TREE_PURPOSE is
2256 the type of the complete object. By using this function as a queue
2257 function, you will walk over exactly those BINFOs that actually
2258 exist in the complete object, including those for virtual base
2259 classes. If you SET_BINFO_MARKED for each binfo you process, you
2260 are further guaranteed that you will walk into each virtual base
2261 class exactly once. */
2264 dfs_unmarked_real_bases_queue_p (binfo
, data
)
2268 tree type
= (tree
) data
;
2270 if (TREE_CODE (type
) == TREE_LIST
)
2271 type
= TREE_PURPOSE (type
);
2272 binfo
= get_shared_vbase_if_not_primary (binfo
, type
);
2273 return binfo
? unmarkedp (binfo
, NULL
) : NULL_TREE
;
2276 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2277 that are marked, rather than unmarked. */
2280 dfs_marked_real_bases_queue_p (binfo
, data
)
2284 tree type
= (tree
) data
;
2286 if (TREE_CODE (type
) == TREE_LIST
)
2287 type
= TREE_PURPOSE (type
);
2288 binfo
= get_shared_vbase_if_not_primary (binfo
, type
);
2289 return binfo
? markedp (binfo
, NULL
) : NULL_TREE
;
2292 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2295 dfs_get_pure_virtuals (binfo
, data
)
2299 tree type
= (tree
) data
;
2301 /* We're not interested in primary base classes; the derived class
2302 of which they are a primary base will contain the information we
2304 if (!BINFO_PRIMARY_MARKED_P (binfo
))
2308 for (virtuals
= skip_rtti_stuff (binfo
,
2312 virtuals
= TREE_CHAIN (virtuals
))
2313 if (DECL_PURE_VIRTUAL_P (TREE_VALUE (virtuals
)))
2314 CLASSTYPE_PURE_VIRTUALS (type
)
2315 = tree_cons (NULL_TREE
, TREE_VALUE (virtuals
),
2316 CLASSTYPE_PURE_VIRTUALS (type
));
2319 SET_BINFO_MARKED (binfo
);
2324 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2327 get_pure_virtuals (type
)
2332 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2333 is going to be overridden. */
2334 CLASSTYPE_PURE_VIRTUALS (type
) = NULL_TREE
;
2335 /* Now, run through all the bases which are not primary bases, and
2336 collect the pure virtual functions. We look at the vtable in
2337 each class to determine what pure virtual functions are present.
2338 (A primary base is not interesting because the derived class of
2339 which it is a primary base will contain vtable entries for the
2340 pure virtuals in the base class. */
2341 dfs_walk (TYPE_BINFO (type
), dfs_get_pure_virtuals
,
2342 dfs_unmarked_real_bases_queue_p
, type
);
2343 dfs_walk (TYPE_BINFO (type
), dfs_unmark
,
2344 dfs_marked_real_bases_queue_p
, type
);
2346 /* Put the pure virtuals in dfs order. */
2347 CLASSTYPE_PURE_VIRTUALS (type
) = nreverse (CLASSTYPE_PURE_VIRTUALS (type
));
2349 for (vbases
= CLASSTYPE_VBASECLASSES (type
);
2351 vbases
= TREE_CHAIN (vbases
))
2355 for (virtuals
= skip_rtti_stuff (vbases
, BINFO_TYPE (vbases
), NULL
);
2357 virtuals
= TREE_CHAIN (virtuals
))
2359 tree base_fndecl
= TREE_VALUE (virtuals
);
2360 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl
))
2361 cp_error ("`%#D' needs a final overrider", base_fndecl
);
2367 next_baselink (baselink
)
2370 tree tmp
= TREE_TYPE (baselink
);
2371 baselink
= TREE_CHAIN (baselink
);
2374 /* @@ does not yet add previous base types. */
2375 baselink
= tree_cons (TREE_PURPOSE (tmp
), TREE_VALUE (tmp
),
2377 TREE_TYPE (baselink
) = TREE_TYPE (tmp
);
2378 tmp
= TREE_CHAIN (tmp
);
2383 /* DEPTH-FIRST SEARCH ROUTINES. */
2385 /* This routine converts a pointer to be a pointer of an immediate
2386 base class. The normal convert_pointer_to routine would diagnose
2387 the conversion as ambiguous, under MI code that has the base class
2388 as an ambiguous base class. */
2391 convert_pointer_to_single_level (to_type
, expr
)
2395 tree binfo_of_derived
;
2398 derived
= TREE_TYPE (TREE_TYPE (expr
));
2399 binfo_of_derived
= TYPE_BINFO (derived
);
2400 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived
) == NULL_TREE
,
2402 for (i
= CLASSTYPE_N_BASECLASSES (derived
) - 1; i
>= 0; --i
)
2404 tree binfo
= BINFO_BASETYPE (binfo_of_derived
, i
);
2405 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo
) == binfo_of_derived
,
2407 if (same_type_p (BINFO_TYPE (binfo
), to_type
))
2408 return build_vbase_path (PLUS_EXPR
,
2409 build_pointer_type (to_type
),
2413 my_friendly_abort (19990607);
2420 markedp (binfo
, data
)
2422 void *data ATTRIBUTE_UNUSED
;
2424 return BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2428 unmarkedp (binfo
, data
)
2430 void *data ATTRIBUTE_UNUSED
;
2432 return !BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2436 marked_vtable_pathp (binfo
, data
)
2438 void *data ATTRIBUTE_UNUSED
;
2440 return BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2444 unmarked_vtable_pathp (binfo
, data
)
2446 void *data ATTRIBUTE_UNUSED
;
2448 return !BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2452 marked_new_vtablep (binfo
, data
)
2454 void *data ATTRIBUTE_UNUSED
;
2456 return BINFO_NEW_VTABLE_MARKED (binfo
) ? binfo
: NULL_TREE
;
2460 unmarked_new_vtablep (binfo
, data
)
2462 void *data ATTRIBUTE_UNUSED
;
2464 return !BINFO_NEW_VTABLE_MARKED (binfo
) ? binfo
: NULL_TREE
;
2468 marked_pushdecls_p (binfo
, data
)
2470 void *data ATTRIBUTE_UNUSED
;
2472 return (CLASS_TYPE_P (BINFO_TYPE (binfo
))
2473 && BINFO_PUSHDECLS_MARKED (binfo
)) ? binfo
: NULL_TREE
;
2477 unmarked_pushdecls_p (binfo
, data
)
2479 void *data ATTRIBUTE_UNUSED
;
2481 return (CLASS_TYPE_P (BINFO_TYPE (binfo
))
2482 && !BINFO_PUSHDECLS_MARKED (binfo
)) ? binfo
: NULL_TREE
;
2486 static int dfs_search_slot_nonempty_p (binfo
) tree binfo
;
2487 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) != 0; }
2490 dfs_debug_unmarkedp (binfo
, data
)
2492 void *data ATTRIBUTE_UNUSED
;
2494 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo
))
2495 ? binfo
: NULL_TREE
);
2499 /* The worker functions for `dfs_walk'. These do not need to
2500 test anything (vis a vis marking) if they are paired with
2501 a predicate function (above). */
2505 dfs_mark (binfo
) tree binfo
;
2506 { SET_BINFO_MARKED (binfo
); }
2510 dfs_unmark (binfo
, data
)
2512 void *data ATTRIBUTE_UNUSED
;
2514 CLEAR_BINFO_MARKED (binfo
);
2518 /* Clear both BINFO_MARKED and BINFO_VBASE_MARKED. */
2521 dfs_vbase_unmark (binfo
, data
)
2523 void *data ATTRIBUTE_UNUSED
;
2525 CLEAR_BINFO_VBASE_MARKED (binfo
);
2526 return dfs_unmark (binfo
, data
);
2531 dfs_mark_vtable_path (binfo
) tree binfo
;
2532 { SET_BINFO_VTABLE_PATH_MARKED (binfo
); }
2535 dfs_unmark_vtable_path (binfo
) tree binfo
;
2536 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
); }
2539 dfs_mark_new_vtable (binfo
) tree binfo
;
2540 { SET_BINFO_NEW_VTABLE_MARKED (binfo
); }
2543 dfs_unmark_new_vtable (binfo
) tree binfo
;
2544 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
); }
2547 dfs_clear_search_slot (binfo
) tree binfo
;
2548 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) = 0; }
2550 /* Keep this code around in case we later want to control debug info
2551 based on whether a type is "used". Currently, we only suppress debug
2552 info if we can emit it with the vtable. jason 1999-11-11) */
2554 dfs_debug_mark (binfo
, data
)
2556 void *data ATTRIBUTE_UNUSED
;
2558 tree t
= BINFO_TYPE (binfo
);
2560 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2562 /* If interface info is known, either we've already emitted the debug
2563 info or we don't need to. */
2564 if (CLASSTYPE_INTERFACE_KNOWN (t
))
2567 /* If the class has virtual functions, we'll emit the debug info
2569 if (TYPE_POLYMORPHIC_P (t
))
2572 /* We cannot rely on some alien method to solve our problems,
2573 so we must write out the debug info ourselves. */
2574 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = 0;
2575 rest_of_type_compilation (t
, toplevel_bindings_p ());
2588 /* Attach to the type of the virtual base class, the pointer to the
2589 virtual base class. */
2592 dfs_find_vbases (binfo
, data
)
2596 struct vbase_info
*vi
= (struct vbase_info
*) data
;
2597 tree binfos
= BINFO_BASETYPES (binfo
);
2598 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2600 for (i
= n_baselinks
-1; i
>= 0; i
--)
2602 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2604 if (TREE_VIA_VIRTUAL (base_binfo
)
2605 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo
)) == 0)
2607 tree vbase
= BINFO_TYPE (base_binfo
);
2608 tree binfo
= binfo_member (vbase
, vi
->vbase_types
);
2610 CLASSTYPE_SEARCH_SLOT (vbase
)
2611 = build (PLUS_EXPR
, build_pointer_type (vbase
),
2612 vi
->decl_ptr
, BINFO_OFFSET (binfo
));
2615 SET_BINFO_VTABLE_PATH_MARKED (binfo
);
2616 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
2622 dfs_init_vbase_pointers (binfo
, data
)
2626 struct vbase_info
*vi
= (struct vbase_info
*) data
;
2627 tree type
= BINFO_TYPE (binfo
);
2629 tree this_vbase_ptr
;
2631 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2633 if (BINFO_INHERITANCE_CHAIN (binfo
))
2635 this_vbase_ptr
= TREE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo
));
2636 if (TREE_VIA_VIRTUAL (binfo
))
2637 this_vbase_ptr
= CLASSTYPE_SEARCH_SLOT (type
);
2639 this_vbase_ptr
= convert_pointer_to_single_level (type
,
2641 TREE_CHAIN (binfo
) = this_vbase_ptr
;
2644 this_vbase_ptr
= TREE_CHAIN (binfo
);
2646 /* We're going to iterate through all the pointers to virtual
2647 base-classes. They come at the beginning of the class. */
2648 fields
= TYPE_FIELDS (type
);
2649 if (fields
== TYPE_VFIELD (type
))
2650 /* If the first field is the vtbl pointer (as happens in the new
2652 fields
= TREE_CHAIN (fields
);
2654 if (fields
== NULL_TREE
2655 || DECL_NAME (fields
) == NULL_TREE
2656 || ! VBASE_NAME_P (DECL_NAME (fields
)))
2659 if (build_pointer_type (type
)
2660 != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr
)))
2661 my_friendly_abort (125);
2663 while (fields
&& DECL_NAME (fields
) && VBASE_NAME_P (DECL_NAME (fields
)))
2665 tree ref
= build (COMPONENT_REF
, TREE_TYPE (fields
),
2666 build_indirect_ref (this_vbase_ptr
, NULL_PTR
), fields
);
2667 tree init
= CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields
)));
2668 vi
->inits
= tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields
)),
2670 build_modify_expr (ref
, NOP_EXPR
, init
),
2672 fields
= TREE_CHAIN (fields
);
2678 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2679 times, just NEW_VTABLE, but optimizer should make both with equal
2680 efficiency (though it does not currently). */
2683 dfs_clear_vbase_slots (binfo
, data
)
2685 void *data ATTRIBUTE_UNUSED
;
2687 tree type
= BINFO_TYPE (binfo
);
2688 CLASSTYPE_SEARCH_SLOT (type
) = 0;
2689 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2690 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2695 init_vbase_pointers (type
, decl_ptr
)
2699 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
2701 struct vbase_info vi
;
2702 int old_flag
= flag_this_is_variable
;
2703 tree binfo
= TYPE_BINFO (type
);
2704 flag_this_is_variable
= -2;
2706 /* Find all the virtual base classes, marking them for later
2708 vi
.decl_ptr
= decl_ptr
;
2709 vi
.vbase_types
= CLASSTYPE_VBASECLASSES (type
);
2710 vi
.inits
= NULL_TREE
;
2712 dfs_walk (binfo
, dfs_find_vbases
, unmarked_vtable_pathp
, &vi
);
2714 /* Build up a list of the initializers. */
2715 TREE_CHAIN (binfo
) = decl_ptr
;
2716 dfs_walk_real (binfo
,
2717 dfs_init_vbase_pointers
, 0,
2718 marked_vtable_pathp
,
2721 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
, 0);
2722 flag_this_is_variable
= old_flag
;
2728 /* get the virtual context (the vbase that directly contains the
2729 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2730 or NULL_TREE if there is none.
2732 FNDECL must come from a virtual table from a virtual base to ensure that
2733 there is only one possible DECL_CLASS_CONTEXT.
2735 We know that if there is more than one place (binfo) the fndecl that the
2736 declared, they all refer to the same binfo. See get_class_offset_1 for
2737 the check that ensures this. */
2740 virtual_context (fndecl
, t
, vbase
)
2741 tree fndecl
, t
, vbase
;
2744 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl
), t
, 0, &path
) < 0)
2746 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2747 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl
), vbase
, 0, &path
) >= 0)
2751 /* Not sure if checking path == vbase is necessary here, but just in
2753 if (TREE_VIA_VIRTUAL (path
) || path
== vbase
)
2754 return BINFO_FOR_VBASE (BINFO_TYPE (path
), t
);
2755 path
= BINFO_INHERITANCE_CHAIN (path
);
2758 /* This shouldn't happen, I don't want errors! */
2759 warning ("recoverable compiler error, fixups for virtual function");
2764 if (TREE_VIA_VIRTUAL (path
))
2765 return binfo_member (BINFO_TYPE (path
), CLASSTYPE_VBASECLASSES (t
));
2766 path
= BINFO_INHERITANCE_CHAIN (path
);
2771 /* Fixups upcast offsets for one vtable.
2772 Entries may stay within the VBASE given, or
2773 they may upcast into a direct base, or
2774 they may upcast into a different vbase.
2776 We only need to do fixups in case 2 and 3. In case 2, we add in
2777 the virtual base offset to effect an upcast, in case 3, we add in
2778 the virtual base offset to effect an upcast, then subtract out the
2779 offset for the other virtual base, to effect a downcast into it.
2781 This routine mirrors fixup_vtable_deltas in functionality, though
2782 this one is runtime based, and the other is compile time based.
2783 Conceivably that routine could be removed entirely, and all fixups
2786 VBASE_OFFSETS is an association list of virtual bases that contains
2787 offset information for the virtual bases, so the offsets are only
2788 calculated once. The offsets are computed by where we think the
2789 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2790 the vbase really is. */
2793 expand_upcast_fixups (binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
,
2795 tree binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
, *vbase_offsets
;
2800 unsigned HOST_WIDE_INT n
;
2802 delta
= purpose_member (vbase
, *vbase_offsets
);
2805 delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase
));
2806 delta
= build (MINUS_EXPR
, ptrdiff_type_node
, delta
, vbase_addr
);
2807 delta
= save_expr (delta
);
2808 delta
= tree_cons (vbase
, delta
, *vbase_offsets
);
2809 *vbase_offsets
= delta
;
2812 virtuals
= skip_rtti_stuff (binfo
, BINFO_TYPE (binfo
), &n
);
2816 tree current_fndecl
= TREE_VALUE (virtuals
);
2819 && current_fndecl
!= abort_fndecl
2820 && (vc
=virtual_context (current_fndecl
, t
, vbase
)) != vbase
)
2822 /* This may in fact need a runtime fixup. */
2823 tree idx
= build_int_2 (n
, 0);
2824 tree vtbl
= BINFO_VTABLE (binfo
);
2825 tree nvtbl
= lookup_name (DECL_NAME (vtbl
), 0);
2826 tree aref
, ref
, naref
;
2827 tree old_delta
, new_delta
;
2830 if (nvtbl
== NULL_TREE
2831 || nvtbl
== IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl
)))
2833 /* Dup it if it isn't in local scope yet. */
2835 (VAR_DECL
, DECL_NAME (vtbl
),
2836 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl
)));
2837 DECL_ALIGN (nvtbl
) = MAX (TYPE_ALIGN (double_type_node
),
2838 DECL_ALIGN (nvtbl
));
2839 TREE_READONLY (nvtbl
) = 0;
2840 DECL_ARTIFICIAL (nvtbl
) = 1;
2841 nvtbl
= pushdecl (nvtbl
);
2843 cp_finish_decl (nvtbl
, init
, NULL_TREE
,
2844 LOOKUP_ONLYCONVERTING
);
2846 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2847 because they wouldn't be useful; everything that wants to
2848 look at the vtable will look at the decl for the normal
2849 vtable. Setting DECL_CONTEXT also screws up
2850 decl_function_context. */
2852 init
= build (MODIFY_EXPR
, TREE_TYPE (nvtbl
),
2854 finish_expr_stmt (init
);
2855 /* Update the vtable pointers as necessary. */
2856 ref
= build_vfield_ref
2857 (build_indirect_ref (addr
, NULL_PTR
),
2858 DECL_CONTEXT (TYPE_VFIELD (BINFO_TYPE (binfo
))));
2860 (build_modify_expr (ref
, NOP_EXPR
, nvtbl
));
2862 assemble_external (vtbl
);
2863 aref
= build_array_ref (vtbl
, idx
);
2864 naref
= build_array_ref (nvtbl
, idx
);
2865 old_delta
= build_component_ref (aref
, delta_identifier
,
2867 new_delta
= build_component_ref (naref
, delta_identifier
,
2870 /* This is a upcast, so we have to add the offset for the
2872 old_delta
= build_binary_op (PLUS_EXPR
, old_delta
,
2873 TREE_VALUE (delta
));
2876 /* If this is set, we need to subtract out the delta
2877 adjustments for the other virtual base that we
2879 tree vc_delta
= purpose_member (vc
, *vbase_offsets
);
2882 tree vc_addr
= convert_pointer_to_real (vc
, orig_addr
);
2883 vc_delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc
));
2884 vc_delta
= build (MINUS_EXPR
, ptrdiff_type_node
,
2886 vc_delta
= save_expr (vc_delta
);
2887 *vbase_offsets
= tree_cons (vc
, vc_delta
, *vbase_offsets
);
2890 vc_delta
= TREE_VALUE (vc_delta
);
2892 /* This is a downcast, so we have to subtract the offset
2893 for the virtual base. */
2894 old_delta
= build_binary_op (MINUS_EXPR
, old_delta
, vc_delta
);
2897 TREE_READONLY (new_delta
) = 0;
2898 TREE_TYPE (new_delta
) =
2899 cp_build_qualified_type (TREE_TYPE (new_delta
),
2900 CP_TYPE_QUALS (TREE_TYPE (new_delta
))
2901 & ~TYPE_QUAL_CONST
);
2902 finish_expr_stmt (build_modify_expr (new_delta
, NOP_EXPR
,
2906 virtuals
= TREE_CHAIN (virtuals
);
2910 /* Fixup upcast offsets for all direct vtables. Patterned after
2911 expand_direct_vtbls_init. */
2914 fixup_virtual_upcast_offsets (real_binfo
, binfo
, init_self
, can_elide
, addr
, orig_addr
, type
, vbase
, vbase_offsets
)
2915 tree real_binfo
, binfo
;
2916 int init_self
, can_elide
;
2917 tree addr
, orig_addr
, type
, vbase
, *vbase_offsets
;
2919 tree real_binfos
= BINFO_BASETYPES (real_binfo
);
2920 tree binfos
= BINFO_BASETYPES (binfo
);
2921 int i
, n_baselinks
= real_binfos
? TREE_VEC_LENGTH (real_binfos
) : 0;
2923 for (i
= 0; i
< n_baselinks
; i
++)
2925 tree real_base_binfo
= TREE_VEC_ELT (real_binfos
, i
);
2926 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2927 int is_not_base_vtable
2928 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo
));
2929 if (! TREE_VIA_VIRTUAL (real_base_binfo
))
2930 fixup_virtual_upcast_offsets (real_base_binfo
, base_binfo
,
2931 is_not_base_vtable
, can_elide
, addr
,
2932 orig_addr
, type
, vbase
, vbase_offsets
);
2935 /* Before turning this on, make sure it is correct. */
2936 if (can_elide
&& ! BINFO_MODIFIED (binfo
))
2939 /* Should we use something besides CLASSTYPE_VFIELDS? */
2940 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo
)))
2942 tree new_addr
= convert_pointer_to_real (binfo
, addr
);
2943 expand_upcast_fixups (real_binfo
, new_addr
, orig_addr
, vbase
, addr
,
2944 type
, vbase_offsets
);
2948 /* Fixup all the virtual upcast offsets for TYPE. DECL_PTR is the
2949 address of the sub-object being initialized. */
2952 fixup_all_virtual_upcast_offsets (type
, decl_ptr
)
2957 tree in_charge_node
;
2960 /* Only tweak the vtables if we're in charge. */
2961 in_charge_node
= current_in_charge_parm
;
2962 if (!in_charge_node
)
2963 /* There's no need for any fixups in this case. */
2965 in_charge_node
= build_binary_op (EQ_EXPR
,
2966 in_charge_node
, integer_zero_node
);
2967 if_stmt
= begin_if_stmt ();
2968 finish_if_stmt_cond (in_charge_node
, if_stmt
);
2970 /* Iterate through the virtual bases, fixing up the upcast offset
2972 for (vbases
= CLASSTYPE_VBASECLASSES (type
);
2974 vbases
= TREE_CHAIN (vbases
))
2976 if (flag_vtable_thunks
)
2977 /* We don't have dynamic thunks yet! So for now, just fail
2985 vbase_offsets
= NULL_TREE
;
2986 addr
= convert_pointer_to_vbase (TREE_TYPE (vbases
), decl_ptr
);
2987 fixup_virtual_upcast_offsets (vbases
,
2988 TYPE_BINFO (BINFO_TYPE (vbases
)),
2989 1, 0, addr
, decl_ptr
,
2990 type
, vbases
, &vbase_offsets
);
2994 /* Close out the if-statement. */
2995 finish_then_clause (if_stmt
);
2999 /* Generate the code needed to initialize all the virtual function
3000 table slots of all the virtual baseclasses. BINFO is the binfo
3001 which determines the virtual baseclasses to use. TRUE_EXP is the
3002 true object we are initializing, and DECL_PTR is the pointer to the
3003 sub-object we are initializing. */
3006 expand_indirect_vtbls_init (binfo
, decl_ptr
)
3010 tree type
= BINFO_TYPE (binfo
);
3012 /* This function executes during the finish_function() segment,
3013 AFTER the auto variables and temporary stack space has been marked
3014 unused...If space is needed for the virtual function tables,
3015 some of them might fit within what the compiler now thinks
3016 are available stack slots... These values are actually initialized at
3017 the beginnning of the function, so when the automatics use their space,
3018 they will overwrite the values that are placed here. Marking all
3019 temporary space as unavailable prevents this from happening. */
3021 mark_all_temps_used();
3023 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
3025 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
3026 struct vbase_info vi
;
3027 vi
.decl_ptr
= decl_ptr
;
3028 vi
.vbase_types
= vbases
;
3030 dfs_walk (binfo
, dfs_find_vbases
, unmarked_new_vtablep
, &vi
);
3031 fixup_all_virtual_upcast_offsets (type
, vi
.decl_ptr
);
3032 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
, 0);
3036 /* get virtual base class types.
3037 This adds type to the vbase_types list in reverse dfs order.
3038 Ordering is very important, so don't change it. */
3041 dfs_get_vbase_types (binfo
, data
)
3045 tree type
= (tree
) data
;
3047 if (TREE_VIA_VIRTUAL (binfo
) && ! BINFO_VBASE_MARKED (binfo
))
3049 tree new_vbase
= make_binfo (integer_zero_node
,
3051 BINFO_VTABLE (binfo
),
3052 BINFO_VIRTUALS (binfo
));
3053 unshare_base_binfos (new_vbase
);
3054 TREE_VIA_VIRTUAL (new_vbase
) = 1;
3055 BINFO_INHERITANCE_CHAIN (new_vbase
) = TYPE_BINFO (type
);
3056 TREE_CHAIN (new_vbase
) = CLASSTYPE_VBASECLASSES (type
);
3057 CLASSTYPE_VBASECLASSES (type
) = new_vbase
;
3058 SET_BINFO_VBASE_MARKED (binfo
);
3060 SET_BINFO_MARKED (binfo
);
3064 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
3067 get_vbase_types (type
)
3070 CLASSTYPE_VBASECLASSES (type
) = NULL_TREE
;
3071 dfs_walk (TYPE_BINFO (type
), dfs_get_vbase_types
, unmarkedp
, type
);
3072 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
3073 reverse it so that we get normal dfs ordering. */
3074 CLASSTYPE_VBASECLASSES (type
) = nreverse (CLASSTYPE_VBASECLASSES (type
));
3075 dfs_walk (TYPE_BINFO (type
), dfs_vbase_unmark
, markedp
, 0);
3078 /* Debug info for C++ classes can get very large; try to avoid
3079 emitting it everywhere.
3081 Note that this optimization wins even when the target supports
3082 BINCL (if only slightly), and reduces the amount of work for the
3086 maybe_suppress_debug_info (t
)
3089 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
3090 does not support name references between translation units. It supports
3091 symbolic references between translation units, but only within a single
3092 executable or shared library.
3094 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
3095 that the type was never defined, so we only get the members we
3097 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== NO_DEBUG
)
3100 /* We might have set this earlier in cp_finish_decl. */
3101 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 0;
3103 /* If we already know how we're handling this class, handle debug info
3105 if (CLASSTYPE_INTERFACE_ONLY (t
))
3106 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
3107 else if (CLASSTYPE_INTERFACE_KNOWN (t
))
3108 /* Don't set it. */;
3109 /* If the class has virtual functions, write out the debug info
3110 along with the vtable. */
3111 else if (TYPE_POLYMORPHIC_P (t
))
3112 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
3114 /* Otherwise, just emit the debug info normally. */
3118 /* Keep this code around in case we later want to control debug info
3119 based on whether a type is "used". Currently, we only suppress debug
3120 info if we can emit it with the vtable. jason 1999-11-11) */
3122 /* If we want debug info for a type TYPE, make sure all its base types
3123 are also marked as being potentially interesting. This avoids
3124 the problem of not writing any debug info for intermediate basetypes
3125 that have abstract virtual functions. Also mark member types. */
3128 note_debug_info_needed (type
)
3133 if (current_template_parms
)
3136 if (TYPE_BEING_DEFINED (type
))
3137 /* We can't go looking for the base types and fields just yet. */
3140 /* See the comment in maybe_suppress_debug_info. */
3141 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== NO_DEBUG
)
3144 dfs_walk (TYPE_BINFO (type
), dfs_debug_mark
, dfs_debug_unmarkedp
, 0);
3145 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3148 if (TREE_CODE (field
) == FIELD_DECL
3149 && IS_AGGR_TYPE (ttype
= target_type (TREE_TYPE (field
)))
3150 && dfs_debug_unmarkedp (TYPE_BINFO (ttype
), 0))
3151 note_debug_info_needed (ttype
);
3156 /* Subroutines of push_class_decls (). */
3158 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
3159 because it (or one of the intermediate bases) depends on template parms. */
3162 dependent_base_p (binfo
)
3165 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
3167 if (currently_open_class (TREE_TYPE (binfo
)))
3169 if (uses_template_parms (TREE_TYPE (binfo
)))
3176 setup_class_bindings (name
, type_binding_p
)
3180 tree type_binding
= NULL_TREE
;
3183 /* If we've already done the lookup for this declaration, we're
3185 if (IDENTIFIER_CLASS_VALUE (name
))
3188 /* First, deal with the type binding. */
3191 type_binding
= lookup_member (current_class_type
, name
,
3194 if (TREE_CODE (type_binding
) == TREE_LIST
3195 && TREE_TYPE (type_binding
) == error_mark_node
)
3196 /* NAME is ambiguous. */
3197 push_class_level_binding (name
, type_binding
);
3199 pushdecl_class_level (type_binding
);
3202 /* Now, do the value binding. */
3203 value_binding
= lookup_member (current_class_type
, name
,
3208 && (TREE_CODE (value_binding
) == TYPE_DECL
3209 || (TREE_CODE (value_binding
) == TREE_LIST
3210 && TREE_TYPE (value_binding
) == error_mark_node
3211 && (TREE_CODE (TREE_VALUE (value_binding
))
3213 /* We found a type-binding, even when looking for a non-type
3214 binding. This means that we already processed this binding
3216 my_friendly_assert (type_binding_p
, 19990401);
3217 else if (value_binding
)
3219 if (TREE_CODE (value_binding
) == TREE_LIST
3220 && TREE_TYPE (value_binding
) == error_mark_node
)
3221 /* NAME is ambiguous. */
3222 push_class_level_binding (name
, value_binding
);
3225 if (BASELINK_P (value_binding
))
3226 /* NAME is some overloaded functions. */
3227 value_binding
= TREE_VALUE (value_binding
);
3228 pushdecl_class_level (value_binding
);
3233 /* Push class-level declarations for any names appearing in BINFO that
3237 dfs_push_type_decls (binfo
, data
)
3239 void *data ATTRIBUTE_UNUSED
;
3244 type
= BINFO_TYPE (binfo
);
3245 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3246 if (DECL_NAME (fields
) && TREE_CODE (fields
) == TYPE_DECL
3247 && !(!same_type_p (type
, current_class_type
)
3248 && template_self_reference_p (type
, fields
)))
3249 setup_class_bindings (DECL_NAME (fields
), /*type_binding_p=*/1);
3251 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3252 DERIVED_FROM_P, which calls get_base_distance. */
3253 SET_BINFO_PUSHDECLS_MARKED (binfo
);
3258 /* Push class-level declarations for any names appearing in BINFO that
3259 are not TYPE_DECLS. */
3262 dfs_push_decls (binfo
, data
)
3270 type
= BINFO_TYPE (binfo
);
3271 dep_base_p
= (processing_template_decl
&& type
!= current_class_type
3272 && dependent_base_p (binfo
));
3276 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3277 if (DECL_NAME (fields
)
3278 && TREE_CODE (fields
) != TYPE_DECL
3279 && TREE_CODE (fields
) != USING_DECL
)
3280 setup_class_bindings (DECL_NAME (fields
), /*type_binding_p=*/0);
3281 else if (TREE_CODE (fields
) == FIELD_DECL
3282 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
3283 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields
)), data
);
3285 method_vec
= (CLASS_TYPE_P (type
)
3286 ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
);
3292 /* Farm out constructors and destructors. */
3293 end
= TREE_VEC_END (method_vec
);
3295 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
3296 *methods
&& methods
!= end
;
3298 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods
)),
3299 /*type_binding_p=*/0);
3303 CLEAR_BINFO_PUSHDECLS_MARKED (binfo
);
3308 /* When entering the scope of a class, we cache all of the
3309 fields that that class provides within its inheritance
3310 lattice. Where ambiguities result, we mark them
3311 with `error_mark_node' so that if they are encountered
3312 without explicit qualification, we can emit an error
3316 push_class_decls (type
)
3319 search_stack
= push_search_level (search_stack
, &search_obstack
);
3321 /* Enter type declarations and mark. */
3322 dfs_walk (TYPE_BINFO (type
), dfs_push_type_decls
, unmarked_pushdecls_p
, 0);
3324 /* Enter non-type declarations and unmark. */
3325 dfs_walk (TYPE_BINFO (type
), dfs_push_decls
, marked_pushdecls_p
, 0);
3328 /* Here's a subroutine we need because C lacks lambdas. */
3331 dfs_unuse_fields (binfo
, data
)
3333 void *data ATTRIBUTE_UNUSED
;
3335 tree type
= TREE_TYPE (binfo
);
3338 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3340 if (TREE_CODE (fields
) != FIELD_DECL
)
3343 TREE_USED (fields
) = 0;
3344 if (DECL_NAME (fields
) == NULL_TREE
3345 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
3346 unuse_fields (TREE_TYPE (fields
));
3356 dfs_walk (TYPE_BINFO (type
), dfs_unuse_fields
, unmarkedp
, 0);
3362 /* We haven't pushed a search level when dealing with cached classes,
3363 so we'd better not try to pop it. */
3365 search_stack
= pop_search_level (search_stack
);
3369 print_search_statistics ()
3371 #ifdef GATHER_STATISTICS
3372 fprintf (stderr
, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3373 n_fields_searched
, n_calls_lookup_field
, n_calls_lookup_field_1
);
3374 fprintf (stderr
, "%d fnfields searched in %d calls to lookup_fnfields\n",
3375 n_outer_fields_searched
, n_calls_lookup_fnfields
);
3376 fprintf (stderr
, "%d calls to get_base_type\n", n_calls_get_base_type
);
3377 #else /* GATHER_STATISTICS */
3378 fprintf (stderr
, "no search statistics\n");
3379 #endif /* GATHER_STATISTICS */
3383 init_search_processing ()
3385 gcc_obstack_init (&search_obstack
);
3386 vptr_identifier
= get_identifier ("_vptr");
3390 reinit_search_statistics ()
3392 #ifdef GATHER_STATISTICS
3393 n_fields_searched
= 0;
3394 n_calls_lookup_field
= 0, n_calls_lookup_field_1
= 0;
3395 n_calls_lookup_fnfields
= 0, n_calls_lookup_fnfields_1
= 0;
3396 n_calls_get_base_type
= 0;
3397 n_outer_fields_searched
= 0;
3398 n_contexts_saved
= 0;
3399 #endif /* GATHER_STATISTICS */
3403 add_conversions (binfo
, data
)
3408 tree method_vec
= CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo
));
3409 tree
*conversions
= (tree
*) data
;
3411 /* Some builtin types have no method vector, not even an empty one. */
3415 for (i
= 2; i
< TREE_VEC_LENGTH (method_vec
); ++i
)
3417 tree tmp
= TREE_VEC_ELT (method_vec
, i
);
3420 if (!tmp
|| ! DECL_CONV_FN_P (OVL_CURRENT (tmp
)))
3423 name
= DECL_NAME (OVL_CURRENT (tmp
));
3425 /* Make sure we don't already have this conversion. */
3426 if (! IDENTIFIER_MARKED (name
))
3428 *conversions
= tree_cons (binfo
, tmp
, *conversions
);
3429 IDENTIFIER_MARKED (name
) = 1;
3435 /* Return a TREE_LIST containing all the non-hidden user-defined
3436 conversion functions for TYPE (and its base-classes). The
3437 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
3438 containing the conversion functions. The TREE_PURPOSE is the BINFO
3439 from which the conversion functions in this node were selected. */
3442 lookup_conversions (type
)
3446 tree conversions
= NULL_TREE
;
3448 if (TYPE_SIZE (type
))
3449 bfs_walk (TYPE_BINFO (type
), add_conversions
, 0, &conversions
);
3451 for (t
= conversions
; t
; t
= TREE_CHAIN (t
))
3452 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t
)))) = 0;
3463 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3464 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3467 dfs_check_overlap (empty_binfo
, data
)
3471 struct overlap_info
*oi
= (struct overlap_info
*) data
;
3473 for (binfo
= TYPE_BINFO (oi
->compare_type
);
3475 binfo
= BINFO_BASETYPE (binfo
, 0))
3477 if (BINFO_TYPE (binfo
) == BINFO_TYPE (empty_binfo
))
3479 oi
->found_overlap
= 1;
3482 else if (BINFO_BASETYPES (binfo
) == NULL_TREE
)
3489 /* Trivial function to stop base traversal when we find something. */
3492 dfs_no_overlap_yet (binfo
, data
)
3496 struct overlap_info
*oi
= (struct overlap_info
*) data
;
3497 return !oi
->found_overlap
? binfo
: NULL_TREE
;
3500 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3501 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3504 types_overlap_p (empty_type
, next_type
)
3505 tree empty_type
, next_type
;
3507 struct overlap_info oi
;
3509 if (! IS_AGGR_TYPE (next_type
))
3511 oi
.compare_type
= next_type
;
3512 oi
.found_overlap
= 0;
3513 dfs_walk (TYPE_BINFO (empty_type
), dfs_check_overlap
,
3514 dfs_no_overlap_yet
, &oi
);
3515 return oi
.found_overlap
;
3518 /* Given a vtable VAR, determine which binfo it comes from.
3520 FIXME What about secondary vtables? */
3523 binfo_for_vtable (var
)
3526 tree binfo
= TYPE_BINFO (DECL_CONTEXT (var
));
3532 binfos
= BINFO_BASETYPES (binfo
);
3533 if (binfos
== NULL_TREE
)
3536 i
= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
3540 binfo
= TREE_VEC_ELT (binfos
, i
);
3546 /* Returns 1 iff BINFO is from a direct or indirect virtual base. */
3549 binfo_from_vbase (binfo
)
3552 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
3554 if (TREE_VIA_VIRTUAL (binfo
))