78fb5a44e82adb257e1a4e7ee58ed9156ffec36a
[gcc.git] / gcc / cp / search.c
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 /* High-level class interface. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "obstack.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "output.h"
34 #include "toplev.h"
35
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
38
39 extern struct obstack *current_obstack;
40
41 #include "stack.h"
42
43 /* Obstack used for remembering decision points of breadth-first. */
44
45 static struct obstack search_obstack;
46
47 /* Methods for pushing and popping objects to and from obstacks. */
48
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;
54 {
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;
63 }
64
65 struct stack_level *
66 pop_stack_level (stack)
67 struct stack_level *stack;
68 {
69 struct stack_level *tem = stack;
70 struct obstack *obstack = tem->obstack;
71 stack = tem->prev;
72 obstack_free (obstack, tem);
73 return stack;
74 }
75
76 #define search_level stack_level
77 static struct search_level *search_stack;
78
79 struct vbase_info
80 {
81 /* The class dominating the hierarchy. */
82 tree type;
83 /* A pointer to a complete object of the indicated TYPE. */
84 tree decl_ptr;
85 tree inits;
86 };
87
88 static tree next_baselink PARAMS ((tree));
89 static tree get_vbase_1 PARAMS ((tree, tree, unsigned int *));
90 static tree lookup_field_1 PARAMS ((tree, tree));
91 static int lookup_fnfields_here PARAMS ((tree, tree));
92 static int is_subobject_of_p PARAMS ((tree, tree));
93 static int hides PARAMS ((tree, tree));
94 static tree virtual_context PARAMS ((tree, tree, tree));
95 static tree dfs_check_overlap PARAMS ((tree, void *));
96 static tree dfs_no_overlap_yet PARAMS ((tree, void *));
97 static int get_base_distance_recursive
98 PARAMS ((tree, int, int, int, int *, tree *, tree,
99 int, int *, int, int));
100 static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
101 static void expand_upcast_fixups
102 PARAMS ((tree, tree, tree, tree, tree, tree, tree *));
103 static void fixup_virtual_upcast_offsets
104 PARAMS ((tree, tree, int, int, tree, tree, tree, tree,
105 tree *));
106 static tree marked_pushdecls_p PARAMS ((tree, void *));
107 static tree unmarked_pushdecls_p PARAMS ((tree, void *));
108 static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
109 static tree dfs_debug_mark PARAMS ((tree, void *));
110 static tree dfs_init_vbase_pointers PARAMS ((tree, void *));
111 static tree dfs_get_vbase_types PARAMS ((tree, void *));
112 static tree dfs_push_type_decls PARAMS ((tree, void *));
113 static tree dfs_push_decls PARAMS ((tree, void *));
114 static tree dfs_unuse_fields PARAMS ((tree, void *));
115 static tree add_conversions PARAMS ((tree, void *));
116 static tree get_virtuals_named_this PARAMS ((tree, tree));
117 static tree get_virtual_destructor PARAMS ((tree, void *));
118 static tree tree_has_any_destructor_p PARAMS ((tree, void *));
119 static int covariant_return_p PARAMS ((tree, tree));
120 static int check_final_overrider PARAMS ((tree, tree));
121 static struct search_level *push_search_level
122 PARAMS ((struct stack_level *, struct obstack *));
123 static struct search_level *pop_search_level
124 PARAMS ((struct stack_level *));
125 static tree bfs_walk
126 PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
127 void *));
128 static tree lookup_field_queue_p PARAMS ((tree, void *));
129 static tree lookup_field_r PARAMS ((tree, void *));
130 static tree get_virtuals_named_this_r PARAMS ((tree, void *));
131 static tree context_for_name_lookup PARAMS ((tree));
132 static tree canonical_binfo PARAMS ((tree));
133 static tree shared_marked_p PARAMS ((tree, void *));
134 static tree shared_unmarked_p PARAMS ((tree, void *));
135 static int dependent_base_p PARAMS ((tree));
136 static tree dfs_accessible_queue_p PARAMS ((tree, void *));
137 static tree dfs_accessible_p PARAMS ((tree, void *));
138 static tree dfs_access_in_type PARAMS ((tree, void *));
139 static access_kind access_in_type PARAMS ((tree, tree));
140 static tree dfs_canonical_queue PARAMS ((tree, void *));
141 static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
142 static void assert_canonical_unmarked PARAMS ((tree));
143 static int protected_accessible_p PARAMS ((tree, tree, tree));
144 static int friend_accessible_p PARAMS ((tree, tree, tree));
145 static void setup_class_bindings PARAMS ((tree, int));
146 static int template_self_reference_p PARAMS ((tree, tree));
147 static tree get_shared_vbase_if_not_primary PARAMS ((tree, void *));
148 static tree dfs_find_vbase_instance PARAMS ((tree, void *));
149 static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
150 static tree dfs_build_inheritance_graph_order PARAMS ((tree, void *));
151 static tree dfs_vtable_path_unmark PARAMS ((tree, void *));
152
153 /* Allocate a level of searching. */
154
155 static struct search_level *
156 push_search_level (stack, obstack)
157 struct stack_level *stack;
158 struct obstack *obstack;
159 {
160 struct search_level tem;
161
162 tem.prev = stack;
163 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
164 }
165
166 /* Discard a level of search allocation. */
167
168 static struct search_level *
169 pop_search_level (obstack)
170 struct stack_level *obstack;
171 {
172 register struct search_level *stack = pop_stack_level (obstack);
173
174 return stack;
175 }
176 \f
177 /* Variables for gathering statistics. */
178 #ifdef GATHER_STATISTICS
179 static int n_fields_searched;
180 static int n_calls_lookup_field, n_calls_lookup_field_1;
181 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
182 static int n_calls_get_base_type;
183 static int n_outer_fields_searched;
184 static int n_contexts_saved;
185 #endif /* GATHER_STATISTICS */
186
187 \f
188 /* Get a virtual binfo that is found inside BINFO's hierarchy that is
189 the same type as the type given in PARENT. To be optimal, we want
190 the first one that is found by going through the least number of
191 virtual bases.
192
193 This uses a clever algorithm that updates *depth when we find the vbase,
194 and cuts off other paths of search when they reach that depth. */
195
196 static tree
197 get_vbase_1 (parent, binfo, depth)
198 tree parent, binfo;
199 unsigned int *depth;
200 {
201 tree binfos;
202 int i, n_baselinks;
203 tree rval = NULL_TREE;
204
205 if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo))
206 {
207 *depth = 0;
208 return binfo;
209 }
210
211 *depth = *depth - 1;
212
213 binfos = BINFO_BASETYPES (binfo);
214 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
215
216 /* Process base types. */
217 for (i = 0; i < n_baselinks; i++)
218 {
219 tree base_binfo = TREE_VEC_ELT (binfos, i);
220 tree nrval;
221
222 if (*depth == 0)
223 break;
224
225 nrval = get_vbase_1 (parent, base_binfo, depth);
226 if (nrval)
227 rval = nrval;
228 }
229 *depth = *depth+1;
230 return rval;
231 }
232
233 /* Return the shortest path to vbase PARENT within BINFO, ignoring
234 access and ambiguity. */
235
236 tree
237 get_vbase (parent, binfo)
238 tree parent;
239 tree binfo;
240 {
241 unsigned int d = (unsigned int)-1;
242 return get_vbase_1 (parent, binfo, &d);
243 }
244
245 /* Convert EXPR to a virtual base class of type TYPE. We know that
246 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
247 the type of what expr points to has a virtual base of type TYPE. */
248
249 tree
250 convert_pointer_to_vbase (type, expr)
251 tree type;
252 tree expr;
253 {
254 tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))));
255 return convert_pointer_to_real (vb, expr);
256 }
257
258 /* Check whether the type given in BINFO is derived from PARENT. If
259 it isn't, return 0. If it is, but the derivation is MI-ambiguous
260 AND protect != 0, emit an error message and return error_mark_node.
261
262 Otherwise, if TYPE is derived from PARENT, return the actual base
263 information, unless a one of the protection violations below
264 occurs, in which case emit an error message and return error_mark_node.
265
266 If PROTECT is 1, then check if access to a public field of PARENT
267 would be private. Also check for ambiguity. */
268
269 tree
270 get_binfo (parent, binfo, protect)
271 register tree parent, binfo;
272 int protect;
273 {
274 tree type = NULL_TREE;
275 int dist;
276 tree rval = NULL_TREE;
277
278 if (TREE_CODE (parent) == TREE_VEC)
279 parent = BINFO_TYPE (parent);
280 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
281 my_friendly_abort (89);
282
283 if (TREE_CODE (binfo) == TREE_VEC)
284 type = BINFO_TYPE (binfo);
285 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
286 type = binfo;
287 else
288 my_friendly_abort (90);
289
290 dist = get_base_distance (parent, binfo, protect, &rval);
291
292 if (dist == -3)
293 {
294 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
295 parent, type);
296 return error_mark_node;
297 }
298 else if (dist == -2 && protect)
299 {
300 cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
301 type);
302 return error_mark_node;
303 }
304
305 return rval;
306 }
307
308 /* This is the newer depth first get_base_distance routine. */
309
310 static int
311 get_base_distance_recursive (binfo, depth, is_private, rval,
312 rval_private_ptr, new_binfo_ptr, parent,
313 protect, via_virtual_ptr, via_virtual,
314 current_scope_in_chain)
315 tree binfo;
316 int depth, is_private, rval;
317 int *rval_private_ptr;
318 tree *new_binfo_ptr, parent;
319 int protect, *via_virtual_ptr, via_virtual;
320 int current_scope_in_chain;
321 {
322 tree binfos;
323 int i, n_baselinks;
324
325 if (protect == 1
326 && !current_scope_in_chain
327 && is_friend (BINFO_TYPE (binfo), current_scope ()))
328 current_scope_in_chain = 1;
329
330 if (BINFO_TYPE (binfo) == parent || binfo == parent)
331 {
332 int better = 0;
333
334 if (rval == -1)
335 /* This is the first time we've found parent. */
336 better = 1;
337 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
338 BINFO_OFFSET (binfo))
339 && *via_virtual_ptr && via_virtual)
340 {
341 /* A new path to the same vbase. If this one has better
342 access or is shorter, take it. */
343
344 if (protect)
345 better = *rval_private_ptr - is_private;
346 if (better == 0)
347 better = rval - depth;
348 }
349 else
350 {
351 /* Ambiguous base class. */
352 rval = depth = -2;
353
354 /* If we get an ambiguity between virtual and non-virtual base
355 class, return the non-virtual in case we are ignoring
356 ambiguity. */
357 better = *via_virtual_ptr - via_virtual;
358 }
359
360 if (better > 0)
361 {
362 rval = depth;
363 *rval_private_ptr = is_private;
364 *new_binfo_ptr = binfo;
365 *via_virtual_ptr = via_virtual;
366 }
367
368 return rval;
369 }
370
371 binfos = BINFO_BASETYPES (binfo);
372 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
373 depth += 1;
374
375 /* Process base types. */
376 for (i = 0; i < n_baselinks; i++)
377 {
378 tree base_binfo = TREE_VEC_ELT (binfos, i);
379
380 int via_private
381 = ((protect == 1
382 && (is_private
383 || (!TREE_VIA_PUBLIC (base_binfo)
384 && !(TREE_VIA_PROTECTED (base_binfo)
385 && current_scope_in_chain)
386 && !is_friend (BINFO_TYPE (binfo), current_scope ()))))
387 || (protect > 1
388 && (is_private || !TREE_VIA_PUBLIC (base_binfo))));
389
390 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
391
392 rval = get_base_distance_recursive (base_binfo, depth, via_private,
393 rval, rval_private_ptr,
394 new_binfo_ptr, parent,
395 protect, via_virtual_ptr,
396 this_virtual,
397 current_scope_in_chain);
398
399 /* If we've found a non-virtual, ambiguous base class, we don't need
400 to keep searching. */
401 if (rval == -2 && *via_virtual_ptr == 0)
402 return rval;
403 }
404
405 return rval;
406 }
407
408 /* Return the number of levels between type PARENT and the type given
409 in BINFO, following the leftmost path to PARENT not found along a
410 virtual path, if there are no real PARENTs (all come from virtual
411 base classes), then follow the shortest public path to PARENT.
412
413 Return -1 if TYPE is not derived from PARENT.
414 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
415 non-negative.
416 Return -3 if PARENT is not accessible in TYPE, and PROTECT is non-zero.
417
418 If PATH_PTR is non-NULL, then also build the list of types
419 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
420 set.
421
422 If PROTECT is greater than 1, ignore any special access the current
423 scope might have when determining whether PARENT is inaccessible.
424
425 PARENT can also be a binfo, in which case that exact parent is found
426 and no other. convert_pointer_to_real uses this functionality.
427
428 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
429
430 int
431 get_base_distance (parent, binfo, protect, path_ptr)
432 register tree parent, binfo;
433 int protect;
434 tree *path_ptr;
435 {
436 int rval;
437 int rval_private = 0;
438 tree type = NULL_TREE;
439 tree new_binfo = NULL_TREE;
440 int via_virtual;
441 int watch_access = protect;
442
443 /* Should we be completing types here? */
444 if (TREE_CODE (parent) != TREE_VEC)
445 parent = complete_type (TYPE_MAIN_VARIANT (parent));
446 else
447 complete_type (TREE_TYPE (parent));
448
449 if (TREE_CODE (binfo) == TREE_VEC)
450 type = BINFO_TYPE (binfo);
451 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
452 {
453 type = complete_type (binfo);
454 binfo = TYPE_BINFO (type);
455
456 if (path_ptr)
457 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE,
458 980827);
459 }
460 else
461 my_friendly_abort (92);
462
463 if (parent == type || parent == binfo)
464 {
465 /* If the distance is 0, then we don't really need
466 a path pointer, but we shouldn't let garbage go back. */
467 if (path_ptr)
468 *path_ptr = binfo;
469 return 0;
470 }
471
472 if (path_ptr && watch_access == 0)
473 watch_access = 1;
474
475 rval = get_base_distance_recursive (binfo, 0, 0, -1,
476 &rval_private, &new_binfo, parent,
477 watch_access, &via_virtual, 0,
478 0);
479
480 /* Access restrictions don't count if we found an ambiguous basetype. */
481 if (rval == -2 && protect >= 0)
482 rval_private = 0;
483
484 if (rval && protect && rval_private)
485 return -3;
486
487 /* If they gave us the real vbase binfo, which isn't in the main binfo
488 tree, deal with it. This happens when we are called from
489 expand_upcast_fixups. */
490 if (rval == -1 && TREE_CODE (parent) == TREE_VEC
491 && parent == binfo_for_vbase (BINFO_TYPE (parent), type))
492 {
493 new_binfo = parent;
494 rval = 1;
495 }
496
497 if (path_ptr)
498 *path_ptr = new_binfo;
499 return rval;
500 }
501
502 /* Worker function for get_dynamic_cast_base_type. */
503
504 static int
505 dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
506 tree subtype;
507 tree binfo;
508 int via_virtual;
509 tree *offset_ptr;
510 {
511 tree binfos;
512 int i, n_baselinks;
513 int worst = -2;
514
515 if (BINFO_TYPE (binfo) == subtype)
516 {
517 if (via_virtual)
518 return -1;
519 else
520 {
521 *offset_ptr = BINFO_OFFSET (binfo);
522 return 0;
523 }
524 }
525
526 binfos = BINFO_BASETYPES (binfo);
527 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
528 for (i = 0; i < n_baselinks; i++)
529 {
530 tree base_binfo = TREE_VEC_ELT (binfos, i);
531 int rval;
532
533 if (!TREE_VIA_PUBLIC (base_binfo))
534 continue;
535 rval = dynamic_cast_base_recurse
536 (subtype, base_binfo,
537 via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
538 if (worst == -2)
539 worst = rval;
540 else if (rval >= 0)
541 worst = worst >= 0 ? -3 : worst;
542 else if (rval == -1)
543 worst = -1;
544 else if (rval == -3 && worst != -1)
545 worst = -3;
546 }
547 return worst;
548 }
549
550 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
551 started from is related to the required TARGET type, in order to optimize
552 the inheritance graph search. This information is independant of the
553 current context, and ignores private paths, hence get_base_distance is
554 inappropriate. Return a TREE specifying the base offset, BOFF.
555 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
556 and there are no public virtual SUBTYPE bases.
557 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
558 BOFF == -2, SUBTYPE is not a public base.
559 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
560
561 tree
562 get_dynamic_cast_base_type (subtype, target)
563 tree subtype;
564 tree target;
565 {
566 tree offset = NULL_TREE;
567 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
568 0, &offset);
569
570 if (!boff)
571 return offset;
572 return build_int_2 (boff, -1);
573 }
574
575 /* Search for a member with name NAME in a multiple inheritance lattice
576 specified by TYPE. If it does not exist, return NULL_TREE.
577 If the member is ambiguously referenced, return `error_mark_node'.
578 Otherwise, return the FIELD_DECL. */
579
580 /* Do a 1-level search for NAME as a member of TYPE. The caller must
581 figure out whether it can access this field. (Since it is only one
582 level, this is reasonable.) */
583
584 static tree
585 lookup_field_1 (type, name)
586 tree type, name;
587 {
588 register tree field;
589
590 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
591 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
592 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
593 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
594 the code often worked even when we treated the index as a list
595 of fields!) */
596 return NULL_TREE;
597
598 if (TYPE_NAME (type)
599 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
600 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
601 {
602 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
603 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
604 int i;
605
606 while (lo < hi)
607 {
608 i = (lo + hi) / 2;
609
610 #ifdef GATHER_STATISTICS
611 n_fields_searched++;
612 #endif /* GATHER_STATISTICS */
613
614 if (DECL_NAME (fields[i]) > name)
615 hi = i;
616 else if (DECL_NAME (fields[i]) < name)
617 lo = i + 1;
618 else
619 {
620 /* We might have a nested class and a field with the
621 same name; we sorted them appropriately via
622 field_decl_cmp, so just look for the last field with
623 this name. */
624 while (i + 1 < hi
625 && DECL_NAME (fields[i+1]) == name)
626 ++i;
627 return fields[i];
628 }
629 }
630 return NULL_TREE;
631 }
632
633 field = TYPE_FIELDS (type);
634
635 #ifdef GATHER_STATISTICS
636 n_calls_lookup_field_1++;
637 #endif /* GATHER_STATISTICS */
638 while (field)
639 {
640 #ifdef GATHER_STATISTICS
641 n_fields_searched++;
642 #endif /* GATHER_STATISTICS */
643 my_friendly_assert (DECL_P (field), 0);
644 if (DECL_NAME (field) == NULL_TREE
645 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
646 {
647 tree temp = lookup_field_1 (TREE_TYPE (field), name);
648 if (temp)
649 return temp;
650 }
651 if (TREE_CODE (field) == USING_DECL)
652 /* For now, we're just treating member using declarations as
653 old ARM-style access declarations. Thus, there's no reason
654 to return a USING_DECL, and the rest of the compiler can't
655 handle it. Once the class is defined, these are purged
656 from TYPE_FIELDS anyhow; see handle_using_decl. */
657 ;
658 else if (DECL_NAME (field) == name)
659 {
660 if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
661 && DECL_ASSEMBLER_NAME (field) != NULL)
662 GNU_xref_ref(current_function_decl,
663 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
664 return field;
665 }
666 field = TREE_CHAIN (field);
667 }
668 /* Not found. */
669 if (name == vptr_identifier)
670 {
671 /* Give the user what s/he thinks s/he wants. */
672 if (TYPE_POLYMORPHIC_P (type))
673 return TYPE_VFIELD (type);
674 }
675 return NULL_TREE;
676 }
677
678 /* There are a number of cases we need to be aware of here:
679 current_class_type current_function_decl
680 global NULL NULL
681 fn-local NULL SET
682 class-local SET NULL
683 class->fn SET SET
684 fn->class SET SET
685
686 Those last two make life interesting. If we're in a function which is
687 itself inside a class, we need decls to go into the fn's decls (our
688 second case below). But if we're in a class and the class itself is
689 inside a function, we need decls to go into the decls for the class. To
690 achieve this last goal, we must see if, when both current_class_ptr and
691 current_function_decl are set, the class was declared inside that
692 function. If so, we know to put the decls into the class's scope. */
693
694 tree
695 current_scope ()
696 {
697 if (current_function_decl == NULL_TREE)
698 return current_class_type;
699 if (current_class_type == NULL_TREE)
700 return current_function_decl;
701 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
702 && same_type_p (DECL_CONTEXT (current_function_decl),
703 current_class_type))
704 || (DECL_FRIEND_CONTEXT (current_function_decl)
705 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
706 current_class_type)))
707 return current_function_decl;
708
709 return current_class_type;
710 }
711
712 /* Returns non-zero if we are currently in a function scope. Note
713 that this function returns zero if we are within a local class, but
714 not within a member function body of the local class. */
715
716 int
717 at_function_scope_p ()
718 {
719 tree cs = current_scope ();
720 return cs && TREE_CODE (cs) == FUNCTION_DECL;
721 }
722
723 /* Return the scope of DECL, as appropriate when doing name-lookup. */
724
725 static tree
726 context_for_name_lookup (decl)
727 tree decl;
728 {
729 /* [class.union]
730
731 For the purposes of name lookup, after the anonymous union
732 definition, the members of the anonymous union are considered to
733 have been defined in the scope in which the anonymous union is
734 declared. */
735 tree context = CP_DECL_CONTEXT (decl);
736
737 while (TYPE_P (context) && ANON_AGGR_TYPE_P (context))
738 context = TYPE_CONTEXT (context);
739 if (!context)
740 context = global_namespace;
741
742 return context;
743 }
744
745 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
746 otherwise. */
747
748 static tree
749 canonical_binfo (binfo)
750 tree binfo;
751 {
752 return (TREE_VIA_VIRTUAL (binfo)
753 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
754 }
755
756 /* A queue function that simply ensures that we walk into the
757 canonical versions of virtual bases. */
758
759 static tree
760 dfs_canonical_queue (binfo, data)
761 tree binfo;
762 void *data ATTRIBUTE_UNUSED;
763 {
764 return canonical_binfo (binfo);
765 }
766
767 /* Called via dfs_walk from assert_canonical_unmarked. */
768
769 static tree
770 dfs_assert_unmarked_p (binfo, data)
771 tree binfo;
772 void *data ATTRIBUTE_UNUSED;
773 {
774 my_friendly_assert (!BINFO_MARKED (binfo), 0);
775 return NULL_TREE;
776 }
777
778 /* Asserts that all the nodes below BINFO (using the canonical
779 versions of virtual bases) are unmarked. */
780
781 static void
782 assert_canonical_unmarked (binfo)
783 tree binfo;
784 {
785 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
786 }
787
788 /* If BINFO is marked, return a canonical version of BINFO.
789 Otherwise, return NULL_TREE. */
790
791 static tree
792 shared_marked_p (binfo, data)
793 tree binfo;
794 void *data;
795 {
796 binfo = canonical_binfo (binfo);
797 return markedp (binfo, data);
798 }
799
800 /* If BINFO is not marked, return a canonical version of BINFO.
801 Otherwise, return NULL_TREE. */
802
803 static tree
804 shared_unmarked_p (binfo, data)
805 tree binfo;
806 void *data;
807 {
808 binfo = canonical_binfo (binfo);
809 return unmarkedp (binfo, data);
810 }
811
812 /* The accessibility routines use BINFO_ACCESS for scratch space
813 during the computation of the accssibility of some declaration. */
814
815 #define BINFO_ACCESS(NODE) \
816 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
817
818 /* Set the access associated with NODE to ACCESS. */
819
820 #define SET_BINFO_ACCESS(NODE, ACCESS) \
821 ((TREE_LANG_FLAG_1 (NODE) = (ACCESS & 2) != 0), \
822 (TREE_LANG_FLAG_6 (NODE) = (ACCESS & 1) != 0))
823
824 /* Called from access_in_type via dfs_walk. Calculate the access to
825 DATA (which is really a DECL) in BINFO. */
826
827 static tree
828 dfs_access_in_type (binfo, data)
829 tree binfo;
830 void *data;
831 {
832 tree decl = (tree) data;
833 tree type = BINFO_TYPE (binfo);
834 access_kind access = ak_none;
835
836 if (context_for_name_lookup (decl) == type)
837 {
838 /* If we have desceneded to the scope of DECL, just note the
839 appropriate access. */
840 if (TREE_PRIVATE (decl))
841 access = ak_private;
842 else if (TREE_PROTECTED (decl))
843 access = ak_protected;
844 else
845 access = ak_public;
846 }
847 else
848 {
849 /* First, check for an access-declaration that gives us more
850 access to the DECL. The CONST_DECL for an enumeration
851 constant will not have DECL_LANG_SPECIFIC, and thus no
852 DECL_ACCESS. */
853 if (DECL_LANG_SPECIFIC (decl))
854 {
855 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
856 if (decl_access)
857 access = ((access_kind)
858 TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
859 }
860
861 if (!access)
862 {
863 int i;
864 int n_baselinks;
865 tree binfos;
866
867 /* Otherwise, scan our baseclasses, and pick the most favorable
868 access. */
869 binfos = BINFO_BASETYPES (binfo);
870 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
871 for (i = 0; i < n_baselinks; ++i)
872 {
873 tree base_binfo = TREE_VEC_ELT (binfos, i);
874 access_kind base_access
875 = BINFO_ACCESS (canonical_binfo (base_binfo));
876
877 if (base_access == ak_none || base_access == ak_private)
878 /* If it was not accessible in the base, or only
879 accessible as a private member, we can't access it
880 all. */
881 base_access = ak_none;
882 else if (TREE_VIA_PROTECTED (base_binfo))
883 /* Public and protected members in the base are
884 protected here. */
885 base_access = ak_protected;
886 else if (!TREE_VIA_PUBLIC (base_binfo))
887 /* Public and protected members in the base are
888 private here. */
889 base_access = ak_private;
890
891 /* See if the new access, via this base, gives more
892 access than our previous best access. */
893 if (base_access != ak_none
894 && (base_access == ak_public
895 || (base_access == ak_protected
896 && access != ak_public)
897 || (base_access == ak_private
898 && access == ak_none)))
899 {
900 access = base_access;
901
902 /* If the new access is public, we can't do better. */
903 if (access == ak_public)
904 break;
905 }
906 }
907 }
908 }
909
910 /* Note the access to DECL in TYPE. */
911 SET_BINFO_ACCESS (binfo, access);
912
913 /* Mark TYPE as visited so that if we reach it again we do not
914 duplicate our efforts here. */
915 SET_BINFO_MARKED (binfo);
916
917 return NULL_TREE;
918 }
919
920 /* Return the access to DECL in TYPE. */
921
922 static access_kind
923 access_in_type (type, decl)
924 tree type;
925 tree decl;
926 {
927 tree binfo = TYPE_BINFO (type);
928
929 /* We must take into account
930
931 [class.paths]
932
933 If a name can be reached by several paths through a multiple
934 inheritance graph, the access is that of the path that gives
935 most access.
936
937 The algorithm we use is to make a post-order depth-first traversal
938 of the base-class hierarchy. As we come up the tree, we annotate
939 each node with the most lenient access. */
940 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
941 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
942 assert_canonical_unmarked (binfo);
943
944 return BINFO_ACCESS (binfo);
945 }
946
947 /* Called from dfs_accessible_p via dfs_walk. */
948
949 static tree
950 dfs_accessible_queue_p (binfo, data)
951 tree binfo;
952 void *data ATTRIBUTE_UNUSED;
953 {
954 if (BINFO_MARKED (binfo))
955 return NULL_TREE;
956
957 /* If this class is inherited via private or protected inheritance,
958 then we can't see it, unless we are a friend of the subclass. */
959 if (!TREE_VIA_PUBLIC (binfo)
960 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
961 current_scope ()))
962 return NULL_TREE;
963
964 return canonical_binfo (binfo);
965 }
966
967 /* Called from dfs_accessible_p via dfs_walk. */
968
969 static tree
970 dfs_accessible_p (binfo, data)
971 tree binfo;
972 void *data;
973 {
974 int protected_ok = data != 0;
975 access_kind access;
976
977 SET_BINFO_MARKED (binfo);
978 access = BINFO_ACCESS (binfo);
979 if (access == ak_public || (access == ak_protected && protected_ok))
980 return binfo;
981 else if (access != ak_none
982 && is_friend (BINFO_TYPE (binfo), current_scope ()))
983 return binfo;
984
985 return NULL_TREE;
986 }
987
988 /* Returns non-zero if it is OK to access DECL through an object
989 indiated by BINFO in the context of DERIVED. */
990
991 static int
992 protected_accessible_p (decl, derived, binfo)
993 tree decl;
994 tree derived;
995 tree binfo;
996 {
997 access_kind access;
998
999 /* We're checking this clause from [class.access.base]
1000
1001 m as a member of N is protected, and the reference occurs in a
1002 member or friend of class N, or in a member or friend of a
1003 class P derived from N, where m as a member of P is private or
1004 protected.
1005
1006 Here DERIVED is a possible P and DECL is m. accessible_p will
1007 iterate over various values of N, but the access to m in DERIVED
1008 does not change.
1009
1010 Note that I believe that the passage above is wrong, and should read
1011 "...is private or protected or public"; otherwise you get bizarre results
1012 whereby a public using-decl can prevent you from accessing a protected
1013 member of a base. (jason 2000/02/28) */
1014
1015 /* If DERIVED isn't derived from m's class, then it can't be a P. */
1016 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
1017 return 0;
1018
1019 access = access_in_type (derived, decl);
1020
1021 /* If m is inaccessible in DERIVED, then it's not a P. */
1022 if (access == ak_none)
1023 return 0;
1024
1025 /* [class.protected]
1026
1027 When a friend or a member function of a derived class references
1028 a protected nonstatic member of a base class, an access check
1029 applies in addition to those described earlier in clause
1030 _class.access_) Except when forming a pointer to member
1031 (_expr.unary.op_), the access must be through a pointer to,
1032 reference to, or object of the derived class itself (or any class
1033 derived from that class) (_expr.ref_). If the access is to form
1034 a pointer to member, the nested-name-specifier shall name the
1035 derived class (or any class derived from that class). */
1036 if (DECL_NONSTATIC_MEMBER_P (decl))
1037 {
1038 /* We can tell through what the reference is occurring by
1039 chasing BINFO up to the root. */
1040 tree t = binfo;
1041 while (BINFO_INHERITANCE_CHAIN (t))
1042 t = BINFO_INHERITANCE_CHAIN (t);
1043
1044 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
1045 return 0;
1046 }
1047
1048 return 1;
1049 }
1050
1051 /* Returns non-zero if SCOPE is a friend of a type which would be able
1052 to access DECL through the object indicated by BINFO. */
1053
1054 static int
1055 friend_accessible_p (scope, decl, binfo)
1056 tree scope;
1057 tree decl;
1058 tree binfo;
1059 {
1060 tree befriending_classes;
1061 tree t;
1062
1063 if (!scope)
1064 return 0;
1065
1066 if (TREE_CODE (scope) == FUNCTION_DECL
1067 || DECL_FUNCTION_TEMPLATE_P (scope))
1068 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
1069 else if (TYPE_P (scope))
1070 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
1071 else
1072 return 0;
1073
1074 for (t = befriending_classes; t; t = TREE_CHAIN (t))
1075 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
1076 return 1;
1077
1078 /* Nested classes are implicitly friends of their enclosing types, as
1079 per core issue 45 (this is a change from the standard). */
1080 if (TYPE_P (scope))
1081 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
1082 if (protected_accessible_p (decl, t, binfo))
1083 return 1;
1084
1085 if (TREE_CODE (scope) == FUNCTION_DECL
1086 || DECL_FUNCTION_TEMPLATE_P (scope))
1087 {
1088 /* Perhaps this SCOPE is a member of a class which is a
1089 friend. */
1090 if (DECL_CLASS_SCOPE_P (decl)
1091 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
1092 return 1;
1093
1094 /* Or an instantiation of something which is a friend. */
1095 if (DECL_TEMPLATE_INFO (scope))
1096 return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
1097 }
1098 else if (CLASSTYPE_TEMPLATE_INFO (scope))
1099 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
1100
1101 return 0;
1102 }
1103
1104 /* Perform access control on TYPE_DECL VAL, which was looked up in TYPE.
1105 This is fairly complex, so here's the design:
1106
1107 The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
1108 start to process a top-level declaration.
1109 As we process the decl-specifier-seq for the declaration, any types we
1110 see that might need access control are passed to type_access_control,
1111 which defers checking by adding them to type_lookups.
1112 When we are done with the decl-specifier-seq, we record the lookups we've
1113 seen in the lookups field of the typed_declspecs nonterminal.
1114 When we process the first declarator, either in parse_decl or
1115 begin_function_definition, we call save_type_access_control,
1116 which stores the lookups from the decl-specifier-seq in
1117 current_type_lookups.
1118 As we finish with each declarator, we process everything in type_lookups
1119 via decl_type_access_control, which resets type_lookups to the value of
1120 current_type_lookups for subsequent declarators.
1121 When we enter a function, we set type_lookups to error_mark_node, so all
1122 lookups are processed immediately. */
1123
1124 void
1125 type_access_control (type, val)
1126 tree type, val;
1127 {
1128 if (val == NULL_TREE || TREE_CODE (val) != TYPE_DECL
1129 || ! DECL_CLASS_SCOPE_P (val))
1130 return;
1131
1132 if (type_lookups == error_mark_node)
1133 enforce_access (type, val);
1134 else if (! accessible_p (type, val))
1135 type_lookups = tree_cons (type, val, type_lookups);
1136 }
1137
1138 /* DECL is a declaration from a base class of TYPE, which was the
1139 class used to name DECL. Return non-zero if, in the current
1140 context, DECL is accessible. If TYPE is actually a BINFO node,
1141 then we can tell in what context the access is occurring by looking
1142 at the most derived class along the path indicated by BINFO. */
1143
1144 int
1145 accessible_p (type, decl)
1146 tree type;
1147 tree decl;
1148
1149 {
1150 tree binfo;
1151 tree t;
1152
1153 /* Non-zero if it's OK to access DECL if it has protected
1154 accessibility in TYPE. */
1155 int protected_ok = 0;
1156
1157 /* If we're not checking access, everything is accessible. */
1158 if (!flag_access_control)
1159 return 1;
1160
1161 /* If this declaration is in a block or namespace scope, there's no
1162 access control. */
1163 if (!TYPE_P (context_for_name_lookup (decl)))
1164 return 1;
1165
1166 if (!TYPE_P (type))
1167 {
1168 binfo = type;
1169 type = BINFO_TYPE (type);
1170 }
1171 else
1172 binfo = TYPE_BINFO (type);
1173
1174 /* [class.access.base]
1175
1176 A member m is accessible when named in class N if
1177
1178 --m as a member of N is public, or
1179
1180 --m as a member of N is private, and the reference occurs in a
1181 member or friend of class N, or
1182
1183 --m as a member of N is protected, and the reference occurs in a
1184 member or friend of class N, or in a member or friend of a
1185 class P derived from N, where m as a member of P is private or
1186 protected, or
1187
1188 --there exists a base class B of N that is accessible at the point
1189 of reference, and m is accessible when named in class B.
1190
1191 We walk the base class hierarchy, checking these conditions. */
1192
1193 /* Figure out where the reference is occurring. Check to see if
1194 DECL is private or protected in this scope, since that will
1195 determine whether protected access is allowed. */
1196 if (current_class_type)
1197 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
1198
1199 /* Now, loop through the classes of which we are a friend. */
1200 if (!protected_ok)
1201 protected_ok = friend_accessible_p (current_scope (), decl, binfo);
1202
1203 /* Standardize the binfo that access_in_type will use. We don't
1204 need to know what path was chosen from this point onwards. */
1205 binfo = TYPE_BINFO (type);
1206
1207 /* Compute the accessibility of DECL in the class hierarchy
1208 dominated by type. */
1209 access_in_type (type, decl);
1210 /* Walk the hierarchy again, looking for a base class that allows
1211 access. */
1212 t = dfs_walk (binfo, dfs_accessible_p,
1213 dfs_accessible_queue_p,
1214 protected_ok ? &protected_ok : 0);
1215 /* Clear any mark bits. Note that we have to walk the whole tree
1216 here, since we have aborted the previous walk from some point
1217 deep in the tree. */
1218 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1219 assert_canonical_unmarked (binfo);
1220
1221 return t != NULL_TREE;
1222 }
1223
1224 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1225 found as a base class and sub-object of the object denoted by
1226 BINFO. This routine relies upon binfos not being shared, except
1227 for binfos for virtual bases. */
1228
1229 static int
1230 is_subobject_of_p (parent, binfo)
1231 tree parent, binfo;
1232 {
1233 tree binfos;
1234 int i, n_baselinks;
1235
1236 /* We want to canonicalize for comparison purposes. But, when we
1237 iterate through basetypes later, we want the binfos from the
1238 original hierarchy. That's why we have to calculate BINFOS
1239 first, and then canonicalize. */
1240 binfos = BINFO_BASETYPES (binfo);
1241 parent = canonical_binfo (parent);
1242 binfo = canonical_binfo (binfo);
1243
1244 if (parent == binfo)
1245 return 1;
1246
1247 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1248
1249 /* Process and/or queue base types. */
1250 for (i = 0; i < n_baselinks; i++)
1251 {
1252 tree base_binfo = TREE_VEC_ELT (binfos, i);
1253 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo)))
1254 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1255 class there's no way to descend into it. */
1256 continue;
1257
1258 if (is_subobject_of_p (parent, base_binfo))
1259 return 1;
1260 }
1261 return 0;
1262 }
1263
1264 /* See if a one FIELD_DECL hides another. This routine is meant to
1265 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1266 binfos given are the binfos corresponding to the particular places
1267 the FIELD_DECLs are found. This routine relies upon binfos not
1268 being shared, except for virtual bases. */
1269
1270 static int
1271 hides (hider_binfo, hidee_binfo)
1272 tree hider_binfo, hidee_binfo;
1273 {
1274 /* hider hides hidee, if hider has hidee as a base class and
1275 the instance of hidee is a sub-object of hider. The first
1276 part is always true is the second part is true.
1277
1278 When hider and hidee are the same (two ways to get to the exact
1279 same member) we consider either one as hiding the other. */
1280 return is_subobject_of_p (hidee_binfo, hider_binfo);
1281 }
1282
1283 /* Very similar to lookup_fnfields_1 but it ensures that at least one
1284 function was declared inside the class given by TYPE. It really should
1285 only return functions that match the given TYPE. */
1286
1287 static int
1288 lookup_fnfields_here (type, name)
1289 tree type, name;
1290 {
1291 int idx = lookup_fnfields_1 (type, name);
1292 tree fndecls;
1293
1294 /* ctors and dtors are always only in the right class. */
1295 if (idx <= 1)
1296 return idx;
1297 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1298 while (fndecls)
1299 {
1300 if (TYPE_MAIN_VARIANT (DECL_CONTEXT (OVL_CURRENT (fndecls)))
1301 == TYPE_MAIN_VARIANT (type))
1302 return idx;
1303 fndecls = OVL_CHAIN (fndecls);
1304 }
1305 return -1;
1306 }
1307
1308 struct lookup_field_info {
1309 /* The type in which we're looking. */
1310 tree type;
1311 /* The name of the field for which we're looking. */
1312 tree name;
1313 /* If non-NULL, the current result of the lookup. */
1314 tree rval;
1315 /* The path to RVAL. */
1316 tree rval_binfo;
1317 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1318 candidates. */
1319 tree ambiguous;
1320 /* If non-zero, we are looking for types, not data members. */
1321 int want_type;
1322 /* If non-zero, RVAL was found by looking through a dependent base. */
1323 int from_dep_base_p;
1324 /* If something went wrong, a message indicating what. */
1325 const char *errstr;
1326 };
1327
1328 /* Returns non-zero if BINFO is not hidden by the value found by the
1329 lookup so far. If BINFO is hidden, then there's no need to look in
1330 it. DATA is really a struct lookup_field_info. Called from
1331 lookup_field via breadth_first_search. */
1332
1333 static tree
1334 lookup_field_queue_p (binfo, data)
1335 tree binfo;
1336 void *data;
1337 {
1338 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1339
1340 /* Don't look for constructors or destructors in base classes. */
1341 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1342 return NULL_TREE;
1343
1344 /* If this base class is hidden by the best-known value so far, we
1345 don't need to look. */
1346 if (!lfi->from_dep_base_p && lfi->rval_binfo
1347 && hides (lfi->rval_binfo, binfo))
1348 return NULL_TREE;
1349
1350 return CANONICAL_BINFO (binfo, lfi->type);
1351 }
1352
1353 /* Within the scope of a template class, you can refer to the to the
1354 current specialization with the name of the template itself. For
1355 example:
1356
1357 template <typename T> struct S { S* sp; }
1358
1359 Returns non-zero if DECL is such a declaration in a class TYPE. */
1360
1361 static int
1362 template_self_reference_p (type, decl)
1363 tree type;
1364 tree decl;
1365 {
1366 return (CLASSTYPE_USE_TEMPLATE (type)
1367 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
1368 && TREE_CODE (decl) == TYPE_DECL
1369 && DECL_ARTIFICIAL (decl)
1370 && DECL_NAME (decl) == constructor_name (type));
1371 }
1372
1373 /* DATA is really a struct lookup_field_info. Look for a field with
1374 the name indicated there in BINFO. If this function returns a
1375 non-NULL value it is the result of the lookup. Called from
1376 lookup_field via breadth_first_search. */
1377
1378 static tree
1379 lookup_field_r (binfo, data)
1380 tree binfo;
1381 void *data;
1382 {
1383 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1384 tree type = BINFO_TYPE (binfo);
1385 tree nval = NULL_TREE;
1386 int from_dep_base_p;
1387
1388 /* First, look for a function. There can't be a function and a data
1389 member with the same name, and if there's a function and a type
1390 with the same name, the type is hidden by the function. */
1391 if (!lfi->want_type)
1392 {
1393 int idx = lookup_fnfields_here (type, lfi->name);
1394 if (idx >= 0)
1395 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1396 }
1397
1398 if (!nval)
1399 /* Look for a data member or type. */
1400 nval = lookup_field_1 (type, lfi->name);
1401
1402 /* If there is no declaration with the indicated name in this type,
1403 then there's nothing to do. */
1404 if (!nval)
1405 return NULL_TREE;
1406
1407 /* If we're looking up a type (as with an elaborated type specifier)
1408 we ignore all non-types we find. */
1409 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL)
1410 {
1411 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1412 if (nval)
1413 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1414 else
1415 return NULL_TREE;
1416 }
1417
1418 /* You must name a template base class with a template-id. */
1419 if (!same_type_p (type, lfi->type)
1420 && template_self_reference_p (type, nval))
1421 return NULL_TREE;
1422
1423 from_dep_base_p = dependent_base_p (binfo);
1424 if (lfi->from_dep_base_p && !from_dep_base_p)
1425 {
1426 /* If the new declaration is not found via a dependent base, and
1427 the old one was, then we must prefer the new one. We weren't
1428 really supposed to be able to find the old one, so we don't
1429 want to be affected by a specialization. Consider:
1430
1431 struct B { typedef int I; };
1432 template <typename T> struct D1 : virtual public B {};
1433 template <typename T> struct D :
1434 public D1, virtual pubic B { I i; };
1435
1436 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1437 D1 is specialized. */
1438 lfi->from_dep_base_p = 0;
1439 lfi->rval = NULL_TREE;
1440 lfi->rval_binfo = NULL_TREE;
1441 lfi->ambiguous = NULL_TREE;
1442 lfi->errstr = 0;
1443 }
1444 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1445 /* Similarly, if the old declaration was not found via a dependent
1446 base, and the new one is, ignore the new one. */
1447 return NULL_TREE;
1448
1449 /* If the lookup already found a match, and the new value doesn't
1450 hide the old one, we might have an ambiguity. */
1451 if (lfi->rval_binfo && !hides (binfo, lfi->rval_binfo))
1452 {
1453 if (nval == lfi->rval && SHARED_MEMBER_P (nval))
1454 /* The two things are really the same. */
1455 ;
1456 else if (hides (lfi->rval_binfo, binfo))
1457 /* The previous value hides the new one. */
1458 ;
1459 else
1460 {
1461 /* We have a real ambiguity. We keep a chain of all the
1462 candidates. */
1463 if (!lfi->ambiguous && lfi->rval)
1464 {
1465 /* This is the first time we noticed an ambiguity. Add
1466 what we previously thought was a reasonable candidate
1467 to the list. */
1468 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1469 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1470 }
1471
1472 /* Add the new value. */
1473 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1474 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1475 lfi->errstr = "request for member `%D' is ambiguous";
1476 }
1477 }
1478 else
1479 {
1480 /* If the thing we're looking for is a virtual base class, then
1481 we know we've got what we want at this point; there's no way
1482 to get an ambiguity. */
1483 if (VBASE_NAME_P (lfi->name))
1484 {
1485 lfi->rval = nval;
1486 return nval;
1487 }
1488
1489 if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
1490 /* We need to return a member template class so we can
1491 define partial specializations. Is there a better
1492 way? */
1493 && !DECL_CLASS_TEMPLATE_P (nval))
1494 /* The thing we're looking for isn't a type, so the implicit
1495 typename extension doesn't apply, so we just pretend we
1496 didn't find anything. */
1497 return NULL_TREE;
1498
1499 lfi->rval = nval;
1500 lfi->from_dep_base_p = from_dep_base_p;
1501 lfi->rval_binfo = binfo;
1502 }
1503
1504 return NULL_TREE;
1505 }
1506
1507 /* Look for a memer named NAME in an inheritance lattice dominated by
1508 XBASETYPE. PROTECT is 0 or two, we do not check access. If it is
1509 1, we enforce accessibility. If PROTECT is zero, then, for an
1510 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
1511 error message. If PROTECT is 2, we return a TREE_LIST whose
1512 TREE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
1513 ambiguous candidates.
1514
1515 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1516 TYPE_DECL can be found return NULL_TREE. */
1517
1518 tree
1519 lookup_member (xbasetype, name, protect, want_type)
1520 register tree xbasetype, name;
1521 int protect, want_type;
1522 {
1523 tree rval, rval_binfo = NULL_TREE;
1524 tree type = NULL_TREE, basetype_path = NULL_TREE;
1525 struct lookup_field_info lfi;
1526
1527 /* rval_binfo is the binfo associated with the found member, note,
1528 this can be set with useful information, even when rval is not
1529 set, because it must deal with ALL members, not just non-function
1530 members. It is used for ambiguity checking and the hidden
1531 checks. Whereas rval is only set if a proper (not hidden)
1532 non-function member is found. */
1533
1534 const char *errstr = 0;
1535
1536 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1537 && IDENTIFIER_CLASS_VALUE (name))
1538 {
1539 tree field = IDENTIFIER_CLASS_VALUE (name);
1540 if (TREE_CODE (field) != FUNCTION_DECL
1541 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1542 /* We're in the scope of this class, and the value has already
1543 been looked up. Just return the cached value. */
1544 return field;
1545 }
1546
1547 if (TREE_CODE (xbasetype) == TREE_VEC)
1548 {
1549 type = BINFO_TYPE (xbasetype);
1550 basetype_path = xbasetype;
1551 }
1552 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1553 {
1554 type = xbasetype;
1555 basetype_path = TYPE_BINFO (type);
1556 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1557 980827);
1558 }
1559 else
1560 my_friendly_abort (97);
1561
1562 complete_type (type);
1563
1564 #ifdef GATHER_STATISTICS
1565 n_calls_lookup_field++;
1566 #endif /* GATHER_STATISTICS */
1567
1568 bzero ((PTR) &lfi, sizeof (lfi));
1569 lfi.type = type;
1570 lfi.name = name;
1571 lfi.want_type = want_type;
1572 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
1573 rval = lfi.rval;
1574 rval_binfo = lfi.rval_binfo;
1575 if (rval_binfo)
1576 type = BINFO_TYPE (rval_binfo);
1577 errstr = lfi.errstr;
1578
1579 /* If we are not interested in ambiguities, don't report them;
1580 just return NULL_TREE. */
1581 if (!protect && lfi.ambiguous)
1582 return NULL_TREE;
1583
1584 if (protect == 2)
1585 {
1586 if (lfi.ambiguous)
1587 return lfi.ambiguous;
1588 else
1589 protect = 0;
1590 }
1591
1592 /* [class.access]
1593
1594 In the case of overloaded function names, access control is
1595 applied to the function selected by overloaded resolution. */
1596 if (rval && protect && !is_overloaded_fn (rval)
1597 && !enforce_access (xbasetype, rval))
1598 return error_mark_node;
1599
1600 if (errstr && protect)
1601 {
1602 cp_error (errstr, name, type);
1603 if (lfi.ambiguous)
1604 print_candidates (lfi.ambiguous);
1605 rval = error_mark_node;
1606 }
1607
1608 /* If the thing we found was found via the implicit typename
1609 extension, build the typename type. */
1610 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1611 rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
1612 name, name,
1613 TREE_TYPE (rval)));
1614
1615 if (rval && is_overloaded_fn (rval))
1616 {
1617 /* Note that the binfo we put in the baselink is the binfo where
1618 we found the functions, which we need for overload
1619 resolution, but which should not be passed to enforce_access;
1620 rather, enforce_access wants a binfo which refers to the
1621 scope in which we started looking for the function. This
1622 will generally be the binfo passed into this function as
1623 xbasetype. */
1624
1625 rval = tree_cons (rval_binfo, rval, NULL_TREE);
1626 SET_BASELINK_P (rval);
1627 }
1628
1629 return rval;
1630 }
1631
1632 /* Like lookup_member, except that if we find a function member we
1633 return NULL_TREE. */
1634
1635 tree
1636 lookup_field (xbasetype, name, protect, want_type)
1637 register tree xbasetype, name;
1638 int protect, want_type;
1639 {
1640 tree rval = lookup_member (xbasetype, name, protect, want_type);
1641
1642 /* Ignore functions. */
1643 if (rval && TREE_CODE (rval) == TREE_LIST)
1644 return NULL_TREE;
1645
1646 return rval;
1647 }
1648
1649 /* Like lookup_member, except that if we find a non-function member we
1650 return NULL_TREE. */
1651
1652 tree
1653 lookup_fnfields (xbasetype, name, protect)
1654 register tree xbasetype, name;
1655 int protect;
1656 {
1657 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1658
1659 /* Ignore non-functions. */
1660 if (rval && TREE_CODE (rval) != TREE_LIST)
1661 return NULL_TREE;
1662
1663 return rval;
1664 }
1665
1666 /* TYPE is a class type. Return the index of the fields within
1667 the method vector with name NAME, or -1 is no such field exists. */
1668
1669 int
1670 lookup_fnfields_1 (type, name)
1671 tree type, name;
1672 {
1673 tree method_vec
1674 = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE;
1675
1676 if (method_vec != 0)
1677 {
1678 register int i;
1679 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1680 int len = TREE_VEC_LENGTH (method_vec);
1681 tree tmp;
1682
1683 #ifdef GATHER_STATISTICS
1684 n_calls_lookup_fnfields_1++;
1685 #endif /* GATHER_STATISTICS */
1686
1687 /* Constructors are first... */
1688 if (name == ctor_identifier)
1689 return (methods[CLASSTYPE_CONSTRUCTOR_SLOT]
1690 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
1691 /* and destructors are second. */
1692 if (name == dtor_identifier)
1693 return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1694 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
1695
1696 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1697 i < len && methods[i];
1698 ++i)
1699 {
1700 #ifdef GATHER_STATISTICS
1701 n_outer_fields_searched++;
1702 #endif /* GATHER_STATISTICS */
1703
1704 tmp = OVL_CURRENT (methods[i]);
1705 if (DECL_NAME (tmp) == name)
1706 return i;
1707
1708 /* If the type is complete and we're past the conversion ops,
1709 switch to binary search. */
1710 if (! DECL_CONV_FN_P (tmp)
1711 && COMPLETE_TYPE_P (type))
1712 {
1713 int lo = i + 1, hi = len;
1714
1715 while (lo < hi)
1716 {
1717 i = (lo + hi) / 2;
1718
1719 #ifdef GATHER_STATISTICS
1720 n_outer_fields_searched++;
1721 #endif /* GATHER_STATISTICS */
1722
1723 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1724
1725 if (tmp > name)
1726 hi = i;
1727 else if (tmp < name)
1728 lo = i + 1;
1729 else
1730 return i;
1731 }
1732 break;
1733 }
1734 }
1735
1736 /* If we didn't find it, it might have been a template
1737 conversion operator. (Note that we don't look for this case
1738 above so that we will always find specializations first.) */
1739 if (IDENTIFIER_TYPENAME_P (name))
1740 {
1741 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1742 i < len && methods[i];
1743 ++i)
1744 {
1745 tmp = OVL_CURRENT (methods[i]);
1746 if (! DECL_CONV_FN_P (tmp))
1747 {
1748 /* Since all conversion operators come first, we know
1749 there is no such operator. */
1750 break;
1751 }
1752 else if (TREE_CODE (tmp) == TEMPLATE_DECL)
1753 return i;
1754 }
1755 }
1756 }
1757
1758 return -1;
1759 }
1760 \f
1761 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1762 type in the hierarchy, in a breadth-first preorder traversal. .
1763 If it ever returns a non-NULL value, that value is immediately
1764 returned and the walk is terminated. At each node FN, is passed a
1765 BINFO indicating the path from the curently visited base-class to
1766 TYPE. Before each base-class is walked QFN is called. If the
1767 value returned is non-zero, the base-class is walked; otherwise it
1768 is not. If QFN is NULL, it is treated as a function which always
1769 returns 1. Both FN and QFN are passed the DATA whenever they are
1770 called. */
1771
1772 static tree
1773 bfs_walk (binfo, fn, qfn, data)
1774 tree binfo;
1775 tree (*fn) PARAMS ((tree, void *));
1776 tree (*qfn) PARAMS ((tree, void *));
1777 void *data;
1778 {
1779 size_t head;
1780 size_t tail;
1781 tree rval = NULL_TREE;
1782 /* An array of the base classes of BINFO. These will be built up in
1783 breadth-first order, except where QFN prunes the search. */
1784 varray_type bfs_bases;
1785
1786 /* Start with enough room for ten base classes. That will be enough
1787 for most hierarchies. */
1788 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
1789
1790 /* Put the first type into the stack. */
1791 VARRAY_TREE (bfs_bases, 0) = binfo;
1792 tail = 1;
1793
1794 for (head = 0; head < tail; ++head)
1795 {
1796 int i;
1797 int n_baselinks;
1798 tree binfos;
1799
1800 /* Pull the next type out of the queue. */
1801 binfo = VARRAY_TREE (bfs_bases, head);
1802
1803 /* If this is the one we're looking for, we're done. */
1804 rval = (*fn) (binfo, data);
1805 if (rval)
1806 break;
1807
1808 /* Queue up the base types. */
1809 binfos = BINFO_BASETYPES (binfo);
1810 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1811 for (i = 0; i < n_baselinks; i++)
1812 {
1813 tree base_binfo = TREE_VEC_ELT (binfos, i);
1814
1815 if (qfn)
1816 base_binfo = (*qfn) (base_binfo, data);
1817
1818 if (base_binfo)
1819 {
1820 if (tail == VARRAY_SIZE (bfs_bases))
1821 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1822 VARRAY_TREE (bfs_bases, tail) = base_binfo;
1823 ++tail;
1824 }
1825 }
1826 }
1827
1828 /* Clean up. */
1829 VARRAY_FREE (bfs_bases);
1830
1831 return rval;
1832 }
1833
1834 /* Exactly like bfs_walk, except that a depth-first traversal is
1835 performed, and PREFN is called in preorder, while POSTFN is called
1836 in postorder. */
1837
1838 tree
1839 dfs_walk_real (binfo, prefn, postfn, qfn, data)
1840 tree binfo;
1841 tree (*prefn) PARAMS ((tree, void *));
1842 tree (*postfn) PARAMS ((tree, void *));
1843 tree (*qfn) PARAMS ((tree, void *));
1844 void *data;
1845 {
1846 int i;
1847 int n_baselinks;
1848 tree binfos;
1849 tree rval = NULL_TREE;
1850
1851 /* Call the pre-order walking function. */
1852 if (prefn)
1853 {
1854 rval = (*prefn) (binfo, data);
1855 if (rval)
1856 return rval;
1857 }
1858
1859 /* Process the basetypes. */
1860 binfos = BINFO_BASETYPES (binfo);
1861 n_baselinks = BINFO_N_BASETYPES (binfo);
1862 for (i = 0; i < n_baselinks; i++)
1863 {
1864 tree base_binfo = TREE_VEC_ELT (binfos, i);
1865
1866 if (qfn)
1867 base_binfo = (*qfn) (base_binfo, data);
1868
1869 if (base_binfo)
1870 {
1871 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1872 if (rval)
1873 return rval;
1874 }
1875 }
1876
1877 /* Call the post-order walking function. */
1878 if (postfn)
1879 rval = (*postfn) (binfo, data);
1880
1881 return rval;
1882 }
1883
1884 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1885 performed. */
1886
1887 tree
1888 dfs_walk (binfo, fn, qfn, data)
1889 tree binfo;
1890 tree (*fn) PARAMS ((tree, void *));
1891 tree (*qfn) PARAMS ((tree, void *));
1892 void *data;
1893 {
1894 return dfs_walk_real (binfo, 0, fn, qfn, data);
1895 }
1896
1897 struct gvnt_info
1898 {
1899 /* The name of the function we are looking for. */
1900 tree name;
1901 /* The overloaded functions we have found. */
1902 tree fields;
1903 };
1904
1905 /* Called from get_virtuals_named_this via bfs_walk. */
1906
1907 static tree
1908 get_virtuals_named_this_r (binfo, data)
1909 tree binfo;
1910 void *data;
1911 {
1912 struct gvnt_info *gvnti = (struct gvnt_info *) data;
1913 tree type = BINFO_TYPE (binfo);
1914 int idx;
1915
1916 idx = lookup_fnfields_here (BINFO_TYPE (binfo), gvnti->name);
1917 if (idx >= 0)
1918 gvnti->fields
1919 = tree_cons (binfo,
1920 TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx),
1921 gvnti->fields);
1922
1923 return NULL_TREE;
1924 }
1925
1926 /* Return the virtual functions with the indicated NAME in the type
1927 indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE
1928 indicates the base class from which the TREE_VALUE (an OVERLOAD or
1929 just a FUNCTION_DECL) originated. */
1930
1931 static tree
1932 get_virtuals_named_this (binfo, name)
1933 tree binfo;
1934 tree name;
1935 {
1936 struct gvnt_info gvnti;
1937 tree fields;
1938
1939 gvnti.name = name;
1940 gvnti.fields = NULL_TREE;
1941
1942 bfs_walk (binfo, get_virtuals_named_this_r, 0, &gvnti);
1943
1944 /* Get to the function decls, and return the first virtual function
1945 with this name, if there is one. */
1946 for (fields = gvnti.fields; fields; fields = next_baselink (fields))
1947 {
1948 tree fndecl;
1949
1950 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl))
1951 if (DECL_VINDEX (OVL_CURRENT (fndecl)))
1952 return fields;
1953 }
1954 return NULL_TREE;
1955 }
1956
1957 static tree
1958 get_virtual_destructor (binfo, data)
1959 tree binfo;
1960 void *data ATTRIBUTE_UNUSED;
1961 {
1962 tree type = BINFO_TYPE (binfo);
1963 if (TYPE_HAS_DESTRUCTOR (type)
1964 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1)))
1965 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1);
1966 return 0;
1967 }
1968
1969 static tree
1970 tree_has_any_destructor_p (binfo, data)
1971 tree binfo;
1972 void *data ATTRIBUTE_UNUSED;
1973 {
1974 tree type = BINFO_TYPE (binfo);
1975 return TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) ? binfo : NULL_TREE;
1976 }
1977
1978 /* Returns > 0 if a function with type DRETTYPE overriding a function
1979 with type BRETTYPE is covariant, as defined in [class.virtual].
1980
1981 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1982 adjustment), or -1 if pedantically invalid covariance. */
1983
1984 static int
1985 covariant_return_p (brettype, drettype)
1986 tree brettype, drettype;
1987 {
1988 tree binfo;
1989
1990 if (TREE_CODE (brettype) == FUNCTION_DECL)
1991 {
1992 brettype = TREE_TYPE (TREE_TYPE (brettype));
1993 drettype = TREE_TYPE (TREE_TYPE (drettype));
1994 }
1995 else if (TREE_CODE (brettype) == METHOD_TYPE)
1996 {
1997 brettype = TREE_TYPE (brettype);
1998 drettype = TREE_TYPE (drettype);
1999 }
2000
2001 if (same_type_p (brettype, drettype))
2002 return 0;
2003
2004 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
2005 && (TREE_CODE (brettype) == POINTER_TYPE
2006 || TREE_CODE (brettype) == REFERENCE_TYPE)
2007 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
2008 return 0;
2009
2010 if (! can_convert (brettype, drettype))
2011 return 0;
2012
2013 brettype = TREE_TYPE (brettype);
2014 drettype = TREE_TYPE (drettype);
2015
2016 /* If not pedantic, allow any standard pointer conversion. */
2017 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
2018 return -1;
2019
2020 binfo = get_binfo (brettype, drettype, 1);
2021
2022 /* If we get an error_mark_node from get_binfo, it already complained,
2023 so let's just succeed. */
2024 if (binfo == error_mark_node)
2025 return 1;
2026
2027 if (! BINFO_OFFSET_ZEROP (binfo) || TREE_VIA_VIRTUAL (binfo))
2028 return 2;
2029 return 1;
2030 }
2031
2032 /* Check that virtual overrider OVERRIDER is acceptable for base function
2033 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2034
2035 static int
2036 check_final_overrider (overrider, basefn)
2037 tree overrider, basefn;
2038 {
2039 tree over_type = TREE_TYPE (overrider);
2040 tree base_type = TREE_TYPE (basefn);
2041 tree over_return = TREE_TYPE (over_type);
2042 tree base_return = TREE_TYPE (base_type);
2043 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
2044 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
2045 int i;
2046
2047 if (same_type_p (base_return, over_return))
2048 /* OK */;
2049 else if ((i = covariant_return_p (base_return, over_return)))
2050 {
2051 if (i == 2)
2052 sorry ("adjusting pointers for covariant returns");
2053
2054 if (pedantic && i == -1)
2055 {
2056 cp_pedwarn_at ("invalid covariant return type for `virtual %#D'", overrider);
2057 cp_pedwarn_at (" overriding `virtual %#D' (must be pointer or reference to class)", basefn);
2058 }
2059 }
2060 else if (IS_AGGR_TYPE_2 (base_return, over_return)
2061 && same_or_base_type_p (base_return, over_return))
2062 {
2063 cp_error_at ("invalid covariant return type for `virtual %#D'", overrider);
2064 cp_error_at (" overriding `virtual %#D' (must use pointer or reference)", basefn);
2065 return 0;
2066 }
2067 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)) == NULL_TREE)
2068 {
2069 cp_error_at ("conflicting return type specified for `virtual %#D'", overrider);
2070 cp_error_at (" overriding `virtual %#D'", basefn);
2071 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
2072 DECL_CONTEXT (overrider));
2073 return 0;
2074 }
2075
2076 /* Check throw specifier is subset. */
2077 /* XXX At the moment, punt on an overriding artificial function. We
2078 don't generate its exception specifier, so can't check it properly. */
2079 if (! DECL_ARTIFICIAL (overrider)
2080 && !comp_except_specs (base_throw, over_throw, 0))
2081 {
2082 cp_error_at ("looser throw specifier for `virtual %#F'", overrider);
2083 cp_error_at (" overriding `virtual %#F'", basefn);
2084 return 0;
2085 }
2086 return 1;
2087 }
2088
2089 /* Given a class type TYPE, and a function decl FNDECL, look for a
2090 virtual function in TYPE's hierarchy which FNDECL could match as a
2091 virtual function. It doesn't matter which one we find.
2092
2093 DTORP is nonzero if we are looking for a destructor. Destructors
2094 need special treatment because they do not match by name. */
2095
2096 tree
2097 get_matching_virtual (binfo, fndecl, dtorp)
2098 tree binfo, fndecl;
2099 int dtorp;
2100 {
2101 tree tmp = NULL_TREE;
2102
2103 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
2104 /* In [temp.mem] we have:
2105
2106 A specialization of a member function template does not
2107 override a virtual function from a base class. */
2108 return NULL_TREE;
2109
2110 /* Breadth first search routines start searching basetypes
2111 of TYPE, so we must perform first ply of search here. */
2112 if (dtorp)
2113 return bfs_walk (binfo, get_virtual_destructor,
2114 tree_has_any_destructor_p, 0);
2115 else
2116 {
2117 tree drettype, dtypes, btypes, instptr_type;
2118 tree baselink, best = NULL_TREE;
2119 tree declarator = DECL_NAME (fndecl);
2120 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
2121 return NULL_TREE;
2122
2123 baselink = get_virtuals_named_this (binfo, declarator);
2124 if (baselink == NULL_TREE)
2125 return NULL_TREE;
2126
2127 drettype = TREE_TYPE (TREE_TYPE (fndecl));
2128 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2129 if (DECL_STATIC_FUNCTION_P (fndecl))
2130 instptr_type = NULL_TREE;
2131 else
2132 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
2133
2134 for (; baselink; baselink = next_baselink (baselink))
2135 {
2136 tree tmps;
2137 for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps))
2138 {
2139 tmp = OVL_CURRENT (tmps);
2140 if (! DECL_VINDEX (tmp))
2141 continue;
2142
2143 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
2144 if (instptr_type == NULL_TREE)
2145 {
2146 if (compparms (TREE_CHAIN (btypes), dtypes))
2147 /* Caller knows to give error in this case. */
2148 return tmp;
2149 return NULL_TREE;
2150 }
2151
2152 if (/* The first parameter is the `this' parameter,
2153 which has POINTER_TYPE, and we can therefore
2154 safely use TYPE_QUALS, rather than
2155 CP_TYPE_QUALS. */
2156 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes)))
2157 == TYPE_QUALS (instptr_type))
2158 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes)))
2159 {
2160 check_final_overrider (fndecl, tmp);
2161
2162 /* FNDECL overrides this function. We continue to
2163 check all the other functions in order to catch
2164 errors; it might be that in some other baseclass
2165 a virtual function was declared with the same
2166 parameter types, but a different return type. */
2167 best = tmp;
2168 }
2169 }
2170 }
2171
2172 return best;
2173 }
2174 }
2175
2176 /* A queue function for dfs_walk that skips any nonprimary virtual
2177 bases and any already marked bases. */
2178
2179 tree
2180 dfs_skip_nonprimary_vbases_unmarkedp (binfo, data)
2181 tree binfo;
2182 void *data ATTRIBUTE_UNUSED;
2183 {
2184 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
2185 /* This is a non-primary virtual base. Skip it. */
2186 return NULL_TREE;
2187
2188 return unmarkedp (binfo, NULL);
2189 }
2190
2191 /* A queue function for dfs_walk that skips any nonprimary virtual
2192 bases and any unmarked bases. */
2193
2194 tree
2195 dfs_skip_nonprimary_vbases_markedp (binfo, data)
2196 tree binfo;
2197 void *data ATTRIBUTE_UNUSED;
2198 {
2199 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
2200 /* This is a non-primary virtual base. Skip it. */
2201 return NULL_TREE;
2202
2203 return markedp (binfo, NULL);
2204 }
2205
2206 /* If BINFO is a non-primary virtual baseclass (in the hierarchy
2207 dominated by TYPE), and no primary copy appears anywhere in the
2208 hierarchy, return the shared copy. If a primary copy appears
2209 elsewhere, return NULL_TREE. Otherwise, return BINFO itself; it is
2210 either a non-virtual base or a primary virtual base. */
2211
2212 static tree
2213 get_shared_vbase_if_not_primary (binfo, data)
2214 tree binfo;
2215 void *data;
2216 {
2217 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
2218 {
2219 tree type = (tree) data;
2220
2221 if (TREE_CODE (type) == TREE_LIST)
2222 type = TREE_PURPOSE (type);
2223
2224 /* This is a non-primary virtual base. If there is no primary
2225 version, get the shared version. */
2226 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2227 if (BINFO_VBASE_PRIMARY_P (binfo))
2228 return NULL_TREE;
2229 }
2230
2231 return binfo;
2232 }
2233
2234 /* A queue function to use with dfs_walk that prevents travel into any
2235 nonprimary virtual base, or its baseclasses. DATA should be the
2236 type of the complete object, or a TREE_LIST whose TREE_PURPOSE is
2237 the type of the complete object. By using this function as a queue
2238 function, you will walk over exactly those BINFOs that actually
2239 exist in the complete object, including those for virtual base
2240 classes. If you SET_BINFO_MARKED for each binfo you process, you
2241 are further guaranteed that you will walk into each virtual base
2242 class exactly once. */
2243
2244 tree
2245 dfs_unmarked_real_bases_queue_p (binfo, data)
2246 tree binfo;
2247 void *data;
2248 {
2249 binfo = get_shared_vbase_if_not_primary (binfo, data);
2250 return binfo ? unmarkedp (binfo, NULL) : NULL_TREE;
2251 }
2252
2253 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2254 that are marked, rather than unmarked. */
2255
2256 tree
2257 dfs_marked_real_bases_queue_p (binfo, data)
2258 tree binfo;
2259 void *data;
2260 {
2261 binfo = get_shared_vbase_if_not_primary (binfo, data);
2262 return binfo ? markedp (binfo, NULL) : NULL_TREE;
2263 }
2264
2265 /* A queue function that skips all virtual bases (and their
2266 bases). */
2267
2268 tree
2269 dfs_skip_vbases (binfo, data)
2270 tree binfo;
2271 void *data ATTRIBUTE_UNUSED;
2272 {
2273 if (TREE_VIA_VIRTUAL (binfo))
2274 return NULL_TREE;
2275
2276 return binfo;
2277 }
2278
2279 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2280
2281 static tree
2282 dfs_get_pure_virtuals (binfo, data)
2283 tree binfo;
2284 void *data;
2285 {
2286 tree type = (tree) data;
2287
2288 /* We're not interested in primary base classes; the derived class
2289 of which they are a primary base will contain the information we
2290 need. */
2291 if (!BINFO_PRIMARY_MARKED_P (binfo))
2292 {
2293 tree virtuals;
2294
2295 for (virtuals = BINFO_VIRTUALS (binfo);
2296 virtuals;
2297 virtuals = TREE_CHAIN (virtuals))
2298 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2299 CLASSTYPE_PURE_VIRTUALS (type)
2300 = tree_cons (NULL_TREE, BV_FN (virtuals),
2301 CLASSTYPE_PURE_VIRTUALS (type));
2302 }
2303
2304 SET_BINFO_MARKED (binfo);
2305
2306 return NULL_TREE;
2307 }
2308
2309 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2310
2311 void
2312 get_pure_virtuals (type)
2313 tree type;
2314 {
2315 tree vbases;
2316
2317 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2318 is going to be overridden. */
2319 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
2320 /* Now, run through all the bases which are not primary bases, and
2321 collect the pure virtual functions. We look at the vtable in
2322 each class to determine what pure virtual functions are present.
2323 (A primary base is not interesting because the derived class of
2324 which it is a primary base will contain vtable entries for the
2325 pure virtuals in the base class. */
2326 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2327 dfs_unmarked_real_bases_queue_p, type);
2328 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2329 dfs_marked_real_bases_queue_p, type);
2330
2331 /* Put the pure virtuals in dfs order. */
2332 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2333
2334 for (vbases = CLASSTYPE_VBASECLASSES (type);
2335 vbases;
2336 vbases = TREE_CHAIN (vbases))
2337 {
2338 tree virtuals;
2339
2340 for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
2341 virtuals;
2342 virtuals = TREE_CHAIN (virtuals))
2343 {
2344 tree base_fndecl = BV_FN (virtuals);
2345 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2346 cp_error ("`%#D' needs a final overrider", base_fndecl);
2347 }
2348 }
2349 }
2350
2351 static tree
2352 next_baselink (baselink)
2353 tree baselink;
2354 {
2355 tree tmp = TREE_TYPE (baselink);
2356 baselink = TREE_CHAIN (baselink);
2357 while (tmp)
2358 {
2359 /* @@ does not yet add previous base types. */
2360 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2361 baselink);
2362 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2363 tmp = TREE_CHAIN (tmp);
2364 }
2365 return baselink;
2366 }
2367 \f
2368 /* DEPTH-FIRST SEARCH ROUTINES. */
2369
2370 tree
2371 markedp (binfo, data)
2372 tree binfo;
2373 void *data ATTRIBUTE_UNUSED;
2374 {
2375 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2376 }
2377
2378 tree
2379 unmarkedp (binfo, data)
2380 tree binfo;
2381 void *data ATTRIBUTE_UNUSED;
2382 {
2383 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2384 }
2385
2386 tree
2387 marked_vtable_pathp (binfo, data)
2388 tree binfo;
2389 void *data ATTRIBUTE_UNUSED;
2390 {
2391 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2392 }
2393
2394 tree
2395 unmarked_vtable_pathp (binfo, data)
2396 tree binfo;
2397 void *data ATTRIBUTE_UNUSED;
2398 {
2399 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2400 }
2401
2402 static tree
2403 marked_pushdecls_p (binfo, data)
2404 tree binfo;
2405 void *data ATTRIBUTE_UNUSED;
2406 {
2407 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2408 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2409 }
2410
2411 static tree
2412 unmarked_pushdecls_p (binfo, data)
2413 tree binfo;
2414 void *data ATTRIBUTE_UNUSED;
2415 {
2416 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2417 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2418 }
2419
2420 /* The worker functions for `dfs_walk'. These do not need to
2421 test anything (vis a vis marking) if they are paired with
2422 a predicate function (above). */
2423
2424 tree
2425 dfs_unmark (binfo, data)
2426 tree binfo;
2427 void *data ATTRIBUTE_UNUSED;
2428 {
2429 CLEAR_BINFO_MARKED (binfo);
2430 return NULL_TREE;
2431 }
2432
2433 \f
2434 static tree
2435 dfs_init_vbase_pointers (binfo, data)
2436 tree binfo;
2437 void *data;
2438 {
2439 struct vbase_info *vi = (struct vbase_info *) data;
2440 tree type = BINFO_TYPE (binfo);
2441 tree fields;
2442 tree this_vbase_ptr;
2443
2444 /* Don't initialize the same base more than once. */
2445 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2446
2447 /* We know that VI->DECL_PTR points to the complete object. So,
2448 finding a pointer to this subobject is easy. */
2449 this_vbase_ptr = build (PLUS_EXPR,
2450 build_pointer_type (type),
2451 vi->decl_ptr,
2452 BINFO_OFFSET (binfo));
2453
2454 /* We're going to iterate through all the pointers to virtual
2455 base-classes. They come at the beginning of the class. */
2456 fields = TYPE_FIELDS (type);
2457
2458 if (fields == NULL_TREE
2459 || DECL_NAME (fields) == NULL_TREE
2460 || ! VBASE_NAME_P (DECL_NAME (fields)))
2461 return NULL_TREE;
2462
2463 if (build_pointer_type (type)
2464 != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
2465 my_friendly_abort (125);
2466
2467 while (fields && DECL_NAME (fields) && VBASE_NAME_P (DECL_NAME (fields)))
2468 {
2469 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2470 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
2471 tree init;
2472 tree vbase_type;
2473 tree vbase_binfo;
2474
2475 vbase_type = TREE_TYPE (TREE_TYPE (fields));
2476 vbase_binfo = binfo_for_vbase (vbase_type, vi->type);
2477 init = build (PLUS_EXPR,
2478 build_pointer_type (vbase_type),
2479 vi->decl_ptr,
2480 BINFO_OFFSET (vbase_binfo));
2481 vi->inits
2482 = tree_cons (vbase_binfo,
2483 build_modify_expr (ref, NOP_EXPR, init),
2484 vi->inits);
2485 fields = TREE_CHAIN (fields);
2486 }
2487
2488 return NULL_TREE;
2489 }
2490
2491 /* Call CLEAR_BINFO_VTABLE_PATH_MARKED for BINFO. */
2492
2493 static tree
2494 dfs_vtable_path_unmark (binfo, data)
2495 tree binfo;
2496 void *data ATTRIBUTE_UNUSED;
2497 {
2498 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2499 return NULL_TREE;
2500 }
2501
2502 tree
2503 init_vbase_pointers (type, decl_ptr)
2504 tree type;
2505 tree decl_ptr;
2506 {
2507 my_friendly_assert (!vbase_offsets_in_vtable_p (), 20000516);
2508
2509 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2510 {
2511 struct vbase_info vi;
2512 int old_flag = flag_this_is_variable;
2513 tree binfo = TYPE_BINFO (type);
2514 flag_this_is_variable = -2;
2515
2516 /* Find all the virtual base classes, marking them for later
2517 initialization. */
2518 vi.type = type;
2519 vi.decl_ptr = decl_ptr;
2520 vi.inits = NULL_TREE;
2521
2522 /* Build up a list of the initializers. */
2523 dfs_walk_real (binfo,
2524 dfs_init_vbase_pointers, 0,
2525 unmarked_vtable_pathp,
2526 &vi);
2527 dfs_walk (binfo,
2528 dfs_vtable_path_unmark,
2529 marked_vtable_pathp,
2530 NULL);
2531
2532 flag_this_is_variable = old_flag;
2533 return vi.inits;
2534 }
2535
2536 return 0;
2537 }
2538
2539 /* get the virtual context (the vbase that directly contains the
2540 DECL_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2541 or NULL_TREE if there is none.
2542
2543 FNDECL must come from a virtual table from a virtual base to ensure
2544 that there is only one possible DECL_CONTEXT.
2545
2546 We know that if there is more than one place (binfo) the fndecl that the
2547 declared, they all refer to the same binfo. See get_class_offset_1 for
2548 the check that ensures this. */
2549
2550 static tree
2551 virtual_context (fndecl, t, vbase)
2552 tree fndecl, t, vbase;
2553 {
2554 tree path;
2555 if (get_base_distance (DECL_CONTEXT (fndecl), t, 0, &path) < 0)
2556 {
2557 /* DECL_CONTEXT can be ambiguous in t. */
2558 if (get_base_distance (DECL_CONTEXT (fndecl), vbase, 0, &path) >= 0)
2559 {
2560 while (path)
2561 {
2562 /* Not sure if checking path == vbase is necessary here, but just in
2563 case it is. */
2564 if (TREE_VIA_VIRTUAL (path) || path == vbase)
2565 return binfo_for_vbase (BINFO_TYPE (path), t);
2566 path = BINFO_INHERITANCE_CHAIN (path);
2567 }
2568 }
2569 /* This shouldn't happen, I don't want errors! */
2570 warning ("recoverable compiler error, fixups for virtual function");
2571 return vbase;
2572 }
2573 while (path)
2574 {
2575 if (TREE_VIA_VIRTUAL (path))
2576 return binfo_for_vbase (BINFO_TYPE (path), t);
2577 path = BINFO_INHERITANCE_CHAIN (path);
2578 }
2579 return 0;
2580 }
2581
2582 /* Fixups upcast offsets for one vtable.
2583 Entries may stay within the VBASE given, or
2584 they may upcast into a direct base, or
2585 they may upcast into a different vbase.
2586
2587 We only need to do fixups in case 2 and 3. In case 2, we add in
2588 the virtual base offset to effect an upcast, in case 3, we add in
2589 the virtual base offset to effect an upcast, then subtract out the
2590 offset for the other virtual base, to effect a downcast into it.
2591
2592 This routine mirrors fixup_vtable_deltas in functionality, though
2593 this one is runtime based, and the other is compile time based.
2594 Conceivably that routine could be removed entirely, and all fixups
2595 done at runtime.
2596
2597 VBASE_OFFSETS is an association list of virtual bases that contains
2598 offset information for the virtual bases, so the offsets are only
2599 calculated once. */
2600
2601 static void
2602 expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t,
2603 vbase_offsets)
2604 tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets;
2605 {
2606 tree virtuals;
2607 tree vc;
2608 tree delta;
2609 HOST_WIDE_INT n;
2610
2611 while (BINFO_PRIMARY_MARKED_P (binfo))
2612 {
2613 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2614 if (TREE_VIA_VIRTUAL (binfo))
2615 return;
2616 }
2617
2618 delta = purpose_member (vbase, *vbase_offsets);
2619 if (! delta)
2620 {
2621 delta = build (PLUS_EXPR,
2622 build_pointer_type (BINFO_TYPE (vbase)),
2623 orig_addr,
2624 BINFO_OFFSET (vbase));
2625 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr);
2626 delta = save_expr (delta);
2627 delta = tree_cons (vbase, delta, *vbase_offsets);
2628 *vbase_offsets = delta;
2629 }
2630
2631 for (virtuals = BINFO_VIRTUALS (binfo),
2632 n = first_vfun_index (BINFO_TYPE (binfo));
2633 virtuals;
2634 virtuals = TREE_CHAIN (virtuals), ++n)
2635 {
2636 tree current_fndecl = TREE_VALUE (virtuals);
2637
2638 if (current_fndecl
2639 && current_fndecl != abort_fndecl
2640 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2641 {
2642 /* This may in fact need a runtime fixup. */
2643 tree idx = build_int_2 (n, 0);
2644 tree vtbl = BINFO_VTABLE (binfo);
2645 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2646 tree aref, ref, naref;
2647 tree old_delta, new_delta;
2648 tree init;
2649
2650 if (nvtbl == NULL_TREE
2651 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2652 {
2653 /* Dup it if it isn't in local scope yet. */
2654 nvtbl = build_decl
2655 (VAR_DECL, DECL_NAME (vtbl),
2656 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl)));
2657 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2658 DECL_ALIGN (nvtbl));
2659 TREE_READONLY (nvtbl) = 0;
2660 DECL_ARTIFICIAL (nvtbl) = 1;
2661 nvtbl = pushdecl (nvtbl);
2662 init = NULL_TREE;
2663 cp_finish_decl (nvtbl, init, NULL_TREE,
2664 LOOKUP_ONLYCONVERTING);
2665
2666 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2667 because they wouldn't be useful; everything that wants to
2668 look at the vtable will look at the decl for the normal
2669 vtable. Setting DECL_CONTEXT also screws up
2670 decl_function_context. */
2671
2672 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2673 nvtbl, vtbl);
2674 finish_expr_stmt (init);
2675 /* Update the vtable pointers as necessary. */
2676 ref = build_vfield_ref
2677 (build_indirect_ref (addr, NULL_PTR),
2678 DECL_CONTEXT (TYPE_VFIELD (BINFO_TYPE (binfo))));
2679 finish_expr_stmt
2680 (build_modify_expr (ref, NOP_EXPR, nvtbl));
2681 }
2682 assemble_external (vtbl);
2683 aref = build_array_ref (vtbl, idx);
2684 naref = build_array_ref (nvtbl, idx);
2685 old_delta = build_component_ref (aref, delta_identifier,
2686 NULL_TREE, 0);
2687 new_delta = build_component_ref (naref, delta_identifier,
2688 NULL_TREE, 0);
2689
2690 /* This is a upcast, so we have to add the offset for the
2691 virtual base. */
2692 old_delta = build_binary_op (PLUS_EXPR, old_delta,
2693 TREE_VALUE (delta));
2694 if (vc)
2695 {
2696 /* If this is set, we need to subtract out the delta
2697 adjustments for the other virtual base that we
2698 downcast into. */
2699 tree vc_delta = purpose_member (vc, *vbase_offsets);
2700 if (! vc_delta)
2701 {
2702 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
2703 vc_delta = build (PLUS_EXPR,
2704 build_pointer_type (BINFO_TYPE (vc)),
2705 orig_addr,
2706 BINFO_OFFSET (vc));
2707 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
2708 vc_delta, vc_addr);
2709 vc_delta = save_expr (vc_delta);
2710 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2711 }
2712 else
2713 vc_delta = TREE_VALUE (vc_delta);
2714
2715 /* This is a downcast, so we have to subtract the offset
2716 for the virtual base. */
2717 old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta);
2718 }
2719
2720 TREE_READONLY (new_delta) = 0;
2721 TREE_TYPE (new_delta) =
2722 cp_build_qualified_type (TREE_TYPE (new_delta),
2723 CP_TYPE_QUALS (TREE_TYPE (new_delta))
2724 & ~TYPE_QUAL_CONST);
2725 finish_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
2726 old_delta));
2727 }
2728 }
2729 }
2730
2731 /* Fixup upcast offsets for all direct vtables. Patterned after
2732 expand_direct_vtbls_init. */
2733
2734 static void
2735 fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
2736 tree real_binfo, binfo;
2737 int init_self, can_elide;
2738 tree addr, orig_addr, type, vbase, *vbase_offsets;
2739 {
2740 tree real_binfos = BINFO_BASETYPES (real_binfo);
2741 tree binfos = BINFO_BASETYPES (binfo);
2742 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
2743
2744 for (i = 0; i < n_baselinks; i++)
2745 {
2746 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
2747 tree base_binfo = TREE_VEC_ELT (binfos, i);
2748 int is_not_base_vtable
2749 = !BINFO_PRIMARY_MARKED_P (real_base_binfo);
2750 if (! TREE_VIA_VIRTUAL (real_base_binfo))
2751 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
2752 is_not_base_vtable, can_elide, addr,
2753 orig_addr, type, vbase, vbase_offsets);
2754 }
2755 #if 0
2756 /* Before turning this on, make sure it is correct. */
2757 if (can_elide && ! BINFO_MODIFIED (binfo))
2758 return;
2759 #endif
2760 /* Should we use something besides CLASSTYPE_VFIELDS? */
2761 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
2762 {
2763 tree new_addr = convert_pointer_to_real (binfo, addr);
2764 expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr,
2765 type, vbase_offsets);
2766 }
2767 }
2768
2769 /* Fixup all the virtual upcast offsets for TYPE. DECL_PTR is the
2770 address of the sub-object being initialized. */
2771
2772 void
2773 fixup_all_virtual_upcast_offsets (decl_ptr)
2774 tree decl_ptr;
2775 {
2776 tree if_stmt;
2777 tree in_charge_node;
2778 tree vbases;
2779 tree type;
2780
2781 /* Only tweak the vtables if we're in charge. */
2782 in_charge_node = current_in_charge_parm;
2783 if (!in_charge_node)
2784 /* There's no need for any fixups in this case. */
2785 return;
2786 in_charge_node = build_binary_op (EQ_EXPR,
2787 in_charge_node, integer_zero_node);
2788 if_stmt = begin_if_stmt ();
2789 finish_if_stmt_cond (in_charge_node, if_stmt);
2790
2791 /* Iterate through the virtual bases, fixing up the upcast offset
2792 for each one. */
2793 type = TREE_TYPE (TREE_TYPE (decl_ptr));
2794 for (vbases = CLASSTYPE_VBASECLASSES (type);
2795 vbases;
2796 vbases = TREE_CHAIN (vbases))
2797 {
2798 if (flag_vtable_thunks)
2799 /* We don't have dynamic thunks yet! So for now, just fail
2800 silently. */
2801 ;
2802 else
2803 {
2804 tree vbase;
2805 tree vbase_offsets;
2806 tree addr;
2807
2808 vbase = find_vbase_instance (TREE_PURPOSE (vbases), type);
2809 vbase_offsets = NULL_TREE;
2810 addr = convert_pointer_to_vbase (TREE_PURPOSE (vbases), decl_ptr);
2811 fixup_virtual_upcast_offsets (vbase,
2812 TYPE_BINFO (TREE_PURPOSE (vbases)),
2813 1, 0, addr, decl_ptr,
2814 type, vbase, &vbase_offsets);
2815 }
2816 }
2817
2818 /* Close out the if-statement. */
2819 finish_then_clause (if_stmt);
2820 finish_if_stmt ();
2821 }
2822
2823 /* get virtual base class types.
2824 This adds type to the vbase_types list in reverse dfs order.
2825 Ordering is very important, so don't change it. */
2826
2827 static tree
2828 dfs_get_vbase_types (binfo, data)
2829 tree binfo;
2830 void *data;
2831 {
2832 tree type = (tree) data;
2833
2834 if (TREE_VIA_VIRTUAL (binfo))
2835 CLASSTYPE_VBASECLASSES (type)
2836 = tree_cons (BINFO_TYPE (binfo),
2837 binfo,
2838 CLASSTYPE_VBASECLASSES (type));
2839 SET_BINFO_MARKED (binfo);
2840 return NULL_TREE;
2841 }
2842
2843 /* Called via dfs_walk from mark_primary_bases. Builds the
2844 inheritance graph order list of BINFOs. */
2845
2846 static tree
2847 dfs_build_inheritance_graph_order (binfo, data)
2848 tree binfo;
2849 void *data;
2850 {
2851 tree *last_binfo = (tree *) data;
2852
2853 if (*last_binfo)
2854 TREE_CHAIN (*last_binfo) = binfo;
2855 *last_binfo = binfo;
2856 SET_BINFO_MARKED (binfo);
2857 return NULL_TREE;
2858 }
2859
2860 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
2861
2862 void
2863 get_vbase_types (type)
2864 tree type;
2865 {
2866 tree last_binfo;
2867
2868 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
2869 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
2870 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2871 reverse it so that we get normal dfs ordering. */
2872 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
2873 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2874 /* Thread the BINFOs in inheritance-graph order. */
2875 last_binfo = NULL;
2876 dfs_walk_real (TYPE_BINFO (type),
2877 dfs_build_inheritance_graph_order,
2878 NULL,
2879 unmarkedp,
2880 &last_binfo);
2881 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
2882 }
2883
2884 /* Called from find_vbase_instance via dfs_walk. */
2885
2886 static tree
2887 dfs_find_vbase_instance (binfo, data)
2888 tree binfo;
2889 void *data;
2890 {
2891 tree base = TREE_VALUE ((tree) data);
2892
2893 if (BINFO_PRIMARY_MARKED_P (binfo)
2894 && same_type_p (BINFO_TYPE (binfo), base))
2895 return binfo;
2896
2897 return NULL_TREE;
2898 }
2899
2900 /* Find the real occurrence of the virtual BASE (a class type) in the
2901 hierarchy dominated by TYPE. */
2902
2903 tree
2904 find_vbase_instance (base, type)
2905 tree base;
2906 tree type;
2907 {
2908 tree instance;
2909
2910 instance = binfo_for_vbase (base, type);
2911 if (!BINFO_VBASE_PRIMARY_P (instance))
2912 return instance;
2913
2914 return dfs_walk (TYPE_BINFO (type),
2915 dfs_find_vbase_instance,
2916 NULL,
2917 build_tree_list (type, base));
2918 }
2919
2920 \f
2921 /* Debug info for C++ classes can get very large; try to avoid
2922 emitting it everywhere.
2923
2924 Note that this optimization wins even when the target supports
2925 BINCL (if only slightly), and reduces the amount of work for the
2926 linker. */
2927
2928 void
2929 maybe_suppress_debug_info (t)
2930 tree t;
2931 {
2932 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2933 does not support name references between translation units. It supports
2934 symbolic references between translation units, but only within a single
2935 executable or shared library.
2936
2937 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2938 that the type was never defined, so we only get the members we
2939 actually define. */
2940 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
2941 return;
2942
2943 /* We might have set this earlier in cp_finish_decl. */
2944 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2945
2946 /* If we already know how we're handling this class, handle debug info
2947 the same way. */
2948 if (CLASSTYPE_INTERFACE_KNOWN (t))
2949 {
2950 if (CLASSTYPE_INTERFACE_ONLY (t))
2951 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2952 /* else don't set it. */
2953 }
2954 /* If the class has a vtable, write out the debug info along with
2955 the vtable. */
2956 else if (TYPE_CONTAINS_VPTR_P (t))
2957 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2958
2959 /* Otherwise, just emit the debug info normally. */
2960 }
2961
2962 /* Note that we want debugging information for a base class of a class
2963 whose vtable is being emitted. Normally, this would happen because
2964 calling the constructor for a derived class implies calling the
2965 constructors for all bases, which involve initializing the
2966 appropriate vptr with the vtable for the base class; but in the
2967 presence of optimization, this initialization may be optimized
2968 away, so we tell finish_vtable_vardecl that we want the debugging
2969 information anyway. */
2970
2971 static tree
2972 dfs_debug_mark (binfo, data)
2973 tree binfo;
2974 void *data ATTRIBUTE_UNUSED;
2975 {
2976 tree t = BINFO_TYPE (binfo);
2977
2978 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2979
2980 return NULL_TREE;
2981 }
2982
2983 /* Returns BINFO if we haven't already noted that we want debugging
2984 info for this base class. */
2985
2986 static tree
2987 dfs_debug_unmarkedp (binfo, data)
2988 tree binfo;
2989 void *data ATTRIBUTE_UNUSED;
2990 {
2991 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2992 ? binfo : NULL_TREE);
2993 }
2994
2995 /* Write out the debugging information for TYPE, whose vtable is being
2996 emitted. Also walk through our bases and note that we want to
2997 write out information for them. This avoids the problem of not
2998 writing any debug info for intermediate basetypes whose
2999 constructors, and thus the references to their vtables, and thus
3000 the vtables themselves, were optimized away. */
3001
3002 void
3003 note_debug_info_needed (type)
3004 tree type;
3005 {
3006 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
3007 {
3008 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
3009 rest_of_type_compilation (type, toplevel_bindings_p ());
3010 }
3011
3012 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
3013 }
3014 \f
3015 /* Subroutines of push_class_decls (). */
3016
3017 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
3018 because it (or one of the intermediate bases) depends on template parms. */
3019
3020 static int
3021 dependent_base_p (binfo)
3022 tree binfo;
3023 {
3024 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
3025 {
3026 if (currently_open_class (TREE_TYPE (binfo)))
3027 break;
3028 if (uses_template_parms (TREE_TYPE (binfo)))
3029 return 1;
3030 }
3031 return 0;
3032 }
3033
3034 static void
3035 setup_class_bindings (name, type_binding_p)
3036 tree name;
3037 int type_binding_p;
3038 {
3039 tree type_binding = NULL_TREE;
3040 tree value_binding;
3041
3042 /* If we've already done the lookup for this declaration, we're
3043 done. */
3044 if (IDENTIFIER_CLASS_VALUE (name))
3045 return;
3046
3047 /* First, deal with the type binding. */
3048 if (type_binding_p)
3049 {
3050 type_binding = lookup_member (current_class_type, name,
3051 /*protect=*/2,
3052 /*want_type=*/1);
3053 if (TREE_CODE (type_binding) == TREE_LIST
3054 && TREE_TYPE (type_binding) == error_mark_node)
3055 /* NAME is ambiguous. */
3056 push_class_level_binding (name, type_binding);
3057 else
3058 pushdecl_class_level (type_binding);
3059 }
3060
3061 /* Now, do the value binding. */
3062 value_binding = lookup_member (current_class_type, name,
3063 /*protect=*/2,
3064 /*want_type=*/0);
3065
3066 if (type_binding_p
3067 && (TREE_CODE (value_binding) == TYPE_DECL
3068 || (TREE_CODE (value_binding) == TREE_LIST
3069 && TREE_TYPE (value_binding) == error_mark_node
3070 && (TREE_CODE (TREE_VALUE (value_binding))
3071 == TYPE_DECL))))
3072 /* We found a type-binding, even when looking for a non-type
3073 binding. This means that we already processed this binding
3074 above. */
3075 my_friendly_assert (type_binding_p, 19990401);
3076 else if (value_binding)
3077 {
3078 if (TREE_CODE (value_binding) == TREE_LIST
3079 && TREE_TYPE (value_binding) == error_mark_node)
3080 /* NAME is ambiguous. */
3081 push_class_level_binding (name, value_binding);
3082 else
3083 {
3084 if (BASELINK_P (value_binding))
3085 /* NAME is some overloaded functions. */
3086 value_binding = TREE_VALUE (value_binding);
3087 pushdecl_class_level (value_binding);
3088 }
3089 }
3090 }
3091
3092 /* Push class-level declarations for any names appearing in BINFO that
3093 are TYPE_DECLS. */
3094
3095 static tree
3096 dfs_push_type_decls (binfo, data)
3097 tree binfo;
3098 void *data ATTRIBUTE_UNUSED;
3099 {
3100 tree type;
3101 tree fields;
3102
3103 type = BINFO_TYPE (binfo);
3104 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3105 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
3106 && !(!same_type_p (type, current_class_type)
3107 && template_self_reference_p (type, fields)))
3108 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
3109
3110 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3111 DERIVED_FROM_P, which calls get_base_distance. */
3112 SET_BINFO_PUSHDECLS_MARKED (binfo);
3113
3114 return NULL_TREE;
3115 }
3116
3117 /* Push class-level declarations for any names appearing in BINFO that
3118 are not TYPE_DECLS. */
3119
3120 static tree
3121 dfs_push_decls (binfo, data)
3122 tree binfo;
3123 void *data;
3124 {
3125 tree type;
3126 tree method_vec;
3127 int dep_base_p;
3128
3129 type = BINFO_TYPE (binfo);
3130 dep_base_p = (processing_template_decl && type != current_class_type
3131 && dependent_base_p (binfo));
3132 if (!dep_base_p)
3133 {
3134 tree fields;
3135 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3136 if (DECL_NAME (fields)
3137 && TREE_CODE (fields) != TYPE_DECL
3138 && TREE_CODE (fields) != USING_DECL)
3139 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
3140 else if (TREE_CODE (fields) == FIELD_DECL
3141 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
3142 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
3143
3144 method_vec = (CLASS_TYPE_P (type)
3145 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
3146 if (method_vec)
3147 {
3148 tree *methods;
3149 tree *end;
3150
3151 /* Farm out constructors and destructors. */
3152 end = TREE_VEC_END (method_vec);
3153
3154 for (methods = &TREE_VEC_ELT (method_vec, 2);
3155 *methods && methods != end;
3156 methods++)
3157 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
3158 /*type_binding_p=*/0);
3159 }
3160 }
3161
3162 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
3163
3164 return NULL_TREE;
3165 }
3166
3167 /* When entering the scope of a class, we cache all of the
3168 fields that that class provides within its inheritance
3169 lattice. Where ambiguities result, we mark them
3170 with `error_mark_node' so that if they are encountered
3171 without explicit qualification, we can emit an error
3172 message. */
3173
3174 void
3175 push_class_decls (type)
3176 tree type;
3177 {
3178 search_stack = push_search_level (search_stack, &search_obstack);
3179
3180 /* Enter type declarations and mark. */
3181 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
3182
3183 /* Enter non-type declarations and unmark. */
3184 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
3185 }
3186
3187 /* Here's a subroutine we need because C lacks lambdas. */
3188
3189 static tree
3190 dfs_unuse_fields (binfo, data)
3191 tree binfo;
3192 void *data ATTRIBUTE_UNUSED;
3193 {
3194 tree type = TREE_TYPE (binfo);
3195 tree fields;
3196
3197 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3198 {
3199 if (TREE_CODE (fields) != FIELD_DECL)
3200 continue;
3201
3202 TREE_USED (fields) = 0;
3203 if (DECL_NAME (fields) == NULL_TREE
3204 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
3205 unuse_fields (TREE_TYPE (fields));
3206 }
3207
3208 return NULL_TREE;
3209 }
3210
3211 void
3212 unuse_fields (type)
3213 tree type;
3214 {
3215 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
3216 }
3217
3218 void
3219 pop_class_decls ()
3220 {
3221 /* We haven't pushed a search level when dealing with cached classes,
3222 so we'd better not try to pop it. */
3223 if (search_stack)
3224 search_stack = pop_search_level (search_stack);
3225 }
3226
3227 void
3228 print_search_statistics ()
3229 {
3230 #ifdef GATHER_STATISTICS
3231 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3232 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3233 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3234 n_outer_fields_searched, n_calls_lookup_fnfields);
3235 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
3236 #else /* GATHER_STATISTICS */
3237 fprintf (stderr, "no search statistics\n");
3238 #endif /* GATHER_STATISTICS */
3239 }
3240
3241 void
3242 init_search_processing ()
3243 {
3244 gcc_obstack_init (&search_obstack);
3245 }
3246
3247 void
3248 reinit_search_statistics ()
3249 {
3250 #ifdef GATHER_STATISTICS
3251 n_fields_searched = 0;
3252 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3253 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3254 n_calls_get_base_type = 0;
3255 n_outer_fields_searched = 0;
3256 n_contexts_saved = 0;
3257 #endif /* GATHER_STATISTICS */
3258 }
3259
3260 static tree
3261 add_conversions (binfo, data)
3262 tree binfo;
3263 void *data;
3264 {
3265 int i;
3266 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
3267 tree *conversions = (tree *) data;
3268
3269 /* Some builtin types have no method vector, not even an empty one. */
3270 if (!method_vec)
3271 return NULL_TREE;
3272
3273 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
3274 {
3275 tree tmp = TREE_VEC_ELT (method_vec, i);
3276 tree name;
3277
3278 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
3279 break;
3280
3281 name = DECL_NAME (OVL_CURRENT (tmp));
3282
3283 /* Make sure we don't already have this conversion. */
3284 if (! IDENTIFIER_MARKED (name))
3285 {
3286 *conversions = tree_cons (binfo, tmp, *conversions);
3287 IDENTIFIER_MARKED (name) = 1;
3288 }
3289 }
3290 return NULL_TREE;
3291 }
3292
3293 /* Return a TREE_LIST containing all the non-hidden user-defined
3294 conversion functions for TYPE (and its base-classes). The
3295 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
3296 containing the conversion functions. The TREE_PURPOSE is the BINFO
3297 from which the conversion functions in this node were selected. */
3298
3299 tree
3300 lookup_conversions (type)
3301 tree type;
3302 {
3303 tree t;
3304 tree conversions = NULL_TREE;
3305
3306 if (COMPLETE_TYPE_P (type))
3307 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
3308
3309 for (t = conversions; t; t = TREE_CHAIN (t))
3310 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
3311
3312 return conversions;
3313 }
3314
3315 struct overlap_info
3316 {
3317 tree compare_type;
3318 int found_overlap;
3319 };
3320
3321 /* Check whether the empty class indicated by EMPTY_BINFO is also present
3322 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3323
3324 static tree
3325 dfs_check_overlap (empty_binfo, data)
3326 tree empty_binfo;
3327 void *data;
3328 {
3329 struct overlap_info *oi = (struct overlap_info *) data;
3330 tree binfo;
3331 for (binfo = TYPE_BINFO (oi->compare_type);
3332 ;
3333 binfo = BINFO_BASETYPE (binfo, 0))
3334 {
3335 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
3336 {
3337 oi->found_overlap = 1;
3338 break;
3339 }
3340 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
3341 break;
3342 }
3343
3344 return NULL_TREE;
3345 }
3346
3347 /* Trivial function to stop base traversal when we find something. */
3348
3349 static tree
3350 dfs_no_overlap_yet (binfo, data)
3351 tree binfo;
3352 void *data;
3353 {
3354 struct overlap_info *oi = (struct overlap_info *) data;
3355 return !oi->found_overlap ? binfo : NULL_TREE;
3356 }
3357
3358 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3359 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3360
3361 int
3362 types_overlap_p (empty_type, next_type)
3363 tree empty_type, next_type;
3364 {
3365 struct overlap_info oi;
3366
3367 if (! IS_AGGR_TYPE (next_type))
3368 return 0;
3369 oi.compare_type = next_type;
3370 oi.found_overlap = 0;
3371 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
3372 dfs_no_overlap_yet, &oi);
3373 return oi.found_overlap;
3374 }
3375
3376 /* Given a vtable VAR, determine which binfo it comes from.
3377
3378 FIXME What about secondary vtables? */
3379
3380 tree
3381 binfo_for_vtable (var)
3382 tree var;
3383 {
3384 tree binfo = TYPE_BINFO (DECL_CONTEXT (var));
3385 tree binfos;
3386 int i;
3387
3388 while (1)
3389 {
3390 binfos = BINFO_BASETYPES (binfo);
3391 if (binfos == NULL_TREE)
3392 break;
3393
3394 i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
3395 if (i == -1)
3396 break;
3397
3398 binfo = TREE_VEC_ELT (binfos, i);
3399 }
3400
3401 return binfo;
3402 }
3403
3404 /* Returns 1 iff BINFO is from a direct or indirect virtual base. */
3405
3406 int
3407 binfo_from_vbase (binfo)
3408 tree binfo;
3409 {
3410 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
3411 {
3412 if (TREE_VIA_VIRTUAL (binfo))
3413 return 1;
3414 }
3415 return 0;
3416 }
3417
3418 /* Returns the BINFO (if any) for the virtual baseclass T of the class
3419 C from the CLASSTYPE_VBASECLASSES list. */
3420
3421 tree
3422 binfo_for_vbase (basetype, classtype)
3423 tree basetype;
3424 tree classtype;
3425 {
3426 tree binfo;
3427
3428 binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
3429 return binfo ? TREE_VALUE (binfo) : NULL_TREE;
3430 }