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