]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/search.c
Obvious, micro-change: "Vladimir Markarov" -> "Vladimir Makarov".
[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++.
fed3cef0 3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d6a8bdff 4 1999, 2000 Free Software Foundation, Inc.
8d08fdba
MS
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
14GNU CC is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
21the Free Software Foundation, 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA. */
8d08fdba 23
e92cc029 24/* High-level class interface. */
8d08fdba
MS
25
26#include "config.h"
8d052bc7 27#include "system.h"
e7a587ef 28#include "tree.h"
8d08fdba
MS
29#include "cp-tree.h"
30#include "obstack.h"
31#include "flags.h"
43f2999d 32#include "rtl.h"
e8abc66f 33#include "output.h"
54f92bfb 34#include "toplev.h"
8d08fdba
MS
35
36#define obstack_chunk_alloc xmalloc
37#define obstack_chunk_free free
38
8d08fdba
MS
39extern struct obstack *current_obstack;
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
158991b7
KG
79static tree next_baselink PARAMS ((tree));
80static tree get_vbase_1 PARAMS ((tree, tree, unsigned int *));
81static tree lookup_field_1 PARAMS ((tree, tree));
82static tree convert_pointer_to_single_level PARAMS ((tree, tree));
83static int lookup_fnfields_here PARAMS ((tree, tree));
84static int is_subobject_of_p PARAMS ((tree, tree));
85static int hides PARAMS ((tree, tree));
86static tree virtual_context PARAMS ((tree, tree, tree));
87static tree dfs_check_overlap PARAMS ((tree, void *));
88static tree dfs_no_overlap_yet PARAMS ((tree, void *));
49c249e1 89static int get_base_distance_recursive
158991b7 90 PARAMS ((tree, int, int, int, int *, tree *, tree,
49c249e1 91 int, int *, int, int));
158991b7 92static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
49c249e1 93static void expand_upcast_fixups
158991b7 94 PARAMS ((tree, tree, tree, tree, tree, tree, tree *));
49c249e1 95static void fixup_virtual_upcast_offsets
158991b7 96 PARAMS ((tree, tree, int, int, tree, tree, tree, tree,
49c249e1 97 tree *));
158991b7
KG
98static tree marked_vtable_pathp PARAMS ((tree, void *));
99static tree unmarked_vtable_pathp PARAMS ((tree, void *));
100static tree marked_new_vtablep PARAMS ((tree, void *));
101static tree unmarked_new_vtablep PARAMS ((tree, void *));
102static tree marked_pushdecls_p PARAMS ((tree, void *));
103static tree unmarked_pushdecls_p PARAMS ((tree, void *));
ae673f14 104#if 0
158991b7
KG
105static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
106static tree dfs_debug_mark PARAMS ((tree, void *));
ae673f14 107#endif
158991b7
KG
108static tree dfs_find_vbases PARAMS ((tree, void *));
109static tree dfs_clear_vbase_slots PARAMS ((tree, void *));
110static tree dfs_init_vbase_pointers PARAMS ((tree, void *));
111static tree dfs_get_vbase_types PARAMS ((tree, void *));
112static tree dfs_push_type_decls PARAMS ((tree, void *));
113static tree dfs_push_decls PARAMS ((tree, void *));
114static tree dfs_unuse_fields PARAMS ((tree, void *));
115static tree add_conversions PARAMS ((tree, void *));
116static tree get_virtuals_named_this PARAMS ((tree, tree));
117static tree get_virtual_destructor PARAMS ((tree, void *));
118static tree tree_has_any_destructor_p PARAMS ((tree, void *));
119static int covariant_return_p PARAMS ((tree, tree));
120static int check_final_overrider PARAMS ((tree, tree));
49c249e1 121static struct search_level *push_search_level
158991b7 122 PARAMS ((struct stack_level *, struct obstack *));
49c249e1 123static struct search_level *pop_search_level
158991b7 124 PARAMS ((struct stack_level *));
d6479fe7 125static tree bfs_walk
158991b7 126 PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
d6479fe7 127 void *));
158991b7
KG
128static tree lookup_field_queue_p PARAMS ((tree, void *));
129static tree lookup_field_r PARAMS ((tree, void *));
130static tree get_virtuals_named_this_r PARAMS ((tree, void *));
131static tree context_for_name_lookup PARAMS ((tree));
132static tree canonical_binfo PARAMS ((tree));
133static tree shared_marked_p PARAMS ((tree, void *));
134static tree shared_unmarked_p PARAMS ((tree, void *));
135static int dependent_base_p PARAMS ((tree));
136static tree dfs_accessible_queue_p PARAMS ((tree, void *));
137static tree dfs_accessible_p PARAMS ((tree, void *));
138static tree dfs_access_in_type PARAMS ((tree, void *));
139static tree access_in_type PARAMS ((tree, tree));
140static tree dfs_canonical_queue PARAMS ((tree, void *));
141static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
142static void assert_canonical_unmarked PARAMS ((tree));
143static int protected_accessible_p PARAMS ((tree, tree, tree, tree));
144static int friend_accessible_p PARAMS ((tree, tree, tree, tree));
145static void setup_class_bindings PARAMS ((tree, int));
146static int template_self_reference_p PARAMS ((tree, tree));
147static void fixup_all_virtual_upcast_offsets PARAMS ((tree, tree));
148static tree dfs_mark_primary_bases PARAMS ((tree, void *));
149static tree get_shared_vbase_if_not_primary PARAMS ((tree, void *));
150static tree dfs_find_vbase_instance PARAMS ((tree, void *));
70a51bda 151static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
8d08fdba
MS
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
8d08fdba 177/* Variables for gathering statistics. */
5566b478 178#ifdef GATHER_STATISTICS
8d08fdba
MS
179static int n_fields_searched;
180static int n_calls_lookup_field, n_calls_lookup_field_1;
181static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
182static int n_calls_get_base_type;
183static int n_outer_fields_searched;
184static int n_contexts_saved;
fc378698 185#endif /* GATHER_STATISTICS */
8d08fdba 186
8d08fdba 187\f
acc9fe20
RK
188/* Get a virtual binfo that is found inside BINFO's hierarchy that is
189 the same type as the type given in PARENT. To be optimal, we want
190 the first one that is found by going through the least number of
514a1f18
JM
191 virtual bases.
192
193 This uses a clever algorithm that updates *depth when we find the vbase,
194 and cuts off other paths of search when they reach that depth. */
e92cc029 195
acc9fe20 196static tree
bd6dd845 197get_vbase_1 (parent, binfo, depth)
acc9fe20
RK
198 tree parent, binfo;
199 unsigned int *depth;
200{
201 tree binfos;
202 int i, n_baselinks;
203 tree rval = NULL_TREE;
204
acc9fe20
RK
205 if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo))
206 {
207 *depth = 0;
208 return binfo;
209 }
210
211 *depth = *depth - 1;
212
213 binfos = BINFO_BASETYPES (binfo);
214 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
215
216 /* Process base types. */
217 for (i = 0; i < n_baselinks; i++)
218 {
219 tree base_binfo = TREE_VEC_ELT (binfos, i);
220 tree nrval;
221
222 if (*depth == 0)
223 break;
224
bd6dd845 225 nrval = get_vbase_1 (parent, base_binfo, depth);
acc9fe20
RK
226 if (nrval)
227 rval = nrval;
228 }
229 *depth = *depth+1;
230 return rval;
231}
232
514a1f18
JM
233/* Return the shortest path to vbase PARENT within BINFO, ignoring
234 access and ambiguity. */
235
bd6dd845
MS
236tree
237get_vbase (parent, binfo)
238 tree parent;
239 tree binfo;
240{
241 unsigned int d = (unsigned int)-1;
242 return get_vbase_1 (parent, binfo, &d);
243}
244
f30432d7
MS
245/* Convert EXPR to a virtual base class of type TYPE. We know that
246 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
247 the type of what expr points to has a virtual base of type TYPE. */
e92cc029 248
d569399b 249tree
f30432d7
MS
250convert_pointer_to_vbase (type, expr)
251 tree type;
252 tree expr;
253{
bd6dd845 254 tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))));
f30432d7
MS
255 return convert_pointer_to_real (vb, expr);
256}
257
8d08fdba
MS
258/* Check whether the type given in BINFO is derived from PARENT. If
259 it isn't, return 0. If it is, but the derivation is MI-ambiguous
260 AND protect != 0, emit an error message and return error_mark_node.
261
262 Otherwise, if TYPE is derived from PARENT, return the actual base
263 information, unless a one of the protection violations below
264 occurs, in which case emit an error message and return error_mark_node.
265
266 If PROTECT is 1, then check if access to a public field of PARENT
267 would be private. Also check for ambiguity. */
268
269tree
270get_binfo (parent, binfo, protect)
271 register tree parent, binfo;
272 int protect;
273{
a703fb38 274 tree type = NULL_TREE;
8d08fdba
MS
275 int dist;
276 tree rval = NULL_TREE;
277
278 if (TREE_CODE (parent) == TREE_VEC)
279 parent = BINFO_TYPE (parent);
71851aaa 280 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
8d08fdba
MS
281 my_friendly_abort (89);
282
283 if (TREE_CODE (binfo) == TREE_VEC)
284 type = BINFO_TYPE (binfo);
71851aaa 285 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
8d08fdba
MS
286 type = binfo;
287 else
288 my_friendly_abort (90);
289
290 dist = get_base_distance (parent, binfo, protect, &rval);
291
292 if (dist == -3)
293 {
8251199e 294 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
8d08fdba
MS
295 parent, type);
296 return error_mark_node;
297 }
298 else if (dist == -2 && protect)
299 {
8251199e 300 cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
8d08fdba
MS
301 type);
302 return error_mark_node;
303 }
304
305 return rval;
306}
307
308/* This is the newer depth first get_base_distance routine. */
e92cc029 309
8926095f 310static int
5566b478 311get_base_distance_recursive (binfo, depth, is_private, rval,
514a1f18 312 rval_private_ptr, new_binfo_ptr, parent,
6b5fbb55
MS
313 protect, via_virtual_ptr, via_virtual,
314 current_scope_in_chain)
315 tree binfo;
316 int depth, is_private, rval;
317 int *rval_private_ptr;
514a1f18 318 tree *new_binfo_ptr, parent;
6b5fbb55
MS
319 int protect, *via_virtual_ptr, via_virtual;
320 int current_scope_in_chain;
8d08fdba
MS
321{
322 tree binfos;
323 int i, n_baselinks;
324
6b5fbb55
MS
325 if (protect
326 && !current_scope_in_chain
327 && is_friend (BINFO_TYPE (binfo), current_scope ()))
328 current_scope_in_chain = 1;
329
8d08fdba
MS
330 if (BINFO_TYPE (binfo) == parent || binfo == parent)
331 {
514a1f18
JM
332 int better = 0;
333
8d08fdba 334 if (rval == -1)
514a1f18
JM
335 /* This is the first time we've found parent. */
336 better = 1;
337 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
338 BINFO_OFFSET (binfo))
339 && *via_virtual_ptr && via_virtual)
340 {
341 /* A new path to the same vbase. If this one has better
342 access or is shorter, take it. */
343
344 if (protect)
345 better = *rval_private_ptr - is_private;
346 if (better == 0)
347 better = rval - depth;
348 }
349 else
350 {
351 /* Ambiguous base class. */
352 rval = depth = -2;
353
354 /* If we get an ambiguity between virtual and non-virtual base
355 class, return the non-virtual in case we are ignoring
356 ambiguity. */
357 better = *via_virtual_ptr - via_virtual;
358 }
359
360 if (better > 0)
8d08fdba
MS
361 {
362 rval = depth;
363 *rval_private_ptr = is_private;
364 *new_binfo_ptr = binfo;
365 *via_virtual_ptr = via_virtual;
366 }
514a1f18 367
8d08fdba
MS
368 return rval;
369 }
370
371 binfos = BINFO_BASETYPES (binfo);
372 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
373 depth += 1;
374
375 /* Process base types. */
376 for (i = 0; i < n_baselinks; i++)
377 {
378 tree base_binfo = TREE_VEC_ELT (binfos, i);
379
514a1f18
JM
380 int via_private
381 = (protect
382 && (is_private
383 || (!TREE_VIA_PUBLIC (base_binfo)
384 && !(TREE_VIA_PROTECTED (base_binfo)
385 && current_scope_in_chain)
386 && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
387 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
388
389 rval = get_base_distance_recursive (base_binfo, depth, via_private,
390 rval, rval_private_ptr,
391 new_binfo_ptr, parent,
392 protect, via_virtual_ptr,
393 this_virtual,
394 current_scope_in_chain);
395
396 /* If we've found a non-virtual, ambiguous base class, we don't need
397 to keep searching. */
398 if (rval == -2 && *via_virtual_ptr == 0)
399 return rval;
8d08fdba
MS
400 }
401
402 return rval;
403}
404
405/* Return the number of levels between type PARENT and the type given
406 in BINFO, following the leftmost path to PARENT not found along a
407 virtual path, if there are no real PARENTs (all come from virtual
514a1f18 408 base classes), then follow the shortest public path to PARENT.
8d08fdba
MS
409
410 Return -1 if TYPE is not derived from PARENT.
411 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
412 non-negative.
413 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
414
415 If PATH_PTR is non-NULL, then also build the list of types
ddd5a7c1 416 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
8d08fdba
MS
417 set.
418
419 PARENT can also be a binfo, in which case that exact parent is found
420 and no other. convert_pointer_to_real uses this functionality.
421
39211cd5 422 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
8d08fdba
MS
423
424int
425get_base_distance (parent, binfo, protect, path_ptr)
426 register tree parent, binfo;
427 int protect;
428 tree *path_ptr;
429{
8d08fdba 430 int rval;
8d08fdba 431 int rval_private = 0;
a703fb38 432 tree type = NULL_TREE;
8d08fdba
MS
433 tree new_binfo = NULL_TREE;
434 int via_virtual;
435 int watch_access = protect;
436
5566b478 437 /* Should we be completing types here? */
8d08fdba 438 if (TREE_CODE (parent) != TREE_VEC)
5566b478
MS
439 parent = complete_type (TYPE_MAIN_VARIANT (parent));
440 else
441 complete_type (TREE_TYPE (parent));
8d08fdba
MS
442
443 if (TREE_CODE (binfo) == TREE_VEC)
444 type = BINFO_TYPE (binfo);
f0e01782 445 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
8d08fdba 446 {
e92cc029 447 type = complete_type (binfo);
8d08fdba
MS
448 binfo = TYPE_BINFO (type);
449
450 if (path_ptr)
dfbcd65a
JM
451 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE,
452 980827);
8d08fdba
MS
453 }
454 else
455 my_friendly_abort (92);
456
457 if (parent == type || parent == binfo)
458 {
459 /* If the distance is 0, then we don't really need
460 a path pointer, but we shouldn't let garbage go back. */
461 if (path_ptr)
462 *path_ptr = binfo;
463 return 0;
464 }
465
466 if (path_ptr)
467 watch_access = 1;
468
5566b478 469 rval = get_base_distance_recursive (binfo, 0, 0, -1,
8d08fdba 470 &rval_private, &new_binfo, parent,
514a1f18 471 watch_access, &via_virtual, 0,
6b5fbb55 472 0);
8d08fdba 473
8d08fdba
MS
474 /* Access restrictions don't count if we found an ambiguous basetype. */
475 if (rval == -2 && protect >= 0)
476 rval_private = 0;
477
478 if (rval && protect && rval_private)
479 return -3;
480
514a1f18 481 /* If they gave us the real vbase binfo, which isn't in the main binfo
18141e4c
JM
482 tree, deal with it. This happens when we are called from
483 expand_upcast_fixups. */
39211cd5 484 if (rval == -1 && TREE_CODE (parent) == TREE_VEC
23381155 485 && parent == BINFO_FOR_VBASE (BINFO_TYPE (parent), type))
39211cd5 486 {
dfbcd65a 487 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent) == binfo, 980827);
39211cd5
MS
488 new_binfo = parent;
489 rval = 1;
490 }
491
8d08fdba
MS
492 if (path_ptr)
493 *path_ptr = new_binfo;
494 return rval;
495}
496
4a9e5c67
NS
497/* Worker function for get_dynamic_cast_base_type. */
498
499static int
500dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
501 tree subtype;
502 tree binfo;
503 int via_virtual;
504 tree *offset_ptr;
505{
506 tree binfos;
507 int i, n_baselinks;
f08dda39 508 int worst = -2;
4a9e5c67
NS
509
510 if (BINFO_TYPE (binfo) == subtype)
511 {
512 if (via_virtual)
f08dda39 513 return -1;
4a9e5c67
NS
514 else
515 {
516 *offset_ptr = BINFO_OFFSET (binfo);
517 return 0;
518 }
519 }
520
521 binfos = BINFO_BASETYPES (binfo);
522 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
523 for (i = 0; i < n_baselinks; i++)
524 {
525 tree base_binfo = TREE_VEC_ELT (binfos, i);
526 int rval;
527
528 if (!TREE_VIA_PUBLIC (base_binfo))
529 continue;
530 rval = dynamic_cast_base_recurse
531 (subtype, base_binfo,
532 via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
f08dda39 533 if (worst == -2)
4a9e5c67
NS
534 worst = rval;
535 else if (rval >= 0)
f08dda39
NS
536 worst = worst >= 0 ? -3 : worst;
537 else if (rval == -1)
538 worst = -1;
539 else if (rval == -3 && worst != -1)
540 worst = -3;
4a9e5c67
NS
541 }
542 return worst;
543}
544
f08dda39
NS
545/* The dynamic cast runtime needs a hint about how the static SUBTYPE type
546 started from is related to the required TARGET type, in order to optimize
547 the inheritance graph search. This information is independant of the
4a9e5c67
NS
548 current context, and ignores private paths, hence get_base_distance is
549 inappropriate. Return a TREE specifying the base offset, BOFF.
550 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
551 and there are no public virtual SUBTYPE bases.
f08dda39
NS
552 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
553 BOFF == -2, SUBTYPE is not a public base.
554 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
4a9e5c67
NS
555
556tree
557get_dynamic_cast_base_type (subtype, target)
558 tree subtype;
559 tree target;
560{
561 tree offset = NULL_TREE;
562 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
563 0, &offset);
564
565 if (!boff)
566 return offset;
567 return build_int_2 (boff, -1);
568}
569
8d08fdba
MS
570/* Search for a member with name NAME in a multiple inheritance lattice
571 specified by TYPE. If it does not exist, return NULL_TREE.
572 If the member is ambiguously referenced, return `error_mark_node'.
573 Otherwise, return the FIELD_DECL. */
574
575/* Do a 1-level search for NAME as a member of TYPE. The caller must
576 figure out whether it can access this field. (Since it is only one
577 level, this is reasonable.) */
e92cc029 578
8d08fdba
MS
579static tree
580lookup_field_1 (type, name)
581 tree type, name;
582{
f84b4be9
JM
583 register tree field;
584
585 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
586 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
587 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
588 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
589 the code often worked even when we treated the index as a list
590 of fields!) */
591 return NULL_TREE;
592
f90cdf34
MT
593 if (TYPE_NAME (type)
594 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
595 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
596 {
597 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
598 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
599 int i;
600
601 while (lo < hi)
602 {
603 i = (lo + hi) / 2;
604
605#ifdef GATHER_STATISTICS
606 n_fields_searched++;
607#endif /* GATHER_STATISTICS */
608
609 if (DECL_NAME (fields[i]) > name)
610 hi = i;
611 else if (DECL_NAME (fields[i]) < name)
612 lo = i + 1;
613 else
bff3ce71
JM
614 {
615 /* We might have a nested class and a field with the
616 same name; we sorted them appropriately via
617 field_decl_cmp, so just look for the last field with
618 this name. */
619 while (i + 1 < hi
620 && DECL_NAME (fields[i+1]) == name)
621 ++i;
622 return fields[i];
623 }
f90cdf34
MT
624 }
625 return NULL_TREE;
626 }
627
f84b4be9 628 field = TYPE_FIELDS (type);
8d08fdba
MS
629
630#ifdef GATHER_STATISTICS
631 n_calls_lookup_field_1++;
fc378698 632#endif /* GATHER_STATISTICS */
8d08fdba
MS
633 while (field)
634 {
635#ifdef GATHER_STATISTICS
636 n_fields_searched++;
fc378698 637#endif /* GATHER_STATISTICS */
f84b4be9 638 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field)) == 'd', 0);
8d08fdba 639 if (DECL_NAME (field) == NULL_TREE
6bdb8141 640 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8d08fdba
MS
641 {
642 tree temp = lookup_field_1 (TREE_TYPE (field), name);
643 if (temp)
644 return temp;
645 }
2036a15c
MM
646 if (TREE_CODE (field) == USING_DECL)
647 /* For now, we're just treating member using declarations as
648 old ARM-style access declarations. Thus, there's no reason
649 to return a USING_DECL, and the rest of the compiler can't
650 handle it. Once the class is defined, these are purged
651 from TYPE_FIELDS anyhow; see handle_using_decl. */
652 ;
653 else if (DECL_NAME (field) == name)
8d08fdba
MS
654 {
655 if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
656 && DECL_ASSEMBLER_NAME (field) != NULL)
657 GNU_xref_ref(current_function_decl,
658 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
659 return field;
660 }
661 field = TREE_CHAIN (field);
662 }
663 /* Not found. */
9cd64686 664 if (name == vptr_identifier)
8d08fdba
MS
665 {
666 /* Give the user what s/he thinks s/he wants. */
4c6b7393 667 if (TYPE_POLYMORPHIC_P (type))
d3a3fb6a 668 return TYPE_VFIELD (type);
8d08fdba
MS
669 }
670 return NULL_TREE;
671}
672
7177d104
MS
673/* There are a number of cases we need to be aware of here:
674 current_class_type current_function_decl
e92cc029
MS
675 global NULL NULL
676 fn-local NULL SET
677 class-local SET NULL
678 class->fn SET SET
679 fn->class SET SET
7177d104
MS
680
681 Those last two make life interesting. If we're in a function which is
682 itself inside a class, we need decls to go into the fn's decls (our
683 second case below). But if we're in a class and the class itself is
684 inside a function, we need decls to go into the decls for the class. To
4ac14744 685 achieve this last goal, we must see if, when both current_class_ptr and
7177d104
MS
686 current_function_decl are set, the class was declared inside that
687 function. If so, we know to put the decls into the class's scope. */
688
8d08fdba
MS
689tree
690current_scope ()
691{
692 if (current_function_decl == NULL_TREE)
693 return current_class_type;
694 if (current_class_type == NULL_TREE)
695 return current_function_decl;
4f1c5b7d
MM
696 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
697 && same_type_p (DECL_CONTEXT (current_function_decl),
698 current_class_type))
699 || (DECL_FRIEND_CONTEXT (current_function_decl)
700 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
701 current_class_type)))
8d08fdba
MS
702 return current_function_decl;
703
704 return current_class_type;
705}
706
9188c363
MM
707/* Returns non-zero if we are currently in a function scope. Note
708 that this function returns zero if we are within a local class, but
709 not within a member function body of the local class. */
710
711int
712at_function_scope_p ()
713{
714 tree cs = current_scope ();
715 return cs && TREE_CODE (cs) == FUNCTION_DECL;
716}
717
d6479fe7 718/* Return the scope of DECL, as appropriate when doing name-lookup. */
8d08fdba 719
d6479fe7
MM
720static tree
721context_for_name_lookup (decl)
722 tree decl;
723{
724 /* [class.union]
725
726 For the purposes of name lookup, after the anonymous union
727 definition, the members of the anonymous union are considered to
728 have been defined in the scope in which teh anonymous union is
729 declared. */
4f1c5b7d 730 tree context = CP_DECL_CONTEXT (decl);
d6479fe7 731
6bdb8141 732 while (TYPE_P (context) && ANON_AGGR_TYPE_P (context))
d6479fe7
MM
733 context = TYPE_CONTEXT (context);
734 if (!context)
735 context = global_namespace;
8d08fdba 736
d6479fe7
MM
737 return context;
738}
8d08fdba 739
d6479fe7
MM
740/* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
741 otherwise. */
8d08fdba 742
d6479fe7
MM
743static tree
744canonical_binfo (binfo)
745 tree binfo;
746{
747 return (TREE_VIA_VIRTUAL (binfo)
748 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
749}
8d08fdba 750
6cbd257e
MM
751/* A queue function that simply ensures that we walk into the
752 canonical versions of virtual bases. */
753
754static tree
755dfs_canonical_queue (binfo, data)
756 tree binfo;
757 void *data ATTRIBUTE_UNUSED;
758{
759 return canonical_binfo (binfo);
760}
761
762/* Called via dfs_walk from assert_canonical_unmarked. */
763
764static tree
765dfs_assert_unmarked_p (binfo, data)
766 tree binfo;
767 void *data ATTRIBUTE_UNUSED;
768{
769 my_friendly_assert (!BINFO_MARKED (binfo), 0);
770 return NULL_TREE;
771}
772
773/* Asserts that all the nodes below BINFO (using the canonical
774 versions of virtual bases) are unmarked. */
775
776static void
777assert_canonical_unmarked (binfo)
778 tree binfo;
779{
780 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
781}
782
d6479fe7
MM
783/* If BINFO is marked, return a canonical version of BINFO.
784 Otherwise, return NULL_TREE. */
8d08fdba 785
d6479fe7
MM
786static tree
787shared_marked_p (binfo, data)
788 tree binfo;
789 void *data;
790{
791 binfo = canonical_binfo (binfo);
8026246f 792 return markedp (binfo, data);
d6479fe7 793}
8d08fdba 794
d6479fe7
MM
795/* If BINFO is not marked, return a canonical version of BINFO.
796 Otherwise, return NULL_TREE. */
8d08fdba 797
d6479fe7
MM
798static tree
799shared_unmarked_p (binfo, data)
800 tree binfo;
801 void *data;
8d08fdba 802{
d6479fe7 803 binfo = canonical_binfo (binfo);
8026246f 804 return unmarkedp (binfo, data);
d6479fe7 805}
8d08fdba 806
d6479fe7
MM
807/* Called from access_in_type via dfs_walk. Calculate the access to
808 DATA (which is really a DECL) in BINFO. */
eae89e04 809
d6479fe7
MM
810static tree
811dfs_access_in_type (binfo, data)
812 tree binfo;
813 void *data;
814{
815 tree decl = (tree) data;
816 tree type = BINFO_TYPE (binfo);
817 tree access = NULL_TREE;
8d08fdba 818
d6479fe7 819 if (context_for_name_lookup (decl) == type)
8d08fdba 820 {
d6479fe7
MM
821 /* If we have desceneded to the scope of DECL, just note the
822 appropriate access. */
823 if (TREE_PRIVATE (decl))
824 access = access_private_node;
825 else if (TREE_PROTECTED (decl))
826 access = access_protected_node;
827 else
828 access = access_public_node;
8d08fdba 829 }
d6479fe7
MM
830 else
831 {
832 /* First, check for an access-declaration that gives us more
833 access to the DECL. The CONST_DECL for an enumeration
834 constant will not have DECL_LANG_SPECIFIC, and thus no
835 DECL_ACCESS. */
836 if (DECL_LANG_SPECIFIC (decl))
837 {
838 access = purpose_member (type, DECL_ACCESS (decl));
839 if (access)
840 access = TREE_VALUE (access);
841 }
842
843 if (!access)
844 {
845 int i;
846 int n_baselinks;
847 tree binfos;
848
849 /* Otherwise, scan our baseclasses, and pick the most favorable
850 access. */
851 binfos = BINFO_BASETYPES (binfo);
852 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
853 for (i = 0; i < n_baselinks; ++i)
854 {
855 tree base_binfo = TREE_VEC_ELT (binfos, i);
856 tree base_access = TREE_CHAIN (canonical_binfo (base_binfo));
857
858 if (!base_access || base_access == access_private_node)
859 /* If it was not accessible in the base, or only
860 accessible as a private member, we can't access it
861 all. */
862 base_access = NULL_TREE;
863 else if (TREE_VIA_PROTECTED (base_binfo))
864 /* Public and protected members in the base are
865 protected here. */
866 base_access = access_protected_node;
867 else if (!TREE_VIA_PUBLIC (base_binfo))
868 /* Public and protected members in the base are
869 private here. */
870 base_access = access_private_node;
871
872 /* See if the new access, via this base, gives more
873 access than our previous best access. */
874 if (base_access &&
875 (base_access == access_public_node
876 || (base_access == access_protected_node
877 && access != access_public_node)
878 || (base_access == access_private_node
879 && !access)))
880 {
881 access = base_access;
8d08fdba 882
d6479fe7
MM
883 /* If the new access is public, we can't do better. */
884 if (access == access_public_node)
885 break;
886 }
887 }
888 }
889 }
faae18ab 890
d6479fe7
MM
891 /* Note the access to DECL in TYPE. */
892 TREE_CHAIN (binfo) = access;
02020185 893
d6479fe7
MM
894 /* Mark TYPE as visited so that if we reach it again we do not
895 duplicate our efforts here. */
896 SET_BINFO_MARKED (binfo);
8d08fdba 897
d6479fe7
MM
898 return NULL_TREE;
899}
8d08fdba 900
d6479fe7 901/* Return the access to DECL in TYPE. */
8d08fdba 902
d6479fe7
MM
903static tree
904access_in_type (type, decl)
905 tree type;
906 tree decl;
907{
908 tree binfo = TYPE_BINFO (type);
8d08fdba 909
d6479fe7 910 /* We must take into account
8d08fdba 911
d6479fe7 912 [class.paths]
8d08fdba 913
d6479fe7
MM
914 If a name can be reached by several paths through a multiple
915 inheritance graph, the access is that of the path that gives
916 most access.
8d08fdba 917
d6479fe7
MM
918 The algorithm we use is to make a post-order depth-first traversal
919 of the base-class hierarchy. As we come up the tree, we annotate
920 each node with the most lenient access. */
921 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
922 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
6cbd257e 923 assert_canonical_unmarked (binfo);
8d08fdba 924
d6479fe7
MM
925 return TREE_CHAIN (binfo);
926}
927
928/* Called from dfs_accessible_p via dfs_walk. */
929
930static tree
931dfs_accessible_queue_p (binfo, data)
932 tree binfo;
933 void *data ATTRIBUTE_UNUSED;
934{
935 if (BINFO_MARKED (binfo))
936 return NULL_TREE;
937
938 /* If this class is inherited via private or protected inheritance,
939 then we can't see it, unless we are a friend of the subclass. */
940 if (!TREE_VIA_PUBLIC (binfo)
941 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
942 current_scope ()))
943 return NULL_TREE;
944
945 return canonical_binfo (binfo);
946}
947
948/* Called from dfs_accessible_p via dfs_walk. */
949
950static tree
951dfs_accessible_p (binfo, data)
952 tree binfo;
953 void *data;
954{
955 int protected_ok = data != 0;
956 tree access;
957
958 /* We marked the binfos while computing the access in each type.
959 So, we unmark as we go now. */
960 SET_BINFO_MARKED (binfo);
961
962 access = TREE_CHAIN (binfo);
963 if (access == access_public_node
964 || (access == access_protected_node && protected_ok))
965 return binfo;
966 else if (access && is_friend (BINFO_TYPE (binfo), current_scope ()))
967 return binfo;
968
969 return NULL_TREE;
970}
971
6a629cac
MM
972/* Returns non-zero if it is OK to access DECL when named in TYPE
973 through an object indiated by BINFO in the context of DERIVED. */
974
975static int
976protected_accessible_p (type, decl, derived, binfo)
977 tree type;
978 tree decl;
979 tree derived;
980 tree binfo;
981{
982 tree access;
983
984 /* We're checking this clause from [class.access.base]
985
986 m as a member of N is protected, and the reference occurs in a
987 member or friend of class N, or in a member or friend of a
988 class P derived from N, where m as a member of P is private or
989 protected.
990
991 If DERIVED isn't derived from TYPE, then it certainly does not
992 apply. */
993 if (!DERIVED_FROM_P (type, derived))
994 return 0;
995
996 access = access_in_type (derived, decl);
997 if (same_type_p (derived, type))
998 {
999 if (access != access_private_node)
1000 return 0;
1001 }
1002 else if (access != access_private_node
1003 && access != access_protected_node)
1004 return 0;
1005
1006 /* [class.protected]
1007
1008 When a friend or a member function of a derived class references
1009 a protected nonstatic member of a base class, an access check
1010 applies in addition to those described earlier in clause
1011 _class.access_.4) Except when forming a pointer to member
1012 (_expr.unary.op_), the access must be through a pointer to,
1013 reference to, or object of the derived class itself (or any class
1014 derived from that class) (_expr.ref_). If the access is to form
1015 a pointer to member, the nested-name-specifier shall name the
1016 derived class (or any class derived from that class). */
1017 if (DECL_NONSTATIC_MEMBER_P (decl))
1018 {
1019 /* We can tell through what the reference is occurring by
1020 chasing BINFO up to the root. */
1021 tree t = binfo;
1022 while (BINFO_INHERITANCE_CHAIN (t))
1023 t = BINFO_INHERITANCE_CHAIN (t);
1024
1025 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
1026 return 0;
1027 }
1028
1029 return 1;
1030}
1031
1032/* Returns non-zero if SCOPE is a friend of a type which would be able
1033 to acces DECL, named in TYPE, through the object indicated by
1034 BINFO. */
1035
1036static int
1037friend_accessible_p (scope, type, decl, binfo)
1038 tree scope;
1039 tree type;
1040 tree decl;
1041 tree binfo;
1042{
1043 tree befriending_classes;
1044 tree t;
1045
1046 if (!scope)
1047 return 0;
1048
1049 if (TREE_CODE (scope) == FUNCTION_DECL
1050 || DECL_FUNCTION_TEMPLATE_P (scope))
1051 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
1052 else if (TYPE_P (scope))
1053 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
1054 else
1055 return 0;
1056
1057 for (t = befriending_classes; t; t = TREE_CHAIN (t))
1058 if (protected_accessible_p (type, decl, TREE_VALUE (t), binfo))
1059 return 1;
1060
445ab443
JM
1061 /* Nested classes are implicitly friends of their enclosing types, as
1062 per core issue 45 (this is a change from the standard). */
1063 if (TYPE_P (scope))
1064 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
1065 if (protected_accessible_p (type, decl, t, binfo))
1066 return 1;
1067
6a629cac
MM
1068 if (TREE_CODE (scope) == FUNCTION_DECL
1069 || DECL_FUNCTION_TEMPLATE_P (scope))
1070 {
1071 /* Perhaps this SCOPE is a member of a class which is a
1072 friend. */
4f1c5b7d
MM
1073 if (DECL_CLASS_SCOPE_P (decl)
1074 && friend_accessible_p (DECL_CONTEXT (scope), type,
1075 decl, binfo))
6a629cac
MM
1076 return 1;
1077
1078 /* Or an instantiation of something which is a friend. */
1079 if (DECL_TEMPLATE_INFO (scope))
1080 return friend_accessible_p (DECL_TI_TEMPLATE (scope),
1081 type, decl, binfo);
1082 }
1083 else if (CLASSTYPE_TEMPLATE_INFO (scope))
1084 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope),
1085 type, decl, binfo);
1086
1087 return 0;
1088}
70adf8a9
JM
1089
1090/* Perform access control on TYPE_DECL VAL, which was looked up in TYPE.
1091 This is fairly complex, so here's the design:
1092
1093 The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
1094 start to process a top-level declaration.
1095 As we process the decl-specifier-seq for the declaration, any types we
1096 see that might need access control are passed to type_access_control,
1097 which defers checking by adding them to type_lookups.
1098 When we are done with the decl-specifier-seq, we record the lookups we've
1099 seen in the lookups field of the typed_declspecs nonterminal.
1100 When we process the first declarator, either in parse_decl or
1f51a992
JM
1101 begin_function_definition, we call save_type_access_control,
1102 which stores the lookups from the decl-specifier-seq in
1103 current_type_lookups.
1104 As we finish with each declarator, we process everything in type_lookups
1105 via decl_type_access_control, which resets type_lookups to the value of
1106 current_type_lookups for subsequent declarators.
1107 When we enter a function, we set type_lookups to error_mark_node, so all
1108 lookups are processed immediately. */
70adf8a9
JM
1109
1110void
1111type_access_control (type, val)
1112 tree type, val;
1113{
1114 if (val == NULL_TREE || TREE_CODE (val) != TYPE_DECL
1115 || ! DECL_CLASS_SCOPE_P (val))
1116 return;
1117
1118 if (type_lookups == error_mark_node)
1119 enforce_access (type, val);
1120 else if (! accessible_p (type, val))
1121 type_lookups = tree_cons (type, val, type_lookups);
1122}
1123
d6479fe7 1124/* DECL is a declaration from a base class of TYPE, which was the
70adf8a9 1125 class used to name DECL. Return non-zero if, in the current
d6479fe7 1126 context, DECL is accessible. If TYPE is actually a BINFO node,
8084bf81
MM
1127 then we can tell in what context the access is occurring by looking
1128 at the most derived class along the path indicated by BINFO. */
d6479fe7
MM
1129
1130int
1131accessible_p (type, decl)
1132 tree type;
1133 tree decl;
1134
1135{
d6479fe7
MM
1136 tree binfo;
1137 tree t;
1138
1139 /* Non-zero if it's OK to access DECL if it has protected
1140 accessibility in TYPE. */
1141 int protected_ok = 0;
1142
1143 /* If we're not checking access, everything is accessible. */
1144 if (!flag_access_control)
1145 return 1;
1146
1147 /* If this declaration is in a block or namespace scope, there's no
1148 access control. */
1149 if (!TYPE_P (context_for_name_lookup (decl)))
1150 return 1;
1151
d6479fe7
MM
1152 if (!TYPE_P (type))
1153 {
1154 binfo = type;
1155 type = BINFO_TYPE (type);
8d08fdba 1156 }
d6479fe7
MM
1157 else
1158 binfo = TYPE_BINFO (type);
1159
1160 /* [class.access.base]
1161
1162 A member m is accessible when named in class N if
1163
1164 --m as a member of N is public, or
8d08fdba 1165
d6479fe7
MM
1166 --m as a member of N is private, and the reference occurs in a
1167 member or friend of class N, or
8d08fdba 1168
d6479fe7
MM
1169 --m as a member of N is protected, and the reference occurs in a
1170 member or friend of class N, or in a member or friend of a
1171 class P derived from N, where m as a member of P is private or
1172 protected, or
1173
1174 --there exists a base class B of N that is accessible at the point
1175 of reference, and m is accessible when named in class B.
1176
1177 We walk the base class hierarchy, checking these conditions. */
1178
1179 /* Figure out where the reference is occurring. Check to see if
1180 DECL is private or protected in this scope, since that will
1181 determine whether protected access in TYPE allowed. */
6a629cac
MM
1182 if (current_class_type)
1183 protected_ok
1184 = protected_accessible_p (type, decl, current_class_type,
1185 binfo);
8d08fdba 1186
6a629cac
MM
1187 /* Now, loop through the classes of which we are a friend. */
1188 if (!protected_ok)
1189 protected_ok = friend_accessible_p (current_scope (),
1190 type, decl, binfo);
8d08fdba 1191
70adf8a9
JM
1192 /* Standardize the binfo that access_in_type will use. We don't
1193 need to know what path was chosen from this point onwards. */
d6479fe7
MM
1194 binfo = TYPE_BINFO (type);
1195
1196 /* Compute the accessibility of DECL in the class hierarchy
1197 dominated by type. */
1198 access_in_type (type, decl);
1199 /* Walk the hierarchy again, looking for a base class that allows
1200 access. */
1201 t = dfs_walk (binfo, dfs_accessible_p,
1202 dfs_accessible_queue_p,
1203 protected_ok ? &protected_ok : 0);
6cbd257e
MM
1204 /* Clear any mark bits. Note that we have to walk the whole tree
1205 here, since we have aborted the previous walk from some point
1206 deep in the tree. */
1207 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1208 assert_canonical_unmarked (binfo);
d6479fe7
MM
1209
1210 return t != NULL_TREE;
8d08fdba
MS
1211}
1212
1213/* Routine to see if the sub-object denoted by the binfo PARENT can be
1214 found as a base class and sub-object of the object denoted by
1215 BINFO. This routine relies upon binfos not being shared, except
1216 for binfos for virtual bases. */
e92cc029 1217
8d08fdba
MS
1218static int
1219is_subobject_of_p (parent, binfo)
1220 tree parent, binfo;
1221{
d2675b98
MM
1222 tree binfos;
1223 int i, n_baselinks;
8d08fdba 1224
86eab6f6
MM
1225 /* We want to canonicalize for comparison purposes. But, when we
1226 iterate through basetypes later, we want the binfos from the
1227 original hierarchy. That's why we have to calculate BINFOS
1228 first, and then canonicalize. */
1229 binfos = BINFO_BASETYPES (binfo);
d2675b98
MM
1230 parent = canonical_binfo (parent);
1231 binfo = canonical_binfo (binfo);
d6479fe7 1232
8d08fdba
MS
1233 if (parent == binfo)
1234 return 1;
1235
d2675b98
MM
1236 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1237
8d08fdba
MS
1238 /* Process and/or queue base types. */
1239 for (i = 0; i < n_baselinks; i++)
1240 {
d2675b98
MM
1241 tree base_binfo = TREE_VEC_ELT (binfos, i);
1242 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo)))
1243 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1244 class there's no way to descend into it. */
1245 continue;
1246
8d08fdba
MS
1247 if (is_subobject_of_p (parent, base_binfo))
1248 return 1;
1249 }
1250 return 0;
1251}
1252
1253/* See if a one FIELD_DECL hides another. This routine is meant to
1254 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1255 binfos given are the binfos corresponding to the particular places
1256 the FIELD_DECLs are found. This routine relies upon binfos not
e92cc029
MS
1257 being shared, except for virtual bases. */
1258
8d08fdba
MS
1259static int
1260hides (hider_binfo, hidee_binfo)
1261 tree hider_binfo, hidee_binfo;
1262{
1263 /* hider hides hidee, if hider has hidee as a base class and
1264 the instance of hidee is a sub-object of hider. The first
1265 part is always true is the second part is true.
1266
1267 When hider and hidee are the same (two ways to get to the exact
e92cc029 1268 same member) we consider either one as hiding the other. */
8d08fdba
MS
1269 return is_subobject_of_p (hidee_binfo, hider_binfo);
1270}
1271
1272/* Very similar to lookup_fnfields_1 but it ensures that at least one
1273 function was declared inside the class given by TYPE. It really should
1274 only return functions that match the given TYPE. */
e92cc029 1275
8d08fdba
MS
1276static int
1277lookup_fnfields_here (type, name)
1278 tree type, name;
1279{
e92cc029 1280 int idx = lookup_fnfields_1 (type, name);
8d08fdba
MS
1281 tree fndecls;
1282
fc378698 1283 /* ctors and dtors are always only in the right class. */
e92cc029
MS
1284 if (idx <= 1)
1285 return idx;
1286 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
8d08fdba
MS
1287 while (fndecls)
1288 {
4f1c5b7d 1289 if (TYPE_MAIN_VARIANT (DECL_CONTEXT (OVL_CURRENT (fndecls)))
8d08fdba 1290 == TYPE_MAIN_VARIANT (type))
e92cc029 1291 return idx;
2c73f9f5 1292 fndecls = OVL_CHAIN (fndecls);
8d08fdba
MS
1293 }
1294 return -1;
1295}
1296
7d4bdeed 1297struct lookup_field_info {
d6479fe7
MM
1298 /* The type in which we're looking. */
1299 tree type;
7d4bdeed
MM
1300 /* The name of the field for which we're looking. */
1301 tree name;
1302 /* If non-NULL, the current result of the lookup. */
1303 tree rval;
1304 /* The path to RVAL. */
1305 tree rval_binfo;
d6479fe7
MM
1306 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1307 candidates. */
7d4bdeed 1308 tree ambiguous;
7d4bdeed
MM
1309 /* If non-zero, we are looking for types, not data members. */
1310 int want_type;
d6479fe7
MM
1311 /* If non-zero, RVAL was found by looking through a dependent base. */
1312 int from_dep_base_p;
7d4bdeed 1313 /* If something went wrong, a message indicating what. */
d8e178a0 1314 const char *errstr;
7d4bdeed
MM
1315};
1316
1317/* Returns non-zero if BINFO is not hidden by the value found by the
1318 lookup so far. If BINFO is hidden, then there's no need to look in
1319 it. DATA is really a struct lookup_field_info. Called from
1320 lookup_field via breadth_first_search. */
1321
d6479fe7 1322static tree
7d4bdeed
MM
1323lookup_field_queue_p (binfo, data)
1324 tree binfo;
1325 void *data;
1326{
1327 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
d6479fe7
MM
1328
1329 /* Don't look for constructors or destructors in base classes. */
1330 if (lfi->name == ctor_identifier || lfi->name == dtor_identifier)
1331 return NULL_TREE;
1332
1333 /* If this base class is hidden by the best-known value so far, we
1334 don't need to look. */
1335 if (!lfi->from_dep_base_p && lfi->rval_binfo
1336 && hides (lfi->rval_binfo, binfo))
1337 return NULL_TREE;
1338
1339 if (TREE_VIA_VIRTUAL (binfo))
23381155 1340 return BINFO_FOR_VBASE (BINFO_TYPE (binfo), lfi->type);
d6479fe7
MM
1341 else
1342 return binfo;
7d4bdeed
MM
1343}
1344
9188c363
MM
1345/* Within the scope of a template class, you can refer to the to the
1346 current specialization with the name of the template itself. For
1347 example:
8f032717
MM
1348
1349 template <typename T> struct S { S* sp; }
1350
1351 Returns non-zero if DECL is such a declaration in a class TYPE. */
1352
1353static int
1354template_self_reference_p (type, decl)
1355 tree type;
1356 tree decl;
1357{
1358 return (CLASSTYPE_USE_TEMPLATE (type)
3fc5037b 1359 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
8f032717
MM
1360 && TREE_CODE (decl) == TYPE_DECL
1361 && DECL_ARTIFICIAL (decl)
1362 && DECL_NAME (decl) == constructor_name (type));
1363}
1364
7d4bdeed
MM
1365/* DATA is really a struct lookup_field_info. Look for a field with
1366 the name indicated there in BINFO. If this function returns a
1367 non-NULL value it is the result of the lookup. Called from
1368 lookup_field via breadth_first_search. */
1369
1370static tree
1371lookup_field_r (binfo, data)
1372 tree binfo;
1373 void *data;
1374{
1375 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1376 tree type = BINFO_TYPE (binfo);
4bb0968f 1377 tree nval = NULL_TREE;
d6479fe7 1378 int from_dep_base_p;
7d4bdeed 1379
d6479fe7
MM
1380 /* First, look for a function. There can't be a function and a data
1381 member with the same name, and if there's a function and a type
1382 with the same name, the type is hidden by the function. */
4bb0968f
MM
1383 if (!lfi->want_type)
1384 {
1385 int idx = lookup_fnfields_here (type, lfi->name);
1386 if (idx >= 0)
1387 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1388 }
1389
1390 if (!nval)
d6479fe7
MM
1391 /* Look for a data member or type. */
1392 nval = lookup_field_1 (type, lfi->name);
1393
1394 /* If there is no declaration with the indicated name in this type,
1395 then there's nothing to do. */
7d4bdeed 1396 if (!nval)
d6479fe7 1397 return NULL_TREE;
7d4bdeed 1398
4bb0968f
MM
1399 /* If we're looking up a type (as with an elaborated type specifier)
1400 we ignore all non-types we find. */
1401 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL)
1402 {
1403 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1404 if (nval)
1405 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1406 else
1407 return NULL_TREE;
1408 }
1409
8f032717
MM
1410 /* You must name a template base class with a template-id. */
1411 if (!same_type_p (type, lfi->type)
1412 && template_self_reference_p (type, nval))
1413 return NULL_TREE;
1414
d6479fe7
MM
1415 from_dep_base_p = dependent_base_p (binfo);
1416 if (lfi->from_dep_base_p && !from_dep_base_p)
1417 {
1418 /* If the new declaration is not found via a dependent base, and
1419 the old one was, then we must prefer the new one. We weren't
1420 really supposed to be able to find the old one, so we don't
1421 want to be affected by a specialization. Consider:
1422
1423 struct B { typedef int I; };
1424 template <typename T> struct D1 : virtual public B {};
1425 template <typename T> struct D :
1426 public D1, virtual pubic B { I i; };
1427
1428 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1429 D1 is specialized. */
1430 lfi->from_dep_base_p = 0;
1431 lfi->rval = NULL_TREE;
1432 lfi->rval_binfo = NULL_TREE;
1433 lfi->ambiguous = NULL_TREE;
1434 lfi->errstr = 0;
1435 }
1436 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1437 /* Similarly, if the old declaration was not found via a dependent
1438 base, and the new one is, ignore the new one. */
7d4bdeed
MM
1439 return NULL_TREE;
1440
1441 /* If the lookup already found a match, and the new value doesn't
1442 hide the old one, we might have an ambiguity. */
1443 if (lfi->rval_binfo && !hides (binfo, lfi->rval_binfo))
1444 {
d6479fe7 1445 if (nval == lfi->rval && SHARED_MEMBER_P (nval))
7d4bdeed
MM
1446 /* The two things are really the same. */
1447 ;
1448 else if (hides (lfi->rval_binfo, binfo))
1449 /* The previous value hides the new one. */
1450 ;
1451 else
1452 {
1453 /* We have a real ambiguity. We keep a chain of all the
1454 candidates. */
1455 if (!lfi->ambiguous && lfi->rval)
aa65d1a2
MM
1456 {
1457 /* This is the first time we noticed an ambiguity. Add
1458 what we previously thought was a reasonable candidate
1459 to the list. */
e1b3e07d 1460 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
aa65d1a2
MM
1461 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1462 }
1463
7d4bdeed 1464 /* Add the new value. */
e1b3e07d 1465 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
aa65d1a2 1466 TREE_TYPE (lfi->ambiguous) = error_mark_node;
7d4bdeed
MM
1467 lfi->errstr = "request for member `%D' is ambiguous";
1468 }
1469 }
1470 else
1471 {
4bb0968f
MM
1472 /* If the thing we're looking for is a virtual base class, then
1473 we know we've got what we want at this point; there's no way
1474 to get an ambiguity. */
1475 if (VBASE_NAME_P (lfi->name))
7d4bdeed 1476 {
4bb0968f
MM
1477 lfi->rval = nval;
1478 return nval;
7d4bdeed
MM
1479 }
1480
4bb0968f
MM
1481 if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
1482 /* We need to return a member template class so we can
1483 define partial specializations. Is there a better
1484 way? */
1485 && !DECL_CLASS_TEMPLATE_P (nval))
1486 /* The thing we're looking for isn't a type, so the implicit
1487 typename extension doesn't apply, so we just pretend we
1488 didn't find anything. */
1489 return NULL_TREE;
7d4bdeed 1490
d6479fe7
MM
1491 lfi->rval = nval;
1492 lfi->from_dep_base_p = from_dep_base_p;
7d4bdeed
MM
1493 lfi->rval_binfo = binfo;
1494 }
1495
d6479fe7 1496 return NULL_TREE;
7d4bdeed
MM
1497}
1498
d6479fe7 1499/* Look for a memer named NAME in an inheritance lattice dominated by
8f032717
MM
1500 XBASETYPE. PROTECT is 0 or two, we do not check access. If it is
1501 1, we enforce accessibility. If PROTECT is zero, then, for an
1502 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
aa65d1a2 1503 error message. If PROTECT is 2, we return a TREE_LIST whose
70adf8a9 1504 TREE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
8f032717 1505 ambiguous candidates.
8d08fdba 1506
8f032717
MM
1507 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1508 TYPE_DECL can be found return NULL_TREE. */
e92cc029 1509
8d08fdba 1510tree
d6479fe7 1511lookup_member (xbasetype, name, protect, want_type)
8d08fdba
MS
1512 register tree xbasetype, name;
1513 int protect, want_type;
1514{
7d4bdeed
MM
1515 tree rval, rval_binfo = NULL_TREE;
1516 tree type = NULL_TREE, basetype_path = NULL_TREE;
1517 struct lookup_field_info lfi;
8d08fdba
MS
1518
1519 /* rval_binfo is the binfo associated with the found member, note,
1520 this can be set with useful information, even when rval is not
1521 set, because it must deal with ALL members, not just non-function
1522 members. It is used for ambiguity checking and the hidden
1523 checks. Whereas rval is only set if a proper (not hidden)
1524 non-function member is found. */
1525
d8e178a0 1526 const char *errstr = 0;
8d08fdba 1527
de22184b
MS
1528 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1529 && IDENTIFIER_CLASS_VALUE (name))
1530 {
1531 tree field = IDENTIFIER_CLASS_VALUE (name);
1532 if (TREE_CODE (field) != FUNCTION_DECL
1533 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
aa65d1a2
MM
1534 /* We're in the scope of this class, and the value has already
1535 been looked up. Just return the cached value. */
de22184b
MS
1536 return field;
1537 }
1538
8d08fdba
MS
1539 if (TREE_CODE (xbasetype) == TREE_VEC)
1540 {
8d08fdba 1541 type = BINFO_TYPE (xbasetype);
39211cd5 1542 basetype_path = xbasetype;
8d08fdba
MS
1543 }
1544 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
39211cd5 1545 {
238109cd 1546 type = xbasetype;
5566b478 1547 basetype_path = TYPE_BINFO (type);
dfbcd65a
JM
1548 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1549 980827);
39211cd5 1550 }
238109cd
JM
1551 else
1552 my_friendly_abort (97);
1553
1554 complete_type (type);
8d08fdba 1555
8d08fdba
MS
1556#ifdef GATHER_STATISTICS
1557 n_calls_lookup_field++;
fc378698 1558#endif /* GATHER_STATISTICS */
8d08fdba 1559
d8e178a0 1560 bzero ((PTR) &lfi, sizeof (lfi));
d6479fe7 1561 lfi.type = type;
7d4bdeed 1562 lfi.name = name;
7d4bdeed 1563 lfi.want_type = want_type;
d6479fe7 1564 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
7d4bdeed
MM
1565 rval = lfi.rval;
1566 rval_binfo = lfi.rval_binfo;
1567 if (rval_binfo)
1568 type = BINFO_TYPE (rval_binfo);
1569 errstr = lfi.errstr;
1570
1571 /* If we are not interested in ambiguities, don't report them;
1572 just return NULL_TREE. */
1573 if (!protect && lfi.ambiguous)
1574 return NULL_TREE;
d6479fe7 1575
8f032717
MM
1576 if (protect == 2)
1577 {
1578 if (lfi.ambiguous)
aa65d1a2 1579 return lfi.ambiguous;
8f032717
MM
1580 else
1581 protect = 0;
1582 }
1583
d6479fe7
MM
1584 /* [class.access]
1585
1586 In the case of overloaded function names, access control is
1587 applied to the function selected by overloaded resolution. */
1588 if (rval && protect && !is_overloaded_fn (rval)
d6479fe7
MM
1589 && !enforce_access (xbasetype, rval))
1590 return error_mark_node;
9e9ff709 1591
8251199e 1592 if (errstr && protect)
8d08fdba 1593 {
8251199e 1594 cp_error (errstr, name, type);
7d4bdeed
MM
1595 if (lfi.ambiguous)
1596 print_candidates (lfi.ambiguous);
8d08fdba
MS
1597 rval = error_mark_node;
1598 }
b3709d9b 1599
d6479fe7
MM
1600 /* If the thing we found was found via the implicit typename
1601 extension, build the typename type. */
1602 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1603 rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
1604 name, name,
1605 TREE_TYPE (rval)));
1606
4bb0968f
MM
1607 if (rval && is_overloaded_fn (rval))
1608 {
aa52c1ff
JM
1609 /* Note that the binfo we put in the baselink is the binfo where
1610 we found the functions, which we need for overload
1611 resolution, but which should not be passed to enforce_access;
1612 rather, enforce_access wants a binfo which refers to the
1613 scope in which we started looking for the function. This
1614 will generally be the binfo passed into this function as
1615 xbasetype. */
1616
1617 rval = tree_cons (rval_binfo, rval, NULL_TREE);
4bb0968f
MM
1618 SET_BASELINK_P (rval);
1619 }
d6479fe7
MM
1620
1621 return rval;
1622}
1623
1624/* Like lookup_member, except that if we find a function member we
1625 return NULL_TREE. */
1626
1627tree
1628lookup_field (xbasetype, name, protect, want_type)
1629 register tree xbasetype, name;
1630 int protect, want_type;
1631{
1632 tree rval = lookup_member (xbasetype, name, protect, want_type);
1633
1634 /* Ignore functions. */
1635 if (rval && TREE_CODE (rval) == TREE_LIST)
1636 return NULL_TREE;
1637
1638 return rval;
1639}
1640
1641/* Like lookup_member, except that if we find a non-function member we
1642 return NULL_TREE. */
1643
1644tree
1645lookup_fnfields (xbasetype, name, protect)
1646 register tree xbasetype, name;
1647 int protect;
1648{
1649 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1650
1651 /* Ignore non-functions. */
1652 if (rval && TREE_CODE (rval) != TREE_LIST)
1653 return NULL_TREE;
1654
8d08fdba
MS
1655 return rval;
1656}
1657
8d08fdba
MS
1658/* TYPE is a class type. Return the index of the fields within
1659 the method vector with name NAME, or -1 is no such field exists. */
e92cc029 1660
03017874 1661int
8d08fdba
MS
1662lookup_fnfields_1 (type, name)
1663 tree type, name;
1664{
f90cdf34 1665 tree method_vec
7ddedda4 1666 = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE;
8d08fdba
MS
1667
1668 if (method_vec != 0)
1669 {
f90cdf34 1670 register int i;
8d08fdba 1671 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
f90cdf34
MT
1672 int len = TREE_VEC_LENGTH (method_vec);
1673 tree tmp;
8d08fdba
MS
1674
1675#ifdef GATHER_STATISTICS
1676 n_calls_lookup_fnfields_1++;
fc378698
MS
1677#endif /* GATHER_STATISTICS */
1678
1679 /* Constructors are first... */
f90cdf34
MT
1680 if (name == ctor_identifier)
1681 return methods[0] ? 0 : -1;
8d08fdba 1682
fc378698 1683 /* and destructors are second. */
f90cdf34
MT
1684 if (name == dtor_identifier)
1685 return methods[1] ? 1 : -1;
fc378698 1686
f90cdf34 1687 for (i = 2; i < len && methods[i]; ++i)
8d08fdba
MS
1688 {
1689#ifdef GATHER_STATISTICS
1690 n_outer_fields_searched++;
fc378698 1691#endif /* GATHER_STATISTICS */
f90cdf34
MT
1692
1693 tmp = OVL_CURRENT (methods[i]);
1694 if (DECL_NAME (tmp) == name)
1695 return i;
1696
1697 /* If the type is complete and we're past the conversion ops,
1698 switch to binary search. */
1699 if (! DECL_CONV_FN_P (tmp)
1700 && TYPE_SIZE (type))
1701 {
1702 int lo = i + 1, hi = len;
1703
1704 while (lo < hi)
1705 {
1706 i = (lo + hi) / 2;
1707
1708#ifdef GATHER_STATISTICS
1709 n_outer_fields_searched++;
1710#endif /* GATHER_STATISTICS */
1711
1712 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1713
1714 if (tmp > name)
1715 hi = i;
1716 else if (tmp < name)
1717 lo = i + 1;
1718 else
1719 return i;
1720 }
1721 break;
1722 }
8d08fdba 1723 }
98c1c668
JM
1724
1725 /* If we didn't find it, it might have been a template
1726 conversion operator. (Note that we don't look for this case
1727 above so that we will always find specializations first.) */
f90cdf34 1728 if (IDENTIFIER_TYPENAME_P (name))
98c1c668 1729 {
f90cdf34 1730 for (i = 2; i < len && methods[i]; ++i)
98c1c668 1731 {
f90cdf34
MT
1732 tmp = OVL_CURRENT (methods[i]);
1733 if (! DECL_CONV_FN_P (tmp))
61a127b3
MM
1734 {
1735 /* Since all conversion operators come first, we know
1736 there is no such operator. */
d6479fe7 1737 break;
8d08fdba 1738 }
f90cdf34
MT
1739 else if (TREE_CODE (tmp) == TEMPLATE_DECL)
1740 return i;
8d08fdba
MS
1741 }
1742 }
8d08fdba
MS
1743 }
1744
d6479fe7 1745 return -1;
d23a1bb1 1746}
8d08fdba 1747\f
d6479fe7
MM
1748/* Walk the class hierarchy dominated by TYPE. FN is called for each
1749 type in the hierarchy, in a breadth-first preorder traversal. .
1750 If it ever returns a non-NULL value, that value is immediately
1751 returned and the walk is terminated. At each node FN, is passed a
1752 BINFO indicating the path from the curently visited base-class to
1753 TYPE. The TREE_CHAINs of the BINFOs may be used for scratch space;
1754 they are otherwise unused. Before each base-class is walked QFN is
1755 called. If the value returned is non-zero, the base-class is
1756 walked; otherwise it is not. If QFN is NULL, it is treated as a
1757 function which always returns 1. Both FN and QFN are passed the
1758 DATA whenever they are called. */
8d08fdba 1759
72c4a2a6 1760static tree
d6479fe7 1761bfs_walk (binfo, fn, qfn, data)
8d08fdba 1762 tree binfo;
158991b7
KG
1763 tree (*fn) PARAMS ((tree, void *));
1764 tree (*qfn) PARAMS ((tree, void *));
7d4bdeed 1765 void *data;
8d08fdba 1766{
d6479fe7
MM
1767 size_t head;
1768 size_t tail;
72c4a2a6 1769 tree rval = NULL_TREE;
d6479fe7
MM
1770 /* An array of the base classes of BINFO. These will be built up in
1771 breadth-first order, except where QFN prunes the search. */
1772 varray_type bfs_bases;
8d08fdba 1773
d6479fe7
MM
1774 /* Start with enough room for ten base classes. That will be enough
1775 for most hierarchies. */
1776 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
8d08fdba 1777
d6479fe7
MM
1778 /* Put the first type into the stack. */
1779 VARRAY_TREE (bfs_bases, 0) = binfo;
1780 tail = 1;
72c4a2a6 1781
d6479fe7 1782 for (head = 0; head < tail; ++head)
8d08fdba 1783 {
8d08fdba 1784 int i;
d6479fe7
MM
1785 int n_baselinks;
1786 tree binfos;
8d08fdba 1787
7d4bdeed 1788 /* Pull the next type out of the queue. */
d6479fe7 1789 binfo = VARRAY_TREE (bfs_bases, head);
7d4bdeed
MM
1790
1791 /* If this is the one we're looking for, we're done. */
d6479fe7 1792 rval = (*fn) (binfo, data);
7d4bdeed
MM
1793 if (rval)
1794 break;
1795
1796 /* Queue up the base types. */
1797 binfos = BINFO_BASETYPES (binfo);
1798 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
8d08fdba
MS
1799 for (i = 0; i < n_baselinks; i++)
1800 {
1801 tree base_binfo = TREE_VEC_ELT (binfos, i);
1802
d6479fe7
MM
1803 if (qfn)
1804 base_binfo = (*qfn) (base_binfo, data);
7d4bdeed 1805
d6479fe7 1806 if (base_binfo)
8d08fdba 1807 {
d6479fe7
MM
1808 if (tail == VARRAY_SIZE (bfs_bases))
1809 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1810 VARRAY_TREE (bfs_bases, tail) = base_binfo;
72c4a2a6 1811 ++tail;
8d08fdba
MS
1812 }
1813 }
7d4bdeed 1814 }
8d08fdba 1815
d6479fe7
MM
1816 /* Clean up. */
1817 VARRAY_FREE (bfs_bases);
1818
1819 return rval;
1820}
1821
1822/* Exactly like bfs_walk, except that a depth-first traversal is
1823 performed, and PREFN is called in preorder, while POSTFN is called
1824 in postorder. */
1825
bbd15aac 1826tree
d6479fe7
MM
1827dfs_walk_real (binfo, prefn, postfn, qfn, data)
1828 tree binfo;
158991b7
KG
1829 tree (*prefn) PARAMS ((tree, void *));
1830 tree (*postfn) PARAMS ((tree, void *));
1831 tree (*qfn) PARAMS ((tree, void *));
d6479fe7
MM
1832 void *data;
1833{
1834 int i;
1835 int n_baselinks;
1836 tree binfos;
1837 tree rval = NULL_TREE;
1838
1839 /* Call the pre-order walking function. */
1840 if (prefn)
7d4bdeed 1841 {
d6479fe7
MM
1842 rval = (*prefn) (binfo, data);
1843 if (rval)
1844 return rval;
8d08fdba 1845 }
8d08fdba 1846
d6479fe7
MM
1847 /* Process the basetypes. */
1848 binfos = BINFO_BASETYPES (binfo);
1849 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1850 for (i = 0; i < n_baselinks; i++)
1851 {
1852 tree base_binfo = TREE_VEC_ELT (binfos, i);
1853
1854 if (qfn)
1855 base_binfo = (*qfn) (base_binfo, data);
1856
1857 if (base_binfo)
1858 {
1859 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1860 if (rval)
1861 return rval;
1862 }
1863 }
1864
1865 /* Call the post-order walking function. */
1866 if (postfn)
1867 rval = (*postfn) (binfo, data);
1868
8d08fdba
MS
1869 return rval;
1870}
1871
d6479fe7
MM
1872/* Exactly like bfs_walk, except that a depth-first post-order traversal is
1873 performed. */
1874
1875tree
1876dfs_walk (binfo, fn, qfn, data)
1877 tree binfo;
158991b7
KG
1878 tree (*fn) PARAMS ((tree, void *));
1879 tree (*qfn) PARAMS ((tree, void *));
d6479fe7
MM
1880 void *data;
1881{
1882 return dfs_walk_real (binfo, 0, fn, qfn, data);
1883}
1884
1885struct gvnt_info
1886{
1887 /* The name of the function we are looking for. */
1888 tree name;
1889 /* The overloaded functions we have found. */
1890 tree fields;
1891};
1892
1893/* Called from get_virtuals_named_this via bfs_walk. */
1894
1895static tree
1896get_virtuals_named_this_r (binfo, data)
1897 tree binfo;
1898 void *data;
1899{
1900 struct gvnt_info *gvnti = (struct gvnt_info *) data;
1901 tree type = BINFO_TYPE (binfo);
1902 int idx;
1903
1904 idx = lookup_fnfields_here (BINFO_TYPE (binfo), gvnti->name);
1905 if (idx >= 0)
1906 gvnti->fields
e1b3e07d
MM
1907 = tree_cons (binfo,
1908 TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx),
1909 gvnti->fields);
d6479fe7
MM
1910
1911 return NULL_TREE;
1912}
8d08fdba 1913
d6479fe7
MM
1914/* Return the virtual functions with the indicated NAME in the type
1915 indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE
1916 indicates the base class from which the TREE_VALUE (an OVERLOAD or
1917 just a FUNCTION_DECL) originated. */
8d08fdba
MS
1918
1919static tree
d6479fe7 1920get_virtuals_named_this (binfo, name)
8d08fdba 1921 tree binfo;
d6479fe7 1922 tree name;
8d08fdba 1923{
d6479fe7 1924 struct gvnt_info gvnti;
8d08fdba
MS
1925 tree fields;
1926
d6479fe7
MM
1927 gvnti.name = name;
1928 gvnti.fields = NULL_TREE;
8d08fdba 1929
d6479fe7 1930 bfs_walk (binfo, get_virtuals_named_this_r, 0, &gvnti);
8d08fdba
MS
1931
1932 /* Get to the function decls, and return the first virtual function
1933 with this name, if there is one. */
d6479fe7 1934 for (fields = gvnti.fields; fields; fields = next_baselink (fields))
8d08fdba
MS
1935 {
1936 tree fndecl;
1937
2c73f9f5
ML
1938 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl))
1939 if (DECL_VINDEX (OVL_CURRENT (fndecl)))
8d08fdba 1940 return fields;
8d08fdba
MS
1941 }
1942 return NULL_TREE;
1943}
1944
fc378698 1945static tree
7d4bdeed 1946get_virtual_destructor (binfo, data)
8d08fdba 1947 tree binfo;
d6479fe7 1948 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
1949{
1950 tree type = BINFO_TYPE (binfo);
8d08fdba 1951 if (TYPE_HAS_DESTRUCTOR (type)
fc378698
MS
1952 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1)))
1953 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1);
8d08fdba
MS
1954 return 0;
1955}
1956
d6479fe7 1957static tree
7d4bdeed 1958tree_has_any_destructor_p (binfo, data)
8d08fdba 1959 tree binfo;
d6479fe7 1960 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
1961{
1962 tree type = BINFO_TYPE (binfo);
d6479fe7 1963 return TYPE_NEEDS_DESTRUCTOR (type) ? binfo : NULL_TREE;
8d08fdba
MS
1964}
1965
cc600f33
JM
1966/* Returns > 0 if a function with type DRETTYPE overriding a function
1967 with type BRETTYPE is covariant, as defined in [class.virtual].
1968
1969 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1970 adjustment), or -1 if pedantically invalid covariance. */
1971
c6160f8f 1972static int
cc600f33
JM
1973covariant_return_p (brettype, drettype)
1974 tree brettype, drettype;
1975{
1976 tree binfo;
1977
1978 if (TREE_CODE (brettype) == FUNCTION_DECL
1979 || TREE_CODE (brettype) == THUNK_DECL)
1980 {
1981 brettype = TREE_TYPE (TREE_TYPE (brettype));
1982 drettype = TREE_TYPE (TREE_TYPE (drettype));
1983 }
1984 else if (TREE_CODE (brettype) == METHOD_TYPE)
1985 {
1986 brettype = TREE_TYPE (brettype);
1987 drettype = TREE_TYPE (drettype);
1988 }
1989
3bfdc719 1990 if (same_type_p (brettype, drettype))
cc600f33
JM
1991 return 0;
1992
1993 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1994 && (TREE_CODE (brettype) == POINTER_TYPE
1995 || TREE_CODE (brettype) == REFERENCE_TYPE)
91063b51 1996 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
cc600f33
JM
1997 return 0;
1998
1999 if (! can_convert (brettype, drettype))
2000 return 0;
2001
2002 brettype = TREE_TYPE (brettype);
2003 drettype = TREE_TYPE (drettype);
2004
2005 /* If not pedantic, allow any standard pointer conversion. */
2006 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
2007 return -1;
2008
e76e4a68
JM
2009 binfo = get_binfo (brettype, drettype, 1);
2010
2011 /* If we get an error_mark_node from get_binfo, it already complained,
2012 so let's just succeed. */
2013 if (binfo == error_mark_node)
2014 return 1;
cc600f33
JM
2015
2016 if (! BINFO_OFFSET_ZEROP (binfo) || TREE_VIA_VIRTUAL (binfo))
2017 return 2;
2018 return 1;
2019}
2020
4cc1d462
NS
2021/* Check that virtual overrider OVERRIDER is acceptable for base function
2022 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2023
9c0758dd 2024static int
4cc1d462
NS
2025check_final_overrider (overrider, basefn)
2026 tree overrider, basefn;
2027{
2028 tree over_type = TREE_TYPE (overrider);
2029 tree base_type = TREE_TYPE (basefn);
2030 tree over_return = TREE_TYPE (over_type);
2031 tree base_return = TREE_TYPE (base_type);
2032 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
2033 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
2034 int i;
2035
2036 if (same_type_p (base_return, over_return))
2037 /* OK */;
2038 else if ((i = covariant_return_p (base_return, over_return)))
2039 {
2040 if (i == 2)
2041 sorry ("adjusting pointers for covariant returns");
2042
2043 if (pedantic && i == -1)
2044 {
2045 cp_pedwarn_at ("invalid covariant return type for `virtual %#D'", overrider);
2046 cp_pedwarn_at (" overriding `virtual %#D' (must be pointer or reference to class)", basefn);
2047 }
2048 }
2049 else if (IS_AGGR_TYPE_2 (base_return, over_return)
2050 && same_or_base_type_p (base_return, over_return))
2051 {
2052 cp_error_at ("invalid covariant return type for `virtual %#D'", overrider);
2053 cp_error_at (" overriding `virtual %#D' (must use pointer or reference)", basefn);
2054 return 0;
2055 }
2056 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)) == NULL_TREE)
2057 {
2058 cp_error_at ("conflicting return type specified for `virtual %#D'", overrider);
2059 cp_error_at (" overriding `virtual %#D'", basefn);
2060 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
4f1c5b7d 2061 DECL_CONTEXT (overrider));
4cc1d462
NS
2062 return 0;
2063 }
2064
2065 /* Check throw specifier is subset. */
2066 /* XXX At the moment, punt on an overriding artificial function. We
2067 don't generate its exception specifier, so can't check it properly. */
2068 if (! DECL_ARTIFICIAL (overrider)
2069 && !comp_except_specs (base_throw, over_throw, 0))
2070 {
2071 cp_error_at ("looser throw specifier for `virtual %#F'", overrider);
2072 cp_error_at (" overriding `virtual %#F'", basefn);
2073 return 0;
2074 }
2075 return 1;
2076}
2077
7177d104
MS
2078/* Given a class type TYPE, and a function decl FNDECL, look for a
2079 virtual function in TYPE's hierarchy which FNDECL could match as a
2080 virtual function. It doesn't matter which one we find.
8d08fdba
MS
2081
2082 DTORP is nonzero if we are looking for a destructor. Destructors
2083 need special treatment because they do not match by name. */
e92cc029 2084
8d08fdba 2085tree
7177d104 2086get_matching_virtual (binfo, fndecl, dtorp)
8d08fdba
MS
2087 tree binfo, fndecl;
2088 int dtorp;
2089{
2090 tree tmp = NULL_TREE;
2091
5e795528
MM
2092 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
2093 /* In [temp.mem] we have:
2094
2095 A specialization of a member function template does not
2096 override a virtual function from a base class. */
2097 return NULL_TREE;
2098
8d08fdba
MS
2099 /* Breadth first search routines start searching basetypes
2100 of TYPE, so we must perform first ply of search here. */
2101 if (dtorp)
d6479fe7
MM
2102 return bfs_walk (binfo, get_virtual_destructor,
2103 tree_has_any_destructor_p, 0);
8d08fdba
MS
2104 else
2105 {
2106 tree drettype, dtypes, btypes, instptr_type;
8d08fdba 2107 tree baselink, best = NULL_TREE;
d6479fe7 2108 tree declarator = DECL_NAME (fndecl);
8d08fdba
MS
2109 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
2110 return NULL_TREE;
2111
d6479fe7 2112 baselink = get_virtuals_named_this (binfo, declarator);
a292b002
MS
2113 if (baselink == NULL_TREE)
2114 return NULL_TREE;
2115
8d08fdba
MS
2116 drettype = TREE_TYPE (TREE_TYPE (fndecl));
2117 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2118 if (DECL_STATIC_FUNCTION_P (fndecl))
2119 instptr_type = NULL_TREE;
2120 else
2121 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
2122
a292b002 2123 for (; baselink; baselink = next_baselink (baselink))
8d08fdba 2124 {
2c73f9f5
ML
2125 tree tmps;
2126 for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps))
8d08fdba 2127 {
2c73f9f5 2128 tmp = OVL_CURRENT (tmps);
8d08fdba
MS
2129 if (! DECL_VINDEX (tmp))
2130 continue;
2131
2132 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
2133 if (instptr_type == NULL_TREE)
2134 {
91063b51 2135 if (compparms (TREE_CHAIN (btypes), dtypes))
8d08fdba
MS
2136 /* Caller knows to give error in this case. */
2137 return tmp;
2138 return NULL_TREE;
2139 }
2140
91063b51
MM
2141 if (/* The first parameter is the `this' parameter,
2142 which has POINTER_TYPE, and we can therefore
2143 safely use TYPE_QUALS, rather than
2144 CP_TYPE_QUALS. */
2145 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes)))
2146 == TYPE_QUALS (instptr_type))
2147 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes)))
8d08fdba 2148 {
4cc1d462 2149 check_final_overrider (fndecl, tmp);
ed70c426
MM
2150
2151 /* FNDECL overrides this function. We continue to
2152 check all the other functions in order to catch
2153 errors; it might be that in some other baseclass
2154 a virtual function was declared with the same
2155 parameter types, but a different return type. */
2156 best = tmp;
8d08fdba
MS
2157 }
2158 }
8d08fdba 2159 }
8d08fdba 2160
8d08fdba
MS
2161 return best;
2162 }
2163}
2164
174eceea
MM
2165/* A queue function for dfs_walk that skips any nonprimary virtual
2166 bases and any already marked bases. */
2167
2168tree
2169dfs_skip_nonprimary_vbases_unmarkedp (binfo, data)
2170 tree binfo;
2171 void *data ATTRIBUTE_UNUSED;
2172{
2173 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
2174 /* This is a non-primary virtual base. SKip it. */
2175 return NULL_TREE;
2176
2177 return unmarkedp (binfo, NULL);
2178}
2179
2180/* A queue function for dfs_walk that skips any nonprimary virtual
2181 bases and any unmarked bases. */
2182
2183tree
2184dfs_skip_nonprimary_vbases_markedp (binfo, data)
2185 tree binfo;
2186 void *data ATTRIBUTE_UNUSED;
2187{
2188 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
2189 /* This is a non-primary virtual base. SKip it. */
2190 return NULL_TREE;
2191
2192 return markedp (binfo, NULL);
2193}
2194
80fd5f48 2195/* Called via dfs_walk from mark_primary_bases. */
e92cc029 2196
8926095f 2197static tree
99a6c6f4 2198dfs_mark_primary_bases (binfo, data)
6b5fbb55 2199 tree binfo;
174eceea 2200 void *data;
8d08fdba 2201{
174eceea
MM
2202 int i;
2203 tree base_binfo;
2204
2205 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
2206 return NULL_TREE;
2207
2208 i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2209 base_binfo = BINFO_BASETYPE (binfo, i);
2210
2211 if (!TREE_VIA_VIRTUAL (base_binfo))
2212 /* Non-virtual base classes are easy. */
2213 BINFO_PRIMARY_MARKED_P (base_binfo) = 1;
2214 else
8d08fdba 2215 {
174eceea 2216 tree shared_binfo;
99a6c6f4 2217
174eceea
MM
2218 shared_binfo
2219 = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), (tree) data);
99a6c6f4 2220
174eceea
MM
2221 /* If this virtual base is not already primary somewhere else in
2222 the hiearchy, then we'll be using this copy. */
2223 if (!BINFO_VBASE_PRIMARY_P (shared_binfo)
2224 && !BINFO_VBASE_MARKED (shared_binfo))
2225 {
2226 BINFO_VBASE_PRIMARY_P (shared_binfo) = 1;
2227 BINFO_PRIMARY_MARKED_P (base_binfo) = 1;
2228 }
2229 }
99a6c6f4
MM
2230
2231 return NULL_TREE;
2232}
2233
2234/* Set BINFO_PRIMARY_MARKED_P for all binfos in the hierarchy
8026246f 2235 dominated by BINFO that are primary bases. */
99a6c6f4
MM
2236
2237void
2238mark_primary_bases (type)
2239 tree type;
2240{
174eceea
MM
2241 tree vbase;
2242
dd42e135
MM
2243 /* Mark the TYPE_BINFO hierarchy. We need to mark primary bases in
2244 pre-order to deal with primary virtual bases. (The virtual base
2245 would be skipped if it were not marked as primary, and that
2246 requires getting to dfs_mark_primary_bases before
2247 dfs_skip_nonprimary_vbases_unmarkedp has a chance to skip the
2248 virtual base.) */
2249 dfs_walk_real (TYPE_BINFO (type), dfs_mark_primary_bases, NULL,
2250 dfs_skip_nonprimary_vbases_unmarkedp, type);
174eceea
MM
2251
2252 /* Now go through the virtual base classes. Any that are not
2253 already primary will need to be allocated in TYPE, and so we need
2254 to mark their primary bases. */
2255 for (vbase = CLASSTYPE_VBASECLASSES (type);
2256 vbase;
2257 vbase = TREE_CHAIN (vbase))
2258 {
2259 if (BINFO_VBASE_PRIMARY_P (vbase))
2260 /* This virtual base was already included in the hierarchy, so
2261 there's nothing to do here. */
2262 continue;
2263
2264 /* Temporarily pretend that VBASE is primary so that its bases
2265 will be walked; this is the real copy of VBASE. */
2266 BINFO_PRIMARY_MARKED_P (vbase) = 1;
2267
2268 /* Now, walk its bases. */
2269 dfs_walk (vbase, dfs_mark_primary_bases,
2270 dfs_skip_nonprimary_vbases_unmarkedp, type);
2271
2272 /* VBASE wasn't really primary. */
2273 BINFO_PRIMARY_MARKED_P (vbase) = 0;
2274 /* And we don't want to allow it to *become* primary if it is a
2275 base of some subsequent base class. */
2276 SET_BINFO_VBASE_MARKED (vbase);
2277 }
2278
2279 /* Clear the VBASE_MARKED bits we set above. */
2280 for (vbase = CLASSTYPE_VBASECLASSES (type);
2281 vbase;
2282 vbase = TREE_CHAIN (vbase))
2283 CLEAR_BINFO_VBASE_MARKED (vbase);
2284}
2285
2286/* If BINFO is a non-primary virtual baseclass (in the hierarchy
2287 dominated by TYPE), and no primary copy appears anywhere in the
2288 hierarchy, return the shared copy. If a primary copy appears
2289 elsewhere, return NULL_TREE. Otherwise, return BINFO itself; it is
2290 either a non-virtual base or a primary virtual base. */
2291
2292static tree
70ae3201 2293get_shared_vbase_if_not_primary (binfo, data)
174eceea 2294 tree binfo;
70ae3201 2295 void *data;
174eceea
MM
2296{
2297 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
2298 {
70ae3201
MM
2299 tree type = (tree) data;
2300
2301 if (TREE_CODE (type) == TREE_LIST)
2302 type = TREE_PURPOSE (type);
2303
174eceea
MM
2304 /* This is a non-primary virtual base. If there is no primary
2305 version, get the shared version. */
2306 binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), type);
2307 if (BINFO_VBASE_PRIMARY_P (binfo))
2308 return NULL_TREE;
2309 }
2310
2311 return binfo;
2312}
2313
2314/* A queue function to use with dfs_walk that prevents travel into any
2315 nonprimary virtual base, or its baseclasses. DATA should be the
2316 type of the complete object, or a TREE_LIST whose TREE_PURPOSE is
2317 the type of the complete object. By using this function as a queue
2318 function, you will walk over exactly those BINFOs that actually
2319 exist in the complete object, including those for virtual base
2320 classes. If you SET_BINFO_MARKED for each binfo you process, you
2321 are further guaranteed that you will walk into each virtual base
2322 class exactly once. */
2323
2324tree
2325dfs_unmarked_real_bases_queue_p (binfo, data)
2326 tree binfo;
2327 void *data;
2328{
70ae3201 2329 binfo = get_shared_vbase_if_not_primary (binfo, data);
174eceea
MM
2330 return binfo ? unmarkedp (binfo, NULL) : NULL_TREE;
2331}
2332
2333/* Like dfs_unmarked_real_bases_queue_p but walks only into things
2334 that are marked, rather than unmarked. */
2335
2336tree
2337dfs_marked_real_bases_queue_p (binfo, data)
2338 tree binfo;
2339 void *data;
2340{
70ae3201 2341 binfo = get_shared_vbase_if_not_primary (binfo, data);
174eceea 2342 return binfo ? markedp (binfo, NULL) : NULL_TREE;
99a6c6f4
MM
2343}
2344
70ae3201
MM
2345/* Like dfs_unmarked_real_bases_queue_p but walks only into things
2346 that are not BINFO_VTABLE_PATH_MARKED. */
2347
2348tree
2349dfs_vtable_path_unmarked_real_bases_queue_p (binfo, data)
2350 tree binfo;
2351 void *data;
2352{
2353 binfo = get_shared_vbase_if_not_primary (binfo, data);
2354 return binfo ? unmarked_vtable_pathp (binfo, NULL): NULL_TREE;
2355}
2356
2357/* Like dfs_unmarked_real_bases_queue_p but walks only into things
2358 that are BINFO_VTABLE_PATH_MARKED. */
2359
2360tree
2361dfs_vtable_path_marked_real_bases_queue_p (binfo, data)
2362 tree binfo;
2363 void *data;
2364{
2365 binfo = get_shared_vbase_if_not_primary (binfo, data);
2366 return binfo ? marked_vtable_pathp (binfo, NULL): NULL_TREE;
2367}
2368
dd42e135
MM
2369/* A queue function that skips all virtual bases (and their
2370 bases). */
2371
2372tree
2373dfs_skip_vbases (binfo, data)
2374 tree binfo;
2375 void *data ATTRIBUTE_UNUSED;
2376{
2377 if (TREE_VIA_VIRTUAL (binfo))
2378 return NULL_TREE;
2379
2380 return binfo;
2381}
2382
99a6c6f4
MM
2383/* Called via dfs_walk from dfs_get_pure_virtuals. */
2384
2385static tree
2386dfs_get_pure_virtuals (binfo, data)
2387 tree binfo;
2388 void *data;
2389{
174eceea
MM
2390 tree type = (tree) data;
2391
99a6c6f4
MM
2392 /* We're not interested in primary base classes; the derived class
2393 of which they are a primary base will contain the information we
2394 need. */
2395 if (!BINFO_PRIMARY_MARKED_P (binfo))
8926095f 2396 {
07b7a812 2397 tree virtuals;
99a6c6f4 2398
174eceea
MM
2399 for (virtuals = skip_rtti_stuff (binfo,
2400 BINFO_TYPE (binfo),
99a6c6f4
MM
2401 NULL);
2402 virtuals;
2403 virtuals = TREE_CHAIN (virtuals))
2404 if (DECL_PURE_VIRTUAL_P (TREE_VALUE (virtuals)))
2405 CLASSTYPE_PURE_VIRTUALS (type)
2406 = tree_cons (NULL_TREE, TREE_VALUE (virtuals),
2407 CLASSTYPE_PURE_VIRTUALS (type));
2408 }
8026246f
MM
2409
2410 SET_BINFO_MARKED (binfo);
8d08fdba 2411
99a6c6f4 2412 return NULL_TREE;
8926095f
MS
2413}
2414
fee7654e 2415/* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
e92cc029 2416
fee7654e
MM
2417void
2418get_pure_virtuals (type)
8926095f
MS
2419 tree type;
2420{
f30432d7 2421 tree vbases;
8926095f 2422
99a6c6f4
MM
2423 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2424 is going to be overridden. */
2425 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
99a6c6f4
MM
2426 /* Now, run through all the bases which are not primary bases, and
2427 collect the pure virtual functions. We look at the vtable in
2428 each class to determine what pure virtual functions are present.
2429 (A primary base is not interesting because the derived class of
2430 which it is a primary base will contain vtable entries for the
2431 pure virtuals in the base class. */
174eceea
MM
2432 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2433 dfs_unmarked_real_bases_queue_p, type);
2434 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2435 dfs_marked_real_bases_queue_p, type);
8026246f 2436
99a6c6f4
MM
2437 /* Put the pure virtuals in dfs order. */
2438 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2439
174eceea
MM
2440 for (vbases = CLASSTYPE_VBASECLASSES (type);
2441 vbases;
2442 vbases = TREE_CHAIN (vbases))
8d08fdba 2443 {
174eceea 2444 tree virtuals;
8d08fdba 2445
174eceea
MM
2446 for (virtuals = skip_rtti_stuff (vbases, BINFO_TYPE (vbases), NULL);
2447 virtuals;
2448 virtuals = TREE_CHAIN (virtuals))
8d08fdba 2449 {
83f2ccf4 2450 tree base_fndecl = TREE_VALUE (virtuals);
8ebeee52
JM
2451 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2452 cp_error ("`%#D' needs a final overrider", base_fndecl);
8d08fdba
MS
2453 }
2454 }
8d08fdba
MS
2455}
2456
9e0781b5 2457static tree
8d08fdba
MS
2458next_baselink (baselink)
2459 tree baselink;
2460{
2461 tree tmp = TREE_TYPE (baselink);
2462 baselink = TREE_CHAIN (baselink);
2463 while (tmp)
2464 {
2465 /* @@ does not yet add previous base types. */
2466 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2467 baselink);
2468 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2469 tmp = TREE_CHAIN (tmp);
2470 }
2471 return baselink;
2472}
2473\f
2474/* DEPTH-FIRST SEARCH ROUTINES. */
2475
8d08fdba
MS
2476/* This routine converts a pointer to be a pointer of an immediate
2477 base class. The normal convert_pointer_to routine would diagnose
2478 the conversion as ambiguous, under MI code that has the base class
e92cc029
MS
2479 as an ambiguous base class. */
2480
8d08fdba
MS
2481static tree
2482convert_pointer_to_single_level (to_type, expr)
2483 tree to_type, expr;
2484{
bfec1a84 2485 tree derived;
8d08fdba 2486 tree binfo_of_derived;
bfec1a84 2487 int i;
8d08fdba 2488
bfec1a84
MM
2489 derived = TREE_TYPE (TREE_TYPE (expr));
2490 binfo_of_derived = TYPE_BINFO (derived);
dfbcd65a
JM
2491 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived) == NULL_TREE,
2492 980827);
bfec1a84
MM
2493 for (i = CLASSTYPE_N_BASECLASSES (derived) - 1; i >= 0; --i)
2494 {
2495 tree binfo = BINFO_BASETYPE (binfo_of_derived, i);
2496 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == binfo_of_derived,
2497 980827);
2498 if (same_type_p (BINFO_TYPE (binfo), to_type))
2499 return build_vbase_path (PLUS_EXPR,
2500 build_pointer_type (to_type),
2501 expr, binfo, 1);
2502 }
2503
2504 my_friendly_abort (19990607);
2505
2506 /* NOTREACHED */
2507 return NULL_TREE;
8d08fdba
MS
2508}
2509
8026246f
MM
2510tree
2511markedp (binfo, data)
d6479fe7
MM
2512 tree binfo;
2513 void *data ATTRIBUTE_UNUSED;
2514{
2515 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2516}
2517
8026246f 2518tree
d6479fe7
MM
2519unmarkedp (binfo, data)
2520 tree binfo;
2521 void *data ATTRIBUTE_UNUSED;
2522{
2523 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2524}
5566b478 2525
d6479fe7
MM
2526static tree
2527marked_vtable_pathp (binfo, data)
2528 tree binfo;
2529 void *data ATTRIBUTE_UNUSED;
2530{
2531 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2532}
2533
2534static tree
2535unmarked_vtable_pathp (binfo, data)
2536 tree binfo;
2537 void *data ATTRIBUTE_UNUSED;
2538{
2539 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2540}
2541
2542static tree
2543marked_new_vtablep (binfo, data)
2544 tree binfo;
2545 void *data ATTRIBUTE_UNUSED;
2546{
2547 return BINFO_NEW_VTABLE_MARKED (binfo) ? binfo : NULL_TREE;
2548}
2549
2550static tree
2551unmarked_new_vtablep (binfo, data)
2552 tree binfo;
2553 void *data ATTRIBUTE_UNUSED;
2554{
2555 return !BINFO_NEW_VTABLE_MARKED (binfo) ? binfo : NULL_TREE;
2556}
2557
2558static tree
2559marked_pushdecls_p (binfo, data)
2560 tree binfo;
2561 void *data ATTRIBUTE_UNUSED;
2562{
856216bb
MM
2563 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2564 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
d6479fe7 2565}
5566b478 2566
d6479fe7
MM
2567static tree
2568unmarked_pushdecls_p (binfo, data)
2569 tree binfo;
2570 void *data ATTRIBUTE_UNUSED;
2571{
856216bb
MM
2572 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2573 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
d6479fe7 2574}
8d08fdba 2575
5566b478 2576#if 0
8d08fdba
MS
2577static int dfs_search_slot_nonempty_p (binfo) tree binfo;
2578{ return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
2579
d6479fe7
MM
2580static tree
2581dfs_debug_unmarkedp (binfo, data)
2582 tree binfo;
2583 void *data ATTRIBUTE_UNUSED;
2584{
2585 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2586 ? binfo : NULL_TREE);
2587}
ae673f14 2588#endif
8d08fdba
MS
2589
2590/* The worker functions for `dfs_walk'. These do not need to
2591 test anything (vis a vis marking) if they are paired with
2592 a predicate function (above). */
2593
5566b478 2594#if 0
8d08fdba
MS
2595static void
2596dfs_mark (binfo) tree binfo;
2597{ SET_BINFO_MARKED (binfo); }
5566b478 2598#endif
8d08fdba 2599
d6479fe7
MM
2600tree
2601dfs_unmark (binfo, data)
2602 tree binfo;
2603 void *data ATTRIBUTE_UNUSED;
2604{
2605 CLEAR_BINFO_MARKED (binfo);
2606 return NULL_TREE;
2607}
8d08fdba 2608
8026246f
MM
2609/* Clear both BINFO_MARKED and BINFO_VBASE_MARKED. */
2610
2611tree
2612dfs_vbase_unmark (binfo, data)
2613 tree binfo;
2614 void *data ATTRIBUTE_UNUSED;
2615{
2616 CLEAR_BINFO_VBASE_MARKED (binfo);
2617 return dfs_unmark (binfo, data);
2618}
2619
70ae3201
MM
2620/* Clear BINFO_VTABLE_PATH_MARKED. */
2621
2622tree
2623dfs_vtable_path_unmark (binfo, data)
2624 tree binfo;
2625 void *data ATTRIBUTE_UNUSED;
2626{
2627 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2628 return NULL_TREE;
2629}
2630
5566b478 2631#if 0
8d08fdba
MS
2632static void
2633dfs_mark_vtable_path (binfo) tree binfo;
2634{ SET_BINFO_VTABLE_PATH_MARKED (binfo); }
2635
8d08fdba
MS
2636static void
2637dfs_mark_new_vtable (binfo) tree binfo;
2638{ SET_BINFO_NEW_VTABLE_MARKED (binfo); }
2639
2640static void
2641dfs_unmark_new_vtable (binfo) tree binfo;
2642{ CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
2643
2644static void
2645dfs_clear_search_slot (binfo) tree binfo;
2646{ CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
2647
ae673f14
JM
2648/* Keep this code around in case we later want to control debug info
2649 based on whether a type is "used". Currently, we only suppress debug
2650 info if we can emit it with the vtable. jason 1999-11-11) */
d6479fe7
MM
2651static tree
2652dfs_debug_mark (binfo, data)
8d08fdba 2653 tree binfo;
d6479fe7 2654 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
2655{
2656 tree t = BINFO_TYPE (binfo);
2657
8d08fdba
MS
2658 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2659
9a3b49ac
MS
2660 /* If interface info is known, either we've already emitted the debug
2661 info or we don't need to. */
56ae6d77 2662 if (CLASSTYPE_INTERFACE_KNOWN (t))
d6479fe7 2663 return NULL_TREE;
8d08fdba 2664
ae673f14
JM
2665 /* If the class has virtual functions, we'll emit the debug info
2666 with the vtable. */
4c6b7393 2667 if (TYPE_POLYMORPHIC_P (t))
ae673f14
JM
2668 return NULL_TREE;
2669
8d08fdba
MS
2670 /* We cannot rely on some alien method to solve our problems,
2671 so we must write out the debug info ourselves. */
f0e01782 2672 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
2c73f9f5 2673 rest_of_type_compilation (t, toplevel_bindings_p ());
d6479fe7
MM
2674
2675 return NULL_TREE;
8d08fdba 2676}
ae673f14 2677#endif
8d08fdba 2678\f
d6479fe7
MM
2679struct vbase_info
2680{
2681 tree decl_ptr;
2682 tree inits;
2683 tree vbase_types;
2684};
7177d104 2685
d6479fe7
MM
2686/* Attach to the type of the virtual base class, the pointer to the
2687 virtual base class. */
e92cc029 2688
d6479fe7
MM
2689static tree
2690dfs_find_vbases (binfo, data)
8d08fdba 2691 tree binfo;
d6479fe7 2692 void *data;
8d08fdba 2693{
d6479fe7 2694 struct vbase_info *vi = (struct vbase_info *) data;
8d08fdba
MS
2695 tree binfos = BINFO_BASETYPES (binfo);
2696 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2697
2698 for (i = n_baselinks-1; i >= 0; i--)
2699 {
2700 tree base_binfo = TREE_VEC_ELT (binfos, i);
2701
2702 if (TREE_VIA_VIRTUAL (base_binfo)
2703 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
2704 {
2705 tree vbase = BINFO_TYPE (base_binfo);
d6479fe7 2706 tree binfo = binfo_member (vbase, vi->vbase_types);
fed3cef0 2707 tree ptr_type = build_pointer_type (vbase);
8d08fdba
MS
2708
2709 CLASSTYPE_SEARCH_SLOT (vbase)
fed3cef0
RK
2710 = build (PLUS_EXPR, ptr_type, vi->decl_ptr,
2711 convert (ptr_type, BINFO_OFFSET (binfo)));
8d08fdba
MS
2712 }
2713 }
2714 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2715 SET_BINFO_NEW_VTABLE_MARKED (binfo);
d6479fe7
MM
2716
2717 return NULL_TREE;
8d08fdba
MS
2718}
2719
d6479fe7
MM
2720static tree
2721dfs_init_vbase_pointers (binfo, data)
8d08fdba 2722 tree binfo;
d6479fe7 2723 void *data;
8d08fdba 2724{
d6479fe7 2725 struct vbase_info *vi = (struct vbase_info *) data;
8d08fdba 2726 tree type = BINFO_TYPE (binfo);
3ef397c1 2727 tree fields;
8926095f 2728 tree this_vbase_ptr;
8d08fdba
MS
2729
2730 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2731
d6479fe7
MM
2732 if (BINFO_INHERITANCE_CHAIN (binfo))
2733 {
2734 this_vbase_ptr = TREE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo));
2735 if (TREE_VIA_VIRTUAL (binfo))
2736 this_vbase_ptr = CLASSTYPE_SEARCH_SLOT (type);
2737 else
2738 this_vbase_ptr = convert_pointer_to_single_level (type,
2739 this_vbase_ptr);
2740 TREE_CHAIN (binfo) = this_vbase_ptr;
2741 }
2742 else
2743 this_vbase_ptr = TREE_CHAIN (binfo);
2744
3ef397c1
MM
2745 /* We're going to iterate through all the pointers to virtual
2746 base-classes. They come at the beginning of the class. */
2747 fields = TYPE_FIELDS (type);
2748 if (fields == TYPE_VFIELD (type))
2749 /* If the first field is the vtbl pointer (as happens in the new
2750 ABI), skip it. */
2751 fields = TREE_CHAIN (fields);
2752
8d08fdba
MS
2753 if (fields == NULL_TREE
2754 || DECL_NAME (fields) == NULL_TREE
2755 || ! VBASE_NAME_P (DECL_NAME (fields)))
d6479fe7 2756 return NULL_TREE;
8d08fdba 2757
d6479fe7
MM
2758 if (build_pointer_type (type)
2759 != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
8d08fdba
MS
2760 my_friendly_abort (125);
2761
d6479fe7 2762 while (fields && DECL_NAME (fields) && VBASE_NAME_P (DECL_NAME (fields)))
8d08fdba
MS
2763 {
2764 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2765 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
fc378698 2766 tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
d6479fe7
MM
2767 vi->inits = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
2768 vi->vbase_types),
2769 build_modify_expr (ref, NOP_EXPR, init),
2770 vi->inits);
8d08fdba
MS
2771 fields = TREE_CHAIN (fields);
2772 }
d6479fe7
MM
2773
2774 return NULL_TREE;
8d08fdba
MS
2775}
2776
2777/* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2778 times, just NEW_VTABLE, but optimizer should make both with equal
2779 efficiency (though it does not currently). */
e92cc029 2780
d6479fe7
MM
2781static tree
2782dfs_clear_vbase_slots (binfo, data)
8d08fdba 2783 tree binfo;
d6479fe7 2784 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
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);
d6479fe7 2790 return NULL_TREE;
8d08fdba
MS
2791}
2792
2793tree
2794init_vbase_pointers (type, decl_ptr)
2795 tree type;
2796 tree decl_ptr;
2797{
2798 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2799 {
d6479fe7 2800 struct vbase_info vi;
8d08fdba
MS
2801 int old_flag = flag_this_is_variable;
2802 tree binfo = TYPE_BINFO (type);
2803 flag_this_is_variable = -2;
d6479fe7
MM
2804
2805 /* Find all the virtual base classes, marking them for later
2806 initialization. */
2807 vi.decl_ptr = decl_ptr;
2808 vi.vbase_types = CLASSTYPE_VBASECLASSES (type);
2809 vi.inits = NULL_TREE;
2810
2811 dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp, &vi);
2812
2813 /* Build up a list of the initializers. */
2814 TREE_CHAIN (binfo) = decl_ptr;
2815 dfs_walk_real (binfo,
2816 dfs_init_vbase_pointers, 0,
2817 marked_vtable_pathp,
2818 &vi);
2819
2820 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep, 0);
8d08fdba 2821 flag_this_is_variable = old_flag;
d6479fe7 2822 return vi.inits;
8d08fdba
MS
2823 }
2824 return 0;
2825}
2826
43f2999d 2827/* get the virtual context (the vbase that directly contains the
4f1c5b7d 2828 DECL_CONTEXT of the FNDECL) that the given FNDECL is declared in,
43f2999d
MS
2829 or NULL_TREE if there is none.
2830
4f1c5b7d
MM
2831 FNDECL must come from a virtual table from a virtual base to ensure
2832 that there is only one possible DECL_CONTEXT.
43f2999d
MS
2833
2834 We know that if there is more than one place (binfo) the fndecl that the
2835 declared, they all refer to the same binfo. See get_class_offset_1 for
2836 the check that ensures this. */
e92cc029 2837
43f2999d
MS
2838static tree
2839virtual_context (fndecl, t, vbase)
2840 tree fndecl, t, vbase;
2841{
2842 tree path;
4f1c5b7d 2843 if (get_base_distance (DECL_CONTEXT (fndecl), t, 0, &path) < 0)
43f2999d 2844 {
4f1c5b7d
MM
2845 /* DECL_CONTEXT can be ambiguous in t. */
2846 if (get_base_distance (DECL_CONTEXT (fndecl), vbase, 0, &path) >= 0)
f30432d7
MS
2847 {
2848 while (path)
2849 {
2850 /* Not sure if checking path == vbase is necessary here, but just in
2851 case it is. */
2852 if (TREE_VIA_VIRTUAL (path) || path == vbase)
23381155 2853 return BINFO_FOR_VBASE (BINFO_TYPE (path), t);
f30432d7
MS
2854 path = BINFO_INHERITANCE_CHAIN (path);
2855 }
2856 }
43f2999d 2857 /* This shouldn't happen, I don't want errors! */
8251199e 2858 warning ("recoverable compiler error, fixups for virtual function");
43f2999d
MS
2859 return vbase;
2860 }
2861 while (path)
2862 {
2863 if (TREE_VIA_VIRTUAL (path))
2864 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2865 path = BINFO_INHERITANCE_CHAIN (path);
2866 }
2867 return 0;
2868}
2869
2870/* Fixups upcast offsets for one vtable.
2871 Entries may stay within the VBASE given, or
2872 they may upcast into a direct base, or
2873 they may upcast into a different vbase.
2874
45537677
MS
2875 We only need to do fixups in case 2 and 3. In case 2, we add in
2876 the virtual base offset to effect an upcast, in case 3, we add in
2877 the virtual base offset to effect an upcast, then subtract out the
2878 offset for the other virtual base, to effect a downcast into it.
43f2999d
MS
2879
2880 This routine mirrors fixup_vtable_deltas in functionality, though
2881 this one is runtime based, and the other is compile time based.
2882 Conceivably that routine could be removed entirely, and all fixups
2883 done at runtime.
2884
2885 VBASE_OFFSETS is an association list of virtual bases that contains
45537677
MS
2886 offset information for the virtual bases, so the offsets are only
2887 calculated once. The offsets are computed by where we think the
2888 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
e92cc029
MS
2889 the vbase really is. */
2890
43f2999d 2891static void
45537677
MS
2892expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t,
2893 vbase_offsets)
2894 tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets;
43f2999d 2895{
07b7a812 2896 tree virtuals;
43f2999d
MS
2897 tree vc;
2898 tree delta;
fed3cef0 2899 HOST_WIDE_INT n;
dd42e135
MM
2900
2901 while (BINFO_PRIMARY_MARKED_P (binfo))
2902 {
2903 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2904 if (TREE_VIA_VIRTUAL (binfo))
2905 return;
2906 }
2907
43f2999d
MS
2908 delta = purpose_member (vbase, *vbase_offsets);
2909 if (! delta)
2910 {
fc378698 2911 delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
45537677 2912 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr);
43f2999d
MS
2913 delta = save_expr (delta);
2914 delta = tree_cons (vbase, delta, *vbase_offsets);
2915 *vbase_offsets = delta;
2916 }
2917
07b7a812 2918 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
f30432d7 2919
43f2999d
MS
2920 while (virtuals)
2921 {
2922 tree current_fndecl = TREE_VALUE (virtuals);
83f2ccf4 2923
43f2999d 2924 if (current_fndecl
e8abc66f 2925 && current_fndecl != abort_fndecl
43f2999d
MS
2926 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2927 {
e92cc029 2928 /* This may in fact need a runtime fixup. */
de22184b 2929 tree idx = build_int_2 (n, 0);
43f2999d
MS
2930 tree vtbl = BINFO_VTABLE (binfo);
2931 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2932 tree aref, ref, naref;
2933 tree old_delta, new_delta;
2934 tree init;
2935
2936 if (nvtbl == NULL_TREE
2937 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2938 {
2939 /* Dup it if it isn't in local scope yet. */
8c90d611
JM
2940 nvtbl = build_decl
2941 (VAR_DECL, DECL_NAME (vtbl),
52bf7d5d 2942 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl)));
43f2999d
MS
2943 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2944 DECL_ALIGN (nvtbl));
2945 TREE_READONLY (nvtbl) = 0;
6b5fbb55 2946 DECL_ARTIFICIAL (nvtbl) = 1;
43f2999d
MS
2947 nvtbl = pushdecl (nvtbl);
2948 init = NULL_TREE;
cd9f6678 2949 cp_finish_decl (nvtbl, init, NULL_TREE,
8c90d611
JM
2950 LOOKUP_ONLYCONVERTING);
2951
2952 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2953 because they wouldn't be useful; everything that wants to
2954 look at the vtable will look at the decl for the normal
2955 vtable. Setting DECL_CONTEXT also screws up
2956 decl_function_context. */
2957
43f2999d
MS
2958 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2959 nvtbl, vtbl);
9bfadf57 2960 finish_expr_stmt (init);
e92cc029 2961 /* Update the vtable pointers as necessary. */
8c90d611
JM
2962 ref = build_vfield_ref
2963 (build_indirect_ref (addr, NULL_PTR),
d3a3fb6a 2964 DECL_CONTEXT (TYPE_VFIELD (BINFO_TYPE (binfo))));
9bfadf57 2965 finish_expr_stmt
a9e6be8e 2966 (build_modify_expr (ref, NOP_EXPR, nvtbl));
43f2999d
MS
2967 }
2968 assemble_external (vtbl);
2969 aref = build_array_ref (vtbl, idx);
2970 naref = build_array_ref (nvtbl, idx);
8c90d611
JM
2971 old_delta = build_component_ref (aref, delta_identifier,
2972 NULL_TREE, 0);
2973 new_delta = build_component_ref (naref, delta_identifier,
2974 NULL_TREE, 0);
45537677
MS
2975
2976 /* This is a upcast, so we have to add the offset for the
2977 virtual base. */
43f2999d 2978 old_delta = build_binary_op (PLUS_EXPR, old_delta,
337c90cc 2979 TREE_VALUE (delta));
43f2999d
MS
2980 if (vc)
2981 {
45537677
MS
2982 /* If this is set, we need to subtract out the delta
2983 adjustments for the other virtual base that we
2984 downcast into. */
43f2999d
MS
2985 tree vc_delta = purpose_member (vc, *vbase_offsets);
2986 if (! vc_delta)
2987 {
2988 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
fc378698 2989 vc_delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
43f2999d 2990 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
45537677 2991 vc_delta, vc_addr);
43f2999d
MS
2992 vc_delta = save_expr (vc_delta);
2993 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2994 }
2995 else
2996 vc_delta = TREE_VALUE (vc_delta);
2997
45537677
MS
2998 /* This is a downcast, so we have to subtract the offset
2999 for the virtual base. */
337c90cc 3000 old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta);
43f2999d
MS
3001 }
3002
3003 TREE_READONLY (new_delta) = 0;
6396c3a5 3004 TREE_TYPE (new_delta) =
91063b51
MM
3005 cp_build_qualified_type (TREE_TYPE (new_delta),
3006 CP_TYPE_QUALS (TREE_TYPE (new_delta))
3007 & ~TYPE_QUAL_CONST);
9bfadf57 3008 finish_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
43f2999d
MS
3009 old_delta));
3010 }
3011 ++n;
3012 virtuals = TREE_CHAIN (virtuals);
3013 }
3014}
3015
3016/* Fixup upcast offsets for all direct vtables. Patterned after
3017 expand_direct_vtbls_init. */
e92cc029 3018
43f2999d
MS
3019static void
3020fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
6b5fbb55 3021 tree real_binfo, binfo;
43f2999d 3022 int init_self, can_elide;
6b5fbb55 3023 tree addr, orig_addr, type, vbase, *vbase_offsets;
43f2999d
MS
3024{
3025 tree real_binfos = BINFO_BASETYPES (real_binfo);
3026 tree binfos = BINFO_BASETYPES (binfo);
3027 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
3028
3029 for (i = 0; i < n_baselinks; i++)
3030 {
3031 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
3032 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8 3033 int is_not_base_vtable
dd42e135 3034 = !BINFO_PRIMARY_MARKED_P (real_base_binfo);
43f2999d
MS
3035 if (! TREE_VIA_VIRTUAL (real_base_binfo))
3036 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
3037 is_not_base_vtable, can_elide, addr,
3038 orig_addr, type, vbase, vbase_offsets);
3039 }
3040#if 0
3041 /* Before turning this on, make sure it is correct. */
3042 if (can_elide && ! BINFO_MODIFIED (binfo))
3043 return;
3044#endif
3045 /* Should we use something besides CLASSTYPE_VFIELDS? */
3046 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
3047 {
45537677
MS
3048 tree new_addr = convert_pointer_to_real (binfo, addr);
3049 expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr,
3050 type, vbase_offsets);
43f2999d
MS
3051 }
3052}
3053
018fc244
MM
3054/* Fixup all the virtual upcast offsets for TYPE. DECL_PTR is the
3055 address of the sub-object being initialized. */
8d08fdba 3056
018fc244
MM
3057static void
3058fixup_all_virtual_upcast_offsets (type, decl_ptr)
3059 tree type;
3060 tree decl_ptr;
3061{
3062 tree if_stmt;
3063 tree in_charge_node;
3064 tree vbases;
3065
3066 /* Only tweak the vtables if we're in charge. */
3067 in_charge_node = current_in_charge_parm;
3068 if (!in_charge_node)
3069 /* There's no need for any fixups in this case. */
3070 return;
3071 in_charge_node = build_binary_op (EQ_EXPR,
3072 in_charge_node, integer_zero_node);
3073 if_stmt = begin_if_stmt ();
3074 finish_if_stmt_cond (in_charge_node, if_stmt);
3075
3076 /* Iterate through the virtual bases, fixing up the upcast offset
3077 for each one. */
3078 for (vbases = CLASSTYPE_VBASECLASSES (type);
3079 vbases;
3080 vbases = TREE_CHAIN (vbases))
3081 {
3082 if (flag_vtable_thunks)
3083 /* We don't have dynamic thunks yet! So for now, just fail
3084 silently. */
3085 ;
3086 else
3087 {
dd42e135 3088 tree vbase;
018fc244
MM
3089 tree vbase_offsets;
3090 tree addr;
3091
dd42e135 3092 vbase = find_vbase_instance (BINFO_TYPE (vbases), type);
018fc244 3093 vbase_offsets = NULL_TREE;
dd42e135
MM
3094 addr = convert_pointer_to_vbase (BINFO_TYPE (vbases), decl_ptr);
3095 fixup_virtual_upcast_offsets (vbase,
018fc244
MM
3096 TYPE_BINFO (BINFO_TYPE (vbases)),
3097 1, 0, addr, decl_ptr,
dd42e135 3098 type, vbase, &vbase_offsets);
018fc244
MM
3099 }
3100 }
3101
3102 /* Close out the if-statement. */
3103 finish_then_clause (if_stmt);
3104 finish_if_stmt ();
3105}
3106
3107/* Generate the code needed to initialize all the virtual function
3108 table slots of all the virtual baseclasses. BINFO is the binfo
3109 which determines the virtual baseclasses to use. TRUE_EXP is the
3110 true object we are initializing, and DECL_PTR is the pointer to the
3111 sub-object we are initializing. */
8d08fdba 3112
8926095f 3113void
d569399b 3114expand_indirect_vtbls_init (binfo, decl_ptr)
7177d104 3115 tree binfo;
d569399b 3116 tree decl_ptr;
8d08fdba 3117{
8d08fdba 3118 tree type = BINFO_TYPE (binfo);
9e9ff709 3119
8a21007c
AM
3120 /* This function executes during the finish_function() segment,
3121 AFTER the auto variables and temporary stack space has been marked
3122 unused...If space is needed for the virtual function tables,
3123 some of them might fit within what the compiler now thinks
3124 are available stack slots... These values are actually initialized at
3125 the beginnning of the function, so when the automatics use their space,
3126 they will overwrite the values that are placed here. Marking all
3127 temporary space as unavailable prevents this from happening. */
3128
3129 mark_all_temps_used();
3130
8d08fdba
MS
3131 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
3132 {
8d08fdba 3133 tree vbases = CLASSTYPE_VBASECLASSES (type);
d6479fe7 3134 struct vbase_info vi;
d569399b 3135 vi.decl_ptr = decl_ptr;
d6479fe7 3136 vi.vbase_types = vbases;
8d08fdba 3137
d6479fe7 3138 dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep, &vi);
d569399b 3139 fixup_all_virtual_upcast_offsets (type, vi.decl_ptr);
d6479fe7 3140 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep, 0);
8d08fdba 3141 }
8d08fdba
MS
3142}
3143
8d08fdba
MS
3144/* get virtual base class types.
3145 This adds type to the vbase_types list in reverse dfs order.
3146 Ordering is very important, so don't change it. */
3147
d6479fe7
MM
3148static tree
3149dfs_get_vbase_types (binfo, data)
8d08fdba 3150 tree binfo;
d6479fe7 3151 void *data;
8d08fdba 3152{
4b3b5328 3153 tree type = (tree) data;
d6479fe7 3154
51c184be 3155 if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
8d08fdba 3156 {
fed3cef0 3157 tree new_vbase = make_binfo (size_zero_node,
9d4c0187 3158 BINFO_TYPE (binfo),
ca107ded
MM
3159 BINFO_VTABLE (binfo),
3160 BINFO_VIRTUALS (binfo));
4b3b5328 3161 unshare_base_binfos (new_vbase);
ca107ded 3162 TREE_VIA_VIRTUAL (new_vbase) = 1;
4b3b5328
MM
3163 BINFO_INHERITANCE_CHAIN (new_vbase) = TYPE_BINFO (type);
3164 TREE_CHAIN (new_vbase) = CLASSTYPE_VBASECLASSES (type);
3165 CLASSTYPE_VBASECLASSES (type) = new_vbase;
51c184be 3166 SET_BINFO_VBASE_MARKED (binfo);
8d08fdba
MS
3167 }
3168 SET_BINFO_MARKED (binfo);
d6479fe7 3169 return NULL_TREE;
8d08fdba
MS
3170}
3171
4b3b5328 3172/* Set CLASSTYPE_VBASECLASSES for TYPE. */
e92cc029 3173
23381155 3174void
8d08fdba
MS
3175get_vbase_types (type)
3176 tree type;
3177{
8026246f 3178 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
4b3b5328 3179 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
8d08fdba
MS
3180 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
3181 reverse it so that we get normal dfs ordering. */
8026246f
MM
3182 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
3183 dfs_walk (TYPE_BINFO (type), dfs_vbase_unmark, markedp, 0);
8d08fdba 3184}
dd42e135
MM
3185
3186/* Called from find_vbase_instance via dfs_walk. */
3187
3188static tree
3189dfs_find_vbase_instance (binfo, data)
3190 tree binfo;
3191 void *data;
3192{
3193 tree base = TREE_VALUE ((tree) data);
3194
3195 if (BINFO_PRIMARY_MARKED_P (binfo)
3196 && same_type_p (BINFO_TYPE (binfo), base))
3197 return binfo;
3198
3199 return NULL_TREE;
3200}
3201
3202/* Find the real occurrence of the virtual BASE (a class type) in the
3203 hierarchy dominated by TYPE. */
3204
3205tree
3206find_vbase_instance (base, type)
3207 tree base;
3208 tree type;
3209{
3210 tree instance;
3211
3212 instance = BINFO_FOR_VBASE (base, type);
3213 if (!BINFO_VBASE_PRIMARY_P (instance))
3214 return instance;
3215
3216 return dfs_walk (TYPE_BINFO (type),
3217 dfs_find_vbase_instance,
3218 NULL,
3219 build_tree_list (type, base));
3220}
3221
8d08fdba 3222\f
ae673f14
JM
3223/* Debug info for C++ classes can get very large; try to avoid
3224 emitting it everywhere.
3225
50e159f6
JM
3226 Note that this optimization wins even when the target supports
3227 BINCL (if only slightly), and reduces the amount of work for the
3228 linker. */
ae673f14
JM
3229
3230void
3231maybe_suppress_debug_info (t)
3232 tree t;
3233{
50e159f6
JM
3234 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
3235 does not support name references between translation units. It supports
3236 symbolic references between translation units, but only within a single
3237 executable or shared library.
3238
3239 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
3240 that the type was never defined, so we only get the members we
3241 actually define. */
3242 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
ae673f14
JM
3243 return;
3244
50e159f6
JM
3245 /* We might have set this earlier in cp_finish_decl. */
3246 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
3247
ae673f14
JM
3248 /* If we already know how we're handling this class, handle debug info
3249 the same way. */
3250 if (CLASSTYPE_INTERFACE_ONLY (t))
3251 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
3252 else if (CLASSTYPE_INTERFACE_KNOWN (t))
3253 /* Don't set it. */;
bbd15aac
MM
3254 /* If the class has a vtable, write out the debug info along with
3255 the vtable. */
3256 else if (TYPE_CONTAINS_VPTR_P (t))
ae673f14
JM
3257 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
3258
3259 /* Otherwise, just emit the debug info normally. */
3260}
3261
3262#if 0
3263/* Keep this code around in case we later want to control debug info
3264 based on whether a type is "used". Currently, we only suppress debug
3265 info if we can emit it with the vtable. jason 1999-11-11) */
3266
8d08fdba
MS
3267/* If we want debug info for a type TYPE, make sure all its base types
3268 are also marked as being potentially interesting. This avoids
3269 the problem of not writing any debug info for intermediate basetypes
a292b002 3270 that have abstract virtual functions. Also mark member types. */
8d08fdba
MS
3271
3272void
3273note_debug_info_needed (type)
3274 tree type;
3275{
a292b002 3276 tree field;
9a3b49ac
MS
3277
3278 if (current_template_parms)
3279 return;
8857f91e
MM
3280
3281 if (TYPE_BEING_DEFINED (type))
3282 /* We can't go looking for the base types and fields just yet. */
3283 return;
9a3b49ac 3284
50e159f6
JM
3285 /* See the comment in maybe_suppress_debug_info. */
3286 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
d2e5ee5c
MS
3287 return;
3288
d6479fe7 3289 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
a292b002
MS
3290 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3291 {
3292 tree ttype;
3293 if (TREE_CODE (field) == FIELD_DECL
3294 && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
d6479fe7 3295 && dfs_debug_unmarkedp (TYPE_BINFO (ttype), 0))
a292b002
MS
3296 note_debug_info_needed (ttype);
3297 }
8d08fdba 3298}
ae673f14 3299#endif
8d08fdba
MS
3300\f
3301/* Subroutines of push_class_decls (). */
3302
c1def683
JM
3303/* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
3304 because it (or one of the intermediate bases) depends on template parms. */
3305
3306static int
3307dependent_base_p (binfo)
3308 tree binfo;
3309{
3310 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
3311 {
d6479fe7 3312 if (currently_open_class (TREE_TYPE (binfo)))
c1def683
JM
3313 break;
3314 if (uses_template_parms (TREE_TYPE (binfo)))
3315 return 1;
3316 }
3317 return 0;
3318}
3319
8f032717
MM
3320static void
3321setup_class_bindings (name, type_binding_p)
3322 tree name;
3323 int type_binding_p;
8d08fdba 3324{
8f032717
MM
3325 tree type_binding = NULL_TREE;
3326 tree value_binding;
c1def683 3327
8f032717
MM
3328 /* If we've already done the lookup for this declaration, we're
3329 done. */
3330 if (IDENTIFIER_CLASS_VALUE (name))
3331 return;
8d08fdba 3332
8f032717
MM
3333 /* First, deal with the type binding. */
3334 if (type_binding_p)
8d08fdba 3335 {
8f032717
MM
3336 type_binding = lookup_member (current_class_type, name,
3337 /*protect=*/2,
3338 /*want_type=*/1);
3339 if (TREE_CODE (type_binding) == TREE_LIST
aa65d1a2 3340 && TREE_TYPE (type_binding) == error_mark_node)
8f032717 3341 /* NAME is ambiguous. */
aa65d1a2 3342 push_class_level_binding (name, type_binding);
8f032717
MM
3343 else
3344 pushdecl_class_level (type_binding);
8d08fdba
MS
3345 }
3346
8f032717
MM
3347 /* Now, do the value binding. */
3348 value_binding = lookup_member (current_class_type, name,
3349 /*protect=*/2,
3350 /*want_type=*/0);
3351
3352 if (type_binding_p
3353 && (TREE_CODE (value_binding) == TYPE_DECL
3354 || (TREE_CODE (value_binding) == TREE_LIST
aa65d1a2
MM
3355 && TREE_TYPE (value_binding) == error_mark_node
3356 && (TREE_CODE (TREE_VALUE (value_binding))
8f032717
MM
3357 == TYPE_DECL))))
3358 /* We found a type-binding, even when looking for a non-type
3359 binding. This means that we already processed this binding
3360 above. */
3361 my_friendly_assert (type_binding_p, 19990401);
dfe2b0b3 3362 else if (value_binding)
8d08fdba 3363 {
8f032717 3364 if (TREE_CODE (value_binding) == TREE_LIST
aa65d1a2 3365 && TREE_TYPE (value_binding) == error_mark_node)
8f032717 3366 /* NAME is ambiguous. */
aa65d1a2 3367 push_class_level_binding (name, value_binding);
8f032717 3368 else
8d08fdba 3369 {
aa65d1a2 3370 if (BASELINK_P (value_binding))
8f032717
MM
3371 /* NAME is some overloaded functions. */
3372 value_binding = TREE_VALUE (value_binding);
3373 pushdecl_class_level (value_binding);
3374 }
3375 }
3376}
f30432d7 3377
8f032717
MM
3378/* Push class-level declarations for any names appearing in BINFO that
3379 are TYPE_DECLS. */
7ddedda4 3380
8f032717
MM
3381static tree
3382dfs_push_type_decls (binfo, data)
3383 tree binfo;
3384 void *data ATTRIBUTE_UNUSED;
3385{
3386 tree type;
3387 tree fields;
f30432d7 3388
8f032717
MM
3389 type = BINFO_TYPE (binfo);
3390 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3391 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
908c4e83
MM
3392 && !(!same_type_p (type, current_class_type)
3393 && template_self_reference_p (type, fields)))
8f032717 3394 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
0ec57017
JM
3395
3396 /* We can't just use BINFO_MARKED because envelope_add_decl uses
3397 DERIVED_FROM_P, which calls get_base_distance. */
3398 SET_BINFO_PUSHDECLS_MARKED (binfo);
8f032717 3399
d6479fe7 3400 return NULL_TREE;
8d08fdba
MS
3401}
3402
8f032717
MM
3403/* Push class-level declarations for any names appearing in BINFO that
3404 are not TYPE_DECLS. */
e92cc029 3405
d6479fe7 3406static tree
8f032717 3407dfs_push_decls (binfo, data)
8d08fdba 3408 tree binfo;
8f032717 3409 void *data;
8d08fdba 3410{
8f032717
MM
3411 tree type;
3412 tree method_vec;
3413 int dep_base_p;
8d08fdba 3414
8f032717
MM
3415 type = BINFO_TYPE (binfo);
3416 dep_base_p = (processing_template_decl && type != current_class_type
3417 && dependent_base_p (binfo));
3418 if (!dep_base_p)
8d08fdba 3419 {
8f032717
MM
3420 tree fields;
3421 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3422 if (DECL_NAME (fields)
3423 && TREE_CODE (fields) != TYPE_DECL
3424 && TREE_CODE (fields) != USING_DECL)
3425 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
3426 else if (TREE_CODE (fields) == FIELD_DECL
6bdb8141 3427 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
8f032717
MM
3428 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
3429
3430 method_vec = (CLASS_TYPE_P (type)
3431 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
3432 if (method_vec)
8d08fdba 3433 {
8f032717
MM
3434 tree *methods;
3435 tree *end;
3436
3437 /* Farm out constructors and destructors. */
3438 end = TREE_VEC_END (method_vec);
3439
3440 for (methods = &TREE_VEC_ELT (method_vec, 2);
3441 *methods && methods != end;
3442 methods++)
3443 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
3444 /*type_binding_p=*/0);
8d08fdba
MS
3445 }
3446 }
8f032717 3447
0ec57017 3448 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
d6479fe7
MM
3449
3450 return NULL_TREE;
8d08fdba
MS
3451}
3452
3453/* When entering the scope of a class, we cache all of the
3454 fields that that class provides within its inheritance
3455 lattice. Where ambiguities result, we mark them
3456 with `error_mark_node' so that if they are encountered
3457 without explicit qualification, we can emit an error
45537677 3458 message. */
e92cc029 3459
8d08fdba 3460void
45537677 3461push_class_decls (type)
8d08fdba
MS
3462 tree type;
3463{
8d08fdba
MS
3464 search_stack = push_search_level (search_stack, &search_obstack);
3465
aa65d1a2 3466 /* Enter type declarations and mark. */
8f032717 3467 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
8d08fdba 3468
aa65d1a2 3469 /* Enter non-type declarations and unmark. */
8f032717 3470 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
8d08fdba
MS
3471}
3472
3473/* Here's a subroutine we need because C lacks lambdas. */
e92cc029 3474
d6479fe7
MM
3475static tree
3476dfs_unuse_fields (binfo, data)
8d08fdba 3477 tree binfo;
d6479fe7 3478 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
3479{
3480 tree type = TREE_TYPE (binfo);
3481 tree fields;
3482
3483 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3484 {
3485 if (TREE_CODE (fields) != FIELD_DECL)
3486 continue;
3487
3488 TREE_USED (fields) = 0;
3489 if (DECL_NAME (fields) == NULL_TREE
6bdb8141 3490 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
8d08fdba
MS
3491 unuse_fields (TREE_TYPE (fields));
3492 }
d6479fe7
MM
3493
3494 return NULL_TREE;
8d08fdba
MS
3495}
3496
3497void
3498unuse_fields (type)
3499 tree type;
3500{
d6479fe7 3501 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
8d08fdba
MS
3502}
3503
3504void
5566b478 3505pop_class_decls ()
8d08fdba
MS
3506{
3507 /* We haven't pushed a search level when dealing with cached classes,
3508 so we'd better not try to pop it. */
3509 if (search_stack)
3510 search_stack = pop_search_level (search_stack);
3511}
3512
8d08fdba
MS
3513void
3514print_search_statistics ()
3515{
3516#ifdef GATHER_STATISTICS
8d08fdba
MS
3517 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3518 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3519 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3520 n_outer_fields_searched, n_calls_lookup_fnfields);
3521 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
fc378698 3522#else /* GATHER_STATISTICS */
8d08fdba 3523 fprintf (stderr, "no search statistics\n");
fc378698 3524#endif /* GATHER_STATISTICS */
8d08fdba
MS
3525}
3526
3527void
3528init_search_processing ()
3529{
3530 gcc_obstack_init (&search_obstack);
9cd64686 3531 vptr_identifier = get_identifier ("_vptr");
8d08fdba
MS
3532}
3533
3534void
3535reinit_search_statistics ()
3536{
5566b478 3537#ifdef GATHER_STATISTICS
8d08fdba
MS
3538 n_fields_searched = 0;
3539 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3540 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3541 n_calls_get_base_type = 0;
3542 n_outer_fields_searched = 0;
3543 n_contexts_saved = 0;
fc378698 3544#endif /* GATHER_STATISTICS */
8d08fdba 3545}
e1cd6e56 3546
72c4a2a6 3547static tree
7d4bdeed 3548add_conversions (binfo, data)
e1cd6e56 3549 tree binfo;
7d4bdeed 3550 void *data;
e1cd6e56 3551{
72b7eeff 3552 int i;
fc378698 3553 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
7d4bdeed 3554 tree *conversions = (tree *) data;
72b7eeff 3555
a7a64a77
MM
3556 /* Some builtin types have no method vector, not even an empty one. */
3557 if (!method_vec)
3558 return NULL_TREE;
3559
fc378698 3560 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
72b7eeff 3561 {
fc378698 3562 tree tmp = TREE_VEC_ELT (method_vec, i);
37b6eb34 3563 tree name;
61a127b3 3564
aa45967f 3565 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
72b7eeff 3566 break;
72c4a2a6 3567
37b6eb34 3568 name = DECL_NAME (OVL_CURRENT (tmp));
59e76fc6 3569
72c4a2a6 3570 /* Make sure we don't already have this conversion. */
37b6eb34 3571 if (! IDENTIFIER_MARKED (name))
72c4a2a6 3572 {
e1b3e07d 3573 *conversions = tree_cons (binfo, tmp, *conversions);
37b6eb34 3574 IDENTIFIER_MARKED (name) = 1;
72c4a2a6 3575 }
72b7eeff 3576 }
72c4a2a6 3577 return NULL_TREE;
e1cd6e56
MS
3578}
3579
27b8d0cd
MM
3580/* Return a TREE_LIST containing all the non-hidden user-defined
3581 conversion functions for TYPE (and its base-classes). The
3582 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
3583 containing the conversion functions. The TREE_PURPOSE is the BINFO
3584 from which the conversion functions in this node were selected. */
3585
e1cd6e56
MS
3586tree
3587lookup_conversions (type)
3588 tree type;
3589{
72c4a2a6 3590 tree t;
7d4bdeed 3591 tree conversions = NULL_TREE;
72c4a2a6 3592
e92cc029 3593 if (TYPE_SIZE (type))
d6479fe7 3594 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
72c4a2a6
JM
3595
3596 for (t = conversions; t; t = TREE_CHAIN (t))
37b6eb34 3597 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
72c4a2a6 3598
e1cd6e56
MS
3599 return conversions;
3600}
6467930b 3601
d6479fe7
MM
3602struct overlap_info
3603{
3604 tree compare_type;
3605 int found_overlap;
3606};
3607
732dcb6f
JM
3608/* Check whether the empty class indicated by EMPTY_BINFO is also present
3609 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3610
d6479fe7
MM
3611static tree
3612dfs_check_overlap (empty_binfo, data)
732dcb6f 3613 tree empty_binfo;
d6479fe7 3614 void *data;
732dcb6f 3615{
d6479fe7 3616 struct overlap_info *oi = (struct overlap_info *) data;
732dcb6f 3617 tree binfo;
d6479fe7
MM
3618 for (binfo = TYPE_BINFO (oi->compare_type);
3619 ;
3620 binfo = BINFO_BASETYPE (binfo, 0))
732dcb6f
JM
3621 {
3622 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
3623 {
d6479fe7 3624 oi->found_overlap = 1;
732dcb6f
JM
3625 break;
3626 }
3627 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
3628 break;
3629 }
d6479fe7
MM
3630
3631 return NULL_TREE;
732dcb6f
JM
3632}
3633
3634/* Trivial function to stop base traversal when we find something. */
3635
d6479fe7
MM
3636static tree
3637dfs_no_overlap_yet (binfo, data)
3638 tree binfo;
3639 void *data;
732dcb6f 3640{
d6479fe7
MM
3641 struct overlap_info *oi = (struct overlap_info *) data;
3642 return !oi->found_overlap ? binfo : NULL_TREE;
732dcb6f
JM
3643}
3644
3645/* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3646 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3647
3648int
3649types_overlap_p (empty_type, next_type)
3650 tree empty_type, next_type;
3651{
d6479fe7
MM
3652 struct overlap_info oi;
3653
732dcb6f
JM
3654 if (! IS_AGGR_TYPE (next_type))
3655 return 0;
d6479fe7
MM
3656 oi.compare_type = next_type;
3657 oi.found_overlap = 0;
3658 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
3659 dfs_no_overlap_yet, &oi);
3660 return oi.found_overlap;
3661}
3662
ae673f14
JM
3663/* Given a vtable VAR, determine which binfo it comes from.
3664
3665 FIXME What about secondary vtables? */
a1dd0d36
JM
3666
3667tree
d6479fe7
MM
3668binfo_for_vtable (var)
3669 tree var;
a1dd0d36 3670{
5430acc8
JM
3671 tree binfo = TYPE_BINFO (DECL_CONTEXT (var));
3672 tree binfos;
3673 int i;
d6479fe7 3674
5430acc8
JM
3675 while (1)
3676 {
3677 binfos = BINFO_BASETYPES (binfo);
3678 if (binfos == NULL_TREE)
3679 break;
3680
3681 i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
3682 if (i == -1)
3683 break;
3684
3685 binfo = TREE_VEC_ELT (binfos, i);
3686 }
3687
3688 return binfo;
a1dd0d36 3689}
6ad07332
JM
3690
3691/* Returns 1 iff BINFO is from a direct or indirect virtual base. */
3692
3693int
3694binfo_from_vbase (binfo)
3695 tree binfo;
3696{
3697 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
3698 {
3699 if (TREE_VIA_VIRTUAL (binfo))
3700 return 1;
3701 }
3702 return 0;
3703}
This page took 1.048377 seconds and 5 git commands to generate.