PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / search.c
blob1de04f237d9c1bce9d54fd1ff3ab4e167cb903c5
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2017 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 if (DECL_NAME (field))
1229 m_candidates.safe_push (DECL_NAME (field));
1234 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1235 DATA is really a lookup_field_fuzzy_info. Look for a field with
1236 the name indicated there in BINFO. Gathers pertinent identifiers into
1237 m_candidates. */
1239 static tree
1240 lookup_field_fuzzy_r (tree binfo, void *data)
1242 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1243 tree type = BINFO_TYPE (binfo);
1245 lffi->fuzzy_lookup_field (type);
1247 return NULL_TREE;
1250 /* Like lookup_member, but try to find the closest match for NAME,
1251 rather than an exact match, and return an identifier (or NULL_TREE).
1252 Do not complain. */
1254 tree
1255 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1257 tree type = NULL_TREE, basetype_path = NULL_TREE;
1258 struct lookup_field_fuzzy_info lffi (want_type_p);
1260 /* rval_binfo is the binfo associated with the found member, note,
1261 this can be set with useful information, even when rval is not
1262 set, because it must deal with ALL members, not just non-function
1263 members. It is used for ambiguity checking and the hidden
1264 checks. Whereas rval is only set if a proper (not hidden)
1265 non-function member is found. */
1267 if (name == error_mark_node
1268 || xbasetype == NULL_TREE
1269 || xbasetype == error_mark_node)
1270 return NULL_TREE;
1272 gcc_assert (identifier_p (name));
1274 if (TREE_CODE (xbasetype) == TREE_BINFO)
1276 type = BINFO_TYPE (xbasetype);
1277 basetype_path = xbasetype;
1279 else
1281 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1282 return NULL_TREE;
1283 type = xbasetype;
1284 xbasetype = NULL_TREE;
1287 type = complete_type (type);
1289 /* Make sure we're looking for a member of the current instantiation in the
1290 right partial specialization. */
1291 if (flag_concepts && dependent_type_p (type))
1292 type = currently_open_class (type);
1294 if (!basetype_path)
1295 basetype_path = TYPE_BINFO (type);
1297 if (!basetype_path)
1298 return NULL_TREE;
1300 /* Populate lffi.m_candidates. */
1301 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1303 return find_closest_identifier (name, &lffi.m_candidates);
1306 /* Like lookup_member, except that if we find a function member we
1307 return NULL_TREE. */
1309 tree
1310 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1312 tree rval = lookup_member (xbasetype, name, protect, want_type,
1313 tf_warning_or_error);
1315 /* Ignore functions, but propagate the ambiguity list. */
1316 if (!error_operand_p (rval)
1317 && (rval && BASELINK_P (rval)))
1318 return NULL_TREE;
1320 return rval;
1323 /* Like lookup_member, except that if we find a non-function member we
1324 return NULL_TREE. */
1326 tree
1327 lookup_fnfields (tree xbasetype, tree name, int protect)
1329 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1330 tf_warning_or_error);
1332 /* Ignore non-functions, but propagate the ambiguity list. */
1333 if (!error_operand_p (rval)
1334 && (rval && !BASELINK_P (rval)))
1335 return NULL_TREE;
1337 return rval;
1340 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1341 the class or namespace used to qualify the name. CONTEXT_CLASS is
1342 the class corresponding to the object in which DECL will be used.
1343 Return a possibly modified version of DECL that takes into account
1344 the CONTEXT_CLASS.
1346 In particular, consider an expression like `B::m' in the context of
1347 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1348 then the most derived class indicated by the BASELINK_BINFO will be
1349 `B', not `D'. This function makes that adjustment. */
1351 tree
1352 adjust_result_of_qualified_name_lookup (tree decl,
1353 tree qualifying_scope,
1354 tree context_class)
1356 if (context_class && context_class != error_mark_node
1357 && CLASS_TYPE_P (context_class)
1358 && CLASS_TYPE_P (qualifying_scope)
1359 && DERIVED_FROM_P (qualifying_scope, context_class)
1360 && BASELINK_P (decl))
1362 tree base;
1364 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1365 Because we do not yet know which function will be chosen by
1366 overload resolution, we cannot yet check either accessibility
1367 or ambiguity -- in either case, the choice of a static member
1368 function might make the usage valid. */
1369 base = lookup_base (context_class, qualifying_scope,
1370 ba_unique, NULL, tf_none);
1371 if (base && base != error_mark_node)
1373 BASELINK_ACCESS_BINFO (decl) = base;
1374 tree decl_binfo
1375 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1376 ba_unique, NULL, tf_none);
1377 if (decl_binfo && decl_binfo != error_mark_node)
1378 BASELINK_BINFO (decl) = decl_binfo;
1382 if (BASELINK_P (decl))
1383 BASELINK_QUALIFIED_P (decl) = true;
1385 return decl;
1389 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1390 PRE_FN is called in preorder, while POST_FN is called in postorder.
1391 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1392 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1393 that value is immediately returned and the walk is terminated. One
1394 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1395 POST_FN are passed the binfo to examine and the caller's DATA
1396 value. All paths are walked, thus virtual and morally virtual
1397 binfos can be multiply walked. */
1399 tree
1400 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1401 tree (*post_fn) (tree, void *), void *data)
1403 tree rval;
1404 unsigned ix;
1405 tree base_binfo;
1407 /* Call the pre-order walking function. */
1408 if (pre_fn)
1410 rval = pre_fn (binfo, data);
1411 if (rval)
1413 if (rval == dfs_skip_bases)
1414 goto skip_bases;
1415 return rval;
1419 /* Find the next child binfo to walk. */
1420 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1422 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1423 if (rval)
1424 return rval;
1427 skip_bases:
1428 /* Call the post-order walking function. */
1429 if (post_fn)
1431 rval = post_fn (binfo, data);
1432 gcc_assert (rval != dfs_skip_bases);
1433 return rval;
1436 return NULL_TREE;
1439 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1440 that binfos are walked at most once. */
1442 static tree
1443 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1444 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1445 void *data)
1447 tree rval;
1448 unsigned ix;
1449 tree base_binfo;
1451 /* Call the pre-order walking function. */
1452 if (pre_fn)
1454 rval = pre_fn (binfo, data);
1455 if (rval)
1457 if (rval == dfs_skip_bases)
1458 goto skip_bases;
1460 return rval;
1464 /* Find the next child binfo to walk. */
1465 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1467 if (BINFO_VIRTUAL_P (base_binfo))
1468 if (pset->add (base_binfo))
1469 continue;
1471 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1472 if (rval)
1473 return rval;
1476 skip_bases:
1477 /* Call the post-order walking function. */
1478 if (post_fn)
1480 rval = post_fn (binfo, data);
1481 gcc_assert (rval != dfs_skip_bases);
1482 return rval;
1485 return NULL_TREE;
1488 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1489 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1490 For diamond shaped hierarchies we must mark the virtual bases, to
1491 avoid multiple walks. */
1493 tree
1494 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1495 tree (*post_fn) (tree, void *), void *data)
1497 static int active = 0; /* We must not be called recursively. */
1498 tree rval;
1500 gcc_assert (pre_fn || post_fn);
1501 gcc_assert (!active);
1502 active++;
1504 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1505 /* We are not diamond shaped, and therefore cannot encounter the
1506 same binfo twice. */
1507 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1508 else
1510 hash_set<tree> pset;
1511 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1514 active--;
1516 return rval;
1519 /* Worker function for dfs_walk_once_accessible. Behaves like
1520 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1521 access given by the current context should be considered, (b) ONCE
1522 indicates whether bases should be marked during traversal. */
1524 static tree
1525 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1526 tree (*pre_fn) (tree, void *),
1527 tree (*post_fn) (tree, void *), void *data)
1529 tree rval = NULL_TREE;
1530 unsigned ix;
1531 tree base_binfo;
1533 /* Call the pre-order walking function. */
1534 if (pre_fn)
1536 rval = pre_fn (binfo, data);
1537 if (rval)
1539 if (rval == dfs_skip_bases)
1540 goto skip_bases;
1542 return rval;
1546 /* Find the next child binfo to walk. */
1547 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1549 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1551 if (mark && pset->contains (base_binfo))
1552 continue;
1554 /* If the base is inherited via private or protected
1555 inheritance, then we can't see it, unless we are a friend of
1556 the current binfo. */
1557 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1559 tree scope;
1560 if (!friends_p)
1561 continue;
1562 scope = current_scope ();
1563 if (!scope
1564 || TREE_CODE (scope) == NAMESPACE_DECL
1565 || !is_friend (BINFO_TYPE (binfo), scope))
1566 continue;
1569 if (mark)
1570 pset->add (base_binfo);
1572 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1573 pre_fn, post_fn, data);
1574 if (rval)
1575 return rval;
1578 skip_bases:
1579 /* Call the post-order walking function. */
1580 if (post_fn)
1582 rval = post_fn (binfo, data);
1583 gcc_assert (rval != dfs_skip_bases);
1584 return rval;
1587 return NULL_TREE;
1590 /* Like dfs_walk_once except that only accessible bases are walked.
1591 FRIENDS_P indicates whether friendship of the local context
1592 should be considered when determining accessibility. */
1594 static tree
1595 dfs_walk_once_accessible (tree binfo, bool friends_p,
1596 tree (*pre_fn) (tree, void *),
1597 tree (*post_fn) (tree, void *), void *data)
1599 hash_set<tree> *pset = NULL;
1600 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1601 pset = new hash_set<tree>;
1602 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1603 pre_fn, post_fn, data);
1605 if (pset)
1606 delete pset;
1607 return rval;
1610 /* Return true iff the code of T is CODE, and it has compatible
1611 type with TYPE. */
1613 static bool
1614 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1616 if (TREE_CODE (t) != code)
1617 return false;
1618 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1619 return false;
1620 return true;
1623 /* Subroutine of direct_accessor_p and reference_accessor_p.
1624 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1625 We expect a tree of the form:
1626 <component_ref:
1627 <indirect_ref:S>
1628 <nop_expr:P*
1629 <parm_decl (this)>
1630 <field_decl (FIELD_DECL)>>>. */
1632 static bool
1633 field_access_p (tree component_ref, tree field_decl, tree field_type)
1635 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
1636 return false;
1638 tree indirect_ref = TREE_OPERAND (component_ref, 0);
1639 if (TREE_CODE (indirect_ref) != INDIRECT_REF)
1640 return false;
1642 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
1643 if (!is_this_parameter (ptr))
1644 return false;
1646 /* Must access the correct field. */
1647 if (TREE_OPERAND (component_ref, 1) != field_decl)
1648 return false;
1649 return true;
1652 /* Subroutine of field_accessor_p.
1654 Assuming that INIT_EXPR has already had its code and type checked,
1655 determine if it is a simple accessor for FIELD_DECL
1656 (of type FIELD_TYPE).
1658 Specifically, a simple accessor within struct S of the form:
1659 T get_field () { return m_field; }
1660 should have a DECL_SAVED_TREE of the form:
1661 <return_expr
1662 <init_expr:T
1663 <result_decl:T
1664 <nop_expr:T
1665 <component_ref:
1666 <indirect_ref:S>
1667 <nop_expr:P*
1668 <parm_decl (this)>
1669 <field_decl (FIELD_DECL)>>>. */
1671 static bool
1672 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1674 tree result_decl = TREE_OPERAND (init_expr, 0);
1675 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1676 return false;
1678 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1679 if (!field_access_p (component_ref, field_decl, field_type))
1680 return false;
1682 return true;
1685 /* Subroutine of field_accessor_p.
1687 Assuming that INIT_EXPR has already had its code and type checked,
1688 determine if it is a "reference" accessor for FIELD_DECL
1689 (of type FIELD_REFERENCE_TYPE).
1691 Specifically, a simple accessor within struct S of the form:
1692 T& get_field () { return m_field; }
1693 should have a DECL_SAVED_TREE of the form:
1694 <return_expr
1695 <init_expr:T&
1696 <result_decl:T&
1697 <nop_expr: T&
1698 <addr_expr: T*
1699 <component_ref:T
1700 <indirect_ref:S
1701 <nop_expr
1702 <parm_decl (this)>>
1703 <field (FIELD_DECL)>>>>>>. */
1704 static bool
1705 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1706 tree field_reference_type)
1708 tree result_decl = TREE_OPERAND (init_expr, 0);
1709 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1710 return false;
1712 tree field_pointer_type = build_pointer_type (field_type);
1713 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1714 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1715 return false;
1717 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1719 if (!field_access_p (component_ref, field_decl, field_type))
1720 return false;
1722 return true;
1725 /* Return true if FN is an accessor method for FIELD_DECL.
1726 i.e. a method of the form { return FIELD; }, with no
1727 conversions.
1729 If CONST_P, then additionally require that FN be a const
1730 method. */
1732 static bool
1733 field_accessor_p (tree fn, tree field_decl, bool const_p)
1735 if (TREE_CODE (fn) != FUNCTION_DECL)
1736 return false;
1738 /* We don't yet support looking up static data, just fields. */
1739 if (TREE_CODE (field_decl) != FIELD_DECL)
1740 return false;
1742 tree fntype = TREE_TYPE (fn);
1743 if (TREE_CODE (fntype) != METHOD_TYPE)
1744 return false;
1746 /* If the field is accessed via a const "this" argument, verify
1747 that the "this" parameter is const. */
1748 if (const_p)
1750 tree this_type = type_of_this_parm (fntype);
1751 if (!TYPE_READONLY (this_type))
1752 return false;
1755 tree saved_tree = DECL_SAVED_TREE (fn);
1757 if (saved_tree == NULL_TREE)
1758 return false;
1760 if (TREE_CODE (saved_tree) != RETURN_EXPR)
1761 return false;
1763 tree init_expr = TREE_OPERAND (saved_tree, 0);
1764 if (TREE_CODE (init_expr) != INIT_EXPR)
1765 return false;
1767 /* Determine if this is a simple accessor within struct S of the form:
1768 T get_field () { return m_field; }. */
1769 tree field_type = TREE_TYPE (field_decl);
1770 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1771 return direct_accessor_p (init_expr, field_decl, field_type);
1773 /* Failing that, determine if it is an accessor of the form:
1774 T& get_field () { return m_field; }. */
1775 tree field_reference_type = cp_build_reference_type (field_type, false);
1776 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1777 return reference_accessor_p (init_expr, field_decl, field_type,
1778 field_reference_type);
1780 return false;
1783 /* Callback data for dfs_locate_field_accessor_pre. */
1785 struct locate_field_data
1787 locate_field_data (tree field_decl_, bool const_p_)
1788 : field_decl (field_decl_), const_p (const_p_) {}
1790 tree field_decl;
1791 bool const_p;
1794 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1795 callable via binfo, if one exists, otherwise return NULL_TREE.
1797 Callback for dfs_walk_once_accessible for use within
1798 locate_field_accessor. */
1800 static tree
1801 dfs_locate_field_accessor_pre (tree binfo, void *data)
1803 locate_field_data *lfd = (locate_field_data *)data;
1804 tree type = BINFO_TYPE (binfo);
1806 vec<tree, va_gc> *member_vec;
1807 tree fn;
1808 size_t i;
1810 if (!CLASS_TYPE_P (type))
1811 return NULL_TREE;
1813 member_vec = CLASSTYPE_MEMBER_VEC (type);
1814 if (!member_vec)
1815 return NULL_TREE;
1817 for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1818 if (fn)
1819 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1820 return fn;
1822 return NULL_TREE;
1825 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1826 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1828 tree
1829 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1831 if (TREE_CODE (basetype_path) != TREE_BINFO)
1832 return NULL_TREE;
1834 /* Walk the hierarchy, looking for a method of some base class that allows
1835 access to the field. */
1836 locate_field_data lfd (field_decl, const_p);
1837 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1838 dfs_locate_field_accessor_pre,
1839 NULL, &lfd);
1842 /* Check that virtual overrider OVERRIDER is acceptable for base function
1843 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1845 static int
1846 check_final_overrider (tree overrider, tree basefn)
1848 tree over_type = TREE_TYPE (overrider);
1849 tree base_type = TREE_TYPE (basefn);
1850 tree over_return = fndecl_declared_return_type (overrider);
1851 tree base_return = fndecl_declared_return_type (basefn);
1852 tree over_throw, base_throw;
1854 int fail = 0;
1856 if (DECL_INVALID_OVERRIDER_P (overrider))
1857 return 0;
1859 if (same_type_p (base_return, over_return))
1860 /* OK */;
1861 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1862 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1863 && POINTER_TYPE_P (base_return)))
1865 /* Potentially covariant. */
1866 unsigned base_quals, over_quals;
1868 fail = !POINTER_TYPE_P (base_return);
1869 if (!fail)
1871 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1873 base_return = TREE_TYPE (base_return);
1874 over_return = TREE_TYPE (over_return);
1876 base_quals = cp_type_quals (base_return);
1877 over_quals = cp_type_quals (over_return);
1879 if ((base_quals & over_quals) != over_quals)
1880 fail = 1;
1882 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1884 /* Strictly speaking, the standard requires the return type to be
1885 complete even if it only differs in cv-quals, but that seems
1886 like a bug in the wording. */
1887 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1888 over_return))
1890 tree binfo = lookup_base (over_return, base_return,
1891 ba_check, NULL, tf_none);
1893 if (!binfo || binfo == error_mark_node)
1894 fail = 1;
1897 else if (can_convert_standard (TREE_TYPE (base_type),
1898 TREE_TYPE (over_type),
1899 tf_warning_or_error))
1900 /* GNU extension, allow trivial pointer conversions such as
1901 converting to void *, or qualification conversion. */
1903 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1904 "invalid covariant return type for %q#D", overrider))
1905 inform (DECL_SOURCE_LOCATION (basefn),
1906 " overriding %q#D", basefn);
1908 else
1909 fail = 2;
1911 else
1912 fail = 2;
1913 if (!fail)
1914 /* OK */;
1915 else
1917 if (fail == 1)
1919 error ("invalid covariant return type for %q+#D", overrider);
1920 error (" overriding %q+#D", basefn);
1922 else
1924 error ("conflicting return type specified for %q+#D", overrider);
1925 error (" overriding %q+#D", basefn);
1927 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1928 return 0;
1931 /* Check throw specifier is at least as strict. */
1932 maybe_instantiate_noexcept (basefn);
1933 maybe_instantiate_noexcept (overrider);
1934 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1935 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1937 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1939 error ("looser throw specifier for %q+#F", overrider);
1940 error (" overriding %q+#F", basefn);
1941 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1942 return 0;
1945 /* Check for conflicting type attributes. But leave transaction_safe for
1946 set_one_vmethod_tm_attributes. */
1947 if (!comp_type_attributes (over_type, base_type)
1948 && !tx_safe_fn_type_p (base_type)
1949 && !tx_safe_fn_type_p (over_type))
1951 error ("conflicting type attributes specified for %q+#D", overrider);
1952 error (" overriding %q+#D", basefn);
1953 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1954 return 0;
1957 /* A function declared transaction_safe_dynamic that overrides a function
1958 declared transaction_safe (but not transaction_safe_dynamic) is
1959 ill-formed. */
1960 if (tx_safe_fn_type_p (base_type)
1961 && lookup_attribute ("transaction_safe_dynamic",
1962 DECL_ATTRIBUTES (overrider))
1963 && !lookup_attribute ("transaction_safe_dynamic",
1964 DECL_ATTRIBUTES (basefn)))
1966 error_at (DECL_SOURCE_LOCATION (overrider),
1967 "%qD declared %<transaction_safe_dynamic%>", overrider);
1968 inform (DECL_SOURCE_LOCATION (basefn),
1969 "overriding %qD declared %<transaction_safe%>", basefn);
1972 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1974 if (DECL_DELETED_FN (overrider))
1976 error ("deleted function %q+D", overrider);
1977 error ("overriding non-deleted function %q+D", basefn);
1978 maybe_explain_implicit_delete (overrider);
1980 else
1982 error ("non-deleted function %q+D", overrider);
1983 error ("overriding deleted function %q+D", basefn);
1985 return 0;
1987 if (DECL_FINAL_P (basefn))
1989 error ("virtual function %q+D", overrider);
1990 error ("overriding final function %q+D", basefn);
1991 return 0;
1993 return 1;
1996 /* Given a class TYPE, and a function decl FNDECL, look for
1997 virtual functions in TYPE's hierarchy which FNDECL overrides.
1998 We do not look in TYPE itself, only its bases.
2000 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2001 find that it overrides anything.
2003 We check that every function which is overridden, is correctly
2004 overridden. */
2007 look_for_overrides (tree type, tree fndecl)
2009 tree binfo = TYPE_BINFO (type);
2010 tree base_binfo;
2011 int ix;
2012 int found = 0;
2014 /* A constructor for a class T does not override a function T
2015 in a base class. */
2016 if (DECL_CONSTRUCTOR_P (fndecl))
2017 return 0;
2019 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2021 tree basetype = BINFO_TYPE (base_binfo);
2023 if (TYPE_POLYMORPHIC_P (basetype))
2024 found += look_for_overrides_r (basetype, fndecl);
2026 return found;
2029 /* Look in TYPE for virtual functions with the same signature as
2030 FNDECL. */
2032 tree
2033 look_for_overrides_here (tree type, tree fndecl)
2035 tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2037 for (ovl_iterator iter (ovl); iter; ++iter)
2039 tree fn = *iter;
2041 if (!DECL_VIRTUAL_P (fn))
2042 /* Not a virtual. */;
2043 else if (DECL_CONTEXT (fn) != type)
2044 /* Introduced with a using declaration. */;
2045 else if (DECL_STATIC_FUNCTION_P (fndecl))
2047 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2048 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2049 if (compparms (TREE_CHAIN (btypes), dtypes))
2050 return fn;
2052 else if (same_signature_p (fndecl, fn))
2053 return fn;
2056 return NULL_TREE;
2059 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2060 TYPE itself and its bases. */
2062 static int
2063 look_for_overrides_r (tree type, tree fndecl)
2065 tree fn = look_for_overrides_here (type, fndecl);
2066 if (fn)
2068 if (DECL_STATIC_FUNCTION_P (fndecl))
2070 /* A static member function cannot match an inherited
2071 virtual member function. */
2072 error ("%q+#D cannot be declared", fndecl);
2073 error (" since %q+#D declared in base class", fn);
2075 else
2077 /* It's definitely virtual, even if not explicitly set. */
2078 DECL_VIRTUAL_P (fndecl) = 1;
2079 check_final_overrider (fndecl, fn);
2081 return 1;
2084 /* We failed to find one declared in this class. Look in its bases. */
2085 return look_for_overrides (type, fndecl);
2088 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2090 static tree
2091 dfs_get_pure_virtuals (tree binfo, void *data)
2093 tree type = (tree) data;
2095 /* We're not interested in primary base classes; the derived class
2096 of which they are a primary base will contain the information we
2097 need. */
2098 if (!BINFO_PRIMARY_P (binfo))
2100 tree virtuals;
2102 for (virtuals = BINFO_VIRTUALS (binfo);
2103 virtuals;
2104 virtuals = TREE_CHAIN (virtuals))
2105 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2106 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2109 return NULL_TREE;
2112 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2114 void
2115 get_pure_virtuals (tree type)
2117 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2118 is going to be overridden. */
2119 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2120 /* Now, run through all the bases which are not primary bases, and
2121 collect the pure virtual functions. We look at the vtable in
2122 each class to determine what pure virtual functions are present.
2123 (A primary base is not interesting because the derived class of
2124 which it is a primary base will contain vtable entries for the
2125 pure virtuals in the base class. */
2126 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2129 /* Debug info for C++ classes can get very large; try to avoid
2130 emitting it everywhere.
2132 Note that this optimization wins even when the target supports
2133 BINCL (if only slightly), and reduces the amount of work for the
2134 linker. */
2136 void
2137 maybe_suppress_debug_info (tree t)
2139 if (write_symbols == NO_DEBUG)
2140 return;
2142 /* We might have set this earlier in cp_finish_decl. */
2143 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2145 /* Always emit the information for each class every time. */
2146 if (flag_emit_class_debug_always)
2147 return;
2149 /* If we already know how we're handling this class, handle debug info
2150 the same way. */
2151 if (CLASSTYPE_INTERFACE_KNOWN (t))
2153 if (CLASSTYPE_INTERFACE_ONLY (t))
2154 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2155 /* else don't set it. */
2157 /* If the class has a vtable, write out the debug info along with
2158 the vtable. */
2159 else if (TYPE_CONTAINS_VPTR_P (t))
2160 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2162 /* Otherwise, just emit the debug info normally. */
2165 /* Note that we want debugging information for a base class of a class
2166 whose vtable is being emitted. Normally, this would happen because
2167 calling the constructor for a derived class implies calling the
2168 constructors for all bases, which involve initializing the
2169 appropriate vptr with the vtable for the base class; but in the
2170 presence of optimization, this initialization may be optimized
2171 away, so we tell finish_vtable_vardecl that we want the debugging
2172 information anyway. */
2174 static tree
2175 dfs_debug_mark (tree binfo, void * /*data*/)
2177 tree t = BINFO_TYPE (binfo);
2179 if (CLASSTYPE_DEBUG_REQUESTED (t))
2180 return dfs_skip_bases;
2182 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2184 return NULL_TREE;
2187 /* Write out the debugging information for TYPE, whose vtable is being
2188 emitted. Also walk through our bases and note that we want to
2189 write out information for them. This avoids the problem of not
2190 writing any debug info for intermediate basetypes whose
2191 constructors, and thus the references to their vtables, and thus
2192 the vtables themselves, were optimized away. */
2194 void
2195 note_debug_info_needed (tree type)
2197 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2199 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2200 rest_of_type_compilation (type, namespace_bindings_p ());
2203 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2206 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2207 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2208 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2209 bases have been encountered already in the tree walk. PARENT_CONVS
2210 is the list of lists of conversion functions that could hide CONV
2211 and OTHER_CONVS is the list of lists of conversion functions that
2212 could hide or be hidden by CONV, should virtualness be involved in
2213 the hierarchy. Merely checking the conversion op's name is not
2214 enough because two conversion operators to the same type can have
2215 different names. Return nonzero if we are visible. */
2217 static int
2218 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2219 tree to_type, tree parent_convs, tree other_convs)
2221 tree level, probe;
2223 /* See if we are hidden by a parent conversion. */
2224 for (level = parent_convs; level; level = TREE_CHAIN (level))
2225 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2226 if (same_type_p (to_type, TREE_TYPE (probe)))
2227 return 0;
2229 if (virtual_depth || virtualness)
2231 /* In a virtual hierarchy, we could be hidden, or could hide a
2232 conversion function on the other_convs list. */
2233 for (level = other_convs; level; level = TREE_CHAIN (level))
2235 int we_hide_them;
2236 int they_hide_us;
2237 tree *prev, other;
2239 if (!(virtual_depth || TREE_STATIC (level)))
2240 /* Neither is morally virtual, so cannot hide each other. */
2241 continue;
2243 if (!TREE_VALUE (level))
2244 /* They evaporated away already. */
2245 continue;
2247 they_hide_us = (virtual_depth
2248 && original_binfo (binfo, TREE_PURPOSE (level)));
2249 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2250 && original_binfo (TREE_PURPOSE (level), binfo));
2252 if (!(we_hide_them || they_hide_us))
2253 /* Neither is within the other, so no hiding can occur. */
2254 continue;
2256 for (prev = &TREE_VALUE (level), other = *prev; other;)
2258 if (same_type_p (to_type, TREE_TYPE (other)))
2260 if (they_hide_us)
2261 /* We are hidden. */
2262 return 0;
2264 if (we_hide_them)
2266 /* We hide the other one. */
2267 other = TREE_CHAIN (other);
2268 *prev = other;
2269 continue;
2272 prev = &TREE_CHAIN (other);
2273 other = *prev;
2277 return 1;
2280 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2281 of conversion functions, the first slot will be for the current
2282 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2283 of conversion functions from children of the current binfo,
2284 concatenated with conversions from elsewhere in the hierarchy --
2285 that list begins with OTHER_CONVS. Return a single list of lists
2286 containing only conversions from the current binfo and its
2287 children. */
2289 static tree
2290 split_conversions (tree my_convs, tree parent_convs,
2291 tree child_convs, tree other_convs)
2293 tree t;
2294 tree prev;
2296 /* Remove the original other_convs portion from child_convs. */
2297 for (prev = NULL, t = child_convs;
2298 t != other_convs; prev = t, t = TREE_CHAIN (t))
2299 continue;
2301 if (prev)
2302 TREE_CHAIN (prev) = NULL_TREE;
2303 else
2304 child_convs = NULL_TREE;
2306 /* Attach the child convs to any we had at this level. */
2307 if (my_convs)
2309 my_convs = parent_convs;
2310 TREE_CHAIN (my_convs) = child_convs;
2312 else
2313 my_convs = child_convs;
2315 return my_convs;
2318 /* Worker for lookup_conversions. Lookup conversion functions in
2319 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2320 morally virtual base, and VIRTUALNESS is nonzero, if we've
2321 encountered virtual bases already in the tree walk. PARENT_CONVS
2322 is a list of conversions within parent binfos. OTHER_CONVS are
2323 conversions found elsewhere in the tree. Return the conversions
2324 found within this portion of the graph in CONVS. Return nonzero if
2325 we encountered virtualness. We keep template and non-template
2326 conversions separate, to avoid unnecessary type comparisons.
2328 The located conversion functions are held in lists of lists. The
2329 TREE_VALUE of the outer list is the list of conversion functions
2330 found in a particular binfo. The TREE_PURPOSE of both the outer
2331 and inner lists is the binfo at which those conversions were
2332 found. TREE_STATIC is set for those lists within of morally
2333 virtual binfos. The TREE_VALUE of the inner list is the conversion
2334 function or overload itself. The TREE_TYPE of each inner list node
2335 is the converted-to type. */
2337 static int
2338 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2339 tree parent_convs, tree other_convs, tree *convs)
2341 int my_virtualness = 0;
2342 tree my_convs = NULL_TREE;
2343 tree child_convs = NULL_TREE;
2345 /* If we have no conversion operators, then don't look. */
2346 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2348 *convs = NULL_TREE;
2350 return 0;
2353 if (BINFO_VIRTUAL_P (binfo))
2354 virtual_depth++;
2356 /* First, locate the unhidden ones at this level. */
2357 if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2358 for (ovl_iterator iter (conv); iter; ++iter)
2360 tree fn = *iter;
2361 tree type = DECL_CONV_FN_TYPE (fn);
2363 if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2365 mark_used (fn);
2366 type = DECL_CONV_FN_TYPE (fn);
2369 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2370 type, parent_convs, other_convs))
2372 my_convs = tree_cons (binfo, fn, my_convs);
2373 TREE_TYPE (my_convs) = type;
2374 if (virtual_depth)
2376 TREE_STATIC (my_convs) = 1;
2377 my_virtualness = 1;
2382 if (my_convs)
2384 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2385 if (virtual_depth)
2386 TREE_STATIC (parent_convs) = 1;
2389 child_convs = other_convs;
2391 /* Now iterate over each base, looking for more conversions. */
2392 unsigned i;
2393 tree base_binfo;
2394 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2396 tree base_convs;
2397 unsigned base_virtualness;
2399 base_virtualness = lookup_conversions_r (base_binfo,
2400 virtual_depth, virtualness,
2401 parent_convs, child_convs,
2402 &base_convs);
2403 if (base_virtualness)
2404 my_virtualness = virtualness = 1;
2405 child_convs = chainon (base_convs, child_convs);
2408 *convs = split_conversions (my_convs, parent_convs,
2409 child_convs, other_convs);
2411 return my_virtualness;
2414 /* Return a TREE_LIST containing all the non-hidden user-defined
2415 conversion functions for TYPE (and its base-classes). The
2416 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2417 function. The TREE_PURPOSE is the BINFO from which the conversion
2418 functions in this node were selected. This function is effectively
2419 performing a set of member lookups as lookup_fnfield does, but
2420 using the type being converted to as the unique key, rather than the
2421 field name. */
2423 tree
2424 lookup_conversions (tree type)
2426 tree convs;
2428 complete_type (type);
2429 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2430 return NULL_TREE;
2432 lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2434 tree list = NULL_TREE;
2436 /* Flatten the list-of-lists */
2437 for (; convs; convs = TREE_CHAIN (convs))
2439 tree probe, next;
2441 for (probe = TREE_VALUE (convs); probe; probe = next)
2443 next = TREE_CHAIN (probe);
2445 TREE_CHAIN (probe) = list;
2446 list = probe;
2450 return list;
2453 /* Returns the binfo of the first direct or indirect virtual base derived
2454 from BINFO, or NULL if binfo is not via virtual. */
2456 tree
2457 binfo_from_vbase (tree binfo)
2459 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2461 if (BINFO_VIRTUAL_P (binfo))
2462 return binfo;
2464 return NULL_TREE;
2467 /* Returns the binfo of the first direct or indirect virtual base derived
2468 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2469 via virtual. */
2471 tree
2472 binfo_via_virtual (tree binfo, tree limit)
2474 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2475 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2476 return NULL_TREE;
2478 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2479 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2481 if (BINFO_VIRTUAL_P (binfo))
2482 return binfo;
2484 return NULL_TREE;
2487 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2488 direct base. */
2490 bool
2491 binfo_direct_p (tree binfo)
2493 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2494 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2495 /* A second inheritance chain means indirect. */
2496 return false;
2497 if (!BINFO_VIRTUAL_P (binfo))
2498 /* Non-virtual, so only one inheritance chain means direct. */
2499 return true;
2500 /* A virtual base looks like a direct base, so we need to look through the
2501 direct bases to see if it's there. */
2502 tree b_binfo;
2503 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2504 if (b_binfo == binfo)
2505 return true;
2506 return false;
2509 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2510 Find the equivalent binfo within whatever graph HERE is located.
2511 This is the inverse of original_binfo. */
2513 tree
2514 copied_binfo (tree binfo, tree here)
2516 tree result = NULL_TREE;
2518 if (BINFO_VIRTUAL_P (binfo))
2520 tree t;
2522 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2523 t = BINFO_INHERITANCE_CHAIN (t))
2524 continue;
2526 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2528 else if (BINFO_INHERITANCE_CHAIN (binfo))
2530 tree cbinfo;
2531 tree base_binfo;
2532 int ix;
2534 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2535 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2536 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2538 result = base_binfo;
2539 break;
2542 else
2544 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2545 result = here;
2548 gcc_assert (result);
2549 return result;
2552 tree
2553 binfo_for_vbase (tree base, tree t)
2555 unsigned ix;
2556 tree binfo;
2557 vec<tree, va_gc> *vbases;
2559 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2560 vec_safe_iterate (vbases, ix, &binfo); ix++)
2561 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2562 return binfo;
2563 return NULL;
2566 /* BINFO is some base binfo of HERE, within some other
2567 hierarchy. Return the equivalent binfo, but in the hierarchy
2568 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2569 is not a base binfo of HERE, returns NULL_TREE. */
2571 tree
2572 original_binfo (tree binfo, tree here)
2574 tree result = NULL;
2576 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2577 result = here;
2578 else if (BINFO_VIRTUAL_P (binfo))
2579 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2580 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2581 : NULL_TREE);
2582 else if (BINFO_INHERITANCE_CHAIN (binfo))
2584 tree base_binfos;
2586 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2587 if (base_binfos)
2589 int ix;
2590 tree base_binfo;
2592 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2593 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2594 BINFO_TYPE (binfo)))
2596 result = base_binfo;
2597 break;
2602 return result;
2605 /* True iff TYPE has any dependent bases (and therefore we can't say
2606 definitively that another class is not a base of an instantiation of
2607 TYPE). */
2609 bool
2610 any_dependent_bases_p (tree type)
2612 if (!type || !CLASS_TYPE_P (type) || !processing_template_decl)
2613 return false;
2615 unsigned i;
2616 tree base_binfo;
2617 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2618 if (BINFO_DEPENDENT_BASE_P (base_binfo))
2619 return true;
2621 return false;