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