1 /* Breadth-first and depth-first routines for
2    searching multiple-inheritance lattice for GNU C++.
3    Copyright (C) 1987-2021 Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12 
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21 
22 /* High-level class interface.  */
23 
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "cp-tree.h"
28 #include "intl.h"
29 #include "toplev.h"
30 #include "spellcheck-tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 
34 static int is_subobject_of_p (tree, tree);
35 static tree dfs_lookup_base (tree, void *);
36 static tree dfs_dcast_hint_pre (tree, void *);
37 static tree dfs_dcast_hint_post (tree, void *);
38 static tree dfs_debug_mark (tree, void *);
39 static int check_hidden_convs (tree, int, int, tree, tree, tree);
40 static tree split_conversions (tree, tree, tree, tree);
41 static int lookup_conversions_r (tree, int, int, tree, tree, tree *);
42 static int look_for_overrides_r (tree, tree);
43 static tree lookup_field_r (tree, void *);
44 static tree dfs_accessible_post (tree, void *);
45 static tree dfs_walk_once_accessible (tree, bool,
46 				      tree (*pre_fn) (tree, void *),
47 				      tree (*post_fn) (tree, void *),
48 				      void *data);
49 static tree dfs_access_in_type (tree, void *);
50 static access_kind access_in_type (tree, tree);
51 static tree dfs_get_pure_virtuals (tree, void *);
52 
53 
54 /* Data for lookup_base and its workers.  */
55 
56 struct lookup_base_data_s
57 {
58   tree t;		/* type being searched.  */
59   tree base;		/* The base type we're looking for.  */
60   tree binfo;		/* Found binfo.  */
61   bool via_virtual;	/* Found via a virtual path.  */
62   bool ambiguous;	/* Found multiply ambiguous */
63   bool repeated_base;	/* Whether there are repeated bases in the
64 			    hierarchy.  */
65   bool want_any;	/* Whether we want any matching binfo.  */
66 };
67 
68 /* Worker function for lookup_base.  See if we've found the desired
69    base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S).  */
70 
71 static tree
dfs_lookup_base(tree binfo,void * data_)72 dfs_lookup_base (tree binfo, void *data_)
73 {
74   struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
75 
76   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
77     {
78       if (!data->binfo)
79 	{
80 	  data->binfo = binfo;
81 	  data->via_virtual
82 	    = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
83 
84 	  if (!data->repeated_base)
85 	    /* If there are no repeated bases, we can stop now.  */
86 	    return binfo;
87 
88 	  if (data->want_any && !data->via_virtual)
89 	    /* If this is a non-virtual base, then we can't do
90 	       better.  */
91 	    return binfo;
92 
93 	  return dfs_skip_bases;
94 	}
95       else
96 	{
97 	  gcc_assert (binfo != data->binfo);
98 
99 	  /* We've found more than one matching binfo.  */
100 	  if (!data->want_any)
101 	    {
102 	      /* This is immediately ambiguous.  */
103 	      data->binfo = NULL_TREE;
104 	      data->ambiguous = true;
105 	      return error_mark_node;
106 	    }
107 
108 	  /* Prefer one via a non-virtual path.  */
109 	  if (!binfo_via_virtual (binfo, data->t))
110 	    {
111 	      data->binfo = binfo;
112 	      data->via_virtual = false;
113 	      return binfo;
114 	    }
115 
116 	  /* There must be repeated bases, otherwise we'd have stopped
117 	     on the first base we found.  */
118 	  return dfs_skip_bases;
119 	}
120     }
121 
122   return NULL_TREE;
123 }
124 
125 /* This deals with bug PR17314.
126 
127    DECL is a declaration and BINFO represents a class that has attempted (but
128    failed) to access DECL.
129 
130    Examine the parent binfos of BINFO and determine whether any of them had
131    private access to DECL.  If they did, return the parent binfo.  This helps
132    in figuring out the correct error message to show (if the parents had
133    access, it's their fault for not giving sufficient access to BINFO).
134 
135    If no parents had access, return NULL_TREE.  */
136 
137 tree
get_parent_with_private_access(tree decl,tree binfo)138 get_parent_with_private_access (tree decl, tree binfo)
139 {
140   /* Only BINFOs should come through here.  */
141   gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
142 
143   tree base_binfo = NULL_TREE;
144 
145   /* Iterate through immediate parent classes.  */
146   for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
147     {
148       /* This parent had private access.  Therefore that's why BINFO can't
149 	  access DECL.  */
150       if (access_in_type (BINFO_TYPE (base_binfo), decl) == ak_private)
151 	return base_binfo;
152     }
153 
154   /* None of the parents had access.  Note: it's impossible for one of the
155      parents to have had public or protected access to DECL, since then
156      BINFO would have been able to access DECL too.  */
157   return NULL_TREE;
158 }
159 
160 /* Returns true if type BASE is accessible in T.  (BASE is known to be
161    a (possibly non-proper) base class of T.)  If CONSIDER_LOCAL_P is
162    true, consider any special access of the current scope, or access
163    bestowed by friendship.  */
164 
165 bool
accessible_base_p(tree t,tree base,bool consider_local_p)166 accessible_base_p (tree t, tree base, bool consider_local_p)
167 {
168   tree decl;
169 
170   /* [class.access.base]
171 
172      A base class is said to be accessible if an invented public
173      member of the base class is accessible.
174 
175      If BASE is a non-proper base, this condition is trivially
176      true.  */
177   if (same_type_p (t, base))
178     return true;
179   /* Rather than inventing a public member, we use the implicit
180      public typedef created in the scope of every class.  */
181   decl = TYPE_FIELDS (base);
182   while (!DECL_SELF_REFERENCE_P (decl))
183     decl = DECL_CHAIN (decl);
184   while (ANON_AGGR_TYPE_P (t))
185     t = TYPE_CONTEXT (t);
186   return accessible_p (t, decl, consider_local_p);
187 }
188 
189 /* Lookup BASE in the hierarchy dominated by T.  Do access checking as
190    ACCESS specifies.  Return the binfo we discover.  If KIND_PTR is
191    non-NULL, fill with information about what kind of base we
192    discovered.
193 
194    If the base is inaccessible, or ambiguous, then error_mark_node is
195    returned.  If the tf_error bit of COMPLAIN is not set, no error
196    is issued.  */
197 
198 tree
lookup_base(tree t,tree base,base_access access,base_kind * kind_ptr,tsubst_flags_t complain)199 lookup_base (tree t, tree base, base_access access,
200 	     base_kind *kind_ptr, tsubst_flags_t complain)
201 {
202   tree binfo;
203   tree t_binfo;
204   base_kind bk;
205 
206   /* "Nothing" is definitely not derived from Base.  */
207   if (t == NULL_TREE)
208     {
209       if (kind_ptr)
210 	*kind_ptr = bk_not_base;
211       return NULL_TREE;
212     }
213 
214   if (t == error_mark_node || base == error_mark_node)
215     {
216       if (kind_ptr)
217 	*kind_ptr = bk_not_base;
218       return error_mark_node;
219     }
220   gcc_assert (TYPE_P (base));
221 
222   if (!TYPE_P (t))
223     {
224       t_binfo = t;
225       t = BINFO_TYPE (t);
226     }
227   else
228     {
229       t = complete_type (TYPE_MAIN_VARIANT (t));
230       if (dependent_type_p (t))
231 	if (tree open = currently_open_class (t))
232 	  t = open;
233       t_binfo = TYPE_BINFO (t);
234     }
235 
236   base = TYPE_MAIN_VARIANT (base);
237 
238   /* If BASE is incomplete, it can't be a base of T--and instantiating it
239      might cause an error.  */
240   if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
241     {
242       struct lookup_base_data_s data;
243 
244       data.t = t;
245       data.base = base;
246       data.binfo = NULL_TREE;
247       data.ambiguous = data.via_virtual = false;
248       data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
249       data.want_any = access == ba_any;
250 
251       dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
252       binfo = data.binfo;
253 
254       if (!binfo)
255 	bk = data.ambiguous ? bk_ambig : bk_not_base;
256       else if (binfo == t_binfo)
257 	bk = bk_same_type;
258       else if (data.via_virtual)
259 	bk = bk_via_virtual;
260       else
261 	bk = bk_proper_base;
262     }
263   else
264     {
265       binfo = NULL_TREE;
266       bk = bk_not_base;
267     }
268 
269   /* Check that the base is unambiguous and accessible.  */
270   if (access != ba_any)
271     switch (bk)
272       {
273       case bk_not_base:
274 	break;
275 
276       case bk_ambig:
277 	if (complain & tf_error)
278 	  error ("%qT is an ambiguous base of %qT", base, t);
279 	binfo = error_mark_node;
280 	break;
281 
282       default:
283 	if ((access & ba_check_bit)
284 	    /* If BASE is incomplete, then BASE and TYPE are probably
285 	       the same, in which case BASE is accessible.  If they
286 	       are not the same, then TYPE is invalid.  In that case,
287 	       there's no need to issue another error here, and
288 	       there's no implicit typedef to use in the code that
289 	       follows, so we skip the check.  */
290 	    && COMPLETE_TYPE_P (base)
291 	    && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
292 	  {
293 	    if (complain & tf_error)
294 	      error ("%qT is an inaccessible base of %qT", base, t);
295 	    binfo = error_mark_node;
296 	    bk = bk_inaccessible;
297 	  }
298 	break;
299       }
300 
301   if (kind_ptr)
302     *kind_ptr = bk;
303 
304   return binfo;
305 }
306 
307 /* Data for dcast_base_hint walker.  */
308 
309 struct dcast_data_s
310 {
311   tree subtype;   /* The base type we're looking for.  */
312   int virt_depth; /* Number of virtual bases encountered from most
313 		     derived.  */
314   tree offset;    /* Best hint offset discovered so far.  */
315   bool repeated_base;  /* Whether there are repeated bases in the
316 			  hierarchy.  */
317 };
318 
319 /* Worker for dcast_base_hint.  Search for the base type being cast
320    from.  */
321 
322 static tree
dfs_dcast_hint_pre(tree binfo,void * data_)323 dfs_dcast_hint_pre (tree binfo, void *data_)
324 {
325   struct dcast_data_s *data = (struct dcast_data_s *) data_;
326 
327   if (BINFO_VIRTUAL_P (binfo))
328     data->virt_depth++;
329 
330   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
331     {
332       if (data->virt_depth)
333 	{
334 	  data->offset = ssize_int (-1);
335 	  return data->offset;
336 	}
337       if (data->offset)
338 	data->offset = ssize_int (-3);
339       else
340 	data->offset = BINFO_OFFSET (binfo);
341 
342       return data->repeated_base ? dfs_skip_bases : data->offset;
343     }
344 
345   return NULL_TREE;
346 }
347 
348 /* Worker for dcast_base_hint.  Track the virtual depth.  */
349 
350 static tree
dfs_dcast_hint_post(tree binfo,void * data_)351 dfs_dcast_hint_post (tree binfo, void *data_)
352 {
353   struct dcast_data_s *data = (struct dcast_data_s *) data_;
354 
355   if (BINFO_VIRTUAL_P (binfo))
356     data->virt_depth--;
357 
358   return NULL_TREE;
359 }
360 
361 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
362    started from is related to the required TARGET type, in order to optimize
363    the inheritance graph search. This information is independent of the
364    current context, and ignores private paths, hence get_base_distance is
365    inappropriate. Return a TREE specifying the base offset, BOFF.
366    BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
367       and there are no public virtual SUBTYPE bases.
368    BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
369    BOFF == -2, SUBTYPE is not a public base.
370    BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases.  */
371 
372 tree
dcast_base_hint(tree subtype,tree target)373 dcast_base_hint (tree subtype, tree target)
374 {
375   struct dcast_data_s data;
376 
377   data.subtype = subtype;
378   data.virt_depth = 0;
379   data.offset = NULL_TREE;
380   data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
381 
382   dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
383 			    dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
384   return data.offset ? data.offset : ssize_int (-2);
385 }
386 
387 /* Search for a member with name NAME in a multiple inheritance
388    lattice specified by TYPE.  If it does not exist, return NULL_TREE.
389    If the member is ambiguously referenced, return `error_mark_node'.
390    Otherwise, return a DECL with the indicated name.  If WANT_TYPE is
391    true, type declarations are preferred.  */
392 
393 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
394    NAMESPACE_DECL corresponding to the innermost non-block scope.  */
395 
396 tree
current_scope(void)397 current_scope (void)
398 {
399   /* There are a number of cases we need to be aware of here:
400 			 current_class_type	current_function_decl
401      global			NULL			NULL
402      fn-local			NULL			SET
403      class-local		SET			NULL
404      class->fn			SET			SET
405      fn->class			SET			SET
406 
407      Those last two make life interesting.  If we're in a function which is
408      itself inside a class, we need decls to go into the fn's decls (our
409      second case below).  But if we're in a class and the class itself is
410      inside a function, we need decls to go into the decls for the class.  To
411      achieve this last goal, we must see if, when both current_class_ptr and
412      current_function_decl are set, the class was declared inside that
413      function.  If so, we know to put the decls into the class's scope.  */
414   if (current_function_decl && current_class_type
415       && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
416 	   && same_type_p (DECL_CONTEXT (current_function_decl),
417 			   current_class_type))
418 	  || (DECL_FRIEND_CONTEXT (current_function_decl)
419 	      && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
420 			      current_class_type))))
421     return current_function_decl;
422 
423   if (current_class_type)
424     return current_class_type;
425 
426   if (current_function_decl)
427     return current_function_decl;
428 
429   return current_namespace;
430 }
431 
432 /* Returns nonzero if we are currently in a function scope.  Note
433    that this function returns zero if we are within a local class, but
434    not within a member function body of the local class.  */
435 
436 int
at_function_scope_p(void)437 at_function_scope_p (void)
438 {
439   tree cs = current_scope ();
440   /* Also check cfun to make sure that we're really compiling
441      this function (as opposed to having set current_function_decl
442      for access checking or some such).  */
443   return (cs && TREE_CODE (cs) == FUNCTION_DECL
444 	  && cfun && cfun->decl == current_function_decl);
445 }
446 
447 /* Returns true if the innermost active scope is a class scope.  */
448 
449 bool
at_class_scope_p(void)450 at_class_scope_p (void)
451 {
452   tree cs = current_scope ();
453   return cs && TYPE_P (cs);
454 }
455 
456 /* Returns true if the innermost active scope is a namespace scope.  */
457 
458 bool
at_namespace_scope_p(void)459 at_namespace_scope_p (void)
460 {
461   tree cs = current_scope ();
462   return cs && TREE_CODE (cs) == NAMESPACE_DECL;
463 }
464 
465 /* Return the scope of DECL, as appropriate when doing name-lookup.  */
466 
467 tree
context_for_name_lookup(tree decl)468 context_for_name_lookup (tree decl)
469 {
470   /* [class.union]
471 
472      For the purposes of name lookup, after the anonymous union
473      definition, the members of the anonymous union are considered to
474      have been defined in the scope in which the anonymous union is
475      declared.  */
476   tree context = DECL_CONTEXT (decl);
477 
478   while (context && TYPE_P (context)
479 	 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
480     context = TYPE_CONTEXT (context);
481   if (!context)
482     context = global_namespace;
483 
484   return context;
485 }
486 
487 /* Returns true iff DECL is declared in TYPE.  */
488 
489 static bool
member_declared_in_type(tree decl,tree type)490 member_declared_in_type (tree decl, tree type)
491 {
492   /* A normal declaration obviously counts.  */
493   if (context_for_name_lookup (decl) == type)
494     return true;
495   /* So does a using or access declaration.  */
496   if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
497       && purpose_member (type, DECL_ACCESS (decl)))
498     return true;
499   return false;
500 }
501 
502 /* The accessibility routines use BINFO_ACCESS for scratch space
503    during the computation of the accessibility of some declaration.  */
504 
505 /* Avoid walking up past a declaration of the member.  */
506 
507 static tree
dfs_access_in_type_pre(tree binfo,void * data)508 dfs_access_in_type_pre (tree binfo, void *data)
509 {
510   tree decl = (tree) data;
511   tree type = BINFO_TYPE (binfo);
512   if (member_declared_in_type (decl, type))
513     return dfs_skip_bases;
514   return NULL_TREE;
515 }
516 
517 #define BINFO_ACCESS(NODE) \
518   ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
519 
520 /* Set the access associated with NODE to ACCESS.  */
521 
522 #define SET_BINFO_ACCESS(NODE, ACCESS)			\
523   ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0),	\
524    (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
525 
526 /* Called from access_in_type via dfs_walk.  Calculate the access to
527    DATA (which is really a DECL) in BINFO.  */
528 
529 static tree
dfs_access_in_type(tree binfo,void * data)530 dfs_access_in_type (tree binfo, void *data)
531 {
532   tree decl = (tree) data;
533   tree type = BINFO_TYPE (binfo);
534   access_kind access = ak_none;
535 
536   if (context_for_name_lookup (decl) == type)
537     {
538       /* If we have descended to the scope of DECL, just note the
539 	 appropriate access.  */
540       if (TREE_PRIVATE (decl))
541 	access = ak_private;
542       else if (TREE_PROTECTED (decl))
543 	access = ak_protected;
544       else
545 	access = ak_public;
546     }
547   else
548     {
549       /* First, check for an access-declaration that gives us more
550 	 access to the DECL.  */
551       if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
552 	{
553 	  tree decl_access = purpose_member (type, DECL_ACCESS (decl));
554 
555 	  if (decl_access)
556 	    {
557 	      decl_access = TREE_VALUE (decl_access);
558 
559 	      if (decl_access == access_public_node)
560 		access = ak_public;
561 	      else if (decl_access == access_protected_node)
562 		access = ak_protected;
563 	      else if (decl_access == access_private_node)
564 		access = ak_private;
565 	      else
566 		gcc_unreachable ();
567 	    }
568 	}
569 
570       if (!access)
571 	{
572 	  int i;
573 	  tree base_binfo;
574 	  vec<tree, va_gc> *accesses;
575 
576 	  /* Otherwise, scan our baseclasses, and pick the most favorable
577 	     access.  */
578 	  accesses = BINFO_BASE_ACCESSES (binfo);
579 	  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
580 	    {
581 	      tree base_access = (*accesses)[i];
582 	      access_kind base_access_now = BINFO_ACCESS (base_binfo);
583 
584 	      if (base_access_now == ak_none || base_access_now == ak_private)
585 		/* If it was not accessible in the base, or only
586 		   accessible as a private member, we can't access it
587 		   all.  */
588 		base_access_now = ak_none;
589 	      else if (base_access == access_protected_node)
590 		/* Public and protected members in the base become
591 		   protected here.  */
592 		base_access_now = ak_protected;
593 	      else if (base_access == access_private_node)
594 		/* Public and protected members in the base become
595 		   private here.  */
596 		base_access_now = ak_private;
597 
598 	      /* See if the new access, via this base, gives more
599 		 access than our previous best access.  */
600 	      if (base_access_now != ak_none
601 		  && (access == ak_none || base_access_now < access))
602 		{
603 		  access = base_access_now;
604 
605 		  /* If the new access is public, we can't do better.  */
606 		  if (access == ak_public)
607 		    break;
608 		}
609 	    }
610 	}
611     }
612 
613   /* Note the access to DECL in TYPE.  */
614   SET_BINFO_ACCESS (binfo, access);
615 
616   return NULL_TREE;
617 }
618 
619 /* Return the access to DECL in TYPE.  */
620 
621 static access_kind
access_in_type(tree type,tree decl)622 access_in_type (tree type, tree decl)
623 {
624   tree binfo = TYPE_BINFO (type);
625 
626   /* We must take into account
627 
628        [class.paths]
629 
630        If a name can be reached by several paths through a multiple
631        inheritance graph, the access is that of the path that gives
632        most access.
633 
634     The algorithm we use is to make a post-order depth-first traversal
635     of the base-class hierarchy.  As we come up the tree, we annotate
636     each node with the most lenient access.  */
637   dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
638 
639   return BINFO_ACCESS (binfo);
640 }
641 
642 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
643    of OTYPE in the context of DERIVED.  */
644 
645 static int
protected_accessible_p(tree decl,tree derived,tree type,tree otype)646 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
647 {
648   /* We're checking this clause from [class.access.base]
649 
650        m as a member of N is protected, and the reference occurs in a
651        member or friend of class N, or in a member or friend of a
652        class P derived from N, where m as a member of P is public, private
653        or protected.
654 
655     Here DERIVED is a possible P, DECL is m and TYPE is N.  */
656 
657   /* If DERIVED isn't derived from N, then it can't be a P.  */
658   if (!DERIVED_FROM_P (type, derived))
659     return 0;
660 
661   /* DECL_NONSTATIC_MEMBER_P won't work for USING_DECLs.  */
662   decl = strip_using_decl (decl);
663   /* We don't expect or support dependent decls.  */
664   gcc_assert (TREE_CODE (decl) != USING_DECL);
665 
666   /* [class.protected]
667 
668      When a friend or a member function of a derived class references
669      a protected non-static member of a base class, an access check
670      applies in addition to those described earlier in clause
671      _class.access_) Except when forming a pointer to member
672      (_expr.unary.op_), the access must be through a pointer to,
673      reference to, or object of the derived class itself (or any class
674      derived from that class) (_expr.ref_).  If the access is to form
675      a pointer to member, the nested-name-specifier shall name the
676      derived class (or any class derived from that class).  */
677   if (DECL_NONSTATIC_MEMBER_P (decl)
678       && !DERIVED_FROM_P (derived, otype))
679     return 0;
680 
681   return 1;
682 }
683 
684 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
685    to access DECL through TYPE.  OTYPE is the type of the object.  */
686 
687 static int
friend_accessible_p(tree scope,tree decl,tree type,tree otype)688 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
689 {
690   /* We're checking this clause from [class.access.base]
691 
692        m as a member of N is protected, and the reference occurs in a
693        member or friend of class N, or in a member or friend of a
694        class P derived from N, where m as a member of P is public, private
695        or protected.
696 
697     Here DECL is m and TYPE is N.  SCOPE is the current context,
698     and we check all its possible Ps.  */
699   tree befriending_classes;
700   tree t;
701 
702   if (!scope)
703     return 0;
704 
705   if (is_global_friend (scope))
706     return 1;
707 
708   /* Is SCOPE itself a suitable P?  */
709   if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
710     return 1;
711 
712   if (DECL_DECLARES_FUNCTION_P (scope))
713     befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
714   else if (TYPE_P (scope))
715     befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
716   else
717     return 0;
718 
719   for (t = befriending_classes; t; t = TREE_CHAIN (t))
720     if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
721       return 1;
722 
723   /* Nested classes have the same access as their enclosing types, as
724      per DR 45 (this is a change from C++98).  */
725   if (TYPE_P (scope))
726     if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
727       return 1;
728 
729   if (DECL_DECLARES_FUNCTION_P (scope))
730     {
731       /* Perhaps this SCOPE is a member of a class which is a
732 	 friend.  */
733       if (DECL_CLASS_SCOPE_P (scope)
734 	  && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
735 	return 1;
736       /* Perhaps SCOPE is a friend function defined inside a class from which
737 	 DECL is accessible.  Checking this is necessary only when the class
738 	 is dependent, for otherwise add_friend will already have added the
739 	 class to SCOPE's DECL_BEFRIENDING_CLASSES.  */
740       if (tree fctx = DECL_FRIEND_CONTEXT (scope))
741 	if (dependent_type_p (fctx)
742 	    && protected_accessible_p (decl, fctx, type, otype))
743 	  return 1;
744     }
745 
746   /* Maybe scope's template is a friend.  */
747   if (tree tinfo = get_template_info (scope))
748     {
749       tree tmpl = TI_TEMPLATE (tinfo);
750       if (DECL_CLASS_TEMPLATE_P (tmpl))
751 	tmpl = TREE_TYPE (tmpl);
752       else
753 	tmpl = DECL_TEMPLATE_RESULT (tmpl);
754       if (tmpl != scope)
755 	{
756 	  /* Increment processing_template_decl to make sure that
757 	     dependent_type_p works correctly.  */
758 	  ++processing_template_decl;
759 	  int ret = friend_accessible_p (tmpl, decl, type, otype);
760 	  --processing_template_decl;
761 	  if (ret)
762 	    return 1;
763 	}
764     }
765 
766   /* If is_friend is true, we should have found a befriending class.  */
767   gcc_checking_assert (!is_friend (type, scope));
768 
769   return 0;
770 }
771 
772 struct dfs_accessible_data
773 {
774   tree decl;
775   tree object_type;
776 };
777 
778 /* Avoid walking up past a declaration of the member.  */
779 
780 static tree
dfs_accessible_pre(tree binfo,void * data)781 dfs_accessible_pre (tree binfo, void *data)
782 {
783   dfs_accessible_data *d = (dfs_accessible_data *)data;
784   tree type = BINFO_TYPE (binfo);
785   if (member_declared_in_type (d->decl, type))
786     return dfs_skip_bases;
787   return NULL_TREE;
788 }
789 
790 /* Called via dfs_walk_once_accessible from accessible_p */
791 
792 static tree
dfs_accessible_post(tree binfo,void * data)793 dfs_accessible_post (tree binfo, void *data)
794 {
795   /* access_in_type already set BINFO_ACCESS for us.  */
796   access_kind access = BINFO_ACCESS (binfo);
797   tree N = BINFO_TYPE (binfo);
798   dfs_accessible_data *d = (dfs_accessible_data *)data;
799   tree decl = d->decl;
800   tree scope = current_nonlambda_scope ();
801 
802   /* A member m is accessible at the point R when named in class N if */
803   switch (access)
804     {
805     case ak_none:
806       return NULL_TREE;
807 
808     case ak_public:
809       /* m as a member of N is public, or */
810       return binfo;
811 
812     case ak_private:
813       {
814 	/* m as a member of N is private, and R occurs in a member or friend of
815 	   class N, or */
816 	if (scope && TREE_CODE (scope) != NAMESPACE_DECL
817 	    && is_friend (N, scope))
818 	  return binfo;
819 	return NULL_TREE;
820       }
821 
822     case ak_protected:
823       {
824 	/* m as a member of N is protected, and R occurs in a member or friend
825 	   of class N, or in a member or friend of a class P derived from N,
826 	   where m as a member of P is public, private, or protected  */
827 	if (friend_accessible_p (scope, decl, N, d->object_type))
828 	  return binfo;
829 	return NULL_TREE;
830       }
831 
832     default:
833       gcc_unreachable ();
834     }
835 }
836 
837 /* Like accessible_p below, but within a template returns true iff DECL is
838    accessible in TYPE to all possible instantiations of the template.  */
839 
840 int
accessible_in_template_p(tree type,tree decl)841 accessible_in_template_p (tree type, tree decl)
842 {
843   int save_ptd = processing_template_decl;
844   processing_template_decl = 0;
845   int val = accessible_p (type, decl, false);
846   processing_template_decl = save_ptd;
847   return val;
848 }
849 
850 /* DECL is a declaration from a base class of TYPE, which was the
851    class used to name DECL.  Return nonzero if, in the current
852    context, DECL is accessible.  If TYPE is actually a BINFO node,
853    then we can tell in what context the access is occurring by looking
854    at the most derived class along the path indicated by BINFO.  If
855    CONSIDER_LOCAL is true, do consider special access the current
856    scope or friendship thereof we might have.  */
857 
858 int
accessible_p(tree type,tree decl,bool consider_local_p)859 accessible_p (tree type, tree decl, bool consider_local_p)
860 {
861   tree binfo;
862   access_kind access;
863 
864   /* If this declaration is in a block or namespace scope, there's no
865      access control.  */
866   if (!TYPE_P (context_for_name_lookup (decl)))
867     return 1;
868 
869   /* There is no need to perform access checks inside a thunk.  */
870   if (current_function_decl && DECL_THUNK_P (current_function_decl))
871     return 1;
872 
873   tree otype = NULL_TREE;
874   if (!TYPE_P (type))
875     {
876       /* When accessing a non-static member, the most derived type in the
877 	 binfo chain is the type of the object; remember that type for
878 	 protected_accessible_p.  */
879       for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
880 	otype = BINFO_TYPE (b);
881       type = BINFO_TYPE (type);
882     }
883   else
884     otype = type;
885 
886   /* [class.access.base]
887 
888      A member m is accessible when named in class N if
889 
890      --m as a member of N is public, or
891 
892      --m as a member of N is private, and the reference occurs in a
893        member or friend of class N, or
894 
895      --m as a member of N is protected, and the reference occurs in a
896        member or friend of class N, or in a member or friend of a
897        class P derived from N, where m as a member of P is public, private or
898        protected, or
899 
900      --there exists a base class B of N that is accessible at the point
901        of reference, and m is accessible when named in class B.
902 
903     We walk the base class hierarchy, checking these conditions.  */
904 
905   /* We walk using TYPE_BINFO (type) because access_in_type will set
906      BINFO_ACCESS on it and its bases.  */
907   binfo = TYPE_BINFO (type);
908 
909   /* Compute the accessibility of DECL in the class hierarchy
910      dominated by type.  */
911   access = access_in_type (type, decl);
912   if (access == ak_public)
913     return 1;
914 
915   /* If we aren't considering the point of reference, only the first bullet
916      applies.  */
917   if (!consider_local_p)
918     return 0;
919 
920   dfs_accessible_data d = { decl, otype };
921 
922   /* Walk the hierarchy again, looking for a base class that allows
923      access.  */
924   return dfs_walk_once_accessible (binfo, /*friends=*/true,
925 				   dfs_accessible_pre,
926 				   dfs_accessible_post, &d)
927     != NULL_TREE;
928 }
929 
930 struct lookup_field_info {
931   /* The type in which we're looking.  */
932   tree type;
933   /* The name of the field for which we're looking.  */
934   tree name;
935   /* If non-NULL, the current result of the lookup.  */
936   tree rval;
937   /* The path to RVAL.  */
938   tree rval_binfo;
939   /* If non-NULL, the lookup was ambiguous, and this is a list of the
940      candidates.  */
941   tree ambiguous;
942   /* If nonzero, we are looking for types, not data members.  */
943   int want_type;
944   /* If something went wrong, a message indicating what.  */
945   const char *errstr;
946 };
947 
948 /* True for a class member means that it is shared between all objects
949    of that class.
950 
951    [class.member.lookup]:If the resulting set of declarations are not all
952    from sub-objects of the same type, or the set has a non-static member
953    and  includes members from distinct sub-objects, there is an ambiguity
954    and the program is ill-formed.
955 
956    This function checks that T contains no non-static members.  */
957 
958 bool
shared_member_p(tree t)959 shared_member_p (tree t)
960 {
961   if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL
962       || TREE_CODE (t) == CONST_DECL)
963     return true;
964   if (is_overloaded_fn (t))
965     {
966       for (ovl_iterator iter (get_fns (t)); iter; ++iter)
967 	{
968 	  tree decl = strip_using_decl (*iter);
969 	  if (TREE_CODE (decl) == USING_DECL)
970 	    /* Conservatively assume a dependent using-declaration
971 	       might resolve to a non-static member.  */
972 	    return false;
973 	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
974 	    return false;
975 	}
976       return true;
977     }
978   return false;
979 }
980 
981 /* Routine to see if the sub-object denoted by the binfo PARENT can be
982    found as a base class and sub-object of the object denoted by
983    BINFO.  */
984 
985 static int
is_subobject_of_p(tree parent,tree binfo)986 is_subobject_of_p (tree parent, tree binfo)
987 {
988   tree probe;
989 
990   for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
991     {
992       if (probe == binfo)
993 	return 1;
994       if (BINFO_VIRTUAL_P (probe))
995 	return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
996 		!= NULL_TREE);
997     }
998   return 0;
999 }
1000 
1001 /* DATA is really a struct lookup_field_info.  Look for a field with
1002    the name indicated there in BINFO.  If this function returns a
1003    non-NULL value it is the result of the lookup.  Called from
1004    lookup_field via breadth_first_search.  */
1005 
1006 static tree
lookup_field_r(tree binfo,void * data)1007 lookup_field_r (tree binfo, void *data)
1008 {
1009   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1010   tree type = BINFO_TYPE (binfo);
1011   tree nval = NULL_TREE;
1012 
1013   /* If this is a dependent base, don't look in it.  */
1014   if (BINFO_DEPENDENT_BASE_P (binfo))
1015     return NULL_TREE;
1016 
1017   /* If this base class is hidden by the best-known value so far, we
1018      don't need to look.  */
1019   if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1020       && !BINFO_VIRTUAL_P (binfo))
1021     return dfs_skip_bases;
1022 
1023   nval = get_class_binding (type, lfi->name, lfi->want_type);
1024 
1025   /* If there is no declaration with the indicated name in this type,
1026      then there's nothing to do.  */
1027   if (!nval)
1028     goto done;
1029 
1030   /* If the lookup already found a match, and the new value doesn't
1031      hide the old one, we might have an ambiguity.  */
1032   if (lfi->rval_binfo
1033       && !is_subobject_of_p (lfi->rval_binfo, binfo))
1034 
1035     {
1036       if (nval == lfi->rval && shared_member_p (nval))
1037 	/* The two things are really the same.  */
1038 	;
1039       else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1040 	/* The previous value hides the new one.  */
1041 	;
1042       else
1043 	{
1044 	  /* We have a real ambiguity.  We keep a chain of all the
1045 	     candidates.  */
1046 	  if (!lfi->ambiguous && lfi->rval)
1047 	    {
1048 	      /* This is the first time we noticed an ambiguity.  Add
1049 		 what we previously thought was a reasonable candidate
1050 		 to the list.  */
1051 	      lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1052 	      TREE_TYPE (lfi->ambiguous) = error_mark_node;
1053 	    }
1054 
1055 	  /* Add the new value.  */
1056 	  lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1057 	  TREE_TYPE (lfi->ambiguous) = error_mark_node;
1058 	  lfi->errstr = G_("request for member %qD is ambiguous");
1059 	}
1060     }
1061   else
1062     {
1063       lfi->rval = nval;
1064       lfi->rval_binfo = binfo;
1065     }
1066 
1067  done:
1068   /* Don't look for constructors or destructors in base classes.  */
1069   if (IDENTIFIER_CDTOR_P (lfi->name))
1070     return dfs_skip_bases;
1071   return NULL_TREE;
1072 }
1073 
1074 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1075    BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1076    FUNCTIONS, and OPTYPE respectively.  */
1077 
1078 tree
build_baselink(tree binfo,tree access_binfo,tree functions,tree optype)1079 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1080 {
1081   tree baselink;
1082 
1083   gcc_assert (OVL_P (functions) || TREE_CODE (functions) == TEMPLATE_ID_EXPR);
1084   gcc_assert (!optype || TYPE_P (optype));
1085   gcc_assert (TREE_TYPE (functions));
1086 
1087   baselink = make_node (BASELINK);
1088   TREE_TYPE (baselink) = TREE_TYPE (functions);
1089   BASELINK_BINFO (baselink) = binfo;
1090   BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1091   BASELINK_FUNCTIONS (baselink) = functions;
1092   BASELINK_OPTYPE (baselink) = optype;
1093 
1094   return baselink;
1095 }
1096 
1097 /* Look for a member named NAME in an inheritance lattice dominated by
1098    XBASETYPE.  If PROTECT is 0 or two, we do not check access.  If it
1099    is 1, we enforce accessibility.  If PROTECT is zero, then, for an
1100    ambiguous lookup, we return NULL.  If PROTECT is 1, we issue error
1101    messages about inaccessible or ambiguous lookup.  If PROTECT is 2,
1102    we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1103    TREE_VALUEs are the list of ambiguous candidates.
1104 
1105    WANT_TYPE is 1 when we should only return TYPE_DECLs.
1106 
1107    If nothing can be found return NULL_TREE and do not issue an error.
1108 
1109    If non-NULL, failure information is written back to AFI.  */
1110 
1111 tree
lookup_member(tree xbasetype,tree name,int protect,bool want_type,tsubst_flags_t complain,access_failure_info * afi)1112 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1113 	       tsubst_flags_t complain, access_failure_info *afi)
1114 {
1115   tree rval, rval_binfo = NULL_TREE;
1116   tree type = NULL_TREE, basetype_path = NULL_TREE;
1117   struct lookup_field_info lfi;
1118 
1119   /* rval_binfo is the binfo associated with the found member, note,
1120      this can be set with useful information, even when rval is not
1121      set, because it must deal with ALL members, not just non-function
1122      members.  It is used for ambiguity checking and the hidden
1123      checks.  Whereas rval is only set if a proper (not hidden)
1124      non-function member is found.  */
1125 
1126   const char *errstr = 0;
1127 
1128   if (name == error_mark_node
1129       || xbasetype == NULL_TREE
1130       || xbasetype == error_mark_node)
1131     return NULL_TREE;
1132 
1133   gcc_assert (identifier_p (name));
1134 
1135   if (TREE_CODE (xbasetype) == TREE_BINFO)
1136     {
1137       type = BINFO_TYPE (xbasetype);
1138       basetype_path = xbasetype;
1139     }
1140   else
1141     {
1142       if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1143 	return NULL_TREE;
1144       type = xbasetype;
1145       xbasetype = NULL_TREE;
1146     }
1147 
1148   type = complete_type (type);
1149 
1150   /* Make sure we're looking for a member of the current instantiation in the
1151      right partial specialization.  */
1152   if (dependent_type_p (type))
1153     if (tree t = currently_open_class (type))
1154       type = t;
1155 
1156   if (!basetype_path)
1157     basetype_path = TYPE_BINFO (type);
1158 
1159   if (!basetype_path)
1160     return NULL_TREE;
1161 
1162   memset (&lfi, 0, sizeof (lfi));
1163   lfi.type = type;
1164   lfi.name = name;
1165   lfi.want_type = want_type;
1166   dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1167   rval = lfi.rval;
1168   rval_binfo = lfi.rval_binfo;
1169   if (rval_binfo)
1170     type = BINFO_TYPE (rval_binfo);
1171   errstr = lfi.errstr;
1172 
1173   /* If we are not interested in ambiguities, don't report them;
1174      just return NULL_TREE.  */
1175   if (!protect && lfi.ambiguous)
1176     return NULL_TREE;
1177 
1178   if (protect == 2)
1179     {
1180       if (lfi.ambiguous)
1181 	return lfi.ambiguous;
1182       else
1183 	protect = 0;
1184     }
1185 
1186   /* [class.access]
1187 
1188      In the case of overloaded function names, access control is
1189      applied to the function selected by overloaded resolution.
1190 
1191      We cannot check here, even if RVAL is only a single non-static
1192      member function, since we do not know what the "this" pointer
1193      will be.  For:
1194 
1195         class A { protected: void f(); };
1196         class B : public A {
1197           void g(A *p) {
1198             f(); // OK
1199             p->f(); // Not OK.
1200           }
1201         };
1202 
1203     only the first call to "f" is valid.  However, if the function is
1204     static, we can check.  */
1205   if (rval && protect
1206       && !really_overloaded_fn (rval))
1207     {
1208       tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1209       decl = strip_using_decl (decl);
1210       /* A dependent USING_DECL will be checked after tsubsting.  */
1211       if (TREE_CODE (decl) != USING_DECL
1212 	  && !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1213 	  && !perform_or_defer_access_check (basetype_path, decl, decl,
1214 					     complain, afi))
1215 	rval = error_mark_node;
1216     }
1217 
1218   if (errstr && protect)
1219     {
1220       if (complain & tf_error)
1221 	{
1222 	  error (errstr, name, type);
1223 	  if (lfi.ambiguous)
1224 	    print_candidates (lfi.ambiguous);
1225 	}
1226       rval = error_mark_node;
1227     }
1228 
1229   if (rval && is_overloaded_fn (rval))
1230     rval = build_baselink (rval_binfo, basetype_path, rval,
1231 			   (IDENTIFIER_CONV_OP_P (name)
1232 			   ? TREE_TYPE (name): NULL_TREE));
1233   return rval;
1234 }
1235 
1236 /* Helper class for lookup_member_fuzzy.  */
1237 
1238 class lookup_field_fuzzy_info
1239 {
1240  public:
lookup_field_fuzzy_info(bool want_type_p)1241   lookup_field_fuzzy_info (bool want_type_p) :
1242     m_want_type_p (want_type_p), m_candidates () {}
1243 
1244   void fuzzy_lookup_field (tree type);
1245 
1246   /* If true, we are looking for types, not data members.  */
1247   bool m_want_type_p;
1248   /* The result: a vec of identifiers.  */
1249   auto_vec<tree> m_candidates;
1250 };
1251 
1252 /* Locate all fields within TYPE, append them to m_candidates.  */
1253 
1254 void
fuzzy_lookup_field(tree type)1255 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1256 {
1257   if (!CLASS_TYPE_P (type))
1258     return;
1259 
1260   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1261     {
1262       if (m_want_type_p && !DECL_DECLARES_TYPE_P (field))
1263 	continue;
1264 
1265       if (!DECL_NAME (field))
1266 	continue;
1267 
1268       if (is_lambda_ignored_entity (field))
1269 	continue;
1270 
1271       m_candidates.safe_push (DECL_NAME (field));
1272     }
1273 }
1274 
1275 
1276 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1277    DATA is really a lookup_field_fuzzy_info.  Look for a field with
1278    the name indicated there in BINFO.  Gathers pertinent identifiers into
1279    m_candidates.  */
1280 
1281 static tree
lookup_field_fuzzy_r(tree binfo,void * data)1282 lookup_field_fuzzy_r (tree binfo, void *data)
1283 {
1284   lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1285   tree type = BINFO_TYPE (binfo);
1286 
1287   lffi->fuzzy_lookup_field (type);
1288 
1289   return NULL_TREE;
1290 }
1291 
1292 /* Like lookup_member, but try to find the closest match for NAME,
1293    rather than an exact match, and return an identifier (or NULL_TREE).
1294    Do not complain.  */
1295 
1296 tree
lookup_member_fuzzy(tree xbasetype,tree name,bool want_type_p)1297 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1298 {
1299   tree type = NULL_TREE, basetype_path = NULL_TREE;
1300   class lookup_field_fuzzy_info lffi (want_type_p);
1301 
1302   /* rval_binfo is the binfo associated with the found member, note,
1303      this can be set with useful information, even when rval is not
1304      set, because it must deal with ALL members, not just non-function
1305      members.  It is used for ambiguity checking and the hidden
1306      checks.  Whereas rval is only set if a proper (not hidden)
1307      non-function member is found.  */
1308 
1309   if (name == error_mark_node
1310       || xbasetype == NULL_TREE
1311       || xbasetype == error_mark_node)
1312     return NULL_TREE;
1313 
1314   gcc_assert (identifier_p (name));
1315 
1316   if (TREE_CODE (xbasetype) == TREE_BINFO)
1317     {
1318       type = BINFO_TYPE (xbasetype);
1319       basetype_path = xbasetype;
1320     }
1321   else
1322     {
1323       if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1324 	return NULL_TREE;
1325       type = xbasetype;
1326       xbasetype = NULL_TREE;
1327     }
1328 
1329   type = complete_type (type);
1330 
1331   /* Make sure we're looking for a member of the current instantiation in the
1332      right partial specialization.  */
1333   if (flag_concepts && dependent_type_p (type))
1334     type = currently_open_class (type);
1335 
1336   if (!basetype_path)
1337     basetype_path = TYPE_BINFO (type);
1338 
1339   if (!basetype_path)
1340     return NULL_TREE;
1341 
1342   /* Populate lffi.m_candidates.  */
1343   dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1344 
1345   return find_closest_identifier (name, &lffi.m_candidates);
1346 }
1347 
1348 /* Like lookup_member, except that if we find a function member we
1349    return NULL_TREE.  */
1350 
1351 tree
lookup_field(tree xbasetype,tree name,int protect,bool want_type)1352 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1353 {
1354   tree rval = lookup_member (xbasetype, name, protect, want_type,
1355 			     tf_warning_or_error);
1356 
1357   /* Ignore functions, but propagate the ambiguity list.  */
1358   if (!error_operand_p (rval)
1359       && (rval && BASELINK_P (rval)))
1360     return NULL_TREE;
1361 
1362   return rval;
1363 }
1364 
1365 /* Like lookup_member, except that if we find a non-function member we
1366    return NULL_TREE.  */
1367 
1368 tree
lookup_fnfields(tree xbasetype,tree name,int protect,tsubst_flags_t complain)1369 lookup_fnfields (tree xbasetype, tree name, int protect,
1370 		 tsubst_flags_t complain)
1371 {
1372   tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1373 			     complain);
1374 
1375   /* Ignore non-functions, but propagate the ambiguity list.  */
1376   if (!error_operand_p (rval)
1377       && (rval && !BASELINK_P (rval)))
1378     return NULL_TREE;
1379 
1380   return rval;
1381 }
1382 
1383 /* DECL is the result of a qualified name lookup.  QUALIFYING_SCOPE is
1384    the class or namespace used to qualify the name.  CONTEXT_CLASS is
1385    the class corresponding to the object in which DECL will be used.
1386    Return a possibly modified version of DECL that takes into account
1387    the CONTEXT_CLASS.
1388 
1389    In particular, consider an expression like `B::m' in the context of
1390    a derived class `D'.  If `B::m' has been resolved to a BASELINK,
1391    then the most derived class indicated by the BASELINK_BINFO will be
1392    `B', not `D'.  This function makes that adjustment.  */
1393 
1394 tree
adjust_result_of_qualified_name_lookup(tree decl,tree qualifying_scope,tree context_class)1395 adjust_result_of_qualified_name_lookup (tree decl,
1396 					tree qualifying_scope,
1397 					tree context_class)
1398 {
1399   if (context_class && context_class != error_mark_node
1400       && CLASS_TYPE_P (context_class)
1401       && CLASS_TYPE_P (qualifying_scope)
1402       && DERIVED_FROM_P (qualifying_scope, context_class)
1403       && BASELINK_P (decl))
1404     {
1405       tree base;
1406 
1407       /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1408 	 Because we do not yet know which function will be chosen by
1409 	 overload resolution, we cannot yet check either accessibility
1410 	 or ambiguity -- in either case, the choice of a static member
1411 	 function might make the usage valid.  */
1412       base = lookup_base (context_class, qualifying_scope,
1413 			  ba_unique, NULL, tf_none);
1414       if (base && base != error_mark_node)
1415 	{
1416 	  BASELINK_ACCESS_BINFO (decl) = base;
1417 	  tree decl_binfo
1418 	    = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1419 			   ba_unique, NULL, tf_none);
1420 	  if (decl_binfo && decl_binfo != error_mark_node)
1421 	    BASELINK_BINFO (decl) = decl_binfo;
1422 	}
1423     }
1424 
1425   if (BASELINK_P (decl))
1426     BASELINK_QUALIFIED_P (decl) = true;
1427 
1428   return decl;
1429 }
1430 
1431 
1432 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1433    PRE_FN is called in preorder, while POST_FN is called in postorder.
1434    If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1435    walked.  If PRE_FN or POST_FN returns a different non-NULL value,
1436    that value is immediately returned and the walk is terminated.  One
1437    of PRE_FN and POST_FN can be NULL.  At each node, PRE_FN and
1438    POST_FN are passed the binfo to examine and the caller's DATA
1439    value.  All paths are walked, thus virtual and morally virtual
1440    binfos can be multiply walked.  */
1441 
1442 tree
dfs_walk_all(tree binfo,tree (* pre_fn)(tree,void *),tree (* post_fn)(tree,void *),void * data)1443 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1444 	      tree (*post_fn) (tree, void *), void *data)
1445 {
1446   tree rval;
1447   unsigned ix;
1448   tree base_binfo;
1449 
1450   /* Call the pre-order walking function.  */
1451   if (pre_fn)
1452     {
1453       rval = pre_fn (binfo, data);
1454       if (rval)
1455 	{
1456 	  if (rval == dfs_skip_bases)
1457 	    goto skip_bases;
1458 	  return rval;
1459 	}
1460     }
1461 
1462   /* Find the next child binfo to walk.  */
1463   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1464     {
1465       rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1466       if (rval)
1467 	return rval;
1468     }
1469 
1470  skip_bases:
1471   /* Call the post-order walking function.  */
1472   if (post_fn)
1473     {
1474       rval = post_fn (binfo, data);
1475       gcc_assert (rval != dfs_skip_bases);
1476       return rval;
1477     }
1478 
1479   return NULL_TREE;
1480 }
1481 
1482 /* Worker for dfs_walk_once.  This behaves as dfs_walk_all, except
1483    that binfos are walked at most once.  */
1484 
1485 static tree
dfs_walk_once_r(tree binfo,tree (* pre_fn)(tree,void *),tree (* post_fn)(tree,void *),hash_set<tree> * pset,void * data)1486 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1487 		 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1488 		 void *data)
1489 {
1490   tree rval;
1491   unsigned ix;
1492   tree base_binfo;
1493 
1494   /* Call the pre-order walking function.  */
1495   if (pre_fn)
1496     {
1497       rval = pre_fn (binfo, data);
1498       if (rval)
1499 	{
1500 	  if (rval == dfs_skip_bases)
1501 	    goto skip_bases;
1502 
1503 	  return rval;
1504 	}
1505     }
1506 
1507   /* Find the next child binfo to walk.  */
1508   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1509     {
1510       if (BINFO_VIRTUAL_P (base_binfo))
1511 	if (pset->add (base_binfo))
1512 	  continue;
1513 
1514       rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1515       if (rval)
1516 	return rval;
1517     }
1518 
1519  skip_bases:
1520   /* Call the post-order walking function.  */
1521   if (post_fn)
1522     {
1523       rval = post_fn (binfo, data);
1524       gcc_assert (rval != dfs_skip_bases);
1525       return rval;
1526     }
1527 
1528   return NULL_TREE;
1529 }
1530 
1531 /* Like dfs_walk_all, except that binfos are not multiply walked.  For
1532    non-diamond shaped hierarchies this is the same as dfs_walk_all.
1533    For diamond shaped hierarchies we must mark the virtual bases, to
1534    avoid multiple walks.  */
1535 
1536 tree
dfs_walk_once(tree binfo,tree (* pre_fn)(tree,void *),tree (* post_fn)(tree,void *),void * data)1537 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1538 	       tree (*post_fn) (tree, void *), void *data)
1539 {
1540   static int active = 0;  /* We must not be called recursively. */
1541   tree rval;
1542 
1543   gcc_assert (pre_fn || post_fn);
1544   gcc_assert (!active);
1545   active++;
1546 
1547   if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1548     /* We are not diamond shaped, and therefore cannot encounter the
1549        same binfo twice.  */
1550     rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1551   else
1552     {
1553       hash_set<tree> pset;
1554       rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1555     }
1556 
1557   active--;
1558 
1559   return rval;
1560 }
1561 
1562 /* Worker function for dfs_walk_once_accessible.  Behaves like
1563    dfs_walk_once_r, except (a) FRIENDS_P is true if special
1564    access given by the current context should be considered, (b) ONCE
1565    indicates whether bases should be marked during traversal.  */
1566 
1567 static tree
dfs_walk_once_accessible_r(tree binfo,bool friends_p,hash_set<tree> * pset,tree (* pre_fn)(tree,void *),tree (* post_fn)(tree,void *),void * data)1568 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1569 			    tree (*pre_fn) (tree, void *),
1570 			    tree (*post_fn) (tree, void *), void *data)
1571 {
1572   tree rval = NULL_TREE;
1573   unsigned ix;
1574   tree base_binfo;
1575 
1576   /* Call the pre-order walking function.  */
1577   if (pre_fn)
1578     {
1579       rval = pre_fn (binfo, data);
1580       if (rval)
1581 	{
1582 	  if (rval == dfs_skip_bases)
1583 	    goto skip_bases;
1584 
1585 	  return rval;
1586 	}
1587     }
1588 
1589   /* Find the next child binfo to walk.  */
1590   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1591     {
1592       bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1593 
1594       if (mark && pset->contains (base_binfo))
1595 	continue;
1596 
1597       /* If the base is inherited via private or protected
1598 	 inheritance, then we can't see it, unless we are a friend of
1599 	 the current binfo.  */
1600       if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1601 	{
1602 	  tree scope;
1603 	  if (!friends_p)
1604 	    continue;
1605 	  scope = current_scope ();
1606 	  if (!scope
1607 	      || TREE_CODE (scope) == NAMESPACE_DECL
1608 	      || !is_friend (BINFO_TYPE (binfo), scope))
1609 	    continue;
1610 	}
1611 
1612       if (mark)
1613 	pset->add (base_binfo);
1614 
1615       rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1616 					 pre_fn, post_fn, data);
1617       if (rval)
1618 	return rval;
1619     }
1620 
1621  skip_bases:
1622   /* Call the post-order walking function.  */
1623   if (post_fn)
1624     {
1625       rval = post_fn (binfo, data);
1626       gcc_assert (rval != dfs_skip_bases);
1627       return rval;
1628     }
1629 
1630   return NULL_TREE;
1631 }
1632 
1633 /* Like dfs_walk_once except that only accessible bases are walked.
1634    FRIENDS_P indicates whether friendship of the local context
1635    should be considered when determining accessibility.  */
1636 
1637 static tree
dfs_walk_once_accessible(tree binfo,bool friends_p,tree (* pre_fn)(tree,void *),tree (* post_fn)(tree,void *),void * data)1638 dfs_walk_once_accessible (tree binfo, bool friends_p,
1639 			    tree (*pre_fn) (tree, void *),
1640 			    tree (*post_fn) (tree, void *), void *data)
1641 {
1642   hash_set<tree> *pset = NULL;
1643   if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1644     pset = new hash_set<tree>;
1645   tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1646 					  pre_fn, post_fn, data);
1647 
1648   if (pset)
1649     delete pset;
1650   return rval;
1651 }
1652 
1653 /* Return true iff the code of T is CODE, and it has compatible
1654    type with TYPE.  */
1655 
1656 static bool
matches_code_and_type_p(tree t,enum tree_code code,tree type)1657 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1658 {
1659   if (TREE_CODE (t) != code)
1660     return false;
1661   if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1662     return false;
1663   return true;
1664 }
1665 
1666 /* Subroutine of direct_accessor_p and reference_accessor_p.
1667    Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1668    We expect a tree of the form:
1669 	     <component_ref:
1670 	       <indirect_ref:S>
1671 		 <nop_expr:P*
1672 		   <parm_decl (this)>
1673 		 <field_decl (FIELD_DECL)>>>.  */
1674 
1675 static bool
field_access_p(tree component_ref,tree field_decl,tree field_type)1676 field_access_p (tree component_ref, tree field_decl, tree field_type)
1677 {
1678   if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
1679     return false;
1680 
1681   tree indirect_ref = TREE_OPERAND (component_ref, 0);
1682   if (!INDIRECT_REF_P (indirect_ref))
1683     return false;
1684 
1685   tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
1686   if (!is_this_parameter (ptr))
1687     return false;
1688 
1689   /* Must access the correct field.  */
1690   if (TREE_OPERAND (component_ref, 1) != field_decl)
1691     return false;
1692   return true;
1693 }
1694 
1695 /* Subroutine of field_accessor_p.
1696 
1697    Assuming that INIT_EXPR has already had its code and type checked,
1698    determine if it is a simple accessor for FIELD_DECL
1699    (of type FIELD_TYPE).
1700 
1701    Specifically, a simple accessor within struct S of the form:
1702        T get_field () { return m_field; }
1703    should have a constexpr_fn_retval (saved_tree) of the form:
1704 	 <init_expr:T
1705 	   <result_decl:T
1706 	   <nop_expr:T
1707 	     <component_ref:
1708 	       <indirect_ref:S>
1709 		 <nop_expr:P*
1710 		   <parm_decl (this)>
1711 		 <field_decl (FIELD_DECL)>>>>>.  */
1712 
1713 static bool
direct_accessor_p(tree init_expr,tree field_decl,tree field_type)1714 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1715 {
1716   tree result_decl = TREE_OPERAND (init_expr, 0);
1717   if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1718     return false;
1719 
1720   tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1721   if (!field_access_p (component_ref, field_decl, field_type))
1722     return false;
1723 
1724   return true;
1725 }
1726 
1727 /* Subroutine of field_accessor_p.
1728 
1729    Assuming that INIT_EXPR has already had its code and type checked,
1730    determine if it is a "reference" accessor for FIELD_DECL
1731    (of type FIELD_REFERENCE_TYPE).
1732 
1733    Specifically, a simple accessor within struct S of the form:
1734        T& get_field () { return m_field; }
1735    should have a constexpr_fn_retval (saved_tree) of the form:
1736 	 <init_expr:T&
1737 	   <result_decl:T&
1738 	   <nop_expr: T&
1739 	     <addr_expr: T*
1740 	       <component_ref:T
1741 		 <indirect_ref:S
1742 		   <nop_expr
1743 		     <parm_decl (this)>>
1744 		   <field (FIELD_DECL)>>>>>>.  */
1745 static bool
reference_accessor_p(tree init_expr,tree field_decl,tree field_type,tree field_reference_type)1746 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1747 		      tree field_reference_type)
1748 {
1749   tree result_decl = TREE_OPERAND (init_expr, 0);
1750   if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1751     return false;
1752 
1753   tree field_pointer_type = build_pointer_type (field_type);
1754   tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1755   if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1756     return false;
1757 
1758   tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1759 
1760   if (!field_access_p (component_ref, field_decl, field_type))
1761     return false;
1762 
1763   return true;
1764 }
1765 
1766 /* Return true if FN is an accessor method for FIELD_DECL.
1767    i.e. a method of the form { return FIELD; }, with no
1768    conversions.
1769 
1770    If CONST_P, then additionally require that FN be a const
1771    method.  */
1772 
1773 static bool
field_accessor_p(tree fn,tree field_decl,bool const_p)1774 field_accessor_p (tree fn, tree field_decl, bool const_p)
1775 {
1776   if (TREE_CODE (fn) != FUNCTION_DECL)
1777     return false;
1778 
1779   /* We don't yet support looking up static data, just fields.  */
1780   if (TREE_CODE (field_decl) != FIELD_DECL)
1781     return false;
1782 
1783   tree fntype = TREE_TYPE (fn);
1784   if (TREE_CODE (fntype) != METHOD_TYPE)
1785     return false;
1786 
1787   /* If the field is accessed via a const "this" argument, verify
1788      that the "this" parameter is const.  */
1789   if (const_p)
1790     {
1791       tree this_class = class_of_this_parm (fntype);
1792       if (!TYPE_READONLY (this_class))
1793 	return false;
1794     }
1795 
1796   tree saved_tree = DECL_SAVED_TREE (fn);
1797 
1798   if (saved_tree == NULL_TREE)
1799     return false;
1800 
1801   /* Attempt to extract a single return value from the function,
1802      if it has one.  */
1803   tree retval = constexpr_fn_retval (saved_tree);
1804   if (retval == NULL_TREE || retval == error_mark_node)
1805     return false;
1806   /* Require an INIT_EXPR.  */
1807   if (TREE_CODE (retval) != INIT_EXPR)
1808     return false;
1809   tree init_expr = retval;
1810 
1811   /* Determine if this is a simple accessor within struct S of the form:
1812        T get_field () { return m_field; }.  */
1813   tree field_type = TREE_TYPE (field_decl);
1814   if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1815     return direct_accessor_p (init_expr, field_decl, field_type);
1816 
1817   /* Failing that, determine if it is an accessor of the form:
1818        T& get_field () { return m_field; }.  */
1819   tree field_reference_type = cp_build_reference_type (field_type, false);
1820   if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1821     return reference_accessor_p (init_expr, field_decl, field_type,
1822 				 field_reference_type);
1823 
1824   return false;
1825 }
1826 
1827 /* Callback data for dfs_locate_field_accessor_pre.  */
1828 
1829 class locate_field_data
1830 {
1831 public:
locate_field_data(tree field_decl_,bool const_p_)1832   locate_field_data (tree field_decl_, bool const_p_)
1833   : field_decl (field_decl_), const_p (const_p_) {}
1834 
1835   tree field_decl;
1836   bool const_p;
1837 };
1838 
1839 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1840    callable via binfo, if one exists, otherwise return NULL_TREE.
1841 
1842    Callback for dfs_walk_once_accessible for use within
1843    locate_field_accessor.  */
1844 
1845 static tree
dfs_locate_field_accessor_pre(tree binfo,void * data)1846 dfs_locate_field_accessor_pre (tree binfo, void *data)
1847 {
1848   locate_field_data *lfd = (locate_field_data *)data;
1849   tree type = BINFO_TYPE (binfo);
1850 
1851   vec<tree, va_gc> *member_vec;
1852   tree fn;
1853   size_t i;
1854 
1855   if (!CLASS_TYPE_P (type))
1856     return NULL_TREE;
1857 
1858   member_vec = CLASSTYPE_MEMBER_VEC (type);
1859   if (!member_vec)
1860     return NULL_TREE;
1861 
1862   for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1863     if (fn)
1864       if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1865 	return fn;
1866 
1867   return NULL_TREE;
1868 }
1869 
1870 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1871    callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE.  */
1872 
1873 tree
locate_field_accessor(tree basetype_path,tree field_decl,bool const_p)1874 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1875 {
1876   if (TREE_CODE (basetype_path) != TREE_BINFO)
1877     return NULL_TREE;
1878 
1879   /* Walk the hierarchy, looking for a method of some base class that allows
1880      access to the field.  */
1881   locate_field_data lfd (field_decl, const_p);
1882   return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1883 				   dfs_locate_field_accessor_pre,
1884 				   NULL, &lfd);
1885 }
1886 
1887 /* Check throw specifier of OVERRIDER is at least as strict as
1888    the one of BASEFN.  */
1889 
1890 bool
maybe_check_overriding_exception_spec(tree overrider,tree basefn)1891 maybe_check_overriding_exception_spec (tree overrider, tree basefn)
1892 {
1893   maybe_instantiate_noexcept (basefn);
1894   maybe_instantiate_noexcept (overrider);
1895   tree base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1896   tree over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1897 
1898   if (DECL_INVALID_OVERRIDER_P (overrider))
1899     return true;
1900 
1901   /* Can't check this yet.  Pretend this is fine and let
1902      noexcept_override_late_checks check this later.  */
1903   if (UNPARSED_NOEXCEPT_SPEC_P (base_throw)
1904       || UNPARSED_NOEXCEPT_SPEC_P (over_throw))
1905     return true;
1906 
1907   if (!comp_except_specs (base_throw, over_throw, ce_derived))
1908     {
1909       auto_diagnostic_group d;
1910       error ("looser exception specification on overriding virtual function "
1911 	     "%q+#F", overrider);
1912       inform (DECL_SOURCE_LOCATION (basefn),
1913 	      "overridden function is %q#F", basefn);
1914       DECL_INVALID_OVERRIDER_P (overrider) = 1;
1915       return false;
1916     }
1917   return true;
1918 }
1919 
1920 /* Check that virtual overrider OVERRIDER is acceptable for base function
1921    BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
1922 
1923 static int
check_final_overrider(tree overrider,tree basefn)1924 check_final_overrider (tree overrider, tree basefn)
1925 {
1926   tree over_type = TREE_TYPE (overrider);
1927   tree base_type = TREE_TYPE (basefn);
1928   tree over_return = fndecl_declared_return_type (overrider);
1929   tree base_return = fndecl_declared_return_type (basefn);
1930 
1931   int fail = 0;
1932 
1933   if (DECL_INVALID_OVERRIDER_P (overrider))
1934     return 0;
1935 
1936   if (same_type_p (base_return, over_return))
1937     /* OK */;
1938   else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1939 	   || (TREE_CODE (base_return) == TREE_CODE (over_return)
1940 	       && INDIRECT_TYPE_P (base_return)))
1941     {
1942       /* Potentially covariant.  */
1943       unsigned base_quals, over_quals;
1944 
1945       fail = !INDIRECT_TYPE_P (base_return);
1946       if (!fail)
1947 	{
1948 	  fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1949 
1950 	  base_return = TREE_TYPE (base_return);
1951 	  over_return = TREE_TYPE (over_return);
1952 	}
1953       base_quals = cp_type_quals (base_return);
1954       over_quals = cp_type_quals (over_return);
1955 
1956       if ((base_quals & over_quals) != over_quals)
1957 	fail = 1;
1958 
1959       if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1960 	{
1961 	  /* Strictly speaking, the standard requires the return type to be
1962 	     complete even if it only differs in cv-quals, but that seems
1963 	     like a bug in the wording.  */
1964 	  if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1965 							  over_return))
1966 	    {
1967 	      tree binfo = lookup_base (over_return, base_return,
1968 					ba_check, NULL, tf_none);
1969 
1970 	      if (!binfo || binfo == error_mark_node)
1971 		fail = 1;
1972 	    }
1973 	}
1974       else if (can_convert_standard (TREE_TYPE (base_type),
1975 				     TREE_TYPE (over_type),
1976 				     tf_warning_or_error))
1977 	/* GNU extension, allow trivial pointer conversions such as
1978 	   converting to void *, or qualification conversion.  */
1979 	{
1980 	  auto_diagnostic_group d;
1981 	  if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1982 		       "invalid covariant return type for %q#D", overrider))
1983 	    inform (DECL_SOURCE_LOCATION (basefn),
1984 		    "overridden function is %q#D", basefn);
1985 	}
1986       else
1987 	fail = 2;
1988     }
1989   else
1990     fail = 2;
1991   if (!fail)
1992     /* OK */;
1993   else
1994     {
1995       auto_diagnostic_group d;
1996       if (fail == 1)
1997 	error ("invalid covariant return type for %q+#D", overrider);
1998       else
1999 	error ("conflicting return type specified for %q+#D", overrider);
2000       inform (DECL_SOURCE_LOCATION (basefn),
2001 	      "overridden function is %q#D", basefn);
2002       DECL_INVALID_OVERRIDER_P (overrider) = 1;
2003       return 0;
2004     }
2005 
2006   if (!maybe_check_overriding_exception_spec (overrider, basefn))
2007     return 0;
2008 
2009   /* Check for conflicting type attributes.  But leave transaction_safe for
2010      set_one_vmethod_tm_attributes.  */
2011   if (!comp_type_attributes (over_type, base_type)
2012       && !tx_safe_fn_type_p (base_type)
2013       && !tx_safe_fn_type_p (over_type))
2014     {
2015       auto_diagnostic_group d;
2016       error ("conflicting type attributes specified for %q+#D", overrider);
2017       inform (DECL_SOURCE_LOCATION (basefn),
2018 	      "overridden function is %q#D", basefn);
2019       DECL_INVALID_OVERRIDER_P (overrider) = 1;
2020       return 0;
2021     }
2022 
2023   /* A consteval virtual function shall not override a virtual function that is
2024      not consteval. A consteval virtual function shall not be overridden by a
2025      virtual function that is not consteval.  */
2026   if (DECL_IMMEDIATE_FUNCTION_P (overrider)
2027       != DECL_IMMEDIATE_FUNCTION_P (basefn))
2028     {
2029       auto_diagnostic_group d;
2030       if (DECL_IMMEDIATE_FUNCTION_P (overrider))
2031 	error ("%<consteval%> function %q+D overriding non-%<consteval%> "
2032 	       "function", overrider);
2033       else
2034 	error ("non-%<consteval%> function %q+D overriding %<consteval%> "
2035 	       "function", overrider);
2036       inform (DECL_SOURCE_LOCATION (basefn),
2037 	      "overridden function is %qD", basefn);
2038       DECL_INVALID_OVERRIDER_P (overrider) = 1;
2039       return 0;
2040     }
2041 
2042   /* A function declared transaction_safe_dynamic that overrides a function
2043      declared transaction_safe (but not transaction_safe_dynamic) is
2044      ill-formed.  */
2045   if (tx_safe_fn_type_p (base_type)
2046       && lookup_attribute ("transaction_safe_dynamic",
2047 			   DECL_ATTRIBUTES (overrider))
2048       && !lookup_attribute ("transaction_safe_dynamic",
2049 			    DECL_ATTRIBUTES (basefn)))
2050     {
2051       auto_diagnostic_group d;
2052       error_at (DECL_SOURCE_LOCATION (overrider),
2053 		"%qD declared %<transaction_safe_dynamic%>", overrider);
2054       inform (DECL_SOURCE_LOCATION (basefn),
2055 	      "overriding %qD declared %<transaction_safe%>", basefn);
2056     }
2057 
2058   if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2059     {
2060       if (DECL_DELETED_FN (overrider))
2061 	{
2062 	  auto_diagnostic_group d;
2063 	  error ("deleted function %q+D overriding non-deleted function",
2064 		 overrider);
2065 	  inform (DECL_SOURCE_LOCATION (basefn),
2066 		  "overridden function is %qD", basefn);
2067 	  maybe_explain_implicit_delete (overrider);
2068 	}
2069       else
2070 	{
2071 	  auto_diagnostic_group d;
2072 	  error ("non-deleted function %q+D overriding deleted function",
2073 		 overrider);
2074 	  inform (DECL_SOURCE_LOCATION (basefn),
2075 		  "overridden function is %qD", basefn);
2076 	}
2077       return 0;
2078     }
2079   if (DECL_FINAL_P (basefn))
2080     {
2081       auto_diagnostic_group d;
2082       error ("virtual function %q+D overriding final function", overrider);
2083       inform (DECL_SOURCE_LOCATION (basefn),
2084 	      "overridden function is %qD", basefn);
2085       return 0;
2086     }
2087   return 1;
2088 }
2089 
2090 /* Given a class TYPE, and a function decl FNDECL, look for
2091    virtual functions in TYPE's hierarchy which FNDECL overrides.
2092    We do not look in TYPE itself, only its bases.
2093 
2094    Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2095    find that it overrides anything.
2096 
2097    We check that every function which is overridden, is correctly
2098    overridden.  */
2099 
2100 int
look_for_overrides(tree type,tree fndecl)2101 look_for_overrides (tree type, tree fndecl)
2102 {
2103   tree binfo = TYPE_BINFO (type);
2104   tree base_binfo;
2105   int ix;
2106   int found = 0;
2107 
2108   /* A constructor for a class T does not override a function T
2109      in a base class.  */
2110   if (DECL_CONSTRUCTOR_P (fndecl))
2111     return 0;
2112 
2113   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2114     {
2115       tree basetype = BINFO_TYPE (base_binfo);
2116 
2117       if (TYPE_POLYMORPHIC_P (basetype))
2118 	found += look_for_overrides_r (basetype, fndecl);
2119     }
2120   return found;
2121 }
2122 
2123 /* Look in TYPE for virtual functions with the same signature as
2124    FNDECL.  */
2125 
2126 tree
look_for_overrides_here(tree type,tree fndecl)2127 look_for_overrides_here (tree type, tree fndecl)
2128 {
2129   tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2130 
2131   for (ovl_iterator iter (ovl); iter; ++iter)
2132     {
2133       tree fn = *iter;
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(tree type,tree fndecl)2157 look_for_overrides_r (tree type, tree fndecl)
2158 {
2159   tree fn = look_for_overrides_here (type, fndecl);
2160   if (fn)
2161     {
2162       if (DECL_STATIC_FUNCTION_P (fndecl))
2163 	{
2164 	  /* A static member function cannot match an inherited
2165 	     virtual member function.  */
2166 	  auto_diagnostic_group d;
2167 	  error ("%q+#D cannot be declared", fndecl);
2168 	  error ("  since %q+#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 /* Called via dfs_walk from dfs_get_pure_virtuals.  */
2184 
2185 static tree
dfs_get_pure_virtuals(tree binfo,void * data)2186 dfs_get_pure_virtuals (tree binfo, void *data)
2187 {
2188   tree type = (tree) data;
2189 
2190   /* We're not interested in primary base classes; the derived class
2191      of which they are a primary base will contain the information we
2192      need.  */
2193   if (!BINFO_PRIMARY_P (binfo))
2194     {
2195       tree virtuals;
2196 
2197       for (virtuals = BINFO_VIRTUALS (binfo);
2198 	   virtuals;
2199 	   virtuals = TREE_CHAIN (virtuals))
2200 	if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2201 	  vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2202     }
2203 
2204   return NULL_TREE;
2205 }
2206 
2207 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE.  */
2208 
2209 void
get_pure_virtuals(tree type)2210 get_pure_virtuals (tree type)
2211 {
2212   /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2213      is going to be overridden.  */
2214   CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2215   /* Now, run through all the bases which are not primary bases, and
2216      collect the pure virtual functions.  We look at the vtable in
2217      each class to determine what pure virtual functions are present.
2218      (A primary base is not interesting because the derived class of
2219      which it is a primary base will contain vtable entries for the
2220      pure virtuals in the base class.  */
2221   dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2222 }
2223 
2224 /* Debug info for C++ classes can get very large; try to avoid
2225    emitting it everywhere.
2226 
2227    Note that this optimization wins even when the target supports
2228    BINCL (if only slightly), and reduces the amount of work for the
2229    linker.  */
2230 
2231 void
maybe_suppress_debug_info(tree t)2232 maybe_suppress_debug_info (tree t)
2233 {
2234   if (write_symbols == NO_DEBUG)
2235     return;
2236 
2237   /* We might have set this earlier in cp_finish_decl.  */
2238   TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2239 
2240   /* Always emit the information for each class every time. */
2241   if (flag_emit_class_debug_always)
2242     return;
2243 
2244   /* If we already know how we're handling this class, handle debug info
2245      the same way.  */
2246   if (CLASSTYPE_INTERFACE_KNOWN (t))
2247     {
2248       if (CLASSTYPE_INTERFACE_ONLY (t))
2249 	TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2250       /* else don't set it.  */
2251     }
2252   /* If the class has a vtable, write out the debug info along with
2253      the vtable.  */
2254   else if (TYPE_CONTAINS_VPTR_P (t))
2255     TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2256 
2257   /* Otherwise, just emit the debug info normally.  */
2258 }
2259 
2260 /* Note that we want debugging information for a base class of a class
2261    whose vtable is being emitted.  Normally, this would happen because
2262    calling the constructor for a derived class implies calling the
2263    constructors for all bases, which involve initializing the
2264    appropriate vptr with the vtable for the base class; but in the
2265    presence of optimization, this initialization may be optimized
2266    away, so we tell finish_vtable_vardecl that we want the debugging
2267    information anyway.  */
2268 
2269 static tree
dfs_debug_mark(tree binfo,void *)2270 dfs_debug_mark (tree binfo, void * /*data*/)
2271 {
2272   tree t = BINFO_TYPE (binfo);
2273 
2274   if (CLASSTYPE_DEBUG_REQUESTED (t))
2275     return dfs_skip_bases;
2276 
2277   CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2278 
2279   return NULL_TREE;
2280 }
2281 
2282 /* Write out the debugging information for TYPE, whose vtable is being
2283    emitted.  Also walk through our bases and note that we want to
2284    write out information for them.  This avoids the problem of not
2285    writing any debug info for intermediate basetypes whose
2286    constructors, and thus the references to their vtables, and thus
2287    the vtables themselves, were optimized away.  */
2288 
2289 void
note_debug_info_needed(tree type)2290 note_debug_info_needed (tree type)
2291 {
2292   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2293     {
2294       TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2295       rest_of_type_compilation (type, namespace_bindings_p ());
2296     }
2297 
2298   dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2299 }
2300 
2301 /* Helper for lookup_conversions_r.  TO_TYPE is the type converted to
2302    by a conversion op in base BINFO.  VIRTUAL_DEPTH is nonzero if
2303    BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2304    bases have been encountered already in the tree walk.  PARENT_CONVS
2305    is the list of lists of conversion functions that could hide CONV
2306    and OTHER_CONVS is the list of lists of conversion functions that
2307    could hide or be hidden by CONV, should virtualness be involved in
2308    the hierarchy.  Merely checking the conversion op's name is not
2309    enough because two conversion operators to the same type can have
2310    different names.  Return nonzero if we are visible.  */
2311 
2312 static int
check_hidden_convs(tree binfo,int virtual_depth,int virtualness,tree to_type,tree parent_convs,tree other_convs)2313 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2314 		    tree to_type, tree parent_convs, tree other_convs)
2315 {
2316   tree level, probe;
2317 
2318   /* See if we are hidden by a parent conversion.  */
2319   for (level = parent_convs; level; level = TREE_CHAIN (level))
2320     for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2321       if (same_type_p (to_type, TREE_TYPE (probe)))
2322 	return 0;
2323 
2324   if (virtual_depth || virtualness)
2325     {
2326      /* In a virtual hierarchy, we could be hidden, or could hide a
2327 	conversion function on the other_convs list.  */
2328       for (level = other_convs; level; level = TREE_CHAIN (level))
2329 	{
2330 	  int we_hide_them;
2331 	  int they_hide_us;
2332 	  tree *prev, other;
2333 
2334 	  if (!(virtual_depth || TREE_STATIC (level)))
2335 	    /* Neither is morally virtual, so cannot hide each other.  */
2336 	    continue;
2337 
2338 	  if (!TREE_VALUE (level))
2339 	    /* They evaporated away already.  */
2340 	    continue;
2341 
2342 	  they_hide_us = (virtual_depth
2343 			  && original_binfo (binfo, TREE_PURPOSE (level)));
2344 	  we_hide_them = (!they_hide_us && TREE_STATIC (level)
2345 			  && original_binfo (TREE_PURPOSE (level), binfo));
2346 
2347 	  if (!(we_hide_them || they_hide_us))
2348 	    /* Neither is within the other, so no hiding can occur.  */
2349 	    continue;
2350 
2351 	  for (prev = &TREE_VALUE (level), other = *prev; other;)
2352 	    {
2353 	      if (same_type_p (to_type, TREE_TYPE (other)))
2354 		{
2355 		  if (they_hide_us)
2356 		    /* We are hidden.  */
2357 		    return 0;
2358 
2359 		  if (we_hide_them)
2360 		    {
2361 		      /* We hide the other one.  */
2362 		      other = TREE_CHAIN (other);
2363 		      *prev = other;
2364 		      continue;
2365 		    }
2366 		}
2367 	      prev = &TREE_CHAIN (other);
2368 	      other = *prev;
2369 	    }
2370 	}
2371     }
2372   return 1;
2373 }
2374 
2375 /* Helper for lookup_conversions_r.  PARENT_CONVS is a list of lists
2376    of conversion functions, the first slot will be for the current
2377    binfo, if MY_CONVS is non-NULL.  CHILD_CONVS is the list of lists
2378    of conversion functions from children of the current binfo,
2379    concatenated with conversions from elsewhere in the hierarchy --
2380    that list begins with OTHER_CONVS.  Return a single list of lists
2381    containing only conversions from the current binfo and its
2382    children.  */
2383 
2384 static tree
split_conversions(tree my_convs,tree parent_convs,tree child_convs,tree other_convs)2385 split_conversions (tree my_convs, tree parent_convs,
2386 		   tree child_convs, tree other_convs)
2387 {
2388   tree t;
2389   tree prev;
2390 
2391   /* Remove the original other_convs portion from child_convs.  */
2392   for (prev = NULL, t = child_convs;
2393        t != other_convs; prev = t, t = TREE_CHAIN (t))
2394     continue;
2395 
2396   if (prev)
2397     TREE_CHAIN (prev) = NULL_TREE;
2398   else
2399     child_convs = NULL_TREE;
2400 
2401   /* Attach the child convs to any we had at this level.  */
2402   if (my_convs)
2403     {
2404       my_convs = parent_convs;
2405       TREE_CHAIN (my_convs) = child_convs;
2406     }
2407   else
2408     my_convs = child_convs;
2409 
2410   return my_convs;
2411 }
2412 
2413 /* Worker for lookup_conversions.  Lookup conversion functions in
2414    BINFO and its children.  VIRTUAL_DEPTH is nonzero, if BINFO is in a
2415    morally virtual base, and VIRTUALNESS is nonzero, if we've
2416    encountered virtual bases already in the tree walk.  PARENT_CONVS
2417    is a list of conversions within parent binfos.  OTHER_CONVS are
2418    conversions found elsewhere in the tree.  Return the conversions
2419    found within this portion of the graph in CONVS.  Return nonzero if
2420    we encountered virtualness.  We keep template and non-template
2421    conversions separate, to avoid unnecessary type comparisons.
2422 
2423    The located conversion functions are held in lists of lists.  The
2424    TREE_VALUE of the outer list is the list of conversion functions
2425    found in a particular binfo.  The TREE_PURPOSE of both the outer
2426    and inner lists is the binfo at which those conversions were
2427    found.  TREE_STATIC is set for those lists within of morally
2428    virtual binfos.  The TREE_VALUE of the inner list is the conversion
2429    function or overload itself.  The TREE_TYPE of each inner list node
2430    is the converted-to type.  */
2431 
2432 static int
lookup_conversions_r(tree binfo,int virtual_depth,int virtualness,tree parent_convs,tree other_convs,tree * convs)2433 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2434 		      tree parent_convs, tree other_convs, tree *convs)
2435 {
2436   int my_virtualness = 0;
2437   tree my_convs = NULL_TREE;
2438   tree child_convs = NULL_TREE;
2439 
2440   /* If we have no conversion operators, then don't look.  */
2441   if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2442     {
2443       *convs = NULL_TREE;
2444 
2445       return 0;
2446     }
2447 
2448   if (BINFO_VIRTUAL_P (binfo))
2449     virtual_depth++;
2450 
2451   /* First, locate the unhidden ones at this level.  */
2452   if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2453   for (ovl_iterator iter (conv); iter; ++iter)
2454     {
2455       tree fn = *iter;
2456       tree type = DECL_CONV_FN_TYPE (fn);
2457 
2458       if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2459 	{
2460 	  mark_used (fn);
2461 	  type = DECL_CONV_FN_TYPE (fn);
2462 	}
2463 
2464       if (check_hidden_convs (binfo, virtual_depth, virtualness,
2465 			      type, parent_convs, other_convs))
2466 	{
2467 	  my_convs = tree_cons (binfo, fn, my_convs);
2468 	  TREE_TYPE (my_convs) = type;
2469 	  if (virtual_depth)
2470 	    {
2471 	      TREE_STATIC (my_convs) = 1;
2472 	      my_virtualness = 1;
2473 	    }
2474 	}
2475     }
2476 
2477   if (my_convs)
2478     {
2479       parent_convs = tree_cons (binfo, my_convs, parent_convs);
2480       if (virtual_depth)
2481 	TREE_STATIC (parent_convs) = 1;
2482     }
2483 
2484   child_convs = other_convs;
2485 
2486   /* Now iterate over each base, looking for more conversions.  */
2487   unsigned i;
2488   tree base_binfo;
2489   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2490     {
2491       tree base_convs;
2492       unsigned base_virtualness;
2493 
2494       base_virtualness = lookup_conversions_r (base_binfo,
2495 					       virtual_depth, virtualness,
2496 					       parent_convs, child_convs,
2497 					       &base_convs);
2498       if (base_virtualness)
2499 	my_virtualness = virtualness = 1;
2500       child_convs = chainon (base_convs, child_convs);
2501     }
2502 
2503   *convs = split_conversions (my_convs, parent_convs,
2504 			      child_convs, other_convs);
2505 
2506   return my_virtualness;
2507 }
2508 
2509 /* Return a TREE_LIST containing all the non-hidden user-defined
2510    conversion functions for TYPE (and its base-classes).  The
2511    TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2512    function.  The TREE_PURPOSE is the BINFO from which the conversion
2513    functions in this node were selected.  This function is effectively
2514    performing a set of member lookups as lookup_fnfield does, but
2515    using the type being converted to as the unique key, rather than the
2516    field name.  */
2517 
2518 tree
lookup_conversions(tree type)2519 lookup_conversions (tree type)
2520 {
2521   tree convs;
2522 
2523   complete_type (type);
2524   if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2525     return NULL_TREE;
2526 
2527   lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2528 
2529   tree list = NULL_TREE;
2530 
2531   /* Flatten the list-of-lists */
2532   for (; convs; convs = TREE_CHAIN (convs))
2533     {
2534       tree probe, next;
2535 
2536       for (probe = TREE_VALUE (convs); probe; probe = next)
2537 	{
2538 	  next = TREE_CHAIN (probe);
2539 
2540 	  TREE_CHAIN (probe) = list;
2541 	  list = probe;
2542 	}
2543     }
2544 
2545   return list;
2546 }
2547 
2548 /* Returns the binfo of the first direct or indirect virtual base derived
2549    from BINFO, or NULL if binfo is not via virtual.  */
2550 
2551 tree
binfo_from_vbase(tree binfo)2552 binfo_from_vbase (tree binfo)
2553 {
2554   for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2555     {
2556       if (BINFO_VIRTUAL_P (binfo))
2557 	return binfo;
2558     }
2559   return NULL_TREE;
2560 }
2561 
2562 /* Returns the binfo of the first direct or indirect virtual base derived
2563    from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2564    via virtual.  */
2565 
2566 tree
binfo_via_virtual(tree binfo,tree limit)2567 binfo_via_virtual (tree binfo, tree limit)
2568 {
2569   if (limit && !CLASSTYPE_VBASECLASSES (limit))
2570     /* LIMIT has no virtual bases, so BINFO cannot be via one.  */
2571     return NULL_TREE;
2572 
2573   for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2574        binfo = BINFO_INHERITANCE_CHAIN (binfo))
2575     {
2576       if (BINFO_VIRTUAL_P (binfo))
2577 	return binfo;
2578     }
2579   return NULL_TREE;
2580 }
2581 
2582 /* BINFO is for a base class in some hierarchy.  Return true iff it is a
2583    direct base.  */
2584 
2585 bool
binfo_direct_p(tree binfo)2586 binfo_direct_p (tree binfo)
2587 {
2588   tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2589   if (BINFO_INHERITANCE_CHAIN (d_binfo))
2590     /* A second inheritance chain means indirect.  */
2591     return false;
2592   if (!BINFO_VIRTUAL_P (binfo))
2593     /* Non-virtual, so only one inheritance chain means direct.  */
2594     return true;
2595   /* A virtual base looks like a direct base, so we need to look through the
2596      direct bases to see if it's there.  */
2597   tree b_binfo;
2598   for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2599     if (b_binfo == binfo)
2600       return true;
2601   return false;
2602 }
2603 
2604 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2605    Find the equivalent binfo within whatever graph HERE is located.
2606    This is the inverse of original_binfo.  */
2607 
2608 tree
copied_binfo(tree binfo,tree here)2609 copied_binfo (tree binfo, tree here)
2610 {
2611   tree result = NULL_TREE;
2612 
2613   if (BINFO_VIRTUAL_P (binfo))
2614     {
2615       tree t;
2616 
2617       for (t = here; BINFO_INHERITANCE_CHAIN (t);
2618 	   t = BINFO_INHERITANCE_CHAIN (t))
2619 	continue;
2620 
2621       result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2622     }
2623   else if (BINFO_INHERITANCE_CHAIN (binfo))
2624     {
2625       tree cbinfo;
2626       tree base_binfo;
2627       int ix;
2628 
2629       cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2630       for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2631 	if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2632 	  {
2633 	    result = base_binfo;
2634 	    break;
2635 	  }
2636     }
2637   else
2638     {
2639       gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2640       result = here;
2641     }
2642 
2643   gcc_assert (result);
2644   return result;
2645 }
2646 
2647 tree
binfo_for_vbase(tree base,tree t)2648 binfo_for_vbase (tree base, tree t)
2649 {
2650   unsigned ix;
2651   tree binfo;
2652   vec<tree, va_gc> *vbases;
2653 
2654   for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2655        vec_safe_iterate (vbases, ix, &binfo); ix++)
2656     if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2657       return binfo;
2658   return NULL;
2659 }
2660 
2661 /* BINFO is some base binfo of HERE, within some other
2662    hierarchy. Return the equivalent binfo, but in the hierarchy
2663    dominated by HERE.  This is the inverse of copied_binfo.  If BINFO
2664    is not a base binfo of HERE, returns NULL_TREE.  */
2665 
2666 tree
original_binfo(tree binfo,tree here)2667 original_binfo (tree binfo, tree here)
2668 {
2669   tree result = NULL;
2670 
2671   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2672     result = here;
2673   else if (BINFO_VIRTUAL_P (binfo))
2674     result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2675 	      ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2676 	      : NULL_TREE);
2677   else if (BINFO_INHERITANCE_CHAIN (binfo))
2678     {
2679       tree base_binfos;
2680 
2681       base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2682       if (base_binfos)
2683 	{
2684 	  int ix;
2685 	  tree base_binfo;
2686 
2687 	  for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2688 	    if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2689 				   BINFO_TYPE (binfo)))
2690 	      {
2691 		result = base_binfo;
2692 		break;
2693 	      }
2694 	}
2695     }
2696 
2697   return result;
2698 }
2699 
2700 /* True iff TYPE has any dependent bases (and therefore we can't say
2701    definitively that another class is not a base of an instantiation of
2702    TYPE).  */
2703 
2704 bool
any_dependent_bases_p(tree type)2705 any_dependent_bases_p (tree type)
2706 {
2707   if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
2708     return false;
2709 
2710   /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2711      Return false because in this situation we aren't actually looking up names
2712      in the scope of the class, so it doesn't matter whether it has dependent
2713      bases.  */
2714   if (!TYPE_BINFO (type))
2715     return false;
2716 
2717   unsigned i;
2718   tree base_binfo;
2719   FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2720     if (BINFO_DEPENDENT_BASE_P (base_binfo))
2721       return true;
2722 
2723   return false;
2724 }
2725