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