CWG 616, 1213 - value category of subobject references.
[official-gcc.git] / gcc / cp / search.c
blob22c0492f535c1922808ecc7bd2ac02142c6d5915
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2018 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"
34 static int is_subobject_of_p (tree, tree);
35 static tree dfs_lookup_base (tree, void *);
36 static tree dfs_dcast_hint_pre (tree, void *);
37 static tree dfs_dcast_hint_post (tree, void *);
38 static tree dfs_debug_mark (tree, void *);
39 static int check_hidden_convs (tree, int, int, tree, tree, tree);
40 static tree split_conversions (tree, tree, tree, tree);
41 static int lookup_conversions_r (tree, int, int, tree, tree, tree *);
42 static int look_for_overrides_r (tree, tree);
43 static tree lookup_field_r (tree, void *);
44 static tree dfs_accessible_post (tree, void *);
45 static tree dfs_walk_once_accessible (tree, bool,
46 tree (*pre_fn) (tree, void *),
47 tree (*post_fn) (tree, void *),
48 void *data);
49 static tree dfs_access_in_type (tree, void *);
50 static access_kind access_in_type (tree, tree);
51 static tree dfs_get_pure_virtuals (tree, void *);
54 /* Data for lookup_base and its workers. */
56 struct lookup_base_data_s
58 tree t; /* type being searched. */
59 tree base; /* The base type we're looking for. */
60 tree binfo; /* Found binfo. */
61 bool via_virtual; /* Found via a virtual path. */
62 bool ambiguous; /* Found multiply ambiguous */
63 bool repeated_base; /* Whether there are repeated bases in the
64 hierarchy. */
65 bool want_any; /* Whether we want any matching binfo. */
68 /* Worker function for lookup_base. See if we've found the desired
69 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
71 static tree
72 dfs_lookup_base (tree binfo, void *data_)
74 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
76 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
78 if (!data->binfo)
80 data->binfo = binfo;
81 data->via_virtual
82 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
84 if (!data->repeated_base)
85 /* If there are no repeated bases, we can stop now. */
86 return binfo;
88 if (data->want_any && !data->via_virtual)
89 /* If this is a non-virtual base, then we can't do
90 better. */
91 return binfo;
93 return dfs_skip_bases;
95 else
97 gcc_assert (binfo != data->binfo);
99 /* We've found more than one matching binfo. */
100 if (!data->want_any)
102 /* This is immediately ambiguous. */
103 data->binfo = NULL_TREE;
104 data->ambiguous = true;
105 return error_mark_node;
108 /* Prefer one via a non-virtual path. */
109 if (!binfo_via_virtual (binfo, data->t))
111 data->binfo = binfo;
112 data->via_virtual = false;
113 return binfo;
116 /* There must be repeated bases, otherwise we'd have stopped
117 on the first base we found. */
118 return dfs_skip_bases;
122 return NULL_TREE;
125 /* Returns true if type BASE is accessible in T. (BASE is known to be
126 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
127 true, consider any special access of the current scope, or access
128 bestowed by friendship. */
130 bool
131 accessible_base_p (tree t, tree base, bool consider_local_p)
133 tree decl;
135 /* [class.access.base]
137 A base class is said to be accessible if an invented public
138 member of the base class is accessible.
140 If BASE is a non-proper base, this condition is trivially
141 true. */
142 if (same_type_p (t, base))
143 return true;
144 /* Rather than inventing a public member, we use the implicit
145 public typedef created in the scope of every class. */
146 decl = TYPE_FIELDS (base);
147 while (!DECL_SELF_REFERENCE_P (decl))
148 decl = DECL_CHAIN (decl);
149 while (ANON_AGGR_TYPE_P (t))
150 t = TYPE_CONTEXT (t);
151 return accessible_p (t, decl, consider_local_p);
154 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
155 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
156 non-NULL, fill with information about what kind of base we
157 discovered.
159 If the base is inaccessible, or ambiguous, then error_mark_node is
160 returned. If the tf_error bit of COMPLAIN is not set, no error
161 is issued. */
163 tree
164 lookup_base (tree t, tree base, base_access access,
165 base_kind *kind_ptr, tsubst_flags_t complain)
167 tree binfo;
168 tree t_binfo;
169 base_kind bk;
171 /* "Nothing" is definitely not derived from Base. */
172 if (t == NULL_TREE)
174 if (kind_ptr)
175 *kind_ptr = bk_not_base;
176 return NULL_TREE;
179 if (t == error_mark_node || base == error_mark_node)
181 if (kind_ptr)
182 *kind_ptr = bk_not_base;
183 return error_mark_node;
185 gcc_assert (TYPE_P (base));
187 if (!TYPE_P (t))
189 t_binfo = t;
190 t = BINFO_TYPE (t);
192 else
194 t = complete_type (TYPE_MAIN_VARIANT (t));
195 t_binfo = TYPE_BINFO (t);
198 base = TYPE_MAIN_VARIANT (base);
200 /* If BASE is incomplete, it can't be a base of T--and instantiating it
201 might cause an error. */
202 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
204 struct lookup_base_data_s data;
206 data.t = t;
207 data.base = base;
208 data.binfo = NULL_TREE;
209 data.ambiguous = data.via_virtual = false;
210 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
211 data.want_any = access == ba_any;
213 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
214 binfo = data.binfo;
216 if (!binfo)
217 bk = data.ambiguous ? bk_ambig : bk_not_base;
218 else if (binfo == t_binfo)
219 bk = bk_same_type;
220 else if (data.via_virtual)
221 bk = bk_via_virtual;
222 else
223 bk = bk_proper_base;
225 else
227 binfo = NULL_TREE;
228 bk = bk_not_base;
231 /* Check that the base is unambiguous and accessible. */
232 if (access != ba_any)
233 switch (bk)
235 case bk_not_base:
236 break;
238 case bk_ambig:
239 if (complain & tf_error)
240 error ("%qT is an ambiguous base of %qT", base, t);
241 binfo = error_mark_node;
242 break;
244 default:
245 if ((access & ba_check_bit)
246 /* If BASE is incomplete, then BASE and TYPE are probably
247 the same, in which case BASE is accessible. If they
248 are not the same, then TYPE is invalid. In that case,
249 there's no need to issue another error here, and
250 there's no implicit typedef to use in the code that
251 follows, so we skip the check. */
252 && COMPLETE_TYPE_P (base)
253 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
255 if (complain & tf_error)
256 error ("%qT is an inaccessible base of %qT", base, t);
257 binfo = error_mark_node;
258 bk = bk_inaccessible;
260 break;
263 if (kind_ptr)
264 *kind_ptr = bk;
266 return binfo;
269 /* Data for dcast_base_hint walker. */
271 struct dcast_data_s
273 tree subtype; /* The base type we're looking for. */
274 int virt_depth; /* Number of virtual bases encountered from most
275 derived. */
276 tree offset; /* Best hint offset discovered so far. */
277 bool repeated_base; /* Whether there are repeated bases in the
278 hierarchy. */
281 /* Worker for dcast_base_hint. Search for the base type being cast
282 from. */
284 static tree
285 dfs_dcast_hint_pre (tree binfo, void *data_)
287 struct dcast_data_s *data = (struct dcast_data_s *) data_;
289 if (BINFO_VIRTUAL_P (binfo))
290 data->virt_depth++;
292 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
294 if (data->virt_depth)
296 data->offset = ssize_int (-1);
297 return data->offset;
299 if (data->offset)
300 data->offset = ssize_int (-3);
301 else
302 data->offset = BINFO_OFFSET (binfo);
304 return data->repeated_base ? dfs_skip_bases : data->offset;
307 return NULL_TREE;
310 /* Worker for dcast_base_hint. Track the virtual depth. */
312 static tree
313 dfs_dcast_hint_post (tree binfo, void *data_)
315 struct dcast_data_s *data = (struct dcast_data_s *) data_;
317 if (BINFO_VIRTUAL_P (binfo))
318 data->virt_depth--;
320 return NULL_TREE;
323 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
324 started from is related to the required TARGET type, in order to optimize
325 the inheritance graph search. This information is independent of the
326 current context, and ignores private paths, hence get_base_distance is
327 inappropriate. Return a TREE specifying the base offset, BOFF.
328 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
329 and there are no public virtual SUBTYPE bases.
330 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
331 BOFF == -2, SUBTYPE is not a public base.
332 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
334 tree
335 dcast_base_hint (tree subtype, tree target)
337 struct dcast_data_s data;
339 data.subtype = subtype;
340 data.virt_depth = 0;
341 data.offset = NULL_TREE;
342 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
344 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
345 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
346 return data.offset ? data.offset : ssize_int (-2);
349 /* Search for a member with name NAME in a multiple inheritance
350 lattice specified by TYPE. If it does not exist, return NULL_TREE.
351 If the member is ambiguously referenced, return `error_mark_node'.
352 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
353 true, type declarations are preferred. */
355 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
356 NAMESPACE_DECL corresponding to the innermost non-block scope. */
358 tree
359 current_scope (void)
361 /* There are a number of cases we need to be aware of here:
362 current_class_type current_function_decl
363 global NULL NULL
364 fn-local NULL SET
365 class-local SET NULL
366 class->fn SET SET
367 fn->class SET SET
369 Those last two make life interesting. If we're in a function which is
370 itself inside a class, we need decls to go into the fn's decls (our
371 second case below). But if we're in a class and the class itself is
372 inside a function, we need decls to go into the decls for the class. To
373 achieve this last goal, we must see if, when both current_class_ptr and
374 current_function_decl are set, the class was declared inside that
375 function. If so, we know to put the decls into the class's scope. */
376 if (current_function_decl && current_class_type
377 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
378 && same_type_p (DECL_CONTEXT (current_function_decl),
379 current_class_type))
380 || (DECL_FRIEND_CONTEXT (current_function_decl)
381 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
382 current_class_type))))
383 return current_function_decl;
385 if (current_class_type)
386 return current_class_type;
388 if (current_function_decl)
389 return current_function_decl;
391 return current_namespace;
394 /* Returns nonzero if we are currently in a function scope. Note
395 that this function returns zero if we are within a local class, but
396 not within a member function body of the local class. */
399 at_function_scope_p (void)
401 tree cs = current_scope ();
402 /* Also check cfun to make sure that we're really compiling
403 this function (as opposed to having set current_function_decl
404 for access checking or some such). */
405 return (cs && TREE_CODE (cs) == FUNCTION_DECL
406 && cfun && cfun->decl == current_function_decl);
409 /* Returns true if the innermost active scope is a class scope. */
411 bool
412 at_class_scope_p (void)
414 tree cs = current_scope ();
415 return cs && TYPE_P (cs);
418 /* Returns true if the innermost active scope is a namespace scope. */
420 bool
421 at_namespace_scope_p (void)
423 tree cs = current_scope ();
424 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
427 /* Return the scope of DECL, as appropriate when doing name-lookup. */
429 tree
430 context_for_name_lookup (tree decl)
432 /* [class.union]
434 For the purposes of name lookup, after the anonymous union
435 definition, the members of the anonymous union are considered to
436 have been defined in the scope in which the anonymous union is
437 declared. */
438 tree context = DECL_CONTEXT (decl);
440 while (context && TYPE_P (context)
441 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
442 context = TYPE_CONTEXT (context);
443 if (!context)
444 context = global_namespace;
446 return context;
449 /* Returns true iff DECL is declared in TYPE. */
451 static bool
452 member_declared_in_type (tree decl, tree type)
454 /* A normal declaration obviously counts. */
455 if (context_for_name_lookup (decl) == type)
456 return true;
457 /* So does a using or access declaration. */
458 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
459 && purpose_member (type, DECL_ACCESS (decl)))
460 return true;
461 return false;
464 /* The accessibility routines use BINFO_ACCESS for scratch space
465 during the computation of the accessibility of some declaration. */
467 /* Avoid walking up past a declaration of the member. */
469 static tree
470 dfs_access_in_type_pre (tree binfo, void *data)
472 tree decl = (tree) data;
473 tree type = BINFO_TYPE (binfo);
474 if (member_declared_in_type (decl, type))
475 return dfs_skip_bases;
476 return NULL_TREE;
479 #define BINFO_ACCESS(NODE) \
480 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
482 /* Set the access associated with NODE to ACCESS. */
484 #define SET_BINFO_ACCESS(NODE, ACCESS) \
485 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
486 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
488 /* Called from access_in_type via dfs_walk. Calculate the access to
489 DATA (which is really a DECL) in BINFO. */
491 static tree
492 dfs_access_in_type (tree binfo, void *data)
494 tree decl = (tree) data;
495 tree type = BINFO_TYPE (binfo);
496 access_kind access = ak_none;
498 if (context_for_name_lookup (decl) == type)
500 /* If we have descended to the scope of DECL, just note the
501 appropriate access. */
502 if (TREE_PRIVATE (decl))
503 access = ak_private;
504 else if (TREE_PROTECTED (decl))
505 access = ak_protected;
506 else
507 access = ak_public;
509 else
511 /* First, check for an access-declaration that gives us more
512 access to the DECL. */
513 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
515 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
517 if (decl_access)
519 decl_access = TREE_VALUE (decl_access);
521 if (decl_access == access_public_node)
522 access = ak_public;
523 else if (decl_access == access_protected_node)
524 access = ak_protected;
525 else if (decl_access == access_private_node)
526 access = ak_private;
527 else
528 gcc_unreachable ();
532 if (!access)
534 int i;
535 tree base_binfo;
536 vec<tree, va_gc> *accesses;
538 /* Otherwise, scan our baseclasses, and pick the most favorable
539 access. */
540 accesses = BINFO_BASE_ACCESSES (binfo);
541 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
543 tree base_access = (*accesses)[i];
544 access_kind base_access_now = BINFO_ACCESS (base_binfo);
546 if (base_access_now == ak_none || base_access_now == ak_private)
547 /* If it was not accessible in the base, or only
548 accessible as a private member, we can't access it
549 all. */
550 base_access_now = ak_none;
551 else if (base_access == access_protected_node)
552 /* Public and protected members in the base become
553 protected here. */
554 base_access_now = ak_protected;
555 else if (base_access == access_private_node)
556 /* Public and protected members in the base become
557 private here. */
558 base_access_now = ak_private;
560 /* See if the new access, via this base, gives more
561 access than our previous best access. */
562 if (base_access_now != ak_none
563 && (access == ak_none || base_access_now < access))
565 access = base_access_now;
567 /* If the new access is public, we can't do better. */
568 if (access == ak_public)
569 break;
575 /* Note the access to DECL in TYPE. */
576 SET_BINFO_ACCESS (binfo, access);
578 return NULL_TREE;
581 /* Return the access to DECL in TYPE. */
583 static access_kind
584 access_in_type (tree type, tree decl)
586 tree binfo = TYPE_BINFO (type);
588 /* We must take into account
590 [class.paths]
592 If a name can be reached by several paths through a multiple
593 inheritance graph, the access is that of the path that gives
594 most access.
596 The algorithm we use is to make a post-order depth-first traversal
597 of the base-class hierarchy. As we come up the tree, we annotate
598 each node with the most lenient access. */
599 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
601 return BINFO_ACCESS (binfo);
604 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
605 of OTYPE in the context of DERIVED. */
607 static int
608 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
610 /* We're checking this clause from [class.access.base]
612 m as a member of N is protected, and the reference occurs in a
613 member or friend of class N, or in a member or friend of a
614 class P derived from N, where m as a member of P is public, private
615 or protected.
617 Here DERIVED is a possible P, DECL is m and TYPE is N. */
619 /* If DERIVED isn't derived from N, then it can't be a P. */
620 if (!DERIVED_FROM_P (type, derived))
621 return 0;
623 /* [class.protected]
625 When a friend or a member function of a derived class references
626 a protected nonstatic member of a base class, an access check
627 applies in addition to those described earlier in clause
628 _class.access_) Except when forming a pointer to member
629 (_expr.unary.op_), the access must be through a pointer to,
630 reference to, or object of the derived class itself (or any class
631 derived from that class) (_expr.ref_). If the access is to form
632 a pointer to member, the nested-name-specifier shall name the
633 derived class (or any class derived from that class). */
634 if (DECL_NONSTATIC_MEMBER_P (decl)
635 && !DERIVED_FROM_P (derived, otype))
636 return 0;
638 return 1;
641 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
642 to access DECL through TYPE. OTYPE is the type of the object. */
644 static int
645 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
647 /* We're checking this clause from [class.access.base]
649 m as a member of N is protected, and the reference occurs in a
650 member or friend of class N, or in a member or friend of a
651 class P derived from N, where m as a member of P is public, private
652 or protected.
654 Here DECL is m and TYPE is N. SCOPE is the current context,
655 and we check all its possible Ps. */
656 tree befriending_classes;
657 tree t;
659 if (!scope)
660 return 0;
662 if (is_global_friend (scope))
663 return 1;
665 /* Is SCOPE itself a suitable P? */
666 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
667 return 1;
669 if (DECL_DECLARES_FUNCTION_P (scope))
670 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
671 else if (TYPE_P (scope))
672 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
673 else
674 return 0;
676 for (t = befriending_classes; t; t = TREE_CHAIN (t))
677 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
678 return 1;
680 /* Nested classes have the same access as their enclosing types, as
681 per DR 45 (this is a change from C++98). */
682 if (TYPE_P (scope))
683 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
684 return 1;
686 if (DECL_DECLARES_FUNCTION_P (scope))
688 /* Perhaps this SCOPE is a member of a class which is a
689 friend. */
690 if (DECL_CLASS_SCOPE_P (scope)
691 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
692 return 1;
695 /* Maybe scope's template is a friend. */
696 if (tree tinfo = get_template_info (scope))
698 tree tmpl = TI_TEMPLATE (tinfo);
699 if (DECL_CLASS_TEMPLATE_P (tmpl))
700 tmpl = TREE_TYPE (tmpl);
701 else
702 tmpl = DECL_TEMPLATE_RESULT (tmpl);
703 if (tmpl != scope)
705 /* Increment processing_template_decl to make sure that
706 dependent_type_p works correctly. */
707 ++processing_template_decl;
708 int ret = friend_accessible_p (tmpl, decl, type, otype);
709 --processing_template_decl;
710 if (ret)
711 return 1;
715 /* If is_friend is true, we should have found a befriending class. */
716 gcc_checking_assert (!is_friend (type, scope));
718 return 0;
721 struct dfs_accessible_data
723 tree decl;
724 tree object_type;
727 /* Avoid walking up past a declaration of the member. */
729 static tree
730 dfs_accessible_pre (tree binfo, void *data)
732 dfs_accessible_data *d = (dfs_accessible_data *)data;
733 tree type = BINFO_TYPE (binfo);
734 if (member_declared_in_type (d->decl, type))
735 return dfs_skip_bases;
736 return NULL_TREE;
739 /* Called via dfs_walk_once_accessible from accessible_p */
741 static tree
742 dfs_accessible_post (tree binfo, void *data)
744 /* access_in_type already set BINFO_ACCESS for us. */
745 access_kind access = BINFO_ACCESS (binfo);
746 tree N = BINFO_TYPE (binfo);
747 dfs_accessible_data *d = (dfs_accessible_data *)data;
748 tree decl = d->decl;
749 tree scope = current_nonlambda_scope ();
751 /* A member m is accessible at the point R when named in class N if */
752 switch (access)
754 case ak_none:
755 return NULL_TREE;
757 case ak_public:
758 /* m as a member of N is public, or */
759 return binfo;
761 case ak_private:
763 /* m as a member of N is private, and R occurs in a member or friend of
764 class N, or */
765 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
766 && is_friend (N, scope))
767 return binfo;
768 return NULL_TREE;
771 case ak_protected:
773 /* m as a member of N is protected, and R occurs in a member or friend
774 of class N, or in a member or friend of a class P derived from N,
775 where m as a member of P is public, private, or protected */
776 if (friend_accessible_p (scope, decl, N, d->object_type))
777 return binfo;
778 return NULL_TREE;
781 default:
782 gcc_unreachable ();
786 /* Like accessible_p below, but within a template returns true iff DECL is
787 accessible in TYPE to all possible instantiations of the template. */
790 accessible_in_template_p (tree type, tree decl)
792 int save_ptd = processing_template_decl;
793 processing_template_decl = 0;
794 int val = accessible_p (type, decl, false);
795 processing_template_decl = save_ptd;
796 return val;
799 /* DECL is a declaration from a base class of TYPE, which was the
800 class used to name DECL. Return nonzero if, in the current
801 context, DECL is accessible. If TYPE is actually a BINFO node,
802 then we can tell in what context the access is occurring by looking
803 at the most derived class along the path indicated by BINFO. If
804 CONSIDER_LOCAL is true, do consider special access the current
805 scope or friendship thereof we might have. */
808 accessible_p (tree type, tree decl, bool consider_local_p)
810 tree binfo;
811 access_kind access;
813 /* If this declaration is in a block or namespace scope, there's no
814 access control. */
815 if (!TYPE_P (context_for_name_lookup (decl)))
816 return 1;
818 /* There is no need to perform access checks inside a thunk. */
819 if (current_function_decl && DECL_THUNK_P (current_function_decl))
820 return 1;
822 /* In a template declaration, we cannot be sure whether the
823 particular specialization that is instantiated will be a friend
824 or not. Therefore, all access checks are deferred until
825 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
826 parameter list for a template (because we may see dependent types
827 in default arguments for template parameters), and access
828 checking should be performed in the outermost parameter list. */
829 if (processing_template_decl
830 && !expanding_concept ()
831 && (!processing_template_parmlist || processing_template_decl > 1))
832 return 1;
834 tree otype = NULL_TREE;
835 if (!TYPE_P (type))
837 /* When accessing a non-static member, the most derived type in the
838 binfo chain is the type of the object; remember that type for
839 protected_accessible_p. */
840 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
841 otype = BINFO_TYPE (b);
842 type = BINFO_TYPE (type);
844 else
845 otype = type;
847 /* [class.access.base]
849 A member m is accessible when named in class N if
851 --m as a member of N is public, or
853 --m as a member of N is private, and the reference occurs in a
854 member or friend of class N, or
856 --m as a member of N is protected, and the reference occurs in a
857 member or friend of class N, or in a member or friend of a
858 class P derived from N, where m as a member of P is public, private or
859 protected, or
861 --there exists a base class B of N that is accessible at the point
862 of reference, and m is accessible when named in class B.
864 We walk the base class hierarchy, checking these conditions. */
866 /* We walk using TYPE_BINFO (type) because access_in_type will set
867 BINFO_ACCESS on it and its bases. */
868 binfo = TYPE_BINFO (type);
870 /* Compute the accessibility of DECL in the class hierarchy
871 dominated by type. */
872 access = access_in_type (type, decl);
873 if (access == ak_public)
874 return 1;
876 /* If we aren't considering the point of reference, only the first bullet
877 applies. */
878 if (!consider_local_p)
879 return 0;
881 dfs_accessible_data d = { decl, otype };
883 /* Walk the hierarchy again, looking for a base class that allows
884 access. */
885 return dfs_walk_once_accessible (binfo, /*friends=*/true,
886 dfs_accessible_pre,
887 dfs_accessible_post, &d)
888 != NULL_TREE;
891 struct lookup_field_info {
892 /* The type in which we're looking. */
893 tree type;
894 /* The name of the field for which we're looking. */
895 tree name;
896 /* If non-NULL, the current result of the lookup. */
897 tree rval;
898 /* The path to RVAL. */
899 tree rval_binfo;
900 /* If non-NULL, the lookup was ambiguous, and this is a list of the
901 candidates. */
902 tree ambiguous;
903 /* If nonzero, we are looking for types, not data members. */
904 int want_type;
905 /* If something went wrong, a message indicating what. */
906 const char *errstr;
909 /* Nonzero for a class member means that it is shared between all objects
910 of that class.
912 [class.member.lookup]:If the resulting set of declarations are not all
913 from sub-objects of the same type, or the set has a nonstatic member
914 and includes members from distinct sub-objects, there is an ambiguity
915 and the program is ill-formed.
917 This function checks that T contains no nonstatic members. */
920 shared_member_p (tree t)
922 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
923 || TREE_CODE (t) == CONST_DECL)
924 return 1;
925 if (is_overloaded_fn (t))
927 for (ovl_iterator iter (get_fns (t)); iter; ++iter)
928 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter))
929 return 0;
930 return 1;
932 return 0;
935 /* Routine to see if the sub-object denoted by the binfo PARENT can be
936 found as a base class and sub-object of the object denoted by
937 BINFO. */
939 static int
940 is_subobject_of_p (tree parent, tree binfo)
942 tree probe;
944 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
946 if (probe == binfo)
947 return 1;
948 if (BINFO_VIRTUAL_P (probe))
949 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
950 != NULL_TREE);
952 return 0;
955 /* DATA is really a struct lookup_field_info. Look for a field with
956 the name indicated there in BINFO. If this function returns a
957 non-NULL value it is the result of the lookup. Called from
958 lookup_field via breadth_first_search. */
960 static tree
961 lookup_field_r (tree binfo, void *data)
963 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
964 tree type = BINFO_TYPE (binfo);
965 tree nval = NULL_TREE;
967 /* If this is a dependent base, don't look in it. */
968 if (BINFO_DEPENDENT_BASE_P (binfo))
969 return NULL_TREE;
971 /* If this base class is hidden by the best-known value so far, we
972 don't need to look. */
973 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
974 && !BINFO_VIRTUAL_P (binfo))
975 return dfs_skip_bases;
977 nval = get_class_binding (type, lfi->name, lfi->want_type);
979 /* If we're looking up a type (as with an elaborated type specifier)
980 we ignore all non-types we find. */
981 if (lfi->want_type && nval && !DECL_DECLARES_TYPE_P (nval))
983 nval = NULL_TREE;
984 if (CLASSTYPE_NESTED_UTDS (type))
985 if (binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
986 lfi->name))
987 nval = TYPE_MAIN_DECL (e->type);
990 /* If there is no declaration with the indicated name in this type,
991 then there's nothing to do. */
992 if (!nval)
993 goto done;
995 /* If the lookup already found a match, and the new value doesn't
996 hide the old one, we might have an ambiguity. */
997 if (lfi->rval_binfo
998 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1001 if (nval == lfi->rval && shared_member_p (nval))
1002 /* The two things are really the same. */
1004 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1005 /* The previous value hides the new one. */
1007 else
1009 /* We have a real ambiguity. We keep a chain of all the
1010 candidates. */
1011 if (!lfi->ambiguous && lfi->rval)
1013 /* This is the first time we noticed an ambiguity. Add
1014 what we previously thought was a reasonable candidate
1015 to the list. */
1016 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1017 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1020 /* Add the new value. */
1021 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1022 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1023 lfi->errstr = G_("request for member %qD is ambiguous");
1026 else
1028 lfi->rval = nval;
1029 lfi->rval_binfo = binfo;
1032 done:
1033 /* Don't look for constructors or destructors in base classes. */
1034 if (IDENTIFIER_CDTOR_P (lfi->name))
1035 return dfs_skip_bases;
1036 return NULL_TREE;
1039 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1040 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1041 FUNCTIONS, and OPTYPE respectively. */
1043 tree
1044 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1046 tree baselink;
1048 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1049 || TREE_CODE (functions) == TEMPLATE_DECL
1050 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1051 || TREE_CODE (functions) == OVERLOAD);
1052 gcc_assert (!optype || TYPE_P (optype));
1053 gcc_assert (TREE_TYPE (functions));
1055 baselink = make_node (BASELINK);
1056 TREE_TYPE (baselink) = TREE_TYPE (functions);
1057 BASELINK_BINFO (baselink) = binfo;
1058 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1059 BASELINK_FUNCTIONS (baselink) = functions;
1060 BASELINK_OPTYPE (baselink) = optype;
1062 return baselink;
1065 /* Look for a member named NAME in an inheritance lattice dominated by
1066 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1067 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1068 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1069 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1070 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1071 TREE_VALUEs are the list of ambiguous candidates.
1073 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1075 If nothing can be found return NULL_TREE and do not issue an error.
1077 If non-NULL, failure information is written back to AFI. */
1079 tree
1080 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1081 tsubst_flags_t complain, access_failure_info *afi)
1083 tree rval, rval_binfo = NULL_TREE;
1084 tree type = NULL_TREE, basetype_path = NULL_TREE;
1085 struct lookup_field_info lfi;
1087 /* rval_binfo is the binfo associated with the found member, note,
1088 this can be set with useful information, even when rval is not
1089 set, because it must deal with ALL members, not just non-function
1090 members. It is used for ambiguity checking and the hidden
1091 checks. Whereas rval is only set if a proper (not hidden)
1092 non-function member is found. */
1094 const char *errstr = 0;
1096 if (name == error_mark_node
1097 || xbasetype == NULL_TREE
1098 || xbasetype == error_mark_node)
1099 return NULL_TREE;
1101 gcc_assert (identifier_p (name));
1103 if (TREE_CODE (xbasetype) == TREE_BINFO)
1105 type = BINFO_TYPE (xbasetype);
1106 basetype_path = xbasetype;
1108 else
1110 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1111 return NULL_TREE;
1112 type = xbasetype;
1113 xbasetype = NULL_TREE;
1116 type = complete_type (type);
1118 /* Make sure we're looking for a member of the current instantiation in the
1119 right partial specialization. */
1120 if (flag_concepts && dependent_type_p (type))
1121 if (tree t = currently_open_class (type))
1122 type = t;
1124 if (!basetype_path)
1125 basetype_path = TYPE_BINFO (type);
1127 if (!basetype_path)
1128 return NULL_TREE;
1130 memset (&lfi, 0, sizeof (lfi));
1131 lfi.type = type;
1132 lfi.name = name;
1133 lfi.want_type = want_type;
1134 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1135 rval = lfi.rval;
1136 rval_binfo = lfi.rval_binfo;
1137 if (rval_binfo)
1138 type = BINFO_TYPE (rval_binfo);
1139 errstr = lfi.errstr;
1141 /* If we are not interested in ambiguities, don't report them;
1142 just return NULL_TREE. */
1143 if (!protect && lfi.ambiguous)
1144 return NULL_TREE;
1146 if (protect == 2)
1148 if (lfi.ambiguous)
1149 return lfi.ambiguous;
1150 else
1151 protect = 0;
1154 /* [class.access]
1156 In the case of overloaded function names, access control is
1157 applied to the function selected by overloaded resolution.
1159 We cannot check here, even if RVAL is only a single non-static
1160 member function, since we do not know what the "this" pointer
1161 will be. For:
1163 class A { protected: void f(); };
1164 class B : public A {
1165 void g(A *p) {
1166 f(); // OK
1167 p->f(); // Not OK.
1171 only the first call to "f" is valid. However, if the function is
1172 static, we can check. */
1173 if (rval && protect
1174 && !really_overloaded_fn (rval))
1176 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1177 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1178 && !perform_or_defer_access_check (basetype_path, decl, decl,
1179 complain, afi))
1180 rval = error_mark_node;
1183 if (errstr && protect)
1185 if (complain & tf_error)
1187 error (errstr, name, type);
1188 if (lfi.ambiguous)
1189 print_candidates (lfi.ambiguous);
1191 rval = error_mark_node;
1194 if (rval && is_overloaded_fn (rval))
1195 rval = build_baselink (rval_binfo, basetype_path, rval,
1196 (IDENTIFIER_CONV_OP_P (name)
1197 ? TREE_TYPE (name): NULL_TREE));
1198 return rval;
1201 /* Helper class for lookup_member_fuzzy. */
1203 class lookup_field_fuzzy_info
1205 public:
1206 lookup_field_fuzzy_info (bool want_type_p) :
1207 m_want_type_p (want_type_p), m_candidates () {}
1209 void fuzzy_lookup_field (tree type);
1211 /* If true, we are looking for types, not data members. */
1212 bool m_want_type_p;
1213 /* The result: a vec of identifiers. */
1214 auto_vec<tree> m_candidates;
1217 /* Locate all fields within TYPE, append them to m_candidates. */
1219 void
1220 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1222 if (!CLASS_TYPE_P (type))
1223 return;
1225 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1227 if (m_want_type_p && !DECL_DECLARES_TYPE_P (field))
1228 continue;
1230 if (!DECL_NAME (field))
1231 continue;
1233 if (is_lambda_ignored_entity (field))
1234 continue;
1236 m_candidates.safe_push (DECL_NAME (field));
1241 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1242 DATA is really a lookup_field_fuzzy_info. Look for a field with
1243 the name indicated there in BINFO. Gathers pertinent identifiers into
1244 m_candidates. */
1246 static tree
1247 lookup_field_fuzzy_r (tree binfo, void *data)
1249 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1250 tree type = BINFO_TYPE (binfo);
1252 lffi->fuzzy_lookup_field (type);
1254 return NULL_TREE;
1257 /* Like lookup_member, but try to find the closest match for NAME,
1258 rather than an exact match, and return an identifier (or NULL_TREE).
1259 Do not complain. */
1261 tree
1262 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1264 tree type = NULL_TREE, basetype_path = NULL_TREE;
1265 struct lookup_field_fuzzy_info lffi (want_type_p);
1267 /* rval_binfo is the binfo associated with the found member, note,
1268 this can be set with useful information, even when rval is not
1269 set, because it must deal with ALL members, not just non-function
1270 members. It is used for ambiguity checking and the hidden
1271 checks. Whereas rval is only set if a proper (not hidden)
1272 non-function member is found. */
1274 if (name == error_mark_node
1275 || xbasetype == NULL_TREE
1276 || xbasetype == error_mark_node)
1277 return NULL_TREE;
1279 gcc_assert (identifier_p (name));
1281 if (TREE_CODE (xbasetype) == TREE_BINFO)
1283 type = BINFO_TYPE (xbasetype);
1284 basetype_path = xbasetype;
1286 else
1288 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1289 return NULL_TREE;
1290 type = xbasetype;
1291 xbasetype = NULL_TREE;
1294 type = complete_type (type);
1296 /* Make sure we're looking for a member of the current instantiation in the
1297 right partial specialization. */
1298 if (flag_concepts && dependent_type_p (type))
1299 type = currently_open_class (type);
1301 if (!basetype_path)
1302 basetype_path = TYPE_BINFO (type);
1304 if (!basetype_path)
1305 return NULL_TREE;
1307 /* Populate lffi.m_candidates. */
1308 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1310 return find_closest_identifier (name, &lffi.m_candidates);
1313 /* Like lookup_member, except that if we find a function member we
1314 return NULL_TREE. */
1316 tree
1317 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1319 tree rval = lookup_member (xbasetype, name, protect, want_type,
1320 tf_warning_or_error);
1322 /* Ignore functions, but propagate the ambiguity list. */
1323 if (!error_operand_p (rval)
1324 && (rval && BASELINK_P (rval)))
1325 return NULL_TREE;
1327 return rval;
1330 /* Like lookup_member, except that if we find a non-function member we
1331 return NULL_TREE. */
1333 tree
1334 lookup_fnfields (tree xbasetype, tree name, int protect)
1336 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1337 tf_warning_or_error);
1339 /* Ignore non-functions, but propagate the ambiguity list. */
1340 if (!error_operand_p (rval)
1341 && (rval && !BASELINK_P (rval)))
1342 return NULL_TREE;
1344 return rval;
1347 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1348 the class or namespace used to qualify the name. CONTEXT_CLASS is
1349 the class corresponding to the object in which DECL will be used.
1350 Return a possibly modified version of DECL that takes into account
1351 the CONTEXT_CLASS.
1353 In particular, consider an expression like `B::m' in the context of
1354 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1355 then the most derived class indicated by the BASELINK_BINFO will be
1356 `B', not `D'. This function makes that adjustment. */
1358 tree
1359 adjust_result_of_qualified_name_lookup (tree decl,
1360 tree qualifying_scope,
1361 tree context_class)
1363 if (context_class && context_class != error_mark_node
1364 && CLASS_TYPE_P (context_class)
1365 && CLASS_TYPE_P (qualifying_scope)
1366 && DERIVED_FROM_P (qualifying_scope, context_class)
1367 && BASELINK_P (decl))
1369 tree base;
1371 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1372 Because we do not yet know which function will be chosen by
1373 overload resolution, we cannot yet check either accessibility
1374 or ambiguity -- in either case, the choice of a static member
1375 function might make the usage valid. */
1376 base = lookup_base (context_class, qualifying_scope,
1377 ba_unique, NULL, tf_none);
1378 if (base && base != error_mark_node)
1380 BASELINK_ACCESS_BINFO (decl) = base;
1381 tree decl_binfo
1382 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1383 ba_unique, NULL, tf_none);
1384 if (decl_binfo && decl_binfo != error_mark_node)
1385 BASELINK_BINFO (decl) = decl_binfo;
1389 if (BASELINK_P (decl))
1390 BASELINK_QUALIFIED_P (decl) = true;
1392 return decl;
1396 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1397 PRE_FN is called in preorder, while POST_FN is called in postorder.
1398 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1399 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1400 that value is immediately returned and the walk is terminated. One
1401 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1402 POST_FN are passed the binfo to examine and the caller's DATA
1403 value. All paths are walked, thus virtual and morally virtual
1404 binfos can be multiply walked. */
1406 tree
1407 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1408 tree (*post_fn) (tree, void *), void *data)
1410 tree rval;
1411 unsigned ix;
1412 tree base_binfo;
1414 /* Call the pre-order walking function. */
1415 if (pre_fn)
1417 rval = pre_fn (binfo, data);
1418 if (rval)
1420 if (rval == dfs_skip_bases)
1421 goto skip_bases;
1422 return rval;
1426 /* Find the next child binfo to walk. */
1427 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1429 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1430 if (rval)
1431 return rval;
1434 skip_bases:
1435 /* Call the post-order walking function. */
1436 if (post_fn)
1438 rval = post_fn (binfo, data);
1439 gcc_assert (rval != dfs_skip_bases);
1440 return rval;
1443 return NULL_TREE;
1446 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1447 that binfos are walked at most once. */
1449 static tree
1450 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1451 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1452 void *data)
1454 tree rval;
1455 unsigned ix;
1456 tree base_binfo;
1458 /* Call the pre-order walking function. */
1459 if (pre_fn)
1461 rval = pre_fn (binfo, data);
1462 if (rval)
1464 if (rval == dfs_skip_bases)
1465 goto skip_bases;
1467 return rval;
1471 /* Find the next child binfo to walk. */
1472 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1474 if (BINFO_VIRTUAL_P (base_binfo))
1475 if (pset->add (base_binfo))
1476 continue;
1478 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1479 if (rval)
1480 return rval;
1483 skip_bases:
1484 /* Call the post-order walking function. */
1485 if (post_fn)
1487 rval = post_fn (binfo, data);
1488 gcc_assert (rval != dfs_skip_bases);
1489 return rval;
1492 return NULL_TREE;
1495 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1496 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1497 For diamond shaped hierarchies we must mark the virtual bases, to
1498 avoid multiple walks. */
1500 tree
1501 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1502 tree (*post_fn) (tree, void *), void *data)
1504 static int active = 0; /* We must not be called recursively. */
1505 tree rval;
1507 gcc_assert (pre_fn || post_fn);
1508 gcc_assert (!active);
1509 active++;
1511 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1512 /* We are not diamond shaped, and therefore cannot encounter the
1513 same binfo twice. */
1514 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1515 else
1517 hash_set<tree> pset;
1518 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1521 active--;
1523 return rval;
1526 /* Worker function for dfs_walk_once_accessible. Behaves like
1527 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1528 access given by the current context should be considered, (b) ONCE
1529 indicates whether bases should be marked during traversal. */
1531 static tree
1532 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1533 tree (*pre_fn) (tree, void *),
1534 tree (*post_fn) (tree, void *), void *data)
1536 tree rval = NULL_TREE;
1537 unsigned ix;
1538 tree base_binfo;
1540 /* Call the pre-order walking function. */
1541 if (pre_fn)
1543 rval = pre_fn (binfo, data);
1544 if (rval)
1546 if (rval == dfs_skip_bases)
1547 goto skip_bases;
1549 return rval;
1553 /* Find the next child binfo to walk. */
1554 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1556 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1558 if (mark && pset->contains (base_binfo))
1559 continue;
1561 /* If the base is inherited via private or protected
1562 inheritance, then we can't see it, unless we are a friend of
1563 the current binfo. */
1564 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1566 tree scope;
1567 if (!friends_p)
1568 continue;
1569 scope = current_scope ();
1570 if (!scope
1571 || TREE_CODE (scope) == NAMESPACE_DECL
1572 || !is_friend (BINFO_TYPE (binfo), scope))
1573 continue;
1576 if (mark)
1577 pset->add (base_binfo);
1579 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1580 pre_fn, post_fn, data);
1581 if (rval)
1582 return rval;
1585 skip_bases:
1586 /* Call the post-order walking function. */
1587 if (post_fn)
1589 rval = post_fn (binfo, data);
1590 gcc_assert (rval != dfs_skip_bases);
1591 return rval;
1594 return NULL_TREE;
1597 /* Like dfs_walk_once except that only accessible bases are walked.
1598 FRIENDS_P indicates whether friendship of the local context
1599 should be considered when determining accessibility. */
1601 static tree
1602 dfs_walk_once_accessible (tree binfo, bool friends_p,
1603 tree (*pre_fn) (tree, void *),
1604 tree (*post_fn) (tree, void *), void *data)
1606 hash_set<tree> *pset = NULL;
1607 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1608 pset = new hash_set<tree>;
1609 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1610 pre_fn, post_fn, data);
1612 if (pset)
1613 delete pset;
1614 return rval;
1617 /* Return true iff the code of T is CODE, and it has compatible
1618 type with TYPE. */
1620 static bool
1621 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1623 if (TREE_CODE (t) != code)
1624 return false;
1625 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1626 return false;
1627 return true;
1630 /* Subroutine of direct_accessor_p and reference_accessor_p.
1631 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1632 We expect a tree of the form:
1633 <component_ref:
1634 <indirect_ref:S>
1635 <nop_expr:P*
1636 <parm_decl (this)>
1637 <field_decl (FIELD_DECL)>>>. */
1639 static bool
1640 field_access_p (tree component_ref, tree field_decl, tree field_type)
1642 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
1643 return false;
1645 tree indirect_ref = TREE_OPERAND (component_ref, 0);
1646 if (!INDIRECT_REF_P (indirect_ref))
1647 return false;
1649 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
1650 if (!is_this_parameter (ptr))
1651 return false;
1653 /* Must access the correct field. */
1654 if (TREE_OPERAND (component_ref, 1) != field_decl)
1655 return false;
1656 return true;
1659 /* Subroutine of field_accessor_p.
1661 Assuming that INIT_EXPR has already had its code and type checked,
1662 determine if it is a simple accessor for FIELD_DECL
1663 (of type FIELD_TYPE).
1665 Specifically, a simple accessor within struct S of the form:
1666 T get_field () { return m_field; }
1667 should have a constexpr_fn_retval (saved_tree) of the form:
1668 <init_expr:T
1669 <result_decl:T
1670 <nop_expr:T
1671 <component_ref:
1672 <indirect_ref:S>
1673 <nop_expr:P*
1674 <parm_decl (this)>
1675 <field_decl (FIELD_DECL)>>>>>. */
1677 static bool
1678 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1680 tree result_decl = TREE_OPERAND (init_expr, 0);
1681 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1682 return false;
1684 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1685 if (!field_access_p (component_ref, field_decl, field_type))
1686 return false;
1688 return true;
1691 /* Subroutine of field_accessor_p.
1693 Assuming that INIT_EXPR has already had its code and type checked,
1694 determine if it is a "reference" accessor for FIELD_DECL
1695 (of type FIELD_REFERENCE_TYPE).
1697 Specifically, a simple accessor within struct S of the form:
1698 T& get_field () { return m_field; }
1699 should have a constexpr_fn_retval (saved_tree) of the form:
1700 <init_expr:T&
1701 <result_decl:T&
1702 <nop_expr: T&
1703 <addr_expr: T*
1704 <component_ref:T
1705 <indirect_ref:S
1706 <nop_expr
1707 <parm_decl (this)>>
1708 <field (FIELD_DECL)>>>>>>. */
1709 static bool
1710 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1711 tree field_reference_type)
1713 tree result_decl = TREE_OPERAND (init_expr, 0);
1714 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1715 return false;
1717 tree field_pointer_type = build_pointer_type (field_type);
1718 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1719 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1720 return false;
1722 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1724 if (!field_access_p (component_ref, field_decl, field_type))
1725 return false;
1727 return true;
1730 /* Return true if FN is an accessor method for FIELD_DECL.
1731 i.e. a method of the form { return FIELD; }, with no
1732 conversions.
1734 If CONST_P, then additionally require that FN be a const
1735 method. */
1737 static bool
1738 field_accessor_p (tree fn, tree field_decl, bool const_p)
1740 if (TREE_CODE (fn) != FUNCTION_DECL)
1741 return false;
1743 /* We don't yet support looking up static data, just fields. */
1744 if (TREE_CODE (field_decl) != FIELD_DECL)
1745 return false;
1747 tree fntype = TREE_TYPE (fn);
1748 if (TREE_CODE (fntype) != METHOD_TYPE)
1749 return false;
1751 /* If the field is accessed via a const "this" argument, verify
1752 that the "this" parameter is const. */
1753 if (const_p)
1755 tree this_class = class_of_this_parm (fntype);
1756 if (!TYPE_READONLY (this_class))
1757 return false;
1760 tree saved_tree = DECL_SAVED_TREE (fn);
1762 if (saved_tree == NULL_TREE)
1763 return false;
1765 /* Attempt to extract a single return value from the function,
1766 if it has one. */
1767 tree retval = constexpr_fn_retval (saved_tree);
1768 if (retval == NULL_TREE || retval == error_mark_node)
1769 return false;
1770 /* Require an INIT_EXPR. */
1771 if (TREE_CODE (retval) != INIT_EXPR)
1772 return false;
1773 tree init_expr = retval;
1775 /* Determine if this is a simple accessor within struct S of the form:
1776 T get_field () { return m_field; }. */
1777 tree field_type = TREE_TYPE (field_decl);
1778 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1779 return direct_accessor_p (init_expr, field_decl, field_type);
1781 /* Failing that, determine if it is an accessor of the form:
1782 T& get_field () { return m_field; }. */
1783 tree field_reference_type = cp_build_reference_type (field_type, false);
1784 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1785 return reference_accessor_p (init_expr, field_decl, field_type,
1786 field_reference_type);
1788 return false;
1791 /* Callback data for dfs_locate_field_accessor_pre. */
1793 struct locate_field_data
1795 locate_field_data (tree field_decl_, bool const_p_)
1796 : field_decl (field_decl_), const_p (const_p_) {}
1798 tree field_decl;
1799 bool const_p;
1802 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1803 callable via binfo, if one exists, otherwise return NULL_TREE.
1805 Callback for dfs_walk_once_accessible for use within
1806 locate_field_accessor. */
1808 static tree
1809 dfs_locate_field_accessor_pre (tree binfo, void *data)
1811 locate_field_data *lfd = (locate_field_data *)data;
1812 tree type = BINFO_TYPE (binfo);
1814 vec<tree, va_gc> *member_vec;
1815 tree fn;
1816 size_t i;
1818 if (!CLASS_TYPE_P (type))
1819 return NULL_TREE;
1821 member_vec = CLASSTYPE_MEMBER_VEC (type);
1822 if (!member_vec)
1823 return NULL_TREE;
1825 for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1826 if (fn)
1827 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1828 return fn;
1830 return NULL_TREE;
1833 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1834 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1836 tree
1837 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1839 if (TREE_CODE (basetype_path) != TREE_BINFO)
1840 return NULL_TREE;
1842 /* Walk the hierarchy, looking for a method of some base class that allows
1843 access to the field. */
1844 locate_field_data lfd (field_decl, const_p);
1845 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1846 dfs_locate_field_accessor_pre,
1847 NULL, &lfd);
1850 /* Check that virtual overrider OVERRIDER is acceptable for base function
1851 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1853 static int
1854 check_final_overrider (tree overrider, tree basefn)
1856 tree over_type = TREE_TYPE (overrider);
1857 tree base_type = TREE_TYPE (basefn);
1858 tree over_return = fndecl_declared_return_type (overrider);
1859 tree base_return = fndecl_declared_return_type (basefn);
1860 tree over_throw, base_throw;
1862 int fail = 0;
1864 if (DECL_INVALID_OVERRIDER_P (overrider))
1865 return 0;
1867 if (same_type_p (base_return, over_return))
1868 /* OK */;
1869 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1870 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1871 && POINTER_TYPE_P (base_return)))
1873 /* Potentially covariant. */
1874 unsigned base_quals, over_quals;
1876 fail = !POINTER_TYPE_P (base_return);
1877 if (!fail)
1879 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1881 base_return = TREE_TYPE (base_return);
1882 over_return = TREE_TYPE (over_return);
1884 base_quals = cp_type_quals (base_return);
1885 over_quals = cp_type_quals (over_return);
1887 if ((base_quals & over_quals) != over_quals)
1888 fail = 1;
1890 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1892 /* Strictly speaking, the standard requires the return type to be
1893 complete even if it only differs in cv-quals, but that seems
1894 like a bug in the wording. */
1895 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1896 over_return))
1898 tree binfo = lookup_base (over_return, base_return,
1899 ba_check, NULL, tf_none);
1901 if (!binfo || binfo == error_mark_node)
1902 fail = 1;
1905 else if (can_convert_standard (TREE_TYPE (base_type),
1906 TREE_TYPE (over_type),
1907 tf_warning_or_error))
1908 /* GNU extension, allow trivial pointer conversions such as
1909 converting to void *, or qualification conversion. */
1911 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1912 "invalid covariant return type for %q#D", overrider))
1913 inform (DECL_SOURCE_LOCATION (basefn),
1914 "overridden function is %q#D", basefn);
1916 else
1917 fail = 2;
1919 else
1920 fail = 2;
1921 if (!fail)
1922 /* OK */;
1923 else
1925 if (fail == 1)
1927 error ("invalid covariant return type for %q+#D", overrider);
1928 inform (DECL_SOURCE_LOCATION (basefn),
1929 "overridden function is %q#D", basefn);
1931 else
1933 error ("conflicting return type specified for %q+#D", overrider);
1934 inform (DECL_SOURCE_LOCATION (basefn),
1935 "overridden function is %q#D", basefn);
1937 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1938 return 0;
1941 /* Check throw specifier is at least as strict. */
1942 maybe_instantiate_noexcept (basefn);
1943 maybe_instantiate_noexcept (overrider);
1944 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1945 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1947 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1949 error ("looser throw specifier for %q+#F", overrider);
1950 inform (DECL_SOURCE_LOCATION (basefn),
1951 "overridden function is %q#F", basefn);
1952 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1953 return 0;
1956 /* Check for conflicting type attributes. But leave transaction_safe for
1957 set_one_vmethod_tm_attributes. */
1958 if (!comp_type_attributes (over_type, base_type)
1959 && !tx_safe_fn_type_p (base_type)
1960 && !tx_safe_fn_type_p (over_type))
1962 error ("conflicting type attributes specified for %q+#D", overrider);
1963 inform (DECL_SOURCE_LOCATION (basefn),
1964 "overridden function is %q#D", basefn);
1965 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1966 return 0;
1969 /* A function declared transaction_safe_dynamic that overrides a function
1970 declared transaction_safe (but not transaction_safe_dynamic) is
1971 ill-formed. */
1972 if (tx_safe_fn_type_p (base_type)
1973 && lookup_attribute ("transaction_safe_dynamic",
1974 DECL_ATTRIBUTES (overrider))
1975 && !lookup_attribute ("transaction_safe_dynamic",
1976 DECL_ATTRIBUTES (basefn)))
1978 error_at (DECL_SOURCE_LOCATION (overrider),
1979 "%qD declared %<transaction_safe_dynamic%>", overrider);
1980 inform (DECL_SOURCE_LOCATION (basefn),
1981 "overriding %qD declared %<transaction_safe%>", basefn);
1984 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1986 if (DECL_DELETED_FN (overrider))
1988 error ("deleted function %q+D overriding non-deleted function",
1989 overrider);
1990 inform (DECL_SOURCE_LOCATION (basefn),
1991 "overridden function is %qD", basefn);
1992 maybe_explain_implicit_delete (overrider);
1994 else
1996 error ("non-deleted function %q+D overriding deleted function",
1997 overrider);
1998 inform (DECL_SOURCE_LOCATION (basefn),
1999 "overridden function is %qD", basefn);
2001 return 0;
2003 if (DECL_FINAL_P (basefn))
2005 error ("virtual function %q+D overriding final function", overrider);
2006 inform (DECL_SOURCE_LOCATION (basefn),
2007 "overridden function is %qD", basefn);
2008 return 0;
2010 return 1;
2013 /* Given a class TYPE, and a function decl FNDECL, look for
2014 virtual functions in TYPE's hierarchy which FNDECL overrides.
2015 We do not look in TYPE itself, only its bases.
2017 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2018 find that it overrides anything.
2020 We check that every function which is overridden, is correctly
2021 overridden. */
2024 look_for_overrides (tree type, tree fndecl)
2026 tree binfo = TYPE_BINFO (type);
2027 tree base_binfo;
2028 int ix;
2029 int found = 0;
2031 /* A constructor for a class T does not override a function T
2032 in a base class. */
2033 if (DECL_CONSTRUCTOR_P (fndecl))
2034 return 0;
2036 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2038 tree basetype = BINFO_TYPE (base_binfo);
2040 if (TYPE_POLYMORPHIC_P (basetype))
2041 found += look_for_overrides_r (basetype, fndecl);
2043 return found;
2046 /* Look in TYPE for virtual functions with the same signature as
2047 FNDECL. */
2049 tree
2050 look_for_overrides_here (tree type, tree fndecl)
2052 tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2054 for (ovl_iterator iter (ovl); iter; ++iter)
2056 tree fn = *iter;
2058 if (!DECL_VIRTUAL_P (fn))
2059 /* Not a virtual. */;
2060 else if (DECL_CONTEXT (fn) != type)
2061 /* Introduced with a using declaration. */;
2062 else if (DECL_STATIC_FUNCTION_P (fndecl))
2064 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2065 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2066 if (compparms (TREE_CHAIN (btypes), dtypes))
2067 return fn;
2069 else if (same_signature_p (fndecl, fn))
2070 return fn;
2073 return NULL_TREE;
2076 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2077 TYPE itself and its bases. */
2079 static int
2080 look_for_overrides_r (tree type, tree fndecl)
2082 tree fn = look_for_overrides_here (type, fndecl);
2083 if (fn)
2085 if (DECL_STATIC_FUNCTION_P (fndecl))
2087 /* A static member function cannot match an inherited
2088 virtual member function. */
2089 error ("%q+#D cannot be declared", fndecl);
2090 error (" since %q+#D declared in base class", fn);
2092 else
2094 /* It's definitely virtual, even if not explicitly set. */
2095 DECL_VIRTUAL_P (fndecl) = 1;
2096 check_final_overrider (fndecl, fn);
2098 return 1;
2101 /* We failed to find one declared in this class. Look in its bases. */
2102 return look_for_overrides (type, fndecl);
2105 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2107 static tree
2108 dfs_get_pure_virtuals (tree binfo, void *data)
2110 tree type = (tree) data;
2112 /* We're not interested in primary base classes; the derived class
2113 of which they are a primary base will contain the information we
2114 need. */
2115 if (!BINFO_PRIMARY_P (binfo))
2117 tree virtuals;
2119 for (virtuals = BINFO_VIRTUALS (binfo);
2120 virtuals;
2121 virtuals = TREE_CHAIN (virtuals))
2122 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2123 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2126 return NULL_TREE;
2129 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2131 void
2132 get_pure_virtuals (tree type)
2134 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2135 is going to be overridden. */
2136 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2137 /* Now, run through all the bases which are not primary bases, and
2138 collect the pure virtual functions. We look at the vtable in
2139 each class to determine what pure virtual functions are present.
2140 (A primary base is not interesting because the derived class of
2141 which it is a primary base will contain vtable entries for the
2142 pure virtuals in the base class. */
2143 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2146 /* Debug info for C++ classes can get very large; try to avoid
2147 emitting it everywhere.
2149 Note that this optimization wins even when the target supports
2150 BINCL (if only slightly), and reduces the amount of work for the
2151 linker. */
2153 void
2154 maybe_suppress_debug_info (tree t)
2156 if (write_symbols == NO_DEBUG)
2157 return;
2159 /* We might have set this earlier in cp_finish_decl. */
2160 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2162 /* Always emit the information for each class every time. */
2163 if (flag_emit_class_debug_always)
2164 return;
2166 /* If we already know how we're handling this class, handle debug info
2167 the same way. */
2168 if (CLASSTYPE_INTERFACE_KNOWN (t))
2170 if (CLASSTYPE_INTERFACE_ONLY (t))
2171 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2172 /* else don't set it. */
2174 /* If the class has a vtable, write out the debug info along with
2175 the vtable. */
2176 else if (TYPE_CONTAINS_VPTR_P (t))
2177 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2179 /* Otherwise, just emit the debug info normally. */
2182 /* Note that we want debugging information for a base class of a class
2183 whose vtable is being emitted. Normally, this would happen because
2184 calling the constructor for a derived class implies calling the
2185 constructors for all bases, which involve initializing the
2186 appropriate vptr with the vtable for the base class; but in the
2187 presence of optimization, this initialization may be optimized
2188 away, so we tell finish_vtable_vardecl that we want the debugging
2189 information anyway. */
2191 static tree
2192 dfs_debug_mark (tree binfo, void * /*data*/)
2194 tree t = BINFO_TYPE (binfo);
2196 if (CLASSTYPE_DEBUG_REQUESTED (t))
2197 return dfs_skip_bases;
2199 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2201 return NULL_TREE;
2204 /* Write out the debugging information for TYPE, whose vtable is being
2205 emitted. Also walk through our bases and note that we want to
2206 write out information for them. This avoids the problem of not
2207 writing any debug info for intermediate basetypes whose
2208 constructors, and thus the references to their vtables, and thus
2209 the vtables themselves, were optimized away. */
2211 void
2212 note_debug_info_needed (tree type)
2214 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2216 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2217 rest_of_type_compilation (type, namespace_bindings_p ());
2220 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2223 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2224 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2225 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2226 bases have been encountered already in the tree walk. PARENT_CONVS
2227 is the list of lists of conversion functions that could hide CONV
2228 and OTHER_CONVS is the list of lists of conversion functions that
2229 could hide or be hidden by CONV, should virtualness be involved in
2230 the hierarchy. Merely checking the conversion op's name is not
2231 enough because two conversion operators to the same type can have
2232 different names. Return nonzero if we are visible. */
2234 static int
2235 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2236 tree to_type, tree parent_convs, tree other_convs)
2238 tree level, probe;
2240 /* See if we are hidden by a parent conversion. */
2241 for (level = parent_convs; level; level = TREE_CHAIN (level))
2242 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2243 if (same_type_p (to_type, TREE_TYPE (probe)))
2244 return 0;
2246 if (virtual_depth || virtualness)
2248 /* In a virtual hierarchy, we could be hidden, or could hide a
2249 conversion function on the other_convs list. */
2250 for (level = other_convs; level; level = TREE_CHAIN (level))
2252 int we_hide_them;
2253 int they_hide_us;
2254 tree *prev, other;
2256 if (!(virtual_depth || TREE_STATIC (level)))
2257 /* Neither is morally virtual, so cannot hide each other. */
2258 continue;
2260 if (!TREE_VALUE (level))
2261 /* They evaporated away already. */
2262 continue;
2264 they_hide_us = (virtual_depth
2265 && original_binfo (binfo, TREE_PURPOSE (level)));
2266 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2267 && original_binfo (TREE_PURPOSE (level), binfo));
2269 if (!(we_hide_them || they_hide_us))
2270 /* Neither is within the other, so no hiding can occur. */
2271 continue;
2273 for (prev = &TREE_VALUE (level), other = *prev; other;)
2275 if (same_type_p (to_type, TREE_TYPE (other)))
2277 if (they_hide_us)
2278 /* We are hidden. */
2279 return 0;
2281 if (we_hide_them)
2283 /* We hide the other one. */
2284 other = TREE_CHAIN (other);
2285 *prev = other;
2286 continue;
2289 prev = &TREE_CHAIN (other);
2290 other = *prev;
2294 return 1;
2297 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2298 of conversion functions, the first slot will be for the current
2299 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2300 of conversion functions from children of the current binfo,
2301 concatenated with conversions from elsewhere in the hierarchy --
2302 that list begins with OTHER_CONVS. Return a single list of lists
2303 containing only conversions from the current binfo and its
2304 children. */
2306 static tree
2307 split_conversions (tree my_convs, tree parent_convs,
2308 tree child_convs, tree other_convs)
2310 tree t;
2311 tree prev;
2313 /* Remove the original other_convs portion from child_convs. */
2314 for (prev = NULL, t = child_convs;
2315 t != other_convs; prev = t, t = TREE_CHAIN (t))
2316 continue;
2318 if (prev)
2319 TREE_CHAIN (prev) = NULL_TREE;
2320 else
2321 child_convs = NULL_TREE;
2323 /* Attach the child convs to any we had at this level. */
2324 if (my_convs)
2326 my_convs = parent_convs;
2327 TREE_CHAIN (my_convs) = child_convs;
2329 else
2330 my_convs = child_convs;
2332 return my_convs;
2335 /* Worker for lookup_conversions. Lookup conversion functions in
2336 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2337 morally virtual base, and VIRTUALNESS is nonzero, if we've
2338 encountered virtual bases already in the tree walk. PARENT_CONVS
2339 is a list of conversions within parent binfos. OTHER_CONVS are
2340 conversions found elsewhere in the tree. Return the conversions
2341 found within this portion of the graph in CONVS. Return nonzero if
2342 we encountered virtualness. We keep template and non-template
2343 conversions separate, to avoid unnecessary type comparisons.
2345 The located conversion functions are held in lists of lists. The
2346 TREE_VALUE of the outer list is the list of conversion functions
2347 found in a particular binfo. The TREE_PURPOSE of both the outer
2348 and inner lists is the binfo at which those conversions were
2349 found. TREE_STATIC is set for those lists within of morally
2350 virtual binfos. The TREE_VALUE of the inner list is the conversion
2351 function or overload itself. The TREE_TYPE of each inner list node
2352 is the converted-to type. */
2354 static int
2355 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2356 tree parent_convs, tree other_convs, tree *convs)
2358 int my_virtualness = 0;
2359 tree my_convs = NULL_TREE;
2360 tree child_convs = NULL_TREE;
2362 /* If we have no conversion operators, then don't look. */
2363 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2365 *convs = NULL_TREE;
2367 return 0;
2370 if (BINFO_VIRTUAL_P (binfo))
2371 virtual_depth++;
2373 /* First, locate the unhidden ones at this level. */
2374 if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2375 for (ovl_iterator iter (conv); iter; ++iter)
2377 tree fn = *iter;
2378 tree type = DECL_CONV_FN_TYPE (fn);
2380 if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2382 mark_used (fn);
2383 type = DECL_CONV_FN_TYPE (fn);
2386 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2387 type, parent_convs, other_convs))
2389 my_convs = tree_cons (binfo, fn, my_convs);
2390 TREE_TYPE (my_convs) = type;
2391 if (virtual_depth)
2393 TREE_STATIC (my_convs) = 1;
2394 my_virtualness = 1;
2399 if (my_convs)
2401 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2402 if (virtual_depth)
2403 TREE_STATIC (parent_convs) = 1;
2406 child_convs = other_convs;
2408 /* Now iterate over each base, looking for more conversions. */
2409 unsigned i;
2410 tree base_binfo;
2411 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2413 tree base_convs;
2414 unsigned base_virtualness;
2416 base_virtualness = lookup_conversions_r (base_binfo,
2417 virtual_depth, virtualness,
2418 parent_convs, child_convs,
2419 &base_convs);
2420 if (base_virtualness)
2421 my_virtualness = virtualness = 1;
2422 child_convs = chainon (base_convs, child_convs);
2425 *convs = split_conversions (my_convs, parent_convs,
2426 child_convs, other_convs);
2428 return my_virtualness;
2431 /* Return a TREE_LIST containing all the non-hidden user-defined
2432 conversion functions for TYPE (and its base-classes). The
2433 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2434 function. The TREE_PURPOSE is the BINFO from which the conversion
2435 functions in this node were selected. This function is effectively
2436 performing a set of member lookups as lookup_fnfield does, but
2437 using the type being converted to as the unique key, rather than the
2438 field name. */
2440 tree
2441 lookup_conversions (tree type)
2443 tree convs;
2445 complete_type (type);
2446 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2447 return NULL_TREE;
2449 lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2451 tree list = NULL_TREE;
2453 /* Flatten the list-of-lists */
2454 for (; convs; convs = TREE_CHAIN (convs))
2456 tree probe, next;
2458 for (probe = TREE_VALUE (convs); probe; probe = next)
2460 next = TREE_CHAIN (probe);
2462 TREE_CHAIN (probe) = list;
2463 list = probe;
2467 return list;
2470 /* Returns the binfo of the first direct or indirect virtual base derived
2471 from BINFO, or NULL if binfo is not via virtual. */
2473 tree
2474 binfo_from_vbase (tree binfo)
2476 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2478 if (BINFO_VIRTUAL_P (binfo))
2479 return binfo;
2481 return NULL_TREE;
2484 /* Returns the binfo of the first direct or indirect virtual base derived
2485 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2486 via virtual. */
2488 tree
2489 binfo_via_virtual (tree binfo, tree limit)
2491 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2492 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2493 return NULL_TREE;
2495 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2496 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2498 if (BINFO_VIRTUAL_P (binfo))
2499 return binfo;
2501 return NULL_TREE;
2504 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2505 direct base. */
2507 bool
2508 binfo_direct_p (tree binfo)
2510 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2511 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2512 /* A second inheritance chain means indirect. */
2513 return false;
2514 if (!BINFO_VIRTUAL_P (binfo))
2515 /* Non-virtual, so only one inheritance chain means direct. */
2516 return true;
2517 /* A virtual base looks like a direct base, so we need to look through the
2518 direct bases to see if it's there. */
2519 tree b_binfo;
2520 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2521 if (b_binfo == binfo)
2522 return true;
2523 return false;
2526 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2527 Find the equivalent binfo within whatever graph HERE is located.
2528 This is the inverse of original_binfo. */
2530 tree
2531 copied_binfo (tree binfo, tree here)
2533 tree result = NULL_TREE;
2535 if (BINFO_VIRTUAL_P (binfo))
2537 tree t;
2539 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2540 t = BINFO_INHERITANCE_CHAIN (t))
2541 continue;
2543 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2545 else if (BINFO_INHERITANCE_CHAIN (binfo))
2547 tree cbinfo;
2548 tree base_binfo;
2549 int ix;
2551 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2552 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2553 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2555 result = base_binfo;
2556 break;
2559 else
2561 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2562 result = here;
2565 gcc_assert (result);
2566 return result;
2569 tree
2570 binfo_for_vbase (tree base, tree t)
2572 unsigned ix;
2573 tree binfo;
2574 vec<tree, va_gc> *vbases;
2576 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2577 vec_safe_iterate (vbases, ix, &binfo); ix++)
2578 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2579 return binfo;
2580 return NULL;
2583 /* BINFO is some base binfo of HERE, within some other
2584 hierarchy. Return the equivalent binfo, but in the hierarchy
2585 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2586 is not a base binfo of HERE, returns NULL_TREE. */
2588 tree
2589 original_binfo (tree binfo, tree here)
2591 tree result = NULL;
2593 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2594 result = here;
2595 else if (BINFO_VIRTUAL_P (binfo))
2596 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2597 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2598 : NULL_TREE);
2599 else if (BINFO_INHERITANCE_CHAIN (binfo))
2601 tree base_binfos;
2603 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2604 if (base_binfos)
2606 int ix;
2607 tree base_binfo;
2609 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2610 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2611 BINFO_TYPE (binfo)))
2613 result = base_binfo;
2614 break;
2619 return result;
2622 /* True iff TYPE has any dependent bases (and therefore we can't say
2623 definitively that another class is not a base of an instantiation of
2624 TYPE). */
2626 bool
2627 any_dependent_bases_p (tree type)
2629 if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
2630 return false;
2632 /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2633 Return false because in this situation we aren't actually looking up names
2634 in the scope of the class, so it doesn't matter whether it has dependent
2635 bases. */
2636 if (!TYPE_BINFO (type))
2637 return false;
2639 unsigned i;
2640 tree base_binfo;
2641 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2642 if (BINFO_DEPENDENT_BASE_P (base_binfo))
2643 return true;
2645 return false;