1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2021 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)
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. */
26 #include "coretypes.h"
30 #include "spellcheck-tree.h"
31 #include "stringpool.h"
34 static int is_subobject_of_p (tree
, tree
);
35 static tree
dfs_lookup_base (tree
, void *);
36 static tree
dfs_dcast_hint_pre (tree
, void *);
37 static tree
dfs_dcast_hint_post (tree
, void *);
38 static tree
dfs_debug_mark (tree
, void *);
39 static int check_hidden_convs (tree
, int, int, tree
, tree
, tree
);
40 static tree
split_conversions (tree
, tree
, tree
, tree
);
41 static int lookup_conversions_r (tree
, int, int, tree
, tree
, tree
*);
42 static int look_for_overrides_r (tree
, tree
);
43 static tree
lookup_field_r (tree
, void *);
44 static tree
dfs_accessible_post (tree
, void *);
45 static tree
dfs_walk_once_accessible (tree
, bool,
46 tree (*pre_fn
) (tree
, void *),
47 tree (*post_fn
) (tree
, void *),
49 static tree
dfs_access_in_type (tree
, void *);
50 static access_kind
access_in_type (tree
, tree
);
51 static tree
dfs_get_pure_virtuals (tree
, void *);
54 /* Data for lookup_base and its workers. */
56 struct lookup_base_data_s
58 tree t
; /* type being searched. */
59 tree base
; /* The base type we're looking for. */
60 tree binfo
; /* Found binfo. */
61 bool via_virtual
; /* Found via a virtual path. */
62 bool ambiguous
; /* Found multiply ambiguous */
63 bool repeated_base
; /* Whether there are repeated bases in the
65 bool want_any
; /* Whether we want any matching binfo. */
68 /* Worker function for lookup_base. See if we've found the desired
69 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
72 dfs_lookup_base (tree binfo
, void *data_
)
74 struct lookup_base_data_s
*data
= (struct lookup_base_data_s
*) data_
;
76 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->base
))
82 = binfo_via_virtual (data
->binfo
, data
->t
) != NULL_TREE
;
84 if (!data
->repeated_base
)
85 /* If there are no repeated bases, we can stop now. */
88 if (data
->want_any
&& !data
->via_virtual
)
89 /* If this is a non-virtual base, then we can't do
93 return dfs_skip_bases
;
97 gcc_assert (binfo
!= data
->binfo
);
99 /* We've found more than one matching binfo. */
102 /* This is immediately ambiguous. */
103 data
->binfo
= NULL_TREE
;
104 data
->ambiguous
= true;
105 return error_mark_node
;
108 /* Prefer one via a non-virtual path. */
109 if (!binfo_via_virtual (binfo
, data
->t
))
112 data
->via_virtual
= false;
116 /* There must be repeated bases, otherwise we'd have stopped
117 on the first base we found. */
118 return dfs_skip_bases
;
125 /* This deals with bug PR17314.
127 DECL is a declaration and BINFO represents a class that has attempted (but
128 failed) to access DECL.
130 Examine the parent binfos of BINFO and determine whether any of them had
131 private access to DECL. If they did, return the parent binfo. This helps
132 in figuring out the correct error message to show (if the parents had
133 access, it's their fault for not giving sufficient access to BINFO).
135 If no parents had access, return NULL_TREE. */
138 get_parent_with_private_access (tree decl
, tree binfo
)
140 /* Only BINFOs should come through here. */
141 gcc_assert (TREE_CODE (binfo
) == TREE_BINFO
);
143 tree base_binfo
= NULL_TREE
;
145 /* Iterate through immediate parent classes. */
146 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
148 /* This parent had private access. Therefore that's why BINFO can't
150 if (access_in_type (BINFO_TYPE (base_binfo
), decl
) == ak_private
)
154 /* None of the parents had access. Note: it's impossible for one of the
155 parents to have had public or protected access to DECL, since then
156 BINFO would have been able to access DECL too. */
160 /* Returns true if type BASE is accessible in T. (BASE is known to be
161 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
162 true, consider any special access of the current scope, or access
163 bestowed by friendship. */
166 accessible_base_p (tree t
, tree base
, bool consider_local_p
)
170 /* [class.access.base]
172 A base class is said to be accessible if an invented public
173 member of the base class is accessible.
175 If BASE is a non-proper base, this condition is trivially
177 if (same_type_p (t
, base
))
179 /* Rather than inventing a public member, we use the implicit
180 public typedef created in the scope of every class. */
181 decl
= TYPE_FIELDS (base
);
182 while (!DECL_SELF_REFERENCE_P (decl
))
183 decl
= DECL_CHAIN (decl
);
184 while (ANON_AGGR_TYPE_P (t
))
185 t
= TYPE_CONTEXT (t
);
186 return accessible_p (t
, decl
, consider_local_p
);
189 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
190 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
191 non-NULL, fill with information about what kind of base we
194 If the base is inaccessible, or ambiguous, then error_mark_node is
195 returned. If the tf_error bit of COMPLAIN is not set, no error
199 lookup_base (tree t
, tree base
, base_access access
,
200 base_kind
*kind_ptr
, tsubst_flags_t complain
)
206 /* "Nothing" is definitely not derived from Base. */
210 *kind_ptr
= bk_not_base
;
214 if (t
== error_mark_node
|| base
== error_mark_node
)
217 *kind_ptr
= bk_not_base
;
218 return error_mark_node
;
220 gcc_assert (TYPE_P (base
));
229 t
= complete_type (TYPE_MAIN_VARIANT (t
));
230 if (dependent_type_p (t
))
231 if (tree open
= currently_open_class (t
))
233 t_binfo
= TYPE_BINFO (t
);
236 base
= TYPE_MAIN_VARIANT (base
);
238 /* If BASE is incomplete, it can't be a base of T--and instantiating it
239 might cause an error. */
240 if (t_binfo
&& CLASS_TYPE_P (base
) && COMPLETE_OR_OPEN_TYPE_P (base
))
242 struct lookup_base_data_s data
;
246 data
.binfo
= NULL_TREE
;
247 data
.ambiguous
= data
.via_virtual
= false;
248 data
.repeated_base
= CLASSTYPE_REPEATED_BASE_P (t
);
249 data
.want_any
= access
== ba_any
;
251 dfs_walk_once (t_binfo
, dfs_lookup_base
, NULL
, &data
);
255 bk
= data
.ambiguous
? bk_ambig
: bk_not_base
;
256 else if (binfo
== t_binfo
)
258 else if (data
.via_virtual
)
269 /* Check that the base is unambiguous and accessible. */
270 if (access
!= ba_any
)
277 if (complain
& tf_error
)
278 error ("%qT is an ambiguous base of %qT", base
, t
);
279 binfo
= error_mark_node
;
283 if ((access
& ba_check_bit
)
284 /* If BASE is incomplete, then BASE and TYPE are probably
285 the same, in which case BASE is accessible. If they
286 are not the same, then TYPE is invalid. In that case,
287 there's no need to issue another error here, and
288 there's no implicit typedef to use in the code that
289 follows, so we skip the check. */
290 && COMPLETE_TYPE_P (base
)
291 && !accessible_base_p (t
, base
, !(access
& ba_ignore_scope
)))
293 if (complain
& tf_error
)
294 error ("%qT is an inaccessible base of %qT", base
, t
);
295 binfo
= error_mark_node
;
296 bk
= bk_inaccessible
;
307 /* Data for dcast_base_hint walker. */
311 tree subtype
; /* The base type we're looking for. */
312 int virt_depth
; /* Number of virtual bases encountered from most
314 tree offset
; /* Best hint offset discovered so far. */
315 bool repeated_base
; /* Whether there are repeated bases in the
319 /* Worker for dcast_base_hint. Search for the base type being cast
323 dfs_dcast_hint_pre (tree binfo
, void *data_
)
325 struct dcast_data_s
*data
= (struct dcast_data_s
*) data_
;
327 if (BINFO_VIRTUAL_P (binfo
))
330 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->subtype
))
332 if (data
->virt_depth
)
334 data
->offset
= ssize_int (-1);
338 data
->offset
= ssize_int (-3);
340 data
->offset
= BINFO_OFFSET (binfo
);
342 return data
->repeated_base
? dfs_skip_bases
: data
->offset
;
348 /* Worker for dcast_base_hint. Track the virtual depth. */
351 dfs_dcast_hint_post (tree binfo
, void *data_
)
353 struct dcast_data_s
*data
= (struct dcast_data_s
*) data_
;
355 if (BINFO_VIRTUAL_P (binfo
))
361 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
362 started from is related to the required TARGET type, in order to optimize
363 the inheritance graph search. This information is independent of the
364 current context, and ignores private paths, hence get_base_distance is
365 inappropriate. Return a TREE specifying the base offset, BOFF.
366 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
367 and there are no public virtual SUBTYPE bases.
368 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
369 BOFF == -2, SUBTYPE is not a public base.
370 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
373 dcast_base_hint (tree subtype
, tree target
)
375 struct dcast_data_s data
;
377 data
.subtype
= subtype
;
379 data
.offset
= NULL_TREE
;
380 data
.repeated_base
= CLASSTYPE_REPEATED_BASE_P (target
);
382 dfs_walk_once_accessible (TYPE_BINFO (target
), /*friends=*/false,
383 dfs_dcast_hint_pre
, dfs_dcast_hint_post
, &data
);
384 return data
.offset
? data
.offset
: ssize_int (-2);
387 /* Search for a member with name NAME in a multiple inheritance
388 lattice specified by TYPE. If it does not exist, return NULL_TREE.
389 If the member is ambiguously referenced, return `error_mark_node'.
390 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
391 true, type declarations are preferred. */
393 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
394 NAMESPACE_DECL corresponding to the innermost non-block scope. */
399 /* There are a number of cases we need to be aware of here:
400 current_class_type current_function_decl
407 Those last two make life interesting. If we're in a function which is
408 itself inside a class, we need decls to go into the fn's decls (our
409 second case below). But if we're in a class and the class itself is
410 inside a function, we need decls to go into the decls for the class. To
411 achieve this last goal, we must see if, when both current_class_ptr and
412 current_function_decl are set, the class was declared inside that
413 function. If so, we know to put the decls into the class's scope. */
414 if (current_function_decl
&& current_class_type
415 && ((DECL_FUNCTION_MEMBER_P (current_function_decl
)
416 && same_type_p (DECL_CONTEXT (current_function_decl
),
418 || (DECL_FRIEND_CONTEXT (current_function_decl
)
419 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl
),
420 current_class_type
))))
421 return current_function_decl
;
423 if (current_class_type
)
424 return current_class_type
;
426 if (current_function_decl
)
427 return current_function_decl
;
429 return current_namespace
;
432 /* Returns nonzero if we are currently in a function scope. Note
433 that this function returns zero if we are within a local class, but
434 not within a member function body of the local class. */
437 at_function_scope_p (void)
439 tree cs
= current_scope ();
440 /* Also check cfun to make sure that we're really compiling
441 this function (as opposed to having set current_function_decl
442 for access checking or some such). */
443 return (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
444 && cfun
&& cfun
->decl
== current_function_decl
);
447 /* Returns true if the innermost active scope is a class scope. */
450 at_class_scope_p (void)
452 tree cs
= current_scope ();
453 return cs
&& TYPE_P (cs
);
456 /* Returns true if the innermost active scope is a namespace scope. */
459 at_namespace_scope_p (void)
461 tree cs
= current_scope ();
462 return cs
&& TREE_CODE (cs
) == NAMESPACE_DECL
;
465 /* Return the scope of DECL, as appropriate when doing name-lookup. */
468 context_for_name_lookup (tree decl
)
472 For the purposes of name lookup, after the anonymous union
473 definition, the members of the anonymous union are considered to
474 have been defined in the scope in which the anonymous union is
476 tree context
= DECL_CONTEXT (decl
);
478 while (context
&& TYPE_P (context
)
479 && (ANON_AGGR_TYPE_P (context
) || UNSCOPED_ENUM_P (context
)))
480 context
= TYPE_CONTEXT (context
);
482 context
= global_namespace
;
487 /* Returns true iff DECL is declared in TYPE. */
490 member_declared_in_type (tree decl
, tree type
)
492 /* A normal declaration obviously counts. */
493 if (context_for_name_lookup (decl
) == type
)
495 /* So does a using or access declaration. */
496 if (DECL_LANG_SPECIFIC (decl
) && !DECL_DISCRIMINATOR_P (decl
)
497 && purpose_member (type
, DECL_ACCESS (decl
)))
502 /* The accessibility routines use BINFO_ACCESS for scratch space
503 during the computation of the accessibility of some declaration. */
505 /* Avoid walking up past a declaration of the member. */
508 dfs_access_in_type_pre (tree binfo
, void *data
)
510 tree decl
= (tree
) data
;
511 tree type
= BINFO_TYPE (binfo
);
512 if (member_declared_in_type (decl
, type
))
513 return dfs_skip_bases
;
517 #define BINFO_ACCESS(NODE) \
518 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
520 /* Set the access associated with NODE to ACCESS. */
522 #define SET_BINFO_ACCESS(NODE, ACCESS) \
523 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
524 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
526 /* Called from access_in_type via dfs_walk. Calculate the access to
527 DATA (which is really a DECL) in BINFO. */
530 dfs_access_in_type (tree binfo
, void *data
)
532 tree decl
= (tree
) data
;
533 tree type
= BINFO_TYPE (binfo
);
534 access_kind access
= ak_none
;
536 if (context_for_name_lookup (decl
) == type
)
538 /* If we have descended to the scope of DECL, just note the
539 appropriate access. */
540 if (TREE_PRIVATE (decl
))
542 else if (TREE_PROTECTED (decl
))
543 access
= ak_protected
;
549 /* First, check for an access-declaration that gives us more
550 access to the DECL. */
551 if (DECL_LANG_SPECIFIC (decl
) && !DECL_DISCRIMINATOR_P (decl
))
553 tree decl_access
= purpose_member (type
, DECL_ACCESS (decl
));
557 decl_access
= TREE_VALUE (decl_access
);
559 if (decl_access
== access_public_node
)
561 else if (decl_access
== access_protected_node
)
562 access
= ak_protected
;
563 else if (decl_access
== access_private_node
)
574 vec
<tree
, va_gc
> *accesses
;
576 /* Otherwise, scan our baseclasses, and pick the most favorable
578 accesses
= BINFO_BASE_ACCESSES (binfo
);
579 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
581 tree base_access
= (*accesses
)[i
];
582 access_kind base_access_now
= BINFO_ACCESS (base_binfo
);
584 if (base_access_now
== ak_none
|| base_access_now
== ak_private
)
585 /* If it was not accessible in the base, or only
586 accessible as a private member, we can't access it
588 base_access_now
= ak_none
;
589 else if (base_access
== access_protected_node
)
590 /* Public and protected members in the base become
592 base_access_now
= ak_protected
;
593 else if (base_access
== access_private_node
)
594 /* Public and protected members in the base become
596 base_access_now
= ak_private
;
598 /* See if the new access, via this base, gives more
599 access than our previous best access. */
600 if (base_access_now
!= ak_none
601 && (access
== ak_none
|| base_access_now
< access
))
603 access
= base_access_now
;
605 /* If the new access is public, we can't do better. */
606 if (access
== ak_public
)
613 /* Note the access to DECL in TYPE. */
614 SET_BINFO_ACCESS (binfo
, access
);
619 /* Return the access to DECL in TYPE. */
622 access_in_type (tree type
, tree decl
)
624 tree binfo
= TYPE_BINFO (type
);
626 /* We must take into account
630 If a name can be reached by several paths through a multiple
631 inheritance graph, the access is that of the path that gives
634 The algorithm we use is to make a post-order depth-first traversal
635 of the base-class hierarchy. As we come up the tree, we annotate
636 each node with the most lenient access. */
637 dfs_walk_once (binfo
, dfs_access_in_type_pre
, dfs_access_in_type
, decl
);
639 return BINFO_ACCESS (binfo
);
642 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
643 of OTYPE in the context of DERIVED. */
646 protected_accessible_p (tree decl
, tree derived
, tree type
, tree otype
)
648 /* We're checking this clause from [class.access.base]
650 m as a member of N is protected, and the reference occurs in a
651 member or friend of class N, or in a member or friend of a
652 class P derived from N, where m as a member of P is public, private
655 Here DERIVED is a possible P, DECL is m and TYPE is N. */
657 /* If DERIVED isn't derived from N, then it can't be a P. */
658 if (!DERIVED_FROM_P (type
, derived
))
661 /* DECL_NONSTATIC_MEMBER_P won't work for USING_DECLs. */
662 decl
= strip_using_decl (decl
);
663 /* We don't expect or support dependent decls. */
664 gcc_assert (TREE_CODE (decl
) != USING_DECL
);
668 When a friend or a member function of a derived class references
669 a protected non-static member of a base class, an access check
670 applies in addition to those described earlier in clause
671 _class.access_) Except when forming a pointer to member
672 (_expr.unary.op_), the access must be through a pointer to,
673 reference to, or object of the derived class itself (or any class
674 derived from that class) (_expr.ref_). If the access is to form
675 a pointer to member, the nested-name-specifier shall name the
676 derived class (or any class derived from that class). */
677 if (DECL_NONSTATIC_MEMBER_P (decl
)
678 && !DERIVED_FROM_P (derived
, otype
))
684 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
685 to access DECL through TYPE. OTYPE is the type of the object. */
688 friend_accessible_p (tree scope
, tree decl
, tree type
, tree otype
)
690 /* We're checking this clause from [class.access.base]
692 m as a member of N is protected, and the reference occurs in a
693 member or friend of class N, or in a member or friend of a
694 class P derived from N, where m as a member of P is public, private
697 Here DECL is m and TYPE is N. SCOPE is the current context,
698 and we check all its possible Ps. */
699 tree befriending_classes
;
705 if (is_global_friend (scope
))
708 /* Is SCOPE itself a suitable P? */
709 if (TYPE_P (scope
) && protected_accessible_p (decl
, scope
, type
, otype
))
712 if (DECL_DECLARES_FUNCTION_P (scope
))
713 befriending_classes
= DECL_BEFRIENDING_CLASSES (scope
);
714 else if (TYPE_P (scope
))
715 befriending_classes
= CLASSTYPE_BEFRIENDING_CLASSES (scope
);
719 for (t
= befriending_classes
; t
; t
= TREE_CHAIN (t
))
720 if (protected_accessible_p (decl
, TREE_VALUE (t
), type
, otype
))
723 /* Nested classes have the same access as their enclosing types, as
724 per DR 45 (this is a change from C++98). */
726 if (friend_accessible_p (TYPE_CONTEXT (scope
), decl
, type
, otype
))
729 if (DECL_DECLARES_FUNCTION_P (scope
))
731 /* Perhaps this SCOPE is a member of a class which is a
733 if (DECL_CLASS_SCOPE_P (scope
)
734 && friend_accessible_p (DECL_CONTEXT (scope
), decl
, type
, otype
))
736 /* Perhaps SCOPE is a friend function defined inside a class from which
737 DECL is accessible. Checking this is necessary only when the class
738 is dependent, for otherwise add_friend will already have added the
739 class to SCOPE's DECL_BEFRIENDING_CLASSES. */
740 if (tree fctx
= DECL_FRIEND_CONTEXT (scope
))
741 if (dependent_type_p (fctx
)
742 && protected_accessible_p (decl
, fctx
, type
, otype
))
746 /* Maybe scope's template is a friend. */
747 if (tree tinfo
= get_template_info (scope
))
749 tree tmpl
= TI_TEMPLATE (tinfo
);
750 if (DECL_CLASS_TEMPLATE_P (tmpl
))
751 tmpl
= TREE_TYPE (tmpl
);
753 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
756 /* Increment processing_template_decl to make sure that
757 dependent_type_p works correctly. */
758 ++processing_template_decl
;
759 int ret
= friend_accessible_p (tmpl
, decl
, type
, otype
);
760 --processing_template_decl
;
766 /* If is_friend is true, we should have found a befriending class. */
767 gcc_checking_assert (!is_friend (type
, scope
));
772 struct dfs_accessible_data
778 /* Avoid walking up past a declaration of the member. */
781 dfs_accessible_pre (tree binfo
, void *data
)
783 dfs_accessible_data
*d
= (dfs_accessible_data
*)data
;
784 tree type
= BINFO_TYPE (binfo
);
785 if (member_declared_in_type (d
->decl
, type
))
786 return dfs_skip_bases
;
790 /* Called via dfs_walk_once_accessible from accessible_p */
793 dfs_accessible_post (tree binfo
, void *data
)
795 /* access_in_type already set BINFO_ACCESS for us. */
796 access_kind access
= BINFO_ACCESS (binfo
);
797 tree N
= BINFO_TYPE (binfo
);
798 dfs_accessible_data
*d
= (dfs_accessible_data
*)data
;
800 tree scope
= current_nonlambda_scope ();
802 /* A member m is accessible at the point R when named in class N if */
809 /* m as a member of N is public, or */
814 /* m as a member of N is private, and R occurs in a member or friend of
816 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
817 && is_friend (N
, scope
))
824 /* m as a member of N is protected, and R occurs in a member or friend
825 of class N, or in a member or friend of a class P derived from N,
826 where m as a member of P is public, private, or protected */
827 if (friend_accessible_p (scope
, decl
, N
, d
->object_type
))
837 /* Like accessible_p below, but within a template returns true iff DECL is
838 accessible in TYPE to all possible instantiations of the template. */
841 accessible_in_template_p (tree type
, tree decl
)
843 int save_ptd
= processing_template_decl
;
844 processing_template_decl
= 0;
845 int val
= accessible_p (type
, decl
, false);
846 processing_template_decl
= save_ptd
;
850 /* DECL is a declaration from a base class of TYPE, which was the
851 class used to name DECL. Return nonzero if, in the current
852 context, DECL is accessible. If TYPE is actually a BINFO node,
853 then we can tell in what context the access is occurring by looking
854 at the most derived class along the path indicated by BINFO. If
855 CONSIDER_LOCAL is true, do consider special access the current
856 scope or friendship thereof we might have. */
859 accessible_p (tree type
, tree decl
, bool consider_local_p
)
864 /* If this declaration is in a block or namespace scope, there's no
866 if (!TYPE_P (context_for_name_lookup (decl
)))
869 /* There is no need to perform access checks inside a thunk. */
870 if (current_function_decl
&& DECL_THUNK_P (current_function_decl
))
873 tree otype
= NULL_TREE
;
876 /* When accessing a non-static member, the most derived type in the
877 binfo chain is the type of the object; remember that type for
878 protected_accessible_p. */
879 for (tree b
= type
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
880 otype
= BINFO_TYPE (b
);
881 type
= BINFO_TYPE (type
);
886 /* [class.access.base]
888 A member m is accessible when named in class N if
890 --m as a member of N is public, or
892 --m as a member of N is private, and the reference occurs in a
893 member or friend of class N, or
895 --m as a member of N is protected, and the reference occurs in a
896 member or friend of class N, or in a member or friend of a
897 class P derived from N, where m as a member of P is public, private or
900 --there exists a base class B of N that is accessible at the point
901 of reference, and m is accessible when named in class B.
903 We walk the base class hierarchy, checking these conditions. */
905 /* We walk using TYPE_BINFO (type) because access_in_type will set
906 BINFO_ACCESS on it and its bases. */
907 binfo
= TYPE_BINFO (type
);
909 /* Compute the accessibility of DECL in the class hierarchy
910 dominated by type. */
911 access
= access_in_type (type
, decl
);
912 if (access
== ak_public
)
915 /* If we aren't considering the point of reference, only the first bullet
917 if (!consider_local_p
)
920 dfs_accessible_data d
= { decl
, otype
};
922 /* Walk the hierarchy again, looking for a base class that allows
924 return dfs_walk_once_accessible (binfo
, /*friends=*/true,
926 dfs_accessible_post
, &d
)
930 struct lookup_field_info
{
931 /* The type in which we're looking. */
933 /* The name of the field for which we're looking. */
935 /* If non-NULL, the current result of the lookup. */
937 /* The path to RVAL. */
939 /* If non-NULL, the lookup was ambiguous, and this is a list of the
942 /* If nonzero, we are looking for types, not data members. */
944 /* If something went wrong, a message indicating what. */
948 /* True for a class member means that it is shared between all objects
951 [class.member.lookup]:If the resulting set of declarations are not all
952 from sub-objects of the same type, or the set has a non-static member
953 and includes members from distinct sub-objects, there is an ambiguity
954 and the program is ill-formed.
956 This function checks that T contains no non-static members. */
959 shared_member_p (tree t
)
961 if (VAR_P (t
) || TREE_CODE (t
) == TYPE_DECL
962 || TREE_CODE (t
) == CONST_DECL
)
964 if (is_overloaded_fn (t
))
966 for (ovl_iterator
iter (get_fns (t
)); iter
; ++iter
)
968 tree decl
= strip_using_decl (*iter
);
969 if (TREE_CODE (decl
) == USING_DECL
)
970 /* Conservatively assume a dependent using-declaration
971 might resolve to a non-static member. */
973 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
981 /* Routine to see if the sub-object denoted by the binfo PARENT can be
982 found as a base class and sub-object of the object denoted by
986 is_subobject_of_p (tree parent
, tree binfo
)
990 for (probe
= parent
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
994 if (BINFO_VIRTUAL_P (probe
))
995 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (binfo
))
1001 /* DATA is really a struct lookup_field_info. Look for a field with
1002 the name indicated there in BINFO. If this function returns a
1003 non-NULL value it is the result of the lookup. Called from
1004 lookup_field via breadth_first_search. */
1007 lookup_field_r (tree binfo
, void *data
)
1009 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1010 tree type
= BINFO_TYPE (binfo
);
1011 tree nval
= NULL_TREE
;
1013 /* If this is a dependent base, don't look in it. */
1014 if (BINFO_DEPENDENT_BASE_P (binfo
))
1017 /* If this base class is hidden by the best-known value so far, we
1018 don't need to look. */
1019 if (lfi
->rval_binfo
&& BINFO_INHERITANCE_CHAIN (binfo
) == lfi
->rval_binfo
1020 && !BINFO_VIRTUAL_P (binfo
))
1021 return dfs_skip_bases
;
1023 nval
= get_class_binding (type
, lfi
->name
, lfi
->want_type
);
1025 /* If there is no declaration with the indicated name in this type,
1026 then there's nothing to do. */
1030 /* If the lookup already found a match, and the new value doesn't
1031 hide the old one, we might have an ambiguity. */
1033 && !is_subobject_of_p (lfi
->rval_binfo
, binfo
))
1036 if (nval
== lfi
->rval
&& shared_member_p (nval
))
1037 /* The two things are really the same. */
1039 else if (is_subobject_of_p (binfo
, lfi
->rval_binfo
))
1040 /* The previous value hides the new one. */
1044 /* We have a real ambiguity. We keep a chain of all the
1046 if (!lfi
->ambiguous
&& lfi
->rval
)
1048 /* This is the first time we noticed an ambiguity. Add
1049 what we previously thought was a reasonable candidate
1051 lfi
->ambiguous
= tree_cons (NULL_TREE
, lfi
->rval
, NULL_TREE
);
1052 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1055 /* Add the new value. */
1056 lfi
->ambiguous
= tree_cons (NULL_TREE
, nval
, lfi
->ambiguous
);
1057 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1058 lfi
->errstr
= G_("request for member %qD is ambiguous");
1064 lfi
->rval_binfo
= binfo
;
1068 /* Don't look for constructors or destructors in base classes. */
1069 if (IDENTIFIER_CDTOR_P (lfi
->name
))
1070 return dfs_skip_bases
;
1074 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1075 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1076 FUNCTIONS, and OPTYPE respectively. */
1079 build_baselink (tree binfo
, tree access_binfo
, tree functions
, tree optype
)
1083 gcc_assert (OVL_P (functions
) || TREE_CODE (functions
) == TEMPLATE_ID_EXPR
);
1084 gcc_assert (!optype
|| TYPE_P (optype
));
1085 gcc_assert (TREE_TYPE (functions
));
1087 baselink
= make_node (BASELINK
);
1088 TREE_TYPE (baselink
) = TREE_TYPE (functions
);
1089 BASELINK_BINFO (baselink
) = binfo
;
1090 BASELINK_ACCESS_BINFO (baselink
) = access_binfo
;
1091 BASELINK_FUNCTIONS (baselink
) = functions
;
1092 BASELINK_OPTYPE (baselink
) = optype
;
1097 /* Look for a member named NAME in an inheritance lattice dominated by
1098 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1099 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1100 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1101 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1102 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1103 TREE_VALUEs are the list of ambiguous candidates.
1105 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1107 If nothing can be found return NULL_TREE and do not issue an error.
1109 If non-NULL, failure information is written back to AFI. */
1112 lookup_member (tree xbasetype
, tree name
, int protect
, bool want_type
,
1113 tsubst_flags_t complain
, access_failure_info
*afi
)
1115 tree rval
, rval_binfo
= NULL_TREE
;
1116 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1117 struct lookup_field_info lfi
;
1119 /* rval_binfo is the binfo associated with the found member, note,
1120 this can be set with useful information, even when rval is not
1121 set, because it must deal with ALL members, not just non-function
1122 members. It is used for ambiguity checking and the hidden
1123 checks. Whereas rval is only set if a proper (not hidden)
1124 non-function member is found. */
1126 const char *errstr
= 0;
1128 if (name
== error_mark_node
1129 || xbasetype
== NULL_TREE
1130 || xbasetype
== error_mark_node
)
1133 gcc_assert (identifier_p (name
));
1135 if (TREE_CODE (xbasetype
) == TREE_BINFO
)
1137 type
= BINFO_TYPE (xbasetype
);
1138 basetype_path
= xbasetype
;
1142 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype
)))
1145 xbasetype
= NULL_TREE
;
1148 type
= complete_type (type
);
1150 /* Make sure we're looking for a member of the current instantiation in the
1151 right partial specialization. */
1152 if (dependent_type_p (type
))
1153 if (tree t
= currently_open_class (type
))
1157 basetype_path
= TYPE_BINFO (type
);
1162 memset (&lfi
, 0, sizeof (lfi
));
1165 lfi
.want_type
= want_type
;
1166 dfs_walk_all (basetype_path
, &lookup_field_r
, NULL
, &lfi
);
1168 rval_binfo
= lfi
.rval_binfo
;
1170 type
= BINFO_TYPE (rval_binfo
);
1171 errstr
= lfi
.errstr
;
1173 /* If we are not interested in ambiguities, don't report them;
1174 just return NULL_TREE. */
1175 if (!protect
&& lfi
.ambiguous
)
1181 return lfi
.ambiguous
;
1188 In the case of overloaded function names, access control is
1189 applied to the function selected by overloaded resolution.
1191 We cannot check here, even if RVAL is only a single non-static
1192 member function, since we do not know what the "this" pointer
1195 class A { protected: void f(); };
1196 class B : public A {
1203 only the first call to "f" is valid. However, if the function is
1204 static, we can check. */
1206 && !really_overloaded_fn (rval
))
1208 tree decl
= is_overloaded_fn (rval
) ? get_first_fn (rval
) : rval
;
1209 decl
= strip_using_decl (decl
);
1210 /* A dependent USING_DECL will be checked after tsubsting. */
1211 if (TREE_CODE (decl
) != USING_DECL
1212 && !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
1213 && !perform_or_defer_access_check (basetype_path
, decl
, decl
,
1215 rval
= error_mark_node
;
1218 if (errstr
&& protect
)
1220 if (complain
& tf_error
)
1222 error (errstr
, name
, type
);
1224 print_candidates (lfi
.ambiguous
);
1226 rval
= error_mark_node
;
1229 if (rval
&& is_overloaded_fn (rval
))
1230 rval
= build_baselink (rval_binfo
, basetype_path
, rval
,
1231 (IDENTIFIER_CONV_OP_P (name
)
1232 ? TREE_TYPE (name
): NULL_TREE
));
1236 /* Helper class for lookup_member_fuzzy. */
1238 class lookup_field_fuzzy_info
1241 lookup_field_fuzzy_info (bool want_type_p
) :
1242 m_want_type_p (want_type_p
), m_candidates () {}
1244 void fuzzy_lookup_field (tree type
);
1246 /* If true, we are looking for types, not data members. */
1248 /* The result: a vec of identifiers. */
1249 auto_vec
<tree
> m_candidates
;
1252 /* Locate all fields within TYPE, append them to m_candidates. */
1255 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type
)
1257 if (!CLASS_TYPE_P (type
))
1260 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1262 if (m_want_type_p
&& !DECL_DECLARES_TYPE_P (field
))
1265 if (!DECL_NAME (field
))
1268 if (is_lambda_ignored_entity (field
))
1271 m_candidates
.safe_push (DECL_NAME (field
));
1276 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1277 DATA is really a lookup_field_fuzzy_info. Look for a field with
1278 the name indicated there in BINFO. Gathers pertinent identifiers into
1282 lookup_field_fuzzy_r (tree binfo
, void *data
)
1284 lookup_field_fuzzy_info
*lffi
= (lookup_field_fuzzy_info
*) data
;
1285 tree type
= BINFO_TYPE (binfo
);
1287 lffi
->fuzzy_lookup_field (type
);
1292 /* Like lookup_member, but try to find the closest match for NAME,
1293 rather than an exact match, and return an identifier (or NULL_TREE).
1297 lookup_member_fuzzy (tree xbasetype
, tree name
, bool want_type_p
)
1299 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1300 class lookup_field_fuzzy_info
lffi (want_type_p
);
1302 /* rval_binfo is the binfo associated with the found member, note,
1303 this can be set with useful information, even when rval is not
1304 set, because it must deal with ALL members, not just non-function
1305 members. It is used for ambiguity checking and the hidden
1306 checks. Whereas rval is only set if a proper (not hidden)
1307 non-function member is found. */
1309 if (name
== error_mark_node
1310 || xbasetype
== NULL_TREE
1311 || xbasetype
== error_mark_node
)
1314 gcc_assert (identifier_p (name
));
1316 if (TREE_CODE (xbasetype
) == TREE_BINFO
)
1318 type
= BINFO_TYPE (xbasetype
);
1319 basetype_path
= xbasetype
;
1323 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype
)))
1326 xbasetype
= NULL_TREE
;
1329 type
= complete_type (type
);
1331 /* Make sure we're looking for a member of the current instantiation in the
1332 right partial specialization. */
1333 if (flag_concepts
&& dependent_type_p (type
))
1334 type
= currently_open_class (type
);
1337 basetype_path
= TYPE_BINFO (type
);
1342 /* Populate lffi.m_candidates. */
1343 dfs_walk_all (basetype_path
, &lookup_field_fuzzy_r
, NULL
, &lffi
);
1345 return find_closest_identifier (name
, &lffi
.m_candidates
);
1348 /* Like lookup_member, except that if we find a function member we
1349 return NULL_TREE. */
1352 lookup_field (tree xbasetype
, tree name
, int protect
, bool want_type
)
1354 tree rval
= lookup_member (xbasetype
, name
, protect
, want_type
,
1355 tf_warning_or_error
);
1357 /* Ignore functions, but propagate the ambiguity list. */
1358 if (!error_operand_p (rval
)
1359 && (rval
&& BASELINK_P (rval
)))
1365 /* Like lookup_member, except that if we find a non-function member we
1366 return NULL_TREE. */
1369 lookup_fnfields (tree xbasetype
, tree name
, int protect
,
1370 tsubst_flags_t complain
)
1372 tree rval
= lookup_member (xbasetype
, name
, protect
, /*want_type=*/false,
1375 /* Ignore non-functions, but propagate the ambiguity list. */
1376 if (!error_operand_p (rval
)
1377 && (rval
&& !BASELINK_P (rval
)))
1383 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1384 the class or namespace used to qualify the name. CONTEXT_CLASS is
1385 the class corresponding to the object in which DECL will be used.
1386 Return a possibly modified version of DECL that takes into account
1389 In particular, consider an expression like `B::m' in the context of
1390 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1391 then the most derived class indicated by the BASELINK_BINFO will be
1392 `B', not `D'. This function makes that adjustment. */
1395 adjust_result_of_qualified_name_lookup (tree decl
,
1396 tree qualifying_scope
,
1399 if (context_class
&& context_class
!= error_mark_node
1400 && CLASS_TYPE_P (context_class
)
1401 && CLASS_TYPE_P (qualifying_scope
)
1402 && DERIVED_FROM_P (qualifying_scope
, context_class
)
1403 && BASELINK_P (decl
))
1407 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1408 Because we do not yet know which function will be chosen by
1409 overload resolution, we cannot yet check either accessibility
1410 or ambiguity -- in either case, the choice of a static member
1411 function might make the usage valid. */
1412 base
= lookup_base (context_class
, qualifying_scope
,
1413 ba_unique
, NULL
, tf_none
);
1414 if (base
&& base
!= error_mark_node
)
1416 BASELINK_ACCESS_BINFO (decl
) = base
;
1418 = lookup_base (base
, BINFO_TYPE (BASELINK_BINFO (decl
)),
1419 ba_unique
, NULL
, tf_none
);
1420 if (decl_binfo
&& decl_binfo
!= error_mark_node
)
1421 BASELINK_BINFO (decl
) = decl_binfo
;
1425 if (BASELINK_P (decl
))
1426 BASELINK_QUALIFIED_P (decl
) = true;
1432 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1433 PRE_FN is called in preorder, while POST_FN is called in postorder.
1434 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1435 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1436 that value is immediately returned and the walk is terminated. One
1437 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1438 POST_FN are passed the binfo to examine and the caller's DATA
1439 value. All paths are walked, thus virtual and morally virtual
1440 binfos can be multiply walked. */
1443 dfs_walk_all (tree binfo
, tree (*pre_fn
) (tree
, void *),
1444 tree (*post_fn
) (tree
, void *), void *data
)
1450 /* Call the pre-order walking function. */
1453 rval
= pre_fn (binfo
, data
);
1456 if (rval
== dfs_skip_bases
)
1462 /* Find the next child binfo to walk. */
1463 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
1465 rval
= dfs_walk_all (base_binfo
, pre_fn
, post_fn
, data
);
1471 /* Call the post-order walking function. */
1474 rval
= post_fn (binfo
, data
);
1475 gcc_assert (rval
!= dfs_skip_bases
);
1482 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1483 that binfos are walked at most once. */
1486 dfs_walk_once_r (tree binfo
, tree (*pre_fn
) (tree
, void *),
1487 tree (*post_fn
) (tree
, void *), hash_set
<tree
> *pset
,
1494 /* Call the pre-order walking function. */
1497 rval
= pre_fn (binfo
, data
);
1500 if (rval
== dfs_skip_bases
)
1507 /* Find the next child binfo to walk. */
1508 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
1510 if (BINFO_VIRTUAL_P (base_binfo
))
1511 if (pset
->add (base_binfo
))
1514 rval
= dfs_walk_once_r (base_binfo
, pre_fn
, post_fn
, pset
, data
);
1520 /* Call the post-order walking function. */
1523 rval
= post_fn (binfo
, data
);
1524 gcc_assert (rval
!= dfs_skip_bases
);
1531 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1532 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1533 For diamond shaped hierarchies we must mark the virtual bases, to
1534 avoid multiple walks. */
1537 dfs_walk_once (tree binfo
, tree (*pre_fn
) (tree
, void *),
1538 tree (*post_fn
) (tree
, void *), void *data
)
1540 static int active
= 0; /* We must not be called recursively. */
1543 gcc_assert (pre_fn
|| post_fn
);
1544 gcc_assert (!active
);
1547 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo
)))
1548 /* We are not diamond shaped, and therefore cannot encounter the
1549 same binfo twice. */
1550 rval
= dfs_walk_all (binfo
, pre_fn
, post_fn
, data
);
1553 hash_set
<tree
> pset
;
1554 rval
= dfs_walk_once_r (binfo
, pre_fn
, post_fn
, &pset
, data
);
1562 /* Worker function for dfs_walk_once_accessible. Behaves like
1563 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1564 access given by the current context should be considered, (b) ONCE
1565 indicates whether bases should be marked during traversal. */
1568 dfs_walk_once_accessible_r (tree binfo
, bool friends_p
, hash_set
<tree
> *pset
,
1569 tree (*pre_fn
) (tree
, void *),
1570 tree (*post_fn
) (tree
, void *), void *data
)
1572 tree rval
= NULL_TREE
;
1576 /* Call the pre-order walking function. */
1579 rval
= pre_fn (binfo
, data
);
1582 if (rval
== dfs_skip_bases
)
1589 /* Find the next child binfo to walk. */
1590 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
1592 bool mark
= pset
&& BINFO_VIRTUAL_P (base_binfo
);
1594 if (mark
&& pset
->contains (base_binfo
))
1597 /* If the base is inherited via private or protected
1598 inheritance, then we can't see it, unless we are a friend of
1599 the current binfo. */
1600 if (BINFO_BASE_ACCESS (binfo
, ix
) != access_public_node
)
1605 scope
= current_scope ();
1607 || TREE_CODE (scope
) == NAMESPACE_DECL
1608 || !is_friend (BINFO_TYPE (binfo
), scope
))
1613 pset
->add (base_binfo
);
1615 rval
= dfs_walk_once_accessible_r (base_binfo
, friends_p
, pset
,
1616 pre_fn
, post_fn
, data
);
1622 /* Call the post-order walking function. */
1625 rval
= post_fn (binfo
, data
);
1626 gcc_assert (rval
!= dfs_skip_bases
);
1633 /* Like dfs_walk_once except that only accessible bases are walked.
1634 FRIENDS_P indicates whether friendship of the local context
1635 should be considered when determining accessibility. */
1638 dfs_walk_once_accessible (tree binfo
, bool friends_p
,
1639 tree (*pre_fn
) (tree
, void *),
1640 tree (*post_fn
) (tree
, void *), void *data
)
1642 hash_set
<tree
> *pset
= NULL
;
1643 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo
)))
1644 pset
= new hash_set
<tree
>;
1645 tree rval
= dfs_walk_once_accessible_r (binfo
, friends_p
, pset
,
1646 pre_fn
, post_fn
, data
);
1653 /* Return true iff the code of T is CODE, and it has compatible
1657 matches_code_and_type_p (tree t
, enum tree_code code
, tree type
)
1659 if (TREE_CODE (t
) != code
)
1661 if (!cxx_types_compatible_p (TREE_TYPE (t
), type
))
1666 /* Subroutine of direct_accessor_p and reference_accessor_p.
1667 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1668 We expect a tree of the form:
1673 <field_decl (FIELD_DECL)>>>. */
1676 field_access_p (tree component_ref
, tree field_decl
, tree field_type
)
1678 if (!matches_code_and_type_p (component_ref
, COMPONENT_REF
, field_type
))
1681 tree indirect_ref
= TREE_OPERAND (component_ref
, 0);
1682 if (!INDIRECT_REF_P (indirect_ref
))
1685 tree ptr
= STRIP_NOPS (TREE_OPERAND (indirect_ref
, 0));
1686 if (!is_this_parameter (ptr
))
1689 /* Must access the correct field. */
1690 if (TREE_OPERAND (component_ref
, 1) != field_decl
)
1695 /* Subroutine of field_accessor_p.
1697 Assuming that INIT_EXPR has already had its code and type checked,
1698 determine if it is a simple accessor for FIELD_DECL
1699 (of type FIELD_TYPE).
1701 Specifically, a simple accessor within struct S of the form:
1702 T get_field () { return m_field; }
1703 should have a constexpr_fn_retval (saved_tree) of the form:
1711 <field_decl (FIELD_DECL)>>>>>. */
1714 direct_accessor_p (tree init_expr
, tree field_decl
, tree field_type
)
1716 tree result_decl
= TREE_OPERAND (init_expr
, 0);
1717 if (!matches_code_and_type_p (result_decl
, RESULT_DECL
, field_type
))
1720 tree component_ref
= STRIP_NOPS (TREE_OPERAND (init_expr
, 1));
1721 if (!field_access_p (component_ref
, field_decl
, field_type
))
1727 /* Subroutine of field_accessor_p.
1729 Assuming that INIT_EXPR has already had its code and type checked,
1730 determine if it is a "reference" accessor for FIELD_DECL
1731 (of type FIELD_REFERENCE_TYPE).
1733 Specifically, a simple accessor within struct S of the form:
1734 T& get_field () { return m_field; }
1735 should have a constexpr_fn_retval (saved_tree) of the form:
1744 <field (FIELD_DECL)>>>>>>. */
1746 reference_accessor_p (tree init_expr
, tree field_decl
, tree field_type
,
1747 tree field_reference_type
)
1749 tree result_decl
= TREE_OPERAND (init_expr
, 0);
1750 if (!matches_code_and_type_p (result_decl
, RESULT_DECL
, field_reference_type
))
1753 tree field_pointer_type
= build_pointer_type (field_type
);
1754 tree addr_expr
= STRIP_NOPS (TREE_OPERAND (init_expr
, 1));
1755 if (!matches_code_and_type_p (addr_expr
, ADDR_EXPR
, field_pointer_type
))
1758 tree component_ref
= STRIP_NOPS (TREE_OPERAND (addr_expr
, 0));
1760 if (!field_access_p (component_ref
, field_decl
, field_type
))
1766 /* Return true if FN is an accessor method for FIELD_DECL.
1767 i.e. a method of the form { return FIELD; }, with no
1770 If CONST_P, then additionally require that FN be a const
1774 field_accessor_p (tree fn
, tree field_decl
, bool const_p
)
1776 if (TREE_CODE (fn
) != FUNCTION_DECL
)
1779 /* We don't yet support looking up static data, just fields. */
1780 if (TREE_CODE (field_decl
) != FIELD_DECL
)
1783 tree fntype
= TREE_TYPE (fn
);
1784 if (TREE_CODE (fntype
) != METHOD_TYPE
)
1787 /* If the field is accessed via a const "this" argument, verify
1788 that the "this" parameter is const. */
1791 tree this_class
= class_of_this_parm (fntype
);
1792 if (!TYPE_READONLY (this_class
))
1796 tree saved_tree
= DECL_SAVED_TREE (fn
);
1798 if (saved_tree
== NULL_TREE
)
1801 /* Attempt to extract a single return value from the function,
1803 tree retval
= constexpr_fn_retval (saved_tree
);
1804 if (retval
== NULL_TREE
|| retval
== error_mark_node
)
1806 /* Require an INIT_EXPR. */
1807 if (TREE_CODE (retval
) != INIT_EXPR
)
1809 tree init_expr
= retval
;
1811 /* Determine if this is a simple accessor within struct S of the form:
1812 T get_field () { return m_field; }. */
1813 tree field_type
= TREE_TYPE (field_decl
);
1814 if (cxx_types_compatible_p (TREE_TYPE (init_expr
), field_type
))
1815 return direct_accessor_p (init_expr
, field_decl
, field_type
);
1817 /* Failing that, determine if it is an accessor of the form:
1818 T& get_field () { return m_field; }. */
1819 tree field_reference_type
= cp_build_reference_type (field_type
, false);
1820 if (cxx_types_compatible_p (TREE_TYPE (init_expr
), field_reference_type
))
1821 return reference_accessor_p (init_expr
, field_decl
, field_type
,
1822 field_reference_type
);
1827 /* Callback data for dfs_locate_field_accessor_pre. */
1829 class locate_field_data
1832 locate_field_data (tree field_decl_
, bool const_p_
)
1833 : field_decl (field_decl_
), const_p (const_p_
) {}
1839 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1840 callable via binfo, if one exists, otherwise return NULL_TREE.
1842 Callback for dfs_walk_once_accessible for use within
1843 locate_field_accessor. */
1846 dfs_locate_field_accessor_pre (tree binfo
, void *data
)
1848 locate_field_data
*lfd
= (locate_field_data
*)data
;
1849 tree type
= BINFO_TYPE (binfo
);
1851 vec
<tree
, va_gc
> *member_vec
;
1855 if (!CLASS_TYPE_P (type
))
1858 member_vec
= CLASSTYPE_MEMBER_VEC (type
);
1862 for (i
= 0; vec_safe_iterate (member_vec
, i
, &fn
); ++i
)
1864 if (field_accessor_p (fn
, lfd
->field_decl
, lfd
->const_p
))
1870 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1871 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1874 locate_field_accessor (tree basetype_path
, tree field_decl
, bool const_p
)
1876 if (TREE_CODE (basetype_path
) != TREE_BINFO
)
1879 /* Walk the hierarchy, looking for a method of some base class that allows
1880 access to the field. */
1881 locate_field_data
lfd (field_decl
, const_p
);
1882 return dfs_walk_once_accessible (basetype_path
, /*friends=*/true,
1883 dfs_locate_field_accessor_pre
,
1887 /* Check throw specifier of OVERRIDER is at least as strict as
1888 the one of BASEFN. */
1891 maybe_check_overriding_exception_spec (tree overrider
, tree basefn
)
1893 maybe_instantiate_noexcept (basefn
);
1894 maybe_instantiate_noexcept (overrider
);
1895 tree base_throw
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn
));
1896 tree over_throw
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider
));
1898 if (DECL_INVALID_OVERRIDER_P (overrider
))
1901 /* Can't check this yet. Pretend this is fine and let
1902 noexcept_override_late_checks check this later. */
1903 if (UNPARSED_NOEXCEPT_SPEC_P (base_throw
)
1904 || UNPARSED_NOEXCEPT_SPEC_P (over_throw
))
1907 if (!comp_except_specs (base_throw
, over_throw
, ce_derived
))
1909 auto_diagnostic_group d
;
1910 error ("looser exception specification on overriding virtual function "
1911 "%q+#F", overrider
);
1912 inform (DECL_SOURCE_LOCATION (basefn
),
1913 "overridden function is %q#F", basefn
);
1914 DECL_INVALID_OVERRIDER_P (overrider
) = 1;
1920 /* Check that virtual overrider OVERRIDER is acceptable for base function
1921 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1924 check_final_overrider (tree overrider
, tree basefn
)
1926 tree over_type
= TREE_TYPE (overrider
);
1927 tree base_type
= TREE_TYPE (basefn
);
1928 tree over_return
= fndecl_declared_return_type (overrider
);
1929 tree base_return
= fndecl_declared_return_type (basefn
);
1933 if (DECL_INVALID_OVERRIDER_P (overrider
))
1936 if (same_type_p (base_return
, over_return
))
1938 else if ((CLASS_TYPE_P (over_return
) && CLASS_TYPE_P (base_return
))
1939 || (TREE_CODE (base_return
) == TREE_CODE (over_return
)
1940 && INDIRECT_TYPE_P (base_return
)))
1942 /* Potentially covariant. */
1943 unsigned base_quals
, over_quals
;
1945 fail
= !INDIRECT_TYPE_P (base_return
);
1948 fail
= cp_type_quals (base_return
) != cp_type_quals (over_return
);
1950 base_return
= TREE_TYPE (base_return
);
1951 over_return
= TREE_TYPE (over_return
);
1953 base_quals
= cp_type_quals (base_return
);
1954 over_quals
= cp_type_quals (over_return
);
1956 if ((base_quals
& over_quals
) != over_quals
)
1959 if (CLASS_TYPE_P (base_return
) && CLASS_TYPE_P (over_return
))
1961 /* Strictly speaking, the standard requires the return type to be
1962 complete even if it only differs in cv-quals, but that seems
1963 like a bug in the wording. */
1964 if (!same_type_ignoring_top_level_qualifiers_p (base_return
,
1967 tree binfo
= lookup_base (over_return
, base_return
,
1968 ba_check
, NULL
, tf_none
);
1970 if (!binfo
|| binfo
== error_mark_node
)
1974 else if (can_convert_standard (TREE_TYPE (base_type
),
1975 TREE_TYPE (over_type
),
1976 tf_warning_or_error
))
1977 /* GNU extension, allow trivial pointer conversions such as
1978 converting to void *, or qualification conversion. */
1980 auto_diagnostic_group d
;
1981 if (pedwarn (DECL_SOURCE_LOCATION (overrider
), 0,
1982 "invalid covariant return type for %q#D", overrider
))
1983 inform (DECL_SOURCE_LOCATION (basefn
),
1984 "overridden function is %q#D", basefn
);
1995 auto_diagnostic_group d
;
1997 error ("invalid covariant return type for %q+#D", overrider
);
1999 error ("conflicting return type specified for %q+#D", overrider
);
2000 inform (DECL_SOURCE_LOCATION (basefn
),
2001 "overridden function is %q#D", basefn
);
2002 DECL_INVALID_OVERRIDER_P (overrider
) = 1;
2006 if (!maybe_check_overriding_exception_spec (overrider
, basefn
))
2009 /* Check for conflicting type attributes. But leave transaction_safe for
2010 set_one_vmethod_tm_attributes. */
2011 if (!comp_type_attributes (over_type
, base_type
)
2012 && !tx_safe_fn_type_p (base_type
)
2013 && !tx_safe_fn_type_p (over_type
))
2015 auto_diagnostic_group d
;
2016 error ("conflicting type attributes specified for %q+#D", overrider
);
2017 inform (DECL_SOURCE_LOCATION (basefn
),
2018 "overridden function is %q#D", basefn
);
2019 DECL_INVALID_OVERRIDER_P (overrider
) = 1;
2023 /* A consteval virtual function shall not override a virtual function that is
2024 not consteval. A consteval virtual function shall not be overridden by a
2025 virtual function that is not consteval. */
2026 if (DECL_IMMEDIATE_FUNCTION_P (overrider
)
2027 != DECL_IMMEDIATE_FUNCTION_P (basefn
))
2029 auto_diagnostic_group d
;
2030 if (DECL_IMMEDIATE_FUNCTION_P (overrider
))
2031 error ("%<consteval%> function %q+D overriding non-%<consteval%> "
2032 "function", overrider
);
2034 error ("non-%<consteval%> function %q+D overriding %<consteval%> "
2035 "function", overrider
);
2036 inform (DECL_SOURCE_LOCATION (basefn
),
2037 "overridden function is %qD", basefn
);
2038 DECL_INVALID_OVERRIDER_P (overrider
) = 1;
2042 /* A function declared transaction_safe_dynamic that overrides a function
2043 declared transaction_safe (but not transaction_safe_dynamic) is
2045 if (tx_safe_fn_type_p (base_type
)
2046 && lookup_attribute ("transaction_safe_dynamic",
2047 DECL_ATTRIBUTES (overrider
))
2048 && !lookup_attribute ("transaction_safe_dynamic",
2049 DECL_ATTRIBUTES (basefn
)))
2051 auto_diagnostic_group d
;
2052 error_at (DECL_SOURCE_LOCATION (overrider
),
2053 "%qD declared %<transaction_safe_dynamic%>", overrider
);
2054 inform (DECL_SOURCE_LOCATION (basefn
),
2055 "overriding %qD declared %<transaction_safe%>", basefn
);
2058 if (DECL_DELETED_FN (basefn
) != DECL_DELETED_FN (overrider
))
2060 if (DECL_DELETED_FN (overrider
))
2062 auto_diagnostic_group d
;
2063 error ("deleted function %q+D overriding non-deleted function",
2065 inform (DECL_SOURCE_LOCATION (basefn
),
2066 "overridden function is %qD", basefn
);
2067 maybe_explain_implicit_delete (overrider
);
2071 auto_diagnostic_group d
;
2072 error ("non-deleted function %q+D overriding deleted function",
2074 inform (DECL_SOURCE_LOCATION (basefn
),
2075 "overridden function is %qD", basefn
);
2079 if (DECL_FINAL_P (basefn
))
2081 auto_diagnostic_group d
;
2082 error ("virtual function %q+D overriding final function", overrider
);
2083 inform (DECL_SOURCE_LOCATION (basefn
),
2084 "overridden function is %qD", basefn
);
2090 /* Given a class TYPE, and a function decl FNDECL, look for
2091 virtual functions in TYPE's hierarchy which FNDECL overrides.
2092 We do not look in TYPE itself, only its bases.
2094 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2095 find that it overrides anything.
2097 We check that every function which is overridden, is correctly
2101 look_for_overrides (tree type
, tree fndecl
)
2103 tree binfo
= TYPE_BINFO (type
);
2108 /* A constructor for a class T does not override a function T
2110 if (DECL_CONSTRUCTOR_P (fndecl
))
2113 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
2115 tree basetype
= BINFO_TYPE (base_binfo
);
2117 if (TYPE_POLYMORPHIC_P (basetype
))
2118 found
+= look_for_overrides_r (basetype
, fndecl
);
2123 /* Look in TYPE for virtual functions with the same signature as
2127 look_for_overrides_here (tree type
, tree fndecl
)
2129 tree ovl
= get_class_binding (type
, DECL_NAME (fndecl
));
2131 for (ovl_iterator
iter (ovl
); iter
; ++iter
)
2135 if (!DECL_VIRTUAL_P (fn
))
2136 /* Not a virtual. */;
2137 else if (DECL_CONTEXT (fn
) != type
)
2138 /* Introduced with a using declaration. */;
2139 else if (DECL_STATIC_FUNCTION_P (fndecl
))
2141 tree btypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2142 tree dtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2143 if (compparms (TREE_CHAIN (btypes
), dtypes
))
2146 else if (same_signature_p (fndecl
, fn
))
2153 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2154 TYPE itself and its bases. */
2157 look_for_overrides_r (tree type
, tree fndecl
)
2159 tree fn
= look_for_overrides_here (type
, fndecl
);
2162 if (DECL_STATIC_FUNCTION_P (fndecl
))
2164 /* A static member function cannot match an inherited
2165 virtual member function. */
2166 auto_diagnostic_group d
;
2167 error ("%q+#D cannot be declared", fndecl
);
2168 error (" since %q+#D declared in base class", fn
);
2172 /* It's definitely virtual, even if not explicitly set. */
2173 DECL_VIRTUAL_P (fndecl
) = 1;
2174 check_final_overrider (fndecl
, fn
);
2179 /* We failed to find one declared in this class. Look in its bases. */
2180 return look_for_overrides (type
, fndecl
);
2183 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2186 dfs_get_pure_virtuals (tree binfo
, void *data
)
2188 tree type
= (tree
) data
;
2190 /* We're not interested in primary base classes; the derived class
2191 of which they are a primary base will contain the information we
2193 if (!BINFO_PRIMARY_P (binfo
))
2197 for (virtuals
= BINFO_VIRTUALS (binfo
);
2199 virtuals
= TREE_CHAIN (virtuals
))
2200 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals
)))
2201 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type
), BV_FN (virtuals
));
2207 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2210 get_pure_virtuals (tree type
)
2212 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2213 is going to be overridden. */
2214 CLASSTYPE_PURE_VIRTUALS (type
) = NULL
;
2215 /* Now, run through all the bases which are not primary bases, and
2216 collect the pure virtual functions. We look at the vtable in
2217 each class to determine what pure virtual functions are present.
2218 (A primary base is not interesting because the derived class of
2219 which it is a primary base will contain vtable entries for the
2220 pure virtuals in the base class. */
2221 dfs_walk_once (TYPE_BINFO (type
), NULL
, dfs_get_pure_virtuals
, type
);
2224 /* Debug info for C++ classes can get very large; try to avoid
2225 emitting it everywhere.
2227 Note that this optimization wins even when the target supports
2228 BINCL (if only slightly), and reduces the amount of work for the
2232 maybe_suppress_debug_info (tree t
)
2234 if (write_symbols
== NO_DEBUG
)
2237 /* We might have set this earlier in cp_finish_decl. */
2238 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 0;
2240 /* Always emit the information for each class every time. */
2241 if (flag_emit_class_debug_always
)
2244 /* If we already know how we're handling this class, handle debug info
2246 if (CLASSTYPE_INTERFACE_KNOWN (t
))
2248 if (CLASSTYPE_INTERFACE_ONLY (t
))
2249 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
2250 /* else don't set it. */
2252 /* If the class has a vtable, write out the debug info along with
2254 else if (TYPE_CONTAINS_VPTR_P (t
))
2255 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
2257 /* Otherwise, just emit the debug info normally. */
2260 /* Note that we want debugging information for a base class of a class
2261 whose vtable is being emitted. Normally, this would happen because
2262 calling the constructor for a derived class implies calling the
2263 constructors for all bases, which involve initializing the
2264 appropriate vptr with the vtable for the base class; but in the
2265 presence of optimization, this initialization may be optimized
2266 away, so we tell finish_vtable_vardecl that we want the debugging
2267 information anyway. */
2270 dfs_debug_mark (tree binfo
, void * /*data*/)
2272 tree t
= BINFO_TYPE (binfo
);
2274 if (CLASSTYPE_DEBUG_REQUESTED (t
))
2275 return dfs_skip_bases
;
2277 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2282 /* Write out the debugging information for TYPE, whose vtable is being
2283 emitted. Also walk through our bases and note that we want to
2284 write out information for them. This avoids the problem of not
2285 writing any debug info for intermediate basetypes whose
2286 constructors, and thus the references to their vtables, and thus
2287 the vtables themselves, were optimized away. */
2290 note_debug_info_needed (tree type
)
2292 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type
)))
2294 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type
)) = 0;
2295 rest_of_type_compilation (type
, namespace_bindings_p ());
2298 dfs_walk_all (TYPE_BINFO (type
), dfs_debug_mark
, NULL
, 0);
2301 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2302 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2303 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2304 bases have been encountered already in the tree walk. PARENT_CONVS
2305 is the list of lists of conversion functions that could hide CONV
2306 and OTHER_CONVS is the list of lists of conversion functions that
2307 could hide or be hidden by CONV, should virtualness be involved in
2308 the hierarchy. Merely checking the conversion op's name is not
2309 enough because two conversion operators to the same type can have
2310 different names. Return nonzero if we are visible. */
2313 check_hidden_convs (tree binfo
, int virtual_depth
, int virtualness
,
2314 tree to_type
, tree parent_convs
, tree other_convs
)
2318 /* See if we are hidden by a parent conversion. */
2319 for (level
= parent_convs
; level
; level
= TREE_CHAIN (level
))
2320 for (probe
= TREE_VALUE (level
); probe
; probe
= TREE_CHAIN (probe
))
2321 if (same_type_p (to_type
, TREE_TYPE (probe
)))
2324 if (virtual_depth
|| virtualness
)
2326 /* In a virtual hierarchy, we could be hidden, or could hide a
2327 conversion function on the other_convs list. */
2328 for (level
= other_convs
; level
; level
= TREE_CHAIN (level
))
2334 if (!(virtual_depth
|| TREE_STATIC (level
)))
2335 /* Neither is morally virtual, so cannot hide each other. */
2338 if (!TREE_VALUE (level
))
2339 /* They evaporated away already. */
2342 they_hide_us
= (virtual_depth
2343 && original_binfo (binfo
, TREE_PURPOSE (level
)));
2344 we_hide_them
= (!they_hide_us
&& TREE_STATIC (level
)
2345 && original_binfo (TREE_PURPOSE (level
), binfo
));
2347 if (!(we_hide_them
|| they_hide_us
))
2348 /* Neither is within the other, so no hiding can occur. */
2351 for (prev
= &TREE_VALUE (level
), other
= *prev
; other
;)
2353 if (same_type_p (to_type
, TREE_TYPE (other
)))
2356 /* We are hidden. */
2361 /* We hide the other one. */
2362 other
= TREE_CHAIN (other
);
2367 prev
= &TREE_CHAIN (other
);
2375 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2376 of conversion functions, the first slot will be for the current
2377 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2378 of conversion functions from children of the current binfo,
2379 concatenated with conversions from elsewhere in the hierarchy --
2380 that list begins with OTHER_CONVS. Return a single list of lists
2381 containing only conversions from the current binfo and its
2385 split_conversions (tree my_convs
, tree parent_convs
,
2386 tree child_convs
, tree other_convs
)
2391 /* Remove the original other_convs portion from child_convs. */
2392 for (prev
= NULL
, t
= child_convs
;
2393 t
!= other_convs
; prev
= t
, t
= TREE_CHAIN (t
))
2397 TREE_CHAIN (prev
) = NULL_TREE
;
2399 child_convs
= NULL_TREE
;
2401 /* Attach the child convs to any we had at this level. */
2404 my_convs
= parent_convs
;
2405 TREE_CHAIN (my_convs
) = child_convs
;
2408 my_convs
= child_convs
;
2413 /* Worker for lookup_conversions. Lookup conversion functions in
2414 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2415 morally virtual base, and VIRTUALNESS is nonzero, if we've
2416 encountered virtual bases already in the tree walk. PARENT_CONVS
2417 is a list of conversions within parent binfos. OTHER_CONVS are
2418 conversions found elsewhere in the tree. Return the conversions
2419 found within this portion of the graph in CONVS. Return nonzero if
2420 we encountered virtualness. We keep template and non-template
2421 conversions separate, to avoid unnecessary type comparisons.
2423 The located conversion functions are held in lists of lists. The
2424 TREE_VALUE of the outer list is the list of conversion functions
2425 found in a particular binfo. The TREE_PURPOSE of both the outer
2426 and inner lists is the binfo at which those conversions were
2427 found. TREE_STATIC is set for those lists within of morally
2428 virtual binfos. The TREE_VALUE of the inner list is the conversion
2429 function or overload itself. The TREE_TYPE of each inner list node
2430 is the converted-to type. */
2433 lookup_conversions_r (tree binfo
, int virtual_depth
, int virtualness
,
2434 tree parent_convs
, tree other_convs
, tree
*convs
)
2436 int my_virtualness
= 0;
2437 tree my_convs
= NULL_TREE
;
2438 tree child_convs
= NULL_TREE
;
2440 /* If we have no conversion operators, then don't look. */
2441 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo
)))
2448 if (BINFO_VIRTUAL_P (binfo
))
2451 /* First, locate the unhidden ones at this level. */
2452 if (tree conv
= get_class_binding (BINFO_TYPE (binfo
), conv_op_identifier
))
2453 for (ovl_iterator
iter (conv
); iter
; ++iter
)
2456 tree type
= DECL_CONV_FN_TYPE (fn
);
2458 if (TREE_CODE (fn
) != TEMPLATE_DECL
&& type_uses_auto (type
))
2461 type
= DECL_CONV_FN_TYPE (fn
);
2464 if (check_hidden_convs (binfo
, virtual_depth
, virtualness
,
2465 type
, parent_convs
, other_convs
))
2467 my_convs
= tree_cons (binfo
, fn
, my_convs
);
2468 TREE_TYPE (my_convs
) = type
;
2471 TREE_STATIC (my_convs
) = 1;
2479 parent_convs
= tree_cons (binfo
, my_convs
, parent_convs
);
2481 TREE_STATIC (parent_convs
) = 1;
2484 child_convs
= other_convs
;
2486 /* Now iterate over each base, looking for more conversions. */
2489 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2492 unsigned base_virtualness
;
2494 base_virtualness
= lookup_conversions_r (base_binfo
,
2495 virtual_depth
, virtualness
,
2496 parent_convs
, child_convs
,
2498 if (base_virtualness
)
2499 my_virtualness
= virtualness
= 1;
2500 child_convs
= chainon (base_convs
, child_convs
);
2503 *convs
= split_conversions (my_convs
, parent_convs
,
2504 child_convs
, other_convs
);
2506 return my_virtualness
;
2509 /* Return a TREE_LIST containing all the non-hidden user-defined
2510 conversion functions for TYPE (and its base-classes). The
2511 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2512 function. The TREE_PURPOSE is the BINFO from which the conversion
2513 functions in this node were selected. This function is effectively
2514 performing a set of member lookups as lookup_fnfield does, but
2515 using the type being converted to as the unique key, rather than the
2519 lookup_conversions (tree type
)
2523 complete_type (type
);
2524 if (!CLASS_TYPE_P (type
) || !TYPE_BINFO (type
))
2527 lookup_conversions_r (TYPE_BINFO (type
), 0, 0, NULL_TREE
, NULL_TREE
, &convs
);
2529 tree list
= NULL_TREE
;
2531 /* Flatten the list-of-lists */
2532 for (; convs
; convs
= TREE_CHAIN (convs
))
2536 for (probe
= TREE_VALUE (convs
); probe
; probe
= next
)
2538 next
= TREE_CHAIN (probe
);
2540 TREE_CHAIN (probe
) = list
;
2548 /* Returns the binfo of the first direct or indirect virtual base derived
2549 from BINFO, or NULL if binfo is not via virtual. */
2552 binfo_from_vbase (tree binfo
)
2554 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2556 if (BINFO_VIRTUAL_P (binfo
))
2562 /* Returns the binfo of the first direct or indirect virtual base derived
2563 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2567 binfo_via_virtual (tree binfo
, tree limit
)
2569 if (limit
&& !CLASSTYPE_VBASECLASSES (limit
))
2570 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2573 for (; binfo
&& !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), limit
);
2574 binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2576 if (BINFO_VIRTUAL_P (binfo
))
2582 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2586 binfo_direct_p (tree binfo
)
2588 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
2589 if (BINFO_INHERITANCE_CHAIN (d_binfo
))
2590 /* A second inheritance chain means indirect. */
2592 if (!BINFO_VIRTUAL_P (binfo
))
2593 /* Non-virtual, so only one inheritance chain means direct. */
2595 /* A virtual base looks like a direct base, so we need to look through the
2596 direct bases to see if it's there. */
2598 for (int i
= 0; BINFO_BASE_ITERATE (d_binfo
, i
, b_binfo
); ++i
)
2599 if (b_binfo
== binfo
)
2604 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2605 Find the equivalent binfo within whatever graph HERE is located.
2606 This is the inverse of original_binfo. */
2609 copied_binfo (tree binfo
, tree here
)
2611 tree result
= NULL_TREE
;
2613 if (BINFO_VIRTUAL_P (binfo
))
2617 for (t
= here
; BINFO_INHERITANCE_CHAIN (t
);
2618 t
= BINFO_INHERITANCE_CHAIN (t
))
2621 result
= binfo_for_vbase (BINFO_TYPE (binfo
), BINFO_TYPE (t
));
2623 else if (BINFO_INHERITANCE_CHAIN (binfo
))
2629 cbinfo
= copied_binfo (BINFO_INHERITANCE_CHAIN (binfo
), here
);
2630 for (ix
= 0; BINFO_BASE_ITERATE (cbinfo
, ix
, base_binfo
); ix
++)
2631 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
), BINFO_TYPE (binfo
)))
2633 result
= base_binfo
;
2639 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here
), BINFO_TYPE (binfo
)));
2643 gcc_assert (result
);
2648 binfo_for_vbase (tree base
, tree t
)
2652 vec
<tree
, va_gc
> *vbases
;
2654 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
2655 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
2656 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), base
))
2661 /* BINFO is some base binfo of HERE, within some other
2662 hierarchy. Return the equivalent binfo, but in the hierarchy
2663 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2664 is not a base binfo of HERE, returns NULL_TREE. */
2667 original_binfo (tree binfo
, tree here
)
2671 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (here
)))
2673 else if (BINFO_VIRTUAL_P (binfo
))
2674 result
= (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here
))
2675 ? binfo_for_vbase (BINFO_TYPE (binfo
), BINFO_TYPE (here
))
2677 else if (BINFO_INHERITANCE_CHAIN (binfo
))
2681 base_binfos
= original_binfo (BINFO_INHERITANCE_CHAIN (binfo
), here
);
2687 for (ix
= 0; (base_binfo
= BINFO_BASE_BINFO (base_binfos
, ix
)); ix
++)
2688 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
2689 BINFO_TYPE (binfo
)))
2691 result
= base_binfo
;
2700 /* True iff TYPE has any dependent bases (and therefore we can't say
2701 definitively that another class is not a base of an instantiation of
2705 any_dependent_bases_p (tree type
)
2707 if (!type
|| !CLASS_TYPE_P (type
) || !uses_template_parms (type
))
2710 /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2711 Return false because in this situation we aren't actually looking up names
2712 in the scope of the class, so it doesn't matter whether it has dependent
2714 if (!TYPE_BINFO (type
))
2719 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type
)), i
, base_binfo
)
2720 if (BINFO_DEPENDENT_BASE_P (base_binfo
))