1998-09-21 Ben Elliston <bje@cygnus.com>
[official-gcc.git] / gcc / cp / search.c
blob6de2d740557106a0af0a32ba2e3638fad1008e78
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 89, 92-96, 1997 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)
11 any later version.
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. */
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "obstack.h"
30 #include "flags.h"
31 #include "rtl.h"
32 #include "output.h"
33 #include "toplev.h"
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 extern struct obstack *current_obstack;
39 extern tree abort_fndecl;
41 #include "stack.h"
43 /* Obstack used for remembering decision points of breadth-first. */
45 static struct obstack search_obstack;
47 /* Methods for pushing and popping objects to and from obstacks. */
49 struct stack_level *
50 push_stack_level (obstack, tp, size)
51 struct obstack *obstack;
52 char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
53 int size;
55 struct stack_level *stack;
56 obstack_grow (obstack, tp, size);
57 stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
58 obstack_finish (obstack);
59 stack->obstack = obstack;
60 stack->first = (tree *) obstack_base (obstack);
61 stack->limit = obstack_room (obstack) / sizeof (tree *);
62 return stack;
65 struct stack_level *
66 pop_stack_level (stack)
67 struct stack_level *stack;
69 struct stack_level *tem = stack;
70 struct obstack *obstack = tem->obstack;
71 stack = tem->prev;
72 obstack_free (obstack, tem);
73 return stack;
76 #define search_level stack_level
77 static struct search_level *search_stack;
79 static tree get_abstract_virtuals_1 PROTO((tree, int, tree));
80 static tree get_vbase_1 PROTO((tree, tree, unsigned int *));
81 static tree convert_pointer_to_vbase PROTO((tree, tree));
82 static tree lookup_field_1 PROTO((tree, tree));
83 static tree convert_pointer_to_single_level PROTO((tree, tree));
84 static int lookup_fnfields_1 PROTO((tree, tree));
85 static int lookup_fnfields_here PROTO((tree, tree));
86 static int is_subobject_of_p PROTO((tree, tree));
87 static int hides PROTO((tree, tree));
88 static tree virtual_context PROTO((tree, tree, tree));
89 static tree get_template_base_recursive
90 PROTO((tree, tree, tree, int));
91 static void dfs_walk PROTO((tree, void (*) (tree), int (*) (tree)));
92 static void dfs_check_overlap PROTO((tree));
93 static int dfs_no_overlap_yet PROTO((tree));
94 static void envelope_add_decl PROTO((tree, tree, tree *));
95 static int get_base_distance_recursive
96 PROTO((tree, int, int, int, int *, tree *, tree,
97 int, int *, int, int));
98 static void expand_upcast_fixups
99 PROTO((tree, tree, tree, tree, tree, tree, tree *));
100 static void fixup_virtual_upcast_offsets
101 PROTO((tree, tree, int, int, tree, tree, tree, tree,
102 tree *));
103 static int markedp PROTO((tree));
104 static int unmarkedp PROTO((tree));
105 static int marked_vtable_pathp PROTO((tree));
106 static int unmarked_vtable_pathp PROTO((tree));
107 static int marked_new_vtablep PROTO((tree));
108 static int unmarked_new_vtablep PROTO((tree));
109 static int dfs_debug_unmarkedp PROTO((tree));
110 static void dfs_debug_mark PROTO((tree));
111 static void dfs_find_vbases PROTO((tree));
112 static void dfs_clear_vbase_slots PROTO((tree));
113 static void dfs_unmark PROTO((tree));
114 static void dfs_init_vbase_pointers PROTO((tree));
115 static void dfs_get_vbase_types PROTO((tree));
116 static void dfs_pushdecls PROTO((tree));
117 static void dfs_compress_decls PROTO((tree));
118 static void dfs_unuse_fields PROTO((tree));
119 static void add_conversions PROTO((tree));
120 static tree get_virtuals_named_this PROTO((tree));
121 static tree get_virtual_destructor PROTO((tree, int));
122 static int tree_has_any_destructor_p PROTO((tree, int));
123 static int covariant_return_p PROTO((tree, tree));
124 static struct search_level *push_search_level
125 PROTO((struct stack_level *, struct obstack *));
126 static struct search_level *pop_search_level
127 PROTO((struct stack_level *));
128 static HOST_WIDE_INT breadth_first_search
129 PROTO((tree, int (*) (tree, int), int (*) (tree, int)));
131 static tree vbase_types;
132 static tree vbase_decl_ptr_intermediate, vbase_decl_ptr;
133 static tree vbase_init_result;
135 /* Allocate a level of searching. */
137 static struct search_level *
138 push_search_level (stack, obstack)
139 struct stack_level *stack;
140 struct obstack *obstack;
142 struct search_level tem;
144 tem.prev = stack;
145 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
148 /* Discard a level of search allocation. */
150 static struct search_level *
151 pop_search_level (obstack)
152 struct stack_level *obstack;
154 register struct search_level *stack = pop_stack_level (obstack);
156 return stack;
159 static tree _vptr_name;
161 /* Variables for gathering statistics. */
162 #ifdef GATHER_STATISTICS
163 static int n_fields_searched;
164 static int n_calls_lookup_field, n_calls_lookup_field_1;
165 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
166 static int n_calls_get_base_type;
167 static int n_outer_fields_searched;
168 static int n_contexts_saved;
169 #endif /* GATHER_STATISTICS */
171 /* This list is used by push_class_decls to know what decls need to
172 be pushed into class scope. */
173 static tree closed_envelopes = NULL_TREE;
175 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
176 the same type as the type given in PARENT. To be optimal, we want
177 the first one that is found by going through the least number of
178 virtual bases.
180 This uses a clever algorithm that updates *depth when we find the vbase,
181 and cuts off other paths of search when they reach that depth. */
183 static tree
184 get_vbase_1 (parent, binfo, depth)
185 tree parent, binfo;
186 unsigned int *depth;
188 tree binfos;
189 int i, n_baselinks;
190 tree rval = NULL_TREE;
192 if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo))
194 *depth = 0;
195 return binfo;
198 *depth = *depth - 1;
200 binfos = BINFO_BASETYPES (binfo);
201 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
203 /* Process base types. */
204 for (i = 0; i < n_baselinks; i++)
206 tree base_binfo = TREE_VEC_ELT (binfos, i);
207 tree nrval;
209 if (*depth == 0)
210 break;
212 nrval = get_vbase_1 (parent, base_binfo, depth);
213 if (nrval)
214 rval = nrval;
216 *depth = *depth+1;
217 return rval;
220 /* Return the shortest path to vbase PARENT within BINFO, ignoring
221 access and ambiguity. */
223 tree
224 get_vbase (parent, binfo)
225 tree parent;
226 tree binfo;
228 unsigned int d = (unsigned int)-1;
229 return get_vbase_1 (parent, binfo, &d);
232 /* Convert EXPR to a virtual base class of type TYPE. We know that
233 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
234 the type of what expr points to has a virtual base of type TYPE. */
236 static tree
237 convert_pointer_to_vbase (type, expr)
238 tree type;
239 tree expr;
241 tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))));
242 return convert_pointer_to_real (vb, expr);
245 /* Check whether the type given in BINFO is derived from PARENT. If
246 it isn't, return 0. If it is, but the derivation is MI-ambiguous
247 AND protect != 0, emit an error message and return error_mark_node.
249 Otherwise, if TYPE is derived from PARENT, return the actual base
250 information, unless a one of the protection violations below
251 occurs, in which case emit an error message and return error_mark_node.
253 If PROTECT is 1, then check if access to a public field of PARENT
254 would be private. Also check for ambiguity. */
256 tree
257 get_binfo (parent, binfo, protect)
258 register tree parent, binfo;
259 int protect;
261 tree type = NULL_TREE;
262 int dist;
263 tree rval = NULL_TREE;
265 if (TREE_CODE (parent) == TREE_VEC)
266 parent = BINFO_TYPE (parent);
267 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
268 my_friendly_abort (89);
270 if (TREE_CODE (binfo) == TREE_VEC)
271 type = BINFO_TYPE (binfo);
272 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
273 type = binfo;
274 else
275 my_friendly_abort (90);
277 dist = get_base_distance (parent, binfo, protect, &rval);
279 if (dist == -3)
281 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
282 parent, type);
283 return error_mark_node;
285 else if (dist == -2 && protect)
287 cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
288 type);
289 return error_mark_node;
292 return rval;
295 /* This is the newer depth first get_base_distance routine. */
297 static int
298 get_base_distance_recursive (binfo, depth, is_private, rval,
299 rval_private_ptr, new_binfo_ptr, parent,
300 protect, via_virtual_ptr, via_virtual,
301 current_scope_in_chain)
302 tree binfo;
303 int depth, is_private, rval;
304 int *rval_private_ptr;
305 tree *new_binfo_ptr, parent;
306 int protect, *via_virtual_ptr, via_virtual;
307 int current_scope_in_chain;
309 tree binfos;
310 int i, n_baselinks;
312 if (protect
313 && !current_scope_in_chain
314 && is_friend (BINFO_TYPE (binfo), current_scope ()))
315 current_scope_in_chain = 1;
317 if (BINFO_TYPE (binfo) == parent || binfo == parent)
319 int better = 0;
321 if (rval == -1)
322 /* This is the first time we've found parent. */
323 better = 1;
324 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
325 BINFO_OFFSET (binfo))
326 && *via_virtual_ptr && via_virtual)
328 /* A new path to the same vbase. If this one has better
329 access or is shorter, take it. */
331 if (protect)
332 better = *rval_private_ptr - is_private;
333 if (better == 0)
334 better = rval - depth;
336 else
338 /* Ambiguous base class. */
339 rval = depth = -2;
341 /* If we get an ambiguity between virtual and non-virtual base
342 class, return the non-virtual in case we are ignoring
343 ambiguity. */
344 better = *via_virtual_ptr - via_virtual;
347 if (better > 0)
349 rval = depth;
350 *rval_private_ptr = is_private;
351 *new_binfo_ptr = binfo;
352 *via_virtual_ptr = via_virtual;
355 return rval;
358 binfos = BINFO_BASETYPES (binfo);
359 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
360 depth += 1;
362 /* Process base types. */
363 for (i = 0; i < n_baselinks; i++)
365 tree base_binfo = TREE_VEC_ELT (binfos, i);
367 int via_private
368 = (protect
369 && (is_private
370 || (!TREE_VIA_PUBLIC (base_binfo)
371 && !(TREE_VIA_PROTECTED (base_binfo)
372 && current_scope_in_chain)
373 && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
374 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
376 rval = get_base_distance_recursive (base_binfo, depth, via_private,
377 rval, rval_private_ptr,
378 new_binfo_ptr, parent,
379 protect, via_virtual_ptr,
380 this_virtual,
381 current_scope_in_chain);
383 /* If we've found a non-virtual, ambiguous base class, we don't need
384 to keep searching. */
385 if (rval == -2 && *via_virtual_ptr == 0)
386 return rval;
389 return rval;
392 /* Return the number of levels between type PARENT and the type given
393 in BINFO, following the leftmost path to PARENT not found along a
394 virtual path, if there are no real PARENTs (all come from virtual
395 base classes), then follow the shortest public path to PARENT.
397 Return -1 if TYPE is not derived from PARENT.
398 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
399 non-negative.
400 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
402 If PATH_PTR is non-NULL, then also build the list of types
403 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
404 set.
406 PARENT can also be a binfo, in which case that exact parent is found
407 and no other. convert_pointer_to_real uses this functionality.
409 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
412 get_base_distance (parent, binfo, protect, path_ptr)
413 register tree parent, binfo;
414 int protect;
415 tree *path_ptr;
417 int rval;
418 int rval_private = 0;
419 tree type = NULL_TREE;
420 tree new_binfo = NULL_TREE;
421 int via_virtual;
422 int watch_access = protect;
424 /* Should we be completing types here? */
425 if (TREE_CODE (parent) != TREE_VEC)
426 parent = complete_type (TYPE_MAIN_VARIANT (parent));
427 else
428 complete_type (TREE_TYPE (parent));
430 if (TREE_CODE (binfo) == TREE_VEC)
431 type = BINFO_TYPE (binfo);
432 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
434 type = complete_type (binfo);
435 binfo = TYPE_BINFO (type);
437 if (path_ptr)
438 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE,
439 980827);
441 else
442 my_friendly_abort (92);
444 if (parent == type || parent == binfo)
446 /* If the distance is 0, then we don't really need
447 a path pointer, but we shouldn't let garbage go back. */
448 if (path_ptr)
449 *path_ptr = binfo;
450 return 0;
453 if (path_ptr)
454 watch_access = 1;
456 rval = get_base_distance_recursive (binfo, 0, 0, -1,
457 &rval_private, &new_binfo, parent,
458 watch_access, &via_virtual, 0,
461 /* Access restrictions don't count if we found an ambiguous basetype. */
462 if (rval == -2 && protect >= 0)
463 rval_private = 0;
465 if (rval && protect && rval_private)
466 return -3;
468 /* If they gave us the real vbase binfo, which isn't in the main binfo
469 tree, deal with it. */
470 if (rval == -1 && TREE_CODE (parent) == TREE_VEC
471 && parent == binfo_member (BINFO_TYPE (parent),
472 CLASSTYPE_VBASECLASSES (type)))
474 my_friendly_abort (980901);
475 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent) == binfo, 980827);
476 new_binfo = parent;
477 rval = 1;
480 if (path_ptr)
481 *path_ptr = new_binfo;
482 return rval;
485 /* Search for a member with name NAME in a multiple inheritance lattice
486 specified by TYPE. If it does not exist, return NULL_TREE.
487 If the member is ambiguously referenced, return `error_mark_node'.
488 Otherwise, return the FIELD_DECL. */
490 /* Do a 1-level search for NAME as a member of TYPE. The caller must
491 figure out whether it can access this field. (Since it is only one
492 level, this is reasonable.) */
494 static tree
495 lookup_field_1 (type, name)
496 tree type, name;
498 register tree field;
500 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
501 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
502 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
503 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
504 the code often worked even when we treated the index as a list
505 of fields!) */
506 return NULL_TREE;
508 field = TYPE_FIELDS (type);
510 #ifdef GATHER_STATISTICS
511 n_calls_lookup_field_1++;
512 #endif /* GATHER_STATISTICS */
513 while (field)
515 #ifdef GATHER_STATISTICS
516 n_fields_searched++;
517 #endif /* GATHER_STATISTICS */
518 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field)) == 'd', 0);
519 if (DECL_NAME (field) == NULL_TREE
520 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
522 tree temp = lookup_field_1 (TREE_TYPE (field), name);
523 if (temp)
524 return temp;
526 if (DECL_NAME (field) == name)
528 if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
529 && DECL_ASSEMBLER_NAME (field) != NULL)
530 GNU_xref_ref(current_function_decl,
531 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
532 return field;
534 field = TREE_CHAIN (field);
536 /* Not found. */
537 if (name == _vptr_name)
539 /* Give the user what s/he thinks s/he wants. */
540 if (TYPE_VIRTUAL_P (type))
541 return CLASSTYPE_VFIELD (type);
543 return NULL_TREE;
546 /* There are a number of cases we need to be aware of here:
547 current_class_type current_function_decl
548 global NULL NULL
549 fn-local NULL SET
550 class-local SET NULL
551 class->fn SET SET
552 fn->class SET SET
554 Those last two make life interesting. If we're in a function which is
555 itself inside a class, we need decls to go into the fn's decls (our
556 second case below). But if we're in a class and the class itself is
557 inside a function, we need decls to go into the decls for the class. To
558 achieve this last goal, we must see if, when both current_class_ptr and
559 current_function_decl are set, the class was declared inside that
560 function. If so, we know to put the decls into the class's scope. */
562 tree
563 current_scope ()
565 if (current_function_decl == NULL_TREE)
566 return current_class_type;
567 if (current_class_type == NULL_TREE)
568 return current_function_decl;
569 if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type)
570 return current_function_decl;
572 return current_class_type;
575 /* Compute the access of FIELD. This is done by computing
576 the access available to each type in BASETYPES (which comes
577 as a list of [via_public/basetype] in reverse order, namely base
578 class before derived class). The first one which defines a
579 access defines the access for the field. Otherwise, the
580 access of the field is that which occurs normally.
582 Uses global variables CURRENT_CLASS_TYPE and
583 CURRENT_FUNCTION_DECL to use friend relationships
584 if necessary.
586 This will be static when lookup_fnfield comes into this file.
588 access_public_node means that the field can be accessed by the current lexical
589 scope.
591 access_protected_node means that the field cannot be accessed by the current
592 lexical scope because it is protected.
594 access_private_node means that the field cannot be accessed by the current
595 lexical scope because it is private. */
597 #if 0
598 #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public_node
599 #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected_node
600 #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private_node
601 #else
602 #define PUBLIC_RETURN return access_public_node
603 #define PROTECTED_RETURN return access_protected_node
604 #define PRIVATE_RETURN return access_private_node
605 #endif
607 #if 0
608 /* Disabled with DECL_PUBLIC &c. */
609 static tree previous_scope = NULL_TREE;
610 #endif
612 tree
613 compute_access (basetype_path, field)
614 tree basetype_path, field;
616 tree access;
617 tree types;
618 tree context;
619 int protected_ok, via_protected;
620 extern int flag_access_control;
621 #if 1
622 /* Replaces static decl above. */
623 tree previous_scope;
624 #endif
625 int static_mem
626 = ((TREE_CODE (field) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (field))
627 || (TREE_CODE (field) != FUNCTION_DECL && TREE_STATIC (field)));
629 if (! flag_access_control)
630 return access_public_node;
632 /* The field lives in the current class. */
633 if (BINFO_TYPE (basetype_path) == current_class_type)
634 return access_public_node;
636 #if 0
637 /* Disabled until pushing function scope clears these out. If ever. */
638 /* Make these special cases fast. */
639 if (current_scope () == previous_scope)
641 if (DECL_PUBLIC (field))
642 return access_public_node;
643 if (DECL_PROTECTED (field))
644 return access_protected_node;
645 if (DECL_PRIVATE (field))
646 return access_private_node;
648 #endif
650 /* We don't currently support access control on nested types. */
651 if (TREE_CODE (field) == TYPE_DECL)
652 return access_public_node;
654 previous_scope = current_scope ();
656 context = DECL_REAL_CONTEXT (field);
658 /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT
659 slot set to the union type rather than the record type containing
660 the anonymous union. */
661 if (context && ANON_UNION_TYPE_P (context)
662 && TREE_CODE (field) == FIELD_DECL)
663 context = TYPE_CONTEXT (context);
665 /* Virtual function tables are never private. But we should know that
666 we are looking for this, and not even try to hide it. */
667 if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1)
668 PUBLIC_RETURN;
670 /* Member found immediately within object. */
671 if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE)
673 /* Are we (or an enclosing scope) friends with the class that has
674 FIELD? */
675 if (is_friend (context, previous_scope))
676 PUBLIC_RETURN;
678 /* If it's private, it's private, you letch. */
679 if (TREE_PRIVATE (field))
680 PRIVATE_RETURN;
682 /* ARM $11.5. Member functions of a derived class can access the
683 non-static protected members of a base class only through a
684 pointer to the derived class, a reference to it, or an object
685 of it. Also any subsequently derived classes also have
686 access. */
687 else if (TREE_PROTECTED (field))
689 if (current_class_type
690 && (static_mem || DECL_CONSTRUCTOR_P (field))
691 && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type))
692 PUBLIC_RETURN;
693 else
694 PROTECTED_RETURN;
696 else
697 PUBLIC_RETURN;
700 /* must reverse more than one element */
701 basetype_path = reverse_path (basetype_path);
702 types = basetype_path;
703 via_protected = 0;
704 access = access_default_node;
705 protected_ok = static_mem && current_class_type
706 && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type);
708 while (1)
710 tree member;
711 tree binfo = types;
712 tree type = BINFO_TYPE (binfo);
713 int private_ok = 0;
715 /* Friends of a class can see protected members of its bases.
716 Note that classes are their own friends. */
717 if (is_friend (type, previous_scope))
719 protected_ok = 1;
720 private_ok = 1;
723 member = purpose_member (type, DECL_ACCESS (field));
724 if (member)
726 access = TREE_VALUE (member);
727 break;
730 types = BINFO_INHERITANCE_CHAIN (types);
732 /* If the next type was VIA_PROTECTED, then fields of all remaining
733 classes past that one are *at least* protected. */
734 if (types)
736 if (TREE_VIA_PROTECTED (types))
737 via_protected = 1;
738 else if (! TREE_VIA_PUBLIC (types) && ! private_ok)
740 access = access_private_node;
741 break;
744 else
745 break;
748 /* No special visibilities apply. Use normal rules. */
750 if (access == access_default_node)
752 if (is_friend (context, previous_scope))
753 access = access_public_node;
754 else if (TREE_PRIVATE (field))
755 access = access_private_node;
756 else if (TREE_PROTECTED (field))
757 access = access_protected_node;
758 else
759 access = access_public_node;
762 if (access == access_public_node && via_protected)
763 access = access_protected_node;
765 if (access == access_protected_node && protected_ok)
766 access = access_public_node;
768 #if 0
769 if (access == access_public_node)
770 DECL_PUBLIC (field) = 1;
771 else if (access == access_protected_node)
772 DECL_PROTECTED (field) = 1;
773 else if (access == access_private_node)
774 DECL_PRIVATE (field) = 1;
775 else my_friendly_abort (96);
776 #endif
777 return access;
780 /* Routine to see if the sub-object denoted by the binfo PARENT can be
781 found as a base class and sub-object of the object denoted by
782 BINFO. This routine relies upon binfos not being shared, except
783 for binfos for virtual bases. */
785 static int
786 is_subobject_of_p (parent, binfo)
787 tree parent, binfo;
789 tree binfos = BINFO_BASETYPES (binfo);
790 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
792 if (parent == binfo)
793 return 1;
795 /* Process and/or queue base types. */
796 for (i = 0; i < n_baselinks; i++)
798 tree base_binfo = TREE_VEC_ELT (binfos, i);
799 if (TREE_VIA_VIRTUAL (base_binfo))
800 base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
801 if (is_subobject_of_p (parent, base_binfo))
802 return 1;
804 return 0;
807 /* See if a one FIELD_DECL hides another. This routine is meant to
808 correspond to ANSI working paper Sept 17, 1992 10p4. The two
809 binfos given are the binfos corresponding to the particular places
810 the FIELD_DECLs are found. This routine relies upon binfos not
811 being shared, except for virtual bases. */
813 static int
814 hides (hider_binfo, hidee_binfo)
815 tree hider_binfo, hidee_binfo;
817 /* hider hides hidee, if hider has hidee as a base class and
818 the instance of hidee is a sub-object of hider. The first
819 part is always true is the second part is true.
821 When hider and hidee are the same (two ways to get to the exact
822 same member) we consider either one as hiding the other. */
823 return is_subobject_of_p (hidee_binfo, hider_binfo);
826 /* Very similar to lookup_fnfields_1 but it ensures that at least one
827 function was declared inside the class given by TYPE. It really should
828 only return functions that match the given TYPE. */
830 static int
831 lookup_fnfields_here (type, name)
832 tree type, name;
834 int idx = lookup_fnfields_1 (type, name);
835 tree fndecls;
837 /* ctors and dtors are always only in the right class. */
838 if (idx <= 1)
839 return idx;
840 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
841 while (fndecls)
843 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls)))
844 == TYPE_MAIN_VARIANT (type))
845 return idx;
846 fndecls = OVL_CHAIN (fndecls);
848 return -1;
851 /* Look for a field named NAME in an inheritance lattice dominated by
852 XBASETYPE. PROTECT is zero if we can avoid computing access
853 information, otherwise it is 1. WANT_TYPE is 1 when we should only
854 return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
856 It was not clear what should happen if WANT_TYPE is set, and an
857 ambiguity is found. At least one use (lookup_name) to not see
858 the error. */
860 tree
861 lookup_field (xbasetype, name, protect, want_type)
862 register tree xbasetype, name;
863 int protect, want_type;
865 int head = 0, tail = 0;
866 tree rval, rval_binfo = NULL_TREE, rval_binfo_h = NULL_TREE;
867 tree type = NULL_TREE, basetype_chain, basetype_path = NULL_TREE;
868 tree this_v = access_default_node;
869 tree entry, binfo, binfo_h;
870 tree own_access = access_default_node;
871 int vbase_name_p = VBASE_NAME_P (name);
873 /* rval_binfo is the binfo associated with the found member, note,
874 this can be set with useful information, even when rval is not
875 set, because it must deal with ALL members, not just non-function
876 members. It is used for ambiguity checking and the hidden
877 checks. Whereas rval is only set if a proper (not hidden)
878 non-function member is found. */
880 /* rval_binfo_h and binfo_h are binfo values used when we perform the
881 hiding checks, as virtual base classes may not be shared. The strategy
882 is we always go into the binfo hierarchy owned by TYPE_BINFO of
883 virtual base classes, as we cross virtual base class lines. This way
884 we know that binfo of a virtual base class will always == itself when
885 found along any line. (mrs) */
887 char *errstr = 0;
889 #if 0
890 /* We cannot search for constructor/destructor names like this. */
891 /* This can't go here, but where should it go? */
892 /* If we are looking for a constructor in a templated type, use the
893 unspecialized name, as that is how we store it. */
894 if (IDENTIFIER_TEMPLATE (name))
895 name = constructor_name (name);
896 #endif
898 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
899 && IDENTIFIER_CLASS_VALUE (name))
901 tree field = IDENTIFIER_CLASS_VALUE (name);
902 if (TREE_CODE (field) != FUNCTION_DECL
903 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
904 return field;
907 if (TREE_CODE (xbasetype) == TREE_VEC)
909 type = BINFO_TYPE (xbasetype);
910 basetype_path = xbasetype;
912 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
914 type = xbasetype;
915 basetype_path = TYPE_BINFO (type);
916 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
917 980827);
919 else
920 my_friendly_abort (97);
922 complete_type (type);
924 #ifdef GATHER_STATISTICS
925 n_calls_lookup_field++;
926 #endif /* GATHER_STATISTICS */
928 rval = lookup_field_1 (type, name);
930 if (rval || lookup_fnfields_here (type, name) >= 0)
932 if (rval)
934 if (want_type)
936 if (TREE_CODE (rval) != TYPE_DECL)
938 rval = purpose_member (name, CLASSTYPE_TAGS (type));
939 if (rval)
940 rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
943 else
945 if (TREE_CODE (rval) == TYPE_DECL
946 && lookup_fnfields_here (type, name) >= 0)
947 rval = NULL_TREE;
951 if (protect && rval)
953 if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
954 this_v = compute_access (basetype_path, rval);
955 if (TREE_CODE (rval) == CONST_DECL)
957 if (this_v == access_private_node)
958 errstr = "enum `%D' is a private value of class `%T'";
959 else if (this_v == access_protected_node)
960 errstr = "enum `%D' is a protected value of class `%T'";
962 else
964 if (this_v == access_private_node)
965 errstr = "member `%D' is a private member of class `%T'";
966 else if (this_v == access_protected_node)
967 errstr = "member `%D' is a protected member of class `%T'";
971 rval_binfo = basetype_path;
972 goto out;
975 basetype_chain = build_expr_list (NULL_TREE, basetype_path);
977 /* The ambiguity check relies upon breadth first searching. */
979 search_stack = push_search_level (search_stack, &search_obstack);
980 binfo = basetype_path;
981 binfo_h = binfo;
983 while (1)
985 tree binfos = BINFO_BASETYPES (binfo);
986 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
987 tree nval;
989 /* Process and/or queue base types. */
990 for (i = 0; i < n_baselinks; i++)
992 tree base_binfo = TREE_VEC_ELT (binfos, i);
993 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
995 tree btypes;
997 SET_BINFO_FIELDS_MARKED (base_binfo);
998 btypes = scratch_tree_cons (NULL_TREE, base_binfo, basetype_chain);
999 if (TREE_VIA_VIRTUAL (base_binfo))
1000 btypes = scratch_tree_cons (NULL_TREE,
1001 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1002 btypes);
1003 else
1004 btypes = scratch_tree_cons (NULL_TREE,
1005 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1006 btypes);
1007 obstack_ptr_grow (&search_obstack, btypes);
1008 tail += 1;
1009 if (tail >= search_stack->limit)
1010 my_friendly_abort (98);
1014 /* Process head of queue, if one exists. */
1015 if (head >= tail)
1016 break;
1018 basetype_chain = search_stack->first[head++];
1019 binfo_h = TREE_VALUE (basetype_chain);
1020 basetype_chain = TREE_CHAIN (basetype_chain);
1021 basetype_path = TREE_VALUE (basetype_chain);
1022 if (TREE_CHAIN (basetype_chain))
1023 my_friendly_assert
1024 ((BINFO_INHERITANCE_CHAIN (basetype_path)
1025 == TREE_VALUE (TREE_CHAIN (basetype_chain)))
1026 /* We only approximate base info for partial instantiations. */
1027 || current_template_parms,
1028 980827);
1029 else
1030 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path)
1031 == NULL_TREE, 980827);
1033 binfo = basetype_path;
1034 type = BINFO_TYPE (binfo);
1036 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1037 and we do find NAME in TYPE, verify that such a second
1038 sighting is in fact valid. */
1040 nval = lookup_field_1 (type, name);
1042 if (nval || lookup_fnfields_here (type, name)>=0)
1044 if (nval && nval == rval && SHARED_MEMBER_P (nval))
1046 /* This is ok, the member found is the same [class.ambig] */
1048 else if (rval_binfo && hides (rval_binfo_h, binfo_h))
1050 /* This is ok, the member found is in rval_binfo, not
1051 here (binfo). */
1053 else if (rval_binfo==NULL_TREE || hides (binfo_h, rval_binfo_h))
1055 /* This is ok, the member found is here (binfo), not in
1056 rval_binfo. */
1057 if (nval)
1059 rval = nval;
1060 if (protect)
1061 this_v = compute_access (basetype_path, rval);
1062 /* These may look ambiguous, but they really are not. */
1063 if (vbase_name_p)
1064 break;
1066 else
1068 /* Undo finding it before, as something else hides it. */
1069 rval = NULL_TREE;
1071 rval_binfo = binfo;
1072 rval_binfo_h = binfo_h;
1074 else
1076 /* This is ambiguous. */
1077 errstr = "request for member `%D' is ambiguous";
1078 protect += 2;
1079 break;
1084 tree *tp = search_stack->first;
1085 tree *search_tail = tp + tail;
1087 if (rval_binfo)
1089 type = BINFO_TYPE (rval_binfo);
1091 if (rval)
1093 if (want_type)
1095 if (TREE_CODE (rval) != TYPE_DECL)
1097 rval = purpose_member (name, CLASSTYPE_TAGS (type));
1098 if (rval)
1099 rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
1102 else
1104 if (TREE_CODE (rval) == TYPE_DECL
1105 && lookup_fnfields_here (type, name) >= 0)
1106 rval = NULL_TREE;
1111 if (rval == NULL_TREE)
1112 errstr = 0;
1114 /* If this FIELD_DECL defines its own access level, deal with that. */
1115 if (rval && errstr == 0
1116 && (protect & 1)
1117 && DECL_LANG_SPECIFIC (rval)
1118 && DECL_ACCESS (rval))
1120 while (tp < search_tail)
1122 /* If is possible for one of the derived types on the path to
1123 have defined special access for this field. Look for such
1124 declarations and report an error if a conflict is found. */
1125 tree new_v = NULL_TREE;
1127 if (this_v != access_default_node)
1128 new_v = compute_access (TREE_VALUE (TREE_CHAIN (*tp)), rval);
1129 if (this_v != access_default_node && new_v != this_v)
1131 errstr = "conflicting access to member `%D'";
1132 this_v = access_default_node;
1134 own_access = new_v;
1135 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1136 tp += 1;
1139 else
1141 while (tp < search_tail)
1143 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1144 tp += 1;
1148 search_stack = pop_search_level (search_stack);
1150 if (errstr == 0)
1152 if (own_access == access_private_node)
1153 errstr = "member `%D' declared private";
1154 else if (own_access == access_protected_node)
1155 errstr = "member `%D' declared protected";
1156 else if (this_v == access_private_node)
1157 errstr = TREE_PRIVATE (rval)
1158 ? "member `%D' is private"
1159 : "member `%D' is from private base class";
1160 else if (this_v == access_protected_node)
1161 errstr = TREE_PROTECTED (rval)
1162 ? "member `%D' is protected"
1163 : "member `%D' is from protected base class";
1166 out:
1167 if (protect == 2)
1169 /* If we are not interested in ambiguities, don't report them,
1170 just return NULL_TREE. */
1171 rval = NULL_TREE;
1172 protect = 0;
1175 if (errstr && protect)
1177 cp_error (errstr, name, type);
1178 rval = error_mark_node;
1181 /* Do implicit typename stuff. */
1182 if (rval && TREE_CODE (rval) == TYPE_DECL
1183 && processing_template_decl
1184 && ! currently_open_class (BINFO_TYPE (rval_binfo))
1185 && uses_template_parms (type))
1187 binfo = rval_binfo;
1188 for (; ; binfo = BINFO_INHERITANCE_CHAIN (binfo))
1189 if (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE
1190 || (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo))
1191 == current_class_type))
1192 break;
1194 entry = make_typename_type (BINFO_TYPE (binfo), name);
1195 TREE_TYPE (entry) = TREE_TYPE (rval);
1196 rval = TYPE_MAIN_DECL (entry);
1199 return rval;
1202 /* Try to find NAME inside a nested class. */
1204 tree
1205 lookup_nested_field (name, complain)
1206 tree name;
1207 int complain;
1209 register tree t;
1211 tree id = NULL_TREE;
1212 if (TYPE_MAIN_DECL (current_class_type))
1214 /* Climb our way up the nested ladder, seeing if we're trying to
1215 modify a field in an enclosing class. If so, we should only
1216 be able to modify if it's static. */
1217 for (t = TYPE_MAIN_DECL (current_class_type);
1218 t && DECL_CONTEXT (t);
1219 t = TYPE_MAIN_DECL (DECL_CONTEXT (t)))
1221 if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE)
1222 break;
1224 /* N.B.: lookup_field will do the access checking for us */
1225 id = lookup_field (DECL_CONTEXT (t), name, complain, 0);
1226 if (id == error_mark_node)
1228 id = NULL_TREE;
1229 continue;
1232 if (id != NULL_TREE)
1234 if (TREE_CODE (id) == FIELD_DECL
1235 && ! TREE_STATIC (id)
1236 && TREE_TYPE (id) != error_mark_node)
1238 if (complain)
1240 /* At parse time, we don't want to give this error, since
1241 we won't have enough state to make this kind of
1242 decision properly. But there are times (e.g., with
1243 enums in nested classes) when we do need to call
1244 this fn at parse time. So, in those cases, we pass
1245 complain as a 0 and just return a NULL_TREE. */
1246 cp_error ("assignment to non-static member `%D' of enclosing class `%T'",
1247 id, DECL_CONTEXT (t));
1248 /* Mark this for do_identifier(). It would otherwise
1249 claim that the variable was undeclared. */
1250 TREE_TYPE (id) = error_mark_node;
1252 else
1254 id = NULL_TREE;
1255 continue;
1258 break;
1263 return id;
1266 /* TYPE is a class type. Return the index of the fields within
1267 the method vector with name NAME, or -1 is no such field exists. */
1269 static int
1270 lookup_fnfields_1 (type, name)
1271 tree type, name;
1273 register tree method_vec = CLASSTYPE_METHOD_VEC (type);
1275 if (method_vec != 0)
1277 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1278 register tree *end = TREE_VEC_END (method_vec);
1280 #ifdef GATHER_STATISTICS
1281 n_calls_lookup_fnfields_1++;
1282 #endif /* GATHER_STATISTICS */
1284 /* Constructors are first... */
1285 if (*methods && name == ctor_identifier)
1286 return 0;
1288 /* and destructors are second. */
1289 if (*++methods && name == dtor_identifier)
1290 return 1;
1292 while (++methods != end)
1294 #ifdef GATHER_STATISTICS
1295 n_outer_fields_searched++;
1296 #endif /* GATHER_STATISTICS */
1297 if (DECL_NAME (OVL_CURRENT (*methods)) == name)
1298 break;
1301 /* If we didn't find it, it might have been a template
1302 conversion operator. (Note that we don't look for this case
1303 above so that we will always find specializations first.) */
1304 if (methods == end
1305 && IDENTIFIER_TYPENAME_P (name))
1307 methods = &TREE_VEC_ELT (method_vec, 0) + 1;
1309 while (++methods != end)
1311 if (TREE_CODE (OVL_CURRENT (*methods)) == TEMPLATE_DECL
1312 && IDENTIFIER_TYPENAME_P (DECL_NAME (OVL_CURRENT (*methods))))
1313 break;
1317 if (methods != end)
1318 return methods - &TREE_VEC_ELT (method_vec, 0);
1321 return -1;
1324 /* Starting from BASETYPE, return a TREE_BASELINK-like object
1325 which gives the following information (in a list):
1327 TREE_TYPE: list of basetypes needed to get to...
1328 TREE_VALUE: list of all functions in a given type
1329 which have name NAME.
1331 No access information is computed by this function,
1332 other then to adorn the list of basetypes with
1333 TREE_VIA_PUBLIC.
1335 If there are two ways to find a name (two members), if COMPLAIN is
1336 non-zero, then error_mark_node is returned, and an error message is
1337 printed, otherwise, just an error_mark_node is returned.
1339 As a special case, is COMPLAIN is -1, we don't complain, and we
1340 don't return error_mark_node, but rather the complete list of
1341 virtuals. This is used by get_virtuals_named_this. */
1343 tree
1344 lookup_fnfields (basetype_path, name, complain)
1345 tree basetype_path, name;
1346 int complain;
1348 int head = 0, tail = 0;
1349 tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE;
1350 tree rval_binfo_h = NULL_TREE, binfo, basetype_chain, binfo_h;
1351 int idx, find_all = 0;
1353 /* rval_binfo is the binfo associated with the found member, note,
1354 this can be set with useful information, even when rval is not
1355 set, because it must deal with ALL members, not just function
1356 members. It is used for ambiguity checking and the hidden
1357 checks. Whereas rval is only set if a proper (not hidden)
1358 function member is found. */
1360 /* rval_binfo_h and binfo_h are binfo values used when we perform the
1361 hiding checks, as virtual base classes may not be shared. The strategy
1362 is we always go into the binfo hierarchy owned by TYPE_BINFO of
1363 virtual base classes, as we cross virtual base class lines. This way
1364 we know that binfo of a virtual base class will always == itself when
1365 found along any line. (mrs) */
1367 /* For now, don't try this. */
1368 int protect = complain;
1370 char *errstr = 0;
1372 if (complain == -1)
1374 find_all = 1;
1375 protect = complain = 0;
1378 #if 0
1379 /* We cannot search for constructor/destructor names like this. */
1380 /* This can't go here, but where should it go? */
1381 /* If we are looking for a constructor in a templated type, use the
1382 unspecialized name, as that is how we store it. */
1383 if (IDENTIFIER_TEMPLATE (name))
1384 name = constructor_name (name);
1385 #endif
1387 binfo = basetype_path;
1388 binfo_h = binfo;
1389 type = complete_type (BINFO_TYPE (basetype_path));
1391 #ifdef GATHER_STATISTICS
1392 n_calls_lookup_fnfields++;
1393 #endif /* GATHER_STATISTICS */
1395 idx = lookup_fnfields_here (type, name);
1396 if (idx >= 0 || lookup_field_1 (type, name))
1398 rval_binfo = basetype_path;
1399 rval_binfo_h = rval_binfo;
1402 if (idx >= 0)
1404 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1405 rvals = scratch_tree_cons (basetype_path, rval, rvals);
1406 if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type))
1407 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx);
1409 return rvals;
1411 rval = NULL_TREE;
1413 if (name == ctor_identifier || name == dtor_identifier)
1415 /* Don't allow lookups of constructors and destructors to go
1416 deeper than the first place we look. */
1417 return NULL_TREE;
1420 if (basetype_path == TYPE_BINFO (type))
1422 basetype_chain = CLASSTYPE_BINFO_AS_LIST (type);
1423 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1424 980827);
1426 else
1427 basetype_chain = build_expr_list (NULL_TREE, basetype_path);
1429 /* The ambiguity check relies upon breadth first searching. */
1431 search_stack = push_search_level (search_stack, &search_obstack);
1432 binfo = basetype_path;
1433 binfo_h = binfo;
1435 while (1)
1437 tree binfos = BINFO_BASETYPES (binfo);
1438 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1439 int idx;
1441 /* Process and/or queue base types. */
1442 for (i = 0; i < n_baselinks; i++)
1444 tree base_binfo = TREE_VEC_ELT (binfos, i);
1445 if (BINFO_FIELDS_MARKED (base_binfo) == 0)
1447 tree btypes;
1449 SET_BINFO_FIELDS_MARKED (base_binfo);
1450 btypes = scratch_tree_cons (NULL_TREE, base_binfo, basetype_chain);
1451 if (TREE_VIA_VIRTUAL (base_binfo))
1452 btypes = scratch_tree_cons (NULL_TREE,
1453 TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
1454 btypes);
1455 else
1456 btypes = scratch_tree_cons (NULL_TREE,
1457 TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
1458 btypes);
1459 obstack_ptr_grow (&search_obstack, btypes);
1460 tail += 1;
1461 if (tail >= search_stack->limit)
1462 my_friendly_abort (99);
1466 /* Process head of queue, if one exists. */
1467 if (head >= tail)
1468 break;
1470 basetype_chain = search_stack->first[head++];
1471 binfo_h = TREE_VALUE (basetype_chain);
1472 basetype_chain = TREE_CHAIN (basetype_chain);
1473 basetype_path = TREE_VALUE (basetype_chain);
1474 if (TREE_CHAIN (basetype_chain))
1475 my_friendly_assert
1476 ((BINFO_INHERITANCE_CHAIN (basetype_path)
1477 == TREE_VALUE (TREE_CHAIN (basetype_chain)))
1478 /* We only approximate base info for partial instantiations. */
1479 || current_template_parms,
1480 980827);
1481 else
1482 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path)
1483 == NULL_TREE, 980827);
1485 binfo = basetype_path;
1486 type = BINFO_TYPE (binfo);
1488 /* See if we can find NAME in TYPE. If RVAL is nonzero,
1489 and we do find NAME in TYPE, verify that such a second
1490 sighting is in fact valid. */
1492 idx = lookup_fnfields_here (type, name);
1494 if (idx >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all))
1496 if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h))
1498 /* This is ok, the member found is in rval_binfo, not
1499 here (binfo). */
1501 else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h))
1503 /* This is ok, the member found is here (binfo), not in
1504 rval_binfo. */
1505 if (idx >= 0)
1507 rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1508 /* Note, rvals can only be previously set if find_all is
1509 true. */
1510 rvals = scratch_tree_cons (basetype_path, rval, rvals);
1511 if (TYPE_BINFO_BASETYPES (type)
1512 && CLASSTYPE_BASELINK_VEC (type))
1513 TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx);
1515 else
1517 /* Undo finding it before, as something else hides it. */
1518 rval = NULL_TREE;
1519 rvals = NULL_TREE;
1521 rval_binfo = binfo;
1522 rval_binfo_h = binfo_h;
1524 else
1526 /* This is ambiguous. */
1527 errstr = "request for method `%D' is ambiguous";
1528 rvals = error_mark_node;
1529 break;
1534 tree *tp = search_stack->first;
1535 tree *search_tail = tp + tail;
1537 while (tp < search_tail)
1539 CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
1540 tp += 1;
1543 search_stack = pop_search_level (search_stack);
1545 if (errstr && protect)
1547 cp_error (errstr, name);
1548 rvals = error_mark_node;
1551 return rvals;
1554 /* Look for a field or function named NAME in an inheritance lattice
1555 dominated by XBASETYPE. PROTECT is zero if we can avoid computing
1556 access information, otherwise it is 1. WANT_TYPE is 1 when we should
1557 only return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE. */
1559 tree
1560 lookup_member (xbasetype, name, protect, want_type)
1561 tree xbasetype, name;
1562 int protect, want_type;
1564 tree ret, basetype_path;
1566 if (TREE_CODE (xbasetype) == TREE_VEC)
1567 basetype_path = xbasetype;
1568 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1570 basetype_path = TYPE_BINFO (xbasetype);
1571 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path)
1572 == NULL_TREE, 980827);
1574 else
1575 my_friendly_abort (97);
1577 ret = lookup_field (basetype_path, name, protect, want_type);
1578 if (! ret && ! want_type)
1579 ret = lookup_fnfields (basetype_path, name, protect);
1580 return ret;
1583 /* BREADTH-FIRST SEARCH ROUTINES. */
1585 /* Search a multiple inheritance hierarchy by breadth-first search.
1587 BINFO is an aggregate type, possibly in a multiple-inheritance hierarchy.
1588 TESTFN is a function, which, if true, means that our condition has been met,
1589 and its return value should be returned.
1590 QFN, if non-NULL, is a predicate dictating whether the type should
1591 even be queued. */
1593 static HOST_WIDE_INT
1594 breadth_first_search (binfo, testfn, qfn)
1595 tree binfo;
1596 int (*testfn) PROTO((tree, int));
1597 int (*qfn) PROTO((tree, int));
1599 int head = 0, tail = 0;
1600 int rval = 0;
1602 search_stack = push_search_level (search_stack, &search_obstack);
1604 while (1)
1606 tree binfos = BINFO_BASETYPES (binfo);
1607 int n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1608 int i;
1610 /* Process and/or queue base types. */
1611 for (i = 0; i < n_baselinks; i++)
1613 tree base_binfo = TREE_VEC_ELT (binfos, i);
1615 if (BINFO_MARKED (base_binfo) == 0
1616 && (qfn == 0 || (*qfn) (binfo, i)))
1618 SET_BINFO_MARKED (base_binfo);
1619 obstack_ptr_grow (&search_obstack, binfo);
1620 obstack_ptr_grow (&search_obstack, (HOST_WIDE_INT) i);
1621 tail += 2;
1622 if (tail >= search_stack->limit)
1623 my_friendly_abort (100);
1626 /* Process head of queue, if one exists. */
1627 if (head >= tail)
1629 rval = 0;
1630 break;
1633 binfo = search_stack->first[head++];
1634 i = (HOST_WIDE_INT) search_stack->first[head++];
1635 if ((rval = (*testfn) (binfo, i)))
1636 break;
1637 binfo = BINFO_BASETYPE (binfo, i);
1640 tree *tp = search_stack->first;
1641 tree *search_tail = tp + tail;
1642 while (tp < search_tail)
1644 tree binfo = *tp++;
1645 int i = (HOST_WIDE_INT)(*tp++);
1646 CLEAR_BINFO_MARKED (BINFO_BASETYPE (binfo, i));
1650 search_stack = pop_search_level (search_stack);
1651 return rval;
1654 /* Functions to use in breadth first searches. */
1655 typedef int (*pfi) PROTO((tree, int));
1657 static tree declarator;
1659 static tree
1660 get_virtuals_named_this (binfo)
1661 tree binfo;
1663 tree fields;
1665 fields = lookup_fnfields (binfo, declarator, -1);
1666 /* fields cannot be error_mark_node */
1668 if (fields == 0)
1669 return 0;
1671 /* Get to the function decls, and return the first virtual function
1672 with this name, if there is one. */
1673 while (fields)
1675 tree fndecl;
1677 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl))
1678 if (DECL_VINDEX (OVL_CURRENT (fndecl)))
1679 return fields;
1680 fields = next_baselink (fields);
1682 return NULL_TREE;
1685 static tree
1686 get_virtual_destructor (binfo, i)
1687 tree binfo;
1688 int i;
1690 tree type = BINFO_TYPE (binfo);
1691 if (i >= 0)
1692 type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
1693 if (TYPE_HAS_DESTRUCTOR (type)
1694 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1)))
1695 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1);
1696 return 0;
1699 static int
1700 tree_has_any_destructor_p (binfo, i)
1701 tree binfo;
1702 int i;
1704 tree type = BINFO_TYPE (binfo);
1705 if (i >= 0)
1706 type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
1707 return TYPE_NEEDS_DESTRUCTOR (type);
1710 /* Returns > 0 if a function with type DRETTYPE overriding a function
1711 with type BRETTYPE is covariant, as defined in [class.virtual].
1713 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1714 adjustment), or -1 if pedantically invalid covariance. */
1716 static int
1717 covariant_return_p (brettype, drettype)
1718 tree brettype, drettype;
1720 tree binfo;
1722 if (TREE_CODE (brettype) == FUNCTION_DECL
1723 || TREE_CODE (brettype) == THUNK_DECL)
1725 brettype = TREE_TYPE (TREE_TYPE (brettype));
1726 drettype = TREE_TYPE (TREE_TYPE (drettype));
1728 else if (TREE_CODE (brettype) == METHOD_TYPE)
1730 brettype = TREE_TYPE (brettype);
1731 drettype = TREE_TYPE (drettype);
1734 if (comptypes (brettype, drettype, 1))
1735 return 0;
1737 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1738 && (TREE_CODE (brettype) == POINTER_TYPE
1739 || TREE_CODE (brettype) == REFERENCE_TYPE)
1740 && TYPE_READONLY (brettype) == TYPE_READONLY (drettype)
1741 && TYPE_VOLATILE (brettype) == TYPE_VOLATILE (drettype)))
1742 return 0;
1744 if (! can_convert (brettype, drettype))
1745 return 0;
1747 brettype = TREE_TYPE (brettype);
1748 drettype = TREE_TYPE (drettype);
1750 /* If not pedantic, allow any standard pointer conversion. */
1751 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
1752 return -1;
1754 binfo = get_binfo (brettype, drettype, 1);
1756 /* If we get an error_mark_node from get_binfo, it already complained,
1757 so let's just succeed. */
1758 if (binfo == error_mark_node)
1759 return 1;
1761 if (! BINFO_OFFSET_ZEROP (binfo) || TREE_VIA_VIRTUAL (binfo))
1762 return 2;
1763 return 1;
1766 /* Given a class type TYPE, and a function decl FNDECL, look for a
1767 virtual function in TYPE's hierarchy which FNDECL could match as a
1768 virtual function. It doesn't matter which one we find.
1770 DTORP is nonzero if we are looking for a destructor. Destructors
1771 need special treatment because they do not match by name. */
1773 tree
1774 get_matching_virtual (binfo, fndecl, dtorp)
1775 tree binfo, fndecl;
1776 int dtorp;
1778 tree tmp = NULL_TREE;
1779 int i;
1781 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
1782 /* In [temp.mem] we have:
1784 A specialization of a member function template does not
1785 override a virtual function from a base class. */
1786 return NULL_TREE;
1788 /* Breadth first search routines start searching basetypes
1789 of TYPE, so we must perform first ply of search here. */
1790 if (dtorp)
1792 if (tree_has_any_destructor_p (binfo, -1))
1793 tmp = get_virtual_destructor (binfo, -1);
1795 if (tmp)
1796 return tmp;
1798 tmp = (tree) breadth_first_search (binfo,
1799 (pfi) get_virtual_destructor,
1800 tree_has_any_destructor_p);
1801 return tmp;
1803 else
1805 tree drettype, dtypes, btypes, instptr_type;
1806 tree basetype = DECL_CLASS_CONTEXT (fndecl);
1807 tree baselink, best = NULL_TREE;
1808 tree name = DECL_ASSEMBLER_NAME (fndecl);
1810 declarator = DECL_NAME (fndecl);
1811 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
1812 return NULL_TREE;
1814 baselink = get_virtuals_named_this (binfo);
1815 if (baselink == NULL_TREE)
1816 return NULL_TREE;
1818 drettype = TREE_TYPE (TREE_TYPE (fndecl));
1819 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1820 if (DECL_STATIC_FUNCTION_P (fndecl))
1821 instptr_type = NULL_TREE;
1822 else
1823 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
1825 for (; baselink; baselink = next_baselink (baselink))
1827 tree tmps;
1828 for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps))
1830 tmp = OVL_CURRENT (tmps);
1831 if (! DECL_VINDEX (tmp))
1832 continue;
1834 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
1835 if (instptr_type == NULL_TREE)
1837 if (compparms (TREE_CHAIN (btypes), dtypes, 3))
1838 /* Caller knows to give error in this case. */
1839 return tmp;
1840 return NULL_TREE;
1843 if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
1844 == TYPE_READONLY (instptr_type))
1845 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3))
1847 tree brettype = TREE_TYPE (TREE_TYPE (tmp));
1848 if (comptypes (brettype, drettype, 1))
1849 /* OK */;
1850 else if ((i = covariant_return_p (brettype, drettype)))
1852 if (i == 2)
1853 sorry ("adjusting pointers for covariant returns");
1855 if (pedantic && i == -1)
1857 cp_pedwarn_at ("invalid covariant return type for `%#D' (must be pointer or reference to class)", fndecl);
1858 cp_pedwarn_at (" overriding `%#D'", tmp);
1861 else if (IS_AGGR_TYPE_2 (brettype, drettype)
1862 && comptypes (brettype, drettype, 0))
1864 error ("invalid covariant return type (must use pointer or reference)");
1865 cp_error_at (" overriding `%#D'", tmp);
1866 cp_error_at (" with `%#D'", fndecl);
1868 else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE)
1870 cp_error_at ("conflicting return type specified for virtual function `%#D'", fndecl);
1871 cp_error_at (" overriding definition as `%#D'", tmp);
1872 SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
1874 break;
1877 /* If not at the end */
1878 if (tmps)
1880 best = tmp;
1881 break;
1885 return best;
1889 /* Return the list of virtual functions which are abstract in type
1890 TYPE that come from non virtual base classes. See
1891 expand_direct_vtbls_init for the style of search we do. */
1893 static tree
1894 get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
1895 tree binfo;
1896 int do_self;
1897 tree abstract_virtuals;
1899 tree binfos = BINFO_BASETYPES (binfo);
1900 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1902 for (i = 0; i < n_baselinks; i++)
1904 tree base_binfo = TREE_VEC_ELT (binfos, i);
1905 int is_not_base_vtable
1906 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
1907 if (! TREE_VIA_VIRTUAL (base_binfo))
1908 abstract_virtuals
1909 = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
1910 abstract_virtuals);
1912 /* Should we use something besides CLASSTYPE_VFIELDS? */
1913 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
1915 tree virtuals = BINFO_VIRTUALS (binfo);
1917 skip_rtti_stuff (&virtuals);
1919 while (virtuals)
1921 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
1922 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
1923 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
1924 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
1925 virtuals = TREE_CHAIN (virtuals);
1928 return abstract_virtuals;
1931 /* Return the list of virtual functions which are abstract in type TYPE.
1932 This information is cached, and so must be built on a
1933 non-temporary obstack. */
1935 tree
1936 get_abstract_virtuals (type)
1937 tree type;
1939 tree vbases;
1940 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
1942 /* First get all from non-virtual bases. */
1943 abstract_virtuals
1944 = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
1946 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
1948 tree virtuals = BINFO_VIRTUALS (vbases);
1950 skip_rtti_stuff (&virtuals);
1952 while (virtuals)
1954 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
1955 tree base_fndecl = TREE_OPERAND (base_pfn, 0);
1956 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
1957 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
1958 virtuals = TREE_CHAIN (virtuals);
1961 return nreverse (abstract_virtuals);
1964 /* For the type TYPE, return a list of member functions available from
1965 base classes with name NAME. The TREE_VALUE of the list is a chain of
1966 member functions with name NAME. The TREE_PURPOSE of the list is a
1967 basetype, or a list of base types (in reverse order) which were
1968 traversed to reach the chain of member functions. If we reach a base
1969 type which provides a member function of name NAME, and which has at
1970 most one base type itself, then we can terminate the search. */
1972 tree
1973 get_baselinks (type_as_binfo_list, type, name)
1974 tree type_as_binfo_list;
1975 tree type, name;
1977 int head = 0, tail = 0, idx;
1978 tree rval = 0, nval = 0;
1979 tree basetypes = type_as_binfo_list;
1980 tree binfo = TYPE_BINFO (type);
1982 search_stack = push_search_level (search_stack, &search_obstack);
1984 while (1)
1986 tree binfos = BINFO_BASETYPES (binfo);
1987 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1989 /* Process and/or queue base types. */
1990 for (i = 0; i < n_baselinks; i++)
1992 tree base_binfo = TREE_VEC_ELT (binfos, i);
1993 tree btypes;
1995 btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo),
1996 TREE_VIA_VIRTUAL (base_binfo),
1997 TREE_VIA_PROTECTED (base_binfo),
1998 NULL_TREE, base_binfo,
1999 basetypes);
2000 obstack_ptr_grow (&search_obstack, btypes);
2001 search_stack->first = (tree *)obstack_base (&search_obstack);
2002 tail += 1;
2005 dont_queue:
2006 /* Process head of queue, if one exists. */
2007 if (head >= tail)
2008 break;
2010 basetypes = search_stack->first[head++];
2011 binfo = TREE_VALUE (basetypes);
2012 type = BINFO_TYPE (binfo);
2013 idx = lookup_fnfields_1 (type, name);
2014 if (idx >= 0)
2016 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
2017 rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval);
2018 if (TYPE_BINFO_BASETYPES (type) == 0)
2019 goto dont_queue;
2020 else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1)
2022 if (CLASSTYPE_BASELINK_VEC (type))
2023 TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx);
2024 goto dont_queue;
2027 nval = NULL_TREE;
2030 search_stack = pop_search_level (search_stack);
2031 return rval;
2034 tree
2035 next_baselink (baselink)
2036 tree baselink;
2038 tree tmp = TREE_TYPE (baselink);
2039 baselink = TREE_CHAIN (baselink);
2040 while (tmp)
2042 /* @@ does not yet add previous base types. */
2043 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2044 baselink);
2045 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2046 tmp = TREE_CHAIN (tmp);
2048 return baselink;
2051 /* DEPTH-FIRST SEARCH ROUTINES. */
2053 /* This routine converts a pointer to be a pointer of an immediate
2054 base class. The normal convert_pointer_to routine would diagnose
2055 the conversion as ambiguous, under MI code that has the base class
2056 as an ambiguous base class. */
2058 static tree
2059 convert_pointer_to_single_level (to_type, expr)
2060 tree to_type, expr;
2062 tree binfo_of_derived;
2063 tree last;
2065 binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)));
2066 last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0);
2067 my_friendly_assert (BINFO_INHERITANCE_CHAIN (last) == binfo_of_derived,
2068 980827);
2069 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived) == NULL_TREE,
2070 980827);
2071 return build_vbase_path (PLUS_EXPR, build_pointer_type (to_type), expr,
2072 last, 1);
2075 /* The main function which implements depth first search.
2077 This routine has to remember the path it walked up, when
2078 dfs_init_vbase_pointers is the work function, as otherwise there
2079 would be no record. */
2081 static void
2082 dfs_walk (binfo, fn, qfn)
2083 tree binfo;
2084 void (*fn) PROTO((tree));
2085 int (*qfn) PROTO((tree));
2087 tree binfos = BINFO_BASETYPES (binfo);
2088 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2090 for (i = 0; i < n_baselinks; i++)
2092 tree base_binfo = TREE_VEC_ELT (binfos, i);
2094 if (qfn == 0 || (*qfn)(base_binfo))
2096 if (TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TYPE_PARM
2097 || TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TEMPLATE_PARM)
2098 /* Pass */;
2099 else if (fn == dfs_init_vbase_pointers)
2101 /* When traversing an arbitrary MI hierarchy, we need to keep
2102 a record of the path we took to get down to the final base
2103 type, as otherwise there would be no record of it, and just
2104 trying to blindly convert at the bottom would be ambiguous.
2106 The easiest way is to do the conversions one step at a time,
2107 as we know we want the immediate base class at each step.
2109 The only special trick to converting one step at a time,
2110 is that when we hit the last virtual base class, we must
2111 use the SLOT value for it, and not use the normal convert
2112 routine. We use the last virtual base class, as in our
2113 implementation, we have pointers to all virtual base
2114 classes in the base object. */
2116 tree saved_vbase_decl_ptr_intermediate
2117 = vbase_decl_ptr_intermediate;
2119 if (TREE_VIA_VIRTUAL (base_binfo))
2121 /* No need for the conversion here, as we know it is the
2122 right type. */
2123 vbase_decl_ptr_intermediate
2124 = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo));
2126 else
2128 vbase_decl_ptr_intermediate
2129 = convert_pointer_to_single_level (BINFO_TYPE (base_binfo),
2130 vbase_decl_ptr_intermediate);
2133 dfs_walk (base_binfo, fn, qfn);
2135 vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate;
2137 else
2138 dfs_walk (base_binfo, fn, qfn);
2142 fn (binfo);
2145 /* Like dfs_walk, but only walk until fn returns something, and return
2146 that. We also use the real vbase binfos instead of the placeholders
2147 in the normal binfo hierarchy. START is the most-derived type for this
2148 hierarchy, so that we can find the vbase binfos. */
2150 static tree
2151 dfs_search (binfo, fn, start)
2152 tree binfo, start;
2153 tree (*fn) PROTO((tree));
2155 tree binfos = BINFO_BASETYPES (binfo);
2156 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2157 tree retval;
2159 for (i = 0; i < n_baselinks; i++)
2161 tree base_binfo = TREE_VEC_ELT (binfos, i);
2163 if (TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TYPE_PARM
2164 || TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TEMPLATE_PARM)
2165 /* Pass */;
2166 else
2168 if (TREE_VIA_VIRTUAL (base_binfo) && start)
2169 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2170 CLASSTYPE_VBASECLASSES (start));
2171 retval = dfs_search (base_binfo, fn, start);
2172 if (retval)
2173 return retval;
2177 return fn (binfo);
2180 static int markedp (binfo) tree binfo;
2181 { return BINFO_MARKED (binfo); }
2182 static int unmarkedp (binfo) tree binfo;
2183 { return BINFO_MARKED (binfo) == 0; }
2185 #if 0
2186 static int bfs_markedp (binfo, i) tree binfo; int i;
2187 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); }
2188 static int bfs_unmarkedp (binfo, i) tree binfo; int i;
2189 { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2190 static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i;
2191 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); }
2192 static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i;
2193 { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2194 static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i;
2195 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); }
2196 static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i;
2197 { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
2198 #endif
2200 static int marked_vtable_pathp (binfo) tree binfo;
2201 { return BINFO_VTABLE_PATH_MARKED (binfo); }
2202 static int unmarked_vtable_pathp (binfo) tree binfo;
2203 { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; }
2204 static int marked_new_vtablep (binfo) tree binfo;
2205 { return BINFO_NEW_VTABLE_MARKED (binfo); }
2206 static int unmarked_new_vtablep (binfo) tree binfo;
2207 { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; }
2208 static int marked_pushdecls_p (binfo) tree binfo;
2209 { return BINFO_PUSHDECLS_MARKED (binfo); }
2210 static int unmarked_pushdecls_p (binfo) tree binfo;
2211 { return BINFO_PUSHDECLS_MARKED (binfo) == 0; }
2213 #if 0
2214 static int dfs_search_slot_nonempty_p (binfo) tree binfo;
2215 { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
2216 #endif
2218 static int dfs_debug_unmarkedp (binfo) tree binfo;
2219 { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; }
2221 /* The worker functions for `dfs_walk'. These do not need to
2222 test anything (vis a vis marking) if they are paired with
2223 a predicate function (above). */
2225 #if 0
2226 static void
2227 dfs_mark (binfo) tree binfo;
2228 { SET_BINFO_MARKED (binfo); }
2229 #endif
2231 static void
2232 dfs_unmark (binfo) tree binfo;
2233 { CLEAR_BINFO_MARKED (binfo); }
2235 #if 0
2236 static void
2237 dfs_mark_vtable_path (binfo) tree binfo;
2238 { SET_BINFO_VTABLE_PATH_MARKED (binfo); }
2240 static void
2241 dfs_unmark_vtable_path (binfo) tree binfo;
2242 { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
2244 static void
2245 dfs_mark_new_vtable (binfo) tree binfo;
2246 { SET_BINFO_NEW_VTABLE_MARKED (binfo); }
2248 static void
2249 dfs_unmark_new_vtable (binfo) tree binfo;
2250 { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
2252 static void
2253 dfs_clear_search_slot (binfo) tree binfo;
2254 { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
2255 #endif
2257 static void
2258 dfs_debug_mark (binfo)
2259 tree binfo;
2261 tree t = BINFO_TYPE (binfo);
2263 /* Use heuristic that if there are virtual functions,
2264 ignore until we see a non-inline virtual function. */
2265 tree methods = CLASSTYPE_METHOD_VEC (t);
2267 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2269 if (methods == 0)
2270 return;
2272 /* If interface info is known, either we've already emitted the debug
2273 info or we don't need to. */
2274 if (CLASSTYPE_INTERFACE_KNOWN (t))
2275 return;
2277 /* If debug info is requested from this context for this type, supply it.
2278 If debug info is requested from another context for this type,
2279 see if some third context can supply it. */
2280 if (current_function_decl == NULL_TREE
2281 || DECL_CLASS_CONTEXT (current_function_decl) != t)
2283 if (TREE_VEC_ELT (methods, 1))
2284 methods = TREE_VEC_ELT (methods, 1);
2285 else if (TREE_VEC_ELT (methods, 0))
2286 methods = TREE_VEC_ELT (methods, 0);
2287 else
2288 methods = TREE_VEC_ELT (methods, 2);
2289 methods = OVL_CURRENT (methods);
2290 while (methods)
2292 if (DECL_VINDEX (methods)
2293 && DECL_THIS_INLINE (methods) == 0
2294 && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
2296 /* Somebody, somewhere is going to have to define this
2297 virtual function. When they do, they will provide
2298 the debugging info. */
2299 return;
2301 methods = TREE_CHAIN (methods);
2304 /* We cannot rely on some alien method to solve our problems,
2305 so we must write out the debug info ourselves. */
2306 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
2307 rest_of_type_compilation (t, toplevel_bindings_p ());
2310 /* Attach to the type of the virtual base class, the pointer to the
2311 virtual base class, given the global pointer vbase_decl_ptr.
2313 We use the global vbase_types. ICK! */
2315 static void
2316 dfs_find_vbases (binfo)
2317 tree binfo;
2319 tree binfos = BINFO_BASETYPES (binfo);
2320 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2322 for (i = n_baselinks-1; i >= 0; i--)
2324 tree base_binfo = TREE_VEC_ELT (binfos, i);
2326 if (TREE_VIA_VIRTUAL (base_binfo)
2327 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
2329 tree vbase = BINFO_TYPE (base_binfo);
2330 tree binfo = binfo_member (vbase, vbase_types);
2332 CLASSTYPE_SEARCH_SLOT (vbase)
2333 = build (PLUS_EXPR, build_pointer_type (vbase),
2334 vbase_decl_ptr, BINFO_OFFSET (binfo));
2337 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2338 SET_BINFO_NEW_VTABLE_MARKED (binfo);
2341 static void
2342 dfs_init_vbase_pointers (binfo)
2343 tree binfo;
2345 tree type = BINFO_TYPE (binfo);
2346 tree fields = TYPE_FIELDS (type);
2347 tree this_vbase_ptr;
2349 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2351 #if 0
2352 /* See finish_struct_1 for when we can enable this. */
2353 /* If we have a vtable pointer first, skip it. */
2354 if (VFIELD_NAME_P (DECL_NAME (fields)))
2355 fields = TREE_CHAIN (fields);
2356 #endif
2358 if (fields == NULL_TREE
2359 || DECL_NAME (fields) == NULL_TREE
2360 || ! VBASE_NAME_P (DECL_NAME (fields)))
2361 return;
2363 this_vbase_ptr = vbase_decl_ptr_intermediate;
2365 if (build_pointer_type (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
2366 my_friendly_abort (125);
2368 while (fields && DECL_NAME (fields)
2369 && VBASE_NAME_P (DECL_NAME (fields)))
2371 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2372 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
2373 tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
2374 vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
2375 vbase_types),
2376 build_modify_expr (ref, NOP_EXPR, init),
2377 vbase_init_result);
2378 fields = TREE_CHAIN (fields);
2382 /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2383 times, just NEW_VTABLE, but optimizer should make both with equal
2384 efficiency (though it does not currently). */
2386 static void
2387 dfs_clear_vbase_slots (binfo)
2388 tree binfo;
2390 tree type = BINFO_TYPE (binfo);
2391 CLASSTYPE_SEARCH_SLOT (type) = 0;
2392 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2393 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2396 tree
2397 init_vbase_pointers (type, decl_ptr)
2398 tree type;
2399 tree decl_ptr;
2401 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2403 int old_flag = flag_this_is_variable;
2404 tree binfo = TYPE_BINFO (type);
2405 flag_this_is_variable = -2;
2406 vbase_types = CLASSTYPE_VBASECLASSES (type);
2407 vbase_decl_ptr = vbase_decl_ptr_intermediate = decl_ptr;
2408 vbase_init_result = NULL_TREE;
2409 dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp);
2410 dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp);
2411 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2412 flag_this_is_variable = old_flag;
2413 return vbase_init_result;
2415 return 0;
2418 /* get the virtual context (the vbase that directly contains the
2419 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2420 or NULL_TREE if there is none.
2422 FNDECL must come from a virtual table from a virtual base to ensure that
2423 there is only one possible DECL_CLASS_CONTEXT.
2425 We know that if there is more than one place (binfo) the fndecl that the
2426 declared, they all refer to the same binfo. See get_class_offset_1 for
2427 the check that ensures this. */
2429 static tree
2430 virtual_context (fndecl, t, vbase)
2431 tree fndecl, t, vbase;
2433 tree path;
2434 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
2436 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2437 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0)
2439 while (path)
2441 /* Not sure if checking path == vbase is necessary here, but just in
2442 case it is. */
2443 if (TREE_VIA_VIRTUAL (path) || path == vbase)
2444 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2445 path = BINFO_INHERITANCE_CHAIN (path);
2448 /* This shouldn't happen, I don't want errors! */
2449 warning ("recoverable compiler error, fixups for virtual function");
2450 return vbase;
2452 while (path)
2454 if (TREE_VIA_VIRTUAL (path))
2455 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2456 path = BINFO_INHERITANCE_CHAIN (path);
2458 return 0;
2461 /* Fixups upcast offsets for one vtable.
2462 Entries may stay within the VBASE given, or
2463 they may upcast into a direct base, or
2464 they may upcast into a different vbase.
2466 We only need to do fixups in case 2 and 3. In case 2, we add in
2467 the virtual base offset to effect an upcast, in case 3, we add in
2468 the virtual base offset to effect an upcast, then subtract out the
2469 offset for the other virtual base, to effect a downcast into it.
2471 This routine mirrors fixup_vtable_deltas in functionality, though
2472 this one is runtime based, and the other is compile time based.
2473 Conceivably that routine could be removed entirely, and all fixups
2474 done at runtime.
2476 VBASE_OFFSETS is an association list of virtual bases that contains
2477 offset information for the virtual bases, so the offsets are only
2478 calculated once. The offsets are computed by where we think the
2479 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
2480 the vbase really is. */
2482 static void
2483 expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t,
2484 vbase_offsets)
2485 tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets;
2487 tree virtuals = BINFO_VIRTUALS (binfo);
2488 tree vc;
2489 tree delta;
2490 unsigned HOST_WIDE_INT n;
2492 delta = purpose_member (vbase, *vbase_offsets);
2493 if (! delta)
2495 delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
2496 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr);
2497 delta = save_expr (delta);
2498 delta = tree_cons (vbase, delta, *vbase_offsets);
2499 *vbase_offsets = delta;
2502 n = skip_rtti_stuff (&virtuals);
2504 while (virtuals)
2506 tree current_fndecl = TREE_VALUE (virtuals);
2507 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2508 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2509 if (current_fndecl
2510 && current_fndecl != abort_fndecl
2511 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2513 /* This may in fact need a runtime fixup. */
2514 tree idx = build_int_2 (n, 0);
2515 tree vtbl = BINFO_VTABLE (binfo);
2516 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2517 tree aref, ref, naref;
2518 tree old_delta, new_delta;
2519 tree init;
2521 if (nvtbl == NULL_TREE
2522 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2524 /* Dup it if it isn't in local scope yet. */
2525 nvtbl = build_decl
2526 (VAR_DECL, DECL_NAME (vtbl),
2527 TYPE_MAIN_VARIANT (TREE_TYPE (BINFO_VTABLE (binfo))));
2528 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2529 DECL_ALIGN (nvtbl));
2530 TREE_READONLY (nvtbl) = 0;
2531 DECL_ARTIFICIAL (nvtbl) = 1;
2532 nvtbl = pushdecl (nvtbl);
2533 init = NULL_TREE;
2534 cp_finish_decl (nvtbl, init, NULL_TREE, 0,
2535 LOOKUP_ONLYCONVERTING);
2537 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2538 because they wouldn't be useful; everything that wants to
2539 look at the vtable will look at the decl for the normal
2540 vtable. Setting DECL_CONTEXT also screws up
2541 decl_function_context. */
2543 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2544 nvtbl, vtbl);
2545 TREE_SIDE_EFFECTS (init) = 1;
2546 expand_expr_stmt (init);
2547 /* Update the vtable pointers as necessary. */
2548 ref = build_vfield_ref
2549 (build_indirect_ref (addr, NULL_PTR),
2550 DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
2551 expand_expr_stmt
2552 (build_modify_expr (ref, NOP_EXPR,
2553 build_unary_op (ADDR_EXPR, nvtbl, 0)));
2555 assemble_external (vtbl);
2556 aref = build_array_ref (vtbl, idx);
2557 naref = build_array_ref (nvtbl, idx);
2558 old_delta = build_component_ref (aref, delta_identifier,
2559 NULL_TREE, 0);
2560 new_delta = build_component_ref (naref, delta_identifier,
2561 NULL_TREE, 0);
2563 /* This is a upcast, so we have to add the offset for the
2564 virtual base. */
2565 old_delta = build_binary_op (PLUS_EXPR, old_delta,
2566 TREE_VALUE (delta), 0);
2567 if (vc)
2569 /* If this is set, we need to subtract out the delta
2570 adjustments for the other virtual base that we
2571 downcast into. */
2572 tree vc_delta = purpose_member (vc, *vbase_offsets);
2573 if (! vc_delta)
2575 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
2576 vc_delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
2577 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
2578 vc_delta, vc_addr);
2579 vc_delta = save_expr (vc_delta);
2580 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2582 else
2583 vc_delta = TREE_VALUE (vc_delta);
2585 /* This is a downcast, so we have to subtract the offset
2586 for the virtual base. */
2587 old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta, 0);
2590 TREE_READONLY (new_delta) = 0;
2591 expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
2592 old_delta));
2594 ++n;
2595 virtuals = TREE_CHAIN (virtuals);
2599 /* Fixup upcast offsets for all direct vtables. Patterned after
2600 expand_direct_vtbls_init. */
2602 static void
2603 fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
2604 tree real_binfo, binfo;
2605 int init_self, can_elide;
2606 tree addr, orig_addr, type, vbase, *vbase_offsets;
2608 tree real_binfos = BINFO_BASETYPES (real_binfo);
2609 tree binfos = BINFO_BASETYPES (binfo);
2610 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
2612 for (i = 0; i < n_baselinks; i++)
2614 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
2615 tree base_binfo = TREE_VEC_ELT (binfos, i);
2616 int is_not_base_vtable
2617 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
2618 if (! TREE_VIA_VIRTUAL (real_base_binfo))
2619 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
2620 is_not_base_vtable, can_elide, addr,
2621 orig_addr, type, vbase, vbase_offsets);
2623 #if 0
2624 /* Before turning this on, make sure it is correct. */
2625 if (can_elide && ! BINFO_MODIFIED (binfo))
2626 return;
2627 #endif
2628 /* Should we use something besides CLASSTYPE_VFIELDS? */
2629 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
2631 tree new_addr = convert_pointer_to_real (binfo, addr);
2632 expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr,
2633 type, vbase_offsets);
2637 /* Build a COMPOUND_EXPR which when expanded will generate the code
2638 needed to initialize all the virtual function table slots of all
2639 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2640 the virtual baseclasses to use; TYPE is the type of the object to
2641 which the initialization applies. TRUE_EXP is the true object we
2642 are initializing, and DECL_PTR is the pointer to the sub-object we
2643 are initializing.
2645 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
2646 object was laid out by a top-level constructor and the computed
2647 offsets are valid to store vtables. When zero, we must store new
2648 vtables through virtual baseclass pointers.
2650 We setup and use the globals: vbase_decl_ptr, vbase_types
2651 ICK! */
2653 void
2654 expand_indirect_vtbls_init (binfo, true_exp, decl_ptr)
2655 tree binfo;
2656 tree true_exp, decl_ptr;
2658 tree type = BINFO_TYPE (binfo);
2660 /* This function executes during the finish_function() segment,
2661 AFTER the auto variables and temporary stack space has been marked
2662 unused...If space is needed for the virtual function tables,
2663 some of them might fit within what the compiler now thinks
2664 are available stack slots... These values are actually initialized at
2665 the beginnning of the function, so when the automatics use their space,
2666 they will overwrite the values that are placed here. Marking all
2667 temporary space as unavailable prevents this from happening. */
2669 mark_all_temps_used();
2671 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2673 rtx fixup_insns = NULL_RTX;
2674 tree vbases = CLASSTYPE_VBASECLASSES (type);
2675 vbase_types = vbases;
2676 vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
2678 dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep);
2680 /* Initialized with vtables of type TYPE. */
2681 for (; vbases; vbases = TREE_CHAIN (vbases))
2683 tree addr;
2685 addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vbase_decl_ptr);
2687 /* Do all vtables from this virtual base. */
2688 /* This assumes that virtual bases can never serve as parent
2689 binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */
2690 expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
2691 1, 0, addr);
2693 /* Now we adjust the offsets for virtual functions that
2694 cross virtual boundaries on an implicit upcast on vf call
2695 so that the layout of the most complete type is used,
2696 instead of assuming the layout of the virtual bases from
2697 our current type. */
2699 if (flag_vtable_thunks)
2701 /* We don't have dynamic thunks yet!
2702 So for now, just fail silently. */
2704 else
2706 tree vbase_offsets = NULL_TREE;
2707 push_to_sequence (fixup_insns);
2708 fixup_virtual_upcast_offsets (vbases,
2709 TYPE_BINFO (BINFO_TYPE (vbases)),
2710 1, 0, addr, vbase_decl_ptr,
2711 type, vbases, &vbase_offsets);
2712 fixup_insns = get_insns ();
2713 end_sequence ();
2717 if (fixup_insns)
2719 extern tree in_charge_identifier;
2720 tree in_charge_node = lookup_name (in_charge_identifier, 0);
2721 if (! in_charge_node)
2723 warning ("recoverable internal compiler error, nobody's in charge!");
2724 in_charge_node = integer_zero_node;
2726 in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1);
2727 expand_start_cond (in_charge_node, 0);
2728 emit_insns (fixup_insns);
2729 expand_end_cond ();
2732 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
2736 /* get virtual base class types.
2737 This adds type to the vbase_types list in reverse dfs order.
2738 Ordering is very important, so don't change it. */
2740 static void
2741 dfs_get_vbase_types (binfo)
2742 tree binfo;
2744 if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
2746 tree new_vbase = make_binfo (integer_zero_node, binfo,
2747 BINFO_VTABLE (binfo),
2748 BINFO_VIRTUALS (binfo));
2749 TREE_CHAIN (new_vbase) = vbase_types;
2750 TREE_VIA_VIRTUAL (new_vbase) = 1;
2751 vbase_types = new_vbase;
2752 SET_BINFO_VBASE_MARKED (binfo);
2754 SET_BINFO_MARKED (binfo);
2757 /* get a list of virtual base classes in dfs order. */
2759 tree
2760 get_vbase_types (type)
2761 tree type;
2763 tree vbases;
2764 tree binfo;
2766 binfo = TYPE_BINFO (type);
2767 vbase_types = NULL_TREE;
2768 dfs_walk (binfo, dfs_get_vbase_types, unmarkedp);
2769 dfs_walk (binfo, dfs_unmark, markedp);
2770 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2771 reverse it so that we get normal dfs ordering. */
2772 vbase_types = nreverse (vbase_types);
2774 /* unmark marked vbases */
2775 for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
2776 CLEAR_BINFO_VBASE_MARKED (vbases);
2778 return vbase_types;
2781 /* If we want debug info for a type TYPE, make sure all its base types
2782 are also marked as being potentially interesting. This avoids
2783 the problem of not writing any debug info for intermediate basetypes
2784 that have abstract virtual functions. Also mark member types. */
2786 void
2787 note_debug_info_needed (type)
2788 tree type;
2790 tree field;
2792 if (current_template_parms)
2793 return;
2795 if (TYPE_BEING_DEFINED (type))
2796 /* We can't go looking for the base types and fields just yet. */
2797 return;
2799 /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2800 does not support name references between translation units. Well, we
2801 could, but that would mean putting global labels in the debug output
2802 before each exported type and each of its functions and static data
2803 members. */
2804 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2805 return;
2807 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp);
2808 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2810 tree ttype;
2811 if (TREE_CODE (field) == FIELD_DECL
2812 && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
2813 && dfs_debug_unmarkedp (TYPE_BINFO (ttype)))
2814 note_debug_info_needed (ttype);
2818 /* Subroutines of push_class_decls (). */
2820 /* Add in a decl to the envelope. */
2821 static void
2822 envelope_add_decl (type, decl, values)
2823 tree type, decl, *values;
2825 tree context, *tmp;
2826 tree name = DECL_NAME (decl);
2827 int dont_add = 0;
2829 /* Yet Another Implicit Typename Kludge: Since we don't tsubst
2830 the members for partial instantiations, DECL_CONTEXT (decl) is wrong.
2831 But pretend it's right for this function. */
2832 if (processing_template_decl)
2833 type = DECL_REAL_CONTEXT (decl);
2835 /* virtual base names are always unique. */
2836 if (VBASE_NAME_P (name))
2837 *values = NULL_TREE;
2839 /* Possible ambiguity. If its defining type(s)
2840 is (are all) derived from us, no problem. */
2841 else if (*values && TREE_CODE (*values) != TREE_LIST)
2843 tree value = *values;
2844 /* Only complain if we shadow something we can access. */
2845 if (warn_shadow && TREE_CODE (decl) == FUNCTION_DECL
2846 && ((DECL_LANG_SPECIFIC (*values)
2847 && DECL_CLASS_CONTEXT (value) == current_class_type)
2848 || ! TREE_PRIVATE (value)))
2849 /* Should figure out access control more accurately. */
2851 cp_warning_at ("member `%#D' is shadowed", value);
2852 cp_warning_at ("by member function `%#D'", decl);
2853 warning ("in this context");
2856 context = DECL_REAL_CONTEXT (value);
2858 if (context == type)
2860 if (TREE_CODE (value) == TYPE_DECL
2861 && DECL_ARTIFICIAL (value))
2862 *values = NULL_TREE;
2863 else
2864 dont_add = 1;
2866 else if (type == current_class_type
2867 || DERIVED_FROM_P (context, type))
2869 /* Don't add in *values to list */
2870 *values = NULL_TREE;
2872 else
2873 *values = build_tree_list (NULL_TREE, value);
2875 else
2876 for (tmp = values; *tmp;)
2878 tree value = TREE_VALUE (*tmp);
2879 my_friendly_assert (TREE_CODE (value) != TREE_LIST, 999);
2880 context = (TREE_CODE (value) == FUNCTION_DECL
2881 && DECL_VIRTUAL_P (value))
2882 ? DECL_CLASS_CONTEXT (value)
2883 : DECL_CONTEXT (value);
2885 if (type == current_class_type
2886 || DERIVED_FROM_P (context, type))
2888 /* remove *tmp from list */
2889 *tmp = TREE_CHAIN (*tmp);
2891 else
2892 tmp = &TREE_CHAIN (*tmp);
2895 if (! dont_add)
2897 /* Put the new contents in our envelope. */
2898 if (TREE_CODE (decl) == FUNCTION_DECL)
2900 *values = tree_cons (name, decl, *values);
2901 TREE_NONLOCAL_FLAG (*values) = 1;
2902 TREE_TYPE (*values) = unknown_type_node;
2904 else
2906 if (*values)
2908 *values = tree_cons (NULL_TREE, decl, *values);
2909 /* Mark this as a potentially ambiguous member. */
2910 /* Leaving TREE_TYPE blank is intentional.
2911 We cannot use `error_mark_node' (lookup_name)
2912 or `unknown_type_node' (all member functions use this). */
2913 TREE_NONLOCAL_FLAG (*values) = 1;
2915 else
2916 *values = decl;
2921 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2922 because it (or one of the intermediate bases) depends on template parms. */
2924 static int
2925 dependent_base_p (binfo)
2926 tree binfo;
2928 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2930 if (TREE_TYPE (binfo) == current_class_type)
2931 break;
2932 if (uses_template_parms (TREE_TYPE (binfo)))
2933 return 1;
2935 return 0;
2938 /* Add the instance variables which this class contributed to the
2939 current class binding contour. When a redefinition occurs, if the
2940 redefinition is strictly within a single inheritance path, we just
2941 overwrite the old declaration with the new. If the fields are not
2942 within a single inheritance path, we must cons them.
2944 In order to know what decls are new (stemming from the current
2945 invocation of push_class_decls) we enclose them in an "envelope",
2946 which is a TREE_LIST node where the TREE_PURPOSE slot contains the
2947 new decl (or possibly a list of competing ones), the TREE_VALUE slot
2948 points to the old value and the TREE_CHAIN slot chains together all
2949 envelopes which needs to be "opened" in push_class_decls. Opening an
2950 envelope means: push the old value onto the class_shadowed list,
2951 install the new one and if it's a TYPE_DECL do the same to the
2952 IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that
2953 the TREE_PURPOSE slot is non-null, and that it is not an identifier.
2954 Because if it is, it could be a set of overloaded methods from an
2955 outer scope. */
2957 static void
2958 dfs_pushdecls (binfo)
2959 tree binfo;
2961 tree type = BINFO_TYPE (binfo);
2962 tree fields, *methods, *end;
2963 tree method_vec;
2964 int dummy = 0;
2966 /* Only record types if we're a template base. */
2967 if (processing_template_decl && type != current_class_type
2968 && dependent_base_p (binfo))
2969 dummy = 1;
2971 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2973 if (dummy && TREE_CODE (fields) != TYPE_DECL)
2974 continue;
2976 /* Unmark so that if we are in a constructor, and then find that
2977 this field was initialized by a base initializer,
2978 we can emit an error message. */
2979 if (TREE_CODE (fields) == FIELD_DECL)
2980 TREE_USED (fields) = 0;
2982 /* Recurse into anonymous unions. */
2983 if (DECL_NAME (fields) == NULL_TREE
2984 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
2986 dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields)));
2987 continue;
2990 if (DECL_NAME (fields))
2992 tree name = DECL_NAME (fields);
2993 tree class_value = IDENTIFIER_CLASS_VALUE (name);
2995 /* If the class value is not an envelope of the kind described in
2996 the comment above, we create a new envelope. */
2997 if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
2998 || TREE_PURPOSE (class_value) == NULL_TREE
2999 || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
3001 /* See comment above for a description of envelopes. */
3002 closed_envelopes = tree_cons (NULL_TREE, class_value,
3003 closed_envelopes);
3004 IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
3005 class_value = IDENTIFIER_CLASS_VALUE (name);
3008 envelope_add_decl (type, fields, &TREE_PURPOSE (class_value));
3012 method_vec = CLASSTYPE_METHOD_VEC (type);
3013 if (method_vec && ! dummy)
3015 /* Farm out constructors and destructors. */
3016 methods = &TREE_VEC_ELT (method_vec, 2);
3017 end = TREE_VEC_END (method_vec);
3019 while (methods != end)
3021 /* This will cause lookup_name to return a pointer
3022 to the tree_list of possible methods of this name. */
3023 tree name = DECL_NAME (OVL_CURRENT (*methods));
3024 tree class_value = IDENTIFIER_CLASS_VALUE (name);
3026 /* If the class value is not an envelope of the kind described in
3027 the comment above, we create a new envelope. */
3028 if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
3029 || TREE_PURPOSE (class_value) == NULL_TREE
3030 || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
3032 /* See comment above for a description of envelopes. */
3033 closed_envelopes = tree_cons (NULL_TREE, class_value,
3034 closed_envelopes);
3035 IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
3036 class_value = IDENTIFIER_CLASS_VALUE (name);
3039 /* Here we try to rule out possible ambiguities.
3040 If we can't do that, keep a TREE_LIST with possibly ambiguous
3041 decls in there. */
3042 maybe_push_cache_obstack ();
3043 /* Arbitrarily choose the first function in the list. This is OK
3044 because this is only used for initial lookup; anything that
3045 actually uses the function will look it up again. */
3046 envelope_add_decl (type, OVL_CURRENT (*methods),
3047 &TREE_PURPOSE (class_value));
3048 pop_obstacks ();
3050 methods++;
3054 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3055 DERIVED_FROM_P, which calls get_base_distance. */
3056 SET_BINFO_PUSHDECLS_MARKED (binfo);
3059 /* Consolidate unique (by name) member functions. */
3061 static void
3062 dfs_compress_decls (binfo)
3063 tree binfo;
3065 tree type = BINFO_TYPE (binfo);
3066 tree method_vec = CLASSTYPE_METHOD_VEC (type);
3068 if (processing_template_decl && type != current_class_type
3069 && dependent_base_p (binfo))
3070 /* We only record types if we're a template base. */;
3071 else if (method_vec != 0)
3073 /* Farm out constructors and destructors. */
3074 tree *methods = &TREE_VEC_ELT (method_vec, 2);
3075 tree *end = TREE_VEC_END (method_vec);
3077 for (; methods != end; methods++)
3079 /* This is known to be an envelope of the kind described before
3080 dfs_pushdecls. */
3081 tree class_value =
3082 IDENTIFIER_CLASS_VALUE (DECL_NAME (OVL_CURRENT (*methods)));
3083 tree tmp = TREE_PURPOSE (class_value);
3085 /* This was replaced in scope by somebody else. Just leave it
3086 alone. */
3087 if (TREE_CODE (tmp) != TREE_LIST)
3088 continue;
3090 if (TREE_CHAIN (tmp) == NULL_TREE
3091 && TREE_VALUE (tmp)
3092 && OVL_NEXT (TREE_VALUE (tmp)) == NULL_TREE)
3094 TREE_PURPOSE (class_value) = TREE_VALUE (tmp);
3098 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
3101 /* When entering the scope of a class, we cache all of the
3102 fields that that class provides within its inheritance
3103 lattice. Where ambiguities result, we mark them
3104 with `error_mark_node' so that if they are encountered
3105 without explicit qualification, we can emit an error
3106 message. */
3108 void
3109 push_class_decls (type)
3110 tree type;
3112 struct obstack *ambient_obstack = current_obstack;
3113 search_stack = push_search_level (search_stack, &search_obstack);
3115 /* Push class fields into CLASS_VALUE scope, and mark. */
3116 dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarked_pushdecls_p);
3118 /* Compress fields which have only a single entry
3119 by a given name, and unmark. */
3120 dfs_walk (TYPE_BINFO (type), dfs_compress_decls, marked_pushdecls_p);
3122 /* Open up all the closed envelopes and push the contained decls into
3123 class scope. */
3124 while (closed_envelopes)
3126 tree new = TREE_PURPOSE (closed_envelopes);
3127 tree id;
3129 /* This is messy because the class value may be a *_DECL, or a
3130 TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
3131 *_DECLs. The name is stored at different places in these three
3132 cases. */
3133 if (TREE_CODE (new) == TREE_LIST)
3135 if (TREE_PURPOSE (new) != NULL_TREE)
3136 id = TREE_PURPOSE (new);
3137 else
3139 tree node = TREE_VALUE (new);
3141 if (TREE_CODE (node) == TYPE_DECL
3142 && DECL_ARTIFICIAL (node)
3143 && IS_AGGR_TYPE (TREE_TYPE (node))
3144 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (node)))
3146 tree t = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (node));
3147 tree n = new;
3149 for (; n; n = TREE_CHAIN (n))
3151 tree d = TREE_VALUE (n);
3152 if (TREE_CODE (d) == TYPE_DECL
3153 && DECL_ARTIFICIAL (node)
3154 && IS_AGGR_TYPE (TREE_TYPE (d))
3155 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d))
3156 && CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d)) == t)
3157 /* OK */;
3158 else
3159 break;
3162 if (n == NULL_TREE)
3163 new = t;
3165 else while (TREE_CODE (node) == TREE_LIST)
3166 node = TREE_VALUE (node);
3167 id = DECL_NAME (node);
3170 else
3171 id = DECL_NAME (new);
3173 /* Install the original class value in order to make
3174 pushdecl_class_level work correctly. */
3175 IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes);
3176 if (TREE_CODE (new) == TREE_LIST)
3177 push_class_level_binding (id, new);
3178 else
3179 pushdecl_class_level (new);
3180 closed_envelopes = TREE_CHAIN (closed_envelopes);
3182 current_obstack = ambient_obstack;
3185 /* Here's a subroutine we need because C lacks lambdas. */
3187 static void
3188 dfs_unuse_fields (binfo)
3189 tree binfo;
3191 tree type = TREE_TYPE (binfo);
3192 tree fields;
3194 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3196 if (TREE_CODE (fields) != FIELD_DECL)
3197 continue;
3199 TREE_USED (fields) = 0;
3200 if (DECL_NAME (fields) == NULL_TREE
3201 && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3202 unuse_fields (TREE_TYPE (fields));
3206 void
3207 unuse_fields (type)
3208 tree type;
3210 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp);
3213 void
3214 pop_class_decls ()
3216 /* We haven't pushed a search level when dealing with cached classes,
3217 so we'd better not try to pop it. */
3218 if (search_stack)
3219 search_stack = pop_search_level (search_stack);
3222 void
3223 print_search_statistics ()
3225 #ifdef GATHER_STATISTICS
3226 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3227 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3228 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3229 n_outer_fields_searched, n_calls_lookup_fnfields);
3230 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
3231 #else /* GATHER_STATISTICS */
3232 fprintf (stderr, "no search statistics\n");
3233 #endif /* GATHER_STATISTICS */
3236 void
3237 init_search_processing ()
3239 gcc_obstack_init (&search_obstack);
3240 _vptr_name = get_identifier ("_vptr");
3243 void
3244 reinit_search_statistics ()
3246 #ifdef GATHER_STATISTICS
3247 n_fields_searched = 0;
3248 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3249 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3250 n_calls_get_base_type = 0;
3251 n_outer_fields_searched = 0;
3252 n_contexts_saved = 0;
3253 #endif /* GATHER_STATISTICS */
3256 #define scratch_tree_cons expr_tree_cons
3258 static tree conversions;
3259 static void
3260 add_conversions (binfo)
3261 tree binfo;
3263 int i;
3264 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
3266 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
3268 tree tmp = TREE_VEC_ELT (method_vec, i);
3269 if (! IDENTIFIER_TYPENAME_P (DECL_NAME (OVL_CURRENT (tmp))))
3270 break;
3271 conversions = scratch_tree_cons (binfo, tmp, conversions);
3273 SET_BINFO_MARKED (binfo);
3276 tree
3277 lookup_conversions (type)
3278 tree type;
3280 conversions = NULL_TREE;
3281 if (TYPE_SIZE (type))
3283 dfs_walk (TYPE_BINFO (type), add_conversions, unmarkedp);
3284 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp);
3286 return conversions;
3289 /* Subroutine of get_template_base. */
3291 static tree
3292 get_template_base_recursive (binfo, rval, template, via_virtual)
3293 tree binfo, template, rval;
3294 int via_virtual;
3296 tree binfos;
3297 int i, n_baselinks;
3298 tree type = BINFO_TYPE (binfo);
3300 if (CLASSTYPE_TEMPLATE_INFO (type)
3301 && CLASSTYPE_TI_TEMPLATE (type) == template)
3303 if (rval == NULL_TREE || rval == type)
3304 return type;
3305 else
3306 return error_mark_node;
3309 binfos = BINFO_BASETYPES (binfo);
3310 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3312 /* Process base types. */
3313 for (i = 0; i < n_baselinks; i++)
3315 tree base_binfo = TREE_VEC_ELT (binfos, i);
3317 /* Find any specific instance of a virtual base, when searching with
3318 a binfo... */
3319 if (BINFO_MARKED (base_binfo) == 0)
3321 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
3323 /* When searching for a non-virtual, we cannot mark
3324 virtually found binfos. */
3325 if (! this_virtual)
3326 SET_BINFO_MARKED (base_binfo);
3328 rval = get_template_base_recursive
3329 (base_binfo, rval, template, this_virtual);
3330 if (rval == error_mark_node)
3331 return rval;
3335 return rval;
3338 /* Given a class template TEMPLATE and a class type or binfo node BINFO,
3339 find the unique base type in BINFO that is an instance of TEMPLATE.
3340 If there are more than one, return error_mark_node. Used by unify. */
3342 tree
3343 get_template_base (template, binfo)
3344 register tree template, binfo;
3346 tree type = NULL_TREE, rval;
3348 if (TREE_CODE (binfo) == TREE_VEC)
3349 type = BINFO_TYPE (binfo);
3350 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
3352 type = complete_type (binfo);
3353 binfo = TYPE_BINFO (type);
3355 else
3356 my_friendly_abort (92);
3358 if (CLASSTYPE_TEMPLATE_INFO (type)
3359 && CLASSTYPE_TI_TEMPLATE (type) == template)
3360 return type;
3362 rval = get_template_base_recursive (binfo, NULL_TREE, template, 0);
3363 dfs_walk (binfo, dfs_unmark, markedp);
3365 return rval;
3368 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3369 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3371 static tree compare_type;
3372 static int found_overlap;
3373 static void
3374 dfs_check_overlap (empty_binfo)
3375 tree empty_binfo;
3377 tree binfo;
3378 for (binfo = TYPE_BINFO (compare_type); ; binfo = BINFO_BASETYPE (binfo, 0))
3380 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
3382 found_overlap = 1;
3383 break;
3385 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
3386 break;
3390 /* Trivial function to stop base traversal when we find something. */
3392 static int
3393 dfs_no_overlap_yet (t)
3394 tree t ATTRIBUTE_UNUSED;
3396 return found_overlap == 0;
3399 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3400 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3403 types_overlap_p (empty_type, next_type)
3404 tree empty_type, next_type;
3406 if (! IS_AGGR_TYPE (next_type))
3407 return 0;
3408 compare_type = next_type;
3409 found_overlap = 0;
3410 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap, dfs_no_overlap_yet);
3411 return found_overlap;
3414 /* Passed to dfs_search by binfo_for_vtable; determine if bvtable comes
3415 from BINFO. */
3417 static tree bvtable;
3418 static tree
3419 dfs_bfv_helper (binfo)
3420 tree binfo;
3422 if (BINFO_VTABLE (binfo) == bvtable)
3423 return binfo;
3424 return NULL_TREE;
3427 /* Given a vtable VARS, determine which binfo it comes from. */
3429 tree
3430 binfo_for_vtable (vars)
3431 tree vars;
3433 bvtable = vars;
3434 return dfs_search (TYPE_BINFO (DECL_CONTEXT (vars)), dfs_bfv_helper,
3435 DECL_CONTEXT (vars));