PR bootstrap/66085
[official-gcc.git] / gcc / cp / search.c
blob0031acdd848a9f1e2f1995c6056a7567cf9ce9ba
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 "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "cp-tree.h"
39 #include "intl.h"
40 #include "flags.h"
41 #include "toplev.h"
42 #include "target.h"
44 static int is_subobject_of_p (tree, tree);
45 static tree dfs_lookup_base (tree, void *);
46 static tree dfs_dcast_hint_pre (tree, void *);
47 static tree dfs_dcast_hint_post (tree, void *);
48 static tree dfs_debug_mark (tree, void *);
49 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
50 tree (*post_fn) (tree, void *), void *data);
51 static void dfs_unmark_r (tree);
52 static int check_hidden_convs (tree, int, int, tree, tree, tree);
53 static tree split_conversions (tree, tree, tree, tree);
54 static int lookup_conversions_r (tree, int, int,
55 tree, tree, tree, tree, tree *, tree *);
56 static int look_for_overrides_r (tree, tree);
57 static tree lookup_field_r (tree, void *);
58 static tree dfs_accessible_post (tree, void *);
59 static tree dfs_walk_once_accessible_r (tree, bool, bool,
60 tree (*pre_fn) (tree, void *),
61 tree (*post_fn) (tree, void *),
62 void *data);
63 static tree dfs_walk_once_accessible (tree, bool,
64 tree (*pre_fn) (tree, void *),
65 tree (*post_fn) (tree, void *),
66 void *data);
67 static tree dfs_access_in_type (tree, void *);
68 static access_kind access_in_type (tree, tree);
69 static int protected_accessible_p (tree, tree, tree);
70 static int friend_accessible_p (tree, tree, tree);
71 static tree dfs_get_pure_virtuals (tree, void *);
74 /* Variables for gathering statistics. */
75 static int n_fields_searched;
76 static int n_calls_lookup_field, n_calls_lookup_field_1;
77 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
78 static int n_calls_get_base_type;
79 static int n_outer_fields_searched;
80 static int n_contexts_saved;
83 /* Data for lookup_base and its workers. */
85 struct lookup_base_data_s
87 tree t; /* type being searched. */
88 tree base; /* The base type we're looking for. */
89 tree binfo; /* Found binfo. */
90 bool via_virtual; /* Found via a virtual path. */
91 bool ambiguous; /* Found multiply ambiguous */
92 bool repeated_base; /* Whether there are repeated bases in the
93 hierarchy. */
94 bool want_any; /* Whether we want any matching binfo. */
97 /* Worker function for lookup_base. See if we've found the desired
98 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
100 static tree
101 dfs_lookup_base (tree binfo, void *data_)
103 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
105 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
107 if (!data->binfo)
109 data->binfo = binfo;
110 data->via_virtual
111 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
113 if (!data->repeated_base)
114 /* If there are no repeated bases, we can stop now. */
115 return binfo;
117 if (data->want_any && !data->via_virtual)
118 /* If this is a non-virtual base, then we can't do
119 better. */
120 return binfo;
122 return dfs_skip_bases;
124 else
126 gcc_assert (binfo != data->binfo);
128 /* We've found more than one matching binfo. */
129 if (!data->want_any)
131 /* This is immediately ambiguous. */
132 data->binfo = NULL_TREE;
133 data->ambiguous = true;
134 return error_mark_node;
137 /* Prefer one via a non-virtual path. */
138 if (!binfo_via_virtual (binfo, data->t))
140 data->binfo = binfo;
141 data->via_virtual = false;
142 return binfo;
145 /* There must be repeated bases, otherwise we'd have stopped
146 on the first base we found. */
147 return dfs_skip_bases;
151 return NULL_TREE;
154 /* Returns true if type BASE is accessible in T. (BASE is known to be
155 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
156 true, consider any special access of the current scope, or access
157 bestowed by friendship. */
159 bool
160 accessible_base_p (tree t, tree base, bool consider_local_p)
162 tree decl;
164 /* [class.access.base]
166 A base class is said to be accessible if an invented public
167 member of the base class is accessible.
169 If BASE is a non-proper base, this condition is trivially
170 true. */
171 if (same_type_p (t, base))
172 return true;
173 /* Rather than inventing a public member, we use the implicit
174 public typedef created in the scope of every class. */
175 decl = TYPE_FIELDS (base);
176 while (!DECL_SELF_REFERENCE_P (decl))
177 decl = DECL_CHAIN (decl);
178 while (ANON_AGGR_TYPE_P (t))
179 t = TYPE_CONTEXT (t);
180 return accessible_p (t, decl, consider_local_p);
183 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
184 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
185 non-NULL, fill with information about what kind of base we
186 discovered.
188 If the base is inaccessible, or ambiguous, then error_mark_node is
189 returned. If the tf_error bit of COMPLAIN is not set, no error
190 is issued. */
192 tree
193 lookup_base (tree t, tree base, base_access access,
194 base_kind *kind_ptr, tsubst_flags_t complain)
196 tree binfo;
197 tree t_binfo;
198 base_kind bk;
200 /* "Nothing" is definitely not derived from Base. */
201 if (t == NULL_TREE)
203 if (kind_ptr)
204 *kind_ptr = bk_not_base;
205 return NULL_TREE;
208 if (t == error_mark_node || base == error_mark_node)
210 if (kind_ptr)
211 *kind_ptr = bk_not_base;
212 return error_mark_node;
214 gcc_assert (TYPE_P (base));
216 if (!TYPE_P (t))
218 t_binfo = t;
219 t = BINFO_TYPE (t);
221 else
223 t = complete_type (TYPE_MAIN_VARIANT (t));
224 t_binfo = TYPE_BINFO (t);
227 base = TYPE_MAIN_VARIANT (base);
229 /* If BASE is incomplete, it can't be a base of T--and instantiating it
230 might cause an error. */
231 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
233 struct lookup_base_data_s data;
235 data.t = t;
236 data.base = base;
237 data.binfo = NULL_TREE;
238 data.ambiguous = data.via_virtual = false;
239 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
240 data.want_any = access == ba_any;
242 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
243 binfo = data.binfo;
245 if (!binfo)
246 bk = data.ambiguous ? bk_ambig : bk_not_base;
247 else if (binfo == t_binfo)
248 bk = bk_same_type;
249 else if (data.via_virtual)
250 bk = bk_via_virtual;
251 else
252 bk = bk_proper_base;
254 else
256 binfo = NULL_TREE;
257 bk = bk_not_base;
260 /* Check that the base is unambiguous and accessible. */
261 if (access != ba_any)
262 switch (bk)
264 case bk_not_base:
265 break;
267 case bk_ambig:
268 if (complain & tf_error)
269 error ("%qT is an ambiguous base of %qT", base, t);
270 binfo = error_mark_node;
271 break;
273 default:
274 if ((access & ba_check_bit)
275 /* If BASE is incomplete, then BASE and TYPE are probably
276 the same, in which case BASE is accessible. If they
277 are not the same, then TYPE is invalid. In that case,
278 there's no need to issue another error here, and
279 there's no implicit typedef to use in the code that
280 follows, so we skip the check. */
281 && COMPLETE_TYPE_P (base)
282 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
284 if (complain & tf_error)
285 error ("%qT is an inaccessible base of %qT", base, t);
286 binfo = error_mark_node;
287 bk = bk_inaccessible;
289 break;
292 if (kind_ptr)
293 *kind_ptr = bk;
295 return binfo;
298 /* Data for dcast_base_hint walker. */
300 struct dcast_data_s
302 tree subtype; /* The base type we're looking for. */
303 int virt_depth; /* Number of virtual bases encountered from most
304 derived. */
305 tree offset; /* Best hint offset discovered so far. */
306 bool repeated_base; /* Whether there are repeated bases in the
307 hierarchy. */
310 /* Worker for dcast_base_hint. Search for the base type being cast
311 from. */
313 static tree
314 dfs_dcast_hint_pre (tree binfo, void *data_)
316 struct dcast_data_s *data = (struct dcast_data_s *) data_;
318 if (BINFO_VIRTUAL_P (binfo))
319 data->virt_depth++;
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
323 if (data->virt_depth)
325 data->offset = ssize_int (-1);
326 return data->offset;
328 if (data->offset)
329 data->offset = ssize_int (-3);
330 else
331 data->offset = BINFO_OFFSET (binfo);
333 return data->repeated_base ? dfs_skip_bases : data->offset;
336 return NULL_TREE;
339 /* Worker for dcast_base_hint. Track the virtual depth. */
341 static tree
342 dfs_dcast_hint_post (tree binfo, void *data_)
344 struct dcast_data_s *data = (struct dcast_data_s *) data_;
346 if (BINFO_VIRTUAL_P (binfo))
347 data->virt_depth--;
349 return NULL_TREE;
352 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
353 started from is related to the required TARGET type, in order to optimize
354 the inheritance graph search. This information is independent of the
355 current context, and ignores private paths, hence get_base_distance is
356 inappropriate. Return a TREE specifying the base offset, BOFF.
357 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
358 and there are no public virtual SUBTYPE bases.
359 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
360 BOFF == -2, SUBTYPE is not a public base.
361 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
363 tree
364 dcast_base_hint (tree subtype, tree target)
366 struct dcast_data_s data;
368 data.subtype = subtype;
369 data.virt_depth = 0;
370 data.offset = NULL_TREE;
371 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
373 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
374 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
375 return data.offset ? data.offset : ssize_int (-2);
378 /* Search for a member with name NAME in a multiple inheritance
379 lattice specified by TYPE. If it does not exist, return NULL_TREE.
380 If the member is ambiguously referenced, return `error_mark_node'.
381 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
382 true, type declarations are preferred. */
384 /* Do a 1-level search for NAME as a member of TYPE. The caller must
385 figure out whether it can access this field. (Since it is only one
386 level, this is reasonable.) */
388 tree
389 lookup_field_1 (tree type, tree name, bool want_type)
391 tree field;
393 gcc_assert (identifier_p (name));
395 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
396 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
397 || TREE_CODE (type) == TYPENAME_TYPE)
398 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
399 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
400 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
401 the code often worked even when we treated the index as a list
402 of fields!)
403 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
404 return NULL_TREE;
406 if (CLASSTYPE_SORTED_FIELDS (type))
408 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
409 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
410 int i;
412 while (lo < hi)
414 i = (lo + hi) / 2;
416 if (GATHER_STATISTICS)
417 n_fields_searched++;
419 if (DECL_NAME (fields[i]) > name)
420 hi = i;
421 else if (DECL_NAME (fields[i]) < name)
422 lo = i + 1;
423 else
425 field = NULL_TREE;
427 /* We might have a nested class and a field with the
428 same name; we sorted them appropriately via
429 field_decl_cmp, so just look for the first or last
430 field with this name. */
431 if (want_type)
434 field = fields[i--];
435 while (i >= lo && DECL_NAME (fields[i]) == name);
436 if (!DECL_DECLARES_TYPE_P (field))
437 field = NULL_TREE;
439 else
442 field = fields[i++];
443 while (i < hi && DECL_NAME (fields[i]) == name);
446 if (field)
448 field = strip_using_decl (field);
449 if (is_overloaded_fn (field))
450 field = NULL_TREE;
453 return field;
456 return NULL_TREE;
459 field = TYPE_FIELDS (type);
461 if (GATHER_STATISTICS)
462 n_calls_lookup_field_1++;
464 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
466 tree decl = field;
468 if (GATHER_STATISTICS)
469 n_fields_searched++;
471 gcc_assert (DECL_P (field));
472 if (DECL_NAME (field) == NULL_TREE
473 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
475 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
476 if (temp)
477 return temp;
480 if (TREE_CODE (decl) == USING_DECL
481 && DECL_NAME (decl) == name)
483 decl = strip_using_decl (decl);
484 if (is_overloaded_fn (decl))
485 continue;
488 if (DECL_NAME (decl) == name
489 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
490 return decl;
492 /* Not found. */
493 if (name == vptr_identifier)
495 /* Give the user what s/he thinks s/he wants. */
496 if (TYPE_POLYMORPHIC_P (type))
497 return TYPE_VFIELD (type);
499 return NULL_TREE;
502 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
503 NAMESPACE_DECL corresponding to the innermost non-block scope. */
505 tree
506 current_scope (void)
508 /* There are a number of cases we need to be aware of here:
509 current_class_type current_function_decl
510 global NULL NULL
511 fn-local NULL SET
512 class-local SET NULL
513 class->fn SET SET
514 fn->class SET SET
516 Those last two make life interesting. If we're in a function which is
517 itself inside a class, we need decls to go into the fn's decls (our
518 second case below). But if we're in a class and the class itself is
519 inside a function, we need decls to go into the decls for the class. To
520 achieve this last goal, we must see if, when both current_class_ptr and
521 current_function_decl are set, the class was declared inside that
522 function. If so, we know to put the decls into the class's scope. */
523 if (current_function_decl && current_class_type
524 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
525 && same_type_p (DECL_CONTEXT (current_function_decl),
526 current_class_type))
527 || (DECL_FRIEND_CONTEXT (current_function_decl)
528 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
529 current_class_type))))
530 return current_function_decl;
531 if (current_class_type)
532 return current_class_type;
533 if (current_function_decl)
534 return current_function_decl;
535 return current_namespace;
538 /* Returns nonzero if we are currently in a function scope. Note
539 that this function returns zero if we are within a local class, but
540 not within a member function body of the local class. */
543 at_function_scope_p (void)
545 tree cs = current_scope ();
546 /* Also check cfun to make sure that we're really compiling
547 this function (as opposed to having set current_function_decl
548 for access checking or some such). */
549 return (cs && TREE_CODE (cs) == FUNCTION_DECL
550 && cfun && cfun->decl == current_function_decl);
553 /* Returns true if the innermost active scope is a class scope. */
555 bool
556 at_class_scope_p (void)
558 tree cs = current_scope ();
559 return cs && TYPE_P (cs);
562 /* Returns true if the innermost active scope is a namespace scope. */
564 bool
565 at_namespace_scope_p (void)
567 tree cs = current_scope ();
568 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
571 /* Return the scope of DECL, as appropriate when doing name-lookup. */
573 tree
574 context_for_name_lookup (tree decl)
576 /* [class.union]
578 For the purposes of name lookup, after the anonymous union
579 definition, the members of the anonymous union are considered to
580 have been defined in the scope in which the anonymous union is
581 declared. */
582 tree context = DECL_CONTEXT (decl);
584 while (context && TYPE_P (context)
585 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
586 context = TYPE_CONTEXT (context);
587 if (!context)
588 context = global_namespace;
590 return context;
593 /* The accessibility routines use BINFO_ACCESS for scratch space
594 during the computation of the accessibility of some declaration. */
596 #define BINFO_ACCESS(NODE) \
597 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
599 /* Set the access associated with NODE to ACCESS. */
601 #define SET_BINFO_ACCESS(NODE, ACCESS) \
602 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
603 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
605 /* Called from access_in_type via dfs_walk. Calculate the access to
606 DATA (which is really a DECL) in BINFO. */
608 static tree
609 dfs_access_in_type (tree binfo, void *data)
611 tree decl = (tree) data;
612 tree type = BINFO_TYPE (binfo);
613 access_kind access = ak_none;
615 if (context_for_name_lookup (decl) == type)
617 /* If we have descended to the scope of DECL, just note the
618 appropriate access. */
619 if (TREE_PRIVATE (decl))
620 access = ak_private;
621 else if (TREE_PROTECTED (decl))
622 access = ak_protected;
623 else
624 access = ak_public;
626 else
628 /* First, check for an access-declaration that gives us more
629 access to the DECL. */
630 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
632 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
634 if (decl_access)
636 decl_access = TREE_VALUE (decl_access);
638 if (decl_access == access_public_node)
639 access = ak_public;
640 else if (decl_access == access_protected_node)
641 access = ak_protected;
642 else if (decl_access == access_private_node)
643 access = ak_private;
644 else
645 gcc_unreachable ();
649 if (!access)
651 int i;
652 tree base_binfo;
653 vec<tree, va_gc> *accesses;
655 /* Otherwise, scan our baseclasses, and pick the most favorable
656 access. */
657 accesses = BINFO_BASE_ACCESSES (binfo);
658 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
660 tree base_access = (*accesses)[i];
661 access_kind base_access_now = BINFO_ACCESS (base_binfo);
663 if (base_access_now == ak_none || base_access_now == ak_private)
664 /* If it was not accessible in the base, or only
665 accessible as a private member, we can't access it
666 all. */
667 base_access_now = ak_none;
668 else if (base_access == access_protected_node)
669 /* Public and protected members in the base become
670 protected here. */
671 base_access_now = ak_protected;
672 else if (base_access == access_private_node)
673 /* Public and protected members in the base become
674 private here. */
675 base_access_now = ak_private;
677 /* See if the new access, via this base, gives more
678 access than our previous best access. */
679 if (base_access_now != ak_none
680 && (access == ak_none || base_access_now < access))
682 access = base_access_now;
684 /* If the new access is public, we can't do better. */
685 if (access == ak_public)
686 break;
692 /* Note the access to DECL in TYPE. */
693 SET_BINFO_ACCESS (binfo, access);
695 return NULL_TREE;
698 /* Return the access to DECL in TYPE. */
700 static access_kind
701 access_in_type (tree type, tree decl)
703 tree binfo = TYPE_BINFO (type);
705 /* We must take into account
707 [class.paths]
709 If a name can be reached by several paths through a multiple
710 inheritance graph, the access is that of the path that gives
711 most access.
713 The algorithm we use is to make a post-order depth-first traversal
714 of the base-class hierarchy. As we come up the tree, we annotate
715 each node with the most lenient access. */
716 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
718 return BINFO_ACCESS (binfo);
721 /* Returns nonzero if it is OK to access DECL through an object
722 indicated by BINFO in the context of DERIVED. */
724 static int
725 protected_accessible_p (tree decl, tree derived, tree binfo)
727 access_kind access;
729 /* We're checking this clause from [class.access.base]
731 m as a member of N is protected, and the reference occurs in a
732 member or friend of class N, or in a member or friend of a
733 class P derived from N, where m as a member of P is public, private
734 or protected.
736 Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N. */
738 /* If DERIVED isn't derived from N, then it can't be a P. */
739 if (!DERIVED_FROM_P (BINFO_TYPE (binfo), derived))
740 return 0;
742 access = access_in_type (derived, decl);
744 /* If m is inaccessible in DERIVED, then it's not a P. */
745 if (access == ak_none)
746 return 0;
748 /* [class.protected]
750 When a friend or a member function of a derived class references
751 a protected nonstatic member of a base class, an access check
752 applies in addition to those described earlier in clause
753 _class.access_) Except when forming a pointer to member
754 (_expr.unary.op_), the access must be through a pointer to,
755 reference to, or object of the derived class itself (or any class
756 derived from that class) (_expr.ref_). If the access is to form
757 a pointer to member, the nested-name-specifier shall name the
758 derived class (or any class derived from that class). */
759 if (DECL_NONSTATIC_MEMBER_P (decl))
761 /* We can tell through what the reference is occurring by
762 chasing BINFO up to the root. */
763 tree t = binfo;
764 while (BINFO_INHERITANCE_CHAIN (t))
765 t = BINFO_INHERITANCE_CHAIN (t);
767 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
768 return 0;
771 return 1;
774 /* Returns nonzero if SCOPE is a friend of a type which would be able
775 to access DECL through the object indicated by BINFO. */
777 static int
778 friend_accessible_p (tree scope, tree decl, tree binfo)
780 tree befriending_classes;
781 tree t;
783 if (!scope)
784 return 0;
786 if (DECL_DECLARES_FUNCTION_P (scope))
787 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
788 else if (TYPE_P (scope))
789 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
790 else
791 return 0;
793 for (t = befriending_classes; t; t = TREE_CHAIN (t))
794 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
795 return 1;
797 /* Nested classes have the same access as their enclosing types, as
798 per DR 45 (this is a change from the standard). */
799 if (TYPE_P (scope))
800 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
801 if (protected_accessible_p (decl, t, binfo))
802 return 1;
804 if (DECL_DECLARES_FUNCTION_P (scope))
806 /* Perhaps this SCOPE is a member of a class which is a
807 friend. */
808 if (DECL_CLASS_SCOPE_P (scope)
809 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
810 return 1;
812 /* Or an instantiation of something which is a friend. */
813 if (DECL_TEMPLATE_INFO (scope))
815 int ret;
816 /* Increment processing_template_decl to make sure that
817 dependent_type_p works correctly. */
818 ++processing_template_decl;
819 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
820 --processing_template_decl;
821 return ret;
825 return 0;
828 /* Called via dfs_walk_once_accessible from accessible_p */
830 static tree
831 dfs_accessible_post (tree binfo, void * /*data*/)
833 if (BINFO_ACCESS (binfo) != ak_none)
835 tree scope = current_scope ();
836 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
837 && is_friend (BINFO_TYPE (binfo), scope))
838 return binfo;
841 return NULL_TREE;
844 /* Like accessible_p below, but within a template returns true iff DECL is
845 accessible in TYPE to all possible instantiations of the template. */
848 accessible_in_template_p (tree type, tree decl)
850 int save_ptd = processing_template_decl;
851 processing_template_decl = 0;
852 int val = accessible_p (type, decl, false);
853 processing_template_decl = save_ptd;
854 return val;
857 /* DECL is a declaration from a base class of TYPE, which was the
858 class used to name DECL. Return nonzero if, in the current
859 context, DECL is accessible. If TYPE is actually a BINFO node,
860 then we can tell in what context the access is occurring by looking
861 at the most derived class along the path indicated by BINFO. If
862 CONSIDER_LOCAL is true, do consider special access the current
863 scope or friendship thereof we might have. */
866 accessible_p (tree type, tree decl, bool consider_local_p)
868 tree binfo;
869 tree scope;
870 access_kind access;
872 /* Nonzero if it's OK to access DECL if it has protected
873 accessibility in TYPE. */
874 int protected_ok = 0;
876 /* If this declaration is in a block or namespace scope, there's no
877 access control. */
878 if (!TYPE_P (context_for_name_lookup (decl)))
879 return 1;
881 /* There is no need to perform access checks inside a thunk. */
882 scope = current_scope ();
883 if (scope && DECL_THUNK_P (scope))
884 return 1;
886 /* In a template declaration, we cannot be sure whether the
887 particular specialization that is instantiated will be a friend
888 or not. Therefore, all access checks are deferred until
889 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
890 parameter list for a template (because we may see dependent types
891 in default arguments for template parameters), and access
892 checking should be performed in the outermost parameter list. */
893 if (processing_template_decl
894 && (!processing_template_parmlist || processing_template_decl > 1))
895 return 1;
897 if (!TYPE_P (type))
899 binfo = type;
900 type = BINFO_TYPE (type);
902 else
903 binfo = TYPE_BINFO (type);
905 /* [class.access.base]
907 A member m is accessible when named in class N if
909 --m as a member of N is public, or
911 --m as a member of N is private, and the reference occurs in a
912 member or friend of class N, or
914 --m as a member of N is protected, and the reference occurs in a
915 member or friend of class N, or in a member or friend of a
916 class P derived from N, where m as a member of P is private or
917 protected, or
919 --there exists a base class B of N that is accessible at the point
920 of reference, and m is accessible when named in class B.
922 We walk the base class hierarchy, checking these conditions. */
924 if (consider_local_p)
926 /* Figure out where the reference is occurring. Check to see if
927 DECL is private or protected in this scope, since that will
928 determine whether protected access is allowed. */
929 tree ct = current_nonlambda_class_type ();
930 if (ct)
931 protected_ok = protected_accessible_p (decl,
933 binfo);
935 /* Now, loop through the classes of which we are a friend. */
936 if (!protected_ok)
937 protected_ok = friend_accessible_p (scope, decl, binfo);
940 /* Standardize the binfo that access_in_type will use. We don't
941 need to know what path was chosen from this point onwards. */
942 binfo = TYPE_BINFO (type);
944 /* Compute the accessibility of DECL in the class hierarchy
945 dominated by type. */
946 access = access_in_type (type, decl);
947 if (access == ak_public
948 || (access == ak_protected && protected_ok))
949 return 1;
951 if (!consider_local_p)
952 return 0;
954 /* Walk the hierarchy again, looking for a base class that allows
955 access. */
956 return dfs_walk_once_accessible (binfo, /*friends=*/true,
957 NULL, dfs_accessible_post, NULL)
958 != NULL_TREE;
961 struct lookup_field_info {
962 /* The type in which we're looking. */
963 tree type;
964 /* The name of the field for which we're looking. */
965 tree name;
966 /* If non-NULL, the current result of the lookup. */
967 tree rval;
968 /* The path to RVAL. */
969 tree rval_binfo;
970 /* If non-NULL, the lookup was ambiguous, and this is a list of the
971 candidates. */
972 tree ambiguous;
973 /* If nonzero, we are looking for types, not data members. */
974 int want_type;
975 /* If something went wrong, a message indicating what. */
976 const char *errstr;
979 /* Nonzero for a class member means that it is shared between all objects
980 of that class.
982 [class.member.lookup]:If the resulting set of declarations are not all
983 from sub-objects of the same type, or the set has a nonstatic member
984 and includes members from distinct sub-objects, there is an ambiguity
985 and the program is ill-formed.
987 This function checks that T contains no nonstatic members. */
990 shared_member_p (tree t)
992 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
993 || TREE_CODE (t) == CONST_DECL)
994 return 1;
995 if (is_overloaded_fn (t))
997 t = get_fns (t);
998 for (; t; t = OVL_NEXT (t))
1000 tree fn = OVL_CURRENT (t);
1001 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1002 return 0;
1004 return 1;
1006 return 0;
1009 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1010 found as a base class and sub-object of the object denoted by
1011 BINFO. */
1013 static int
1014 is_subobject_of_p (tree parent, tree binfo)
1016 tree probe;
1018 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1020 if (probe == binfo)
1021 return 1;
1022 if (BINFO_VIRTUAL_P (probe))
1023 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1024 != NULL_TREE);
1026 return 0;
1029 /* DATA is really a struct lookup_field_info. Look for a field with
1030 the name indicated there in BINFO. If this function returns a
1031 non-NULL value it is the result of the lookup. Called from
1032 lookup_field via breadth_first_search. */
1034 static tree
1035 lookup_field_r (tree binfo, void *data)
1037 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1038 tree type = BINFO_TYPE (binfo);
1039 tree nval = NULL_TREE;
1041 /* If this is a dependent base, don't look in it. */
1042 if (BINFO_DEPENDENT_BASE_P (binfo))
1043 return NULL_TREE;
1045 /* If this base class is hidden by the best-known value so far, we
1046 don't need to look. */
1047 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1048 && !BINFO_VIRTUAL_P (binfo))
1049 return dfs_skip_bases;
1051 /* First, look for a function. There can't be a function and a data
1052 member with the same name, and if there's a function and a type
1053 with the same name, the type is hidden by the function. */
1054 if (!lfi->want_type)
1055 nval = lookup_fnfields_slot (type, lfi->name);
1057 if (!nval)
1058 /* Look for a data member or type. */
1059 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1061 /* If there is no declaration with the indicated name in this type,
1062 then there's nothing to do. */
1063 if (!nval)
1064 goto done;
1066 /* If we're looking up a type (as with an elaborated type specifier)
1067 we ignore all non-types we find. */
1068 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1070 if (lfi->name == TYPE_IDENTIFIER (type))
1072 /* If the aggregate has no user defined constructors, we allow
1073 it to have fields with the same name as the enclosing type.
1074 If we are looking for that name, find the corresponding
1075 TYPE_DECL. */
1076 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1077 if (DECL_NAME (nval) == lfi->name
1078 && TREE_CODE (nval) == TYPE_DECL)
1079 break;
1081 else
1082 nval = NULL_TREE;
1083 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1085 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1086 lfi->name);
1087 if (e != NULL)
1088 nval = TYPE_MAIN_DECL (e->type);
1089 else
1090 goto done;
1094 /* If the lookup already found a match, and the new value doesn't
1095 hide the old one, we might have an ambiguity. */
1096 if (lfi->rval_binfo
1097 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1100 if (nval == lfi->rval && shared_member_p (nval))
1101 /* The two things are really the same. */
1103 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1104 /* The previous value hides the new one. */
1106 else
1108 /* We have a real ambiguity. We keep a chain of all the
1109 candidates. */
1110 if (!lfi->ambiguous && lfi->rval)
1112 /* This is the first time we noticed an ambiguity. Add
1113 what we previously thought was a reasonable candidate
1114 to the list. */
1115 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1116 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1119 /* Add the new value. */
1120 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1121 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1122 lfi->errstr = G_("request for member %qD is ambiguous");
1125 else
1127 lfi->rval = nval;
1128 lfi->rval_binfo = binfo;
1131 done:
1132 /* Don't look for constructors or destructors in base classes. */
1133 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1134 return dfs_skip_bases;
1135 return NULL_TREE;
1138 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1139 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1140 FUNCTIONS, and OPTYPE respectively. */
1142 tree
1143 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1145 tree baselink;
1147 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1148 || TREE_CODE (functions) == TEMPLATE_DECL
1149 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1150 || TREE_CODE (functions) == OVERLOAD);
1151 gcc_assert (!optype || TYPE_P (optype));
1152 gcc_assert (TREE_TYPE (functions));
1154 baselink = make_node (BASELINK);
1155 TREE_TYPE (baselink) = TREE_TYPE (functions);
1156 BASELINK_BINFO (baselink) = binfo;
1157 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1158 BASELINK_FUNCTIONS (baselink) = functions;
1159 BASELINK_OPTYPE (baselink) = optype;
1161 return baselink;
1164 /* Look for a member named NAME in an inheritance lattice dominated by
1165 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1166 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1167 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1168 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1169 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1170 TREE_VALUEs are the list of ambiguous candidates.
1172 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1174 If nothing can be found return NULL_TREE and do not issue an error. */
1176 tree
1177 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1178 tsubst_flags_t complain)
1180 tree rval, rval_binfo = NULL_TREE;
1181 tree type = NULL_TREE, basetype_path = NULL_TREE;
1182 struct lookup_field_info lfi;
1184 /* rval_binfo is the binfo associated with the found member, note,
1185 this can be set with useful information, even when rval is not
1186 set, because it must deal with ALL members, not just non-function
1187 members. It is used for ambiguity checking and the hidden
1188 checks. Whereas rval is only set if a proper (not hidden)
1189 non-function member is found. */
1191 const char *errstr = 0;
1193 if (name == error_mark_node
1194 || xbasetype == NULL_TREE
1195 || xbasetype == error_mark_node)
1196 return NULL_TREE;
1198 gcc_assert (identifier_p (name));
1200 if (TREE_CODE (xbasetype) == TREE_BINFO)
1202 type = BINFO_TYPE (xbasetype);
1203 basetype_path = xbasetype;
1205 else
1207 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1208 return NULL_TREE;
1209 type = xbasetype;
1210 xbasetype = NULL_TREE;
1213 type = complete_type (type);
1214 if (!basetype_path)
1215 basetype_path = TYPE_BINFO (type);
1217 if (!basetype_path)
1218 return NULL_TREE;
1220 if (GATHER_STATISTICS)
1221 n_calls_lookup_field++;
1223 memset (&lfi, 0, sizeof (lfi));
1224 lfi.type = type;
1225 lfi.name = name;
1226 lfi.want_type = want_type;
1227 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1228 rval = lfi.rval;
1229 rval_binfo = lfi.rval_binfo;
1230 if (rval_binfo)
1231 type = BINFO_TYPE (rval_binfo);
1232 errstr = lfi.errstr;
1234 /* If we are not interested in ambiguities, don't report them;
1235 just return NULL_TREE. */
1236 if (!protect && lfi.ambiguous)
1237 return NULL_TREE;
1239 if (protect == 2)
1241 if (lfi.ambiguous)
1242 return lfi.ambiguous;
1243 else
1244 protect = 0;
1247 /* [class.access]
1249 In the case of overloaded function names, access control is
1250 applied to the function selected by overloaded resolution.
1252 We cannot check here, even if RVAL is only a single non-static
1253 member function, since we do not know what the "this" pointer
1254 will be. For:
1256 class A { protected: void f(); };
1257 class B : public A {
1258 void g(A *p) {
1259 f(); // OK
1260 p->f(); // Not OK.
1264 only the first call to "f" is valid. However, if the function is
1265 static, we can check. */
1266 if (rval && protect
1267 && !really_overloaded_fn (rval))
1269 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1270 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1271 && !perform_or_defer_access_check (basetype_path, decl, decl,
1272 complain))
1273 rval = error_mark_node;
1276 if (errstr && protect)
1278 if (complain & tf_error)
1280 error (errstr, name, type);
1281 if (lfi.ambiguous)
1282 print_candidates (lfi.ambiguous);
1284 rval = error_mark_node;
1287 if (rval && is_overloaded_fn (rval))
1288 rval = build_baselink (rval_binfo, basetype_path, rval,
1289 (IDENTIFIER_TYPENAME_P (name)
1290 ? TREE_TYPE (name): NULL_TREE));
1291 return rval;
1294 /* Like lookup_member, except that if we find a function member we
1295 return NULL_TREE. */
1297 tree
1298 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1300 tree rval = lookup_member (xbasetype, name, protect, want_type,
1301 tf_warning_or_error);
1303 /* Ignore functions, but propagate the ambiguity list. */
1304 if (!error_operand_p (rval)
1305 && (rval && BASELINK_P (rval)))
1306 return NULL_TREE;
1308 return rval;
1311 /* Like lookup_member, except that if we find a non-function member we
1312 return NULL_TREE. */
1314 tree
1315 lookup_fnfields (tree xbasetype, tree name, int protect)
1317 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1318 tf_warning_or_error);
1320 /* Ignore non-functions, but propagate the ambiguity list. */
1321 if (!error_operand_p (rval)
1322 && (rval && !BASELINK_P (rval)))
1323 return NULL_TREE;
1325 return rval;
1328 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1329 corresponding to "operator TYPE ()", or -1 if there is no such
1330 operator. Only CLASS_TYPE itself is searched; this routine does
1331 not scan the base classes of CLASS_TYPE. */
1333 static int
1334 lookup_conversion_operator (tree class_type, tree type)
1336 int tpl_slot = -1;
1338 if (TYPE_HAS_CONVERSION (class_type))
1340 int i;
1341 tree fn;
1342 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1344 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1345 vec_safe_iterate (methods, i, &fn); ++i)
1347 /* All the conversion operators come near the beginning of
1348 the class. Therefore, if FN is not a conversion
1349 operator, there is no matching conversion operator in
1350 CLASS_TYPE. */
1351 fn = OVL_CURRENT (fn);
1352 if (!DECL_CONV_FN_P (fn))
1353 break;
1355 if (TREE_CODE (fn) == TEMPLATE_DECL)
1356 /* All the templated conversion functions are on the same
1357 slot, so remember it. */
1358 tpl_slot = i;
1359 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1360 return i;
1364 return tpl_slot;
1367 /* TYPE is a class type. Return the index of the fields within
1368 the method vector with name NAME, or -1 if no such field exists.
1369 Does not lazily declare implicitly-declared member functions. */
1371 static int
1372 lookup_fnfields_idx_nolazy (tree type, tree name)
1374 vec<tree, va_gc> *method_vec;
1375 tree fn;
1376 tree tmp;
1377 size_t i;
1379 if (!CLASS_TYPE_P (type))
1380 return -1;
1382 method_vec = CLASSTYPE_METHOD_VEC (type);
1383 if (!method_vec)
1384 return -1;
1386 if (GATHER_STATISTICS)
1387 n_calls_lookup_fnfields_1++;
1389 /* Constructors are first... */
1390 if (name == ctor_identifier)
1392 fn = CLASSTYPE_CONSTRUCTORS (type);
1393 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1395 /* and destructors are second. */
1396 if (name == dtor_identifier)
1398 fn = CLASSTYPE_DESTRUCTORS (type);
1399 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1401 if (IDENTIFIER_TYPENAME_P (name))
1402 return lookup_conversion_operator (type, TREE_TYPE (name));
1404 /* Skip the conversion operators. */
1405 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1406 vec_safe_iterate (method_vec, i, &fn);
1407 ++i)
1408 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1409 break;
1411 /* If the type is complete, use binary search. */
1412 if (COMPLETE_TYPE_P (type))
1414 int lo;
1415 int hi;
1417 lo = i;
1418 hi = method_vec->length ();
1419 while (lo < hi)
1421 i = (lo + hi) / 2;
1423 if (GATHER_STATISTICS)
1424 n_outer_fields_searched++;
1426 tmp = (*method_vec)[i];
1427 tmp = DECL_NAME (OVL_CURRENT (tmp));
1428 if (tmp > name)
1429 hi = i;
1430 else if (tmp < name)
1431 lo = i + 1;
1432 else
1433 return i;
1436 else
1437 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1439 if (GATHER_STATISTICS)
1440 n_outer_fields_searched++;
1441 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1442 return i;
1445 return -1;
1448 /* TYPE is a class type. Return the index of the fields within
1449 the method vector with name NAME, or -1 if no such field exists. */
1452 lookup_fnfields_1 (tree type, tree name)
1454 if (!CLASS_TYPE_P (type))
1455 return -1;
1457 if (COMPLETE_TYPE_P (type))
1459 if ((name == ctor_identifier
1460 || name == base_ctor_identifier
1461 || name == complete_ctor_identifier))
1463 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1464 lazily_declare_fn (sfk_constructor, type);
1465 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1466 lazily_declare_fn (sfk_copy_constructor, type);
1467 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1468 lazily_declare_fn (sfk_move_constructor, type);
1470 else if (name == ansi_assopname (NOP_EXPR))
1472 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1473 lazily_declare_fn (sfk_copy_assignment, type);
1474 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1475 lazily_declare_fn (sfk_move_assignment, type);
1477 else if ((name == dtor_identifier
1478 || name == base_dtor_identifier
1479 || name == complete_dtor_identifier
1480 || name == deleting_dtor_identifier)
1481 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1482 lazily_declare_fn (sfk_destructor, type);
1485 return lookup_fnfields_idx_nolazy (type, name);
1488 /* TYPE is a class type. Return the field within the method vector with
1489 name NAME, or NULL_TREE if no such field exists. */
1491 tree
1492 lookup_fnfields_slot (tree type, tree name)
1494 int ix = lookup_fnfields_1 (complete_type (type), name);
1495 if (ix < 0)
1496 return NULL_TREE;
1497 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1500 /* As above, but avoid lazily declaring functions. */
1502 tree
1503 lookup_fnfields_slot_nolazy (tree type, tree name)
1505 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1506 if (ix < 0)
1507 return NULL_TREE;
1508 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1511 /* Like lookup_fnfields_1, except that the name is extracted from
1512 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1515 class_method_index_for_fn (tree class_type, tree function)
1517 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1519 return lookup_fnfields_1 (class_type,
1520 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1521 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1522 DECL_NAME (function));
1526 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1527 the class or namespace used to qualify the name. CONTEXT_CLASS is
1528 the class corresponding to the object in which DECL will be used.
1529 Return a possibly modified version of DECL that takes into account
1530 the CONTEXT_CLASS.
1532 In particular, consider an expression like `B::m' in the context of
1533 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1534 then the most derived class indicated by the BASELINK_BINFO will be
1535 `B', not `D'. This function makes that adjustment. */
1537 tree
1538 adjust_result_of_qualified_name_lookup (tree decl,
1539 tree qualifying_scope,
1540 tree context_class)
1542 if (context_class && context_class != error_mark_node
1543 && CLASS_TYPE_P (context_class)
1544 && CLASS_TYPE_P (qualifying_scope)
1545 && DERIVED_FROM_P (qualifying_scope, context_class)
1546 && BASELINK_P (decl))
1548 tree base;
1550 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1551 Because we do not yet know which function will be chosen by
1552 overload resolution, we cannot yet check either accessibility
1553 or ambiguity -- in either case, the choice of a static member
1554 function might make the usage valid. */
1555 base = lookup_base (context_class, qualifying_scope,
1556 ba_unique, NULL, tf_none);
1557 if (base && base != error_mark_node)
1559 BASELINK_ACCESS_BINFO (decl) = base;
1560 BASELINK_BINFO (decl)
1561 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1562 ba_unique, NULL, tf_none);
1566 if (BASELINK_P (decl))
1567 BASELINK_QUALIFIED_P (decl) = true;
1569 return decl;
1573 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1574 PRE_FN is called in preorder, while POST_FN is called in postorder.
1575 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1576 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1577 that value is immediately returned and the walk is terminated. One
1578 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1579 POST_FN are passed the binfo to examine and the caller's DATA
1580 value. All paths are walked, thus virtual and morally virtual
1581 binfos can be multiply walked. */
1583 tree
1584 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1585 tree (*post_fn) (tree, void *), void *data)
1587 tree rval;
1588 unsigned ix;
1589 tree base_binfo;
1591 /* Call the pre-order walking function. */
1592 if (pre_fn)
1594 rval = pre_fn (binfo, data);
1595 if (rval)
1597 if (rval == dfs_skip_bases)
1598 goto skip_bases;
1599 return rval;
1603 /* Find the next child binfo to walk. */
1604 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1606 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1607 if (rval)
1608 return rval;
1611 skip_bases:
1612 /* Call the post-order walking function. */
1613 if (post_fn)
1615 rval = post_fn (binfo, data);
1616 gcc_assert (rval != dfs_skip_bases);
1617 return rval;
1620 return NULL_TREE;
1623 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1624 that binfos are walked at most once. */
1626 static tree
1627 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1628 tree (*post_fn) (tree, void *), void *data)
1630 tree rval;
1631 unsigned ix;
1632 tree base_binfo;
1634 /* Call the pre-order walking function. */
1635 if (pre_fn)
1637 rval = pre_fn (binfo, data);
1638 if (rval)
1640 if (rval == dfs_skip_bases)
1641 goto skip_bases;
1643 return rval;
1647 /* Find the next child binfo to walk. */
1648 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1650 if (BINFO_VIRTUAL_P (base_binfo))
1652 if (BINFO_MARKED (base_binfo))
1653 continue;
1654 BINFO_MARKED (base_binfo) = 1;
1657 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1658 if (rval)
1659 return rval;
1662 skip_bases:
1663 /* Call the post-order walking function. */
1664 if (post_fn)
1666 rval = post_fn (binfo, data);
1667 gcc_assert (rval != dfs_skip_bases);
1668 return rval;
1671 return NULL_TREE;
1674 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1675 BINFO. */
1677 static void
1678 dfs_unmark_r (tree binfo)
1680 unsigned ix;
1681 tree base_binfo;
1683 /* Process the basetypes. */
1684 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1686 if (BINFO_VIRTUAL_P (base_binfo))
1688 if (!BINFO_MARKED (base_binfo))
1689 continue;
1690 BINFO_MARKED (base_binfo) = 0;
1692 /* Only walk, if it can contain more virtual bases. */
1693 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1694 dfs_unmark_r (base_binfo);
1698 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1699 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1700 For diamond shaped hierarchies we must mark the virtual bases, to
1701 avoid multiple walks. */
1703 tree
1704 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1705 tree (*post_fn) (tree, void *), void *data)
1707 static int active = 0; /* We must not be called recursively. */
1708 tree rval;
1710 gcc_assert (pre_fn || post_fn);
1711 gcc_assert (!active);
1712 active++;
1714 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1715 /* We are not diamond shaped, and therefore cannot encounter the
1716 same binfo twice. */
1717 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1718 else
1720 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1721 if (!BINFO_INHERITANCE_CHAIN (binfo))
1723 /* We are at the top of the hierarchy, and can use the
1724 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1725 bases. */
1726 vec<tree, va_gc> *vbases;
1727 unsigned ix;
1728 tree base_binfo;
1730 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1731 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1732 BINFO_MARKED (base_binfo) = 0;
1734 else
1735 dfs_unmark_r (binfo);
1738 active--;
1740 return rval;
1743 /* Worker function for dfs_walk_once_accessible. Behaves like
1744 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1745 access given by the current context should be considered, (b) ONCE
1746 indicates whether bases should be marked during traversal. */
1748 static tree
1749 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1750 tree (*pre_fn) (tree, void *),
1751 tree (*post_fn) (tree, void *), void *data)
1753 tree rval = NULL_TREE;
1754 unsigned ix;
1755 tree base_binfo;
1757 /* Call the pre-order walking function. */
1758 if (pre_fn)
1760 rval = pre_fn (binfo, data);
1761 if (rval)
1763 if (rval == dfs_skip_bases)
1764 goto skip_bases;
1766 return rval;
1770 /* Find the next child binfo to walk. */
1771 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1773 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1775 if (mark && BINFO_MARKED (base_binfo))
1776 continue;
1778 /* If the base is inherited via private or protected
1779 inheritance, then we can't see it, unless we are a friend of
1780 the current binfo. */
1781 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1783 tree scope;
1784 if (!friends_p)
1785 continue;
1786 scope = current_scope ();
1787 if (!scope
1788 || TREE_CODE (scope) == NAMESPACE_DECL
1789 || !is_friend (BINFO_TYPE (binfo), scope))
1790 continue;
1793 if (mark)
1794 BINFO_MARKED (base_binfo) = 1;
1796 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1797 pre_fn, post_fn, data);
1798 if (rval)
1799 return rval;
1802 skip_bases:
1803 /* Call the post-order walking function. */
1804 if (post_fn)
1806 rval = post_fn (binfo, data);
1807 gcc_assert (rval != dfs_skip_bases);
1808 return rval;
1811 return NULL_TREE;
1814 /* Like dfs_walk_once except that only accessible bases are walked.
1815 FRIENDS_P indicates whether friendship of the local context
1816 should be considered when determining accessibility. */
1818 static tree
1819 dfs_walk_once_accessible (tree binfo, bool friends_p,
1820 tree (*pre_fn) (tree, void *),
1821 tree (*post_fn) (tree, void *), void *data)
1823 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1824 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1825 pre_fn, post_fn, data);
1827 if (diamond_shaped)
1829 if (!BINFO_INHERITANCE_CHAIN (binfo))
1831 /* We are at the top of the hierarchy, and can use the
1832 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1833 bases. */
1834 vec<tree, va_gc> *vbases;
1835 unsigned ix;
1836 tree base_binfo;
1838 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1839 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1840 BINFO_MARKED (base_binfo) = 0;
1842 else
1843 dfs_unmark_r (binfo);
1845 return rval;
1848 /* Check that virtual overrider OVERRIDER is acceptable for base function
1849 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1851 static int
1852 check_final_overrider (tree overrider, tree basefn)
1854 tree over_type = TREE_TYPE (overrider);
1855 tree base_type = TREE_TYPE (basefn);
1856 tree over_return = fndecl_declared_return_type (overrider);
1857 tree base_return = fndecl_declared_return_type (basefn);
1858 tree over_throw, base_throw;
1860 int fail = 0;
1862 if (DECL_INVALID_OVERRIDER_P (overrider))
1863 return 0;
1865 if (same_type_p (base_return, over_return))
1866 /* OK */;
1867 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1868 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1869 && POINTER_TYPE_P (base_return)))
1871 /* Potentially covariant. */
1872 unsigned base_quals, over_quals;
1874 fail = !POINTER_TYPE_P (base_return);
1875 if (!fail)
1877 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1879 base_return = TREE_TYPE (base_return);
1880 over_return = TREE_TYPE (over_return);
1882 base_quals = cp_type_quals (base_return);
1883 over_quals = cp_type_quals (over_return);
1885 if ((base_quals & over_quals) != over_quals)
1886 fail = 1;
1888 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1890 /* Strictly speaking, the standard requires the return type to be
1891 complete even if it only differs in cv-quals, but that seems
1892 like a bug in the wording. */
1893 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1894 over_return))
1896 tree binfo = lookup_base (over_return, base_return,
1897 ba_check, NULL, tf_none);
1899 if (!binfo || binfo == error_mark_node)
1900 fail = 1;
1903 else if (can_convert_standard (TREE_TYPE (base_type),
1904 TREE_TYPE (over_type),
1905 tf_warning_or_error))
1906 /* GNU extension, allow trivial pointer conversions such as
1907 converting to void *, or qualification conversion. */
1909 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1910 "invalid covariant return type for %q#D", overrider))
1911 inform (DECL_SOURCE_LOCATION (basefn),
1912 " overriding %q+#D", basefn);
1914 else
1915 fail = 2;
1917 else
1918 fail = 2;
1919 if (!fail)
1920 /* OK */;
1921 else
1923 if (fail == 1)
1925 error ("invalid covariant return type for %q+#D", overrider);
1926 error (" overriding %q+#D", basefn);
1928 else
1930 error ("conflicting return type specified for %q+#D", overrider);
1931 error (" overriding %q+#D", basefn);
1933 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1934 return 0;
1937 /* Check throw specifier is at least as strict. */
1938 maybe_instantiate_noexcept (basefn);
1939 maybe_instantiate_noexcept (overrider);
1940 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1941 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1943 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1945 error ("looser throw specifier for %q+#F", overrider);
1946 error (" overriding %q+#F", basefn);
1947 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1948 return 0;
1951 /* Check for conflicting type attributes. */
1952 if (!comp_type_attributes (over_type, base_type))
1954 error ("conflicting type attributes specified for %q+#D", overrider);
1955 error (" overriding %q+#D", basefn);
1956 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1957 return 0;
1960 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1962 if (DECL_DELETED_FN (overrider))
1964 error ("deleted function %q+D", overrider);
1965 error ("overriding non-deleted function %q+D", basefn);
1966 maybe_explain_implicit_delete (overrider);
1968 else
1970 error ("non-deleted function %q+D", overrider);
1971 error ("overriding deleted function %q+D", basefn);
1973 return 0;
1975 if (DECL_FINAL_P (basefn))
1977 error ("virtual function %q+D", overrider);
1978 error ("overriding final function %q+D", basefn);
1979 return 0;
1981 return 1;
1984 /* Given a class TYPE, and a function decl FNDECL, look for
1985 virtual functions in TYPE's hierarchy which FNDECL overrides.
1986 We do not look in TYPE itself, only its bases.
1988 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1989 find that it overrides anything.
1991 We check that every function which is overridden, is correctly
1992 overridden. */
1995 look_for_overrides (tree type, tree fndecl)
1997 tree binfo = TYPE_BINFO (type);
1998 tree base_binfo;
1999 int ix;
2000 int found = 0;
2002 /* A constructor for a class T does not override a function T
2003 in a base class. */
2004 if (DECL_CONSTRUCTOR_P (fndecl))
2005 return 0;
2007 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2009 tree basetype = BINFO_TYPE (base_binfo);
2011 if (TYPE_POLYMORPHIC_P (basetype))
2012 found += look_for_overrides_r (basetype, fndecl);
2014 return found;
2017 /* Look in TYPE for virtual functions with the same signature as
2018 FNDECL. */
2020 tree
2021 look_for_overrides_here (tree type, tree fndecl)
2023 int ix;
2025 /* If there are no methods in TYPE (meaning that only implicitly
2026 declared methods will ever be provided for TYPE), then there are
2027 no virtual functions. */
2028 if (!CLASSTYPE_METHOD_VEC (type))
2029 return NULL_TREE;
2031 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2032 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2033 else
2034 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2035 if (ix >= 0)
2037 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
2039 for (; fns; fns = OVL_NEXT (fns))
2041 tree fn = OVL_CURRENT (fns);
2043 if (!DECL_VIRTUAL_P (fn))
2044 /* Not a virtual. */;
2045 else if (DECL_CONTEXT (fn) != type)
2046 /* Introduced with a using declaration. */;
2047 else if (DECL_STATIC_FUNCTION_P (fndecl))
2049 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2050 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2051 if (compparms (TREE_CHAIN (btypes), dtypes))
2052 return fn;
2054 else if (same_signature_p (fndecl, fn))
2055 return fn;
2058 return NULL_TREE;
2061 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2062 TYPE itself and its bases. */
2064 static int
2065 look_for_overrides_r (tree type, tree fndecl)
2067 tree fn = look_for_overrides_here (type, fndecl);
2068 if (fn)
2070 if (DECL_STATIC_FUNCTION_P (fndecl))
2072 /* A static member function cannot match an inherited
2073 virtual member function. */
2074 error ("%q+#D cannot be declared", fndecl);
2075 error (" since %q+#D declared in base class", fn);
2077 else
2079 /* It's definitely virtual, even if not explicitly set. */
2080 DECL_VIRTUAL_P (fndecl) = 1;
2081 check_final_overrider (fndecl, fn);
2083 return 1;
2086 /* We failed to find one declared in this class. Look in its bases. */
2087 return look_for_overrides (type, fndecl);
2090 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2092 static tree
2093 dfs_get_pure_virtuals (tree binfo, void *data)
2095 tree type = (tree) data;
2097 /* We're not interested in primary base classes; the derived class
2098 of which they are a primary base will contain the information we
2099 need. */
2100 if (!BINFO_PRIMARY_P (binfo))
2102 tree virtuals;
2104 for (virtuals = BINFO_VIRTUALS (binfo);
2105 virtuals;
2106 virtuals = TREE_CHAIN (virtuals))
2107 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2108 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2111 return NULL_TREE;
2114 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2116 void
2117 get_pure_virtuals (tree type)
2119 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2120 is going to be overridden. */
2121 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2122 /* Now, run through all the bases which are not primary bases, and
2123 collect the pure virtual functions. We look at the vtable in
2124 each class to determine what pure virtual functions are present.
2125 (A primary base is not interesting because the derived class of
2126 which it is a primary base will contain vtable entries for the
2127 pure virtuals in the base class. */
2128 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2131 /* Debug info for C++ classes can get very large; try to avoid
2132 emitting it everywhere.
2134 Note that this optimization wins even when the target supports
2135 BINCL (if only slightly), and reduces the amount of work for the
2136 linker. */
2138 void
2139 maybe_suppress_debug_info (tree t)
2141 if (write_symbols == NO_DEBUG)
2142 return;
2144 /* We might have set this earlier in cp_finish_decl. */
2145 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2147 /* Always emit the information for each class every time. */
2148 if (flag_emit_class_debug_always)
2149 return;
2151 /* If we already know how we're handling this class, handle debug info
2152 the same way. */
2153 if (CLASSTYPE_INTERFACE_KNOWN (t))
2155 if (CLASSTYPE_INTERFACE_ONLY (t))
2156 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2157 /* else don't set it. */
2159 /* If the class has a vtable, write out the debug info along with
2160 the vtable. */
2161 else if (TYPE_CONTAINS_VPTR_P (t))
2162 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2164 /* Otherwise, just emit the debug info normally. */
2167 /* Note that we want debugging information for a base class of a class
2168 whose vtable is being emitted. Normally, this would happen because
2169 calling the constructor for a derived class implies calling the
2170 constructors for all bases, which involve initializing the
2171 appropriate vptr with the vtable for the base class; but in the
2172 presence of optimization, this initialization may be optimized
2173 away, so we tell finish_vtable_vardecl that we want the debugging
2174 information anyway. */
2176 static tree
2177 dfs_debug_mark (tree binfo, void * /*data*/)
2179 tree t = BINFO_TYPE (binfo);
2181 if (CLASSTYPE_DEBUG_REQUESTED (t))
2182 return dfs_skip_bases;
2184 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2186 return NULL_TREE;
2189 /* Write out the debugging information for TYPE, whose vtable is being
2190 emitted. Also walk through our bases and note that we want to
2191 write out information for them. This avoids the problem of not
2192 writing any debug info for intermediate basetypes whose
2193 constructors, and thus the references to their vtables, and thus
2194 the vtables themselves, were optimized away. */
2196 void
2197 note_debug_info_needed (tree type)
2199 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2201 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2202 rest_of_type_compilation (type, toplevel_bindings_p ());
2205 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2208 void
2209 print_search_statistics (void)
2211 if (! GATHER_STATISTICS)
2213 fprintf (stderr, "no search statistics\n");
2214 return;
2217 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2218 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2219 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2220 n_outer_fields_searched, n_calls_lookup_fnfields);
2221 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2224 void
2225 reinit_search_statistics (void)
2227 n_fields_searched = 0;
2228 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2229 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2230 n_calls_get_base_type = 0;
2231 n_outer_fields_searched = 0;
2232 n_contexts_saved = 0;
2235 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2236 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2237 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2238 bases have been encountered already in the tree walk. PARENT_CONVS
2239 is the list of lists of conversion functions that could hide CONV
2240 and OTHER_CONVS is the list of lists of conversion functions that
2241 could hide or be hidden by CONV, should virtualness be involved in
2242 the hierarchy. Merely checking the conversion op's name is not
2243 enough because two conversion operators to the same type can have
2244 different names. Return nonzero if we are visible. */
2246 static int
2247 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2248 tree to_type, tree parent_convs, tree other_convs)
2250 tree level, probe;
2252 /* See if we are hidden by a parent conversion. */
2253 for (level = parent_convs; level; level = TREE_CHAIN (level))
2254 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2255 if (same_type_p (to_type, TREE_TYPE (probe)))
2256 return 0;
2258 if (virtual_depth || virtualness)
2260 /* In a virtual hierarchy, we could be hidden, or could hide a
2261 conversion function on the other_convs list. */
2262 for (level = other_convs; level; level = TREE_CHAIN (level))
2264 int we_hide_them;
2265 int they_hide_us;
2266 tree *prev, other;
2268 if (!(virtual_depth || TREE_STATIC (level)))
2269 /* Neither is morally virtual, so cannot hide each other. */
2270 continue;
2272 if (!TREE_VALUE (level))
2273 /* They evaporated away already. */
2274 continue;
2276 they_hide_us = (virtual_depth
2277 && original_binfo (binfo, TREE_PURPOSE (level)));
2278 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2279 && original_binfo (TREE_PURPOSE (level), binfo));
2281 if (!(we_hide_them || they_hide_us))
2282 /* Neither is within the other, so no hiding can occur. */
2283 continue;
2285 for (prev = &TREE_VALUE (level), other = *prev; other;)
2287 if (same_type_p (to_type, TREE_TYPE (other)))
2289 if (they_hide_us)
2290 /* We are hidden. */
2291 return 0;
2293 if (we_hide_them)
2295 /* We hide the other one. */
2296 other = TREE_CHAIN (other);
2297 *prev = other;
2298 continue;
2301 prev = &TREE_CHAIN (other);
2302 other = *prev;
2306 return 1;
2309 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2310 of conversion functions, the first slot will be for the current
2311 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2312 of conversion functions from children of the current binfo,
2313 concatenated with conversions from elsewhere in the hierarchy --
2314 that list begins with OTHER_CONVS. Return a single list of lists
2315 containing only conversions from the current binfo and its
2316 children. */
2318 static tree
2319 split_conversions (tree my_convs, tree parent_convs,
2320 tree child_convs, tree other_convs)
2322 tree t;
2323 tree prev;
2325 /* Remove the original other_convs portion from child_convs. */
2326 for (prev = NULL, t = child_convs;
2327 t != other_convs; prev = t, t = TREE_CHAIN (t))
2328 continue;
2330 if (prev)
2331 TREE_CHAIN (prev) = NULL_TREE;
2332 else
2333 child_convs = NULL_TREE;
2335 /* Attach the child convs to any we had at this level. */
2336 if (my_convs)
2338 my_convs = parent_convs;
2339 TREE_CHAIN (my_convs) = child_convs;
2341 else
2342 my_convs = child_convs;
2344 return my_convs;
2347 /* Worker for lookup_conversions. Lookup conversion functions in
2348 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2349 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2350 encountered virtual bases already in the tree walk. PARENT_CONVS &
2351 PARENT_TPL_CONVS are lists of list of conversions within parent
2352 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2353 elsewhere in the tree. Return the conversions found within this
2354 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2355 encountered virtualness. We keep template and non-template
2356 conversions separate, to avoid unnecessary type comparisons.
2358 The located conversion functions are held in lists of lists. The
2359 TREE_VALUE of the outer list is the list of conversion functions
2360 found in a particular binfo. The TREE_PURPOSE of both the outer
2361 and inner lists is the binfo at which those conversions were
2362 found. TREE_STATIC is set for those lists within of morally
2363 virtual binfos. The TREE_VALUE of the inner list is the conversion
2364 function or overload itself. The TREE_TYPE of each inner list node
2365 is the converted-to type. */
2367 static int
2368 lookup_conversions_r (tree binfo,
2369 int virtual_depth, int virtualness,
2370 tree parent_convs, tree parent_tpl_convs,
2371 tree other_convs, tree other_tpl_convs,
2372 tree *convs, tree *tpl_convs)
2374 int my_virtualness = 0;
2375 tree my_convs = NULL_TREE;
2376 tree my_tpl_convs = NULL_TREE;
2377 tree child_convs = NULL_TREE;
2378 tree child_tpl_convs = NULL_TREE;
2379 unsigned i;
2380 tree base_binfo;
2381 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2382 tree conv;
2384 /* If we have no conversion operators, then don't look. */
2385 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2387 *convs = *tpl_convs = NULL_TREE;
2389 return 0;
2392 if (BINFO_VIRTUAL_P (binfo))
2393 virtual_depth++;
2395 /* First, locate the unhidden ones at this level. */
2396 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2397 vec_safe_iterate (method_vec, i, &conv);
2398 ++i)
2400 tree cur = OVL_CURRENT (conv);
2402 if (!DECL_CONV_FN_P (cur))
2403 break;
2405 if (TREE_CODE (cur) == TEMPLATE_DECL)
2407 /* Only template conversions can be overloaded, and we must
2408 flatten them out and check each one individually. */
2409 tree tpls;
2411 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2413 tree tpl = OVL_CURRENT (tpls);
2414 tree type = DECL_CONV_FN_TYPE (tpl);
2416 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2417 type, parent_tpl_convs, other_tpl_convs))
2419 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2420 TREE_TYPE (my_tpl_convs) = type;
2421 if (virtual_depth)
2423 TREE_STATIC (my_tpl_convs) = 1;
2424 my_virtualness = 1;
2429 else
2431 tree name = DECL_NAME (cur);
2433 if (!IDENTIFIER_MARKED (name))
2435 tree type = DECL_CONV_FN_TYPE (cur);
2436 if (type_uses_auto (type))
2438 mark_used (cur);
2439 type = DECL_CONV_FN_TYPE (cur);
2442 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2443 type, parent_convs, other_convs))
2445 my_convs = tree_cons (binfo, conv, my_convs);
2446 TREE_TYPE (my_convs) = type;
2447 if (virtual_depth)
2449 TREE_STATIC (my_convs) = 1;
2450 my_virtualness = 1;
2452 IDENTIFIER_MARKED (name) = 1;
2458 if (my_convs)
2460 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2461 if (virtual_depth)
2462 TREE_STATIC (parent_convs) = 1;
2465 if (my_tpl_convs)
2467 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2468 if (virtual_depth)
2469 TREE_STATIC (parent_tpl_convs) = 1;
2472 child_convs = other_convs;
2473 child_tpl_convs = other_tpl_convs;
2475 /* Now iterate over each base, looking for more conversions. */
2476 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2478 tree base_convs, base_tpl_convs;
2479 unsigned base_virtualness;
2481 base_virtualness = lookup_conversions_r (base_binfo,
2482 virtual_depth, virtualness,
2483 parent_convs, parent_tpl_convs,
2484 child_convs, child_tpl_convs,
2485 &base_convs, &base_tpl_convs);
2486 if (base_virtualness)
2487 my_virtualness = virtualness = 1;
2488 child_convs = chainon (base_convs, child_convs);
2489 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2492 /* Unmark the conversions found at this level */
2493 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2494 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2496 *convs = split_conversions (my_convs, parent_convs,
2497 child_convs, other_convs);
2498 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2499 child_tpl_convs, other_tpl_convs);
2501 return my_virtualness;
2504 /* Return a TREE_LIST containing all the non-hidden user-defined
2505 conversion functions for TYPE (and its base-classes). The
2506 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2507 function. The TREE_PURPOSE is the BINFO from which the conversion
2508 functions in this node were selected. This function is effectively
2509 performing a set of member lookups as lookup_fnfield does, but
2510 using the type being converted to as the unique key, rather than the
2511 field name. */
2513 tree
2514 lookup_conversions (tree type)
2516 tree convs, tpl_convs;
2517 tree list = NULL_TREE;
2519 complete_type (type);
2520 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2521 return NULL_TREE;
2523 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2524 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2525 &convs, &tpl_convs);
2527 /* Flatten the list-of-lists */
2528 for (; convs; convs = TREE_CHAIN (convs))
2530 tree probe, next;
2532 for (probe = TREE_VALUE (convs); probe; probe = next)
2534 next = TREE_CHAIN (probe);
2536 TREE_CHAIN (probe) = list;
2537 list = probe;
2541 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2543 tree probe, next;
2545 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2547 next = TREE_CHAIN (probe);
2549 TREE_CHAIN (probe) = list;
2550 list = probe;
2554 return list;
2557 /* Returns the binfo of the first direct or indirect virtual base derived
2558 from BINFO, or NULL if binfo is not via virtual. */
2560 tree
2561 binfo_from_vbase (tree binfo)
2563 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2565 if (BINFO_VIRTUAL_P (binfo))
2566 return binfo;
2568 return NULL_TREE;
2571 /* Returns the binfo of the first direct or indirect virtual base derived
2572 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2573 via virtual. */
2575 tree
2576 binfo_via_virtual (tree binfo, tree limit)
2578 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2579 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2580 return NULL_TREE;
2582 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2583 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2585 if (BINFO_VIRTUAL_P (binfo))
2586 return binfo;
2588 return NULL_TREE;
2591 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2592 Find the equivalent binfo within whatever graph HERE is located.
2593 This is the inverse of original_binfo. */
2595 tree
2596 copied_binfo (tree binfo, tree here)
2598 tree result = NULL_TREE;
2600 if (BINFO_VIRTUAL_P (binfo))
2602 tree t;
2604 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2605 t = BINFO_INHERITANCE_CHAIN (t))
2606 continue;
2608 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2610 else if (BINFO_INHERITANCE_CHAIN (binfo))
2612 tree cbinfo;
2613 tree base_binfo;
2614 int ix;
2616 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2617 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2618 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2620 result = base_binfo;
2621 break;
2624 else
2626 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2627 result = here;
2630 gcc_assert (result);
2631 return result;
2634 tree
2635 binfo_for_vbase (tree base, tree t)
2637 unsigned ix;
2638 tree binfo;
2639 vec<tree, va_gc> *vbases;
2641 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2642 vec_safe_iterate (vbases, ix, &binfo); ix++)
2643 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2644 return binfo;
2645 return NULL;
2648 /* BINFO is some base binfo of HERE, within some other
2649 hierarchy. Return the equivalent binfo, but in the hierarchy
2650 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2651 is not a base binfo of HERE, returns NULL_TREE. */
2653 tree
2654 original_binfo (tree binfo, tree here)
2656 tree result = NULL;
2658 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2659 result = here;
2660 else if (BINFO_VIRTUAL_P (binfo))
2661 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2662 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2663 : NULL_TREE);
2664 else if (BINFO_INHERITANCE_CHAIN (binfo))
2666 tree base_binfos;
2668 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2669 if (base_binfos)
2671 int ix;
2672 tree base_binfo;
2674 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2675 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2676 BINFO_TYPE (binfo)))
2678 result = base_binfo;
2679 break;
2684 return result;