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