OSDN Git Service

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