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