OSDN Git Service

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