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 /* Convert EXPR to a virtual base class of type TYPE. We know that
432 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
433 the type of what expr points to has a virtual base of type TYPE. */
435 convert_pointer_to_vbase (type, expr)
439 tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))), NULL_PTR);
440 return convert_pointer_to_real (vb, expr);
443 /* This is the newer recursive depth first search routine. */
445 /* Return non-zero if PARENT is directly derived from TYPE. By directly
446 we mean it's only one step up the inheritance lattice. We check this
447 by walking horizontally across the types that TYPE directly inherits
448 from, to see if PARENT is among them. This is used by get_binfo and
449 by compute_access. */
451 immediately_derived (parent, type)
454 if (TYPE_BINFO (type))
456 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
457 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
459 for (i = 0; i < n_baselinks; i++)
461 tree base_binfo = TREE_VEC_ELT (binfos, i);
463 if (parent == BINFO_TYPE (base_binfo))
471 /* Check whether the type given in BINFO is derived from PARENT. If
472 it isn't, return 0. If it is, but the derivation is MI-ambiguous
473 AND protect != 0, emit an error message and return error_mark_node.
475 Otherwise, if TYPE is derived from PARENT, return the actual base
476 information, unless a one of the protection violations below
477 occurs, in which case emit an error message and return error_mark_node.
479 If PROTECT is 1, then check if access to a public field of PARENT
480 would be private. Also check for ambiguity. */
483 get_binfo (parent, binfo, protect)
484 register tree parent, binfo;
489 tree rval = NULL_TREE;
491 if (TREE_CODE (parent) == TREE_VEC)
492 parent = BINFO_TYPE (parent);
493 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
494 my_friendly_abort (89);
496 if (TREE_CODE (binfo) == TREE_VEC)
497 type = BINFO_TYPE (binfo);
498 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
501 my_friendly_abort (90);
503 dist = get_base_distance (parent, binfo, protect, &rval);
507 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
509 return error_mark_node;
511 else if (dist == -2 && protect)
513 cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
515 return error_mark_node;
521 /* This is the newer depth first get_base_distance routine. */
523 get_base_distance_recursive (binfo, depth, is_private, basetype_path, rval,
524 rval_private_ptr, new_binfo_ptr, parent, path_ptr,
525 protect, via_virtual_ptr, via_virtual)
526 tree binfo, basetype_path, *new_binfo_ptr, parent, *path_ptr;
527 int *rval_private_ptr, depth, is_private, rval, protect, *via_virtual_ptr,
533 if (BINFO_TYPE (binfo) == parent || binfo == parent)
538 *rval_private_ptr = is_private;
539 *new_binfo_ptr = binfo;
540 *via_virtual_ptr = via_virtual;
544 int same_object = (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
545 BINFO_OFFSET (binfo))
546 && *via_virtual_ptr && via_virtual);
548 if (*via_virtual_ptr && via_virtual==0)
550 *rval_private_ptr = is_private;
551 *new_binfo_ptr = binfo;
552 *via_virtual_ptr = via_virtual;
554 else if (same_object)
556 if (*rval_private_ptr && ! is_private)
558 *rval_private_ptr = is_private;
559 *new_binfo_ptr = binfo;
560 *via_virtual_ptr = via_virtual;
570 binfos = BINFO_BASETYPES (binfo);
571 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
574 /* Process base types. */
575 for (i = 0; i < n_baselinks; i++)
577 tree base_binfo = TREE_VEC_ELT (binfos, i);
579 /* Find any specific instance of a virtual base, when searching with
581 if (BINFO_MARKED (base_binfo) == 0 || TREE_CODE (parent) == TREE_VEC)
586 || (!TREE_VIA_PUBLIC (base_binfo)
587 && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
588 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
591 /* When searching for a non-virtual, we cannot mark
592 virtually found binfos. */
594 SET_BINFO_MARKED (base_binfo);
596 #define WATCH_VALUES(rval, via_private) (rval == -1 ? 3 : via_private)
598 was = WATCH_VALUES (rval, *via_virtual_ptr);
599 rval = get_base_distance_recursive (base_binfo, depth, via_private,
600 binfo, rval, rval_private_ptr,
601 new_binfo_ptr, parent, path_ptr,
602 protect, via_virtual_ptr,
604 /* watch for updates; only update if path is good. */
605 if (path_ptr && WATCH_VALUES (rval, *via_virtual_ptr) != was)
606 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
607 if (rval == -2 && *via_virtual_ptr == 0)
618 /* Return the number of levels between type PARENT and the type given
619 in BINFO, following the leftmost path to PARENT not found along a
620 virtual path, if there are no real PARENTs (all come from virtual
621 base classes), then follow the leftmost path to PARENT.
623 Return -1 if TYPE is not derived from PARENT.
624 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
626 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
628 If PATH_PTR is non-NULL, then also build the list of types
629 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
632 PARENT can also be a binfo, in which case that exact parent is found
633 and no other. convert_pointer_to_real uses this functionality.
635 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
638 get_base_distance (parent, binfo, protect, path_ptr)
639 register tree parent, binfo;
644 int rval_private = 0;
646 tree new_binfo = NULL_TREE;
648 int watch_access = protect;
650 if (TREE_CODE (parent) != TREE_VEC)
651 parent = TYPE_MAIN_VARIANT (parent);
653 if (TREE_CODE (binfo) == TREE_VEC)
654 type = BINFO_TYPE (binfo);
655 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
658 binfo = TYPE_BINFO (type);
661 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
664 my_friendly_abort (92);
666 if (parent == type || parent == binfo)
668 /* If the distance is 0, then we don't really need
669 a path pointer, but we shouldn't let garbage go back. */
678 rval = get_base_distance_recursive (binfo, 0, 0, NULL_TREE, -1,
679 &rval_private, &new_binfo, parent,
680 path_ptr, watch_access, &via_virtual, 0);
682 dfs_walk (binfo, dfs_unmark, markedp);
684 /* Access restrictions don't count if we found an ambiguous basetype. */
685 if (rval == -2 && protect >= 0)
688 if (rval && protect && rval_private)
691 /* find real virtual base classes. */
692 if (rval == -1 && TREE_CODE (parent) == TREE_VEC
693 && parent == binfo_member (BINFO_TYPE (parent),
694 CLASSTYPE_VBASECLASSES (type)))
696 BINFO_INHERITANCE_CHAIN (parent) = binfo;
702 *path_ptr = new_binfo;
706 /* Search for a member with name NAME in a multiple inheritance lattice
707 specified by TYPE. If it does not exist, return NULL_TREE.
708 If the member is ambiguously referenced, return `error_mark_node'.
709 Otherwise, return the FIELD_DECL. */
711 /* Do a 1-level search for NAME as a member of TYPE. The caller must
712 figure out whether it can access this field. (Since it is only one
713 level, this is reasonable.) */
715 lookup_field_1 (type, name)
718 register tree field = TYPE_FIELDS (type);
720 #ifdef GATHER_STATISTICS
721 n_calls_lookup_field_1++;
725 #ifdef GATHER_STATISTICS
728 if (DECL_NAME (field) == NULL_TREE
729 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
731 tree temp = lookup_field_1 (TREE_TYPE (field), name);
735 if (DECL_NAME (field) == name)
737 if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
738 && DECL_ASSEMBLER_NAME (field) != NULL)
739 GNU_xref_ref(current_function_decl,
740 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
743 field = TREE_CHAIN (field);
746 if (name == _vptr_name)
748 /* Give the user what s/he thinks s/he wants. */
749 if (TYPE_VIRTUAL_P (type))
750 return CLASSTYPE_VFIELD (type);
755 /* There are a number of cases we need to be aware of here:
756 current_class_type current_function_decl
759 * class-local SET NULL
763 Those last two make life interesting. If we're in a function which is
764 itself inside a class, we need decls to go into the fn's decls (our
765 second case below). But if we're in a class and the class itself is
766 inside a function, we need decls to go into the decls for the class. To
767 achieve this last goal, we must see if, when both current_class_decl and
768 current_function_decl are set, the class was declared inside that
769 function. If so, we know to put the decls into the class's scope. */
774 if (current_function_decl == NULL_TREE)
775 return current_class_type;
776 if (current_class_type == NULL_TREE)
777 return current_function_decl;
778 if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type)
779 return current_function_decl;
781 return current_class_type;
784 /* Compute the access of FIELD. This is done by computing
785 the access available to each type in BASETYPES (which comes
786 as a list of [via_public/basetype] in reverse order, namely base
787 class before derived class). The first one which defines a
788 access defines the access for the field. Otherwise, the
789 access of the field is that which occurs normally.
791 Uses global variables CURRENT_CLASS_TYPE and
792 CURRENT_FUNCTION_DECL to use friend relationships
795 This will be static when lookup_fnfield comes into this file.
797 access_public_node means that the field can be accessed by the current lexical
800 access_protected_node means that the field cannot be accessed by the current
801 lexical scope because it is protected.
803 access_private_node means that the field cannot be accessed by the current
804 lexical scope because it is private. */
807 #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public_node
808 #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected_node
809 #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private_node
811 #define PUBLIC_RETURN return access_public_node
812 #define PROTECTED_RETURN return access_protected_node
813 #define PRIVATE_RETURN return access_private_node
817 /* Disabled with DECL_PUBLIC &c. */
818 static tree previous_scope = NULL_TREE;
822 compute_access (basetype_path, field)
823 tree basetype_path, field;
828 int protected_ok, via_protected;
829 extern int flag_access_control;
831 /* Replaces static decl above. */
835 ((TREE_CODE (field) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (field))
836 || (TREE_CODE (field) != FUNCTION_DECL && TREE_STATIC (field)));
838 if (! flag_access_control)
839 return access_public_node;
841 /* The field lives in the current class. */
842 if (BINFO_TYPE (basetype_path) == current_class_type)
843 return access_public_node;
846 /* Disabled until pushing function scope clears these out. If ever. */
847 /* Make these special cases fast. */
848 if (current_scope () == previous_scope)
850 if (DECL_PUBLIC (field))
851 return access_public_node;
852 if (DECL_PROTECTED (field))
853 return access_protected_node;
854 if (DECL_PRIVATE (field))
855 return access_private_node;
859 /* We don't currently support access control on nested types. */
860 if (TREE_CODE (field) == TYPE_DECL)
861 return access_public_node;
863 previous_scope = current_scope ();
865 context = DECL_CLASS_CONTEXT (field);
866 if (context == NULL_TREE)
867 context = DECL_CONTEXT (field);
869 /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT
870 slot set to the union type rather than the record type containing
871 the anonymous union. In this case, DECL_FIELD_CONTEXT is correct. */
872 if (context && TREE_CODE (context) == UNION_TYPE
873 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
874 context = DECL_FIELD_CONTEXT (field);
876 /* Virtual function tables are never private. But we should know that
877 we are looking for this, and not even try to hide it. */
878 if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1)
881 /* Member found immediately within object. */
882 if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE)
884 /* Are we (or an enclosing scope) friends with the class that has
886 if (is_friend (context, previous_scope))
889 /* If it's private, it's private, you letch. */
890 if (TREE_PRIVATE (field))
893 /* ARM $11.5. Member functions of a derived class can access the
894 non-static protected members of a base class only through a
895 pointer to the derived class, a reference to it, or an object
896 of it. Also any subsequently derived classes also have
898 else if (TREE_PROTECTED (field))
900 if (current_class_type
902 && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type))
911 /* must reverse more than one element */
912 basetype_path = reverse_path (basetype_path);
913 types = basetype_path;
915 access = access_default_node;
916 protected_ok = static_mem && current_class_type
917 && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type);
923 tree type = BINFO_TYPE (binfo);
926 /* Friends of a class can see protected members of its bases.
927 Note that classes are their own friends. */
928 if (is_friend (type, previous_scope))
934 member = purpose_member (type, DECL_ACCESS (field));
937 access = TREE_VALUE (member);
941 types = BINFO_INHERITANCE_CHAIN (types);
943 /* If the next type was VIA_PROTECTED, then fields of all remaining
944 classes past that one are *at least* protected. */
947 if (TREE_VIA_PROTECTED (types))
949 else if (! TREE_VIA_PUBLIC (types) && ! private_ok)
951 access = access_private_node;
958 reverse_path (basetype_path);
960 /* No special visibilities apply. Use normal rules. */
962 if (access == access_default_node)
964 if (is_friend (context, previous_scope))
965 access = access_public_node;
966 else if (TREE_PRIVATE (field))
967 access = access_private_node;
968 else if (TREE_PROTECTED (field))
969 access = access_protected_node;
971 access = access_public_node;
974 if (access == access_public_node && via_protected)
975 access = access_protected_node;
977 if (access == access_protected_node && protected_ok)
978 access = access_public_node;
981 if (access == access_public_node)
982 DECL_PUBLIC (field) = 1;
983 else if (access == access_protected_node)
984 DECL_PROTECTED (field) = 1;
985 else if (access == access_private_node)
986 DECL_PRIVATE (field) = 1;
987 else my_friendly_abort (96);
992 /* Routine to see if the sub-object denoted by the binfo PARENT can be
993 found as a base class and sub-object of the object denoted by
994 BINFO. This routine relies upon binfos not being shared, except
995 for binfos for virtual bases. */
997 is_subobject_of_p (parent, binfo)
1000 tree binfos = BINFO_BASETYPES (binfo);
1001 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1003 if (parent == binfo)
1006 /* Process and/or queue base types. */
1007 for (i = 0; i < n_baselinks; i++)
1009 tree base_binfo = TREE_VEC_ELT (binfos, i);
1010 if (TREE_VIA_VIRTUAL (base_binfo))
1011 base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
1012 if (is_subobject_of_p (parent, base_binfo))
1018 /* See if a one FIELD_DECL hides another. This routine is meant to
1019 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1020 binfos given are the binfos corresponding to the particular places
1021 the FIELD_DECLs are found. This routine relies upon binfos not
1022 being shared, except for virtual bases. */
1024 hides (hider_binfo, hidee_binfo)
1025 tree hider_binfo, hidee_binfo;
1027 /* hider hides hidee, if hider has hidee as a base class and
1028 the instance of hidee is a sub-object of hider. The first
1029 part is always true is the second part is true.
1031 When hider and hidee are the same (two ways to get to the exact
1032 same member) we consider either one as hiding the other. */
1033 return is_subobject_of_p (hidee_binfo, hider_binfo);
1036 /* Very similar to lookup_fnfields_1 but it ensures that at least one
1037 function was declared inside the class given by TYPE. It really should
1038 only return functions that match the given TYPE. */
1040 lookup_fnfields_here (type, name)
1043 int index = lookup_fnfields_1 (type, name);
1048 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
1051 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (fndecls))
1052 == TYPE_MAIN_VARIANT (type))
1054 fndecls = TREE_CHAIN (fndecls);
1059 /* Look for a field named NAME in an inheritance lattice dominated by
1060 XBASETYPE. PROTECT is zero if we can avoid computing access
1061 information, otherwise it is 1. WANT_TYPE is 1 when we should only
1062 return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
1064 It was not clear what should happen if WANT_TYPE is set, and an
1065 ambiguity is found. At least one use (lookup_name) to not see
1068 lookup_field (xbasetype, name, protect, want_type)
1069 register tree xbasetype, name;
1070 int protect, want_type;
1072 int head = 0, tail = 0;
1073 tree rval, rval_binfo = NULL_TREE, rval_binfo_h;
1074 tree type, basetype_chain, basetype_path;
1075 tree this_v = access_default_node;
1076 tree entry, binfo, binfo_h;
1077 tree own_access = access_default_node;
1078 int vbase_name_p = VBASE_NAME_P (name);
1080 /* rval_binfo is the binfo associated with the found member, note,
1081 this can be set with useful information, even when rval is not
1082 set, because it must deal with ALL members, not just non-function
1083 members. It is used for ambiguity checking and the hidden
1084 checks. Whereas rval is only set if a proper (not hidden)
1085 non-function member is found. */
1087 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1088 hiding checks, as virtual base classes may not be shared. The strategy
1089 is we always go into the the binfo hierarchy owned by TYPE_BINFO of
1090 virtual base classes, as we cross virtual base class lines. This way
1091 we know that binfo of a virtual base class will always == itself when
1092 found along any line. (mrs) */
1096 /* Set this to nonzero if we don't know how to compute
1097 accurate error messages for access control. */
1098 int index = MEMOIZED_HASH_FN (name);
1100 /* If we are looking for a constructor in a templated type, use the
1101 unspecialized name, as that is how we store it. */
1102 if (IDENTIFIER_TEMPLATE (name))
1103 name = constructor_name (name);
1105 if (TREE_CODE (xbasetype) == TREE_VEC)
1107 type = BINFO_TYPE (xbasetype);
1108 basetype_path = xbasetype;
1110 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1113 basetype_path = TYPE_BINFO (xbasetype);
1114 BINFO_VIA_PUBLIC (basetype_path) = 1;
1115 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1117 else my_friendly_abort (97);
1119 if (CLASSTYPE_MTABLE_ENTRY (type))
1121 tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
1123 while (tem && TREE_PURPOSE (tem) != name)
1125 memoized_fields_searched[0]++;
1126 tem = TREE_CHAIN (tem);
1130 if (protect && TREE_TYPE (tem))
1132 error (TREE_STRING_POINTER (TREE_TYPE (tem)),
1133 IDENTIFIER_POINTER (name),
1134 TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
1135 return error_mark_node;
1137 if (TREE_VALUE (tem) == NULL_TREE)
1138 memoized_fast_rejects[0] += 1;
1140 memoized_fast_finds[0] += 1;
1141 return TREE_VALUE (tem);
1145 #ifdef GATHER_STATISTICS
1146 n_calls_lookup_field++;
1148 if (protect && flag_memoize_lookups && ! global_bindings_p ())
1149 entry = make_memoized_table_entry (type, name, 0);
1153 rval = lookup_field_1 (type, name);
1155 if (rval || lookup_fnfields_here (type, name) >= 0)
1161 if (TREE_CODE (rval) != TYPE_DECL)
1163 rval = purpose_member (name, CLASSTYPE_TAGS (type));
1165 rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
1170 if (TREE_CODE (rval) == TYPE_DECL
1171 && lookup_fnfields_here (type, name) >= 0)
1176 if (protect && rval)
1178 if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
1179 this_v = compute_access (basetype_path, rval);
1180 if (TREE_CODE (rval) == CONST_DECL)
1182 if (this_v == access_private_node)
1183 errstr = "enum `%D' is a private value of class `%T'";
1184 else if (this_v == access_protected_node)
1185 errstr = "enum `%D' is a protected value of class `%T'";
1189 if (this_v == access_private_node)
1190 errstr = "member `%D' is a private member of class `%T'";
1191 else if (this_v == access_protected_node)
1192 errstr = "member `%D' is a protected member of class `%T'";
1200 /* This depends on behavior of lookup_field_1! */
1201 tree error_string = my_build_string (errstr);
1202 TREE_TYPE (entry) = error_string;
1206 /* Let entry know there is no problem with this access. */
1207 TREE_TYPE (entry) = NULL_TREE;
1209 TREE_VALUE (entry) = rval;
1212 if (errstr && protect)
1214 cp_error (errstr, name, type);
1215 return error_mark_node;
1220 basetype_chain = build_tree_list (NULL_TREE, basetype_path);
1221 TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
1222 TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
1223 TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
1225 /* The ambiguity check relies upon breadth first searching. */
1227 search_stack = push_search_level (search_stack, &search_obstack);
1228 binfo = basetype_path;
1233 tree binfos = BINFO_BASETYPES (binfo);
1234 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1237 /* Process and/or queue base types. */
1238 for (i = 0; i < n_baselinks; i++)
1240 tree base_binfo = TREE_VEC_ELT (binfos, i);
1241 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
1245 SET_BINFO_FIELDS_MARKED (base_binfo);
1246 btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
1247 TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
1248 TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
1249 TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
1250 if (TREE_VIA_VIRTUAL (base_binfo))
1251 btypes = tree_cons (NULL_TREE,
1252 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1255 btypes = tree_cons (NULL_TREE,
1256 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1258 obstack_ptr_grow (&search_obstack, btypes);
1260 if (tail >= search_stack->limit)
1261 my_friendly_abort (98);
1265 /* Process head of queue, if one exists. */
1269 basetype_chain = search_stack->first[head++];
1270 binfo_h = TREE_VALUE (basetype_chain);
1271 basetype_chain = TREE_CHAIN (basetype_chain);
1272 basetype_path = TREE_VALUE (basetype_chain);
1273 if (TREE_CHAIN (basetype_chain))
1274 BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
1276 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1278 binfo = basetype_path;
1279 type = BINFO_TYPE (binfo);
1281 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1282 and we do find NAME in TYPE, verify that such a second
1283 sighting is in fact valid. */
1285 nval = lookup_field_1 (type, name);
1287 if (nval || lookup_fnfields_here (type, name)>=0)
1289 if (nval && nval == rval && SHARED_MEMBER_P (nval))
1291 /* This is ok, the member found is the same [class.ambig] */
1293 else if (rval_binfo && hides (rval_binfo_h, binfo_h))
1295 /* This is ok, the member found is in rval_binfo, not
1298 else if (rval_binfo==NULL_TREE || hides (binfo_h, rval_binfo_h))
1300 /* This is ok, the member found is here (binfo), not in
1305 if (entry || protect)
1306 this_v = compute_access (basetype_path, rval);
1307 /* These may look ambiguous, but they really are not. */
1313 /* Undo finding it before, as something else hides it. */
1317 rval_binfo_h = binfo_h;
1321 /* This is ambiguous. */
1322 errstr = "request for member `%D' is ambiguous";
1329 tree *tp = search_stack->first;
1330 tree *search_tail = tp + tail;
1333 TREE_VALUE (entry) = rval;
1337 type = BINFO_TYPE (rval_binfo);
1343 if (TREE_CODE (rval) != TYPE_DECL)
1345 rval = purpose_member (name, CLASSTYPE_TAGS (type));
1347 rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
1352 if (TREE_CODE (rval) == TYPE_DECL
1353 && lookup_fnfields_here (type, name) >= 0)
1359 if (rval == NULL_TREE)
1362 /* If this FIELD_DECL defines its own access level, deal with that. */
1363 if (rval && errstr == 0
1364 && ((protect&1) || entry)
1365 && DECL_LANG_SPECIFIC (rval)
1366 && DECL_ACCESS (rval))
1368 while (tp < search_tail)
1370 /* If is possible for one of the derived types on the path to
1371 have defined special access for this field. Look for such
1372 declarations and report an error if a conflict is found. */
1375 if (this_v != access_default_node)
1376 new_v = compute_access (TREE_VALUE (TREE_CHAIN (*tp)), rval);
1377 if (this_v != access_default_node && new_v != this_v)
1379 errstr = "conflicting access to member `%D'";
1380 this_v = access_default_node;
1383 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1389 while (tp < search_tail)
1391 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1396 search_stack = pop_search_level (search_stack);
1400 if (own_access == access_private_node)
1401 errstr = "member `%D' declared private";
1402 else if (own_access == access_protected_node)
1403 errstr = "member `%D' declared protected";
1404 else if (this_v == access_private_node)
1405 errstr = TREE_PRIVATE (rval)
1406 ? "member `%D' is private"
1407 : "member `%D' is from private base class";
1408 else if (this_v == access_protected_node)
1409 errstr = TREE_PROTECTED (rval)
1410 ? "member `%D' is protected"
1411 : "member `%D' is from protected base class";
1418 tree error_string = my_build_string (errstr);
1419 /* Save error message with entry. */
1420 TREE_TYPE (entry) = error_string;
1424 /* Mark entry as having no error string. */
1425 TREE_TYPE (entry) = NULL_TREE;
1429 if (errstr && protect)
1431 cp_error (errstr, name, type);
1432 rval = error_mark_node;
1437 /* Try to find NAME inside a nested class. */
1439 lookup_nested_field (name, complain)
1445 tree id = NULL_TREE;
1446 if (TREE_CHAIN (current_class_type))
1448 /* Climb our way up the nested ladder, seeing if we're trying to
1449 modify a field in an enclosing class. If so, we should only
1450 be able to modify if it's static. */
1451 for (t = TREE_CHAIN (current_class_type);
1452 t && DECL_CONTEXT (t);
1453 t = TREE_CHAIN (DECL_CONTEXT (t)))
1455 if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE)
1458 /* N.B.: lookup_field will do the access checking for us */
1459 id = lookup_field (DECL_CONTEXT (t), name, complain, 0);
1460 if (id == error_mark_node)
1466 if (id != NULL_TREE)
1468 if (TREE_CODE (id) == FIELD_DECL
1469 && ! TREE_STATIC (id)
1470 && TREE_TYPE (id) != error_mark_node)
1474 /* At parse time, we don't want to give this error, since
1475 we won't have enough state to make this kind of
1476 decision properly. But there are times (e.g., with
1477 enums in nested classes) when we do need to call
1478 this fn at parse time. So, in those cases, we pass
1479 complain as a 0 and just return a NULL_TREE. */
1480 error ("assignment to non-static member `%s' of enclosing class `%s'",
1481 lang_printable_name (id),
1482 IDENTIFIER_POINTER (TYPE_IDENTIFIER
1483 (DECL_CONTEXT (t))));
1484 /* Mark this for do_identifier(). It would otherwise
1485 claim that the variable was undeclared. */
1486 TREE_TYPE (id) = error_mark_node;
1502 /* TYPE is a class type. Return the index of the fields within
1503 the method vector with name NAME, or -1 is no such field exists. */
1505 lookup_fnfields_1 (type, name)
1508 register tree method_vec = CLASSTYPE_METHOD_VEC (type);
1510 if (method_vec != 0)
1512 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1513 register tree *end = TREE_VEC_END (method_vec);
1515 #ifdef GATHER_STATISTICS
1516 n_calls_lookup_fnfields_1++;
1518 if (*methods && name == constructor_name (type))
1521 while (++methods != end)
1523 #ifdef GATHER_STATISTICS
1524 n_outer_fields_searched++;
1526 if (DECL_NAME (*methods) == name)
1530 return methods - &TREE_VEC_ELT (method_vec, 0);
1536 /* Starting from BASETYPE, return a TREE_BASELINK-like object
1537 which gives the following information (in a list):
1539 TREE_TYPE: list of basetypes needed to get to...
1540 TREE_VALUE: list of all functions in of given type
1541 which have name NAME.
1543 No access information is computed by this function,
1544 other then to adorn the list of basetypes with
1547 If there are two ways to find a name (two members), if COMPLAIN is
1548 non-zero, then error_mark_node is returned, and an error message is
1549 printed, otherwise, just an error_mark_node is returned.
1551 As a special case, is COMPLAIN is -1, we don't complain, and we
1552 don't return error_mark_node, but rather the complete list of
1553 virtuals. This is used by get_virtuals_named_this. */
1555 lookup_fnfields (basetype_path, name, complain)
1556 tree basetype_path, name;
1559 int head = 0, tail = 0;
1560 tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE, rval_binfo_h;
1561 tree entry, binfo, basetype_chain, binfo_h;
1564 /* rval_binfo is the binfo associated with the found member, note,
1565 this can be set with useful information, even when rval is not
1566 set, because it must deal with ALL members, not just function
1567 members. It is used for ambiguity checking and the hidden
1568 checks. Whereas rval is only set if a proper (not hidden)
1569 function member is found. */
1571 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1572 hiding checks, as virtual base classes may not be shared. The strategy
1573 is we always go into the the binfo hierarchy owned by TYPE_BINFO of
1574 virtual base classes, as we cross virtual base class lines. This way
1575 we know that binfo of a virtual base class will always == itself when
1576 found along any line. (mrs) */
1578 /* For now, don't try this. */
1579 int protect = complain;
1583 /* Set this to nonzero if we don't know how to compute
1584 accurate error messages for access control. */
1585 int index = MEMOIZED_HASH_FN (name);
1590 protect = complain = 0;
1593 /* If we are looking for a constructor in a templated type, use the
1594 unspecialized name, as that is how we store it. */
1595 if (IDENTIFIER_TEMPLATE (name))
1596 name = constructor_name (name);
1598 binfo = basetype_path;
1600 type = BINFO_TYPE (basetype_path);
1602 /* The memoization code is in need of maintenance. */
1603 if (!find_all && CLASSTYPE_MTABLE_ENTRY (type))
1605 tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
1607 while (tem && TREE_PURPOSE (tem) != name)
1609 memoized_fields_searched[1]++;
1610 tem = TREE_CHAIN (tem);
1614 if (protect && TREE_TYPE (tem))
1616 error (TREE_STRING_POINTER (TREE_TYPE (tem)),
1617 IDENTIFIER_POINTER (name),
1618 TYPE_NAME_STRING (DECL_CLASS_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
1619 return error_mark_node;
1621 if (TREE_VALUE (tem) == NULL_TREE)
1623 memoized_fast_rejects[1] += 1;
1628 /* Want to return this, but we must make sure
1629 that access information is consistent. */
1630 tree baselink = TREE_VALUE (tem);
1631 tree memoized_basetypes = TREE_PURPOSE (baselink);
1632 tree these_basetypes = basetype_path;
1633 while (memoized_basetypes && these_basetypes)
1635 memoized_fields_searched[1]++;
1636 if (TREE_VALUE (memoized_basetypes) != these_basetypes)
1638 memoized_basetypes = TREE_CHAIN (memoized_basetypes);
1639 these_basetypes = BINFO_INHERITANCE_CHAIN (these_basetypes);
1641 /* The following statement is true only when both are NULL. */
1642 if (memoized_basetypes == these_basetypes)
1644 memoized_fast_finds[1] += 1;
1645 return TREE_VALUE (tem);
1647 /* else, we must re-find this field by hand. */
1648 baselink = tree_cons (basetype_path, TREE_VALUE (baselink), TREE_CHAIN (baselink));
1654 #ifdef GATHER_STATISTICS
1655 n_calls_lookup_fnfields++;
1657 if (protect && flag_memoize_lookups && ! global_bindings_p ())
1658 entry = make_memoized_table_entry (type, name, 1);
1662 index = lookup_fnfields_here (type, name);
1663 if (index >= 0 || lookup_field_1 (type, name))
1665 rval_binfo = basetype_path;
1666 rval_binfo_h = rval_binfo;
1671 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
1672 rvals = my_tree_cons (basetype_path, rval, rvals);
1673 if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type))
1674 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
1678 TREE_VALUE (entry) = rvals;
1679 TREE_TYPE (entry) = NULL_TREE;
1686 if (basetype_path == TYPE_BINFO (type))
1688 basetype_chain = CLASSTYPE_BINFO_AS_LIST (type);
1689 TREE_VIA_PUBLIC (basetype_chain) = 1;
1690 BINFO_VIA_PUBLIC (basetype_path) = 1;
1691 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1695 basetype_chain = build_tree_list (NULL_TREE, basetype_path);
1696 TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
1697 TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
1698 TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
1701 /* The ambiguity check relies upon breadth first searching. */
1703 search_stack = push_search_level (search_stack, &search_obstack);
1704 binfo = basetype_path;
1709 tree binfos = BINFO_BASETYPES (binfo);
1710 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1713 /* Process and/or queue base types. */
1714 for (i = 0; i < n_baselinks; i++)
1716 tree base_binfo = TREE_VEC_ELT (binfos, i);
1717 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
1721 SET_BINFO_FIELDS_MARKED (base_binfo);
1722 btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
1723 TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
1724 TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
1725 TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
1726 if (TREE_VIA_VIRTUAL (base_binfo))
1727 btypes = tree_cons (NULL_TREE,
1728 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1731 btypes = tree_cons (NULL_TREE,
1732 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1734 obstack_ptr_grow (&search_obstack, btypes);
1736 if (tail >= search_stack->limit)
1737 my_friendly_abort (99);
1741 /* Process head of queue, if one exists. */
1745 basetype_chain = search_stack->first[head++];
1746 binfo_h = TREE_VALUE (basetype_chain);
1747 basetype_chain = TREE_CHAIN (basetype_chain);
1748 basetype_path = TREE_VALUE (basetype_chain);
1749 if (TREE_CHAIN (basetype_chain))
1750 BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
1752 BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
1754 binfo = basetype_path;
1755 type = BINFO_TYPE (binfo);
1757 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1758 and we do find NAME in TYPE, verify that such a second
1759 sighting is in fact valid. */
1761 index = lookup_fnfields_here (type, name);
1763 if (index >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all))
1765 if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h))
1767 /* This is ok, the member found is in rval_binfo, not
1770 else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h))
1772 /* This is ok, the member found is here (binfo), not in
1776 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
1777 /* Note, rvals can only be previously set if find_all is
1779 rvals = my_tree_cons (basetype_path, rval, rvals);
1780 if (TYPE_BINFO_BASETYPES (type)
1781 && CLASSTYPE_BASELINK_VEC (type))
1782 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
1786 /* Undo finding it before, as something else hides it. */
1791 rval_binfo_h = binfo_h;
1795 /* This is ambiguous. */
1796 errstr = "request for method `%D' is ambiguous";
1797 rvals = error_mark_node;
1803 tree *tp = search_stack->first;
1804 tree *search_tail = tp + tail;
1806 while (tp < search_tail)
1808 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1812 search_stack = pop_search_level (search_stack);
1818 tree error_string = my_build_string (errstr);
1819 /* Save error message with entry. */
1820 TREE_TYPE (entry) = error_string;
1824 /* Mark entry as having no error string. */
1825 TREE_TYPE (entry) = NULL_TREE;
1826 TREE_VALUE (entry) = rvals;
1830 if (errstr && protect)
1832 cp_error (errstr, name);
1833 rvals = error_mark_node;
1839 /* BREADTH-FIRST SEARCH ROUTINES. */
1841 /* Search a multiple inheritance hierarchy by breadth-first search.
1843 TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
1844 TESTFN is a function, which, if true, means that our condition has been met,
1845 and its return value should be returned.
1846 QFN, if non-NULL, is a predicate dictating whether the type should
1850 breadth_first_search (binfo, testfn, qfn)
1855 int head = 0, tail = 0;
1858 search_stack = push_search_level (search_stack, &search_obstack);
1862 tree binfos = BINFO_BASETYPES (binfo);
1863 int n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1866 /* Process and/or queue base types. */
1867 for (i = 0; i < n_baselinks; i++)
1869 tree base_binfo = TREE_VEC_ELT (binfos, i);
1871 if (BINFO_MARKED (base_binfo) == 0
1872 && (qfn == 0 || (*qfn) (binfo, i)))
1874 SET_BINFO_MARKED (base_binfo);
1875 obstack_ptr_grow (&search_obstack, binfo);
1876 obstack_ptr_grow (&search_obstack, (HOST_WIDE_INT) i);
1878 if (tail >= search_stack->limit)
1879 my_friendly_abort (100);
1882 /* Process head of queue, if one exists. */
1889 binfo = search_stack->first[head++];
1890 i = (HOST_WIDE_INT) search_stack->first[head++];
1891 if (rval = (*testfn) (binfo, i))
1893 binfo = BINFO_BASETYPE (binfo, i);
1896 tree *tp = search_stack->first;
1897 tree *search_tail = tp + tail;
1898 while (tp < search_tail)
1901 int i = (HOST_WIDE_INT)(*tp++);
1902 CLEAR_BINFO_MARKED (BINFO_BASETYPE (binfo, i));
1906 search_stack = pop_search_level (search_stack);
1910 /* Functions to use in breadth first searches. */
1911 typedef tree (*pft)();
1912 typedef int (*pfi)();
1914 int tree_needs_constructor_p (binfo, i)
1919 my_friendly_assert (i != 0, 296);
1920 basetype = BINFO_TYPE (BINFO_BASETYPE (binfo, i));
1921 return TYPE_NEEDS_CONSTRUCTING (basetype);
1924 static tree declarator;
1927 get_virtuals_named_this (binfo)
1932 fields = lookup_fnfields (binfo, declarator, -1);
1933 /* fields cannot be error_mark_node */
1938 /* Get to the function decls, and return the first virtual function
1939 with this name, if there is one. */
1944 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = DECL_CHAIN (fndecl))
1945 if (DECL_VINDEX (fndecl))
1947 fields = next_baselink (fields);
1952 static tree get_virtual_destructor (binfo, i)
1956 tree type = BINFO_TYPE (binfo);
1958 type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
1959 if (TYPE_HAS_DESTRUCTOR (type)
1960 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
1961 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1965 int tree_has_any_destructor_p (binfo, i)
1969 tree type = BINFO_TYPE (binfo);
1971 type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
1972 return TYPE_NEEDS_DESTRUCTOR (type);
1975 /* Given a class type TYPE, and a function decl FNDECL, look for a
1976 virtual function in TYPE's hierarchy which FNDECL could match as a
1977 virtual function. It doesn't matter which one we find.
1979 DTORP is nonzero if we are looking for a destructor. Destructors
1980 need special treatment because they do not match by name. */
1982 get_matching_virtual (binfo, fndecl, dtorp)
1986 tree tmp = NULL_TREE;
1988 /* Breadth first search routines start searching basetypes
1989 of TYPE, so we must perform first ply of search here. */
1992 if (tree_has_any_destructor_p (binfo, -1))
1993 tmp = get_virtual_destructor (binfo, -1);
1998 tmp = (tree) breadth_first_search (binfo,
1999 (pfi) get_virtual_destructor,
2000 tree_has_any_destructor_p);
2005 tree drettype, dtypes, btypes, instptr_type;
2006 tree basetype = DECL_CLASS_CONTEXT (fndecl);
2007 tree baselink, best = NULL_TREE;
2008 tree name = DECL_ASSEMBLER_NAME (fndecl);
2010 declarator = DECL_NAME (fndecl);
2011 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
2014 baselink = get_virtuals_named_this (binfo);
2015 if (baselink == NULL_TREE)
2018 drettype = TREE_TYPE (TREE_TYPE (fndecl));
2019 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2020 if (DECL_STATIC_FUNCTION_P (fndecl))
2021 instptr_type = NULL_TREE;
2023 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
2025 for (; baselink; baselink = next_baselink (baselink))
2027 for (tmp = TREE_VALUE (baselink); tmp; tmp = DECL_CHAIN (tmp))
2029 if (! DECL_VINDEX (tmp))
2032 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
2033 if (instptr_type == NULL_TREE)
2035 if (compparms (TREE_CHAIN (btypes), dtypes, 3))
2036 /* Caller knows to give error in this case. */
2041 if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
2042 == TYPE_READONLY (instptr_type))
2043 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3))
2045 tree brettype = TREE_TYPE (TREE_TYPE (tmp));
2046 if (comptypes (brettype, drettype, 1))
2049 (TREE_CODE (brettype) == TREE_CODE (drettype)
2050 && (TREE_CODE (brettype) == POINTER_TYPE
2051 || TREE_CODE (brettype) == REFERENCE_TYPE)
2052 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (brettype)),
2053 TYPE_MAIN_VARIANT (TREE_TYPE (drettype)),
2055 /* covariant return type */
2057 tree b = TREE_TYPE (brettype), d = TREE_TYPE (drettype);
2058 if (TYPE_MAIN_VARIANT (b) != TYPE_MAIN_VARIANT (d))
2060 tree binfo = get_binfo (b, d, 1);
2061 if (binfo != error_mark_node
2062 && ! BINFO_OFFSET_ZEROP (binfo))
2063 sorry ("adjusting pointers for covariant returns");
2065 if (TYPE_READONLY (d) > TYPE_READONLY (b))
2067 cp_error ("return type of `%#D' adds const", fndecl);
2068 cp_error_at (" overriding definition as `%#D'",
2071 else if (TYPE_VOLATILE (d) > TYPE_VOLATILE (b))
2073 cp_error ("return type of `%#D' adds volatile",
2075 cp_error_at (" overriding definition as `%#D'",
2079 else if (IS_AGGR_TYPE_2 (brettype, drettype)
2080 && comptypes (brettype, drettype, 0))
2082 error ("invalid covariant return type (must use pointer or reference)");
2083 cp_error_at (" overriding `%#D'", tmp);
2084 cp_error (" with `%#D'", fndecl);
2086 else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE)
2088 cp_error ("conflicting return type specified for virtual function `%#D'", fndecl);
2089 cp_error_at (" overriding definition as `%#D'", tmp);
2090 SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
2101 if (best == NULL_TREE && warn_overloaded_virtual)
2102 cp_warning_at ("conflicting specification deriving virtual function `%D'", fndecl);
2108 /* Return the list of virtual functions which are abstract in type
2109 TYPE that come from non virtual base classes. See
2110 expand_direct_vtbls_init for the style of search we do. */
2112 get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
2113 tree binfo, abstract_virtuals;
2116 tree binfos = BINFO_BASETYPES (binfo);
2117 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2119 for (i = 0; i < n_baselinks; i++)
2121 tree base_binfo = TREE_VEC_ELT (binfos, i);
2122 int is_not_base_vtable =
2123 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2124 if (! TREE_VIA_VIRTUAL (base_binfo))
2126 = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
2129 /* Should we use something besides CLASSTYPE_VFIELDS? */
2130 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2132 tree virtuals = BINFO_VIRTUALS (binfo);
2134 skip_rtti_stuff (&virtuals);
2138 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
2139 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
2140 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
2141 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
2142 virtuals = TREE_CHAIN (virtuals);
2145 return abstract_virtuals;
2148 /* Return the list of virtual functions which are abstract in type TYPE.
2149 This information is cached, and so must be built on a
2150 non-temporary obstack. */
2152 get_abstract_virtuals (type)
2156 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
2158 /* First get all from non-virtual bases. */
2160 = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
2162 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
2164 tree virtuals = BINFO_VIRTUALS (vbases);
2166 skip_rtti_stuff (&virtuals);
2170 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
2171 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
2172 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
2173 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
2174 virtuals = TREE_CHAIN (virtuals);
2177 return nreverse (abstract_virtuals);
2180 /* For the type TYPE, return a list of member functions available from
2181 base classes with name NAME. The TREE_VALUE of the list is a chain of
2182 member functions with name NAME. The TREE_PURPOSE of the list is a
2183 basetype, or a list of base types (in reverse order) which were
2184 traversed to reach the chain of member functions. If we reach a base
2185 type which provides a member function of name NAME, and which has at
2186 most one base type itself, then we can terminate the search. */
2189 get_baselinks (type_as_binfo_list, type, name)
2190 tree type_as_binfo_list;
2193 int head = 0, tail = 0, index;
2194 tree rval = 0, nval = 0;
2195 tree basetypes = type_as_binfo_list;
2196 tree binfo = TYPE_BINFO (type);
2198 search_stack = push_search_level (search_stack, &search_obstack);
2202 tree binfos = BINFO_BASETYPES (binfo);
2203 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2205 /* Process and/or queue base types. */
2206 for (i = 0; i < n_baselinks; i++)
2208 tree base_binfo = TREE_VEC_ELT (binfos, i);
2211 btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo),
2212 TREE_VIA_VIRTUAL (base_binfo),
2213 TREE_VIA_PROTECTED (base_binfo),
2214 NULL_TREE, base_binfo,
2216 obstack_ptr_grow (&search_obstack, btypes);
2217 search_stack->first = (tree *)obstack_base (&search_obstack);
2222 /* Process head of queue, if one exists. */
2226 basetypes = search_stack->first[head++];
2227 binfo = TREE_VALUE (basetypes);
2228 type = BINFO_TYPE (binfo);
2229 index = lookup_fnfields_1 (type, name);
2232 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
2233 rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval);
2234 if (TYPE_BINFO_BASETYPES (type) == 0)
2236 else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1)
2238 if (CLASSTYPE_BASELINK_VEC (type))
2239 TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
2246 search_stack = pop_search_level (search_stack);
2251 next_baselink (baselink)
2254 tree tmp = TREE_TYPE (baselink);
2255 baselink = TREE_CHAIN (baselink);
2258 /* @@ does not yet add previous base types. */
2259 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2261 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2262 tmp = TREE_CHAIN (tmp);
2267 /* DEPTH-FIRST SEARCH ROUTINES. */
2269 /* Assign unique numbers to _CLASSTYPE members of the lattice
2270 specified by TYPE. The root nodes are marked first; the nodes
2271 are marked depth-fisrt, left-right. */
2275 /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
2276 Relation yields 1 if C1 <= C2, 0 otherwise. */
2277 typedef char mi_boolean;
2278 static mi_boolean *mi_matrix;
2280 /* Type for which this matrix is defined. */
2281 static tree mi_type;
2283 /* Size of the matrix for indexing purposes. */
2286 /* Return nonzero if class C2 derives from class C1. */
2287 #define BINFO_DERIVES_FROM(C1, C2) \
2288 ((mi_matrix+mi_size*(BINFO_CID (C1)-1))[BINFO_CID (C2)-1])
2289 #define TYPE_DERIVES_FROM(C1, C2) \
2290 ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
2291 #define BINFO_DERIVES_FROM_STAR(C) \
2292 (mi_matrix+(BINFO_CID (C)-1))
2294 /* This routine converts a pointer to be a pointer of an immediate
2295 base class. The normal convert_pointer_to routine would diagnose
2296 the conversion as ambiguous, under MI code that has the base class
2297 as an ambiguous base class. */
2299 convert_pointer_to_single_level (to_type, expr)
2302 tree binfo_of_derived;
2305 binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)));
2306 last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0);
2307 BINFO_INHERITANCE_CHAIN (last) = binfo_of_derived;
2308 BINFO_INHERITANCE_CHAIN (binfo_of_derived) = NULL_TREE;
2309 return build_vbase_path (PLUS_EXPR, build_pointer_type (to_type), expr, last, 1);
2312 /* The main function which implements depth first search.
2314 This routine has to remember the path it walked up, when
2315 dfs_init_vbase_pointers is the work function, as otherwise there
2316 would be no record. */
2318 dfs_walk (binfo, fn, qfn)
2323 tree binfos = BINFO_BASETYPES (binfo);
2324 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2326 for (i = 0; i < n_baselinks; i++)
2328 tree base_binfo = TREE_VEC_ELT (binfos, i);
2330 if (qfn == 0 || (*qfn)(base_binfo))
2332 if (fn == dfs_init_vbase_pointers)
2334 /* When traversing an arbitrary MI hierarchy, we need to keep
2335 a record of the path we took to get down to the final base
2336 type, as otherwise there would be no record of it, and just
2337 trying to blindly convert at the bottom would be ambiguous.
2339 The easiest way is to do the conversions one step at a time,
2340 as we know we want the immediate base class at each step.
2342 The only special trick to converting one step at a time,
2343 is that when we hit the last virtual base class, we must
2344 use the SLOT value for it, and not use the normal convert
2345 routine. We use the last virtual base class, as in our
2346 implementation, we have pointers to all virtual base
2347 classes in the base object. */
2349 tree saved_vbase_decl_ptr_intermediate
2350 = vbase_decl_ptr_intermediate;
2352 if (TREE_VIA_VIRTUAL (base_binfo))
2354 /* No need for the conversion here, as we know it is the
2356 vbase_decl_ptr_intermediate
2357 = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo));
2361 vbase_decl_ptr_intermediate
2362 = convert_pointer_to_single_level (BINFO_TYPE (base_binfo),
2363 vbase_decl_ptr_intermediate);
2366 dfs_walk (base_binfo, fn, qfn);
2368 vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate;
2370 dfs_walk (base_binfo, fn, qfn);
2377 /* Predicate functions which serve for dfs_walk. */
2378 static int numberedp (binfo) tree binfo;
2379 { return BINFO_CID (binfo); }
2380 static int unnumberedp (binfo) tree binfo;
2381 { return BINFO_CID (binfo) == 0; }
2383 static int markedp (binfo) tree binfo;
2384 { return BINFO_MARKED (binfo); }
2385 static int bfs_markedp (binfo, i) tree binfo; int i;
2386 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); }
2387 static int unmarkedp (binfo) tree binfo;
2388 { return BINFO_MARKED (binfo) == 0; }
2389 static int bfs_unmarkedp (binfo, i) tree binfo; int i;
2390 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2391 static int marked_vtable_pathp (binfo) tree binfo;
2392 { return BINFO_VTABLE_PATH_MARKED (binfo); }
2393 static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i;
2394 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); }
2395 static int unmarked_vtable_pathp (binfo) tree binfo;
2396 { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; }
2397 static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i;
2398 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2399 static int marked_new_vtablep (binfo) tree binfo;
2400 { return BINFO_NEW_VTABLE_MARKED (binfo); }
2401 static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i;
2402 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); }
2403 static int unmarked_new_vtablep (binfo) tree binfo;
2404 { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; }
2405 static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i;
2406 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2408 static int dfs_search_slot_nonempty_p (binfo) tree binfo;
2409 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
2411 static int dfs_debug_unmarkedp (binfo) tree binfo;
2412 { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; }
2414 /* The worker functions for `dfs_walk'. These do not need to
2415 test anything (vis a vis marking) if they are paired with
2416 a predicate function (above). */
2418 /* Assign each type within the lattice a number which is unique
2419 in the lattice. The first number assigned is 1. */
2425 BINFO_CID (binfo) = ++cid;
2429 dfs_unnumber (binfo)
2432 BINFO_CID (binfo) = 0;
2436 dfs_mark (binfo) tree binfo;
2437 { SET_BINFO_MARKED (binfo); }
2440 dfs_unmark (binfo) tree binfo;
2441 { CLEAR_BINFO_MARKED (binfo); }
2444 dfs_mark_vtable_path (binfo) tree binfo;
2445 { SET_BINFO_VTABLE_PATH_MARKED (binfo); }
2448 dfs_unmark_vtable_path (binfo) tree binfo;
2449 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
2452 dfs_mark_new_vtable (binfo) tree binfo;
2453 { SET_BINFO_NEW_VTABLE_MARKED (binfo); }
2456 dfs_unmark_new_vtable (binfo) tree binfo;
2457 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
2460 dfs_clear_search_slot (binfo) tree binfo;
2461 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
2464 dfs_debug_mark (binfo)
2467 tree t = BINFO_TYPE (binfo);
2469 /* Use heuristic that if there are virtual functions,
2470 ignore until we see a non-inline virtual function. */
2471 tree methods = CLASSTYPE_METHOD_VEC (t);
2473 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2475 /* If interface info is known, the value of (?@@?) is correct. */
2477 || CLASSTYPE_INTERFACE_KNOWN (t)
2478 || (write_virtuals == 2 && TYPE_VIRTUAL_P (t)))
2481 /* If debug info is requested from this context for this type, supply it.
2482 If debug info is requested from another context for this type,
2483 see if some third context can supply it. */
2484 if (current_function_decl == NULL_TREE
2485 || DECL_CLASS_CONTEXT (current_function_decl) != t)
2487 if (TREE_VEC_ELT (methods, 0))
2488 methods = TREE_VEC_ELT (methods, 0);
2490 methods = TREE_VEC_ELT (methods, 1);
2493 if (DECL_VINDEX (methods)
2494 && DECL_THIS_INLINE (methods) == 0
2495 && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
2497 /* Somebody, somewhere is going to have to define this
2498 virtual function. When they do, they will provide
2499 the debugging info. */
2502 methods = TREE_CHAIN (methods);
2505 /* We cannot rely on some alien method to solve our problems,
2506 so we must write out the debug info ourselves. */
2507 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
2508 rest_of_type_compilation (t, global_bindings_p ());
2511 /* Attach to the type of the virtual base class, the pointer to the
2512 virtual base class, given the global pointer vbase_decl_ptr.
2514 We use the global vbase_types. ICK! */
2516 dfs_find_vbases (binfo)
2519 tree binfos = BINFO_BASETYPES (binfo);
2520 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2522 for (i = n_baselinks-1; i >= 0; i--)
2524 tree base_binfo = TREE_VEC_ELT (binfos, i);
2526 if (TREE_VIA_VIRTUAL (base_binfo)
2527 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
2529 tree vbase = BINFO_TYPE (base_binfo);
2530 tree binfo = binfo_member (vbase, vbase_types);
2532 CLASSTYPE_SEARCH_SLOT (vbase)
2533 = (char *) build (PLUS_EXPR, build_pointer_type (vbase),
2534 vbase_decl_ptr, BINFO_OFFSET (binfo));
2537 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2538 SET_BINFO_NEW_VTABLE_MARKED (binfo);
2542 dfs_init_vbase_pointers (binfo)
2545 tree type = BINFO_TYPE (binfo);
2546 tree fields = TYPE_FIELDS (type);
2547 tree this_vbase_ptr;
2549 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2551 /* If there is a rtti, it is the first field, though perhaps from
2552 the base class. Otherwise, the first fields are virtual base class
2554 if (CLASSTYPE_RTTI (type) && VFIELD_NAME_P (DECL_NAME (fields)))
2555 /* Get past vtable for the object. */
2556 fields = TREE_CHAIN (fields);
2558 if (fields == NULL_TREE
2559 || DECL_NAME (fields) == NULL_TREE
2560 || ! VBASE_NAME_P (DECL_NAME (fields)))
2563 this_vbase_ptr = vbase_decl_ptr_intermediate;
2565 if (build_pointer_type (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
2566 my_friendly_abort (125);
2568 while (fields && DECL_NAME (fields)
2569 && VBASE_NAME_P (DECL_NAME (fields)))
2571 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2572 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
2573 tree init = (tree)CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
2574 vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
2576 build_modify_expr (ref, NOP_EXPR, init),
2578 fields = TREE_CHAIN (fields);
2582 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2583 times, just NEW_VTABLE, but optimizer should make both with equal
2584 efficiency (though it does not currently). */
2586 dfs_clear_vbase_slots (binfo)
2589 tree type = BINFO_TYPE (binfo);
2590 CLASSTYPE_SEARCH_SLOT (type) = 0;
2591 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2592 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2596 init_vbase_pointers (type, decl_ptr)
2600 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2602 int old_flag = flag_this_is_variable;
2603 tree binfo = TYPE_BINFO (type);
2604 flag_this_is_variable = -2;
2605 vbase_types = CLASSTYPE_VBASECLASSES (type);
2606 vbase_decl_ptr = decl_ptr;
2607 vbase_decl = build_indirect_ref (decl_ptr, NULL_PTR);
2608 vbase_decl_ptr_intermediate = vbase_decl_ptr;
2609 vbase_init_result = NULL_TREE;
2610 dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp);
2611 dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp);
2612 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2613 flag_this_is_variable = old_flag;
2614 return vbase_init_result;
2619 /* get the virtual context (the vbase that directly contains the
2620 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2621 or NULL_TREE if there is none.
2623 FNDECL must come from a virtual table from a virtual base to ensure that
2624 there is only one possible DECL_CLASS_CONTEXT.
2626 We know that if there is more than one place (binfo) the fndecl that the
2627 declared, they all refer to the same binfo. See get_class_offset_1 for
2628 the check that ensures this. */
2630 virtual_context (fndecl, t, vbase)
2631 tree fndecl, t, vbase;
2634 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
2636 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2637 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0)
2641 /* Not sure if checking path == vbase is necessary here, but just in
2643 if (TREE_VIA_VIRTUAL (path) || path == vbase)
2644 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2645 path = BINFO_INHERITANCE_CHAIN (path);
2648 /* This shouldn't happen, I don't want errors! */
2649 warning ("recoverable compiler error, fixups for virtual function");
2654 if (TREE_VIA_VIRTUAL (path))
2655 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2656 path = BINFO_INHERITANCE_CHAIN (path);
2661 /* Fixups upcast offsets for one vtable.
2662 Entries may stay within the VBASE given, or
2663 they may upcast into a direct base, or
2664 they may upcast into a different vbase.
2666 We only need to do fixups in case 2 and 3. In case 2, we add in
2667 the virtual base offset to effect an upcast, in case 3, we add in
2668 the virtual base offset to effect an upcast, then subtract out the
2669 offset for the other virtual base, to effect a downcast into it.
2671 This routine mirrors fixup_vtable_deltas in functionality, though
2672 this one is runtime based, and the other is compile time based.
2673 Conceivably that routine could be removed entirely, and all fixups
2676 VBASE_OFFSETS is an association list of virtual bases that contains
2677 offset information for the virtual bases, so the offsets are only
2678 calculated once. The offsets are computed by where we think the
2679 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2680 the vbase really is. */
2682 expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t,
2684 tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets;
2686 tree virtuals = BINFO_VIRTUALS (binfo);
2689 unsigned HOST_WIDE_INT n;
2691 delta = purpose_member (vbase, *vbase_offsets);
2694 delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
2695 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr);
2696 delta = save_expr (delta);
2697 delta = tree_cons (vbase, delta, *vbase_offsets);
2698 *vbase_offsets = delta;
2701 n = skip_rtti_stuff (&virtuals);
2705 tree current_fndecl = TREE_VALUE (virtuals);
2706 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2707 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2709 && current_fndecl != abort_fndecl
2710 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2712 /* This may in fact need a runtime fixup. */
2713 tree idx = DECL_VINDEX (current_fndecl);
2714 tree vtbl = BINFO_VTABLE (binfo);
2715 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2716 tree aref, ref, naref;
2717 tree old_delta, new_delta;
2720 if (nvtbl == NULL_TREE
2721 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2723 /* Dup it if it isn't in local scope yet. */
2724 nvtbl = build_decl (VAR_DECL,
2726 TYPE_MAIN_VARIANT (TREE_TYPE (BINFO_VTABLE (binfo))));
2727 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2728 DECL_ALIGN (nvtbl));
2729 TREE_READONLY (nvtbl) = 0;
2730 nvtbl = pushdecl (nvtbl);
2732 cp_finish_decl (nvtbl, init, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
2733 DECL_VIRTUAL_P (nvtbl) = 1;
2734 DECL_CONTEXT (nvtbl) = t;
2735 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2737 TREE_SIDE_EFFECTS (init) = 1;
2738 expand_expr_stmt (init);
2739 /* Update the vtable pointers as necessary. */
2740 ref = build_vfield_ref (build_indirect_ref (addr, NULL_PTR), DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
2741 expand_expr_stmt (build_modify_expr (ref, NOP_EXPR,
2742 build_unary_op (ADDR_EXPR, nvtbl, 0)));
2744 assemble_external (vtbl);
2745 aref = build_array_ref (vtbl, idx);
2746 naref = build_array_ref (nvtbl, idx);
2747 old_delta = build_component_ref (aref, delta_identifier, 0, 0);
2748 new_delta = build_component_ref (naref, delta_identifier, 0, 0);
2750 /* This is a upcast, so we have to add the offset for the
2752 old_delta = build_binary_op (PLUS_EXPR, old_delta,
2753 TREE_VALUE (delta), 0);
2756 /* If this is set, we need to subtract out the delta
2757 adjustments for the other virtual base that we
2759 tree vc_delta = purpose_member (vc, *vbase_offsets);
2762 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
2763 vc_delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
2764 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
2766 vc_delta = save_expr (vc_delta);
2767 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2770 vc_delta = TREE_VALUE (vc_delta);
2772 /* This is a downcast, so we have to subtract the offset
2773 for the virtual base. */
2774 old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta, 0);
2777 TREE_READONLY (new_delta) = 0;
2778 expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
2782 virtuals = TREE_CHAIN (virtuals);
2786 /* Fixup upcast offsets for all direct vtables. Patterned after
2787 expand_direct_vtbls_init. */
2789 fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
2790 tree real_binfo, binfo, addr, orig_addr, type, vbase, *vbase_offsets;
2791 int init_self, can_elide;
2793 tree real_binfos = BINFO_BASETYPES (real_binfo);
2794 tree binfos = BINFO_BASETYPES (binfo);
2795 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
2797 for (i = 0; i < n_baselinks; i++)
2799 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
2800 tree base_binfo = TREE_VEC_ELT (binfos, i);
2801 int is_not_base_vtable =
2802 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
2803 if (! TREE_VIA_VIRTUAL (real_base_binfo))
2804 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
2805 is_not_base_vtable, can_elide, addr,
2806 orig_addr, type, vbase, vbase_offsets);
2809 /* Before turning this on, make sure it is correct. */
2810 if (can_elide && ! BINFO_MODIFIED (binfo))
2813 /* Should we use something besides CLASSTYPE_VFIELDS? */
2814 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
2816 tree new_addr = convert_pointer_to_real (binfo, addr);
2817 expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr,
2818 type, vbase_offsets);
2822 /* Build a COMPOUND_EXPR which when expanded will generate the code
2823 needed to initialize all the virtual function table slots of all
2824 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2825 the virtual baseclasses to use; TYPE is the type of the object to
2826 which the initialization applies. TRUE_EXP is the true object we
2827 are initializing, and DECL_PTR is the pointer to the sub-object we
2830 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
2831 object was laid out by a top-level constructor and the computed
2832 offsets are valid to store vtables. When zero, we must store new
2833 vtables through virtual baseclass pointers.
2835 We setup and use the globals: vbase_decl, vbase_decl_ptr, vbase_types
2839 expand_indirect_vtbls_init (binfo, true_exp, decl_ptr, use_computed_offsets)
2841 tree true_exp, decl_ptr;
2842 int use_computed_offsets;
2844 tree type = BINFO_TYPE (binfo);
2845 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2847 rtx fixup_insns = NULL_RTX;
2848 int old_flag = flag_this_is_variable;
2849 tree vbases = CLASSTYPE_VBASECLASSES (type);
2850 vbase_types = vbases;
2851 vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
2852 vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, NULL_PTR);
2854 if (use_computed_offsets)
2856 /* This is an object of type IN_TYPE, */
2857 flag_this_is_variable = -2;
2860 dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep);
2862 /* Initialized with vtables of type TYPE. */
2863 for (; vbases; vbases = TREE_CHAIN (vbases))
2866 if (use_computed_offsets)
2867 addr = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbases));
2871 addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vbase_decl_ptr);
2873 /* This should should never work better than the above. (mrs) */
2874 tree vbinfo = get_binfo (TREE_TYPE (vbases),
2875 TREE_TYPE (vbase_decl),
2878 /* See is we can get lucky. */
2879 if (TREE_VIA_VIRTUAL (vbinfo))
2880 addr = convert_pointer_to_real (vbinfo, vbase_decl_ptr);
2883 /* We go through all these contortions to avoid this
2884 call, as it will fail when the virtual base type
2885 is ambiguous from here. We don't yet have a way
2886 to search for and find just an instance of the
2887 virtual base class. Searching for the binfo in
2888 vbases won't work, as we don't have the vbase
2889 pointer field, for all vbases in the main class,
2890 only direct vbases. */
2891 addr = convert_pointer_to_real (TREE_TYPE (vbases),
2893 if (addr == error_mark_node)
2899 /* Do all vtables from this virtual base. */
2900 /* This assumes that virtual bases can never serve as parent
2901 binfos. (in the CLASSTPE_VFIELD_PARENT sense) */
2902 expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
2905 /* If we are using computed offsets we can skip fixups. */
2906 if (use_computed_offsets)
2909 /* Now we adjust the offsets for virtual functions that cross
2910 virtual boundaries on an implicit upcast on vf call so that
2911 the layout of the most complete type is used, instead of
2912 assuming the layout of the virtual bases from our current type. */
2914 if (flag_vtable_thunks)
2916 /* We don't have dynamic thunks yet! So for now, just fail silently. */
2920 tree vbase_offsets = NULL_TREE;
2921 push_to_sequence (fixup_insns);
2922 fixup_virtual_upcast_offsets (vbases,
2923 TYPE_BINFO (BINFO_TYPE (vbases)),
2924 1, 0, addr, vbase_decl_ptr,
2925 type, vbases, &vbase_offsets);
2926 fixup_insns = get_insns ();
2933 extern tree in_charge_identifier;
2934 tree in_charge_node = lookup_name (in_charge_identifier, 0);
2935 if (! in_charge_node)
2937 warning ("recoverable internal compiler error, nobody's in charge!");
2938 in_charge_node = integer_zero_node;
2940 in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1);
2941 expand_start_cond (in_charge_node, 0);
2942 emit_insns (fixup_insns);
2946 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2948 flag_this_is_variable = old_flag;
2953 clear_search_slots (type)
2956 dfs_walk (TYPE_BINFO (type),
2957 dfs_clear_search_slot, dfs_search_slot_nonempty_p);
2960 /* get virtual base class types.
2961 This adds type to the vbase_types list in reverse dfs order.
2962 Ordering is very important, so don't change it. */
2965 dfs_get_vbase_types (binfo)
2968 if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
2970 vbase_types = make_binfo (integer_zero_node, binfo,
2971 BINFO_VTABLE (binfo),
2972 BINFO_VIRTUALS (binfo), vbase_types);
2973 TREE_VIA_VIRTUAL (vbase_types) = 1;
2974 SET_BINFO_VBASE_MARKED (binfo);
2976 SET_BINFO_MARKED (binfo);
2979 /* get a list of virtual base classes in dfs order. */
2981 get_vbase_types (type)
2987 if (TREE_CODE (type) == TREE_VEC)
2990 binfo = TYPE_BINFO (type);
2992 vbase_types = NULL_TREE;
2993 dfs_walk (binfo, dfs_get_vbase_types, unmarkedp);
2994 dfs_walk (binfo, dfs_unmark, markedp);
2995 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2996 reverse it so that we get normal dfs ordering. */
2997 vbase_types = nreverse (vbase_types);
2999 /* unmark marked vbases */
3000 for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
3001 CLEAR_BINFO_VBASE_MARKED (vbases);
3007 dfs_record_inheritance (binfo)
3010 tree binfos = BINFO_BASETYPES (binfo);
3011 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3012 mi_boolean *derived_row = BINFO_DERIVES_FROM_STAR (binfo);
3014 for (i = n_baselinks-1; i >= 0; i--)
3017 tree base_binfo = TREE_VEC_ELT (binfos, i);
3018 tree baseclass = BINFO_TYPE (base_binfo);
3019 mi_boolean *base_row = BINFO_DERIVES_FROM_STAR (base_binfo);
3021 /* Don't search if there's nothing there! MI_SIZE can be
3022 zero as a result of parse errors. */
3023 if (TYPE_BINFO_BASETYPES (baseclass) && mi_size > 0)
3024 for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
3025 derived_row[j] |= base_row[j];
3026 TYPE_DERIVES_FROM (baseclass, BINFO_TYPE (binfo)) = 1;
3029 SET_BINFO_MARKED (binfo);
3032 /* Given a _CLASSTYPE node in a multiple inheritance lattice,
3033 convert the lattice into a simple relation such that,
3034 given to CIDs, C1 and C2, one can determine if C1 <= C2
3035 or C2 <= C1 or C1 <> C2.
3037 Once constructed, we walk the lattice depth fisrt,
3038 applying various functions to elements as they are encountered.
3040 We use xmalloc here, in case we want to randomly free these tables. */
3042 #define SAVE_MI_MATRIX
3045 build_mi_matrix (type)
3048 tree binfo = TYPE_BINFO (type);
3051 #ifdef SAVE_MI_MATRIX
3052 if (CLASSTYPE_MI_MATRIX (type))
3054 mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
3055 mi_matrix = CLASSTYPE_MI_MATRIX (type);
3057 dfs_walk (binfo, dfs_number, unnumberedp);
3062 mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
3063 mi_matrix = (char *)xmalloc ((mi_size + 1) * (mi_size + 1));
3065 bzero (mi_matrix, (mi_size + 1) * (mi_size + 1));
3066 dfs_walk (binfo, dfs_number, unnumberedp);
3067 dfs_walk (binfo, dfs_record_inheritance, unmarkedp);
3068 dfs_walk (binfo, dfs_unmark, markedp);
3074 dfs_walk (TYPE_BINFO (mi_type), dfs_unnumber, numberedp);
3076 #ifdef SAVE_MI_MATRIX
3077 CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
3085 /* If we want debug info for a type TYPE, make sure all its base types
3086 are also marked as being potentially interesting. This avoids
3087 the problem of not writing any debug info for intermediate basetypes
3088 that have abstract virtual functions. Also mark member types. */
3091 note_debug_info_needed (type)
3095 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp);
3096 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3099 if (TREE_CODE (field) == FIELD_DECL
3100 && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
3101 && dfs_debug_unmarkedp (TYPE_BINFO (ttype)))
3102 note_debug_info_needed (ttype);
3106 /* Subroutines of push_class_decls (). */
3108 /* Add in a decl to the envelope. */
3110 envelope_add_decl (type, decl, values)
3111 tree type, decl, *values;
3114 tree name = DECL_NAME (decl);
3117 /* virtual base names are always unique. */
3118 if (VBASE_NAME_P (name))
3119 *values = NULL_TREE;
3121 /* Possible ambiguity. If its defining type(s)
3122 is (are all) derived from us, no problem. */
3123 else if (*values && TREE_CODE (*values) != TREE_LIST)
3125 tree value = *values;
3126 /* Only complain if we shadow something we can access. */
3127 if (warn_shadow && TREE_CODE (decl) == FUNCTION_DECL
3128 && ((DECL_LANG_SPECIFIC (*values)
3129 && DECL_CLASS_CONTEXT (value) == current_class_type)
3130 || ! TREE_PRIVATE (value)))
3131 /* Should figure out access control more accurately. */
3133 cp_warning_at ("member `%#D' is shadowed", value);
3134 cp_warning_at ("by member function `%#D'", decl);
3135 warning ("in this context");
3138 context = (TREE_CODE (value) == FUNCTION_DECL
3139 && DECL_VIRTUAL_P (value))
3140 ? DECL_CLASS_CONTEXT (value)
3141 : DECL_CONTEXT (value);
3143 if (context == type)
3145 if (TREE_CODE (value) == TYPE_DECL
3146 && DECL_ARTIFICIAL (value))
3147 *values = NULL_TREE;
3151 else if (context && TYPE_DERIVES_FROM (context, type))
3153 /* Don't add in *values to list */
3154 *values = NULL_TREE;
3157 *values = build_tree_list (NULL_TREE, value);
3160 for (tmp = values; *tmp;)
3162 tree value = TREE_VALUE (*tmp);
3163 my_friendly_assert (TREE_CODE (value) != TREE_LIST, 999);
3164 context = (TREE_CODE (value) == FUNCTION_DECL
3165 && DECL_VIRTUAL_P (value))
3166 ? DECL_CLASS_CONTEXT (value)
3167 : DECL_CONTEXT (value);
3169 if (context && TYPE_DERIVES_FROM (context, type))
3171 /* remove *tmp from list */
3172 *tmp = TREE_CHAIN (*tmp);
3175 tmp = &TREE_CHAIN (*tmp);
3180 /* Put the new contents in our envelope. */
3181 if (TREE_CODE (decl) == FUNCTION_DECL)
3183 *values = tree_cons (name, decl, *values);
3184 TREE_NONLOCAL_FLAG (*values) = 1;
3185 TREE_TYPE (*values) = unknown_type_node;
3191 *values = tree_cons (NULL_TREE, decl, *values);
3192 /* Mark this as a potentially ambiguous member. */
3193 /* Leaving TREE_TYPE blank is intentional.
3194 We cannot use `error_mark_node' (lookup_name)
3195 or `unknown_type_node' (all member functions use this). */
3196 TREE_NONLOCAL_FLAG (*values) = 1;
3204 /* Add the instance variables which this class contributed to the
3205 current class binding contour. When a redefinition occurs, if the
3206 redefinition is strictly within a single inheritance path, we just
3207 overwrite the old declaration with the new. If the fields are not
3208 within a single inheritance path, we must cons them.
3210 In order to know what decls are new (stemming from the current
3211 invocation of push_class_decls) we enclose them in an "envelope",
3212 which is a TREE_LIST node where the TREE_PURPOSE slot contains the
3213 new decl (or possibly a list of competing ones), the TREE_VALUE slot
3214 points to the old value and the TREE_CHAIN slot chains together all
3215 envelopes which needs to be "opened" in push_class_decls. Opening an
3216 envelope means: push the old value onto the class_shadowed list,
3217 install the new one and if it's a TYPE_DECL do the same to the
3218 IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that
3219 the TREE_PURPOSE slot is non-null, and that it is not an identifier.
3220 Because if it is, it could be a set of overloaded methods from an
3224 dfs_pushdecls (binfo)
3227 tree type = BINFO_TYPE (binfo);
3228 tree fields, *methods, *end;
3231 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3233 /* Unmark so that if we are in a constructor, and then find that
3234 this field was initialized by a base initializer,
3235 we can emit an error message. */
3236 if (TREE_CODE (fields) == FIELD_DECL)
3237 TREE_USED (fields) = 0;
3239 /* Recurse into anonymous unions. */
3240 if (DECL_NAME (fields) == NULL_TREE
3241 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3243 dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields)));
3247 if (DECL_NAME (fields))
3249 tree name = DECL_NAME (fields);
3250 tree class_value = IDENTIFIER_CLASS_VALUE (name);
3252 /* If the class value is not an envelope of the kind described in
3253 the comment above, we create a new envelope. */
3254 if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
3255 || TREE_PURPOSE (class_value) == NULL_TREE
3256 || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
3258 /* See comment above for a description of envelopes. */
3259 closed_envelopes = tree_cons (NULL_TREE, class_value,
3261 IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
3262 class_value = IDENTIFIER_CLASS_VALUE (name);
3265 envelope_add_decl (type, fields, &TREE_PURPOSE (class_value));
3269 method_vec = CLASSTYPE_METHOD_VEC (type);
3270 if (method_vec != 0)
3272 /* Farm out constructors and destructors. */
3273 methods = &TREE_VEC_ELT (method_vec, 1);
3274 end = TREE_VEC_END (method_vec);
3276 while (methods != end)
3278 /* This will cause lookup_name to return a pointer
3279 to the tree_list of possible methods of this name. */
3280 tree name = DECL_NAME (*methods);
3281 tree class_value = IDENTIFIER_CLASS_VALUE (name);
3283 /* If the class value is not an envelope of the kind described in
3284 the comment above, we create a new envelope. */
3285 if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
3286 || TREE_PURPOSE (class_value) == NULL_TREE
3287 || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
3289 /* See comment above for a description of envelopes. */
3290 closed_envelopes = tree_cons (NULL_TREE, class_value,
3292 IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
3293 class_value = IDENTIFIER_CLASS_VALUE (name);
3296 /* Here we try to rule out possible ambiguities.
3297 If we can't do that, keep a TREE_LIST with possibly ambiguous
3299 maybe_push_cache_obstack ();
3300 envelope_add_decl (type, *methods, &TREE_PURPOSE (class_value));
3306 SET_BINFO_MARKED (binfo);
3309 /* Consolidate unique (by name) member functions. */
3311 dfs_compress_decls (binfo)
3314 tree type = BINFO_TYPE (binfo);
3315 tree method_vec = CLASSTYPE_METHOD_VEC (type);
3317 if (method_vec != 0)
3319 /* Farm out constructors and destructors. */
3320 tree *methods = &TREE_VEC_ELT (method_vec, 1);
3321 tree *end = TREE_VEC_END (method_vec);
3323 for (; methods != end; methods++)
3325 /* This is known to be an envelope of the kind described before
3327 tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods));
3328 tree tmp = TREE_PURPOSE (class_value);
3330 /* This was replaced in scope by somebody else. Just leave it
3332 if (TREE_CODE (tmp) != TREE_LIST)
3335 if (TREE_CHAIN (tmp) == NULL_TREE
3337 && DECL_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
3339 TREE_PURPOSE (class_value) = TREE_VALUE (tmp);
3343 CLEAR_BINFO_MARKED (binfo);
3346 /* When entering the scope of a class, we cache all of the
3347 fields that that class provides within its inheritance
3348 lattice. Where ambiguities result, we mark them
3349 with `error_mark_node' so that if they are encountered
3350 without explicit qualification, we can emit an error
3353 push_class_decls (type)
3357 struct obstack *ambient_obstack = current_obstack;
3359 search_stack = push_search_level (search_stack, &search_obstack);
3361 id = TYPE_IDENTIFIER (type);
3363 if (IDENTIFIER_TEMPLATE (id) != 0)
3365 tree tmpl = IDENTIFIER_TEMPLATE (id);
3366 push_template_decls (DECL_ARGUMENTS (TREE_PURPOSE (tmpl)),
3367 TREE_VALUE (tmpl), 1);
3368 overload_template_name (id, 1);
3372 /* Push class fields into CLASS_VALUE scope, and mark. */
3373 dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarkedp);
3375 /* Compress fields which have only a single entry
3376 by a given name, and unmark. */
3377 dfs_walk (TYPE_BINFO (type), dfs_compress_decls, markedp);
3379 /* Open up all the closed envelopes and push the contained decls into
3381 while (closed_envelopes)
3383 tree new = TREE_PURPOSE (closed_envelopes);
3386 /* This is messy because the class value may be a *_DECL, or a
3387 TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
3388 *_DECLs. The name is stored at different places in these three
3390 if (TREE_CODE (new) == TREE_LIST)
3392 if (TREE_PURPOSE (new) != NULL_TREE)
3393 id = TREE_PURPOSE (new);
3396 tree node = TREE_VALUE (new);
3398 while (TREE_CODE (node) == TREE_LIST)
3399 node = TREE_VALUE (node);
3400 id = DECL_NAME (node);
3404 id = DECL_NAME (new);
3406 /* Install the original class value in order to make
3407 pushdecl_class_level work correctly. */
3408 IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes);
3409 if (TREE_CODE (new) == TREE_LIST)
3410 push_class_level_binding (id, new);
3412 pushdecl_class_level (new);
3413 closed_envelopes = TREE_CHAIN (closed_envelopes);
3415 current_obstack = ambient_obstack;
3418 /* Here's a subroutine we need because C lacks lambdas. */
3420 dfs_unuse_fields (binfo)
3423 tree type = TREE_TYPE (binfo);
3426 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3428 if (TREE_CODE (fields) != FIELD_DECL)
3431 TREE_USED (fields) = 0;
3432 if (DECL_NAME (fields) == NULL_TREE
3433 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3434 unuse_fields (TREE_TYPE (fields));
3442 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp);
3446 pop_class_decls (type)
3449 /* We haven't pushed a search level when dealing with cached classes,
3450 so we'd better not try to pop it. */
3452 search_stack = pop_search_level (search_stack);
3456 print_search_statistics ()
3458 #ifdef GATHER_STATISTICS
3459 if (flag_memoize_lookups)
3461 fprintf (stderr, "%d memoized contexts saved\n",
3463 fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
3464 fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
3465 fprintf (stderr, "fields statistics:\n");
3466 fprintf (stderr, " memoized finds = %d; rejects = %d; (searches = %d)\n",
3467 memoized_fast_finds[0], memoized_fast_rejects[0],
3468 memoized_fields_searched[0]);
3469 fprintf (stderr, " memoized_adds = %d\n", memoized_adds[0]);
3470 fprintf (stderr, "fnfields statistics:\n");
3471 fprintf (stderr, " memoized finds = %d; rejects = %d; (searches = %d)\n",
3472 memoized_fast_finds[1], memoized_fast_rejects[1],
3473 memoized_fields_searched[1]);
3474 fprintf (stderr, " memoized_adds = %d\n", memoized_adds[1]);
3476 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3477 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3478 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3479 n_outer_fields_searched, n_calls_lookup_fnfields);
3480 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
3482 fprintf (stderr, "no search statistics\n");
3487 init_search_processing ()
3489 gcc_obstack_init (&search_obstack);
3490 gcc_obstack_init (&type_obstack);
3491 gcc_obstack_init (&type_obstack_entries);
3493 /* This gives us room to build our chains of basetypes,
3494 whether or not we decide to memoize them. */
3495 type_stack = push_type_level (0, &type_obstack);
3496 _vptr_name = get_identifier ("_vptr");
3500 reinit_search_statistics ()
3502 my_memoized_entry_counter = 0;
3503 memoized_fast_finds[0] = 0;
3504 memoized_fast_finds[1] = 0;
3505 memoized_adds[0] = 0;
3506 memoized_adds[1] = 0;
3507 memoized_fast_rejects[0] = 0;
3508 memoized_fast_rejects[1] = 0;
3509 memoized_fields_searched[0] = 0;
3510 memoized_fields_searched[1] = 0;
3511 n_fields_searched = 0;
3512 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3513 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3514 n_calls_get_base_type = 0;
3515 n_outer_fields_searched = 0;
3516 n_contexts_saved = 0;
3519 static tree conversions;
3521 add_conversions (binfo)
3525 tree vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
3527 for (i = 1; i < TREE_VEC_LENGTH (vec); ++i)
3529 tree tmp = TREE_VEC_ELT (vec, i);
3530 if (! IDENTIFIER_TYPENAME_P (DECL_NAME (tmp)))
3532 conversions = tree_cons (DECL_NAME (tmp), TREE_TYPE (TREE_TYPE (tmp)),
3538 lookup_conversions (type)
3541 conversions = NULL_TREE;
3542 dfs_walk (TYPE_BINFO (type), add_conversions, 0);