1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 89, 92, 93, 94, 1995 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. */
34 #define obstack_chunk_alloc xmalloc
35 #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. */
44 static struct obstack search_obstack;
46 /* Methods for pushing and popping objects to and from obstacks. */
48 push_stack_level (obstack, tp, size)
49 struct obstack *obstack;
50 char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
53 struct stack_level *stack;
54 obstack_grow (obstack, tp, size);
55 stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
56 obstack_finish (obstack);
57 stack->obstack = obstack;
58 stack->first = (tree *) obstack_base (obstack);
59 stack->limit = obstack_room (obstack) / sizeof (tree *);
64 pop_stack_level (stack)
65 struct stack_level *stack;
67 struct stack_level *tem = stack;
68 struct obstack *obstack = tem->obstack;
70 obstack_free (obstack, tem);
74 #define search_level stack_level
75 static struct search_level *search_stack;
77 static tree lookup_field_1 ();
78 static int lookup_fnfields_1 ();
79 static void dfs_walk ();
80 static int markedp ();
81 static void dfs_unmark ();
82 static void dfs_init_vbase_pointers ();
84 static tree vbase_types;
85 static tree vbase_decl, vbase_decl_ptr;
86 static tree vbase_decl_ptr_intermediate;
87 static tree vbase_init_result;
89 /* Allocate a level of searching. */
90 static struct search_level *
91 push_search_level (stack, obstack)
92 struct stack_level *stack;
93 struct obstack *obstack;
95 struct search_level tem;
98 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
101 /* Discard a level of search allocation. */
102 static struct search_level *
103 pop_search_level (obstack)
104 struct stack_level *obstack;
106 register struct search_level *stack = pop_stack_level (obstack);
111 /* Search memoization. */
114 struct stack_level base;
116 /* First object allocated in obstack of entries. */
119 /* Number of types memoized in this context. */
122 /* Type being memoized; save this if we are saving
123 memoized contexts. */
127 /* Obstack used for memoizing member and member function lookup. */
129 static struct obstack type_obstack, type_obstack_entries;
130 static struct type_level *type_stack;
131 static tree _vptr_name;
133 /* Make things that look like tree nodes, but allocate them
134 on type_obstack_entries. */
135 static int my_tree_node_counter;
136 static tree my_tree_cons (), my_build_string ();
138 extern int flag_memoize_lookups, flag_save_memoized_contexts;
140 /* Variables for gathering statistics. */
141 static int my_memoized_entry_counter;
142 static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2];
143 static int memoized_fields_searched[2];
144 static int n_fields_searched;
145 static int n_calls_lookup_field, n_calls_lookup_field_1;
146 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
147 static int n_calls_get_base_type;
148 static int n_outer_fields_searched;
149 static int n_contexts_saved;
151 /* Local variables to help save memoization contexts. */
152 static tree prev_type_memoized;
153 static struct type_level *prev_type_stack;
155 /* This list is used by push_class_decls to know what decls need to
156 be pushed into class scope. */
157 static tree closed_envelopes = NULL_TREE;
159 /* Allocate a level of type memoization context. */
160 static struct type_level *
161 push_type_level (stack, obstack)
162 struct stack_level *stack;
163 struct obstack *obstack;
165 struct type_level tem;
167 tem.base.prev = stack;
169 obstack_finish (&type_obstack_entries);
170 tem.entries = (char *) obstack_base (&type_obstack_entries);
172 tem.type = NULL_TREE;
174 return (struct type_level *)push_stack_level (obstack, (char *)&tem, sizeof (tem));
177 /* Discard a level of type memoization context. */
179 static struct type_level *
180 pop_type_level (stack)
181 struct type_level *stack;
183 obstack_free (&type_obstack_entries, stack->entries);
184 return (struct type_level *)pop_stack_level ((struct stack_level *)stack);
187 /* Make something that looks like a TREE_LIST, but
188 do it on the type_obstack_entries obstack. */
190 my_tree_cons (purpose, value, chain)
191 tree purpose, value, chain;
193 tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list));
194 ++my_tree_node_counter;
195 TREE_TYPE (p) = NULL_TREE;
196 ((HOST_WIDE_INT *)p)[3] = 0;
197 TREE_SET_CODE (p, TREE_LIST);
198 TREE_PURPOSE (p) = purpose;
199 TREE_VALUE (p) = value;
200 TREE_CHAIN (p) = chain;
205 my_build_string (str)
208 tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string));
209 ++my_tree_node_counter;
212 TREE_SET_CODE (p, STRING_CST);
213 TREE_STRING_POINTER (p) = str;
214 TREE_STRING_LENGTH (p) = strlen (str);
218 /* Memoizing machinery to make searches for multiple inheritance
219 reasonably efficient. */
220 #define MEMOIZE_HASHSIZE 8
221 typedef struct memoized_entry
223 struct memoized_entry *chain;
225 tree data_members[MEMOIZE_HASHSIZE];
226 tree function_members[MEMOIZE_HASHSIZE];
229 #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain)
230 #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid)
231 #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX])
232 #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX])
233 /* The following is probably a lousy hash function. */
234 #define MEMOIZED_HASH_FN(NODE) (((long)(NODE)>>4)&(MEMOIZE_HASHSIZE - 1))
236 static struct memoized_entry *
237 my_new_memoized_entry (chain)
238 struct memoized_entry *chain;
240 struct memoized_entry *p =
241 (struct memoized_entry *)obstack_alloc (&type_obstack_entries,
242 sizeof (struct memoized_entry));
243 bzero ((char *) p, sizeof (struct memoized_entry));
244 MEMOIZED_CHAIN (p) = chain;
245 MEMOIZED_UID (p) = ++my_memoized_entry_counter;
249 /* Make an entry in the memoized table for type TYPE
250 that the entry for NAME is FIELD. */
253 make_memoized_table_entry (type, name, function_p)
257 int index = MEMOIZED_HASH_FN (name);
258 tree entry, *prev_entry;
260 memoized_adds[function_p] += 1;
261 if (CLASSTYPE_MTABLE_ENTRY (type) == 0)
263 obstack_ptr_grow (&type_obstack, type);
264 obstack_blank (&type_obstack, sizeof (struct memoized_entry *));
265 CLASSTYPE_MTABLE_ENTRY (type) = (char *)my_new_memoized_entry ((struct memoized_entry *)0);
267 if (type_stack->len * 2 >= type_stack->base.limit)
268 my_friendly_abort (88);
271 prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
273 prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
275 entry = my_tree_cons (name, NULL_TREE, *prev_entry);
278 /* Don't know the error message to give yet. */
279 TREE_TYPE (entry) = error_mark_node;
284 /* When a new function or class context is entered, we build
285 a table of types which have been searched for members.
286 The table is an array (obstack) of types. When a type is
287 entered into the obstack, its CLASSTYPE_MTABLE_ENTRY
288 field is set to point to a new record, of type struct memoized_entry.
290 A non-NULL TREE_TYPE of the entry contains an access control error message.
292 The slots for the data members are arrays of tree nodes.
293 These tree nodes are lists, with the TREE_PURPOSE
294 of this list the known member name, and the TREE_VALUE
295 as the FIELD_DECL for the member.
297 For member functions, the TREE_PURPOSE is again the
298 name of the member functions for that class,
299 and the TREE_VALUE of the list is a pairs
300 whose TREE_PURPOSE is a member functions of this name,
301 and whose TREE_VALUE is a list of known argument lists this
302 member function has been called with. The TREE_TYPE of the pair,
303 if non-NULL, is an error message to print. */
305 /* Tell search machinery that we are entering a new context, and
306 to update tables appropriately.
308 TYPE is the type of the context we are entering, which can
309 be NULL_TREE if we are not in a class's scope.
311 USE_OLD, if nonzero tries to use previous context. */
313 push_memoized_context (type, use_old)
322 if (use_old && prev_type_memoized == type)
324 #ifdef GATHER_STATISTICS
327 type_stack = prev_type_stack;
330 tem = &type_stack->base.first[0];
331 len = type_stack->len;
333 CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = (char *)tem[len*2+1];
336 /* Otherwise, need to pop old stack here. */
337 type_stack = pop_type_level (prev_type_stack);
338 prev_type_memoized = 0;
342 type_stack = push_type_level ((struct stack_level *)type_stack,
344 type_stack->type = type;
347 /* Tell search machinery that we have left a context.
348 We do not currently save these contexts for later use.
349 If we wanted to, we could not use pop_search_level, since
350 poping that level allows the data we have collected to
351 be clobbered; a stack of obstacks would be needed. */
353 pop_memoized_context (use_old)
357 tree *tem = &type_stack->base.first[0];
359 if (! flag_save_memoized_contexts)
363 len = type_stack->len;
365 tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]);
367 prev_type_stack = type_stack;
368 prev_type_memoized = type_stack->type;
371 if (flag_memoize_lookups)
373 len = type_stack->len;
375 CLASSTYPE_MTABLE_ENTRY (tem[len*2])
376 = (char *)MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2]));
379 type_stack = pop_type_level (type_stack);
381 type_stack = (struct type_level *)type_stack->base.prev;
384 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
385 the same type as the type given in PARENT. To be optimal, we want
386 the first one that is found by going through the least number of
387 virtual bases. DEPTH should be NULL_PTR. */
389 get_vbase (parent, binfo, depth)
395 tree rval = NULL_TREE;
399 unsigned int d = (unsigned int)-1;
400 return get_vbase (parent, binfo, &d);
403 if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo))
411 binfos = BINFO_BASETYPES (binfo);
412 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
414 /* Process base types. */
415 for (i = 0; i < n_baselinks; i++)
417 tree base_binfo = TREE_VEC_ELT (binfos, i);
423 nrval = get_vbase (parent, base_binfo, depth);
431 /* This is the newer recursive depth first search routine. */
433 /* Return non-zero if PARENT is directly derived from TYPE. By directly
434 we mean it's only one step up the inheritance lattice. We check this
435 by walking horizontally across the types that TYPE directly inherits
436 from, to see if PARENT is among them. This is used by get_binfo and
437 by compute_access. */
439 immediately_derived (parent, type)
442 if (TYPE_BINFO (type))
444 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
445 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
447 for (i = 0; i < n_baselinks; i++)
449 tree base_binfo = TREE_VEC_ELT (binfos, i);
451 if (parent == BINFO_TYPE (base_binfo))
459 /* Check whether the type given in BINFO is derived from PARENT. If
460 it isn't, return 0. If it is, but the derivation is MI-ambiguous
461 AND protect != 0, emit an error message and return error_mark_node.
463 Otherwise, if TYPE is derived from PARENT, return the actual base
464 information, unless a one of the protection violations below
465 occurs, in which case emit an error message and return error_mark_node.
467 If PROTECT is 1, then check if access to a public field of PARENT
468 would be private. Also check for ambiguity. */
471 get_binfo (parent, binfo, protect)
472 register tree parent, binfo;
477 tree rval = NULL_TREE;
479 if (TREE_CODE (parent) == TREE_VEC)
480 parent = BINFO_TYPE (parent);
481 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
482 my_friendly_abort (89);
484 if (TREE_CODE (binfo) == TREE_VEC)
485 type = BINFO_TYPE (binfo);
486 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
489 my_friendly_abort (90);
491 dist = get_base_distance (parent, binfo, protect, &rval);
495 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
497 return error_mark_node;
499 else if (dist == -2 && protect)
501 cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
503 return error_mark_node;
509 /* This is the newer depth first get_base_distance routine. */
511 get_base_distance_recursive (binfo, depth, is_private, basetype_path, rval,
512 rval_private_ptr, new_binfo_ptr, parent, path_ptr,
513 protect, via_virtual_ptr, via_virtual)
514 tree binfo, basetype_path, *new_binfo_ptr, parent, *path_ptr;
515 int *rval_private_ptr, depth, is_private, rval, protect, *via_virtual_ptr,
521 if (BINFO_TYPE (binfo) == parent || binfo == parent)
526 *rval_private_ptr = is_private;
527 *new_binfo_ptr = binfo;
528 *via_virtual_ptr = via_virtual;
532 int same_object = (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
533 BINFO_OFFSET (binfo))
534 && *via_virtual_ptr && via_virtual);
536 if (*via_virtual_ptr && via_virtual==0)
538 *rval_private_ptr = is_private;
539 *new_binfo_ptr = binfo;
540 *via_virtual_ptr = via_virtual;
542 else if (same_object)
544 if (*rval_private_ptr && ! is_private)
546 *rval_private_ptr = is_private;
547 *new_binfo_ptr = binfo;
548 *via_virtual_ptr = via_virtual;
558 binfos = BINFO_BASETYPES (binfo);
559 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
562 /* Process base types. */
563 for (i = 0; i < n_baselinks; i++)
565 tree base_binfo = TREE_VEC_ELT (binfos, i);
567 /* Find any specific instance of a virtual base, when searching with
569 if (BINFO_MARKED (base_binfo) == 0 || TREE_CODE (parent) == TREE_VEC)
574 || (!TREE_VIA_PUBLIC (base_binfo)
575 && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
576 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
579 /* When searching for a non-virtual, we cannot mark
580 virtually found binfos. */
582 SET_BINFO_MARKED (base_binfo);
584 #define WATCH_VALUES(rval, via_private) (rval == -1 ? 3 : via_private)
586 was = WATCH_VALUES (rval, *via_virtual_ptr);
587 rval = get_base_distance_recursive (base_binfo, depth, via_private,
588 binfo, rval, rval_private_ptr,
589 new_binfo_ptr, parent, path_ptr,
590 protect, via_virtual_ptr,
592 /* watch for updates; only update if path is good. */
593 if (path_ptr && WATCH_VALUES (rval, *via_virtual_ptr) != was)
594 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
595 if (rval == -2 && *via_virtual_ptr == 0)
606 /* Return the number of levels between type PARENT and the type given
607 in BINFO, following the leftmost path to PARENT not found along a
608 virtual path, if there are no real PARENTs (all come from virtual
609 base classes), then follow the leftmost path to PARENT.
611 Return -1 if TYPE is not derived from PARENT.
612 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
614 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
616 If PATH_PTR is non-NULL, then also build the list of types
617 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
620 PARENT can also be a binfo, in which case that exact parent is found
621 and no other. convert_pointer_to_real uses this functionality.
623 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
626 get_base_distance (parent, binfo, protect, path_ptr)
627 register tree parent, binfo;
632 int rval_private = 0;
634 tree new_binfo = NULL_TREE;
636 int watch_access = protect;
638 if (TREE_CODE (parent) != TREE_VEC)
639 parent = TYPE_MAIN_VARIANT (parent);
641 if (TREE_CODE (binfo) == TREE_VEC)
642 type = BINFO_TYPE (binfo);
643 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
646 binfo = TYPE_BINFO (type);
649 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
652 my_friendly_abort (92);
654 if (parent == type || parent == binfo)
656 /* If the distance is 0, then we don't really need
657 a path pointer, but we shouldn't let garbage go back. */
666 rval = get_base_distance_recursive (binfo, 0, 0, NULL_TREE, -1,
667 &rval_private, &new_binfo, parent,
668 path_ptr, watch_access, &via_virtual, 0);
670 dfs_walk (binfo, dfs_unmark, markedp);
672 /* Access restrictions don't count if we found an ambiguous basetype. */
673 if (rval == -2 && protect >= 0)
676 if (rval && protect && rval_private)
679 /* find real virtual base classes. */
680 if (rval == -1 && TREE_CODE (parent) == TREE_VEC
681 && parent == binfo_member (BINFO_TYPE (parent),
682 CLASSTYPE_VBASECLASSES (type)))
684 BINFO_INHERITANCE_CHAIN (parent) = binfo;
690 *path_ptr = new_binfo;
694 /* Search for a member with name NAME in a multiple inheritance lattice
695 specified by TYPE. If it does not exist, return NULL_TREE.
696 If the member is ambiguously referenced, return `error_mark_node'.
697 Otherwise, return the FIELD_DECL. */
699 /* Do a 1-level search for NAME as a member of TYPE. The caller must
700 figure out whether it can access this field. (Since it is only one
701 level, this is reasonable.) */
703 lookup_field_1 (type, name)
706 register tree field = TYPE_FIELDS (type);
708 #ifdef GATHER_STATISTICS
709 n_calls_lookup_field_1++;
713 #ifdef GATHER_STATISTICS
716 if (DECL_NAME (field) == NULL_TREE
717 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
719 tree temp = lookup_field_1 (TREE_TYPE (field), name);
723 if (DECL_NAME (field) == name)
725 if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
726 && DECL_ASSEMBLER_NAME (field) != NULL)
727 GNU_xref_ref(current_function_decl,
728 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
731 field = TREE_CHAIN (field);
734 if (name == _vptr_name)
736 /* Give the user what s/he thinks s/he wants. */
737 if (TYPE_VIRTUAL_P (type))
738 return CLASSTYPE_VFIELD (type);
743 /* There are a number of cases we need to be aware of here:
744 current_class_type current_function_decl
747 * class-local SET NULL
751 Those last two make life interesting. If we're in a function which is
752 itself inside a class, we need decls to go into the fn's decls (our
753 second case below). But if we're in a class and the class itself is
754 inside a function, we need decls to go into the decls for the class. To
755 achieve this last goal, we must see if, when both current_class_decl and
756 current_function_decl are set, the class was declared inside that
757 function. If so, we know to put the decls into the class's scope. */
762 if (current_function_decl == NULL_TREE)
763 return current_class_type;
764 if (current_class_type == NULL_TREE)
765 return current_function_decl;
766 if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type)
767 return current_function_decl;
769 return current_class_type;
772 /* Compute the access of FIELD. This is done by computing
773 the access available to each type in BASETYPES (which comes
774 as a list of [via_public/basetype] in reverse order, namely base
775 class before derived class). The first one which defines a
776 access defines the access for the field. Otherwise, the
777 access of the field is that which occurs normally.
779 Uses global variables CURRENT_CLASS_TYPE and
780 CURRENT_FUNCTION_DECL to use friend relationships
783 This will be static when lookup_fnfield comes into this file.
785 access_public means that the field can be accessed by the current lexical
788 access_protected means that the field cannot be accessed by the current
789 lexical scope because it is protected.
791 access_private means that the field cannot be accessed by the current
792 lexical scope because it is private. */
795 #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public
796 #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected
797 #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private
799 #define PUBLIC_RETURN return access_public
800 #define PROTECTED_RETURN return access_protected
801 #define PRIVATE_RETURN return access_private
805 /* Disabled with DECL_PUBLIC &c. */
806 static tree previous_scope = NULL_TREE;
810 compute_access (basetype_path, field)
811 tree basetype_path, field;
813 enum access_type access;
816 int protected_ok, via_protected;
817 extern int flag_access_control;
819 /* Replaces static decl above. */
823 ((TREE_CODE (field) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (field))
824 || (TREE_CODE (field) != FUNCTION_DECL && TREE_STATIC (field)));
826 if (! flag_access_control)
827 return access_public;
829 /* The field lives in the current class. */
830 if (BINFO_TYPE (basetype_path) == current_class_type)
831 return access_public;
834 /* Disabled until pushing function scope clears these out. If ever. */
835 /* Make these special cases fast. */
836 if (current_scope () == previous_scope)
838 if (DECL_PUBLIC (field))
839 return access_public;
840 if (DECL_PROTECTED (field))
841 return access_protected;
842 if (DECL_PRIVATE (field))
843 return access_private;
847 /* We don't currently support access control on nested types. */
848 if (TREE_CODE (field) == TYPE_DECL)
849 return access_public;
851 previous_scope = current_scope ();
853 context = DECL_CLASS_CONTEXT (field);
854 if (context == NULL_TREE)
855 context = DECL_CONTEXT (field);
857 /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT
858 slot set to the union type rather than the record type containing
859 the anonymous union. In this case, DECL_FIELD_CONTEXT is correct. */
860 if (context && TREE_CODE (context) == UNION_TYPE
861 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
862 context = DECL_FIELD_CONTEXT (field);
864 /* Virtual function tables are never private. But we should know that
865 we are looking for this, and not even try to hide it. */
866 if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1)
869 /* Member found immediately within object. */
870 if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE)
872 /* Are we (or an enclosing scope) friends with the class that has
874 if (is_friend (context, previous_scope))
877 /* If it's private, it's private, you letch. */
878 if (TREE_PRIVATE (field))
881 /* ARM $11.5. Member functions of a derived class can access the
882 non-static protected members of a base class only through a
883 pointer to the derived class, a reference to it, or an object
884 of it. Also any subsequently derived classes also have
886 else if (TREE_PROTECTED (field))
888 if (current_class_type
890 && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type))
899 /* must reverse more than one element */
900 basetype_path = reverse_path (basetype_path);
901 types = basetype_path;
903 access = access_default;
904 protected_ok = static_mem && current_class_type
905 && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type);
911 tree type = BINFO_TYPE (binfo);
914 /* Friends of a class can see protected members of its bases.
915 Note that classes are their own friends. */
916 if (is_friend (type, previous_scope))
922 member = purpose_member (type, DECL_ACCESS (field));
925 access = (enum access_type) TREE_VALUE (member);
929 types = BINFO_INHERITANCE_CHAIN (types);
931 /* If the next type was VIA_PROTECTED, then fields of all remaining
932 classes past that one are *at least* protected. */
935 if (TREE_VIA_PROTECTED (types))
937 else if (! TREE_VIA_PUBLIC (types) && ! private_ok)
939 access = access_private;
946 reverse_path (basetype_path);
948 /* No special visibilities apply. Use normal rules. */
950 if (access == access_default)
952 if (is_friend (context, previous_scope))
953 access = access_public;
954 else if (TREE_PRIVATE (field))
955 access = access_private;
956 else if (TREE_PROTECTED (field))
957 access = access_protected;
959 access = access_public;
962 if (access == access_public && via_protected)
963 access = access_protected;
965 if (access == access_protected && protected_ok)
966 access = access_public;
969 if (access == access_public)
970 DECL_PUBLIC (field) = 1;
971 else if (access == access_protected)
972 DECL_PROTECTED (field) = 1;
973 else if (access == access_private)
974 DECL_PRIVATE (field) = 1;
975 else my_friendly_abort (96);
980 /* Routine to see if the sub-object denoted by the binfo PARENT can be
981 found as a base class and sub-object of the object denoted by
982 BINFO. This routine relies upon binfos not being shared, except
983 for binfos for virtual bases. */
985 is_subobject_of_p (parent, binfo)
988 tree binfos = BINFO_BASETYPES (binfo);
989 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
994 /* Process and/or queue base types. */
995 for (i = 0; i < n_baselinks; i++)
997 tree base_binfo = TREE_VEC_ELT (binfos, i);
998 if (TREE_VIA_VIRTUAL (base_binfo))
999 base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
1000 if (is_subobject_of_p (parent, base_binfo))
1006 /* See if a one FIELD_DECL hides another. This routine is meant to
1007 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1008 binfos given are the binfos corresponding to the particular places
1009 the FIELD_DECLs are found. This routine relies upon binfos not
1010 being shared, except for virtual bases. */
1012 hides (hider_binfo, hidee_binfo)
1013 tree hider_binfo, hidee_binfo;
1015 /* hider hides hidee, if hider has hidee as a base class and
1016 the instance of hidee is a sub-object of hider. The first
1017 part is always true is the second part is true.
1019 When hider and hidee are the same (two ways to get to the exact
1020 same member) we consider either one as hiding the other. */
1021 return is_subobject_of_p (hidee_binfo, hider_binfo);
1024 /* Very similar to lookup_fnfields_1 but it ensures that at least one
1025 function was declared inside the class given by TYPE. It really should
1026 only return functions that match the given TYPE. */
1028 lookup_fnfields_here (type, name)
1031 int index = lookup_fnfields_1 (type, name);
1036 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
1039 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (fndecls))
1040 == TYPE_MAIN_VARIANT (type))
1042 fndecls = TREE_CHAIN (fndecls);
1047 /* Look for a field named NAME in an inheritance lattice dominated by
1048 XBASETYPE. PROTECT is zero if we can avoid computing access
1049 information, otherwise it is 1. WANT_TYPE is 1 when we should only
1050 return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
1052 It was not clear what should happen if WANT_TYPE is set, and an
1053 ambiguity is found. At least one use (lookup_name) to not see
1056 lookup_field (xbasetype, name, protect, want_type)
1057 register tree xbasetype, name;
1058 int protect, want_type;
1060 int head = 0, tail = 0;
1061 tree rval, rval_binfo = NULL_TREE, rval_binfo_h;
1062 tree type, basetype_chain, basetype_path;
1063 enum access_type this_v = access_default;
1064 tree entry, binfo, binfo_h;
1065 enum access_type own_access = access_default;
1066 int vbase_name_p = VBASE_NAME_P (name);
1068 /* rval_binfo is the binfo associated with the found member, note,
1069 this can be set with useful information, even when rval is not
1070 set, because it must deal with ALL members, not just non-function
1071 members. It is used for ambiguity checking and the hidden
1072 checks. Whereas rval is only set if a proper (not hidden)
1073 non-function member is found. */
1075 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1076 hiding checks, as virtual base classes may not be shared. The strategy
1077 is we always go into the the binfo hierarchy owned by TYPE_BINFO of
1078 virtual base classes, as we cross virtual base class lines. This way
1079 we know that binfo of a virtual base class will always == itself when
1080 found along any line. (mrs) */
1084 /* Set this to nonzero if we don't know how to compute
1085 accurate error messages for access control. */
1086 int index = MEMOIZED_HASH_FN (name);
1088 /* If we are looking for a constructor in a templated type, use the
1089 unspecialized name, as that is how we store it. */
1090 if (IDENTIFIER_TEMPLATE (name))
1091 name = constructor_name (name);
1093 if (TREE_CODE (xbasetype) == TREE_VEC)
1095 type = BINFO_TYPE (xbasetype);
1096 basetype_path = xbasetype;
1098 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1101 basetype_path = TYPE_BINFO (xbasetype);
1102 BINFO_VIA_PUBLIC (basetype_path) = 1;
1103 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1105 else my_friendly_abort (97);
1107 if (CLASSTYPE_MTABLE_ENTRY (type))
1109 tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
1111 while (tem && TREE_PURPOSE (tem) != name)
1113 memoized_fields_searched[0]++;
1114 tem = TREE_CHAIN (tem);
1118 if (protect && TREE_TYPE (tem))
1120 error (TREE_STRING_POINTER (TREE_TYPE (tem)),
1121 IDENTIFIER_POINTER (name),
1122 TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
1123 return error_mark_node;
1125 if (TREE_VALUE (tem) == NULL_TREE)
1126 memoized_fast_rejects[0] += 1;
1128 memoized_fast_finds[0] += 1;
1129 return TREE_VALUE (tem);
1133 #ifdef GATHER_STATISTICS
1134 n_calls_lookup_field++;
1136 if (protect && flag_memoize_lookups && ! global_bindings_p ())
1137 entry = make_memoized_table_entry (type, name, 0);
1141 rval = lookup_field_1 (type, name);
1143 if (rval || lookup_fnfields_here (type, name) >= 0)
1149 if (TREE_CODE (rval) != TYPE_DECL)
1151 rval = purpose_member (name, CLASSTYPE_TAGS (type));
1153 rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
1158 if (TREE_CODE (rval) == TYPE_DECL
1159 && lookup_fnfields_here (type, name) >= 0)
1164 if (protect && rval)
1166 if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
1167 this_v = compute_access (basetype_path, rval);
1168 if (TREE_CODE (rval) == CONST_DECL)
1170 if (this_v == access_private)
1171 errstr = "enum `%D' is a private value of class `%T'";
1172 else if (this_v == access_protected)
1173 errstr = "enum `%D' is a protected value of class `%T'";
1177 if (this_v == access_private)
1178 errstr = "member `%D' is a private member of class `%T'";
1179 else if (this_v == access_protected)
1180 errstr = "member `%D' is a protected member of class `%T'";
1188 /* This depends on behavior of lookup_field_1! */
1189 tree error_string = my_build_string (errstr);
1190 TREE_TYPE (entry) = error_string;
1194 /* Let entry know there is no problem with this access. */
1195 TREE_TYPE (entry) = NULL_TREE;
1197 TREE_VALUE (entry) = rval;
1200 if (errstr && protect)
1202 cp_error (errstr, name, type);
1203 return error_mark_node;
1208 basetype_chain = build_tree_list (NULL_TREE, basetype_path);
1209 TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
1210 TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
1211 TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
1213 /* The ambiguity check relies upon breadth first searching. */
1215 search_stack = push_search_level (search_stack, &search_obstack);
1216 binfo = basetype_path;
1221 tree binfos = BINFO_BASETYPES (binfo);
1222 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1225 /* Process and/or queue base types. */
1226 for (i = 0; i < n_baselinks; i++)
1228 tree base_binfo = TREE_VEC_ELT (binfos, i);
1229 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
1233 SET_BINFO_FIELDS_MARKED (base_binfo);
1234 btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
1235 TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
1236 TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
1237 TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
1238 if (TREE_VIA_VIRTUAL (base_binfo))
1239 btypes = tree_cons (NULL_TREE,
1240 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1243 btypes = tree_cons (NULL_TREE,
1244 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1246 obstack_ptr_grow (&search_obstack, btypes);
1248 if (tail >= search_stack->limit)
1249 my_friendly_abort (98);
1253 /* Process head of queue, if one exists. */
1257 basetype_chain = search_stack->first[head++];
1258 binfo_h = TREE_VALUE (basetype_chain);
1259 basetype_chain = TREE_CHAIN (basetype_chain);
1260 basetype_path = TREE_VALUE (basetype_chain);
1261 if (TREE_CHAIN (basetype_chain))
1262 BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
1264 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1266 binfo = basetype_path;
1267 type = BINFO_TYPE (binfo);
1269 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1270 and we do find NAME in TYPE, verify that such a second
1271 sighting is in fact valid. */
1273 nval = lookup_field_1 (type, name);
1275 if (nval || lookup_fnfields_here (type, name)>=0)
1277 if (nval && nval == rval && SHARED_MEMBER_P (nval))
1279 /* This is ok, the member found is the same [class.ambig] */
1281 else if (rval_binfo && hides (rval_binfo_h, binfo_h))
1283 /* This is ok, the member found is in rval_binfo, not
1286 else if (rval_binfo==NULL_TREE || hides (binfo_h, rval_binfo_h))
1288 /* This is ok, the member found is here (binfo), not in
1293 if (entry || protect)
1294 this_v = compute_access (basetype_path, rval);
1295 /* These may look ambiguous, but they really are not. */
1301 /* Undo finding it before, as something else hides it. */
1305 rval_binfo_h = binfo_h;
1309 /* This is ambiguous. */
1310 errstr = "request for member `%D' is ambiguous";
1317 tree *tp = search_stack->first;
1318 tree *search_tail = tp + tail;
1321 TREE_VALUE (entry) = rval;
1325 type = BINFO_TYPE (rval_binfo);
1331 if (TREE_CODE (rval) != TYPE_DECL)
1333 rval = purpose_member (name, CLASSTYPE_TAGS (type));
1335 rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
1340 if (TREE_CODE (rval) == TYPE_DECL
1341 && lookup_fnfields_here (type, name) >= 0)
1347 if (rval == NULL_TREE)
1350 /* If this FIELD_DECL defines its own access level, deal with that. */
1351 if (rval && errstr == 0
1352 && ((protect&1) || entry)
1353 && DECL_LANG_SPECIFIC (rval)
1354 && DECL_ACCESS (rval))
1356 while (tp < search_tail)
1358 /* If is possible for one of the derived types on the path to
1359 have defined special access for this field. Look for such
1360 declarations and report an error if a conflict is found. */
1361 enum access_type new_v;
1363 if (this_v != access_default)
1364 new_v = compute_access (TREE_VALUE (TREE_CHAIN (*tp)), rval);
1365 if (this_v != access_default && new_v != this_v)
1367 errstr = "conflicting access to member `%D'";
1368 this_v = access_default;
1371 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1377 while (tp < search_tail)
1379 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1384 search_stack = pop_search_level (search_stack);
1388 if (own_access == access_private)
1389 errstr = "member `%D' declared private";
1390 else if (own_access == access_protected)
1391 errstr = "member `%D' declared protected";
1392 else if (this_v == access_private)
1393 errstr = TREE_PRIVATE (rval)
1394 ? "member `%D' is private"
1395 : "member `%D' is from private base class";
1396 else if (this_v == access_protected)
1397 errstr = TREE_PROTECTED (rval)
1398 ? "member `%D' is protected"
1399 : "member `%D' is from protected base class";
1406 tree error_string = my_build_string (errstr);
1407 /* Save error message with entry. */
1408 TREE_TYPE (entry) = error_string;
1412 /* Mark entry as having no error string. */
1413 TREE_TYPE (entry) = NULL_TREE;
1417 if (errstr && protect)
1419 cp_error (errstr, name, type);
1420 rval = error_mark_node;
1425 /* Try to find NAME inside a nested class. */
1427 lookup_nested_field (name, complain)
1433 tree id = NULL_TREE;
1434 if (TREE_CHAIN (current_class_type))
1436 /* Climb our way up the nested ladder, seeing if we're trying to
1437 modify a field in an enclosing class. If so, we should only
1438 be able to modify if it's static. */
1439 for (t = TREE_CHAIN (current_class_type);
1440 t && DECL_CONTEXT (t);
1441 t = TREE_CHAIN (DECL_CONTEXT (t)))
1443 if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE)
1446 /* N.B.: lookup_field will do the access checking for us */
1447 id = lookup_field (DECL_CONTEXT (t), name, complain, 0);
1448 if (id == error_mark_node)
1454 if (id != NULL_TREE)
1456 if (TREE_CODE (id) == FIELD_DECL
1457 && ! TREE_STATIC (id)
1458 && TREE_TYPE (id) != error_mark_node)
1462 /* At parse time, we don't want to give this error, since
1463 we won't have enough state to make this kind of
1464 decision properly. But there are times (e.g., with
1465 enums in nested classes) when we do need to call
1466 this fn at parse time. So, in those cases, we pass
1467 complain as a 0 and just return a NULL_TREE. */
1468 error ("assignment to non-static member `%s' of enclosing class `%s'",
1469 lang_printable_name (id),
1470 IDENTIFIER_POINTER (TYPE_IDENTIFIER
1471 (DECL_CONTEXT (t))));
1472 /* Mark this for do_identifier(). It would otherwise
1473 claim that the variable was undeclared. */
1474 TREE_TYPE (id) = error_mark_node;
1490 /* TYPE is a class type. Return the index of the fields within
1491 the method vector with name NAME, or -1 is no such field exists. */
1493 lookup_fnfields_1 (type, name)
1496 register tree method_vec = CLASSTYPE_METHOD_VEC (type);
1498 if (method_vec != 0)
1500 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1501 register tree *end = TREE_VEC_END (method_vec);
1503 #ifdef GATHER_STATISTICS
1504 n_calls_lookup_fnfields_1++;
1506 if (*methods && name == constructor_name (type))
1509 while (++methods != end)
1511 #ifdef GATHER_STATISTICS
1512 n_outer_fields_searched++;
1514 if (DECL_NAME (*methods) == name)
1518 return methods - &TREE_VEC_ELT (method_vec, 0);
1524 /* Starting from BASETYPE, return a TREE_BASELINK-like object
1525 which gives the following information (in a list):
1527 TREE_TYPE: list of basetypes needed to get to...
1528 TREE_VALUE: list of all functions in of given type
1529 which have name NAME.
1531 No access information is computed by this function,
1532 other then to adorn the list of basetypes with
1535 If there are two ways to find a name (two members), if COMPLAIN is
1536 non-zero, then error_mark_node is returned, and an error message is
1537 printed, otherwise, just an error_mark_node is returned.
1539 As a special case, is COMPLAIN is -1, we don't complain, and we
1540 don't return error_mark_node, but rather the complete list of
1541 virtuals. This is used by get_virtuals_named_this. */
1543 lookup_fnfields (basetype_path, name, complain)
1544 tree basetype_path, name;
1547 int head = 0, tail = 0;
1548 tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE, rval_binfo_h;
1549 tree entry, binfo, basetype_chain, binfo_h;
1552 /* rval_binfo is the binfo associated with the found member, note,
1553 this can be set with useful information, even when rval is not
1554 set, because it must deal with ALL members, not just function
1555 members. It is used for ambiguity checking and the hidden
1556 checks. Whereas rval is only set if a proper (not hidden)
1557 function member is found. */
1559 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1560 hiding checks, as virtual base classes may not be shared. The strategy
1561 is we always go into the the binfo hierarchy owned by TYPE_BINFO of
1562 virtual base classes, as we cross virtual base class lines. This way
1563 we know that binfo of a virtual base class will always == itself when
1564 found along any line. (mrs) */
1566 /* For now, don't try this. */
1567 int protect = complain;
1571 /* Set this to nonzero if we don't know how to compute
1572 accurate error messages for access control. */
1573 int index = MEMOIZED_HASH_FN (name);
1578 protect = complain = 0;
1581 /* If we are looking for a constructor in a templated type, use the
1582 unspecialized name, as that is how we store it. */
1583 if (IDENTIFIER_TEMPLATE (name))
1584 name = constructor_name (name);
1586 binfo = basetype_path;
1588 type = BINFO_TYPE (basetype_path);
1590 /* The memoization code is in need of maintenance. */
1591 if (!find_all && CLASSTYPE_MTABLE_ENTRY (type))
1593 tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
1595 while (tem && TREE_PURPOSE (tem) != name)
1597 memoized_fields_searched[1]++;
1598 tem = TREE_CHAIN (tem);
1602 if (protect && TREE_TYPE (tem))
1604 error (TREE_STRING_POINTER (TREE_TYPE (tem)),
1605 IDENTIFIER_POINTER (name),
1606 TYPE_NAME_STRING (DECL_CLASS_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
1607 return error_mark_node;
1609 if (TREE_VALUE (tem) == NULL_TREE)
1611 memoized_fast_rejects[1] += 1;
1616 /* Want to return this, but we must make sure
1617 that access information is consistent. */
1618 tree baselink = TREE_VALUE (tem);
1619 tree memoized_basetypes = TREE_PURPOSE (baselink);
1620 tree these_basetypes = basetype_path;
1621 while (memoized_basetypes && these_basetypes)
1623 memoized_fields_searched[1]++;
1624 if (TREE_VALUE (memoized_basetypes) != these_basetypes)
1626 memoized_basetypes = TREE_CHAIN (memoized_basetypes);
1627 these_basetypes = BINFO_INHERITANCE_CHAIN (these_basetypes);
1629 /* The following statement is true only when both are NULL. */
1630 if (memoized_basetypes == these_basetypes)
1632 memoized_fast_finds[1] += 1;
1633 return TREE_VALUE (tem);
1635 /* else, we must re-find this field by hand. */
1636 baselink = tree_cons (basetype_path, TREE_VALUE (baselink), TREE_CHAIN (baselink));
1642 #ifdef GATHER_STATISTICS
1643 n_calls_lookup_fnfields++;
1645 if (protect && flag_memoize_lookups && ! global_bindings_p ())
1646 entry = make_memoized_table_entry (type, name, 1);
1650 index = lookup_fnfields_here (type, name);
1651 if (index >= 0 || lookup_field_1 (type, name))
1653 rval_binfo = basetype_path;
1654 rval_binfo_h = rval_binfo;
1659 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
1660 rvals = my_tree_cons (basetype_path, rval, rvals);
1661 if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type))
1662 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
1666 TREE_VALUE (entry) = rvals;
1667 TREE_TYPE (entry) = NULL_TREE;
1674 if (basetype_path == TYPE_BINFO (type))
1676 basetype_chain = CLASSTYPE_BINFO_AS_LIST (type);
1677 TREE_VIA_PUBLIC (basetype_chain) = 1;
1678 BINFO_VIA_PUBLIC (basetype_path) = 1;
1679 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1683 basetype_chain = build_tree_list (NULL_TREE, basetype_path);
1684 TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
1685 TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
1686 TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
1689 /* The ambiguity check relies upon breadth first searching. */
1691 search_stack = push_search_level (search_stack, &search_obstack);
1692 binfo = basetype_path;
1697 tree binfos = BINFO_BASETYPES (binfo);
1698 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1701 /* Process and/or queue base types. */
1702 for (i = 0; i < n_baselinks; i++)
1704 tree base_binfo = TREE_VEC_ELT (binfos, i);
1705 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
1709 SET_BINFO_FIELDS_MARKED (base_binfo);
1710 btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
1711 TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
1712 TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
1713 TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
1714 if (TREE_VIA_VIRTUAL (base_binfo))
1715 btypes = tree_cons (NULL_TREE,
1716 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1719 btypes = tree_cons (NULL_TREE,
1720 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1722 obstack_ptr_grow (&search_obstack, btypes);
1724 if (tail >= search_stack->limit)
1725 my_friendly_abort (99);
1729 /* Process head of queue, if one exists. */
1733 basetype_chain = search_stack->first[head++];
1734 binfo_h = TREE_VALUE (basetype_chain);
1735 basetype_chain = TREE_CHAIN (basetype_chain);
1736 basetype_path = TREE_VALUE (basetype_chain);
1737 if (TREE_CHAIN (basetype_chain))
1738 BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
1740 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1742 binfo = basetype_path;
1743 type = BINFO_TYPE (binfo);
1745 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1746 and we do find NAME in TYPE, verify that such a second
1747 sighting is in fact valid. */
1749 index = lookup_fnfields_here (type, name);
1751 if (index >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all))
1753 if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h))
1755 /* This is ok, the member found is in rval_binfo, not
1758 else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h))
1760 /* This is ok, the member found is here (binfo), not in
1764 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
1765 /* Note, rvals can only be previously set if find_all is
1767 rvals = my_tree_cons (basetype_path, rval, rvals);
1768 if (TYPE_BINFO_BASETYPES (type)
1769 && CLASSTYPE_BASELINK_VEC (type))
1770 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
1774 /* Undo finding it before, as something else hides it. */
1779 rval_binfo_h = binfo_h;
1783 /* This is ambiguous. */
1784 errstr = "request for method `%D' is ambiguous";
1785 rvals = error_mark_node;
1791 tree *tp = search_stack->first;
1792 tree *search_tail = tp + tail;
1794 while (tp < search_tail)
1796 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1800 search_stack = pop_search_level (search_stack);
1806 tree error_string = my_build_string (errstr);
1807 /* Save error message with entry. */
1808 TREE_TYPE (entry) = error_string;
1812 /* Mark entry as having no error string. */
1813 TREE_TYPE (entry) = NULL_TREE;
1814 TREE_VALUE (entry) = rvals;
1818 if (errstr && protect)
1820 cp_error (errstr, name);
1821 rvals = error_mark_node;
1827 /* BREADTH-FIRST SEARCH ROUTINES. */
1829 /* Search a multiple inheritance hierarchy by breadth-first search.
1831 TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
1832 TESTFN is a function, which, if true, means that our condition has been met,
1833 and its return value should be returned.
1834 QFN, if non-NULL, is a predicate dictating whether the type should
1838 breadth_first_search (binfo, testfn, qfn)
1843 int head = 0, tail = 0;
1846 search_stack = push_search_level (search_stack, &search_obstack);
1850 tree binfos = BINFO_BASETYPES (binfo);
1851 int n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1854 /* Process and/or queue base types. */
1855 for (i = 0; i < n_baselinks; i++)
1857 tree base_binfo = TREE_VEC_ELT (binfos, i);
1859 if (BINFO_MARKED (base_binfo) == 0
1860 && (qfn == 0 || (*qfn) (binfo, i)))
1862 SET_BINFO_MARKED (base_binfo);
1863 obstack_ptr_grow (&search_obstack, binfo);
1864 obstack_ptr_grow (&search_obstack, (HOST_WIDE_INT) i);
1866 if (tail >= search_stack->limit)
1867 my_friendly_abort (100);
1870 /* Process head of queue, if one exists. */
1877 binfo = search_stack->first[head++];
1878 i = (HOST_WIDE_INT) search_stack->first[head++];
1879 if (rval = (*testfn) (binfo, i))
1881 binfo = BINFO_BASETYPE (binfo, i);
1884 tree *tp = search_stack->first;
1885 tree *search_tail = tp + tail;
1886 while (tp < search_tail)
1889 int i = (HOST_WIDE_INT)(*tp++);
1890 CLEAR_BINFO_MARKED (BINFO_BASETYPE (binfo, i));
1894 search_stack = pop_search_level (search_stack);
1898 /* Functions to use in breadth first searches. */
1899 typedef tree (*pft)();
1900 typedef int (*pfi)();
1902 int tree_needs_constructor_p (binfo, i)
1907 my_friendly_assert (i != 0, 296);
1908 basetype = BINFO_TYPE (BINFO_BASETYPE (binfo, i));
1909 return TYPE_NEEDS_CONSTRUCTING (basetype);
1912 static tree declarator;
1915 get_virtuals_named_this (binfo)
1920 fields = lookup_fnfields (binfo, declarator, -1);
1921 /* fields cannot be error_mark_node */
1926 /* Get to the function decls, and return the first virtual function
1927 with this name, if there is one. */
1932 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = DECL_CHAIN (fndecl))
1933 if (DECL_VINDEX (fndecl))
1935 fields = next_baselink (fields);
1940 static tree get_virtual_destructor (binfo, i)
1944 tree type = BINFO_TYPE (binfo);
1946 type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
1947 if (TYPE_HAS_DESTRUCTOR (type)
1948 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
1949 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1953 int tree_has_any_destructor_p (binfo, i)
1957 tree type = BINFO_TYPE (binfo);
1959 type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
1960 return TYPE_NEEDS_DESTRUCTOR (type);
1963 /* Given a class type TYPE, and a function decl FNDECL, look for a
1964 virtual function in TYPE's hierarchy which FNDECL could match as a
1965 virtual function. It doesn't matter which one we find.
1967 DTORP is nonzero if we are looking for a destructor. Destructors
1968 need special treatment because they do not match by name. */
1970 get_matching_virtual (binfo, fndecl, dtorp)
1974 tree tmp = NULL_TREE;
1976 /* Breadth first search routines start searching basetypes
1977 of TYPE, so we must perform first ply of search here. */
1980 if (tree_has_any_destructor_p (binfo, -1))
1981 tmp = get_virtual_destructor (binfo, -1);
1986 tmp = (tree) breadth_first_search (binfo,
1987 (pfi) get_virtual_destructor,
1988 tree_has_any_destructor_p);
1993 tree drettype, dtypes, btypes, instptr_type;
1994 tree basetype = DECL_CLASS_CONTEXT (fndecl);
1995 tree baselink, best = NULL_TREE;
1996 tree name = DECL_ASSEMBLER_NAME (fndecl);
1998 declarator = DECL_NAME (fndecl);
1999 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
2002 baselink = get_virtuals_named_this (binfo);
2003 if (baselink == NULL_TREE)
2006 drettype = TREE_TYPE (TREE_TYPE (fndecl));
2007 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2008 if (DECL_STATIC_FUNCTION_P (fndecl))
2009 instptr_type = NULL_TREE;
2011 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
2013 for (; baselink; baselink = next_baselink (baselink))
2015 for (tmp = TREE_VALUE (baselink); tmp; tmp = DECL_CHAIN (tmp))
2017 if (! DECL_VINDEX (tmp))
2020 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
2021 if (instptr_type == NULL_TREE)
2023 if (compparms (TREE_CHAIN (btypes), dtypes, 3))
2024 /* Caller knows to give error in this case. */
2029 if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
2030 == TYPE_READONLY (instptr_type))
2031 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3))
2033 tree brettype = TREE_TYPE (TREE_TYPE (tmp));
2034 if (comptypes (brettype, drettype, 1))
2037 (TREE_CODE (brettype) == TREE_CODE (drettype)
2038 && (TREE_CODE (brettype) == POINTER_TYPE
2039 || TREE_CODE (brettype) == REFERENCE_TYPE)
2040 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (brettype)),
2041 TYPE_MAIN_VARIANT (TREE_TYPE (drettype)),
2043 /* covariant return type */
2045 tree b = TREE_TYPE (brettype), d = TREE_TYPE (drettype);
2046 if (TYPE_MAIN_VARIANT (b) != TYPE_MAIN_VARIANT (d))
2048 tree binfo = get_binfo (b, d, 1);
2049 if (binfo != error_mark_node
2050 && ! BINFO_OFFSET_ZEROP (binfo))
2051 sorry ("adjusting pointers for covariant returns");
2053 if (TYPE_READONLY (d) > TYPE_READONLY (b))
2055 cp_error ("return type of `%#D' adds const", fndecl);
2056 cp_error_at (" overriding definition as `%#D'",
2059 else if (TYPE_VOLATILE (d) > TYPE_VOLATILE (b))
2061 cp_error ("return type of `%#D' adds volatile",
2063 cp_error_at (" overriding definition as `%#D'",
2067 else if (IS_AGGR_TYPE_2 (brettype, drettype)
2068 && comptypes (brettype, drettype, 0))
2070 error ("invalid covariant return type (must use pointer or reference)");
2071 cp_error_at (" overriding `%#D'", tmp);
2072 cp_error (" with `%#D'", fndecl);
2074 else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE)
2076 cp_error ("conflicting return type specified for virtual function `%#D'", fndecl);
2077 cp_error_at (" overriding definition as `%#D'", tmp);
2078 SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
2089 if (best == NULL_TREE && warn_overloaded_virtual)
2090 cp_warning_at ("conflicting specification deriving virtual function `%D'", fndecl);
2096 /* Return the list of virtual functions which are abstract in type
2097 TYPE that come from non virtual base classes. See
2098 expand_direct_vtbls_init for the style of search we do. */
2100 get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
2101 tree binfo, abstract_virtuals;
2104 tree binfos = BINFO_BASETYPES (binfo);
2105 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2107 for (i = 0; i < n_baselinks; i++)
2109 tree base_binfo = TREE_VEC_ELT (binfos, i);
2110 int is_not_base_vtable =
2111 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2112 if (! TREE_VIA_VIRTUAL (base_binfo))
2114 = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
2117 /* Should we use something besides CLASSTYPE_VFIELDS? */
2118 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2120 /* Get around first entry reserved for RTTI. */
2121 tree tmp = TREE_CHAIN (BINFO_VIRTUALS (binfo));
2125 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
2126 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
2127 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
2128 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
2129 tmp = TREE_CHAIN (tmp);
2132 return abstract_virtuals;
2135 /* Return the list of virtual functions which are abstract in type TYPE.
2136 This information is cached, and so must be built on a
2137 non-temporary obstack. */
2139 get_abstract_virtuals (type)
2143 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
2145 /* First get all from non-virtual bases. */
2147 = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
2149 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
2151 if (! BINFO_VIRTUALS (vbases))
2154 tmp = TREE_CHAIN (BINFO_VIRTUALS (vbases));
2157 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
2158 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
2159 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
2160 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
2161 tmp = TREE_CHAIN (tmp);
2164 return nreverse (abstract_virtuals);
2167 /* For the type TYPE, return a list of member functions available from
2168 base classes with name NAME. The TREE_VALUE of the list is a chain of
2169 member functions with name NAME. The TREE_PURPOSE of the list is a
2170 basetype, or a list of base types (in reverse order) which were
2171 traversed to reach the chain of member functions. If we reach a base
2172 type which provides a member function of name NAME, and which has at
2173 most one base type itself, then we can terminate the search. */
2176 get_baselinks (type_as_binfo_list, type, name)
2177 tree type_as_binfo_list;
2180 int head = 0, tail = 0, index;
2181 tree rval = 0, nval = 0;
2182 tree basetypes = type_as_binfo_list;
2183 tree binfo = TYPE_BINFO (type);
2185 search_stack = push_search_level (search_stack, &search_obstack);
2189 tree binfos = BINFO_BASETYPES (binfo);
2190 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2192 /* Process and/or queue base types. */
2193 for (i = 0; i < n_baselinks; i++)
2195 tree base_binfo = TREE_VEC_ELT (binfos, i);
2198 btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo),
2199 TREE_VIA_VIRTUAL (base_binfo),
2200 TREE_VIA_PROTECTED (base_binfo),
2201 NULL_TREE, base_binfo,
2203 obstack_ptr_grow (&search_obstack, btypes);
2204 search_stack->first = (tree *)obstack_base (&search_obstack);
2209 /* Process head of queue, if one exists. */
2213 basetypes = search_stack->first[head++];
2214 binfo = TREE_VALUE (basetypes);
2215 type = BINFO_TYPE (binfo);
2216 index = lookup_fnfields_1 (type, name);
2219 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
2220 rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval);
2221 if (TYPE_BINFO_BASETYPES (type) == 0)
2223 else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1)
2225 if (CLASSTYPE_BASELINK_VEC (type))
2226 TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
2233 search_stack = pop_search_level (search_stack);
2238 next_baselink (baselink)
2241 tree tmp = TREE_TYPE (baselink);
2242 baselink = TREE_CHAIN (baselink);
2245 /* @@ does not yet add previous base types. */
2246 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2248 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2249 tmp = TREE_CHAIN (tmp);
2254 /* DEPTH-FIRST SEARCH ROUTINES. */
2256 /* Assign unique numbers to _CLASSTYPE members of the lattice
2257 specified by TYPE. The root nodes are marked first; the nodes
2258 are marked depth-fisrt, left-right. */
2262 /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
2263 Relation yields 1 if C1 <= C2, 0 otherwise. */
2264 typedef char mi_boolean;
2265 static mi_boolean *mi_matrix;
2267 /* Type for which this matrix is defined. */
2268 static tree mi_type;
2270 /* Size of the matrix for indexing purposes. */
2273 /* Return nonzero if class C2 derives from class C1. */
2274 #define BINFO_DERIVES_FROM(C1, C2) \
2275 ((mi_matrix+mi_size*(BINFO_CID (C1)-1))[BINFO_CID (C2)-1])
2276 #define TYPE_DERIVES_FROM(C1, C2) \
2277 ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
2278 #define BINFO_DERIVES_FROM_STAR(C) \
2279 (mi_matrix+(BINFO_CID (C)-1))
2281 /* This routine converts a pointer to be a pointer of an immediate
2282 base class. The normal convert_pointer_to routine would diagnose
2283 the conversion as ambiguous, under MI code that has the base class
2284 as an ambiguous base class. */
2286 convert_pointer_to_single_level (to_type, expr)
2289 tree binfo_of_derived;
2292 binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)));
2293 last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0);
2294 BINFO_INHERITANCE_CHAIN (last) = binfo_of_derived;
2295 BINFO_INHERITANCE_CHAIN (binfo_of_derived) = NULL_TREE;
2296 return build_vbase_path (PLUS_EXPR, TYPE_POINTER_TO (to_type), expr, last, 1);
2299 /* The main function which implements depth first search.
2301 This routine has to remember the path it walked up, when
2302 dfs_init_vbase_pointers is the work function, as otherwise there
2303 would be no record. */
2305 dfs_walk (binfo, fn, qfn)
2310 tree binfos = BINFO_BASETYPES (binfo);
2311 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2313 for (i = 0; i < n_baselinks; i++)
2315 tree base_binfo = TREE_VEC_ELT (binfos, i);
2317 if (qfn == 0 || (*qfn)(base_binfo))
2319 if (fn == dfs_init_vbase_pointers)
2321 /* When traversing an arbitrary MI hierarchy, we need to keep
2322 a record of the path we took to get down to the final base
2323 type, as otherwise there would be no record of it, and just
2324 trying to blindly convert at the bottom would be ambiguous.
2326 The easiest way is to do the conversions one step at a time,
2327 as we know we want the immediate base class at each step.
2329 The only special trick to converting one step at a time,
2330 is that when we hit the last virtual base class, we must
2331 use the SLOT value for it, and not use the normal convert
2332 routine. We use the last virtual base class, as in our
2333 implementation, we have pointers to all virtual base
2334 classes in the base object. */
2336 tree saved_vbase_decl_ptr_intermediate
2337 = vbase_decl_ptr_intermediate;
2339 if (TREE_VIA_VIRTUAL (base_binfo))
2341 /* No need for the conversion here, as we know it is the
2343 vbase_decl_ptr_intermediate
2344 = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo));
2348 vbase_decl_ptr_intermediate
2349 = convert_pointer_to_single_level (BINFO_TYPE (base_binfo),
2350 vbase_decl_ptr_intermediate);
2353 dfs_walk (base_binfo, fn, qfn);
2355 vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate;
2357 dfs_walk (base_binfo, fn, qfn);
2364 /* Predicate functions which serve for dfs_walk. */
2365 static int numberedp (binfo) tree binfo;
2366 { return BINFO_CID (binfo); }
2367 static int unnumberedp (binfo) tree binfo;
2368 { return BINFO_CID (binfo) == 0; }
2370 static int markedp (binfo) tree binfo;
2371 { return BINFO_MARKED (binfo); }
2372 static int bfs_markedp (binfo, i) tree binfo; int i;
2373 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); }
2374 static int unmarkedp (binfo) tree binfo;
2375 { return BINFO_MARKED (binfo) == 0; }
2376 static int bfs_unmarkedp (binfo, i) tree binfo; int i;
2377 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2378 static int marked_vtable_pathp (binfo) tree binfo;
2379 { return BINFO_VTABLE_PATH_MARKED (binfo); }
2380 static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i;
2381 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); }
2382 static int unmarked_vtable_pathp (binfo) tree binfo;
2383 { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; }
2384 static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i;
2385 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2386 static int marked_new_vtablep (binfo) tree binfo;
2387 { return BINFO_NEW_VTABLE_MARKED (binfo); }
2388 static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i;
2389 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); }
2390 static int unmarked_new_vtablep (binfo) tree binfo;
2391 { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; }
2392 static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i;
2393 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2395 static int dfs_search_slot_nonempty_p (binfo) tree binfo;
2396 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
2398 static int dfs_debug_unmarkedp (binfo) tree binfo;
2399 { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; }
2401 /* The worker functions for `dfs_walk'. These do not need to
2402 test anything (vis a vis marking) if they are paired with
2403 a predicate function (above). */
2405 /* Assign each type within the lattice a number which is unique
2406 in the lattice. The first number assigned is 1. */
2412 BINFO_CID (binfo) = ++cid;
2416 dfs_unnumber (binfo)
2419 BINFO_CID (binfo) = 0;
2423 dfs_mark (binfo) tree binfo;
2424 { SET_BINFO_MARKED (binfo); }
2427 dfs_unmark (binfo) tree binfo;
2428 { CLEAR_BINFO_MARKED (binfo); }
2431 dfs_mark_vtable_path (binfo) tree binfo;
2432 { SET_BINFO_VTABLE_PATH_MARKED (binfo); }
2435 dfs_unmark_vtable_path (binfo) tree binfo;
2436 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
2439 dfs_mark_new_vtable (binfo) tree binfo;
2440 { SET_BINFO_NEW_VTABLE_MARKED (binfo); }
2443 dfs_unmark_new_vtable (binfo) tree binfo;
2444 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
2447 dfs_clear_search_slot (binfo) tree binfo;
2448 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
2451 dfs_debug_mark (binfo)
2454 tree t = BINFO_TYPE (binfo);
2456 /* Use heuristic that if there are virtual functions,
2457 ignore until we see a non-inline virtual function. */
2458 tree methods = CLASSTYPE_METHOD_VEC (t);
2460 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2462 /* If interface info is known, the value of (?@@?) is correct. */
2464 || CLASSTYPE_INTERFACE_KNOWN (t)
2465 || (write_virtuals == 2 && TYPE_VIRTUAL_P (t)))
2468 /* If debug info is requested from this context for this type, supply it.
2469 If debug info is requested from another context for this type,
2470 see if some third context can supply it. */
2471 if (current_function_decl == NULL_TREE
2472 || DECL_CLASS_CONTEXT (current_function_decl) != t)
2474 if (TREE_VEC_ELT (methods, 0))
2475 methods = TREE_VEC_ELT (methods, 0);
2477 methods = TREE_VEC_ELT (methods, 1);
2480 if (DECL_VINDEX (methods)
2481 && DECL_THIS_INLINE (methods) == 0
2482 && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
2484 /* Somebody, somewhere is going to have to define this
2485 virtual function. When they do, they will provide
2486 the debugging info. */
2489 methods = TREE_CHAIN (methods);
2492 /* We cannot rely on some alien method to solve our problems,
2493 so we must write out the debug info ourselves. */
2494 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
2495 rest_of_type_compilation (t, global_bindings_p ());
2498 /* Attach to the type of the virtual base class, the pointer to the
2499 virtual base class, given the global pointer vbase_decl_ptr.
2501 We use the global vbase_types. ICK! */
2503 dfs_find_vbases (binfo)
2506 tree binfos = BINFO_BASETYPES (binfo);
2507 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2509 for (i = n_baselinks-1; i >= 0; i--)
2511 tree base_binfo = TREE_VEC_ELT (binfos, i);
2513 if (TREE_VIA_VIRTUAL (base_binfo)
2514 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
2516 tree vbase = BINFO_TYPE (base_binfo);
2517 tree binfo = binfo_member (vbase, vbase_types);
2519 CLASSTYPE_SEARCH_SLOT (vbase)
2520 = (char *) build (PLUS_EXPR, TYPE_POINTER_TO (vbase),
2521 vbase_decl_ptr, BINFO_OFFSET (binfo));
2524 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2525 SET_BINFO_NEW_VTABLE_MARKED (binfo);
2529 dfs_init_vbase_pointers (binfo)
2532 tree type = BINFO_TYPE (binfo);
2533 tree fields = TYPE_FIELDS (type);
2534 tree this_vbase_ptr;
2536 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2538 /* If there is a rtti, it is the first field, though perhaps from
2539 the base class. Otherwise, the first fields are virtual base class
2541 if (CLASSTYPE_RTTI (type) && VFIELD_NAME_P (DECL_NAME (fields)))
2542 /* Get past vtable for the object. */
2543 fields = TREE_CHAIN (fields);
2545 if (fields == NULL_TREE
2546 || DECL_NAME (fields) == NULL_TREE
2547 || ! VBASE_NAME_P (DECL_NAME (fields)))
2550 this_vbase_ptr = vbase_decl_ptr_intermediate;
2552 if (TYPE_POINTER_TO (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
2553 my_friendly_abort (125);
2555 while (fields && DECL_NAME (fields)
2556 && VBASE_NAME_P (DECL_NAME (fields)))
2558 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2559 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
2560 tree init = (tree)CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
2561 vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
2563 build_modify_expr (ref, NOP_EXPR, init),
2565 fields = TREE_CHAIN (fields);
2569 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2570 times, just NEW_VTABLE, but optimizer should make both with equal
2571 efficiency (though it does not currently). */
2573 dfs_clear_vbase_slots (binfo)
2576 tree type = BINFO_TYPE (binfo);
2577 CLASSTYPE_SEARCH_SLOT (type) = 0;
2578 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2579 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2583 init_vbase_pointers (type, decl_ptr)
2587 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2589 int old_flag = flag_this_is_variable;
2590 tree binfo = TYPE_BINFO (type);
2591 flag_this_is_variable = -2;
2592 vbase_types = CLASSTYPE_VBASECLASSES (type);
2593 vbase_decl_ptr = decl_ptr;
2594 vbase_decl = build_indirect_ref (decl_ptr, NULL_PTR);
2595 vbase_decl_ptr_intermediate = vbase_decl_ptr;
2596 vbase_init_result = NULL_TREE;
2597 dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp);
2598 dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp);
2599 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2600 flag_this_is_variable = old_flag;
2601 return vbase_init_result;
2606 /* get the virtual context (the vbase that directly contains the
2607 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2608 or NULL_TREE if there is none.
2610 FNDECL must come from a virtual table from a virtual base to ensure that
2611 there is only one possible DECL_CLASS_CONTEXT.
2613 We know that if there is more than one place (binfo) the fndecl that the
2614 declared, they all refer to the same binfo. See get_class_offset_1 for
2615 the check that ensures this. */
2617 virtual_context (fndecl, t, vbase)
2618 tree fndecl, t, vbase;
2621 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
2623 /* This shouldn't happen, I don't want errors! */
2624 warning ("recoverable compiler error, fixups for virtual function");
2629 if (TREE_VIA_VIRTUAL (path))
2630 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2631 path = BINFO_INHERITANCE_CHAIN (path);
2636 /* Fixups upcast offsets for one vtable.
2637 Entries may stay within the VBASE given, or
2638 they may upcast into a direct base, or
2639 they may upcast into a different vbase.
2641 We only need to do fixups in case 2 and 3.
2643 This routine mirrors fixup_vtable_deltas in functionality, though
2644 this one is runtime based, and the other is compile time based.
2645 Conceivably that routine could be removed entirely, and all fixups
2648 VBASE_OFFSETS is an association list of virtual bases that contains
2649 offset information, so the offsets are only calculated once. */
2651 expand_upcast_fixups (binfo, addr, orig_addr, vbase, t, vbase_offsets)
2652 tree binfo, addr, orig_addr, vbase, t, *vbase_offsets;
2654 tree virtuals = BINFO_VIRTUALS (binfo);
2657 unsigned HOST_WIDE_INT n;
2659 delta = purpose_member (vbase, *vbase_offsets);
2662 delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
2663 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, addr);
2664 delta = save_expr (delta);
2665 delta = tree_cons (vbase, delta, *vbase_offsets);
2666 *vbase_offsets = delta;
2669 /* Skip RTTI fake object. */
2672 virtuals = TREE_CHAIN (virtuals);
2675 tree current_fndecl = TREE_VALUE (virtuals);
2676 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2677 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2679 && current_fndecl != abort_fndecl
2680 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2682 /* This may in fact need a runtime fixup. */
2683 tree idx = DECL_VINDEX (current_fndecl);
2684 tree vtbl = BINFO_VTABLE (binfo);
2685 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2686 tree aref, ref, naref;
2687 tree old_delta, new_delta;
2690 if (nvtbl == NULL_TREE
2691 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2693 /* Dup it if it isn't in local scope yet. */
2694 nvtbl = build_decl (VAR_DECL,
2696 TYPE_MAIN_VARIANT (TREE_TYPE (BINFO_VTABLE (binfo))));
2697 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2698 DECL_ALIGN (nvtbl));
2699 TREE_READONLY (nvtbl) = 0;
2700 nvtbl = pushdecl (nvtbl);
2702 cp_finish_decl (nvtbl, init, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
2703 DECL_VIRTUAL_P (nvtbl) = 1;
2704 DECL_CONTEXT (nvtbl) = t;
2705 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2707 TREE_SIDE_EFFECTS (init) = 1;
2708 expand_expr_stmt (init);
2709 /* Update the vtable pointers as necessary. */
2710 ref = build_vfield_ref (build_indirect_ref (addr, NULL_PTR), DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
2711 expand_expr_stmt (build_modify_expr (ref, NOP_EXPR,
2712 build_unary_op (ADDR_EXPR, nvtbl, 0)));
2714 assemble_external (vtbl);
2715 aref = build_array_ref (vtbl, idx);
2716 naref = build_array_ref (nvtbl, idx);
2717 old_delta = build_component_ref (aref, delta_identifier, 0, 0);
2718 new_delta = build_component_ref (naref, delta_identifier, 0, 0);
2719 old_delta = build_binary_op (PLUS_EXPR, old_delta,
2720 TREE_VALUE (delta), 0);
2723 /* If this is set, we need to add in delta adjustments for
2724 the other virtual base. */
2725 tree vc_delta = purpose_member (vc, *vbase_offsets);
2728 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
2729 vc_delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
2730 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
2732 vc_delta = save_expr (vc_delta);
2733 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2736 vc_delta = TREE_VALUE (vc_delta);
2738 old_delta = build_binary_op (PLUS_EXPR, old_delta, vc_delta, 0);
2741 TREE_READONLY (new_delta) = 0;
2742 expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
2746 virtuals = TREE_CHAIN (virtuals);
2750 /* Fixup upcast offsets for all direct vtables. Patterned after
2751 expand_direct_vtbls_init. */
2753 fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
2754 tree real_binfo, binfo, addr, orig_addr, type, vbase, *vbase_offsets;
2755 int init_self, can_elide;
2757 tree real_binfos = BINFO_BASETYPES (real_binfo);
2758 tree binfos = BINFO_BASETYPES (binfo);
2759 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
2761 for (i = 0; i < n_baselinks; i++)
2763 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
2764 tree base_binfo = TREE_VEC_ELT (binfos, i);
2765 int is_not_base_vtable =
2766 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
2767 if (! TREE_VIA_VIRTUAL (real_base_binfo))
2768 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
2769 is_not_base_vtable, can_elide, addr,
2770 orig_addr, type, vbase, vbase_offsets);
2773 /* Before turning this on, make sure it is correct. */
2774 if (can_elide && ! BINFO_MODIFIED (binfo))
2777 /* Should we use something besides CLASSTYPE_VFIELDS? */
2778 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
2780 addr = convert_pointer_to_real (binfo, addr);
2781 expand_upcast_fixups (real_binfo, addr, orig_addr, vbase, type, vbase_offsets);
2785 /* Build a COMPOUND_EXPR which when expanded will generate the code
2786 needed to initialize all the virtual function table slots of all
2787 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2788 the virtual baseclasses to use; TYPE is the type of the object to
2789 which the initialization applies. TRUE_EXP is the true object we
2790 are initializing, and DECL_PTR is the pointer to the sub-object we
2793 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
2794 object was laid out by a top-level constructor and the computed
2795 offsets are valid to store vtables. When zero, we must store new
2796 vtables through virtual baseclass pointers.
2798 We setup and use the globals: vbase_decl, vbase_decl_ptr, vbase_types
2802 expand_indirect_vtbls_init (binfo, true_exp, decl_ptr, use_computed_offsets)
2804 tree true_exp, decl_ptr;
2805 int use_computed_offsets;
2807 tree type = BINFO_TYPE (binfo);
2808 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2810 rtx fixup_insns = NULL_RTX;
2811 int old_flag = flag_this_is_variable;
2812 tree vbases = CLASSTYPE_VBASECLASSES (type);
2813 vbase_types = vbases;
2814 vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
2815 vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, NULL_PTR);
2817 if (use_computed_offsets)
2819 /* This is an object of type IN_TYPE, */
2820 flag_this_is_variable = -2;
2823 dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep);
2825 /* Initialized with vtables of type TYPE. */
2826 for (; vbases; vbases = TREE_CHAIN (vbases))
2829 if (use_computed_offsets)
2830 addr = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbases));
2834 tree vb = get_vbase (TREE_TYPE (vbases), TYPE_BINFO (TREE_TYPE (vbase_decl)),
2836 addr = convert_pointer_to_real (vb, vbase_decl_ptr);
2838 tree vbinfo = get_binfo (TREE_TYPE (vbases),
2839 TREE_TYPE (vbase_decl),
2842 /* See is we can get lucky. */
2843 if (TREE_VIA_VIRTUAL (vbinfo))
2844 addr = convert_pointer_to_real (vbinfo, vbase_decl_ptr);
2847 /* We go through all these contortions to avoid this
2848 call, as it will fail when the virtual base type
2849 is ambiguous from here. We don't yet have a way
2850 to search for and find just an instance of the
2851 virtual base class. Searching for the binfo in
2852 vbases won't work, as we don't have the vbase
2853 pointer field, for all vbases in the main class,
2854 only direct vbases. */
2855 addr = convert_pointer_to_real (TREE_TYPE (vbases),
2857 if (addr == error_mark_node)
2863 /* Do all vtables from this virtual base. */
2864 /* This assumes that virtual bases can never serve as parent
2865 binfos. (in the CLASSTPE_VFIELD_PARENT sense) */
2866 expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
2869 /* If we are using computed offsets we can skip fixups. */
2870 if (use_computed_offsets)
2873 /* Now we adjust the offsets for virtual functions that cross
2874 virtual boundaries on an implicit upcast on vf call so that
2875 the layout of the most complete type is used, instead of
2876 assuming the layout of the virtual bases from our current type. */
2878 if (flag_vtable_thunks)
2880 /* We don't have dynamic thunks yet! So for now, just fail silently. */
2884 tree vbase_offsets = NULL_TREE;
2885 push_to_sequence (fixup_insns);
2886 fixup_virtual_upcast_offsets (vbases,
2887 TYPE_BINFO (BINFO_TYPE (vbases)),
2888 1, 0, addr, vbase_decl_ptr,
2889 type, vbases, &vbase_offsets);
2890 fixup_insns = get_insns ();
2897 extern tree in_charge_identifier;
2898 tree in_charge_node = lookup_name (in_charge_identifier, 0);
2899 if (! in_charge_node)
2901 warning ("recoverable internal compiler error, nobody's in charge!");
2902 in_charge_node = integer_zero_node;
2904 in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1);
2905 expand_start_cond (in_charge_node, 0);
2906 emit_insns (fixup_insns);
2910 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2912 flag_this_is_variable = old_flag;
2917 clear_search_slots (type)
2920 dfs_walk (TYPE_BINFO (type),
2921 dfs_clear_search_slot, dfs_search_slot_nonempty_p);
2924 /* get virtual base class types.
2925 This adds type to the vbase_types list in reverse dfs order.
2926 Ordering is very important, so don't change it. */
2929 dfs_get_vbase_types (binfo)
2932 if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
2934 vbase_types = make_binfo (integer_zero_node, binfo,
2935 BINFO_VTABLE (binfo),
2936 BINFO_VIRTUALS (binfo), vbase_types);
2937 TREE_VIA_VIRTUAL (vbase_types) = 1;
2938 SET_BINFO_VBASE_MARKED (binfo);
2940 SET_BINFO_MARKED (binfo);
2943 /* get a list of virtual base classes in dfs order. */
2945 get_vbase_types (type)
2951 if (TREE_CODE (type) == TREE_VEC)
2954 binfo = TYPE_BINFO (type);
2956 vbase_types = NULL_TREE;
2957 dfs_walk (binfo, dfs_get_vbase_types, unmarkedp);
2958 dfs_walk (binfo, dfs_unmark, markedp);
2959 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2960 reverse it so that we get normal dfs ordering. */
2961 vbase_types = nreverse (vbase_types);
2963 /* unmark marked vbases */
2964 for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
2965 CLEAR_BINFO_VBASE_MARKED (vbases);
2971 dfs_record_inheritance (binfo)
2974 tree binfos = BINFO_BASETYPES (binfo);
2975 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2976 mi_boolean *derived_row = BINFO_DERIVES_FROM_STAR (binfo);
2978 for (i = n_baselinks-1; i >= 0; i--)
2981 tree base_binfo = TREE_VEC_ELT (binfos, i);
2982 tree baseclass = BINFO_TYPE (base_binfo);
2983 mi_boolean *base_row = BINFO_DERIVES_FROM_STAR (base_binfo);
2985 /* Don't search if there's nothing there! MI_SIZE can be
2986 zero as a result of parse errors. */
2987 if (TYPE_BINFO_BASETYPES (baseclass) && mi_size > 0)
2988 for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
2989 derived_row[j] |= base_row[j];
2990 TYPE_DERIVES_FROM (baseclass, BINFO_TYPE (binfo)) = 1;
2993 SET_BINFO_MARKED (binfo);
2996 /* Given a _CLASSTYPE node in a multiple inheritance lattice,
2997 convert the lattice into a simple relation such that,
2998 given to CIDs, C1 and C2, one can determine if C1 <= C2
2999 or C2 <= C1 or C1 <> C2.
3001 Once constructed, we walk the lattice depth fisrt,
3002 applying various functions to elements as they are encountered.
3004 We use xmalloc here, in case we want to randomly free these tables. */
3006 #define SAVE_MI_MATRIX
3009 build_mi_matrix (type)
3012 tree binfo = TYPE_BINFO (type);
3015 #ifdef SAVE_MI_MATRIX
3016 if (CLASSTYPE_MI_MATRIX (type))
3018 mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
3019 mi_matrix = CLASSTYPE_MI_MATRIX (type);
3021 dfs_walk (binfo, dfs_number, unnumberedp);
3026 mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
3027 mi_matrix = (char *)xmalloc ((mi_size + 1) * (mi_size + 1));
3029 bzero (mi_matrix, (mi_size + 1) * (mi_size + 1));
3030 dfs_walk (binfo, dfs_number, unnumberedp);
3031 dfs_walk (binfo, dfs_record_inheritance, unmarkedp);
3032 dfs_walk (binfo, dfs_unmark, markedp);
3038 dfs_walk (TYPE_BINFO (mi_type), dfs_unnumber, numberedp);
3040 #ifdef SAVE_MI_MATRIX
3041 CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
3049 /* If we want debug info for a type TYPE, make sure all its base types
3050 are also marked as being potentially interesting. This avoids
3051 the problem of not writing any debug info for intermediate basetypes
3052 that have abstract virtual functions. Also mark member types. */
3055 note_debug_info_needed (type)
3059 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp);
3060 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3063 if (TREE_CODE (field) == FIELD_DECL
3064 && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
3065 && dfs_debug_unmarkedp (TYPE_BINFO (ttype)))
3066 note_debug_info_needed (ttype);
3070 /* Subroutines of push_class_decls (). */
3072 /* Add the instance variables which this class contributed to the
3073 current class binding contour. When a redefinition occurs,
3074 if the redefinition is strictly within a single inheritance path,
3075 we just overwrite (in the case of a data field) or
3076 cons (in the case of a member function) the old declaration with
3077 the new. If the fields are not within a single inheritance path,
3078 we must cons them in either case.
3080 In order to know what decls are new (stemming from the current
3081 invocation of push_class_decls) we enclose them in an "envelope",
3082 which is a TREE_LIST node where the TREE_PURPOSE slot contains the
3083 new decl (or possibly a list of competing ones), the TREE_VALUE slot
3084 points to the old value and the TREE_CHAIN slot chains together all
3085 envelopes which needs to be "opened" in push_class_decls. Opening an
3086 envelope means: push the old value onto the class_shadowed list,
3087 install the new one and if it's a TYPE_DECL do the same to the
3088 IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that
3089 the TREE_PURPOSE slot is non-null, and that it is not an identifier.
3090 Because if it is, it could be a set of overloaded methods from an
3094 dfs_pushdecls (binfo)
3097 tree type = BINFO_TYPE (binfo);
3098 tree fields, *methods, *end;
3101 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3103 /* Unmark so that if we are in a constructor, and then find that
3104 this field was initialized by a base initializer,
3105 we can emit an error message. */
3106 if (TREE_CODE (fields) == FIELD_DECL)
3107 TREE_USED (fields) = 0;
3109 /* Recurse into anonymous unions. */
3110 if (DECL_NAME (fields) == NULL_TREE
3111 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3113 dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields)));
3118 if (TREE_CODE (fields) != TYPE_DECL)
3120 DECL_PUBLIC (fields) = 0;
3121 DECL_PROTECTED (fields) = 0;
3122 DECL_PRIVATE (fields) = 0;
3126 if (DECL_NAME (fields))
3128 tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (fields));
3130 /* If the class value is an envelope of the kind described in
3131 the comment above, we try to rule out possible ambiguities.
3132 If we can't do that, keep a TREE_LIST with possibly ambiguous
3134 if (class_value && TREE_CODE (class_value) == TREE_LIST
3135 && TREE_PURPOSE (class_value) != NULL_TREE
3136 && (TREE_CODE (TREE_PURPOSE (class_value))
3137 != IDENTIFIER_NODE))
3139 tree value = TREE_PURPOSE (class_value);
3142 /* Possible ambiguity. If its defining type(s)
3143 is (are all) derived from us, no problem. */
3144 if (TREE_CODE (value) != TREE_LIST)
3146 context = (TREE_CODE (value) == FUNCTION_DECL
3147 && DECL_VIRTUAL_P (value))
3148 ? DECL_CLASS_CONTEXT (value)
3149 : DECL_CONTEXT (value);
3151 if (context == type)
3153 if (TREE_CODE (value) == TYPE_DECL
3154 && DECL_ARTIFICIAL (value))
3156 /* else the old value wins */
3158 else if (context && TYPE_DERIVES_FROM (context, type))
3161 value = tree_cons (NULL_TREE, fields,
3162 build_tree_list (NULL_TREE, value));
3166 /* All children may derive from us, in which case
3167 there is no problem. Otherwise, we have to
3168 keep lists around of what the ambiguities might be. */
3172 for (values = value; values; values = TREE_CHAIN (values))
3174 tree sub_values = TREE_VALUE (values);
3176 if (TREE_CODE (sub_values) == TREE_LIST)
3178 for (; sub_values; sub_values = TREE_CHAIN (sub_values))
3180 register tree list_mbr = TREE_VALUE (sub_values);
3182 context = (TREE_CODE (list_mbr) == FUNCTION_DECL
3183 && DECL_VIRTUAL_P (list_mbr))
3184 ? DECL_CLASS_CONTEXT (list_mbr)
3185 : DECL_CONTEXT (list_mbr);
3187 if (! TYPE_DERIVES_FROM (context, type))
3189 value = tree_cons (NULL_TREE, TREE_VALUE (values), value);
3197 context = (TREE_CODE (sub_values) == FUNCTION_DECL
3198 && DECL_VIRTUAL_P (sub_values))
3199 ? DECL_CLASS_CONTEXT (sub_values)
3200 : DECL_CONTEXT (sub_values);
3202 if (context && ! TYPE_DERIVES_FROM (context, type))
3204 value = tree_cons (NULL_TREE, values, value);
3210 if (! problem) value = fields;
3213 /* Mark this as a potentially ambiguous member. */
3214 if (TREE_CODE (value) == TREE_LIST)
3216 /* Leaving TREE_TYPE blank is intentional.
3217 We cannot use `error_mark_node' (lookup_name)
3218 or `unknown_type_node' (all member functions use this). */
3219 TREE_NONLOCAL_FLAG (value) = 1;
3222 /* Put the new contents in our envelope. */
3223 TREE_PURPOSE (class_value) = value;
3227 /* See comment above for a description of envelopes. */
3228 tree envelope = tree_cons (fields, class_value,
3231 closed_envelopes = envelope;
3232 IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = envelope;
3237 method_vec = CLASSTYPE_METHOD_VEC (type);
3238 if (method_vec != 0)
3240 /* Farm out constructors and destructors. */
3241 methods = &TREE_VEC_ELT (method_vec, 1);
3242 end = TREE_VEC_END (method_vec);
3244 /* This does not work for multiple inheritance yet. */
3245 while (methods != end)
3247 /* This will cause lookup_name to return a pointer
3248 to the tree_list of possible methods of this name.
3249 If the order is a problem, we can nreverse them. */
3251 tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods));
3253 if (class_value && TREE_CODE (class_value) == TREE_LIST
3254 && TREE_PURPOSE (class_value) != NULL_TREE
3255 && TREE_CODE (TREE_PURPOSE (class_value)) != IDENTIFIER_NODE)
3257 tree old = TREE_PURPOSE (class_value);
3259 maybe_push_cache_obstack ();
3260 if (TREE_CODE (old) == TREE_LIST)
3261 tmp = tree_cons (DECL_NAME (*methods), *methods, old);
3264 /* Only complain if we shadow something we can access. */
3267 && ((DECL_LANG_SPECIFIC (old)
3268 && DECL_CLASS_CONTEXT (old) == current_class_type)
3269 || ! TREE_PRIVATE (old)))
3270 /* Should figure out access control more accurately. */
3272 cp_warning_at ("member `%#D' is shadowed", old);
3273 cp_warning_at ("by member function `%#D'", *methods);
3274 warning ("in this context");
3276 tmp = build_tree_list (DECL_NAME (*methods), *methods);
3280 TREE_TYPE (tmp) = unknown_type_node;
3282 TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
3284 TREE_NONLOCAL_FLAG (tmp) = 1;
3286 /* Put the new contents in our envelope. */
3287 TREE_PURPOSE (class_value) = tmp;
3291 maybe_push_cache_obstack ();
3292 tmp = build_tree_list (DECL_NAME (*methods), *methods);
3295 TREE_TYPE (tmp) = unknown_type_node;
3297 TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
3299 TREE_NONLOCAL_FLAG (tmp) = 1;
3301 /* See comment above for a description of envelopes. */
3302 closed_envelopes = tree_cons (tmp, class_value,
3304 IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods)) = closed_envelopes;
3310 DECL_PUBLIC (tmp) = 0;
3311 DECL_PROTECTED (tmp) = 0;
3312 DECL_PRIVATE (tmp) = 0;
3313 tmp = DECL_CHAIN (tmp);
3320 SET_BINFO_MARKED (binfo);
3323 /* Consolidate unique (by name) member functions. */
3325 dfs_compress_decls (binfo)
3328 tree type = BINFO_TYPE (binfo);
3329 tree method_vec = CLASSTYPE_METHOD_VEC (type);
3331 if (method_vec != 0)
3333 /* Farm out constructors and destructors. */
3334 tree *methods = &TREE_VEC_ELT (method_vec, 1);
3335 tree *end = TREE_VEC_END (method_vec);
3337 for (; methods != end; methods++)
3339 /* This is known to be an envelope of the kind described before
3341 tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods));
3342 tree tmp = TREE_PURPOSE (class_value);
3344 /* This was replaced in scope by somebody else. Just leave it
3346 if (TREE_CODE (tmp) != TREE_LIST)
3349 if (TREE_CHAIN (tmp) == NULL_TREE
3351 && DECL_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
3353 TREE_PURPOSE (class_value) = TREE_VALUE (tmp);
3357 CLEAR_BINFO_MARKED (binfo);
3360 /* When entering the scope of a class, we cache all of the
3361 fields that that class provides within its inheritance
3362 lattice. Where ambiguities result, we mark them
3363 with `error_mark_node' so that if they are encountered
3364 without explicit qualification, we can emit an error
3367 push_class_decls (type)
3371 struct obstack *ambient_obstack = current_obstack;
3374 tree tags = CLASSTYPE_TAGS (type);
3378 tree code_type_node;
3381 switch (TREE_CODE (TREE_VALUE (tags)))
3384 code_type_node = enum_type_node;
3387 code_type_node = record_type_node;
3390 code_type_node = class_type_node;
3393 code_type_node = union_type_node;
3396 my_friendly_abort (297);
3398 tag = xref_tag (code_type_node, TREE_PURPOSE (tags),
3399 TYPE_BINFO_BASETYPE (TREE_VALUE (tags), 0), 0);
3400 #if 0 /* not yet, should get fixed properly later */
3401 pushdecl (make_type_decl (TREE_PURPOSE (tags), TREE_VALUE (tags)));
3403 pushdecl (build_decl (TYPE_DECL, TREE_PURPOSE (tags), TREE_VALUE (tags)));
3408 search_stack = push_search_level (search_stack, &search_obstack);
3410 id = TYPE_IDENTIFIER (type);
3412 if (IDENTIFIER_TEMPLATE (id) != 0)
3414 tree tmpl = IDENTIFIER_TEMPLATE (id);
3415 push_template_decls (DECL_ARGUMENTS (TREE_PURPOSE (tmpl)),
3416 TREE_VALUE (tmpl), 1);
3417 overload_template_name (id, 1);
3421 /* Push class fields into CLASS_VALUE scope, and mark. */
3422 dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarkedp);
3424 /* Compress fields which have only a single entry
3425 by a given name, and unmark. */
3426 dfs_walk (TYPE_BINFO (type), dfs_compress_decls, markedp);
3428 /* Open up all the closed envelopes and push the contained decls into
3430 while (closed_envelopes)
3432 tree new = TREE_PURPOSE (closed_envelopes);
3435 /* This is messy because the class value may be a *_DECL, or a
3436 TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
3437 *_DECLs. The name is stored at different places in these three
3439 if (TREE_CODE (new) == TREE_LIST)
3441 if (TREE_PURPOSE (new) != NULL_TREE)
3442 id = TREE_PURPOSE (new);
3445 tree node = TREE_VALUE (new);
3447 while (TREE_CODE (node) == TREE_LIST)
3448 node = TREE_VALUE (node);
3449 id = DECL_NAME (node);
3453 id = DECL_NAME (new);
3455 /* Install the original class value in order to make
3456 pushdecl_class_level work correctly. */
3457 IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes);
3458 if (TREE_CODE (new) == TREE_LIST)
3459 push_class_level_binding (id, new);
3461 pushdecl_class_level (new);
3462 closed_envelopes = TREE_CHAIN (closed_envelopes);
3464 current_obstack = ambient_obstack;
3467 /* Here's a subroutine we need because C lacks lambdas. */
3469 dfs_unuse_fields (binfo)
3472 tree type = TREE_TYPE (binfo);
3475 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3477 if (TREE_CODE (fields) != FIELD_DECL)
3480 TREE_USED (fields) = 0;
3481 if (DECL_NAME (fields) == NULL_TREE
3482 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3483 unuse_fields (TREE_TYPE (fields));
3491 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp);
3495 pop_class_decls (type)
3498 /* We haven't pushed a search level when dealing with cached classes,
3499 so we'd better not try to pop it. */
3501 search_stack = pop_search_level (search_stack);
3505 print_search_statistics ()
3507 #ifdef GATHER_STATISTICS
3508 if (flag_memoize_lookups)
3510 fprintf (stderr, "%d memoized contexts saved\n",
3512 fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
3513 fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
3514 fprintf (stderr, "fields statistics:\n");
3515 fprintf (stderr, " memoized finds = %d; rejects = %d; (searches = %d)\n",
3516 memoized_fast_finds[0], memoized_fast_rejects[0],
3517 memoized_fields_searched[0]);
3518 fprintf (stderr, " memoized_adds = %d\n", memoized_adds[0]);
3519 fprintf (stderr, "fnfields statistics:\n");
3520 fprintf (stderr, " memoized finds = %d; rejects = %d; (searches = %d)\n",
3521 memoized_fast_finds[1], memoized_fast_rejects[1],
3522 memoized_fields_searched[1]);
3523 fprintf (stderr, " memoized_adds = %d\n", memoized_adds[1]);
3525 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3526 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3527 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3528 n_outer_fields_searched, n_calls_lookup_fnfields);
3529 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
3531 fprintf (stderr, "no search statistics\n");
3536 init_search_processing ()
3538 gcc_obstack_init (&search_obstack);
3539 gcc_obstack_init (&type_obstack);
3540 gcc_obstack_init (&type_obstack_entries);
3542 /* This gives us room to build our chains of basetypes,
3543 whether or not we decide to memoize them. */
3544 type_stack = push_type_level (0, &type_obstack);
3545 _vptr_name = get_identifier ("_vptr");
3549 reinit_search_statistics ()
3551 my_memoized_entry_counter = 0;
3552 memoized_fast_finds[0] = 0;
3553 memoized_fast_finds[1] = 0;
3554 memoized_adds[0] = 0;
3555 memoized_adds[1] = 0;
3556 memoized_fast_rejects[0] = 0;
3557 memoized_fast_rejects[1] = 0;
3558 memoized_fields_searched[0] = 0;
3559 memoized_fields_searched[1] = 0;
3560 n_fields_searched = 0;
3561 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3562 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3563 n_calls_get_base_type = 0;
3564 n_outer_fields_searched = 0;
3565 n_contexts_saved = 0;
3568 static tree conversions;
3570 add_conversions (binfo)
3573 tree tmp = CLASSTYPE_FIRST_CONVERSION (BINFO_TYPE (binfo));
3574 for (; tmp && IDENTIFIER_TYPENAME_P (DECL_NAME (tmp));
3575 tmp = TREE_CHAIN (tmp))
3576 conversions = tree_cons (DECL_NAME (tmp), TREE_TYPE (TREE_TYPE (tmp)),
3581 lookup_conversions (type)
3584 conversions = NULL_TREE;
3585 dfs_walk (TYPE_BINFO (type), add_conversions, 0);