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