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