OSDN Git Service

* cp/search.c (grow_bfs_bases): New subroutine of bfs_walk.
[pf3gnuchains/gcc-fork.git] / gcc / cp / search.c
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, 2003 Free Software Foundation, Inc.
5    Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC 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.
13
14 GCC 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.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 /* High-level class interface.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "obstack.h"
33 #include "flags.h"
34 #include "rtl.h"
35 #include "output.h"
36 #include "toplev.h"
37 #include "stack.h"
38
39 /* Obstack used for remembering decision points of breadth-first.  */
40
41 static struct obstack search_obstack;
42
43 /* Methods for pushing and popping objects to and from obstacks.  */
44
45 struct stack_level *
46 push_stack_level (obstack, tp, size)
47      struct obstack *obstack;
48      char *tp;  /* Sony NewsOS 5.0 compiler doesn't like void * here.  */
49      int size;
50 {
51   struct stack_level *stack;
52   obstack_grow (obstack, tp, size);
53   stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
54   obstack_finish (obstack);
55   stack->obstack = obstack;
56   stack->first = (tree *) obstack_base (obstack);
57   stack->limit = obstack_room (obstack) / sizeof (tree *);
58   return stack;
59 }
60
61 struct stack_level *
62 pop_stack_level (stack)
63      struct stack_level *stack;
64 {
65   struct stack_level *tem = stack;
66   struct obstack *obstack = tem->obstack;
67   stack = tem->prev;
68   obstack_free (obstack, tem);
69   return stack;
70 }
71
72 #define search_level stack_level
73 static struct search_level *search_stack;
74
75 struct vbase_info 
76 {
77   /* The class dominating the hierarchy.  */
78   tree type;
79   /* A pointer to a complete object of the indicated TYPE.  */
80   tree decl_ptr;
81   tree inits;
82 };
83
84 static tree lookup_field_1 (tree, tree);
85 static int is_subobject_of_p (tree, tree, tree);
86 static int is_subobject_of_p_1 (tree, tree, tree);
87 static tree dfs_check_overlap (tree, void *);
88 static tree dfs_no_overlap_yet (tree, void *);
89 static base_kind lookup_base_r (tree, tree, base_access,
90                                 bool, bool, bool, tree *);
91 static int dynamic_cast_base_recurse (tree, tree, bool, tree *);
92 static tree marked_pushdecls_p (tree, void *);
93 static tree unmarked_pushdecls_p (tree, void *);
94 static tree dfs_debug_unmarkedp (tree, void *);
95 static tree dfs_debug_mark (tree, void *);
96 static tree dfs_get_vbase_types (tree, void *);
97 static tree dfs_push_type_decls (tree, void *);
98 static tree dfs_push_decls (tree, void *);
99 static tree dfs_unuse_fields (tree, void *);
100 static tree add_conversions (tree, void *);
101 static int look_for_overrides_r (tree, tree);
102 static struct search_level *push_search_level (struct stack_level *,
103                                                struct obstack *);
104 static struct search_level *pop_search_level (struct stack_level *);
105 static void grow_bfs_bases (tree **, size_t *, size_t *);
106 static tree bfs_walk (tree, tree (*) (tree, void *),
107                       tree (*) (tree, void *), void *);
108 static tree lookup_field_queue_p (tree, void *);
109 static int shared_member_p (tree);
110 static tree lookup_field_r (tree, void *);
111 static tree canonical_binfo (tree);
112 static tree shared_marked_p (tree, void *);
113 static tree shared_unmarked_p (tree, void *);
114 static int  dependent_base_p (tree);
115 static tree dfs_accessible_queue_p (tree, void *);
116 static tree dfs_accessible_p (tree, void *);
117 static tree dfs_access_in_type (tree, void *);
118 static access_kind access_in_type (tree, tree);
119 static tree dfs_canonical_queue (tree, void *);
120 static tree dfs_assert_unmarked_p (tree, void *);
121 static void assert_canonical_unmarked (tree);
122 static int protected_accessible_p (tree, tree, tree);
123 static int friend_accessible_p (tree, tree, tree);
124 static void setup_class_bindings (tree, int);
125 static int template_self_reference_p (tree, tree);
126 static tree dfs_find_vbase_instance (tree, void *);
127 static tree dfs_get_pure_virtuals (tree, void *);
128 static tree dfs_build_inheritance_graph_order (tree, void *);
129
130 /* Allocate a level of searching.  */
131
132 static struct search_level *
133 push_search_level (struct stack_level *stack, struct obstack *obstack)
134 {
135   struct search_level tem;
136
137   tem.prev = stack;
138   return push_stack_level (obstack, (char *)&tem, sizeof (tem));
139 }
140
141 /* Discard a level of search allocation.  */
142
143 static struct search_level *
144 pop_search_level (struct stack_level *obstack)
145 {
146   register struct search_level *stack = pop_stack_level (obstack);
147
148   return stack;
149 }
150 \f
151 /* Variables for gathering statistics.  */
152 #ifdef GATHER_STATISTICS
153 static int n_fields_searched;
154 static int n_calls_lookup_field, n_calls_lookup_field_1;
155 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
156 static int n_calls_get_base_type;
157 static int n_outer_fields_searched;
158 static int n_contexts_saved;
159 #endif /* GATHER_STATISTICS */
160
161 \f
162 /* Worker for lookup_base.  BINFO is the binfo we are searching at,
163    BASE is the RECORD_TYPE we are searching for.  ACCESS is the
164    required access checks.  WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
165    IS_VIRTUAL indicate how BINFO was reached from the start of the
166    search.  WITHIN_CURRENT_SCOPE is true if we met the current scope,
167    or friend thereof (this allows us to determine whether a protected
168    base is accessible or not).  IS_NON_PUBLIC indicates whether BINFO
169    is accessible and IS_VIRTUAL indicates if it is morally virtual.
170
171    If BINFO is of the required type, then *BINFO_PTR is examined to
172    compare with any other instance of BASE we might have already
173    discovered. *BINFO_PTR is initialized and a base_kind return value
174    indicates what kind of base was located.
175
176    Otherwise BINFO's bases are searched.  */
177
178 static base_kind
179 lookup_base_r (tree binfo, tree base, base_access access,
180                bool within_current_scope,
181                bool is_non_public,              /* inside a non-public part */
182                bool is_virtual,                 /* inside a virtual part */
183                tree *binfo_ptr)
184 {
185   int i;
186   tree bases;
187   base_kind found = bk_not_base;
188   
189   if (access == ba_check
190       && !within_current_scope
191       && is_friend (BINFO_TYPE (binfo), current_scope ()))
192     {
193       /* Do not clear is_non_public here.  If A is a private base of B, A
194          is not allowed to convert a B* to an A*.  */
195       within_current_scope = 1;
196     }
197   
198   if (same_type_p (BINFO_TYPE (binfo), base))
199     {
200       /* We have found a base. Check against what we have found
201          already.  */
202       found = bk_same_type;
203       if (is_virtual)
204         found = bk_via_virtual;
205       if (is_non_public)
206         found = bk_inaccessible;
207       
208       if (!*binfo_ptr)
209         *binfo_ptr = binfo;
210       else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
211                                                    BINFO_OFFSET (*binfo_ptr)))
212         {
213           if (access != ba_any)
214             *binfo_ptr = NULL;
215           else if (!is_virtual)
216             /* Prefer a non-virtual base.  */
217             *binfo_ptr = binfo;
218           found = bk_ambig;
219         }
220       
221       return found;
222     }
223   
224   bases = BINFO_BASETYPES (binfo);
225   if (!bases)
226     return bk_not_base;
227   
228   for (i = TREE_VEC_LENGTH (bases); i--;)
229     {
230       tree base_binfo = TREE_VEC_ELT (bases, i);
231       int this_non_public = is_non_public;
232       int this_virtual = is_virtual;
233       base_kind bk;
234
235       if (access <= ba_ignore)
236         ; /* no change */
237       else if (TREE_VIA_PUBLIC (base_binfo))
238         ; /* no change */
239       else if (access == ba_not_special)
240         this_non_public = 1;
241       else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
242         ; /* no change */
243       else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
244         ; /* no change */
245       else
246         this_non_public = 1;
247       
248       if (TREE_VIA_VIRTUAL (base_binfo))
249         this_virtual = 1;
250       
251       bk = lookup_base_r (base_binfo, base,
252                           access, within_current_scope,
253                           this_non_public, this_virtual,
254                           binfo_ptr);
255
256       switch (bk)
257         {
258         case bk_ambig:
259           if (access != ba_any)
260             return bk;
261           found = bk;
262           break;
263           
264         case bk_inaccessible:
265           if (found == bk_not_base)
266             found = bk;
267           my_friendly_assert (found == bk_via_virtual
268                               || found == bk_inaccessible, 20010723);
269           
270           break;
271           
272         case bk_same_type:
273           bk = bk_proper_base;
274           /* FALLTHROUGH */
275         case bk_proper_base:
276           my_friendly_assert (found == bk_not_base, 20010723);
277           found = bk;
278           break;
279           
280         case bk_via_virtual:
281           if (found != bk_ambig)
282             found = bk;
283           break;
284           
285         case bk_not_base:
286           break;
287         }
288     }
289   return found;
290 }
291
292 /* Lookup BASE in the hierarchy dominated by T.  Do access checking as
293    ACCESS specifies.  Return the binfo we discover (which might not be
294    canonical).  If KIND_PTR is non-NULL, fill with information about
295    what kind of base we discovered.
296
297    If the base is inaccessible, or ambiguous, and the ba_quiet bit is
298    not set in ACCESS, then an error is issued and error_mark_node is
299    returned.  If the ba_quiet bit is set, then no error is issued and
300    NULL_TREE is returned.  */
301
302 tree
303 lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
304 {
305   tree binfo = NULL;            /* The binfo we've found so far.  */
306   tree t_binfo = NULL;
307   base_kind bk;
308   
309   if (t == error_mark_node || base == error_mark_node)
310     {
311       if (kind_ptr)
312         *kind_ptr = bk_not_base;
313       return error_mark_node;
314     }
315   my_friendly_assert (TYPE_P (base), 20011127);
316   
317   if (!TYPE_P (t))
318     {
319       t_binfo = t;
320       t = BINFO_TYPE (t);
321     }
322   else 
323     t_binfo = TYPE_BINFO (t);
324
325   /* Ensure that the types are instantiated.  */
326   t = complete_type (TYPE_MAIN_VARIANT (t));
327   base = complete_type (TYPE_MAIN_VARIANT (base));
328   
329   bk = lookup_base_r (t_binfo, base, access & ~ba_quiet,
330                       0, 0, 0, &binfo);
331
332   switch (bk)
333     {
334     case bk_inaccessible:
335       binfo = NULL_TREE;
336       if (!(access & ba_quiet))
337         {
338           error ("`%T' is an inaccessible base of `%T'", base, t);
339           binfo = error_mark_node;
340         }
341       break;
342     case bk_ambig:
343       if (access != ba_any)
344         {
345           binfo = NULL_TREE;
346           if (!(access & ba_quiet))
347             {
348               error ("`%T' is an ambiguous base of `%T'", base, t);
349               binfo = error_mark_node;
350             }
351         }
352       break;
353     default:;
354     }
355   
356   if (kind_ptr)
357     *kind_ptr = bk;
358   
359   return binfo;
360 }
361
362 /* Worker function for get_dynamic_cast_base_type.  */
363
364 static int
365 dynamic_cast_base_recurse (tree subtype, tree binfo, bool is_via_virtual,
366                            tree *offset_ptr)
367 {
368   tree binfos;
369   int i, n_baselinks;
370   int worst = -2;
371   
372   if (BINFO_TYPE (binfo) == subtype)
373     {
374       if (is_via_virtual)
375         return -1;
376       else
377         {
378           *offset_ptr = BINFO_OFFSET (binfo);
379           return 0;
380         }
381     }
382   
383   binfos = BINFO_BASETYPES (binfo);
384   n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
385   for (i = 0; i < n_baselinks; i++)
386     {
387       tree base_binfo = TREE_VEC_ELT (binfos, i);
388       int rval;
389       
390       if (!TREE_VIA_PUBLIC (base_binfo))
391         continue;
392       rval = dynamic_cast_base_recurse
393              (subtype, base_binfo,
394               is_via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
395       if (worst == -2)
396         worst = rval;
397       else if (rval >= 0)
398         worst = worst >= 0 ? -3 : worst;
399       else if (rval == -1)
400         worst = -1;
401       else if (rval == -3 && worst != -1)
402         worst = -3;
403     }
404   return worst;
405 }
406
407 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
408    started from is related to the required TARGET type, in order to optimize
409    the inheritance graph search. This information is independent of the
410    current context, and ignores private paths, hence get_base_distance is
411    inappropriate. Return a TREE specifying the base offset, BOFF.
412    BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
413       and there are no public virtual SUBTYPE bases.
414    BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
415    BOFF == -2, SUBTYPE is not a public base.
416    BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases.  */
417
418 tree
419 get_dynamic_cast_base_type (tree subtype, tree target)
420 {
421   tree offset = NULL_TREE;
422   int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
423                                         false, &offset);
424   
425   if (!boff)
426     return offset;
427   offset = build_int_2 (boff, -1);
428   TREE_TYPE (offset) = ssizetype;
429   return offset;
430 }
431
432 /* Search for a member with name NAME in a multiple inheritance lattice
433    specified by TYPE.  If it does not exist, return NULL_TREE.
434    If the member is ambiguously referenced, return `error_mark_node'.
435    Otherwise, return the FIELD_DECL.  */
436
437 /* Do a 1-level search for NAME as a member of TYPE.  The caller must
438    figure out whether it can access this field.  (Since it is only one
439    level, this is reasonable.)  */
440
441 static tree
442 lookup_field_1 (tree type, tree name)
443 {
444   register tree field;
445
446   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
447       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
448       || TREE_CODE (type) == TYPENAME_TYPE)
449     /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and 
450        BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
451        instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX.  (Miraculously,
452        the code often worked even when we treated the index as a list
453        of fields!)
454        The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME.  */
455     return NULL_TREE;
456
457   if (TYPE_NAME (type)
458       && DECL_LANG_SPECIFIC (TYPE_NAME (type))
459       && DECL_SORTED_FIELDS (TYPE_NAME (type)))
460     {
461       tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
462       int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
463       int i;
464
465       while (lo < hi)
466         {
467           i = (lo + hi) / 2;
468
469 #ifdef GATHER_STATISTICS
470           n_fields_searched++;
471 #endif /* GATHER_STATISTICS */
472
473           if (DECL_NAME (fields[i]) > name)
474             hi = i;
475           else if (DECL_NAME (fields[i]) < name)
476             lo = i + 1;
477           else
478             {
479               /* We might have a nested class and a field with the
480                  same name; we sorted them appropriately via
481                  field_decl_cmp, so just look for the last field with
482                  this name.  */
483               while (i + 1 < hi
484                      && DECL_NAME (fields[i+1]) == name)
485                 ++i;
486               return fields[i];
487             }
488         }
489       return NULL_TREE;
490     }
491
492   field = TYPE_FIELDS (type);
493
494 #ifdef GATHER_STATISTICS
495   n_calls_lookup_field_1++;
496 #endif /* GATHER_STATISTICS */
497   while (field)
498     {
499 #ifdef GATHER_STATISTICS
500       n_fields_searched++;
501 #endif /* GATHER_STATISTICS */
502       my_friendly_assert (DECL_P (field), 0);
503       if (DECL_NAME (field) == NULL_TREE
504           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
505         {
506           tree temp = lookup_field_1 (TREE_TYPE (field), name);
507           if (temp)
508             return temp;
509         }
510       if (TREE_CODE (field) == USING_DECL)
511         /* For now, we're just treating member using declarations as
512            old ARM-style access declarations.  Thus, there's no reason
513            to return a USING_DECL, and the rest of the compiler can't
514            handle it.  Once the class is defined, these are purged
515            from TYPE_FIELDS anyhow; see handle_using_decl.  */
516         ;
517       else if (DECL_NAME (field) == name)
518         return field;
519       field = TREE_CHAIN (field);
520     }
521   /* Not found.  */
522   if (name == vptr_identifier)
523     {
524       /* Give the user what s/he thinks s/he wants.  */
525       if (TYPE_POLYMORPHIC_P (type))
526         return TYPE_VFIELD (type);
527     }
528   return NULL_TREE;
529 }
530
531 /* There are a number of cases we need to be aware of here:
532                          current_class_type     current_function_decl
533      global                     NULL                    NULL
534      fn-local                   NULL                    SET
535      class-local                SET                     NULL
536      class->fn                  SET                     SET
537      fn->class                  SET                     SET
538
539    Those last two make life interesting.  If we're in a function which is
540    itself inside a class, we need decls to go into the fn's decls (our
541    second case below).  But if we're in a class and the class itself is
542    inside a function, we need decls to go into the decls for the class.  To
543    achieve this last goal, we must see if, when both current_class_ptr and
544    current_function_decl are set, the class was declared inside that
545    function.  If so, we know to put the decls into the class's scope.  */
546
547 tree
548 current_scope ()
549 {
550   if (current_function_decl == NULL_TREE)
551     return current_class_type;
552   if (current_class_type == NULL_TREE)
553     return current_function_decl;
554   if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
555        && same_type_p (DECL_CONTEXT (current_function_decl),
556                        current_class_type))
557       || (DECL_FRIEND_CONTEXT (current_function_decl)
558           && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
559                           current_class_type)))
560     return current_function_decl;
561
562   return current_class_type;
563 }
564
565 /* Returns nonzero if we are currently in a function scope.  Note
566    that this function returns zero if we are within a local class, but
567    not within a member function body of the local class.  */
568
569 int
570 at_function_scope_p ()
571 {
572   tree cs = current_scope ();
573   return cs && TREE_CODE (cs) == FUNCTION_DECL;
574 }
575
576 /* Returns true if the innermost active scope is a class scope.  */
577
578 bool
579 at_class_scope_p ()
580 {
581   tree cs = current_scope ();
582   return cs && TYPE_P (cs);
583 }
584
585 /* Return the scope of DECL, as appropriate when doing name-lookup.  */
586
587 tree
588 context_for_name_lookup (tree decl)
589 {
590   /* [class.union]
591      
592      For the purposes of name lookup, after the anonymous union
593      definition, the members of the anonymous union are considered to
594      have been defined in the scope in which the anonymous union is
595      declared.  */ 
596   tree context = DECL_CONTEXT (decl);
597
598   while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
599     context = TYPE_CONTEXT (context);
600   if (!context)
601     context = global_namespace;
602
603   return context;
604 }
605
606 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
607    otherwise.  */
608
609 static tree
610 canonical_binfo (tree binfo)
611 {
612   return (TREE_VIA_VIRTUAL (binfo)
613           ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
614 }
615
616 /* A queue function that simply ensures that we walk into the
617    canonical versions of virtual bases.  */
618
619 static tree
620 dfs_canonical_queue (tree binfo, void *data ATTRIBUTE_UNUSED)
621 {
622   return canonical_binfo (binfo);
623 }
624
625 /* Called via dfs_walk from assert_canonical_unmarked.  */
626
627 static tree
628 dfs_assert_unmarked_p (tree binfo, void *data ATTRIBUTE_UNUSED)
629 {
630   my_friendly_assert (!BINFO_MARKED (binfo), 0);
631   return NULL_TREE;
632 }
633
634 /* Asserts that all the nodes below BINFO (using the canonical
635    versions of virtual bases) are unmarked.  */
636
637 static void
638 assert_canonical_unmarked (tree binfo)
639 {
640   dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
641 }
642
643 /* If BINFO is marked, return a canonical version of BINFO.
644    Otherwise, return NULL_TREE.  */
645
646 static tree
647 shared_marked_p (tree binfo, void *data)
648 {
649   binfo = canonical_binfo (binfo);
650   return markedp (binfo, data);
651 }
652
653 /* If BINFO is not marked, return a canonical version of BINFO.
654    Otherwise, return NULL_TREE.  */
655
656 static tree
657 shared_unmarked_p (tree binfo, void *data)
658 {
659   binfo = canonical_binfo (binfo);
660   return unmarkedp (binfo, data);
661 }
662
663 /* The accessibility routines use BINFO_ACCESS for scratch space
664    during the computation of the accssibility of some declaration.  */
665
666 #define BINFO_ACCESS(NODE) \
667   ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
668
669 /* Set the access associated with NODE to ACCESS.  */
670
671 #define SET_BINFO_ACCESS(NODE, ACCESS)                  \
672   ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0),     \
673    (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
674
675 /* Called from access_in_type via dfs_walk.  Calculate the access to
676    DATA (which is really a DECL) in BINFO.  */
677
678 static tree
679 dfs_access_in_type (tree binfo, void *data)
680 {
681   tree decl = (tree) data;
682   tree type = BINFO_TYPE (binfo);
683   access_kind access = ak_none;
684
685   if (context_for_name_lookup (decl) == type)
686     {
687       /* If we have desceneded to the scope of DECL, just note the
688          appropriate access.  */
689       if (TREE_PRIVATE (decl))
690         access = ak_private;
691       else if (TREE_PROTECTED (decl))
692         access = ak_protected;
693       else
694         access = ak_public;
695     }
696   else 
697     {
698       /* First, check for an access-declaration that gives us more
699          access to the DECL.  The CONST_DECL for an enumeration
700          constant will not have DECL_LANG_SPECIFIC, and thus no
701          DECL_ACCESS.  */
702       if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
703         {
704           tree decl_access = purpose_member (type, DECL_ACCESS (decl));
705           if (decl_access)
706             access = ((access_kind) 
707                       TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
708         }
709
710       if (!access)
711         {
712           int i;
713           int n_baselinks;
714           tree binfos;
715           
716           /* Otherwise, scan our baseclasses, and pick the most favorable
717              access.  */
718           binfos = BINFO_BASETYPES (binfo);
719           n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
720           for (i = 0; i < n_baselinks; ++i)
721             {
722               tree base_binfo = TREE_VEC_ELT (binfos, i);
723               access_kind base_access 
724                 = BINFO_ACCESS (canonical_binfo (base_binfo));
725
726               if (base_access == ak_none || base_access == ak_private)
727                 /* If it was not accessible in the base, or only
728                    accessible as a private member, we can't access it
729                    all.  */
730                 base_access = ak_none;
731               else if (TREE_VIA_PROTECTED (base_binfo))
732                 /* Public and protected members in the base are
733                    protected here.  */
734                 base_access = ak_protected;
735               else if (!TREE_VIA_PUBLIC (base_binfo))
736                 /* Public and protected members in the base are
737                    private here.  */
738                 base_access = ak_private;
739
740               /* See if the new access, via this base, gives more
741                  access than our previous best access.  */
742               if (base_access != ak_none
743                   && (base_access == ak_public
744                       || (base_access == ak_protected
745                           && access != ak_public)
746                       || (base_access == ak_private 
747                           && access == ak_none)))
748                 {
749                   access = base_access;
750
751                   /* If the new access is public, we can't do better.  */
752                   if (access == ak_public)
753                     break;
754                 }
755             }
756         }
757     }
758
759   /* Note the access to DECL in TYPE.  */
760   SET_BINFO_ACCESS (binfo, access);
761
762   /* Mark TYPE as visited so that if we reach it again we do not
763      duplicate our efforts here.  */
764   SET_BINFO_MARKED (binfo);
765
766   return NULL_TREE;
767 }
768
769 /* Return the access to DECL in TYPE.  */
770
771 static access_kind
772 access_in_type (tree type, tree decl)
773 {
774   tree binfo = TYPE_BINFO (type);
775
776   /* We must take into account
777
778        [class.paths]
779
780        If a name can be reached by several paths through a multiple
781        inheritance graph, the access is that of the path that gives
782        most access.  
783
784     The algorithm we use is to make a post-order depth-first traversal
785     of the base-class hierarchy.  As we come up the tree, we annotate
786     each node with the most lenient access.  */
787   dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
788   dfs_walk (binfo, dfs_unmark, shared_marked_p,  0);
789   assert_canonical_unmarked (binfo);
790
791   return BINFO_ACCESS (binfo);
792 }
793
794 /* Called from dfs_accessible_p via dfs_walk.  */
795
796 static tree
797 dfs_accessible_queue_p (tree binfo, void *data ATTRIBUTE_UNUSED)
798 {
799   if (BINFO_MARKED (binfo))
800     return NULL_TREE;
801
802   /* If this class is inherited via private or protected inheritance,
803      then we can't see it, unless we are a friend of the subclass.  */
804   if (!TREE_VIA_PUBLIC (binfo)
805       && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
806                      current_scope ()))
807     return NULL_TREE;
808
809   return canonical_binfo (binfo);
810 }
811
812 /* Called from dfs_accessible_p via dfs_walk.  */
813
814 static tree
815 dfs_accessible_p (tree binfo, void *data)
816 {
817   int protected_ok = data != 0;
818   access_kind access;
819
820   SET_BINFO_MARKED (binfo);
821   access = BINFO_ACCESS (binfo);
822   if (access == ak_public || (access == ak_protected && protected_ok))
823     return binfo;
824   else if (access != ak_none
825            && is_friend (BINFO_TYPE (binfo), current_scope ()))
826     return binfo;
827
828   return NULL_TREE;
829 }
830
831 /* Returns nonzero if it is OK to access DECL through an object
832    indiated by BINFO in the context of DERIVED.  */
833
834 static int
835 protected_accessible_p (tree decl, tree derived, tree binfo)
836 {
837   access_kind access;
838
839   /* We're checking this clause from [class.access.base]
840
841        m as a member of N is protected, and the reference occurs in a
842        member or friend of class N, or in a member or friend of a
843        class P derived from N, where m as a member of P is private or
844        protected.  
845
846     Here DERIVED is a possible P and DECL is m.  accessible_p will
847     iterate over various values of N, but the access to m in DERIVED
848     does not change.
849
850     Note that I believe that the passage above is wrong, and should read
851     "...is private or protected or public"; otherwise you get bizarre results
852     whereby a public using-decl can prevent you from accessing a protected
853     member of a base.  (jason 2000/02/28)  */
854
855   /* If DERIVED isn't derived from m's class, then it can't be a P.  */
856   if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
857     return 0;
858
859   access = access_in_type (derived, decl);
860
861   /* If m is inaccessible in DERIVED, then it's not a P.  */
862   if (access == ak_none)
863     return 0;
864   
865   /* [class.protected]
866
867      When a friend or a member function of a derived class references
868      a protected nonstatic member of a base class, an access check
869      applies in addition to those described earlier in clause
870      _class.access_) Except when forming a pointer to member
871      (_expr.unary.op_), the access must be through a pointer to,
872      reference to, or object of the derived class itself (or any class
873      derived from that class) (_expr.ref_).  If the access is to form
874      a pointer to member, the nested-name-specifier shall name the
875      derived class (or any class derived from that class).  */
876   if (DECL_NONSTATIC_MEMBER_P (decl))
877     {
878       /* We can tell through what the reference is occurring by
879          chasing BINFO up to the root.  */
880       tree t = binfo;
881       while (BINFO_INHERITANCE_CHAIN (t))
882         t = BINFO_INHERITANCE_CHAIN (t);
883       
884       if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
885         return 0;
886     }
887
888   return 1;
889 }
890
891 /* Returns nonzero if SCOPE is a friend of a type which would be able
892    to access DECL through the object indicated by BINFO.  */
893
894 static int
895 friend_accessible_p (tree scope, tree decl, tree binfo)
896 {
897   tree befriending_classes;
898   tree t;
899
900   if (!scope)
901     return 0;
902
903   if (TREE_CODE (scope) == FUNCTION_DECL
904       || DECL_FUNCTION_TEMPLATE_P (scope))
905     befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
906   else if (TYPE_P (scope))
907     befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
908   else
909     return 0;
910
911   for (t = befriending_classes; t; t = TREE_CHAIN (t))
912     if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
913       return 1;
914
915   /* Nested classes are implicitly friends of their enclosing types, as
916      per core issue 45 (this is a change from the standard).  */
917   if (TYPE_P (scope))
918     for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
919       if (protected_accessible_p (decl, t, binfo))
920         return 1;
921
922   if (TREE_CODE (scope) == FUNCTION_DECL
923       || DECL_FUNCTION_TEMPLATE_P (scope))
924     {
925       /* Perhaps this SCOPE is a member of a class which is a 
926          friend.  */ 
927       if (DECL_CLASS_SCOPE_P (decl)
928           && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
929         return 1;
930
931       /* Or an instantiation of something which is a friend.  */
932       if (DECL_TEMPLATE_INFO (scope))
933         return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
934     }
935   else if (CLASSTYPE_TEMPLATE_INFO (scope))
936     return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
937
938   return 0;
939 }
940
941 /* DECL is a declaration from a base class of TYPE, which was the
942    class used to name DECL.  Return nonzero if, in the current
943    context, DECL is accessible.  If TYPE is actually a BINFO node,
944    then we can tell in what context the access is occurring by looking
945    at the most derived class along the path indicated by BINFO.  */
946
947 int 
948 accessible_p (tree type, tree decl)
949 {
950   tree binfo;
951   tree t;
952
953   /* Nonzero if it's OK to access DECL if it has protected
954      accessibility in TYPE.  */
955   int protected_ok = 0;
956
957   /* If we're not checking access, everything is accessible.  */
958   if (!scope_chain->check_access)
959     return 1;
960
961   /* If this declaration is in a block or namespace scope, there's no
962      access control.  */
963   if (!TYPE_P (context_for_name_lookup (decl)))
964     return 1;
965
966   if (!TYPE_P (type))
967     {
968       binfo = type;
969       type = BINFO_TYPE (type);
970     }
971   else
972     binfo = TYPE_BINFO (type);
973
974   /* [class.access.base]
975
976      A member m is accessible when named in class N if
977
978      --m as a member of N is public, or
979
980      --m as a member of N is private, and the reference occurs in a
981        member or friend of class N, or
982
983      --m as a member of N is protected, and the reference occurs in a
984        member or friend of class N, or in a member or friend of a
985        class P derived from N, where m as a member of P is private or
986        protected, or
987
988      --there exists a base class B of N that is accessible at the point
989        of reference, and m is accessible when named in class B.  
990
991     We walk the base class hierarchy, checking these conditions.  */
992
993   /* Figure out where the reference is occurring.  Check to see if
994      DECL is private or protected in this scope, since that will
995      determine whether protected access is allowed.  */
996   if (current_class_type)
997     protected_ok = protected_accessible_p (decl, current_class_type, binfo);
998
999   /* Now, loop through the classes of which we are a friend.  */
1000   if (!protected_ok)
1001     protected_ok = friend_accessible_p (current_scope (), decl, binfo);
1002
1003   /* Standardize the binfo that access_in_type will use.  We don't
1004      need to know what path was chosen from this point onwards.  */
1005   binfo = TYPE_BINFO (type);
1006
1007   /* Compute the accessibility of DECL in the class hierarchy
1008      dominated by type.  */
1009   access_in_type (type, decl);
1010   /* Walk the hierarchy again, looking for a base class that allows
1011      access.  */
1012   t = dfs_walk (binfo, dfs_accessible_p, 
1013                 dfs_accessible_queue_p,
1014                 protected_ok ? &protected_ok : 0);
1015   /* Clear any mark bits.  Note that we have to walk the whole tree
1016      here, since we have aborted the previous walk from some point
1017      deep in the tree.  */
1018   dfs_walk (binfo, dfs_unmark, dfs_canonical_queue,  0);
1019   assert_canonical_unmarked (binfo);
1020
1021   return t != NULL_TREE;
1022 }
1023
1024 /* Recursive helper funciton for is_subobject_of_p; see that routine
1025    for documentation of the parameters.  */
1026
1027 static int
1028 is_subobject_of_p_1 (tree parent, tree binfo, tree most_derived)
1029 {
1030   tree binfos;
1031   int i, n_baselinks;
1032
1033   if (parent == binfo)
1034     return 1;
1035
1036   binfos = BINFO_BASETYPES (binfo);
1037   n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1038
1039   /* Iterate through the base types.  */
1040   for (i = 0; i < n_baselinks; i++)
1041     {
1042       tree base_binfo = TREE_VEC_ELT (binfos, i);
1043       tree base_type;
1044
1045       base_type = TREE_TYPE (base_binfo);
1046       if (!CLASS_TYPE_P (base_type))
1047         /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1048            class there's no way to descend into it.  */
1049         continue;
1050
1051       /* Avoid walking into the same virtual base more than once.  */
1052       if (TREE_VIA_VIRTUAL (base_binfo))
1053         {
1054           if (CLASSTYPE_MARKED4 (base_type))
1055             continue;
1056           SET_CLASSTYPE_MARKED4 (base_type);
1057           base_binfo = binfo_for_vbase (base_type, most_derived);
1058         }
1059
1060       if (is_subobject_of_p_1 (parent, base_binfo, most_derived))
1061         return 1;
1062     }
1063   return 0;
1064 }
1065
1066 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1067    found as a base class and sub-object of the object denoted by
1068    BINFO.  MOST_DERIVED is the most derived type of the hierarchy being
1069    searched.  */
1070
1071 static int
1072 is_subobject_of_p (tree parent, tree binfo, tree most_derived)
1073 {
1074   int result;
1075   tree vbase;
1076
1077   result = is_subobject_of_p_1 (parent, binfo, most_derived);
1078   /* Clear the mark bits on virtual bases.  */
1079   for (vbase = CLASSTYPE_VBASECLASSES (most_derived);
1080        vbase;
1081        vbase = TREE_CHAIN (vbase))
1082     CLEAR_CLASSTYPE_MARKED4 (TREE_TYPE (TREE_VALUE (vbase)));
1083
1084   return result;
1085 }
1086
1087 struct lookup_field_info {
1088   /* The type in which we're looking.  */
1089   tree type;
1090   /* The name of the field for which we're looking.  */
1091   tree name;
1092   /* If non-NULL, the current result of the lookup.  */
1093   tree rval;
1094   /* The path to RVAL.  */
1095   tree rval_binfo;
1096   /* If non-NULL, the lookup was ambiguous, and this is a list of the
1097      candidates.  */
1098   tree ambiguous;
1099   /* If nonzero, we are looking for types, not data members.  */
1100   int want_type;
1101   /* If nonzero, RVAL was found by looking through a dependent base.  */
1102   int from_dep_base_p;
1103   /* If something went wrong, a message indicating what.  */
1104   const char *errstr;
1105 };
1106
1107 /* Returns nonzero if BINFO is not hidden by the value found by the
1108    lookup so far.  If BINFO is hidden, then there's no need to look in
1109    it.  DATA is really a struct lookup_field_info.  Called from
1110    lookup_field via breadth_first_search.  */
1111
1112 static tree
1113 lookup_field_queue_p (tree binfo, void *data)
1114 {
1115   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1116
1117   /* Don't look for constructors or destructors in base classes.  */
1118   if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1119     return NULL_TREE;
1120
1121   /* If this base class is hidden by the best-known value so far, we
1122      don't need to look.  */
1123   binfo = CANONICAL_BINFO (binfo, lfi->type);
1124   if (!lfi->from_dep_base_p && lfi->rval_binfo
1125       && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1126     return NULL_TREE;
1127
1128   return binfo;
1129 }
1130
1131 /* Within the scope of a template class, you can refer to the to the
1132    current specialization with the name of the template itself.  For
1133    example:
1134    
1135      template <typename T> struct S { S* sp; }
1136
1137    Returns nonzero if DECL is such a declaration in a class TYPE.  */
1138
1139 static int
1140 template_self_reference_p (tree type, tree decl)
1141 {
1142   return  (CLASSTYPE_USE_TEMPLATE (type)
1143            && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
1144            && TREE_CODE (decl) == TYPE_DECL
1145            && DECL_ARTIFICIAL (decl)
1146            && DECL_NAME (decl) == constructor_name (type));
1147 }
1148
1149
1150 /* Nonzero for a class member means that it is shared between all objects
1151    of that class.
1152
1153    [class.member.lookup]:If the resulting set of declarations are not all
1154    from sub-objects of the same type, or the set has a  nonstatic  member
1155    and  includes members from distinct sub-objects, there is an ambiguity
1156    and the program is ill-formed.
1157
1158    This function checks that T contains no nonstatic members.  */
1159
1160 static int
1161 shared_member_p (tree t)
1162 {
1163   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
1164       || TREE_CODE (t) == CONST_DECL)
1165     return 1;
1166   if (is_overloaded_fn (t))
1167     {
1168       for (; t; t = OVL_NEXT (t))
1169         {
1170           tree fn = OVL_CURRENT (t);
1171           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1172             return 0;
1173         }
1174       return 1;
1175     }
1176   return 0;
1177 }
1178
1179 /* DATA is really a struct lookup_field_info.  Look for a field with
1180    the name indicated there in BINFO.  If this function returns a
1181    non-NULL value it is the result of the lookup.  Called from
1182    lookup_field via breadth_first_search.  */
1183
1184 static tree
1185 lookup_field_r (tree binfo, void *data)
1186 {
1187   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1188   tree type = BINFO_TYPE (binfo);
1189   tree nval = NULL_TREE;
1190   int from_dep_base_p;
1191
1192   /* First, look for a function.  There can't be a function and a data
1193      member with the same name, and if there's a function and a type
1194      with the same name, the type is hidden by the function.  */
1195   if (!lfi->want_type)
1196     {
1197       int idx = lookup_fnfields_1 (type, lfi->name);
1198       if (idx >= 0)
1199         nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1200     }
1201
1202   if (!nval)
1203     /* Look for a data member or type.  */
1204     nval = lookup_field_1 (type, lfi->name);
1205
1206   /* If there is no declaration with the indicated name in this type,
1207      then there's nothing to do.  */
1208   if (!nval)
1209     return NULL_TREE;
1210
1211   /* If we're looking up a type (as with an elaborated type specifier)
1212      we ignore all non-types we find.  */
1213   if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1214       && !DECL_CLASS_TEMPLATE_P (nval))
1215     {
1216       if (lfi->name == TYPE_IDENTIFIER (type))
1217         {
1218           /* If the aggregate has no user defined constructors, we allow
1219              it to have fields with the same name as the enclosing type.
1220              If we are looking for that name, find the corresponding
1221              TYPE_DECL.  */
1222           for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1223             if (DECL_NAME (nval) == lfi->name
1224                 && TREE_CODE (nval) == TYPE_DECL)
1225               break;
1226         }
1227       else
1228         nval = NULL_TREE;
1229       if (!nval)
1230         {
1231           nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1232           if (nval)
1233             nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1234           else 
1235             return NULL_TREE;
1236         }
1237     }
1238
1239   /* You must name a template base class with a template-id.  */
1240   if (!same_type_p (type, lfi->type) 
1241       && template_self_reference_p (type, nval))
1242     return NULL_TREE;
1243
1244   from_dep_base_p = dependent_base_p (binfo);
1245   if (lfi->from_dep_base_p && !from_dep_base_p)
1246     {
1247       /* If the new declaration is not found via a dependent base, and
1248          the old one was, then we must prefer the new one.  We weren't
1249          really supposed to be able to find the old one, so we don't
1250          want to be affected by a specialization.  Consider:
1251
1252            struct B { typedef int I; };
1253            template <typename T> struct D1 : virtual public B {}; 
1254            template <typename T> struct D :
1255            public D1, virtual pubic B { I i; };
1256
1257          The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1258          D1 is specialized.  */
1259       lfi->from_dep_base_p = 0;
1260       lfi->rval = NULL_TREE;
1261       lfi->rval_binfo = NULL_TREE;
1262       lfi->ambiguous = NULL_TREE;
1263       lfi->errstr = 0;
1264     }
1265   else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1266     /* Similarly, if the old declaration was not found via a dependent
1267        base, and the new one is, ignore the new one.  */
1268     return NULL_TREE;
1269
1270   /* If the lookup already found a match, and the new value doesn't
1271      hide the old one, we might have an ambiguity.  */
1272   if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
1273     {
1274       if (nval == lfi->rval && shared_member_p (nval))
1275         /* The two things are really the same.  */
1276         ;
1277       else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1278         /* The previous value hides the new one.  */
1279         ;
1280       else
1281         {
1282           /* We have a real ambiguity.  We keep a chain of all the
1283              candidates.  */
1284           if (!lfi->ambiguous && lfi->rval)
1285             {
1286               /* This is the first time we noticed an ambiguity.  Add
1287                  what we previously thought was a reasonable candidate
1288                  to the list.  */
1289               lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1290               TREE_TYPE (lfi->ambiguous) = error_mark_node;
1291             }
1292
1293           /* Add the new value.  */
1294           lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1295           TREE_TYPE (lfi->ambiguous) = error_mark_node;
1296           lfi->errstr = "request for member `%D' is ambiguous";
1297         }
1298     }
1299   else
1300     {
1301       if (from_dep_base_p && TREE_CODE (nval) == TYPE_DECL
1302           /* We need to return a member template class so we can
1303              define partial specializations.  Is there a better
1304              way?  */
1305           && !DECL_CLASS_TEMPLATE_P (nval))
1306         /* The thing we're looking for isn't a type, so the implicit
1307            typename extension doesn't apply, so we just pretend we
1308            didn't find anything.  */
1309         return NULL_TREE;
1310
1311       lfi->rval = nval;
1312       lfi->from_dep_base_p = from_dep_base_p;
1313       lfi->rval_binfo = binfo;
1314     }
1315
1316   return NULL_TREE;
1317 }
1318
1319 /* Return a "baselink" which BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1320    BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1321    FUNCTIONS, and OPTYPE respectively.  */
1322
1323 tree
1324 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1325 {
1326   tree baselink;
1327
1328   my_friendly_assert (TREE_CODE (functions) == FUNCTION_DECL
1329                       || TREE_CODE (functions) == TEMPLATE_DECL
1330                       || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1331                       || TREE_CODE (functions) == OVERLOAD,
1332                       20020730);
1333   my_friendly_assert (!optype || TYPE_P (optype), 20020730);
1334   my_friendly_assert (TREE_TYPE (functions), 20020805);
1335
1336   baselink = make_node (BASELINK);
1337   TREE_TYPE (baselink) = TREE_TYPE (functions);
1338   BASELINK_BINFO (baselink) = binfo;
1339   BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1340   BASELINK_FUNCTIONS (baselink) = functions;
1341   BASELINK_OPTYPE (baselink) = optype;
1342
1343   return baselink;
1344 }
1345
1346 /* Look for a member named NAME in an inheritance lattice dominated by
1347    XBASETYPE.  If PROTECT is 0 or two, we do not check access.  If it
1348    is 1, we enforce accessibility.  If PROTECT is zero, then, for an
1349    ambiguous lookup, we return NULL.  If PROTECT is 1, we issue error
1350    messages about inaccessible or ambiguous lookup.  If PROTECT is 2,
1351    we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1352    TREE_VALUEs are the list of ambiguous candidates.
1353
1354    WANT_TYPE is 1 when we should only return TYPE_DECLs.
1355
1356    If nothing can be found return NULL_TREE and do not issue an error.  */
1357
1358 tree
1359 lookup_member (tree xbasetype, tree name, int protect, bool want_type)
1360 {
1361   tree rval, rval_binfo = NULL_TREE;
1362   tree type = NULL_TREE, basetype_path = NULL_TREE;
1363   struct lookup_field_info lfi;
1364
1365   /* rval_binfo is the binfo associated with the found member, note,
1366      this can be set with useful information, even when rval is not
1367      set, because it must deal with ALL members, not just non-function
1368      members.  It is used for ambiguity checking and the hidden
1369      checks.  Whereas rval is only set if a proper (not hidden)
1370      non-function member is found.  */
1371
1372   const char *errstr = 0;
1373
1374   if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1375       && IDENTIFIER_CLASS_VALUE (name))
1376     {
1377       tree field = IDENTIFIER_CLASS_VALUE (name);
1378       if (TREE_CODE (field) != FUNCTION_DECL
1379           && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1380         /* We're in the scope of this class, and the value has already
1381            been looked up.  Just return the cached value.  */
1382         return field;
1383     }
1384
1385   if (TREE_CODE (xbasetype) == TREE_VEC)
1386     {
1387       type = BINFO_TYPE (xbasetype);
1388       basetype_path = xbasetype;
1389     }
1390   else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1391     {
1392       type = xbasetype;
1393       basetype_path = TYPE_BINFO (type);
1394       my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1395                           980827);
1396     }
1397   else
1398     abort ();
1399
1400   complete_type (type);
1401
1402 #ifdef GATHER_STATISTICS
1403   n_calls_lookup_field++;
1404 #endif /* GATHER_STATISTICS */
1405
1406   memset ((PTR) &lfi, 0, sizeof (lfi));
1407   lfi.type = type;
1408   lfi.name = name;
1409   lfi.want_type = want_type;
1410   bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
1411   rval = lfi.rval;
1412   rval_binfo = lfi.rval_binfo;
1413   if (rval_binfo)
1414     type = BINFO_TYPE (rval_binfo);
1415   errstr = lfi.errstr;
1416
1417   /* If we are not interested in ambiguities, don't report them;
1418      just return NULL_TREE.  */
1419   if (!protect && lfi.ambiguous)
1420     return NULL_TREE;
1421   
1422   if (protect == 2) 
1423     {
1424       if (lfi.ambiguous)
1425         return lfi.ambiguous;
1426       else
1427         protect = 0;
1428     }
1429
1430   /* [class.access]
1431
1432      In the case of overloaded function names, access control is
1433      applied to the function selected by overloaded resolution.  */
1434   if (rval && protect && !is_overloaded_fn (rval)
1435       && !enforce_access (xbasetype, rval))
1436     return error_mark_node;
1437
1438   if (errstr && protect)
1439     {
1440       error (errstr, name, type);
1441       if (lfi.ambiguous)
1442         print_candidates (lfi.ambiguous);
1443       rval = error_mark_node;
1444     }
1445
1446   /* If the thing we found was found via the implicit typename
1447      extension, build the typename type.  */
1448   if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1449     abort ();
1450
1451   if (rval && is_overloaded_fn (rval)) 
1452     rval = build_baselink (rval_binfo, basetype_path, rval,
1453                            (IDENTIFIER_TYPENAME_P (name)
1454                            ? TREE_TYPE (name): NULL_TREE));
1455   return rval;
1456 }
1457
1458 /* Like lookup_member, except that if we find a function member we
1459    return NULL_TREE.  */
1460
1461 tree
1462 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1463 {
1464   tree rval = lookup_member (xbasetype, name, protect, want_type);
1465   
1466   /* Ignore functions.  */
1467   if (rval && BASELINK_P (rval))
1468     return NULL_TREE;
1469
1470   return rval;
1471 }
1472
1473 /* Like lookup_member, except that if we find a non-function member we
1474    return NULL_TREE.  */
1475
1476 tree
1477 lookup_fnfields (tree xbasetype, tree name, int protect)
1478 {
1479   tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false);
1480
1481   /* Ignore non-functions.  */
1482   if (rval && !BASELINK_P (rval))
1483     return NULL_TREE;
1484
1485   return rval;
1486 }
1487
1488 /* TYPE is a class type. Return the index of the fields within
1489    the method vector with name NAME, or -1 is no such field exists.  */
1490
1491 int
1492 lookup_fnfields_1 (tree type, tree name)
1493 {
1494   tree method_vec = (CLASS_TYPE_P (type)
1495                      ? CLASSTYPE_METHOD_VEC (type)
1496                      : NULL_TREE);
1497
1498   if (method_vec != 0)
1499     {
1500       register int i;
1501       register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1502       int len = TREE_VEC_LENGTH (method_vec);
1503       tree tmp;
1504
1505 #ifdef GATHER_STATISTICS
1506       n_calls_lookup_fnfields_1++;
1507 #endif /* GATHER_STATISTICS */
1508
1509       /* Constructors are first...  */
1510       if (name == ctor_identifier)
1511         return (methods[CLASSTYPE_CONSTRUCTOR_SLOT] 
1512                 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
1513       /* and destructors are second.  */
1514       if (name == dtor_identifier)
1515         return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1516                 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
1517
1518       for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 
1519            i < len && methods[i]; 
1520            ++i)
1521         {
1522 #ifdef GATHER_STATISTICS
1523           n_outer_fields_searched++;
1524 #endif /* GATHER_STATISTICS */
1525
1526           tmp = OVL_CURRENT (methods[i]);
1527           if (DECL_NAME (tmp) == name)
1528             return i;
1529
1530           /* If the type is complete and we're past the conversion ops,
1531              switch to binary search.  */
1532           if (! DECL_CONV_FN_P (tmp)
1533               && COMPLETE_TYPE_P (type))
1534             {
1535               int lo = i + 1, hi = len;
1536
1537               while (lo < hi)
1538                 {
1539                   i = (lo + hi) / 2;
1540
1541 #ifdef GATHER_STATISTICS
1542                   n_outer_fields_searched++;
1543 #endif /* GATHER_STATISTICS */
1544
1545                   tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1546
1547                   if (tmp > name)
1548                     hi = i;
1549                   else if (tmp < name)
1550                     lo = i + 1;
1551                   else
1552                     return i;
1553                 }
1554               break;
1555             }
1556         }
1557
1558       /* If we didn't find it, it might have been a template
1559          conversion operator to a templated type.  If there are any,
1560          such template conversion operators will all be overloaded on
1561          the first conversion slot.  (Note that we don't look for this
1562          case above so that we will always find specializations
1563          first.)  */
1564       if (IDENTIFIER_TYPENAME_P (name)) 
1565         {
1566           i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1567           if (i < len && methods[i])
1568             {
1569               tmp = OVL_CURRENT (methods[i]);
1570               if (TREE_CODE (tmp) == TEMPLATE_DECL
1571                   && DECL_TEMPLATE_CONV_FN_P (tmp))
1572                 return i;
1573             }
1574         }
1575     }
1576
1577   return -1;
1578 }
1579
1580 /* DECL is the result of a qualified name lookup.  QUALIFYING_SCOPE is
1581    the class or namespace used to qualify the name.  CONTEXT_CLASS is
1582    the class corresponding to the object in which DECL will be used.
1583    Return a possibly modified version of DECL that takes into account
1584    the CONTEXT_CLASS.
1585
1586    In particular, consider an expression like `B::m' in the context of
1587    a derived class `D'.  If `B::m' has been resolved to a BASELINK,
1588    then the most derived class indicated by the BASELINK_BINFO will be
1589    `B', not `D'.  This function makes that adjustment.  */
1590
1591 tree
1592 adjust_result_of_qualified_name_lookup (tree decl, 
1593                                         tree qualifying_scope,
1594                                         tree context_class)
1595 {
1596   if (context_class && CLASS_TYPE_P (qualifying_scope) 
1597       && DERIVED_FROM_P (qualifying_scope, context_class)
1598       && BASELINK_P (decl))
1599     {
1600       tree base;
1601
1602       my_friendly_assert (CLASS_TYPE_P (context_class), 20020808);
1603
1604       /* Look for the QUALIFYING_SCOPE as a base of the
1605          CONTEXT_CLASS.  If QUALIFYING_SCOPE is ambiguous, we cannot
1606          be sure yet than an error has occurred; perhaps the function
1607          chosen by overload resolution will be static.  */
1608       base = lookup_base (context_class, qualifying_scope,
1609                           ba_ignore | ba_quiet, NULL);
1610       if (base)
1611         {
1612           BASELINK_ACCESS_BINFO (decl) = base;
1613           BASELINK_BINFO (decl) 
1614             = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1615                            ba_ignore | ba_quiet,
1616                            NULL);
1617         }
1618     }
1619
1620   return decl;
1621 }
1622
1623 \f
1624 /* Start with enough room for ten concurrent base classes.  That
1625    will be enough for most hierarchies.  */
1626 #define BFS_WALK_INITIAL_QUEUE_SIZE 10
1627
1628 /* Subroutine of bfs_walk; enlarges the buffer it uses for its
1629    circular queue.  */
1630 static void
1631 grow_bfs_bases (tree **basep, size_t *sizep, size_t *headp)
1632 {
1633   tree *base;
1634   size_t size = *sizep;
1635   size_t head = *headp;
1636
1637   /* If the size is BFS_WALK_INITIAL_QUEUE_SIZE, the old array is on
1638      the stack.  */
1639   if (size == BFS_WALK_INITIAL_QUEUE_SIZE)
1640     {
1641       base = xmalloc (size * 2 * sizeof(tree));
1642       memcpy (base, *basep, size * sizeof(tree));
1643     }
1644   else
1645     base = xrealloc (*basep, size * 2 * sizeof(tree));
1646
1647   *basep = base;
1648   *sizep = size * 2;
1649
1650   /* Shift all the elements between head and the former end of the
1651      array, opening up a gap between tail and head.  If head==0 we
1652      don't need to do anything to achieve this.  */
1653   if (head != 0)
1654     {
1655       memmove (&base[head + size], &base[head],
1656                (size - head) * sizeof (tree));
1657       *headp = head + size;
1658     }
1659 }
1660
1661 /* Walk the class hierarchy dominated by TYPE.  FN is called for each
1662    type in the hierarchy, in a breadth-first preorder traversal.
1663    If it ever returns a non-NULL value, that value is immediately
1664    returned and the walk is terminated.  At each node, FN is passed a
1665    BINFO indicating the path from the curently visited base-class to
1666    TYPE.  Before each base-class is walked QFN is called.  If the
1667    value returned is nonzero, the base-class is walked; otherwise it
1668    is not.  If QFN is NULL, it is treated as a function which always
1669    returns 1.  Both FN and QFN are passed the DATA whenever they are
1670    called.
1671
1672    Implementation notes: Uses a circular queue, which starts off on
1673    the stack but gets moved to the malloc arena if it needs to be
1674    enlarged.  The underflow and overflow conditions are
1675    indistinguishable except by context: if head == tail and we just
1676    moved the head pointer, the queue is empty, but if we just moved
1677    the tail pointer, the queue is full.  Base class vectors are only
1678    put on the queue if they are nonempty, which is why it's safe to
1679    use do-while for the inner loop.  */
1680
1681 static tree
1682 bfs_walk (tree binfo, tree (*fn) (tree, void *),
1683           tree (*qfn) (tree, void *), void *data)
1684 {
1685   tree rval = NULL_TREE;
1686
1687   tree bfs_bases_initial[BFS_WALK_INITIAL_QUEUE_SIZE];
1688   /* A circular queue of the base classes of BINFO.  These will be
1689      built up in breadth-first order, except where QFN prunes the
1690      search.  */
1691   size_t head, tail;
1692   size_t bfs_bases_size = BFS_WALK_INITIAL_QUEUE_SIZE;
1693   tree *bfs_bases = bfs_bases_initial;
1694
1695   /* Is the first one what we're looking for?  If so, we're done.  */
1696   rval = fn (binfo, data);
1697   if (rval)
1698     return rval;
1699
1700   /* If it has no base types, we are also done.  */
1701   if (BINFO_BASETYPES (binfo) == 0
1702       || TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 0)
1703     return 0;
1704
1705   /* Otherwise, initialize the queue with its basetypes vector
1706      and proceed.  */
1707
1708   head = tail = 0;
1709   bfs_bases[tail++] = BINFO_BASETYPES (binfo);
1710
1711   do
1712     {
1713       int i, n_baselinks;
1714       tree binfos;
1715       
1716       binfos = bfs_bases[head++];
1717       if (head == bfs_bases_size)
1718         head = 0;
1719
1720       i = 0;
1721       n_baselinks = TREE_VEC_LENGTH (binfos);
1722       do
1723         {
1724           binfo = TREE_VEC_ELT (binfos, i);
1725           i++;
1726
1727           if (qfn)
1728             binfo = qfn (binfo, data);
1729           if (!binfo)
1730             continue;
1731
1732           rval = fn (binfo, data);
1733           if (rval)
1734             goto done;
1735
1736           if (BINFO_BASETYPES (binfo) == 0
1737               || TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 0)
1738             continue;
1739
1740           bfs_bases[tail++] = BINFO_BASETYPES (binfo);
1741           if (tail == bfs_bases_size)
1742             tail = 0;
1743           if (tail == head)
1744             grow_bfs_bases (&bfs_bases, &bfs_bases_size, &head);
1745         }
1746       while (i < n_baselinks);
1747     }
1748   while (head != tail);
1749
1750  done:
1751   if (bfs_bases != bfs_bases_initial)
1752     free (bfs_bases);
1753   return rval;
1754 }
1755
1756 /* Exactly like bfs_walk, except that a depth-first traversal is
1757    performed, and PREFN is called in preorder, while POSTFN is called
1758    in postorder.  */
1759
1760 tree
1761 dfs_walk_real (tree binfo, 
1762                tree (*prefn) (tree, void *), tree (*postfn) (tree, void *),
1763                tree (*qfn) (tree, void *), void *data)
1764 {
1765   int i;
1766   int n_baselinks;
1767   tree binfos;
1768   tree rval = NULL_TREE;
1769
1770   /* Call the pre-order walking function.  */
1771   if (prefn)
1772     {
1773       rval = (*prefn) (binfo, data);
1774       if (rval)
1775         return rval;
1776     }
1777
1778   /* Process the basetypes.  */
1779   binfos = BINFO_BASETYPES (binfo);
1780   n_baselinks = BINFO_N_BASETYPES (binfo);
1781   for (i = 0; i < n_baselinks; i++)
1782     {
1783       tree base_binfo = TREE_VEC_ELT (binfos, i);
1784       
1785       if (qfn)
1786         base_binfo = (*qfn) (base_binfo, data);
1787
1788       if (base_binfo)
1789         {
1790           rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1791           if (rval)
1792             return rval;
1793         }
1794     }
1795
1796   /* Call the post-order walking function.  */
1797   if (postfn)
1798     rval = (*postfn) (binfo, data);
1799   
1800   return rval;
1801 }
1802
1803 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1804    performed.  */
1805
1806 tree
1807 dfs_walk (tree binfo, tree (*fn) (tree, void *),
1808           tree (*qfn) (tree, void *), void *data)
1809 {
1810   return dfs_walk_real (binfo, 0, fn, qfn, data);
1811 }
1812
1813 /* Check that virtual overrider OVERRIDER is acceptable for base function
1814    BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
1815
1816 int
1817 check_final_overrider (tree overrider, tree basefn)
1818 {
1819   tree over_type = TREE_TYPE (overrider);
1820   tree base_type = TREE_TYPE (basefn);
1821   tree over_return = TREE_TYPE (over_type);
1822   tree base_return = TREE_TYPE (base_type);
1823   tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1824   tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1825   int fail = 0;
1826   
1827   if (same_type_p (base_return, over_return))
1828     /* OK */;
1829   else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1830            || (TREE_CODE (base_return) == TREE_CODE (over_return)
1831                && POINTER_TYPE_P (base_return)))
1832     {
1833       /* Potentially covariant. */
1834       unsigned base_quals, over_quals;
1835       
1836       fail = !POINTER_TYPE_P (base_return);
1837       if (!fail)
1838         {
1839           fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1840           
1841           base_return = TREE_TYPE (base_return);
1842           over_return = TREE_TYPE (over_return);
1843         }
1844       base_quals = cp_type_quals (base_return);
1845       over_quals = cp_type_quals (over_return);
1846
1847       if ((base_quals & over_quals) != over_quals)
1848         fail = 1;
1849       
1850       if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1851         {
1852           tree binfo = lookup_base (over_return, base_return,
1853                                     ba_check | ba_quiet, NULL);
1854
1855           if (!binfo)
1856             fail = 1;
1857         }
1858       else if (!pedantic
1859                && can_convert (TREE_TYPE (base_type), TREE_TYPE (over_type)))
1860         /* GNU extension, allow trivial pointer conversions such as
1861            converting to void *, or qualification conversion.  */
1862         {
1863           /* can_convert will permit user defined conversion from a
1864              (reference to) class type. We must reject them. */
1865           over_return = TREE_TYPE (over_type);
1866           if (TREE_CODE (over_return) == REFERENCE_TYPE)
1867             over_return = TREE_TYPE (over_return);
1868           if (CLASS_TYPE_P (over_return))
1869             fail = 2;
1870         }
1871       else
1872         fail = 2;
1873     }
1874   else
1875     fail = 2;
1876   if (!fail)
1877     /* OK */;
1878   else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
1879     return 0;
1880   else
1881     {
1882       if (fail == 1)
1883         {
1884           cp_error_at ("invalid covariant return type for `%#D'", overrider);
1885           cp_error_at ("  overriding `%#D'", basefn);
1886         }
1887       else
1888         {
1889           cp_error_at ("conflicting return type specified for `%#D'",
1890                        overrider);
1891           cp_error_at ("  overriding `%#D'", basefn);
1892         }
1893       SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1894                                   DECL_CONTEXT (overrider));
1895       return 0;
1896     }
1897   
1898   /* Check throw specifier is at least as strict.  */
1899   if (!comp_except_specs (base_throw, over_throw, 0))
1900     {
1901       if (!IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
1902         {
1903           cp_error_at ("looser throw specifier for `%#F'", overrider);
1904           cp_error_at ("  overriding `%#F'", basefn);
1905           SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1906                                       DECL_CONTEXT (overrider));
1907         }
1908       return 0;
1909     }
1910   
1911   return 1;
1912 }
1913
1914 /* Given a class TYPE, and a function decl FNDECL, look for
1915    virtual functions in TYPE's hierarchy which FNDECL overrides.
1916    We do not look in TYPE itself, only its bases.
1917    
1918    Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1919    find that it overrides anything.
1920    
1921    We check that every function which is overridden, is correctly
1922    overridden.  */
1923
1924 int
1925 look_for_overrides (tree type, tree fndecl)
1926 {
1927   tree binfo = TYPE_BINFO (type);
1928   tree basebinfos = BINFO_BASETYPES (binfo);
1929   int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
1930   int ix;
1931   int found = 0;
1932
1933   for (ix = 0; ix != nbasebinfos; ix++)
1934     {
1935       tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
1936       
1937       if (TYPE_POLYMORPHIC_P (basetype))
1938         found += look_for_overrides_r (basetype, fndecl);
1939     }
1940   return found;
1941 }
1942
1943 /* Look in TYPE for virtual functions with the same signature as
1944    FNDECL.  */
1945
1946 tree
1947 look_for_overrides_here (tree type, tree fndecl)
1948 {
1949   int ix;
1950
1951   if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
1952     ix = CLASSTYPE_DESTRUCTOR_SLOT;
1953   else
1954     ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
1955   if (ix >= 0)
1956     {
1957       tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
1958   
1959       for (; fns; fns = OVL_NEXT (fns))
1960         {
1961           tree fn = OVL_CURRENT (fns);
1962
1963           if (!DECL_VIRTUAL_P (fn))
1964             /* Not a virtual.  */;
1965           else if (DECL_CONTEXT (fn) != type)
1966             /* Introduced with a using declaration.  */;
1967           else if (DECL_STATIC_FUNCTION_P (fndecl))
1968             {
1969               tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1970               tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1971               if (compparms (TREE_CHAIN (btypes), dtypes))
1972                 return fn;
1973             }
1974           else if (same_signature_p (fndecl, fn))
1975             return fn;
1976         }
1977     }
1978   return NULL_TREE;
1979 }
1980
1981 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1982    TYPE itself and its bases.  */
1983
1984 static int
1985 look_for_overrides_r (tree type, tree fndecl)
1986 {
1987   tree fn = look_for_overrides_here (type, fndecl);
1988   if (fn)
1989     {
1990       if (DECL_STATIC_FUNCTION_P (fndecl))
1991         {
1992           /* A static member function cannot match an inherited
1993              virtual member function.  */
1994           cp_error_at ("`%#D' cannot be declared", fndecl);
1995           cp_error_at ("  since `%#D' declared in base class", fn);
1996         }
1997       else
1998         {
1999           /* It's definitely virtual, even if not explicitly set.  */
2000           DECL_VIRTUAL_P (fndecl) = 1;
2001           check_final_overrider (fndecl, fn);
2002         }
2003       return 1;
2004     }
2005
2006   /* We failed to find one declared in this class. Look in its bases.  */
2007   return look_for_overrides (type, fndecl);
2008 }
2009
2010 /* A queue function to use with dfs_walk that only walks into
2011    canonical bases.  DATA should be the type of the complete object,
2012    or a TREE_LIST whose TREE_PURPOSE is the type of the complete
2013    object.  By using this function as a queue function, you will walk
2014    over exactly those BINFOs that actually exist in the complete
2015    object, including those for virtual base classes.  If you
2016    SET_BINFO_MARKED for each binfo you process, you are further
2017    guaranteed that you will walk into each virtual base class exactly
2018    once.  */
2019
2020 tree
2021 dfs_unmarked_real_bases_queue_p (tree binfo, void *data)
2022 {
2023   if (TREE_VIA_VIRTUAL (binfo))
2024     {
2025       tree type = (tree) data;
2026
2027       if (TREE_CODE (type) == TREE_LIST)
2028         type = TREE_PURPOSE (type);
2029       binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2030     }
2031   return unmarkedp (binfo, NULL);
2032 }
2033
2034 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2035    that are marked, rather than unmarked.  */
2036
2037 tree
2038 dfs_marked_real_bases_queue_p (tree binfo, void *data)
2039 {
2040   if (TREE_VIA_VIRTUAL (binfo))
2041     {
2042       tree type = (tree) data;
2043
2044       if (TREE_CODE (type) == TREE_LIST)
2045         type = TREE_PURPOSE (type);
2046       binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2047     }
2048   return markedp (binfo, NULL);
2049 }
2050
2051 /* A queue function that skips all virtual bases (and their 
2052    bases).  */
2053
2054 tree
2055 dfs_skip_vbases (tree binfo, void *data ATTRIBUTE_UNUSED)
2056 {
2057   if (TREE_VIA_VIRTUAL (binfo))
2058     return NULL_TREE;
2059
2060   return binfo;
2061 }
2062
2063 /* Called via dfs_walk from dfs_get_pure_virtuals.  */
2064
2065 static tree
2066 dfs_get_pure_virtuals (tree binfo, void *data)
2067 {
2068   tree type = (tree) data;
2069
2070   /* We're not interested in primary base classes; the derived class
2071      of which they are a primary base will contain the information we
2072      need.  */
2073   if (!BINFO_PRIMARY_P (binfo))
2074     {
2075       tree virtuals;
2076       
2077       for (virtuals = BINFO_VIRTUALS (binfo);
2078            virtuals;
2079            virtuals = TREE_CHAIN (virtuals))
2080         if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2081           CLASSTYPE_PURE_VIRTUALS (type) 
2082             = tree_cons (NULL_TREE, BV_FN (virtuals),
2083                          CLASSTYPE_PURE_VIRTUALS (type));
2084     }
2085   
2086   SET_BINFO_MARKED (binfo);
2087
2088   return NULL_TREE;
2089 }
2090
2091 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE.  */
2092
2093 void
2094 get_pure_virtuals (tree type)
2095 {
2096   tree vbases;
2097
2098   /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2099      is going to be overridden.  */
2100   CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
2101   /* Now, run through all the bases which are not primary bases, and
2102      collect the pure virtual functions.  We look at the vtable in
2103      each class to determine what pure virtual functions are present.
2104      (A primary base is not interesting because the derived class of
2105      which it is a primary base will contain vtable entries for the
2106      pure virtuals in the base class.  */
2107   dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals, 
2108             dfs_unmarked_real_bases_queue_p, type);
2109   dfs_walk (TYPE_BINFO (type), dfs_unmark, 
2110             dfs_marked_real_bases_queue_p, type);
2111
2112   /* Put the pure virtuals in dfs order.  */
2113   CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2114
2115   for (vbases = CLASSTYPE_VBASECLASSES (type); 
2116        vbases; 
2117        vbases = TREE_CHAIN (vbases))
2118     {
2119       tree virtuals;
2120
2121       for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
2122            virtuals;
2123            virtuals = TREE_CHAIN (virtuals))
2124         {
2125           tree base_fndecl = BV_FN (virtuals);
2126           if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2127             error ("`%#D' needs a final overrider", base_fndecl);
2128         }
2129     }
2130 }
2131 \f
2132 /* DEPTH-FIRST SEARCH ROUTINES.  */
2133
2134 tree 
2135 markedp (tree binfo, void *data ATTRIBUTE_UNUSED)
2136 {
2137   return BINFO_MARKED (binfo) ? binfo : NULL_TREE; 
2138 }
2139
2140 tree
2141 unmarkedp (tree binfo, void *data ATTRIBUTE_UNUSED)
2142 {
2143   return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2144 }
2145
2146 tree
2147 marked_vtable_pathp (tree binfo, void *data ATTRIBUTE_UNUSED)
2148
2149   return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE; 
2150 }
2151
2152 tree
2153 unmarked_vtable_pathp (tree binfo, void *data ATTRIBUTE_UNUSED)
2154
2155   return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE; 
2156 }
2157
2158 static tree
2159 marked_pushdecls_p (tree binfo, void *data ATTRIBUTE_UNUSED)
2160 {
2161   return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2162           && !dependent_base_p (binfo)
2163           && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE; 
2164 }
2165
2166 static tree
2167 unmarked_pushdecls_p (tree binfo, void *data ATTRIBUTE_UNUSED)
2168
2169   return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2170           && !dependent_base_p (binfo)
2171           && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2172 }
2173
2174 /* The worker functions for `dfs_walk'.  These do not need to
2175    test anything (vis a vis marking) if they are paired with
2176    a predicate function (above).  */
2177
2178 tree
2179 dfs_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
2180
2181   CLEAR_BINFO_MARKED (binfo); 
2182   return NULL_TREE;
2183 }
2184
2185 /* get virtual base class types.
2186    This adds type to the vbase_types list in reverse dfs order.
2187    Ordering is very important, so don't change it.  */
2188
2189 static tree
2190 dfs_get_vbase_types (tree binfo, void *data)
2191 {
2192   tree type = (tree) data;
2193
2194   if (TREE_VIA_VIRTUAL (binfo))
2195     CLASSTYPE_VBASECLASSES (type)
2196       = tree_cons (BINFO_TYPE (binfo), 
2197                    binfo, 
2198                    CLASSTYPE_VBASECLASSES (type));
2199   SET_BINFO_MARKED (binfo);
2200   return NULL_TREE;
2201 }
2202
2203 /* Called via dfs_walk from mark_primary_bases.  Builds the
2204    inheritance graph order list of BINFOs.  */
2205
2206 static tree
2207 dfs_build_inheritance_graph_order (tree binfo, void *data)
2208 {
2209   tree *last_binfo = (tree *) data;
2210
2211   if (*last_binfo)
2212     TREE_CHAIN (*last_binfo) = binfo;
2213   *last_binfo = binfo;
2214   SET_BINFO_MARKED (binfo);
2215   return NULL_TREE;
2216 }
2217
2218 /* Set CLASSTYPE_VBASECLASSES for TYPE.  */
2219
2220 void
2221 get_vbase_types (tree type)
2222 {
2223   tree last_binfo;
2224
2225   CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
2226   dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
2227   /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2228      reverse it so that we get normal dfs ordering.  */
2229   CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
2230   dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2231   /* Thread the BINFOs in inheritance-graph order.  */
2232   last_binfo = NULL;
2233   dfs_walk_real (TYPE_BINFO (type),
2234                  dfs_build_inheritance_graph_order,
2235                  NULL,
2236                  unmarkedp,
2237                  &last_binfo);
2238   dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
2239 }
2240
2241 /* Called from find_vbase_instance via dfs_walk.  */
2242
2243 static tree
2244 dfs_find_vbase_instance (tree binfo, void *data)
2245 {
2246   tree base = TREE_VALUE ((tree) data);
2247
2248   if (BINFO_PRIMARY_P (binfo)
2249       && same_type_p (BINFO_TYPE (binfo), base))
2250     return binfo;
2251
2252   return NULL_TREE;
2253 }
2254
2255 /* Find the real occurrence of the virtual BASE (a class type) in the
2256    hierarchy dominated by TYPE.  */
2257
2258 tree
2259 find_vbase_instance (tree base, tree type)
2260 {
2261   tree instance;
2262
2263   instance = binfo_for_vbase (base, type);
2264   if (!BINFO_PRIMARY_P (instance))
2265     return instance;
2266
2267   return dfs_walk (TYPE_BINFO (type), 
2268                    dfs_find_vbase_instance, 
2269                    NULL,
2270                    build_tree_list (type, base));
2271 }
2272
2273 \f
2274 /* Debug info for C++ classes can get very large; try to avoid
2275    emitting it everywhere.
2276
2277    Note that this optimization wins even when the target supports
2278    BINCL (if only slightly), and reduces the amount of work for the
2279    linker.  */
2280
2281 void
2282 maybe_suppress_debug_info (tree t)
2283 {
2284   /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2285      does not support name references between translation units.  It supports
2286      symbolic references between translation units, but only within a single
2287      executable or shared library.
2288
2289      For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2290      that the type was never defined, so we only get the members we
2291      actually define.  */
2292   if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
2293     return;
2294
2295   /* We might have set this earlier in cp_finish_decl.  */
2296   TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2297
2298   /* If we already know how we're handling this class, handle debug info
2299      the same way.  */
2300   if (CLASSTYPE_INTERFACE_KNOWN (t))
2301     {
2302       if (CLASSTYPE_INTERFACE_ONLY (t))
2303         TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2304       /* else don't set it.  */
2305     }
2306   /* If the class has a vtable, write out the debug info along with
2307      the vtable.  */
2308   else if (TYPE_CONTAINS_VPTR_P (t))
2309     TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2310
2311   /* Otherwise, just emit the debug info normally.  */
2312 }
2313
2314 /* Note that we want debugging information for a base class of a class
2315    whose vtable is being emitted.  Normally, this would happen because
2316    calling the constructor for a derived class implies calling the
2317    constructors for all bases, which involve initializing the
2318    appropriate vptr with the vtable for the base class; but in the
2319    presence of optimization, this initialization may be optimized
2320    away, so we tell finish_vtable_vardecl that we want the debugging
2321    information anyway.  */
2322
2323 static tree
2324 dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
2325 {
2326   tree t = BINFO_TYPE (binfo);
2327
2328   CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2329
2330   return NULL_TREE;
2331 }
2332
2333 /* Returns BINFO if we haven't already noted that we want debugging
2334    info for this base class.  */
2335
2336 static tree 
2337 dfs_debug_unmarkedp (tree binfo, void *data ATTRIBUTE_UNUSED)
2338
2339   return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) 
2340           ? binfo : NULL_TREE);
2341 }
2342
2343 /* Write out the debugging information for TYPE, whose vtable is being
2344    emitted.  Also walk through our bases and note that we want to
2345    write out information for them.  This avoids the problem of not
2346    writing any debug info for intermediate basetypes whose
2347    constructors, and thus the references to their vtables, and thus
2348    the vtables themselves, were optimized away.  */
2349
2350 void
2351 note_debug_info_needed (tree type)
2352 {
2353   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2354     {
2355       TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2356       rest_of_type_compilation (type, toplevel_bindings_p ());
2357     }
2358
2359   dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
2360 }
2361 \f
2362 /* Subroutines of push_class_decls ().  */
2363
2364 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2365    because it (or one of the intermediate bases) depends on template parms.  */
2366
2367 static int
2368 dependent_base_p (tree binfo)
2369 {
2370   for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2371     {
2372       if (currently_open_class (TREE_TYPE (binfo)))
2373         break;
2374       if (dependent_type_p (TREE_TYPE (binfo)))
2375         return 1;
2376     }
2377   return 0;
2378 }
2379
2380 static void
2381 setup_class_bindings (tree name, int type_binding_p)
2382 {
2383   tree type_binding = NULL_TREE;
2384   tree value_binding;
2385
2386   /* If we've already done the lookup for this declaration, we're
2387      done.  */
2388   if (IDENTIFIER_CLASS_VALUE (name))
2389     return;
2390
2391   /* First, deal with the type binding.  */
2392   if (type_binding_p)
2393     {
2394       type_binding = lookup_member (current_class_type, name,
2395                                     /*protect=*/2, /*want_type=*/true);
2396       if (TREE_CODE (type_binding) == TREE_LIST 
2397           && TREE_TYPE (type_binding) == error_mark_node)
2398         /* NAME is ambiguous.  */
2399         push_class_level_binding (name, type_binding);
2400       else
2401         pushdecl_class_level (type_binding);
2402     }
2403
2404   /* Now, do the value binding.  */
2405   value_binding = lookup_member (current_class_type, name,
2406                                  /*protect=*/2, /*want_type=*/false);
2407
2408   if (type_binding_p
2409       && (TREE_CODE (value_binding) == TYPE_DECL
2410           || DECL_CLASS_TEMPLATE_P (value_binding)
2411           || (TREE_CODE (value_binding) == TREE_LIST
2412               && TREE_TYPE (value_binding) == error_mark_node
2413               && (TREE_CODE (TREE_VALUE (value_binding))
2414                   == TYPE_DECL))))
2415     /* We found a type-binding, even when looking for a non-type
2416        binding.  This means that we already processed this binding
2417        above.  */;
2418   else if (value_binding)
2419     {
2420       if (TREE_CODE (value_binding) == TREE_LIST 
2421           && TREE_TYPE (value_binding) == error_mark_node)
2422         /* NAME is ambiguous.  */
2423         push_class_level_binding (name, value_binding);
2424       else
2425         {
2426           if (BASELINK_P (value_binding))
2427             /* NAME is some overloaded functions.  */
2428             value_binding = BASELINK_FUNCTIONS (value_binding);
2429           pushdecl_class_level (value_binding);
2430         }
2431     }
2432 }
2433
2434 /* Push class-level declarations for any names appearing in BINFO that
2435    are TYPE_DECLS.  */
2436
2437 static tree
2438 dfs_push_type_decls (tree binfo, void *data ATTRIBUTE_UNUSED)
2439 {
2440   tree type;
2441   tree fields;
2442
2443   type = BINFO_TYPE (binfo);
2444   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2445     if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
2446         && !(!same_type_p (type, current_class_type)
2447              && template_self_reference_p (type, fields)))
2448       setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
2449
2450   /* We can't just use BINFO_MARKED because envelope_add_decl uses
2451      DERIVED_FROM_P, which calls get_base_distance.  */
2452   SET_BINFO_PUSHDECLS_MARKED (binfo);
2453
2454   return NULL_TREE;
2455 }
2456
2457 /* Push class-level declarations for any names appearing in BINFO that
2458    are not TYPE_DECLS.  */
2459
2460 static tree
2461 dfs_push_decls (tree binfo, void *data)
2462 {
2463   tree type;
2464   tree method_vec;
2465   int dep_base_p;
2466
2467   type = BINFO_TYPE (binfo);
2468   dep_base_p = (processing_template_decl && type != current_class_type
2469                 && dependent_base_p (binfo));
2470   if (!dep_base_p)
2471     {
2472       tree fields;
2473       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2474         if (DECL_NAME (fields) 
2475             && TREE_CODE (fields) != TYPE_DECL
2476             && TREE_CODE (fields) != USING_DECL
2477             && !DECL_ARTIFICIAL (fields))
2478           setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
2479         else if (TREE_CODE (fields) == FIELD_DECL
2480                  && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2481           dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
2482           
2483       method_vec = (CLASS_TYPE_P (type) 
2484                     ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
2485
2486       if (method_vec && TREE_VEC_LENGTH (method_vec) >= 3)
2487         {
2488           tree *methods;
2489           tree *end;
2490
2491           /* Farm out constructors and destructors.  */
2492           end = TREE_VEC_END (method_vec);
2493
2494           for (methods = &TREE_VEC_ELT (method_vec, 2);
2495                methods < end && *methods;
2496                methods++)
2497             setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)), 
2498                                   /*type_binding_p=*/0);
2499         }
2500     }
2501
2502   CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
2503
2504   return NULL_TREE;
2505 }
2506
2507 /* When entering the scope of a class, we cache all of the
2508    fields that that class provides within its inheritance
2509    lattice.  Where ambiguities result, we mark them
2510    with `error_mark_node' so that if they are encountered
2511    without explicit qualification, we can emit an error
2512    message.  */
2513
2514 void
2515 push_class_decls (tree type)
2516 {
2517   search_stack = push_search_level (search_stack, &search_obstack);
2518
2519   /* Enter type declarations and mark.  */
2520   dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
2521
2522   /* Enter non-type declarations and unmark.  */
2523   dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
2524 }
2525
2526 /* Here's a subroutine we need because C lacks lambdas.  */
2527
2528 static tree
2529 dfs_unuse_fields (tree binfo, void *data ATTRIBUTE_UNUSED)
2530 {
2531   tree type = TREE_TYPE (binfo);
2532   tree fields;
2533
2534   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2535     {
2536       if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
2537         continue;
2538
2539       TREE_USED (fields) = 0;
2540       if (DECL_NAME (fields) == NULL_TREE
2541           && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2542         unuse_fields (TREE_TYPE (fields));
2543     }
2544
2545   return NULL_TREE;
2546 }
2547
2548 void
2549 unuse_fields (tree type)
2550 {
2551   dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
2552 }
2553
2554 void
2555 pop_class_decls ()
2556 {
2557   /* We haven't pushed a search level when dealing with cached classes,
2558      so we'd better not try to pop it.  */
2559   if (search_stack)
2560     search_stack = pop_search_level (search_stack);
2561 }
2562
2563 void
2564 print_search_statistics ()
2565 {
2566 #ifdef GATHER_STATISTICS
2567   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2568            n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2569   fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2570            n_outer_fields_searched, n_calls_lookup_fnfields);
2571   fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2572 #else /* GATHER_STATISTICS */
2573   fprintf (stderr, "no search statistics\n");
2574 #endif /* GATHER_STATISTICS */
2575 }
2576
2577 void
2578 init_search_processing ()
2579 {
2580   gcc_obstack_init (&search_obstack);
2581 }
2582
2583 void
2584 reinit_search_statistics ()
2585 {
2586 #ifdef GATHER_STATISTICS
2587   n_fields_searched = 0;
2588   n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2589   n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2590   n_calls_get_base_type = 0;
2591   n_outer_fields_searched = 0;
2592   n_contexts_saved = 0;
2593 #endif /* GATHER_STATISTICS */
2594 }
2595
2596 static tree
2597 add_conversions (tree binfo, void *data)
2598 {
2599   int i;
2600   tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2601   tree *conversions = (tree *) data;
2602
2603   /* Some builtin types have no method vector, not even an empty one.  */
2604   if (!method_vec)
2605     return NULL_TREE;
2606
2607   for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
2608     {
2609       tree tmp = TREE_VEC_ELT (method_vec, i);
2610       tree name;
2611
2612       if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
2613         break;
2614
2615       name = DECL_NAME (OVL_CURRENT (tmp));
2616
2617       /* Make sure we don't already have this conversion.  */
2618       if (! IDENTIFIER_MARKED (name))
2619         {
2620           *conversions = tree_cons (binfo, tmp, *conversions);
2621           IDENTIFIER_MARKED (name) = 1;
2622         }
2623     }
2624   return NULL_TREE;
2625 }
2626
2627 /* Return a TREE_LIST containing all the non-hidden user-defined
2628    conversion functions for TYPE (and its base-classes).  The
2629    TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2630    containing the conversion functions.  The TREE_PURPOSE is the BINFO
2631    from which the conversion functions in this node were selected.  */
2632
2633 tree
2634 lookup_conversions (tree type)
2635 {
2636   tree t;
2637   tree conversions = NULL_TREE;
2638
2639   if (COMPLETE_TYPE_P (type))
2640     bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
2641
2642   for (t = conversions; t; t = TREE_CHAIN (t))
2643     IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
2644
2645   return conversions;
2646 }
2647
2648 struct overlap_info 
2649 {
2650   tree compare_type;
2651   int found_overlap;
2652 };
2653
2654 /* Check whether the empty class indicated by EMPTY_BINFO is also present
2655    at offset 0 in COMPARE_TYPE, and set found_overlap if so.  */
2656
2657 static tree
2658 dfs_check_overlap (tree empty_binfo, void *data)
2659 {
2660   struct overlap_info *oi = (struct overlap_info *) data;
2661   tree binfo;
2662   for (binfo = TYPE_BINFO (oi->compare_type); 
2663        ; 
2664        binfo = BINFO_BASETYPE (binfo, 0))
2665     {
2666       if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
2667         {
2668           oi->found_overlap = 1;
2669           break;
2670         }
2671       else if (BINFO_BASETYPES (binfo) == NULL_TREE)
2672         break;
2673     }
2674
2675   return NULL_TREE;
2676 }
2677
2678 /* Trivial function to stop base traversal when we find something.  */
2679
2680 static tree
2681 dfs_no_overlap_yet (tree binfo, void *data)
2682 {
2683   struct overlap_info *oi = (struct overlap_info *) data;
2684   return !oi->found_overlap ? binfo : NULL_TREE;
2685 }
2686
2687 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2688    offset 0 in NEXT_TYPE.  Used in laying out empty base class subobjects.  */
2689
2690 int
2691 types_overlap_p (tree empty_type, tree next_type)
2692 {
2693   struct overlap_info oi;
2694
2695   if (! IS_AGGR_TYPE (next_type))
2696     return 0;
2697   oi.compare_type = next_type;
2698   oi.found_overlap = 0;
2699   dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
2700             dfs_no_overlap_yet, &oi);
2701   return oi.found_overlap;
2702 }
2703
2704 /* Given a vtable VAR, determine which of the inherited classes the vtable
2705    inherits (in a loose sense) functions from.
2706
2707    FIXME: This does not work with the new ABI.  */
2708
2709 tree
2710 binfo_for_vtable (tree var)
2711 {
2712   tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
2713   tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
2714   int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
2715   int i;
2716
2717   for (i = 0; i < n_baseclasses; i++)
2718     {
2719       tree base_binfo = TREE_VEC_ELT (binfos, i);
2720       if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
2721         return base_binfo;
2722     }
2723
2724   /* If no secondary base classes matched, return the primary base, if
2725      there is one.  */
2726   if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
2727     return get_primary_binfo (main_binfo);
2728
2729   return main_binfo;
2730 }
2731
2732 /* Returns the binfo of the first direct or indirect virtual base derived
2733    from BINFO, or NULL if binfo is not via virtual.  */
2734
2735 tree
2736 binfo_from_vbase (tree binfo)
2737 {
2738   for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2739     {
2740       if (TREE_VIA_VIRTUAL (binfo))
2741         return binfo;
2742     }
2743   return NULL_TREE;
2744 }
2745
2746 /* Returns the binfo of the first direct or indirect virtual base derived
2747    from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2748    via virtual.  */
2749
2750 tree
2751 binfo_via_virtual (tree binfo, tree limit)
2752 {
2753   for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
2754        binfo = BINFO_INHERITANCE_CHAIN (binfo))
2755     {
2756       if (TREE_VIA_VIRTUAL (binfo))
2757         return binfo;
2758     }
2759   return NULL_TREE;
2760 }
2761
2762 /* Returns the BINFO (if any) for the virtual baseclass T of the class
2763    C from the CLASSTYPE_VBASECLASSES list.  */
2764
2765 tree
2766 binfo_for_vbase (tree basetype, tree classtype)
2767 {
2768   tree binfo;
2769
2770   binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
2771   return binfo ? TREE_VALUE (binfo) : NULL_TREE;
2772 }