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