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