libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / search.c
blob2630150bfe60599101b0ad7ddfd70b0c9bf4b711
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;
510 if (current_class_type)
511 return current_class_type;
512 if (current_function_decl)
513 return current_function_decl;
514 return current_namespace;
517 /* Returns nonzero if we are currently in a function scope. Note
518 that this function returns zero if we are within a local class, but
519 not within a member function body of the local class. */
522 at_function_scope_p (void)
524 tree cs = current_scope ();
525 /* Also check cfun to make sure that we're really compiling
526 this function (as opposed to having set current_function_decl
527 for access checking or some such). */
528 return (cs && TREE_CODE (cs) == FUNCTION_DECL
529 && cfun && cfun->decl == current_function_decl);
532 /* Returns true if the innermost active scope is a class scope. */
534 bool
535 at_class_scope_p (void)
537 tree cs = current_scope ();
538 return cs && TYPE_P (cs);
541 /* Returns true if the innermost active scope is a namespace scope. */
543 bool
544 at_namespace_scope_p (void)
546 tree cs = current_scope ();
547 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
550 /* Return the scope of DECL, as appropriate when doing name-lookup. */
552 tree
553 context_for_name_lookup (tree decl)
555 /* [class.union]
557 For the purposes of name lookup, after the anonymous union
558 definition, the members of the anonymous union are considered to
559 have been defined in the scope in which the anonymous union is
560 declared. */
561 tree context = DECL_CONTEXT (decl);
563 while (context && TYPE_P (context)
564 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
565 context = TYPE_CONTEXT (context);
566 if (!context)
567 context = global_namespace;
569 return context;
572 /* Returns true iff DECL is declared in TYPE. */
574 static bool
575 member_declared_in_type (tree decl, tree type)
577 /* A normal declaration obviously counts. */
578 if (context_for_name_lookup (decl) == type)
579 return true;
580 /* So does a using or access declaration. */
581 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
582 && purpose_member (type, DECL_ACCESS (decl)))
583 return true;
584 return false;
587 /* The accessibility routines use BINFO_ACCESS for scratch space
588 during the computation of the accessibility of some declaration. */
590 /* Avoid walking up past a declaration of the member. */
592 static tree
593 dfs_access_in_type_pre (tree binfo, void *data)
595 tree decl = (tree) data;
596 tree type = BINFO_TYPE (binfo);
597 if (member_declared_in_type (decl, type))
598 return dfs_skip_bases;
599 return NULL_TREE;
602 #define BINFO_ACCESS(NODE) \
603 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
605 /* Set the access associated with NODE to ACCESS. */
607 #define SET_BINFO_ACCESS(NODE, ACCESS) \
608 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
609 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
611 /* Called from access_in_type via dfs_walk. Calculate the access to
612 DATA (which is really a DECL) in BINFO. */
614 static tree
615 dfs_access_in_type (tree binfo, void *data)
617 tree decl = (tree) data;
618 tree type = BINFO_TYPE (binfo);
619 access_kind access = ak_none;
621 if (context_for_name_lookup (decl) == type)
623 /* If we have descended to the scope of DECL, just note the
624 appropriate access. */
625 if (TREE_PRIVATE (decl))
626 access = ak_private;
627 else if (TREE_PROTECTED (decl))
628 access = ak_protected;
629 else
630 access = ak_public;
632 else
634 /* First, check for an access-declaration that gives us more
635 access to the DECL. */
636 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
638 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
640 if (decl_access)
642 decl_access = TREE_VALUE (decl_access);
644 if (decl_access == access_public_node)
645 access = ak_public;
646 else if (decl_access == access_protected_node)
647 access = ak_protected;
648 else if (decl_access == access_private_node)
649 access = ak_private;
650 else
651 gcc_unreachable ();
655 if (!access)
657 int i;
658 tree base_binfo;
659 vec<tree, va_gc> *accesses;
661 /* Otherwise, scan our baseclasses, and pick the most favorable
662 access. */
663 accesses = BINFO_BASE_ACCESSES (binfo);
664 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
666 tree base_access = (*accesses)[i];
667 access_kind base_access_now = BINFO_ACCESS (base_binfo);
669 if (base_access_now == ak_none || base_access_now == ak_private)
670 /* If it was not accessible in the base, or only
671 accessible as a private member, we can't access it
672 all. */
673 base_access_now = ak_none;
674 else if (base_access == access_protected_node)
675 /* Public and protected members in the base become
676 protected here. */
677 base_access_now = ak_protected;
678 else if (base_access == access_private_node)
679 /* Public and protected members in the base become
680 private here. */
681 base_access_now = ak_private;
683 /* See if the new access, via this base, gives more
684 access than our previous best access. */
685 if (base_access_now != ak_none
686 && (access == ak_none || base_access_now < access))
688 access = base_access_now;
690 /* If the new access is public, we can't do better. */
691 if (access == ak_public)
692 break;
698 /* Note the access to DECL in TYPE. */
699 SET_BINFO_ACCESS (binfo, access);
701 return NULL_TREE;
704 /* Return the access to DECL in TYPE. */
706 static access_kind
707 access_in_type (tree type, tree decl)
709 tree binfo = TYPE_BINFO (type);
711 /* We must take into account
713 [class.paths]
715 If a name can be reached by several paths through a multiple
716 inheritance graph, the access is that of the path that gives
717 most access.
719 The algorithm we use is to make a post-order depth-first traversal
720 of the base-class hierarchy. As we come up the tree, we annotate
721 each node with the most lenient access. */
722 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
724 return BINFO_ACCESS (binfo);
727 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
728 of OTYPE in the context of DERIVED. */
730 static int
731 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
733 /* We're checking this clause from [class.access.base]
735 m as a member of N is protected, and the reference occurs in a
736 member or friend of class N, or in a member or friend of a
737 class P derived from N, where m as a member of P is public, private
738 or protected.
740 Here DERIVED is a possible P, DECL is m and TYPE is N. */
742 /* If DERIVED isn't derived from N, then it can't be a P. */
743 if (!DERIVED_FROM_P (type, derived))
744 return 0;
746 /* [class.protected]
748 When a friend or a member function of a derived class references
749 a protected nonstatic member of a base class, an access check
750 applies in addition to those described earlier in clause
751 _class.access_) Except when forming a pointer to member
752 (_expr.unary.op_), the access must be through a pointer to,
753 reference to, or object of the derived class itself (or any class
754 derived from that class) (_expr.ref_). If the access is to form
755 a pointer to member, the nested-name-specifier shall name the
756 derived class (or any class derived from that class). */
757 if (DECL_NONSTATIC_MEMBER_P (decl)
758 && !DERIVED_FROM_P (derived, otype))
759 return 0;
761 return 1;
764 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
765 to access DECL through TYPE. OTYPE is the type of the object. */
767 static int
768 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
770 /* We're checking this clause from [class.access.base]
772 m as a member of N is protected, and the reference occurs in a
773 member or friend of class N, or in a member or friend of a
774 class P derived from N, where m as a member of P is public, private
775 or protected.
777 Here DECL is m and TYPE is N. SCOPE is the current context,
778 and we check all its possible Ps. */
779 tree befriending_classes;
780 tree t;
782 if (!scope)
783 return 0;
785 if (is_global_friend (scope))
786 return 1;
788 /* Is SCOPE itself a suitable P? */
789 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
790 return 1;
792 if (DECL_DECLARES_FUNCTION_P (scope))
793 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
794 else if (TYPE_P (scope))
795 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
796 else
797 return 0;
799 for (t = befriending_classes; t; t = TREE_CHAIN (t))
800 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
801 return 1;
803 /* Nested classes have the same access as their enclosing types, as
804 per DR 45 (this is a change from C++98). */
805 if (TYPE_P (scope))
806 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
807 return 1;
809 if (DECL_DECLARES_FUNCTION_P (scope))
811 /* Perhaps this SCOPE is a member of a class which is a
812 friend. */
813 if (DECL_CLASS_SCOPE_P (scope)
814 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
815 return 1;
818 /* Maybe scope's template is a friend. */
819 if (tree tinfo = get_template_info (scope))
821 tree tmpl = TI_TEMPLATE (tinfo);
822 if (DECL_CLASS_TEMPLATE_P (tmpl))
823 tmpl = TREE_TYPE (tmpl);
824 else
825 tmpl = DECL_TEMPLATE_RESULT (tmpl);
826 if (tmpl != scope)
828 /* Increment processing_template_decl to make sure that
829 dependent_type_p works correctly. */
830 ++processing_template_decl;
831 int ret = friend_accessible_p (tmpl, decl, type, otype);
832 --processing_template_decl;
833 if (ret)
834 return 1;
838 /* If is_friend is true, we should have found a befriending class. */
839 gcc_checking_assert (!is_friend (type, scope));
841 return 0;
844 struct dfs_accessible_data
846 tree decl;
847 tree object_type;
850 /* Avoid walking up past a declaration of the member. */
852 static tree
853 dfs_accessible_pre (tree binfo, void *data)
855 dfs_accessible_data *d = (dfs_accessible_data *)data;
856 tree type = BINFO_TYPE (binfo);
857 if (member_declared_in_type (d->decl, type))
858 return dfs_skip_bases;
859 return NULL_TREE;
862 /* Called via dfs_walk_once_accessible from accessible_p */
864 static tree
865 dfs_accessible_post (tree binfo, void *data)
867 /* access_in_type already set BINFO_ACCESS for us. */
868 access_kind access = BINFO_ACCESS (binfo);
869 tree N = BINFO_TYPE (binfo);
870 dfs_accessible_data *d = (dfs_accessible_data *)data;
871 tree decl = d->decl;
872 tree scope = current_nonlambda_scope ();
874 /* A member m is accessible at the point R when named in class N if */
875 switch (access)
877 case ak_none:
878 return NULL_TREE;
880 case ak_public:
881 /* m as a member of N is public, or */
882 return binfo;
884 case ak_private:
886 /* m as a member of N is private, and R occurs in a member or friend of
887 class N, or */
888 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
889 && is_friend (N, scope))
890 return binfo;
891 return NULL_TREE;
894 case ak_protected:
896 /* m as a member of N is protected, and R occurs in a member or friend
897 of class N, or in a member or friend of a class P derived from N,
898 where m as a member of P is public, private, or protected */
899 if (friend_accessible_p (scope, decl, N, d->object_type))
900 return binfo;
901 return NULL_TREE;
904 default:
905 gcc_unreachable ();
909 /* Like accessible_p below, but within a template returns true iff DECL is
910 accessible in TYPE to all possible instantiations of the template. */
913 accessible_in_template_p (tree type, tree decl)
915 int save_ptd = processing_template_decl;
916 processing_template_decl = 0;
917 int val = accessible_p (type, decl, false);
918 processing_template_decl = save_ptd;
919 return val;
922 /* DECL is a declaration from a base class of TYPE, which was the
923 class used to name DECL. Return nonzero if, in the current
924 context, DECL is accessible. If TYPE is actually a BINFO node,
925 then we can tell in what context the access is occurring by looking
926 at the most derived class along the path indicated by BINFO. If
927 CONSIDER_LOCAL is true, do consider special access the current
928 scope or friendship thereof we might have. */
931 accessible_p (tree type, tree decl, bool consider_local_p)
933 tree binfo;
934 access_kind access;
936 /* If this declaration is in a block or namespace scope, there's no
937 access control. */
938 if (!TYPE_P (context_for_name_lookup (decl)))
939 return 1;
941 /* There is no need to perform access checks inside a thunk. */
942 if (current_function_decl && DECL_THUNK_P (current_function_decl))
943 return 1;
945 /* In a template declaration, we cannot be sure whether the
946 particular specialization that is instantiated will be a friend
947 or not. Therefore, all access checks are deferred until
948 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
949 parameter list for a template (because we may see dependent types
950 in default arguments for template parameters), and access
951 checking should be performed in the outermost parameter list. */
952 if (processing_template_decl
953 && !expanding_concept ()
954 && (!processing_template_parmlist || processing_template_decl > 1))
955 return 1;
957 tree otype = NULL_TREE;
958 if (!TYPE_P (type))
960 /* When accessing a non-static member, the most derived type in the
961 binfo chain is the type of the object; remember that type for
962 protected_accessible_p. */
963 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
964 otype = BINFO_TYPE (b);
965 type = BINFO_TYPE (type);
967 else
968 otype = type;
970 /* [class.access.base]
972 A member m is accessible when named in class N if
974 --m as a member of N is public, or
976 --m as a member of N is private, and the reference occurs in a
977 member or friend of class N, or
979 --m as a member of N is protected, and the reference occurs in a
980 member or friend of class N, or in a member or friend of a
981 class P derived from N, where m as a member of P is public, private or
982 protected, or
984 --there exists a base class B of N that is accessible at the point
985 of reference, and m is accessible when named in class B.
987 We walk the base class hierarchy, checking these conditions. */
989 /* We walk using TYPE_BINFO (type) because access_in_type will set
990 BINFO_ACCESS on it and its bases. */
991 binfo = TYPE_BINFO (type);
993 /* Compute the accessibility of DECL in the class hierarchy
994 dominated by type. */
995 access = access_in_type (type, decl);
996 if (access == ak_public)
997 return 1;
999 /* If we aren't considering the point of reference, only the first bullet
1000 applies. */
1001 if (!consider_local_p)
1002 return 0;
1004 dfs_accessible_data d = { decl, otype };
1006 /* Walk the hierarchy again, looking for a base class that allows
1007 access. */
1008 return dfs_walk_once_accessible (binfo, /*friends=*/true,
1009 dfs_accessible_pre,
1010 dfs_accessible_post, &d)
1011 != NULL_TREE;
1014 struct lookup_field_info {
1015 /* The type in which we're looking. */
1016 tree type;
1017 /* The name of the field for which we're looking. */
1018 tree name;
1019 /* If non-NULL, the current result of the lookup. */
1020 tree rval;
1021 /* The path to RVAL. */
1022 tree rval_binfo;
1023 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1024 candidates. */
1025 tree ambiguous;
1026 /* If nonzero, we are looking for types, not data members. */
1027 int want_type;
1028 /* If something went wrong, a message indicating what. */
1029 const char *errstr;
1032 /* Nonzero for a class member means that it is shared between all objects
1033 of that class.
1035 [class.member.lookup]:If the resulting set of declarations are not all
1036 from sub-objects of the same type, or the set has a nonstatic member
1037 and includes members from distinct sub-objects, there is an ambiguity
1038 and the program is ill-formed.
1040 This function checks that T contains no nonstatic members. */
1043 shared_member_p (tree t)
1045 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
1046 || TREE_CODE (t) == CONST_DECL)
1047 return 1;
1048 if (is_overloaded_fn (t))
1050 for (ovl_iterator iter (get_fns (t)); iter; ++iter)
1051 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter))
1052 return 0;
1053 return 1;
1055 return 0;
1058 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1059 found as a base class and sub-object of the object denoted by
1060 BINFO. */
1062 static int
1063 is_subobject_of_p (tree parent, tree binfo)
1065 tree probe;
1067 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1069 if (probe == binfo)
1070 return 1;
1071 if (BINFO_VIRTUAL_P (probe))
1072 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1073 != NULL_TREE);
1075 return 0;
1078 /* DATA is really a struct lookup_field_info. Look for a field with
1079 the name indicated there in BINFO. If this function returns a
1080 non-NULL value it is the result of the lookup. Called from
1081 lookup_field via breadth_first_search. */
1083 static tree
1084 lookup_field_r (tree binfo, void *data)
1086 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1087 tree type = BINFO_TYPE (binfo);
1088 tree nval = NULL_TREE;
1090 /* If this is a dependent base, don't look in it. */
1091 if (BINFO_DEPENDENT_BASE_P (binfo))
1092 return NULL_TREE;
1094 /* If this base class is hidden by the best-known value so far, we
1095 don't need to look. */
1096 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1097 && !BINFO_VIRTUAL_P (binfo))
1098 return dfs_skip_bases;
1100 /* First, look for a function. There can't be a function and a data
1101 member with the same name, and if there's a function and a type
1102 with the same name, the type is hidden by the function. */
1103 if (!lfi->want_type)
1104 nval = lookup_fnfields_slot (type, lfi->name);
1106 if (!nval)
1107 /* Look for a data member or type. */
1108 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1109 else if (TREE_CODE (nval) == OVERLOAD && OVL_USING_P (nval))
1111 /* If we have both dependent and non-dependent using-declarations, return
1112 the dependent one rather than an incomplete list of functions. */
1113 tree dep_using = lookup_field_1 (type, lfi->name, lfi->want_type);
1114 if (dep_using && TREE_CODE (dep_using) == USING_DECL)
1115 nval = dep_using;
1118 /* If there is no declaration with the indicated name in this type,
1119 then there's nothing to do. */
1120 if (!nval)
1121 goto done;
1123 /* If we're looking up a type (as with an elaborated type specifier)
1124 we ignore all non-types we find. */
1125 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1127 if (lfi->name == TYPE_IDENTIFIER (type))
1129 /* If the aggregate has no user defined constructors, we allow
1130 it to have fields with the same name as the enclosing type.
1131 If we are looking for that name, find the corresponding
1132 TYPE_DECL. */
1133 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1134 if (DECL_NAME (nval) == lfi->name
1135 && TREE_CODE (nval) == TYPE_DECL)
1136 break;
1138 else
1139 nval = NULL_TREE;
1140 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1142 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1143 lfi->name);
1144 if (e != NULL)
1145 nval = TYPE_MAIN_DECL (e->type);
1146 else
1147 goto done;
1151 /* If the lookup already found a match, and the new value doesn't
1152 hide the old one, we might have an ambiguity. */
1153 if (lfi->rval_binfo
1154 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1157 if (nval == lfi->rval && shared_member_p (nval))
1158 /* The two things are really the same. */
1160 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1161 /* The previous value hides the new one. */
1163 else
1165 /* We have a real ambiguity. We keep a chain of all the
1166 candidates. */
1167 if (!lfi->ambiguous && lfi->rval)
1169 /* This is the first time we noticed an ambiguity. Add
1170 what we previously thought was a reasonable candidate
1171 to the list. */
1172 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1173 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1176 /* Add the new value. */
1177 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1178 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1179 lfi->errstr = G_("request for member %qD is ambiguous");
1182 else
1184 lfi->rval = nval;
1185 lfi->rval_binfo = binfo;
1188 done:
1189 /* Don't look for constructors or destructors in base classes. */
1190 if (IDENTIFIER_CDTOR_P (lfi->name))
1191 return dfs_skip_bases;
1192 return NULL_TREE;
1195 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1196 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1197 FUNCTIONS, and OPTYPE respectively. */
1199 tree
1200 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1202 tree baselink;
1204 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1205 || TREE_CODE (functions) == TEMPLATE_DECL
1206 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1207 || TREE_CODE (functions) == OVERLOAD);
1208 gcc_assert (!optype || TYPE_P (optype));
1209 gcc_assert (TREE_TYPE (functions));
1211 baselink = make_node (BASELINK);
1212 TREE_TYPE (baselink) = TREE_TYPE (functions);
1213 BASELINK_BINFO (baselink) = binfo;
1214 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1215 BASELINK_FUNCTIONS (baselink) = functions;
1216 BASELINK_OPTYPE (baselink) = optype;
1218 return baselink;
1221 /* Look for a member named NAME in an inheritance lattice dominated by
1222 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1223 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1224 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1225 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1226 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1227 TREE_VALUEs are the list of ambiguous candidates.
1229 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1231 If nothing can be found return NULL_TREE and do not issue an error.
1233 If non-NULL, failure information is written back to AFI. */
1235 tree
1236 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1237 tsubst_flags_t complain, access_failure_info *afi)
1239 tree rval, rval_binfo = NULL_TREE;
1240 tree type = NULL_TREE, basetype_path = NULL_TREE;
1241 struct lookup_field_info lfi;
1243 /* rval_binfo is the binfo associated with the found member, note,
1244 this can be set with useful information, even when rval is not
1245 set, because it must deal with ALL members, not just non-function
1246 members. It is used for ambiguity checking and the hidden
1247 checks. Whereas rval is only set if a proper (not hidden)
1248 non-function member is found. */
1250 const char *errstr = 0;
1252 if (name == error_mark_node
1253 || xbasetype == NULL_TREE
1254 || xbasetype == error_mark_node)
1255 return NULL_TREE;
1257 gcc_assert (identifier_p (name));
1259 if (TREE_CODE (xbasetype) == TREE_BINFO)
1261 type = BINFO_TYPE (xbasetype);
1262 basetype_path = xbasetype;
1264 else
1266 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1267 return NULL_TREE;
1268 type = xbasetype;
1269 xbasetype = NULL_TREE;
1272 type = complete_type (type);
1274 /* Make sure we're looking for a member of the current instantiation in the
1275 right partial specialization. */
1276 if (flag_concepts && dependent_type_p (type))
1277 if (tree t = currently_open_class (type))
1278 type = t;
1280 if (!basetype_path)
1281 basetype_path = TYPE_BINFO (type);
1283 if (!basetype_path)
1284 return NULL_TREE;
1286 if (GATHER_STATISTICS)
1287 n_calls_lookup_field++;
1289 memset (&lfi, 0, sizeof (lfi));
1290 lfi.type = type;
1291 lfi.name = name;
1292 lfi.want_type = want_type;
1293 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1294 rval = lfi.rval;
1295 rval_binfo = lfi.rval_binfo;
1296 if (rval_binfo)
1297 type = BINFO_TYPE (rval_binfo);
1298 errstr = lfi.errstr;
1300 /* If we are not interested in ambiguities, don't report them;
1301 just return NULL_TREE. */
1302 if (!protect && lfi.ambiguous)
1303 return NULL_TREE;
1305 if (protect == 2)
1307 if (lfi.ambiguous)
1308 return lfi.ambiguous;
1309 else
1310 protect = 0;
1313 /* [class.access]
1315 In the case of overloaded function names, access control is
1316 applied to the function selected by overloaded resolution.
1318 We cannot check here, even if RVAL is only a single non-static
1319 member function, since we do not know what the "this" pointer
1320 will be. For:
1322 class A { protected: void f(); };
1323 class B : public A {
1324 void g(A *p) {
1325 f(); // OK
1326 p->f(); // Not OK.
1330 only the first call to "f" is valid. However, if the function is
1331 static, we can check. */
1332 if (rval && protect
1333 && !really_overloaded_fn (rval))
1335 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1336 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1337 && !perform_or_defer_access_check (basetype_path, decl, decl,
1338 complain, afi))
1339 rval = error_mark_node;
1342 if (errstr && protect)
1344 if (complain & tf_error)
1346 error (errstr, name, type);
1347 if (lfi.ambiguous)
1348 print_candidates (lfi.ambiguous);
1350 rval = error_mark_node;
1353 if (rval && is_overloaded_fn (rval))
1354 rval = build_baselink (rval_binfo, basetype_path, rval,
1355 (IDENTIFIER_CONV_OP_P (name)
1356 ? TREE_TYPE (name): NULL_TREE));
1357 return rval;
1360 /* Helper class for lookup_member_fuzzy. */
1362 class lookup_field_fuzzy_info
1364 public:
1365 lookup_field_fuzzy_info (bool want_type_p) :
1366 m_want_type_p (want_type_p), m_candidates () {}
1368 void fuzzy_lookup_fnfields (tree type);
1369 void fuzzy_lookup_field (tree type);
1371 /* If true, we are looking for types, not data members. */
1372 bool m_want_type_p;
1373 /* The result: a vec of identifiers. */
1374 auto_vec<tree> m_candidates;
1377 /* Locate all methods within TYPE, append them to m_candidates. */
1379 void
1380 lookup_field_fuzzy_info::fuzzy_lookup_fnfields (tree type)
1382 vec<tree, va_gc> *method_vec;
1383 tree fn;
1384 size_t i;
1386 if (!CLASS_TYPE_P (type))
1387 return;
1389 method_vec = CLASSTYPE_METHOD_VEC (type);
1390 if (!method_vec)
1391 return;
1393 for (i = 0; vec_safe_iterate (method_vec, i, &fn); ++i)
1394 if (fn)
1395 m_candidates.safe_push (OVL_NAME (fn));
1398 /* Locate all fields within TYPE, append them to m_candidates. */
1400 void
1401 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1403 if (!CLASS_TYPE_P (type))
1404 return;
1406 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1408 if (!m_want_type_p || DECL_DECLARES_TYPE_P (field))
1409 if (DECL_NAME (field))
1410 m_candidates.safe_push (DECL_NAME (field));
1415 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1416 DATA is really a lookup_field_fuzzy_info. Look for a field with
1417 the name indicated there in BINFO. Gathers pertinent identifiers into
1418 m_candidates. */
1420 static tree
1421 lookup_field_fuzzy_r (tree binfo, void *data)
1423 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1424 tree type = BINFO_TYPE (binfo);
1426 /* First, look for functions. */
1427 if (!lffi->m_want_type_p)
1428 lffi->fuzzy_lookup_fnfields (type);
1430 /* Look for data member and types. */
1431 lffi->fuzzy_lookup_field (type);
1433 return NULL_TREE;
1436 /* Like lookup_member, but try to find the closest match for NAME,
1437 rather than an exact match, and return an identifier (or NULL_TREE).
1438 Do not complain. */
1440 tree
1441 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1443 tree type = NULL_TREE, basetype_path = NULL_TREE;
1444 struct lookup_field_fuzzy_info lffi (want_type_p);
1446 /* rval_binfo is the binfo associated with the found member, note,
1447 this can be set with useful information, even when rval is not
1448 set, because it must deal with ALL members, not just non-function
1449 members. It is used for ambiguity checking and the hidden
1450 checks. Whereas rval is only set if a proper (not hidden)
1451 non-function member is found. */
1453 if (name == error_mark_node
1454 || xbasetype == NULL_TREE
1455 || xbasetype == error_mark_node)
1456 return NULL_TREE;
1458 gcc_assert (identifier_p (name));
1460 if (TREE_CODE (xbasetype) == TREE_BINFO)
1462 type = BINFO_TYPE (xbasetype);
1463 basetype_path = xbasetype;
1465 else
1467 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1468 return NULL_TREE;
1469 type = xbasetype;
1470 xbasetype = NULL_TREE;
1473 type = complete_type (type);
1475 /* Make sure we're looking for a member of the current instantiation in the
1476 right partial specialization. */
1477 if (flag_concepts && dependent_type_p (type))
1478 type = currently_open_class (type);
1480 if (!basetype_path)
1481 basetype_path = TYPE_BINFO (type);
1483 if (!basetype_path)
1484 return NULL_TREE;
1486 /* Populate lffi.m_candidates. */
1487 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1489 return find_closest_identifier (name, &lffi.m_candidates);
1492 /* Like lookup_member, except that if we find a function member we
1493 return NULL_TREE. */
1495 tree
1496 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1498 tree rval = lookup_member (xbasetype, name, protect, want_type,
1499 tf_warning_or_error);
1501 /* Ignore functions, but propagate the ambiguity list. */
1502 if (!error_operand_p (rval)
1503 && (rval && BASELINK_P (rval)))
1504 return NULL_TREE;
1506 return rval;
1509 /* Like lookup_member, except that if we find a non-function member we
1510 return NULL_TREE. */
1512 tree
1513 lookup_fnfields (tree xbasetype, tree name, int protect)
1515 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1516 tf_warning_or_error);
1518 /* Ignore non-functions, but propagate the ambiguity list. */
1519 if (!error_operand_p (rval)
1520 && (rval && !BASELINK_P (rval)))
1521 return NULL_TREE;
1523 return rval;
1526 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1527 corresponding to "operator TYPE ()", or -1 if there is no such
1528 operator. Only CLASS_TYPE itself is searched; this routine does
1529 not scan the base classes of CLASS_TYPE. */
1531 static int
1532 lookup_conversion_operator (tree class_type, tree type)
1534 int tpl_slot = -1;
1536 if (TYPE_HAS_CONVERSION (class_type))
1538 int i;
1539 tree fn;
1540 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1542 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1543 vec_safe_iterate (methods, i, &fn); ++i)
1545 /* All the conversion operators come near the beginning of
1546 the class. Therefore, if FN is not a conversion
1547 operator, there is no matching conversion operator in
1548 CLASS_TYPE. */
1549 fn = OVL_FIRST (fn);
1550 if (!DECL_CONV_FN_P (fn))
1551 break;
1553 if (TREE_CODE (fn) == TEMPLATE_DECL)
1554 /* All the templated conversion functions are on the same
1555 slot, so remember it. */
1556 tpl_slot = i;
1557 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1558 return i;
1562 return tpl_slot;
1565 /* TYPE is a class type. Return the index of the fields within
1566 the method vector with name NAME, or -1 if no such field exists.
1567 Does not lazily declare implicitly-declared member functions. */
1569 static int
1570 lookup_fnfields_idx_nolazy (tree type, tree name)
1572 vec<tree, va_gc> *method_vec;
1573 tree fn;
1574 size_t i;
1576 if (!CLASS_TYPE_P (type))
1577 return -1;
1579 method_vec = CLASSTYPE_METHOD_VEC (type);
1580 if (!method_vec)
1581 return -1;
1583 if (GATHER_STATISTICS)
1584 n_calls_lookup_fnfields_1++;
1586 /* Constructors are first... */
1587 if (name == ctor_identifier)
1589 fn = CLASSTYPE_CONSTRUCTORS (type);
1590 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1592 /* and destructors are second. */
1593 if (name == dtor_identifier)
1595 fn = CLASSTYPE_DESTRUCTORS (type);
1596 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1598 if (IDENTIFIER_CONV_OP_P (name))
1599 return lookup_conversion_operator (type, TREE_TYPE (name));
1601 /* Skip the conversion operators. */
1602 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1603 vec_safe_iterate (method_vec, i, &fn);
1604 ++i)
1605 if (!DECL_CONV_FN_P (OVL_FIRST (fn)))
1606 break;
1608 /* If the type is complete, use binary search. */
1609 if (COMPLETE_TYPE_P (type))
1611 int lo;
1612 int hi;
1614 lo = i;
1615 hi = method_vec->length ();
1616 while (lo < hi)
1618 i = (lo + hi) / 2;
1620 if (GATHER_STATISTICS)
1621 n_outer_fields_searched++;
1623 tree tmp = (*method_vec)[i];
1624 tmp = OVL_NAME (tmp);
1625 if (tmp > name)
1626 hi = i;
1627 else if (tmp < name)
1628 lo = i + 1;
1629 else
1630 return i;
1633 else
1634 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1636 if (GATHER_STATISTICS)
1637 n_outer_fields_searched++;
1638 if (OVL_NAME (fn) == name)
1639 return i;
1642 return -1;
1645 /* TYPE is a class type. Return the index of the fields within
1646 the method vector with name NAME, or -1 if no such field exists. */
1649 lookup_fnfields_1 (tree type, tree name)
1651 if (!CLASS_TYPE_P (type))
1652 return -1;
1654 if (COMPLETE_TYPE_P (type))
1656 if (IDENTIFIER_CTOR_P (name))
1658 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1659 lazily_declare_fn (sfk_constructor, type);
1660 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1661 lazily_declare_fn (sfk_copy_constructor, type);
1662 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1663 lazily_declare_fn (sfk_move_constructor, type);
1665 else if (name == cp_assignment_operator_id (NOP_EXPR))
1667 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1668 lazily_declare_fn (sfk_copy_assignment, type);
1669 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1670 lazily_declare_fn (sfk_move_assignment, type);
1672 else if (IDENTIFIER_DTOR_P (name))
1674 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
1675 lazily_declare_fn (sfk_destructor, type);
1679 return lookup_fnfields_idx_nolazy (type, name);
1682 /* TYPE is a class type. Return the field within the method vector with
1683 name NAME, or NULL_TREE if no such field exists. */
1685 tree
1686 lookup_fnfields_slot (tree type, tree name)
1688 int ix = lookup_fnfields_1 (complete_type (type), name);
1689 if (ix < 0)
1690 return NULL_TREE;
1691 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1694 /* As above, but avoid lazily declaring functions. */
1696 tree
1697 lookup_fnfields_slot_nolazy (tree type, tree name)
1699 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1700 if (ix < 0)
1701 return NULL_TREE;
1702 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1705 /* Like lookup_fnfields_1, except that the name is extracted from
1706 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1709 class_method_index_for_fn (tree class_type, tree function)
1711 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1713 return lookup_fnfields_1 (class_type,
1714 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1715 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1716 DECL_NAME (function));
1720 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1721 the class or namespace used to qualify the name. CONTEXT_CLASS is
1722 the class corresponding to the object in which DECL will be used.
1723 Return a possibly modified version of DECL that takes into account
1724 the CONTEXT_CLASS.
1726 In particular, consider an expression like `B::m' in the context of
1727 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1728 then the most derived class indicated by the BASELINK_BINFO will be
1729 `B', not `D'. This function makes that adjustment. */
1731 tree
1732 adjust_result_of_qualified_name_lookup (tree decl,
1733 tree qualifying_scope,
1734 tree context_class)
1736 if (context_class && context_class != error_mark_node
1737 && CLASS_TYPE_P (context_class)
1738 && CLASS_TYPE_P (qualifying_scope)
1739 && DERIVED_FROM_P (qualifying_scope, context_class)
1740 && BASELINK_P (decl))
1742 tree base;
1744 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1745 Because we do not yet know which function will be chosen by
1746 overload resolution, we cannot yet check either accessibility
1747 or ambiguity -- in either case, the choice of a static member
1748 function might make the usage valid. */
1749 base = lookup_base (context_class, qualifying_scope,
1750 ba_unique, NULL, tf_none);
1751 if (base && base != error_mark_node)
1753 BASELINK_ACCESS_BINFO (decl) = base;
1754 tree decl_binfo
1755 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1756 ba_unique, NULL, tf_none);
1757 if (decl_binfo && decl_binfo != error_mark_node)
1758 BASELINK_BINFO (decl) = decl_binfo;
1762 if (BASELINK_P (decl))
1763 BASELINK_QUALIFIED_P (decl) = true;
1765 return decl;
1769 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1770 PRE_FN is called in preorder, while POST_FN is called in postorder.
1771 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1772 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1773 that value is immediately returned and the walk is terminated. One
1774 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1775 POST_FN are passed the binfo to examine and the caller's DATA
1776 value. All paths are walked, thus virtual and morally virtual
1777 binfos can be multiply walked. */
1779 tree
1780 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1781 tree (*post_fn) (tree, void *), void *data)
1783 tree rval;
1784 unsigned ix;
1785 tree base_binfo;
1787 /* Call the pre-order walking function. */
1788 if (pre_fn)
1790 rval = pre_fn (binfo, data);
1791 if (rval)
1793 if (rval == dfs_skip_bases)
1794 goto skip_bases;
1795 return rval;
1799 /* Find the next child binfo to walk. */
1800 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1802 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1803 if (rval)
1804 return rval;
1807 skip_bases:
1808 /* Call the post-order walking function. */
1809 if (post_fn)
1811 rval = post_fn (binfo, data);
1812 gcc_assert (rval != dfs_skip_bases);
1813 return rval;
1816 return NULL_TREE;
1819 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1820 that binfos are walked at most once. */
1822 static tree
1823 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1824 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1825 void *data)
1827 tree rval;
1828 unsigned ix;
1829 tree base_binfo;
1831 /* Call the pre-order walking function. */
1832 if (pre_fn)
1834 rval = pre_fn (binfo, data);
1835 if (rval)
1837 if (rval == dfs_skip_bases)
1838 goto skip_bases;
1840 return rval;
1844 /* Find the next child binfo to walk. */
1845 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1847 if (BINFO_VIRTUAL_P (base_binfo))
1848 if (pset->add (base_binfo))
1849 continue;
1851 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1852 if (rval)
1853 return rval;
1856 skip_bases:
1857 /* Call the post-order walking function. */
1858 if (post_fn)
1860 rval = post_fn (binfo, data);
1861 gcc_assert (rval != dfs_skip_bases);
1862 return rval;
1865 return NULL_TREE;
1868 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1869 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1870 For diamond shaped hierarchies we must mark the virtual bases, to
1871 avoid multiple walks. */
1873 tree
1874 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1875 tree (*post_fn) (tree, void *), void *data)
1877 static int active = 0; /* We must not be called recursively. */
1878 tree rval;
1880 gcc_assert (pre_fn || post_fn);
1881 gcc_assert (!active);
1882 active++;
1884 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1885 /* We are not diamond shaped, and therefore cannot encounter the
1886 same binfo twice. */
1887 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1888 else
1890 hash_set<tree> pset;
1891 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1894 active--;
1896 return rval;
1899 /* Worker function for dfs_walk_once_accessible. Behaves like
1900 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1901 access given by the current context should be considered, (b) ONCE
1902 indicates whether bases should be marked during traversal. */
1904 static tree
1905 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1906 tree (*pre_fn) (tree, void *),
1907 tree (*post_fn) (tree, void *), void *data)
1909 tree rval = NULL_TREE;
1910 unsigned ix;
1911 tree base_binfo;
1913 /* Call the pre-order walking function. */
1914 if (pre_fn)
1916 rval = pre_fn (binfo, data);
1917 if (rval)
1919 if (rval == dfs_skip_bases)
1920 goto skip_bases;
1922 return rval;
1926 /* Find the next child binfo to walk. */
1927 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1929 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1931 if (mark && pset->contains (base_binfo))
1932 continue;
1934 /* If the base is inherited via private or protected
1935 inheritance, then we can't see it, unless we are a friend of
1936 the current binfo. */
1937 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1939 tree scope;
1940 if (!friends_p)
1941 continue;
1942 scope = current_scope ();
1943 if (!scope
1944 || TREE_CODE (scope) == NAMESPACE_DECL
1945 || !is_friend (BINFO_TYPE (binfo), scope))
1946 continue;
1949 if (mark)
1950 pset->add (base_binfo);
1952 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1953 pre_fn, post_fn, data);
1954 if (rval)
1955 return rval;
1958 skip_bases:
1959 /* Call the post-order walking function. */
1960 if (post_fn)
1962 rval = post_fn (binfo, data);
1963 gcc_assert (rval != dfs_skip_bases);
1964 return rval;
1967 return NULL_TREE;
1970 /* Like dfs_walk_once except that only accessible bases are walked.
1971 FRIENDS_P indicates whether friendship of the local context
1972 should be considered when determining accessibility. */
1974 static tree
1975 dfs_walk_once_accessible (tree binfo, bool friends_p,
1976 tree (*pre_fn) (tree, void *),
1977 tree (*post_fn) (tree, void *), void *data)
1979 hash_set<tree> *pset = NULL;
1980 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1981 pset = new hash_set<tree>;
1982 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1983 pre_fn, post_fn, data);
1985 if (pset)
1986 delete pset;
1987 return rval;
1990 /* Return true iff the code of T is CODE, and it has compatible
1991 type with TYPE. */
1993 static bool
1994 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1996 if (TREE_CODE (t) != code)
1997 return false;
1998 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1999 return false;
2000 return true;
2003 /* Subroutine of direct_accessor_p and reference_accessor_p.
2004 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
2005 We expect a tree of the form:
2006 <component_ref:
2007 <indirect_ref:S>
2008 <nop_expr:P*
2009 <parm_decl (this)>
2010 <field_decl (FIELD_DECL)>>>. */
2012 static bool
2013 field_access_p (tree component_ref, tree field_decl, tree field_type)
2015 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
2016 return false;
2018 tree indirect_ref = TREE_OPERAND (component_ref, 0);
2019 if (TREE_CODE (indirect_ref) != INDIRECT_REF)
2020 return false;
2022 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
2023 if (!is_this_parameter (ptr))
2024 return false;
2026 /* Must access the correct field. */
2027 if (TREE_OPERAND (component_ref, 1) != field_decl)
2028 return false;
2029 return true;
2032 /* Subroutine of field_accessor_p.
2034 Assuming that INIT_EXPR has already had its code and type checked,
2035 determine if it is a simple accessor for FIELD_DECL
2036 (of type FIELD_TYPE).
2038 Specifically, a simple accessor within struct S of the form:
2039 T get_field () { return m_field; }
2040 should have a DECL_SAVED_TREE of the form:
2041 <return_expr
2042 <init_expr:T
2043 <result_decl:T
2044 <nop_expr:T
2045 <component_ref:
2046 <indirect_ref:S>
2047 <nop_expr:P*
2048 <parm_decl (this)>
2049 <field_decl (FIELD_DECL)>>>. */
2051 static bool
2052 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
2054 tree result_decl = TREE_OPERAND (init_expr, 0);
2055 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
2056 return false;
2058 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
2059 if (!field_access_p (component_ref, field_decl, field_type))
2060 return false;
2062 return true;
2065 /* Subroutine of field_accessor_p.
2067 Assuming that INIT_EXPR has already had its code and type checked,
2068 determine if it is a "reference" accessor for FIELD_DECL
2069 (of type FIELD_REFERENCE_TYPE).
2071 Specifically, a simple accessor within struct S of the form:
2072 T& get_field () { return m_field; }
2073 should have a DECL_SAVED_TREE of the form:
2074 <return_expr
2075 <init_expr:T&
2076 <result_decl:T&
2077 <nop_expr: T&
2078 <addr_expr: T*
2079 <component_ref:T
2080 <indirect_ref:S
2081 <nop_expr
2082 <parm_decl (this)>>
2083 <field (FIELD_DECL)>>>>>>. */
2084 static bool
2085 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
2086 tree field_reference_type)
2088 tree result_decl = TREE_OPERAND (init_expr, 0);
2089 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
2090 return false;
2092 tree field_pointer_type = build_pointer_type (field_type);
2093 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
2094 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
2095 return false;
2097 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
2099 if (!field_access_p (component_ref, field_decl, field_type))
2100 return false;
2102 return true;
2105 /* Return true if FN is an accessor method for FIELD_DECL.
2106 i.e. a method of the form { return FIELD; }, with no
2107 conversions.
2109 If CONST_P, then additionally require that FN be a const
2110 method. */
2112 static bool
2113 field_accessor_p (tree fn, tree field_decl, bool const_p)
2115 if (TREE_CODE (fn) != FUNCTION_DECL)
2116 return false;
2118 /* We don't yet support looking up static data, just fields. */
2119 if (TREE_CODE (field_decl) != FIELD_DECL)
2120 return false;
2122 tree fntype = TREE_TYPE (fn);
2123 if (TREE_CODE (fntype) != METHOD_TYPE)
2124 return false;
2126 /* If the field is accessed via a const "this" argument, verify
2127 that the "this" parameter is const. */
2128 if (const_p)
2130 tree this_type = type_of_this_parm (fntype);
2131 if (!TYPE_READONLY (this_type))
2132 return false;
2135 tree saved_tree = DECL_SAVED_TREE (fn);
2137 if (saved_tree == NULL_TREE)
2138 return false;
2140 if (TREE_CODE (saved_tree) != RETURN_EXPR)
2141 return false;
2143 tree init_expr = TREE_OPERAND (saved_tree, 0);
2144 if (TREE_CODE (init_expr) != INIT_EXPR)
2145 return false;
2147 /* Determine if this is a simple accessor within struct S of the form:
2148 T get_field () { return m_field; }. */
2149 tree field_type = TREE_TYPE (field_decl);
2150 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
2151 return direct_accessor_p (init_expr, field_decl, field_type);
2153 /* Failing that, determine if it is an accessor of the form:
2154 T& get_field () { return m_field; }. */
2155 tree field_reference_type = cp_build_reference_type (field_type, false);
2156 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
2157 return reference_accessor_p (init_expr, field_decl, field_type,
2158 field_reference_type);
2160 return false;
2163 /* Callback data for dfs_locate_field_accessor_pre. */
2165 struct locate_field_data
2167 locate_field_data (tree field_decl_, bool const_p_)
2168 : field_decl (field_decl_), const_p (const_p_) {}
2170 tree field_decl;
2171 bool const_p;
2174 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
2175 callable via binfo, if one exists, otherwise return NULL_TREE.
2177 Callback for dfs_walk_once_accessible for use within
2178 locate_field_accessor. */
2180 static tree
2181 dfs_locate_field_accessor_pre (tree binfo, void *data)
2183 locate_field_data *lfd = (locate_field_data *)data;
2184 tree type = BINFO_TYPE (binfo);
2186 vec<tree, va_gc> *method_vec;
2187 tree fn;
2188 size_t i;
2190 if (!CLASS_TYPE_P (type))
2191 return NULL_TREE;
2193 method_vec = CLASSTYPE_METHOD_VEC (type);
2194 if (!method_vec)
2195 return NULL_TREE;
2197 for (i = 0; vec_safe_iterate (method_vec, i, &fn); ++i)
2198 if (fn)
2199 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
2200 return fn;
2202 return NULL_TREE;
2205 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
2206 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
2208 tree
2209 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
2211 if (TREE_CODE (basetype_path) != TREE_BINFO)
2212 return NULL_TREE;
2214 /* Walk the hierarchy, looking for a method of some base class that allows
2215 access to the field. */
2216 locate_field_data lfd (field_decl, const_p);
2217 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
2218 dfs_locate_field_accessor_pre,
2219 NULL, &lfd);
2222 /* Check that virtual overrider OVERRIDER is acceptable for base function
2223 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2225 static int
2226 check_final_overrider (tree overrider, tree basefn)
2228 tree over_type = TREE_TYPE (overrider);
2229 tree base_type = TREE_TYPE (basefn);
2230 tree over_return = fndecl_declared_return_type (overrider);
2231 tree base_return = fndecl_declared_return_type (basefn);
2232 tree over_throw, base_throw;
2234 int fail = 0;
2236 if (DECL_INVALID_OVERRIDER_P (overrider))
2237 return 0;
2239 if (same_type_p (base_return, over_return))
2240 /* OK */;
2241 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
2242 || (TREE_CODE (base_return) == TREE_CODE (over_return)
2243 && POINTER_TYPE_P (base_return)))
2245 /* Potentially covariant. */
2246 unsigned base_quals, over_quals;
2248 fail = !POINTER_TYPE_P (base_return);
2249 if (!fail)
2251 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
2253 base_return = TREE_TYPE (base_return);
2254 over_return = TREE_TYPE (over_return);
2256 base_quals = cp_type_quals (base_return);
2257 over_quals = cp_type_quals (over_return);
2259 if ((base_quals & over_quals) != over_quals)
2260 fail = 1;
2262 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
2264 /* Strictly speaking, the standard requires the return type to be
2265 complete even if it only differs in cv-quals, but that seems
2266 like a bug in the wording. */
2267 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
2268 over_return))
2270 tree binfo = lookup_base (over_return, base_return,
2271 ba_check, NULL, tf_none);
2273 if (!binfo || binfo == error_mark_node)
2274 fail = 1;
2277 else if (can_convert_standard (TREE_TYPE (base_type),
2278 TREE_TYPE (over_type),
2279 tf_warning_or_error))
2280 /* GNU extension, allow trivial pointer conversions such as
2281 converting to void *, or qualification conversion. */
2283 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
2284 "invalid covariant return type for %q#D", overrider))
2285 inform (DECL_SOURCE_LOCATION (basefn),
2286 " overriding %q#D", basefn);
2288 else
2289 fail = 2;
2291 else
2292 fail = 2;
2293 if (!fail)
2294 /* OK */;
2295 else
2297 if (fail == 1)
2299 error ("invalid covariant return type for %q+#D", overrider);
2300 error (" overriding %q+#D", basefn);
2302 else
2304 error ("conflicting return type specified for %q+#D", overrider);
2305 error (" overriding %q+#D", basefn);
2307 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2308 return 0;
2311 /* Check throw specifier is at least as strict. */
2312 maybe_instantiate_noexcept (basefn);
2313 maybe_instantiate_noexcept (overrider);
2314 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
2315 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
2317 if (!comp_except_specs (base_throw, over_throw, ce_derived))
2319 error ("looser throw specifier for %q+#F", overrider);
2320 error (" overriding %q+#F", basefn);
2321 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2322 return 0;
2325 /* Check for conflicting type attributes. But leave transaction_safe for
2326 set_one_vmethod_tm_attributes. */
2327 if (!comp_type_attributes (over_type, base_type)
2328 && !tx_safe_fn_type_p (base_type)
2329 && !tx_safe_fn_type_p (over_type))
2331 error ("conflicting type attributes specified for %q+#D", overrider);
2332 error (" overriding %q+#D", basefn);
2333 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2334 return 0;
2337 /* A function declared transaction_safe_dynamic that overrides a function
2338 declared transaction_safe (but not transaction_safe_dynamic) is
2339 ill-formed. */
2340 if (tx_safe_fn_type_p (base_type)
2341 && lookup_attribute ("transaction_safe_dynamic",
2342 DECL_ATTRIBUTES (overrider))
2343 && !lookup_attribute ("transaction_safe_dynamic",
2344 DECL_ATTRIBUTES (basefn)))
2346 error_at (DECL_SOURCE_LOCATION (overrider),
2347 "%qD declared %<transaction_safe_dynamic%>", overrider);
2348 inform (DECL_SOURCE_LOCATION (basefn),
2349 "overriding %qD declared %<transaction_safe%>", basefn);
2352 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2354 if (DECL_DELETED_FN (overrider))
2356 error ("deleted function %q+D", overrider);
2357 error ("overriding non-deleted function %q+D", basefn);
2358 maybe_explain_implicit_delete (overrider);
2360 else
2362 error ("non-deleted function %q+D", overrider);
2363 error ("overriding deleted function %q+D", basefn);
2365 return 0;
2367 if (DECL_FINAL_P (basefn))
2369 error ("virtual function %q+D", overrider);
2370 error ("overriding final function %q+D", basefn);
2371 return 0;
2373 return 1;
2376 /* Given a class TYPE, and a function decl FNDECL, look for
2377 virtual functions in TYPE's hierarchy which FNDECL overrides.
2378 We do not look in TYPE itself, only its bases.
2380 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2381 find that it overrides anything.
2383 We check that every function which is overridden, is correctly
2384 overridden. */
2387 look_for_overrides (tree type, tree fndecl)
2389 tree binfo = TYPE_BINFO (type);
2390 tree base_binfo;
2391 int ix;
2392 int found = 0;
2394 /* A constructor for a class T does not override a function T
2395 in a base class. */
2396 if (DECL_CONSTRUCTOR_P (fndecl))
2397 return 0;
2399 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2401 tree basetype = BINFO_TYPE (base_binfo);
2403 if (TYPE_POLYMORPHIC_P (basetype))
2404 found += look_for_overrides_r (basetype, fndecl);
2406 return found;
2409 /* Look in TYPE for virtual functions with the same signature as
2410 FNDECL. */
2412 tree
2413 look_for_overrides_here (tree type, tree fndecl)
2415 int ix;
2417 /* If there are no methods in TYPE (meaning that only implicitly
2418 declared methods will ever be provided for TYPE), then there are
2419 no virtual functions. */
2420 if (!CLASSTYPE_METHOD_VEC (type))
2421 return NULL_TREE;
2423 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2424 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2425 else
2426 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2427 if (ix >= 0)
2428 for (ovl_iterator iter ((*CLASSTYPE_METHOD_VEC (type))[ix]); iter; ++iter)
2430 tree fn = *iter;
2432 if (!DECL_VIRTUAL_P (fn))
2433 /* Not a virtual. */;
2434 else if (DECL_CONTEXT (fn) != type)
2435 /* Introduced with a using declaration. */;
2436 else if (DECL_STATIC_FUNCTION_P (fndecl))
2438 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2439 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2440 if (compparms (TREE_CHAIN (btypes), dtypes))
2441 return fn;
2443 else if (same_signature_p (fndecl, fn))
2444 return fn;
2447 return NULL_TREE;
2450 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2451 TYPE itself and its bases. */
2453 static int
2454 look_for_overrides_r (tree type, tree fndecl)
2456 tree fn = look_for_overrides_here (type, fndecl);
2457 if (fn)
2459 if (DECL_STATIC_FUNCTION_P (fndecl))
2461 /* A static member function cannot match an inherited
2462 virtual member function. */
2463 error ("%q+#D cannot be declared", fndecl);
2464 error (" since %q+#D declared in base class", fn);
2466 else
2468 /* It's definitely virtual, even if not explicitly set. */
2469 DECL_VIRTUAL_P (fndecl) = 1;
2470 check_final_overrider (fndecl, fn);
2472 return 1;
2475 /* We failed to find one declared in this class. Look in its bases. */
2476 return look_for_overrides (type, fndecl);
2479 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2481 static tree
2482 dfs_get_pure_virtuals (tree binfo, void *data)
2484 tree type = (tree) data;
2486 /* We're not interested in primary base classes; the derived class
2487 of which they are a primary base will contain the information we
2488 need. */
2489 if (!BINFO_PRIMARY_P (binfo))
2491 tree virtuals;
2493 for (virtuals = BINFO_VIRTUALS (binfo);
2494 virtuals;
2495 virtuals = TREE_CHAIN (virtuals))
2496 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2497 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2500 return NULL_TREE;
2503 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2505 void
2506 get_pure_virtuals (tree type)
2508 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2509 is going to be overridden. */
2510 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2511 /* Now, run through all the bases which are not primary bases, and
2512 collect the pure virtual functions. We look at the vtable in
2513 each class to determine what pure virtual functions are present.
2514 (A primary base is not interesting because the derived class of
2515 which it is a primary base will contain vtable entries for the
2516 pure virtuals in the base class. */
2517 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2520 /* Debug info for C++ classes can get very large; try to avoid
2521 emitting it everywhere.
2523 Note that this optimization wins even when the target supports
2524 BINCL (if only slightly), and reduces the amount of work for the
2525 linker. */
2527 void
2528 maybe_suppress_debug_info (tree t)
2530 if (write_symbols == NO_DEBUG)
2531 return;
2533 /* We might have set this earlier in cp_finish_decl. */
2534 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2536 /* Always emit the information for each class every time. */
2537 if (flag_emit_class_debug_always)
2538 return;
2540 /* If we already know how we're handling this class, handle debug info
2541 the same way. */
2542 if (CLASSTYPE_INTERFACE_KNOWN (t))
2544 if (CLASSTYPE_INTERFACE_ONLY (t))
2545 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2546 /* else don't set it. */
2548 /* If the class has a vtable, write out the debug info along with
2549 the vtable. */
2550 else if (TYPE_CONTAINS_VPTR_P (t))
2551 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2553 /* Otherwise, just emit the debug info normally. */
2556 /* Note that we want debugging information for a base class of a class
2557 whose vtable is being emitted. Normally, this would happen because
2558 calling the constructor for a derived class implies calling the
2559 constructors for all bases, which involve initializing the
2560 appropriate vptr with the vtable for the base class; but in the
2561 presence of optimization, this initialization may be optimized
2562 away, so we tell finish_vtable_vardecl that we want the debugging
2563 information anyway. */
2565 static tree
2566 dfs_debug_mark (tree binfo, void * /*data*/)
2568 tree t = BINFO_TYPE (binfo);
2570 if (CLASSTYPE_DEBUG_REQUESTED (t))
2571 return dfs_skip_bases;
2573 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2575 return NULL_TREE;
2578 /* Write out the debugging information for TYPE, whose vtable is being
2579 emitted. Also walk through our bases and note that we want to
2580 write out information for them. This avoids the problem of not
2581 writing any debug info for intermediate basetypes whose
2582 constructors, and thus the references to their vtables, and thus
2583 the vtables themselves, were optimized away. */
2585 void
2586 note_debug_info_needed (tree type)
2588 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2590 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2591 rest_of_type_compilation (type, namespace_bindings_p ());
2594 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2597 void
2598 print_search_statistics (void)
2600 if (! GATHER_STATISTICS)
2602 fprintf (stderr, "no search statistics\n");
2603 return;
2606 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2607 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2608 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2609 n_outer_fields_searched, n_calls_lookup_fnfields);
2610 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2613 void
2614 reinit_search_statistics (void)
2616 n_fields_searched = 0;
2617 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2618 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2619 n_calls_get_base_type = 0;
2620 n_outer_fields_searched = 0;
2621 n_contexts_saved = 0;
2624 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2625 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2626 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2627 bases have been encountered already in the tree walk. PARENT_CONVS
2628 is the list of lists of conversion functions that could hide CONV
2629 and OTHER_CONVS is the list of lists of conversion functions that
2630 could hide or be hidden by CONV, should virtualness be involved in
2631 the hierarchy. Merely checking the conversion op's name is not
2632 enough because two conversion operators to the same type can have
2633 different names. Return nonzero if we are visible. */
2635 static int
2636 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2637 tree to_type, tree parent_convs, tree other_convs)
2639 tree level, probe;
2641 /* See if we are hidden by a parent conversion. */
2642 for (level = parent_convs; level; level = TREE_CHAIN (level))
2643 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2644 if (same_type_p (to_type, TREE_TYPE (probe)))
2645 return 0;
2647 if (virtual_depth || virtualness)
2649 /* In a virtual hierarchy, we could be hidden, or could hide a
2650 conversion function on the other_convs list. */
2651 for (level = other_convs; level; level = TREE_CHAIN (level))
2653 int we_hide_them;
2654 int they_hide_us;
2655 tree *prev, other;
2657 if (!(virtual_depth || TREE_STATIC (level)))
2658 /* Neither is morally virtual, so cannot hide each other. */
2659 continue;
2661 if (!TREE_VALUE (level))
2662 /* They evaporated away already. */
2663 continue;
2665 they_hide_us = (virtual_depth
2666 && original_binfo (binfo, TREE_PURPOSE (level)));
2667 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2668 && original_binfo (TREE_PURPOSE (level), binfo));
2670 if (!(we_hide_them || they_hide_us))
2671 /* Neither is within the other, so no hiding can occur. */
2672 continue;
2674 for (prev = &TREE_VALUE (level), other = *prev; other;)
2676 if (same_type_p (to_type, TREE_TYPE (other)))
2678 if (they_hide_us)
2679 /* We are hidden. */
2680 return 0;
2682 if (we_hide_them)
2684 /* We hide the other one. */
2685 other = TREE_CHAIN (other);
2686 *prev = other;
2687 continue;
2690 prev = &TREE_CHAIN (other);
2691 other = *prev;
2695 return 1;
2698 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2699 of conversion functions, the first slot will be for the current
2700 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2701 of conversion functions from children of the current binfo,
2702 concatenated with conversions from elsewhere in the hierarchy --
2703 that list begins with OTHER_CONVS. Return a single list of lists
2704 containing only conversions from the current binfo and its
2705 children. */
2707 static tree
2708 split_conversions (tree my_convs, tree parent_convs,
2709 tree child_convs, tree other_convs)
2711 tree t;
2712 tree prev;
2714 /* Remove the original other_convs portion from child_convs. */
2715 for (prev = NULL, t = child_convs;
2716 t != other_convs; prev = t, t = TREE_CHAIN (t))
2717 continue;
2719 if (prev)
2720 TREE_CHAIN (prev) = NULL_TREE;
2721 else
2722 child_convs = NULL_TREE;
2724 /* Attach the child convs to any we had at this level. */
2725 if (my_convs)
2727 my_convs = parent_convs;
2728 TREE_CHAIN (my_convs) = child_convs;
2730 else
2731 my_convs = child_convs;
2733 return my_convs;
2736 /* Worker for lookup_conversions. Lookup conversion functions in
2737 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2738 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2739 encountered virtual bases already in the tree walk. PARENT_CONVS &
2740 PARENT_TPL_CONVS are lists of list of conversions within parent
2741 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2742 elsewhere in the tree. Return the conversions found within this
2743 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2744 encountered virtualness. We keep template and non-template
2745 conversions separate, to avoid unnecessary type comparisons.
2747 The located conversion functions are held in lists of lists. The
2748 TREE_VALUE of the outer list is the list of conversion functions
2749 found in a particular binfo. The TREE_PURPOSE of both the outer
2750 and inner lists is the binfo at which those conversions were
2751 found. TREE_STATIC is set for those lists within of morally
2752 virtual binfos. The TREE_VALUE of the inner list is the conversion
2753 function or overload itself. The TREE_TYPE of each inner list node
2754 is the converted-to type. */
2756 static int
2757 lookup_conversions_r (tree binfo,
2758 int virtual_depth, int virtualness,
2759 tree parent_convs, tree parent_tpl_convs,
2760 tree other_convs, tree other_tpl_convs,
2761 tree *convs, tree *tpl_convs)
2763 int my_virtualness = 0;
2764 tree my_convs = NULL_TREE;
2765 tree my_tpl_convs = NULL_TREE;
2766 tree child_convs = NULL_TREE;
2767 tree child_tpl_convs = NULL_TREE;
2768 unsigned i;
2769 tree base_binfo;
2770 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2771 tree conv;
2773 /* If we have no conversion operators, then don't look. */
2774 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2776 *convs = *tpl_convs = NULL_TREE;
2778 return 0;
2781 if (BINFO_VIRTUAL_P (binfo))
2782 virtual_depth++;
2784 /* First, locate the unhidden ones at this level. */
2785 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2786 vec_safe_iterate (method_vec, i, &conv);
2787 ++i)
2789 tree cur = OVL_FIRST (conv);
2791 if (!DECL_CONV_FN_P (cur))
2792 break;
2794 if (TREE_CODE (cur) == TEMPLATE_DECL)
2795 /* Only template conversions can be overloaded, and we must
2796 flatten them out and check each one individually. */
2797 for (ovl_iterator iter (conv); iter; ++iter)
2799 tree tpl = *iter;
2800 tree type = DECL_CONV_FN_TYPE (tpl);
2802 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2803 type, parent_tpl_convs, other_tpl_convs))
2805 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2806 TREE_TYPE (my_tpl_convs) = type;
2807 if (virtual_depth)
2809 TREE_STATIC (my_tpl_convs) = 1;
2810 my_virtualness = 1;
2814 else
2816 tree name = DECL_NAME (cur);
2818 if (!IDENTIFIER_MARKED (name))
2820 tree type = DECL_CONV_FN_TYPE (cur);
2821 if (type_uses_auto (type))
2823 mark_used (cur);
2824 type = DECL_CONV_FN_TYPE (cur);
2827 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2828 type, parent_convs, other_convs))
2830 my_convs = tree_cons (binfo, conv, my_convs);
2831 TREE_TYPE (my_convs) = type;
2832 if (virtual_depth)
2834 TREE_STATIC (my_convs) = 1;
2835 my_virtualness = 1;
2837 IDENTIFIER_MARKED (name) = 1;
2843 if (my_convs)
2845 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2846 if (virtual_depth)
2847 TREE_STATIC (parent_convs) = 1;
2850 if (my_tpl_convs)
2852 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2853 if (virtual_depth)
2854 TREE_STATIC (parent_tpl_convs) = 1;
2857 child_convs = other_convs;
2858 child_tpl_convs = other_tpl_convs;
2860 /* Now iterate over each base, looking for more conversions. */
2861 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2863 tree base_convs, base_tpl_convs;
2864 unsigned base_virtualness;
2866 base_virtualness = lookup_conversions_r (base_binfo,
2867 virtual_depth, virtualness,
2868 parent_convs, parent_tpl_convs,
2869 child_convs, child_tpl_convs,
2870 &base_convs, &base_tpl_convs);
2871 if (base_virtualness)
2872 my_virtualness = virtualness = 1;
2873 child_convs = chainon (base_convs, child_convs);
2874 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2877 /* Unmark the conversions found at this level */
2878 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2879 IDENTIFIER_MARKED (OVL_NAME (TREE_VALUE (conv))) = 0;
2881 *convs = split_conversions (my_convs, parent_convs,
2882 child_convs, other_convs);
2883 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2884 child_tpl_convs, other_tpl_convs);
2886 return my_virtualness;
2889 /* Return a TREE_LIST containing all the non-hidden user-defined
2890 conversion functions for TYPE (and its base-classes). The
2891 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2892 function. The TREE_PURPOSE is the BINFO from which the conversion
2893 functions in this node were selected. This function is effectively
2894 performing a set of member lookups as lookup_fnfield does, but
2895 using the type being converted to as the unique key, rather than the
2896 field name. */
2898 tree
2899 lookup_conversions (tree type)
2901 tree convs, tpl_convs;
2902 tree list = NULL_TREE;
2904 complete_type (type);
2905 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2906 return NULL_TREE;
2908 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2909 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2910 &convs, &tpl_convs);
2912 /* Flatten the list-of-lists */
2913 for (; convs; convs = TREE_CHAIN (convs))
2915 tree probe, next;
2917 for (probe = TREE_VALUE (convs); probe; probe = next)
2919 next = TREE_CHAIN (probe);
2921 TREE_CHAIN (probe) = list;
2922 list = probe;
2926 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2928 tree probe, next;
2930 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2932 next = TREE_CHAIN (probe);
2934 TREE_CHAIN (probe) = list;
2935 list = probe;
2939 return list;
2942 /* Returns the binfo of the first direct or indirect virtual base derived
2943 from BINFO, or NULL if binfo is not via virtual. */
2945 tree
2946 binfo_from_vbase (tree binfo)
2948 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2950 if (BINFO_VIRTUAL_P (binfo))
2951 return binfo;
2953 return NULL_TREE;
2956 /* Returns the binfo of the first direct or indirect virtual base derived
2957 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2958 via virtual. */
2960 tree
2961 binfo_via_virtual (tree binfo, tree limit)
2963 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2964 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2965 return NULL_TREE;
2967 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2968 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2970 if (BINFO_VIRTUAL_P (binfo))
2971 return binfo;
2973 return NULL_TREE;
2976 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2977 Find the equivalent binfo within whatever graph HERE is located.
2978 This is the inverse of original_binfo. */
2980 tree
2981 copied_binfo (tree binfo, tree here)
2983 tree result = NULL_TREE;
2985 if (BINFO_VIRTUAL_P (binfo))
2987 tree t;
2989 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2990 t = BINFO_INHERITANCE_CHAIN (t))
2991 continue;
2993 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2995 else if (BINFO_INHERITANCE_CHAIN (binfo))
2997 tree cbinfo;
2998 tree base_binfo;
2999 int ix;
3001 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
3002 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
3003 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
3005 result = base_binfo;
3006 break;
3009 else
3011 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
3012 result = here;
3015 gcc_assert (result);
3016 return result;
3019 tree
3020 binfo_for_vbase (tree base, tree t)
3022 unsigned ix;
3023 tree binfo;
3024 vec<tree, va_gc> *vbases;
3026 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
3027 vec_safe_iterate (vbases, ix, &binfo); ix++)
3028 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
3029 return binfo;
3030 return NULL;
3033 /* BINFO is some base binfo of HERE, within some other
3034 hierarchy. Return the equivalent binfo, but in the hierarchy
3035 dominated by HERE. This is the inverse of copied_binfo. If BINFO
3036 is not a base binfo of HERE, returns NULL_TREE. */
3038 tree
3039 original_binfo (tree binfo, tree here)
3041 tree result = NULL;
3043 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
3044 result = here;
3045 else if (BINFO_VIRTUAL_P (binfo))
3046 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
3047 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
3048 : NULL_TREE);
3049 else if (BINFO_INHERITANCE_CHAIN (binfo))
3051 tree base_binfos;
3053 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
3054 if (base_binfos)
3056 int ix;
3057 tree base_binfo;
3059 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
3060 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
3061 BINFO_TYPE (binfo)))
3063 result = base_binfo;
3064 break;
3069 return result;
3072 /* True iff TYPE has any dependent bases (and therefore we can't say
3073 definitively that another class is not a base of an instantiation of
3074 TYPE). */
3076 bool
3077 any_dependent_bases_p (tree type)
3079 if (!type || !CLASS_TYPE_P (type) || !processing_template_decl)
3080 return false;
3082 unsigned i;
3083 tree base_binfo;
3084 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
3085 if (BINFO_DEPENDENT_BASE_P (base_binfo))
3086 return true;
3088 return false;