OSDN Git Service

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