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