OSDN Git Service

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