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