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