Daily bump.
[official-gcc.git] / gcc / cp / search.c
blobbfeaf2cc81968ca2e461ad7ce00de155a51bbad9
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 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 (!INDIRECT_REF_P (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 constexpr_fn_retval (saved_tree) of the form:
1661 <init_expr:T
1662 <result_decl:T
1663 <nop_expr:T
1664 <component_ref:
1665 <indirect_ref:S>
1666 <nop_expr:P*
1667 <parm_decl (this)>
1668 <field_decl (FIELD_DECL)>>>>>. */
1670 static bool
1671 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1673 tree result_decl = TREE_OPERAND (init_expr, 0);
1674 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1675 return false;
1677 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1678 if (!field_access_p (component_ref, field_decl, field_type))
1679 return false;
1681 return true;
1684 /* Subroutine of field_accessor_p.
1686 Assuming that INIT_EXPR has already had its code and type checked,
1687 determine if it is a "reference" accessor for FIELD_DECL
1688 (of type FIELD_REFERENCE_TYPE).
1690 Specifically, a simple accessor within struct S of the form:
1691 T& get_field () { return m_field; }
1692 should have a constexpr_fn_retval (saved_tree) of the form:
1693 <init_expr:T&
1694 <result_decl:T&
1695 <nop_expr: T&
1696 <addr_expr: T*
1697 <component_ref:T
1698 <indirect_ref:S
1699 <nop_expr
1700 <parm_decl (this)>>
1701 <field (FIELD_DECL)>>>>>>. */
1702 static bool
1703 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1704 tree field_reference_type)
1706 tree result_decl = TREE_OPERAND (init_expr, 0);
1707 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1708 return false;
1710 tree field_pointer_type = build_pointer_type (field_type);
1711 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1712 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1713 return false;
1715 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1717 if (!field_access_p (component_ref, field_decl, field_type))
1718 return false;
1720 return true;
1723 /* Return true if FN is an accessor method for FIELD_DECL.
1724 i.e. a method of the form { return FIELD; }, with no
1725 conversions.
1727 If CONST_P, then additionally require that FN be a const
1728 method. */
1730 static bool
1731 field_accessor_p (tree fn, tree field_decl, bool const_p)
1733 if (TREE_CODE (fn) != FUNCTION_DECL)
1734 return false;
1736 /* We don't yet support looking up static data, just fields. */
1737 if (TREE_CODE (field_decl) != FIELD_DECL)
1738 return false;
1740 tree fntype = TREE_TYPE (fn);
1741 if (TREE_CODE (fntype) != METHOD_TYPE)
1742 return false;
1744 /* If the field is accessed via a const "this" argument, verify
1745 that the "this" parameter is const. */
1746 if (const_p)
1748 tree this_class = class_of_this_parm (fntype);
1749 if (!TYPE_READONLY (this_class))
1750 return false;
1753 tree saved_tree = DECL_SAVED_TREE (fn);
1755 if (saved_tree == NULL_TREE)
1756 return false;
1758 /* Attempt to extract a single return value from the function,
1759 if it has one. */
1760 tree retval = constexpr_fn_retval (saved_tree);
1761 if (retval == NULL_TREE || retval == error_mark_node)
1762 return false;
1763 /* Require an INIT_EXPR. */
1764 if (TREE_CODE (retval) != INIT_EXPR)
1765 return false;
1766 tree init_expr = retval;
1768 /* Determine if this is a simple accessor within struct S of the form:
1769 T get_field () { return m_field; }. */
1770 tree field_type = TREE_TYPE (field_decl);
1771 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1772 return direct_accessor_p (init_expr, field_decl, field_type);
1774 /* Failing that, determine if it is an accessor of the form:
1775 T& get_field () { return m_field; }. */
1776 tree field_reference_type = cp_build_reference_type (field_type, false);
1777 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1778 return reference_accessor_p (init_expr, field_decl, field_type,
1779 field_reference_type);
1781 return false;
1784 /* Callback data for dfs_locate_field_accessor_pre. */
1786 struct locate_field_data
1788 locate_field_data (tree field_decl_, bool const_p_)
1789 : field_decl (field_decl_), const_p (const_p_) {}
1791 tree field_decl;
1792 bool const_p;
1795 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1796 callable via binfo, if one exists, otherwise return NULL_TREE.
1798 Callback for dfs_walk_once_accessible for use within
1799 locate_field_accessor. */
1801 static tree
1802 dfs_locate_field_accessor_pre (tree binfo, void *data)
1804 locate_field_data *lfd = (locate_field_data *)data;
1805 tree type = BINFO_TYPE (binfo);
1807 vec<tree, va_gc> *member_vec;
1808 tree fn;
1809 size_t i;
1811 if (!CLASS_TYPE_P (type))
1812 return NULL_TREE;
1814 member_vec = CLASSTYPE_MEMBER_VEC (type);
1815 if (!member_vec)
1816 return NULL_TREE;
1818 for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1819 if (fn)
1820 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1821 return fn;
1823 return NULL_TREE;
1826 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1827 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1829 tree
1830 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1832 if (TREE_CODE (basetype_path) != TREE_BINFO)
1833 return NULL_TREE;
1835 /* Walk the hierarchy, looking for a method of some base class that allows
1836 access to the field. */
1837 locate_field_data lfd (field_decl, const_p);
1838 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1839 dfs_locate_field_accessor_pre,
1840 NULL, &lfd);
1843 /* Check that virtual overrider OVERRIDER is acceptable for base function
1844 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1846 static int
1847 check_final_overrider (tree overrider, tree basefn)
1849 tree over_type = TREE_TYPE (overrider);
1850 tree base_type = TREE_TYPE (basefn);
1851 tree over_return = fndecl_declared_return_type (overrider);
1852 tree base_return = fndecl_declared_return_type (basefn);
1853 tree over_throw, base_throw;
1855 int fail = 0;
1857 if (DECL_INVALID_OVERRIDER_P (overrider))
1858 return 0;
1860 if (same_type_p (base_return, over_return))
1861 /* OK */;
1862 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1863 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1864 && POINTER_TYPE_P (base_return)))
1866 /* Potentially covariant. */
1867 unsigned base_quals, over_quals;
1869 fail = !POINTER_TYPE_P (base_return);
1870 if (!fail)
1872 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1874 base_return = TREE_TYPE (base_return);
1875 over_return = TREE_TYPE (over_return);
1877 base_quals = cp_type_quals (base_return);
1878 over_quals = cp_type_quals (over_return);
1880 if ((base_quals & over_quals) != over_quals)
1881 fail = 1;
1883 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1885 /* Strictly speaking, the standard requires the return type to be
1886 complete even if it only differs in cv-quals, but that seems
1887 like a bug in the wording. */
1888 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1889 over_return))
1891 tree binfo = lookup_base (over_return, base_return,
1892 ba_check, NULL, tf_none);
1894 if (!binfo || binfo == error_mark_node)
1895 fail = 1;
1898 else if (can_convert_standard (TREE_TYPE (base_type),
1899 TREE_TYPE (over_type),
1900 tf_warning_or_error))
1901 /* GNU extension, allow trivial pointer conversions such as
1902 converting to void *, or qualification conversion. */
1904 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1905 "invalid covariant return type for %q#D", overrider))
1906 inform (DECL_SOURCE_LOCATION (basefn),
1907 "overridden function is %q#D", basefn);
1909 else
1910 fail = 2;
1912 else
1913 fail = 2;
1914 if (!fail)
1915 /* OK */;
1916 else
1918 if (fail == 1)
1920 error ("invalid covariant return type for %q+#D", overrider);
1921 inform (DECL_SOURCE_LOCATION (basefn),
1922 "overridden function is %q#D", basefn);
1924 else
1926 error ("conflicting return type specified for %q+#D", overrider);
1927 inform (DECL_SOURCE_LOCATION (basefn),
1928 "overridden function is %q#D", basefn);
1930 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1931 return 0;
1934 /* Check throw specifier is at least as strict. */
1935 maybe_instantiate_noexcept (basefn);
1936 maybe_instantiate_noexcept (overrider);
1937 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1938 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1940 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1942 error ("looser throw specifier for %q+#F", overrider);
1943 inform (DECL_SOURCE_LOCATION (basefn),
1944 "overridden function is %q#F", basefn);
1945 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1946 return 0;
1949 /* Check for conflicting type attributes. But leave transaction_safe for
1950 set_one_vmethod_tm_attributes. */
1951 if (!comp_type_attributes (over_type, base_type)
1952 && !tx_safe_fn_type_p (base_type)
1953 && !tx_safe_fn_type_p (over_type))
1955 error ("conflicting type attributes specified for %q+#D", overrider);
1956 inform (DECL_SOURCE_LOCATION (basefn),
1957 "overridden function is %q#D", basefn);
1958 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1959 return 0;
1962 /* A function declared transaction_safe_dynamic that overrides a function
1963 declared transaction_safe (but not transaction_safe_dynamic) is
1964 ill-formed. */
1965 if (tx_safe_fn_type_p (base_type)
1966 && lookup_attribute ("transaction_safe_dynamic",
1967 DECL_ATTRIBUTES (overrider))
1968 && !lookup_attribute ("transaction_safe_dynamic",
1969 DECL_ATTRIBUTES (basefn)))
1971 error_at (DECL_SOURCE_LOCATION (overrider),
1972 "%qD declared %<transaction_safe_dynamic%>", overrider);
1973 inform (DECL_SOURCE_LOCATION (basefn),
1974 "overriding %qD declared %<transaction_safe%>", basefn);
1977 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1979 if (DECL_DELETED_FN (overrider))
1981 error ("deleted function %q+D overriding non-deleted function",
1982 overrider);
1983 inform (DECL_SOURCE_LOCATION (basefn),
1984 "overridden function is %qD", basefn);
1985 maybe_explain_implicit_delete (overrider);
1987 else
1989 error ("non-deleted function %q+D overriding deleted function",
1990 overrider);
1991 inform (DECL_SOURCE_LOCATION (basefn),
1992 "overridden function is %qD", basefn);
1994 return 0;
1996 if (DECL_FINAL_P (basefn))
1998 error ("virtual function %q+D overriding final function", overrider);
1999 inform (DECL_SOURCE_LOCATION (basefn),
2000 "overridden function is %qD", basefn);
2001 return 0;
2003 return 1;
2006 /* Given a class TYPE, and a function decl FNDECL, look for
2007 virtual functions in TYPE's hierarchy which FNDECL overrides.
2008 We do not look in TYPE itself, only its bases.
2010 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2011 find that it overrides anything.
2013 We check that every function which is overridden, is correctly
2014 overridden. */
2017 look_for_overrides (tree type, tree fndecl)
2019 tree binfo = TYPE_BINFO (type);
2020 tree base_binfo;
2021 int ix;
2022 int found = 0;
2024 /* A constructor for a class T does not override a function T
2025 in a base class. */
2026 if (DECL_CONSTRUCTOR_P (fndecl))
2027 return 0;
2029 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2031 tree basetype = BINFO_TYPE (base_binfo);
2033 if (TYPE_POLYMORPHIC_P (basetype))
2034 found += look_for_overrides_r (basetype, fndecl);
2036 return found;
2039 /* Look in TYPE for virtual functions with the same signature as
2040 FNDECL. */
2042 tree
2043 look_for_overrides_here (tree type, tree fndecl)
2045 tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2047 for (ovl_iterator iter (ovl); iter; ++iter)
2049 tree fn = *iter;
2051 if (!DECL_VIRTUAL_P (fn))
2052 /* Not a virtual. */;
2053 else if (DECL_CONTEXT (fn) != type)
2054 /* Introduced with a using declaration. */;
2055 else if (DECL_STATIC_FUNCTION_P (fndecl))
2057 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2058 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2059 if (compparms (TREE_CHAIN (btypes), dtypes))
2060 return fn;
2062 else if (same_signature_p (fndecl, fn))
2063 return fn;
2066 return NULL_TREE;
2069 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2070 TYPE itself and its bases. */
2072 static int
2073 look_for_overrides_r (tree type, tree fndecl)
2075 tree fn = look_for_overrides_here (type, fndecl);
2076 if (fn)
2078 if (DECL_STATIC_FUNCTION_P (fndecl))
2080 /* A static member function cannot match an inherited
2081 virtual member function. */
2082 error ("%q+#D cannot be declared", fndecl);
2083 error (" since %q+#D declared in base class", fn);
2085 else
2087 /* It's definitely virtual, even if not explicitly set. */
2088 DECL_VIRTUAL_P (fndecl) = 1;
2089 check_final_overrider (fndecl, fn);
2091 return 1;
2094 /* We failed to find one declared in this class. Look in its bases. */
2095 return look_for_overrides (type, fndecl);
2098 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2100 static tree
2101 dfs_get_pure_virtuals (tree binfo, void *data)
2103 tree type = (tree) data;
2105 /* We're not interested in primary base classes; the derived class
2106 of which they are a primary base will contain the information we
2107 need. */
2108 if (!BINFO_PRIMARY_P (binfo))
2110 tree virtuals;
2112 for (virtuals = BINFO_VIRTUALS (binfo);
2113 virtuals;
2114 virtuals = TREE_CHAIN (virtuals))
2115 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2116 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2119 return NULL_TREE;
2122 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2124 void
2125 get_pure_virtuals (tree type)
2127 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2128 is going to be overridden. */
2129 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2130 /* Now, run through all the bases which are not primary bases, and
2131 collect the pure virtual functions. We look at the vtable in
2132 each class to determine what pure virtual functions are present.
2133 (A primary base is not interesting because the derived class of
2134 which it is a primary base will contain vtable entries for the
2135 pure virtuals in the base class. */
2136 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2139 /* Debug info for C++ classes can get very large; try to avoid
2140 emitting it everywhere.
2142 Note that this optimization wins even when the target supports
2143 BINCL (if only slightly), and reduces the amount of work for the
2144 linker. */
2146 void
2147 maybe_suppress_debug_info (tree t)
2149 if (write_symbols == NO_DEBUG)
2150 return;
2152 /* We might have set this earlier in cp_finish_decl. */
2153 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2155 /* Always emit the information for each class every time. */
2156 if (flag_emit_class_debug_always)
2157 return;
2159 /* If we already know how we're handling this class, handle debug info
2160 the same way. */
2161 if (CLASSTYPE_INTERFACE_KNOWN (t))
2163 if (CLASSTYPE_INTERFACE_ONLY (t))
2164 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2165 /* else don't set it. */
2167 /* If the class has a vtable, write out the debug info along with
2168 the vtable. */
2169 else if (TYPE_CONTAINS_VPTR_P (t))
2170 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2172 /* Otherwise, just emit the debug info normally. */
2175 /* Note that we want debugging information for a base class of a class
2176 whose vtable is being emitted. Normally, this would happen because
2177 calling the constructor for a derived class implies calling the
2178 constructors for all bases, which involve initializing the
2179 appropriate vptr with the vtable for the base class; but in the
2180 presence of optimization, this initialization may be optimized
2181 away, so we tell finish_vtable_vardecl that we want the debugging
2182 information anyway. */
2184 static tree
2185 dfs_debug_mark (tree binfo, void * /*data*/)
2187 tree t = BINFO_TYPE (binfo);
2189 if (CLASSTYPE_DEBUG_REQUESTED (t))
2190 return dfs_skip_bases;
2192 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2194 return NULL_TREE;
2197 /* Write out the debugging information for TYPE, whose vtable is being
2198 emitted. Also walk through our bases and note that we want to
2199 write out information for them. This avoids the problem of not
2200 writing any debug info for intermediate basetypes whose
2201 constructors, and thus the references to their vtables, and thus
2202 the vtables themselves, were optimized away. */
2204 void
2205 note_debug_info_needed (tree type)
2207 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2209 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2210 rest_of_type_compilation (type, namespace_bindings_p ());
2213 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2216 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2217 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2218 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2219 bases have been encountered already in the tree walk. PARENT_CONVS
2220 is the list of lists of conversion functions that could hide CONV
2221 and OTHER_CONVS is the list of lists of conversion functions that
2222 could hide or be hidden by CONV, should virtualness be involved in
2223 the hierarchy. Merely checking the conversion op's name is not
2224 enough because two conversion operators to the same type can have
2225 different names. Return nonzero if we are visible. */
2227 static int
2228 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2229 tree to_type, tree parent_convs, tree other_convs)
2231 tree level, probe;
2233 /* See if we are hidden by a parent conversion. */
2234 for (level = parent_convs; level; level = TREE_CHAIN (level))
2235 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2236 if (same_type_p (to_type, TREE_TYPE (probe)))
2237 return 0;
2239 if (virtual_depth || virtualness)
2241 /* In a virtual hierarchy, we could be hidden, or could hide a
2242 conversion function on the other_convs list. */
2243 for (level = other_convs; level; level = TREE_CHAIN (level))
2245 int we_hide_them;
2246 int they_hide_us;
2247 tree *prev, other;
2249 if (!(virtual_depth || TREE_STATIC (level)))
2250 /* Neither is morally virtual, so cannot hide each other. */
2251 continue;
2253 if (!TREE_VALUE (level))
2254 /* They evaporated away already. */
2255 continue;
2257 they_hide_us = (virtual_depth
2258 && original_binfo (binfo, TREE_PURPOSE (level)));
2259 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2260 && original_binfo (TREE_PURPOSE (level), binfo));
2262 if (!(we_hide_them || they_hide_us))
2263 /* Neither is within the other, so no hiding can occur. */
2264 continue;
2266 for (prev = &TREE_VALUE (level), other = *prev; other;)
2268 if (same_type_p (to_type, TREE_TYPE (other)))
2270 if (they_hide_us)
2271 /* We are hidden. */
2272 return 0;
2274 if (we_hide_them)
2276 /* We hide the other one. */
2277 other = TREE_CHAIN (other);
2278 *prev = other;
2279 continue;
2282 prev = &TREE_CHAIN (other);
2283 other = *prev;
2287 return 1;
2290 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2291 of conversion functions, the first slot will be for the current
2292 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2293 of conversion functions from children of the current binfo,
2294 concatenated with conversions from elsewhere in the hierarchy --
2295 that list begins with OTHER_CONVS. Return a single list of lists
2296 containing only conversions from the current binfo and its
2297 children. */
2299 static tree
2300 split_conversions (tree my_convs, tree parent_convs,
2301 tree child_convs, tree other_convs)
2303 tree t;
2304 tree prev;
2306 /* Remove the original other_convs portion from child_convs. */
2307 for (prev = NULL, t = child_convs;
2308 t != other_convs; prev = t, t = TREE_CHAIN (t))
2309 continue;
2311 if (prev)
2312 TREE_CHAIN (prev) = NULL_TREE;
2313 else
2314 child_convs = NULL_TREE;
2316 /* Attach the child convs to any we had at this level. */
2317 if (my_convs)
2319 my_convs = parent_convs;
2320 TREE_CHAIN (my_convs) = child_convs;
2322 else
2323 my_convs = child_convs;
2325 return my_convs;
2328 /* Worker for lookup_conversions. Lookup conversion functions in
2329 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2330 morally virtual base, and VIRTUALNESS is nonzero, if we've
2331 encountered virtual bases already in the tree walk. PARENT_CONVS
2332 is a list of conversions within parent binfos. OTHER_CONVS are
2333 conversions found elsewhere in the tree. Return the conversions
2334 found within this portion of the graph in CONVS. Return nonzero if
2335 we encountered virtualness. We keep template and non-template
2336 conversions separate, to avoid unnecessary type comparisons.
2338 The located conversion functions are held in lists of lists. The
2339 TREE_VALUE of the outer list is the list of conversion functions
2340 found in a particular binfo. The TREE_PURPOSE of both the outer
2341 and inner lists is the binfo at which those conversions were
2342 found. TREE_STATIC is set for those lists within of morally
2343 virtual binfos. The TREE_VALUE of the inner list is the conversion
2344 function or overload itself. The TREE_TYPE of each inner list node
2345 is the converted-to type. */
2347 static int
2348 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2349 tree parent_convs, tree other_convs, tree *convs)
2351 int my_virtualness = 0;
2352 tree my_convs = NULL_TREE;
2353 tree child_convs = NULL_TREE;
2355 /* If we have no conversion operators, then don't look. */
2356 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2358 *convs = NULL_TREE;
2360 return 0;
2363 if (BINFO_VIRTUAL_P (binfo))
2364 virtual_depth++;
2366 /* First, locate the unhidden ones at this level. */
2367 if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2368 for (ovl_iterator iter (conv); iter; ++iter)
2370 tree fn = *iter;
2371 tree type = DECL_CONV_FN_TYPE (fn);
2373 if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2375 mark_used (fn);
2376 type = DECL_CONV_FN_TYPE (fn);
2379 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2380 type, parent_convs, other_convs))
2382 my_convs = tree_cons (binfo, fn, my_convs);
2383 TREE_TYPE (my_convs) = type;
2384 if (virtual_depth)
2386 TREE_STATIC (my_convs) = 1;
2387 my_virtualness = 1;
2392 if (my_convs)
2394 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2395 if (virtual_depth)
2396 TREE_STATIC (parent_convs) = 1;
2399 child_convs = other_convs;
2401 /* Now iterate over each base, looking for more conversions. */
2402 unsigned i;
2403 tree base_binfo;
2404 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2406 tree base_convs;
2407 unsigned base_virtualness;
2409 base_virtualness = lookup_conversions_r (base_binfo,
2410 virtual_depth, virtualness,
2411 parent_convs, child_convs,
2412 &base_convs);
2413 if (base_virtualness)
2414 my_virtualness = virtualness = 1;
2415 child_convs = chainon (base_convs, child_convs);
2418 *convs = split_conversions (my_convs, parent_convs,
2419 child_convs, other_convs);
2421 return my_virtualness;
2424 /* Return a TREE_LIST containing all the non-hidden user-defined
2425 conversion functions for TYPE (and its base-classes). The
2426 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2427 function. The TREE_PURPOSE is the BINFO from which the conversion
2428 functions in this node were selected. This function is effectively
2429 performing a set of member lookups as lookup_fnfield does, but
2430 using the type being converted to as the unique key, rather than the
2431 field name. */
2433 tree
2434 lookup_conversions (tree type)
2436 tree convs;
2438 complete_type (type);
2439 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2440 return NULL_TREE;
2442 lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2444 tree list = NULL_TREE;
2446 /* Flatten the list-of-lists */
2447 for (; convs; convs = TREE_CHAIN (convs))
2449 tree probe, next;
2451 for (probe = TREE_VALUE (convs); probe; probe = next)
2453 next = TREE_CHAIN (probe);
2455 TREE_CHAIN (probe) = list;
2456 list = probe;
2460 return list;
2463 /* Returns the binfo of the first direct or indirect virtual base derived
2464 from BINFO, or NULL if binfo is not via virtual. */
2466 tree
2467 binfo_from_vbase (tree binfo)
2469 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2471 if (BINFO_VIRTUAL_P (binfo))
2472 return binfo;
2474 return NULL_TREE;
2477 /* Returns the binfo of the first direct or indirect virtual base derived
2478 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2479 via virtual. */
2481 tree
2482 binfo_via_virtual (tree binfo, tree limit)
2484 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2485 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2486 return NULL_TREE;
2488 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2489 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2491 if (BINFO_VIRTUAL_P (binfo))
2492 return binfo;
2494 return NULL_TREE;
2497 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2498 direct base. */
2500 bool
2501 binfo_direct_p (tree binfo)
2503 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2504 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2505 /* A second inheritance chain means indirect. */
2506 return false;
2507 if (!BINFO_VIRTUAL_P (binfo))
2508 /* Non-virtual, so only one inheritance chain means direct. */
2509 return true;
2510 /* A virtual base looks like a direct base, so we need to look through the
2511 direct bases to see if it's there. */
2512 tree b_binfo;
2513 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2514 if (b_binfo == binfo)
2515 return true;
2516 return false;
2519 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2520 Find the equivalent binfo within whatever graph HERE is located.
2521 This is the inverse of original_binfo. */
2523 tree
2524 copied_binfo (tree binfo, tree here)
2526 tree result = NULL_TREE;
2528 if (BINFO_VIRTUAL_P (binfo))
2530 tree t;
2532 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2533 t = BINFO_INHERITANCE_CHAIN (t))
2534 continue;
2536 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2538 else if (BINFO_INHERITANCE_CHAIN (binfo))
2540 tree cbinfo;
2541 tree base_binfo;
2542 int ix;
2544 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2545 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2546 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2548 result = base_binfo;
2549 break;
2552 else
2554 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2555 result = here;
2558 gcc_assert (result);
2559 return result;
2562 tree
2563 binfo_for_vbase (tree base, tree t)
2565 unsigned ix;
2566 tree binfo;
2567 vec<tree, va_gc> *vbases;
2569 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2570 vec_safe_iterate (vbases, ix, &binfo); ix++)
2571 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2572 return binfo;
2573 return NULL;
2576 /* BINFO is some base binfo of HERE, within some other
2577 hierarchy. Return the equivalent binfo, but in the hierarchy
2578 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2579 is not a base binfo of HERE, returns NULL_TREE. */
2581 tree
2582 original_binfo (tree binfo, tree here)
2584 tree result = NULL;
2586 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2587 result = here;
2588 else if (BINFO_VIRTUAL_P (binfo))
2589 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2590 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2591 : NULL_TREE);
2592 else if (BINFO_INHERITANCE_CHAIN (binfo))
2594 tree base_binfos;
2596 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2597 if (base_binfos)
2599 int ix;
2600 tree base_binfo;
2602 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2603 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2604 BINFO_TYPE (binfo)))
2606 result = base_binfo;
2607 break;
2612 return result;
2615 /* True iff TYPE has any dependent bases (and therefore we can't say
2616 definitively that another class is not a base of an instantiation of
2617 TYPE). */
2619 bool
2620 any_dependent_bases_p (tree type)
2622 if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
2623 return false;
2625 /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2626 Return false because in this situation we aren't actually looking up names
2627 in the scope of the class, so it doesn't matter whether it has dependent
2628 bases. */
2629 if (!TYPE_BINFO (type))
2630 return false;
2632 unsigned i;
2633 tree base_binfo;
2634 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2635 if (BINFO_DEPENDENT_BASE_P (base_binfo))
2636 return true;
2638 return false;