2002-05-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / cp / search.c
blob38841589dfd257bc503fa05e7b7c9fe164b0eb01
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "obstack.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "output.h"
34 #include "toplev.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 #include "stack.h"
41 /* Obstack used for remembering decision points of breadth-first. */
43 static struct obstack search_obstack;
45 /* Methods for pushing and popping objects to and from obstacks. */
47 struct stack_level *
48 push_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;
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;
63 struct stack_level *
64 pop_stack_level (stack)
65 struct stack_level *stack;
67 struct stack_level *tem = stack;
68 struct obstack *obstack = tem->obstack;
69 stack = tem->prev;
70 obstack_free (obstack, tem);
71 return stack;
74 #define search_level stack_level
75 static struct search_level *search_stack;
77 struct vbase_info
79 /* The class dominating the hierarchy. */
80 tree type;
81 /* A pointer to a complete object of the indicated TYPE. */
82 tree decl_ptr;
83 tree inits;
86 static tree lookup_field_1 PARAMS ((tree, tree));
87 static int is_subobject_of_p PARAMS ((tree, tree, tree));
88 static tree dfs_check_overlap PARAMS ((tree, void *));
89 static tree dfs_no_overlap_yet PARAMS ((tree, void *));
90 static base_kind lookup_base_r
91 PARAMS ((tree, tree, base_access, int, int, int, tree *));
92 static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
93 static tree marked_pushdecls_p PARAMS ((tree, void *));
94 static tree unmarked_pushdecls_p PARAMS ((tree, void *));
95 static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
96 static tree dfs_debug_mark PARAMS ((tree, void *));
97 static tree dfs_get_vbase_types PARAMS ((tree, void *));
98 static tree dfs_push_type_decls PARAMS ((tree, void *));
99 static tree dfs_push_decls PARAMS ((tree, void *));
100 static tree dfs_unuse_fields PARAMS ((tree, void *));
101 static tree add_conversions PARAMS ((tree, void *));
102 static int covariant_return_p PARAMS ((tree, tree));
103 static int look_for_overrides_r PARAMS ((tree, tree));
104 static struct search_level *push_search_level
105 PARAMS ((struct stack_level *, struct obstack *));
106 static struct search_level *pop_search_level
107 PARAMS ((struct stack_level *));
108 static tree bfs_walk
109 PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
110 void *));
111 static tree lookup_field_queue_p PARAMS ((tree, void *));
112 static int shared_member_p PARAMS ((tree));
113 static tree lookup_field_r PARAMS ((tree, void *));
114 static tree canonical_binfo PARAMS ((tree));
115 static tree shared_marked_p PARAMS ((tree, void *));
116 static tree shared_unmarked_p PARAMS ((tree, void *));
117 static int dependent_base_p PARAMS ((tree));
118 static tree dfs_accessible_queue_p PARAMS ((tree, void *));
119 static tree dfs_accessible_p PARAMS ((tree, void *));
120 static tree dfs_access_in_type PARAMS ((tree, void *));
121 static access_kind access_in_type PARAMS ((tree, tree));
122 static tree dfs_canonical_queue PARAMS ((tree, void *));
123 static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
124 static void assert_canonical_unmarked PARAMS ((tree));
125 static int protected_accessible_p PARAMS ((tree, tree, tree));
126 static int friend_accessible_p PARAMS ((tree, tree, tree));
127 static void setup_class_bindings PARAMS ((tree, int));
128 static int template_self_reference_p PARAMS ((tree, tree));
129 static tree dfs_find_vbase_instance PARAMS ((tree, void *));
130 static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
131 static tree dfs_build_inheritance_graph_order PARAMS ((tree, void *));
133 /* Allocate a level of searching. */
135 static struct search_level *
136 push_search_level (stack, obstack)
137 struct stack_level *stack;
138 struct obstack *obstack;
140 struct search_level tem;
142 tem.prev = stack;
143 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
146 /* Discard a level of search allocation. */
148 static struct search_level *
149 pop_search_level (obstack)
150 struct stack_level *obstack;
152 register struct search_level *stack = pop_stack_level (obstack);
154 return stack;
157 /* Variables for gathering statistics. */
158 #ifdef GATHER_STATISTICS
159 static int n_fields_searched;
160 static int n_calls_lookup_field, n_calls_lookup_field_1;
161 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
162 static int n_calls_get_base_type;
163 static int n_outer_fields_searched;
164 static int n_contexts_saved;
165 #endif /* GATHER_STATISTICS */
168 /* Worker for lookup_base. BINFO is the binfo we are searching at,
169 BASE is the RECORD_TYPE we are searching for. ACCESS is the
170 required access checks. WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
171 IS_VIRTUAL indicate how BINFO was reached from the start of the
172 search. WITHIN_CURRENT_SCOPE is true if we met the current scope,
173 or friend thereof (this allows us to determine whether a protected
174 base is accessible or not). IS_NON_PUBLIC indicates whether BINFO
175 is accessible and IS_VIRTUAL indicates if it is morally virtual.
177 If BINFO is of the required type, then *BINFO_PTR is examined to
178 compare with any other instance of BASE we might have already
179 discovered. *BINFO_PTR is initialized and a base_kind return value
180 indicates what kind of base was located.
182 Otherwise BINFO's bases are searched. */
184 static base_kind
185 lookup_base_r (binfo, base, access, within_current_scope,
186 is_non_public, is_virtual, binfo_ptr)
187 tree binfo, base;
188 base_access access;
189 int within_current_scope;
190 int is_non_public; /* inside a non-public part */
191 int is_virtual; /* inside a virtual part */
192 tree *binfo_ptr;
194 int i;
195 tree bases;
196 base_kind found = bk_not_base;
198 if (access == ba_check
199 && !within_current_scope
200 && is_friend (BINFO_TYPE (binfo), current_scope ()))
202 /* Do not clear is_non_public here. If A is a private base of B, A
203 is not allowed to convert a B* to an A*. */
204 within_current_scope = 1;
207 if (same_type_p (BINFO_TYPE (binfo), base))
209 /* We have found a base. Check against what we have found
210 already. */
211 found = bk_same_type;
212 if (is_virtual)
213 found = bk_via_virtual;
214 if (is_non_public)
215 found = bk_inaccessible;
217 if (!*binfo_ptr)
218 *binfo_ptr = binfo;
219 else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
220 BINFO_OFFSET (*binfo_ptr)))
222 if (access != ba_any)
223 *binfo_ptr = NULL;
224 else if (!is_virtual)
225 /* Prefer a non-virtual base. */
226 *binfo_ptr = binfo;
227 found = bk_ambig;
230 return found;
233 bases = BINFO_BASETYPES (binfo);
234 if (!bases)
235 return bk_not_base;
237 for (i = TREE_VEC_LENGTH (bases); i--;)
239 tree base_binfo = TREE_VEC_ELT (bases, i);
240 int this_non_public = is_non_public;
241 int this_virtual = is_virtual;
242 base_kind bk;
244 if (access <= ba_ignore)
245 ; /* no change */
246 else if (TREE_VIA_PUBLIC (base_binfo))
247 ; /* no change */
248 else if (access == ba_not_special)
249 this_non_public = 1;
250 else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
251 ; /* no change */
252 else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
253 ; /* no change */
254 else
255 this_non_public = 1;
257 if (TREE_VIA_VIRTUAL (base_binfo))
258 this_virtual = 1;
260 bk = lookup_base_r (base_binfo, base,
261 access, within_current_scope,
262 this_non_public, this_virtual,
263 binfo_ptr);
265 switch (bk)
267 case bk_ambig:
268 if (access != ba_any)
269 return bk;
270 found = bk;
271 break;
273 case bk_inaccessible:
274 if (found == bk_not_base)
275 found = bk;
276 my_friendly_assert (found == bk_via_virtual
277 || found == bk_inaccessible, 20010723);
279 break;
281 case bk_same_type:
282 bk = bk_proper_base;
283 /* FALLTHROUGH */
284 case bk_proper_base:
285 my_friendly_assert (found == bk_not_base, 20010723);
286 found = bk;
287 break;
289 case bk_via_virtual:
290 if (found != bk_ambig)
291 found = bk;
292 break;
294 case bk_not_base:
295 break;
298 return found;
301 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
302 ACCESS specifies. Return the binfo we discover (which might not be
303 canonical). If KIND_PTR is non-NULL, fill with information about
304 what kind of base we discovered.
306 If ba_quiet bit is set in ACCESS, then do not issue an error, and
307 return NULL_TREE for failure. */
309 tree
310 lookup_base (t, base, access, kind_ptr)
311 tree t, base;
312 base_access access;
313 base_kind *kind_ptr;
315 tree binfo = NULL; /* The binfo we've found so far. */
316 base_kind bk;
318 if (t == error_mark_node || base == error_mark_node)
320 if (kind_ptr)
321 *kind_ptr = bk_not_base;
322 return error_mark_node;
324 my_friendly_assert (TYPE_P (t) && TYPE_P (base), 20011127);
326 /* Ensure that the types are instantiated. */
327 t = complete_type (TYPE_MAIN_VARIANT (t));
328 base = complete_type (TYPE_MAIN_VARIANT (base));
330 bk = lookup_base_r (TYPE_BINFO (t), base, access & ~ba_quiet,
331 0, 0, 0, &binfo);
333 switch (bk)
335 case bk_inaccessible:
336 binfo = NULL_TREE;
337 if (!(access & ba_quiet))
339 error ("`%T' is an inaccessible base of `%T'", base, t);
340 binfo = error_mark_node;
342 break;
343 case bk_ambig:
344 if (access != ba_any)
346 binfo = NULL_TREE;
347 if (!(access & ba_quiet))
349 error ("`%T' is an ambiguous base of `%T'", base, t);
350 binfo = error_mark_node;
353 break;
354 default:;
357 if (kind_ptr)
358 *kind_ptr = bk;
360 return binfo;
363 /* Worker function for get_dynamic_cast_base_type. */
365 static int
366 dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
367 tree subtype;
368 tree binfo;
369 int via_virtual;
370 tree *offset_ptr;
372 tree binfos;
373 int i, n_baselinks;
374 int worst = -2;
376 if (BINFO_TYPE (binfo) == subtype)
378 if (via_virtual)
379 return -1;
380 else
382 *offset_ptr = BINFO_OFFSET (binfo);
383 return 0;
387 binfos = BINFO_BASETYPES (binfo);
388 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
389 for (i = 0; i < n_baselinks; i++)
391 tree base_binfo = TREE_VEC_ELT (binfos, i);
392 int rval;
394 if (!TREE_VIA_PUBLIC (base_binfo))
395 continue;
396 rval = dynamic_cast_base_recurse
397 (subtype, base_binfo,
398 via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
399 if (worst == -2)
400 worst = rval;
401 else if (rval >= 0)
402 worst = worst >= 0 ? -3 : worst;
403 else if (rval == -1)
404 worst = -1;
405 else if (rval == -3 && worst != -1)
406 worst = -3;
408 return worst;
411 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
412 started from is related to the required TARGET type, in order to optimize
413 the inheritance graph search. This information is independent of the
414 current context, and ignores private paths, hence get_base_distance is
415 inappropriate. Return a TREE specifying the base offset, BOFF.
416 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
417 and there are no public virtual SUBTYPE bases.
418 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
419 BOFF == -2, SUBTYPE is not a public base.
420 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
422 tree
423 get_dynamic_cast_base_type (subtype, target)
424 tree subtype;
425 tree target;
427 tree offset = NULL_TREE;
428 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
429 0, &offset);
431 if (!boff)
432 return offset;
433 offset = build_int_2 (boff, -1);
434 TREE_TYPE (offset) = ssizetype;
435 return offset;
438 /* Search for a member with name NAME in a multiple inheritance lattice
439 specified by TYPE. If it does not exist, return NULL_TREE.
440 If the member is ambiguously referenced, return `error_mark_node'.
441 Otherwise, return the FIELD_DECL. */
443 /* Do a 1-level search for NAME as a member of TYPE. The caller must
444 figure out whether it can access this field. (Since it is only one
445 level, this is reasonable.) */
447 static tree
448 lookup_field_1 (type, name)
449 tree type, name;
451 register tree field;
453 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
454 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
455 || TREE_CODE (type) == TYPENAME_TYPE)
456 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
457 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
458 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
459 the code often worked even when we treated the index as a list
460 of fields!)
461 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
462 return NULL_TREE;
464 if (TYPE_NAME (type)
465 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
466 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
468 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
469 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
470 int i;
472 while (lo < hi)
474 i = (lo + hi) / 2;
476 #ifdef GATHER_STATISTICS
477 n_fields_searched++;
478 #endif /* GATHER_STATISTICS */
480 if (DECL_NAME (fields[i]) > name)
481 hi = i;
482 else if (DECL_NAME (fields[i]) < name)
483 lo = i + 1;
484 else
486 /* We might have a nested class and a field with the
487 same name; we sorted them appropriately via
488 field_decl_cmp, so just look for the last field with
489 this name. */
490 while (i + 1 < hi
491 && DECL_NAME (fields[i+1]) == name)
492 ++i;
493 return fields[i];
496 return NULL_TREE;
499 field = TYPE_FIELDS (type);
501 #ifdef GATHER_STATISTICS
502 n_calls_lookup_field_1++;
503 #endif /* GATHER_STATISTICS */
504 while (field)
506 #ifdef GATHER_STATISTICS
507 n_fields_searched++;
508 #endif /* GATHER_STATISTICS */
509 my_friendly_assert (DECL_P (field), 0);
510 if (DECL_NAME (field) == NULL_TREE
511 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
513 tree temp = lookup_field_1 (TREE_TYPE (field), name);
514 if (temp)
515 return temp;
517 if (TREE_CODE (field) == USING_DECL)
518 /* For now, we're just treating member using declarations as
519 old ARM-style access declarations. Thus, there's no reason
520 to return a USING_DECL, and the rest of the compiler can't
521 handle it. Once the class is defined, these are purged
522 from TYPE_FIELDS anyhow; see handle_using_decl. */
524 else if (DECL_NAME (field) == name)
525 return field;
526 field = TREE_CHAIN (field);
528 /* Not found. */
529 if (name == vptr_identifier)
531 /* Give the user what s/he thinks s/he wants. */
532 if (TYPE_POLYMORPHIC_P (type))
533 return TYPE_VFIELD (type);
535 return NULL_TREE;
538 /* There are a number of cases we need to be aware of here:
539 current_class_type current_function_decl
540 global NULL NULL
541 fn-local NULL SET
542 class-local SET NULL
543 class->fn SET SET
544 fn->class SET SET
546 Those last two make life interesting. If we're in a function which is
547 itself inside a class, we need decls to go into the fn's decls (our
548 second case below). But if we're in a class and the class itself is
549 inside a function, we need decls to go into the decls for the class. To
550 achieve this last goal, we must see if, when both current_class_ptr and
551 current_function_decl are set, the class was declared inside that
552 function. If so, we know to put the decls into the class's scope. */
554 tree
555 current_scope ()
557 if (current_function_decl == NULL_TREE)
558 return current_class_type;
559 if (current_class_type == NULL_TREE)
560 return current_function_decl;
561 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
562 && same_type_p (DECL_CONTEXT (current_function_decl),
563 current_class_type))
564 || (DECL_FRIEND_CONTEXT (current_function_decl)
565 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
566 current_class_type)))
567 return current_function_decl;
569 return current_class_type;
572 /* Returns non-zero if we are currently in a function scope. Note
573 that this function returns zero if we are within a local class, but
574 not within a member function body of the local class. */
577 at_function_scope_p ()
579 tree cs = current_scope ();
580 return cs && TREE_CODE (cs) == FUNCTION_DECL;
583 /* Return the scope of DECL, as appropriate when doing name-lookup. */
585 tree
586 context_for_name_lookup (decl)
587 tree decl;
589 /* [class.union]
591 For the purposes of name lookup, after the anonymous union
592 definition, the members of the anonymous union are considered to
593 have been defined in the scope in which the anonymous union is
594 declared. */
595 tree context = DECL_CONTEXT (decl);
597 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
598 context = TYPE_CONTEXT (context);
599 if (!context)
600 context = global_namespace;
602 return context;
605 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
606 otherwise. */
608 static tree
609 canonical_binfo (binfo)
610 tree binfo;
612 return (TREE_VIA_VIRTUAL (binfo)
613 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
616 /* A queue function that simply ensures that we walk into the
617 canonical versions of virtual bases. */
619 static tree
620 dfs_canonical_queue (binfo, data)
621 tree binfo;
622 void *data ATTRIBUTE_UNUSED;
624 return canonical_binfo (binfo);
627 /* Called via dfs_walk from assert_canonical_unmarked. */
629 static tree
630 dfs_assert_unmarked_p (binfo, data)
631 tree binfo;
632 void *data ATTRIBUTE_UNUSED;
634 my_friendly_assert (!BINFO_MARKED (binfo), 0);
635 return NULL_TREE;
638 /* Asserts that all the nodes below BINFO (using the canonical
639 versions of virtual bases) are unmarked. */
641 static void
642 assert_canonical_unmarked (binfo)
643 tree binfo;
645 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
648 /* If BINFO is marked, return a canonical version of BINFO.
649 Otherwise, return NULL_TREE. */
651 static tree
652 shared_marked_p (binfo, data)
653 tree binfo;
654 void *data;
656 binfo = canonical_binfo (binfo);
657 return markedp (binfo, data);
660 /* If BINFO is not marked, return a canonical version of BINFO.
661 Otherwise, return NULL_TREE. */
663 static tree
664 shared_unmarked_p (binfo, data)
665 tree binfo;
666 void *data;
668 binfo = canonical_binfo (binfo);
669 return unmarkedp (binfo, data);
672 /* The accessibility routines use BINFO_ACCESS for scratch space
673 during the computation of the accssibility of some declaration. */
675 #define BINFO_ACCESS(NODE) \
676 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
678 /* Set the access associated with NODE to ACCESS. */
680 #define SET_BINFO_ACCESS(NODE, ACCESS) \
681 ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0), \
682 (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
684 /* Called from access_in_type via dfs_walk. Calculate the access to
685 DATA (which is really a DECL) in BINFO. */
687 static tree
688 dfs_access_in_type (binfo, data)
689 tree binfo;
690 void *data;
692 tree decl = (tree) data;
693 tree type = BINFO_TYPE (binfo);
694 access_kind access = ak_none;
696 if (context_for_name_lookup (decl) == type)
698 /* If we have desceneded to the scope of DECL, just note the
699 appropriate access. */
700 if (TREE_PRIVATE (decl))
701 access = ak_private;
702 else if (TREE_PROTECTED (decl))
703 access = ak_protected;
704 else
705 access = ak_public;
707 else
709 /* First, check for an access-declaration that gives us more
710 access to the DECL. The CONST_DECL for an enumeration
711 constant will not have DECL_LANG_SPECIFIC, and thus no
712 DECL_ACCESS. */
713 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
715 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
716 if (decl_access)
717 access = ((access_kind)
718 TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
721 if (!access)
723 int i;
724 int n_baselinks;
725 tree binfos;
727 /* Otherwise, scan our baseclasses, and pick the most favorable
728 access. */
729 binfos = BINFO_BASETYPES (binfo);
730 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
731 for (i = 0; i < n_baselinks; ++i)
733 tree base_binfo = TREE_VEC_ELT (binfos, i);
734 access_kind base_access
735 = BINFO_ACCESS (canonical_binfo (base_binfo));
737 if (base_access == ak_none || base_access == ak_private)
738 /* If it was not accessible in the base, or only
739 accessible as a private member, we can't access it
740 all. */
741 base_access = ak_none;
742 else if (TREE_VIA_PROTECTED (base_binfo))
743 /* Public and protected members in the base are
744 protected here. */
745 base_access = ak_protected;
746 else if (!TREE_VIA_PUBLIC (base_binfo))
747 /* Public and protected members in the base are
748 private here. */
749 base_access = ak_private;
751 /* See if the new access, via this base, gives more
752 access than our previous best access. */
753 if (base_access != ak_none
754 && (base_access == ak_public
755 || (base_access == ak_protected
756 && access != ak_public)
757 || (base_access == ak_private
758 && access == ak_none)))
760 access = base_access;
762 /* If the new access is public, we can't do better. */
763 if (access == ak_public)
764 break;
770 /* Note the access to DECL in TYPE. */
771 SET_BINFO_ACCESS (binfo, access);
773 /* Mark TYPE as visited so that if we reach it again we do not
774 duplicate our efforts here. */
775 SET_BINFO_MARKED (binfo);
777 return NULL_TREE;
780 /* Return the access to DECL in TYPE. */
782 static access_kind
783 access_in_type (type, decl)
784 tree type;
785 tree decl;
787 tree binfo = TYPE_BINFO (type);
789 /* We must take into account
791 [class.paths]
793 If a name can be reached by several paths through a multiple
794 inheritance graph, the access is that of the path that gives
795 most access.
797 The algorithm we use is to make a post-order depth-first traversal
798 of the base-class hierarchy. As we come up the tree, we annotate
799 each node with the most lenient access. */
800 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
801 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
802 assert_canonical_unmarked (binfo);
804 return BINFO_ACCESS (binfo);
807 /* Called from dfs_accessible_p via dfs_walk. */
809 static tree
810 dfs_accessible_queue_p (binfo, data)
811 tree binfo;
812 void *data ATTRIBUTE_UNUSED;
814 if (BINFO_MARKED (binfo))
815 return NULL_TREE;
817 /* If this class is inherited via private or protected inheritance,
818 then we can't see it, unless we are a friend of the subclass. */
819 if (!TREE_VIA_PUBLIC (binfo)
820 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
821 current_scope ()))
822 return NULL_TREE;
824 return canonical_binfo (binfo);
827 /* Called from dfs_accessible_p via dfs_walk. */
829 static tree
830 dfs_accessible_p (binfo, data)
831 tree binfo;
832 void *data;
834 int protected_ok = data != 0;
835 access_kind access;
837 SET_BINFO_MARKED (binfo);
838 access = BINFO_ACCESS (binfo);
839 if (access == ak_public || (access == ak_protected && protected_ok))
840 return binfo;
841 else if (access != ak_none
842 && is_friend (BINFO_TYPE (binfo), current_scope ()))
843 return binfo;
845 return NULL_TREE;
848 /* Returns non-zero if it is OK to access DECL through an object
849 indiated by BINFO in the context of DERIVED. */
851 static int
852 protected_accessible_p (decl, derived, binfo)
853 tree decl;
854 tree derived;
855 tree binfo;
857 access_kind access;
859 /* We're checking this clause from [class.access.base]
861 m as a member of N is protected, and the reference occurs in a
862 member or friend of class N, or in a member or friend of a
863 class P derived from N, where m as a member of P is private or
864 protected.
866 Here DERIVED is a possible P and DECL is m. accessible_p will
867 iterate over various values of N, but the access to m in DERIVED
868 does not change.
870 Note that I believe that the passage above is wrong, and should read
871 "...is private or protected or public"; otherwise you get bizarre results
872 whereby a public using-decl can prevent you from accessing a protected
873 member of a base. (jason 2000/02/28) */
875 /* If DERIVED isn't derived from m's class, then it can't be a P. */
876 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
877 return 0;
879 access = access_in_type (derived, decl);
881 /* If m is inaccessible in DERIVED, then it's not a P. */
882 if (access == ak_none)
883 return 0;
885 /* [class.protected]
887 When a friend or a member function of a derived class references
888 a protected nonstatic member of a base class, an access check
889 applies in addition to those described earlier in clause
890 _class.access_) Except when forming a pointer to member
891 (_expr.unary.op_), the access must be through a pointer to,
892 reference to, or object of the derived class itself (or any class
893 derived from that class) (_expr.ref_). If the access is to form
894 a pointer to member, the nested-name-specifier shall name the
895 derived class (or any class derived from that class). */
896 if (DECL_NONSTATIC_MEMBER_P (decl))
898 /* We can tell through what the reference is occurring by
899 chasing BINFO up to the root. */
900 tree t = binfo;
901 while (BINFO_INHERITANCE_CHAIN (t))
902 t = BINFO_INHERITANCE_CHAIN (t);
904 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
905 return 0;
908 return 1;
911 /* Returns non-zero if SCOPE is a friend of a type which would be able
912 to access DECL through the object indicated by BINFO. */
914 static int
915 friend_accessible_p (scope, decl, binfo)
916 tree scope;
917 tree decl;
918 tree binfo;
920 tree befriending_classes;
921 tree t;
923 if (!scope)
924 return 0;
926 if (TREE_CODE (scope) == FUNCTION_DECL
927 || DECL_FUNCTION_TEMPLATE_P (scope))
928 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
929 else if (TYPE_P (scope))
930 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
931 else
932 return 0;
934 for (t = befriending_classes; t; t = TREE_CHAIN (t))
935 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
936 return 1;
938 /* Nested classes are implicitly friends of their enclosing types, as
939 per core issue 45 (this is a change from the standard). */
940 if (TYPE_P (scope))
941 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
942 if (protected_accessible_p (decl, t, binfo))
943 return 1;
945 if (TREE_CODE (scope) == FUNCTION_DECL
946 || DECL_FUNCTION_TEMPLATE_P (scope))
948 /* Perhaps this SCOPE is a member of a class which is a
949 friend. */
950 if (DECL_CLASS_SCOPE_P (decl)
951 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
952 return 1;
954 /* Or an instantiation of something which is a friend. */
955 if (DECL_TEMPLATE_INFO (scope))
956 return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
958 else if (CLASSTYPE_TEMPLATE_INFO (scope))
959 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
961 return 0;
964 /* Perform access control on TYPE_DECL VAL, which was looked up in TYPE.
965 This is fairly complex, so here's the design:
967 The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
968 start to process a top-level declaration.
969 As we process the decl-specifier-seq for the declaration, any types we
970 see that might need access control are passed to type_access_control,
971 which defers checking by adding them to type_lookups.
972 When we are done with the decl-specifier-seq, we record the lookups we've
973 seen in the lookups field of the typed_declspecs nonterminal.
974 When we process the first declarator, either in parse_decl or
975 begin_function_definition, we call save_type_access_control,
976 which stores the lookups from the decl-specifier-seq in
977 current_type_lookups.
978 As we finish with each declarator, we process everything in type_lookups
979 via decl_type_access_control, which resets type_lookups to the value of
980 current_type_lookups for subsequent declarators.
981 When we enter a function, we set type_lookups to error_mark_node, so all
982 lookups are processed immediately. */
984 void
985 type_access_control (type, val)
986 tree type, val;
988 if (val == NULL_TREE || TREE_CODE (val) != TYPE_DECL
989 || ! DECL_CLASS_SCOPE_P (val))
990 return;
992 if (type_lookups == error_mark_node)
993 enforce_access (type, val);
994 else if (! accessible_p (type, val))
995 type_lookups = tree_cons (type, val, type_lookups);
998 /* DECL is a declaration from a base class of TYPE, which was the
999 class used to name DECL. Return non-zero if, in the current
1000 context, DECL is accessible. If TYPE is actually a BINFO node,
1001 then we can tell in what context the access is occurring by looking
1002 at the most derived class along the path indicated by BINFO. */
1004 int
1005 accessible_p (type, decl)
1006 tree type;
1007 tree decl;
1010 tree binfo;
1011 tree t;
1013 /* Non-zero if it's OK to access DECL if it has protected
1014 accessibility in TYPE. */
1015 int protected_ok = 0;
1017 /* If we're not checking access, everything is accessible. */
1018 if (!flag_access_control)
1019 return 1;
1021 /* If this declaration is in a block or namespace scope, there's no
1022 access control. */
1023 if (!TYPE_P (context_for_name_lookup (decl)))
1024 return 1;
1026 if (!TYPE_P (type))
1028 binfo = type;
1029 type = BINFO_TYPE (type);
1031 else
1032 binfo = TYPE_BINFO (type);
1034 /* [class.access.base]
1036 A member m is accessible when named in class N if
1038 --m as a member of N is public, or
1040 --m as a member of N is private, and the reference occurs in a
1041 member or friend of class N, or
1043 --m as a member of N is protected, and the reference occurs in a
1044 member or friend of class N, or in a member or friend of a
1045 class P derived from N, where m as a member of P is private or
1046 protected, or
1048 --there exists a base class B of N that is accessible at the point
1049 of reference, and m is accessible when named in class B.
1051 We walk the base class hierarchy, checking these conditions. */
1053 /* Figure out where the reference is occurring. Check to see if
1054 DECL is private or protected in this scope, since that will
1055 determine whether protected access is allowed. */
1056 if (current_class_type)
1057 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
1059 /* Now, loop through the classes of which we are a friend. */
1060 if (!protected_ok)
1061 protected_ok = friend_accessible_p (current_scope (), decl, binfo);
1063 /* Standardize the binfo that access_in_type will use. We don't
1064 need to know what path was chosen from this point onwards. */
1065 binfo = TYPE_BINFO (type);
1067 /* Compute the accessibility of DECL in the class hierarchy
1068 dominated by type. */
1069 access_in_type (type, decl);
1070 /* Walk the hierarchy again, looking for a base class that allows
1071 access. */
1072 t = dfs_walk (binfo, dfs_accessible_p,
1073 dfs_accessible_queue_p,
1074 protected_ok ? &protected_ok : 0);
1075 /* Clear any mark bits. Note that we have to walk the whole tree
1076 here, since we have aborted the previous walk from some point
1077 deep in the tree. */
1078 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1079 assert_canonical_unmarked (binfo);
1081 return t != NULL_TREE;
1084 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1085 found as a base class and sub-object of the object denoted by
1086 BINFO. MOST_DERIVED is the most derived type of the hierarchy being
1087 searched. */
1089 static int
1090 is_subobject_of_p (parent, binfo, most_derived)
1091 tree parent, binfo, most_derived;
1093 tree binfos;
1094 int i, n_baselinks;
1096 if (parent == binfo)
1097 return 1;
1099 binfos = BINFO_BASETYPES (binfo);
1100 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1102 /* Iterate the base types. */
1103 for (i = 0; i < n_baselinks; i++)
1105 tree base_binfo = TREE_VEC_ELT (binfos, i);
1106 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo)))
1107 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1108 class there's no way to descend into it. */
1109 continue;
1111 if (is_subobject_of_p (parent,
1112 CANONICAL_BINFO (base_binfo, most_derived),
1113 most_derived))
1114 return 1;
1116 return 0;
1119 struct lookup_field_info {
1120 /* The type in which we're looking. */
1121 tree type;
1122 /* The name of the field for which we're looking. */
1123 tree name;
1124 /* If non-NULL, the current result of the lookup. */
1125 tree rval;
1126 /* The path to RVAL. */
1127 tree rval_binfo;
1128 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1129 candidates. */
1130 tree ambiguous;
1131 /* If non-zero, we are looking for types, not data members. */
1132 int want_type;
1133 /* If non-zero, RVAL was found by looking through a dependent base. */
1134 int from_dep_base_p;
1135 /* If something went wrong, a message indicating what. */
1136 const char *errstr;
1139 /* Returns non-zero if BINFO is not hidden by the value found by the
1140 lookup so far. If BINFO is hidden, then there's no need to look in
1141 it. DATA is really a struct lookup_field_info. Called from
1142 lookup_field via breadth_first_search. */
1144 static tree
1145 lookup_field_queue_p (binfo, data)
1146 tree binfo;
1147 void *data;
1149 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1151 /* Don't look for constructors or destructors in base classes. */
1152 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1153 return NULL_TREE;
1155 /* If this base class is hidden by the best-known value so far, we
1156 don't need to look. */
1157 if (!lfi->from_dep_base_p && lfi->rval_binfo
1158 && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1159 return NULL_TREE;
1161 return CANONICAL_BINFO (binfo, lfi->type);
1164 /* Within the scope of a template class, you can refer to the to the
1165 current specialization with the name of the template itself. For
1166 example:
1168 template <typename T> struct S { S* sp; }
1170 Returns non-zero if DECL is such a declaration in a class TYPE. */
1172 static int
1173 template_self_reference_p (type, decl)
1174 tree type;
1175 tree decl;
1177 return (CLASSTYPE_USE_TEMPLATE (type)
1178 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
1179 && TREE_CODE (decl) == TYPE_DECL
1180 && DECL_ARTIFICIAL (decl)
1181 && DECL_NAME (decl) == constructor_name (type));
1185 /* Nonzero for a class member means that it is shared between all objects
1186 of that class.
1188 [class.member.lookup]:If the resulting set of declarations are not all
1189 from sub-objects of the same type, or the set has a nonstatic member
1190 and includes members from distinct sub-objects, there is an ambiguity
1191 and the program is ill-formed.
1193 This function checks that T contains no nonstatic members. */
1195 static int
1196 shared_member_p (t)
1197 tree t;
1199 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
1200 || TREE_CODE (t) == CONST_DECL)
1201 return 1;
1202 if (is_overloaded_fn (t))
1204 for (; t; t = OVL_NEXT (t))
1206 tree fn = OVL_CURRENT (t);
1207 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1208 return 0;
1210 return 1;
1212 return 0;
1215 /* DATA is really a struct lookup_field_info. Look for a field with
1216 the name indicated there in BINFO. If this function returns a
1217 non-NULL value it is the result of the lookup. Called from
1218 lookup_field via breadth_first_search. */
1220 static tree
1221 lookup_field_r (binfo, data)
1222 tree binfo;
1223 void *data;
1225 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1226 tree type = BINFO_TYPE (binfo);
1227 tree nval = NULL_TREE;
1228 int from_dep_base_p;
1230 /* First, look for a function. There can't be a function and a data
1231 member with the same name, and if there's a function and a type
1232 with the same name, the type is hidden by the function. */
1233 if (!lfi->want_type)
1235 int idx = lookup_fnfields_1 (type, lfi->name);
1236 if (idx >= 0)
1237 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1240 if (!nval)
1241 /* Look for a data member or type. */
1242 nval = lookup_field_1 (type, lfi->name);
1244 /* If there is no declaration with the indicated name in this type,
1245 then there's nothing to do. */
1246 if (!nval)
1247 return NULL_TREE;
1249 /* If we're looking up a type (as with an elaborated type specifier)
1250 we ignore all non-types we find. */
1251 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1252 && !DECL_CLASS_TEMPLATE_P (nval))
1254 if (lfi->name == TYPE_IDENTIFIER (type))
1256 /* If the aggregate has no user defined constructors, we allow
1257 it to have fields with the same name as the enclosing type.
1258 If we are looking for that name, find the corresponding
1259 TYPE_DECL. */
1260 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1261 if (DECL_NAME (nval) == lfi->name
1262 && TREE_CODE (nval) == TYPE_DECL)
1263 break;
1265 else
1266 nval = NULL_TREE;
1267 if (!nval)
1269 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1270 if (nval)
1271 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1272 else
1273 return NULL_TREE;
1277 /* You must name a template base class with a template-id. */
1278 if (!same_type_p (type, lfi->type)
1279 && template_self_reference_p (type, nval))
1280 return NULL_TREE;
1282 from_dep_base_p = dependent_base_p (binfo);
1283 if (lfi->from_dep_base_p && !from_dep_base_p)
1285 /* If the new declaration is not found via a dependent base, and
1286 the old one was, then we must prefer the new one. We weren't
1287 really supposed to be able to find the old one, so we don't
1288 want to be affected by a specialization. Consider:
1290 struct B { typedef int I; };
1291 template <typename T> struct D1 : virtual public B {};
1292 template <typename T> struct D :
1293 public D1, virtual pubic B { I i; };
1295 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1296 D1 is specialized. */
1297 lfi->from_dep_base_p = 0;
1298 lfi->rval = NULL_TREE;
1299 lfi->rval_binfo = NULL_TREE;
1300 lfi->ambiguous = NULL_TREE;
1301 lfi->errstr = 0;
1303 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1304 /* Similarly, if the old declaration was not found via a dependent
1305 base, and the new one is, ignore the new one. */
1306 return NULL_TREE;
1308 /* If the lookup already found a match, and the new value doesn't
1309 hide the old one, we might have an ambiguity. */
1310 if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
1312 if (nval == lfi->rval && shared_member_p (nval))
1313 /* The two things are really the same. */
1315 else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1316 /* The previous value hides the new one. */
1318 else
1320 /* We have a real ambiguity. We keep a chain of all the
1321 candidates. */
1322 if (!lfi->ambiguous && lfi->rval)
1324 /* This is the first time we noticed an ambiguity. Add
1325 what we previously thought was a reasonable candidate
1326 to the list. */
1327 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1328 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1331 /* Add the new value. */
1332 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1333 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1334 lfi->errstr = "request for member `%D' is ambiguous";
1337 else
1339 if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
1340 /* We need to return a member template class so we can
1341 define partial specializations. Is there a better
1342 way? */
1343 && !DECL_CLASS_TEMPLATE_P (nval))
1344 /* The thing we're looking for isn't a type, so the implicit
1345 typename extension doesn't apply, so we just pretend we
1346 didn't find anything. */
1347 return NULL_TREE;
1349 lfi->rval = nval;
1350 lfi->from_dep_base_p = from_dep_base_p;
1351 lfi->rval_binfo = binfo;
1354 return NULL_TREE;
1357 /* Look for a member named NAME in an inheritance lattice dominated by
1358 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it is
1359 1, we enforce accessibility. If PROTECT is zero, then, for an
1360 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
1361 error message. If PROTECT is 2, we return a TREE_LIST whose
1362 TREE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
1363 ambiguous candidates.
1365 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1366 TYPE_DECL can be found return NULL_TREE. */
1368 tree
1369 lookup_member (xbasetype, name, protect, want_type)
1370 register tree xbasetype, name;
1371 int protect, want_type;
1373 tree rval, rval_binfo = NULL_TREE;
1374 tree type = NULL_TREE, basetype_path = NULL_TREE;
1375 struct lookup_field_info lfi;
1377 /* rval_binfo is the binfo associated with the found member, note,
1378 this can be set with useful information, even when rval is not
1379 set, because it must deal with ALL members, not just non-function
1380 members. It is used for ambiguity checking and the hidden
1381 checks. Whereas rval is only set if a proper (not hidden)
1382 non-function member is found. */
1384 const char *errstr = 0;
1386 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1387 && IDENTIFIER_CLASS_VALUE (name))
1389 tree field = IDENTIFIER_CLASS_VALUE (name);
1390 if (TREE_CODE (field) != FUNCTION_DECL
1391 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1392 /* We're in the scope of this class, and the value has already
1393 been looked up. Just return the cached value. */
1394 return field;
1397 if (TREE_CODE (xbasetype) == TREE_VEC)
1399 type = BINFO_TYPE (xbasetype);
1400 basetype_path = xbasetype;
1402 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1404 type = xbasetype;
1405 basetype_path = TYPE_BINFO (type);
1406 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1407 980827);
1409 else
1410 abort ();
1412 complete_type (type);
1414 #ifdef GATHER_STATISTICS
1415 n_calls_lookup_field++;
1416 #endif /* GATHER_STATISTICS */
1418 memset ((PTR) &lfi, 0, sizeof (lfi));
1419 lfi.type = type;
1420 lfi.name = name;
1421 lfi.want_type = want_type;
1422 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
1423 rval = lfi.rval;
1424 rval_binfo = lfi.rval_binfo;
1425 if (rval_binfo)
1426 type = BINFO_TYPE (rval_binfo);
1427 errstr = lfi.errstr;
1429 /* If we are not interested in ambiguities, don't report them;
1430 just return NULL_TREE. */
1431 if (!protect && lfi.ambiguous)
1432 return NULL_TREE;
1434 if (protect == 2)
1436 if (lfi.ambiguous)
1437 return lfi.ambiguous;
1438 else
1439 protect = 0;
1442 /* [class.access]
1444 In the case of overloaded function names, access control is
1445 applied to the function selected by overloaded resolution. */
1446 if (rval && protect && !is_overloaded_fn (rval)
1447 && !enforce_access (xbasetype, rval))
1448 return error_mark_node;
1450 if (errstr && protect)
1452 error (errstr, name, type);
1453 if (lfi.ambiguous)
1454 print_candidates (lfi.ambiguous);
1455 rval = error_mark_node;
1458 /* If the thing we found was found via the implicit typename
1459 extension, build the typename type. */
1460 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1461 rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
1462 name, name,
1463 TREE_TYPE (rval)));
1465 if (rval && is_overloaded_fn (rval))
1467 /* Note that the binfo we put in the baselink is the binfo where
1468 we found the functions, which we need for overload
1469 resolution, but which should not be passed to enforce_access;
1470 rather, enforce_access wants a binfo which refers to the
1471 scope in which we started looking for the function. This
1472 will generally be the binfo passed into this function as
1473 xbasetype. */
1475 rval = tree_cons (rval_binfo, rval, NULL_TREE);
1476 SET_BASELINK_P (rval);
1479 return rval;
1482 /* Like lookup_member, except that if we find a function member we
1483 return NULL_TREE. */
1485 tree
1486 lookup_field (xbasetype, name, protect, want_type)
1487 register tree xbasetype, name;
1488 int protect, want_type;
1490 tree rval = lookup_member (xbasetype, name, protect, want_type);
1492 /* Ignore functions. */
1493 if (rval && TREE_CODE (rval) == TREE_LIST)
1494 return NULL_TREE;
1496 return rval;
1499 /* Like lookup_member, except that if we find a non-function member we
1500 return NULL_TREE. */
1502 tree
1503 lookup_fnfields (xbasetype, name, protect)
1504 register tree xbasetype, name;
1505 int protect;
1507 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1509 /* Ignore non-functions. */
1510 if (rval && TREE_CODE (rval) != TREE_LIST)
1511 return NULL_TREE;
1513 return rval;
1516 /* TYPE is a class type. Return the index of the fields within
1517 the method vector with name NAME, or -1 is no such field exists. */
1520 lookup_fnfields_1 (type, name)
1521 tree type, name;
1523 tree method_vec = (CLASS_TYPE_P (type)
1524 ? CLASSTYPE_METHOD_VEC (type)
1525 : NULL_TREE);
1527 if (method_vec != 0)
1529 register int i;
1530 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1531 int len = TREE_VEC_LENGTH (method_vec);
1532 tree tmp;
1534 #ifdef GATHER_STATISTICS
1535 n_calls_lookup_fnfields_1++;
1536 #endif /* GATHER_STATISTICS */
1538 /* Constructors are first... */
1539 if (name == ctor_identifier)
1540 return (methods[CLASSTYPE_CONSTRUCTOR_SLOT]
1541 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
1542 /* and destructors are second. */
1543 if (name == dtor_identifier)
1544 return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1545 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
1547 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1548 i < len && methods[i];
1549 ++i)
1551 #ifdef GATHER_STATISTICS
1552 n_outer_fields_searched++;
1553 #endif /* GATHER_STATISTICS */
1555 tmp = OVL_CURRENT (methods[i]);
1556 if (DECL_NAME (tmp) == name)
1557 return i;
1559 /* If the type is complete and we're past the conversion ops,
1560 switch to binary search. */
1561 if (! DECL_CONV_FN_P (tmp)
1562 && COMPLETE_TYPE_P (type))
1564 int lo = i + 1, hi = len;
1566 while (lo < hi)
1568 i = (lo + hi) / 2;
1570 #ifdef GATHER_STATISTICS
1571 n_outer_fields_searched++;
1572 #endif /* GATHER_STATISTICS */
1574 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1576 if (tmp > name)
1577 hi = i;
1578 else if (tmp < name)
1579 lo = i + 1;
1580 else
1581 return i;
1583 break;
1587 /* If we didn't find it, it might have been a template
1588 conversion operator to a templated type. If there are any,
1589 such template conversion operators will all be overloaded on
1590 the first conversion slot. (Note that we don't look for this
1591 case above so that we will always find specializations
1592 first.) */
1593 if (IDENTIFIER_TYPENAME_P (name))
1595 i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1596 if (i < len && methods[i])
1598 tmp = OVL_CURRENT (methods[i]);
1599 if (TREE_CODE (tmp) == TEMPLATE_DECL
1600 && DECL_TEMPLATE_CONV_FN_P (tmp))
1601 return i;
1606 return -1;
1609 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1610 type in the hierarchy, in a breadth-first preorder traversal.
1611 If it ever returns a non-NULL value, that value is immediately
1612 returned and the walk is terminated. At each node, FN is passed a
1613 BINFO indicating the path from the curently visited base-class to
1614 TYPE. Before each base-class is walked QFN is called. If the
1615 value returned is non-zero, the base-class is walked; otherwise it
1616 is not. If QFN is NULL, it is treated as a function which always
1617 returns 1. Both FN and QFN are passed the DATA whenever they are
1618 called. */
1620 static tree
1621 bfs_walk (binfo, fn, qfn, data)
1622 tree binfo;
1623 tree (*fn) PARAMS ((tree, void *));
1624 tree (*qfn) PARAMS ((tree, void *));
1625 void *data;
1627 size_t head;
1628 size_t tail;
1629 tree rval = NULL_TREE;
1630 /* An array of the base classes of BINFO. These will be built up in
1631 breadth-first order, except where QFN prunes the search. */
1632 varray_type bfs_bases;
1634 /* Start with enough room for ten base classes. That will be enough
1635 for most hierarchies. */
1636 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
1638 /* Put the first type into the stack. */
1639 VARRAY_TREE (bfs_bases, 0) = binfo;
1640 tail = 1;
1642 for (head = 0; head < tail; ++head)
1644 int i;
1645 int n_baselinks;
1646 tree binfos;
1648 /* Pull the next type out of the queue. */
1649 binfo = VARRAY_TREE (bfs_bases, head);
1651 /* If this is the one we're looking for, we're done. */
1652 rval = (*fn) (binfo, data);
1653 if (rval)
1654 break;
1656 /* Queue up the base types. */
1657 binfos = BINFO_BASETYPES (binfo);
1658 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1659 for (i = 0; i < n_baselinks; i++)
1661 tree base_binfo = TREE_VEC_ELT (binfos, i);
1663 if (qfn)
1664 base_binfo = (*qfn) (base_binfo, data);
1666 if (base_binfo)
1668 if (tail == VARRAY_SIZE (bfs_bases))
1669 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1670 VARRAY_TREE (bfs_bases, tail) = base_binfo;
1671 ++tail;
1676 /* Clean up. */
1677 VARRAY_FREE (bfs_bases);
1679 return rval;
1682 /* Exactly like bfs_walk, except that a depth-first traversal is
1683 performed, and PREFN is called in preorder, while POSTFN is called
1684 in postorder. */
1686 tree
1687 dfs_walk_real (binfo, prefn, postfn, qfn, data)
1688 tree binfo;
1689 tree (*prefn) PARAMS ((tree, void *));
1690 tree (*postfn) PARAMS ((tree, void *));
1691 tree (*qfn) PARAMS ((tree, void *));
1692 void *data;
1694 int i;
1695 int n_baselinks;
1696 tree binfos;
1697 tree rval = NULL_TREE;
1699 /* Call the pre-order walking function. */
1700 if (prefn)
1702 rval = (*prefn) (binfo, data);
1703 if (rval)
1704 return rval;
1707 /* Process the basetypes. */
1708 binfos = BINFO_BASETYPES (binfo);
1709 n_baselinks = BINFO_N_BASETYPES (binfo);
1710 for (i = 0; i < n_baselinks; i++)
1712 tree base_binfo = TREE_VEC_ELT (binfos, i);
1714 if (qfn)
1715 base_binfo = (*qfn) (base_binfo, data);
1717 if (base_binfo)
1719 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1720 if (rval)
1721 return rval;
1725 /* Call the post-order walking function. */
1726 if (postfn)
1727 rval = (*postfn) (binfo, data);
1729 return rval;
1732 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1733 performed. */
1735 tree
1736 dfs_walk (binfo, fn, qfn, data)
1737 tree binfo;
1738 tree (*fn) PARAMS ((tree, void *));
1739 tree (*qfn) PARAMS ((tree, void *));
1740 void *data;
1742 return dfs_walk_real (binfo, 0, fn, qfn, data);
1745 /* Returns > 0 if a function with type DRETTYPE overriding a function
1746 with type BRETTYPE is covariant, as defined in [class.virtual].
1748 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1749 adjustment), or -1 if pedantically invalid covariance. */
1751 static int
1752 covariant_return_p (brettype, drettype)
1753 tree brettype, drettype;
1755 tree binfo;
1756 base_kind kind;
1758 if (TREE_CODE (brettype) == FUNCTION_DECL)
1760 brettype = TREE_TYPE (TREE_TYPE (brettype));
1761 drettype = TREE_TYPE (TREE_TYPE (drettype));
1763 else if (TREE_CODE (brettype) == METHOD_TYPE)
1765 brettype = TREE_TYPE (brettype);
1766 drettype = TREE_TYPE (drettype);
1769 if (same_type_p (brettype, drettype))
1770 return 0;
1772 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1773 && (TREE_CODE (brettype) == POINTER_TYPE
1774 || TREE_CODE (brettype) == REFERENCE_TYPE)
1775 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
1776 return 0;
1778 if (! can_convert (brettype, drettype))
1779 return 0;
1781 brettype = TREE_TYPE (brettype);
1782 drettype = TREE_TYPE (drettype);
1784 /* If not pedantic, allow any standard pointer conversion. */
1785 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
1786 return -1;
1788 binfo = lookup_base (drettype, brettype, ba_check | ba_quiet, &kind);
1790 if (!binfo)
1791 return 0;
1792 if (BINFO_OFFSET_ZEROP (binfo) && kind != bk_via_virtual)
1793 return 1;
1794 return 2;
1797 /* Check that virtual overrider OVERRIDER is acceptable for base function
1798 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1801 check_final_overrider (overrider, basefn)
1802 tree overrider, basefn;
1804 tree over_type = TREE_TYPE (overrider);
1805 tree base_type = TREE_TYPE (basefn);
1806 tree over_return = TREE_TYPE (over_type);
1807 tree base_return = TREE_TYPE (base_type);
1808 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1809 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1810 int i;
1812 if (same_type_p (base_return, over_return))
1813 /* OK */;
1814 else if ((i = covariant_return_p (base_return, over_return)))
1816 if (i == 2)
1817 sorry ("adjusting pointers for covariant returns");
1819 if (pedantic && i == -1)
1821 cp_pedwarn_at ("invalid covariant return type for `%#D'", overrider);
1822 cp_pedwarn_at (" overriding `%#D' (must be pointer or reference to class)", basefn);
1825 else if (IS_AGGR_TYPE_2 (base_return, over_return)
1826 && same_or_base_type_p (base_return, over_return))
1828 cp_error_at ("invalid covariant return type for `%#D'", overrider);
1829 cp_error_at (" overriding `%#D' (must use pointer or reference)", basefn);
1830 return 0;
1832 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)) == NULL_TREE)
1834 cp_error_at ("conflicting return type specified for `%#D'", overrider);
1835 cp_error_at (" overriding `%#D'", basefn);
1836 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1837 DECL_CONTEXT (overrider));
1838 return 0;
1841 /* Check throw specifier is at least as strict. */
1842 if (!comp_except_specs (base_throw, over_throw, 0))
1844 cp_error_at ("looser throw specifier for `%#F'", overrider);
1845 cp_error_at (" overriding `%#F'", basefn);
1846 return 0;
1848 return 1;
1851 /* Given a class TYPE, and a function decl FNDECL, look for
1852 virtual functions in TYPE's hierarchy which FNDECL overrides.
1853 We do not look in TYPE itself, only its bases.
1855 Returns non-zero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1856 find that it overrides anything.
1858 We check that every function which is overridden, is correctly
1859 overridden. */
1862 look_for_overrides (type, fndecl)
1863 tree type, fndecl;
1865 tree binfo = TYPE_BINFO (type);
1866 tree basebinfos = BINFO_BASETYPES (binfo);
1867 int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
1868 int ix;
1869 int found = 0;
1871 for (ix = 0; ix != nbasebinfos; ix++)
1873 tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
1875 if (TYPE_POLYMORPHIC_P (basetype))
1876 found += look_for_overrides_r (basetype, fndecl);
1878 return found;
1881 /* Look in TYPE for virtual functions with the same signature as FNDECL.
1882 This differs from get_matching_virtual in that it will only return
1883 a function from TYPE. */
1885 tree
1886 look_for_overrides_here (type, fndecl)
1887 tree type, fndecl;
1889 int ix;
1891 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
1892 ix = CLASSTYPE_DESTRUCTOR_SLOT;
1893 else
1894 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
1895 if (ix >= 0)
1897 tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
1899 for (; fns; fns = OVL_NEXT (fns))
1901 tree fn = OVL_CURRENT (fns);
1903 if (!DECL_VIRTUAL_P (fn))
1904 /* Not a virtual. */;
1905 else if (DECL_CONTEXT (fn) != type)
1906 /* Introduced with a using declaration. */;
1907 else if (DECL_STATIC_FUNCTION_P (fndecl))
1909 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1910 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1911 if (compparms (TREE_CHAIN (btypes), dtypes))
1912 return fn;
1914 else if (same_signature_p (fndecl, fn))
1915 return fn;
1918 return NULL_TREE;
1921 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1922 TYPE itself and its bases. */
1924 static int
1925 look_for_overrides_r (type, fndecl)
1926 tree type, fndecl;
1928 tree fn = look_for_overrides_here (type, fndecl);
1929 if (fn)
1931 if (DECL_STATIC_FUNCTION_P (fndecl))
1933 /* A static member function cannot match an inherited
1934 virtual member function. */
1935 cp_error_at ("`%#D' cannot be declared", fndecl);
1936 cp_error_at (" since `%#D' declared in base class", fn);
1938 else
1940 /* It's definitely virtual, even if not explicitly set. */
1941 DECL_VIRTUAL_P (fndecl) = 1;
1942 check_final_overrider (fndecl, fn);
1944 return 1;
1947 /* We failed to find one declared in this class. Look in its bases. */
1948 return look_for_overrides (type, fndecl);
1951 /* A queue function to use with dfs_walk that only walks into
1952 canonical bases. DATA should be the type of the complete object,
1953 or a TREE_LIST whose TREE_PURPOSE is the type of the complete
1954 object. By using this function as a queue function, you will walk
1955 over exactly those BINFOs that actually exist in the complete
1956 object, including those for virtual base classes. If you
1957 SET_BINFO_MARKED for each binfo you process, you are further
1958 guaranteed that you will walk into each virtual base class exactly
1959 once. */
1961 tree
1962 dfs_unmarked_real_bases_queue_p (binfo, data)
1963 tree binfo;
1964 void *data;
1966 if (TREE_VIA_VIRTUAL (binfo))
1968 tree type = (tree) data;
1970 if (TREE_CODE (type) == TREE_LIST)
1971 type = TREE_PURPOSE (type);
1972 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
1974 return unmarkedp (binfo, NULL);
1977 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
1978 that are marked, rather than unmarked. */
1980 tree
1981 dfs_marked_real_bases_queue_p (binfo, data)
1982 tree binfo;
1983 void *data;
1985 if (TREE_VIA_VIRTUAL (binfo))
1987 tree type = (tree) data;
1989 if (TREE_CODE (type) == TREE_LIST)
1990 type = TREE_PURPOSE (type);
1991 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
1993 return markedp (binfo, NULL);
1996 /* A queue function that skips all virtual bases (and their
1997 bases). */
1999 tree
2000 dfs_skip_vbases (binfo, data)
2001 tree binfo;
2002 void *data ATTRIBUTE_UNUSED;
2004 if (TREE_VIA_VIRTUAL (binfo))
2005 return NULL_TREE;
2007 return binfo;
2010 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2012 static tree
2013 dfs_get_pure_virtuals (binfo, data)
2014 tree binfo;
2015 void *data;
2017 tree type = (tree) data;
2019 /* We're not interested in primary base classes; the derived class
2020 of which they are a primary base will contain the information we
2021 need. */
2022 if (!BINFO_PRIMARY_P (binfo))
2024 tree virtuals;
2026 for (virtuals = BINFO_VIRTUALS (binfo);
2027 virtuals;
2028 virtuals = TREE_CHAIN (virtuals))
2029 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2030 CLASSTYPE_PURE_VIRTUALS (type)
2031 = tree_cons (NULL_TREE, BV_FN (virtuals),
2032 CLASSTYPE_PURE_VIRTUALS (type));
2035 SET_BINFO_MARKED (binfo);
2037 return NULL_TREE;
2040 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2042 void
2043 get_pure_virtuals (type)
2044 tree type;
2046 tree vbases;
2048 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2049 is going to be overridden. */
2050 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
2051 /* Now, run through all the bases which are not primary bases, and
2052 collect the pure virtual functions. We look at the vtable in
2053 each class to determine what pure virtual functions are present.
2054 (A primary base is not interesting because the derived class of
2055 which it is a primary base will contain vtable entries for the
2056 pure virtuals in the base class. */
2057 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2058 dfs_unmarked_real_bases_queue_p, type);
2059 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2060 dfs_marked_real_bases_queue_p, type);
2062 /* Put the pure virtuals in dfs order. */
2063 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2065 for (vbases = CLASSTYPE_VBASECLASSES (type);
2066 vbases;
2067 vbases = TREE_CHAIN (vbases))
2069 tree virtuals;
2071 for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
2072 virtuals;
2073 virtuals = TREE_CHAIN (virtuals))
2075 tree base_fndecl = BV_FN (virtuals);
2076 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2077 error ("`%#D' needs a final overrider", base_fndecl);
2082 /* DEPTH-FIRST SEARCH ROUTINES. */
2084 tree
2085 markedp (binfo, data)
2086 tree binfo;
2087 void *data ATTRIBUTE_UNUSED;
2089 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2092 tree
2093 unmarkedp (binfo, data)
2094 tree binfo;
2095 void *data ATTRIBUTE_UNUSED;
2097 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2100 tree
2101 marked_vtable_pathp (binfo, data)
2102 tree binfo;
2103 void *data ATTRIBUTE_UNUSED;
2105 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2108 tree
2109 unmarked_vtable_pathp (binfo, data)
2110 tree binfo;
2111 void *data ATTRIBUTE_UNUSED;
2113 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2116 static tree
2117 marked_pushdecls_p (binfo, data)
2118 tree binfo;
2119 void *data ATTRIBUTE_UNUSED;
2121 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2122 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2125 static tree
2126 unmarked_pushdecls_p (binfo, data)
2127 tree binfo;
2128 void *data ATTRIBUTE_UNUSED;
2130 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2131 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2134 /* The worker functions for `dfs_walk'. These do not need to
2135 test anything (vis a vis marking) if they are paired with
2136 a predicate function (above). */
2138 tree
2139 dfs_unmark (binfo, data)
2140 tree binfo;
2141 void *data ATTRIBUTE_UNUSED;
2143 CLEAR_BINFO_MARKED (binfo);
2144 return NULL_TREE;
2147 /* get virtual base class types.
2148 This adds type to the vbase_types list in reverse dfs order.
2149 Ordering is very important, so don't change it. */
2151 static tree
2152 dfs_get_vbase_types (binfo, data)
2153 tree binfo;
2154 void *data;
2156 tree type = (tree) data;
2158 if (TREE_VIA_VIRTUAL (binfo))
2159 CLASSTYPE_VBASECLASSES (type)
2160 = tree_cons (BINFO_TYPE (binfo),
2161 binfo,
2162 CLASSTYPE_VBASECLASSES (type));
2163 SET_BINFO_MARKED (binfo);
2164 return NULL_TREE;
2167 /* Called via dfs_walk from mark_primary_bases. Builds the
2168 inheritance graph order list of BINFOs. */
2170 static tree
2171 dfs_build_inheritance_graph_order (binfo, data)
2172 tree binfo;
2173 void *data;
2175 tree *last_binfo = (tree *) data;
2177 if (*last_binfo)
2178 TREE_CHAIN (*last_binfo) = binfo;
2179 *last_binfo = binfo;
2180 SET_BINFO_MARKED (binfo);
2181 return NULL_TREE;
2184 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
2186 void
2187 get_vbase_types (type)
2188 tree type;
2190 tree last_binfo;
2192 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
2193 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
2194 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2195 reverse it so that we get normal dfs ordering. */
2196 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
2197 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2198 /* Thread the BINFOs in inheritance-graph order. */
2199 last_binfo = NULL;
2200 dfs_walk_real (TYPE_BINFO (type),
2201 dfs_build_inheritance_graph_order,
2202 NULL,
2203 unmarkedp,
2204 &last_binfo);
2205 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
2208 /* Called from find_vbase_instance via dfs_walk. */
2210 static tree
2211 dfs_find_vbase_instance (binfo, data)
2212 tree binfo;
2213 void *data;
2215 tree base = TREE_VALUE ((tree) data);
2217 if (BINFO_PRIMARY_P (binfo)
2218 && same_type_p (BINFO_TYPE (binfo), base))
2219 return binfo;
2221 return NULL_TREE;
2224 /* Find the real occurrence of the virtual BASE (a class type) in the
2225 hierarchy dominated by TYPE. */
2227 tree
2228 find_vbase_instance (base, type)
2229 tree base;
2230 tree type;
2232 tree instance;
2234 instance = binfo_for_vbase (base, type);
2235 if (!BINFO_PRIMARY_P (instance))
2236 return instance;
2238 return dfs_walk (TYPE_BINFO (type),
2239 dfs_find_vbase_instance,
2240 NULL,
2241 build_tree_list (type, base));
2245 /* Debug info for C++ classes can get very large; try to avoid
2246 emitting it everywhere.
2248 Note that this optimization wins even when the target supports
2249 BINCL (if only slightly), and reduces the amount of work for the
2250 linker. */
2252 void
2253 maybe_suppress_debug_info (t)
2254 tree t;
2256 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2257 does not support name references between translation units. It supports
2258 symbolic references between translation units, but only within a single
2259 executable or shared library.
2261 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2262 that the type was never defined, so we only get the members we
2263 actually define. */
2264 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
2265 return;
2267 /* We might have set this earlier in cp_finish_decl. */
2268 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2270 /* If we already know how we're handling this class, handle debug info
2271 the same way. */
2272 if (CLASSTYPE_INTERFACE_KNOWN (t))
2274 if (CLASSTYPE_INTERFACE_ONLY (t))
2275 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2276 /* else don't set it. */
2278 /* If the class has a vtable, write out the debug info along with
2279 the vtable. */
2280 else if (TYPE_CONTAINS_VPTR_P (t))
2281 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2283 /* Otherwise, just emit the debug info normally. */
2286 /* Note that we want debugging information for a base class of a class
2287 whose vtable is being emitted. Normally, this would happen because
2288 calling the constructor for a derived class implies calling the
2289 constructors for all bases, which involve initializing the
2290 appropriate vptr with the vtable for the base class; but in the
2291 presence of optimization, this initialization may be optimized
2292 away, so we tell finish_vtable_vardecl that we want the debugging
2293 information anyway. */
2295 static tree
2296 dfs_debug_mark (binfo, data)
2297 tree binfo;
2298 void *data ATTRIBUTE_UNUSED;
2300 tree t = BINFO_TYPE (binfo);
2302 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2304 return NULL_TREE;
2307 /* Returns BINFO if we haven't already noted that we want debugging
2308 info for this base class. */
2310 static tree
2311 dfs_debug_unmarkedp (binfo, data)
2312 tree binfo;
2313 void *data ATTRIBUTE_UNUSED;
2315 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2316 ? binfo : NULL_TREE);
2319 /* Write out the debugging information for TYPE, whose vtable is being
2320 emitted. Also walk through our bases and note that we want to
2321 write out information for them. This avoids the problem of not
2322 writing any debug info for intermediate basetypes whose
2323 constructors, and thus the references to their vtables, and thus
2324 the vtables themselves, were optimized away. */
2326 void
2327 note_debug_info_needed (type)
2328 tree type;
2330 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2332 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2333 rest_of_type_compilation (type, toplevel_bindings_p ());
2336 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
2339 /* Subroutines of push_class_decls (). */
2341 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2342 because it (or one of the intermediate bases) depends on template parms. */
2344 static int
2345 dependent_base_p (binfo)
2346 tree binfo;
2348 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2350 if (currently_open_class (TREE_TYPE (binfo)))
2351 break;
2352 if (uses_template_parms (TREE_TYPE (binfo)))
2353 return 1;
2355 return 0;
2358 static void
2359 setup_class_bindings (name, type_binding_p)
2360 tree name;
2361 int type_binding_p;
2363 tree type_binding = NULL_TREE;
2364 tree value_binding;
2366 /* If we've already done the lookup for this declaration, we're
2367 done. */
2368 if (IDENTIFIER_CLASS_VALUE (name))
2369 return;
2371 /* First, deal with the type binding. */
2372 if (type_binding_p)
2374 type_binding = lookup_member (current_class_type, name,
2375 /*protect=*/2,
2376 /*want_type=*/1);
2377 if (TREE_CODE (type_binding) == TREE_LIST
2378 && TREE_TYPE (type_binding) == error_mark_node)
2379 /* NAME is ambiguous. */
2380 push_class_level_binding (name, type_binding);
2381 else
2382 pushdecl_class_level (type_binding);
2385 /* Now, do the value binding. */
2386 value_binding = lookup_member (current_class_type, name,
2387 /*protect=*/2,
2388 /*want_type=*/0);
2390 if (type_binding_p
2391 && (TREE_CODE (value_binding) == TYPE_DECL
2392 || DECL_CLASS_TEMPLATE_P (value_binding)
2393 || (TREE_CODE (value_binding) == TREE_LIST
2394 && TREE_TYPE (value_binding) == error_mark_node
2395 && (TREE_CODE (TREE_VALUE (value_binding))
2396 == TYPE_DECL))))
2397 /* We found a type-binding, even when looking for a non-type
2398 binding. This means that we already processed this binding
2399 above. */;
2400 else if (value_binding)
2402 if (TREE_CODE (value_binding) == TREE_LIST
2403 && TREE_TYPE (value_binding) == error_mark_node)
2404 /* NAME is ambiguous. */
2405 push_class_level_binding (name, value_binding);
2406 else
2408 if (BASELINK_P (value_binding))
2409 /* NAME is some overloaded functions. */
2410 value_binding = TREE_VALUE (value_binding);
2411 pushdecl_class_level (value_binding);
2416 /* Push class-level declarations for any names appearing in BINFO that
2417 are TYPE_DECLS. */
2419 static tree
2420 dfs_push_type_decls (binfo, data)
2421 tree binfo;
2422 void *data ATTRIBUTE_UNUSED;
2424 tree type;
2425 tree fields;
2427 type = BINFO_TYPE (binfo);
2428 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2429 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
2430 && !(!same_type_p (type, current_class_type)
2431 && template_self_reference_p (type, fields)))
2432 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
2434 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2435 DERIVED_FROM_P, which calls get_base_distance. */
2436 SET_BINFO_PUSHDECLS_MARKED (binfo);
2438 return NULL_TREE;
2441 /* Push class-level declarations for any names appearing in BINFO that
2442 are not TYPE_DECLS. */
2444 static tree
2445 dfs_push_decls (binfo, data)
2446 tree binfo;
2447 void *data;
2449 tree type;
2450 tree method_vec;
2451 int dep_base_p;
2453 type = BINFO_TYPE (binfo);
2454 dep_base_p = (processing_template_decl && type != current_class_type
2455 && dependent_base_p (binfo));
2456 if (!dep_base_p)
2458 tree fields;
2459 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2460 if (DECL_NAME (fields)
2461 && TREE_CODE (fields) != TYPE_DECL
2462 && TREE_CODE (fields) != USING_DECL)
2463 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
2464 else if (TREE_CODE (fields) == FIELD_DECL
2465 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2466 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
2468 method_vec = (CLASS_TYPE_P (type)
2469 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
2470 if (method_vec)
2472 tree *methods;
2473 tree *end;
2475 /* Farm out constructors and destructors. */
2476 end = TREE_VEC_END (method_vec);
2478 for (methods = &TREE_VEC_ELT (method_vec, 2);
2479 *methods && methods != end;
2480 methods++)
2481 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
2482 /*type_binding_p=*/0);
2486 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
2488 return NULL_TREE;
2491 /* When entering the scope of a class, we cache all of the
2492 fields that that class provides within its inheritance
2493 lattice. Where ambiguities result, we mark them
2494 with `error_mark_node' so that if they are encountered
2495 without explicit qualification, we can emit an error
2496 message. */
2498 void
2499 push_class_decls (type)
2500 tree type;
2502 search_stack = push_search_level (search_stack, &search_obstack);
2504 /* Enter type declarations and mark. */
2505 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
2507 /* Enter non-type declarations and unmark. */
2508 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
2511 /* Here's a subroutine we need because C lacks lambdas. */
2513 static tree
2514 dfs_unuse_fields (binfo, data)
2515 tree binfo;
2516 void *data ATTRIBUTE_UNUSED;
2518 tree type = TREE_TYPE (binfo);
2519 tree fields;
2521 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2523 if (TREE_CODE (fields) != FIELD_DECL)
2524 continue;
2526 TREE_USED (fields) = 0;
2527 if (DECL_NAME (fields) == NULL_TREE
2528 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2529 unuse_fields (TREE_TYPE (fields));
2532 return NULL_TREE;
2535 void
2536 unuse_fields (type)
2537 tree type;
2539 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
2542 void
2543 pop_class_decls ()
2545 /* We haven't pushed a search level when dealing with cached classes,
2546 so we'd better not try to pop it. */
2547 if (search_stack)
2548 search_stack = pop_search_level (search_stack);
2551 void
2552 print_search_statistics ()
2554 #ifdef GATHER_STATISTICS
2555 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2556 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2557 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2558 n_outer_fields_searched, n_calls_lookup_fnfields);
2559 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2560 #else /* GATHER_STATISTICS */
2561 fprintf (stderr, "no search statistics\n");
2562 #endif /* GATHER_STATISTICS */
2565 void
2566 init_search_processing ()
2568 gcc_obstack_init (&search_obstack);
2571 void
2572 reinit_search_statistics ()
2574 #ifdef GATHER_STATISTICS
2575 n_fields_searched = 0;
2576 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2577 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2578 n_calls_get_base_type = 0;
2579 n_outer_fields_searched = 0;
2580 n_contexts_saved = 0;
2581 #endif /* GATHER_STATISTICS */
2584 static tree
2585 add_conversions (binfo, data)
2586 tree binfo;
2587 void *data;
2589 int i;
2590 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2591 tree *conversions = (tree *) data;
2593 /* Some builtin types have no method vector, not even an empty one. */
2594 if (!method_vec)
2595 return NULL_TREE;
2597 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
2599 tree tmp = TREE_VEC_ELT (method_vec, i);
2600 tree name;
2602 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
2603 break;
2605 name = DECL_NAME (OVL_CURRENT (tmp));
2607 /* Make sure we don't already have this conversion. */
2608 if (! IDENTIFIER_MARKED (name))
2610 *conversions = tree_cons (binfo, tmp, *conversions);
2611 IDENTIFIER_MARKED (name) = 1;
2614 return NULL_TREE;
2617 /* Return a TREE_LIST containing all the non-hidden user-defined
2618 conversion functions for TYPE (and its base-classes). The
2619 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2620 containing the conversion functions. The TREE_PURPOSE is the BINFO
2621 from which the conversion functions in this node were selected. */
2623 tree
2624 lookup_conversions (type)
2625 tree type;
2627 tree t;
2628 tree conversions = NULL_TREE;
2630 if (COMPLETE_TYPE_P (type))
2631 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
2633 for (t = conversions; t; t = TREE_CHAIN (t))
2634 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
2636 return conversions;
2639 struct overlap_info
2641 tree compare_type;
2642 int found_overlap;
2645 /* Check whether the empty class indicated by EMPTY_BINFO is also present
2646 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
2648 static tree
2649 dfs_check_overlap (empty_binfo, data)
2650 tree empty_binfo;
2651 void *data;
2653 struct overlap_info *oi = (struct overlap_info *) data;
2654 tree binfo;
2655 for (binfo = TYPE_BINFO (oi->compare_type);
2657 binfo = BINFO_BASETYPE (binfo, 0))
2659 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
2661 oi->found_overlap = 1;
2662 break;
2664 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
2665 break;
2668 return NULL_TREE;
2671 /* Trivial function to stop base traversal when we find something. */
2673 static tree
2674 dfs_no_overlap_yet (binfo, data)
2675 tree binfo;
2676 void *data;
2678 struct overlap_info *oi = (struct overlap_info *) data;
2679 return !oi->found_overlap ? binfo : NULL_TREE;
2682 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2683 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
2686 types_overlap_p (empty_type, next_type)
2687 tree empty_type, next_type;
2689 struct overlap_info oi;
2691 if (! IS_AGGR_TYPE (next_type))
2692 return 0;
2693 oi.compare_type = next_type;
2694 oi.found_overlap = 0;
2695 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
2696 dfs_no_overlap_yet, &oi);
2697 return oi.found_overlap;
2700 /* Given a vtable VAR, determine which of the inherited classes the vtable
2701 inherits (in a loose sense) functions from.
2703 FIXME: This does not work with the new ABI. */
2705 tree
2706 binfo_for_vtable (var)
2707 tree var;
2709 tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
2710 tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
2711 int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
2712 int i;
2714 for (i = 0; i < n_baseclasses; i++)
2716 tree base_binfo = TREE_VEC_ELT (binfos, i);
2717 if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
2718 return base_binfo;
2721 /* If no secondary base classes matched, return the primary base, if
2722 there is one. */
2723 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
2724 return get_primary_binfo (main_binfo);
2726 return main_binfo;
2729 /* Returns the binfo of the first direct or indirect virtual base derived
2730 from BINFO, or NULL if binfo is not via virtual. */
2732 tree
2733 binfo_from_vbase (binfo)
2734 tree binfo;
2736 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2738 if (TREE_VIA_VIRTUAL (binfo))
2739 return binfo;
2741 return NULL_TREE;
2744 /* Returns the binfo of the first direct or indirect virtual base derived
2745 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2746 via virtual. */
2748 tree
2749 binfo_via_virtual (binfo, limit)
2750 tree binfo;
2751 tree limit;
2753 for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
2754 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2756 if (TREE_VIA_VIRTUAL (binfo))
2757 return binfo;
2759 return NULL_TREE;
2762 /* Returns the BINFO (if any) for the virtual baseclass T of the class
2763 C from the CLASSTYPE_VBASECLASSES list. */
2765 tree
2766 binfo_for_vbase (basetype, classtype)
2767 tree basetype;
2768 tree classtype;
2770 tree binfo;
2772 binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
2773 return binfo ? TREE_VALUE (binfo) : NULL_TREE;