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