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