1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 89, 92-97, 1998 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 /* Virtual function tables are never private. But we should know that
671 we are looking for this, and not even try to hide it. */
672 if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1)
675 /* Member found immediately within object. */
676 if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE)
678 /* Are we (or an enclosing scope) friends with the class that has
680 if (TYPE_P (context) && is_friend (context, previous_scope))
683 /* If it's private, it's private, you letch. */
684 if (TREE_PRIVATE (field))
687 /* ARM $11.5. Member functions of a derived class can access the
688 non-static protected members of a base class only through a
689 pointer to the derived class, a reference to it, or an object
690 of it. Also any subsequently derived classes also have
692 else if (TREE_PROTECTED (field))
694 if (current_class_type
695 && (static_mem || DECL_CONSTRUCTOR_P (field))
697 && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type))
706 /* must reverse more than one element */
707 basetype_path = reverse_path (basetype_path);
708 types = basetype_path;
710 access = access_default_node;
711 protected_ok = static_mem && current_class_type
712 && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type);
718 tree type = BINFO_TYPE (binfo);
721 /* Friends of a class can see protected members of its bases.
722 Note that classes are their own friends. */
723 if (is_friend (type, previous_scope))
729 member = purpose_member (type, DECL_ACCESS (field));
732 access = TREE_VALUE (member);
736 types = BINFO_INHERITANCE_CHAIN (types);
738 /* If the next type was VIA_PROTECTED, then fields of all remaining
739 classes past that one are *at least* protected. */
742 if (TREE_VIA_PROTECTED (types))
744 else if (! TREE_VIA_PUBLIC (types) && ! private_ok)
746 access = access_private_node;
754 /* No special visibilities apply. Use normal rules. */
756 if (access == access_default_node)
758 if (TYPE_P (context) && is_friend (context, previous_scope))
759 access = access_public_node;
760 else if (TREE_PRIVATE (field))
761 access = access_private_node;
762 else if (TREE_PROTECTED (field))
763 access = access_protected_node;
765 access = access_public_node;
768 if (access == access_public_node && via_protected)
769 access = access_protected_node;
771 if (access == access_protected_node && protected_ok)
772 access = access_public_node;
775 if (access == access_public_node)
776 DECL_PUBLIC (field) = 1;
777 else if (access == access_protected_node)
778 DECL_PROTECTED (field) = 1;
779 else if (access == access_private_node)
780 DECL_PRIVATE (field) = 1;
781 else my_friendly_abort (96);
786 /* Routine to see if the sub-object denoted by the binfo PARENT can be
787 found as a base class and sub-object of the object denoted by
788 BINFO. This routine relies upon binfos not being shared, except
789 for binfos for virtual bases. */
792 is_subobject_of_p (parent, binfo)
795 tree binfos = BINFO_BASETYPES (binfo);
796 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
801 /* Process and/or queue base types. */
802 for (i = 0; i < n_baselinks; i++)
804 tree base_binfo = TREE_VEC_ELT (binfos, i);
805 if (TREE_VIA_VIRTUAL (base_binfo))
806 base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
807 if (is_subobject_of_p (parent, base_binfo))
813 /* See if a one FIELD_DECL hides another. This routine is meant to
814 correspond to ANSI working paper Sept 17, 1992 10p4. The two
815 binfos given are the binfos corresponding to the particular places
816 the FIELD_DECLs are found. This routine relies upon binfos not
817 being shared, except for virtual bases. */
820 hides (hider_binfo, hidee_binfo)
821 tree hider_binfo, hidee_binfo;
823 /* hider hides hidee, if hider has hidee as a base class and
824 the instance of hidee is a sub-object of hider. The first
825 part is always true is the second part is true.
827 When hider and hidee are the same (two ways to get to the exact
828 same member) we consider either one as hiding the other. */
829 return is_subobject_of_p (hidee_binfo, hider_binfo);
832 /* Very similar to lookup_fnfields_1 but it ensures that at least one
833 function was declared inside the class given by TYPE. It really should
834 only return functions that match the given TYPE. */
837 lookup_fnfields_here (type, name)
840 int idx = lookup_fnfields_1 (type, name);
843 /* ctors and dtors are always only in the right class. */
846 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
849 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls)))
850 == TYPE_MAIN_VARIANT (type))
852 fndecls = OVL_CHAIN (fndecls);
857 struct lookup_field_info {
858 /* The name of the field for which we're looking. */
860 /* If non-NULL, the current result of the lookup. */
862 /* The path to RVAL. */
864 /* If non-NULL, a list of the possible candidates. */
866 /* The access computed for RVAL. */
868 /* If non-zero, we must check access. */
870 /* If non-zero, we are looking for types, not data members. */
872 /* If something went wrong, a message indicating what. */
876 /* Returns non-zero if BINFO is not hidden by the value found by the
877 lookup so far. If BINFO is hidden, then there's no need to look in
878 it. DATA is really a struct lookup_field_info. Called from
879 lookup_field via breadth_first_search. */
882 lookup_field_queue_p (binfo, data)
886 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
888 return !(lfi->rval_binfo && hides (lfi->rval_binfo, binfo));
891 /* DATA is really a struct lookup_field_info. Look for a field with
892 the name indicated there in BINFO. If this function returns a
893 non-NULL value it is the result of the lookup. Called from
894 lookup_field via breadth_first_search. */
897 lookup_field_r (binfo, data)
901 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
902 tree type = BINFO_TYPE (binfo);
906 /* See if the field is present in TYPE. */
907 nval = lookup_field_1 (type, lfi->name);
909 idx = lookup_fnfields_here (type, lfi->name);
911 /* If the data member wasn't present, then there's nothing further
912 to do for this type. */
913 if (!nval && idx < 0)
916 /* If the lookup already found a match, and the new value doesn't
917 hide the old one, we might have an ambiguity. */
918 if (lfi->rval_binfo && !hides (binfo, lfi->rval_binfo))
920 if (nval && nval == lfi->rval && SHARED_MEMBER_P (nval))
921 /* The two things are really the same. */
923 else if (hides (lfi->rval_binfo, binfo))
924 /* The previous value hides the new one. */
928 /* We have a real ambiguity. We keep a chain of all the
930 if (!lfi->ambiguous && lfi->rval)
931 /* This is the first time we noticed an ambiguity. Add
932 what we previously thought was a reasonable candidate
934 lfi->ambiguous = scratch_tree_cons (NULL_TREE, lfi->rval,
936 /* If NVAL is NULL here, that means that we found a
937 function, not a data member. Pick a representative
938 function, from the overload set, for use in error
941 nval = OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC
944 /* Add the new value. */
945 lfi->ambiguous = scratch_tree_cons (NULL_TREE, nval,
947 lfi->errstr = "request for member `%D' is ambiguous";
952 /* The new lookup is the best we've got so far. Verify that
953 it's the kind of thing we're looking for. */
956 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL)
958 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
960 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
962 else if (!lfi->want_type && TREE_CODE (nval) == TYPE_DECL
963 && lookup_fnfields_here (type, lfi->name) >= 0)
964 /* The type declaration is actually hidden by the
965 function declaration. */
971 /* The lookup found a data member. */
974 lfi->access = compute_access (binfo, nval);
975 /* If the thing we're looking for is a virtual base class,
976 then we know we've got what we want at this point;
977 there's no way to get an ambiguity. */
978 if (VBASE_NAME_P (lfi->name))
982 /* The lookup found a function member. This lookup hides
983 whatever was there before, so even though we're not
984 interested in this value we keep track of the way in
985 which we found the function. Subsequent lookups
986 shouldn't find a data member if it is hidden by this
988 lfi->rval = NULL_TREE;
990 lfi->rval_binfo = binfo;
996 /* Check to see if the result of the field lookup (as indicated by
997 DATA, which is really a struct field_info) has any access other
998 than that we previously computed. SEARCH_HEAD and SEARCH_TAIL
999 bound the path taken to find the result. Called from lookup_field
1000 via breadth_first_search. */
1003 lookup_field_post (search_head, search_tail, data)
1008 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1009 tree rval = lfi->rval;
1010 tree own_access = access_default_node;
1013 /* If we didn't find anything, or we found ambiguous function
1014 declarations, but no data members, just return. */
1021 /* If we've already hit a snag, we're done. */
1025 /* Check accessibility. */
1028 /* If is possible for one of the derived types on the path to
1029 have defined special access for this field. Look for such
1030 declarations and report an error if a conflict is found. */
1031 if (DECL_LANG_SPECIFIC (rval) && DECL_ACCESS (rval))
1032 for (tp = search_head; tp < search_tail; ++tp)
1034 tree new_v = NULL_TREE;
1036 if (lfi->access != access_default_node)
1037 new_v = compute_access (*tp, lfi->rval);
1038 if (lfi->access != access_default_node && new_v != lfi->access)
1040 lfi->errstr = "conflicting access to member `%D'";
1041 lfi->access = access_default_node;
1048 /* Check to see that access to the member is allowed. */
1049 if (own_access == access_private_node)
1050 lfi->errstr = "member `%D' declared private";
1051 else if (own_access == access_protected_node)
1052 lfi->errstr = "member `%D' declared protected";
1053 else if (lfi->access == access_private_node)
1054 lfi->errstr = TREE_PRIVATE (lfi->rval)
1055 ? "member `%D' is private"
1056 : "member `%D' is from private base class";
1057 else if (lfi->access== access_protected_node)
1058 lfi->errstr = TREE_PROTECTED (rval)
1059 ? "member `%D' is protected"
1060 : "member `%D' is from protected base class";
1063 /* The implicit typename extension allows us to find type
1064 declarations in dependent base clases. It also handles
1065 out-of-class definitions where the enclosing class is a
1066 template. For example:
1068 template <class T> struct S { struct I { void f(); }; };
1069 template <class T> void S<T>::I::f() {}
1071 will come through here to handle `S<T>::I'. The bottom line is
1072 that while searching for the field, we will have happily
1073 descended into dependent base classes, and we must now figure out
1074 what to do about it. */
1076 /* If we're not in a template, or the search terminated in the
1077 current class, then there's no problem. */
1078 if (!processing_template_decl
1079 || currently_open_class (BINFO_TYPE (lfi->rval_binfo)))
1082 /* We need to return a member template class so we can define partial
1083 specializations. Is there a better way? */
1084 if (DECL_CLASS_TEMPLATE_P (rval))
1087 /* Walk the path to the base in which the search finally suceeded,
1088 checking for dependent bases along the way. */
1089 for (tp = (currently_open_class (BINFO_TYPE (*search_head)))
1090 ? search_head + 1 : search_head;
1094 if (!uses_template_parms (BINFO_TYPE (*tp)))
1097 if (TREE_CODE (rval) != TYPE_DECL)
1099 /* The thing we're looking for isn't a type, so the implicit
1100 typename extension doesn't apply, so we just pretend we
1101 didn't find anything. */
1102 lfi->rval = NULL_TREE;
1106 /* We've passed a dependent base on our way to finding the
1107 type. So, create an implicit typename type. The appropriate
1108 context for the typename is *TP. But, there's a small catch;
1109 the base classes for a partial instantiation are not correct,
1110 because we don't tsubst into them when we do the partial
1111 instantiation. So, we just use the context of the current
1113 lfi->rval = TYPE_STUB_DECL (build_typename_type
1114 (BINFO_TYPE (*search_head),
1115 lfi->name, lfi->name,
1121 /* Look for a field named NAME in an inheritance lattice dominated by
1122 XBASETYPE. PROTECT is zero if we can avoid computing access
1123 information, otherwise it is 1. WANT_TYPE is 1 when we should only
1124 return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
1126 It was not clear what should happen if WANT_TYPE is set, and an
1127 ambiguity is found. At least one use (lookup_name) to not see
1131 lookup_field (xbasetype, name, protect, want_type)
1132 register tree xbasetype, name;
1133 int protect, want_type;
1135 tree rval, rval_binfo = NULL_TREE;
1136 tree type = NULL_TREE, basetype_path = NULL_TREE;
1137 struct lookup_field_info lfi;
1139 /* rval_binfo is the binfo associated with the found member, note,
1140 this can be set with useful information, even when rval is not
1141 set, because it must deal with ALL members, not just non-function
1142 members. It is used for ambiguity checking and the hidden
1143 checks. Whereas rval is only set if a proper (not hidden)
1144 non-function member is found. */
1146 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1147 hiding checks, as virtual base classes may not be shared. The strategy
1148 is we always go into the binfo hierarchy owned by TYPE_BINFO of
1149 virtual base classes, as we cross virtual base class lines. This way
1150 we know that binfo of a virtual base class will always == itself when
1151 found along any line. (mrs) */
1155 bzero (&lfi, sizeof (lfi));
1158 /* We cannot search for constructor/destructor names like this. */
1159 /* This can't go here, but where should it go? */
1160 /* If we are looking for a constructor in a templated type, use the
1161 unspecialized name, as that is how we store it. */
1162 if (IDENTIFIER_TEMPLATE (name))
1163 name = constructor_name (name);
1166 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1167 && IDENTIFIER_CLASS_VALUE (name))
1169 tree field = IDENTIFIER_CLASS_VALUE (name);
1170 if (TREE_CODE (field) != FUNCTION_DECL
1171 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1175 if (TREE_CODE (xbasetype) == TREE_VEC)
1177 type = BINFO_TYPE (xbasetype);
1178 basetype_path = xbasetype;
1180 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1183 basetype_path = TYPE_BINFO (type);
1184 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1188 my_friendly_abort (97);
1190 complete_type (type);
1192 #ifdef GATHER_STATISTICS
1193 n_calls_lookup_field++;
1194 #endif /* GATHER_STATISTICS */
1197 lfi.protect = protect;
1198 lfi.want_type = want_type;
1199 lfi.access = access_default_node;
1200 breadth_first_search (basetype_path, &lookup_field_r,
1201 &lookup_field_queue_p, &lookup_field_post, &lfi);
1203 rval_binfo = lfi.rval_binfo;
1205 type = BINFO_TYPE (rval_binfo);
1206 errstr = lfi.errstr;
1208 /* If we are not interested in ambiguities, don't report them;
1209 just return NULL_TREE. */
1210 if (!protect && lfi.ambiguous)
1213 if (errstr && protect)
1215 cp_error (errstr, name, type);
1217 print_candidates (lfi.ambiguous);
1218 rval = error_mark_node;
1224 /* Try to find NAME inside a nested class. */
1227 lookup_nested_field (name, complain)
1233 tree id = NULL_TREE;
1234 if (TYPE_MAIN_DECL (current_class_type))
1236 /* Climb our way up the nested ladder, seeing if we're trying to
1237 modify a field in an enclosing class. If so, we should only
1238 be able to modify if it's static. */
1239 for (t = TYPE_MAIN_DECL (current_class_type);
1240 t && DECL_CONTEXT (t);
1241 t = TYPE_MAIN_DECL (DECL_CONTEXT (t)))
1243 if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE)
1246 /* N.B.: lookup_field will do the access checking for us */
1247 id = lookup_field (DECL_CONTEXT (t), name, complain, 0);
1248 if (id == error_mark_node)
1254 if (id != NULL_TREE)
1256 if (TREE_CODE (id) == FIELD_DECL
1257 && ! TREE_STATIC (id)
1258 && TREE_TYPE (id) != error_mark_node)
1262 /* At parse time, we don't want to give this error, since
1263 we won't have enough state to make this kind of
1264 decision properly. But there are times (e.g., with
1265 enums in nested classes) when we do need to call
1266 this fn at parse time. So, in those cases, we pass
1267 complain as a 0 and just return a NULL_TREE. */
1268 cp_error ("assignment to non-static member `%D' of enclosing class `%T'",
1269 id, DECL_CONTEXT (t));
1270 /* Mark this for do_identifier(). It would otherwise
1271 claim that the variable was undeclared. */
1272 TREE_TYPE (id) = error_mark_node;
1288 /* TYPE is a class type. Return the index of the fields within
1289 the method vector with name NAME, or -1 is no such field exists. */
1292 lookup_fnfields_1 (type, name)
1295 register tree method_vec
1296 = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE;
1298 if (method_vec != 0)
1300 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1301 register tree *end = TREE_VEC_END (method_vec);
1303 #ifdef GATHER_STATISTICS
1304 n_calls_lookup_fnfields_1++;
1305 #endif /* GATHER_STATISTICS */
1307 /* Constructors are first... */
1308 if (*methods && name == ctor_identifier)
1311 /* and destructors are second. */
1312 if (*++methods && name == dtor_identifier)
1315 while (++methods != end && *methods)
1317 #ifdef GATHER_STATISTICS
1318 n_outer_fields_searched++;
1319 #endif /* GATHER_STATISTICS */
1320 if (DECL_NAME (OVL_CURRENT (*methods)) == name)
1324 /* If we didn't find it, it might have been a template
1325 conversion operator. (Note that we don't look for this case
1326 above so that we will always find specializations first.) */
1327 if ((methods == end || !*methods)
1328 && IDENTIFIER_TYPENAME_P (name))
1330 methods = &TREE_VEC_ELT (method_vec, 0) + 1;
1332 while (++methods != end && *methods)
1334 tree method_name = DECL_NAME (OVL_CURRENT (*methods));
1336 if (!IDENTIFIER_TYPENAME_P (method_name))
1338 /* Since all conversion operators come first, we know
1339 there is no such operator. */
1343 else if (TREE_CODE (OVL_CURRENT (*methods)) == TEMPLATE_DECL)
1348 if (methods != end && *methods)
1349 return methods - &TREE_VEC_ELT (method_vec, 0);
1355 /* Starting from BASETYPE, return a TREE_BASELINK-like object
1356 which gives the following information (in a list):
1358 TREE_TYPE: list of basetypes needed to get to...
1359 TREE_VALUE: list of all functions in a given type
1360 which have name NAME.
1362 No access information is computed by this function,
1363 other then to adorn the list of basetypes with
1366 If there are two ways to find a name (two members), if COMPLAIN is
1367 non-zero, then error_mark_node is returned, and an error message is
1368 printed, otherwise, just an error_mark_node is returned.
1370 As a special case, is COMPLAIN is -1, we don't complain, and we
1371 don't return error_mark_node, but rather the complete list of
1372 virtuals. This is used by get_virtuals_named_this. */
1375 lookup_fnfields (basetype_path, name, complain)
1376 tree basetype_path, name;
1379 int head = 0, tail = 0;
1380 tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE;
1381 tree rval_binfo_h = NULL_TREE, binfo, basetype_chain, binfo_h;
1382 int idx, find_all = 0;
1384 /* rval_binfo is the binfo associated with the found member, note,
1385 this can be set with useful information, even when rval is not
1386 set, because it must deal with ALL members, not just function
1387 members. It is used for ambiguity checking and the hidden
1388 checks. Whereas rval is only set if a proper (not hidden)
1389 function member is found. */
1391 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1392 hiding checks, as virtual base classes may not be shared. The strategy
1393 is we always go into the binfo hierarchy owned by TYPE_BINFO of
1394 virtual base classes, as we cross virtual base class lines. This way
1395 we know that binfo of a virtual base class will always == itself when
1396 found along any line. (mrs) */
1398 /* For now, don't try this. */
1399 int protect = complain;
1406 protect = complain = 0;
1410 /* We cannot search for constructor/destructor names like this. */
1411 /* This can't go here, but where should it go? */
1412 /* If we are looking for a constructor in a templated type, use the
1413 unspecialized name, as that is how we store it. */
1414 if (IDENTIFIER_TEMPLATE (name))
1415 name = constructor_name (name);
1418 binfo = basetype_path;
1420 type = complete_type (BINFO_TYPE (basetype_path));
1422 #ifdef GATHER_STATISTICS
1423 n_calls_lookup_fnfields++;
1424 #endif /* GATHER_STATISTICS */
1426 idx = lookup_fnfields_here (type, name);
1427 if (idx >= 0 || lookup_field_1 (type, name))
1429 rval_binfo = basetype_path;
1430 rval_binfo_h = rval_binfo;
1435 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1436 rvals = scratch_tree_cons (basetype_path, rval, rvals);
1437 if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type))
1438 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx);
1444 if (name == ctor_identifier || name == dtor_identifier)
1446 /* Don't allow lookups of constructors and destructors to go
1447 deeper than the first place we look. */
1451 if (basetype_path == TYPE_BINFO (type))
1453 basetype_chain = CLASSTYPE_BINFO_AS_LIST (type);
1454 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1458 basetype_chain = build_expr_list (NULL_TREE, basetype_path);
1460 /* The ambiguity check relies upon breadth first searching. */
1462 search_stack = push_search_level (search_stack, &search_obstack);
1463 binfo = basetype_path;
1468 tree binfos = BINFO_BASETYPES (binfo);
1469 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1472 /* Process and/or queue base types. */
1473 for (i = 0; i < n_baselinks; i++)
1475 tree base_binfo = TREE_VEC_ELT (binfos, i);
1476 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
1480 SET_BINFO_FIELDS_MARKED (base_binfo);
1481 btypes = scratch_tree_cons (NULL_TREE, base_binfo, basetype_chain);
1482 if (TREE_VIA_VIRTUAL (base_binfo))
1483 btypes = scratch_tree_cons (NULL_TREE,
1484 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1487 btypes = scratch_tree_cons (NULL_TREE,
1488 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1490 obstack_ptr_grow (&search_obstack, btypes);
1492 if (tail >= search_stack->limit)
1493 my_friendly_abort (99);
1497 /* Process head of queue, if one exists. */
1501 basetype_chain = search_stack->first[head++];
1502 binfo_h = TREE_VALUE (basetype_chain);
1503 basetype_chain = TREE_CHAIN (basetype_chain);
1504 basetype_path = TREE_VALUE (basetype_chain);
1505 if (TREE_CHAIN (basetype_chain))
1507 ((BINFO_INHERITANCE_CHAIN (basetype_path)
1508 == TREE_VALUE (TREE_CHAIN (basetype_chain)))
1509 /* We only approximate base info for partial instantiations. */
1510 || current_template_parms,
1513 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path)
1514 == NULL_TREE, 980827);
1516 binfo = basetype_path;
1517 type = BINFO_TYPE (binfo);
1519 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1520 and we do find NAME in TYPE, verify that such a second
1521 sighting is in fact valid. */
1523 idx = lookup_fnfields_here (type, name);
1525 if (idx >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all))
1527 if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h))
1529 /* This is ok, the member found is in rval_binfo, not
1532 else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h))
1534 /* This is ok, the member found is here (binfo), not in
1538 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1539 /* Note, rvals can only be previously set if find_all is
1541 rvals = scratch_tree_cons (basetype_path, rval, rvals);
1542 if (TYPE_BINFO_BASETYPES (type)
1543 && CLASSTYPE_BASELINK_VEC (type))
1544 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx);
1548 /* Undo finding it before, as something else hides it. */
1553 rval_binfo_h = binfo_h;
1557 /* This is ambiguous. */
1558 errstr = "request for method `%D' is ambiguous";
1559 rvals = error_mark_node;
1565 tree *tp = search_stack->first;
1566 tree *search_tail = tp + tail;
1568 while (tp < search_tail)
1570 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1574 search_stack = pop_search_level (search_stack);
1576 if (errstr && protect)
1578 cp_error (errstr, name);
1579 rvals = error_mark_node;
1585 /* Look for a field or function named NAME in an inheritance lattice
1586 dominated by XBASETYPE. PROTECT is zero if we can avoid computing
1587 access information, otherwise it is 1. WANT_TYPE is 1 when we should
1588 only return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE. */
1591 lookup_member (xbasetype, name, protect, want_type)
1592 tree xbasetype, name;
1593 int protect, want_type;
1595 tree ret, basetype_path;
1597 if (TREE_CODE (xbasetype) == TREE_VEC)
1598 basetype_path = xbasetype;
1599 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1601 basetype_path = TYPE_BINFO (xbasetype);
1602 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path)
1603 == NULL_TREE, 980827);
1606 my_friendly_abort (97);
1608 ret = lookup_field (basetype_path, name, protect, want_type);
1609 if (! ret && ! want_type)
1610 ret = lookup_fnfields (basetype_path, name, protect);
1614 /* BREADTH-FIRST SEARCH ROUTINES. */
1616 /* Search a multiple inheritance hierarchy by breadth-first search.
1618 BINFO is an aggregate type, possibly in a multiple-inheritance hierarchy.
1619 TESTFN is a function, which, if true, means that our condition has
1620 been met, and its return value should be returned.
1621 QFN, if non-NULL, is a predicate dictating whether the type should
1623 POSTFN, if non-NULL, is a function to call before returning. It is
1624 passed an array whose first element is the most derived type in the
1625 chain, and whose last element is the least derived type.
1627 All of the functions are also passed the DATA, which they may use
1631 breadth_first_search (binfo, testfn, qfn, postfn, data)
1633 tree (*testfn) PROTO((tree, void *));
1634 int (*qfn) PROTO((tree, void *));
1635 void (*postfn) PROTO((tree *, tree *, void *));
1638 int head = 0, tail = 0;
1639 tree rval = NULL_TREE;
1643 search_stack = push_search_level (search_stack, &search_obstack);
1645 SET_BINFO_MARKED (binfo);
1646 obstack_ptr_grow (&search_obstack, binfo);
1655 /* Pull the next type out of the queue. */
1656 binfo = search_stack->first[head++];
1658 /* If this is the one we're looking for, we're done. */
1659 rval = (*testfn) (binfo, data);
1663 /* Queue up the base types. */
1664 binfos = BINFO_BASETYPES (binfo);
1665 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1666 for (i = 0; i < n_baselinks; i++)
1668 tree base_binfo = TREE_VEC_ELT (binfos, i);
1670 if (TREE_VIA_VIRTUAL (base_binfo))
1671 base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
1673 if (BINFO_MARKED (base_binfo) == 0
1674 && (qfn == 0 || (*qfn) (base_binfo, data)))
1676 SET_BINFO_MARKED (base_binfo);
1677 obstack_ptr_grow (&search_obstack, base_binfo);
1679 if (tail >= search_stack->limit)
1680 my_friendly_abort (100);
1685 tp = search_stack->first;
1686 search_tail = tp + tail;
1689 (*postfn) (tp, search_tail, data);
1691 while (tp < search_tail)
1694 CLEAR_BINFO_MARKED (binfo);
1697 search_stack = pop_search_level (search_stack);
1701 /* Functions to use in breadth first searches. */
1702 typedef tree (*pfi) PROTO((tree));
1704 static tree declarator;
1707 get_virtuals_named_this (binfo)
1712 fields = lookup_fnfields (binfo, declarator, -1);
1713 /* fields cannot be error_mark_node */
1718 /* Get to the function decls, and return the first virtual function
1719 with this name, if there is one. */
1724 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl))
1725 if (DECL_VINDEX (OVL_CURRENT (fndecl)))
1727 fields = next_baselink (fields);
1733 get_virtual_destructor (binfo, data)
1737 tree type = BINFO_TYPE (binfo);
1738 if (TYPE_HAS_DESTRUCTOR (type)
1739 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1)))
1740 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1);
1745 tree_has_any_destructor_p (binfo, data)
1749 tree type = BINFO_TYPE (binfo);
1750 return TYPE_NEEDS_DESTRUCTOR (type);
1753 /* Returns > 0 if a function with type DRETTYPE overriding a function
1754 with type BRETTYPE is covariant, as defined in [class.virtual].
1756 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1757 adjustment), or -1 if pedantically invalid covariance. */
1760 covariant_return_p (brettype, drettype)
1761 tree brettype, drettype;
1765 if (TREE_CODE (brettype) == FUNCTION_DECL
1766 || TREE_CODE (brettype) == THUNK_DECL)
1768 brettype = TREE_TYPE (TREE_TYPE (brettype));
1769 drettype = TREE_TYPE (TREE_TYPE (drettype));
1771 else if (TREE_CODE (brettype) == METHOD_TYPE)
1773 brettype = TREE_TYPE (brettype);
1774 drettype = TREE_TYPE (drettype);
1777 if (same_type_p (brettype, drettype))
1780 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1781 && (TREE_CODE (brettype) == POINTER_TYPE
1782 || TREE_CODE (brettype) == REFERENCE_TYPE)
1783 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
1786 if (! can_convert (brettype, drettype))
1789 brettype = TREE_TYPE (brettype);
1790 drettype = TREE_TYPE (drettype);
1792 /* If not pedantic, allow any standard pointer conversion. */
1793 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
1796 binfo = get_binfo (brettype, drettype, 1);
1798 /* If we get an error_mark_node from get_binfo, it already complained,
1799 so let's just succeed. */
1800 if (binfo == error_mark_node)
1803 if (! BINFO_OFFSET_ZEROP (binfo) || TREE_VIA_VIRTUAL (binfo))
1808 /* Given a class type TYPE, and a function decl FNDECL, look for a
1809 virtual function in TYPE's hierarchy which FNDECL could match as a
1810 virtual function. It doesn't matter which one we find.
1812 DTORP is nonzero if we are looking for a destructor. Destructors
1813 need special treatment because they do not match by name. */
1816 get_matching_virtual (binfo, fndecl, dtorp)
1820 tree tmp = NULL_TREE;
1823 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
1824 /* In [temp.mem] we have:
1826 A specialization of a member function template does not
1827 override a virtual function from a base class. */
1830 /* Breadth first search routines start searching basetypes
1831 of TYPE, so we must perform first ply of search here. */
1834 return breadth_first_search (binfo,
1835 get_virtual_destructor,
1836 tree_has_any_destructor_p, 0, 0);
1840 tree drettype, dtypes, btypes, instptr_type;
1841 tree basetype = DECL_CLASS_CONTEXT (fndecl);
1842 tree baselink, best = NULL_TREE;
1843 tree name = DECL_ASSEMBLER_NAME (fndecl);
1845 declarator = DECL_NAME (fndecl);
1846 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
1849 baselink = get_virtuals_named_this (binfo);
1850 if (baselink == NULL_TREE)
1853 drettype = TREE_TYPE (TREE_TYPE (fndecl));
1854 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1855 if (DECL_STATIC_FUNCTION_P (fndecl))
1856 instptr_type = NULL_TREE;
1858 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
1860 for (; baselink; baselink = next_baselink (baselink))
1863 for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps))
1865 tmp = OVL_CURRENT (tmps);
1866 if (! DECL_VINDEX (tmp))
1869 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
1870 if (instptr_type == NULL_TREE)
1872 if (compparms (TREE_CHAIN (btypes), dtypes))
1873 /* Caller knows to give error in this case. */
1878 if (/* The first parameter is the `this' parameter,
1879 which has POINTER_TYPE, and we can therefore
1880 safely use TYPE_QUALS, rather than
1882 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes)))
1883 == TYPE_QUALS (instptr_type))
1884 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes)))
1886 tree brettype = TREE_TYPE (TREE_TYPE (tmp));
1887 if (same_type_p (brettype, drettype))
1889 else if ((i = covariant_return_p (brettype, drettype)))
1892 sorry ("adjusting pointers for covariant returns");
1894 if (pedantic && i == -1)
1896 cp_pedwarn_at ("invalid covariant return type for `%#D' (must be pointer or reference to class)", fndecl);
1897 cp_pedwarn_at (" overriding `%#D'", tmp);
1900 else if (IS_AGGR_TYPE_2 (brettype, drettype)
1901 && same_or_base_type_p (brettype, drettype))
1903 error ("invalid covariant return type (must use pointer or reference)");
1904 cp_error_at (" overriding `%#D'", tmp);
1905 cp_error_at (" with `%#D'", fndecl);
1907 else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE)
1909 cp_error_at ("conflicting return type specified for virtual function `%#D'", fndecl);
1910 cp_error_at (" overriding definition as `%#D'", tmp);
1911 SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
1914 /* FNDECL overrides this function. We continue to
1915 check all the other functions in order to catch
1916 errors; it might be that in some other baseclass
1917 a virtual function was declared with the same
1918 parameter types, but a different return type. */
1928 /* Return the list of virtual functions which are abstract in type
1929 TYPE that come from non virtual base classes. See
1930 expand_direct_vtbls_init for the style of search we do. */
1933 get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
1936 tree abstract_virtuals;
1938 tree binfos = BINFO_BASETYPES (binfo);
1939 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1941 for (i = 0; i < n_baselinks; i++)
1943 tree base_binfo = TREE_VEC_ELT (binfos, i);
1944 int is_not_base_vtable
1945 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
1946 if (! TREE_VIA_VIRTUAL (base_binfo))
1948 = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
1951 /* Should we use something besides CLASSTYPE_VFIELDS? */
1952 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
1954 tree virtuals = BINFO_VIRTUALS (binfo);
1956 skip_rtti_stuff (&virtuals);
1960 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
1961 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
1962 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
1963 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
1964 virtuals = TREE_CHAIN (virtuals);
1967 return abstract_virtuals;
1970 /* Return the list of virtual functions which are abstract in type TYPE.
1971 This information is cached, and so must be built on a
1972 non-temporary obstack. */
1975 get_abstract_virtuals (type)
1979 tree abstract_virtuals = NULL;
1981 /* First get all from non-virtual bases. */
1983 = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
1985 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
1987 tree virtuals = BINFO_VIRTUALS (vbases);
1989 skip_rtti_stuff (&virtuals);
1993 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
1994 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
1995 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
1996 cp_error ("`%#D' needs a final overrider", base_fndecl);
1997 else if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
1998 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
1999 virtuals = TREE_CHAIN (virtuals);
2002 return nreverse (abstract_virtuals);
2005 /* For the type TYPE, return a list of member functions available from
2006 base classes with name NAME. The TREE_VALUE of the list is a chain of
2007 member functions with name NAME. The TREE_PURPOSE of the list is a
2008 basetype, or a list of base types (in reverse order) which were
2009 traversed to reach the chain of member functions. If we reach a base
2010 type which provides a member function of name NAME, and which has at
2011 most one base type itself, then we can terminate the search. */
2014 get_baselinks (type_as_binfo_list, type, name)
2015 tree type_as_binfo_list;
2018 int head = 0, tail = 0, idx;
2019 tree rval = 0, nval = 0;
2020 tree basetypes = type_as_binfo_list;
2021 tree binfo = TYPE_BINFO (type);
2023 search_stack = push_search_level (search_stack, &search_obstack);
2027 tree binfos = BINFO_BASETYPES (binfo);
2028 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2030 /* Process and/or queue base types. */
2031 for (i = 0; i < n_baselinks; i++)
2033 tree base_binfo = TREE_VEC_ELT (binfos, i);
2036 btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo),
2037 TREE_VIA_VIRTUAL (base_binfo),
2038 TREE_VIA_PROTECTED (base_binfo),
2039 NULL_TREE, base_binfo,
2041 obstack_ptr_grow (&search_obstack, btypes);
2042 search_stack->first = (tree *)obstack_base (&search_obstack);
2047 /* Process head of queue, if one exists. */
2051 basetypes = search_stack->first[head++];
2052 binfo = TREE_VALUE (basetypes);
2053 type = BINFO_TYPE (binfo);
2054 idx = lookup_fnfields_1 (type, name);
2057 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
2058 rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval);
2059 if (TYPE_BINFO_BASETYPES (type) == 0)
2061 else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1)
2063 if (CLASSTYPE_BASELINK_VEC (type))
2064 TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx);
2071 search_stack = pop_search_level (search_stack);
2076 next_baselink (baselink)
2079 tree tmp = TREE_TYPE (baselink);
2080 baselink = TREE_CHAIN (baselink);
2083 /* @@ does not yet add previous base types. */
2084 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2086 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2087 tmp = TREE_CHAIN (tmp);
2092 /* DEPTH-FIRST SEARCH ROUTINES. */
2094 /* This routine converts a pointer to be a pointer of an immediate
2095 base class. The normal convert_pointer_to routine would diagnose
2096 the conversion as ambiguous, under MI code that has the base class
2097 as an ambiguous base class. */
2100 convert_pointer_to_single_level (to_type, expr)
2103 tree binfo_of_derived;
2106 binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)));
2107 last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0);
2108 my_friendly_assert (BINFO_INHERITANCE_CHAIN (last) == binfo_of_derived,
2110 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived) == NULL_TREE,
2112 return build_vbase_path (PLUS_EXPR, build_pointer_type (to_type), expr,
2116 /* The main function which implements depth first search.
2118 This routine has to remember the path it walked up, when
2119 dfs_init_vbase_pointers is the work function, as otherwise there
2120 would be no record. */
2123 dfs_walk (binfo, fn, qfn)
2125 void (*fn) PROTO((tree));
2126 int (*qfn) PROTO((tree));
2128 tree binfos = BINFO_BASETYPES (binfo);
2129 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2131 for (i = 0; i < n_baselinks; i++)
2133 tree base_binfo = TREE_VEC_ELT (binfos, i);
2135 if (qfn == 0 || (*qfn)(base_binfo))
2137 if (TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TYPE_PARM
2138 || TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TEMPLATE_PARM)
2140 else if (fn == dfs_init_vbase_pointers)
2142 /* When traversing an arbitrary MI hierarchy, we need to keep
2143 a record of the path we took to get down to the final base
2144 type, as otherwise there would be no record of it, and just
2145 trying to blindly convert at the bottom would be ambiguous.
2147 The easiest way is to do the conversions one step at a time,
2148 as we know we want the immediate base class at each step.
2150 The only special trick to converting one step at a time,
2151 is that when we hit the last virtual base class, we must
2152 use the SLOT value for it, and not use the normal convert
2153 routine. We use the last virtual base class, as in our
2154 implementation, we have pointers to all virtual base
2155 classes in the base object. */
2157 tree saved_vbase_decl_ptr_intermediate
2158 = vbase_decl_ptr_intermediate;
2160 if (TREE_VIA_VIRTUAL (base_binfo))
2162 /* No need for the conversion here, as we know it is the
2164 vbase_decl_ptr_intermediate
2165 = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo));
2169 vbase_decl_ptr_intermediate
2170 = convert_pointer_to_single_level (BINFO_TYPE (base_binfo),
2171 vbase_decl_ptr_intermediate);
2174 dfs_walk (base_binfo, fn, qfn);
2176 vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate;
2179 dfs_walk (base_binfo, fn, qfn);
2186 /* Like dfs_walk, but only walk until fn returns something, and return
2187 that. We also use the real vbase binfos instead of the placeholders
2188 in the normal binfo hierarchy. START is the most-derived type for this
2189 hierarchy, so that we can find the vbase binfos. */
2192 dfs_search (binfo, fn, start)
2194 tree (*fn) PROTO((tree));
2196 tree binfos = BINFO_BASETYPES (binfo);
2197 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2200 for (i = 0; i < n_baselinks; i++)
2202 tree base_binfo = TREE_VEC_ELT (binfos, i);
2204 if (TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TYPE_PARM
2205 || TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TEMPLATE_PARM)
2209 if (TREE_VIA_VIRTUAL (base_binfo) && start)
2210 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2211 CLASSTYPE_VBASECLASSES (start));
2212 retval = dfs_search (base_binfo, fn, start);
2221 int markedp (binfo) tree binfo;
2222 { return BINFO_MARKED (binfo); }
2223 static int unmarkedp (binfo) tree binfo;
2224 { return BINFO_MARKED (binfo) == 0; }
2227 static int bfs_markedp (binfo, i) tree binfo; int i;
2228 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); }
2229 static int bfs_unmarkedp (binfo, i) tree binfo; int i;
2230 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2231 static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i;
2232 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); }
2233 static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i;
2234 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2235 static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i;
2236 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); }
2237 static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i;
2238 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2241 static int marked_vtable_pathp (binfo) tree binfo;
2242 { return BINFO_VTABLE_PATH_MARKED (binfo); }
2243 static int unmarked_vtable_pathp (binfo) tree binfo;
2244 { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; }
2245 static int marked_new_vtablep (binfo) tree binfo;
2246 { return BINFO_NEW_VTABLE_MARKED (binfo); }
2247 static int unmarked_new_vtablep (binfo) tree binfo;
2248 { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; }
2249 static int marked_pushdecls_p (binfo) tree binfo;
2250 { return BINFO_PUSHDECLS_MARKED (binfo); }
2251 static int unmarked_pushdecls_p (binfo) tree binfo;
2252 { return BINFO_PUSHDECLS_MARKED (binfo) == 0; }
2255 static int dfs_search_slot_nonempty_p (binfo) tree binfo;
2256 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
2259 static int dfs_debug_unmarkedp (binfo) tree binfo;
2260 { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; }
2262 /* The worker functions for `dfs_walk'. These do not need to
2263 test anything (vis a vis marking) if they are paired with
2264 a predicate function (above). */
2268 dfs_mark (binfo) tree binfo;
2269 { SET_BINFO_MARKED (binfo); }
2273 dfs_unmark (binfo) tree binfo;
2274 { CLEAR_BINFO_MARKED (binfo); }
2278 dfs_mark_vtable_path (binfo) tree binfo;
2279 { SET_BINFO_VTABLE_PATH_MARKED (binfo); }
2282 dfs_unmark_vtable_path (binfo) tree binfo;
2283 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
2286 dfs_mark_new_vtable (binfo) tree binfo;
2287 { SET_BINFO_NEW_VTABLE_MARKED (binfo); }
2290 dfs_unmark_new_vtable (binfo) tree binfo;
2291 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
2294 dfs_clear_search_slot (binfo) tree binfo;
2295 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
2299 dfs_debug_mark (binfo)
2302 tree t = BINFO_TYPE (binfo);
2304 /* Use heuristic that if there are virtual functions,
2305 ignore until we see a non-inline virtual function. */
2306 tree methods = CLASSTYPE_METHOD_VEC (t);
2308 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2313 /* If interface info is known, either we've already emitted the debug
2314 info or we don't need to. */
2315 if (CLASSTYPE_INTERFACE_KNOWN (t))
2318 /* If debug info is requested from this context for this type, supply it.
2319 If debug info is requested from another context for this type,
2320 see if some third context can supply it. */
2321 if (current_function_decl == NULL_TREE
2322 || DECL_CLASS_CONTEXT (current_function_decl) != t)
2324 if (TREE_VEC_ELT (methods, 1))
2325 methods = TREE_VEC_ELT (methods, 1);
2326 else if (TREE_VEC_ELT (methods, 0))
2327 methods = TREE_VEC_ELT (methods, 0);
2329 methods = TREE_VEC_ELT (methods, 2);
2330 methods = OVL_CURRENT (methods);
2333 if (DECL_VINDEX (methods)
2334 && DECL_THIS_INLINE (methods) == 0
2335 && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
2337 /* Somebody, somewhere is going to have to define this
2338 virtual function. When they do, they will provide
2339 the debugging info. */
2342 methods = TREE_CHAIN (methods);
2345 /* We cannot rely on some alien method to solve our problems,
2346 so we must write out the debug info ourselves. */
2347 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
2348 rest_of_type_compilation (t, toplevel_bindings_p ());
2351 /* Attach to the type of the virtual base class, the pointer to the
2352 virtual base class, given the global pointer vbase_decl_ptr.
2354 We use the global vbase_types. ICK! */
2357 dfs_find_vbases (binfo)
2360 tree binfos = BINFO_BASETYPES (binfo);
2361 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2363 for (i = n_baselinks-1; i >= 0; i--)
2365 tree base_binfo = TREE_VEC_ELT (binfos, i);
2367 if (TREE_VIA_VIRTUAL (base_binfo)
2368 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
2370 tree vbase = BINFO_TYPE (base_binfo);
2371 tree binfo = binfo_member (vbase, vbase_types);
2373 CLASSTYPE_SEARCH_SLOT (vbase)
2374 = build (PLUS_EXPR, build_pointer_type (vbase),
2375 vbase_decl_ptr, BINFO_OFFSET (binfo));
2378 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2379 SET_BINFO_NEW_VTABLE_MARKED (binfo);
2383 dfs_init_vbase_pointers (binfo)
2386 tree type = BINFO_TYPE (binfo);
2387 tree fields = TYPE_FIELDS (type);
2388 tree this_vbase_ptr;
2390 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2393 /* See finish_struct_1 for when we can enable this. */
2394 /* If we have a vtable pointer first, skip it. */
2395 if (VFIELD_NAME_P (DECL_NAME (fields)))
2396 fields = TREE_CHAIN (fields);
2399 if (fields == NULL_TREE
2400 || DECL_NAME (fields) == NULL_TREE
2401 || ! VBASE_NAME_P (DECL_NAME (fields)))
2404 this_vbase_ptr = vbase_decl_ptr_intermediate;
2406 if (build_pointer_type (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
2407 my_friendly_abort (125);
2409 while (fields && DECL_NAME (fields)
2410 && VBASE_NAME_P (DECL_NAME (fields)))
2412 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2413 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
2414 tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
2415 vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
2417 build_modify_expr (ref, NOP_EXPR, init),
2419 fields = TREE_CHAIN (fields);
2423 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2424 times, just NEW_VTABLE, but optimizer should make both with equal
2425 efficiency (though it does not currently). */
2428 dfs_clear_vbase_slots (binfo)
2431 tree type = BINFO_TYPE (binfo);
2432 CLASSTYPE_SEARCH_SLOT (type) = 0;
2433 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2434 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2438 init_vbase_pointers (type, decl_ptr)
2442 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2444 int old_flag = flag_this_is_variable;
2445 tree binfo = TYPE_BINFO (type);
2446 flag_this_is_variable = -2;
2447 vbase_types = CLASSTYPE_VBASECLASSES (type);
2448 vbase_decl_ptr = vbase_decl_ptr_intermediate = decl_ptr;
2449 vbase_init_result = NULL_TREE;
2450 dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp);
2451 dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp);
2452 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2453 flag_this_is_variable = old_flag;
2454 return vbase_init_result;
2459 /* get the virtual context (the vbase that directly contains the
2460 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2461 or NULL_TREE if there is none.
2463 FNDECL must come from a virtual table from a virtual base to ensure that
2464 there is only one possible DECL_CLASS_CONTEXT.
2466 We know that if there is more than one place (binfo) the fndecl that the
2467 declared, they all refer to the same binfo. See get_class_offset_1 for
2468 the check that ensures this. */
2471 virtual_context (fndecl, t, vbase)
2472 tree fndecl, t, vbase;
2475 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
2477 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2478 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0)
2482 /* Not sure if checking path == vbase is necessary here, but just in
2484 if (TREE_VIA_VIRTUAL (path) || path == vbase)
2485 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2486 path = BINFO_INHERITANCE_CHAIN (path);
2489 /* This shouldn't happen, I don't want errors! */
2490 warning ("recoverable compiler error, fixups for virtual function");
2495 if (TREE_VIA_VIRTUAL (path))
2496 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2497 path = BINFO_INHERITANCE_CHAIN (path);
2502 /* Fixups upcast offsets for one vtable.
2503 Entries may stay within the VBASE given, or
2504 they may upcast into a direct base, or
2505 they may upcast into a different vbase.
2507 We only need to do fixups in case 2 and 3. In case 2, we add in
2508 the virtual base offset to effect an upcast, in case 3, we add in
2509 the virtual base offset to effect an upcast, then subtract out the
2510 offset for the other virtual base, to effect a downcast into it.
2512 This routine mirrors fixup_vtable_deltas in functionality, though
2513 this one is runtime based, and the other is compile time based.
2514 Conceivably that routine could be removed entirely, and all fixups
2517 VBASE_OFFSETS is an association list of virtual bases that contains
2518 offset information for the virtual bases, so the offsets are only
2519 calculated once. The offsets are computed by where we think the
2520 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2521 the vbase really is. */
2524 expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t,
2526 tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets;
2528 tree virtuals = BINFO_VIRTUALS (binfo);
2531 unsigned HOST_WIDE_INT n;
2533 delta = purpose_member (vbase, *vbase_offsets);
2536 delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
2537 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr);
2538 delta = save_expr (delta);
2539 delta = tree_cons (vbase, delta, *vbase_offsets);
2540 *vbase_offsets = delta;
2543 n = skip_rtti_stuff (&virtuals);
2547 tree current_fndecl = TREE_VALUE (virtuals);
2548 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2549 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2551 && current_fndecl != abort_fndecl
2552 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2554 /* This may in fact need a runtime fixup. */
2555 tree idx = build_int_2 (n, 0);
2556 tree vtbl = BINFO_VTABLE (binfo);
2557 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2558 tree aref, ref, naref;
2559 tree old_delta, new_delta;
2562 if (nvtbl == NULL_TREE
2563 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2565 /* Dup it if it isn't in local scope yet. */
2567 (VAR_DECL, DECL_NAME (vtbl),
2568 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl)));
2569 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2570 DECL_ALIGN (nvtbl));
2571 TREE_READONLY (nvtbl) = 0;
2572 DECL_ARTIFICIAL (nvtbl) = 1;
2573 nvtbl = pushdecl (nvtbl);
2575 cp_finish_decl (nvtbl, init, NULL_TREE, 0,
2576 LOOKUP_ONLYCONVERTING);
2578 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2579 because they wouldn't be useful; everything that wants to
2580 look at the vtable will look at the decl for the normal
2581 vtable. Setting DECL_CONTEXT also screws up
2582 decl_function_context. */
2584 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2586 TREE_SIDE_EFFECTS (init) = 1;
2587 expand_expr_stmt (init);
2588 /* Update the vtable pointers as necessary. */
2589 ref = build_vfield_ref
2590 (build_indirect_ref (addr, NULL_PTR),
2591 DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
2593 (build_modify_expr (ref, NOP_EXPR, nvtbl));
2595 assemble_external (vtbl);
2596 aref = build_array_ref (vtbl, idx);
2597 naref = build_array_ref (nvtbl, idx);
2598 old_delta = build_component_ref (aref, delta_identifier,
2600 new_delta = build_component_ref (naref, delta_identifier,
2603 /* This is a upcast, so we have to add the offset for the
2605 old_delta = build_binary_op (PLUS_EXPR, old_delta,
2606 TREE_VALUE (delta), 0);
2609 /* If this is set, we need to subtract out the delta
2610 adjustments for the other virtual base that we
2612 tree vc_delta = purpose_member (vc, *vbase_offsets);
2615 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
2616 vc_delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
2617 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
2619 vc_delta = save_expr (vc_delta);
2620 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2623 vc_delta = TREE_VALUE (vc_delta);
2625 /* This is a downcast, so we have to subtract the offset
2626 for the virtual base. */
2627 old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta, 0);
2630 TREE_READONLY (new_delta) = 0;
2631 TREE_TYPE (new_delta) =
2632 cp_build_qualified_type (TREE_TYPE (new_delta),
2633 CP_TYPE_QUALS (TREE_TYPE (new_delta))
2634 & ~TYPE_QUAL_CONST);
2635 expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
2639 virtuals = TREE_CHAIN (virtuals);
2643 /* Fixup upcast offsets for all direct vtables. Patterned after
2644 expand_direct_vtbls_init. */
2647 fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
2648 tree real_binfo, binfo;
2649 int init_self, can_elide;
2650 tree addr, orig_addr, type, vbase, *vbase_offsets;
2652 tree real_binfos = BINFO_BASETYPES (real_binfo);
2653 tree binfos = BINFO_BASETYPES (binfo);
2654 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
2656 for (i = 0; i < n_baselinks; i++)
2658 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
2659 tree base_binfo = TREE_VEC_ELT (binfos, i);
2660 int is_not_base_vtable
2661 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
2662 if (! TREE_VIA_VIRTUAL (real_base_binfo))
2663 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
2664 is_not_base_vtable, can_elide, addr,
2665 orig_addr, type, vbase, vbase_offsets);
2668 /* Before turning this on, make sure it is correct. */
2669 if (can_elide && ! BINFO_MODIFIED (binfo))
2672 /* Should we use something besides CLASSTYPE_VFIELDS? */
2673 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
2675 tree new_addr = convert_pointer_to_real (binfo, addr);
2676 expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr,
2677 type, vbase_offsets);
2681 /* Build a COMPOUND_EXPR which when expanded will generate the code
2682 needed to initialize all the virtual function table slots of all
2683 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2684 the virtual baseclasses to use; TYPE is the type of the object to
2685 which the initialization applies. TRUE_EXP is the true object we
2686 are initializing, and DECL_PTR is the pointer to the sub-object we
2689 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
2690 object was laid out by a top-level constructor and the computed
2691 offsets are valid to store vtables. When zero, we must store new
2692 vtables through virtual baseclass pointers.
2694 We setup and use the globals: vbase_decl_ptr, vbase_types
2698 expand_indirect_vtbls_init (binfo, true_exp, decl_ptr)
2700 tree true_exp, decl_ptr;
2702 tree type = BINFO_TYPE (binfo);
2704 /* This function executes during the finish_function() segment,
2705 AFTER the auto variables and temporary stack space has been marked
2706 unused...If space is needed for the virtual function tables,
2707 some of them might fit within what the compiler now thinks
2708 are available stack slots... These values are actually initialized at
2709 the beginnning of the function, so when the automatics use their space,
2710 they will overwrite the values that are placed here. Marking all
2711 temporary space as unavailable prevents this from happening. */
2713 mark_all_temps_used();
2715 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2717 rtx fixup_insns = NULL_RTX;
2718 tree vbases = CLASSTYPE_VBASECLASSES (type);
2719 vbase_types = vbases;
2720 vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
2722 dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep);
2724 /* Initialized with vtables of type TYPE. */
2725 for (; vbases; vbases = TREE_CHAIN (vbases))
2729 addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vbase_decl_ptr);
2731 /* Do all vtables from this virtual base. */
2732 /* This assumes that virtual bases can never serve as parent
2733 binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */
2734 expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
2737 /* Now we adjust the offsets for virtual functions that
2738 cross virtual boundaries on an implicit upcast on vf call
2739 so that the layout of the most complete type is used,
2740 instead of assuming the layout of the virtual bases from
2741 our current type. */
2743 if (flag_vtable_thunks)
2745 /* We don't have dynamic thunks yet!
2746 So for now, just fail silently. */
2750 tree vbase_offsets = NULL_TREE;
2751 push_to_sequence (fixup_insns);
2752 fixup_virtual_upcast_offsets (vbases,
2753 TYPE_BINFO (BINFO_TYPE (vbases)),
2754 1, 0, addr, vbase_decl_ptr,
2755 type, vbases, &vbase_offsets);
2756 fixup_insns = get_insns ();
2763 extern tree in_charge_identifier;
2764 tree in_charge_node = lookup_name (in_charge_identifier, 0);
2765 if (! in_charge_node)
2767 warning ("recoverable internal compiler error, nobody's in charge!");
2768 in_charge_node = integer_zero_node;
2770 in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1);
2771 expand_start_cond (in_charge_node, 0);
2772 emit_insns (fixup_insns);
2776 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2780 /* get virtual base class types.
2781 This adds type to the vbase_types list in reverse dfs order.
2782 Ordering is very important, so don't change it. */
2785 dfs_get_vbase_types (binfo)
2788 if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
2790 tree new_vbase = make_binfo (integer_zero_node, binfo,
2791 BINFO_VTABLE (binfo),
2792 BINFO_VIRTUALS (binfo));
2793 TREE_CHAIN (new_vbase) = vbase_types;
2794 TREE_VIA_VIRTUAL (new_vbase) = 1;
2795 vbase_types = new_vbase;
2796 SET_BINFO_VBASE_MARKED (binfo);
2798 SET_BINFO_MARKED (binfo);
2801 /* get a list of virtual base classes in dfs order. */
2804 get_vbase_types (type)
2810 binfo = TYPE_BINFO (type);
2811 vbase_types = NULL_TREE;
2812 dfs_walk (binfo, dfs_get_vbase_types, unmarkedp);
2813 dfs_walk (binfo, dfs_unmark, markedp);
2814 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2815 reverse it so that we get normal dfs ordering. */
2816 vbase_types = nreverse (vbase_types);
2818 /* unmark marked vbases */
2819 for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
2820 CLEAR_BINFO_VBASE_MARKED (vbases);
2825 /* If we want debug info for a type TYPE, make sure all its base types
2826 are also marked as being potentially interesting. This avoids
2827 the problem of not writing any debug info for intermediate basetypes
2828 that have abstract virtual functions. Also mark member types. */
2831 note_debug_info_needed (type)
2836 if (current_template_parms)
2839 if (TYPE_BEING_DEFINED (type))
2840 /* We can't go looking for the base types and fields just yet. */
2843 /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2844 does not support name references between translation units. Well, we
2845 could, but that would mean putting global labels in the debug output
2846 before each exported type and each of its functions and static data
2848 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2851 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp);
2852 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2855 if (TREE_CODE (field) == FIELD_DECL
2856 && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
2857 && dfs_debug_unmarkedp (TYPE_BINFO (ttype)))
2858 note_debug_info_needed (ttype);
2862 /* Subroutines of push_class_decls (). */
2864 /* Add in a decl to the envelope. */
2866 envelope_add_decl (type, decl, values)
2867 tree type, decl, *values;
2870 tree name = DECL_NAME (decl);
2873 /* Yet Another Implicit Typename Kludge: Since we don't tsubst
2874 the members for partial instantiations, DECL_CONTEXT (decl) is wrong.
2875 But pretend it's right for this function. */
2876 if (processing_template_decl)
2877 type = DECL_REAL_CONTEXT (decl);
2879 /* virtual base names are always unique. */
2880 if (VBASE_NAME_P (name))
2881 *values = NULL_TREE;
2883 /* Possible ambiguity. If its defining type(s)
2884 is (are all) derived from us, no problem. */
2885 else if (*values && TREE_CODE (*values) != TREE_LIST)
2887 tree value = *values;
2888 /* Only complain if we shadow something we can access. */
2889 if (warn_shadow && TREE_CODE (decl) == FUNCTION_DECL
2890 && ((DECL_LANG_SPECIFIC (*values)
2891 && DECL_CLASS_CONTEXT (value) == current_class_type)
2892 || ! TREE_PRIVATE (value)))
2893 /* Should figure out access control more accurately. */
2895 cp_warning_at ("member `%#D' is shadowed", value);
2896 cp_warning_at ("by member function `%#D'", decl);
2897 warning ("in this context");
2900 context = DECL_REAL_CONTEXT (value);
2902 if (context == type)
2904 if (TREE_CODE (value) == TYPE_DECL
2905 && DECL_ARTIFICIAL (value))
2906 *values = NULL_TREE;
2910 else if (type == current_class_type
2911 || DERIVED_FROM_P (context, type))
2913 /* Don't add in *values to list */
2914 *values = NULL_TREE;
2917 *values = build_tree_list (NULL_TREE, value);
2920 for (tmp = values; *tmp;)
2922 tree value = TREE_VALUE (*tmp);
2923 my_friendly_assert (TREE_CODE (value) != TREE_LIST, 999);
2924 context = (TREE_CODE (value) == FUNCTION_DECL
2925 && DECL_VIRTUAL_P (value))
2926 ? DECL_CLASS_CONTEXT (value)
2927 : DECL_CONTEXT (value);
2929 if (type == current_class_type
2930 || DERIVED_FROM_P (context, type))
2932 /* remove *tmp from list */
2933 *tmp = TREE_CHAIN (*tmp);
2936 tmp = &TREE_CHAIN (*tmp);
2941 /* Put the new contents in our envelope. */
2942 if (TREE_CODE (decl) == FUNCTION_DECL)
2944 *values = tree_cons (name, decl, *values);
2945 TREE_NONLOCAL_FLAG (*values) = 1;
2946 TREE_TYPE (*values) = unknown_type_node;
2952 *values = tree_cons (NULL_TREE, decl, *values);
2953 /* Mark this as a potentially ambiguous member. */
2954 /* Leaving TREE_TYPE blank is intentional.
2955 We cannot use `error_mark_node' (lookup_name)
2956 or `unknown_type_node' (all member functions use this). */
2957 TREE_NONLOCAL_FLAG (*values) = 1;
2965 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2966 because it (or one of the intermediate bases) depends on template parms. */
2969 dependent_base_p (binfo)
2972 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2974 if (TREE_TYPE (binfo) == current_class_type)
2976 if (uses_template_parms (TREE_TYPE (binfo)))
2982 /* Add the instance variables which this class contributed to the
2983 current class binding contour. When a redefinition occurs, if the
2984 redefinition is strictly within a single inheritance path, we just
2985 overwrite the old declaration with the new. If the fields are not
2986 within a single inheritance path, we must cons them.
2988 In order to know what decls are new (stemming from the current
2989 invocation of push_class_decls) we enclose them in an "envelope",
2990 which is a TREE_LIST node where the TREE_PURPOSE slot contains the
2991 new decl (or possibly a list of competing ones), the TREE_VALUE slot
2992 points to the old value and the TREE_CHAIN slot chains together all
2993 envelopes which needs to be "opened" in push_class_decls. Opening an
2994 envelope means: push the old value onto the class_shadowed list,
2995 install the new one and if it's a TYPE_DECL do the same to the
2996 IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that
2997 the TREE_PURPOSE slot is non-null, and that it is not an identifier.
2998 Because if it is, it could be a set of overloaded methods from an
3002 dfs_pushdecls (binfo)
3005 tree type = BINFO_TYPE (binfo);
3010 /* Only record types if we're a template base. */
3011 if (processing_template_decl && type != current_class_type
3012 && dependent_base_p (binfo))
3015 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3017 if (dummy && TREE_CODE (fields) != TYPE_DECL)
3020 /* Unmark so that if we are in a constructor, and then find that
3021 this field was initialized by a base initializer,
3022 we can emit an error message. */
3023 if (TREE_CODE (fields) == FIELD_DECL)
3024 TREE_USED (fields) = 0;
3026 /* Recurse into anonymous unions. */
3027 if (DECL_NAME (fields) == NULL_TREE
3028 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3030 dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields)));
3034 if (DECL_NAME (fields))
3036 tree name = DECL_NAME (fields);
3037 tree class_value = IDENTIFIER_CLASS_VALUE (name);
3039 /* If the class value is not an envelope of the kind described in
3040 the comment above, we create a new envelope. */
3041 maybe_push_cache_obstack ();
3042 if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
3043 || TREE_PURPOSE (class_value) == NULL_TREE
3044 || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
3046 /* See comment above for a description of envelopes. */
3047 closed_envelopes = tree_cons (NULL_TREE, class_value,
3049 IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
3050 class_value = IDENTIFIER_CLASS_VALUE (name);
3053 envelope_add_decl (type, fields, &TREE_PURPOSE (class_value));
3058 method_vec = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE;
3059 if (method_vec && ! dummy)
3064 /* Farm out constructors and destructors. */
3065 end = TREE_VEC_END (method_vec);
3067 for (methods = &TREE_VEC_ELT (method_vec, 2);
3068 *methods && methods != end;
3071 /* This will cause lookup_name to return a pointer
3072 to the tree_list of possible methods of this name. */
3077 name = DECL_NAME (OVL_CURRENT (*methods));
3078 class_value = IDENTIFIER_CLASS_VALUE (name);
3080 maybe_push_cache_obstack ();
3082 /* If the class value is not an envelope of the kind described in
3083 the comment above, we create a new envelope. */
3084 if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
3085 || TREE_PURPOSE (class_value) == NULL_TREE
3086 || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
3088 /* See comment above for a description of envelopes. */
3089 closed_envelopes = tree_cons (NULL_TREE, class_value,
3091 IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
3092 class_value = IDENTIFIER_CLASS_VALUE (name);
3095 /* Here we try to rule out possible ambiguities.
3096 If we can't do that, keep a TREE_LIST with possibly ambiguous
3098 /* Arbitrarily choose the first function in the list. This is OK
3099 because this is only used for initial lookup; anything that
3100 actually uses the function will look it up again. */
3101 envelope_add_decl (type, OVL_CURRENT (*methods),
3102 &TREE_PURPOSE (class_value));
3107 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3108 DERIVED_FROM_P, which calls get_base_distance. */
3109 SET_BINFO_PUSHDECLS_MARKED (binfo);
3112 /* Consolidate unique (by name) member functions. */
3115 dfs_compress_decls (binfo)
3118 tree type = BINFO_TYPE (binfo);
3120 = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE;
3122 if (processing_template_decl && type != current_class_type
3123 && dependent_base_p (binfo))
3124 /* We only record types if we're a template base. */;
3125 else if (method_vec != 0)
3127 /* Farm out constructors and destructors. */
3129 tree *end = TREE_VEC_END (method_vec);
3131 for (methods = &TREE_VEC_ELT (method_vec, 2);
3132 methods != end && *methods; methods++)
3134 /* This is known to be an envelope of the kind described before
3137 IDENTIFIER_CLASS_VALUE (DECL_NAME (OVL_CURRENT (*methods)));
3138 tree tmp = TREE_PURPOSE (class_value);
3140 /* This was replaced in scope by somebody else. Just leave it
3142 if (TREE_CODE (tmp) != TREE_LIST)
3145 if (TREE_CHAIN (tmp) == NULL_TREE
3147 && OVL_NEXT (TREE_VALUE (tmp)) == NULL_TREE)
3149 TREE_PURPOSE (class_value) = TREE_VALUE (tmp);
3153 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
3156 /* When entering the scope of a class, we cache all of the
3157 fields that that class provides within its inheritance
3158 lattice. Where ambiguities result, we mark them
3159 with `error_mark_node' so that if they are encountered
3160 without explicit qualification, we can emit an error
3164 push_class_decls (type)
3167 struct obstack *ambient_obstack = current_obstack;
3168 search_stack = push_search_level (search_stack, &search_obstack);
3170 /* Build up all the relevant bindings and such on the cache
3171 obstack. That way no memory is wasted when we throw away the
3173 maybe_push_cache_obstack ();
3175 /* Push class fields into CLASS_VALUE scope, and mark. */
3176 dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarked_pushdecls_p);
3178 /* Compress fields which have only a single entry
3179 by a given name, and unmark. */
3180 dfs_walk (TYPE_BINFO (type), dfs_compress_decls, marked_pushdecls_p);
3182 /* Open up all the closed envelopes and push the contained decls into
3184 while (closed_envelopes)
3186 tree new = TREE_PURPOSE (closed_envelopes);
3189 /* This is messy because the class value may be a *_DECL, or a
3190 TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
3191 *_DECLs. The name is stored at different places in these three
3193 if (TREE_CODE (new) == TREE_LIST)
3195 if (TREE_PURPOSE (new) != NULL_TREE)
3196 id = TREE_PURPOSE (new);
3199 tree node = TREE_VALUE (new);
3201 if (TREE_CODE (node) == TYPE_DECL
3202 && DECL_ARTIFICIAL (node)
3203 && IS_AGGR_TYPE (TREE_TYPE (node))
3204 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (node)))
3206 tree t = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (node));
3209 for (; n; n = TREE_CHAIN (n))
3211 tree d = TREE_VALUE (n);
3212 if (TREE_CODE (d) == TYPE_DECL
3213 && DECL_ARTIFICIAL (node)
3214 && IS_AGGR_TYPE (TREE_TYPE (d))
3215 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d))
3216 && CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d)) == t)
3225 else while (TREE_CODE (node) == TREE_LIST)
3226 node = TREE_VALUE (node);
3227 id = DECL_NAME (node);
3231 id = DECL_NAME (new);
3233 /* Install the original class value in order to make
3234 pushdecl_class_level work correctly. */
3235 IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes);
3236 if (TREE_CODE (new) == TREE_LIST)
3237 push_class_level_binding (id, new);
3239 pushdecl_class_level (new);
3240 closed_envelopes = TREE_CHAIN (closed_envelopes);
3243 /* Undo the call to maybe_push_cache_obstack above. */
3246 current_obstack = ambient_obstack;
3249 /* Here's a subroutine we need because C lacks lambdas. */
3252 dfs_unuse_fields (binfo)
3255 tree type = TREE_TYPE (binfo);
3258 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3260 if (TREE_CODE (fields) != FIELD_DECL)
3263 TREE_USED (fields) = 0;
3264 if (DECL_NAME (fields) == NULL_TREE
3265 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3266 unuse_fields (TREE_TYPE (fields));
3274 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp);
3280 /* We haven't pushed a search level when dealing with cached classes,
3281 so we'd better not try to pop it. */
3283 search_stack = pop_search_level (search_stack);
3287 print_search_statistics ()
3289 #ifdef GATHER_STATISTICS
3290 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3291 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3292 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3293 n_outer_fields_searched, n_calls_lookup_fnfields);
3294 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
3295 #else /* GATHER_STATISTICS */
3296 fprintf (stderr, "no search statistics\n");
3297 #endif /* GATHER_STATISTICS */
3301 init_search_processing ()
3303 gcc_obstack_init (&search_obstack);
3304 _vptr_name = get_identifier ("_vptr");
3308 reinit_search_statistics ()
3310 #ifdef GATHER_STATISTICS
3311 n_fields_searched = 0;
3312 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3313 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3314 n_calls_get_base_type = 0;
3315 n_outer_fields_searched = 0;
3316 n_contexts_saved = 0;
3317 #endif /* GATHER_STATISTICS */
3320 #define scratch_tree_cons expr_tree_cons
3323 add_conversions (binfo, data)
3328 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
3329 tree *conversions = (tree *) data;
3331 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
3333 tree tmp = TREE_VEC_ELT (method_vec, i);
3336 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
3339 name = DECL_NAME (OVL_CURRENT (tmp));
3341 /* Make sure we don't already have this conversion. */
3342 if (! IDENTIFIER_MARKED (name))
3344 *conversions = scratch_tree_cons (binfo, tmp, *conversions);
3345 IDENTIFIER_MARKED (name) = 1;
3352 lookup_conversions (type)
3356 tree conversions = NULL_TREE;
3358 if (TYPE_SIZE (type))
3359 breadth_first_search (TYPE_BINFO (type), add_conversions,
3360 0, 0, &conversions);
3362 for (t = conversions; t; t = TREE_CHAIN (t))
3363 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
3368 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3369 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3371 static tree compare_type;
3372 static int found_overlap;
3374 dfs_check_overlap (empty_binfo)
3378 for (binfo = TYPE_BINFO (compare_type); ; binfo = BINFO_BASETYPE (binfo, 0))
3380 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
3385 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
3390 /* Trivial function to stop base traversal when we find something. */
3393 dfs_no_overlap_yet (t)
3394 tree t ATTRIBUTE_UNUSED;
3396 return found_overlap == 0;
3399 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3400 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3403 types_overlap_p (empty_type, next_type)
3404 tree empty_type, next_type;
3406 if (! IS_AGGR_TYPE (next_type))
3408 compare_type = next_type;
3410 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap, dfs_no_overlap_yet);
3411 return found_overlap;
3414 /* Passed to dfs_search by binfo_for_vtable; determine if bvtable comes
3417 static tree bvtable;
3419 dfs_bfv_helper (binfo)
3422 if (BINFO_VTABLE (binfo) == bvtable)
3427 /* Given a vtable VARS, determine which binfo it comes from. */
3430 binfo_for_vtable (vars)
3434 return dfs_search (TYPE_BINFO (DECL_CONTEXT (vars)), dfs_bfv_helper,
3435 DECL_CONTEXT (vars));