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