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