c++: P0847R7 (deducing this) - prerequisite changes. [PR102609]
[official-gcc.git] / gcc / cp / search.cc
blobf7be4a1c9b2f840d1607658bf53f39bc1e2a10aa
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
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 #include "tree-inline.h"
35 static int is_subobject_of_p (tree, tree);
36 static tree dfs_lookup_base (tree, void *);
37 static tree dfs_dcast_hint_pre (tree, void *);
38 static tree dfs_dcast_hint_post (tree, void *);
39 static tree dfs_debug_mark (tree, void *);
40 static int check_hidden_convs (tree, int, int, tree, tree, tree);
41 static tree split_conversions (tree, tree, tree, tree);
42 static int lookup_conversions_r (tree, int, int, tree, tree, tree *);
43 static int look_for_overrides_r (tree, tree);
44 static tree lookup_field_r (tree, void *);
45 static tree dfs_accessible_post (tree, void *);
46 static tree dfs_walk_once_accessible (tree, bool,
47 tree (*pre_fn) (tree, void *),
48 tree (*post_fn) (tree, void *),
49 void *data);
50 static tree dfs_access_in_type (tree, void *);
51 static access_kind access_in_type (tree, tree);
52 static tree dfs_get_pure_virtuals (tree, void *);
55 /* Data for lookup_base and its workers. */
57 struct lookup_base_data_s
59 HOST_WIDE_INT offset; /* Offset we want, or -1 if any. */
60 tree t; /* type being searched. */
61 tree base; /* The base type we're looking for. */
62 tree binfo; /* Found binfo. */
63 bool via_virtual; /* Found via a virtual path. */
64 bool ambiguous; /* Found multiply ambiguous */
65 bool repeated_base; /* Whether there are repeated bases in the
66 hierarchy. */
67 bool want_any; /* Whether we want any matching binfo. */
70 /* Worker function for lookup_base. See if we've found the desired
71 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
73 static tree
74 dfs_lookup_base (tree binfo, void *data_)
76 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
78 if (data->offset != -1)
80 /* We're looking for the type at a particular offset. */
81 int comp = compare_tree_int (BINFO_OFFSET (binfo), data->offset);
82 if (comp > 0)
83 /* Don't bother looking into bases laid out later; even if they
84 do virtually inherit from the base we want, we can get there
85 by another path. */
86 return dfs_skip_bases;
87 else if (comp != 0
88 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
89 /* Right type, wrong offset. */
90 return dfs_skip_bases;
91 /* Fall through. */
94 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
96 if (!data->binfo)
98 data->binfo = binfo;
99 data->via_virtual
100 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
102 if (!data->repeated_base)
103 /* If there are no repeated bases, we can stop now. */
104 return binfo;
106 if (data->want_any && !data->via_virtual)
107 /* If this is a non-virtual base, then we can't do
108 better. */
109 return binfo;
111 return dfs_skip_bases;
113 else
115 gcc_assert (binfo != data->binfo);
117 /* We've found more than one matching binfo. */
118 if (!data->want_any)
120 /* This is immediately ambiguous. */
121 data->binfo = NULL_TREE;
122 data->ambiguous = true;
123 return error_mark_node;
126 /* Prefer one via a non-virtual path. */
127 if (!binfo_via_virtual (binfo, data->t))
129 data->binfo = binfo;
130 data->via_virtual = false;
131 return binfo;
134 /* There must be repeated bases, otherwise we'd have stopped
135 on the first base we found. */
136 return dfs_skip_bases;
140 return NULL_TREE;
143 /* This deals with bug PR17314.
145 DECL is a declaration and BINFO represents a class that has attempted (but
146 failed) to access DECL.
148 Examine the parent binfos of BINFO and determine whether any of them had
149 private access to DECL. If they did, return the parent binfo. This helps
150 in figuring out the correct error message to show (if the parents had
151 access, it's their fault for not giving sufficient access to BINFO).
153 If no parents had access, return NULL_TREE. */
155 tree
156 get_parent_with_private_access (tree decl, tree binfo)
158 /* Only BINFOs should come through here. */
159 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
161 tree base_binfo = NULL_TREE;
163 /* Iterate through immediate parent classes. */
164 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
166 /* This parent had private access. Therefore that's why BINFO can't
167 access DECL. */
168 if (access_in_type (BINFO_TYPE (base_binfo), decl) == ak_private)
169 return base_binfo;
172 /* None of the parents had access. Note: it's impossible for one of the
173 parents to have had public or protected access to DECL, since then
174 BINFO would have been able to access DECL too. */
175 return NULL_TREE;
178 /* Returns true if type BASE is accessible in T. (BASE is known to be
179 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
180 true, consider any special access of the current scope, or access
181 bestowed by friendship. */
183 bool
184 accessible_base_p (tree t, tree base, bool consider_local_p)
186 tree decl;
188 /* [class.access.base]
190 A base class is said to be accessible if an invented public
191 member of the base class is accessible.
193 If BASE is a non-proper base, this condition is trivially
194 true. */
195 if (same_type_p (t, base))
196 return true;
197 /* Rather than inventing a public member, we use the implicit
198 public typedef created in the scope of every class. */
199 decl = TYPE_FIELDS (base);
200 while (!DECL_SELF_REFERENCE_P (decl))
201 decl = DECL_CHAIN (decl);
202 while (ANON_AGGR_TYPE_P (t))
203 t = TYPE_CONTEXT (t);
204 return accessible_p (t, decl, consider_local_p);
207 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
208 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
209 non-NULL, fill with information about what kind of base we
210 discovered. If OFFSET is other than -1, only match at that offset.
212 If the base is inaccessible, or ambiguous, then error_mark_node is
213 returned. If the tf_error bit of COMPLAIN is not set, no error
214 is issued. */
216 tree
217 lookup_base (tree t, tree base, base_access access,
218 base_kind *kind_ptr, tsubst_flags_t complain,
219 HOST_WIDE_INT offset /* = -1 */)
221 tree binfo;
222 tree t_binfo;
223 base_kind bk;
225 /* "Nothing" is definitely not derived from Base. */
226 if (t == NULL_TREE)
228 if (kind_ptr)
229 *kind_ptr = bk_not_base;
230 return NULL_TREE;
233 if (t == error_mark_node || base == error_mark_node)
235 if (kind_ptr)
236 *kind_ptr = bk_not_base;
237 return error_mark_node;
239 gcc_assert (TYPE_P (base));
241 if (!TYPE_P (t))
243 t_binfo = t;
244 t = BINFO_TYPE (t);
246 else
248 t = complete_type (TYPE_MAIN_VARIANT (t));
249 if (dependent_type_p (t))
250 if (tree open = currently_open_class (t))
251 t = open;
252 t_binfo = TYPE_BINFO (t);
255 base = TYPE_MAIN_VARIANT (base);
257 /* If BASE is incomplete, it can't be a base of T--and instantiating it
258 might cause an error. */
259 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
261 struct lookup_base_data_s data;
263 data.t = t;
264 data.base = base;
265 data.binfo = NULL_TREE;
266 data.ambiguous = data.via_virtual = false;
267 data.repeated_base = (offset == -1) && CLASSTYPE_REPEATED_BASE_P (t);
268 data.want_any = access == ba_any;
269 data.offset = offset;
271 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
272 binfo = data.binfo;
274 if (!binfo)
275 bk = data.ambiguous ? bk_ambig : bk_not_base;
276 else if (binfo == t_binfo)
277 bk = bk_same_type;
278 else if (data.via_virtual)
279 bk = bk_via_virtual;
280 else
281 bk = bk_proper_base;
283 else
285 binfo = NULL_TREE;
286 bk = bk_not_base;
289 /* Check that the base is unambiguous and accessible. */
290 if (access != ba_any)
291 switch (bk)
293 case bk_not_base:
294 break;
296 case bk_ambig:
297 if (complain & tf_error)
298 error ("%qT is an ambiguous base of %qT", base, t);
299 binfo = error_mark_node;
300 break;
302 default:
303 if ((access & ba_check_bit)
304 /* If BASE is incomplete, then BASE and TYPE are probably
305 the same, in which case BASE is accessible. If they
306 are not the same, then TYPE is invalid. In that case,
307 there's no need to issue another error here, and
308 there's no implicit typedef to use in the code that
309 follows, so we skip the check. */
310 && COMPLETE_TYPE_P (base)
311 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
313 if (complain & tf_error)
314 error ("%qT is an inaccessible base of %qT", base, t);
315 binfo = error_mark_node;
316 bk = bk_inaccessible;
318 break;
321 if (kind_ptr)
322 *kind_ptr = bk;
324 return binfo;
327 /* Data for dcast_base_hint walker. */
329 struct dcast_data_s
331 tree subtype; /* The base type we're looking for. */
332 int virt_depth; /* Number of virtual bases encountered from most
333 derived. */
334 tree offset; /* Best hint offset discovered so far. */
335 bool repeated_base; /* Whether there are repeated bases in the
336 hierarchy. */
339 /* Worker for dcast_base_hint. Search for the base type being cast
340 from. */
342 static tree
343 dfs_dcast_hint_pre (tree binfo, void *data_)
345 struct dcast_data_s *data = (struct dcast_data_s *) data_;
347 if (BINFO_VIRTUAL_P (binfo))
348 data->virt_depth++;
350 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
352 if (data->virt_depth)
354 data->offset = ssize_int (-1);
355 return data->offset;
357 if (data->offset)
358 data->offset = ssize_int (-3);
359 else
360 data->offset = BINFO_OFFSET (binfo);
362 return data->repeated_base ? dfs_skip_bases : data->offset;
365 return NULL_TREE;
368 /* Worker for dcast_base_hint. Track the virtual depth. */
370 static tree
371 dfs_dcast_hint_post (tree binfo, void *data_)
373 struct dcast_data_s *data = (struct dcast_data_s *) data_;
375 if (BINFO_VIRTUAL_P (binfo))
376 data->virt_depth--;
378 return NULL_TREE;
381 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
382 started from is related to the required TARGET type, in order to optimize
383 the inheritance graph search. This information is independent of the
384 current context, and ignores private paths, hence get_base_distance is
385 inappropriate. Return a TREE specifying the base offset, BOFF.
386 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
387 and there are no public virtual SUBTYPE bases.
388 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
389 BOFF == -2, SUBTYPE is not a public base.
390 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
392 tree
393 dcast_base_hint (tree subtype, tree target)
395 struct dcast_data_s data;
397 data.subtype = subtype;
398 data.virt_depth = 0;
399 data.offset = NULL_TREE;
400 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
402 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
403 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
404 return data.offset ? data.offset : ssize_int (-2);
407 /* Search for a member with name NAME in a multiple inheritance
408 lattice specified by TYPE. If it does not exist, return NULL_TREE.
409 If the member is ambiguously referenced, return `error_mark_node'.
410 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
411 true, type declarations are preferred. */
413 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
414 NAMESPACE_DECL corresponding to the innermost non-block scope. */
416 tree
417 current_scope (void)
419 /* There are a number of cases we need to be aware of here:
420 current_class_type current_function_decl
421 global NULL NULL
422 fn-local NULL SET
423 class-local SET NULL
424 class->fn SET SET
425 fn->class SET SET
427 Those last two make life interesting. If we're in a function which is
428 itself inside a class, we need decls to go into the fn's decls (our
429 second case below). But if we're in a class and the class itself is
430 inside a function, we need decls to go into the decls for the class. To
431 achieve this last goal, we must see if, when both current_class_ptr and
432 current_function_decl are set, the class was declared inside that
433 function. If so, we know to put the decls into the class's scope. */
434 if (current_function_decl && current_class_type
435 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
436 && same_type_p (DECL_CONTEXT (current_function_decl),
437 current_class_type))
438 || (DECL_FRIEND_CONTEXT (current_function_decl)
439 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
440 current_class_type))))
441 return current_function_decl;
443 if (current_class_type)
444 return current_class_type;
446 if (current_function_decl)
447 return current_function_decl;
449 return current_namespace;
452 /* Returns nonzero if we are currently in a function scope. Note
453 that this function returns zero if we are within a local class, but
454 not within a member function body of the local class. */
457 at_function_scope_p (void)
459 tree cs = current_scope ();
460 /* Also check cfun to make sure that we're really compiling
461 this function (as opposed to having set current_function_decl
462 for access checking or some such). */
463 return (cs && TREE_CODE (cs) == FUNCTION_DECL
464 && cfun && cfun->decl == current_function_decl);
467 /* Returns true if the innermost active scope is a class scope. */
469 bool
470 at_class_scope_p (void)
472 tree cs = current_scope ();
473 return cs && TYPE_P (cs);
476 /* Returns true if the innermost active scope is a namespace scope. */
478 bool
479 at_namespace_scope_p (void)
481 tree cs = current_scope ();
482 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
485 /* Return the scope of DECL, as appropriate when doing name-lookup. */
487 tree
488 context_for_name_lookup (tree decl)
490 /* [class.union]
492 For the purposes of name lookup, after the anonymous union
493 definition, the members of the anonymous union are considered to
494 have been defined in the scope in which the anonymous union is
495 declared. */
496 tree context = DECL_CONTEXT (decl);
498 while (context && TYPE_P (context)
499 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
500 context = TYPE_CONTEXT (context);
501 if (!context)
502 context = global_namespace;
504 return context;
507 /* Like the above, but always return a type, because it's simpler for member
508 handling to refer to the anonymous aggr rather than a function. */
510 tree
511 type_context_for_name_lookup (tree decl)
513 tree context = DECL_P (decl) ? DECL_CONTEXT (decl) : decl;
514 gcc_checking_assert (CLASS_TYPE_P (context));
516 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
518 tree next = TYPE_CONTEXT (context);
519 if (!TYPE_P (next))
520 break;
521 context = next;
523 return context;
526 /* Returns true iff DECL is declared in TYPE. */
528 static bool
529 member_declared_in_type (tree decl, tree type)
531 /* A normal declaration obviously counts. */
532 if (context_for_name_lookup (decl) == type)
533 return true;
534 /* So does a using or access declaration. */
535 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
536 && purpose_member (type, DECL_ACCESS (decl)))
537 return true;
538 return false;
541 /* The accessibility routines use BINFO_ACCESS for scratch space
542 during the computation of the accessibility of some declaration. */
544 /* Avoid walking up past a declaration of the member. */
546 static tree
547 dfs_access_in_type_pre (tree binfo, void *data)
549 tree decl = (tree) data;
550 tree type = BINFO_TYPE (binfo);
551 if (member_declared_in_type (decl, type))
552 return dfs_skip_bases;
553 return NULL_TREE;
556 #define BINFO_ACCESS(NODE) \
557 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
559 /* Set the access associated with NODE to ACCESS. */
561 #define SET_BINFO_ACCESS(NODE, ACCESS) \
562 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
563 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
565 /* Called from access_in_type via dfs_walk. Calculate the access to
566 DATA (which is really a DECL) in BINFO. */
568 static tree
569 dfs_access_in_type (tree binfo, void *data)
571 tree decl = (tree) data;
572 tree type = BINFO_TYPE (binfo);
573 access_kind access = ak_none;
575 if (context_for_name_lookup (decl) == type)
577 /* If we have descended to the scope of DECL, just note the
578 appropriate access. */
579 if (TREE_PRIVATE (decl))
580 access = ak_private;
581 else if (TREE_PROTECTED (decl))
582 access = ak_protected;
583 else
584 access = ak_public;
586 else
588 /* First, check for an access-declaration that gives us more
589 access to the DECL. */
590 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
592 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
594 if (decl_access)
596 decl_access = TREE_VALUE (decl_access);
598 if (decl_access == access_public_node)
599 access = ak_public;
600 else if (decl_access == access_protected_node)
601 access = ak_protected;
602 else if (decl_access == access_private_node)
603 access = ak_private;
604 else
605 gcc_unreachable ();
609 if (!access)
611 int i;
612 tree base_binfo;
613 vec<tree, va_gc> *accesses;
615 /* Otherwise, scan our baseclasses, and pick the most favorable
616 access. */
617 accesses = BINFO_BASE_ACCESSES (binfo);
618 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
620 tree base_access = (*accesses)[i];
621 access_kind base_access_now = BINFO_ACCESS (base_binfo);
623 if (base_access_now == ak_none || base_access_now == ak_private)
624 /* If it was not accessible in the base, or only
625 accessible as a private member, we can't access it
626 all. */
627 base_access_now = ak_none;
628 else if (base_access == access_protected_node)
629 /* Public and protected members in the base become
630 protected here. */
631 base_access_now = ak_protected;
632 else if (base_access == access_private_node)
633 /* Public and protected members in the base become
634 private here. */
635 base_access_now = ak_private;
637 /* See if the new access, via this base, gives more
638 access than our previous best access. */
639 if (base_access_now != ak_none
640 && (access == ak_none || base_access_now < access))
642 access = base_access_now;
644 /* If the new access is public, we can't do better. */
645 if (access == ak_public)
646 break;
652 /* Note the access to DECL in TYPE. */
653 SET_BINFO_ACCESS (binfo, access);
655 return NULL_TREE;
658 /* Return the access to DECL in TYPE. */
660 static access_kind
661 access_in_type (tree type, tree decl)
663 tree binfo = TYPE_BINFO (type);
665 /* We must take into account
667 [class.paths]
669 If a name can be reached by several paths through a multiple
670 inheritance graph, the access is that of the path that gives
671 most access.
673 The algorithm we use is to make a post-order depth-first traversal
674 of the base-class hierarchy. As we come up the tree, we annotate
675 each node with the most lenient access. */
676 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
678 return BINFO_ACCESS (binfo);
681 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
682 of OTYPE in the context of DERIVED. */
684 static int
685 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
687 /* We're checking this clause from [class.access.base]
689 m as a member of N is protected, and the reference occurs in a
690 member or friend of class N, or in a member or friend of a
691 class P derived from N, where m as a member of P is public, private
692 or protected.
694 Here DERIVED is a possible P, DECL is m and TYPE is N. */
696 /* If DERIVED isn't derived from N, then it can't be a P. */
697 if (!DERIVED_FROM_P (type, derived))
698 return 0;
700 /* DECL_NONSTATIC_MEMBER_P won't work for USING_DECLs. */
701 decl = strip_using_decl (decl);
702 /* We don't expect or support dependent decls. */
703 gcc_assert (TREE_CODE (decl) != USING_DECL);
705 /* [class.protected]
707 When a friend or a member function of a derived class references
708 a protected non-static member of a base class, an access check
709 applies in addition to those described earlier in clause
710 _class.access_) Except when forming a pointer to member
711 (_expr.unary.op_), the access must be through a pointer to,
712 reference to, or object of the derived class itself (or any class
713 derived from that class) (_expr.ref_). If the access is to form
714 a pointer to member, the nested-name-specifier shall name the
715 derived class (or any class derived from that class). */
716 if (DECL_NONSTATIC_MEMBER_P (decl)
717 && !DERIVED_FROM_P (derived, otype))
718 return 0;
720 return 1;
723 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
724 to access DECL through TYPE. OTYPE is the type of the object. */
726 static int
727 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
729 /* We're checking this clause from [class.access.base]
731 m as a member of N is protected, and the reference occurs in a
732 member or friend of class N, or in a member or friend of a
733 class P derived from N, where m as a member of P is public, private
734 or protected.
736 Here DECL is m and TYPE is N. SCOPE is the current context,
737 and we check all its possible Ps. */
738 tree befriending_classes;
739 tree t;
741 if (!scope)
742 return 0;
744 if (is_global_friend (scope))
745 return 1;
747 /* Is SCOPE itself a suitable P? */
748 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
749 return 1;
751 if (DECL_DECLARES_FUNCTION_P (scope))
752 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
753 else if (TYPE_P (scope))
754 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
755 else
756 return 0;
758 for (t = befriending_classes; t; t = TREE_CHAIN (t))
759 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
760 return 1;
762 /* Nested classes have the same access as their enclosing types, as
763 per DR 45 (this is a change from C++98). */
764 if (TYPE_P (scope))
765 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
766 return 1;
768 if (DECL_DECLARES_FUNCTION_P (scope))
770 /* Perhaps this SCOPE is a member of a class which is a
771 friend. */
772 if (DECL_CLASS_SCOPE_P (scope)
773 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
774 return 1;
775 /* Perhaps SCOPE is a friend function defined inside a class from which
776 DECL is accessible. */
777 if (tree fctx = DECL_FRIEND_CONTEXT (scope))
778 if (friend_accessible_p (fctx, decl, type, otype))
779 return 1;
782 /* Maybe scope's template is a friend. */
783 if (tree tinfo = get_template_info (scope))
785 tree tmpl = TI_TEMPLATE (tinfo);
786 if (DECL_CLASS_TEMPLATE_P (tmpl))
787 tmpl = TREE_TYPE (tmpl);
788 else
789 tmpl = DECL_TEMPLATE_RESULT (tmpl);
790 if (tmpl != scope)
792 /* Increment processing_template_decl to make sure that
793 dependent_type_p works correctly. */
794 ++processing_template_decl;
795 int ret = friend_accessible_p (tmpl, decl, type, otype);
796 --processing_template_decl;
797 if (ret)
798 return 1;
802 /* If is_friend is true, we should have found a befriending class. */
803 gcc_checking_assert (!is_friend (type, scope));
805 return 0;
808 struct dfs_accessible_data
810 tree decl;
811 tree object_type;
814 /* Avoid walking up past a declaration of the member. */
816 static tree
817 dfs_accessible_pre (tree binfo, void *data)
819 dfs_accessible_data *d = (dfs_accessible_data *)data;
820 tree type = BINFO_TYPE (binfo);
821 if (member_declared_in_type (d->decl, type))
822 return dfs_skip_bases;
823 return NULL_TREE;
826 /* Called via dfs_walk_once_accessible from accessible_p */
828 static tree
829 dfs_accessible_post (tree binfo, void *data)
831 /* access_in_type already set BINFO_ACCESS for us. */
832 access_kind access = BINFO_ACCESS (binfo);
833 tree N = BINFO_TYPE (binfo);
834 dfs_accessible_data *d = (dfs_accessible_data *)data;
835 tree decl = d->decl;
836 tree scope = current_nonlambda_scope ();
838 /* A member m is accessible at the point R when named in class N if */
839 switch (access)
841 case ak_none:
842 return NULL_TREE;
844 case ak_public:
845 /* m as a member of N is public, or */
846 return binfo;
848 case ak_private:
850 /* m as a member of N is private, and R occurs in a member or friend of
851 class N, or */
852 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
853 && is_friend (N, scope))
854 return binfo;
855 return NULL_TREE;
858 case ak_protected:
860 /* m as a member of N is protected, and R occurs in a member or friend
861 of class N, or in a member or friend of a class P derived from N,
862 where m as a member of P is public, private, or protected */
863 if (friend_accessible_p (scope, decl, N, d->object_type))
864 return binfo;
865 return NULL_TREE;
868 default:
869 gcc_unreachable ();
873 /* Like accessible_p below, but within a template returns true iff DECL is
874 accessible in TYPE to all possible instantiations of the template. */
877 accessible_in_template_p (tree type, tree decl)
879 int save_ptd = processing_template_decl;
880 processing_template_decl = 0;
881 int val = accessible_p (type, decl, false);
882 processing_template_decl = save_ptd;
883 return val;
886 /* DECL is a declaration from a base class of TYPE, which was the
887 class used to name DECL. Return nonzero if, in the current
888 context, DECL is accessible. If TYPE is actually a BINFO node,
889 then we can tell in what context the access is occurring by looking
890 at the most derived class along the path indicated by BINFO. If
891 CONSIDER_LOCAL is true, do consider special access the current
892 scope or friendship thereof we might have. */
895 accessible_p (tree type, tree decl, bool consider_local_p)
897 tree binfo;
898 access_kind access;
900 /* If this declaration is in a block or namespace scope, there's no
901 access control. */
902 if (!TYPE_P (context_for_name_lookup (decl)))
903 return 1;
905 /* There is no need to perform access checks inside a thunk. */
906 if (current_function_decl && DECL_THUNK_P (current_function_decl))
907 return 1;
909 tree otype = NULL_TREE;
910 if (!TYPE_P (type))
912 /* When accessing a non-static member, the most derived type in the
913 binfo chain is the type of the object; remember that type for
914 protected_accessible_p. */
915 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
916 otype = BINFO_TYPE (b);
917 type = BINFO_TYPE (type);
919 else
920 otype = type;
922 /* Anonymous unions don't have their own access. */
923 if (ANON_AGGR_TYPE_P (type))
924 type = type_context_for_name_lookup (type);
926 /* [class.access.base]
928 A member m is accessible when named in class N if
930 --m as a member of N is public, or
932 --m as a member of N is private, and the reference occurs in a
933 member or friend of class N, or
935 --m as a member of N is protected, and the reference occurs in a
936 member or friend of class N, or in a member or friend of a
937 class P derived from N, where m as a member of P is public, private or
938 protected, or
940 --there exists a base class B of N that is accessible at the point
941 of reference, and m is accessible when named in class B.
943 We walk the base class hierarchy, checking these conditions. */
945 /* We walk using TYPE_BINFO (type) because access_in_type will set
946 BINFO_ACCESS on it and its bases. */
947 binfo = TYPE_BINFO (type);
949 /* Compute the accessibility of DECL in the class hierarchy
950 dominated by type. */
951 access = access_in_type (type, decl);
952 if (access == ak_public)
953 return 1;
955 /* If we aren't considering the point of reference, only the first bullet
956 applies. */
957 if (!consider_local_p)
958 return 0;
960 dfs_accessible_data d = { decl, otype };
962 /* Walk the hierarchy again, looking for a base class that allows
963 access. */
964 return dfs_walk_once_accessible (binfo, /*friends=*/true,
965 dfs_accessible_pre,
966 dfs_accessible_post, &d)
967 != NULL_TREE;
970 struct lookup_field_info {
971 /* The type in which we're looking. */
972 tree type;
973 /* The name of the field for which we're looking. */
974 tree name;
975 /* If non-NULL, the current result of the lookup. */
976 tree rval;
977 /* The path to RVAL. */
978 tree rval_binfo;
979 /* If non-NULL, the lookup was ambiguous, and this is a list of the
980 candidates. */
981 tree ambiguous;
982 /* If nonzero, we are looking for types, not data members. */
983 int want_type;
986 /* True for a class member means that it is shared between all objects
987 of that class.
989 [class.member.lookup]:If the resulting set of declarations are not all
990 from sub-objects of the same type, or the set has a non-static member
991 and includes members from distinct sub-objects, there is an ambiguity
992 and the program is ill-formed.
994 This function checks that T contains no non-static members. */
996 bool
997 shared_member_p (tree t)
999 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL
1000 || TREE_CODE (t) == CONST_DECL)
1001 return true;
1002 if (is_overloaded_fn (t))
1004 for (ovl_iterator iter (get_fns (t)); iter; ++iter)
1006 tree decl = strip_using_decl (*iter);
1007 if (TREE_CODE (decl) == USING_DECL)
1008 /* Conservatively assume a dependent using-declaration
1009 might resolve to a non-static member. */
1010 return false;
1011 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl))
1012 return false;
1014 return true;
1016 return false;
1019 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1020 found as a base class and sub-object of the object denoted by
1021 BINFO. */
1023 static int
1024 is_subobject_of_p (tree parent, tree binfo)
1026 tree probe;
1028 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1030 if (probe == binfo)
1031 return 1;
1032 if (BINFO_VIRTUAL_P (probe))
1033 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1034 != NULL_TREE);
1036 return 0;
1039 /* DATA is really a struct lookup_field_info. Look for a field with
1040 the name indicated there in BINFO. If this function returns a
1041 non-NULL value it is the result of the lookup. Called from
1042 lookup_field via breadth_first_search. */
1044 static tree
1045 lookup_field_r (tree binfo, void *data)
1047 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1048 tree type = BINFO_TYPE (binfo);
1049 tree nval = NULL_TREE;
1051 /* If this is a dependent base, don't look in it. */
1052 if (BINFO_DEPENDENT_BASE_P (binfo))
1053 return NULL_TREE;
1055 /* If this base class is hidden by the best-known value so far, we
1056 don't need to look. */
1057 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1058 && !BINFO_VIRTUAL_P (binfo))
1059 return dfs_skip_bases;
1061 nval = get_class_binding (type, lfi->name, lfi->want_type);
1063 /* If there is no declaration with the indicated name in this type,
1064 then there's nothing to do. */
1065 if (!nval)
1066 goto done;
1068 /* If the lookup already found a match, and the new value doesn't
1069 hide the old one, we might have an ambiguity. */
1070 if (lfi->rval_binfo
1071 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1074 if (nval == lfi->rval && shared_member_p (nval))
1075 /* The two things are really the same. */
1077 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1078 /* The previous value hides the new one. */
1080 else
1082 /* We have a real ambiguity. We keep a chain of all the
1083 candidates. */
1084 if (!lfi->ambiguous && lfi->rval)
1086 /* This is the first time we noticed an ambiguity. Add
1087 what we previously thought was a reasonable candidate
1088 to the list. */
1089 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1090 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1093 /* Add the new value. */
1094 if (TREE_CODE (nval) == TREE_LIST)
1095 lfi->ambiguous = chainon (nval, lfi->ambiguous);
1096 else
1098 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1099 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1103 else
1105 if (TREE_CODE (nval) == TREE_LIST)
1107 lfi->ambiguous = chainon (nval, lfi->ambiguous);
1108 lfi->rval = TREE_VALUE (nval);
1110 else
1111 lfi->rval = nval;
1112 lfi->rval_binfo = binfo;
1115 done:
1116 /* Don't look for constructors or destructors in base classes. */
1117 if (IDENTIFIER_CDTOR_P (lfi->name))
1118 return dfs_skip_bases;
1119 return NULL_TREE;
1122 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1123 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1124 FUNCTIONS, and OPTYPE respectively. */
1126 tree
1127 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1129 tree baselink;
1131 gcc_assert (OVL_P (functions) || TREE_CODE (functions) == TEMPLATE_ID_EXPR);
1132 gcc_assert (!optype || TYPE_P (optype));
1133 gcc_assert (TREE_TYPE (functions));
1135 baselink = make_node (BASELINK);
1136 TREE_TYPE (baselink) = TREE_TYPE (functions);
1137 BASELINK_BINFO (baselink) = binfo;
1138 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1139 BASELINK_FUNCTIONS (baselink) = functions;
1140 BASELINK_OPTYPE (baselink) = optype;
1142 if (binfo == access_binfo
1143 && TYPE_BEING_DEFINED (BINFO_TYPE (access_binfo)))
1144 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
1146 return baselink;
1149 /* Look for a member named NAME in an inheritance lattice dominated by
1150 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1151 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1152 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1153 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1154 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1155 TREE_VALUEs are the list of ambiguous candidates.
1157 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1159 If nothing can be found return NULL_TREE and do not issue an error.
1161 If non-NULL, failure information is written back to AFI. */
1163 tree
1164 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1165 tsubst_flags_t complain, access_failure_info *afi /* = NULL */)
1167 tree rval, rval_binfo = NULL_TREE;
1168 tree type = NULL_TREE, basetype_path = NULL_TREE;
1169 struct lookup_field_info lfi;
1171 /* rval_binfo is the binfo associated with the found member, note,
1172 this can be set with useful information, even when rval is not
1173 set, because it must deal with ALL members, not just non-function
1174 members. It is used for ambiguity checking and the hidden
1175 checks. Whereas rval is only set if a proper (not hidden)
1176 non-function member is found. */
1178 if (name == error_mark_node
1179 || xbasetype == NULL_TREE
1180 || xbasetype == error_mark_node)
1181 return NULL_TREE;
1183 gcc_assert (identifier_p (name));
1185 if (TREE_CODE (xbasetype) == TREE_BINFO)
1187 type = BINFO_TYPE (xbasetype);
1188 basetype_path = xbasetype;
1190 else
1192 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1193 return NULL_TREE;
1194 type = xbasetype;
1195 xbasetype = NULL_TREE;
1198 type = complete_type (type);
1200 /* Make sure we're looking for a member of the current instantiation in the
1201 right partial specialization. */
1202 if (dependent_type_p (type))
1203 if (tree t = currently_open_class (type))
1204 type = t;
1206 if (!basetype_path)
1207 basetype_path = TYPE_BINFO (type);
1209 if (!basetype_path)
1210 return NULL_TREE;
1212 memset (&lfi, 0, sizeof (lfi));
1213 lfi.type = type;
1214 lfi.name = name;
1215 lfi.want_type = want_type;
1216 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1217 rval = lfi.rval;
1218 rval_binfo = lfi.rval_binfo;
1219 if (rval_binfo)
1220 type = BINFO_TYPE (rval_binfo);
1222 if (lfi.ambiguous)
1224 if (protect == 0)
1225 return NULL_TREE;
1226 else if (protect == 1)
1228 if (complain & tf_error)
1230 error ("request for member %qD is ambiguous", name);
1231 print_candidates (lfi.ambiguous);
1233 return error_mark_node;
1235 else if (protect == 2)
1236 return lfi.ambiguous;
1239 if (!rval)
1240 return NULL_TREE;
1242 /* [class.access]
1244 In the case of overloaded function names, access control is
1245 applied to the function selected by overloaded resolution.
1247 We cannot check here, even if RVAL is only a single non-static
1248 member function, since we do not know what the "this" pointer
1249 will be. For:
1251 class A { protected: void f(); };
1252 class B : public A {
1253 void g(A *p) {
1254 f(); // OK
1255 p->f(); // Not OK.
1259 only the first call to "f" is valid. However, if the function is
1260 static, we can check. */
1261 if (protect == 1 && !really_overloaded_fn (rval))
1263 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1264 decl = strip_using_decl (decl);
1265 /* A dependent USING_DECL will be checked after tsubsting. */
1266 if (TREE_CODE (decl) != USING_DECL
1267 && !DECL_IOBJ_MEMBER_FUNCTION_P (decl)
1268 && !perform_or_defer_access_check (basetype_path, decl, decl,
1269 complain, afi))
1270 return error_mark_node;
1273 if (is_overloaded_fn (rval)
1274 /* Don't use a BASELINK for class-scope deduction guides since
1275 they're not actually member functions. */
1276 && !dguide_name_p (name))
1277 rval = build_baselink (rval_binfo, basetype_path, rval,
1278 (IDENTIFIER_CONV_OP_P (name)
1279 ? TREE_TYPE (name): NULL_TREE));
1280 return rval;
1283 /* Helper class for lookup_member_fuzzy. */
1285 class lookup_field_fuzzy_info
1287 public:
1288 lookup_field_fuzzy_info (bool want_type_p) :
1289 m_want_type_p (want_type_p), m_candidates () {}
1291 void fuzzy_lookup_field (tree type);
1293 /* If true, we are looking for types, not data members. */
1294 bool m_want_type_p;
1295 /* The result: a vec of identifiers. */
1296 auto_vec<tree> m_candidates;
1299 /* Locate all fields within TYPE, append them to m_candidates. */
1301 void
1302 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1304 if (!CLASS_TYPE_P (type))
1305 return;
1307 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1309 if (m_want_type_p && !DECL_DECLARES_TYPE_P (field))
1310 continue;
1312 if (!DECL_NAME (field))
1313 continue;
1315 if (is_lambda_ignored_entity (field))
1316 continue;
1318 /* Ignore special identifiers with space at the end like cdtor or
1319 conversion op identifiers. */
1320 if (TREE_CODE (DECL_NAME (field)) == IDENTIFIER_NODE)
1321 if (unsigned int len = IDENTIFIER_LENGTH (DECL_NAME (field)))
1322 if (IDENTIFIER_POINTER (DECL_NAME (field))[len - 1] == ' ')
1323 continue;
1325 m_candidates.safe_push (DECL_NAME (field));
1330 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1331 DATA is really a lookup_field_fuzzy_info. Look for a field with
1332 the name indicated there in BINFO. Gathers pertinent identifiers into
1333 m_candidates. */
1335 static tree
1336 lookup_field_fuzzy_r (tree binfo, void *data)
1338 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1339 tree type = BINFO_TYPE (binfo);
1341 lffi->fuzzy_lookup_field (type);
1343 return NULL_TREE;
1346 /* Like lookup_member, but try to find the closest match for NAME,
1347 rather than an exact match, and return an identifier (or NULL_TREE).
1348 Do not complain. */
1350 tree
1351 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1353 tree type = NULL_TREE, basetype_path = NULL_TREE;
1354 class lookup_field_fuzzy_info lffi (want_type_p);
1356 /* rval_binfo is the binfo associated with the found member, note,
1357 this can be set with useful information, even when rval is not
1358 set, because it must deal with ALL members, not just non-function
1359 members. It is used for ambiguity checking and the hidden
1360 checks. Whereas rval is only set if a proper (not hidden)
1361 non-function member is found. */
1363 if (name == error_mark_node
1364 || xbasetype == NULL_TREE
1365 || xbasetype == error_mark_node)
1366 return NULL_TREE;
1368 gcc_assert (identifier_p (name));
1370 if (TREE_CODE (xbasetype) == TREE_BINFO)
1372 type = BINFO_TYPE (xbasetype);
1373 basetype_path = xbasetype;
1375 else
1377 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1378 return NULL_TREE;
1379 type = xbasetype;
1380 xbasetype = NULL_TREE;
1383 type = complete_type (type);
1385 /* Make sure we're looking for a member of the current instantiation in the
1386 right partial specialization. */
1387 if (flag_concepts && dependent_type_p (type))
1388 type = currently_open_class (type);
1390 if (!basetype_path)
1391 basetype_path = TYPE_BINFO (type);
1393 if (!basetype_path)
1394 return NULL_TREE;
1396 /* Populate lffi.m_candidates. */
1397 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1399 return find_closest_identifier (name, &lffi.m_candidates);
1402 /* Like lookup_member, except that if we find a function member we
1403 return NULL_TREE. */
1405 tree
1406 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1408 tree rval = lookup_member (xbasetype, name, protect, want_type,
1409 tf_warning_or_error);
1411 /* Ignore functions, but propagate the ambiguity list. */
1412 if (!error_operand_p (rval)
1413 && (rval && BASELINK_P (rval)))
1414 return NULL_TREE;
1416 return rval;
1419 /* Like lookup_member, except that if we find a non-function member we
1420 return NULL_TREE. */
1422 tree
1423 lookup_fnfields (tree xbasetype, tree name, int protect,
1424 tsubst_flags_t complain)
1426 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1427 complain);
1429 /* Ignore non-functions, but propagate the ambiguity list. */
1430 if (!error_operand_p (rval)
1431 && (rval && !BASELINK_P (rval)))
1432 return NULL_TREE;
1434 return rval;
1437 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1438 the class or namespace used to qualify the name. CONTEXT_CLASS is
1439 the class corresponding to the object in which DECL will be used.
1440 Return a possibly modified version of DECL that takes into account
1441 the CONTEXT_CLASS.
1443 In particular, consider an expression like `B::m' in the context of
1444 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1445 then the most derived class indicated by the BASELINK_BINFO will be
1446 `B', not `D'. This function makes that adjustment. */
1448 tree
1449 adjust_result_of_qualified_name_lookup (tree decl,
1450 tree qualifying_scope,
1451 tree context_class)
1453 if (context_class && context_class != error_mark_node
1454 && CLASS_TYPE_P (context_class)
1455 && CLASS_TYPE_P (qualifying_scope)
1456 && DERIVED_FROM_P (qualifying_scope, context_class)
1457 && BASELINK_P (decl))
1459 tree base;
1461 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1462 Because we do not yet know which function will be chosen by
1463 overload resolution, we cannot yet check either accessibility
1464 or ambiguity -- in either case, the choice of a static member
1465 function might make the usage valid. */
1466 base = lookup_base (context_class, qualifying_scope,
1467 ba_unique, NULL, tf_none);
1468 if (base && base != error_mark_node)
1470 BASELINK_ACCESS_BINFO (decl) = base;
1471 tree decl_binfo
1472 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1473 ba_unique, NULL, tf_none);
1474 if (decl_binfo && decl_binfo != error_mark_node)
1475 BASELINK_BINFO (decl) = decl_binfo;
1479 if (BASELINK_P (decl))
1480 BASELINK_QUALIFIED_P (decl) = true;
1482 return decl;
1486 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1487 PRE_FN is called in preorder, while POST_FN is called in postorder.
1488 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1489 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1490 that value is immediately returned and the walk is terminated. One
1491 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1492 POST_FN are passed the binfo to examine and the caller's DATA
1493 value. All paths are walked, thus virtual and morally virtual
1494 binfos can be multiply walked. */
1496 tree
1497 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1498 tree (*post_fn) (tree, void *), void *data)
1500 tree rval;
1501 unsigned ix;
1502 tree base_binfo;
1504 /* Call the pre-order walking function. */
1505 if (pre_fn)
1507 rval = pre_fn (binfo, data);
1508 if (rval)
1510 if (rval == dfs_skip_bases)
1511 goto skip_bases;
1512 return rval;
1516 /* Find the next child binfo to walk. */
1517 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1519 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1520 if (rval)
1521 return rval;
1524 skip_bases:
1525 /* Call the post-order walking function. */
1526 if (post_fn)
1528 rval = post_fn (binfo, data);
1529 gcc_assert (rval != dfs_skip_bases);
1530 return rval;
1533 return NULL_TREE;
1536 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1537 that binfos are walked at most once. */
1539 static tree
1540 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1541 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1542 void *data)
1544 tree rval;
1545 unsigned ix;
1546 tree base_binfo;
1548 /* Call the pre-order walking function. */
1549 if (pre_fn)
1551 rval = pre_fn (binfo, data);
1552 if (rval)
1554 if (rval == dfs_skip_bases)
1555 goto skip_bases;
1557 return rval;
1561 /* Find the next child binfo to walk. */
1562 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1564 if (BINFO_VIRTUAL_P (base_binfo))
1565 if (pset->add (base_binfo))
1566 continue;
1568 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1569 if (rval)
1570 return rval;
1573 skip_bases:
1574 /* Call the post-order walking function. */
1575 if (post_fn)
1577 rval = post_fn (binfo, data);
1578 gcc_assert (rval != dfs_skip_bases);
1579 return rval;
1582 return NULL_TREE;
1585 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1586 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1587 For diamond shaped hierarchies we must mark the virtual bases, to
1588 avoid multiple walks. */
1590 tree
1591 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1592 tree (*post_fn) (tree, void *), void *data)
1594 static int active = 0; /* We must not be called recursively. */
1595 tree rval;
1597 gcc_assert (pre_fn || post_fn);
1598 gcc_assert (!active);
1599 active++;
1601 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1602 /* We are not diamond shaped, and therefore cannot encounter the
1603 same binfo twice. */
1604 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1605 else
1607 hash_set<tree> pset;
1608 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1611 active--;
1613 return rval;
1616 /* Worker function for dfs_walk_once_accessible. Behaves like
1617 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1618 access given by the current context should be considered, (b) ONCE
1619 indicates whether bases should be marked during traversal. */
1621 static tree
1622 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1623 tree (*pre_fn) (tree, void *),
1624 tree (*post_fn) (tree, void *), void *data)
1626 tree rval = NULL_TREE;
1627 unsigned ix;
1628 tree base_binfo;
1630 /* Call the pre-order walking function. */
1631 if (pre_fn)
1633 rval = pre_fn (binfo, data);
1634 if (rval)
1636 if (rval == dfs_skip_bases)
1637 goto skip_bases;
1639 return rval;
1643 /* Find the next child binfo to walk. */
1644 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1646 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1648 if (mark && pset->contains (base_binfo))
1649 continue;
1651 /* If the base is inherited via private or protected
1652 inheritance, then we can't see it, unless we are a friend of
1653 the current binfo. */
1654 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1656 tree scope;
1657 if (!friends_p)
1658 continue;
1659 scope = current_scope ();
1660 if (!scope
1661 || TREE_CODE (scope) == NAMESPACE_DECL
1662 || !is_friend (BINFO_TYPE (binfo), scope))
1663 continue;
1666 if (mark)
1667 pset->add (base_binfo);
1669 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1670 pre_fn, post_fn, data);
1671 if (rval)
1672 return rval;
1675 skip_bases:
1676 /* Call the post-order walking function. */
1677 if (post_fn)
1679 rval = post_fn (binfo, data);
1680 gcc_assert (rval != dfs_skip_bases);
1681 return rval;
1684 return NULL_TREE;
1687 /* Like dfs_walk_once except that only accessible bases are walked.
1688 FRIENDS_P indicates whether friendship of the local context
1689 should be considered when determining accessibility. */
1691 static tree
1692 dfs_walk_once_accessible (tree binfo, bool friends_p,
1693 tree (*pre_fn) (tree, void *),
1694 tree (*post_fn) (tree, void *), void *data)
1696 hash_set<tree> *pset = NULL;
1697 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1698 pset = new hash_set<tree>;
1699 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1700 pre_fn, post_fn, data);
1702 if (pset)
1703 delete pset;
1704 return rval;
1707 /* Return true iff the code of T is CODE, and it has compatible
1708 type with TYPE. */
1710 static bool
1711 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1713 if (TREE_CODE (t) != code)
1714 return false;
1715 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1716 return false;
1717 return true;
1720 /* Subroutine of direct_accessor_p and reference_accessor_p.
1721 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1722 We expect a tree of the form:
1723 <component_ref:
1724 <indirect_ref:S>
1725 <nop_expr:P*
1726 <parm_decl (this)>
1727 <field_decl (FIELD_DECL)>>>. */
1729 static bool
1730 field_access_p (tree component_ref, tree field_decl, tree field_type)
1732 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
1733 return false;
1735 tree indirect_ref = TREE_OPERAND (component_ref, 0);
1736 if (!INDIRECT_REF_P (indirect_ref))
1737 return false;
1739 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
1740 /* ??? is_object_parameter? */
1741 if (!is_this_parameter (ptr))
1742 return false;
1744 /* Must access the correct field. */
1745 if (TREE_OPERAND (component_ref, 1) != field_decl)
1746 return false;
1747 return true;
1750 /* Subroutine of field_accessor_p.
1752 Assuming that INIT_EXPR has already had its code and type checked,
1753 determine if it is a simple accessor for FIELD_DECL
1754 (of type FIELD_TYPE).
1756 Specifically, a simple accessor within struct S of the form:
1757 T get_field () { return m_field; }
1758 should have a constexpr_fn_retval (saved_tree) of the form:
1759 <init_expr:T
1760 <result_decl:T
1761 <nop_expr:T
1762 <component_ref:
1763 <indirect_ref:S>
1764 <nop_expr:P*
1765 <parm_decl (this)>
1766 <field_decl (FIELD_DECL)>>>>>. */
1768 static bool
1769 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1771 tree result_decl = TREE_OPERAND (init_expr, 0);
1772 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1773 return false;
1775 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1776 if (!field_access_p (component_ref, field_decl, field_type))
1777 return false;
1779 return true;
1782 /* Subroutine of field_accessor_p.
1784 Assuming that INIT_EXPR has already had its code and type checked,
1785 determine if it is a "reference" accessor for FIELD_DECL
1786 (of type FIELD_REFERENCE_TYPE).
1788 Specifically, a simple accessor within struct S of the form:
1789 T& get_field () { return m_field; }
1790 should have a constexpr_fn_retval (saved_tree) of the form:
1791 <init_expr:T&
1792 <result_decl:T&
1793 <nop_expr: T&
1794 <addr_expr: T*
1795 <component_ref:T
1796 <indirect_ref:S
1797 <nop_expr
1798 <parm_decl (this)>>
1799 <field (FIELD_DECL)>>>>>>. */
1800 static bool
1801 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1802 tree field_reference_type)
1804 tree result_decl = TREE_OPERAND (init_expr, 0);
1805 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1806 return false;
1808 tree field_pointer_type = build_pointer_type (field_type);
1809 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1810 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1811 return false;
1813 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1815 if (!field_access_p (component_ref, field_decl, field_type))
1816 return false;
1818 return true;
1821 /* Return true if FN is an accessor method for FIELD_DECL.
1822 i.e. a method of the form { return FIELD; }, with no
1823 conversions.
1825 If CONST_P, then additionally require that FN be a const
1826 method. */
1828 static bool
1829 field_accessor_p (tree fn, tree field_decl, bool const_p)
1831 if (TREE_CODE (fn) != FUNCTION_DECL)
1832 return false;
1834 /* We don't yet support looking up static data, just fields. */
1835 if (TREE_CODE (field_decl) != FIELD_DECL)
1836 return false;
1838 tree fntype = TREE_TYPE (fn);
1839 if (TREE_CODE (fntype) != METHOD_TYPE)
1840 return false;
1842 /* If the field is accessed via a const "this" argument, verify
1843 that the "this" parameter is const. */
1844 if (const_p)
1846 tree this_class = class_of_this_parm (fntype);
1847 if (!TYPE_READONLY (this_class))
1848 return false;
1851 tree saved_tree = DECL_SAVED_TREE (fn);
1853 if (saved_tree == NULL_TREE)
1854 return false;
1856 /* Attempt to extract a single return value from the function,
1857 if it has one. */
1858 tree retval = constexpr_fn_retval (saved_tree);
1859 if (retval == NULL_TREE || retval == error_mark_node)
1860 return false;
1861 /* Require an INIT_EXPR. */
1862 if (TREE_CODE (retval) != INIT_EXPR)
1863 return false;
1864 tree init_expr = retval;
1866 /* Determine if this is a simple accessor within struct S of the form:
1867 T get_field () { return m_field; }. */
1868 tree field_type = TREE_TYPE (field_decl);
1869 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1870 return direct_accessor_p (init_expr, field_decl, field_type);
1872 /* Failing that, determine if it is an accessor of the form:
1873 T& get_field () { return m_field; }. */
1874 tree field_reference_type = cp_build_reference_type (field_type, false);
1875 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1876 return reference_accessor_p (init_expr, field_decl, field_type,
1877 field_reference_type);
1879 return false;
1882 /* Callback data for dfs_locate_field_accessor_pre. */
1884 class locate_field_data
1886 public:
1887 locate_field_data (tree field_decl_, bool const_p_)
1888 : field_decl (field_decl_), const_p (const_p_) {}
1890 tree field_decl;
1891 bool const_p;
1894 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1895 callable via binfo, if one exists, otherwise return NULL_TREE.
1897 Callback for dfs_walk_once_accessible for use within
1898 locate_field_accessor. */
1900 static tree
1901 dfs_locate_field_accessor_pre (tree binfo, void *data)
1903 locate_field_data *lfd = (locate_field_data *)data;
1904 tree type = BINFO_TYPE (binfo);
1906 vec<tree, va_gc> *member_vec;
1907 tree fn;
1908 size_t i;
1910 if (!CLASS_TYPE_P (type))
1911 return NULL_TREE;
1913 member_vec = CLASSTYPE_MEMBER_VEC (type);
1914 if (!member_vec)
1915 return NULL_TREE;
1917 for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1918 if (fn)
1919 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1920 return fn;
1922 return NULL_TREE;
1925 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1926 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1928 tree
1929 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1931 if (TREE_CODE (basetype_path) != TREE_BINFO)
1932 return NULL_TREE;
1934 /* Walk the hierarchy, looking for a method of some base class that allows
1935 access to the field. */
1936 locate_field_data lfd (field_decl, const_p);
1937 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1938 dfs_locate_field_accessor_pre,
1939 NULL, &lfd);
1942 /* Check throw specifier of OVERRIDER is at least as strict as
1943 the one of BASEFN. */
1945 bool
1946 maybe_check_overriding_exception_spec (tree overrider, tree basefn)
1948 maybe_instantiate_noexcept (basefn);
1949 maybe_instantiate_noexcept (overrider);
1950 tree base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1951 tree over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1953 if (DECL_INVALID_OVERRIDER_P (overrider))
1954 return true;
1956 /* Can't check this yet. Pretend this is fine and let
1957 noexcept_override_late_checks check this later. */
1958 if (UNPARSED_NOEXCEPT_SPEC_P (base_throw)
1959 || UNPARSED_NOEXCEPT_SPEC_P (over_throw))
1960 return true;
1962 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1964 auto_diagnostic_group d;
1965 error ("looser exception specification on overriding virtual function "
1966 "%q+#F", overrider);
1967 inform (DECL_SOURCE_LOCATION (basefn),
1968 "overridden function is %q#F", basefn);
1969 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1970 return false;
1972 return true;
1975 /* Check that virtual overrider OVERRIDER is acceptable for base function
1976 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1978 static int
1979 check_final_overrider (tree overrider, tree basefn)
1981 tree over_type = TREE_TYPE (overrider);
1982 tree base_type = TREE_TYPE (basefn);
1983 tree over_return = fndecl_declared_return_type (overrider);
1984 tree base_return = fndecl_declared_return_type (basefn);
1986 int fail = 0;
1988 if (DECL_INVALID_OVERRIDER_P (overrider))
1989 return 0;
1991 if (same_type_p (base_return, over_return))
1992 /* OK */;
1993 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1994 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1995 && INDIRECT_TYPE_P (base_return)))
1997 /* Potentially covariant. */
1998 unsigned base_quals, over_quals;
2000 fail = !INDIRECT_TYPE_P (base_return);
2001 if (!fail)
2003 if (cp_type_quals (base_return) != cp_type_quals (over_return))
2004 fail = 1;
2006 if (TYPE_REF_P (base_return)
2007 && (TYPE_REF_IS_RVALUE (base_return)
2008 != TYPE_REF_IS_RVALUE (over_return)))
2009 fail = 1;
2011 base_return = TREE_TYPE (base_return);
2012 over_return = TREE_TYPE (over_return);
2014 base_quals = cp_type_quals (base_return);
2015 over_quals = cp_type_quals (over_return);
2017 if ((base_quals & over_quals) != over_quals)
2018 fail = 1;
2020 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
2022 /* Strictly speaking, the standard requires the return type to be
2023 complete even if it only differs in cv-quals, but that seems
2024 like a bug in the wording. */
2025 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
2026 over_return))
2028 tree binfo = lookup_base (over_return, base_return,
2029 ba_check, NULL, tf_none);
2031 if (!binfo || binfo == error_mark_node)
2032 fail = 1;
2035 else if (can_convert_standard (TREE_TYPE (base_type),
2036 TREE_TYPE (over_type),
2037 tf_warning_or_error))
2038 /* GNU extension, allow trivial pointer conversions such as
2039 converting to void *, or qualification conversion. */
2041 auto_diagnostic_group d;
2042 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
2043 "invalid covariant return type for %q#D", overrider))
2044 inform (DECL_SOURCE_LOCATION (basefn),
2045 "overridden function is %q#D", basefn);
2047 else
2048 fail = 2;
2050 else
2051 fail = 2;
2052 if (!fail)
2053 /* OK */;
2054 else
2056 auto_diagnostic_group d;
2057 if (fail == 1)
2058 error ("invalid covariant return type for %q+#D", overrider);
2059 else
2060 error ("conflicting return type specified for %q+#D", overrider);
2061 inform (DECL_SOURCE_LOCATION (basefn),
2062 "overridden function is %q#D", basefn);
2063 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2064 return 0;
2067 if (!maybe_check_overriding_exception_spec (overrider, basefn))
2068 return 0;
2070 /* Check for conflicting type attributes. But leave transaction_safe for
2071 set_one_vmethod_tm_attributes. */
2072 if (!comp_type_attributes (over_type, base_type)
2073 && !tx_safe_fn_type_p (base_type)
2074 && !tx_safe_fn_type_p (over_type))
2076 auto_diagnostic_group d;
2077 error ("conflicting type attributes specified for %q+#D", overrider);
2078 inform (DECL_SOURCE_LOCATION (basefn),
2079 "overridden function is %q#D", basefn);
2080 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2081 return 0;
2084 /* A consteval virtual function shall not override a virtual function that is
2085 not consteval. A consteval virtual function shall not be overridden by a
2086 virtual function that is not consteval. */
2087 if (DECL_IMMEDIATE_FUNCTION_P (overrider)
2088 != DECL_IMMEDIATE_FUNCTION_P (basefn))
2090 auto_diagnostic_group d;
2091 if (DECL_IMMEDIATE_FUNCTION_P (overrider))
2092 error ("%<consteval%> function %q+D overriding non-%<consteval%> "
2093 "function", overrider);
2094 else
2095 error ("non-%<consteval%> function %q+D overriding %<consteval%> "
2096 "function", overrider);
2097 inform (DECL_SOURCE_LOCATION (basefn),
2098 "overridden function is %qD", basefn);
2099 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2100 return 0;
2103 /* A function declared transaction_safe_dynamic that overrides a function
2104 declared transaction_safe (but not transaction_safe_dynamic) is
2105 ill-formed. */
2106 if (tx_safe_fn_type_p (base_type)
2107 && lookup_attribute ("transaction_safe_dynamic",
2108 DECL_ATTRIBUTES (overrider))
2109 && !lookup_attribute ("transaction_safe_dynamic",
2110 DECL_ATTRIBUTES (basefn)))
2112 auto_diagnostic_group d;
2113 error_at (DECL_SOURCE_LOCATION (overrider),
2114 "%qD declared %<transaction_safe_dynamic%>", overrider);
2115 inform (DECL_SOURCE_LOCATION (basefn),
2116 "overriding %qD declared %<transaction_safe%>", basefn);
2119 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2121 if (DECL_DELETED_FN (overrider))
2123 auto_diagnostic_group d;
2124 error ("deleted function %q+D overriding non-deleted function",
2125 overrider);
2126 inform (DECL_SOURCE_LOCATION (basefn),
2127 "overridden function is %qD", basefn);
2128 maybe_explain_implicit_delete (overrider);
2130 else
2132 auto_diagnostic_group d;
2133 error ("non-deleted function %q+D overriding deleted function",
2134 overrider);
2135 inform (DECL_SOURCE_LOCATION (basefn),
2136 "overridden function is %qD", basefn);
2138 return 0;
2141 if (!DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
2143 auto_diagnostic_group d;
2144 error ("function with contracts %q+D overriding contractless function",
2145 overrider);
2146 inform (DECL_SOURCE_LOCATION (basefn),
2147 "overridden function is %qD", basefn);
2148 return 0;
2150 else if (DECL_HAS_CONTRACTS_P (basefn) && !DECL_HAS_CONTRACTS_P (overrider))
2152 /* We're inheriting basefn's contracts; create a copy of them but
2153 replace references to their parms to our parms. */
2154 inherit_base_contracts (overrider, basefn);
2156 else if (DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
2158 /* We're in the process of completing the overrider's class, which means
2159 our conditions definitely are not parsed so simply chain on the
2160 basefn for later checking.
2162 Note that OVERRIDER's contracts will have been fully parsed at the
2163 point the deferred match is run. */
2164 defer_guarded_contract_match (overrider, basefn, DECL_CONTRACTS (basefn));
2167 if (DECL_FINAL_P (basefn))
2169 auto_diagnostic_group d;
2170 error ("virtual function %q+D overriding final function", overrider);
2171 inform (DECL_SOURCE_LOCATION (basefn),
2172 "overridden function is %qD", basefn);
2173 return 0;
2175 return 1;
2178 /* Given a class TYPE, and a function decl FNDECL, look for
2179 virtual functions in TYPE's hierarchy which FNDECL overrides.
2180 We do not look in TYPE itself, only its bases.
2182 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2183 find that it overrides anything.
2185 We check that every function which is overridden, is correctly
2186 overridden. */
2189 look_for_overrides (tree type, tree fndecl)
2191 tree binfo = TYPE_BINFO (type);
2192 tree base_binfo;
2193 int ix;
2194 int found = 0;
2196 /* A constructor for a class T does not override a function T
2197 in a base class. */
2198 if (DECL_CONSTRUCTOR_P (fndecl))
2199 return 0;
2201 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2203 tree basetype = BINFO_TYPE (base_binfo);
2205 if (TYPE_POLYMORPHIC_P (basetype))
2206 found += look_for_overrides_r (basetype, fndecl);
2208 return found;
2211 /* Look in TYPE for virtual functions with the same signature as
2212 FNDECL. */
2214 tree
2215 look_for_overrides_here (tree type, tree fndecl)
2217 tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2219 for (ovl_iterator iter (ovl); iter; ++iter)
2221 tree fn = *iter;
2223 if (!DECL_VIRTUAL_P (fn))
2224 /* Not a virtual. */;
2225 else if (DECL_CONTEXT (fn) != type)
2226 /* Introduced with a using declaration. */;
2227 else if (DECL_STATIC_FUNCTION_P (fndecl))
2229 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2230 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2231 if (compparms (TREE_CHAIN (btypes), dtypes))
2232 return fn;
2234 else if (same_signature_p (fndecl, fn))
2235 return fn;
2238 return NULL_TREE;
2241 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2242 TYPE itself and its bases. */
2244 static int
2245 look_for_overrides_r (tree type, tree fndecl)
2247 tree fn = look_for_overrides_here (type, fndecl);
2248 if (fn)
2250 if (DECL_STATIC_FUNCTION_P (fndecl))
2252 /* A static member function cannot match an inherited
2253 virtual member function. */
2254 auto_diagnostic_group d;
2255 error ("%q+#D cannot be declared", fndecl);
2256 error (" since %q+#D declared in base class", fn);
2258 else
2260 /* It's definitely virtual, even if not explicitly set. */
2261 DECL_VIRTUAL_P (fndecl) = 1;
2262 check_final_overrider (fndecl, fn);
2264 return 1;
2267 /* We failed to find one declared in this class. Look in its bases. */
2268 return look_for_overrides (type, fndecl);
2271 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2273 static tree
2274 dfs_get_pure_virtuals (tree binfo, void *data)
2276 tree type = (tree) data;
2278 /* We're not interested in primary base classes; the derived class
2279 of which they are a primary base will contain the information we
2280 need. */
2281 if (!BINFO_PRIMARY_P (binfo))
2283 tree virtuals;
2285 for (virtuals = BINFO_VIRTUALS (binfo);
2286 virtuals;
2287 virtuals = TREE_CHAIN (virtuals))
2288 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2289 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2292 return NULL_TREE;
2295 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2297 void
2298 get_pure_virtuals (tree type)
2300 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2301 is going to be overridden. */
2302 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2303 /* Now, run through all the bases which are not primary bases, and
2304 collect the pure virtual functions. We look at the vtable in
2305 each class to determine what pure virtual functions are present.
2306 (A primary base is not interesting because the derived class of
2307 which it is a primary base will contain vtable entries for the
2308 pure virtuals in the base class. */
2309 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2312 /* Debug info for C++ classes can get very large; try to avoid
2313 emitting it everywhere.
2315 Note that this optimization wins even when the target supports
2316 BINCL (if only slightly), and reduces the amount of work for the
2317 linker. */
2319 void
2320 maybe_suppress_debug_info (tree t)
2322 if (write_symbols == NO_DEBUG)
2323 return;
2325 /* We might have set this earlier in cp_finish_decl. */
2326 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2328 /* Always emit the information for each class every time. */
2329 if (flag_emit_class_debug_always)
2330 return;
2332 /* If we already know how we're handling this class, handle debug info
2333 the same way. */
2334 if (CLASSTYPE_INTERFACE_KNOWN (t))
2336 if (CLASSTYPE_INTERFACE_ONLY (t))
2337 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2338 /* else don't set it. */
2340 /* If the class has a vtable, write out the debug info along with
2341 the vtable. */
2342 else if (TYPE_CONTAINS_VPTR_P (t))
2343 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2345 /* Otherwise, just emit the debug info normally. */
2348 /* Note that we want debugging information for a base class of a class
2349 whose vtable is being emitted. Normally, this would happen because
2350 calling the constructor for a derived class implies calling the
2351 constructors for all bases, which involve initializing the
2352 appropriate vptr with the vtable for the base class; but in the
2353 presence of optimization, this initialization may be optimized
2354 away, so we tell finish_vtable_vardecl that we want the debugging
2355 information anyway. */
2357 static tree
2358 dfs_debug_mark (tree binfo, void * /*data*/)
2360 tree t = BINFO_TYPE (binfo);
2362 if (CLASSTYPE_DEBUG_REQUESTED (t))
2363 return dfs_skip_bases;
2365 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2367 return NULL_TREE;
2370 /* Write out the debugging information for TYPE, whose vtable is being
2371 emitted. Also walk through our bases and note that we want to
2372 write out information for them. This avoids the problem of not
2373 writing any debug info for intermediate basetypes whose
2374 constructors, and thus the references to their vtables, and thus
2375 the vtables themselves, were optimized away. */
2377 void
2378 note_debug_info_needed (tree type)
2380 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2382 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2383 rest_of_type_compilation (type, namespace_bindings_p ());
2386 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2389 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2390 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2391 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2392 bases have been encountered already in the tree walk. PARENT_CONVS
2393 is the list of lists of conversion functions that could hide CONV
2394 and OTHER_CONVS is the list of lists of conversion functions that
2395 could hide or be hidden by CONV, should virtualness be involved in
2396 the hierarchy. Merely checking the conversion op's name is not
2397 enough because two conversion operators to the same type can have
2398 different names. Return nonzero if we are visible. */
2400 static int
2401 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2402 tree to_type, tree parent_convs, tree other_convs)
2404 tree level, probe;
2406 /* See if we are hidden by a parent conversion. */
2407 for (level = parent_convs; level; level = TREE_CHAIN (level))
2408 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2409 if (same_type_p (to_type, TREE_TYPE (probe)))
2410 return 0;
2412 if (virtual_depth || virtualness)
2414 /* In a virtual hierarchy, we could be hidden, or could hide a
2415 conversion function on the other_convs list. */
2416 for (level = other_convs; level; level = TREE_CHAIN (level))
2418 int we_hide_them;
2419 int they_hide_us;
2420 tree *prev, other;
2422 if (!(virtual_depth || TREE_STATIC (level)))
2423 /* Neither is morally virtual, so cannot hide each other. */
2424 continue;
2426 if (!TREE_VALUE (level))
2427 /* They evaporated away already. */
2428 continue;
2430 they_hide_us = (virtual_depth
2431 && original_binfo (binfo, TREE_PURPOSE (level)));
2432 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2433 && original_binfo (TREE_PURPOSE (level), binfo));
2435 if (!(we_hide_them || they_hide_us))
2436 /* Neither is within the other, so no hiding can occur. */
2437 continue;
2439 for (prev = &TREE_VALUE (level), other = *prev; other;)
2441 if (same_type_p (to_type, TREE_TYPE (other)))
2443 if (they_hide_us)
2444 /* We are hidden. */
2445 return 0;
2447 if (we_hide_them)
2449 /* We hide the other one. */
2450 other = TREE_CHAIN (other);
2451 *prev = other;
2452 continue;
2455 prev = &TREE_CHAIN (other);
2456 other = *prev;
2460 return 1;
2463 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2464 of conversion functions, the first slot will be for the current
2465 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2466 of conversion functions from children of the current binfo,
2467 concatenated with conversions from elsewhere in the hierarchy --
2468 that list begins with OTHER_CONVS. Return a single list of lists
2469 containing only conversions from the current binfo and its
2470 children. */
2472 static tree
2473 split_conversions (tree my_convs, tree parent_convs,
2474 tree child_convs, tree other_convs)
2476 tree t;
2477 tree prev;
2479 /* Remove the original other_convs portion from child_convs. */
2480 for (prev = NULL, t = child_convs;
2481 t != other_convs; prev = t, t = TREE_CHAIN (t))
2482 continue;
2484 if (prev)
2485 TREE_CHAIN (prev) = NULL_TREE;
2486 else
2487 child_convs = NULL_TREE;
2489 /* Attach the child convs to any we had at this level. */
2490 if (my_convs)
2492 my_convs = parent_convs;
2493 TREE_CHAIN (my_convs) = child_convs;
2495 else
2496 my_convs = child_convs;
2498 return my_convs;
2501 /* Worker for lookup_conversions. Lookup conversion functions in
2502 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2503 morally virtual base, and VIRTUALNESS is nonzero, if we've
2504 encountered virtual bases already in the tree walk. PARENT_CONVS
2505 is a list of conversions within parent binfos. OTHER_CONVS are
2506 conversions found elsewhere in the tree. Return the conversions
2507 found within this portion of the graph in CONVS. Return nonzero if
2508 we encountered virtualness. We keep template and non-template
2509 conversions separate, to avoid unnecessary type comparisons.
2511 The located conversion functions are held in lists of lists. The
2512 TREE_VALUE of the outer list is the list of conversion functions
2513 found in a particular binfo. The TREE_PURPOSE of both the outer
2514 and inner lists is the binfo at which those conversions were
2515 found. TREE_STATIC is set for those lists within of morally
2516 virtual binfos. The TREE_VALUE of the inner list is the conversion
2517 function or overload itself. The TREE_TYPE of each inner list node
2518 is the converted-to type. */
2520 static int
2521 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2522 tree parent_convs, tree other_convs, tree *convs)
2524 int my_virtualness = 0;
2525 tree my_convs = NULL_TREE;
2526 tree child_convs = NULL_TREE;
2528 /* If we have no conversion operators, then don't look. */
2529 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2531 *convs = NULL_TREE;
2533 return 0;
2536 if (BINFO_VIRTUAL_P (binfo))
2537 virtual_depth++;
2539 /* First, locate the unhidden ones at this level. */
2540 if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2541 for (ovl_iterator iter (conv); iter; ++iter)
2543 tree fn = *iter;
2544 tree type = DECL_CONV_FN_TYPE (fn);
2546 if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2548 mark_used (fn);
2549 type = DECL_CONV_FN_TYPE (fn);
2552 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2553 type, parent_convs, other_convs))
2555 my_convs = tree_cons (binfo, fn, my_convs);
2556 TREE_TYPE (my_convs) = type;
2557 if (virtual_depth)
2559 TREE_STATIC (my_convs) = 1;
2560 my_virtualness = 1;
2565 if (my_convs)
2567 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2568 if (virtual_depth)
2569 TREE_STATIC (parent_convs) = 1;
2572 child_convs = other_convs;
2574 /* Now iterate over each base, looking for more conversions. */
2575 unsigned i;
2576 tree base_binfo;
2577 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2579 tree base_convs;
2580 unsigned base_virtualness;
2582 base_virtualness = lookup_conversions_r (base_binfo,
2583 virtual_depth, virtualness,
2584 parent_convs, child_convs,
2585 &base_convs);
2586 if (base_virtualness)
2587 my_virtualness = virtualness = 1;
2588 child_convs = chainon (base_convs, child_convs);
2591 *convs = split_conversions (my_convs, parent_convs,
2592 child_convs, other_convs);
2594 return my_virtualness;
2597 /* Return a TREE_LIST containing all the non-hidden user-defined
2598 conversion functions for TYPE (and its base-classes). The
2599 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2600 function. The TREE_PURPOSE is the BINFO from which the conversion
2601 functions in this node were selected. This function is effectively
2602 performing a set of member lookups as lookup_fnfield does, but
2603 using the type being converted to as the unique key, rather than the
2604 field name. */
2606 tree
2607 lookup_conversions (tree type)
2609 tree convs;
2611 complete_type (type);
2612 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2613 return NULL_TREE;
2615 lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2617 tree list = NULL_TREE;
2619 /* Flatten the list-of-lists */
2620 for (; convs; convs = TREE_CHAIN (convs))
2622 tree probe, next;
2624 for (probe = TREE_VALUE (convs); probe; probe = next)
2626 next = TREE_CHAIN (probe);
2628 TREE_CHAIN (probe) = list;
2629 list = probe;
2633 return list;
2636 /* Returns the binfo of the first direct or indirect virtual base derived
2637 from BINFO, or NULL if binfo is not via virtual. */
2639 tree
2640 binfo_from_vbase (tree binfo)
2642 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2644 if (BINFO_VIRTUAL_P (binfo))
2645 return binfo;
2647 return NULL_TREE;
2650 /* Returns the binfo of the first direct or indirect virtual base derived
2651 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2652 via virtual. */
2654 tree
2655 binfo_via_virtual (tree binfo, tree limit)
2657 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2658 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2659 return NULL_TREE;
2661 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2662 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2664 if (BINFO_VIRTUAL_P (binfo))
2665 return binfo;
2667 return NULL_TREE;
2670 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2671 direct base. */
2673 bool
2674 binfo_direct_p (tree binfo)
2676 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2677 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2678 /* A second inheritance chain means indirect. */
2679 return false;
2680 if (!BINFO_VIRTUAL_P (binfo))
2681 /* Non-virtual, so only one inheritance chain means direct. */
2682 return true;
2683 /* A virtual base looks like a direct base, so we need to look through the
2684 direct bases to see if it's there. */
2685 tree b_binfo;
2686 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2687 if (b_binfo == binfo)
2688 return true;
2689 return false;
2692 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2693 Find the equivalent binfo within whatever graph HERE is located.
2694 This is the inverse of original_binfo. */
2696 tree
2697 copied_binfo (tree binfo, tree here)
2699 tree result = NULL_TREE;
2701 if (BINFO_VIRTUAL_P (binfo))
2703 tree t;
2705 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2706 t = BINFO_INHERITANCE_CHAIN (t))
2707 continue;
2709 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2711 else if (BINFO_INHERITANCE_CHAIN (binfo))
2713 tree cbinfo;
2714 tree base_binfo;
2715 int ix;
2717 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2718 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2719 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2721 result = base_binfo;
2722 break;
2725 else
2727 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2728 result = here;
2731 gcc_assert (result);
2732 return result;
2735 tree
2736 binfo_for_vbase (tree base, tree t)
2738 unsigned ix;
2739 tree binfo;
2740 vec<tree, va_gc> *vbases;
2742 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2743 vec_safe_iterate (vbases, ix, &binfo); ix++)
2744 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2745 return binfo;
2746 return NULL;
2749 /* BINFO is some base binfo of HERE, within some other
2750 hierarchy. Return the equivalent binfo, but in the hierarchy
2751 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2752 is not a base binfo of HERE, returns NULL_TREE. */
2754 tree
2755 original_binfo (tree binfo, tree here)
2757 tree result = NULL;
2759 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2760 result = here;
2761 else if (BINFO_VIRTUAL_P (binfo))
2762 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2763 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2764 : NULL_TREE);
2765 else if (BINFO_INHERITANCE_CHAIN (binfo))
2767 tree base_binfos;
2769 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2770 if (base_binfos)
2772 int ix;
2773 tree base_binfo;
2775 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2776 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2777 BINFO_TYPE (binfo)))
2779 result = base_binfo;
2780 break;
2785 return result;
2788 /* True iff TYPE has any dependent bases (and therefore we can't say
2789 definitively that another class is not a base of an instantiation of
2790 TYPE). */
2792 bool
2793 any_dependent_bases_p (tree type)
2795 if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
2796 return false;
2798 /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2799 Return false because in this situation we aren't actually looking up names
2800 in the scope of the class, so it doesn't matter whether it has dependent
2801 bases. */
2802 if (!TYPE_BINFO (type))
2803 return false;
2805 unsigned i;
2806 tree base_binfo;
2807 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2808 if (BINFO_DEPENDENT_BASE_P (base_binfo))
2809 return true;
2811 return false;