Merge trunk version 215919 into gupc branch.
[official-gcc.git] / gcc / cp / search.c
blob424b26cd3e1b059b34e5941776411a5d2ab6cea1
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2014 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 "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "target.h"
35 static int is_subobject_of_p (tree, tree);
36 static tree dfs_lookup_base (tree, void *);
37 static tree dfs_dcast_hint_pre (tree, void *);
38 static tree dfs_dcast_hint_post (tree, void *);
39 static tree dfs_debug_mark (tree, void *);
40 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
41 tree (*post_fn) (tree, void *), void *data);
42 static void dfs_unmark_r (tree);
43 static int check_hidden_convs (tree, int, int, tree, tree, tree);
44 static tree split_conversions (tree, tree, tree, tree);
45 static int lookup_conversions_r (tree, int, int,
46 tree, tree, tree, tree, tree *, tree *);
47 static int look_for_overrides_r (tree, tree);
48 static tree lookup_field_r (tree, void *);
49 static tree dfs_accessible_post (tree, void *);
50 static tree dfs_walk_once_accessible_r (tree, bool, bool,
51 tree (*pre_fn) (tree, void *),
52 tree (*post_fn) (tree, void *),
53 void *data);
54 static tree dfs_walk_once_accessible (tree, bool,
55 tree (*pre_fn) (tree, void *),
56 tree (*post_fn) (tree, void *),
57 void *data);
58 static tree dfs_access_in_type (tree, void *);
59 static access_kind access_in_type (tree, tree);
60 static int protected_accessible_p (tree, tree, tree);
61 static int friend_accessible_p (tree, tree, tree);
62 static tree dfs_get_pure_virtuals (tree, void *);
65 /* Variables for gathering statistics. */
66 static int n_fields_searched;
67 static int n_calls_lookup_field, n_calls_lookup_field_1;
68 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
69 static int n_calls_get_base_type;
70 static int n_outer_fields_searched;
71 static int n_contexts_saved;
74 /* Data for lookup_base and its workers. */
76 struct lookup_base_data_s
78 tree t; /* type being searched. */
79 tree base; /* The base type we're looking for. */
80 tree binfo; /* Found binfo. */
81 bool via_virtual; /* Found via a virtual path. */
82 bool ambiguous; /* Found multiply ambiguous */
83 bool repeated_base; /* Whether there are repeated bases in the
84 hierarchy. */
85 bool want_any; /* Whether we want any matching binfo. */
88 /* Worker function for lookup_base. See if we've found the desired
89 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
91 static tree
92 dfs_lookup_base (tree binfo, void *data_)
94 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
96 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
98 if (!data->binfo)
100 data->binfo = binfo;
101 data->via_virtual
102 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
104 if (!data->repeated_base)
105 /* If there are no repeated bases, we can stop now. */
106 return binfo;
108 if (data->want_any && !data->via_virtual)
109 /* If this is a non-virtual base, then we can't do
110 better. */
111 return binfo;
113 return dfs_skip_bases;
115 else
117 gcc_assert (binfo != data->binfo);
119 /* We've found more than one matching binfo. */
120 if (!data->want_any)
122 /* This is immediately ambiguous. */
123 data->binfo = NULL_TREE;
124 data->ambiguous = true;
125 return error_mark_node;
128 /* Prefer one via a non-virtual path. */
129 if (!binfo_via_virtual (binfo, data->t))
131 data->binfo = binfo;
132 data->via_virtual = false;
133 return binfo;
136 /* There must be repeated bases, otherwise we'd have stopped
137 on the first base we found. */
138 return dfs_skip_bases;
142 return NULL_TREE;
145 /* Returns true if type BASE is accessible in T. (BASE is known to be
146 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
147 true, consider any special access of the current scope, or access
148 bestowed by friendship. */
150 bool
151 accessible_base_p (tree t, tree base, bool consider_local_p)
153 tree decl;
155 /* [class.access.base]
157 A base class is said to be accessible if an invented public
158 member of the base class is accessible.
160 If BASE is a non-proper base, this condition is trivially
161 true. */
162 if (same_type_p (t, base))
163 return true;
164 /* Rather than inventing a public member, we use the implicit
165 public typedef created in the scope of every class. */
166 decl = TYPE_FIELDS (base);
167 while (!DECL_SELF_REFERENCE_P (decl))
168 decl = DECL_CHAIN (decl);
169 while (ANON_AGGR_TYPE_P (t))
170 t = TYPE_CONTEXT (t);
171 return accessible_p (t, decl, consider_local_p);
174 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
175 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
176 non-NULL, fill with information about what kind of base we
177 discovered.
179 If the base is inaccessible, or ambiguous, then error_mark_node is
180 returned. If the tf_error bit of COMPLAIN is not set, no error
181 is issued. */
183 tree
184 lookup_base (tree t, tree base, base_access access,
185 base_kind *kind_ptr, tsubst_flags_t complain)
187 tree binfo;
188 tree t_binfo;
189 base_kind bk;
191 /* "Nothing" is definitely not derived from Base. */
192 if (t == NULL_TREE)
194 if (kind_ptr)
195 *kind_ptr = bk_not_base;
196 return NULL_TREE;
199 if (t == error_mark_node || base == error_mark_node)
201 if (kind_ptr)
202 *kind_ptr = bk_not_base;
203 return error_mark_node;
205 gcc_assert (TYPE_P (base));
207 if (!TYPE_P (t))
209 t_binfo = t;
210 t = BINFO_TYPE (t);
212 else
214 t = complete_type (TYPE_MAIN_VARIANT (t));
215 t_binfo = TYPE_BINFO (t);
218 base = TYPE_MAIN_VARIANT (base);
220 /* If BASE is incomplete, it can't be a base of T--and instantiating it
221 might cause an error. */
222 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
224 struct lookup_base_data_s data;
226 data.t = t;
227 data.base = base;
228 data.binfo = NULL_TREE;
229 data.ambiguous = data.via_virtual = false;
230 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
231 data.want_any = access == ba_any;
233 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
234 binfo = data.binfo;
236 if (!binfo)
237 bk = data.ambiguous ? bk_ambig : bk_not_base;
238 else if (binfo == t_binfo)
239 bk = bk_same_type;
240 else if (data.via_virtual)
241 bk = bk_via_virtual;
242 else
243 bk = bk_proper_base;
245 else
247 binfo = NULL_TREE;
248 bk = bk_not_base;
251 /* Check that the base is unambiguous and accessible. */
252 if (access != ba_any)
253 switch (bk)
255 case bk_not_base:
256 break;
258 case bk_ambig:
259 if (complain & tf_error)
260 error ("%qT is an ambiguous base of %qT", base, t);
261 binfo = error_mark_node;
262 break;
264 default:
265 if ((access & ba_check_bit)
266 /* If BASE is incomplete, then BASE and TYPE are probably
267 the same, in which case BASE is accessible. If they
268 are not the same, then TYPE is invalid. In that case,
269 there's no need to issue another error here, and
270 there's no implicit typedef to use in the code that
271 follows, so we skip the check. */
272 && COMPLETE_TYPE_P (base)
273 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
275 if (complain & tf_error)
276 error ("%qT is an inaccessible base of %qT", base, t);
277 binfo = error_mark_node;
278 bk = bk_inaccessible;
280 break;
283 if (kind_ptr)
284 *kind_ptr = bk;
286 return binfo;
289 /* Data for dcast_base_hint walker. */
291 struct dcast_data_s
293 tree subtype; /* The base type we're looking for. */
294 int virt_depth; /* Number of virtual bases encountered from most
295 derived. */
296 tree offset; /* Best hint offset discovered so far. */
297 bool repeated_base; /* Whether there are repeated bases in the
298 hierarchy. */
301 /* Worker for dcast_base_hint. Search for the base type being cast
302 from. */
304 static tree
305 dfs_dcast_hint_pre (tree binfo, void *data_)
307 struct dcast_data_s *data = (struct dcast_data_s *) data_;
309 if (BINFO_VIRTUAL_P (binfo))
310 data->virt_depth++;
312 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
314 if (data->virt_depth)
316 data->offset = ssize_int (-1);
317 return data->offset;
319 if (data->offset)
320 data->offset = ssize_int (-3);
321 else
322 data->offset = BINFO_OFFSET (binfo);
324 return data->repeated_base ? dfs_skip_bases : data->offset;
327 return NULL_TREE;
330 /* Worker for dcast_base_hint. Track the virtual depth. */
332 static tree
333 dfs_dcast_hint_post (tree binfo, void *data_)
335 struct dcast_data_s *data = (struct dcast_data_s *) data_;
337 if (BINFO_VIRTUAL_P (binfo))
338 data->virt_depth--;
340 return NULL_TREE;
343 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
344 started from is related to the required TARGET type, in order to optimize
345 the inheritance graph search. This information is independent of the
346 current context, and ignores private paths, hence get_base_distance is
347 inappropriate. Return a TREE specifying the base offset, BOFF.
348 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
349 and there are no public virtual SUBTYPE bases.
350 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
351 BOFF == -2, SUBTYPE is not a public base.
352 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
354 tree
355 dcast_base_hint (tree subtype, tree target)
357 struct dcast_data_s data;
359 data.subtype = subtype;
360 data.virt_depth = 0;
361 data.offset = NULL_TREE;
362 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
364 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
365 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
366 return data.offset ? data.offset : ssize_int (-2);
369 /* Search for a member with name NAME in a multiple inheritance
370 lattice specified by TYPE. If it does not exist, return NULL_TREE.
371 If the member is ambiguously referenced, return `error_mark_node'.
372 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
373 true, type declarations are preferred. */
375 /* Do a 1-level search for NAME as a member of TYPE. The caller must
376 figure out whether it can access this field. (Since it is only one
377 level, this is reasonable.) */
379 tree
380 lookup_field_1 (tree type, tree name, bool want_type)
382 tree field;
384 gcc_assert (identifier_p (name));
386 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
387 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
388 || TREE_CODE (type) == TYPENAME_TYPE)
389 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
390 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
391 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
392 the code often worked even when we treated the index as a list
393 of fields!)
394 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
395 return NULL_TREE;
397 if (CLASSTYPE_SORTED_FIELDS (type))
399 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
400 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
401 int i;
403 while (lo < hi)
405 i = (lo + hi) / 2;
407 if (GATHER_STATISTICS)
408 n_fields_searched++;
410 if (DECL_NAME (fields[i]) > name)
411 hi = i;
412 else if (DECL_NAME (fields[i]) < name)
413 lo = i + 1;
414 else
416 field = NULL_TREE;
418 /* We might have a nested class and a field with the
419 same name; we sorted them appropriately via
420 field_decl_cmp, so just look for the first or last
421 field with this name. */
422 if (want_type)
425 field = fields[i--];
426 while (i >= lo && DECL_NAME (fields[i]) == name);
427 if (!DECL_DECLARES_TYPE_P (field))
428 field = NULL_TREE;
430 else
433 field = fields[i++];
434 while (i < hi && DECL_NAME (fields[i]) == name);
437 if (field)
439 field = strip_using_decl (field);
440 if (is_overloaded_fn (field))
441 field = NULL_TREE;
444 return field;
447 return NULL_TREE;
450 field = TYPE_FIELDS (type);
452 if (GATHER_STATISTICS)
453 n_calls_lookup_field_1++;
455 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
457 tree decl = field;
459 if (GATHER_STATISTICS)
460 n_fields_searched++;
462 gcc_assert (DECL_P (field));
463 if (DECL_NAME (field) == NULL_TREE
464 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
466 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
467 if (temp)
468 return temp;
471 if (TREE_CODE (decl) == USING_DECL
472 && DECL_NAME (decl) == name)
474 decl = strip_using_decl (decl);
475 if (is_overloaded_fn (decl))
476 continue;
479 if (DECL_NAME (decl) == name
480 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
481 return decl;
483 /* Not found. */
484 if (name == vptr_identifier)
486 /* Give the user what s/he thinks s/he wants. */
487 if (TYPE_POLYMORPHIC_P (type))
488 return TYPE_VFIELD (type);
490 return NULL_TREE;
493 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
494 NAMESPACE_DECL corresponding to the innermost non-block scope. */
496 tree
497 current_scope (void)
499 /* There are a number of cases we need to be aware of here:
500 current_class_type current_function_decl
501 global NULL NULL
502 fn-local NULL SET
503 class-local SET NULL
504 class->fn SET SET
505 fn->class SET SET
507 Those last two make life interesting. If we're in a function which is
508 itself inside a class, we need decls to go into the fn's decls (our
509 second case below). But if we're in a class and the class itself is
510 inside a function, we need decls to go into the decls for the class. To
511 achieve this last goal, we must see if, when both current_class_ptr and
512 current_function_decl are set, the class was declared inside that
513 function. If so, we know to put the decls into the class's scope. */
514 if (current_function_decl && current_class_type
515 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
516 && same_type_p (DECL_CONTEXT (current_function_decl),
517 current_class_type))
518 || (DECL_FRIEND_CONTEXT (current_function_decl)
519 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
520 current_class_type))))
521 return current_function_decl;
522 if (current_class_type)
523 return current_class_type;
524 if (current_function_decl)
525 return current_function_decl;
526 return current_namespace;
529 /* Returns nonzero if we are currently in a function scope. Note
530 that this function returns zero if we are within a local class, but
531 not within a member function body of the local class. */
534 at_function_scope_p (void)
536 tree cs = current_scope ();
537 /* Also check cfun to make sure that we're really compiling
538 this function (as opposed to having set current_function_decl
539 for access checking or some such). */
540 return (cs && TREE_CODE (cs) == FUNCTION_DECL
541 && cfun && cfun->decl == current_function_decl);
544 /* Returns true if the innermost active scope is a class scope. */
546 bool
547 at_class_scope_p (void)
549 tree cs = current_scope ();
550 return cs && TYPE_P (cs);
553 /* Returns true if the innermost active scope is a namespace scope. */
555 bool
556 at_namespace_scope_p (void)
558 tree cs = current_scope ();
559 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
562 /* Return the scope of DECL, as appropriate when doing name-lookup. */
564 tree
565 context_for_name_lookup (tree decl)
567 /* [class.union]
569 For the purposes of name lookup, after the anonymous union
570 definition, the members of the anonymous union are considered to
571 have been defined in the scope in which the anonymous union is
572 declared. */
573 tree context = DECL_CONTEXT (decl);
575 while (context && TYPE_P (context)
576 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
577 context = TYPE_CONTEXT (context);
578 if (!context)
579 context = global_namespace;
581 return context;
584 /* The accessibility routines use BINFO_ACCESS for scratch space
585 during the computation of the accessibility of some declaration. */
587 #define BINFO_ACCESS(NODE) \
588 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
590 /* Set the access associated with NODE to ACCESS. */
592 #define SET_BINFO_ACCESS(NODE, ACCESS) \
593 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
594 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
596 /* Called from access_in_type via dfs_walk. Calculate the access to
597 DATA (which is really a DECL) in BINFO. */
599 static tree
600 dfs_access_in_type (tree binfo, void *data)
602 tree decl = (tree) data;
603 tree type = BINFO_TYPE (binfo);
604 access_kind access = ak_none;
606 if (context_for_name_lookup (decl) == type)
608 /* If we have descended to the scope of DECL, just note the
609 appropriate access. */
610 if (TREE_PRIVATE (decl))
611 access = ak_private;
612 else if (TREE_PROTECTED (decl))
613 access = ak_protected;
614 else
615 access = ak_public;
617 else
619 /* First, check for an access-declaration that gives us more
620 access to the DECL. */
621 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
623 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
625 if (decl_access)
627 decl_access = TREE_VALUE (decl_access);
629 if (decl_access == access_public_node)
630 access = ak_public;
631 else if (decl_access == access_protected_node)
632 access = ak_protected;
633 else if (decl_access == access_private_node)
634 access = ak_private;
635 else
636 gcc_unreachable ();
640 if (!access)
642 int i;
643 tree base_binfo;
644 vec<tree, va_gc> *accesses;
646 /* Otherwise, scan our baseclasses, and pick the most favorable
647 access. */
648 accesses = BINFO_BASE_ACCESSES (binfo);
649 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
651 tree base_access = (*accesses)[i];
652 access_kind base_access_now = BINFO_ACCESS (base_binfo);
654 if (base_access_now == ak_none || base_access_now == ak_private)
655 /* If it was not accessible in the base, or only
656 accessible as a private member, we can't access it
657 all. */
658 base_access_now = ak_none;
659 else if (base_access == access_protected_node)
660 /* Public and protected members in the base become
661 protected here. */
662 base_access_now = ak_protected;
663 else if (base_access == access_private_node)
664 /* Public and protected members in the base become
665 private here. */
666 base_access_now = ak_private;
668 /* See if the new access, via this base, gives more
669 access than our previous best access. */
670 if (base_access_now != ak_none
671 && (access == ak_none || base_access_now < access))
673 access = base_access_now;
675 /* If the new access is public, we can't do better. */
676 if (access == ak_public)
677 break;
683 /* Note the access to DECL in TYPE. */
684 SET_BINFO_ACCESS (binfo, access);
686 return NULL_TREE;
689 /* Return the access to DECL in TYPE. */
691 static access_kind
692 access_in_type (tree type, tree decl)
694 tree binfo = TYPE_BINFO (type);
696 /* We must take into account
698 [class.paths]
700 If a name can be reached by several paths through a multiple
701 inheritance graph, the access is that of the path that gives
702 most access.
704 The algorithm we use is to make a post-order depth-first traversal
705 of the base-class hierarchy. As we come up the tree, we annotate
706 each node with the most lenient access. */
707 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
709 return BINFO_ACCESS (binfo);
712 /* Returns nonzero if it is OK to access DECL through an object
713 indicated by BINFO in the context of DERIVED. */
715 static int
716 protected_accessible_p (tree decl, tree derived, tree binfo)
718 access_kind access;
720 /* We're checking this clause from [class.access.base]
722 m as a member of N is protected, and the reference occurs in a
723 member or friend of class N, or in a member or friend of a
724 class P derived from N, where m as a member of P is public, private
725 or protected.
727 Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N. */
729 /* If DERIVED isn't derived from N, then it can't be a P. */
730 if (!DERIVED_FROM_P (BINFO_TYPE (binfo), derived))
731 return 0;
733 access = access_in_type (derived, decl);
735 /* If m is inaccessible in DERIVED, then it's not a P. */
736 if (access == ak_none)
737 return 0;
739 /* [class.protected]
741 When a friend or a member function of a derived class references
742 a protected nonstatic member of a base class, an access check
743 applies in addition to those described earlier in clause
744 _class.access_) Except when forming a pointer to member
745 (_expr.unary.op_), the access must be through a pointer to,
746 reference to, or object of the derived class itself (or any class
747 derived from that class) (_expr.ref_). If the access is to form
748 a pointer to member, the nested-name-specifier shall name the
749 derived class (or any class derived from that class). */
750 if (DECL_NONSTATIC_MEMBER_P (decl))
752 /* We can tell through what the reference is occurring by
753 chasing BINFO up to the root. */
754 tree t = binfo;
755 while (BINFO_INHERITANCE_CHAIN (t))
756 t = BINFO_INHERITANCE_CHAIN (t);
758 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
759 return 0;
762 return 1;
765 /* Returns nonzero if SCOPE is a friend of a type which would be able
766 to access DECL through the object indicated by BINFO. */
768 static int
769 friend_accessible_p (tree scope, tree decl, tree binfo)
771 tree befriending_classes;
772 tree t;
774 if (!scope)
775 return 0;
777 if (DECL_DECLARES_FUNCTION_P (scope))
778 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
779 else if (TYPE_P (scope))
780 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
781 else
782 return 0;
784 for (t = befriending_classes; t; t = TREE_CHAIN (t))
785 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
786 return 1;
788 /* Nested classes have the same access as their enclosing types, as
789 per DR 45 (this is a change from the standard). */
790 if (TYPE_P (scope))
791 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
792 if (protected_accessible_p (decl, t, binfo))
793 return 1;
795 if (DECL_DECLARES_FUNCTION_P (scope))
797 /* Perhaps this SCOPE is a member of a class which is a
798 friend. */
799 if (DECL_CLASS_SCOPE_P (scope)
800 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
801 return 1;
803 /* Or an instantiation of something which is a friend. */
804 if (DECL_TEMPLATE_INFO (scope))
806 int ret;
807 /* Increment processing_template_decl to make sure that
808 dependent_type_p works correctly. */
809 ++processing_template_decl;
810 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
811 --processing_template_decl;
812 return ret;
816 return 0;
819 /* Called via dfs_walk_once_accessible from accessible_p */
821 static tree
822 dfs_accessible_post (tree binfo, void * /*data*/)
824 if (BINFO_ACCESS (binfo) != ak_none)
826 tree scope = current_scope ();
827 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
828 && is_friend (BINFO_TYPE (binfo), scope))
829 return binfo;
832 return NULL_TREE;
835 /* Like accessible_p below, but within a template returns true iff DECL is
836 accessible in TYPE to all possible instantiations of the template. */
839 accessible_in_template_p (tree type, tree decl)
841 int save_ptd = processing_template_decl;
842 processing_template_decl = 0;
843 int val = accessible_p (type, decl, false);
844 processing_template_decl = save_ptd;
845 return val;
848 /* DECL is a declaration from a base class of TYPE, which was the
849 class used to name DECL. Return nonzero if, in the current
850 context, DECL is accessible. If TYPE is actually a BINFO node,
851 then we can tell in what context the access is occurring by looking
852 at the most derived class along the path indicated by BINFO. If
853 CONSIDER_LOCAL is true, do consider special access the current
854 scope or friendship thereof we might have. */
857 accessible_p (tree type, tree decl, bool consider_local_p)
859 tree binfo;
860 tree scope;
861 access_kind access;
863 /* Nonzero if it's OK to access DECL if it has protected
864 accessibility in TYPE. */
865 int protected_ok = 0;
867 /* If this declaration is in a block or namespace scope, there's no
868 access control. */
869 if (!TYPE_P (context_for_name_lookup (decl)))
870 return 1;
872 /* There is no need to perform access checks inside a thunk. */
873 scope = current_scope ();
874 if (scope && DECL_THUNK_P (scope))
875 return 1;
877 /* In a template declaration, we cannot be sure whether the
878 particular specialization that is instantiated will be a friend
879 or not. Therefore, all access checks are deferred until
880 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
881 parameter list for a template (because we may see dependent types
882 in default arguments for template parameters), and access
883 checking should be performed in the outermost parameter list. */
884 if (processing_template_decl
885 && (!processing_template_parmlist || processing_template_decl > 1))
886 return 1;
888 if (!TYPE_P (type))
890 binfo = type;
891 type = BINFO_TYPE (type);
893 else
894 binfo = TYPE_BINFO (type);
896 /* [class.access.base]
898 A member m is accessible when named in class N if
900 --m as a member of N is public, or
902 --m as a member of N is private, and the reference occurs in a
903 member or friend of class N, or
905 --m as a member of N is protected, and the reference occurs in a
906 member or friend of class N, or in a member or friend of a
907 class P derived from N, where m as a member of P is private or
908 protected, or
910 --there exists a base class B of N that is accessible at the point
911 of reference, and m is accessible when named in class B.
913 We walk the base class hierarchy, checking these conditions. */
915 if (consider_local_p)
917 /* Figure out where the reference is occurring. Check to see if
918 DECL is private or protected in this scope, since that will
919 determine whether protected access is allowed. */
920 tree ct = current_nonlambda_class_type ();
921 if (ct)
922 protected_ok = protected_accessible_p (decl,
924 binfo);
926 /* Now, loop through the classes of which we are a friend. */
927 if (!protected_ok)
928 protected_ok = friend_accessible_p (scope, decl, binfo);
931 /* Standardize the binfo that access_in_type will use. We don't
932 need to know what path was chosen from this point onwards. */
933 binfo = TYPE_BINFO (type);
935 /* Compute the accessibility of DECL in the class hierarchy
936 dominated by type. */
937 access = access_in_type (type, decl);
938 if (access == ak_public
939 || (access == ak_protected && protected_ok))
940 return 1;
942 if (!consider_local_p)
943 return 0;
945 /* Walk the hierarchy again, looking for a base class that allows
946 access. */
947 return dfs_walk_once_accessible (binfo, /*friends=*/true,
948 NULL, dfs_accessible_post, NULL)
949 != NULL_TREE;
952 struct lookup_field_info {
953 /* The type in which we're looking. */
954 tree type;
955 /* The name of the field for which we're looking. */
956 tree name;
957 /* If non-NULL, the current result of the lookup. */
958 tree rval;
959 /* The path to RVAL. */
960 tree rval_binfo;
961 /* If non-NULL, the lookup was ambiguous, and this is a list of the
962 candidates. */
963 tree ambiguous;
964 /* If nonzero, we are looking for types, not data members. */
965 int want_type;
966 /* If something went wrong, a message indicating what. */
967 const char *errstr;
970 /* Nonzero for a class member means that it is shared between all objects
971 of that class.
973 [class.member.lookup]:If the resulting set of declarations are not all
974 from sub-objects of the same type, or the set has a nonstatic member
975 and includes members from distinct sub-objects, there is an ambiguity
976 and the program is ill-formed.
978 This function checks that T contains no nonstatic members. */
981 shared_member_p (tree t)
983 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
984 || TREE_CODE (t) == CONST_DECL)
985 return 1;
986 if (is_overloaded_fn (t))
988 t = get_fns (t);
989 for (; t; t = OVL_NEXT (t))
991 tree fn = OVL_CURRENT (t);
992 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
993 return 0;
995 return 1;
997 return 0;
1000 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1001 found as a base class and sub-object of the object denoted by
1002 BINFO. */
1004 static int
1005 is_subobject_of_p (tree parent, tree binfo)
1007 tree probe;
1009 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1011 if (probe == binfo)
1012 return 1;
1013 if (BINFO_VIRTUAL_P (probe))
1014 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1015 != NULL_TREE);
1017 return 0;
1020 /* DATA is really a struct lookup_field_info. Look for a field with
1021 the name indicated there in BINFO. If this function returns a
1022 non-NULL value it is the result of the lookup. Called from
1023 lookup_field via breadth_first_search. */
1025 static tree
1026 lookup_field_r (tree binfo, void *data)
1028 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1029 tree type = BINFO_TYPE (binfo);
1030 tree nval = NULL_TREE;
1032 /* If this is a dependent base, don't look in it. */
1033 if (BINFO_DEPENDENT_BASE_P (binfo))
1034 return NULL_TREE;
1036 /* If this base class is hidden by the best-known value so far, we
1037 don't need to look. */
1038 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1039 && !BINFO_VIRTUAL_P (binfo))
1040 return dfs_skip_bases;
1042 /* First, look for a function. There can't be a function and a data
1043 member with the same name, and if there's a function and a type
1044 with the same name, the type is hidden by the function. */
1045 if (!lfi->want_type)
1046 nval = lookup_fnfields_slot (type, lfi->name);
1048 if (!nval)
1049 /* Look for a data member or type. */
1050 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1052 /* If there is no declaration with the indicated name in this type,
1053 then there's nothing to do. */
1054 if (!nval)
1055 goto done;
1057 /* If we're looking up a type (as with an elaborated type specifier)
1058 we ignore all non-types we find. */
1059 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1061 if (lfi->name == TYPE_IDENTIFIER (type))
1063 /* If the aggregate has no user defined constructors, we allow
1064 it to have fields with the same name as the enclosing type.
1065 If we are looking for that name, find the corresponding
1066 TYPE_DECL. */
1067 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1068 if (DECL_NAME (nval) == lfi->name
1069 && TREE_CODE (nval) == TYPE_DECL)
1070 break;
1072 else
1073 nval = NULL_TREE;
1074 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1076 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1077 lfi->name);
1078 if (e != NULL)
1079 nval = TYPE_MAIN_DECL (e->type);
1080 else
1081 goto done;
1085 /* If the lookup already found a match, and the new value doesn't
1086 hide the old one, we might have an ambiguity. */
1087 if (lfi->rval_binfo
1088 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1091 if (nval == lfi->rval && shared_member_p (nval))
1092 /* The two things are really the same. */
1094 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1095 /* The previous value hides the new one. */
1097 else
1099 /* We have a real ambiguity. We keep a chain of all the
1100 candidates. */
1101 if (!lfi->ambiguous && lfi->rval)
1103 /* This is the first time we noticed an ambiguity. Add
1104 what we previously thought was a reasonable candidate
1105 to the list. */
1106 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1107 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1110 /* Add the new value. */
1111 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1112 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1113 lfi->errstr = G_("request for member %qD is ambiguous");
1116 else
1118 lfi->rval = nval;
1119 lfi->rval_binfo = binfo;
1122 done:
1123 /* Don't look for constructors or destructors in base classes. */
1124 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1125 return dfs_skip_bases;
1126 return NULL_TREE;
1129 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1130 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1131 FUNCTIONS, and OPTYPE respectively. */
1133 tree
1134 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1136 tree baselink;
1138 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1139 || TREE_CODE (functions) == TEMPLATE_DECL
1140 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1141 || TREE_CODE (functions) == OVERLOAD);
1142 gcc_assert (!optype || TYPE_P (optype));
1143 gcc_assert (TREE_TYPE (functions));
1145 baselink = make_node (BASELINK);
1146 TREE_TYPE (baselink) = TREE_TYPE (functions);
1147 BASELINK_BINFO (baselink) = binfo;
1148 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1149 BASELINK_FUNCTIONS (baselink) = functions;
1150 BASELINK_OPTYPE (baselink) = optype;
1152 return baselink;
1155 /* Look for a member named NAME in an inheritance lattice dominated by
1156 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1157 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1158 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1159 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1160 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1161 TREE_VALUEs are the list of ambiguous candidates.
1163 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1165 If nothing can be found return NULL_TREE and do not issue an error. */
1167 tree
1168 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1169 tsubst_flags_t complain)
1171 tree rval, rval_binfo = NULL_TREE;
1172 tree type = NULL_TREE, basetype_path = NULL_TREE;
1173 struct lookup_field_info lfi;
1175 /* rval_binfo is the binfo associated with the found member, note,
1176 this can be set with useful information, even when rval is not
1177 set, because it must deal with ALL members, not just non-function
1178 members. It is used for ambiguity checking and the hidden
1179 checks. Whereas rval is only set if a proper (not hidden)
1180 non-function member is found. */
1182 const char *errstr = 0;
1184 if (name == error_mark_node
1185 || xbasetype == NULL_TREE
1186 || xbasetype == error_mark_node)
1187 return NULL_TREE;
1189 gcc_assert (identifier_p (name));
1191 if (TREE_CODE (xbasetype) == TREE_BINFO)
1193 type = BINFO_TYPE (xbasetype);
1194 basetype_path = xbasetype;
1196 else
1198 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1199 return NULL_TREE;
1200 type = xbasetype;
1201 xbasetype = NULL_TREE;
1204 type = complete_type (type);
1205 if (!basetype_path)
1206 basetype_path = TYPE_BINFO (type);
1208 if (!basetype_path)
1209 return NULL_TREE;
1211 if (GATHER_STATISTICS)
1212 n_calls_lookup_field++;
1214 memset (&lfi, 0, sizeof (lfi));
1215 lfi.type = type;
1216 lfi.name = name;
1217 lfi.want_type = want_type;
1218 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1219 rval = lfi.rval;
1220 rval_binfo = lfi.rval_binfo;
1221 if (rval_binfo)
1222 type = BINFO_TYPE (rval_binfo);
1223 errstr = lfi.errstr;
1225 /* If we are not interested in ambiguities, don't report them;
1226 just return NULL_TREE. */
1227 if (!protect && lfi.ambiguous)
1228 return NULL_TREE;
1230 if (protect == 2)
1232 if (lfi.ambiguous)
1233 return lfi.ambiguous;
1234 else
1235 protect = 0;
1238 /* [class.access]
1240 In the case of overloaded function names, access control is
1241 applied to the function selected by overloaded resolution.
1243 We cannot check here, even if RVAL is only a single non-static
1244 member function, since we do not know what the "this" pointer
1245 will be. For:
1247 class A { protected: void f(); };
1248 class B : public A {
1249 void g(A *p) {
1250 f(); // OK
1251 p->f(); // Not OK.
1255 only the first call to "f" is valid. However, if the function is
1256 static, we can check. */
1257 if (rval && protect
1258 && !really_overloaded_fn (rval))
1260 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1261 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1262 && !perform_or_defer_access_check (basetype_path, decl, decl,
1263 complain))
1264 rval = error_mark_node;
1267 if (errstr && protect)
1269 if (complain & tf_error)
1271 error (errstr, name, type);
1272 if (lfi.ambiguous)
1273 print_candidates (lfi.ambiguous);
1275 rval = error_mark_node;
1278 if (rval && is_overloaded_fn (rval))
1279 rval = build_baselink (rval_binfo, basetype_path, rval,
1280 (IDENTIFIER_TYPENAME_P (name)
1281 ? TREE_TYPE (name): NULL_TREE));
1282 return rval;
1285 /* Like lookup_member, except that if we find a function member we
1286 return NULL_TREE. */
1288 tree
1289 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1291 tree rval = lookup_member (xbasetype, name, protect, want_type,
1292 tf_warning_or_error);
1294 /* Ignore functions, but propagate the ambiguity list. */
1295 if (!error_operand_p (rval)
1296 && (rval && BASELINK_P (rval)))
1297 return NULL_TREE;
1299 return rval;
1302 /* Like lookup_member, except that if we find a non-function member we
1303 return NULL_TREE. */
1305 tree
1306 lookup_fnfields (tree xbasetype, tree name, int protect)
1308 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1309 tf_warning_or_error);
1311 /* Ignore non-functions, but propagate the ambiguity list. */
1312 if (!error_operand_p (rval)
1313 && (rval && !BASELINK_P (rval)))
1314 return NULL_TREE;
1316 return rval;
1319 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1320 corresponding to "operator TYPE ()", or -1 if there is no such
1321 operator. Only CLASS_TYPE itself is searched; this routine does
1322 not scan the base classes of CLASS_TYPE. */
1324 static int
1325 lookup_conversion_operator (tree class_type, tree type)
1327 int tpl_slot = -1;
1329 if (TYPE_HAS_CONVERSION (class_type))
1331 int i;
1332 tree fn;
1333 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1335 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1336 vec_safe_iterate (methods, i, &fn); ++i)
1338 /* All the conversion operators come near the beginning of
1339 the class. Therefore, if FN is not a conversion
1340 operator, there is no matching conversion operator in
1341 CLASS_TYPE. */
1342 fn = OVL_CURRENT (fn);
1343 if (!DECL_CONV_FN_P (fn))
1344 break;
1346 if (TREE_CODE (fn) == TEMPLATE_DECL)
1347 /* All the templated conversion functions are on the same
1348 slot, so remember it. */
1349 tpl_slot = i;
1350 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1351 return i;
1355 return tpl_slot;
1358 /* TYPE is a class type. Return the index of the fields within
1359 the method vector with name NAME, or -1 if no such field exists.
1360 Does not lazily declare implicitly-declared member functions. */
1362 static int
1363 lookup_fnfields_idx_nolazy (tree type, tree name)
1365 vec<tree, va_gc> *method_vec;
1366 tree fn;
1367 tree tmp;
1368 size_t i;
1370 if (!CLASS_TYPE_P (type))
1371 return -1;
1373 method_vec = CLASSTYPE_METHOD_VEC (type);
1374 if (!method_vec)
1375 return -1;
1377 if (GATHER_STATISTICS)
1378 n_calls_lookup_fnfields_1++;
1380 /* Constructors are first... */
1381 if (name == ctor_identifier)
1383 fn = CLASSTYPE_CONSTRUCTORS (type);
1384 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1386 /* and destructors are second. */
1387 if (name == dtor_identifier)
1389 fn = CLASSTYPE_DESTRUCTORS (type);
1390 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1392 if (IDENTIFIER_TYPENAME_P (name))
1393 return lookup_conversion_operator (type, TREE_TYPE (name));
1395 /* Skip the conversion operators. */
1396 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1397 vec_safe_iterate (method_vec, i, &fn);
1398 ++i)
1399 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1400 break;
1402 /* If the type is complete, use binary search. */
1403 if (COMPLETE_TYPE_P (type))
1405 int lo;
1406 int hi;
1408 lo = i;
1409 hi = method_vec->length ();
1410 while (lo < hi)
1412 i = (lo + hi) / 2;
1414 if (GATHER_STATISTICS)
1415 n_outer_fields_searched++;
1417 tmp = (*method_vec)[i];
1418 tmp = DECL_NAME (OVL_CURRENT (tmp));
1419 if (tmp > name)
1420 hi = i;
1421 else if (tmp < name)
1422 lo = i + 1;
1423 else
1424 return i;
1427 else
1428 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1430 if (GATHER_STATISTICS)
1431 n_outer_fields_searched++;
1432 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1433 return i;
1436 return -1;
1439 /* TYPE is a class type. Return the index of the fields within
1440 the method vector with name NAME, or -1 if no such field exists. */
1443 lookup_fnfields_1 (tree type, tree name)
1445 if (!CLASS_TYPE_P (type))
1446 return -1;
1448 if (COMPLETE_TYPE_P (type))
1450 if ((name == ctor_identifier
1451 || name == base_ctor_identifier
1452 || name == complete_ctor_identifier))
1454 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1455 lazily_declare_fn (sfk_constructor, type);
1456 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1457 lazily_declare_fn (sfk_copy_constructor, type);
1458 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1459 lazily_declare_fn (sfk_move_constructor, type);
1461 else if (name == ansi_assopname (NOP_EXPR))
1463 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1464 lazily_declare_fn (sfk_copy_assignment, type);
1465 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1466 lazily_declare_fn (sfk_move_assignment, type);
1468 else if ((name == dtor_identifier
1469 || name == base_dtor_identifier
1470 || name == complete_dtor_identifier
1471 || name == deleting_dtor_identifier)
1472 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1473 lazily_declare_fn (sfk_destructor, type);
1476 return lookup_fnfields_idx_nolazy (type, name);
1479 /* TYPE is a class type. Return the field within the method vector with
1480 name NAME, or NULL_TREE if no such field exists. */
1482 tree
1483 lookup_fnfields_slot (tree type, tree name)
1485 int ix = lookup_fnfields_1 (complete_type (type), name);
1486 if (ix < 0)
1487 return NULL_TREE;
1488 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1491 /* As above, but avoid lazily declaring functions. */
1493 tree
1494 lookup_fnfields_slot_nolazy (tree type, tree name)
1496 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1497 if (ix < 0)
1498 return NULL_TREE;
1499 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1502 /* Like lookup_fnfields_1, except that the name is extracted from
1503 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1506 class_method_index_for_fn (tree class_type, tree function)
1508 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1510 return lookup_fnfields_1 (class_type,
1511 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1512 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1513 DECL_NAME (function));
1517 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1518 the class or namespace used to qualify the name. CONTEXT_CLASS is
1519 the class corresponding to the object in which DECL will be used.
1520 Return a possibly modified version of DECL that takes into account
1521 the CONTEXT_CLASS.
1523 In particular, consider an expression like `B::m' in the context of
1524 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1525 then the most derived class indicated by the BASELINK_BINFO will be
1526 `B', not `D'. This function makes that adjustment. */
1528 tree
1529 adjust_result_of_qualified_name_lookup (tree decl,
1530 tree qualifying_scope,
1531 tree context_class)
1533 if (context_class && context_class != error_mark_node
1534 && CLASS_TYPE_P (context_class)
1535 && CLASS_TYPE_P (qualifying_scope)
1536 && DERIVED_FROM_P (qualifying_scope, context_class)
1537 && BASELINK_P (decl))
1539 tree base;
1541 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1542 Because we do not yet know which function will be chosen by
1543 overload resolution, we cannot yet check either accessibility
1544 or ambiguity -- in either case, the choice of a static member
1545 function might make the usage valid. */
1546 base = lookup_base (context_class, qualifying_scope,
1547 ba_unique, NULL, tf_none);
1548 if (base && base != error_mark_node)
1550 BASELINK_ACCESS_BINFO (decl) = base;
1551 BASELINK_BINFO (decl)
1552 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1553 ba_unique, NULL, tf_none);
1557 if (BASELINK_P (decl))
1558 BASELINK_QUALIFIED_P (decl) = true;
1560 return decl;
1564 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1565 PRE_FN is called in preorder, while POST_FN is called in postorder.
1566 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1567 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1568 that value is immediately returned and the walk is terminated. One
1569 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1570 POST_FN are passed the binfo to examine and the caller's DATA
1571 value. All paths are walked, thus virtual and morally virtual
1572 binfos can be multiply walked. */
1574 tree
1575 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1576 tree (*post_fn) (tree, void *), void *data)
1578 tree rval;
1579 unsigned ix;
1580 tree base_binfo;
1582 /* Call the pre-order walking function. */
1583 if (pre_fn)
1585 rval = pre_fn (binfo, data);
1586 if (rval)
1588 if (rval == dfs_skip_bases)
1589 goto skip_bases;
1590 return rval;
1594 /* Find the next child binfo to walk. */
1595 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1597 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1598 if (rval)
1599 return rval;
1602 skip_bases:
1603 /* Call the post-order walking function. */
1604 if (post_fn)
1606 rval = post_fn (binfo, data);
1607 gcc_assert (rval != dfs_skip_bases);
1608 return rval;
1611 return NULL_TREE;
1614 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1615 that binfos are walked at most once. */
1617 static tree
1618 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1619 tree (*post_fn) (tree, void *), void *data)
1621 tree rval;
1622 unsigned ix;
1623 tree base_binfo;
1625 /* Call the pre-order walking function. */
1626 if (pre_fn)
1628 rval = pre_fn (binfo, data);
1629 if (rval)
1631 if (rval == dfs_skip_bases)
1632 goto skip_bases;
1634 return rval;
1638 /* Find the next child binfo to walk. */
1639 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1641 if (BINFO_VIRTUAL_P (base_binfo))
1643 if (BINFO_MARKED (base_binfo))
1644 continue;
1645 BINFO_MARKED (base_binfo) = 1;
1648 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1649 if (rval)
1650 return rval;
1653 skip_bases:
1654 /* Call the post-order walking function. */
1655 if (post_fn)
1657 rval = post_fn (binfo, data);
1658 gcc_assert (rval != dfs_skip_bases);
1659 return rval;
1662 return NULL_TREE;
1665 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1666 BINFO. */
1668 static void
1669 dfs_unmark_r (tree binfo)
1671 unsigned ix;
1672 tree base_binfo;
1674 /* Process the basetypes. */
1675 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1677 if (BINFO_VIRTUAL_P (base_binfo))
1679 if (!BINFO_MARKED (base_binfo))
1680 continue;
1681 BINFO_MARKED (base_binfo) = 0;
1683 /* Only walk, if it can contain more virtual bases. */
1684 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1685 dfs_unmark_r (base_binfo);
1689 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1690 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1691 For diamond shaped hierarchies we must mark the virtual bases, to
1692 avoid multiple walks. */
1694 tree
1695 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1696 tree (*post_fn) (tree, void *), void *data)
1698 static int active = 0; /* We must not be called recursively. */
1699 tree rval;
1701 gcc_assert (pre_fn || post_fn);
1702 gcc_assert (!active);
1703 active++;
1705 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1706 /* We are not diamond shaped, and therefore cannot encounter the
1707 same binfo twice. */
1708 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1709 else
1711 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1712 if (!BINFO_INHERITANCE_CHAIN (binfo))
1714 /* We are at the top of the hierarchy, and can use the
1715 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1716 bases. */
1717 vec<tree, va_gc> *vbases;
1718 unsigned ix;
1719 tree base_binfo;
1721 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1722 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1723 BINFO_MARKED (base_binfo) = 0;
1725 else
1726 dfs_unmark_r (binfo);
1729 active--;
1731 return rval;
1734 /* Worker function for dfs_walk_once_accessible. Behaves like
1735 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1736 access given by the current context should be considered, (b) ONCE
1737 indicates whether bases should be marked during traversal. */
1739 static tree
1740 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1741 tree (*pre_fn) (tree, void *),
1742 tree (*post_fn) (tree, void *), void *data)
1744 tree rval = NULL_TREE;
1745 unsigned ix;
1746 tree base_binfo;
1748 /* Call the pre-order walking function. */
1749 if (pre_fn)
1751 rval = pre_fn (binfo, data);
1752 if (rval)
1754 if (rval == dfs_skip_bases)
1755 goto skip_bases;
1757 return rval;
1761 /* Find the next child binfo to walk. */
1762 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1764 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1766 if (mark && BINFO_MARKED (base_binfo))
1767 continue;
1769 /* If the base is inherited via private or protected
1770 inheritance, then we can't see it, unless we are a friend of
1771 the current binfo. */
1772 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1774 tree scope;
1775 if (!friends_p)
1776 continue;
1777 scope = current_scope ();
1778 if (!scope
1779 || TREE_CODE (scope) == NAMESPACE_DECL
1780 || !is_friend (BINFO_TYPE (binfo), scope))
1781 continue;
1784 if (mark)
1785 BINFO_MARKED (base_binfo) = 1;
1787 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1788 pre_fn, post_fn, data);
1789 if (rval)
1790 return rval;
1793 skip_bases:
1794 /* Call the post-order walking function. */
1795 if (post_fn)
1797 rval = post_fn (binfo, data);
1798 gcc_assert (rval != dfs_skip_bases);
1799 return rval;
1802 return NULL_TREE;
1805 /* Like dfs_walk_once except that only accessible bases are walked.
1806 FRIENDS_P indicates whether friendship of the local context
1807 should be considered when determining accessibility. */
1809 static tree
1810 dfs_walk_once_accessible (tree binfo, bool friends_p,
1811 tree (*pre_fn) (tree, void *),
1812 tree (*post_fn) (tree, void *), void *data)
1814 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1815 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1816 pre_fn, post_fn, data);
1818 if (diamond_shaped)
1820 if (!BINFO_INHERITANCE_CHAIN (binfo))
1822 /* We are at the top of the hierarchy, and can use the
1823 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1824 bases. */
1825 vec<tree, va_gc> *vbases;
1826 unsigned ix;
1827 tree base_binfo;
1829 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1830 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1831 BINFO_MARKED (base_binfo) = 0;
1833 else
1834 dfs_unmark_r (binfo);
1836 return rval;
1839 /* Check that virtual overrider OVERRIDER is acceptable for base function
1840 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1842 static int
1843 check_final_overrider (tree overrider, tree basefn)
1845 tree over_type = TREE_TYPE (overrider);
1846 tree base_type = TREE_TYPE (basefn);
1847 tree over_return = fndecl_declared_return_type (overrider);
1848 tree base_return = fndecl_declared_return_type (basefn);
1849 tree over_throw, base_throw;
1851 int fail = 0;
1853 if (DECL_INVALID_OVERRIDER_P (overrider))
1854 return 0;
1856 if (same_type_p (base_return, over_return))
1857 /* OK */;
1858 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1859 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1860 && POINTER_TYPE_P (base_return)))
1862 /* Potentially covariant. */
1863 unsigned base_quals, over_quals;
1865 fail = !POINTER_TYPE_P (base_return);
1866 if (!fail)
1868 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1870 base_return = TREE_TYPE (base_return);
1871 over_return = TREE_TYPE (over_return);
1873 base_quals = cp_type_quals (base_return);
1874 over_quals = cp_type_quals (over_return);
1876 if ((base_quals & over_quals) != over_quals)
1877 fail = 1;
1879 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1881 /* Strictly speaking, the standard requires the return type to be
1882 complete even if it only differs in cv-quals, but that seems
1883 like a bug in the wording. */
1884 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1885 over_return))
1887 tree binfo = lookup_base (over_return, base_return,
1888 ba_check, NULL, tf_none);
1890 if (!binfo || binfo == error_mark_node)
1891 fail = 1;
1894 else if (can_convert_standard (TREE_TYPE (base_type),
1895 TREE_TYPE (over_type),
1896 tf_warning_or_error))
1897 /* GNU extension, allow trivial pointer conversions such as
1898 converting to void *, or qualification conversion. */
1900 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1901 "invalid covariant return type for %q#D", overrider))
1902 inform (DECL_SOURCE_LOCATION (basefn),
1903 " overriding %q+#D", basefn);
1905 else
1906 fail = 2;
1908 else
1909 fail = 2;
1910 if (!fail)
1911 /* OK */;
1912 else
1914 if (fail == 1)
1916 error ("invalid covariant return type for %q+#D", overrider);
1917 error (" overriding %q+#D", basefn);
1919 else
1921 error ("conflicting return type specified for %q+#D", overrider);
1922 error (" overriding %q+#D", basefn);
1924 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1925 return 0;
1928 /* Check throw specifier is at least as strict. */
1929 maybe_instantiate_noexcept (basefn);
1930 maybe_instantiate_noexcept (overrider);
1931 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1932 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1934 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1936 error ("looser throw specifier for %q+#F", overrider);
1937 error (" overriding %q+#F", basefn);
1938 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1939 return 0;
1942 /* Check for conflicting type attributes. */
1943 if (!comp_type_attributes (over_type, base_type))
1945 error ("conflicting type attributes specified for %q+#D", overrider);
1946 error (" overriding %q+#D", basefn);
1947 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1948 return 0;
1951 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1953 if (DECL_DELETED_FN (overrider))
1955 error ("deleted function %q+D", overrider);
1956 error ("overriding non-deleted function %q+D", basefn);
1957 maybe_explain_implicit_delete (overrider);
1959 else
1961 error ("non-deleted function %q+D", overrider);
1962 error ("overriding deleted function %q+D", basefn);
1964 return 0;
1966 if (DECL_FINAL_P (basefn))
1968 error ("virtual function %q+D", overrider);
1969 error ("overriding final function %q+D", basefn);
1970 return 0;
1972 return 1;
1975 /* Given a class TYPE, and a function decl FNDECL, look for
1976 virtual functions in TYPE's hierarchy which FNDECL overrides.
1977 We do not look in TYPE itself, only its bases.
1979 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1980 find that it overrides anything.
1982 We check that every function which is overridden, is correctly
1983 overridden. */
1986 look_for_overrides (tree type, tree fndecl)
1988 tree binfo = TYPE_BINFO (type);
1989 tree base_binfo;
1990 int ix;
1991 int found = 0;
1993 /* A constructor for a class T does not override a function T
1994 in a base class. */
1995 if (DECL_CONSTRUCTOR_P (fndecl))
1996 return 0;
1998 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2000 tree basetype = BINFO_TYPE (base_binfo);
2002 if (TYPE_POLYMORPHIC_P (basetype))
2003 found += look_for_overrides_r (basetype, fndecl);
2005 return found;
2008 /* Look in TYPE for virtual functions with the same signature as
2009 FNDECL. */
2011 tree
2012 look_for_overrides_here (tree type, tree fndecl)
2014 int ix;
2016 /* If there are no methods in TYPE (meaning that only implicitly
2017 declared methods will ever be provided for TYPE), then there are
2018 no virtual functions. */
2019 if (!CLASSTYPE_METHOD_VEC (type))
2020 return NULL_TREE;
2022 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2023 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2024 else
2025 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2026 if (ix >= 0)
2028 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
2030 for (; fns; fns = OVL_NEXT (fns))
2032 tree fn = OVL_CURRENT (fns);
2034 if (!DECL_VIRTUAL_P (fn))
2035 /* Not a virtual. */;
2036 else if (DECL_CONTEXT (fn) != type)
2037 /* Introduced with a using declaration. */;
2038 else if (DECL_STATIC_FUNCTION_P (fndecl))
2040 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2041 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2042 if (compparms (TREE_CHAIN (btypes), dtypes))
2043 return fn;
2045 else if (same_signature_p (fndecl, fn))
2046 return fn;
2049 return NULL_TREE;
2052 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2053 TYPE itself and its bases. */
2055 static int
2056 look_for_overrides_r (tree type, tree fndecl)
2058 tree fn = look_for_overrides_here (type, fndecl);
2059 if (fn)
2061 if (DECL_STATIC_FUNCTION_P (fndecl))
2063 /* A static member function cannot match an inherited
2064 virtual member function. */
2065 error ("%q+#D cannot be declared", fndecl);
2066 error (" since %q+#D declared in base class", fn);
2068 else
2070 /* It's definitely virtual, even if not explicitly set. */
2071 DECL_VIRTUAL_P (fndecl) = 1;
2072 check_final_overrider (fndecl, fn);
2074 return 1;
2077 /* We failed to find one declared in this class. Look in its bases. */
2078 return look_for_overrides (type, fndecl);
2081 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2083 static tree
2084 dfs_get_pure_virtuals (tree binfo, void *data)
2086 tree type = (tree) data;
2088 /* We're not interested in primary base classes; the derived class
2089 of which they are a primary base will contain the information we
2090 need. */
2091 if (!BINFO_PRIMARY_P (binfo))
2093 tree virtuals;
2095 for (virtuals = BINFO_VIRTUALS (binfo);
2096 virtuals;
2097 virtuals = TREE_CHAIN (virtuals))
2098 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2099 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2102 return NULL_TREE;
2105 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2107 void
2108 get_pure_virtuals (tree type)
2110 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2111 is going to be overridden. */
2112 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2113 /* Now, run through all the bases which are not primary bases, and
2114 collect the pure virtual functions. We look at the vtable in
2115 each class to determine what pure virtual functions are present.
2116 (A primary base is not interesting because the derived class of
2117 which it is a primary base will contain vtable entries for the
2118 pure virtuals in the base class. */
2119 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2122 /* Debug info for C++ classes can get very large; try to avoid
2123 emitting it everywhere.
2125 Note that this optimization wins even when the target supports
2126 BINCL (if only slightly), and reduces the amount of work for the
2127 linker. */
2129 void
2130 maybe_suppress_debug_info (tree t)
2132 if (write_symbols == NO_DEBUG)
2133 return;
2135 /* We might have set this earlier in cp_finish_decl. */
2136 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2138 /* Always emit the information for each class every time. */
2139 if (flag_emit_class_debug_always)
2140 return;
2142 /* If we already know how we're handling this class, handle debug info
2143 the same way. */
2144 if (CLASSTYPE_INTERFACE_KNOWN (t))
2146 if (CLASSTYPE_INTERFACE_ONLY (t))
2147 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2148 /* else don't set it. */
2150 /* If the class has a vtable, write out the debug info along with
2151 the vtable. */
2152 else if (TYPE_CONTAINS_VPTR_P (t))
2153 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2155 /* Otherwise, just emit the debug info normally. */
2158 /* Note that we want debugging information for a base class of a class
2159 whose vtable is being emitted. Normally, this would happen because
2160 calling the constructor for a derived class implies calling the
2161 constructors for all bases, which involve initializing the
2162 appropriate vptr with the vtable for the base class; but in the
2163 presence of optimization, this initialization may be optimized
2164 away, so we tell finish_vtable_vardecl that we want the debugging
2165 information anyway. */
2167 static tree
2168 dfs_debug_mark (tree binfo, void * /*data*/)
2170 tree t = BINFO_TYPE (binfo);
2172 if (CLASSTYPE_DEBUG_REQUESTED (t))
2173 return dfs_skip_bases;
2175 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2177 return NULL_TREE;
2180 /* Write out the debugging information for TYPE, whose vtable is being
2181 emitted. Also walk through our bases and note that we want to
2182 write out information for them. This avoids the problem of not
2183 writing any debug info for intermediate basetypes whose
2184 constructors, and thus the references to their vtables, and thus
2185 the vtables themselves, were optimized away. */
2187 void
2188 note_debug_info_needed (tree type)
2190 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2192 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2193 rest_of_type_compilation (type, toplevel_bindings_p ());
2196 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2199 void
2200 print_search_statistics (void)
2202 if (! GATHER_STATISTICS)
2204 fprintf (stderr, "no search statistics\n");
2205 return;
2208 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2209 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2210 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2211 n_outer_fields_searched, n_calls_lookup_fnfields);
2212 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2215 void
2216 reinit_search_statistics (void)
2218 n_fields_searched = 0;
2219 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2220 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2221 n_calls_get_base_type = 0;
2222 n_outer_fields_searched = 0;
2223 n_contexts_saved = 0;
2226 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2227 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2228 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2229 bases have been encountered already in the tree walk. PARENT_CONVS
2230 is the list of lists of conversion functions that could hide CONV
2231 and OTHER_CONVS is the list of lists of conversion functions that
2232 could hide or be hidden by CONV, should virtualness be involved in
2233 the hierarchy. Merely checking the conversion op's name is not
2234 enough because two conversion operators to the same type can have
2235 different names. Return nonzero if we are visible. */
2237 static int
2238 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2239 tree to_type, tree parent_convs, tree other_convs)
2241 tree level, probe;
2243 /* See if we are hidden by a parent conversion. */
2244 for (level = parent_convs; level; level = TREE_CHAIN (level))
2245 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2246 if (same_type_p (to_type, TREE_TYPE (probe)))
2247 return 0;
2249 if (virtual_depth || virtualness)
2251 /* In a virtual hierarchy, we could be hidden, or could hide a
2252 conversion function on the other_convs list. */
2253 for (level = other_convs; level; level = TREE_CHAIN (level))
2255 int we_hide_them;
2256 int they_hide_us;
2257 tree *prev, other;
2259 if (!(virtual_depth || TREE_STATIC (level)))
2260 /* Neither is morally virtual, so cannot hide each other. */
2261 continue;
2263 if (!TREE_VALUE (level))
2264 /* They evaporated away already. */
2265 continue;
2267 they_hide_us = (virtual_depth
2268 && original_binfo (binfo, TREE_PURPOSE (level)));
2269 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2270 && original_binfo (TREE_PURPOSE (level), binfo));
2272 if (!(we_hide_them || they_hide_us))
2273 /* Neither is within the other, so no hiding can occur. */
2274 continue;
2276 for (prev = &TREE_VALUE (level), other = *prev; other;)
2278 if (same_type_p (to_type, TREE_TYPE (other)))
2280 if (they_hide_us)
2281 /* We are hidden. */
2282 return 0;
2284 if (we_hide_them)
2286 /* We hide the other one. */
2287 other = TREE_CHAIN (other);
2288 *prev = other;
2289 continue;
2292 prev = &TREE_CHAIN (other);
2293 other = *prev;
2297 return 1;
2300 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2301 of conversion functions, the first slot will be for the current
2302 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2303 of conversion functions from children of the current binfo,
2304 concatenated with conversions from elsewhere in the hierarchy --
2305 that list begins with OTHER_CONVS. Return a single list of lists
2306 containing only conversions from the current binfo and its
2307 children. */
2309 static tree
2310 split_conversions (tree my_convs, tree parent_convs,
2311 tree child_convs, tree other_convs)
2313 tree t;
2314 tree prev;
2316 /* Remove the original other_convs portion from child_convs. */
2317 for (prev = NULL, t = child_convs;
2318 t != other_convs; prev = t, t = TREE_CHAIN (t))
2319 continue;
2321 if (prev)
2322 TREE_CHAIN (prev) = NULL_TREE;
2323 else
2324 child_convs = NULL_TREE;
2326 /* Attach the child convs to any we had at this level. */
2327 if (my_convs)
2329 my_convs = parent_convs;
2330 TREE_CHAIN (my_convs) = child_convs;
2332 else
2333 my_convs = child_convs;
2335 return my_convs;
2338 /* Worker for lookup_conversions. Lookup conversion functions in
2339 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2340 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2341 encountered virtual bases already in the tree walk. PARENT_CONVS &
2342 PARENT_TPL_CONVS are lists of list of conversions within parent
2343 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2344 elsewhere in the tree. Return the conversions found within this
2345 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2346 encountered virtualness. We keep template and non-template
2347 conversions separate, to avoid unnecessary type comparisons.
2349 The located conversion functions are held in lists of lists. The
2350 TREE_VALUE of the outer list is the list of conversion functions
2351 found in a particular binfo. The TREE_PURPOSE of both the outer
2352 and inner lists is the binfo at which those conversions were
2353 found. TREE_STATIC is set for those lists within of morally
2354 virtual binfos. The TREE_VALUE of the inner list is the conversion
2355 function or overload itself. The TREE_TYPE of each inner list node
2356 is the converted-to type. */
2358 static int
2359 lookup_conversions_r (tree binfo,
2360 int virtual_depth, int virtualness,
2361 tree parent_convs, tree parent_tpl_convs,
2362 tree other_convs, tree other_tpl_convs,
2363 tree *convs, tree *tpl_convs)
2365 int my_virtualness = 0;
2366 tree my_convs = NULL_TREE;
2367 tree my_tpl_convs = NULL_TREE;
2368 tree child_convs = NULL_TREE;
2369 tree child_tpl_convs = NULL_TREE;
2370 unsigned i;
2371 tree base_binfo;
2372 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2373 tree conv;
2375 /* If we have no conversion operators, then don't look. */
2376 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2378 *convs = *tpl_convs = NULL_TREE;
2380 return 0;
2383 if (BINFO_VIRTUAL_P (binfo))
2384 virtual_depth++;
2386 /* First, locate the unhidden ones at this level. */
2387 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2388 vec_safe_iterate (method_vec, i, &conv);
2389 ++i)
2391 tree cur = OVL_CURRENT (conv);
2393 if (!DECL_CONV_FN_P (cur))
2394 break;
2396 if (TREE_CODE (cur) == TEMPLATE_DECL)
2398 /* Only template conversions can be overloaded, and we must
2399 flatten them out and check each one individually. */
2400 tree tpls;
2402 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2404 tree tpl = OVL_CURRENT (tpls);
2405 tree type = DECL_CONV_FN_TYPE (tpl);
2407 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2408 type, parent_tpl_convs, other_tpl_convs))
2410 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2411 TREE_TYPE (my_tpl_convs) = type;
2412 if (virtual_depth)
2414 TREE_STATIC (my_tpl_convs) = 1;
2415 my_virtualness = 1;
2420 else
2422 tree name = DECL_NAME (cur);
2424 if (!IDENTIFIER_MARKED (name))
2426 tree type = DECL_CONV_FN_TYPE (cur);
2427 if (type_uses_auto (type))
2429 mark_used (cur);
2430 type = DECL_CONV_FN_TYPE (cur);
2433 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2434 type, parent_convs, other_convs))
2436 my_convs = tree_cons (binfo, conv, my_convs);
2437 TREE_TYPE (my_convs) = type;
2438 if (virtual_depth)
2440 TREE_STATIC (my_convs) = 1;
2441 my_virtualness = 1;
2443 IDENTIFIER_MARKED (name) = 1;
2449 if (my_convs)
2451 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2452 if (virtual_depth)
2453 TREE_STATIC (parent_convs) = 1;
2456 if (my_tpl_convs)
2458 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2459 if (virtual_depth)
2460 TREE_STATIC (parent_tpl_convs) = 1;
2463 child_convs = other_convs;
2464 child_tpl_convs = other_tpl_convs;
2466 /* Now iterate over each base, looking for more conversions. */
2467 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2469 tree base_convs, base_tpl_convs;
2470 unsigned base_virtualness;
2472 base_virtualness = lookup_conversions_r (base_binfo,
2473 virtual_depth, virtualness,
2474 parent_convs, parent_tpl_convs,
2475 child_convs, child_tpl_convs,
2476 &base_convs, &base_tpl_convs);
2477 if (base_virtualness)
2478 my_virtualness = virtualness = 1;
2479 child_convs = chainon (base_convs, child_convs);
2480 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2483 /* Unmark the conversions found at this level */
2484 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2485 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2487 *convs = split_conversions (my_convs, parent_convs,
2488 child_convs, other_convs);
2489 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2490 child_tpl_convs, other_tpl_convs);
2492 return my_virtualness;
2495 /* Return a TREE_LIST containing all the non-hidden user-defined
2496 conversion functions for TYPE (and its base-classes). The
2497 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2498 function. The TREE_PURPOSE is the BINFO from which the conversion
2499 functions in this node were selected. This function is effectively
2500 performing a set of member lookups as lookup_fnfield does, but
2501 using the type being converted to as the unique key, rather than the
2502 field name. */
2504 tree
2505 lookup_conversions (tree type)
2507 tree convs, tpl_convs;
2508 tree list = NULL_TREE;
2510 complete_type (type);
2511 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2512 return NULL_TREE;
2514 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2515 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2516 &convs, &tpl_convs);
2518 /* Flatten the list-of-lists */
2519 for (; convs; convs = TREE_CHAIN (convs))
2521 tree probe, next;
2523 for (probe = TREE_VALUE (convs); probe; probe = next)
2525 next = TREE_CHAIN (probe);
2527 TREE_CHAIN (probe) = list;
2528 list = probe;
2532 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2534 tree probe, next;
2536 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2538 next = TREE_CHAIN (probe);
2540 TREE_CHAIN (probe) = list;
2541 list = probe;
2545 return 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. */
2551 tree
2552 binfo_from_vbase (tree binfo)
2554 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2556 if (BINFO_VIRTUAL_P (binfo))
2557 return binfo;
2559 return NULL_TREE;
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
2564 via virtual. */
2566 tree
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. */
2571 return NULL_TREE;
2573 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2574 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2576 if (BINFO_VIRTUAL_P (binfo))
2577 return binfo;
2579 return NULL_TREE;
2582 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2583 Find the equivalent binfo within whatever graph HERE is located.
2584 This is the inverse of original_binfo. */
2586 tree
2587 copied_binfo (tree binfo, tree here)
2589 tree result = NULL_TREE;
2591 if (BINFO_VIRTUAL_P (binfo))
2593 tree t;
2595 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2596 t = BINFO_INHERITANCE_CHAIN (t))
2597 continue;
2599 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2601 else if (BINFO_INHERITANCE_CHAIN (binfo))
2603 tree cbinfo;
2604 tree base_binfo;
2605 int ix;
2607 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2608 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2609 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2611 result = base_binfo;
2612 break;
2615 else
2617 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2618 result = here;
2621 gcc_assert (result);
2622 return result;
2625 tree
2626 binfo_for_vbase (tree base, tree t)
2628 unsigned ix;
2629 tree binfo;
2630 vec<tree, va_gc> *vbases;
2632 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2633 vec_safe_iterate (vbases, ix, &binfo); ix++)
2634 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2635 return binfo;
2636 return NULL;
2639 /* BINFO is some base binfo of HERE, within some other
2640 hierarchy. Return the equivalent binfo, but in the hierarchy
2641 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2642 is not a base binfo of HERE, returns NULL_TREE. */
2644 tree
2645 original_binfo (tree binfo, tree here)
2647 tree result = NULL;
2649 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2650 result = here;
2651 else if (BINFO_VIRTUAL_P (binfo))
2652 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2653 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2654 : NULL_TREE);
2655 else if (BINFO_INHERITANCE_CHAIN (binfo))
2657 tree base_binfos;
2659 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2660 if (base_binfos)
2662 int ix;
2663 tree base_binfo;
2665 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2666 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2667 BINFO_TYPE (binfo)))
2669 result = base_binfo;
2670 break;
2675 return result;