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