Ada: Fix assertion failure with iterator in container aggregate
[official-gcc.git] / gcc / cp / search.cc
blobcea9f7c689ba5e4c5341d28fed7d5de4c8d8d982
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2025 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. */
68 bool require_virtual; /* Whether we require a virtual path. */
71 /* Worker function for lookup_base. See if we've found the desired
72 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
74 static tree
75 dfs_lookup_base (tree binfo, void *data_)
77 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
79 if (data->offset != -1)
81 /* We're looking for the type at a particular offset. */
82 int comp = compare_tree_int (BINFO_OFFSET (binfo), data->offset);
83 if (comp > 0)
84 /* Don't bother looking into bases laid out later; even if they
85 do virtually inherit from the base we want, we can get there
86 by another path. */
87 return dfs_skip_bases;
88 else if (comp != 0
89 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
90 /* Right type, wrong offset. */
91 return dfs_skip_bases;
92 /* Fall through. */
95 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
97 const bool via_virtual
98 = binfo_via_virtual (binfo, data->t) != NULL_TREE;
100 if (data->require_virtual && !via_virtual)
101 /* Skip this result if we require virtual inheritance
102 and this is not a virtual base. */
103 return dfs_skip_bases;
105 if (!data->binfo)
107 data->binfo = binfo;
108 data->via_virtual = via_virtual;
110 if (!data->repeated_base)
111 /* If there are no repeated bases, we can stop now. */
112 return binfo;
114 if (data->want_any && !data->via_virtual)
115 /* If this is a non-virtual base, then we can't do
116 better. */
117 return binfo;
119 return dfs_skip_bases;
121 else
123 gcc_assert (binfo != data->binfo);
125 /* We've found more than one matching binfo. */
126 if (!data->want_any)
128 /* This is immediately ambiguous. */
129 data->binfo = NULL_TREE;
130 data->ambiguous = true;
131 return error_mark_node;
134 /* Prefer one via a non-virtual path. */
135 if (!via_virtual)
137 data->binfo = binfo;
138 data->via_virtual = false;
139 return binfo;
142 /* There must be repeated bases, otherwise we'd have stopped
143 on the first base we found. */
144 return dfs_skip_bases;
148 return NULL_TREE;
151 /* This deals with bug PR17314.
153 DECL is a declaration and BINFO represents a class that has attempted (but
154 failed) to access DECL.
156 Examine the parent binfos of BINFO and determine whether any of them had
157 private access to DECL. If they did, return the parent binfo. This helps
158 in figuring out the correct error message to show (if the parents had
159 access, it's their fault for not giving sufficient access to BINFO).
161 If no parents had access, return NULL_TREE. */
163 tree
164 get_parent_with_private_access (tree decl, tree binfo)
166 /* Only BINFOs should come through here. */
167 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
169 tree base_binfo = NULL_TREE;
171 /* Iterate through immediate parent classes.
172 Note that the base list might contain WILDCARD_TYPE_P types, that
173 should be ignored here. */
174 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
176 tree base_binfo_type = BINFO_TYPE (base_binfo);
177 /* This parent had private access. Therefore that's why BINFO can't
178 access DECL. */
179 if (RECORD_OR_UNION_TYPE_P (base_binfo_type)
180 && access_in_type (base_binfo_type, decl) == ak_private)
181 return base_binfo;
184 /* None of the parents had access. Note: it's impossible for one of the
185 parents to have had public or protected access to DECL, since then
186 BINFO would have been able to access DECL too. */
187 return NULL_TREE;
190 /* Returns true if type BASE is accessible in T. (BASE is known to be
191 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
192 true, consider any special access of the current scope, or access
193 bestowed by friendship. */
195 bool
196 accessible_base_p (tree t, tree base, bool consider_local_p)
198 tree decl;
200 /* [class.access.base]
202 A base class is said to be accessible if an invented public
203 member of the base class is accessible.
205 If BASE is a non-proper base, this condition is trivially
206 true. */
207 if (same_type_p (t, base))
208 return true;
209 /* Rather than inventing a public member, we use the implicit
210 public typedef created in the scope of every class. */
211 decl = TYPE_FIELDS (base);
212 while (!DECL_SELF_REFERENCE_P (decl))
213 decl = DECL_CHAIN (decl);
214 while (ANON_AGGR_TYPE_P (t))
215 t = TYPE_CONTEXT (t);
216 return accessible_p (t, decl, consider_local_p);
219 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
220 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
221 non-NULL, fill with information about what kind of base we
222 discovered. If OFFSET is other than -1, only match at that offset.
224 If the base is inaccessible, or ambiguous, then error_mark_node is
225 returned. If the tf_error bit of COMPLAIN is not set, no error
226 is issued. */
228 tree
229 lookup_base (tree t, tree base, base_access access,
230 base_kind *kind_ptr, tsubst_flags_t complain,
231 HOST_WIDE_INT offset /* = -1 */)
233 tree binfo;
234 tree t_binfo;
235 base_kind bk;
237 /* "Nothing" is definitely not derived from Base. */
238 if (t == NULL_TREE)
240 if (kind_ptr)
241 *kind_ptr = bk_not_base;
242 return NULL_TREE;
245 if (t == error_mark_node || base == error_mark_node)
247 if (kind_ptr)
248 *kind_ptr = bk_not_base;
249 return error_mark_node;
251 gcc_assert (TYPE_P (base));
253 if (!TYPE_P (t))
255 t_binfo = t;
256 t = BINFO_TYPE (t);
258 else
260 t = complete_type (TYPE_MAIN_VARIANT (t));
261 if (dependent_type_p (t))
262 if (tree open = currently_open_class (t))
263 t = open;
264 t_binfo = TYPE_BINFO (t);
267 base = TYPE_MAIN_VARIANT (base);
269 /* If BASE is incomplete, it can't be a base of T--and instantiating it
270 might cause an error. */
271 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
273 struct lookup_base_data_s data;
275 data.t = t;
276 data.base = base;
277 data.binfo = NULL_TREE;
278 data.ambiguous = data.via_virtual = false;
279 data.repeated_base = (offset == -1) && CLASSTYPE_REPEATED_BASE_P (t);
280 data.want_any = access == ba_any;
281 data.offset = offset;
282 data.require_virtual = (access & ba_require_virtual);
284 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
285 binfo = data.binfo;
287 if (!binfo)
288 bk = data.ambiguous ? bk_ambig : bk_not_base;
289 else if (binfo == t_binfo)
290 bk = bk_same_type;
291 else if (data.via_virtual)
292 bk = bk_via_virtual;
293 else
294 bk = bk_proper_base;
296 else
298 binfo = NULL_TREE;
299 bk = bk_not_base;
302 /* Check that the base is unambiguous and accessible. */
303 if (access != ba_any)
304 switch (bk)
306 case bk_not_base:
307 break;
309 case bk_ambig:
310 if (complain & tf_error)
311 error ("%qT is an ambiguous base of %qT", base, t);
312 binfo = error_mark_node;
313 break;
315 default:
316 if ((access & ba_check_bit)
317 /* If BASE is incomplete, then BASE and TYPE are probably
318 the same, in which case BASE is accessible. If they
319 are not the same, then TYPE is invalid. In that case,
320 there's no need to issue another error here, and
321 there's no implicit typedef to use in the code that
322 follows, so we skip the check. */
323 && COMPLETE_TYPE_P (base)
324 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
326 if (complain & tf_error)
327 error ("%qT is an inaccessible base of %qT", base, t);
328 binfo = error_mark_node;
329 bk = bk_inaccessible;
331 break;
334 if (kind_ptr)
335 *kind_ptr = bk;
337 return binfo;
340 /* Data for dcast_base_hint walker. */
342 struct dcast_data_s
344 tree subtype; /* The base type we're looking for. */
345 int virt_depth; /* Number of virtual bases encountered from most
346 derived. */
347 tree offset; /* Best hint offset discovered so far. */
348 bool repeated_base; /* Whether there are repeated bases in the
349 hierarchy. */
352 /* Worker for dcast_base_hint. Search for the base type being cast
353 from. */
355 static tree
356 dfs_dcast_hint_pre (tree binfo, void *data_)
358 struct dcast_data_s *data = (struct dcast_data_s *) data_;
360 if (BINFO_VIRTUAL_P (binfo))
361 data->virt_depth++;
363 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
365 if (data->virt_depth)
367 data->offset = ssize_int (-1);
368 return data->offset;
370 if (data->offset)
371 data->offset = ssize_int (-3);
372 else
373 data->offset = BINFO_OFFSET (binfo);
375 return data->repeated_base ? dfs_skip_bases : data->offset;
378 return NULL_TREE;
381 /* Worker for dcast_base_hint. Track the virtual depth. */
383 static tree
384 dfs_dcast_hint_post (tree binfo, void *data_)
386 struct dcast_data_s *data = (struct dcast_data_s *) data_;
388 if (BINFO_VIRTUAL_P (binfo))
389 data->virt_depth--;
391 return NULL_TREE;
394 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
395 started from is related to the required TARGET type, in order to optimize
396 the inheritance graph search. This information is independent of the
397 current context, and ignores private paths, hence get_base_distance is
398 inappropriate. Return a TREE specifying the base offset, BOFF.
399 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
400 and there are no public virtual SUBTYPE bases.
401 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
402 BOFF == -2, SUBTYPE is not a public base.
403 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
405 tree
406 dcast_base_hint (tree subtype, tree target)
408 struct dcast_data_s data;
410 data.subtype = subtype;
411 data.virt_depth = 0;
412 data.offset = NULL_TREE;
413 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
415 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
416 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
417 return data.offset ? data.offset : ssize_int (-2);
420 /* Search for a member with name NAME in a multiple inheritance
421 lattice specified by TYPE. If it does not exist, return NULL_TREE.
422 If the member is ambiguously referenced, return `error_mark_node'.
423 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
424 true, type declarations are preferred. */
426 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
427 NAMESPACE_DECL corresponding to the innermost non-block scope. */
429 tree
430 current_scope (void)
432 /* There are a number of cases we need to be aware of here:
433 current_class_type current_function_decl
434 global NULL NULL
435 fn-local NULL SET
436 class-local SET NULL
437 class->fn SET SET
438 fn->class SET SET
440 Those last two make life interesting. If we're in a function which is
441 itself inside a class, we need decls to go into the fn's decls (our
442 second case below). But if we're in a class and the class itself is
443 inside a function, we need decls to go into the decls for the class. To
444 achieve this last goal, we must see if, when both current_class_ptr and
445 current_function_decl are set, the class was declared inside that
446 function. If so, we know to put the decls into the class's scope. */
447 if (current_function_decl && current_class_type
448 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
449 && same_type_p (DECL_CONTEXT (current_function_decl),
450 current_class_type))
451 || (DECL_FRIEND_CONTEXT (current_function_decl)
452 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
453 current_class_type))))
454 return current_function_decl;
456 if (current_class_type)
457 return current_class_type;
459 if (current_function_decl)
460 return current_function_decl;
462 return current_namespace;
465 /* Returns nonzero if we are currently in a function scope. Note
466 that this function returns zero if we are within a local class, but
467 not within a member function body of the local class. */
470 at_function_scope_p (void)
472 tree cs = current_scope ();
473 /* Also check cfun to make sure that we're really compiling
474 this function (as opposed to having set current_function_decl
475 for access checking or some such). */
476 return (cs && TREE_CODE (cs) == FUNCTION_DECL
477 && cfun && cfun->decl == current_function_decl);
480 /* Returns true if the innermost active scope is a class scope. */
482 bool
483 at_class_scope_p (void)
485 tree cs = current_scope ();
486 return cs && TYPE_P (cs);
489 /* Returns true if the innermost active scope is a namespace scope. */
491 bool
492 at_namespace_scope_p (void)
494 tree cs = current_scope ();
495 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
498 /* Return the scope of DECL, as appropriate when doing name-lookup. */
500 tree
501 context_for_name_lookup (tree decl)
503 /* [class.union]
505 For the purposes of name lookup, after the anonymous union
506 definition, the members of the anonymous union are considered to
507 have been defined in the scope in which the anonymous union is
508 declared. */
509 tree context = DECL_CONTEXT (decl);
511 while (context && TYPE_P (context)
512 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
513 context = TYPE_CONTEXT (context);
514 if (!context)
515 context = global_namespace;
517 return context;
520 /* Like the above, but always return a type, because it's simpler for member
521 handling to refer to the anonymous aggr rather than a function. */
523 tree
524 type_context_for_name_lookup (tree decl)
526 tree context = DECL_P (decl) ? DECL_CONTEXT (decl) : decl;
527 gcc_checking_assert (CLASS_TYPE_P (context));
529 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
531 tree next = TYPE_CONTEXT (context);
532 if (!TYPE_P (next))
533 break;
534 context = next;
536 return context;
539 /* Returns true iff DECL is declared in TYPE. */
541 static bool
542 member_declared_in_type (tree decl, tree type)
544 /* A normal declaration obviously counts. */
545 if (context_for_name_lookup (decl) == type)
546 return true;
547 /* So does a using or access declaration. */
548 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
549 && purpose_member (type, DECL_ACCESS (decl)))
550 return true;
551 return false;
554 /* The accessibility routines use BINFO_ACCESS for scratch space
555 during the computation of the accessibility of some declaration. */
557 /* Avoid walking up past a declaration of the member. */
559 static tree
560 dfs_access_in_type_pre (tree binfo, void *data)
562 tree decl = (tree) data;
563 tree type = BINFO_TYPE (binfo);
564 if (member_declared_in_type (decl, type))
565 return dfs_skip_bases;
566 return NULL_TREE;
569 #define BINFO_ACCESS(NODE) \
570 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
572 /* Set the access associated with NODE to ACCESS. */
574 #define SET_BINFO_ACCESS(NODE, ACCESS) \
575 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
576 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
578 /* Called from access_in_type via dfs_walk. Calculate the access to
579 DATA (which is really a DECL) in BINFO. */
581 static tree
582 dfs_access_in_type (tree binfo, void *data)
584 tree decl = (tree) data;
585 tree type = BINFO_TYPE (binfo);
586 access_kind access = ak_none;
588 if (context_for_name_lookup (decl) == type)
590 /* If we have descended to the scope of DECL, just note the
591 appropriate access. */
592 if (TREE_PRIVATE (decl))
593 access = ak_private;
594 else if (TREE_PROTECTED (decl))
595 access = ak_protected;
596 else
597 access = ak_public;
599 else
601 /* First, check for an access-declaration that gives us more
602 access to the DECL. */
603 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
605 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
607 if (decl_access)
609 decl_access = TREE_VALUE (decl_access);
611 if (decl_access == access_public_node)
612 access = ak_public;
613 else if (decl_access == access_protected_node)
614 access = ak_protected;
615 else if (decl_access == access_private_node)
616 access = ak_private;
617 else
618 gcc_unreachable ();
622 if (!access)
624 int i;
625 tree base_binfo;
626 vec<tree, va_gc> *accesses;
628 /* Otherwise, scan our baseclasses, and pick the most favorable
629 access. */
630 accesses = BINFO_BASE_ACCESSES (binfo);
631 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
633 tree base_access = (*accesses)[i];
634 access_kind base_access_now = BINFO_ACCESS (base_binfo);
636 if (base_access_now == ak_none || base_access_now == ak_private)
637 /* If it was not accessible in the base, or only
638 accessible as a private member, we can't access it
639 all. */
640 base_access_now = ak_none;
641 else if (base_access == access_protected_node)
642 /* Public and protected members in the base become
643 protected here. */
644 base_access_now = ak_protected;
645 else if (base_access == access_private_node)
646 /* Public and protected members in the base become
647 private here. */
648 base_access_now = ak_private;
650 /* See if the new access, via this base, gives more
651 access than our previous best access. */
652 if (base_access_now != ak_none
653 && (access == ak_none || base_access_now < access))
655 access = base_access_now;
657 /* If the new access is public, we can't do better. */
658 if (access == ak_public)
659 break;
665 /* Note the access to DECL in TYPE. */
666 SET_BINFO_ACCESS (binfo, access);
668 return NULL_TREE;
671 /* Return the access to DECL in TYPE. */
673 static access_kind
674 access_in_type (tree type, tree decl)
676 tree binfo = TYPE_BINFO (type);
678 /* We must take into account
680 [class.paths]
682 If a name can be reached by several paths through a multiple
683 inheritance graph, the access is that of the path that gives
684 most access.
686 The algorithm we use is to make a post-order depth-first traversal
687 of the base-class hierarchy. As we come up the tree, we annotate
688 each node with the most lenient access. */
689 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
691 return BINFO_ACCESS (binfo);
694 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
695 of OTYPE in the context of DERIVED. */
697 static int
698 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
700 /* We're checking this clause from [class.access.base]
702 m as a member of N is protected, and the reference occurs in a
703 member or friend of class N, or in a member or friend of a
704 class P derived from N, where m as a member of P is public, private
705 or protected.
707 Here DERIVED is a possible P, DECL is m and TYPE is N. */
709 /* If DERIVED isn't derived from N, then it can't be a P. */
710 if (!DERIVED_FROM_P (type, derived))
711 return 0;
713 /* DECL_NONSTATIC_MEMBER_P won't work for USING_DECLs. */
714 decl = strip_using_decl (decl);
715 /* We don't expect or support dependent decls. */
716 gcc_assert (TREE_CODE (decl) != USING_DECL);
718 /* [class.protected]
720 When a friend or a member function of a derived class references
721 a protected non-static member of a base class, an access check
722 applies in addition to those described earlier in clause
723 _class.access_) Except when forming a pointer to member
724 (_expr.unary.op_), the access must be through a pointer to,
725 reference to, or object of the derived class itself (or any class
726 derived from that class) (_expr.ref_). If the access is to form
727 a pointer to member, the nested-name-specifier shall name the
728 derived class (or any class derived from that class). */
729 if (DECL_NONSTATIC_MEMBER_P (decl)
730 && !DERIVED_FROM_P (derived, otype))
731 return 0;
733 return 1;
736 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
737 to access DECL through TYPE. OTYPE is the type of the object. */
739 static int
740 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
742 /* We're checking this clause from [class.access.base]
744 m as a member of N is protected, and the reference occurs in a
745 member or friend of class N, or in a member or friend of a
746 class P derived from N, where m as a member of P is public, private
747 or protected.
749 Here DECL is m and TYPE is N. SCOPE is the current context,
750 and we check all its possible Ps. */
751 tree befriending_classes;
752 tree t;
754 if (!scope)
755 return 0;
757 if (is_global_friend (scope))
758 return 1;
760 /* Is SCOPE itself a suitable P? */
761 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
762 return 1;
764 if (DECL_DECLARES_FUNCTION_P (scope))
765 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
766 else if (TYPE_P (scope))
767 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
768 else
769 return 0;
771 for (t = befriending_classes; t; t = TREE_CHAIN (t))
772 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
773 return 1;
775 /* Nested classes have the same access as their enclosing types, as
776 per DR 45 (this is a change from C++98). */
777 if (TYPE_P (scope))
778 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
779 return 1;
781 if (DECL_DECLARES_FUNCTION_P (scope))
783 /* Perhaps this SCOPE is a member of a class which is a
784 friend. */
785 if (DECL_CLASS_SCOPE_P (scope)
786 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
787 return 1;
788 /* Perhaps SCOPE is a friend function defined inside a class from which
789 DECL is accessible. */
790 if (tree fctx = DECL_FRIEND_CONTEXT (scope))
791 if (friend_accessible_p (fctx, decl, type, otype))
792 return 1;
795 /* Maybe scope's template is a friend. */
796 if (tree tinfo = get_template_info (scope))
798 tree tmpl = TI_TEMPLATE (tinfo);
799 if (DECL_CLASS_TEMPLATE_P (tmpl))
800 tmpl = TREE_TYPE (tmpl);
801 else
802 tmpl = DECL_TEMPLATE_RESULT (tmpl);
803 if (tmpl != scope)
805 /* Increment processing_template_decl to make sure that
806 dependent_type_p works correctly. */
807 ++processing_template_decl;
808 int ret = friend_accessible_p (tmpl, decl, type, otype);
809 --processing_template_decl;
810 if (ret)
811 return 1;
815 /* If is_friend is true, we should have found a befriending class. */
816 gcc_checking_assert (!is_friend (type, scope));
818 return 0;
821 struct dfs_accessible_data
823 tree decl;
824 tree object_type;
827 /* Avoid walking up past a declaration of the member. */
829 static tree
830 dfs_accessible_pre (tree binfo, void *data)
832 dfs_accessible_data *d = (dfs_accessible_data *)data;
833 tree type = BINFO_TYPE (binfo);
834 if (member_declared_in_type (d->decl, type))
835 return dfs_skip_bases;
836 return NULL_TREE;
839 /* Called via dfs_walk_once_accessible from accessible_p */
841 static tree
842 dfs_accessible_post (tree binfo, void *data)
844 /* access_in_type already set BINFO_ACCESS for us. */
845 access_kind access = BINFO_ACCESS (binfo);
846 tree N = BINFO_TYPE (binfo);
847 dfs_accessible_data *d = (dfs_accessible_data *)data;
848 tree decl = d->decl;
849 tree scope = current_nonlambda_scope ();
851 /* A member m is accessible at the point R when named in class N if */
852 switch (access)
854 case ak_none:
855 return NULL_TREE;
857 case ak_public:
858 /* m as a member of N is public, or */
859 return binfo;
861 case ak_private:
863 /* m as a member of N is private, and R occurs in a member or friend of
864 class N, or */
865 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
866 && is_friend (N, scope))
867 return binfo;
868 return NULL_TREE;
871 case ak_protected:
873 /* m as a member of N is protected, and R occurs in a member or friend
874 of class N, or in a member or friend of a class P derived from N,
875 where m as a member of P is public, private, or protected */
876 if (friend_accessible_p (scope, decl, N, d->object_type))
877 return binfo;
878 return NULL_TREE;
881 default:
882 gcc_unreachable ();
886 /* Like accessible_p below, but within a template returns true iff DECL is
887 accessible in TYPE to all possible instantiations of the template. */
890 accessible_in_template_p (tree type, tree decl)
892 int save_ptd = processing_template_decl;
893 processing_template_decl = 0;
894 int val = accessible_p (type, decl, false);
895 processing_template_decl = save_ptd;
896 return val;
899 /* DECL is a declaration from a base class of TYPE, which was the
900 class used to name DECL. Return nonzero if, in the current
901 context, DECL is accessible. If TYPE is actually a BINFO node,
902 then we can tell in what context the access is occurring by looking
903 at the most derived class along the path indicated by BINFO. If
904 CONSIDER_LOCAL is true, do consider special access the current
905 scope or friendship thereof we might have. */
908 accessible_p (tree type, tree decl, bool consider_local_p)
910 tree binfo;
911 access_kind access;
913 /* If this declaration is in a block or namespace scope, there's no
914 access control. */
915 if (!TYPE_P (context_for_name_lookup (decl)))
916 return 1;
918 /* There is no need to perform access checks inside a thunk. */
919 if (current_function_decl && DECL_THUNK_P (current_function_decl))
920 return 1;
922 tree otype = NULL_TREE;
923 if (!TYPE_P (type))
925 /* When accessing a non-static member, the most derived type in the
926 binfo chain is the type of the object; remember that type for
927 protected_accessible_p. */
928 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
929 otype = BINFO_TYPE (b);
930 type = BINFO_TYPE (type);
932 else
933 otype = type;
935 /* Anonymous unions don't have their own access. */
936 if (ANON_AGGR_TYPE_P (type))
937 type = type_context_for_name_lookup (type);
939 /* [class.access.base]
941 A member m is accessible when named in class N if
943 --m as a member of N is public, or
945 --m as a member of N is private, and the reference occurs in a
946 member or friend of class N, or
948 --m as a member of N is protected, and the reference occurs in a
949 member or friend of class N, or in a member or friend of a
950 class P derived from N, where m as a member of P is public, private or
951 protected, or
953 --there exists a base class B of N that is accessible at the point
954 of reference, and m is accessible when named in class B.
956 We walk the base class hierarchy, checking these conditions. */
958 /* We walk using TYPE_BINFO (type) because access_in_type will set
959 BINFO_ACCESS on it and its bases. */
960 binfo = TYPE_BINFO (type);
962 /* Compute the accessibility of DECL in the class hierarchy
963 dominated by type. */
964 access = access_in_type (type, decl);
965 if (access == ak_public)
966 return 1;
968 /* If we aren't considering the point of reference, only the first bullet
969 applies. */
970 if (!consider_local_p)
971 return 0;
973 dfs_accessible_data d = { decl, otype };
975 /* Walk the hierarchy again, looking for a base class that allows
976 access. */
977 return dfs_walk_once_accessible (binfo, /*friends=*/true,
978 dfs_accessible_pre,
979 dfs_accessible_post, &d)
980 != NULL_TREE;
983 struct lookup_field_info {
984 /* The type in which we're looking. */
985 tree type;
986 /* The name of the field for which we're looking. */
987 tree name;
988 /* If non-NULL, the current result of the lookup. */
989 tree rval;
990 /* The path to RVAL. */
991 tree rval_binfo;
992 /* If non-NULL, the lookup was ambiguous, and this is a list of the
993 candidates. */
994 tree ambiguous;
995 /* If nonzero, we are looking for types, not data members. */
996 int want_type;
999 /* True for a class member means that it is shared between all objects
1000 of that class.
1002 [class.member.lookup]:If the resulting set of declarations are not all
1003 from sub-objects of the same type, or the set has a non-static member
1004 and includes members from distinct sub-objects, there is an ambiguity
1005 and the program is ill-formed.
1007 This function checks that T contains no non-static members. */
1009 bool
1010 shared_member_p (tree t)
1012 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL
1013 || TREE_CODE (t) == CONST_DECL)
1014 return true;
1015 if (is_overloaded_fn (t))
1017 for (ovl_iterator iter (get_fns (t)); iter; ++iter)
1019 tree decl = strip_using_decl (*iter);
1020 if (TREE_CODE (decl) == USING_DECL)
1021 /* Conservatively assume a dependent using-declaration
1022 might resolve to a non-static member. */
1023 return false;
1024 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl))
1025 return false;
1027 return true;
1029 return false;
1032 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1033 found as a base class and sub-object of the object denoted by
1034 BINFO. */
1036 static int
1037 is_subobject_of_p (tree parent, tree binfo)
1039 tree probe;
1041 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1043 if (probe == binfo)
1044 return 1;
1045 if (BINFO_VIRTUAL_P (probe))
1046 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1047 != NULL_TREE);
1049 return 0;
1052 /* DATA is really a struct lookup_field_info. Look for a field with
1053 the name indicated there in BINFO. If this function returns a
1054 non-NULL value it is the result of the lookup. Called from
1055 lookup_field via breadth_first_search. */
1057 static tree
1058 lookup_field_r (tree binfo, void *data)
1060 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1061 tree type = BINFO_TYPE (binfo);
1062 tree nval = NULL_TREE;
1064 /* If this is a dependent base, don't look in it. */
1065 if (BINFO_DEPENDENT_BASE_P (binfo))
1066 return NULL_TREE;
1068 /* If this base class is hidden by the best-known value so far, we
1069 don't need to look. */
1070 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1071 && !BINFO_VIRTUAL_P (binfo))
1072 return dfs_skip_bases;
1074 nval = get_class_binding (type, lfi->name, lfi->want_type);
1076 /* If there is no declaration with the indicated name in this type,
1077 then there's nothing to do. */
1078 if (!nval)
1079 goto done;
1081 /* If the lookup already found a match, and the new value doesn't
1082 hide the old one, we might have an ambiguity. */
1083 if (lfi->rval_binfo
1084 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1087 if (nval == lfi->rval && shared_member_p (nval))
1088 /* The two things are really the same. */
1090 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1091 /* The previous value hides the new one. */
1093 else
1095 /* We have a real ambiguity. We keep a chain of all the
1096 candidates. */
1097 if (!lfi->ambiguous && lfi->rval)
1099 /* This is the first time we noticed an ambiguity. Add
1100 what we previously thought was a reasonable candidate
1101 to the list. */
1102 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1103 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1106 /* Add the new value. */
1107 if (TREE_CODE (nval) == TREE_LIST)
1108 lfi->ambiguous = chainon (nval, lfi->ambiguous);
1109 else
1111 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1112 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1116 else
1118 if (TREE_CODE (nval) == TREE_LIST)
1120 lfi->ambiguous = chainon (nval, lfi->ambiguous);
1121 lfi->rval = TREE_VALUE (nval);
1123 else
1124 lfi->rval = nval;
1125 lfi->rval_binfo = binfo;
1128 done:
1129 /* Don't look for constructors or destructors in base classes. */
1130 if (IDENTIFIER_CDTOR_P (lfi->name))
1131 return dfs_skip_bases;
1132 return NULL_TREE;
1135 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1136 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1137 FUNCTIONS, and OPTYPE respectively. */
1139 tree
1140 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1142 tree baselink;
1144 gcc_assert (OVL_P (functions) || TREE_CODE (functions) == TEMPLATE_ID_EXPR);
1145 gcc_assert (!optype || TYPE_P (optype));
1146 gcc_assert (TREE_TYPE (functions));
1148 baselink = make_node (BASELINK);
1149 TREE_TYPE (baselink) = TREE_TYPE (functions);
1150 BASELINK_BINFO (baselink) = binfo;
1151 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1152 BASELINK_FUNCTIONS (baselink) = functions;
1153 BASELINK_OPTYPE (baselink) = optype;
1155 if (binfo == access_binfo
1156 && TYPE_BEING_DEFINED (BINFO_TYPE (access_binfo)))
1157 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
1159 return baselink;
1162 /* Look for a member named NAME in an inheritance lattice dominated by
1163 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1164 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1165 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1166 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1167 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1168 TREE_VALUEs are the list of ambiguous candidates.
1170 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1172 If nothing can be found return NULL_TREE and do not issue an error.
1174 If non-NULL, failure information is written back to AFI. */
1176 tree
1177 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1178 tsubst_flags_t complain, access_failure_info *afi /* = NULL */)
1180 tree rval, rval_binfo = NULL_TREE;
1181 tree type = NULL_TREE, basetype_path = NULL_TREE;
1182 struct lookup_field_info lfi;
1184 /* rval_binfo is the binfo associated with the found member, note,
1185 this can be set with useful information, even when rval is not
1186 set, because it must deal with ALL members, not just non-function
1187 members. It is used for ambiguity checking and the hidden
1188 checks. Whereas rval is only set if a proper (not hidden)
1189 non-function member is found. */
1191 if (name == error_mark_node
1192 || xbasetype == NULL_TREE
1193 || xbasetype == error_mark_node)
1194 return NULL_TREE;
1196 gcc_assert (identifier_p (name));
1198 if (TREE_CODE (xbasetype) == TREE_BINFO)
1200 type = BINFO_TYPE (xbasetype);
1201 basetype_path = xbasetype;
1203 else
1205 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1206 return NULL_TREE;
1207 type = xbasetype;
1208 xbasetype = NULL_TREE;
1211 type = complete_type (type);
1213 /* Make sure we're looking for a member of the current instantiation in the
1214 right partial specialization. */
1215 if (dependent_type_p (type))
1216 if (tree t = currently_open_class (type))
1217 type = t;
1219 if (!basetype_path)
1220 basetype_path = TYPE_BINFO (type);
1222 if (!basetype_path)
1223 return NULL_TREE;
1225 memset (&lfi, 0, sizeof (lfi));
1226 lfi.type = type;
1227 lfi.name = name;
1228 lfi.want_type = want_type;
1229 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1230 rval = lfi.rval;
1231 rval_binfo = lfi.rval_binfo;
1232 if (rval_binfo)
1233 type = BINFO_TYPE (rval_binfo);
1235 if (lfi.ambiguous)
1237 if (protect == 0)
1238 return NULL_TREE;
1239 else if (protect == 1)
1241 if (complain & tf_error)
1243 auto_diagnostic_group d;
1244 error ("request for member %qD is ambiguous", name);
1245 print_candidates (lfi.ambiguous);
1247 return error_mark_node;
1249 else if (protect == 2)
1250 return lfi.ambiguous;
1253 if (!rval)
1254 return NULL_TREE;
1256 /* [class.access]
1258 In the case of overloaded function names, access control is
1259 applied to the function selected by overloaded resolution.
1261 We cannot check here, even if RVAL is only a single non-static
1262 member function, since we do not know what the "this" pointer
1263 will be. For:
1265 class A { protected: void f(); };
1266 class B : public A {
1267 void g(A *p) {
1268 f(); // OK
1269 p->f(); // Not OK.
1273 only the first call to "f" is valid. However, if the function is
1274 static, we can check. */
1275 if (protect == 1 && !really_overloaded_fn (rval))
1277 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1278 decl = strip_using_decl (decl);
1279 /* A dependent USING_DECL will be checked after tsubsting. */
1280 if (TREE_CODE (decl) != USING_DECL
1281 && !DECL_IOBJ_MEMBER_FUNCTION_P (decl)
1282 && !perform_or_defer_access_check (basetype_path, decl, decl,
1283 complain, afi))
1284 return error_mark_node;
1287 if (is_overloaded_fn (rval)
1288 /* Don't use a BASELINK for class-scope deduction guides since
1289 they're not actually member functions. */
1290 && !dguide_name_p (name))
1291 rval = build_baselink (rval_binfo, basetype_path, rval,
1292 (IDENTIFIER_CONV_OP_P (name)
1293 ? TREE_TYPE (name) : NULL_TREE));
1294 return rval;
1297 /* Helper class for lookup_member_fuzzy. */
1299 class lookup_field_fuzzy_info
1301 public:
1302 lookup_field_fuzzy_info (bool want_type_p) :
1303 m_want_type_p (want_type_p), m_candidates () {}
1305 void fuzzy_lookup_field (tree type);
1307 /* If true, we are looking for types, not data members. */
1308 bool m_want_type_p;
1309 /* The result: a vec of identifiers. */
1310 auto_vec<tree> m_candidates;
1313 /* Locate all fields within TYPE, append them to m_candidates. */
1315 void
1316 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1318 if (!CLASS_TYPE_P (type))
1319 return;
1321 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1323 if (m_want_type_p && !DECL_DECLARES_TYPE_P (field))
1324 continue;
1326 if (!DECL_NAME (field))
1327 continue;
1329 if (is_lambda_ignored_entity (field))
1330 continue;
1332 /* Ignore special identifiers with space at the end like cdtor or
1333 conversion op identifiers. */
1334 if (TREE_CODE (DECL_NAME (field)) == IDENTIFIER_NODE)
1335 if (unsigned int len = IDENTIFIER_LENGTH (DECL_NAME (field)))
1336 if (IDENTIFIER_POINTER (DECL_NAME (field))[len - 1] == ' ')
1337 continue;
1339 m_candidates.safe_push (DECL_NAME (field));
1344 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1345 DATA is really a lookup_field_fuzzy_info. Look for a field with
1346 the name indicated there in BINFO. Gathers pertinent identifiers into
1347 m_candidates. */
1349 static tree
1350 lookup_field_fuzzy_r (tree binfo, void *data)
1352 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1353 tree type = BINFO_TYPE (binfo);
1355 lffi->fuzzy_lookup_field (type);
1357 return NULL_TREE;
1360 /* Like lookup_member, but try to find the closest match for NAME,
1361 rather than an exact match, and return an identifier (or NULL_TREE).
1362 Do not complain. */
1364 tree
1365 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1367 tree type = NULL_TREE, basetype_path = NULL_TREE;
1368 class lookup_field_fuzzy_info lffi (want_type_p);
1370 /* rval_binfo is the binfo associated with the found member, note,
1371 this can be set with useful information, even when rval is not
1372 set, because it must deal with ALL members, not just non-function
1373 members. It is used for ambiguity checking and the hidden
1374 checks. Whereas rval is only set if a proper (not hidden)
1375 non-function member is found. */
1377 if (name == error_mark_node
1378 || xbasetype == NULL_TREE
1379 || xbasetype == error_mark_node)
1380 return NULL_TREE;
1382 gcc_assert (identifier_p (name));
1384 if (TREE_CODE (xbasetype) == TREE_BINFO)
1386 type = BINFO_TYPE (xbasetype);
1387 basetype_path = xbasetype;
1389 else
1391 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1392 return NULL_TREE;
1393 type = xbasetype;
1394 xbasetype = NULL_TREE;
1397 type = complete_type (type);
1399 /* Make sure we're looking for a member of the current instantiation in the
1400 right partial specialization. */
1401 if (flag_concepts && dependent_type_p (type))
1402 type = currently_open_class (type);
1404 if (!basetype_path)
1405 basetype_path = TYPE_BINFO (type);
1407 if (!basetype_path)
1408 return NULL_TREE;
1410 /* Populate lffi.m_candidates. */
1411 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1413 return find_closest_identifier (name, &lffi.m_candidates);
1416 /* Like lookup_member, except that if we find a function member we
1417 return NULL_TREE. */
1419 tree
1420 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1422 tree rval = lookup_member (xbasetype, name, protect, want_type,
1423 tf_warning_or_error);
1425 /* Ignore functions, but propagate the ambiguity list. */
1426 if (!error_operand_p (rval)
1427 && (rval && BASELINK_P (rval)))
1428 return NULL_TREE;
1430 return rval;
1433 /* Like lookup_member, except that if we find a non-function member we
1434 return NULL_TREE. */
1436 tree
1437 lookup_fnfields (tree xbasetype, tree name, int protect,
1438 tsubst_flags_t complain)
1440 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1441 complain);
1443 /* Ignore non-functions, but propagate the ambiguity list. */
1444 if (!error_operand_p (rval)
1445 && (rval && !BASELINK_P (rval)))
1446 return NULL_TREE;
1448 return rval;
1451 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1452 the class or namespace used to qualify the name. CONTEXT_CLASS is
1453 the class corresponding to the object in which DECL will be used.
1454 Return a possibly modified version of DECL that takes into account
1455 the CONTEXT_CLASS.
1457 In particular, consider an expression like `B::m' in the context of
1458 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1459 then the most derived class indicated by the BASELINK_BINFO will be
1460 `B', not `D'. This function makes that adjustment. */
1462 tree
1463 adjust_result_of_qualified_name_lookup (tree decl,
1464 tree qualifying_scope,
1465 tree context_class)
1467 if (context_class && context_class != error_mark_node
1468 && CLASS_TYPE_P (context_class)
1469 && CLASS_TYPE_P (qualifying_scope)
1470 && DERIVED_FROM_P (qualifying_scope, context_class)
1471 && BASELINK_P (decl))
1473 tree base;
1475 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1476 Because we do not yet know which function will be chosen by
1477 overload resolution, we cannot yet check either accessibility
1478 or ambiguity -- in either case, the choice of a static member
1479 function might make the usage valid. */
1480 base = lookup_base (context_class, qualifying_scope,
1481 ba_unique, NULL, tf_none);
1482 if (base && base != error_mark_node)
1484 BASELINK_ACCESS_BINFO (decl) = base;
1485 tree decl_binfo
1486 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1487 ba_unique, NULL, tf_none);
1488 if (decl_binfo && decl_binfo != error_mark_node)
1489 BASELINK_BINFO (decl) = decl_binfo;
1493 if (BASELINK_P (decl))
1494 BASELINK_QUALIFIED_P (decl) = true;
1496 return decl;
1500 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1501 PRE_FN is called in preorder, while POST_FN is called in postorder.
1502 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1503 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1504 that value is immediately returned and the walk is terminated. One
1505 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1506 POST_FN are passed the binfo to examine and the caller's DATA
1507 value. All paths are walked, thus virtual and morally virtual
1508 binfos can be multiply walked. */
1510 tree
1511 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1512 tree (*post_fn) (tree, void *), void *data)
1514 tree rval;
1515 unsigned ix;
1516 tree base_binfo;
1518 /* Call the pre-order walking function. */
1519 if (pre_fn)
1521 rval = pre_fn (binfo, data);
1522 if (rval)
1524 if (rval == dfs_skip_bases)
1525 goto skip_bases;
1526 return rval;
1530 /* Find the next child binfo to walk. */
1531 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1533 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1534 if (rval)
1535 return rval;
1538 skip_bases:
1539 /* Call the post-order walking function. */
1540 if (post_fn)
1542 rval = post_fn (binfo, data);
1543 gcc_assert (rval != dfs_skip_bases);
1544 return rval;
1547 return NULL_TREE;
1550 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1551 that binfos are walked at most once. */
1553 static tree
1554 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1555 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1556 void *data)
1558 tree rval;
1559 unsigned ix;
1560 tree base_binfo;
1562 /* Call the pre-order walking function. */
1563 if (pre_fn)
1565 rval = pre_fn (binfo, data);
1566 if (rval)
1568 if (rval == dfs_skip_bases)
1569 goto skip_bases;
1571 return rval;
1575 /* Find the next child binfo to walk. */
1576 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1578 if (BINFO_VIRTUAL_P (base_binfo))
1579 if (pset->add (base_binfo))
1580 continue;
1582 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1583 if (rval)
1584 return rval;
1587 skip_bases:
1588 /* Call the post-order walking function. */
1589 if (post_fn)
1591 rval = post_fn (binfo, data);
1592 gcc_assert (rval != dfs_skip_bases);
1593 return rval;
1596 return NULL_TREE;
1599 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1600 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1601 For diamond shaped hierarchies we must mark the virtual bases, to
1602 avoid multiple walks. */
1604 tree
1605 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1606 tree (*post_fn) (tree, void *), void *data)
1608 static int active = 0; /* We must not be called recursively. */
1609 tree rval;
1611 gcc_assert (pre_fn || post_fn);
1612 gcc_assert (!active);
1613 active++;
1615 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1616 /* We are not diamond shaped, and therefore cannot encounter the
1617 same binfo twice. */
1618 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1619 else
1621 hash_set<tree> pset;
1622 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1625 active--;
1627 return rval;
1630 /* Worker function for dfs_walk_once_accessible. Behaves like
1631 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1632 access given by the current context should be considered, (b) ONCE
1633 indicates whether bases should be marked during traversal. */
1635 static tree
1636 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1637 tree (*pre_fn) (tree, void *),
1638 tree (*post_fn) (tree, void *), void *data)
1640 tree rval = NULL_TREE;
1641 unsigned ix;
1642 tree base_binfo;
1644 /* Call the pre-order walking function. */
1645 if (pre_fn)
1647 rval = pre_fn (binfo, data);
1648 if (rval)
1650 if (rval == dfs_skip_bases)
1651 goto skip_bases;
1653 return rval;
1657 /* Find the next child binfo to walk. */
1658 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1660 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1662 if (mark && pset->contains (base_binfo))
1663 continue;
1665 /* If the base is inherited via private or protected
1666 inheritance, then we can't see it, unless we are a friend of
1667 the current binfo. */
1668 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1670 tree scope;
1671 if (!friends_p)
1672 continue;
1673 scope = current_scope ();
1674 if (!scope
1675 || TREE_CODE (scope) == NAMESPACE_DECL
1676 || !is_friend (BINFO_TYPE (binfo), scope))
1677 continue;
1680 if (mark)
1681 pset->add (base_binfo);
1683 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1684 pre_fn, post_fn, data);
1685 if (rval)
1686 return rval;
1689 skip_bases:
1690 /* Call the post-order walking function. */
1691 if (post_fn)
1693 rval = post_fn (binfo, data);
1694 gcc_assert (rval != dfs_skip_bases);
1695 return rval;
1698 return NULL_TREE;
1701 /* Like dfs_walk_once except that only accessible bases are walked.
1702 FRIENDS_P indicates whether friendship of the local context
1703 should be considered when determining accessibility. */
1705 static tree
1706 dfs_walk_once_accessible (tree binfo, bool friends_p,
1707 tree (*pre_fn) (tree, void *),
1708 tree (*post_fn) (tree, void *), void *data)
1710 hash_set<tree> *pset = NULL;
1711 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1712 pset = new hash_set<tree>;
1713 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1714 pre_fn, post_fn, data);
1716 if (pset)
1717 delete pset;
1718 return rval;
1721 /* Return true iff the code of T is CODE, and it has compatible
1722 type with TYPE. */
1724 static bool
1725 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1727 if (TREE_CODE (t) != code)
1728 return false;
1729 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1730 return false;
1731 return true;
1734 /* Subroutine of direct_accessor_p and reference_accessor_p.
1735 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1736 We expect a tree of the form:
1737 <component_ref:
1738 <indirect_ref:S>
1739 <nop_expr:P*
1740 <parm_decl (this)>
1741 <field_decl (FIELD_DECL)>>>. */
1743 static bool
1744 field_access_p (tree component_ref, tree field_decl, tree field_type)
1746 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
1747 return false;
1749 tree indirect_ref = TREE_OPERAND (component_ref, 0);
1750 if (!INDIRECT_REF_P (indirect_ref))
1751 return false;
1753 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
1754 if (!is_object_parameter (ptr))
1755 return false;
1757 /* Must access the correct field. */
1758 if (TREE_OPERAND (component_ref, 1) != field_decl)
1759 return false;
1760 return true;
1763 /* Subroutine of field_accessor_p.
1765 Assuming that INIT_EXPR has already had its code and type checked,
1766 determine if it is a simple accessor for FIELD_DECL
1767 (of type FIELD_TYPE).
1769 Specifically, a simple accessor within struct S of the form:
1770 T get_field () { return m_field; }
1771 should have a constexpr_fn_retval (saved_tree) of the form:
1772 <init_expr:T
1773 <result_decl:T
1774 <nop_expr:T
1775 <component_ref:
1776 <indirect_ref:S>
1777 <nop_expr:P*
1778 <parm_decl (this)>
1779 <field_decl (FIELD_DECL)>>>>>. */
1781 static bool
1782 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1784 tree result_decl = TREE_OPERAND (init_expr, 0);
1785 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1786 return false;
1788 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1789 if (!field_access_p (component_ref, field_decl, field_type))
1790 return false;
1792 return true;
1795 /* Subroutine of field_accessor_p.
1797 Assuming that INIT_EXPR has already had its code and type checked,
1798 determine if it is a "reference" accessor for FIELD_DECL
1799 (of type FIELD_REFERENCE_TYPE).
1801 Specifically, a simple accessor within struct S of the form:
1802 T& get_field () { return m_field; }
1803 should have a constexpr_fn_retval (saved_tree) of the form:
1804 <init_expr:T&
1805 <result_decl:T&
1806 <nop_expr: T&
1807 <addr_expr: T*
1808 <component_ref:T
1809 <indirect_ref:S
1810 <nop_expr
1811 <parm_decl (this)>>
1812 <field (FIELD_DECL)>>>>>>. */
1813 static bool
1814 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1815 tree field_reference_type)
1817 tree result_decl = TREE_OPERAND (init_expr, 0);
1818 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1819 return false;
1821 tree field_pointer_type = build_pointer_type (field_type);
1822 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1823 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1824 return false;
1826 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1828 if (!field_access_p (component_ref, field_decl, field_type))
1829 return false;
1831 return true;
1834 /* Return the class of the `this' or explicit object parameter of FN. */
1836 static tree
1837 class_of_object_parm (const_tree fn)
1839 tree fntype = TREE_TYPE (fn);
1840 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
1841 return non_reference (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
1842 return class_of_this_parm (fntype);
1845 /* Return true if FN is an accessor method for FIELD_DECL.
1846 i.e. a method of the form { return FIELD; }, with no
1847 conversions.
1849 If CONST_P, then additionally require that FN be a const
1850 method. */
1852 static bool
1853 field_accessor_p (tree fn, tree field_decl, bool const_p)
1855 if (TREE_CODE (fn) != FUNCTION_DECL)
1856 return false;
1858 /* We don't yet support looking up static data, just fields. */
1859 if (TREE_CODE (field_decl) != FIELD_DECL)
1860 return false;
1862 if (!DECL_OBJECT_MEMBER_FUNCTION_P (fn))
1863 return false;
1865 /* If the field is accessed via a const "this" argument, verify
1866 that the "this" parameter is const. */
1867 if (const_p)
1869 tree this_class = class_of_object_parm (fn);
1870 if (!TYPE_READONLY (this_class))
1871 return false;
1874 tree saved_tree = DECL_SAVED_TREE (fn);
1876 if (saved_tree == NULL_TREE)
1877 return false;
1879 /* Attempt to extract a single return value from the function,
1880 if it has one. */
1881 tree retval = constexpr_fn_retval (saved_tree);
1882 if (retval == NULL_TREE || retval == error_mark_node)
1883 return false;
1884 /* Require an INIT_EXPR. */
1885 if (TREE_CODE (retval) != INIT_EXPR)
1886 return false;
1887 tree init_expr = retval;
1889 /* Determine if this is a simple accessor within struct S of the form:
1890 T get_field () { return m_field; }. */
1891 tree field_type = TREE_TYPE (field_decl);
1892 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1893 return direct_accessor_p (init_expr, field_decl, field_type);
1895 /* Failing that, determine if it is an accessor of the form:
1896 T& get_field () { return m_field; }. */
1897 tree field_reference_type = cp_build_reference_type (field_type, false);
1898 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1899 return reference_accessor_p (init_expr, field_decl, field_type,
1900 field_reference_type);
1902 return false;
1905 /* Callback data for dfs_locate_field_accessor_pre. */
1907 class locate_field_data
1909 public:
1910 locate_field_data (tree field_decl_, bool const_p_)
1911 : field_decl (field_decl_), const_p (const_p_) {}
1913 tree field_decl;
1914 bool const_p;
1917 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1918 callable via binfo, if one exists, otherwise return NULL_TREE.
1920 Callback for dfs_walk_once_accessible for use within
1921 locate_field_accessor. */
1923 static tree
1924 dfs_locate_field_accessor_pre (tree binfo, void *data)
1926 locate_field_data *lfd = (locate_field_data *)data;
1927 tree type = BINFO_TYPE (binfo);
1929 vec<tree, va_gc> *member_vec;
1930 tree fn;
1931 size_t i;
1933 if (!CLASS_TYPE_P (type))
1934 return NULL_TREE;
1936 member_vec = CLASSTYPE_MEMBER_VEC (type);
1937 if (!member_vec)
1938 return NULL_TREE;
1940 for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1941 if (fn)
1942 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1943 return fn;
1945 return NULL_TREE;
1948 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1949 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1951 tree
1952 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1954 if (TREE_CODE (basetype_path) != TREE_BINFO)
1955 return NULL_TREE;
1957 /* Walk the hierarchy, looking for a method of some base class that allows
1958 access to the field. */
1959 locate_field_data lfd (field_decl, const_p);
1960 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1961 dfs_locate_field_accessor_pre,
1962 NULL, &lfd);
1965 /* Check throw specifier of OVERRIDER is at least as strict as
1966 the one of BASEFN. This is due to [except.spec]: "If a virtual function
1967 has a non-throwing exception specification, all declarations, including
1968 the definition, of any function that overrides that virtual function in
1969 any derived class shall have a non-throwing exception specification,
1970 unless the overriding function is defined as deleted." */
1972 bool
1973 maybe_check_overriding_exception_spec (tree overrider, tree basefn)
1975 maybe_instantiate_noexcept (basefn);
1976 maybe_instantiate_noexcept (overrider);
1977 tree base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1978 tree over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1980 if (DECL_INVALID_OVERRIDER_P (overrider)
1981 /* CWG 1351 added the "unless the overriding function is defined as
1982 deleted" wording. */
1983 || DECL_DELETED_FN (overrider))
1984 return true;
1986 /* Can't check this yet. Pretend this is fine and let
1987 noexcept_override_late_checks check this later. */
1988 if (UNPARSED_NOEXCEPT_SPEC_P (base_throw)
1989 || UNPARSED_NOEXCEPT_SPEC_P (over_throw))
1990 return true;
1992 /* We also have to defer checking when we're in a template and couldn't
1993 instantiate & evaluate the noexcept to true/false. */
1994 if (processing_template_decl)
1995 if ((base_throw
1996 && base_throw != noexcept_true_spec
1997 && base_throw != noexcept_false_spec)
1998 || (over_throw
1999 && over_throw != noexcept_true_spec
2000 && over_throw != noexcept_false_spec))
2001 return true;
2003 if (!comp_except_specs (base_throw, over_throw, ce_derived))
2005 auto_diagnostic_group d;
2006 error ("looser exception specification on overriding virtual function "
2007 "%q+#F", overrider);
2008 inform (DECL_SOURCE_LOCATION (basefn),
2009 "overridden function is %q#F", basefn);
2010 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2011 return false;
2013 return true;
2016 /* Check that virtual overrider OVERRIDER is acceptable for base function
2017 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2019 static int
2020 check_final_overrider (tree overrider, tree basefn)
2022 tree over_type = TREE_TYPE (overrider);
2023 tree base_type = TREE_TYPE (basefn);
2024 tree over_return = fndecl_declared_return_type (overrider);
2025 tree base_return = fndecl_declared_return_type (basefn);
2027 int fail = 0;
2029 if (DECL_INVALID_OVERRIDER_P (overrider))
2030 return 0;
2032 if (same_type_p (base_return, over_return))
2033 /* OK */;
2034 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
2035 || (TREE_CODE (base_return) == TREE_CODE (over_return)
2036 && INDIRECT_TYPE_P (base_return)))
2038 /* Potentially covariant. */
2039 unsigned base_quals, over_quals;
2041 fail = !INDIRECT_TYPE_P (base_return);
2042 if (!fail)
2044 if (cp_type_quals (base_return) != cp_type_quals (over_return))
2045 fail = 1;
2047 if (TYPE_REF_P (base_return)
2048 && (TYPE_REF_IS_RVALUE (base_return)
2049 != TYPE_REF_IS_RVALUE (over_return)))
2050 fail = 1;
2052 base_return = TREE_TYPE (base_return);
2053 over_return = TREE_TYPE (over_return);
2055 base_quals = cp_type_quals (base_return);
2056 over_quals = cp_type_quals (over_return);
2058 if ((base_quals & over_quals) != over_quals)
2059 fail = 1;
2061 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
2063 /* Strictly speaking, the standard requires the return type to be
2064 complete even if it only differs in cv-quals, but that seems
2065 like a bug in the wording. */
2066 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
2067 over_return))
2069 tree binfo = lookup_base (over_return, base_return,
2070 ba_check, NULL, tf_none);
2072 if (!binfo || binfo == error_mark_node)
2073 fail = 1;
2076 else if (can_convert_standard (TREE_TYPE (base_type),
2077 TREE_TYPE (over_type),
2078 tf_warning_or_error))
2079 /* GNU extension, allow trivial pointer conversions such as
2080 converting to void *, or qualification conversion. */
2082 auto_diagnostic_group d;
2083 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
2084 "invalid covariant return type for %q#D", overrider))
2085 inform (DECL_SOURCE_LOCATION (basefn),
2086 "overridden function is %q#D", basefn);
2088 else
2089 fail = 2;
2091 else
2092 fail = 2;
2093 if (!fail)
2094 /* OK */;
2095 else
2097 auto_diagnostic_group d;
2098 if (fail == 1)
2099 error ("invalid covariant return type for %q+#D", overrider);
2100 else
2101 error ("conflicting return type specified for %q+#D", overrider);
2102 inform (DECL_SOURCE_LOCATION (basefn),
2103 "overridden function is %q#D", basefn);
2104 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2105 return 0;
2108 if (!maybe_check_overriding_exception_spec (overrider, basefn))
2109 return 0;
2111 /* Check for conflicting type attributes. But leave transaction_safe for
2112 set_one_vmethod_tm_attributes. */
2113 if (!comp_type_attributes (over_type, base_type)
2114 && !tx_safe_fn_type_p (base_type)
2115 && !tx_safe_fn_type_p (over_type))
2117 auto_diagnostic_group d;
2118 error ("conflicting type attributes specified for %q+#D", overrider);
2119 inform (DECL_SOURCE_LOCATION (basefn),
2120 "overridden function is %q#D", basefn);
2121 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2122 return 0;
2125 /* A consteval virtual function shall not override a virtual function that is
2126 not consteval. A consteval virtual function shall not be overridden by a
2127 virtual function that is not consteval. */
2128 if (DECL_IMMEDIATE_FUNCTION_P (overrider)
2129 != DECL_IMMEDIATE_FUNCTION_P (basefn))
2131 auto_diagnostic_group d;
2132 if (DECL_IMMEDIATE_FUNCTION_P (overrider))
2133 error ("%<consteval%> function %q+D overriding non-%<consteval%> "
2134 "function", overrider);
2135 else
2136 error ("non-%<consteval%> function %q+D overriding %<consteval%> "
2137 "function", overrider);
2138 inform (DECL_SOURCE_LOCATION (basefn),
2139 "overridden function is %qD", basefn);
2140 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2141 return 0;
2144 /* A function declared transaction_safe_dynamic that overrides a function
2145 declared transaction_safe (but not transaction_safe_dynamic) is
2146 ill-formed. */
2147 if (tx_safe_fn_type_p (base_type)
2148 && lookup_attribute ("transaction_safe_dynamic",
2149 DECL_ATTRIBUTES (overrider))
2150 && !lookup_attribute ("transaction_safe_dynamic",
2151 DECL_ATTRIBUTES (basefn)))
2153 auto_diagnostic_group d;
2154 error_at (DECL_SOURCE_LOCATION (overrider),
2155 "%qD declared %<transaction_safe_dynamic%>", overrider);
2156 inform (DECL_SOURCE_LOCATION (basefn),
2157 "overriding %qD declared %<transaction_safe%>", basefn);
2160 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2162 if (DECL_DELETED_FN (overrider))
2164 auto_diagnostic_group d;
2165 error ("deleted function %q+D overriding non-deleted function",
2166 overrider);
2167 inform (DECL_SOURCE_LOCATION (basefn),
2168 "overridden function is %qD", basefn);
2169 maybe_explain_implicit_delete (overrider);
2171 else
2173 auto_diagnostic_group d;
2174 error ("non-deleted function %q+D overriding deleted function",
2175 overrider);
2176 inform (DECL_SOURCE_LOCATION (basefn),
2177 "overridden function is %qD", basefn);
2179 return 0;
2182 if (!DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
2184 auto_diagnostic_group d;
2185 error ("function with contracts %q+D overriding contractless function",
2186 overrider);
2187 inform (DECL_SOURCE_LOCATION (basefn),
2188 "overridden function is %qD", basefn);
2189 return 0;
2191 else if (DECL_HAS_CONTRACTS_P (basefn) && !DECL_HAS_CONTRACTS_P (overrider))
2193 /* We're inheriting basefn's contracts; create a copy of them but
2194 replace references to their parms to our parms. */
2195 inherit_base_contracts (overrider, basefn);
2197 else if (DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
2199 /* We're in the process of completing the overrider's class, which means
2200 our conditions definitely are not parsed so simply chain on the
2201 basefn for later checking.
2203 Note that OVERRIDER's contracts will have been fully parsed at the
2204 point the deferred match is run. */
2205 defer_guarded_contract_match (overrider, basefn, DECL_CONTRACTS (basefn));
2208 if (DECL_FINAL_P (basefn))
2210 auto_diagnostic_group d;
2211 error ("virtual function %q+D overriding final function", overrider);
2212 inform (DECL_SOURCE_LOCATION (basefn),
2213 "overridden function is %qD", basefn);
2214 return 0;
2216 return 1;
2219 /* Given a class TYPE, and a function decl FNDECL, look for
2220 virtual functions in TYPE's hierarchy which FNDECL overrides.
2221 We do not look in TYPE itself, only its bases.
2223 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2224 find that it overrides anything.
2226 We check that every function which is overridden, is correctly
2227 overridden. */
2230 look_for_overrides (tree type, tree fndecl)
2232 tree binfo = TYPE_BINFO (type);
2233 tree base_binfo;
2234 int ix;
2235 int found = 0;
2237 /* A constructor for a class T does not override a function T
2238 in a base class. */
2239 if (DECL_CONSTRUCTOR_P (fndecl))
2240 return 0;
2242 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2244 tree basetype = BINFO_TYPE (base_binfo);
2246 if (TYPE_POLYMORPHIC_P (basetype))
2247 found += look_for_overrides_r (basetype, fndecl);
2249 return found;
2252 /* Look in TYPE for virtual functions with the same signature as
2253 FNDECL. */
2255 tree
2256 look_for_overrides_here (tree type, tree fndecl)
2258 tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2260 for (ovl_iterator iter (ovl); iter; ++iter)
2262 tree fn = *iter;
2264 if (!DECL_VIRTUAL_P (fn))
2265 /* Not a virtual. */;
2266 else if (DECL_CONTEXT (fn) != type)
2267 /* Introduced with a using declaration. */;
2268 else if (DECL_STATIC_FUNCTION_P (fndecl)
2269 || DECL_XOBJ_MEMBER_FUNCTION_P (fndecl))
2271 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2272 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2273 dtypes = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl) ? TREE_CHAIN (dtypes)
2274 : dtypes;
2275 if (compparms (TREE_CHAIN (btypes), dtypes))
2276 return fn;
2278 else if (same_signature_p (fndecl, fn))
2279 return fn;
2282 return NULL_TREE;
2285 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2286 TYPE itself and its bases. */
2288 static int
2289 look_for_overrides_r (tree type, tree fndecl)
2291 tree fn = look_for_overrides_here (type, fndecl);
2292 if (fn)
2294 if (DECL_STATIC_FUNCTION_P (fndecl))
2296 /* A static member function cannot match an inherited
2297 virtual member function. */
2298 auto_diagnostic_group d;
2299 error ("%q+#D cannot be declared", fndecl);
2300 error (" since %q+#D declared in base class", fn);
2302 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fndecl))
2304 auto_diagnostic_group d;
2305 error_at (DECL_SOURCE_LOCATION (fndecl),
2306 "explicit object member function "
2307 "overrides virtual function");
2308 inform (DECL_SOURCE_LOCATION (fn),
2309 "virtual function declared here");
2311 else
2313 /* It's definitely virtual, even if not explicitly set. */
2314 DECL_VIRTUAL_P (fndecl) = 1;
2315 check_final_overrider (fndecl, fn);
2317 return 1;
2320 /* We failed to find one declared in this class. Look in its bases. */
2321 return look_for_overrides (type, fndecl);
2324 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2326 static tree
2327 dfs_get_pure_virtuals (tree binfo, void *data)
2329 tree type = (tree) data;
2331 /* We're not interested in primary base classes; the derived class
2332 of which they are a primary base will contain the information we
2333 need. */
2334 if (!BINFO_PRIMARY_P (binfo))
2336 tree virtuals;
2338 for (virtuals = BINFO_VIRTUALS (binfo);
2339 virtuals;
2340 virtuals = TREE_CHAIN (virtuals))
2341 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2342 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2345 return NULL_TREE;
2348 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2350 void
2351 get_pure_virtuals (tree type)
2353 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2354 is going to be overridden. */
2355 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2356 /* Now, run through all the bases which are not primary bases, and
2357 collect the pure virtual functions. We look at the vtable in
2358 each class to determine what pure virtual functions are present.
2359 (A primary base is not interesting because the derived class of
2360 which it is a primary base will contain vtable entries for the
2361 pure virtuals in the base class. */
2362 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2365 /* Debug info for C++ classes can get very large; try to avoid
2366 emitting it everywhere.
2368 Note that this optimization wins even when the target supports
2369 BINCL (if only slightly), and reduces the amount of work for the
2370 linker. */
2372 void
2373 maybe_suppress_debug_info (tree t)
2375 if (write_symbols == NO_DEBUG)
2376 return;
2378 /* We might have set this earlier in cp_finish_decl. */
2379 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2381 /* Always emit the information for each class every time. */
2382 if (flag_emit_class_debug_always)
2383 return;
2385 /* If we already know how we're handling this class, handle debug info
2386 the same way. */
2387 if (CLASSTYPE_INTERFACE_KNOWN (t))
2389 if (CLASSTYPE_INTERFACE_ONLY (t))
2390 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2391 /* else don't set it. */
2393 /* If the class has a vtable, write out the debug info along with
2394 the vtable. */
2395 else if (TYPE_CONTAINS_VPTR_P (t))
2396 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2398 /* Otherwise, just emit the debug info normally. */
2401 /* Note that we want debugging information for a base class of a class
2402 whose vtable is being emitted. Normally, this would happen because
2403 calling the constructor for a derived class implies calling the
2404 constructors for all bases, which involve initializing the
2405 appropriate vptr with the vtable for the base class; but in the
2406 presence of optimization, this initialization may be optimized
2407 away, so we tell finish_vtable_vardecl that we want the debugging
2408 information anyway. */
2410 static tree
2411 dfs_debug_mark (tree binfo, void * /*data*/)
2413 tree t = BINFO_TYPE (binfo);
2415 if (CLASSTYPE_DEBUG_REQUESTED (t))
2416 return dfs_skip_bases;
2418 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2420 return NULL_TREE;
2423 /* Write out the debugging information for TYPE, whose vtable is being
2424 emitted. Also walk through our bases and note that we want to
2425 write out information for them. This avoids the problem of not
2426 writing any debug info for intermediate basetypes whose
2427 constructors, and thus the references to their vtables, and thus
2428 the vtables themselves, were optimized away. */
2430 void
2431 note_debug_info_needed (tree type)
2433 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2435 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2436 rest_of_type_compilation (type, namespace_bindings_p ());
2439 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2442 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2443 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2444 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2445 bases have been encountered already in the tree walk. PARENT_CONVS
2446 is the list of lists of conversion functions that could hide CONV
2447 and OTHER_CONVS is the list of lists of conversion functions that
2448 could hide or be hidden by CONV, should virtualness be involved in
2449 the hierarchy. Merely checking the conversion op's name is not
2450 enough because two conversion operators to the same type can have
2451 different names. Return nonzero if we are visible. */
2453 static int
2454 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2455 tree to_type, tree parent_convs, tree other_convs)
2457 tree level, probe;
2459 /* See if we are hidden by a parent conversion. */
2460 for (level = parent_convs; level; level = TREE_CHAIN (level))
2461 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2462 if (same_type_p (to_type, TREE_TYPE (probe)))
2463 return 0;
2465 if (virtual_depth || virtualness)
2467 /* In a virtual hierarchy, we could be hidden, or could hide a
2468 conversion function on the other_convs list. */
2469 for (level = other_convs; level; level = TREE_CHAIN (level))
2471 int we_hide_them;
2472 int they_hide_us;
2473 tree *prev, other;
2475 if (!(virtual_depth || TREE_STATIC (level)))
2476 /* Neither is morally virtual, so cannot hide each other. */
2477 continue;
2479 if (!TREE_VALUE (level))
2480 /* They evaporated away already. */
2481 continue;
2483 they_hide_us = (virtual_depth
2484 && original_binfo (binfo, TREE_PURPOSE (level)));
2485 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2486 && original_binfo (TREE_PURPOSE (level), binfo));
2488 if (!(we_hide_them || they_hide_us))
2489 /* Neither is within the other, so no hiding can occur. */
2490 continue;
2492 for (prev = &TREE_VALUE (level), other = *prev; other;)
2494 if (same_type_p (to_type, TREE_TYPE (other)))
2496 if (they_hide_us)
2497 /* We are hidden. */
2498 return 0;
2500 if (we_hide_them)
2502 /* We hide the other one. */
2503 other = TREE_CHAIN (other);
2504 *prev = other;
2505 continue;
2508 prev = &TREE_CHAIN (other);
2509 other = *prev;
2513 return 1;
2516 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2517 of conversion functions, the first slot will be for the current
2518 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2519 of conversion functions from children of the current binfo,
2520 concatenated with conversions from elsewhere in the hierarchy --
2521 that list begins with OTHER_CONVS. Return a single list of lists
2522 containing only conversions from the current binfo and its
2523 children. */
2525 static tree
2526 split_conversions (tree my_convs, tree parent_convs,
2527 tree child_convs, tree other_convs)
2529 tree t;
2530 tree prev;
2532 /* Remove the original other_convs portion from child_convs. */
2533 for (prev = NULL, t = child_convs;
2534 t != other_convs; prev = t, t = TREE_CHAIN (t))
2535 continue;
2537 if (prev)
2538 TREE_CHAIN (prev) = NULL_TREE;
2539 else
2540 child_convs = NULL_TREE;
2542 /* Attach the child convs to any we had at this level. */
2543 if (my_convs)
2545 my_convs = parent_convs;
2546 TREE_CHAIN (my_convs) = child_convs;
2548 else
2549 my_convs = child_convs;
2551 return my_convs;
2554 /* Worker for lookup_conversions. Lookup conversion functions in
2555 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2556 morally virtual base, and VIRTUALNESS is nonzero, if we've
2557 encountered virtual bases already in the tree walk. PARENT_CONVS
2558 is a list of conversions within parent binfos. OTHER_CONVS are
2559 conversions found elsewhere in the tree. Return the conversions
2560 found within this portion of the graph in CONVS. Return nonzero if
2561 we encountered virtualness. We keep template and non-template
2562 conversions separate, to avoid unnecessary type comparisons.
2564 The located conversion functions are held in lists of lists. The
2565 TREE_VALUE of the outer list is the list of conversion functions
2566 found in a particular binfo. The TREE_PURPOSE of both the outer
2567 and inner lists is the binfo at which those conversions were
2568 found. TREE_STATIC is set for those lists within of morally
2569 virtual binfos. The TREE_VALUE of the inner list is the conversion
2570 function or overload itself. The TREE_TYPE of each inner list node
2571 is the converted-to type. */
2573 static int
2574 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2575 tree parent_convs, tree other_convs, tree *convs)
2577 int my_virtualness = 0;
2578 tree my_convs = NULL_TREE;
2579 tree child_convs = NULL_TREE;
2581 /* If we have no conversion operators, then don't look. */
2582 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2584 *convs = NULL_TREE;
2586 return 0;
2589 if (BINFO_VIRTUAL_P (binfo))
2590 virtual_depth++;
2592 /* First, locate the unhidden ones at this level. */
2593 if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2594 for (ovl_iterator iter (conv); iter; ++iter)
2596 tree fn = *iter;
2597 tree type = DECL_CONV_FN_TYPE (fn);
2599 if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2601 mark_used (fn);
2602 type = DECL_CONV_FN_TYPE (fn);
2605 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2606 type, parent_convs, other_convs))
2608 my_convs = tree_cons (binfo, fn, my_convs);
2609 TREE_TYPE (my_convs) = type;
2610 if (virtual_depth)
2612 TREE_STATIC (my_convs) = 1;
2613 my_virtualness = 1;
2618 if (my_convs)
2620 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2621 if (virtual_depth)
2622 TREE_STATIC (parent_convs) = 1;
2625 child_convs = other_convs;
2627 /* Now iterate over each base, looking for more conversions. */
2628 unsigned i;
2629 tree base_binfo;
2630 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2632 tree base_convs;
2633 unsigned base_virtualness;
2635 base_virtualness = lookup_conversions_r (base_binfo,
2636 virtual_depth, virtualness,
2637 parent_convs, child_convs,
2638 &base_convs);
2639 if (base_virtualness)
2640 my_virtualness = virtualness = 1;
2641 child_convs = chainon (base_convs, child_convs);
2644 *convs = split_conversions (my_convs, parent_convs,
2645 child_convs, other_convs);
2647 return my_virtualness;
2650 /* Return a TREE_LIST containing all the non-hidden user-defined
2651 conversion functions for TYPE (and its base-classes). The
2652 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2653 function. The TREE_PURPOSE is the BINFO from which the conversion
2654 functions in this node were selected. This function is effectively
2655 performing a set of member lookups as lookup_fnfield does, but
2656 using the type being converted to as the unique key, rather than the
2657 field name. */
2659 tree
2660 lookup_conversions (tree type)
2662 tree convs;
2664 complete_type (type);
2665 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2666 return NULL_TREE;
2668 lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2670 tree list = NULL_TREE;
2672 /* Flatten the list-of-lists */
2673 for (; convs; convs = TREE_CHAIN (convs))
2675 tree probe, next;
2677 for (probe = TREE_VALUE (convs); probe; probe = next)
2679 next = TREE_CHAIN (probe);
2681 TREE_CHAIN (probe) = list;
2682 list = probe;
2686 return list;
2689 /* Returns the binfo of the first direct or indirect virtual base derived
2690 from BINFO, or NULL if binfo is not via virtual. */
2692 tree
2693 binfo_from_vbase (tree binfo)
2695 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2697 if (BINFO_VIRTUAL_P (binfo))
2698 return binfo;
2700 return NULL_TREE;
2703 /* Returns the binfo of the first direct or indirect virtual base derived
2704 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2705 via virtual. */
2707 tree
2708 binfo_via_virtual (tree binfo, tree limit)
2710 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2711 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2712 return NULL_TREE;
2714 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2715 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2717 if (BINFO_VIRTUAL_P (binfo))
2718 return binfo;
2720 return NULL_TREE;
2723 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2724 direct base. */
2726 bool
2727 binfo_direct_p (tree binfo)
2729 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2730 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2731 /* A second inheritance chain means indirect. */
2732 return false;
2733 if (!BINFO_VIRTUAL_P (binfo))
2734 /* Non-virtual, so only one inheritance chain means direct. */
2735 return true;
2736 /* A virtual base looks like a direct base, so we need to look through the
2737 direct bases to see if it's there. */
2738 tree b_binfo;
2739 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2740 if (b_binfo == binfo)
2741 return true;
2742 return false;
2745 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2746 Find the equivalent binfo within whatever graph HERE is located.
2747 This is the inverse of original_binfo. */
2749 tree
2750 copied_binfo (tree binfo, tree here)
2752 tree result = NULL_TREE;
2754 if (BINFO_VIRTUAL_P (binfo))
2756 tree t;
2758 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2759 t = BINFO_INHERITANCE_CHAIN (t))
2760 continue;
2762 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2764 else if (BINFO_INHERITANCE_CHAIN (binfo))
2766 tree cbinfo;
2767 tree base_binfo;
2768 int ix;
2770 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2771 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2772 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2774 result = base_binfo;
2775 break;
2778 else
2780 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2781 result = here;
2784 gcc_assert (result);
2785 return result;
2788 tree
2789 binfo_for_vbase (tree base, tree t)
2791 unsigned ix;
2792 tree binfo;
2793 vec<tree, va_gc> *vbases;
2795 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2796 vec_safe_iterate (vbases, ix, &binfo); ix++)
2797 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2798 return binfo;
2799 return NULL;
2802 /* BINFO is some base binfo of HERE, within some other
2803 hierarchy. Return the equivalent binfo, but in the hierarchy
2804 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2805 is not a base binfo of HERE, returns NULL_TREE. */
2807 tree
2808 original_binfo (tree binfo, tree here)
2810 tree result = NULL;
2812 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2813 result = here;
2814 else if (BINFO_VIRTUAL_P (binfo))
2815 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2816 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2817 : NULL_TREE);
2818 else if (BINFO_INHERITANCE_CHAIN (binfo))
2820 tree base_binfos;
2822 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2823 if (base_binfos)
2825 int ix;
2826 tree base_binfo;
2828 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2829 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2830 BINFO_TYPE (binfo)))
2832 result = base_binfo;
2833 break;
2838 return result;
2841 /* True iff TYPE has any dependent bases (and therefore we can't say
2842 definitively that another class is not a base of an instantiation of
2843 TYPE). */
2845 bool
2846 any_dependent_bases_p (tree type /* = current_nonlambda_class_type () */)
2848 if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
2849 return false;
2851 /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2852 Return false because in this situation we aren't actually looking up names
2853 in the scope of the class, so it doesn't matter whether it has dependent
2854 bases. */
2855 if (!TYPE_BINFO (type))
2856 return false;
2858 unsigned i;
2859 tree base_binfo;
2860 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2861 if (BINFO_DEPENDENT_BASE_P (base_binfo)
2862 /* Recurse to also consider possibly dependent bases of a base that
2863 is part of the current instantiation. */
2864 || any_dependent_bases_p (BINFO_TYPE (base_binfo)))
2865 return true;
2867 return false;