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