2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / search.c
blob53b038e5cf4a5095dcaf4f4b37592554fff8adcf
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2015 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 "alias.h"
29 #include "symtab.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "intl.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "target.h"
37 static int is_subobject_of_p (tree, tree);
38 static tree dfs_lookup_base (tree, void *);
39 static tree dfs_dcast_hint_pre (tree, void *);
40 static tree dfs_dcast_hint_post (tree, void *);
41 static tree dfs_debug_mark (tree, void *);
42 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
43 tree (*post_fn) (tree, void *), void *data);
44 static void dfs_unmark_r (tree);
45 static int check_hidden_convs (tree, int, int, tree, tree, tree);
46 static tree split_conversions (tree, tree, tree, tree);
47 static int lookup_conversions_r (tree, int, int,
48 tree, tree, tree, tree, tree *, tree *);
49 static int look_for_overrides_r (tree, tree);
50 static tree lookup_field_r (tree, void *);
51 static tree dfs_accessible_post (tree, void *);
52 static tree dfs_walk_once_accessible_r (tree, bool, bool,
53 tree (*pre_fn) (tree, void *),
54 tree (*post_fn) (tree, void *),
55 void *data);
56 static tree dfs_walk_once_accessible (tree, bool,
57 tree (*pre_fn) (tree, void *),
58 tree (*post_fn) (tree, void *),
59 void *data);
60 static tree dfs_access_in_type (tree, void *);
61 static access_kind access_in_type (tree, tree);
62 static int protected_accessible_p (tree, tree, tree);
63 static int friend_accessible_p (tree, tree, tree);
64 static tree dfs_get_pure_virtuals (tree, void *);
67 /* Variables for gathering statistics. */
68 static int n_fields_searched;
69 static int n_calls_lookup_field, n_calls_lookup_field_1;
70 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
71 static int n_calls_get_base_type;
72 static int n_outer_fields_searched;
73 static int n_contexts_saved;
76 /* Data for lookup_base and its workers. */
78 struct lookup_base_data_s
80 tree t; /* type being searched. */
81 tree base; /* The base type we're looking for. */
82 tree binfo; /* Found binfo. */
83 bool via_virtual; /* Found via a virtual path. */
84 bool ambiguous; /* Found multiply ambiguous */
85 bool repeated_base; /* Whether there are repeated bases in the
86 hierarchy. */
87 bool want_any; /* Whether we want any matching binfo. */
90 /* Worker function for lookup_base. See if we've found the desired
91 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
93 static tree
94 dfs_lookup_base (tree binfo, void *data_)
96 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
98 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
100 if (!data->binfo)
102 data->binfo = binfo;
103 data->via_virtual
104 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
106 if (!data->repeated_base)
107 /* If there are no repeated bases, we can stop now. */
108 return binfo;
110 if (data->want_any && !data->via_virtual)
111 /* If this is a non-virtual base, then we can't do
112 better. */
113 return binfo;
115 return dfs_skip_bases;
117 else
119 gcc_assert (binfo != data->binfo);
121 /* We've found more than one matching binfo. */
122 if (!data->want_any)
124 /* This is immediately ambiguous. */
125 data->binfo = NULL_TREE;
126 data->ambiguous = true;
127 return error_mark_node;
130 /* Prefer one via a non-virtual path. */
131 if (!binfo_via_virtual (binfo, data->t))
133 data->binfo = binfo;
134 data->via_virtual = false;
135 return binfo;
138 /* There must be repeated bases, otherwise we'd have stopped
139 on the first base we found. */
140 return dfs_skip_bases;
144 return NULL_TREE;
147 /* Returns true if type BASE is accessible in T. (BASE is known to be
148 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
149 true, consider any special access of the current scope, or access
150 bestowed by friendship. */
152 bool
153 accessible_base_p (tree t, tree base, bool consider_local_p)
155 tree decl;
157 /* [class.access.base]
159 A base class is said to be accessible if an invented public
160 member of the base class is accessible.
162 If BASE is a non-proper base, this condition is trivially
163 true. */
164 if (same_type_p (t, base))
165 return true;
166 /* Rather than inventing a public member, we use the implicit
167 public typedef created in the scope of every class. */
168 decl = TYPE_FIELDS (base);
169 while (!DECL_SELF_REFERENCE_P (decl))
170 decl = DECL_CHAIN (decl);
171 while (ANON_AGGR_TYPE_P (t))
172 t = TYPE_CONTEXT (t);
173 return accessible_p (t, decl, consider_local_p);
176 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
177 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
178 non-NULL, fill with information about what kind of base we
179 discovered.
181 If the base is inaccessible, or ambiguous, then error_mark_node is
182 returned. If the tf_error bit of COMPLAIN is not set, no error
183 is issued. */
185 tree
186 lookup_base (tree t, tree base, base_access access,
187 base_kind *kind_ptr, tsubst_flags_t complain)
189 tree binfo;
190 tree t_binfo;
191 base_kind bk;
193 /* "Nothing" is definitely not derived from Base. */
194 if (t == NULL_TREE)
196 if (kind_ptr)
197 *kind_ptr = bk_not_base;
198 return NULL_TREE;
201 if (t == error_mark_node || base == error_mark_node)
203 if (kind_ptr)
204 *kind_ptr = bk_not_base;
205 return error_mark_node;
207 gcc_assert (TYPE_P (base));
209 if (!TYPE_P (t))
211 t_binfo = t;
212 t = BINFO_TYPE (t);
214 else
216 t = complete_type (TYPE_MAIN_VARIANT (t));
217 t_binfo = TYPE_BINFO (t);
220 base = TYPE_MAIN_VARIANT (base);
222 /* If BASE is incomplete, it can't be a base of T--and instantiating it
223 might cause an error. */
224 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
226 struct lookup_base_data_s data;
228 data.t = t;
229 data.base = base;
230 data.binfo = NULL_TREE;
231 data.ambiguous = data.via_virtual = false;
232 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
233 data.want_any = access == ba_any;
235 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
236 binfo = data.binfo;
238 if (!binfo)
239 bk = data.ambiguous ? bk_ambig : bk_not_base;
240 else if (binfo == t_binfo)
241 bk = bk_same_type;
242 else if (data.via_virtual)
243 bk = bk_via_virtual;
244 else
245 bk = bk_proper_base;
247 else
249 binfo = NULL_TREE;
250 bk = bk_not_base;
253 /* Check that the base is unambiguous and accessible. */
254 if (access != ba_any)
255 switch (bk)
257 case bk_not_base:
258 break;
260 case bk_ambig:
261 if (complain & tf_error)
262 error ("%qT is an ambiguous base of %qT", base, t);
263 binfo = error_mark_node;
264 break;
266 default:
267 if ((access & ba_check_bit)
268 /* If BASE is incomplete, then BASE and TYPE are probably
269 the same, in which case BASE is accessible. If they
270 are not the same, then TYPE is invalid. In that case,
271 there's no need to issue another error here, and
272 there's no implicit typedef to use in the code that
273 follows, so we skip the check. */
274 && COMPLETE_TYPE_P (base)
275 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
277 if (complain & tf_error)
278 error ("%qT is an inaccessible base of %qT", base, t);
279 binfo = error_mark_node;
280 bk = bk_inaccessible;
282 break;
285 if (kind_ptr)
286 *kind_ptr = bk;
288 return binfo;
291 /* Data for dcast_base_hint walker. */
293 struct dcast_data_s
295 tree subtype; /* The base type we're looking for. */
296 int virt_depth; /* Number of virtual bases encountered from most
297 derived. */
298 tree offset; /* Best hint offset discovered so far. */
299 bool repeated_base; /* Whether there are repeated bases in the
300 hierarchy. */
303 /* Worker for dcast_base_hint. Search for the base type being cast
304 from. */
306 static tree
307 dfs_dcast_hint_pre (tree binfo, void *data_)
309 struct dcast_data_s *data = (struct dcast_data_s *) data_;
311 if (BINFO_VIRTUAL_P (binfo))
312 data->virt_depth++;
314 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
316 if (data->virt_depth)
318 data->offset = ssize_int (-1);
319 return data->offset;
321 if (data->offset)
322 data->offset = ssize_int (-3);
323 else
324 data->offset = BINFO_OFFSET (binfo);
326 return data->repeated_base ? dfs_skip_bases : data->offset;
329 return NULL_TREE;
332 /* Worker for dcast_base_hint. Track the virtual depth. */
334 static tree
335 dfs_dcast_hint_post (tree binfo, void *data_)
337 struct dcast_data_s *data = (struct dcast_data_s *) data_;
339 if (BINFO_VIRTUAL_P (binfo))
340 data->virt_depth--;
342 return NULL_TREE;
345 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
346 started from is related to the required TARGET type, in order to optimize
347 the inheritance graph search. This information is independent of the
348 current context, and ignores private paths, hence get_base_distance is
349 inappropriate. Return a TREE specifying the base offset, BOFF.
350 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
351 and there are no public virtual SUBTYPE bases.
352 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
353 BOFF == -2, SUBTYPE is not a public base.
354 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
356 tree
357 dcast_base_hint (tree subtype, tree target)
359 struct dcast_data_s data;
361 data.subtype = subtype;
362 data.virt_depth = 0;
363 data.offset = NULL_TREE;
364 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
366 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
367 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
368 return data.offset ? data.offset : ssize_int (-2);
371 /* Search for a member with name NAME in a multiple inheritance
372 lattice specified by TYPE. If it does not exist, return NULL_TREE.
373 If the member is ambiguously referenced, return `error_mark_node'.
374 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
375 true, type declarations are preferred. */
377 /* Do a 1-level search for NAME as a member of TYPE. The caller must
378 figure out whether it can access this field. (Since it is only one
379 level, this is reasonable.) */
381 tree
382 lookup_field_1 (tree type, tree name, bool want_type)
384 tree field;
386 gcc_assert (identifier_p (name));
388 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
389 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
390 || TREE_CODE (type) == TYPENAME_TYPE)
391 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
392 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
393 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
394 the code often worked even when we treated the index as a list
395 of fields!)
396 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
397 return NULL_TREE;
399 if (CLASSTYPE_SORTED_FIELDS (type))
401 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
402 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
403 int i;
405 while (lo < hi)
407 i = (lo + hi) / 2;
409 if (GATHER_STATISTICS)
410 n_fields_searched++;
412 if (DECL_NAME (fields[i]) > name)
413 hi = i;
414 else if (DECL_NAME (fields[i]) < name)
415 lo = i + 1;
416 else
418 field = NULL_TREE;
420 /* We might have a nested class and a field with the
421 same name; we sorted them appropriately via
422 field_decl_cmp, so just look for the first or last
423 field with this name. */
424 if (want_type)
427 field = fields[i--];
428 while (i >= lo && DECL_NAME (fields[i]) == name);
429 if (!DECL_DECLARES_TYPE_P (field))
430 field = NULL_TREE;
432 else
435 field = fields[i++];
436 while (i < hi && DECL_NAME (fields[i]) == name);
439 if (field)
441 field = strip_using_decl (field);
442 if (is_overloaded_fn (field))
443 field = NULL_TREE;
446 return field;
449 return NULL_TREE;
452 field = TYPE_FIELDS (type);
454 if (GATHER_STATISTICS)
455 n_calls_lookup_field_1++;
457 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
459 tree decl = field;
461 if (GATHER_STATISTICS)
462 n_fields_searched++;
464 gcc_assert (DECL_P (field));
465 if (DECL_NAME (field) == NULL_TREE
466 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
468 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
469 if (temp)
470 return temp;
473 if (TREE_CODE (decl) == USING_DECL
474 && DECL_NAME (decl) == name)
476 decl = strip_using_decl (decl);
477 if (is_overloaded_fn (decl))
478 continue;
481 if (DECL_NAME (decl) == name
482 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
483 return decl;
485 /* Not found. */
486 if (name == vptr_identifier)
488 /* Give the user what s/he thinks s/he wants. */
489 if (TYPE_POLYMORPHIC_P (type))
490 return TYPE_VFIELD (type);
492 return NULL_TREE;
495 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
496 NAMESPACE_DECL corresponding to the innermost non-block scope. */
498 tree
499 current_scope (void)
501 /* There are a number of cases we need to be aware of here:
502 current_class_type current_function_decl
503 global NULL NULL
504 fn-local NULL SET
505 class-local SET NULL
506 class->fn SET SET
507 fn->class SET SET
509 Those last two make life interesting. If we're in a function which is
510 itself inside a class, we need decls to go into the fn's decls (our
511 second case below). But if we're in a class and the class itself is
512 inside a function, we need decls to go into the decls for the class. To
513 achieve this last goal, we must see if, when both current_class_ptr and
514 current_function_decl are set, the class was declared inside that
515 function. If so, we know to put the decls into the class's scope. */
516 if (current_function_decl && current_class_type
517 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
518 && same_type_p (DECL_CONTEXT (current_function_decl),
519 current_class_type))
520 || (DECL_FRIEND_CONTEXT (current_function_decl)
521 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
522 current_class_type))))
523 return current_function_decl;
524 if (current_class_type)
525 return current_class_type;
526 if (current_function_decl)
527 return current_function_decl;
528 return current_namespace;
531 /* Returns nonzero if we are currently in a function scope. Note
532 that this function returns zero if we are within a local class, but
533 not within a member function body of the local class. */
536 at_function_scope_p (void)
538 tree cs = current_scope ();
539 /* Also check cfun to make sure that we're really compiling
540 this function (as opposed to having set current_function_decl
541 for access checking or some such). */
542 return (cs && TREE_CODE (cs) == FUNCTION_DECL
543 && cfun && cfun->decl == current_function_decl);
546 /* Returns true if the innermost active scope is a class scope. */
548 bool
549 at_class_scope_p (void)
551 tree cs = current_scope ();
552 return cs && TYPE_P (cs);
555 /* Returns true if the innermost active scope is a namespace scope. */
557 bool
558 at_namespace_scope_p (void)
560 tree cs = current_scope ();
561 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
564 /* Return the scope of DECL, as appropriate when doing name-lookup. */
566 tree
567 context_for_name_lookup (tree decl)
569 /* [class.union]
571 For the purposes of name lookup, after the anonymous union
572 definition, the members of the anonymous union are considered to
573 have been defined in the scope in which the anonymous union is
574 declared. */
575 tree context = DECL_CONTEXT (decl);
577 while (context && TYPE_P (context)
578 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
579 context = TYPE_CONTEXT (context);
580 if (!context)
581 context = global_namespace;
583 return context;
586 /* The accessibility routines use BINFO_ACCESS for scratch space
587 during the computation of the accessibility of some declaration. */
589 #define BINFO_ACCESS(NODE) \
590 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
592 /* Set the access associated with NODE to ACCESS. */
594 #define SET_BINFO_ACCESS(NODE, ACCESS) \
595 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
596 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
598 /* Called from access_in_type via dfs_walk. Calculate the access to
599 DATA (which is really a DECL) in BINFO. */
601 static tree
602 dfs_access_in_type (tree binfo, void *data)
604 tree decl = (tree) data;
605 tree type = BINFO_TYPE (binfo);
606 access_kind access = ak_none;
608 if (context_for_name_lookup (decl) == type)
610 /* If we have descended to the scope of DECL, just note the
611 appropriate access. */
612 if (TREE_PRIVATE (decl))
613 access = ak_private;
614 else if (TREE_PROTECTED (decl))
615 access = ak_protected;
616 else
617 access = ak_public;
619 else
621 /* First, check for an access-declaration that gives us more
622 access to the DECL. */
623 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
625 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
627 if (decl_access)
629 decl_access = TREE_VALUE (decl_access);
631 if (decl_access == access_public_node)
632 access = ak_public;
633 else if (decl_access == access_protected_node)
634 access = ak_protected;
635 else if (decl_access == access_private_node)
636 access = ak_private;
637 else
638 gcc_unreachable ();
642 if (!access)
644 int i;
645 tree base_binfo;
646 vec<tree, va_gc> *accesses;
648 /* Otherwise, scan our baseclasses, and pick the most favorable
649 access. */
650 accesses = BINFO_BASE_ACCESSES (binfo);
651 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
653 tree base_access = (*accesses)[i];
654 access_kind base_access_now = BINFO_ACCESS (base_binfo);
656 if (base_access_now == ak_none || base_access_now == ak_private)
657 /* If it was not accessible in the base, or only
658 accessible as a private member, we can't access it
659 all. */
660 base_access_now = ak_none;
661 else if (base_access == access_protected_node)
662 /* Public and protected members in the base become
663 protected here. */
664 base_access_now = ak_protected;
665 else if (base_access == access_private_node)
666 /* Public and protected members in the base become
667 private here. */
668 base_access_now = ak_private;
670 /* See if the new access, via this base, gives more
671 access than our previous best access. */
672 if (base_access_now != ak_none
673 && (access == ak_none || base_access_now < access))
675 access = base_access_now;
677 /* If the new access is public, we can't do better. */
678 if (access == ak_public)
679 break;
685 /* Note the access to DECL in TYPE. */
686 SET_BINFO_ACCESS (binfo, access);
688 return NULL_TREE;
691 /* Return the access to DECL in TYPE. */
693 static access_kind
694 access_in_type (tree type, tree decl)
696 tree binfo = TYPE_BINFO (type);
698 /* We must take into account
700 [class.paths]
702 If a name can be reached by several paths through a multiple
703 inheritance graph, the access is that of the path that gives
704 most access.
706 The algorithm we use is to make a post-order depth-first traversal
707 of the base-class hierarchy. As we come up the tree, we annotate
708 each node with the most lenient access. */
709 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
711 return BINFO_ACCESS (binfo);
714 /* Returns nonzero if it is OK to access DECL through an object
715 indicated by BINFO in the context of DERIVED. */
717 static int
718 protected_accessible_p (tree decl, tree derived, tree binfo)
720 access_kind access;
722 /* We're checking this clause from [class.access.base]
724 m as a member of N is protected, and the reference occurs in a
725 member or friend of class N, or in a member or friend of a
726 class P derived from N, where m as a member of P is public, private
727 or protected.
729 Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N. */
731 /* If DERIVED isn't derived from N, then it can't be a P. */
732 if (!DERIVED_FROM_P (BINFO_TYPE (binfo), derived))
733 return 0;
735 access = access_in_type (derived, decl);
737 /* If m is inaccessible in DERIVED, then it's not a P. */
738 if (access == ak_none)
739 return 0;
741 /* [class.protected]
743 When a friend or a member function of a derived class references
744 a protected nonstatic member of a base class, an access check
745 applies in addition to those described earlier in clause
746 _class.access_) Except when forming a pointer to member
747 (_expr.unary.op_), the access must be through a pointer to,
748 reference to, or object of the derived class itself (or any class
749 derived from that class) (_expr.ref_). If the access is to form
750 a pointer to member, the nested-name-specifier shall name the
751 derived class (or any class derived from that class). */
752 if (DECL_NONSTATIC_MEMBER_P (decl))
754 /* We can tell through what the reference is occurring by
755 chasing BINFO up to the root. */
756 tree t = binfo;
757 while (BINFO_INHERITANCE_CHAIN (t))
758 t = BINFO_INHERITANCE_CHAIN (t);
760 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
761 return 0;
764 return 1;
767 /* Returns nonzero if SCOPE is a friend of a type which would be able
768 to access DECL through the object indicated by BINFO. */
770 static int
771 friend_accessible_p (tree scope, tree decl, tree binfo)
773 tree befriending_classes;
774 tree t;
776 if (!scope)
777 return 0;
779 if (DECL_DECLARES_FUNCTION_P (scope))
780 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
781 else if (TYPE_P (scope))
782 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
783 else
784 return 0;
786 for (t = befriending_classes; t; t = TREE_CHAIN (t))
787 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
788 return 1;
790 /* Nested classes have the same access as their enclosing types, as
791 per DR 45 (this is a change from the standard). */
792 if (TYPE_P (scope))
793 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
794 if (protected_accessible_p (decl, t, binfo))
795 return 1;
797 if (DECL_DECLARES_FUNCTION_P (scope))
799 /* Perhaps this SCOPE is a member of a class which is a
800 friend. */
801 if (DECL_CLASS_SCOPE_P (scope)
802 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
803 return 1;
805 /* Or an instantiation of something which is a friend. */
806 if (DECL_TEMPLATE_INFO (scope))
808 int ret;
809 /* Increment processing_template_decl to make sure that
810 dependent_type_p works correctly. */
811 ++processing_template_decl;
812 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
813 --processing_template_decl;
814 return ret;
818 return 0;
821 /* Called via dfs_walk_once_accessible from accessible_p */
823 static tree
824 dfs_accessible_post (tree binfo, void * /*data*/)
826 if (BINFO_ACCESS (binfo) != ak_none)
828 tree scope = current_scope ();
829 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
830 && is_friend (BINFO_TYPE (binfo), scope))
831 return binfo;
834 return NULL_TREE;
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;
847 return val;
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)
861 tree binfo;
862 tree scope;
863 access_kind access;
865 /* Nonzero if it's OK to access DECL if it has protected
866 accessibility in TYPE. */
867 int protected_ok = 0;
869 /* If this declaration is in a block or namespace scope, there's no
870 access control. */
871 if (!TYPE_P (context_for_name_lookup (decl)))
872 return 1;
874 /* There is no need to perform access checks inside a thunk. */
875 scope = current_scope ();
876 if (scope && DECL_THUNK_P (scope))
877 return 1;
879 /* In a template declaration, we cannot be sure whether the
880 particular specialization that is instantiated will be a friend
881 or not. Therefore, all access checks are deferred until
882 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
883 parameter list for a template (because we may see dependent types
884 in default arguments for template parameters), and access
885 checking should be performed in the outermost parameter list. */
886 if (processing_template_decl
887 && (!processing_template_parmlist || processing_template_decl > 1))
888 return 1;
890 if (!TYPE_P (type))
892 binfo = type;
893 type = BINFO_TYPE (type);
895 else
896 binfo = TYPE_BINFO (type);
898 /* [class.access.base]
900 A member m is accessible when named in class N if
902 --m as a member of N is public, or
904 --m as a member of N is private, and the reference occurs in a
905 member or friend of class N, or
907 --m as a member of N is protected, and the reference occurs in a
908 member or friend of class N, or in a member or friend of a
909 class P derived from N, where m as a member of P is private or
910 protected, or
912 --there exists a base class B of N that is accessible at the point
913 of reference, and m is accessible when named in class B.
915 We walk the base class hierarchy, checking these conditions. */
917 if (consider_local_p)
919 /* Figure out where the reference is occurring. Check to see if
920 DECL is private or protected in this scope, since that will
921 determine whether protected access is allowed. */
922 tree ct = current_nonlambda_class_type ();
923 if (ct)
924 protected_ok = protected_accessible_p (decl,
926 binfo);
928 /* Now, loop through the classes of which we are a friend. */
929 if (!protected_ok)
930 protected_ok = friend_accessible_p (scope, decl, binfo);
933 /* Standardize the binfo that access_in_type will use. We don't
934 need to know what path was chosen from this point onwards. */
935 binfo = TYPE_BINFO (type);
937 /* Compute the accessibility of DECL in the class hierarchy
938 dominated by type. */
939 access = access_in_type (type, decl);
940 if (access == ak_public
941 || (access == ak_protected && protected_ok))
942 return 1;
944 if (!consider_local_p)
945 return 0;
947 /* Walk the hierarchy again, looking for a base class that allows
948 access. */
949 return dfs_walk_once_accessible (binfo, /*friends=*/true,
950 NULL, dfs_accessible_post, NULL)
951 != NULL_TREE;
954 struct lookup_field_info {
955 /* The type in which we're looking. */
956 tree type;
957 /* The name of the field for which we're looking. */
958 tree name;
959 /* If non-NULL, the current result of the lookup. */
960 tree rval;
961 /* The path to RVAL. */
962 tree rval_binfo;
963 /* If non-NULL, the lookup was ambiguous, and this is a list of the
964 candidates. */
965 tree ambiguous;
966 /* If nonzero, we are looking for types, not data members. */
967 int want_type;
968 /* If something went wrong, a message indicating what. */
969 const char *errstr;
972 /* Nonzero for a class member means that it is shared between all objects
973 of that class.
975 [class.member.lookup]:If the resulting set of declarations are not all
976 from sub-objects of the same type, or the set has a nonstatic member
977 and includes members from distinct sub-objects, there is an ambiguity
978 and the program is ill-formed.
980 This function checks that T contains no nonstatic members. */
983 shared_member_p (tree t)
985 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
986 || TREE_CODE (t) == CONST_DECL)
987 return 1;
988 if (is_overloaded_fn (t))
990 t = get_fns (t);
991 for (; t; t = OVL_NEXT (t))
993 tree fn = OVL_CURRENT (t);
994 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
995 return 0;
997 return 1;
999 return 0;
1002 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1003 found as a base class and sub-object of the object denoted by
1004 BINFO. */
1006 static int
1007 is_subobject_of_p (tree parent, tree binfo)
1009 tree probe;
1011 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1013 if (probe == binfo)
1014 return 1;
1015 if (BINFO_VIRTUAL_P (probe))
1016 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1017 != NULL_TREE);
1019 return 0;
1022 /* DATA is really a struct lookup_field_info. Look for a field with
1023 the name indicated there in BINFO. If this function returns a
1024 non-NULL value it is the result of the lookup. Called from
1025 lookup_field via breadth_first_search. */
1027 static tree
1028 lookup_field_r (tree binfo, void *data)
1030 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1031 tree type = BINFO_TYPE (binfo);
1032 tree nval = NULL_TREE;
1034 /* If this is a dependent base, don't look in it. */
1035 if (BINFO_DEPENDENT_BASE_P (binfo))
1036 return NULL_TREE;
1038 /* If this base class is hidden by the best-known value so far, we
1039 don't need to look. */
1040 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1041 && !BINFO_VIRTUAL_P (binfo))
1042 return dfs_skip_bases;
1044 /* First, look for a function. There can't be a function and a data
1045 member with the same name, and if there's a function and a type
1046 with the same name, the type is hidden by the function. */
1047 if (!lfi->want_type)
1048 nval = lookup_fnfields_slot (type, lfi->name);
1050 if (!nval)
1051 /* Look for a data member or type. */
1052 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1054 /* If there is no declaration with the indicated name in this type,
1055 then there's nothing to do. */
1056 if (!nval)
1057 goto done;
1059 /* If we're looking up a type (as with an elaborated type specifier)
1060 we ignore all non-types we find. */
1061 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1063 if (lfi->name == TYPE_IDENTIFIER (type))
1065 /* If the aggregate has no user defined constructors, we allow
1066 it to have fields with the same name as the enclosing type.
1067 If we are looking for that name, find the corresponding
1068 TYPE_DECL. */
1069 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1070 if (DECL_NAME (nval) == lfi->name
1071 && TREE_CODE (nval) == TYPE_DECL)
1072 break;
1074 else
1075 nval = NULL_TREE;
1076 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1078 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1079 lfi->name);
1080 if (e != NULL)
1081 nval = TYPE_MAIN_DECL (e->type);
1082 else
1083 goto done;
1087 /* If the lookup already found a match, and the new value doesn't
1088 hide the old one, we might have an ambiguity. */
1089 if (lfi->rval_binfo
1090 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1093 if (nval == lfi->rval && shared_member_p (nval))
1094 /* The two things are really the same. */
1096 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1097 /* The previous value hides the new one. */
1099 else
1101 /* We have a real ambiguity. We keep a chain of all the
1102 candidates. */
1103 if (!lfi->ambiguous && lfi->rval)
1105 /* This is the first time we noticed an ambiguity. Add
1106 what we previously thought was a reasonable candidate
1107 to the list. */
1108 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1109 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1112 /* Add the new value. */
1113 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1114 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1115 lfi->errstr = G_("request for member %qD is ambiguous");
1118 else
1120 lfi->rval = nval;
1121 lfi->rval_binfo = binfo;
1124 done:
1125 /* Don't look for constructors or destructors in base classes. */
1126 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1127 return dfs_skip_bases;
1128 return NULL_TREE;
1131 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1132 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1133 FUNCTIONS, and OPTYPE respectively. */
1135 tree
1136 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1138 tree baselink;
1140 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1141 || TREE_CODE (functions) == TEMPLATE_DECL
1142 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1143 || TREE_CODE (functions) == OVERLOAD);
1144 gcc_assert (!optype || TYPE_P (optype));
1145 gcc_assert (TREE_TYPE (functions));
1147 baselink = make_node (BASELINK);
1148 TREE_TYPE (baselink) = TREE_TYPE (functions);
1149 BASELINK_BINFO (baselink) = binfo;
1150 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1151 BASELINK_FUNCTIONS (baselink) = functions;
1152 BASELINK_OPTYPE (baselink) = optype;
1154 return baselink;
1157 /* Look for a member named NAME in an inheritance lattice dominated by
1158 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1159 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1160 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1161 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1162 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1163 TREE_VALUEs are the list of ambiguous candidates.
1165 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1167 If nothing can be found return NULL_TREE and do not issue an error. */
1169 tree
1170 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1171 tsubst_flags_t complain)
1173 tree rval, rval_binfo = NULL_TREE;
1174 tree type = NULL_TREE, basetype_path = NULL_TREE;
1175 struct lookup_field_info lfi;
1177 /* rval_binfo is the binfo associated with the found member, note,
1178 this can be set with useful information, even when rval is not
1179 set, because it must deal with ALL members, not just non-function
1180 members. It is used for ambiguity checking and the hidden
1181 checks. Whereas rval is only set if a proper (not hidden)
1182 non-function member is found. */
1184 const char *errstr = 0;
1186 if (name == error_mark_node
1187 || xbasetype == NULL_TREE
1188 || xbasetype == error_mark_node)
1189 return NULL_TREE;
1191 gcc_assert (identifier_p (name));
1193 if (TREE_CODE (xbasetype) == TREE_BINFO)
1195 type = BINFO_TYPE (xbasetype);
1196 basetype_path = xbasetype;
1198 else
1200 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1201 return NULL_TREE;
1202 type = xbasetype;
1203 xbasetype = NULL_TREE;
1206 type = complete_type (type);
1207 if (!basetype_path)
1208 basetype_path = TYPE_BINFO (type);
1210 if (!basetype_path)
1211 return NULL_TREE;
1213 if (GATHER_STATISTICS)
1214 n_calls_lookup_field++;
1216 memset (&lfi, 0, sizeof (lfi));
1217 lfi.type = type;
1218 lfi.name = name;
1219 lfi.want_type = want_type;
1220 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1221 rval = lfi.rval;
1222 rval_binfo = lfi.rval_binfo;
1223 if (rval_binfo)
1224 type = BINFO_TYPE (rval_binfo);
1225 errstr = lfi.errstr;
1227 /* If we are not interested in ambiguities, don't report them;
1228 just return NULL_TREE. */
1229 if (!protect && lfi.ambiguous)
1230 return NULL_TREE;
1232 if (protect == 2)
1234 if (lfi.ambiguous)
1235 return lfi.ambiguous;
1236 else
1237 protect = 0;
1240 /* [class.access]
1242 In the case of overloaded function names, access control is
1243 applied to the function selected by overloaded resolution.
1245 We cannot check here, even if RVAL is only a single non-static
1246 member function, since we do not know what the "this" pointer
1247 will be. For:
1249 class A { protected: void f(); };
1250 class B : public A {
1251 void g(A *p) {
1252 f(); // OK
1253 p->f(); // Not OK.
1257 only the first call to "f" is valid. However, if the function is
1258 static, we can check. */
1259 if (rval && protect
1260 && !really_overloaded_fn (rval))
1262 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1263 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1264 && !perform_or_defer_access_check (basetype_path, decl, decl,
1265 complain))
1266 rval = error_mark_node;
1269 if (errstr && protect)
1271 if (complain & tf_error)
1273 error (errstr, name, type);
1274 if (lfi.ambiguous)
1275 print_candidates (lfi.ambiguous);
1277 rval = error_mark_node;
1280 if (rval && is_overloaded_fn (rval))
1281 rval = build_baselink (rval_binfo, basetype_path, rval,
1282 (IDENTIFIER_TYPENAME_P (name)
1283 ? TREE_TYPE (name): NULL_TREE));
1284 return rval;
1287 /* Like lookup_member, except that if we find a function member we
1288 return NULL_TREE. */
1290 tree
1291 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1293 tree rval = lookup_member (xbasetype, name, protect, want_type,
1294 tf_warning_or_error);
1296 /* Ignore functions, but propagate the ambiguity list. */
1297 if (!error_operand_p (rval)
1298 && (rval && BASELINK_P (rval)))
1299 return NULL_TREE;
1301 return rval;
1304 /* Like lookup_member, except that if we find a non-function member we
1305 return NULL_TREE. */
1307 tree
1308 lookup_fnfields (tree xbasetype, tree name, int protect)
1310 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1311 tf_warning_or_error);
1313 /* Ignore non-functions, but propagate the ambiguity list. */
1314 if (!error_operand_p (rval)
1315 && (rval && !BASELINK_P (rval)))
1316 return NULL_TREE;
1318 return rval;
1321 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1322 corresponding to "operator TYPE ()", or -1 if there is no such
1323 operator. Only CLASS_TYPE itself is searched; this routine does
1324 not scan the base classes of CLASS_TYPE. */
1326 static int
1327 lookup_conversion_operator (tree class_type, tree type)
1329 int tpl_slot = -1;
1331 if (TYPE_HAS_CONVERSION (class_type))
1333 int i;
1334 tree fn;
1335 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1337 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1338 vec_safe_iterate (methods, i, &fn); ++i)
1340 /* All the conversion operators come near the beginning of
1341 the class. Therefore, if FN is not a conversion
1342 operator, there is no matching conversion operator in
1343 CLASS_TYPE. */
1344 fn = OVL_CURRENT (fn);
1345 if (!DECL_CONV_FN_P (fn))
1346 break;
1348 if (TREE_CODE (fn) == TEMPLATE_DECL)
1349 /* All the templated conversion functions are on the same
1350 slot, so remember it. */
1351 tpl_slot = i;
1352 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1353 return i;
1357 return tpl_slot;
1360 /* TYPE is a class type. Return the index of the fields within
1361 the method vector with name NAME, or -1 if no such field exists.
1362 Does not lazily declare implicitly-declared member functions. */
1364 static int
1365 lookup_fnfields_idx_nolazy (tree type, tree name)
1367 vec<tree, va_gc> *method_vec;
1368 tree fn;
1369 tree tmp;
1370 size_t i;
1372 if (!CLASS_TYPE_P (type))
1373 return -1;
1375 method_vec = CLASSTYPE_METHOD_VEC (type);
1376 if (!method_vec)
1377 return -1;
1379 if (GATHER_STATISTICS)
1380 n_calls_lookup_fnfields_1++;
1382 /* Constructors are first... */
1383 if (name == ctor_identifier)
1385 fn = CLASSTYPE_CONSTRUCTORS (type);
1386 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1388 /* and destructors are second. */
1389 if (name == dtor_identifier)
1391 fn = CLASSTYPE_DESTRUCTORS (type);
1392 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1394 if (IDENTIFIER_TYPENAME_P (name))
1395 return lookup_conversion_operator (type, TREE_TYPE (name));
1397 /* Skip the conversion operators. */
1398 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1399 vec_safe_iterate (method_vec, i, &fn);
1400 ++i)
1401 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1402 break;
1404 /* If the type is complete, use binary search. */
1405 if (COMPLETE_TYPE_P (type))
1407 int lo;
1408 int hi;
1410 lo = i;
1411 hi = method_vec->length ();
1412 while (lo < hi)
1414 i = (lo + hi) / 2;
1416 if (GATHER_STATISTICS)
1417 n_outer_fields_searched++;
1419 tmp = (*method_vec)[i];
1420 tmp = DECL_NAME (OVL_CURRENT (tmp));
1421 if (tmp > name)
1422 hi = i;
1423 else if (tmp < name)
1424 lo = i + 1;
1425 else
1426 return i;
1429 else
1430 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1432 if (GATHER_STATISTICS)
1433 n_outer_fields_searched++;
1434 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1435 return i;
1438 return -1;
1441 /* TYPE is a class type. Return the index of the fields within
1442 the method vector with name NAME, or -1 if no such field exists. */
1445 lookup_fnfields_1 (tree type, tree name)
1447 if (!CLASS_TYPE_P (type))
1448 return -1;
1450 if (COMPLETE_TYPE_P (type))
1452 if ((name == ctor_identifier
1453 || name == base_ctor_identifier
1454 || name == complete_ctor_identifier))
1456 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1457 lazily_declare_fn (sfk_constructor, type);
1458 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1459 lazily_declare_fn (sfk_copy_constructor, type);
1460 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1461 lazily_declare_fn (sfk_move_constructor, type);
1463 else if (name == ansi_assopname (NOP_EXPR))
1465 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1466 lazily_declare_fn (sfk_copy_assignment, type);
1467 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1468 lazily_declare_fn (sfk_move_assignment, type);
1470 else if ((name == dtor_identifier
1471 || name == base_dtor_identifier
1472 || name == complete_dtor_identifier
1473 || name == deleting_dtor_identifier)
1474 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1475 lazily_declare_fn (sfk_destructor, type);
1478 return lookup_fnfields_idx_nolazy (type, name);
1481 /* TYPE is a class type. Return the field within the method vector with
1482 name NAME, or NULL_TREE if no such field exists. */
1484 tree
1485 lookup_fnfields_slot (tree type, tree name)
1487 int ix = lookup_fnfields_1 (complete_type (type), name);
1488 if (ix < 0)
1489 return NULL_TREE;
1490 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1493 /* As above, but avoid lazily declaring functions. */
1495 tree
1496 lookup_fnfields_slot_nolazy (tree type, tree name)
1498 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1499 if (ix < 0)
1500 return NULL_TREE;
1501 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1504 /* Like lookup_fnfields_1, except that the name is extracted from
1505 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1508 class_method_index_for_fn (tree class_type, tree function)
1510 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1512 return lookup_fnfields_1 (class_type,
1513 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1514 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1515 DECL_NAME (function));
1519 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1520 the class or namespace used to qualify the name. CONTEXT_CLASS is
1521 the class corresponding to the object in which DECL will be used.
1522 Return a possibly modified version of DECL that takes into account
1523 the CONTEXT_CLASS.
1525 In particular, consider an expression like `B::m' in the context of
1526 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1527 then the most derived class indicated by the BASELINK_BINFO will be
1528 `B', not `D'. This function makes that adjustment. */
1530 tree
1531 adjust_result_of_qualified_name_lookup (tree decl,
1532 tree qualifying_scope,
1533 tree context_class)
1535 if (context_class && context_class != error_mark_node
1536 && CLASS_TYPE_P (context_class)
1537 && CLASS_TYPE_P (qualifying_scope)
1538 && DERIVED_FROM_P (qualifying_scope, context_class)
1539 && BASELINK_P (decl))
1541 tree base;
1543 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1544 Because we do not yet know which function will be chosen by
1545 overload resolution, we cannot yet check either accessibility
1546 or ambiguity -- in either case, the choice of a static member
1547 function might make the usage valid. */
1548 base = lookup_base (context_class, qualifying_scope,
1549 ba_unique, NULL, tf_none);
1550 if (base && base != error_mark_node)
1552 BASELINK_ACCESS_BINFO (decl) = base;
1553 BASELINK_BINFO (decl)
1554 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1555 ba_unique, NULL, tf_none);
1559 if (BASELINK_P (decl))
1560 BASELINK_QUALIFIED_P (decl) = true;
1562 return decl;
1566 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1567 PRE_FN is called in preorder, while POST_FN is called in postorder.
1568 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1569 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1570 that value is immediately returned and the walk is terminated. One
1571 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1572 POST_FN are passed the binfo to examine and the caller's DATA
1573 value. All paths are walked, thus virtual and morally virtual
1574 binfos can be multiply walked. */
1576 tree
1577 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1578 tree (*post_fn) (tree, void *), void *data)
1580 tree rval;
1581 unsigned ix;
1582 tree base_binfo;
1584 /* Call the pre-order walking function. */
1585 if (pre_fn)
1587 rval = pre_fn (binfo, data);
1588 if (rval)
1590 if (rval == dfs_skip_bases)
1591 goto skip_bases;
1592 return rval;
1596 /* Find the next child binfo to walk. */
1597 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1599 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1600 if (rval)
1601 return rval;
1604 skip_bases:
1605 /* Call the post-order walking function. */
1606 if (post_fn)
1608 rval = post_fn (binfo, data);
1609 gcc_assert (rval != dfs_skip_bases);
1610 return rval;
1613 return NULL_TREE;
1616 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1617 that binfos are walked at most once. */
1619 static tree
1620 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1621 tree (*post_fn) (tree, void *), void *data)
1623 tree rval;
1624 unsigned ix;
1625 tree base_binfo;
1627 /* Call the pre-order walking function. */
1628 if (pre_fn)
1630 rval = pre_fn (binfo, data);
1631 if (rval)
1633 if (rval == dfs_skip_bases)
1634 goto skip_bases;
1636 return rval;
1640 /* Find the next child binfo to walk. */
1641 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1643 if (BINFO_VIRTUAL_P (base_binfo))
1645 if (BINFO_MARKED (base_binfo))
1646 continue;
1647 BINFO_MARKED (base_binfo) = 1;
1650 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1651 if (rval)
1652 return rval;
1655 skip_bases:
1656 /* Call the post-order walking function. */
1657 if (post_fn)
1659 rval = post_fn (binfo, data);
1660 gcc_assert (rval != dfs_skip_bases);
1661 return rval;
1664 return NULL_TREE;
1667 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1668 BINFO. */
1670 static void
1671 dfs_unmark_r (tree binfo)
1673 unsigned ix;
1674 tree base_binfo;
1676 /* Process the basetypes. */
1677 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1679 if (BINFO_VIRTUAL_P (base_binfo))
1681 if (!BINFO_MARKED (base_binfo))
1682 continue;
1683 BINFO_MARKED (base_binfo) = 0;
1685 /* Only walk, if it can contain more virtual bases. */
1686 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1687 dfs_unmark_r (base_binfo);
1691 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1692 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1693 For diamond shaped hierarchies we must mark the virtual bases, to
1694 avoid multiple walks. */
1696 tree
1697 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1698 tree (*post_fn) (tree, void *), void *data)
1700 static int active = 0; /* We must not be called recursively. */
1701 tree rval;
1703 gcc_assert (pre_fn || post_fn);
1704 gcc_assert (!active);
1705 active++;
1707 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1708 /* We are not diamond shaped, and therefore cannot encounter the
1709 same binfo twice. */
1710 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1711 else
1713 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1714 if (!BINFO_INHERITANCE_CHAIN (binfo))
1716 /* We are at the top of the hierarchy, and can use the
1717 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1718 bases. */
1719 vec<tree, va_gc> *vbases;
1720 unsigned ix;
1721 tree base_binfo;
1723 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1724 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1725 BINFO_MARKED (base_binfo) = 0;
1727 else
1728 dfs_unmark_r (binfo);
1731 active--;
1733 return rval;
1736 /* Worker function for dfs_walk_once_accessible. Behaves like
1737 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1738 access given by the current context should be considered, (b) ONCE
1739 indicates whether bases should be marked during traversal. */
1741 static tree
1742 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1743 tree (*pre_fn) (tree, void *),
1744 tree (*post_fn) (tree, void *), void *data)
1746 tree rval = NULL_TREE;
1747 unsigned ix;
1748 tree base_binfo;
1750 /* Call the pre-order walking function. */
1751 if (pre_fn)
1753 rval = pre_fn (binfo, data);
1754 if (rval)
1756 if (rval == dfs_skip_bases)
1757 goto skip_bases;
1759 return rval;
1763 /* Find the next child binfo to walk. */
1764 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1766 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1768 if (mark && BINFO_MARKED (base_binfo))
1769 continue;
1771 /* If the base is inherited via private or protected
1772 inheritance, then we can't see it, unless we are a friend of
1773 the current binfo. */
1774 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1776 tree scope;
1777 if (!friends_p)
1778 continue;
1779 scope = current_scope ();
1780 if (!scope
1781 || TREE_CODE (scope) == NAMESPACE_DECL
1782 || !is_friend (BINFO_TYPE (binfo), scope))
1783 continue;
1786 if (mark)
1787 BINFO_MARKED (base_binfo) = 1;
1789 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1790 pre_fn, post_fn, data);
1791 if (rval)
1792 return rval;
1795 skip_bases:
1796 /* Call the post-order walking function. */
1797 if (post_fn)
1799 rval = post_fn (binfo, data);
1800 gcc_assert (rval != dfs_skip_bases);
1801 return rval;
1804 return NULL_TREE;
1807 /* Like dfs_walk_once except that only accessible bases are walked.
1808 FRIENDS_P indicates whether friendship of the local context
1809 should be considered when determining accessibility. */
1811 static tree
1812 dfs_walk_once_accessible (tree binfo, bool friends_p,
1813 tree (*pre_fn) (tree, void *),
1814 tree (*post_fn) (tree, void *), void *data)
1816 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1817 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1818 pre_fn, post_fn, data);
1820 if (diamond_shaped)
1822 if (!BINFO_INHERITANCE_CHAIN (binfo))
1824 /* We are at the top of the hierarchy, and can use the
1825 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1826 bases. */
1827 vec<tree, va_gc> *vbases;
1828 unsigned ix;
1829 tree base_binfo;
1831 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1832 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1833 BINFO_MARKED (base_binfo) = 0;
1835 else
1836 dfs_unmark_r (binfo);
1838 return rval;
1841 /* Check that virtual overrider OVERRIDER is acceptable for base function
1842 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1844 static int
1845 check_final_overrider (tree overrider, tree basefn)
1847 tree over_type = TREE_TYPE (overrider);
1848 tree base_type = TREE_TYPE (basefn);
1849 tree over_return = fndecl_declared_return_type (overrider);
1850 tree base_return = fndecl_declared_return_type (basefn);
1851 tree over_throw, base_throw;
1853 int fail = 0;
1855 if (DECL_INVALID_OVERRIDER_P (overrider))
1856 return 0;
1858 if (same_type_p (base_return, over_return))
1859 /* OK */;
1860 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1861 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1862 && POINTER_TYPE_P (base_return)))
1864 /* Potentially covariant. */
1865 unsigned base_quals, over_quals;
1867 fail = !POINTER_TYPE_P (base_return);
1868 if (!fail)
1870 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1872 base_return = TREE_TYPE (base_return);
1873 over_return = TREE_TYPE (over_return);
1875 base_quals = cp_type_quals (base_return);
1876 over_quals = cp_type_quals (over_return);
1878 if ((base_quals & over_quals) != over_quals)
1879 fail = 1;
1881 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1883 /* Strictly speaking, the standard requires the return type to be
1884 complete even if it only differs in cv-quals, but that seems
1885 like a bug in the wording. */
1886 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1887 over_return))
1889 tree binfo = lookup_base (over_return, base_return,
1890 ba_check, NULL, tf_none);
1892 if (!binfo || binfo == error_mark_node)
1893 fail = 1;
1896 else if (can_convert_standard (TREE_TYPE (base_type),
1897 TREE_TYPE (over_type),
1898 tf_warning_or_error))
1899 /* GNU extension, allow trivial pointer conversions such as
1900 converting to void *, or qualification conversion. */
1902 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1903 "invalid covariant return type for %q#D", overrider))
1904 inform (DECL_SOURCE_LOCATION (basefn),
1905 " overriding %q+#D", basefn);
1907 else
1908 fail = 2;
1910 else
1911 fail = 2;
1912 if (!fail)
1913 /* OK */;
1914 else
1916 if (fail == 1)
1918 error ("invalid covariant return type for %q+#D", overrider);
1919 error (" overriding %q+#D", basefn);
1921 else
1923 error ("conflicting return type specified for %q+#D", overrider);
1924 error (" overriding %q+#D", basefn);
1926 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1927 return 0;
1930 /* Check throw specifier is at least as strict. */
1931 maybe_instantiate_noexcept (basefn);
1932 maybe_instantiate_noexcept (overrider);
1933 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1934 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1936 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1938 error ("looser throw specifier for %q+#F", overrider);
1939 error (" overriding %q+#F", basefn);
1940 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1941 return 0;
1944 /* Check for conflicting type attributes. */
1945 if (!comp_type_attributes (over_type, base_type))
1947 error ("conflicting type attributes specified for %q+#D", overrider);
1948 error (" overriding %q+#D", basefn);
1949 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1950 return 0;
1953 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1955 if (DECL_DELETED_FN (overrider))
1957 error ("deleted function %q+D", overrider);
1958 error ("overriding non-deleted function %q+D", basefn);
1959 maybe_explain_implicit_delete (overrider);
1961 else
1963 error ("non-deleted function %q+D", overrider);
1964 error ("overriding deleted function %q+D", basefn);
1966 return 0;
1968 if (DECL_FINAL_P (basefn))
1970 error ("virtual function %q+D", overrider);
1971 error ("overriding final function %q+D", basefn);
1972 return 0;
1974 return 1;
1977 /* Given a class TYPE, and a function decl FNDECL, look for
1978 virtual functions in TYPE's hierarchy which FNDECL overrides.
1979 We do not look in TYPE itself, only its bases.
1981 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1982 find that it overrides anything.
1984 We check that every function which is overridden, is correctly
1985 overridden. */
1988 look_for_overrides (tree type, tree fndecl)
1990 tree binfo = TYPE_BINFO (type);
1991 tree base_binfo;
1992 int ix;
1993 int found = 0;
1995 /* A constructor for a class T does not override a function T
1996 in a base class. */
1997 if (DECL_CONSTRUCTOR_P (fndecl))
1998 return 0;
2000 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2002 tree basetype = BINFO_TYPE (base_binfo);
2004 if (TYPE_POLYMORPHIC_P (basetype))
2005 found += look_for_overrides_r (basetype, fndecl);
2007 return found;
2010 /* Look in TYPE for virtual functions with the same signature as
2011 FNDECL. */
2013 tree
2014 look_for_overrides_here (tree type, tree fndecl)
2016 int ix;
2018 /* If there are no methods in TYPE (meaning that only implicitly
2019 declared methods will ever be provided for TYPE), then there are
2020 no virtual functions. */
2021 if (!CLASSTYPE_METHOD_VEC (type))
2022 return NULL_TREE;
2024 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2025 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2026 else
2027 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2028 if (ix >= 0)
2030 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
2032 for (; fns; fns = OVL_NEXT (fns))
2034 tree fn = OVL_CURRENT (fns);
2036 if (!DECL_VIRTUAL_P (fn))
2037 /* Not a virtual. */;
2038 else if (DECL_CONTEXT (fn) != type)
2039 /* Introduced with a using declaration. */;
2040 else if (DECL_STATIC_FUNCTION_P (fndecl))
2042 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2043 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2044 if (compparms (TREE_CHAIN (btypes), dtypes))
2045 return fn;
2047 else if (same_signature_p (fndecl, fn))
2048 return fn;
2051 return NULL_TREE;
2054 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2055 TYPE itself and its bases. */
2057 static int
2058 look_for_overrides_r (tree type, tree fndecl)
2060 tree fn = look_for_overrides_here (type, fndecl);
2061 if (fn)
2063 if (DECL_STATIC_FUNCTION_P (fndecl))
2065 /* A static member function cannot match an inherited
2066 virtual member function. */
2067 error ("%q+#D cannot be declared", fndecl);
2068 error (" since %q+#D declared in base class", fn);
2070 else
2072 /* It's definitely virtual, even if not explicitly set. */
2073 DECL_VIRTUAL_P (fndecl) = 1;
2074 check_final_overrider (fndecl, fn);
2076 return 1;
2079 /* We failed to find one declared in this class. Look in its bases. */
2080 return look_for_overrides (type, fndecl);
2083 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2085 static tree
2086 dfs_get_pure_virtuals (tree binfo, void *data)
2088 tree type = (tree) data;
2090 /* We're not interested in primary base classes; the derived class
2091 of which they are a primary base will contain the information we
2092 need. */
2093 if (!BINFO_PRIMARY_P (binfo))
2095 tree virtuals;
2097 for (virtuals = BINFO_VIRTUALS (binfo);
2098 virtuals;
2099 virtuals = TREE_CHAIN (virtuals))
2100 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2101 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2104 return NULL_TREE;
2107 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2109 void
2110 get_pure_virtuals (tree type)
2112 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2113 is going to be overridden. */
2114 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2115 /* Now, run through all the bases which are not primary bases, and
2116 collect the pure virtual functions. We look at the vtable in
2117 each class to determine what pure virtual functions are present.
2118 (A primary base is not interesting because the derived class of
2119 which it is a primary base will contain vtable entries for the
2120 pure virtuals in the base class. */
2121 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2124 /* Debug info for C++ classes can get very large; try to avoid
2125 emitting it everywhere.
2127 Note that this optimization wins even when the target supports
2128 BINCL (if only slightly), and reduces the amount of work for the
2129 linker. */
2131 void
2132 maybe_suppress_debug_info (tree t)
2134 if (write_symbols == NO_DEBUG)
2135 return;
2137 /* We might have set this earlier in cp_finish_decl. */
2138 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2140 /* Always emit the information for each class every time. */
2141 if (flag_emit_class_debug_always)
2142 return;
2144 /* If we already know how we're handling this class, handle debug info
2145 the same way. */
2146 if (CLASSTYPE_INTERFACE_KNOWN (t))
2148 if (CLASSTYPE_INTERFACE_ONLY (t))
2149 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2150 /* else don't set it. */
2152 /* If the class has a vtable, write out the debug info along with
2153 the vtable. */
2154 else if (TYPE_CONTAINS_VPTR_P (t))
2155 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2157 /* Otherwise, just emit the debug info normally. */
2160 /* Note that we want debugging information for a base class of a class
2161 whose vtable is being emitted. Normally, this would happen because
2162 calling the constructor for a derived class implies calling the
2163 constructors for all bases, which involve initializing the
2164 appropriate vptr with the vtable for the base class; but in the
2165 presence of optimization, this initialization may be optimized
2166 away, so we tell finish_vtable_vardecl that we want the debugging
2167 information anyway. */
2169 static tree
2170 dfs_debug_mark (tree binfo, void * /*data*/)
2172 tree t = BINFO_TYPE (binfo);
2174 if (CLASSTYPE_DEBUG_REQUESTED (t))
2175 return dfs_skip_bases;
2177 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2179 return NULL_TREE;
2182 /* Write out the debugging information for TYPE, whose vtable is being
2183 emitted. Also walk through our bases and note that we want to
2184 write out information for them. This avoids the problem of not
2185 writing any debug info for intermediate basetypes whose
2186 constructors, and thus the references to their vtables, and thus
2187 the vtables themselves, were optimized away. */
2189 void
2190 note_debug_info_needed (tree type)
2192 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2194 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2195 rest_of_type_compilation (type, toplevel_bindings_p ());
2198 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2201 void
2202 print_search_statistics (void)
2204 if (! GATHER_STATISTICS)
2206 fprintf (stderr, "no search statistics\n");
2207 return;
2210 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2211 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2212 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2213 n_outer_fields_searched, n_calls_lookup_fnfields);
2214 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2217 void
2218 reinit_search_statistics (void)
2220 n_fields_searched = 0;
2221 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2222 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2223 n_calls_get_base_type = 0;
2224 n_outer_fields_searched = 0;
2225 n_contexts_saved = 0;
2228 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2229 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2230 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2231 bases have been encountered already in the tree walk. PARENT_CONVS
2232 is the list of lists of conversion functions that could hide CONV
2233 and OTHER_CONVS is the list of lists of conversion functions that
2234 could hide or be hidden by CONV, should virtualness be involved in
2235 the hierarchy. Merely checking the conversion op's name is not
2236 enough because two conversion operators to the same type can have
2237 different names. Return nonzero if we are visible. */
2239 static int
2240 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2241 tree to_type, tree parent_convs, tree other_convs)
2243 tree level, probe;
2245 /* See if we are hidden by a parent conversion. */
2246 for (level = parent_convs; level; level = TREE_CHAIN (level))
2247 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2248 if (same_type_p (to_type, TREE_TYPE (probe)))
2249 return 0;
2251 if (virtual_depth || virtualness)
2253 /* In a virtual hierarchy, we could be hidden, or could hide a
2254 conversion function on the other_convs list. */
2255 for (level = other_convs; level; level = TREE_CHAIN (level))
2257 int we_hide_them;
2258 int they_hide_us;
2259 tree *prev, other;
2261 if (!(virtual_depth || TREE_STATIC (level)))
2262 /* Neither is morally virtual, so cannot hide each other. */
2263 continue;
2265 if (!TREE_VALUE (level))
2266 /* They evaporated away already. */
2267 continue;
2269 they_hide_us = (virtual_depth
2270 && original_binfo (binfo, TREE_PURPOSE (level)));
2271 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2272 && original_binfo (TREE_PURPOSE (level), binfo));
2274 if (!(we_hide_them || they_hide_us))
2275 /* Neither is within the other, so no hiding can occur. */
2276 continue;
2278 for (prev = &TREE_VALUE (level), other = *prev; other;)
2280 if (same_type_p (to_type, TREE_TYPE (other)))
2282 if (they_hide_us)
2283 /* We are hidden. */
2284 return 0;
2286 if (we_hide_them)
2288 /* We hide the other one. */
2289 other = TREE_CHAIN (other);
2290 *prev = other;
2291 continue;
2294 prev = &TREE_CHAIN (other);
2295 other = *prev;
2299 return 1;
2302 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2303 of conversion functions, the first slot will be for the current
2304 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2305 of conversion functions from children of the current binfo,
2306 concatenated with conversions from elsewhere in the hierarchy --
2307 that list begins with OTHER_CONVS. Return a single list of lists
2308 containing only conversions from the current binfo and its
2309 children. */
2311 static tree
2312 split_conversions (tree my_convs, tree parent_convs,
2313 tree child_convs, tree other_convs)
2315 tree t;
2316 tree prev;
2318 /* Remove the original other_convs portion from child_convs. */
2319 for (prev = NULL, t = child_convs;
2320 t != other_convs; prev = t, t = TREE_CHAIN (t))
2321 continue;
2323 if (prev)
2324 TREE_CHAIN (prev) = NULL_TREE;
2325 else
2326 child_convs = NULL_TREE;
2328 /* Attach the child convs to any we had at this level. */
2329 if (my_convs)
2331 my_convs = parent_convs;
2332 TREE_CHAIN (my_convs) = child_convs;
2334 else
2335 my_convs = child_convs;
2337 return my_convs;
2340 /* Worker for lookup_conversions. Lookup conversion functions in
2341 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2342 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2343 encountered virtual bases already in the tree walk. PARENT_CONVS &
2344 PARENT_TPL_CONVS are lists of list of conversions within parent
2345 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2346 elsewhere in the tree. Return the conversions found within this
2347 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2348 encountered virtualness. We keep template and non-template
2349 conversions separate, to avoid unnecessary type comparisons.
2351 The located conversion functions are held in lists of lists. The
2352 TREE_VALUE of the outer list is the list of conversion functions
2353 found in a particular binfo. The TREE_PURPOSE of both the outer
2354 and inner lists is the binfo at which those conversions were
2355 found. TREE_STATIC is set for those lists within of morally
2356 virtual binfos. The TREE_VALUE of the inner list is the conversion
2357 function or overload itself. The TREE_TYPE of each inner list node
2358 is the converted-to type. */
2360 static int
2361 lookup_conversions_r (tree binfo,
2362 int virtual_depth, int virtualness,
2363 tree parent_convs, tree parent_tpl_convs,
2364 tree other_convs, tree other_tpl_convs,
2365 tree *convs, tree *tpl_convs)
2367 int my_virtualness = 0;
2368 tree my_convs = NULL_TREE;
2369 tree my_tpl_convs = NULL_TREE;
2370 tree child_convs = NULL_TREE;
2371 tree child_tpl_convs = NULL_TREE;
2372 unsigned i;
2373 tree base_binfo;
2374 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2375 tree conv;
2377 /* If we have no conversion operators, then don't look. */
2378 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2380 *convs = *tpl_convs = NULL_TREE;
2382 return 0;
2385 if (BINFO_VIRTUAL_P (binfo))
2386 virtual_depth++;
2388 /* First, locate the unhidden ones at this level. */
2389 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2390 vec_safe_iterate (method_vec, i, &conv);
2391 ++i)
2393 tree cur = OVL_CURRENT (conv);
2395 if (!DECL_CONV_FN_P (cur))
2396 break;
2398 if (TREE_CODE (cur) == TEMPLATE_DECL)
2400 /* Only template conversions can be overloaded, and we must
2401 flatten them out and check each one individually. */
2402 tree tpls;
2404 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2406 tree tpl = OVL_CURRENT (tpls);
2407 tree type = DECL_CONV_FN_TYPE (tpl);
2409 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2410 type, parent_tpl_convs, other_tpl_convs))
2412 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2413 TREE_TYPE (my_tpl_convs) = type;
2414 if (virtual_depth)
2416 TREE_STATIC (my_tpl_convs) = 1;
2417 my_virtualness = 1;
2422 else
2424 tree name = DECL_NAME (cur);
2426 if (!IDENTIFIER_MARKED (name))
2428 tree type = DECL_CONV_FN_TYPE (cur);
2429 if (type_uses_auto (type))
2431 mark_used (cur);
2432 type = DECL_CONV_FN_TYPE (cur);
2435 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2436 type, parent_convs, other_convs))
2438 my_convs = tree_cons (binfo, conv, my_convs);
2439 TREE_TYPE (my_convs) = type;
2440 if (virtual_depth)
2442 TREE_STATIC (my_convs) = 1;
2443 my_virtualness = 1;
2445 IDENTIFIER_MARKED (name) = 1;
2451 if (my_convs)
2453 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2454 if (virtual_depth)
2455 TREE_STATIC (parent_convs) = 1;
2458 if (my_tpl_convs)
2460 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2461 if (virtual_depth)
2462 TREE_STATIC (parent_tpl_convs) = 1;
2465 child_convs = other_convs;
2466 child_tpl_convs = other_tpl_convs;
2468 /* Now iterate over each base, looking for more conversions. */
2469 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2471 tree base_convs, base_tpl_convs;
2472 unsigned base_virtualness;
2474 base_virtualness = lookup_conversions_r (base_binfo,
2475 virtual_depth, virtualness,
2476 parent_convs, parent_tpl_convs,
2477 child_convs, child_tpl_convs,
2478 &base_convs, &base_tpl_convs);
2479 if (base_virtualness)
2480 my_virtualness = virtualness = 1;
2481 child_convs = chainon (base_convs, child_convs);
2482 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2485 /* Unmark the conversions found at this level */
2486 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2487 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2489 *convs = split_conversions (my_convs, parent_convs,
2490 child_convs, other_convs);
2491 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2492 child_tpl_convs, other_tpl_convs);
2494 return my_virtualness;
2497 /* Return a TREE_LIST containing all the non-hidden user-defined
2498 conversion functions for TYPE (and its base-classes). The
2499 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2500 function. The TREE_PURPOSE is the BINFO from which the conversion
2501 functions in this node were selected. This function is effectively
2502 performing a set of member lookups as lookup_fnfield does, but
2503 using the type being converted to as the unique key, rather than the
2504 field name. */
2506 tree
2507 lookup_conversions (tree type)
2509 tree convs, tpl_convs;
2510 tree list = NULL_TREE;
2512 complete_type (type);
2513 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2514 return NULL_TREE;
2516 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2517 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2518 &convs, &tpl_convs);
2520 /* Flatten the list-of-lists */
2521 for (; convs; convs = TREE_CHAIN (convs))
2523 tree probe, next;
2525 for (probe = TREE_VALUE (convs); probe; probe = next)
2527 next = TREE_CHAIN (probe);
2529 TREE_CHAIN (probe) = list;
2530 list = probe;
2534 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2536 tree probe, next;
2538 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2540 next = TREE_CHAIN (probe);
2542 TREE_CHAIN (probe) = list;
2543 list = probe;
2547 return list;
2550 /* Returns the binfo of the first direct or indirect virtual base derived
2551 from BINFO, or NULL if binfo is not via virtual. */
2553 tree
2554 binfo_from_vbase (tree binfo)
2556 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2558 if (BINFO_VIRTUAL_P (binfo))
2559 return binfo;
2561 return NULL_TREE;
2564 /* Returns the binfo of the first direct or indirect virtual base derived
2565 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2566 via virtual. */
2568 tree
2569 binfo_via_virtual (tree binfo, tree limit)
2571 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2572 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2573 return NULL_TREE;
2575 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2576 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2578 if (BINFO_VIRTUAL_P (binfo))
2579 return binfo;
2581 return NULL_TREE;
2584 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2585 Find the equivalent binfo within whatever graph HERE is located.
2586 This is the inverse of original_binfo. */
2588 tree
2589 copied_binfo (tree binfo, tree here)
2591 tree result = NULL_TREE;
2593 if (BINFO_VIRTUAL_P (binfo))
2595 tree t;
2597 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2598 t = BINFO_INHERITANCE_CHAIN (t))
2599 continue;
2601 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2603 else if (BINFO_INHERITANCE_CHAIN (binfo))
2605 tree cbinfo;
2606 tree base_binfo;
2607 int ix;
2609 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2610 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2611 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2613 result = base_binfo;
2614 break;
2617 else
2619 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2620 result = here;
2623 gcc_assert (result);
2624 return result;
2627 tree
2628 binfo_for_vbase (tree base, tree t)
2630 unsigned ix;
2631 tree binfo;
2632 vec<tree, va_gc> *vbases;
2634 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2635 vec_safe_iterate (vbases, ix, &binfo); ix++)
2636 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2637 return binfo;
2638 return NULL;
2641 /* BINFO is some base binfo of HERE, within some other
2642 hierarchy. Return the equivalent binfo, but in the hierarchy
2643 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2644 is not a base binfo of HERE, returns NULL_TREE. */
2646 tree
2647 original_binfo (tree binfo, tree here)
2649 tree result = NULL;
2651 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2652 result = here;
2653 else if (BINFO_VIRTUAL_P (binfo))
2654 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2655 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2656 : NULL_TREE);
2657 else if (BINFO_INHERITANCE_CHAIN (binfo))
2659 tree base_binfos;
2661 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2662 if (base_binfos)
2664 int ix;
2665 tree base_binfo;
2667 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2668 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2669 BINFO_TYPE (binfo)))
2671 result = base_binfo;
2672 break;
2677 return result;