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