OSDN Git Service

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