OSDN Git Service

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