1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 89, 92-97, 1998, 1999 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 extern struct obstack
*current_obstack
;
39 extern tree abort_fndecl
;
43 /* Obstack used for remembering decision points of breadth-first. */
45 static struct obstack search_obstack
;
47 /* Methods for pushing and popping objects to and from obstacks. */
50 push_stack_level (obstack
, tp
, size
)
51 struct obstack
*obstack
;
52 char *tp
; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
55 struct stack_level
*stack
;
56 obstack_grow (obstack
, tp
, size
);
57 stack
= (struct stack_level
*) ((char*)obstack_next_free (obstack
) - size
);
58 obstack_finish (obstack
);
59 stack
->obstack
= obstack
;
60 stack
->first
= (tree
*) obstack_base (obstack
);
61 stack
->limit
= obstack_room (obstack
) / sizeof (tree
*);
66 pop_stack_level (stack
)
67 struct stack_level
*stack
;
69 struct stack_level
*tem
= stack
;
70 struct obstack
*obstack
= tem
->obstack
;
72 obstack_free (obstack
, tem
);
76 #define search_level stack_level
77 static struct search_level
*search_stack
;
79 static tree get_abstract_virtuals_1
PROTO((tree
, int, tree
));
80 static tree get_vbase_1
PROTO((tree
, tree
, unsigned int *));
81 static tree convert_pointer_to_vbase
PROTO((tree
, tree
));
82 static tree lookup_field_1
PROTO((tree
, tree
));
83 static tree convert_pointer_to_single_level
PROTO((tree
, tree
));
84 static int lookup_fnfields_here
PROTO((tree
, tree
));
85 static int is_subobject_of_p
PROTO((tree
, tree
));
86 static int hides
PROTO((tree
, tree
));
87 static tree virtual_context
PROTO((tree
, tree
, tree
));
88 static void dfs_check_overlap
PROTO((tree
));
89 static int dfs_no_overlap_yet
PROTO((tree
));
90 static void envelope_add_decl
PROTO((tree
, tree
, tree
*));
91 static int get_base_distance_recursive
92 PROTO((tree
, int, int, int, int *, tree
*, tree
,
93 int, int *, int, int));
94 static void expand_upcast_fixups
95 PROTO((tree
, tree
, tree
, tree
, tree
, tree
, tree
*));
96 static void fixup_virtual_upcast_offsets
97 PROTO((tree
, tree
, int, int, tree
, tree
, tree
, tree
,
99 static int unmarkedp
PROTO((tree
));
100 static int marked_vtable_pathp
PROTO((tree
));
101 static int unmarked_vtable_pathp
PROTO((tree
));
102 static int marked_new_vtablep
PROTO((tree
));
103 static int unmarked_new_vtablep
PROTO((tree
));
104 static int dfs_debug_unmarkedp
PROTO((tree
));
105 static void dfs_debug_mark
PROTO((tree
));
106 static void dfs_find_vbases
PROTO((tree
));
107 static void dfs_clear_vbase_slots
PROTO((tree
));
108 static void dfs_init_vbase_pointers
PROTO((tree
));
109 static void dfs_get_vbase_types
PROTO((tree
));
110 static void dfs_pushdecls
PROTO((tree
));
111 static void dfs_compress_decls
PROTO((tree
));
112 static void dfs_unuse_fields
PROTO((tree
));
113 static tree add_conversions
PROTO((tree
, void *));
114 static tree get_virtuals_named_this
PROTO((tree
));
115 static tree get_virtual_destructor
PROTO((tree
, void *));
116 static int tree_has_any_destructor_p
PROTO((tree
, void *));
117 static int covariant_return_p
PROTO((tree
, tree
));
118 static struct search_level
*push_search_level
119 PROTO((struct stack_level
*, struct obstack
*));
120 static struct search_level
*pop_search_level
121 PROTO((struct stack_level
*));
122 static tree breadth_first_search
123 PROTO((tree
, tree (*) (tree
, void *), int (*) (tree
, void *),
124 void (*) (tree
*, tree
*, void *), void *));
125 static int lookup_field_queue_p
PROTO((tree
, void *));
126 static tree lookup_field_r
PROTO((tree
, void *));
127 static void lookup_field_post
PROTO((tree
*, tree
*, void *));
129 static tree vbase_types
;
130 static tree vbase_decl_ptr_intermediate
, vbase_decl_ptr
;
131 static tree vbase_init_result
;
133 /* Allocate a level of searching. */
135 static struct search_level
*
136 push_search_level (stack
, obstack
)
137 struct stack_level
*stack
;
138 struct obstack
*obstack
;
140 struct search_level tem
;
143 return push_stack_level (obstack
, (char *)&tem
, sizeof (tem
));
146 /* Discard a level of search allocation. */
148 static struct search_level
*
149 pop_search_level (obstack
)
150 struct stack_level
*obstack
;
152 register struct search_level
*stack
= pop_stack_level (obstack
);
157 static tree _vptr_name
;
159 /* Variables for gathering statistics. */
160 #ifdef GATHER_STATISTICS
161 static int n_fields_searched
;
162 static int n_calls_lookup_field
, n_calls_lookup_field_1
;
163 static int n_calls_lookup_fnfields
, n_calls_lookup_fnfields_1
;
164 static int n_calls_get_base_type
;
165 static int n_outer_fields_searched
;
166 static int n_contexts_saved
;
167 #endif /* GATHER_STATISTICS */
169 /* This list is used by push_class_decls to know what decls need to
170 be pushed into class scope. */
171 static tree closed_envelopes
= NULL_TREE
;
173 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
174 the same type as the type given in PARENT. To be optimal, we want
175 the first one that is found by going through the least number of
178 This uses a clever algorithm that updates *depth when we find the vbase,
179 and cuts off other paths of search when they reach that depth. */
182 get_vbase_1 (parent
, binfo
, depth
)
188 tree rval
= NULL_TREE
;
190 if (BINFO_TYPE (binfo
) == parent
&& TREE_VIA_VIRTUAL (binfo
))
198 binfos
= BINFO_BASETYPES (binfo
);
199 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
201 /* Process base types. */
202 for (i
= 0; i
< n_baselinks
; i
++)
204 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
210 nrval
= get_vbase_1 (parent
, base_binfo
, depth
);
218 /* Return the shortest path to vbase PARENT within BINFO, ignoring
219 access and ambiguity. */
222 get_vbase (parent
, binfo
)
226 unsigned int d
= (unsigned int)-1;
227 return get_vbase_1 (parent
, binfo
, &d
);
230 /* Convert EXPR to a virtual base class of type TYPE. We know that
231 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
232 the type of what expr points to has a virtual base of type TYPE. */
235 convert_pointer_to_vbase (type
, expr
)
239 tree vb
= get_vbase (type
, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr
))));
240 return convert_pointer_to_real (vb
, expr
);
243 /* Check whether the type given in BINFO is derived from PARENT. If
244 it isn't, return 0. If it is, but the derivation is MI-ambiguous
245 AND protect != 0, emit an error message and return error_mark_node.
247 Otherwise, if TYPE is derived from PARENT, return the actual base
248 information, unless a one of the protection violations below
249 occurs, in which case emit an error message and return error_mark_node.
251 If PROTECT is 1, then check if access to a public field of PARENT
252 would be private. Also check for ambiguity. */
255 get_binfo (parent
, binfo
, protect
)
256 register tree parent
, binfo
;
259 tree type
= NULL_TREE
;
261 tree rval
= NULL_TREE
;
263 if (TREE_CODE (parent
) == TREE_VEC
)
264 parent
= BINFO_TYPE (parent
);
265 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent
)))
266 my_friendly_abort (89);
268 if (TREE_CODE (binfo
) == TREE_VEC
)
269 type
= BINFO_TYPE (binfo
);
270 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
273 my_friendly_abort (90);
275 dist
= get_base_distance (parent
, binfo
, protect
, &rval
);
279 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
281 return error_mark_node
;
283 else if (dist
== -2 && protect
)
285 cp_error ("type `%T' is ambiguous base class for type `%T'", parent
,
287 return error_mark_node
;
293 /* This is the newer depth first get_base_distance routine. */
296 get_base_distance_recursive (binfo
, depth
, is_private
, rval
,
297 rval_private_ptr
, new_binfo_ptr
, parent
,
298 protect
, via_virtual_ptr
, via_virtual
,
299 current_scope_in_chain
)
301 int depth
, is_private
, rval
;
302 int *rval_private_ptr
;
303 tree
*new_binfo_ptr
, parent
;
304 int protect
, *via_virtual_ptr
, via_virtual
;
305 int current_scope_in_chain
;
311 && !current_scope_in_chain
312 && is_friend (BINFO_TYPE (binfo
), current_scope ()))
313 current_scope_in_chain
= 1;
315 if (BINFO_TYPE (binfo
) == parent
|| binfo
== parent
)
320 /* This is the first time we've found parent. */
322 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr
),
323 BINFO_OFFSET (binfo
))
324 && *via_virtual_ptr
&& via_virtual
)
326 /* A new path to the same vbase. If this one has better
327 access or is shorter, take it. */
330 better
= *rval_private_ptr
- is_private
;
332 better
= rval
- depth
;
336 /* Ambiguous base class. */
339 /* If we get an ambiguity between virtual and non-virtual base
340 class, return the non-virtual in case we are ignoring
342 better
= *via_virtual_ptr
- via_virtual
;
348 *rval_private_ptr
= is_private
;
349 *new_binfo_ptr
= binfo
;
350 *via_virtual_ptr
= via_virtual
;
356 binfos
= BINFO_BASETYPES (binfo
);
357 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
360 /* Process base types. */
361 for (i
= 0; i
< n_baselinks
; i
++)
363 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
368 || (!TREE_VIA_PUBLIC (base_binfo
)
369 && !(TREE_VIA_PROTECTED (base_binfo
)
370 && current_scope_in_chain
)
371 && !is_friend (BINFO_TYPE (binfo
), current_scope ()))));
372 int this_virtual
= via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
);
374 rval
= get_base_distance_recursive (base_binfo
, depth
, via_private
,
375 rval
, rval_private_ptr
,
376 new_binfo_ptr
, parent
,
377 protect
, via_virtual_ptr
,
379 current_scope_in_chain
);
381 /* If we've found a non-virtual, ambiguous base class, we don't need
382 to keep searching. */
383 if (rval
== -2 && *via_virtual_ptr
== 0)
390 /* Return the number of levels between type PARENT and the type given
391 in BINFO, following the leftmost path to PARENT not found along a
392 virtual path, if there are no real PARENTs (all come from virtual
393 base classes), then follow the shortest public path to PARENT.
395 Return -1 if TYPE is not derived from PARENT.
396 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
398 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
400 If PATH_PTR is non-NULL, then also build the list of types
401 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
404 PARENT can also be a binfo, in which case that exact parent is found
405 and no other. convert_pointer_to_real uses this functionality.
407 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
410 get_base_distance (parent
, binfo
, protect
, path_ptr
)
411 register tree parent
, binfo
;
416 int rval_private
= 0;
417 tree type
= NULL_TREE
;
418 tree new_binfo
= NULL_TREE
;
420 int watch_access
= protect
;
422 /* Should we be completing types here? */
423 if (TREE_CODE (parent
) != TREE_VEC
)
424 parent
= complete_type (TYPE_MAIN_VARIANT (parent
));
426 complete_type (TREE_TYPE (parent
));
428 if (TREE_CODE (binfo
) == TREE_VEC
)
429 type
= BINFO_TYPE (binfo
);
430 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
432 type
= complete_type (binfo
);
433 binfo
= TYPE_BINFO (type
);
436 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo
) == NULL_TREE
,
440 my_friendly_abort (92);
442 if (parent
== type
|| parent
== binfo
)
444 /* If the distance is 0, then we don't really need
445 a path pointer, but we shouldn't let garbage go back. */
454 rval
= get_base_distance_recursive (binfo
, 0, 0, -1,
455 &rval_private
, &new_binfo
, parent
,
456 watch_access
, &via_virtual
, 0,
459 /* Access restrictions don't count if we found an ambiguous basetype. */
460 if (rval
== -2 && protect
>= 0)
463 if (rval
&& protect
&& rval_private
)
466 /* If they gave us the real vbase binfo, which isn't in the main binfo
467 tree, deal with it. This happens when we are called from
468 expand_upcast_fixups. */
469 if (rval
== -1 && TREE_CODE (parent
) == TREE_VEC
470 && parent
== binfo_member (BINFO_TYPE (parent
),
471 CLASSTYPE_VBASECLASSES (type
)))
473 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent
) == binfo
, 980827);
479 *path_ptr
= new_binfo
;
483 /* Search for a member with name NAME in a multiple inheritance lattice
484 specified by TYPE. If it does not exist, return NULL_TREE.
485 If the member is ambiguously referenced, return `error_mark_node'.
486 Otherwise, return the FIELD_DECL. */
488 /* Do a 1-level search for NAME as a member of TYPE. The caller must
489 figure out whether it can access this field. (Since it is only one
490 level, this is reasonable.) */
493 lookup_field_1 (type
, name
)
498 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
499 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
500 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
501 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
502 the code often worked even when we treated the index as a list
506 field
= TYPE_FIELDS (type
);
508 #ifdef GATHER_STATISTICS
509 n_calls_lookup_field_1
++;
510 #endif /* GATHER_STATISTICS */
513 #ifdef GATHER_STATISTICS
515 #endif /* GATHER_STATISTICS */
516 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field
)) == 'd', 0);
517 if (DECL_NAME (field
) == NULL_TREE
518 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
520 tree temp
= lookup_field_1 (TREE_TYPE (field
), name
);
524 if (TREE_CODE (field
) == USING_DECL
)
525 /* For now, we're just treating member using declarations as
526 old ARM-style access declarations. Thus, there's no reason
527 to return a USING_DECL, and the rest of the compiler can't
528 handle it. Once the class is defined, these are purged
529 from TYPE_FIELDS anyhow; see handle_using_decl. */
531 else if (DECL_NAME (field
) == name
)
533 if ((TREE_CODE(field
) == VAR_DECL
|| TREE_CODE(field
) == CONST_DECL
)
534 && DECL_ASSEMBLER_NAME (field
) != NULL
)
535 GNU_xref_ref(current_function_decl
,
536 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field
)));
539 field
= TREE_CHAIN (field
);
542 if (name
== _vptr_name
)
544 /* Give the user what s/he thinks s/he wants. */
545 if (TYPE_VIRTUAL_P (type
))
546 return CLASSTYPE_VFIELD (type
);
551 /* There are a number of cases we need to be aware of here:
552 current_class_type current_function_decl
559 Those last two make life interesting. If we're in a function which is
560 itself inside a class, we need decls to go into the fn's decls (our
561 second case below). But if we're in a class and the class itself is
562 inside a function, we need decls to go into the decls for the class. To
563 achieve this last goal, we must see if, when both current_class_ptr and
564 current_function_decl are set, the class was declared inside that
565 function. If so, we know to put the decls into the class's scope. */
570 if (current_function_decl
== NULL_TREE
)
571 return current_class_type
;
572 if (current_class_type
== NULL_TREE
)
573 return current_function_decl
;
574 if (DECL_CLASS_CONTEXT (current_function_decl
) == current_class_type
)
575 return current_function_decl
;
577 return current_class_type
;
580 /* Compute the access of FIELD. This is done by computing
581 the access available to each type in BASETYPES (which comes
582 as a list of [via_public/basetype] in reverse order, namely base
583 class before derived class). The first one which defines a
584 access defines the access for the field. Otherwise, the
585 access of the field is that which occurs normally.
587 Uses global variables CURRENT_CLASS_TYPE and
588 CURRENT_FUNCTION_DECL to use friend relationships
591 This will be static when lookup_fnfield comes into this file.
593 access_public_node means that the field can be accessed by the current lexical
596 access_protected_node means that the field cannot be accessed by the current
597 lexical scope because it is protected.
599 access_private_node means that the field cannot be accessed by the current
600 lexical scope because it is private. */
603 #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public_node
604 #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected_node
605 #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private_node
607 #define PUBLIC_RETURN return access_public_node
608 #define PROTECTED_RETURN return access_protected_node
609 #define PRIVATE_RETURN return access_private_node
613 /* Disabled with DECL_PUBLIC &c. */
614 static tree previous_scope
= NULL_TREE
;
618 compute_access (basetype_path
, field
)
619 tree basetype_path
, field
;
624 int protected_ok
, via_protected
;
625 extern int flag_access_control
;
627 /* Replaces static decl above. */
631 = ((TREE_CODE (field
) == FUNCTION_DECL
&& DECL_STATIC_FUNCTION_P (field
))
632 || (TREE_CODE (field
) != FUNCTION_DECL
&& TREE_STATIC (field
)));
634 if (! flag_access_control
)
635 return access_public_node
;
637 /* The field lives in the current class. */
638 if (BINFO_TYPE (basetype_path
) == current_class_type
)
639 return access_public_node
;
642 /* Disabled until pushing function scope clears these out. If ever. */
643 /* Make these special cases fast. */
644 if (current_scope () == previous_scope
)
646 if (DECL_PUBLIC (field
))
647 return access_public_node
;
648 if (DECL_PROTECTED (field
))
649 return access_protected_node
;
650 if (DECL_PRIVATE (field
))
651 return access_private_node
;
655 /* We don't currently support access control on nested types. */
656 if (TREE_CODE (field
) == TYPE_DECL
)
657 return access_public_node
;
659 previous_scope
= current_scope ();
661 context
= DECL_REAL_CONTEXT (field
);
663 /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT
664 slot set to the union type rather than the record type containing
665 the anonymous union. */
666 if (context
&& ANON_UNION_TYPE_P (context
)
667 && TREE_CODE (field
) == FIELD_DECL
)
668 context
= TYPE_CONTEXT (context
);
670 /* If we aren't a real class member (e.g. we're from a namespace-scope
671 anonymous union), there's no access control. */
672 if (context
== NULL_TREE
|| ! TYPE_P (context
))
675 /* Virtual function tables are never private. But we should know that
676 we are looking for this, and not even try to hide it. */
677 if (DECL_NAME (field
) && VFIELD_NAME_P (DECL_NAME (field
)) == 1)
680 /* Member found immediately within object. */
681 if (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
)
683 /* Are we (or an enclosing scope) friends with the class that has
685 if (is_friend (context
, previous_scope
))
688 /* If it's private, it's private, you letch. */
689 if (TREE_PRIVATE (field
))
692 /* ARM $11.5. Member functions of a derived class can access the
693 non-static protected members of a base class only through a
694 pointer to the derived class, a reference to it, or an object
695 of it. Also any subsequently derived classes also have
697 else if (TREE_PROTECTED (field
))
699 if (current_class_type
700 && (static_mem
|| DECL_CONSTRUCTOR_P (field
))
701 && ACCESSIBLY_DERIVED_FROM_P (context
, current_class_type
))
710 /* must reverse more than one element */
711 basetype_path
= reverse_path (basetype_path
);
712 types
= basetype_path
;
714 access
= access_default_node
;
715 protected_ok
= static_mem
&& current_class_type
716 && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types
), current_class_type
);
722 tree type
= BINFO_TYPE (binfo
);
725 /* Friends of a class can see protected members of its bases.
726 Note that classes are their own friends. */
727 if (is_friend (type
, previous_scope
))
733 member
= purpose_member (type
, DECL_ACCESS (field
));
736 access
= TREE_VALUE (member
);
740 types
= BINFO_INHERITANCE_CHAIN (types
);
742 /* If the next type was VIA_PROTECTED, then fields of all remaining
743 classes past that one are *at least* protected. */
746 if (TREE_VIA_PROTECTED (types
))
748 else if (! TREE_VIA_PUBLIC (types
) && ! private_ok
)
750 access
= access_private_node
;
758 /* No special visibilities apply. Use normal rules. */
760 if (access
== access_default_node
)
762 if (is_friend (context
, previous_scope
))
763 access
= access_public_node
;
764 else if (TREE_PRIVATE (field
))
765 access
= access_private_node
;
766 else if (TREE_PROTECTED (field
))
767 access
= access_protected_node
;
769 access
= access_public_node
;
772 if (access
== access_public_node
&& via_protected
)
773 access
= access_protected_node
;
775 if (access
== access_protected_node
&& protected_ok
)
776 access
= access_public_node
;
779 if (access
== access_public_node
)
780 DECL_PUBLIC (field
) = 1;
781 else if (access
== access_protected_node
)
782 DECL_PROTECTED (field
) = 1;
783 else if (access
== access_private_node
)
784 DECL_PRIVATE (field
) = 1;
785 else my_friendly_abort (96);
790 /* Routine to see if the sub-object denoted by the binfo PARENT can be
791 found as a base class and sub-object of the object denoted by
792 BINFO. This routine relies upon binfos not being shared, except
793 for binfos for virtual bases. */
796 is_subobject_of_p (parent
, binfo
)
799 tree binfos
= BINFO_BASETYPES (binfo
);
800 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
805 /* Process and/or queue base types. */
806 for (i
= 0; i
< n_baselinks
; i
++)
808 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
809 if (TREE_VIA_VIRTUAL (base_binfo
))
810 base_binfo
= TYPE_BINFO (BINFO_TYPE (base_binfo
));
811 if (is_subobject_of_p (parent
, base_binfo
))
817 /* See if a one FIELD_DECL hides another. This routine is meant to
818 correspond to ANSI working paper Sept 17, 1992 10p4. The two
819 binfos given are the binfos corresponding to the particular places
820 the FIELD_DECLs are found. This routine relies upon binfos not
821 being shared, except for virtual bases. */
824 hides (hider_binfo
, hidee_binfo
)
825 tree hider_binfo
, hidee_binfo
;
827 /* hider hides hidee, if hider has hidee as a base class and
828 the instance of hidee is a sub-object of hider. The first
829 part is always true is the second part is true.
831 When hider and hidee are the same (two ways to get to the exact
832 same member) we consider either one as hiding the other. */
833 return is_subobject_of_p (hidee_binfo
, hider_binfo
);
836 /* Very similar to lookup_fnfields_1 but it ensures that at least one
837 function was declared inside the class given by TYPE. It really should
838 only return functions that match the given TYPE. */
841 lookup_fnfields_here (type
, name
)
844 int idx
= lookup_fnfields_1 (type
, name
);
847 /* ctors and dtors are always only in the right class. */
850 fndecls
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
853 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls
)))
854 == TYPE_MAIN_VARIANT (type
))
856 fndecls
= OVL_CHAIN (fndecls
);
861 struct lookup_field_info
{
862 /* The name of the field for which we're looking. */
864 /* If non-NULL, the current result of the lookup. */
866 /* The path to RVAL. */
868 /* If non-NULL, a list of the possible candidates. */
870 /* The access computed for RVAL. */
872 /* If non-zero, we must check access. */
874 /* If non-zero, we are looking for types, not data members. */
876 /* If something went wrong, a message indicating what. */
880 /* Returns non-zero if BINFO is not hidden by the value found by the
881 lookup so far. If BINFO is hidden, then there's no need to look in
882 it. DATA is really a struct lookup_field_info. Called from
883 lookup_field via breadth_first_search. */
886 lookup_field_queue_p (binfo
, data
)
890 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
892 return !(lfi
->rval_binfo
&& hides (lfi
->rval_binfo
, binfo
));
895 /* DATA is really a struct lookup_field_info. Look for a field with
896 the name indicated there in BINFO. If this function returns a
897 non-NULL value it is the result of the lookup. Called from
898 lookup_field via breadth_first_search. */
901 lookup_field_r (binfo
, data
)
905 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
906 tree type
= BINFO_TYPE (binfo
);
910 /* See if the field is present in TYPE. */
911 nval
= lookup_field_1 (type
, lfi
->name
);
913 idx
= lookup_fnfields_here (type
, lfi
->name
);
915 /* If the data member wasn't present, then there's nothing further
916 to do for this type. */
917 if (!nval
&& idx
< 0)
920 /* If the lookup already found a match, and the new value doesn't
921 hide the old one, we might have an ambiguity. */
922 if (lfi
->rval_binfo
&& !hides (binfo
, lfi
->rval_binfo
))
924 if (nval
&& nval
== lfi
->rval
&& SHARED_MEMBER_P (nval
))
925 /* The two things are really the same. */
927 else if (hides (lfi
->rval_binfo
, binfo
))
928 /* The previous value hides the new one. */
932 /* We have a real ambiguity. We keep a chain of all the
934 if (!lfi
->ambiguous
&& lfi
->rval
)
935 /* This is the first time we noticed an ambiguity. Add
936 what we previously thought was a reasonable candidate
938 lfi
->ambiguous
= scratch_tree_cons (NULL_TREE
, lfi
->rval
,
940 /* If NVAL is NULL here, that means that we found a
941 function, not a data member. Pick a representative
942 function, from the overload set, for use in error
945 nval
= OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC
948 /* Add the new value. */
949 lfi
->ambiguous
= scratch_tree_cons (NULL_TREE
, nval
,
951 lfi
->errstr
= "request for member `%D' is ambiguous";
956 /* The new lookup is the best we've got so far. Verify that
957 it's the kind of thing we're looking for. */
960 if (lfi
->want_type
&& TREE_CODE (nval
) != TYPE_DECL
)
962 nval
= purpose_member (lfi
->name
, CLASSTYPE_TAGS (type
));
964 nval
= TYPE_MAIN_DECL (TREE_VALUE (nval
));
966 else if (!lfi
->want_type
&& TREE_CODE (nval
) == TYPE_DECL
967 && lookup_fnfields_here (type
, lfi
->name
) >= 0)
968 /* The type declaration is actually hidden by the
969 function declaration. */
975 /* The lookup found a data member. */
978 lfi
->access
= compute_access (binfo
, nval
);
979 /* If the thing we're looking for is a virtual base class,
980 then we know we've got what we want at this point;
981 there's no way to get an ambiguity. */
982 if (VBASE_NAME_P (lfi
->name
))
986 /* The lookup found a function member. This lookup hides
987 whatever was there before, so even though we're not
988 interested in this value we keep track of the way in
989 which we found the function. Subsequent lookups
990 shouldn't find a data member if it is hidden by this
992 lfi
->rval
= NULL_TREE
;
994 lfi
->rval_binfo
= binfo
;
1000 /* Check to see if the result of the field lookup (as indicated by
1001 DATA, which is really a struct field_info) has any access other
1002 than that we previously computed. SEARCH_HEAD and SEARCH_TAIL
1003 bound the path taken to find the result. Called from lookup_field
1004 via breadth_first_search. */
1007 lookup_field_post (search_head
, search_tail
, data
)
1012 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1013 tree rval
= lfi
->rval
;
1014 tree own_access
= access_default_node
;
1017 /* If we didn't find anything, or we found ambiguous function
1018 declarations, but no data members, just return. */
1025 /* If we've already hit a snag, we're done. */
1029 /* Check accessibility. */
1032 /* If is possible for one of the derived types on the path to
1033 have defined special access for this field. Look for such
1034 declarations and report an error if a conflict is found. */
1035 if (DECL_LANG_SPECIFIC (rval
) && DECL_ACCESS (rval
))
1036 for (tp
= search_head
; tp
< search_tail
; ++tp
)
1038 tree new_v
= NULL_TREE
;
1040 if (lfi
->access
!= access_default_node
)
1041 new_v
= compute_access (*tp
, lfi
->rval
);
1042 if (lfi
->access
!= access_default_node
&& new_v
!= lfi
->access
)
1044 lfi
->errstr
= "conflicting access to member `%D'";
1045 lfi
->access
= access_default_node
;
1052 /* Check to see that access to the member is allowed. */
1053 if (own_access
== access_private_node
)
1054 lfi
->errstr
= "member `%D' declared private";
1055 else if (own_access
== access_protected_node
)
1056 lfi
->errstr
= "member `%D' declared protected";
1057 else if (lfi
->access
== access_private_node
)
1058 lfi
->errstr
= TREE_PRIVATE (lfi
->rval
)
1059 ? "member `%D' is private"
1060 : "member `%D' is from private base class";
1061 else if (lfi
->access
== access_protected_node
)
1062 lfi
->errstr
= TREE_PROTECTED (rval
)
1063 ? "member `%D' is protected"
1064 : "member `%D' is from protected base class";
1067 /* The implicit typename extension allows us to find type
1068 declarations in dependent base clases. It also handles
1069 out-of-class definitions where the enclosing class is a
1070 template. For example:
1072 template <class T> struct S { struct I { void f(); }; };
1073 template <class T> void S<T>::I::f() {}
1075 will come through here to handle `S<T>::I'. The bottom line is
1076 that while searching for the field, we will have happily
1077 descended into dependent base classes, and we must now figure out
1078 what to do about it. */
1080 /* If we're not in a template, or the search terminated in the
1081 current class, then there's no problem. */
1082 if (!processing_template_decl
1083 || currently_open_class (BINFO_TYPE (lfi
->rval_binfo
)))
1086 /* We need to return a member template class so we can define partial
1087 specializations. Is there a better way? */
1088 if (DECL_CLASS_TEMPLATE_P (rval
))
1091 /* Walk the path to the base in which the search finally suceeded,
1092 checking for dependent bases along the way. */
1093 for (tp
= (currently_open_class (BINFO_TYPE (*search_head
)))
1094 ? search_head
+ 1 : search_head
;
1098 if (!uses_template_parms (BINFO_TYPE (*tp
)))
1101 if (TREE_CODE (rval
) != TYPE_DECL
)
1103 /* The thing we're looking for isn't a type, so the implicit
1104 typename extension doesn't apply, so we just pretend we
1105 didn't find anything. */
1106 lfi
->rval
= NULL_TREE
;
1110 /* We've passed a dependent base on our way to finding the
1111 type. So, create an implicit typename type. The appropriate
1112 context for the typename is *TP. But, there's a small catch;
1113 the base classes for a partial instantiation are not correct,
1114 because we don't tsubst into them when we do the partial
1115 instantiation. So, we just use the context of the current
1117 lfi
->rval
= TYPE_STUB_DECL (build_typename_type
1118 (BINFO_TYPE (*search_head
),
1119 lfi
->name
, lfi
->name
,
1125 /* Look for a field named NAME in an inheritance lattice dominated by
1126 XBASETYPE. PROTECT is zero if we can avoid computing access
1127 information, otherwise it is 1. WANT_TYPE is 1 when we should only
1128 return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
1130 It was not clear what should happen if WANT_TYPE is set, and an
1131 ambiguity is found. At least one use (lookup_name) to not see
1135 lookup_field (xbasetype
, name
, protect
, want_type
)
1136 register tree xbasetype
, name
;
1137 int protect
, want_type
;
1139 tree rval
, rval_binfo
= NULL_TREE
;
1140 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1141 struct lookup_field_info lfi
;
1143 /* rval_binfo is the binfo associated with the found member, note,
1144 this can be set with useful information, even when rval is not
1145 set, because it must deal with ALL members, not just non-function
1146 members. It is used for ambiguity checking and the hidden
1147 checks. Whereas rval is only set if a proper (not hidden)
1148 non-function member is found. */
1150 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1151 hiding checks, as virtual base classes may not be shared. The strategy
1152 is we always go into the binfo hierarchy owned by TYPE_BINFO of
1153 virtual base classes, as we cross virtual base class lines. This way
1154 we know that binfo of a virtual base class will always == itself when
1155 found along any line. (mrs) */
1159 bzero (&lfi
, sizeof (lfi
));
1162 /* We cannot search for constructor/destructor names like this. */
1163 /* This can't go here, but where should it go? */
1164 /* If we are looking for a constructor in a templated type, use the
1165 unspecialized name, as that is how we store it. */
1166 if (IDENTIFIER_TEMPLATE (name
))
1167 name
= constructor_name (name
);
1170 if (xbasetype
== current_class_type
&& TYPE_BEING_DEFINED (xbasetype
)
1171 && IDENTIFIER_CLASS_VALUE (name
))
1173 tree field
= IDENTIFIER_CLASS_VALUE (name
);
1174 if (TREE_CODE (field
) != FUNCTION_DECL
1175 && ! (want_type
&& TREE_CODE (field
) != TYPE_DECL
))
1179 if (TREE_CODE (xbasetype
) == TREE_VEC
)
1181 type
= BINFO_TYPE (xbasetype
);
1182 basetype_path
= xbasetype
;
1184 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype
)))
1187 basetype_path
= TYPE_BINFO (type
);
1188 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
,
1192 my_friendly_abort (97);
1194 complete_type (type
);
1196 #ifdef GATHER_STATISTICS
1197 n_calls_lookup_field
++;
1198 #endif /* GATHER_STATISTICS */
1201 lfi
.protect
= protect
;
1202 lfi
.want_type
= want_type
;
1203 lfi
.access
= access_default_node
;
1204 breadth_first_search (basetype_path
, &lookup_field_r
,
1205 &lookup_field_queue_p
, &lookup_field_post
, &lfi
);
1207 rval_binfo
= lfi
.rval_binfo
;
1209 type
= BINFO_TYPE (rval_binfo
);
1210 errstr
= lfi
.errstr
;
1212 /* If we are not interested in ambiguities, don't report them;
1213 just return NULL_TREE. */
1214 if (!protect
&& lfi
.ambiguous
)
1217 if (errstr
&& protect
)
1219 cp_error (errstr
, name
, type
);
1221 print_candidates (lfi
.ambiguous
);
1222 rval
= error_mark_node
;
1228 /* Try to find NAME inside a nested class. */
1231 lookup_nested_field (name
, complain
)
1237 tree id
= NULL_TREE
;
1238 if (TYPE_MAIN_DECL (current_class_type
))
1240 /* Climb our way up the nested ladder, seeing if we're trying to
1241 modify a field in an enclosing class. If so, we should only
1242 be able to modify if it's static. */
1243 for (t
= TYPE_MAIN_DECL (current_class_type
);
1244 t
&& DECL_CONTEXT (t
);
1245 t
= TYPE_MAIN_DECL (DECL_CONTEXT (t
)))
1247 if (TREE_CODE (DECL_CONTEXT (t
)) != RECORD_TYPE
)
1250 /* N.B.: lookup_field will do the access checking for us */
1251 id
= lookup_field (DECL_CONTEXT (t
), name
, complain
, 0);
1252 if (id
== error_mark_node
)
1258 if (id
!= NULL_TREE
)
1260 if (TREE_CODE (id
) == FIELD_DECL
1261 && ! TREE_STATIC (id
)
1262 && TREE_TYPE (id
) != error_mark_node
)
1266 /* At parse time, we don't want to give this error, since
1267 we won't have enough state to make this kind of
1268 decision properly. But there are times (e.g., with
1269 enums in nested classes) when we do need to call
1270 this fn at parse time. So, in those cases, we pass
1271 complain as a 0 and just return a NULL_TREE. */
1272 cp_error ("assignment to non-static member `%D' of enclosing class `%T'",
1273 id
, DECL_CONTEXT (t
));
1274 /* Mark this for do_identifier(). It would otherwise
1275 claim that the variable was undeclared. */
1276 TREE_TYPE (id
) = error_mark_node
;
1292 /* TYPE is a class type. Return the index of the fields within
1293 the method vector with name NAME, or -1 is no such field exists. */
1296 lookup_fnfields_1 (type
, name
)
1299 register tree method_vec
1300 = CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
1302 if (method_vec
!= 0)
1304 register tree
*methods
= &TREE_VEC_ELT (method_vec
, 0);
1305 register tree
*end
= TREE_VEC_END (method_vec
);
1307 #ifdef GATHER_STATISTICS
1308 n_calls_lookup_fnfields_1
++;
1309 #endif /* GATHER_STATISTICS */
1311 /* Constructors are first... */
1312 if (*methods
&& name
== ctor_identifier
)
1315 /* and destructors are second. */
1316 if (*++methods
&& name
== dtor_identifier
)
1319 while (++methods
!= end
&& *methods
)
1321 #ifdef GATHER_STATISTICS
1322 n_outer_fields_searched
++;
1323 #endif /* GATHER_STATISTICS */
1324 if (DECL_NAME (OVL_CURRENT (*methods
)) == name
)
1328 /* If we didn't find it, it might have been a template
1329 conversion operator. (Note that we don't look for this case
1330 above so that we will always find specializations first.) */
1331 if ((methods
== end
|| !*methods
)
1332 && IDENTIFIER_TYPENAME_P (name
))
1334 methods
= &TREE_VEC_ELT (method_vec
, 0) + 1;
1336 while (++methods
!= end
&& *methods
)
1338 tree method_name
= DECL_NAME (OVL_CURRENT (*methods
));
1340 if (!IDENTIFIER_TYPENAME_P (method_name
))
1342 /* Since all conversion operators come first, we know
1343 there is no such operator. */
1347 else if (TREE_CODE (OVL_CURRENT (*methods
)) == TEMPLATE_DECL
)
1352 if (methods
!= end
&& *methods
)
1353 return methods
- &TREE_VEC_ELT (method_vec
, 0);
1359 /* Starting from BASETYPE, return a TREE_BASELINK-like object
1360 which gives the following information (in a list):
1362 TREE_TYPE: list of basetypes needed to get to...
1363 TREE_VALUE: list of all functions in a given type
1364 which have name NAME.
1366 No access information is computed by this function,
1367 other then to adorn the list of basetypes with
1370 If there are two ways to find a name (two members), if COMPLAIN is
1371 non-zero, then error_mark_node is returned, and an error message is
1372 printed, otherwise, just an error_mark_node is returned.
1374 As a special case, is COMPLAIN is -1, we don't complain, and we
1375 don't return error_mark_node, but rather the complete list of
1376 virtuals. This is used by get_virtuals_named_this. */
1379 lookup_fnfields (basetype_path
, name
, complain
)
1380 tree basetype_path
, name
;
1383 int head
= 0, tail
= 0;
1384 tree type
, rval
, rval_binfo
= NULL_TREE
, rvals
= NULL_TREE
;
1385 tree rval_binfo_h
= NULL_TREE
, binfo
, basetype_chain
, binfo_h
;
1386 int idx
, find_all
= 0;
1388 /* rval_binfo is the binfo associated with the found member, note,
1389 this can be set with useful information, even when rval is not
1390 set, because it must deal with ALL members, not just function
1391 members. It is used for ambiguity checking and the hidden
1392 checks. Whereas rval is only set if a proper (not hidden)
1393 function member is found. */
1395 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1396 hiding checks, as virtual base classes may not be shared. The strategy
1397 is we always go into the binfo hierarchy owned by TYPE_BINFO of
1398 virtual base classes, as we cross virtual base class lines. This way
1399 we know that binfo of a virtual base class will always == itself when
1400 found along any line. (mrs) */
1402 /* For now, don't try this. */
1403 int protect
= complain
;
1410 protect
= complain
= 0;
1414 /* We cannot search for constructor/destructor names like this. */
1415 /* This can't go here, but where should it go? */
1416 /* If we are looking for a constructor in a templated type, use the
1417 unspecialized name, as that is how we store it. */
1418 if (IDENTIFIER_TEMPLATE (name
))
1419 name
= constructor_name (name
);
1422 binfo
= basetype_path
;
1424 type
= complete_type (BINFO_TYPE (basetype_path
));
1426 #ifdef GATHER_STATISTICS
1427 n_calls_lookup_fnfields
++;
1428 #endif /* GATHER_STATISTICS */
1430 idx
= lookup_fnfields_here (type
, name
);
1431 if (idx
>= 0 || lookup_field_1 (type
, name
))
1433 rval_binfo
= basetype_path
;
1434 rval_binfo_h
= rval_binfo
;
1439 rval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1440 rvals
= scratch_tree_cons (basetype_path
, rval
, rvals
);
1441 if (BINFO_BASETYPES (binfo
) && CLASSTYPE_BASELINK_VEC (type
))
1442 TREE_TYPE (rvals
) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type
), idx
);
1448 if (name
== ctor_identifier
|| name
== dtor_identifier
)
1450 /* Don't allow lookups of constructors and destructors to go
1451 deeper than the first place we look. */
1455 if (basetype_path
== TYPE_BINFO (type
))
1457 basetype_chain
= CLASSTYPE_BINFO_AS_LIST (type
);
1458 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
,
1462 basetype_chain
= build_expr_list (NULL_TREE
, basetype_path
);
1464 /* The ambiguity check relies upon breadth first searching. */
1466 search_stack
= push_search_level (search_stack
, &search_obstack
);
1467 binfo
= basetype_path
;
1472 tree binfos
= BINFO_BASETYPES (binfo
);
1473 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1476 /* Process and/or queue base types. */
1477 for (i
= 0; i
< n_baselinks
; i
++)
1479 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1480 if (BINFO_FIELDS_MARKED (base_binfo
) == 0)
1484 SET_BINFO_FIELDS_MARKED (base_binfo
);
1485 btypes
= scratch_tree_cons (NULL_TREE
, base_binfo
, basetype_chain
);
1486 if (TREE_VIA_VIRTUAL (base_binfo
))
1487 btypes
= scratch_tree_cons (NULL_TREE
,
1488 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h
), i
))),
1491 btypes
= scratch_tree_cons (NULL_TREE
,
1492 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h
), i
),
1494 obstack_ptr_grow (&search_obstack
, btypes
);
1496 if (tail
>= search_stack
->limit
)
1497 my_friendly_abort (99);
1501 /* Process head of queue, if one exists. */
1505 basetype_chain
= search_stack
->first
[head
++];
1506 binfo_h
= TREE_VALUE (basetype_chain
);
1507 basetype_chain
= TREE_CHAIN (basetype_chain
);
1508 basetype_path
= TREE_VALUE (basetype_chain
);
1509 if (TREE_CHAIN (basetype_chain
))
1511 ((BINFO_INHERITANCE_CHAIN (basetype_path
)
1512 == TREE_VALUE (TREE_CHAIN (basetype_chain
)))
1513 /* We only approximate base info for partial instantiations. */
1514 || current_template_parms
,
1517 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
)
1518 == NULL_TREE
, 980827);
1520 binfo
= basetype_path
;
1521 type
= BINFO_TYPE (binfo
);
1523 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1524 and we do find NAME in TYPE, verify that such a second
1525 sighting is in fact valid. */
1527 idx
= lookup_fnfields_here (type
, name
);
1529 if (idx
>= 0 || (lookup_field_1 (type
, name
)!=NULL_TREE
&& !find_all
))
1531 if (rval_binfo
&& !find_all
&& hides (rval_binfo_h
, binfo_h
))
1533 /* This is ok, the member found is in rval_binfo, not
1536 else if (rval_binfo
==NULL_TREE
|| find_all
|| hides (binfo_h
, rval_binfo_h
))
1538 /* This is ok, the member found is here (binfo), not in
1542 rval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1543 /* Note, rvals can only be previously set if find_all is
1545 rvals
= scratch_tree_cons (basetype_path
, rval
, rvals
);
1546 if (TYPE_BINFO_BASETYPES (type
)
1547 && CLASSTYPE_BASELINK_VEC (type
))
1548 TREE_TYPE (rvals
) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type
), idx
);
1552 /* Undo finding it before, as something else hides it. */
1557 rval_binfo_h
= binfo_h
;
1561 /* This is ambiguous. */
1562 errstr
= "request for method `%D' is ambiguous";
1563 rvals
= error_mark_node
;
1569 tree
*tp
= search_stack
->first
;
1570 tree
*search_tail
= tp
+ tail
;
1572 while (tp
< search_tail
)
1574 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp
)));
1578 search_stack
= pop_search_level (search_stack
);
1580 if (errstr
&& protect
)
1582 cp_error (errstr
, name
);
1583 rvals
= error_mark_node
;
1589 /* Look for a field or function named NAME in an inheritance lattice
1590 dominated by XBASETYPE. PROTECT is zero if we can avoid computing
1591 access information, otherwise it is 1. WANT_TYPE is 1 when we should
1592 only return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE. */
1595 lookup_member (xbasetype
, name
, protect
, want_type
)
1596 tree xbasetype
, name
;
1597 int protect
, want_type
;
1599 tree ret
, basetype_path
;
1601 if (TREE_CODE (xbasetype
) == TREE_VEC
)
1602 basetype_path
= xbasetype
;
1603 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype
)))
1605 basetype_path
= TYPE_BINFO (xbasetype
);
1606 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
)
1607 == NULL_TREE
, 980827);
1610 my_friendly_abort (97);
1612 ret
= lookup_field (basetype_path
, name
, protect
, want_type
);
1613 if (! ret
&& ! want_type
)
1614 ret
= lookup_fnfields (basetype_path
, name
, protect
);
1618 /* BREADTH-FIRST SEARCH ROUTINES. */
1620 /* Search a multiple inheritance hierarchy by breadth-first search.
1622 BINFO is an aggregate type, possibly in a multiple-inheritance hierarchy.
1623 TESTFN is a function, which, if true, means that our condition has
1624 been met, and its return value should be returned.
1625 QFN, if non-NULL, is a predicate dictating whether the type should
1627 POSTFN, if non-NULL, is a function to call before returning. It is
1628 passed an array whose first element is the most derived type in the
1629 chain, and whose last element is the least derived type.
1631 All of the functions are also passed the DATA, which they may use
1635 breadth_first_search (binfo
, testfn
, qfn
, postfn
, data
)
1637 tree (*testfn
) PROTO((tree
, void *));
1638 int (*qfn
) PROTO((tree
, void *));
1639 void (*postfn
) PROTO((tree
*, tree
*, void *));
1642 int head
= 0, tail
= 0;
1643 tree rval
= NULL_TREE
;
1647 search_stack
= push_search_level (search_stack
, &search_obstack
);
1649 SET_BINFO_MARKED (binfo
);
1650 obstack_ptr_grow (&search_obstack
, binfo
);
1659 /* Pull the next type out of the queue. */
1660 binfo
= search_stack
->first
[head
++];
1662 /* If this is the one we're looking for, we're done. */
1663 rval
= (*testfn
) (binfo
, data
);
1667 /* Queue up the base types. */
1668 binfos
= BINFO_BASETYPES (binfo
);
1669 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1670 for (i
= 0; i
< n_baselinks
; i
++)
1672 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1674 if (TREE_VIA_VIRTUAL (base_binfo
))
1675 base_binfo
= TYPE_BINFO (BINFO_TYPE (base_binfo
));
1677 if (BINFO_MARKED (base_binfo
) == 0
1678 && (qfn
== 0 || (*qfn
) (base_binfo
, data
)))
1680 SET_BINFO_MARKED (base_binfo
);
1681 obstack_ptr_grow (&search_obstack
, base_binfo
);
1683 if (tail
>= search_stack
->limit
)
1684 my_friendly_abort (100);
1689 tp
= search_stack
->first
;
1690 search_tail
= tp
+ tail
;
1693 (*postfn
) (tp
, search_tail
, data
);
1695 while (tp
< search_tail
)
1698 CLEAR_BINFO_MARKED (binfo
);
1701 search_stack
= pop_search_level (search_stack
);
1705 /* Functions to use in breadth first searches. */
1706 typedef tree (*pfi
) PROTO((tree
));
1708 static tree declarator
;
1711 get_virtuals_named_this (binfo
)
1716 fields
= lookup_fnfields (binfo
, declarator
, -1);
1717 /* fields cannot be error_mark_node */
1722 /* Get to the function decls, and return the first virtual function
1723 with this name, if there is one. */
1728 for (fndecl
= TREE_VALUE (fields
); fndecl
; fndecl
= OVL_NEXT (fndecl
))
1729 if (DECL_VINDEX (OVL_CURRENT (fndecl
)))
1731 fields
= next_baselink (fields
);
1737 get_virtual_destructor (binfo
, data
)
1741 tree type
= BINFO_TYPE (binfo
);
1742 if (TYPE_HAS_DESTRUCTOR (type
)
1743 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1)))
1744 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), 1);
1749 tree_has_any_destructor_p (binfo
, data
)
1753 tree type
= BINFO_TYPE (binfo
);
1754 return TYPE_NEEDS_DESTRUCTOR (type
);
1757 /* Returns > 0 if a function with type DRETTYPE overriding a function
1758 with type BRETTYPE is covariant, as defined in [class.virtual].
1760 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1761 adjustment), or -1 if pedantically invalid covariance. */
1764 covariant_return_p (brettype
, drettype
)
1765 tree brettype
, drettype
;
1769 if (TREE_CODE (brettype
) == FUNCTION_DECL
1770 || TREE_CODE (brettype
) == THUNK_DECL
)
1772 brettype
= TREE_TYPE (TREE_TYPE (brettype
));
1773 drettype
= TREE_TYPE (TREE_TYPE (drettype
));
1775 else if (TREE_CODE (brettype
) == METHOD_TYPE
)
1777 brettype
= TREE_TYPE (brettype
);
1778 drettype
= TREE_TYPE (drettype
);
1781 if (same_type_p (brettype
, drettype
))
1784 if (! (TREE_CODE (brettype
) == TREE_CODE (drettype
)
1785 && (TREE_CODE (brettype
) == POINTER_TYPE
1786 || TREE_CODE (brettype
) == REFERENCE_TYPE
)
1787 && TYPE_QUALS (brettype
) == TYPE_QUALS (drettype
)))
1790 if (! can_convert (brettype
, drettype
))
1793 brettype
= TREE_TYPE (brettype
);
1794 drettype
= TREE_TYPE (drettype
);
1796 /* If not pedantic, allow any standard pointer conversion. */
1797 if (! IS_AGGR_TYPE (drettype
) || ! IS_AGGR_TYPE (brettype
))
1800 binfo
= get_binfo (brettype
, drettype
, 1);
1802 /* If we get an error_mark_node from get_binfo, it already complained,
1803 so let's just succeed. */
1804 if (binfo
== error_mark_node
)
1807 if (! BINFO_OFFSET_ZEROP (binfo
) || TREE_VIA_VIRTUAL (binfo
))
1812 /* Given a class type TYPE, and a function decl FNDECL, look for a
1813 virtual function in TYPE's hierarchy which FNDECL could match as a
1814 virtual function. It doesn't matter which one we find.
1816 DTORP is nonzero if we are looking for a destructor. Destructors
1817 need special treatment because they do not match by name. */
1820 get_matching_virtual (binfo
, fndecl
, dtorp
)
1824 tree tmp
= NULL_TREE
;
1827 if (TREE_CODE (fndecl
) == TEMPLATE_DECL
)
1828 /* In [temp.mem] we have:
1830 A specialization of a member function template does not
1831 override a virtual function from a base class. */
1834 /* Breadth first search routines start searching basetypes
1835 of TYPE, so we must perform first ply of search here. */
1838 return breadth_first_search (binfo
,
1839 get_virtual_destructor
,
1840 tree_has_any_destructor_p
, 0, 0);
1844 tree drettype
, dtypes
, btypes
, instptr_type
;
1845 tree basetype
= DECL_CLASS_CONTEXT (fndecl
);
1846 tree baselink
, best
= NULL_TREE
;
1847 tree name
= DECL_ASSEMBLER_NAME (fndecl
);
1849 declarator
= DECL_NAME (fndecl
);
1850 if (IDENTIFIER_VIRTUAL_P (declarator
) == 0)
1853 baselink
= get_virtuals_named_this (binfo
);
1854 if (baselink
== NULL_TREE
)
1857 drettype
= TREE_TYPE (TREE_TYPE (fndecl
));
1858 dtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1859 if (DECL_STATIC_FUNCTION_P (fndecl
))
1860 instptr_type
= NULL_TREE
;
1862 instptr_type
= TREE_TYPE (TREE_VALUE (dtypes
));
1864 for (; baselink
; baselink
= next_baselink (baselink
))
1867 for (tmps
= TREE_VALUE (baselink
); tmps
; tmps
= OVL_NEXT (tmps
))
1869 tmp
= OVL_CURRENT (tmps
);
1870 if (! DECL_VINDEX (tmp
))
1873 btypes
= TYPE_ARG_TYPES (TREE_TYPE (tmp
));
1874 if (instptr_type
== NULL_TREE
)
1876 if (compparms (TREE_CHAIN (btypes
), dtypes
))
1877 /* Caller knows to give error in this case. */
1882 if (/* The first parameter is the `this' parameter,
1883 which has POINTER_TYPE, and we can therefore
1884 safely use TYPE_QUALS, rather than
1886 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes
)))
1887 == TYPE_QUALS (instptr_type
))
1888 && compparms (TREE_CHAIN (btypes
), TREE_CHAIN (dtypes
)))
1890 tree brettype
= TREE_TYPE (TREE_TYPE (tmp
));
1891 if (same_type_p (brettype
, drettype
))
1893 else if ((i
= covariant_return_p (brettype
, drettype
)))
1896 sorry ("adjusting pointers for covariant returns");
1898 if (pedantic
&& i
== -1)
1900 cp_pedwarn_at ("invalid covariant return type for `%#D' (must be pointer or reference to class)", fndecl
);
1901 cp_pedwarn_at (" overriding `%#D'", tmp
);
1904 else if (IS_AGGR_TYPE_2 (brettype
, drettype
)
1905 && same_or_base_type_p (brettype
, drettype
))
1907 error ("invalid covariant return type (must use pointer or reference)");
1908 cp_error_at (" overriding `%#D'", tmp
);
1909 cp_error_at (" with `%#D'", fndecl
);
1911 else if (IDENTIFIER_ERROR_LOCUS (name
) == NULL_TREE
)
1913 cp_error_at ("conflicting return type specified for virtual function `%#D'", fndecl
);
1914 cp_error_at (" overriding definition as `%#D'", tmp
);
1915 SET_IDENTIFIER_ERROR_LOCUS (name
, basetype
);
1918 /* FNDECL overrides this function. We continue to
1919 check all the other functions in order to catch
1920 errors; it might be that in some other baseclass
1921 a virtual function was declared with the same
1922 parameter types, but a different return type. */
1932 /* Return the list of virtual functions which are abstract in type
1933 TYPE that come from non virtual base classes. See
1934 expand_direct_vtbls_init for the style of search we do. */
1937 get_abstract_virtuals_1 (binfo
, do_self
, abstract_virtuals
)
1940 tree abstract_virtuals
;
1942 tree binfos
= BINFO_BASETYPES (binfo
);
1943 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1945 for (i
= 0; i
< n_baselinks
; i
++)
1947 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1948 int is_not_base_vtable
1949 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
1950 if (! TREE_VIA_VIRTUAL (base_binfo
))
1952 = get_abstract_virtuals_1 (base_binfo
, is_not_base_vtable
,
1955 /* Should we use something besides CLASSTYPE_VFIELDS? */
1956 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
1958 tree virtuals
= BINFO_VIRTUALS (binfo
);
1960 skip_rtti_stuff (&virtuals
);
1964 tree base_pfn
= FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals
));
1965 tree base_fndecl
= TREE_OPERAND (base_pfn
, 0);
1966 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl
))
1967 abstract_virtuals
= tree_cons (NULL_TREE
, base_fndecl
, abstract_virtuals
);
1968 virtuals
= TREE_CHAIN (virtuals
);
1971 return abstract_virtuals
;
1974 /* Return the list of virtual functions which are abstract in type TYPE.
1975 This information is cached, and so must be built on a
1976 non-temporary obstack. */
1979 get_abstract_virtuals (type
)
1983 tree abstract_virtuals
= NULL
;
1985 /* First get all from non-virtual bases. */
1987 = get_abstract_virtuals_1 (TYPE_BINFO (type
), 1, abstract_virtuals
);
1989 for (vbases
= CLASSTYPE_VBASECLASSES (type
); vbases
; vbases
= TREE_CHAIN (vbases
))
1991 tree virtuals
= BINFO_VIRTUALS (vbases
);
1993 skip_rtti_stuff (&virtuals
);
1997 tree base_pfn
= FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals
));
1998 tree base_fndecl
= TREE_OPERAND (base_pfn
, 0);
1999 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl
))
2000 cp_error ("`%#D' needs a final overrider", base_fndecl
);
2001 else if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl
))
2002 abstract_virtuals
= tree_cons (NULL_TREE
, base_fndecl
, abstract_virtuals
);
2003 virtuals
= TREE_CHAIN (virtuals
);
2006 return nreverse (abstract_virtuals
);
2009 /* For the type TYPE, return a list of member functions available from
2010 base classes with name NAME. The TREE_VALUE of the list is a chain of
2011 member functions with name NAME. The TREE_PURPOSE of the list is a
2012 basetype, or a list of base types (in reverse order) which were
2013 traversed to reach the chain of member functions. If we reach a base
2014 type which provides a member function of name NAME, and which has at
2015 most one base type itself, then we can terminate the search. */
2018 get_baselinks (type_as_binfo_list
, type
, name
)
2019 tree type_as_binfo_list
;
2022 int head
= 0, tail
= 0, idx
;
2023 tree rval
= 0, nval
= 0;
2024 tree basetypes
= type_as_binfo_list
;
2025 tree binfo
= TYPE_BINFO (type
);
2027 search_stack
= push_search_level (search_stack
, &search_obstack
);
2031 tree binfos
= BINFO_BASETYPES (binfo
);
2032 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2034 /* Process and/or queue base types. */
2035 for (i
= 0; i
< n_baselinks
; i
++)
2037 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2040 btypes
= hash_tree_cons (TREE_VIA_PUBLIC (base_binfo
),
2041 TREE_VIA_VIRTUAL (base_binfo
),
2042 TREE_VIA_PROTECTED (base_binfo
),
2043 NULL_TREE
, base_binfo
,
2045 obstack_ptr_grow (&search_obstack
, btypes
);
2046 search_stack
->first
= (tree
*)obstack_base (&search_obstack
);
2051 /* Process head of queue, if one exists. */
2055 basetypes
= search_stack
->first
[head
++];
2056 binfo
= TREE_VALUE (basetypes
);
2057 type
= BINFO_TYPE (binfo
);
2058 idx
= lookup_fnfields_1 (type
, name
);
2061 nval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
2062 rval
= hash_tree_cons (0, 0, 0, basetypes
, nval
, rval
);
2063 if (TYPE_BINFO_BASETYPES (type
) == 0)
2065 else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type
)) == 1)
2067 if (CLASSTYPE_BASELINK_VEC (type
))
2068 TREE_TYPE (rval
) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type
), idx
);
2075 search_stack
= pop_search_level (search_stack
);
2080 next_baselink (baselink
)
2083 tree tmp
= TREE_TYPE (baselink
);
2084 baselink
= TREE_CHAIN (baselink
);
2087 /* @@ does not yet add previous base types. */
2088 baselink
= tree_cons (TREE_PURPOSE (tmp
), TREE_VALUE (tmp
),
2090 TREE_TYPE (baselink
) = TREE_TYPE (tmp
);
2091 tmp
= TREE_CHAIN (tmp
);
2096 /* DEPTH-FIRST SEARCH ROUTINES. */
2098 /* This routine converts a pointer to be a pointer of an immediate
2099 base class. The normal convert_pointer_to routine would diagnose
2100 the conversion as ambiguous, under MI code that has the base class
2101 as an ambiguous base class. */
2104 convert_pointer_to_single_level (to_type
, expr
)
2107 tree binfo_of_derived
;
2110 binfo_of_derived
= TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr
)));
2111 last
= get_binfo (to_type
, TREE_TYPE (TREE_TYPE (expr
)), 0);
2112 my_friendly_assert (BINFO_INHERITANCE_CHAIN (last
) == binfo_of_derived
,
2114 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived
) == NULL_TREE
,
2116 return build_vbase_path (PLUS_EXPR
, build_pointer_type (to_type
), expr
,
2120 /* The main function which implements depth first search.
2122 This routine has to remember the path it walked up, when
2123 dfs_init_vbase_pointers is the work function, as otherwise there
2124 would be no record. */
2127 dfs_walk (binfo
, fn
, qfn
)
2129 void (*fn
) PROTO((tree
));
2130 int (*qfn
) PROTO((tree
));
2132 tree binfos
= BINFO_BASETYPES (binfo
);
2133 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2135 for (i
= 0; i
< n_baselinks
; i
++)
2137 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2139 if (qfn
== 0 || (*qfn
)(base_binfo
))
2141 if (TREE_CODE (BINFO_TYPE (base_binfo
)) == TEMPLATE_TYPE_PARM
2142 || TREE_CODE (BINFO_TYPE (base_binfo
)) == TEMPLATE_TEMPLATE_PARM
)
2144 else if (fn
== dfs_init_vbase_pointers
)
2146 /* When traversing an arbitrary MI hierarchy, we need to keep
2147 a record of the path we took to get down to the final base
2148 type, as otherwise there would be no record of it, and just
2149 trying to blindly convert at the bottom would be ambiguous.
2151 The easiest way is to do the conversions one step at a time,
2152 as we know we want the immediate base class at each step.
2154 The only special trick to converting one step at a time,
2155 is that when we hit the last virtual base class, we must
2156 use the SLOT value for it, and not use the normal convert
2157 routine. We use the last virtual base class, as in our
2158 implementation, we have pointers to all virtual base
2159 classes in the base object. */
2161 tree saved_vbase_decl_ptr_intermediate
2162 = vbase_decl_ptr_intermediate
;
2164 if (TREE_VIA_VIRTUAL (base_binfo
))
2166 /* No need for the conversion here, as we know it is the
2168 vbase_decl_ptr_intermediate
2169 = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo
));
2173 vbase_decl_ptr_intermediate
2174 = convert_pointer_to_single_level (BINFO_TYPE (base_binfo
),
2175 vbase_decl_ptr_intermediate
);
2178 dfs_walk (base_binfo
, fn
, qfn
);
2180 vbase_decl_ptr_intermediate
= saved_vbase_decl_ptr_intermediate
;
2183 dfs_walk (base_binfo
, fn
, qfn
);
2190 /* Like dfs_walk, but only walk until fn returns something, and return
2191 that. We also use the real vbase binfos instead of the placeholders
2192 in the normal binfo hierarchy. START is the most-derived type for this
2193 hierarchy, so that we can find the vbase binfos. */
2196 dfs_search (binfo
, fn
, start
)
2198 tree (*fn
) PROTO((tree
));
2200 tree binfos
= BINFO_BASETYPES (binfo
);
2201 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2204 for (i
= 0; i
< n_baselinks
; i
++)
2206 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2208 if (TREE_CODE (BINFO_TYPE (base_binfo
)) == TEMPLATE_TYPE_PARM
2209 || TREE_CODE (BINFO_TYPE (base_binfo
)) == TEMPLATE_TEMPLATE_PARM
)
2213 if (TREE_VIA_VIRTUAL (base_binfo
) && start
)
2214 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
),
2215 CLASSTYPE_VBASECLASSES (start
));
2216 retval
= dfs_search (base_binfo
, fn
, start
);
2225 int markedp (binfo
) tree binfo
;
2226 { return BINFO_MARKED (binfo
); }
2227 static int unmarkedp (binfo
) tree binfo
;
2228 { return BINFO_MARKED (binfo
) == 0; }
2231 static int bfs_markedp (binfo
, i
) tree binfo
; int i
;
2232 { return BINFO_MARKED (BINFO_BASETYPE (binfo
, i
)); }
2233 static int bfs_unmarkedp (binfo
, i
) tree binfo
; int i
;
2234 { return BINFO_MARKED (BINFO_BASETYPE (binfo
, i
)) == 0; }
2235 static int bfs_marked_vtable_pathp (binfo
, i
) tree binfo
; int i
;
2236 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo
, i
)); }
2237 static int bfs_unmarked_vtable_pathp (binfo
, i
) tree binfo
; int i
;
2238 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo
, i
)) == 0; }
2239 static int bfs_marked_new_vtablep (binfo
, i
) tree binfo
; int i
;
2240 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo
, i
)); }
2241 static int bfs_unmarked_new_vtablep (binfo
, i
) tree binfo
; int i
;
2242 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo
, i
)) == 0; }
2245 static int marked_vtable_pathp (binfo
) tree binfo
;
2246 { return BINFO_VTABLE_PATH_MARKED (binfo
); }
2247 static int unmarked_vtable_pathp (binfo
) tree binfo
;
2248 { return BINFO_VTABLE_PATH_MARKED (binfo
) == 0; }
2249 static int marked_new_vtablep (binfo
) tree binfo
;
2250 { return BINFO_NEW_VTABLE_MARKED (binfo
); }
2251 static int unmarked_new_vtablep (binfo
) tree binfo
;
2252 { return BINFO_NEW_VTABLE_MARKED (binfo
) == 0; }
2253 static int marked_pushdecls_p (binfo
) tree binfo
;
2254 { return BINFO_PUSHDECLS_MARKED (binfo
); }
2255 static int unmarked_pushdecls_p (binfo
) tree binfo
;
2256 { return BINFO_PUSHDECLS_MARKED (binfo
) == 0; }
2259 static int dfs_search_slot_nonempty_p (binfo
) tree binfo
;
2260 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) != 0; }
2263 static int dfs_debug_unmarkedp (binfo
) tree binfo
;
2264 { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo
)) == 0; }
2266 /* The worker functions for `dfs_walk'. These do not need to
2267 test anything (vis a vis marking) if they are paired with
2268 a predicate function (above). */
2272 dfs_mark (binfo
) tree binfo
;
2273 { SET_BINFO_MARKED (binfo
); }
2277 dfs_unmark (binfo
) tree binfo
;
2278 { CLEAR_BINFO_MARKED (binfo
); }
2282 dfs_mark_vtable_path (binfo
) tree binfo
;
2283 { SET_BINFO_VTABLE_PATH_MARKED (binfo
); }
2286 dfs_unmark_vtable_path (binfo
) tree binfo
;
2287 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
); }
2290 dfs_mark_new_vtable (binfo
) tree binfo
;
2291 { SET_BINFO_NEW_VTABLE_MARKED (binfo
); }
2294 dfs_unmark_new_vtable (binfo
) tree binfo
;
2295 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
); }
2298 dfs_clear_search_slot (binfo
) tree binfo
;
2299 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo
)) = 0; }
2303 dfs_debug_mark (binfo
)
2306 tree t
= BINFO_TYPE (binfo
);
2308 /* Use heuristic that if there are virtual functions,
2309 ignore until we see a non-inline virtual function. */
2310 tree methods
= CLASSTYPE_METHOD_VEC (t
);
2312 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2317 /* If interface info is known, either we've already emitted the debug
2318 info or we don't need to. */
2319 if (CLASSTYPE_INTERFACE_KNOWN (t
))
2322 /* If debug info is requested from this context for this type, supply it.
2323 If debug info is requested from another context for this type,
2324 see if some third context can supply it. */
2325 if (current_function_decl
== NULL_TREE
2326 || DECL_CLASS_CONTEXT (current_function_decl
) != t
)
2328 if (TREE_VEC_ELT (methods
, 1))
2329 methods
= TREE_VEC_ELT (methods
, 1);
2330 else if (TREE_VEC_ELT (methods
, 0))
2331 methods
= TREE_VEC_ELT (methods
, 0);
2333 methods
= TREE_VEC_ELT (methods
, 2);
2334 methods
= OVL_CURRENT (methods
);
2337 if (DECL_VINDEX (methods
)
2338 && DECL_THIS_INLINE (methods
) == 0
2339 && DECL_ABSTRACT_VIRTUAL_P (methods
) == 0)
2341 /* Somebody, somewhere is going to have to define this
2342 virtual function. When they do, they will provide
2343 the debugging info. */
2346 methods
= TREE_CHAIN (methods
);
2349 /* We cannot rely on some alien method to solve our problems,
2350 so we must write out the debug info ourselves. */
2351 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = 0;
2352 rest_of_type_compilation (t
, toplevel_bindings_p ());
2355 /* Attach to the type of the virtual base class, the pointer to the
2356 virtual base class, given the global pointer vbase_decl_ptr.
2358 We use the global vbase_types. ICK! */
2361 dfs_find_vbases (binfo
)
2364 tree binfos
= BINFO_BASETYPES (binfo
);
2365 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2367 for (i
= n_baselinks
-1; i
>= 0; i
--)
2369 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2371 if (TREE_VIA_VIRTUAL (base_binfo
)
2372 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo
)) == 0)
2374 tree vbase
= BINFO_TYPE (base_binfo
);
2375 tree binfo
= binfo_member (vbase
, vbase_types
);
2377 CLASSTYPE_SEARCH_SLOT (vbase
)
2378 = build (PLUS_EXPR
, build_pointer_type (vbase
),
2379 vbase_decl_ptr
, BINFO_OFFSET (binfo
));
2382 SET_BINFO_VTABLE_PATH_MARKED (binfo
);
2383 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
2387 dfs_init_vbase_pointers (binfo
)
2390 tree type
= BINFO_TYPE (binfo
);
2391 tree fields
= TYPE_FIELDS (type
);
2392 tree this_vbase_ptr
;
2394 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2397 /* See finish_struct_1 for when we can enable this. */
2398 /* If we have a vtable pointer first, skip it. */
2399 if (VFIELD_NAME_P (DECL_NAME (fields
)))
2400 fields
= TREE_CHAIN (fields
);
2403 if (fields
== NULL_TREE
2404 || DECL_NAME (fields
) == NULL_TREE
2405 || ! VBASE_NAME_P (DECL_NAME (fields
)))
2408 this_vbase_ptr
= vbase_decl_ptr_intermediate
;
2410 if (build_pointer_type (type
) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr
)))
2411 my_friendly_abort (125);
2413 while (fields
&& DECL_NAME (fields
)
2414 && VBASE_NAME_P (DECL_NAME (fields
)))
2416 tree ref
= build (COMPONENT_REF
, TREE_TYPE (fields
),
2417 build_indirect_ref (this_vbase_ptr
, NULL_PTR
), fields
);
2418 tree init
= CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields
)));
2419 vbase_init_result
= tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields
)),
2421 build_modify_expr (ref
, NOP_EXPR
, init
),
2423 fields
= TREE_CHAIN (fields
);
2427 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2428 times, just NEW_VTABLE, but optimizer should make both with equal
2429 efficiency (though it does not currently). */
2432 dfs_clear_vbase_slots (binfo
)
2435 tree type
= BINFO_TYPE (binfo
);
2436 CLASSTYPE_SEARCH_SLOT (type
) = 0;
2437 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo
);
2438 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2442 init_vbase_pointers (type
, decl_ptr
)
2446 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
2448 int old_flag
= flag_this_is_variable
;
2449 tree binfo
= TYPE_BINFO (type
);
2450 flag_this_is_variable
= -2;
2451 vbase_types
= CLASSTYPE_VBASECLASSES (type
);
2452 vbase_decl_ptr
= vbase_decl_ptr_intermediate
= decl_ptr
;
2453 vbase_init_result
= NULL_TREE
;
2454 dfs_walk (binfo
, dfs_find_vbases
, unmarked_vtable_pathp
);
2455 dfs_walk (binfo
, dfs_init_vbase_pointers
, marked_vtable_pathp
);
2456 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
);
2457 flag_this_is_variable
= old_flag
;
2458 return vbase_init_result
;
2463 /* get the virtual context (the vbase that directly contains the
2464 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2465 or NULL_TREE if there is none.
2467 FNDECL must come from a virtual table from a virtual base to ensure that
2468 there is only one possible DECL_CLASS_CONTEXT.
2470 We know that if there is more than one place (binfo) the fndecl that the
2471 declared, they all refer to the same binfo. See get_class_offset_1 for
2472 the check that ensures this. */
2475 virtual_context (fndecl
, t
, vbase
)
2476 tree fndecl
, t
, vbase
;
2479 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl
), t
, 0, &path
) < 0)
2481 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2482 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl
), vbase
, 0, &path
) >= 0)
2486 /* Not sure if checking path == vbase is necessary here, but just in
2488 if (TREE_VIA_VIRTUAL (path
) || path
== vbase
)
2489 return binfo_member (BINFO_TYPE (path
), CLASSTYPE_VBASECLASSES (t
));
2490 path
= BINFO_INHERITANCE_CHAIN (path
);
2493 /* This shouldn't happen, I don't want errors! */
2494 warning ("recoverable compiler error, fixups for virtual function");
2499 if (TREE_VIA_VIRTUAL (path
))
2500 return binfo_member (BINFO_TYPE (path
), CLASSTYPE_VBASECLASSES (t
));
2501 path
= BINFO_INHERITANCE_CHAIN (path
);
2506 /* Fixups upcast offsets for one vtable.
2507 Entries may stay within the VBASE given, or
2508 they may upcast into a direct base, or
2509 they may upcast into a different vbase.
2511 We only need to do fixups in case 2 and 3. In case 2, we add in
2512 the virtual base offset to effect an upcast, in case 3, we add in
2513 the virtual base offset to effect an upcast, then subtract out the
2514 offset for the other virtual base, to effect a downcast into it.
2516 This routine mirrors fixup_vtable_deltas in functionality, though
2517 this one is runtime based, and the other is compile time based.
2518 Conceivably that routine could be removed entirely, and all fixups
2521 VBASE_OFFSETS is an association list of virtual bases that contains
2522 offset information for the virtual bases, so the offsets are only
2523 calculated once. The offsets are computed by where we think the
2524 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2525 the vbase really is. */
2528 expand_upcast_fixups (binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
,
2530 tree binfo
, addr
, orig_addr
, vbase
, vbase_addr
, t
, *vbase_offsets
;
2532 tree virtuals
= BINFO_VIRTUALS (binfo
);
2535 unsigned HOST_WIDE_INT n
;
2537 delta
= purpose_member (vbase
, *vbase_offsets
);
2540 delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase
));
2541 delta
= build (MINUS_EXPR
, ptrdiff_type_node
, delta
, vbase_addr
);
2542 delta
= save_expr (delta
);
2543 delta
= tree_cons (vbase
, delta
, *vbase_offsets
);
2544 *vbase_offsets
= delta
;
2547 n
= skip_rtti_stuff (&virtuals
);
2551 tree current_fndecl
= TREE_VALUE (virtuals
);
2552 current_fndecl
= FNADDR_FROM_VTABLE_ENTRY (current_fndecl
);
2553 current_fndecl
= TREE_OPERAND (current_fndecl
, 0);
2555 && current_fndecl
!= abort_fndecl
2556 && (vc
=virtual_context (current_fndecl
, t
, vbase
)) != vbase
)
2558 /* This may in fact need a runtime fixup. */
2559 tree idx
= build_int_2 (n
, 0);
2560 tree vtbl
= BINFO_VTABLE (binfo
);
2561 tree nvtbl
= lookup_name (DECL_NAME (vtbl
), 0);
2562 tree aref
, ref
, naref
;
2563 tree old_delta
, new_delta
;
2566 if (nvtbl
== NULL_TREE
2567 || nvtbl
== IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl
)))
2569 /* Dup it if it isn't in local scope yet. */
2571 (VAR_DECL
, DECL_NAME (vtbl
),
2572 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl
)));
2573 DECL_ALIGN (nvtbl
) = MAX (TYPE_ALIGN (double_type_node
),
2574 DECL_ALIGN (nvtbl
));
2575 TREE_READONLY (nvtbl
) = 0;
2576 DECL_ARTIFICIAL (nvtbl
) = 1;
2577 nvtbl
= pushdecl (nvtbl
);
2579 cp_finish_decl (nvtbl
, init
, NULL_TREE
, 0,
2580 LOOKUP_ONLYCONVERTING
);
2582 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2583 because they wouldn't be useful; everything that wants to
2584 look at the vtable will look at the decl for the normal
2585 vtable. Setting DECL_CONTEXT also screws up
2586 decl_function_context. */
2588 init
= build (MODIFY_EXPR
, TREE_TYPE (nvtbl
),
2590 TREE_SIDE_EFFECTS (init
) = 1;
2591 expand_expr_stmt (init
);
2592 /* Update the vtable pointers as necessary. */
2593 ref
= build_vfield_ref
2594 (build_indirect_ref (addr
, NULL_PTR
),
2595 DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo
))));
2597 (build_modify_expr (ref
, NOP_EXPR
, nvtbl
));
2599 assemble_external (vtbl
);
2600 aref
= build_array_ref (vtbl
, idx
);
2601 naref
= build_array_ref (nvtbl
, idx
);
2602 old_delta
= build_component_ref (aref
, delta_identifier
,
2604 new_delta
= build_component_ref (naref
, delta_identifier
,
2607 /* This is a upcast, so we have to add the offset for the
2609 old_delta
= build_binary_op (PLUS_EXPR
, old_delta
,
2610 TREE_VALUE (delta
), 0);
2613 /* If this is set, we need to subtract out the delta
2614 adjustments for the other virtual base that we
2616 tree vc_delta
= purpose_member (vc
, *vbase_offsets
);
2619 tree vc_addr
= convert_pointer_to_real (vc
, orig_addr
);
2620 vc_delta
= CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc
));
2621 vc_delta
= build (MINUS_EXPR
, ptrdiff_type_node
,
2623 vc_delta
= save_expr (vc_delta
);
2624 *vbase_offsets
= tree_cons (vc
, vc_delta
, *vbase_offsets
);
2627 vc_delta
= TREE_VALUE (vc_delta
);
2629 /* This is a downcast, so we have to subtract the offset
2630 for the virtual base. */
2631 old_delta
= build_binary_op (MINUS_EXPR
, old_delta
, vc_delta
, 0);
2634 TREE_READONLY (new_delta
) = 0;
2635 TREE_TYPE (new_delta
) =
2636 cp_build_qualified_type (TREE_TYPE (new_delta
),
2637 CP_TYPE_QUALS (TREE_TYPE (new_delta
))
2638 & ~TYPE_QUAL_CONST
);
2639 expand_expr_stmt (build_modify_expr (new_delta
, NOP_EXPR
,
2643 virtuals
= TREE_CHAIN (virtuals
);
2647 /* Fixup upcast offsets for all direct vtables. Patterned after
2648 expand_direct_vtbls_init. */
2651 fixup_virtual_upcast_offsets (real_binfo
, binfo
, init_self
, can_elide
, addr
, orig_addr
, type
, vbase
, vbase_offsets
)
2652 tree real_binfo
, binfo
;
2653 int init_self
, can_elide
;
2654 tree addr
, orig_addr
, type
, vbase
, *vbase_offsets
;
2656 tree real_binfos
= BINFO_BASETYPES (real_binfo
);
2657 tree binfos
= BINFO_BASETYPES (binfo
);
2658 int i
, n_baselinks
= real_binfos
? TREE_VEC_LENGTH (real_binfos
) : 0;
2660 for (i
= 0; i
< n_baselinks
; i
++)
2662 tree real_base_binfo
= TREE_VEC_ELT (real_binfos
, i
);
2663 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2664 int is_not_base_vtable
2665 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo
));
2666 if (! TREE_VIA_VIRTUAL (real_base_binfo
))
2667 fixup_virtual_upcast_offsets (real_base_binfo
, base_binfo
,
2668 is_not_base_vtable
, can_elide
, addr
,
2669 orig_addr
, type
, vbase
, vbase_offsets
);
2672 /* Before turning this on, make sure it is correct. */
2673 if (can_elide
&& ! BINFO_MODIFIED (binfo
))
2676 /* Should we use something besides CLASSTYPE_VFIELDS? */
2677 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo
)))
2679 tree new_addr
= convert_pointer_to_real (binfo
, addr
);
2680 expand_upcast_fixups (real_binfo
, new_addr
, orig_addr
, vbase
, addr
,
2681 type
, vbase_offsets
);
2685 /* Build a COMPOUND_EXPR which when expanded will generate the code
2686 needed to initialize all the virtual function table slots of all
2687 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2688 the virtual baseclasses to use; TYPE is the type of the object to
2689 which the initialization applies. TRUE_EXP is the true object we
2690 are initializing, and DECL_PTR is the pointer to the sub-object we
2693 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
2694 object was laid out by a top-level constructor and the computed
2695 offsets are valid to store vtables. When zero, we must store new
2696 vtables through virtual baseclass pointers.
2698 We setup and use the globals: vbase_decl_ptr, vbase_types
2702 expand_indirect_vtbls_init (binfo
, true_exp
, decl_ptr
)
2704 tree true_exp
, decl_ptr
;
2706 tree type
= BINFO_TYPE (binfo
);
2708 /* This function executes during the finish_function() segment,
2709 AFTER the auto variables and temporary stack space has been marked
2710 unused...If space is needed for the virtual function tables,
2711 some of them might fit within what the compiler now thinks
2712 are available stack slots... These values are actually initialized at
2713 the beginnning of the function, so when the automatics use their space,
2714 they will overwrite the values that are placed here. Marking all
2715 temporary space as unavailable prevents this from happening. */
2717 mark_all_temps_used();
2719 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
2721 rtx fixup_insns
= NULL_RTX
;
2722 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
2723 vbase_types
= vbases
;
2724 vbase_decl_ptr
= true_exp
? build_unary_op (ADDR_EXPR
, true_exp
, 0) : decl_ptr
;
2726 dfs_walk (binfo
, dfs_find_vbases
, unmarked_new_vtablep
);
2728 /* Initialized with vtables of type TYPE. */
2729 for (; vbases
; vbases
= TREE_CHAIN (vbases
))
2733 addr
= convert_pointer_to_vbase (TREE_TYPE (vbases
), vbase_decl_ptr
);
2735 /* Do all vtables from this virtual base. */
2736 /* This assumes that virtual bases can never serve as parent
2737 binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */
2738 expand_direct_vtbls_init (vbases
, TYPE_BINFO (BINFO_TYPE (vbases
)),
2741 /* Now we adjust the offsets for virtual functions that
2742 cross virtual boundaries on an implicit upcast on vf call
2743 so that the layout of the most complete type is used,
2744 instead of assuming the layout of the virtual bases from
2745 our current type. */
2747 if (flag_vtable_thunks
)
2749 /* We don't have dynamic thunks yet!
2750 So for now, just fail silently. */
2754 tree vbase_offsets
= NULL_TREE
;
2755 push_to_sequence (fixup_insns
);
2756 fixup_virtual_upcast_offsets (vbases
,
2757 TYPE_BINFO (BINFO_TYPE (vbases
)),
2758 1, 0, addr
, vbase_decl_ptr
,
2759 type
, vbases
, &vbase_offsets
);
2760 fixup_insns
= get_insns ();
2767 extern tree in_charge_identifier
;
2768 tree in_charge_node
= lookup_name (in_charge_identifier
, 0);
2769 if (! in_charge_node
)
2771 warning ("recoverable internal compiler error, nobody's in charge!");
2772 in_charge_node
= integer_zero_node
;
2774 in_charge_node
= build_binary_op (EQ_EXPR
, in_charge_node
, integer_zero_node
, 1);
2775 expand_start_cond (in_charge_node
, 0);
2776 emit_insns (fixup_insns
);
2780 dfs_walk (binfo
, dfs_clear_vbase_slots
, marked_new_vtablep
);
2784 /* get virtual base class types.
2785 This adds type to the vbase_types list in reverse dfs order.
2786 Ordering is very important, so don't change it. */
2789 dfs_get_vbase_types (binfo
)
2792 if (TREE_VIA_VIRTUAL (binfo
) && ! BINFO_VBASE_MARKED (binfo
))
2794 tree new_vbase
= make_binfo (integer_zero_node
, binfo
,
2795 BINFO_VTABLE (binfo
),
2796 BINFO_VIRTUALS (binfo
));
2797 TREE_CHAIN (new_vbase
) = vbase_types
;
2798 TREE_VIA_VIRTUAL (new_vbase
) = 1;
2799 vbase_types
= new_vbase
;
2800 SET_BINFO_VBASE_MARKED (binfo
);
2802 SET_BINFO_MARKED (binfo
);
2805 /* get a list of virtual base classes in dfs order. */
2808 get_vbase_types (type
)
2814 binfo
= TYPE_BINFO (type
);
2815 vbase_types
= NULL_TREE
;
2816 dfs_walk (binfo
, dfs_get_vbase_types
, unmarkedp
);
2817 dfs_walk (binfo
, dfs_unmark
, markedp
);
2818 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2819 reverse it so that we get normal dfs ordering. */
2820 vbase_types
= nreverse (vbase_types
);
2822 /* unmark marked vbases */
2823 for (vbases
= vbase_types
; vbases
; vbases
= TREE_CHAIN (vbases
))
2824 CLEAR_BINFO_VBASE_MARKED (vbases
);
2829 /* If we want debug info for a type TYPE, make sure all its base types
2830 are also marked as being potentially interesting. This avoids
2831 the problem of not writing any debug info for intermediate basetypes
2832 that have abstract virtual functions. Also mark member types. */
2835 note_debug_info_needed (type
)
2840 if (current_template_parms
)
2843 if (TYPE_BEING_DEFINED (type
))
2844 /* We can't go looking for the base types and fields just yet. */
2847 /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2848 does not support name references between translation units. Well, we
2849 could, but that would mean putting global labels in the debug output
2850 before each exported type and each of its functions and static data
2852 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== DWARF2_DEBUG
)
2855 dfs_walk (TYPE_BINFO (type
), dfs_debug_mark
, dfs_debug_unmarkedp
);
2856 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2859 if (TREE_CODE (field
) == FIELD_DECL
2860 && IS_AGGR_TYPE (ttype
= target_type (TREE_TYPE (field
)))
2861 && dfs_debug_unmarkedp (TYPE_BINFO (ttype
)))
2862 note_debug_info_needed (ttype
);
2866 /* Subroutines of push_class_decls (). */
2868 /* Add in a decl to the envelope. */
2870 envelope_add_decl (type
, decl
, values
)
2871 tree type
, decl
, *values
;
2874 tree name
= DECL_NAME (decl
);
2877 /* Yet Another Implicit Typename Kludge: Since we don't tsubst
2878 the members for partial instantiations, DECL_CONTEXT (decl) is wrong.
2879 But pretend it's right for this function. */
2880 if (processing_template_decl
)
2881 type
= DECL_REAL_CONTEXT (decl
);
2883 /* virtual base names are always unique. */
2884 if (VBASE_NAME_P (name
))
2885 *values
= NULL_TREE
;
2887 /* Possible ambiguity. If its defining type(s)
2888 is (are all) derived from us, no problem. */
2889 else if (*values
&& TREE_CODE (*values
) != TREE_LIST
)
2891 tree value
= *values
;
2892 /* Only complain if we shadow something we can access. */
2893 if (warn_shadow
&& TREE_CODE (decl
) == FUNCTION_DECL
2894 && ((DECL_LANG_SPECIFIC (*values
)
2895 && DECL_CLASS_CONTEXT (value
) == current_class_type
)
2896 || ! TREE_PRIVATE (value
)))
2897 /* Should figure out access control more accurately. */
2899 cp_warning_at ("member `%#D' is shadowed", value
);
2900 cp_warning_at ("by member function `%#D'", decl
);
2901 warning ("in this context");
2904 context
= DECL_REAL_CONTEXT (value
);
2906 if (context
== type
)
2908 if (TREE_CODE (value
) == TYPE_DECL
2909 && DECL_ARTIFICIAL (value
))
2910 *values
= NULL_TREE
;
2914 else if (type
== current_class_type
2915 || DERIVED_FROM_P (context
, type
))
2917 /* Don't add in *values to list */
2918 *values
= NULL_TREE
;
2921 *values
= build_tree_list (NULL_TREE
, value
);
2924 for (tmp
= values
; *tmp
;)
2926 tree value
= TREE_VALUE (*tmp
);
2927 my_friendly_assert (TREE_CODE (value
) != TREE_LIST
, 999);
2928 context
= (TREE_CODE (value
) == FUNCTION_DECL
2929 && DECL_VIRTUAL_P (value
))
2930 ? DECL_CLASS_CONTEXT (value
)
2931 : DECL_CONTEXT (value
);
2933 if (type
== current_class_type
2934 || DERIVED_FROM_P (context
, type
))
2936 /* remove *tmp from list */
2937 *tmp
= TREE_CHAIN (*tmp
);
2940 tmp
= &TREE_CHAIN (*tmp
);
2945 /* Put the new contents in our envelope. */
2946 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2948 *values
= tree_cons (name
, decl
, *values
);
2949 TREE_NONLOCAL_FLAG (*values
) = 1;
2950 TREE_TYPE (*values
) = unknown_type_node
;
2956 *values
= tree_cons (NULL_TREE
, decl
, *values
);
2957 /* Mark this as a potentially ambiguous member. */
2958 /* Leaving TREE_TYPE blank is intentional.
2959 We cannot use `error_mark_node' (lookup_name)
2960 or `unknown_type_node' (all member functions use this). */
2961 TREE_NONLOCAL_FLAG (*values
) = 1;
2969 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2970 because it (or one of the intermediate bases) depends on template parms. */
2973 dependent_base_p (binfo
)
2976 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2978 if (TREE_TYPE (binfo
) == current_class_type
)
2980 if (uses_template_parms (TREE_TYPE (binfo
)))
2986 /* Add the instance variables which this class contributed to the
2987 current class binding contour. When a redefinition occurs, if the
2988 redefinition is strictly within a single inheritance path, we just
2989 overwrite the old declaration with the new. If the fields are not
2990 within a single inheritance path, we must cons them.
2992 In order to know what decls are new (stemming from the current
2993 invocation of push_class_decls) we enclose them in an "envelope",
2994 which is a TREE_LIST node where the TREE_PURPOSE slot contains the
2995 new decl (or possibly a list of competing ones), the TREE_VALUE slot
2996 points to the old value and the TREE_CHAIN slot chains together all
2997 envelopes which needs to be "opened" in push_class_decls. Opening an
2998 envelope means: push the old value onto the class_shadowed list,
2999 install the new one and if it's a TYPE_DECL do the same to the
3000 IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that
3001 the TREE_PURPOSE slot is non-null, and that it is not an identifier.
3002 Because if it is, it could be a set of overloaded methods from an
3006 dfs_pushdecls (binfo
)
3009 tree type
= BINFO_TYPE (binfo
);
3014 /* Only record types if we're a template base. */
3015 if (processing_template_decl
&& type
!= current_class_type
3016 && dependent_base_p (binfo
))
3019 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3021 if (dummy
&& TREE_CODE (fields
) != TYPE_DECL
)
3024 /* Unmark so that if we are in a constructor, and then find that
3025 this field was initialized by a base initializer,
3026 we can emit an error message. */
3027 if (TREE_CODE (fields
) == FIELD_DECL
)
3028 TREE_USED (fields
) = 0;
3030 /* Recurse into anonymous unions. */
3031 if (DECL_NAME (fields
) == NULL_TREE
3032 && TREE_CODE (TREE_TYPE (fields
)) == UNION_TYPE
)
3034 dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields
)));
3038 if (DECL_NAME (fields
))
3040 tree name
= DECL_NAME (fields
);
3041 tree class_value
= IDENTIFIER_CLASS_VALUE (name
);
3043 /* If the class value is not an envelope of the kind described in
3044 the comment above, we create a new envelope. */
3045 maybe_push_cache_obstack ();
3046 if (class_value
== NULL_TREE
|| TREE_CODE (class_value
) != TREE_LIST
3047 || TREE_PURPOSE (class_value
) == NULL_TREE
3048 || TREE_CODE (TREE_PURPOSE (class_value
)) == IDENTIFIER_NODE
)
3050 /* See comment above for a description of envelopes. */
3051 closed_envelopes
= tree_cons (NULL_TREE
, class_value
,
3053 IDENTIFIER_CLASS_VALUE (name
) = closed_envelopes
;
3054 class_value
= IDENTIFIER_CLASS_VALUE (name
);
3057 envelope_add_decl (type
, fields
, &TREE_PURPOSE (class_value
));
3062 method_vec
= CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
3063 if (method_vec
&& ! dummy
)
3068 /* Farm out constructors and destructors. */
3069 end
= TREE_VEC_END (method_vec
);
3071 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
3072 *methods
&& methods
!= end
;
3075 /* This will cause lookup_name to return a pointer
3076 to the tree_list of possible methods of this name. */
3081 name
= DECL_NAME (OVL_CURRENT (*methods
));
3082 class_value
= IDENTIFIER_CLASS_VALUE (name
);
3084 maybe_push_cache_obstack ();
3086 /* If the class value is not an envelope of the kind described in
3087 the comment above, we create a new envelope. */
3088 if (class_value
== NULL_TREE
|| TREE_CODE (class_value
) != TREE_LIST
3089 || TREE_PURPOSE (class_value
) == NULL_TREE
3090 || TREE_CODE (TREE_PURPOSE (class_value
)) == IDENTIFIER_NODE
)
3092 /* See comment above for a description of envelopes. */
3093 closed_envelopes
= tree_cons (NULL_TREE
, class_value
,
3095 IDENTIFIER_CLASS_VALUE (name
) = closed_envelopes
;
3096 class_value
= IDENTIFIER_CLASS_VALUE (name
);
3099 /* Here we try to rule out possible ambiguities.
3100 If we can't do that, keep a TREE_LIST with possibly ambiguous
3102 /* Arbitrarily choose the first function in the list. This is OK
3103 because this is only used for initial lookup; anything that
3104 actually uses the function will look it up again. */
3105 envelope_add_decl (type
, OVL_CURRENT (*methods
),
3106 &TREE_PURPOSE (class_value
));
3111 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3112 DERIVED_FROM_P, which calls get_base_distance. */
3113 SET_BINFO_PUSHDECLS_MARKED (binfo
);
3116 /* Consolidate unique (by name) member functions. */
3119 dfs_compress_decls (binfo
)
3122 tree type
= BINFO_TYPE (binfo
);
3124 = CLASS_TYPE_P (type
) ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
;
3126 if (processing_template_decl
&& type
!= current_class_type
3127 && dependent_base_p (binfo
))
3128 /* We only record types if we're a template base. */;
3129 else if (method_vec
!= 0)
3131 /* Farm out constructors and destructors. */
3133 tree
*end
= TREE_VEC_END (method_vec
);
3135 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
3136 methods
!= end
&& *methods
; methods
++)
3138 /* This is known to be an envelope of the kind described before
3141 IDENTIFIER_CLASS_VALUE (DECL_NAME (OVL_CURRENT (*methods
)));
3142 tree tmp
= TREE_PURPOSE (class_value
);
3144 /* This was replaced in scope by somebody else. Just leave it
3146 if (TREE_CODE (tmp
) != TREE_LIST
)
3149 if (TREE_CHAIN (tmp
) == NULL_TREE
3151 && OVL_NEXT (TREE_VALUE (tmp
)) == NULL_TREE
)
3153 TREE_PURPOSE (class_value
) = TREE_VALUE (tmp
);
3157 CLEAR_BINFO_PUSHDECLS_MARKED (binfo
);
3160 /* When entering the scope of a class, we cache all of the
3161 fields that that class provides within its inheritance
3162 lattice. Where ambiguities result, we mark them
3163 with `error_mark_node' so that if they are encountered
3164 without explicit qualification, we can emit an error
3168 push_class_decls (type
)
3171 struct obstack
*ambient_obstack
= current_obstack
;
3172 search_stack
= push_search_level (search_stack
, &search_obstack
);
3174 /* Build up all the relevant bindings and such on the cache
3175 obstack. That way no memory is wasted when we throw away the
3177 maybe_push_cache_obstack ();
3179 /* Push class fields into CLASS_VALUE scope, and mark. */
3180 dfs_walk (TYPE_BINFO (type
), dfs_pushdecls
, unmarked_pushdecls_p
);
3182 /* Compress fields which have only a single entry
3183 by a given name, and unmark. */
3184 dfs_walk (TYPE_BINFO (type
), dfs_compress_decls
, marked_pushdecls_p
);
3186 /* Open up all the closed envelopes and push the contained decls into
3188 while (closed_envelopes
)
3190 tree
new = TREE_PURPOSE (closed_envelopes
);
3193 /* This is messy because the class value may be a *_DECL, or a
3194 TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
3195 *_DECLs. The name is stored at different places in these three
3197 if (TREE_CODE (new) == TREE_LIST
)
3199 if (TREE_PURPOSE (new) != NULL_TREE
)
3200 id
= TREE_PURPOSE (new);
3203 tree node
= TREE_VALUE (new);
3205 if (TREE_CODE (node
) == TYPE_DECL
3206 && DECL_ARTIFICIAL (node
)
3207 && IS_AGGR_TYPE (TREE_TYPE (node
))
3208 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (node
)))
3210 tree t
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (node
));
3213 for (; n
; n
= TREE_CHAIN (n
))
3215 tree d
= TREE_VALUE (n
);
3216 if (TREE_CODE (d
) == TYPE_DECL
3217 && DECL_ARTIFICIAL (node
)
3218 && IS_AGGR_TYPE (TREE_TYPE (d
))
3219 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d
))
3220 && CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d
)) == t
)
3229 else while (TREE_CODE (node
) == TREE_LIST
)
3230 node
= TREE_VALUE (node
);
3231 id
= DECL_NAME (node
);
3235 id
= DECL_NAME (new);
3237 /* Install the original class value in order to make
3238 pushdecl_class_level work correctly. */
3239 IDENTIFIER_CLASS_VALUE (id
) = TREE_VALUE (closed_envelopes
);
3240 if (TREE_CODE (new) == TREE_LIST
)
3241 push_class_level_binding (id
, new);
3243 pushdecl_class_level (new);
3244 closed_envelopes
= TREE_CHAIN (closed_envelopes
);
3247 /* Undo the call to maybe_push_cache_obstack above. */
3250 current_obstack
= ambient_obstack
;
3253 /* Here's a subroutine we need because C lacks lambdas. */
3256 dfs_unuse_fields (binfo
)
3259 tree type
= TREE_TYPE (binfo
);
3262 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3264 if (TREE_CODE (fields
) != FIELD_DECL
)
3267 TREE_USED (fields
) = 0;
3268 if (DECL_NAME (fields
) == NULL_TREE
3269 && TREE_CODE (TREE_TYPE (fields
)) == UNION_TYPE
)
3270 unuse_fields (TREE_TYPE (fields
));
3278 dfs_walk (TYPE_BINFO (type
), dfs_unuse_fields
, unmarkedp
);
3284 /* We haven't pushed a search level when dealing with cached classes,
3285 so we'd better not try to pop it. */
3287 search_stack
= pop_search_level (search_stack
);
3291 print_search_statistics ()
3293 #ifdef GATHER_STATISTICS
3294 fprintf (stderr
, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3295 n_fields_searched
, n_calls_lookup_field
, n_calls_lookup_field_1
);
3296 fprintf (stderr
, "%d fnfields searched in %d calls to lookup_fnfields\n",
3297 n_outer_fields_searched
, n_calls_lookup_fnfields
);
3298 fprintf (stderr
, "%d calls to get_base_type\n", n_calls_get_base_type
);
3299 #else /* GATHER_STATISTICS */
3300 fprintf (stderr
, "no search statistics\n");
3301 #endif /* GATHER_STATISTICS */
3305 init_search_processing ()
3307 gcc_obstack_init (&search_obstack
);
3308 _vptr_name
= get_identifier ("_vptr");
3312 reinit_search_statistics ()
3314 #ifdef GATHER_STATISTICS
3315 n_fields_searched
= 0;
3316 n_calls_lookup_field
= 0, n_calls_lookup_field_1
= 0;
3317 n_calls_lookup_fnfields
= 0, n_calls_lookup_fnfields_1
= 0;
3318 n_calls_get_base_type
= 0;
3319 n_outer_fields_searched
= 0;
3320 n_contexts_saved
= 0;
3321 #endif /* GATHER_STATISTICS */
3324 #define scratch_tree_cons expr_tree_cons
3327 add_conversions (binfo
, data
)
3332 tree method_vec
= CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo
));
3333 tree
*conversions
= (tree
*) data
;
3335 for (i
= 2; i
< TREE_VEC_LENGTH (method_vec
); ++i
)
3337 tree tmp
= TREE_VEC_ELT (method_vec
, i
);
3340 if (!tmp
|| ! DECL_CONV_FN_P (OVL_CURRENT (tmp
)))
3343 name
= DECL_NAME (OVL_CURRENT (tmp
));
3345 /* Make sure we don't already have this conversion. */
3346 if (! IDENTIFIER_MARKED (name
))
3348 *conversions
= scratch_tree_cons (binfo
, tmp
, *conversions
);
3349 IDENTIFIER_MARKED (name
) = 1;
3356 lookup_conversions (type
)
3360 tree conversions
= NULL_TREE
;
3362 if (TYPE_SIZE (type
))
3363 breadth_first_search (TYPE_BINFO (type
), add_conversions
,
3364 0, 0, &conversions
);
3366 for (t
= conversions
; t
; t
= TREE_CHAIN (t
))
3367 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t
)))) = 0;
3372 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3373 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3375 static tree compare_type
;
3376 static int found_overlap
;
3378 dfs_check_overlap (empty_binfo
)
3382 for (binfo
= TYPE_BINFO (compare_type
); ; binfo
= BINFO_BASETYPE (binfo
, 0))
3384 if (BINFO_TYPE (binfo
) == BINFO_TYPE (empty_binfo
))
3389 else if (BINFO_BASETYPES (binfo
) == NULL_TREE
)
3394 /* Trivial function to stop base traversal when we find something. */
3397 dfs_no_overlap_yet (t
)
3398 tree t ATTRIBUTE_UNUSED
;
3400 return found_overlap
== 0;
3403 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3404 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3407 types_overlap_p (empty_type
, next_type
)
3408 tree empty_type
, next_type
;
3410 if (! IS_AGGR_TYPE (next_type
))
3412 compare_type
= next_type
;
3414 dfs_walk (TYPE_BINFO (empty_type
), dfs_check_overlap
, dfs_no_overlap_yet
);
3415 return found_overlap
;
3418 /* Passed to dfs_search by binfo_for_vtable; determine if bvtable comes
3421 static tree bvtable
;
3423 dfs_bfv_helper (binfo
)
3426 if (BINFO_VTABLE (binfo
) == bvtable
)
3431 /* Given a vtable VARS, determine which binfo it comes from. */
3434 binfo_for_vtable (vars
)
3438 return dfs_search (TYPE_BINFO (DECL_CONTEXT (vars
)), dfs_bfv_helper
,
3439 DECL_CONTEXT (vars
));