* tree-loop-distribution.c (params.h): Include header file.
[official-gcc.git] / gcc / cp / search.c
blobcd06e529fb94d79864f3c59e74d9c13fce0d9fac
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"
32 static int is_subobject_of_p (tree, tree);
33 static tree dfs_lookup_base (tree, void *);
34 static tree dfs_dcast_hint_pre (tree, void *);
35 static tree dfs_dcast_hint_post (tree, void *);
36 static tree dfs_debug_mark (tree, void *);
37 static int check_hidden_convs (tree, int, int, tree, tree, tree);
38 static tree split_conversions (tree, tree, tree, tree);
39 static int lookup_conversions_r (tree, int, int,
40 tree, tree, tree, tree, tree *, tree *);
41 static int look_for_overrides_r (tree, tree);
42 static tree lookup_field_r (tree, void *);
43 static tree dfs_accessible_post (tree, void *);
44 static tree dfs_walk_once_accessible (tree, bool,
45 tree (*pre_fn) (tree, void *),
46 tree (*post_fn) (tree, void *),
47 void *data);
48 static tree dfs_access_in_type (tree, void *);
49 static access_kind access_in_type (tree, tree);
50 static tree dfs_get_pure_virtuals (tree, void *);
53 /* Variables for gathering statistics. */
54 static int n_fields_searched;
55 static int n_calls_lookup_field, n_calls_lookup_field_1;
56 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
57 static int n_calls_get_base_type;
58 static int n_outer_fields_searched;
59 static int n_contexts_saved;
62 /* Data for lookup_base and its workers. */
64 struct lookup_base_data_s
66 tree t; /* type being searched. */
67 tree base; /* The base type we're looking for. */
68 tree binfo; /* Found binfo. */
69 bool via_virtual; /* Found via a virtual path. */
70 bool ambiguous; /* Found multiply ambiguous */
71 bool repeated_base; /* Whether there are repeated bases in the
72 hierarchy. */
73 bool want_any; /* Whether we want any matching binfo. */
76 /* Worker function for lookup_base. See if we've found the desired
77 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
79 static tree
80 dfs_lookup_base (tree binfo, void *data_)
82 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
84 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
86 if (!data->binfo)
88 data->binfo = binfo;
89 data->via_virtual
90 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
92 if (!data->repeated_base)
93 /* If there are no repeated bases, we can stop now. */
94 return binfo;
96 if (data->want_any && !data->via_virtual)
97 /* If this is a non-virtual base, then we can't do
98 better. */
99 return binfo;
101 return dfs_skip_bases;
103 else
105 gcc_assert (binfo != data->binfo);
107 /* We've found more than one matching binfo. */
108 if (!data->want_any)
110 /* This is immediately ambiguous. */
111 data->binfo = NULL_TREE;
112 data->ambiguous = true;
113 return error_mark_node;
116 /* Prefer one via a non-virtual path. */
117 if (!binfo_via_virtual (binfo, data->t))
119 data->binfo = binfo;
120 data->via_virtual = false;
121 return binfo;
124 /* There must be repeated bases, otherwise we'd have stopped
125 on the first base we found. */
126 return dfs_skip_bases;
130 return NULL_TREE;
133 /* Returns true if type BASE is accessible in T. (BASE is known to be
134 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
135 true, consider any special access of the current scope, or access
136 bestowed by friendship. */
138 bool
139 accessible_base_p (tree t, tree base, bool consider_local_p)
141 tree decl;
143 /* [class.access.base]
145 A base class is said to be accessible if an invented public
146 member of the base class is accessible.
148 If BASE is a non-proper base, this condition is trivially
149 true. */
150 if (same_type_p (t, base))
151 return true;
152 /* Rather than inventing a public member, we use the implicit
153 public typedef created in the scope of every class. */
154 decl = TYPE_FIELDS (base);
155 while (!DECL_SELF_REFERENCE_P (decl))
156 decl = DECL_CHAIN (decl);
157 while (ANON_AGGR_TYPE_P (t))
158 t = TYPE_CONTEXT (t);
159 return accessible_p (t, decl, consider_local_p);
162 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
163 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
164 non-NULL, fill with information about what kind of base we
165 discovered.
167 If the base is inaccessible, or ambiguous, then error_mark_node is
168 returned. If the tf_error bit of COMPLAIN is not set, no error
169 is issued. */
171 tree
172 lookup_base (tree t, tree base, base_access access,
173 base_kind *kind_ptr, tsubst_flags_t complain)
175 tree binfo;
176 tree t_binfo;
177 base_kind bk;
179 /* "Nothing" is definitely not derived from Base. */
180 if (t == NULL_TREE)
182 if (kind_ptr)
183 *kind_ptr = bk_not_base;
184 return NULL_TREE;
187 if (t == error_mark_node || base == error_mark_node)
189 if (kind_ptr)
190 *kind_ptr = bk_not_base;
191 return error_mark_node;
193 gcc_assert (TYPE_P (base));
195 if (!TYPE_P (t))
197 t_binfo = t;
198 t = BINFO_TYPE (t);
200 else
202 t = complete_type (TYPE_MAIN_VARIANT (t));
203 t_binfo = TYPE_BINFO (t);
206 base = TYPE_MAIN_VARIANT (base);
208 /* If BASE is incomplete, it can't be a base of T--and instantiating it
209 might cause an error. */
210 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
212 struct lookup_base_data_s data;
214 data.t = t;
215 data.base = base;
216 data.binfo = NULL_TREE;
217 data.ambiguous = data.via_virtual = false;
218 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
219 data.want_any = access == ba_any;
221 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
222 binfo = data.binfo;
224 if (!binfo)
225 bk = data.ambiguous ? bk_ambig : bk_not_base;
226 else if (binfo == t_binfo)
227 bk = bk_same_type;
228 else if (data.via_virtual)
229 bk = bk_via_virtual;
230 else
231 bk = bk_proper_base;
233 else
235 binfo = NULL_TREE;
236 bk = bk_not_base;
239 /* Check that the base is unambiguous and accessible. */
240 if (access != ba_any)
241 switch (bk)
243 case bk_not_base:
244 break;
246 case bk_ambig:
247 if (complain & tf_error)
248 error ("%qT is an ambiguous base of %qT", base, t);
249 binfo = error_mark_node;
250 break;
252 default:
253 if ((access & ba_check_bit)
254 /* If BASE is incomplete, then BASE and TYPE are probably
255 the same, in which case BASE is accessible. If they
256 are not the same, then TYPE is invalid. In that case,
257 there's no need to issue another error here, and
258 there's no implicit typedef to use in the code that
259 follows, so we skip the check. */
260 && COMPLETE_TYPE_P (base)
261 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
263 if (complain & tf_error)
264 error ("%qT is an inaccessible base of %qT", base, t);
265 binfo = error_mark_node;
266 bk = bk_inaccessible;
268 break;
271 if (kind_ptr)
272 *kind_ptr = bk;
274 return binfo;
277 /* Data for dcast_base_hint walker. */
279 struct dcast_data_s
281 tree subtype; /* The base type we're looking for. */
282 int virt_depth; /* Number of virtual bases encountered from most
283 derived. */
284 tree offset; /* Best hint offset discovered so far. */
285 bool repeated_base; /* Whether there are repeated bases in the
286 hierarchy. */
289 /* Worker for dcast_base_hint. Search for the base type being cast
290 from. */
292 static tree
293 dfs_dcast_hint_pre (tree binfo, void *data_)
295 struct dcast_data_s *data = (struct dcast_data_s *) data_;
297 if (BINFO_VIRTUAL_P (binfo))
298 data->virt_depth++;
300 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
302 if (data->virt_depth)
304 data->offset = ssize_int (-1);
305 return data->offset;
307 if (data->offset)
308 data->offset = ssize_int (-3);
309 else
310 data->offset = BINFO_OFFSET (binfo);
312 return data->repeated_base ? dfs_skip_bases : data->offset;
315 return NULL_TREE;
318 /* Worker for dcast_base_hint. Track the virtual depth. */
320 static tree
321 dfs_dcast_hint_post (tree binfo, void *data_)
323 struct dcast_data_s *data = (struct dcast_data_s *) data_;
325 if (BINFO_VIRTUAL_P (binfo))
326 data->virt_depth--;
328 return NULL_TREE;
331 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
332 started from is related to the required TARGET type, in order to optimize
333 the inheritance graph search. This information is independent of the
334 current context, and ignores private paths, hence get_base_distance is
335 inappropriate. Return a TREE specifying the base offset, BOFF.
336 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
337 and there are no public virtual SUBTYPE bases.
338 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
339 BOFF == -2, SUBTYPE is not a public base.
340 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
342 tree
343 dcast_base_hint (tree subtype, tree target)
345 struct dcast_data_s data;
347 data.subtype = subtype;
348 data.virt_depth = 0;
349 data.offset = NULL_TREE;
350 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
352 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
353 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
354 return data.offset ? data.offset : ssize_int (-2);
357 /* Search for a member with name NAME in a multiple inheritance
358 lattice specified by TYPE. If it does not exist, return NULL_TREE.
359 If the member is ambiguously referenced, return `error_mark_node'.
360 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
361 true, type declarations are preferred. */
363 /* Do a 1-level search for NAME as a member of TYPE. The caller must
364 figure out whether it can access this field. (Since it is only one
365 level, this is reasonable.) */
367 tree
368 lookup_field_1 (tree type, tree name, bool want_type)
370 tree field;
372 gcc_assert (identifier_p (name));
374 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
375 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
376 || TREE_CODE (type) == TYPENAME_TYPE)
377 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
378 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
379 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
380 the code often worked even when we treated the index as a list
381 of fields!)
382 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
383 return NULL_TREE;
385 if (CLASSTYPE_SORTED_FIELDS (type))
387 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
388 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
389 int i;
391 while (lo < hi)
393 i = (lo + hi) / 2;
395 if (GATHER_STATISTICS)
396 n_fields_searched++;
398 if (DECL_NAME (fields[i]) > name)
399 hi = i;
400 else if (DECL_NAME (fields[i]) < name)
401 lo = i + 1;
402 else
404 field = NULL_TREE;
406 /* We might have a nested class and a field with the
407 same name; we sorted them appropriately via
408 field_decl_cmp, so just look for the first or last
409 field with this name. */
410 if (want_type)
413 field = fields[i--];
414 while (i >= lo && DECL_NAME (fields[i]) == name);
415 if (!DECL_DECLARES_TYPE_P (field))
416 field = NULL_TREE;
418 else
421 field = fields[i++];
422 while (i < hi && DECL_NAME (fields[i]) == name);
425 if (field)
427 field = strip_using_decl (field);
428 if (is_overloaded_fn (field))
429 field = NULL_TREE;
432 return field;
435 return NULL_TREE;
438 field = TYPE_FIELDS (type);
440 if (GATHER_STATISTICS)
441 n_calls_lookup_field_1++;
443 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
445 tree decl = field;
447 if (GATHER_STATISTICS)
448 n_fields_searched++;
450 gcc_assert (DECL_P (field));
451 if (DECL_NAME (field) == NULL_TREE
452 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
454 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
455 if (temp)
456 return temp;
459 if (TREE_CODE (decl) == USING_DECL
460 && DECL_NAME (decl) == name)
462 decl = strip_using_decl (decl);
463 if (is_overloaded_fn (decl))
464 continue;
467 if (DECL_NAME (decl) == name
468 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
469 return decl;
471 /* Not found. */
472 if (name == vptr_identifier)
474 /* Give the user what s/he thinks s/he wants. */
475 if (TYPE_POLYMORPHIC_P (type))
476 return TYPE_VFIELD (type);
478 return NULL_TREE;
481 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
482 NAMESPACE_DECL corresponding to the innermost non-block scope. */
484 tree
485 current_scope (void)
487 /* There are a number of cases we need to be aware of here:
488 current_class_type current_function_decl
489 global NULL NULL
490 fn-local NULL SET
491 class-local SET NULL
492 class->fn SET SET
493 fn->class SET SET
495 Those last two make life interesting. If we're in a function which is
496 itself inside a class, we need decls to go into the fn's decls (our
497 second case below). But if we're in a class and the class itself is
498 inside a function, we need decls to go into the decls for the class. To
499 achieve this last goal, we must see if, when both current_class_ptr and
500 current_function_decl are set, the class was declared inside that
501 function. If so, we know to put the decls into the class's scope. */
502 if (current_function_decl && current_class_type
503 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
504 && same_type_p (DECL_CONTEXT (current_function_decl),
505 current_class_type))
506 || (DECL_FRIEND_CONTEXT (current_function_decl)
507 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
508 current_class_type))))
509 return current_function_decl;
511 if (current_class_type)
512 return current_class_type;
514 if (current_function_decl)
515 return current_function_decl;
517 return current_namespace;
520 /* Returns nonzero if we are currently in a function scope. Note
521 that this function returns zero if we are within a local class, but
522 not within a member function body of the local class. */
525 at_function_scope_p (void)
527 tree cs = current_scope ();
528 /* Also check cfun to make sure that we're really compiling
529 this function (as opposed to having set current_function_decl
530 for access checking or some such). */
531 return (cs && TREE_CODE (cs) == FUNCTION_DECL
532 && cfun && cfun->decl == current_function_decl);
535 /* Returns true if the innermost active scope is a class scope. */
537 bool
538 at_class_scope_p (void)
540 tree cs = current_scope ();
541 return cs && TYPE_P (cs);
544 /* Returns true if the innermost active scope is a namespace scope. */
546 bool
547 at_namespace_scope_p (void)
549 tree cs = current_scope ();
550 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
553 /* Return the scope of DECL, as appropriate when doing name-lookup. */
555 tree
556 context_for_name_lookup (tree decl)
558 /* [class.union]
560 For the purposes of name lookup, after the anonymous union
561 definition, the members of the anonymous union are considered to
562 have been defined in the scope in which the anonymous union is
563 declared. */
564 tree context = DECL_CONTEXT (decl);
566 while (context && TYPE_P (context)
567 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
568 context = TYPE_CONTEXT (context);
569 if (!context)
570 context = global_namespace;
572 return context;
575 /* Returns true iff DECL is declared in TYPE. */
577 static bool
578 member_declared_in_type (tree decl, tree type)
580 /* A normal declaration obviously counts. */
581 if (context_for_name_lookup (decl) == type)
582 return true;
583 /* So does a using or access declaration. */
584 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
585 && purpose_member (type, DECL_ACCESS (decl)))
586 return true;
587 return false;
590 /* The accessibility routines use BINFO_ACCESS for scratch space
591 during the computation of the accessibility of some declaration. */
593 /* Avoid walking up past a declaration of the member. */
595 static tree
596 dfs_access_in_type_pre (tree binfo, void *data)
598 tree decl = (tree) data;
599 tree type = BINFO_TYPE (binfo);
600 if (member_declared_in_type (decl, type))
601 return dfs_skip_bases;
602 return NULL_TREE;
605 #define BINFO_ACCESS(NODE) \
606 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
608 /* Set the access associated with NODE to ACCESS. */
610 #define SET_BINFO_ACCESS(NODE, ACCESS) \
611 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
612 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
614 /* Called from access_in_type via dfs_walk. Calculate the access to
615 DATA (which is really a DECL) in BINFO. */
617 static tree
618 dfs_access_in_type (tree binfo, void *data)
620 tree decl = (tree) data;
621 tree type = BINFO_TYPE (binfo);
622 access_kind access = ak_none;
624 if (context_for_name_lookup (decl) == type)
626 /* If we have descended to the scope of DECL, just note the
627 appropriate access. */
628 if (TREE_PRIVATE (decl))
629 access = ak_private;
630 else if (TREE_PROTECTED (decl))
631 access = ak_protected;
632 else
633 access = ak_public;
635 else
637 /* First, check for an access-declaration that gives us more
638 access to the DECL. */
639 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
641 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
643 if (decl_access)
645 decl_access = TREE_VALUE (decl_access);
647 if (decl_access == access_public_node)
648 access = ak_public;
649 else if (decl_access == access_protected_node)
650 access = ak_protected;
651 else if (decl_access == access_private_node)
652 access = ak_private;
653 else
654 gcc_unreachable ();
658 if (!access)
660 int i;
661 tree base_binfo;
662 vec<tree, va_gc> *accesses;
664 /* Otherwise, scan our baseclasses, and pick the most favorable
665 access. */
666 accesses = BINFO_BASE_ACCESSES (binfo);
667 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
669 tree base_access = (*accesses)[i];
670 access_kind base_access_now = BINFO_ACCESS (base_binfo);
672 if (base_access_now == ak_none || base_access_now == ak_private)
673 /* If it was not accessible in the base, or only
674 accessible as a private member, we can't access it
675 all. */
676 base_access_now = ak_none;
677 else if (base_access == access_protected_node)
678 /* Public and protected members in the base become
679 protected here. */
680 base_access_now = ak_protected;
681 else if (base_access == access_private_node)
682 /* Public and protected members in the base become
683 private here. */
684 base_access_now = ak_private;
686 /* See if the new access, via this base, gives more
687 access than our previous best access. */
688 if (base_access_now != ak_none
689 && (access == ak_none || base_access_now < access))
691 access = base_access_now;
693 /* If the new access is public, we can't do better. */
694 if (access == ak_public)
695 break;
701 /* Note the access to DECL in TYPE. */
702 SET_BINFO_ACCESS (binfo, access);
704 return NULL_TREE;
707 /* Return the access to DECL in TYPE. */
709 static access_kind
710 access_in_type (tree type, tree decl)
712 tree binfo = TYPE_BINFO (type);
714 /* We must take into account
716 [class.paths]
718 If a name can be reached by several paths through a multiple
719 inheritance graph, the access is that of the path that gives
720 most access.
722 The algorithm we use is to make a post-order depth-first traversal
723 of the base-class hierarchy. As we come up the tree, we annotate
724 each node with the most lenient access. */
725 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
727 return BINFO_ACCESS (binfo);
730 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
731 of OTYPE in the context of DERIVED. */
733 static int
734 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
736 /* We're checking this clause from [class.access.base]
738 m as a member of N is protected, and the reference occurs in a
739 member or friend of class N, or in a member or friend of a
740 class P derived from N, where m as a member of P is public, private
741 or protected.
743 Here DERIVED is a possible P, DECL is m and TYPE is N. */
745 /* If DERIVED isn't derived from N, then it can't be a P. */
746 if (!DERIVED_FROM_P (type, derived))
747 return 0;
749 /* [class.protected]
751 When a friend or a member function of a derived class references
752 a protected nonstatic member of a base class, an access check
753 applies in addition to those described earlier in clause
754 _class.access_) Except when forming a pointer to member
755 (_expr.unary.op_), the access must be through a pointer to,
756 reference to, or object of the derived class itself (or any class
757 derived from that class) (_expr.ref_). If the access is to form
758 a pointer to member, the nested-name-specifier shall name the
759 derived class (or any class derived from that class). */
760 if (DECL_NONSTATIC_MEMBER_P (decl)
761 && !DERIVED_FROM_P (derived, otype))
762 return 0;
764 return 1;
767 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
768 to access DECL through TYPE. OTYPE is the type of the object. */
770 static int
771 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
773 /* We're checking this clause from [class.access.base]
775 m as a member of N is protected, and the reference occurs in a
776 member or friend of class N, or in a member or friend of a
777 class P derived from N, where m as a member of P is public, private
778 or protected.
780 Here DECL is m and TYPE is N. SCOPE is the current context,
781 and we check all its possible Ps. */
782 tree befriending_classes;
783 tree t;
785 if (!scope)
786 return 0;
788 if (is_global_friend (scope))
789 return 1;
791 /* Is SCOPE itself a suitable P? */
792 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
793 return 1;
795 if (DECL_DECLARES_FUNCTION_P (scope))
796 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
797 else if (TYPE_P (scope))
798 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
799 else
800 return 0;
802 for (t = befriending_classes; t; t = TREE_CHAIN (t))
803 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
804 return 1;
806 /* Nested classes have the same access as their enclosing types, as
807 per DR 45 (this is a change from C++98). */
808 if (TYPE_P (scope))
809 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
810 return 1;
812 if (DECL_DECLARES_FUNCTION_P (scope))
814 /* Perhaps this SCOPE is a member of a class which is a
815 friend. */
816 if (DECL_CLASS_SCOPE_P (scope)
817 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
818 return 1;
821 /* Maybe scope's template is a friend. */
822 if (tree tinfo = get_template_info (scope))
824 tree tmpl = TI_TEMPLATE (tinfo);
825 if (DECL_CLASS_TEMPLATE_P (tmpl))
826 tmpl = TREE_TYPE (tmpl);
827 else
828 tmpl = DECL_TEMPLATE_RESULT (tmpl);
829 if (tmpl != scope)
831 /* Increment processing_template_decl to make sure that
832 dependent_type_p works correctly. */
833 ++processing_template_decl;
834 int ret = friend_accessible_p (tmpl, decl, type, otype);
835 --processing_template_decl;
836 if (ret)
837 return 1;
841 /* If is_friend is true, we should have found a befriending class. */
842 gcc_checking_assert (!is_friend (type, scope));
844 return 0;
847 struct dfs_accessible_data
849 tree decl;
850 tree object_type;
853 /* Avoid walking up past a declaration of the member. */
855 static tree
856 dfs_accessible_pre (tree binfo, void *data)
858 dfs_accessible_data *d = (dfs_accessible_data *)data;
859 tree type = BINFO_TYPE (binfo);
860 if (member_declared_in_type (d->decl, type))
861 return dfs_skip_bases;
862 return NULL_TREE;
865 /* Called via dfs_walk_once_accessible from accessible_p */
867 static tree
868 dfs_accessible_post (tree binfo, void *data)
870 /* access_in_type already set BINFO_ACCESS for us. */
871 access_kind access = BINFO_ACCESS (binfo);
872 tree N = BINFO_TYPE (binfo);
873 dfs_accessible_data *d = (dfs_accessible_data *)data;
874 tree decl = d->decl;
875 tree scope = current_nonlambda_scope ();
877 /* A member m is accessible at the point R when named in class N if */
878 switch (access)
880 case ak_none:
881 return NULL_TREE;
883 case ak_public:
884 /* m as a member of N is public, or */
885 return binfo;
887 case ak_private:
889 /* m as a member of N is private, and R occurs in a member or friend of
890 class N, or */
891 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
892 && is_friend (N, scope))
893 return binfo;
894 return NULL_TREE;
897 case ak_protected:
899 /* m as a member of N is protected, and R occurs in a member or friend
900 of class N, or in a member or friend of a class P derived from N,
901 where m as a member of P is public, private, or protected */
902 if (friend_accessible_p (scope, decl, N, d->object_type))
903 return binfo;
904 return NULL_TREE;
907 default:
908 gcc_unreachable ();
912 /* Like accessible_p below, but within a template returns true iff DECL is
913 accessible in TYPE to all possible instantiations of the template. */
916 accessible_in_template_p (tree type, tree decl)
918 int save_ptd = processing_template_decl;
919 processing_template_decl = 0;
920 int val = accessible_p (type, decl, false);
921 processing_template_decl = save_ptd;
922 return val;
925 /* DECL is a declaration from a base class of TYPE, which was the
926 class used to name DECL. Return nonzero if, in the current
927 context, DECL is accessible. If TYPE is actually a BINFO node,
928 then we can tell in what context the access is occurring by looking
929 at the most derived class along the path indicated by BINFO. If
930 CONSIDER_LOCAL is true, do consider special access the current
931 scope or friendship thereof we might have. */
934 accessible_p (tree type, tree decl, bool consider_local_p)
936 tree binfo;
937 access_kind access;
939 /* If this declaration is in a block or namespace scope, there's no
940 access control. */
941 if (!TYPE_P (context_for_name_lookup (decl)))
942 return 1;
944 /* There is no need to perform access checks inside a thunk. */
945 if (current_function_decl && DECL_THUNK_P (current_function_decl))
946 return 1;
948 /* In a template declaration, we cannot be sure whether the
949 particular specialization that is instantiated will be a friend
950 or not. Therefore, all access checks are deferred until
951 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
952 parameter list for a template (because we may see dependent types
953 in default arguments for template parameters), and access
954 checking should be performed in the outermost parameter list. */
955 if (processing_template_decl
956 && !expanding_concept ()
957 && (!processing_template_parmlist || processing_template_decl > 1))
958 return 1;
960 tree otype = NULL_TREE;
961 if (!TYPE_P (type))
963 /* When accessing a non-static member, the most derived type in the
964 binfo chain is the type of the object; remember that type for
965 protected_accessible_p. */
966 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
967 otype = BINFO_TYPE (b);
968 type = BINFO_TYPE (type);
970 else
971 otype = type;
973 /* [class.access.base]
975 A member m is accessible when named in class N if
977 --m as a member of N is public, or
979 --m as a member of N is private, and the reference occurs in a
980 member or friend of class N, or
982 --m as a member of N is protected, and the reference occurs in a
983 member or friend of class N, or in a member or friend of a
984 class P derived from N, where m as a member of P is public, private or
985 protected, or
987 --there exists a base class B of N that is accessible at the point
988 of reference, and m is accessible when named in class B.
990 We walk the base class hierarchy, checking these conditions. */
992 /* We walk using TYPE_BINFO (type) because access_in_type will set
993 BINFO_ACCESS on it and its bases. */
994 binfo = TYPE_BINFO (type);
996 /* Compute the accessibility of DECL in the class hierarchy
997 dominated by type. */
998 access = access_in_type (type, decl);
999 if (access == ak_public)
1000 return 1;
1002 /* If we aren't considering the point of reference, only the first bullet
1003 applies. */
1004 if (!consider_local_p)
1005 return 0;
1007 dfs_accessible_data d = { decl, otype };
1009 /* Walk the hierarchy again, looking for a base class that allows
1010 access. */
1011 return dfs_walk_once_accessible (binfo, /*friends=*/true,
1012 dfs_accessible_pre,
1013 dfs_accessible_post, &d)
1014 != NULL_TREE;
1017 struct lookup_field_info {
1018 /* The type in which we're looking. */
1019 tree type;
1020 /* The name of the field for which we're looking. */
1021 tree name;
1022 /* If non-NULL, the current result of the lookup. */
1023 tree rval;
1024 /* The path to RVAL. */
1025 tree rval_binfo;
1026 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1027 candidates. */
1028 tree ambiguous;
1029 /* If nonzero, we are looking for types, not data members. */
1030 int want_type;
1031 /* If something went wrong, a message indicating what. */
1032 const char *errstr;
1035 /* Nonzero for a class member means that it is shared between all objects
1036 of that class.
1038 [class.member.lookup]:If the resulting set of declarations are not all
1039 from sub-objects of the same type, or the set has a nonstatic member
1040 and includes members from distinct sub-objects, there is an ambiguity
1041 and the program is ill-formed.
1043 This function checks that T contains no nonstatic members. */
1046 shared_member_p (tree t)
1048 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
1049 || TREE_CODE (t) == CONST_DECL)
1050 return 1;
1051 if (is_overloaded_fn (t))
1053 for (ovl_iterator iter (get_fns (t)); iter; ++iter)
1054 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter))
1055 return 0;
1056 return 1;
1058 return 0;
1061 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1062 found as a base class and sub-object of the object denoted by
1063 BINFO. */
1065 static int
1066 is_subobject_of_p (tree parent, tree binfo)
1068 tree probe;
1070 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1072 if (probe == binfo)
1073 return 1;
1074 if (BINFO_VIRTUAL_P (probe))
1075 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1076 != NULL_TREE);
1078 return 0;
1081 /* DATA is really a struct lookup_field_info. Look for a field with
1082 the name indicated there in BINFO. If this function returns a
1083 non-NULL value it is the result of the lookup. Called from
1084 lookup_field via breadth_first_search. */
1086 static tree
1087 lookup_field_r (tree binfo, void *data)
1089 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1090 tree type = BINFO_TYPE (binfo);
1091 tree nval = NULL_TREE;
1093 /* If this is a dependent base, don't look in it. */
1094 if (BINFO_DEPENDENT_BASE_P (binfo))
1095 return NULL_TREE;
1097 /* If this base class is hidden by the best-known value so far, we
1098 don't need to look. */
1099 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1100 && !BINFO_VIRTUAL_P (binfo))
1101 return dfs_skip_bases;
1103 /* First, look for a function. There can't be a function and a data
1104 member with the same name, and if there's a function and a type
1105 with the same name, the type is hidden by the function. */
1106 if (!lfi->want_type)
1107 nval = lookup_fnfields_slot (type, lfi->name);
1109 if (!nval)
1110 /* Look for a data member or type. */
1111 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1112 else if (TREE_CODE (nval) == OVERLOAD && OVL_USING_P (nval))
1114 /* If we have both dependent and non-dependent using-declarations, return
1115 the dependent one rather than an incomplete list of functions. */
1116 tree dep_using = lookup_field_1 (type, lfi->name, lfi->want_type);
1117 if (dep_using && TREE_CODE (dep_using) == USING_DECL)
1118 nval = dep_using;
1121 /* If there is no declaration with the indicated name in this type,
1122 then there's nothing to do. */
1123 if (!nval)
1124 goto done;
1126 /* If we're looking up a type (as with an elaborated type specifier)
1127 we ignore all non-types we find. */
1128 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1130 if (lfi->name == TYPE_IDENTIFIER (type))
1132 /* If the aggregate has no user defined constructors, we allow
1133 it to have fields with the same name as the enclosing type.
1134 If we are looking for that name, find the corresponding
1135 TYPE_DECL. */
1136 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1137 if (DECL_NAME (nval) == lfi->name
1138 && TREE_CODE (nval) == TYPE_DECL)
1139 break;
1141 else
1142 nval = NULL_TREE;
1143 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1145 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1146 lfi->name);
1147 if (e != NULL)
1148 nval = TYPE_MAIN_DECL (e->type);
1149 else
1150 goto done;
1154 /* If the lookup already found a match, and the new value doesn't
1155 hide the old one, we might have an ambiguity. */
1156 if (lfi->rval_binfo
1157 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1160 if (nval == lfi->rval && shared_member_p (nval))
1161 /* The two things are really the same. */
1163 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1164 /* The previous value hides the new one. */
1166 else
1168 /* We have a real ambiguity. We keep a chain of all the
1169 candidates. */
1170 if (!lfi->ambiguous && lfi->rval)
1172 /* This is the first time we noticed an ambiguity. Add
1173 what we previously thought was a reasonable candidate
1174 to the list. */
1175 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1176 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1179 /* Add the new value. */
1180 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1181 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1182 lfi->errstr = G_("request for member %qD is ambiguous");
1185 else
1187 lfi->rval = nval;
1188 lfi->rval_binfo = binfo;
1191 done:
1192 /* Don't look for constructors or destructors in base classes. */
1193 if (IDENTIFIER_CDTOR_P (lfi->name))
1194 return dfs_skip_bases;
1195 return NULL_TREE;
1198 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1199 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1200 FUNCTIONS, and OPTYPE respectively. */
1202 tree
1203 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1205 tree baselink;
1207 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1208 || TREE_CODE (functions) == TEMPLATE_DECL
1209 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1210 || TREE_CODE (functions) == OVERLOAD);
1211 gcc_assert (!optype || TYPE_P (optype));
1212 gcc_assert (TREE_TYPE (functions));
1214 baselink = make_node (BASELINK);
1215 TREE_TYPE (baselink) = TREE_TYPE (functions);
1216 BASELINK_BINFO (baselink) = binfo;
1217 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1218 BASELINK_FUNCTIONS (baselink) = functions;
1219 BASELINK_OPTYPE (baselink) = optype;
1221 return baselink;
1224 /* Look for a member named NAME in an inheritance lattice dominated by
1225 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1226 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1227 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1228 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1229 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1230 TREE_VALUEs are the list of ambiguous candidates.
1232 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1234 If nothing can be found return NULL_TREE and do not issue an error.
1236 If non-NULL, failure information is written back to AFI. */
1238 tree
1239 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1240 tsubst_flags_t complain, access_failure_info *afi)
1242 tree rval, rval_binfo = NULL_TREE;
1243 tree type = NULL_TREE, basetype_path = NULL_TREE;
1244 struct lookup_field_info lfi;
1246 /* rval_binfo is the binfo associated with the found member, note,
1247 this can be set with useful information, even when rval is not
1248 set, because it must deal with ALL members, not just non-function
1249 members. It is used for ambiguity checking and the hidden
1250 checks. Whereas rval is only set if a proper (not hidden)
1251 non-function member is found. */
1253 const char *errstr = 0;
1255 if (name == error_mark_node
1256 || xbasetype == NULL_TREE
1257 || xbasetype == error_mark_node)
1258 return NULL_TREE;
1260 gcc_assert (identifier_p (name));
1262 if (TREE_CODE (xbasetype) == TREE_BINFO)
1264 type = BINFO_TYPE (xbasetype);
1265 basetype_path = xbasetype;
1267 else
1269 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1270 return NULL_TREE;
1271 type = xbasetype;
1272 xbasetype = NULL_TREE;
1275 type = complete_type (type);
1277 /* Make sure we're looking for a member of the current instantiation in the
1278 right partial specialization. */
1279 if (flag_concepts && dependent_type_p (type))
1280 if (tree t = currently_open_class (type))
1281 type = t;
1283 if (!basetype_path)
1284 basetype_path = TYPE_BINFO (type);
1286 if (!basetype_path)
1287 return NULL_TREE;
1289 if (GATHER_STATISTICS)
1290 n_calls_lookup_field++;
1292 memset (&lfi, 0, sizeof (lfi));
1293 lfi.type = type;
1294 lfi.name = name;
1295 lfi.want_type = want_type;
1296 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1297 rval = lfi.rval;
1298 rval_binfo = lfi.rval_binfo;
1299 if (rval_binfo)
1300 type = BINFO_TYPE (rval_binfo);
1301 errstr = lfi.errstr;
1303 /* If we are not interested in ambiguities, don't report them;
1304 just return NULL_TREE. */
1305 if (!protect && lfi.ambiguous)
1306 return NULL_TREE;
1308 if (protect == 2)
1310 if (lfi.ambiguous)
1311 return lfi.ambiguous;
1312 else
1313 protect = 0;
1316 /* [class.access]
1318 In the case of overloaded function names, access control is
1319 applied to the function selected by overloaded resolution.
1321 We cannot check here, even if RVAL is only a single non-static
1322 member function, since we do not know what the "this" pointer
1323 will be. For:
1325 class A { protected: void f(); };
1326 class B : public A {
1327 void g(A *p) {
1328 f(); // OK
1329 p->f(); // Not OK.
1333 only the first call to "f" is valid. However, if the function is
1334 static, we can check. */
1335 if (rval && protect
1336 && !really_overloaded_fn (rval))
1338 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1339 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1340 && !perform_or_defer_access_check (basetype_path, decl, decl,
1341 complain, afi))
1342 rval = error_mark_node;
1345 if (errstr && protect)
1347 if (complain & tf_error)
1349 error (errstr, name, type);
1350 if (lfi.ambiguous)
1351 print_candidates (lfi.ambiguous);
1353 rval = error_mark_node;
1356 if (rval && is_overloaded_fn (rval))
1357 rval = build_baselink (rval_binfo, basetype_path, rval,
1358 (IDENTIFIER_CONV_OP_P (name)
1359 ? TREE_TYPE (name): NULL_TREE));
1360 return rval;
1363 /* Helper class for lookup_member_fuzzy. */
1365 class lookup_field_fuzzy_info
1367 public:
1368 lookup_field_fuzzy_info (bool want_type_p) :
1369 m_want_type_p (want_type_p), m_candidates () {}
1371 void fuzzy_lookup_fnfields (tree type);
1372 void fuzzy_lookup_field (tree type);
1374 /* If true, we are looking for types, not data members. */
1375 bool m_want_type_p;
1376 /* The result: a vec of identifiers. */
1377 auto_vec<tree> m_candidates;
1380 /* Locate all methods within TYPE, append them to m_candidates. */
1382 void
1383 lookup_field_fuzzy_info::fuzzy_lookup_fnfields (tree type)
1385 vec<tree, va_gc> *method_vec;
1386 tree fn;
1387 size_t i;
1389 if (!CLASS_TYPE_P (type))
1390 return;
1392 method_vec = CLASSTYPE_METHOD_VEC (type);
1393 if (!method_vec)
1394 return;
1396 for (i = 0; vec_safe_iterate (method_vec, i, &fn); ++i)
1397 if (fn)
1398 m_candidates.safe_push (OVL_NAME (fn));
1401 /* Locate all fields within TYPE, append them to m_candidates. */
1403 void
1404 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1406 if (!CLASS_TYPE_P (type))
1407 return;
1409 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1411 if (!m_want_type_p || DECL_DECLARES_TYPE_P (field))
1412 if (DECL_NAME (field))
1413 m_candidates.safe_push (DECL_NAME (field));
1418 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1419 DATA is really a lookup_field_fuzzy_info. Look for a field with
1420 the name indicated there in BINFO. Gathers pertinent identifiers into
1421 m_candidates. */
1423 static tree
1424 lookup_field_fuzzy_r (tree binfo, void *data)
1426 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1427 tree type = BINFO_TYPE (binfo);
1429 /* First, look for functions. */
1430 if (!lffi->m_want_type_p)
1431 lffi->fuzzy_lookup_fnfields (type);
1433 /* Look for data member and types. */
1434 lffi->fuzzy_lookup_field (type);
1436 return NULL_TREE;
1439 /* Like lookup_member, but try to find the closest match for NAME,
1440 rather than an exact match, and return an identifier (or NULL_TREE).
1441 Do not complain. */
1443 tree
1444 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1446 tree type = NULL_TREE, basetype_path = NULL_TREE;
1447 struct lookup_field_fuzzy_info lffi (want_type_p);
1449 /* rval_binfo is the binfo associated with the found member, note,
1450 this can be set with useful information, even when rval is not
1451 set, because it must deal with ALL members, not just non-function
1452 members. It is used for ambiguity checking and the hidden
1453 checks. Whereas rval is only set if a proper (not hidden)
1454 non-function member is found. */
1456 if (name == error_mark_node
1457 || xbasetype == NULL_TREE
1458 || xbasetype == error_mark_node)
1459 return NULL_TREE;
1461 gcc_assert (identifier_p (name));
1463 if (TREE_CODE (xbasetype) == TREE_BINFO)
1465 type = BINFO_TYPE (xbasetype);
1466 basetype_path = xbasetype;
1468 else
1470 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1471 return NULL_TREE;
1472 type = xbasetype;
1473 xbasetype = NULL_TREE;
1476 type = complete_type (type);
1478 /* Make sure we're looking for a member of the current instantiation in the
1479 right partial specialization. */
1480 if (flag_concepts && dependent_type_p (type))
1481 type = currently_open_class (type);
1483 if (!basetype_path)
1484 basetype_path = TYPE_BINFO (type);
1486 if (!basetype_path)
1487 return NULL_TREE;
1489 /* Populate lffi.m_candidates. */
1490 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1492 return find_closest_identifier (name, &lffi.m_candidates);
1495 /* Like lookup_member, except that if we find a function member we
1496 return NULL_TREE. */
1498 tree
1499 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1501 tree rval = lookup_member (xbasetype, name, protect, want_type,
1502 tf_warning_or_error);
1504 /* Ignore functions, but propagate the ambiguity list. */
1505 if (!error_operand_p (rval)
1506 && (rval && BASELINK_P (rval)))
1507 return NULL_TREE;
1509 return rval;
1512 /* Like lookup_member, except that if we find a non-function member we
1513 return NULL_TREE. */
1515 tree
1516 lookup_fnfields (tree xbasetype, tree name, int protect)
1518 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1519 tf_warning_or_error);
1521 /* Ignore non-functions, but propagate the ambiguity list. */
1522 if (!error_operand_p (rval)
1523 && (rval && !BASELINK_P (rval)))
1524 return NULL_TREE;
1526 return rval;
1529 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1530 corresponding to "operator TYPE ()", or -1 if there is no such
1531 operator. Only CLASS_TYPE itself is searched; this routine does
1532 not scan the base classes of CLASS_TYPE. */
1534 static int
1535 lookup_conversion_operator (tree class_type, tree type)
1537 int tpl_slot = -1;
1539 if (TYPE_HAS_CONVERSION (class_type))
1541 int i;
1542 tree fn;
1543 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1545 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1546 vec_safe_iterate (methods, i, &fn); ++i)
1548 /* All the conversion operators come near the beginning of
1549 the class. Therefore, if FN is not a conversion
1550 operator, there is no matching conversion operator in
1551 CLASS_TYPE. */
1552 fn = OVL_FIRST (fn);
1553 if (!DECL_CONV_FN_P (fn))
1554 break;
1556 if (TREE_CODE (fn) == TEMPLATE_DECL)
1557 /* All the templated conversion functions are on the same
1558 slot, so remember it. */
1559 tpl_slot = i;
1560 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1561 return i;
1565 return tpl_slot;
1568 /* TYPE is a class type. Return the index of the fields within
1569 the method vector with name NAME, or -1 if no such field exists.
1570 Does not lazily declare implicitly-declared member functions. */
1572 static int
1573 lookup_fnfields_idx_nolazy (tree type, tree name)
1575 vec<tree, va_gc> *method_vec;
1576 tree fn;
1577 size_t i;
1579 if (!CLASS_TYPE_P (type))
1580 return -1;
1582 method_vec = CLASSTYPE_METHOD_VEC (type);
1583 if (!method_vec)
1584 return -1;
1586 if (GATHER_STATISTICS)
1587 n_calls_lookup_fnfields_1++;
1589 /* Constructors are first... */
1590 if (name == ctor_identifier)
1592 fn = CLASSTYPE_CONSTRUCTORS (type);
1593 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1595 /* and destructors are second. */
1596 if (name == dtor_identifier)
1598 fn = CLASSTYPE_DESTRUCTOR (type);
1599 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1601 if (IDENTIFIER_CONV_OP_P (name))
1602 return lookup_conversion_operator (type, TREE_TYPE (name));
1604 /* Skip the conversion operators. */
1605 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1606 vec_safe_iterate (method_vec, i, &fn);
1607 ++i)
1608 if (!DECL_CONV_FN_P (OVL_FIRST (fn)))
1609 break;
1611 /* If the type is complete, use binary search. */
1612 if (COMPLETE_TYPE_P (type))
1614 int lo;
1615 int hi;
1617 lo = i;
1618 hi = method_vec->length ();
1619 while (lo < hi)
1621 i = (lo + hi) / 2;
1623 if (GATHER_STATISTICS)
1624 n_outer_fields_searched++;
1626 tree tmp = (*method_vec)[i];
1627 tmp = OVL_NAME (tmp);
1628 if (tmp > name)
1629 hi = i;
1630 else if (tmp < name)
1631 lo = i + 1;
1632 else
1633 return i;
1636 else
1637 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1639 if (GATHER_STATISTICS)
1640 n_outer_fields_searched++;
1641 if (OVL_NAME (fn) == name)
1642 return i;
1645 return -1;
1648 /* TYPE is a class type. Return the index of the fields within
1649 the method vector with name NAME, or -1 if no such field exists. */
1651 static int
1652 lookup_fnfields_1 (tree type, tree name)
1654 if (!CLASS_TYPE_P (type))
1655 return -1;
1657 if (COMPLETE_TYPE_P (type))
1659 if (IDENTIFIER_CTOR_P (name))
1661 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1662 lazily_declare_fn (sfk_constructor, type);
1663 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1664 lazily_declare_fn (sfk_copy_constructor, type);
1665 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1666 lazily_declare_fn (sfk_move_constructor, type);
1668 else if (name == cp_assignment_operator_id (NOP_EXPR))
1670 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1671 lazily_declare_fn (sfk_copy_assignment, type);
1672 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1673 lazily_declare_fn (sfk_move_assignment, type);
1675 else if (IDENTIFIER_DTOR_P (name))
1677 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
1678 lazily_declare_fn (sfk_destructor, type);
1682 return lookup_fnfields_idx_nolazy (type, name);
1685 /* TYPE is a class type. Return the field within the method vector with
1686 name NAME, or NULL_TREE if no such field exists. */
1688 tree
1689 lookup_fnfields_slot (tree type, tree name)
1691 int ix = lookup_fnfields_1 (complete_type (type), name);
1692 if (ix < 0)
1693 return NULL_TREE;
1694 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1697 /* As above, but avoid lazily declaring functions. */
1699 tree
1700 lookup_fnfields_slot_nolazy (tree type, tree name)
1702 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1703 if (ix < 0)
1704 return NULL_TREE;
1705 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1708 /* Collect all the conversion operators of KLASS. */
1710 tree
1711 lookup_all_conversions (tree klass)
1713 tree lkp = NULL_TREE;
1715 if (vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (klass))
1717 tree ovl;
1718 for (int idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1719 methods->iterate (idx, &ovl); ++idx)
1721 if (!DECL_CONV_FN_P (OVL_FIRST (ovl)))
1722 /* There are no more conversion functions. */
1723 break;
1725 lkp = lookup_add (ovl, lkp);
1729 return lkp;
1732 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1733 the class or namespace used to qualify the name. CONTEXT_CLASS is
1734 the class corresponding to the object in which DECL will be used.
1735 Return a possibly modified version of DECL that takes into account
1736 the CONTEXT_CLASS.
1738 In particular, consider an expression like `B::m' in the context of
1739 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1740 then the most derived class indicated by the BASELINK_BINFO will be
1741 `B', not `D'. This function makes that adjustment. */
1743 tree
1744 adjust_result_of_qualified_name_lookup (tree decl,
1745 tree qualifying_scope,
1746 tree context_class)
1748 if (context_class && context_class != error_mark_node
1749 && CLASS_TYPE_P (context_class)
1750 && CLASS_TYPE_P (qualifying_scope)
1751 && DERIVED_FROM_P (qualifying_scope, context_class)
1752 && BASELINK_P (decl))
1754 tree base;
1756 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1757 Because we do not yet know which function will be chosen by
1758 overload resolution, we cannot yet check either accessibility
1759 or ambiguity -- in either case, the choice of a static member
1760 function might make the usage valid. */
1761 base = lookup_base (context_class, qualifying_scope,
1762 ba_unique, NULL, tf_none);
1763 if (base && base != error_mark_node)
1765 BASELINK_ACCESS_BINFO (decl) = base;
1766 tree decl_binfo
1767 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1768 ba_unique, NULL, tf_none);
1769 if (decl_binfo && decl_binfo != error_mark_node)
1770 BASELINK_BINFO (decl) = decl_binfo;
1774 if (BASELINK_P (decl))
1775 BASELINK_QUALIFIED_P (decl) = true;
1777 return decl;
1781 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1782 PRE_FN is called in preorder, while POST_FN is called in postorder.
1783 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1784 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1785 that value is immediately returned and the walk is terminated. One
1786 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1787 POST_FN are passed the binfo to examine and the caller's DATA
1788 value. All paths are walked, thus virtual and morally virtual
1789 binfos can be multiply walked. */
1791 tree
1792 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1793 tree (*post_fn) (tree, void *), void *data)
1795 tree rval;
1796 unsigned ix;
1797 tree base_binfo;
1799 /* Call the pre-order walking function. */
1800 if (pre_fn)
1802 rval = pre_fn (binfo, data);
1803 if (rval)
1805 if (rval == dfs_skip_bases)
1806 goto skip_bases;
1807 return rval;
1811 /* Find the next child binfo to walk. */
1812 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1814 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1815 if (rval)
1816 return rval;
1819 skip_bases:
1820 /* Call the post-order walking function. */
1821 if (post_fn)
1823 rval = post_fn (binfo, data);
1824 gcc_assert (rval != dfs_skip_bases);
1825 return rval;
1828 return NULL_TREE;
1831 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1832 that binfos are walked at most once. */
1834 static tree
1835 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1836 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1837 void *data)
1839 tree rval;
1840 unsigned ix;
1841 tree base_binfo;
1843 /* Call the pre-order walking function. */
1844 if (pre_fn)
1846 rval = pre_fn (binfo, data);
1847 if (rval)
1849 if (rval == dfs_skip_bases)
1850 goto skip_bases;
1852 return rval;
1856 /* Find the next child binfo to walk. */
1857 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1859 if (BINFO_VIRTUAL_P (base_binfo))
1860 if (pset->add (base_binfo))
1861 continue;
1863 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1864 if (rval)
1865 return rval;
1868 skip_bases:
1869 /* Call the post-order walking function. */
1870 if (post_fn)
1872 rval = post_fn (binfo, data);
1873 gcc_assert (rval != dfs_skip_bases);
1874 return rval;
1877 return NULL_TREE;
1880 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1881 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1882 For diamond shaped hierarchies we must mark the virtual bases, to
1883 avoid multiple walks. */
1885 tree
1886 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1887 tree (*post_fn) (tree, void *), void *data)
1889 static int active = 0; /* We must not be called recursively. */
1890 tree rval;
1892 gcc_assert (pre_fn || post_fn);
1893 gcc_assert (!active);
1894 active++;
1896 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1897 /* We are not diamond shaped, and therefore cannot encounter the
1898 same binfo twice. */
1899 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1900 else
1902 hash_set<tree> pset;
1903 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1906 active--;
1908 return rval;
1911 /* Worker function for dfs_walk_once_accessible. Behaves like
1912 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1913 access given by the current context should be considered, (b) ONCE
1914 indicates whether bases should be marked during traversal. */
1916 static tree
1917 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1918 tree (*pre_fn) (tree, void *),
1919 tree (*post_fn) (tree, void *), void *data)
1921 tree rval = NULL_TREE;
1922 unsigned ix;
1923 tree base_binfo;
1925 /* Call the pre-order walking function. */
1926 if (pre_fn)
1928 rval = pre_fn (binfo, data);
1929 if (rval)
1931 if (rval == dfs_skip_bases)
1932 goto skip_bases;
1934 return rval;
1938 /* Find the next child binfo to walk. */
1939 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1941 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1943 if (mark && pset->contains (base_binfo))
1944 continue;
1946 /* If the base is inherited via private or protected
1947 inheritance, then we can't see it, unless we are a friend of
1948 the current binfo. */
1949 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1951 tree scope;
1952 if (!friends_p)
1953 continue;
1954 scope = current_scope ();
1955 if (!scope
1956 || TREE_CODE (scope) == NAMESPACE_DECL
1957 || !is_friend (BINFO_TYPE (binfo), scope))
1958 continue;
1961 if (mark)
1962 pset->add (base_binfo);
1964 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1965 pre_fn, post_fn, data);
1966 if (rval)
1967 return rval;
1970 skip_bases:
1971 /* Call the post-order walking function. */
1972 if (post_fn)
1974 rval = post_fn (binfo, data);
1975 gcc_assert (rval != dfs_skip_bases);
1976 return rval;
1979 return NULL_TREE;
1982 /* Like dfs_walk_once except that only accessible bases are walked.
1983 FRIENDS_P indicates whether friendship of the local context
1984 should be considered when determining accessibility. */
1986 static tree
1987 dfs_walk_once_accessible (tree binfo, bool friends_p,
1988 tree (*pre_fn) (tree, void *),
1989 tree (*post_fn) (tree, void *), void *data)
1991 hash_set<tree> *pset = NULL;
1992 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1993 pset = new hash_set<tree>;
1994 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1995 pre_fn, post_fn, data);
1997 if (pset)
1998 delete pset;
1999 return rval;
2002 /* Return true iff the code of T is CODE, and it has compatible
2003 type with TYPE. */
2005 static bool
2006 matches_code_and_type_p (tree t, enum tree_code code, tree type)
2008 if (TREE_CODE (t) != code)
2009 return false;
2010 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
2011 return false;
2012 return true;
2015 /* Subroutine of direct_accessor_p and reference_accessor_p.
2016 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
2017 We expect a tree of the form:
2018 <component_ref:
2019 <indirect_ref:S>
2020 <nop_expr:P*
2021 <parm_decl (this)>
2022 <field_decl (FIELD_DECL)>>>. */
2024 static bool
2025 field_access_p (tree component_ref, tree field_decl, tree field_type)
2027 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
2028 return false;
2030 tree indirect_ref = TREE_OPERAND (component_ref, 0);
2031 if (TREE_CODE (indirect_ref) != INDIRECT_REF)
2032 return false;
2034 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
2035 if (!is_this_parameter (ptr))
2036 return false;
2038 /* Must access the correct field. */
2039 if (TREE_OPERAND (component_ref, 1) != field_decl)
2040 return false;
2041 return true;
2044 /* Subroutine of field_accessor_p.
2046 Assuming that INIT_EXPR has already had its code and type checked,
2047 determine if it is a simple accessor for FIELD_DECL
2048 (of type FIELD_TYPE).
2050 Specifically, a simple accessor within struct S of the form:
2051 T get_field () { return m_field; }
2052 should have a DECL_SAVED_TREE of the form:
2053 <return_expr
2054 <init_expr:T
2055 <result_decl:T
2056 <nop_expr:T
2057 <component_ref:
2058 <indirect_ref:S>
2059 <nop_expr:P*
2060 <parm_decl (this)>
2061 <field_decl (FIELD_DECL)>>>. */
2063 static bool
2064 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
2066 tree result_decl = TREE_OPERAND (init_expr, 0);
2067 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
2068 return false;
2070 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
2071 if (!field_access_p (component_ref, field_decl, field_type))
2072 return false;
2074 return true;
2077 /* Subroutine of field_accessor_p.
2079 Assuming that INIT_EXPR has already had its code and type checked,
2080 determine if it is a "reference" accessor for FIELD_DECL
2081 (of type FIELD_REFERENCE_TYPE).
2083 Specifically, a simple accessor within struct S of the form:
2084 T& get_field () { return m_field; }
2085 should have a DECL_SAVED_TREE of the form:
2086 <return_expr
2087 <init_expr:T&
2088 <result_decl:T&
2089 <nop_expr: T&
2090 <addr_expr: T*
2091 <component_ref:T
2092 <indirect_ref:S
2093 <nop_expr
2094 <parm_decl (this)>>
2095 <field (FIELD_DECL)>>>>>>. */
2096 static bool
2097 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
2098 tree field_reference_type)
2100 tree result_decl = TREE_OPERAND (init_expr, 0);
2101 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
2102 return false;
2104 tree field_pointer_type = build_pointer_type (field_type);
2105 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
2106 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
2107 return false;
2109 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
2111 if (!field_access_p (component_ref, field_decl, field_type))
2112 return false;
2114 return true;
2117 /* Return true if FN is an accessor method for FIELD_DECL.
2118 i.e. a method of the form { return FIELD; }, with no
2119 conversions.
2121 If CONST_P, then additionally require that FN be a const
2122 method. */
2124 static bool
2125 field_accessor_p (tree fn, tree field_decl, bool const_p)
2127 if (TREE_CODE (fn) != FUNCTION_DECL)
2128 return false;
2130 /* We don't yet support looking up static data, just fields. */
2131 if (TREE_CODE (field_decl) != FIELD_DECL)
2132 return false;
2134 tree fntype = TREE_TYPE (fn);
2135 if (TREE_CODE (fntype) != METHOD_TYPE)
2136 return false;
2138 /* If the field is accessed via a const "this" argument, verify
2139 that the "this" parameter is const. */
2140 if (const_p)
2142 tree this_type = type_of_this_parm (fntype);
2143 if (!TYPE_READONLY (this_type))
2144 return false;
2147 tree saved_tree = DECL_SAVED_TREE (fn);
2149 if (saved_tree == NULL_TREE)
2150 return false;
2152 if (TREE_CODE (saved_tree) != RETURN_EXPR)
2153 return false;
2155 tree init_expr = TREE_OPERAND (saved_tree, 0);
2156 if (TREE_CODE (init_expr) != INIT_EXPR)
2157 return false;
2159 /* Determine if this is a simple accessor within struct S of the form:
2160 T get_field () { return m_field; }. */
2161 tree field_type = TREE_TYPE (field_decl);
2162 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
2163 return direct_accessor_p (init_expr, field_decl, field_type);
2165 /* Failing that, determine if it is an accessor of the form:
2166 T& get_field () { return m_field; }. */
2167 tree field_reference_type = cp_build_reference_type (field_type, false);
2168 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
2169 return reference_accessor_p (init_expr, field_decl, field_type,
2170 field_reference_type);
2172 return false;
2175 /* Callback data for dfs_locate_field_accessor_pre. */
2177 struct locate_field_data
2179 locate_field_data (tree field_decl_, bool const_p_)
2180 : field_decl (field_decl_), const_p (const_p_) {}
2182 tree field_decl;
2183 bool const_p;
2186 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
2187 callable via binfo, if one exists, otherwise return NULL_TREE.
2189 Callback for dfs_walk_once_accessible for use within
2190 locate_field_accessor. */
2192 static tree
2193 dfs_locate_field_accessor_pre (tree binfo, void *data)
2195 locate_field_data *lfd = (locate_field_data *)data;
2196 tree type = BINFO_TYPE (binfo);
2198 vec<tree, va_gc> *method_vec;
2199 tree fn;
2200 size_t i;
2202 if (!CLASS_TYPE_P (type))
2203 return NULL_TREE;
2205 method_vec = CLASSTYPE_METHOD_VEC (type);
2206 if (!method_vec)
2207 return NULL_TREE;
2209 for (i = 0; vec_safe_iterate (method_vec, i, &fn); ++i)
2210 if (fn)
2211 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
2212 return fn;
2214 return NULL_TREE;
2217 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
2218 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
2220 tree
2221 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
2223 if (TREE_CODE (basetype_path) != TREE_BINFO)
2224 return NULL_TREE;
2226 /* Walk the hierarchy, looking for a method of some base class that allows
2227 access to the field. */
2228 locate_field_data lfd (field_decl, const_p);
2229 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
2230 dfs_locate_field_accessor_pre,
2231 NULL, &lfd);
2234 /* Check that virtual overrider OVERRIDER is acceptable for base function
2235 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2237 static int
2238 check_final_overrider (tree overrider, tree basefn)
2240 tree over_type = TREE_TYPE (overrider);
2241 tree base_type = TREE_TYPE (basefn);
2242 tree over_return = fndecl_declared_return_type (overrider);
2243 tree base_return = fndecl_declared_return_type (basefn);
2244 tree over_throw, base_throw;
2246 int fail = 0;
2248 if (DECL_INVALID_OVERRIDER_P (overrider))
2249 return 0;
2251 if (same_type_p (base_return, over_return))
2252 /* OK */;
2253 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
2254 || (TREE_CODE (base_return) == TREE_CODE (over_return)
2255 && POINTER_TYPE_P (base_return)))
2257 /* Potentially covariant. */
2258 unsigned base_quals, over_quals;
2260 fail = !POINTER_TYPE_P (base_return);
2261 if (!fail)
2263 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
2265 base_return = TREE_TYPE (base_return);
2266 over_return = TREE_TYPE (over_return);
2268 base_quals = cp_type_quals (base_return);
2269 over_quals = cp_type_quals (over_return);
2271 if ((base_quals & over_quals) != over_quals)
2272 fail = 1;
2274 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
2276 /* Strictly speaking, the standard requires the return type to be
2277 complete even if it only differs in cv-quals, but that seems
2278 like a bug in the wording. */
2279 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
2280 over_return))
2282 tree binfo = lookup_base (over_return, base_return,
2283 ba_check, NULL, tf_none);
2285 if (!binfo || binfo == error_mark_node)
2286 fail = 1;
2289 else if (can_convert_standard (TREE_TYPE (base_type),
2290 TREE_TYPE (over_type),
2291 tf_warning_or_error))
2292 /* GNU extension, allow trivial pointer conversions such as
2293 converting to void *, or qualification conversion. */
2295 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
2296 "invalid covariant return type for %q#D", overrider))
2297 inform (DECL_SOURCE_LOCATION (basefn),
2298 " overriding %q#D", basefn);
2300 else
2301 fail = 2;
2303 else
2304 fail = 2;
2305 if (!fail)
2306 /* OK */;
2307 else
2309 if (fail == 1)
2311 error ("invalid covariant return type for %q+#D", overrider);
2312 error (" overriding %q+#D", basefn);
2314 else
2316 error ("conflicting return type specified for %q+#D", overrider);
2317 error (" overriding %q+#D", basefn);
2319 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2320 return 0;
2323 /* Check throw specifier is at least as strict. */
2324 maybe_instantiate_noexcept (basefn);
2325 maybe_instantiate_noexcept (overrider);
2326 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
2327 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
2329 if (!comp_except_specs (base_throw, over_throw, ce_derived))
2331 error ("looser throw specifier for %q+#F", overrider);
2332 error (" overriding %q+#F", basefn);
2333 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2334 return 0;
2337 /* Check for conflicting type attributes. But leave transaction_safe for
2338 set_one_vmethod_tm_attributes. */
2339 if (!comp_type_attributes (over_type, base_type)
2340 && !tx_safe_fn_type_p (base_type)
2341 && !tx_safe_fn_type_p (over_type))
2343 error ("conflicting type attributes specified for %q+#D", overrider);
2344 error (" overriding %q+#D", basefn);
2345 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2346 return 0;
2349 /* A function declared transaction_safe_dynamic that overrides a function
2350 declared transaction_safe (but not transaction_safe_dynamic) is
2351 ill-formed. */
2352 if (tx_safe_fn_type_p (base_type)
2353 && lookup_attribute ("transaction_safe_dynamic",
2354 DECL_ATTRIBUTES (overrider))
2355 && !lookup_attribute ("transaction_safe_dynamic",
2356 DECL_ATTRIBUTES (basefn)))
2358 error_at (DECL_SOURCE_LOCATION (overrider),
2359 "%qD declared %<transaction_safe_dynamic%>", overrider);
2360 inform (DECL_SOURCE_LOCATION (basefn),
2361 "overriding %qD declared %<transaction_safe%>", basefn);
2364 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2366 if (DECL_DELETED_FN (overrider))
2368 error ("deleted function %q+D", overrider);
2369 error ("overriding non-deleted function %q+D", basefn);
2370 maybe_explain_implicit_delete (overrider);
2372 else
2374 error ("non-deleted function %q+D", overrider);
2375 error ("overriding deleted function %q+D", basefn);
2377 return 0;
2379 if (DECL_FINAL_P (basefn))
2381 error ("virtual function %q+D", overrider);
2382 error ("overriding final function %q+D", basefn);
2383 return 0;
2385 return 1;
2388 /* Given a class TYPE, and a function decl FNDECL, look for
2389 virtual functions in TYPE's hierarchy which FNDECL overrides.
2390 We do not look in TYPE itself, only its bases.
2392 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2393 find that it overrides anything.
2395 We check that every function which is overridden, is correctly
2396 overridden. */
2399 look_for_overrides (tree type, tree fndecl)
2401 tree binfo = TYPE_BINFO (type);
2402 tree base_binfo;
2403 int ix;
2404 int found = 0;
2406 /* A constructor for a class T does not override a function T
2407 in a base class. */
2408 if (DECL_CONSTRUCTOR_P (fndecl))
2409 return 0;
2411 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2413 tree basetype = BINFO_TYPE (base_binfo);
2415 if (TYPE_POLYMORPHIC_P (basetype))
2416 found += look_for_overrides_r (basetype, fndecl);
2418 return found;
2421 /* Look in TYPE for virtual functions with the same signature as
2422 FNDECL. */
2424 tree
2425 look_for_overrides_here (tree type, tree fndecl)
2427 int ix;
2429 /* If there are no methods in TYPE (meaning that only implicitly
2430 declared methods will ever be provided for TYPE), then there are
2431 no virtual functions. */
2432 if (!CLASSTYPE_METHOD_VEC (type))
2433 return NULL_TREE;
2435 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2436 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2437 else
2438 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2439 if (ix >= 0)
2440 for (ovl_iterator iter ((*CLASSTYPE_METHOD_VEC (type))[ix]); iter; ++iter)
2442 tree fn = *iter;
2444 if (!DECL_VIRTUAL_P (fn))
2445 /* Not a virtual. */;
2446 else if (DECL_CONTEXT (fn) != type)
2447 /* Introduced with a using declaration. */;
2448 else if (DECL_STATIC_FUNCTION_P (fndecl))
2450 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2451 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2452 if (compparms (TREE_CHAIN (btypes), dtypes))
2453 return fn;
2455 else if (same_signature_p (fndecl, fn))
2456 return fn;
2459 return NULL_TREE;
2462 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2463 TYPE itself and its bases. */
2465 static int
2466 look_for_overrides_r (tree type, tree fndecl)
2468 tree fn = look_for_overrides_here (type, fndecl);
2469 if (fn)
2471 if (DECL_STATIC_FUNCTION_P (fndecl))
2473 /* A static member function cannot match an inherited
2474 virtual member function. */
2475 error ("%q+#D cannot be declared", fndecl);
2476 error (" since %q+#D declared in base class", fn);
2478 else
2480 /* It's definitely virtual, even if not explicitly set. */
2481 DECL_VIRTUAL_P (fndecl) = 1;
2482 check_final_overrider (fndecl, fn);
2484 return 1;
2487 /* We failed to find one declared in this class. Look in its bases. */
2488 return look_for_overrides (type, fndecl);
2491 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2493 static tree
2494 dfs_get_pure_virtuals (tree binfo, void *data)
2496 tree type = (tree) data;
2498 /* We're not interested in primary base classes; the derived class
2499 of which they are a primary base will contain the information we
2500 need. */
2501 if (!BINFO_PRIMARY_P (binfo))
2503 tree virtuals;
2505 for (virtuals = BINFO_VIRTUALS (binfo);
2506 virtuals;
2507 virtuals = TREE_CHAIN (virtuals))
2508 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2509 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2512 return NULL_TREE;
2515 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2517 void
2518 get_pure_virtuals (tree type)
2520 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2521 is going to be overridden. */
2522 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2523 /* Now, run through all the bases which are not primary bases, and
2524 collect the pure virtual functions. We look at the vtable in
2525 each class to determine what pure virtual functions are present.
2526 (A primary base is not interesting because the derived class of
2527 which it is a primary base will contain vtable entries for the
2528 pure virtuals in the base class. */
2529 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2532 /* Debug info for C++ classes can get very large; try to avoid
2533 emitting it everywhere.
2535 Note that this optimization wins even when the target supports
2536 BINCL (if only slightly), and reduces the amount of work for the
2537 linker. */
2539 void
2540 maybe_suppress_debug_info (tree t)
2542 if (write_symbols == NO_DEBUG)
2543 return;
2545 /* We might have set this earlier in cp_finish_decl. */
2546 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2548 /* Always emit the information for each class every time. */
2549 if (flag_emit_class_debug_always)
2550 return;
2552 /* If we already know how we're handling this class, handle debug info
2553 the same way. */
2554 if (CLASSTYPE_INTERFACE_KNOWN (t))
2556 if (CLASSTYPE_INTERFACE_ONLY (t))
2557 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2558 /* else don't set it. */
2560 /* If the class has a vtable, write out the debug info along with
2561 the vtable. */
2562 else if (TYPE_CONTAINS_VPTR_P (t))
2563 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2565 /* Otherwise, just emit the debug info normally. */
2568 /* Note that we want debugging information for a base class of a class
2569 whose vtable is being emitted. Normally, this would happen because
2570 calling the constructor for a derived class implies calling the
2571 constructors for all bases, which involve initializing the
2572 appropriate vptr with the vtable for the base class; but in the
2573 presence of optimization, this initialization may be optimized
2574 away, so we tell finish_vtable_vardecl that we want the debugging
2575 information anyway. */
2577 static tree
2578 dfs_debug_mark (tree binfo, void * /*data*/)
2580 tree t = BINFO_TYPE (binfo);
2582 if (CLASSTYPE_DEBUG_REQUESTED (t))
2583 return dfs_skip_bases;
2585 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2587 return NULL_TREE;
2590 /* Write out the debugging information for TYPE, whose vtable is being
2591 emitted. Also walk through our bases and note that we want to
2592 write out information for them. This avoids the problem of not
2593 writing any debug info for intermediate basetypes whose
2594 constructors, and thus the references to their vtables, and thus
2595 the vtables themselves, were optimized away. */
2597 void
2598 note_debug_info_needed (tree type)
2600 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2602 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2603 rest_of_type_compilation (type, namespace_bindings_p ());
2606 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2609 void
2610 print_search_statistics (void)
2612 if (! GATHER_STATISTICS)
2614 fprintf (stderr, "no search statistics\n");
2615 return;
2618 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2619 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2620 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2621 n_outer_fields_searched, n_calls_lookup_fnfields);
2622 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2625 void
2626 reinit_search_statistics (void)
2628 n_fields_searched = 0;
2629 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2630 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2631 n_calls_get_base_type = 0;
2632 n_outer_fields_searched = 0;
2633 n_contexts_saved = 0;
2636 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2637 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2638 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2639 bases have been encountered already in the tree walk. PARENT_CONVS
2640 is the list of lists of conversion functions that could hide CONV
2641 and OTHER_CONVS is the list of lists of conversion functions that
2642 could hide or be hidden by CONV, should virtualness be involved in
2643 the hierarchy. Merely checking the conversion op's name is not
2644 enough because two conversion operators to the same type can have
2645 different names. Return nonzero if we are visible. */
2647 static int
2648 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2649 tree to_type, tree parent_convs, tree other_convs)
2651 tree level, probe;
2653 /* See if we are hidden by a parent conversion. */
2654 for (level = parent_convs; level; level = TREE_CHAIN (level))
2655 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2656 if (same_type_p (to_type, TREE_TYPE (probe)))
2657 return 0;
2659 if (virtual_depth || virtualness)
2661 /* In a virtual hierarchy, we could be hidden, or could hide a
2662 conversion function on the other_convs list. */
2663 for (level = other_convs; level; level = TREE_CHAIN (level))
2665 int we_hide_them;
2666 int they_hide_us;
2667 tree *prev, other;
2669 if (!(virtual_depth || TREE_STATIC (level)))
2670 /* Neither is morally virtual, so cannot hide each other. */
2671 continue;
2673 if (!TREE_VALUE (level))
2674 /* They evaporated away already. */
2675 continue;
2677 they_hide_us = (virtual_depth
2678 && original_binfo (binfo, TREE_PURPOSE (level)));
2679 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2680 && original_binfo (TREE_PURPOSE (level), binfo));
2682 if (!(we_hide_them || they_hide_us))
2683 /* Neither is within the other, so no hiding can occur. */
2684 continue;
2686 for (prev = &TREE_VALUE (level), other = *prev; other;)
2688 if (same_type_p (to_type, TREE_TYPE (other)))
2690 if (they_hide_us)
2691 /* We are hidden. */
2692 return 0;
2694 if (we_hide_them)
2696 /* We hide the other one. */
2697 other = TREE_CHAIN (other);
2698 *prev = other;
2699 continue;
2702 prev = &TREE_CHAIN (other);
2703 other = *prev;
2707 return 1;
2710 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2711 of conversion functions, the first slot will be for the current
2712 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2713 of conversion functions from children of the current binfo,
2714 concatenated with conversions from elsewhere in the hierarchy --
2715 that list begins with OTHER_CONVS. Return a single list of lists
2716 containing only conversions from the current binfo and its
2717 children. */
2719 static tree
2720 split_conversions (tree my_convs, tree parent_convs,
2721 tree child_convs, tree other_convs)
2723 tree t;
2724 tree prev;
2726 /* Remove the original other_convs portion from child_convs. */
2727 for (prev = NULL, t = child_convs;
2728 t != other_convs; prev = t, t = TREE_CHAIN (t))
2729 continue;
2731 if (prev)
2732 TREE_CHAIN (prev) = NULL_TREE;
2733 else
2734 child_convs = NULL_TREE;
2736 /* Attach the child convs to any we had at this level. */
2737 if (my_convs)
2739 my_convs = parent_convs;
2740 TREE_CHAIN (my_convs) = child_convs;
2742 else
2743 my_convs = child_convs;
2745 return my_convs;
2748 /* Worker for lookup_conversions. Lookup conversion functions in
2749 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2750 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2751 encountered virtual bases already in the tree walk. PARENT_CONVS &
2752 PARENT_TPL_CONVS are lists of list of conversions within parent
2753 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2754 elsewhere in the tree. Return the conversions found within this
2755 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2756 encountered virtualness. We keep template and non-template
2757 conversions separate, to avoid unnecessary type comparisons.
2759 The located conversion functions are held in lists of lists. The
2760 TREE_VALUE of the outer list is the list of conversion functions
2761 found in a particular binfo. The TREE_PURPOSE of both the outer
2762 and inner lists is the binfo at which those conversions were
2763 found. TREE_STATIC is set for those lists within of morally
2764 virtual binfos. The TREE_VALUE of the inner list is the conversion
2765 function or overload itself. The TREE_TYPE of each inner list node
2766 is the converted-to type. */
2768 static int
2769 lookup_conversions_r (tree binfo,
2770 int virtual_depth, int virtualness,
2771 tree parent_convs, tree parent_tpl_convs,
2772 tree other_convs, tree other_tpl_convs,
2773 tree *convs, tree *tpl_convs)
2775 int my_virtualness = 0;
2776 tree my_convs = NULL_TREE;
2777 tree my_tpl_convs = NULL_TREE;
2778 tree child_convs = NULL_TREE;
2779 tree child_tpl_convs = NULL_TREE;
2780 unsigned i;
2781 tree base_binfo;
2782 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2783 tree conv;
2785 /* If we have no conversion operators, then don't look. */
2786 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2788 *convs = *tpl_convs = NULL_TREE;
2790 return 0;
2793 if (BINFO_VIRTUAL_P (binfo))
2794 virtual_depth++;
2796 /* First, locate the unhidden ones at this level. */
2797 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2798 vec_safe_iterate (method_vec, i, &conv);
2799 ++i)
2801 tree cur = OVL_FIRST (conv);
2803 if (!DECL_CONV_FN_P (cur))
2804 break;
2806 if (TREE_CODE (cur) == TEMPLATE_DECL)
2807 /* Only template conversions can be overloaded, and we must
2808 flatten them out and check each one individually. */
2809 for (ovl_iterator iter (conv); iter; ++iter)
2811 tree tpl = *iter;
2812 tree type = DECL_CONV_FN_TYPE (tpl);
2814 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2815 type, parent_tpl_convs, other_tpl_convs))
2817 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2818 TREE_TYPE (my_tpl_convs) = type;
2819 if (virtual_depth)
2821 TREE_STATIC (my_tpl_convs) = 1;
2822 my_virtualness = 1;
2826 else
2828 tree name = DECL_NAME (cur);
2830 if (!IDENTIFIER_MARKED (name))
2832 tree type = DECL_CONV_FN_TYPE (cur);
2833 if (type_uses_auto (type))
2835 mark_used (cur);
2836 type = DECL_CONV_FN_TYPE (cur);
2839 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2840 type, parent_convs, other_convs))
2842 my_convs = tree_cons (binfo, conv, my_convs);
2843 TREE_TYPE (my_convs) = type;
2844 if (virtual_depth)
2846 TREE_STATIC (my_convs) = 1;
2847 my_virtualness = 1;
2849 IDENTIFIER_MARKED (name) = 1;
2855 if (my_convs)
2857 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2858 if (virtual_depth)
2859 TREE_STATIC (parent_convs) = 1;
2862 if (my_tpl_convs)
2864 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2865 if (virtual_depth)
2866 TREE_STATIC (parent_tpl_convs) = 1;
2869 child_convs = other_convs;
2870 child_tpl_convs = other_tpl_convs;
2872 /* Now iterate over each base, looking for more conversions. */
2873 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2875 tree base_convs, base_tpl_convs;
2876 unsigned base_virtualness;
2878 base_virtualness = lookup_conversions_r (base_binfo,
2879 virtual_depth, virtualness,
2880 parent_convs, parent_tpl_convs,
2881 child_convs, child_tpl_convs,
2882 &base_convs, &base_tpl_convs);
2883 if (base_virtualness)
2884 my_virtualness = virtualness = 1;
2885 child_convs = chainon (base_convs, child_convs);
2886 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2889 /* Unmark the conversions found at this level */
2890 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2891 IDENTIFIER_MARKED (OVL_NAME (TREE_VALUE (conv))) = 0;
2893 *convs = split_conversions (my_convs, parent_convs,
2894 child_convs, other_convs);
2895 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2896 child_tpl_convs, other_tpl_convs);
2898 return my_virtualness;
2901 /* Return a TREE_LIST containing all the non-hidden user-defined
2902 conversion functions for TYPE (and its base-classes). The
2903 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2904 function. The TREE_PURPOSE is the BINFO from which the conversion
2905 functions in this node were selected. This function is effectively
2906 performing a set of member lookups as lookup_fnfield does, but
2907 using the type being converted to as the unique key, rather than the
2908 field name. */
2910 tree
2911 lookup_conversions (tree type)
2913 tree convs, tpl_convs;
2914 tree list = NULL_TREE;
2916 complete_type (type);
2917 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2918 return NULL_TREE;
2920 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2921 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2922 &convs, &tpl_convs);
2924 /* Flatten the list-of-lists */
2925 for (; convs; convs = TREE_CHAIN (convs))
2927 tree probe, next;
2929 for (probe = TREE_VALUE (convs); probe; probe = next)
2931 next = TREE_CHAIN (probe);
2933 TREE_CHAIN (probe) = list;
2934 list = probe;
2938 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2940 tree probe, next;
2942 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2944 next = TREE_CHAIN (probe);
2946 TREE_CHAIN (probe) = list;
2947 list = probe;
2951 return list;
2954 /* Returns the binfo of the first direct or indirect virtual base derived
2955 from BINFO, or NULL if binfo is not via virtual. */
2957 tree
2958 binfo_from_vbase (tree binfo)
2960 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2962 if (BINFO_VIRTUAL_P (binfo))
2963 return binfo;
2965 return NULL_TREE;
2968 /* Returns the binfo of the first direct or indirect virtual base derived
2969 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2970 via virtual. */
2972 tree
2973 binfo_via_virtual (tree binfo, tree limit)
2975 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2976 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2977 return NULL_TREE;
2979 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2980 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2982 if (BINFO_VIRTUAL_P (binfo))
2983 return binfo;
2985 return NULL_TREE;
2988 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2989 direct base. */
2991 bool
2992 binfo_direct_p (tree binfo)
2994 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2995 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2996 /* A second inheritance chain means indirect. */
2997 return false;
2998 if (!BINFO_VIRTUAL_P (binfo))
2999 /* Non-virtual, so only one inheritance chain means direct. */
3000 return true;
3001 /* A virtual base looks like a direct base, so we need to look through the
3002 direct bases to see if it's there. */
3003 tree b_binfo;
3004 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
3005 if (b_binfo == binfo)
3006 return true;
3007 return false;
3010 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
3011 Find the equivalent binfo within whatever graph HERE is located.
3012 This is the inverse of original_binfo. */
3014 tree
3015 copied_binfo (tree binfo, tree here)
3017 tree result = NULL_TREE;
3019 if (BINFO_VIRTUAL_P (binfo))
3021 tree t;
3023 for (t = here; BINFO_INHERITANCE_CHAIN (t);
3024 t = BINFO_INHERITANCE_CHAIN (t))
3025 continue;
3027 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
3029 else if (BINFO_INHERITANCE_CHAIN (binfo))
3031 tree cbinfo;
3032 tree base_binfo;
3033 int ix;
3035 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
3036 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
3037 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
3039 result = base_binfo;
3040 break;
3043 else
3045 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
3046 result = here;
3049 gcc_assert (result);
3050 return result;
3053 tree
3054 binfo_for_vbase (tree base, tree t)
3056 unsigned ix;
3057 tree binfo;
3058 vec<tree, va_gc> *vbases;
3060 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
3061 vec_safe_iterate (vbases, ix, &binfo); ix++)
3062 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
3063 return binfo;
3064 return NULL;
3067 /* BINFO is some base binfo of HERE, within some other
3068 hierarchy. Return the equivalent binfo, but in the hierarchy
3069 dominated by HERE. This is the inverse of copied_binfo. If BINFO
3070 is not a base binfo of HERE, returns NULL_TREE. */
3072 tree
3073 original_binfo (tree binfo, tree here)
3075 tree result = NULL;
3077 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
3078 result = here;
3079 else if (BINFO_VIRTUAL_P (binfo))
3080 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
3081 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
3082 : NULL_TREE);
3083 else if (BINFO_INHERITANCE_CHAIN (binfo))
3085 tree base_binfos;
3087 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
3088 if (base_binfos)
3090 int ix;
3091 tree base_binfo;
3093 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
3094 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
3095 BINFO_TYPE (binfo)))
3097 result = base_binfo;
3098 break;
3103 return result;
3106 /* True iff TYPE has any dependent bases (and therefore we can't say
3107 definitively that another class is not a base of an instantiation of
3108 TYPE). */
3110 bool
3111 any_dependent_bases_p (tree type)
3113 if (!type || !CLASS_TYPE_P (type) || !processing_template_decl)
3114 return false;
3116 unsigned i;
3117 tree base_binfo;
3118 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
3119 if (BINFO_DEPENDENT_BASE_P (base_binfo))
3120 return true;
3122 return false;