2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / search.c
blob09c1b4e6456d24682eb3246de361a4e8354f6ba4
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "cp-tree.h"
28 #include "intl.h"
29 #include "toplev.h"
30 #include "spellcheck-tree.h"
32 static int is_subobject_of_p (tree, tree);
33 static tree dfs_lookup_base (tree, void *);
34 static tree dfs_dcast_hint_pre (tree, void *);
35 static tree dfs_dcast_hint_post (tree, void *);
36 static tree dfs_debug_mark (tree, void *);
37 static int check_hidden_convs (tree, int, int, tree, tree, tree);
38 static tree split_conversions (tree, tree, tree, tree);
39 static int lookup_conversions_r (tree, int, int,
40 tree, tree, tree, tree, tree *, tree *);
41 static int look_for_overrides_r (tree, tree);
42 static tree lookup_field_r (tree, void *);
43 static tree dfs_accessible_post (tree, void *);
44 static tree dfs_walk_once_accessible (tree, bool,
45 tree (*pre_fn) (tree, void *),
46 tree (*post_fn) (tree, void *),
47 void *data);
48 static tree dfs_access_in_type (tree, void *);
49 static access_kind access_in_type (tree, tree);
50 static tree dfs_get_pure_virtuals (tree, void *);
53 /* Variables for gathering statistics. */
54 static int n_fields_searched;
55 static int n_calls_lookup_field, n_calls_lookup_field_1;
56 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
57 static int n_calls_get_base_type;
58 static int n_outer_fields_searched;
59 static int n_contexts_saved;
62 /* Data for lookup_base and its workers. */
64 struct lookup_base_data_s
66 tree t; /* type being searched. */
67 tree base; /* The base type we're looking for. */
68 tree binfo; /* Found binfo. */
69 bool via_virtual; /* Found via a virtual path. */
70 bool ambiguous; /* Found multiply ambiguous */
71 bool repeated_base; /* Whether there are repeated bases in the
72 hierarchy. */
73 bool want_any; /* Whether we want any matching binfo. */
76 /* Worker function for lookup_base. See if we've found the desired
77 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
79 static tree
80 dfs_lookup_base (tree binfo, void *data_)
82 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
84 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
86 if (!data->binfo)
88 data->binfo = binfo;
89 data->via_virtual
90 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
92 if (!data->repeated_base)
93 /* If there are no repeated bases, we can stop now. */
94 return binfo;
96 if (data->want_any && !data->via_virtual)
97 /* If this is a non-virtual base, then we can't do
98 better. */
99 return binfo;
101 return dfs_skip_bases;
103 else
105 gcc_assert (binfo != data->binfo);
107 /* We've found more than one matching binfo. */
108 if (!data->want_any)
110 /* This is immediately ambiguous. */
111 data->binfo = NULL_TREE;
112 data->ambiguous = true;
113 return error_mark_node;
116 /* Prefer one via a non-virtual path. */
117 if (!binfo_via_virtual (binfo, data->t))
119 data->binfo = binfo;
120 data->via_virtual = false;
121 return binfo;
124 /* There must be repeated bases, otherwise we'd have stopped
125 on the first base we found. */
126 return dfs_skip_bases;
130 return NULL_TREE;
133 /* Returns true if type BASE is accessible in T. (BASE is known to be
134 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
135 true, consider any special access of the current scope, or access
136 bestowed by friendship. */
138 bool
139 accessible_base_p (tree t, tree base, bool consider_local_p)
141 tree decl;
143 /* [class.access.base]
145 A base class is said to be accessible if an invented public
146 member of the base class is accessible.
148 If BASE is a non-proper base, this condition is trivially
149 true. */
150 if (same_type_p (t, base))
151 return true;
152 /* Rather than inventing a public member, we use the implicit
153 public typedef created in the scope of every class. */
154 decl = TYPE_FIELDS (base);
155 while (!DECL_SELF_REFERENCE_P (decl))
156 decl = DECL_CHAIN (decl);
157 while (ANON_AGGR_TYPE_P (t))
158 t = TYPE_CONTEXT (t);
159 return accessible_p (t, decl, consider_local_p);
162 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
163 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
164 non-NULL, fill with information about what kind of base we
165 discovered.
167 If the base is inaccessible, or ambiguous, then error_mark_node is
168 returned. If the tf_error bit of COMPLAIN is not set, no error
169 is issued. */
171 tree
172 lookup_base (tree t, tree base, base_access access,
173 base_kind *kind_ptr, tsubst_flags_t complain)
175 tree binfo;
176 tree t_binfo;
177 base_kind bk;
179 /* "Nothing" is definitely not derived from Base. */
180 if (t == NULL_TREE)
182 if (kind_ptr)
183 *kind_ptr = bk_not_base;
184 return NULL_TREE;
187 if (t == error_mark_node || base == error_mark_node)
189 if (kind_ptr)
190 *kind_ptr = bk_not_base;
191 return error_mark_node;
193 gcc_assert (TYPE_P (base));
195 if (!TYPE_P (t))
197 t_binfo = t;
198 t = BINFO_TYPE (t);
200 else
202 t = complete_type (TYPE_MAIN_VARIANT (t));
203 t_binfo = TYPE_BINFO (t);
206 base = TYPE_MAIN_VARIANT (base);
208 /* If BASE is incomplete, it can't be a base of T--and instantiating it
209 might cause an error. */
210 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
212 struct lookup_base_data_s data;
214 data.t = t;
215 data.base = base;
216 data.binfo = NULL_TREE;
217 data.ambiguous = data.via_virtual = false;
218 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
219 data.want_any = access == ba_any;
221 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
222 binfo = data.binfo;
224 if (!binfo)
225 bk = data.ambiguous ? bk_ambig : bk_not_base;
226 else if (binfo == t_binfo)
227 bk = bk_same_type;
228 else if (data.via_virtual)
229 bk = bk_via_virtual;
230 else
231 bk = bk_proper_base;
233 else
235 binfo = NULL_TREE;
236 bk = bk_not_base;
239 /* Check that the base is unambiguous and accessible. */
240 if (access != ba_any)
241 switch (bk)
243 case bk_not_base:
244 break;
246 case bk_ambig:
247 if (complain & tf_error)
248 error ("%qT is an ambiguous base of %qT", base, t);
249 binfo = error_mark_node;
250 break;
252 default:
253 if ((access & ba_check_bit)
254 /* If BASE is incomplete, then BASE and TYPE are probably
255 the same, in which case BASE is accessible. If they
256 are not the same, then TYPE is invalid. In that case,
257 there's no need to issue another error here, and
258 there's no implicit typedef to use in the code that
259 follows, so we skip the check. */
260 && COMPLETE_TYPE_P (base)
261 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
263 if (complain & tf_error)
264 error ("%qT is an inaccessible base of %qT", base, t);
265 binfo = error_mark_node;
266 bk = bk_inaccessible;
268 break;
271 if (kind_ptr)
272 *kind_ptr = bk;
274 return binfo;
277 /* Data for dcast_base_hint walker. */
279 struct dcast_data_s
281 tree subtype; /* The base type we're looking for. */
282 int virt_depth; /* Number of virtual bases encountered from most
283 derived. */
284 tree offset; /* Best hint offset discovered so far. */
285 bool repeated_base; /* Whether there are repeated bases in the
286 hierarchy. */
289 /* Worker for dcast_base_hint. Search for the base type being cast
290 from. */
292 static tree
293 dfs_dcast_hint_pre (tree binfo, void *data_)
295 struct dcast_data_s *data = (struct dcast_data_s *) data_;
297 if (BINFO_VIRTUAL_P (binfo))
298 data->virt_depth++;
300 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
302 if (data->virt_depth)
304 data->offset = ssize_int (-1);
305 return data->offset;
307 if (data->offset)
308 data->offset = ssize_int (-3);
309 else
310 data->offset = BINFO_OFFSET (binfo);
312 return data->repeated_base ? dfs_skip_bases : data->offset;
315 return NULL_TREE;
318 /* Worker for dcast_base_hint. Track the virtual depth. */
320 static tree
321 dfs_dcast_hint_post (tree binfo, void *data_)
323 struct dcast_data_s *data = (struct dcast_data_s *) data_;
325 if (BINFO_VIRTUAL_P (binfo))
326 data->virt_depth--;
328 return NULL_TREE;
331 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
332 started from is related to the required TARGET type, in order to optimize
333 the inheritance graph search. This information is independent of the
334 current context, and ignores private paths, hence get_base_distance is
335 inappropriate. Return a TREE specifying the base offset, BOFF.
336 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
337 and there are no public virtual SUBTYPE bases.
338 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
339 BOFF == -2, SUBTYPE is not a public base.
340 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
342 tree
343 dcast_base_hint (tree subtype, tree target)
345 struct dcast_data_s data;
347 data.subtype = subtype;
348 data.virt_depth = 0;
349 data.offset = NULL_TREE;
350 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
352 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
353 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
354 return data.offset ? data.offset : ssize_int (-2);
357 /* Search for a member with name NAME in a multiple inheritance
358 lattice specified by TYPE. If it does not exist, return NULL_TREE.
359 If the member is ambiguously referenced, return `error_mark_node'.
360 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
361 true, type declarations are preferred. */
363 /* Do a 1-level search for NAME as a member of TYPE. The caller must
364 figure out whether it can access this field. (Since it is only one
365 level, this is reasonable.) */
367 tree
368 lookup_field_1 (tree type, tree name, bool want_type)
370 tree field;
372 gcc_assert (identifier_p (name));
374 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
375 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
376 || TREE_CODE (type) == TYPENAME_TYPE)
377 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
378 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
379 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
380 the code often worked even when we treated the index as a list
381 of fields!)
382 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
383 return NULL_TREE;
385 if (CLASSTYPE_SORTED_FIELDS (type))
387 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
388 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
389 int i;
391 while (lo < hi)
393 i = (lo + hi) / 2;
395 if (GATHER_STATISTICS)
396 n_fields_searched++;
398 if (DECL_NAME (fields[i]) > name)
399 hi = i;
400 else if (DECL_NAME (fields[i]) < name)
401 lo = i + 1;
402 else
404 field = NULL_TREE;
406 /* We might have a nested class and a field with the
407 same name; we sorted them appropriately via
408 field_decl_cmp, so just look for the first or last
409 field with this name. */
410 if (want_type)
413 field = fields[i--];
414 while (i >= lo && DECL_NAME (fields[i]) == name);
415 if (!DECL_DECLARES_TYPE_P (field))
416 field = NULL_TREE;
418 else
421 field = fields[i++];
422 while (i < hi && DECL_NAME (fields[i]) == name);
425 if (field)
427 field = strip_using_decl (field);
428 if (is_overloaded_fn (field))
429 field = NULL_TREE;
432 return field;
435 return NULL_TREE;
438 field = TYPE_FIELDS (type);
440 if (GATHER_STATISTICS)
441 n_calls_lookup_field_1++;
443 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
445 tree decl = field;
447 if (GATHER_STATISTICS)
448 n_fields_searched++;
450 gcc_assert (DECL_P (field));
451 if (DECL_NAME (field) == NULL_TREE
452 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
454 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
455 if (temp)
456 return temp;
459 if (TREE_CODE (decl) == USING_DECL
460 && DECL_NAME (decl) == name)
462 decl = strip_using_decl (decl);
463 if (is_overloaded_fn (decl))
464 continue;
467 if (DECL_NAME (decl) == name
468 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
469 return decl;
471 /* Not found. */
472 if (name == vptr_identifier)
474 /* Give the user what s/he thinks s/he wants. */
475 if (TYPE_POLYMORPHIC_P (type))
476 return TYPE_VFIELD (type);
478 return NULL_TREE;
481 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
482 NAMESPACE_DECL corresponding to the innermost non-block scope. */
484 tree
485 current_scope (void)
487 /* There are a number of cases we need to be aware of here:
488 current_class_type current_function_decl
489 global NULL NULL
490 fn-local NULL SET
491 class-local SET NULL
492 class->fn SET SET
493 fn->class SET SET
495 Those last two make life interesting. If we're in a function which is
496 itself inside a class, we need decls to go into the fn's decls (our
497 second case below). But if we're in a class and the class itself is
498 inside a function, we need decls to go into the decls for the class. To
499 achieve this last goal, we must see if, when both current_class_ptr and
500 current_function_decl are set, the class was declared inside that
501 function. If so, we know to put the decls into the class's scope. */
502 if (current_function_decl && current_class_type
503 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
504 && same_type_p (DECL_CONTEXT (current_function_decl),
505 current_class_type))
506 || (DECL_FRIEND_CONTEXT (current_function_decl)
507 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
508 current_class_type))))
509 return current_function_decl;
510 if (current_class_type)
511 return current_class_type;
512 if (current_function_decl)
513 return current_function_decl;
514 return current_namespace;
517 /* Returns nonzero if we are currently in a function scope. Note
518 that this function returns zero if we are within a local class, but
519 not within a member function body of the local class. */
522 at_function_scope_p (void)
524 tree cs = current_scope ();
525 /* Also check cfun to make sure that we're really compiling
526 this function (as opposed to having set current_function_decl
527 for access checking or some such). */
528 return (cs && TREE_CODE (cs) == FUNCTION_DECL
529 && cfun && cfun->decl == current_function_decl);
532 /* Returns true if the innermost active scope is a class scope. */
534 bool
535 at_class_scope_p (void)
537 tree cs = current_scope ();
538 return cs && TYPE_P (cs);
541 /* Returns true if the innermost active scope is a namespace scope. */
543 bool
544 at_namespace_scope_p (void)
546 tree cs = current_scope ();
547 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
550 /* Return the scope of DECL, as appropriate when doing name-lookup. */
552 tree
553 context_for_name_lookup (tree decl)
555 /* [class.union]
557 For the purposes of name lookup, after the anonymous union
558 definition, the members of the anonymous union are considered to
559 have been defined in the scope in which the anonymous union is
560 declared. */
561 tree context = DECL_CONTEXT (decl);
563 while (context && TYPE_P (context)
564 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
565 context = TYPE_CONTEXT (context);
566 if (!context)
567 context = global_namespace;
569 return context;
572 /* Returns true iff DECL is declared in TYPE. */
574 static bool
575 member_declared_in_type (tree decl, tree type)
577 /* A normal declaration obviously counts. */
578 if (context_for_name_lookup (decl) == type)
579 return true;
580 /* So does a using or access declaration. */
581 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
582 && purpose_member (type, DECL_ACCESS (decl)))
583 return true;
584 return false;
587 /* The accessibility routines use BINFO_ACCESS for scratch space
588 during the computation of the accessibility of some declaration. */
590 /* Avoid walking up past a declaration of the member. */
592 static tree
593 dfs_access_in_type_pre (tree binfo, void *data)
595 tree decl = (tree) data;
596 tree type = BINFO_TYPE (binfo);
597 if (member_declared_in_type (decl, type))
598 return dfs_skip_bases;
599 return NULL_TREE;
602 #define BINFO_ACCESS(NODE) \
603 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
605 /* Set the access associated with NODE to ACCESS. */
607 #define SET_BINFO_ACCESS(NODE, ACCESS) \
608 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
609 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
611 /* Called from access_in_type via dfs_walk. Calculate the access to
612 DATA (which is really a DECL) in BINFO. */
614 static tree
615 dfs_access_in_type (tree binfo, void *data)
617 tree decl = (tree) data;
618 tree type = BINFO_TYPE (binfo);
619 access_kind access = ak_none;
621 if (context_for_name_lookup (decl) == type)
623 /* If we have descended to the scope of DECL, just note the
624 appropriate access. */
625 if (TREE_PRIVATE (decl))
626 access = ak_private;
627 else if (TREE_PROTECTED (decl))
628 access = ak_protected;
629 else
630 access = ak_public;
632 else
634 /* First, check for an access-declaration that gives us more
635 access to the DECL. */
636 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
638 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
640 if (decl_access)
642 decl_access = TREE_VALUE (decl_access);
644 if (decl_access == access_public_node)
645 access = ak_public;
646 else if (decl_access == access_protected_node)
647 access = ak_protected;
648 else if (decl_access == access_private_node)
649 access = ak_private;
650 else
651 gcc_unreachable ();
655 if (!access)
657 int i;
658 tree base_binfo;
659 vec<tree, va_gc> *accesses;
661 /* Otherwise, scan our baseclasses, and pick the most favorable
662 access. */
663 accesses = BINFO_BASE_ACCESSES (binfo);
664 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
666 tree base_access = (*accesses)[i];
667 access_kind base_access_now = BINFO_ACCESS (base_binfo);
669 if (base_access_now == ak_none || base_access_now == ak_private)
670 /* If it was not accessible in the base, or only
671 accessible as a private member, we can't access it
672 all. */
673 base_access_now = ak_none;
674 else if (base_access == access_protected_node)
675 /* Public and protected members in the base become
676 protected here. */
677 base_access_now = ak_protected;
678 else if (base_access == access_private_node)
679 /* Public and protected members in the base become
680 private here. */
681 base_access_now = ak_private;
683 /* See if the new access, via this base, gives more
684 access than our previous best access. */
685 if (base_access_now != ak_none
686 && (access == ak_none || base_access_now < access))
688 access = base_access_now;
690 /* If the new access is public, we can't do better. */
691 if (access == ak_public)
692 break;
698 /* Note the access to DECL in TYPE. */
699 SET_BINFO_ACCESS (binfo, access);
701 return NULL_TREE;
704 /* Return the access to DECL in TYPE. */
706 static access_kind
707 access_in_type (tree type, tree decl)
709 tree binfo = TYPE_BINFO (type);
711 /* We must take into account
713 [class.paths]
715 If a name can be reached by several paths through a multiple
716 inheritance graph, the access is that of the path that gives
717 most access.
719 The algorithm we use is to make a post-order depth-first traversal
720 of the base-class hierarchy. As we come up the tree, we annotate
721 each node with the most lenient access. */
722 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
724 return BINFO_ACCESS (binfo);
727 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
728 of OTYPE in the context of DERIVED. */
730 static int
731 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
733 /* We're checking this clause from [class.access.base]
735 m as a member of N is protected, and the reference occurs in a
736 member or friend of class N, or in a member or friend of a
737 class P derived from N, where m as a member of P is public, private
738 or protected.
740 Here DERIVED is a possible P, DECL is m and TYPE is N. */
742 /* If DERIVED isn't derived from N, then it can't be a P. */
743 if (!DERIVED_FROM_P (type, derived))
744 return 0;
746 /* [class.protected]
748 When a friend or a member function of a derived class references
749 a protected nonstatic member of a base class, an access check
750 applies in addition to those described earlier in clause
751 _class.access_) Except when forming a pointer to member
752 (_expr.unary.op_), the access must be through a pointer to,
753 reference to, or object of the derived class itself (or any class
754 derived from that class) (_expr.ref_). If the access is to form
755 a pointer to member, the nested-name-specifier shall name the
756 derived class (or any class derived from that class). */
757 if (DECL_NONSTATIC_MEMBER_P (decl)
758 && !DERIVED_FROM_P (derived, otype))
759 return 0;
761 return 1;
764 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
765 to access DECL through TYPE. OTYPE is the type of the object. */
767 static int
768 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
770 /* We're checking this clause from [class.access.base]
772 m as a member of N is protected, and the reference occurs in a
773 member or friend of class N, or in a member or friend of a
774 class P derived from N, where m as a member of P is public, private
775 or protected.
777 Here DECL is m and TYPE is N. SCOPE is the current context,
778 and we check all its possible Ps. */
779 tree befriending_classes;
780 tree t;
782 if (!scope)
783 return 0;
785 if (is_global_friend (scope))
786 return 1;
788 /* Is SCOPE itself a suitable P? */
789 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
790 return 1;
792 if (DECL_DECLARES_FUNCTION_P (scope))
793 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
794 else if (TYPE_P (scope))
795 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
796 else
797 return 0;
799 for (t = befriending_classes; t; t = TREE_CHAIN (t))
800 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
801 return 1;
803 /* Nested classes have the same access as their enclosing types, as
804 per DR 45 (this is a change from C++98). */
805 if (TYPE_P (scope))
806 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
807 return 1;
809 if (DECL_DECLARES_FUNCTION_P (scope))
811 /* Perhaps this SCOPE is a member of a class which is a
812 friend. */
813 if (DECL_CLASS_SCOPE_P (scope)
814 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
815 return 1;
818 /* Maybe scope's template is a friend. */
819 if (tree tinfo = get_template_info (scope))
821 tree tmpl = TI_TEMPLATE (tinfo);
822 if (DECL_CLASS_TEMPLATE_P (tmpl))
823 tmpl = TREE_TYPE (tmpl);
824 else
825 tmpl = DECL_TEMPLATE_RESULT (tmpl);
826 if (tmpl != scope)
828 /* Increment processing_template_decl to make sure that
829 dependent_type_p works correctly. */
830 ++processing_template_decl;
831 int ret = friend_accessible_p (tmpl, decl, type, otype);
832 --processing_template_decl;
833 if (ret)
834 return 1;
838 /* If is_friend is true, we should have found a befriending class. */
839 gcc_checking_assert (!is_friend (type, scope));
841 return 0;
844 struct dfs_accessible_data
846 tree decl;
847 tree object_type;
850 /* Avoid walking up past a declaration of the member. */
852 static tree
853 dfs_accessible_pre (tree binfo, void *data)
855 dfs_accessible_data *d = (dfs_accessible_data *)data;
856 tree type = BINFO_TYPE (binfo);
857 if (member_declared_in_type (d->decl, type))
858 return dfs_skip_bases;
859 return NULL_TREE;
862 /* Called via dfs_walk_once_accessible from accessible_p */
864 static tree
865 dfs_accessible_post (tree binfo, void *data)
867 /* access_in_type already set BINFO_ACCESS for us. */
868 access_kind access = BINFO_ACCESS (binfo);
869 tree N = BINFO_TYPE (binfo);
870 dfs_accessible_data *d = (dfs_accessible_data *)data;
871 tree decl = d->decl;
872 tree scope = current_nonlambda_scope ();
874 /* A member m is accessible at the point R when named in class N if */
875 switch (access)
877 case ak_none:
878 return NULL_TREE;
880 case ak_public:
881 /* m as a member of N is public, or */
882 return binfo;
884 case ak_private:
886 /* m as a member of N is private, and R occurs in a member or friend of
887 class N, or */
888 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
889 && is_friend (N, scope))
890 return binfo;
891 return NULL_TREE;
894 case ak_protected:
896 /* m as a member of N is protected, and R occurs in a member or friend
897 of class N, or in a member or friend of a class P derived from N,
898 where m as a member of P is public, private, or protected */
899 if (friend_accessible_p (scope, decl, N, d->object_type))
900 return binfo;
901 return NULL_TREE;
904 default:
905 gcc_unreachable ();
909 /* Like accessible_p below, but within a template returns true iff DECL is
910 accessible in TYPE to all possible instantiations of the template. */
913 accessible_in_template_p (tree type, tree decl)
915 int save_ptd = processing_template_decl;
916 processing_template_decl = 0;
917 int val = accessible_p (type, decl, false);
918 processing_template_decl = save_ptd;
919 return val;
922 /* DECL is a declaration from a base class of TYPE, which was the
923 class used to name DECL. Return nonzero if, in the current
924 context, DECL is accessible. If TYPE is actually a BINFO node,
925 then we can tell in what context the access is occurring by looking
926 at the most derived class along the path indicated by BINFO. If
927 CONSIDER_LOCAL is true, do consider special access the current
928 scope or friendship thereof we might have. */
931 accessible_p (tree type, tree decl, bool consider_local_p)
933 tree binfo;
934 access_kind access;
936 /* If this declaration is in a block or namespace scope, there's no
937 access control. */
938 if (!TYPE_P (context_for_name_lookup (decl)))
939 return 1;
941 /* There is no need to perform access checks inside a thunk. */
942 if (current_function_decl && DECL_THUNK_P (current_function_decl))
943 return 1;
945 /* In a template declaration, we cannot be sure whether the
946 particular specialization that is instantiated will be a friend
947 or not. Therefore, all access checks are deferred until
948 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
949 parameter list for a template (because we may see dependent types
950 in default arguments for template parameters), and access
951 checking should be performed in the outermost parameter list. */
952 if (processing_template_decl
953 && !expanding_concept ()
954 && (!processing_template_parmlist || processing_template_decl > 1))
955 return 1;
957 tree otype = NULL_TREE;
958 if (!TYPE_P (type))
960 /* When accessing a non-static member, the most derived type in the
961 binfo chain is the type of the object; remember that type for
962 protected_accessible_p. */
963 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
964 otype = BINFO_TYPE (b);
965 type = BINFO_TYPE (type);
967 else
968 otype = type;
970 /* [class.access.base]
972 A member m is accessible when named in class N if
974 --m as a member of N is public, or
976 --m as a member of N is private, and the reference occurs in a
977 member or friend of class N, or
979 --m as a member of N is protected, and the reference occurs in a
980 member or friend of class N, or in a member or friend of a
981 class P derived from N, where m as a member of P is public, private or
982 protected, or
984 --there exists a base class B of N that is accessible at the point
985 of reference, and m is accessible when named in class B.
987 We walk the base class hierarchy, checking these conditions. */
989 /* We walk using TYPE_BINFO (type) because access_in_type will set
990 BINFO_ACCESS on it and its bases. */
991 binfo = TYPE_BINFO (type);
993 /* Compute the accessibility of DECL in the class hierarchy
994 dominated by type. */
995 access = access_in_type (type, decl);
996 if (access == ak_public)
997 return 1;
999 /* If we aren't considering the point of reference, only the first bullet
1000 applies. */
1001 if (!consider_local_p)
1002 return 0;
1004 dfs_accessible_data d = { decl, otype };
1006 /* Walk the hierarchy again, looking for a base class that allows
1007 access. */
1008 return dfs_walk_once_accessible (binfo, /*friends=*/true,
1009 dfs_accessible_pre,
1010 dfs_accessible_post, &d)
1011 != NULL_TREE;
1014 struct lookup_field_info {
1015 /* The type in which we're looking. */
1016 tree type;
1017 /* The name of the field for which we're looking. */
1018 tree name;
1019 /* If non-NULL, the current result of the lookup. */
1020 tree rval;
1021 /* The path to RVAL. */
1022 tree rval_binfo;
1023 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1024 candidates. */
1025 tree ambiguous;
1026 /* If nonzero, we are looking for types, not data members. */
1027 int want_type;
1028 /* If something went wrong, a message indicating what. */
1029 const char *errstr;
1032 /* Nonzero for a class member means that it is shared between all objects
1033 of that class.
1035 [class.member.lookup]:If the resulting set of declarations are not all
1036 from sub-objects of the same type, or the set has a nonstatic member
1037 and includes members from distinct sub-objects, there is an ambiguity
1038 and the program is ill-formed.
1040 This function checks that T contains no nonstatic members. */
1043 shared_member_p (tree t)
1045 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
1046 || TREE_CODE (t) == CONST_DECL)
1047 return 1;
1048 if (is_overloaded_fn (t))
1050 t = get_fns (t);
1051 for (; t; t = OVL_NEXT (t))
1053 tree fn = OVL_CURRENT (t);
1054 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1055 return 0;
1057 return 1;
1059 return 0;
1062 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1063 found as a base class and sub-object of the object denoted by
1064 BINFO. */
1066 static int
1067 is_subobject_of_p (tree parent, tree binfo)
1069 tree probe;
1071 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1073 if (probe == binfo)
1074 return 1;
1075 if (BINFO_VIRTUAL_P (probe))
1076 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1077 != NULL_TREE);
1079 return 0;
1082 /* DATA is really a struct lookup_field_info. Look for a field with
1083 the name indicated there in BINFO. If this function returns a
1084 non-NULL value it is the result of the lookup. Called from
1085 lookup_field via breadth_first_search. */
1087 static tree
1088 lookup_field_r (tree binfo, void *data)
1090 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1091 tree type = BINFO_TYPE (binfo);
1092 tree nval = NULL_TREE;
1094 /* If this is a dependent base, don't look in it. */
1095 if (BINFO_DEPENDENT_BASE_P (binfo))
1096 return NULL_TREE;
1098 /* If this base class is hidden by the best-known value so far, we
1099 don't need to look. */
1100 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1101 && !BINFO_VIRTUAL_P (binfo))
1102 return dfs_skip_bases;
1104 /* First, look for a function. There can't be a function and a data
1105 member with the same name, and if there's a function and a type
1106 with the same name, the type is hidden by the function. */
1107 if (!lfi->want_type)
1108 nval = lookup_fnfields_slot (type, lfi->name);
1110 if (!nval)
1111 /* Look for a data member or type. */
1112 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1113 else if (TREE_CODE (nval) == OVERLOAD && OVL_USED (nval))
1115 /* If we have both dependent and non-dependent using-declarations, return
1116 the dependent one rather than an incomplete list of functions. */
1117 tree dep_using = lookup_field_1 (type, lfi->name, lfi->want_type);
1118 if (dep_using && TREE_CODE (dep_using) == USING_DECL)
1119 nval = dep_using;
1122 /* If there is no declaration with the indicated name in this type,
1123 then there's nothing to do. */
1124 if (!nval)
1125 goto done;
1127 /* If we're looking up a type (as with an elaborated type specifier)
1128 we ignore all non-types we find. */
1129 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1131 if (lfi->name == TYPE_IDENTIFIER (type))
1133 /* If the aggregate has no user defined constructors, we allow
1134 it to have fields with the same name as the enclosing type.
1135 If we are looking for that name, find the corresponding
1136 TYPE_DECL. */
1137 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1138 if (DECL_NAME (nval) == lfi->name
1139 && TREE_CODE (nval) == TYPE_DECL)
1140 break;
1142 else
1143 nval = NULL_TREE;
1144 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1146 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1147 lfi->name);
1148 if (e != NULL)
1149 nval = TYPE_MAIN_DECL (e->type);
1150 else
1151 goto done;
1155 /* If the lookup already found a match, and the new value doesn't
1156 hide the old one, we might have an ambiguity. */
1157 if (lfi->rval_binfo
1158 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1161 if (nval == lfi->rval && shared_member_p (nval))
1162 /* The two things are really the same. */
1164 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1165 /* The previous value hides the new one. */
1167 else
1169 /* We have a real ambiguity. We keep a chain of all the
1170 candidates. */
1171 if (!lfi->ambiguous && lfi->rval)
1173 /* This is the first time we noticed an ambiguity. Add
1174 what we previously thought was a reasonable candidate
1175 to the list. */
1176 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1177 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1180 /* Add the new value. */
1181 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1182 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1183 lfi->errstr = G_("request for member %qD is ambiguous");
1186 else
1188 lfi->rval = nval;
1189 lfi->rval_binfo = binfo;
1192 done:
1193 /* Don't look for constructors or destructors in base classes. */
1194 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1195 return dfs_skip_bases;
1196 return NULL_TREE;
1199 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1200 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1201 FUNCTIONS, and OPTYPE respectively. */
1203 tree
1204 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1206 tree baselink;
1208 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1209 || TREE_CODE (functions) == TEMPLATE_DECL
1210 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1211 || TREE_CODE (functions) == OVERLOAD);
1212 gcc_assert (!optype || TYPE_P (optype));
1213 gcc_assert (TREE_TYPE (functions));
1215 baselink = make_node (BASELINK);
1216 TREE_TYPE (baselink) = TREE_TYPE (functions);
1217 BASELINK_BINFO (baselink) = binfo;
1218 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1219 BASELINK_FUNCTIONS (baselink) = functions;
1220 BASELINK_OPTYPE (baselink) = optype;
1222 return baselink;
1225 /* Look for a member named NAME in an inheritance lattice dominated by
1226 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1227 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1228 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1229 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1230 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1231 TREE_VALUEs are the list of ambiguous candidates.
1233 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1235 If nothing can be found return NULL_TREE and do not issue an error. */
1237 tree
1238 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1239 tsubst_flags_t complain)
1241 tree rval, rval_binfo = NULL_TREE;
1242 tree type = NULL_TREE, basetype_path = NULL_TREE;
1243 struct lookup_field_info lfi;
1245 /* rval_binfo is the binfo associated with the found member, note,
1246 this can be set with useful information, even when rval is not
1247 set, because it must deal with ALL members, not just non-function
1248 members. It is used for ambiguity checking and the hidden
1249 checks. Whereas rval is only set if a proper (not hidden)
1250 non-function member is found. */
1252 const char *errstr = 0;
1254 if (name == error_mark_node
1255 || xbasetype == NULL_TREE
1256 || xbasetype == error_mark_node)
1257 return NULL_TREE;
1259 gcc_assert (identifier_p (name));
1261 if (TREE_CODE (xbasetype) == TREE_BINFO)
1263 type = BINFO_TYPE (xbasetype);
1264 basetype_path = xbasetype;
1266 else
1268 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1269 return NULL_TREE;
1270 type = xbasetype;
1271 xbasetype = NULL_TREE;
1274 type = complete_type (type);
1276 /* Make sure we're looking for a member of the current instantiation in the
1277 right partial specialization. */
1278 if (flag_concepts && dependent_type_p (type))
1279 if (tree t = currently_open_class (type))
1280 type = t;
1282 if (!basetype_path)
1283 basetype_path = TYPE_BINFO (type);
1285 if (!basetype_path)
1286 return NULL_TREE;
1288 if (GATHER_STATISTICS)
1289 n_calls_lookup_field++;
1291 memset (&lfi, 0, sizeof (lfi));
1292 lfi.type = type;
1293 lfi.name = name;
1294 lfi.want_type = want_type;
1295 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1296 rval = lfi.rval;
1297 rval_binfo = lfi.rval_binfo;
1298 if (rval_binfo)
1299 type = BINFO_TYPE (rval_binfo);
1300 errstr = lfi.errstr;
1302 /* If we are not interested in ambiguities, don't report them;
1303 just return NULL_TREE. */
1304 if (!protect && lfi.ambiguous)
1305 return NULL_TREE;
1307 if (protect == 2)
1309 if (lfi.ambiguous)
1310 return lfi.ambiguous;
1311 else
1312 protect = 0;
1315 /* [class.access]
1317 In the case of overloaded function names, access control is
1318 applied to the function selected by overloaded resolution.
1320 We cannot check here, even if RVAL is only a single non-static
1321 member function, since we do not know what the "this" pointer
1322 will be. For:
1324 class A { protected: void f(); };
1325 class B : public A {
1326 void g(A *p) {
1327 f(); // OK
1328 p->f(); // Not OK.
1332 only the first call to "f" is valid. However, if the function is
1333 static, we can check. */
1334 if (rval && protect
1335 && !really_overloaded_fn (rval))
1337 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1338 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1339 && !perform_or_defer_access_check (basetype_path, decl, decl,
1340 complain))
1341 rval = error_mark_node;
1344 if (errstr && protect)
1346 if (complain & tf_error)
1348 error (errstr, name, type);
1349 if (lfi.ambiguous)
1350 print_candidates (lfi.ambiguous);
1352 rval = error_mark_node;
1355 if (rval && is_overloaded_fn (rval))
1356 rval = build_baselink (rval_binfo, basetype_path, rval,
1357 (IDENTIFIER_TYPENAME_P (name)
1358 ? TREE_TYPE (name): NULL_TREE));
1359 return rval;
1362 /* Helper class for lookup_member_fuzzy. */
1364 class lookup_field_fuzzy_info
1366 public:
1367 lookup_field_fuzzy_info (bool want_type_p) :
1368 m_want_type_p (want_type_p), m_candidates () {}
1370 void fuzzy_lookup_fnfields (tree type);
1371 void fuzzy_lookup_field (tree type);
1373 /* If true, we are looking for types, not data members. */
1374 bool m_want_type_p;
1375 /* The result: a vec of identifiers. */
1376 auto_vec<tree> m_candidates;
1379 /* Locate all methods within TYPE, append them to m_candidates. */
1381 void
1382 lookup_field_fuzzy_info::fuzzy_lookup_fnfields (tree type)
1384 vec<tree, va_gc> *method_vec;
1385 tree fn;
1386 size_t i;
1388 if (!CLASS_TYPE_P (type))
1389 return;
1391 method_vec = CLASSTYPE_METHOD_VEC (type);
1392 if (!method_vec)
1393 return;
1395 for (i = 0; vec_safe_iterate (method_vec, i, &fn); ++i)
1396 if (fn)
1397 m_candidates.safe_push (DECL_NAME (OVL_CURRENT (fn)));
1400 /* Locate all fields within TYPE, append them to m_candidates. */
1402 void
1403 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1405 if (!CLASS_TYPE_P (type))
1406 return;
1408 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1410 if (!m_want_type_p || DECL_DECLARES_TYPE_P (field))
1411 if (DECL_NAME (field))
1412 m_candidates.safe_push (DECL_NAME (field));
1417 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1418 DATA is really a lookup_field_fuzzy_info. Look for a field with
1419 the name indicated there in BINFO. Gathers pertinent identifiers into
1420 m_candidates. */
1422 static tree
1423 lookup_field_fuzzy_r (tree binfo, void *data)
1425 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1426 tree type = BINFO_TYPE (binfo);
1428 /* First, look for functions. */
1429 if (!lffi->m_want_type_p)
1430 lffi->fuzzy_lookup_fnfields (type);
1432 /* Look for data member and types. */
1433 lffi->fuzzy_lookup_field (type);
1435 return NULL_TREE;
1438 /* Like lookup_member, but try to find the closest match for NAME,
1439 rather than an exact match, and return an identifier (or NULL_TREE).
1440 Do not complain. */
1442 tree
1443 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1445 tree type = NULL_TREE, basetype_path = NULL_TREE;
1446 struct lookup_field_fuzzy_info lffi (want_type_p);
1448 /* rval_binfo is the binfo associated with the found member, note,
1449 this can be set with useful information, even when rval is not
1450 set, because it must deal with ALL members, not just non-function
1451 members. It is used for ambiguity checking and the hidden
1452 checks. Whereas rval is only set if a proper (not hidden)
1453 non-function member is found. */
1455 if (name == error_mark_node
1456 || xbasetype == NULL_TREE
1457 || xbasetype == error_mark_node)
1458 return NULL_TREE;
1460 gcc_assert (identifier_p (name));
1462 if (TREE_CODE (xbasetype) == TREE_BINFO)
1464 type = BINFO_TYPE (xbasetype);
1465 basetype_path = xbasetype;
1467 else
1469 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1470 return NULL_TREE;
1471 type = xbasetype;
1472 xbasetype = NULL_TREE;
1475 type = complete_type (type);
1477 /* Make sure we're looking for a member of the current instantiation in the
1478 right partial specialization. */
1479 if (flag_concepts && dependent_type_p (type))
1480 type = currently_open_class (type);
1482 if (!basetype_path)
1483 basetype_path = TYPE_BINFO (type);
1485 if (!basetype_path)
1486 return NULL_TREE;
1488 /* Populate lffi.m_candidates. */
1489 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1491 return find_closest_identifier (name, &lffi.m_candidates);
1494 /* Like lookup_member, except that if we find a function member we
1495 return NULL_TREE. */
1497 tree
1498 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1500 tree rval = lookup_member (xbasetype, name, protect, want_type,
1501 tf_warning_or_error);
1503 /* Ignore functions, but propagate the ambiguity list. */
1504 if (!error_operand_p (rval)
1505 && (rval && BASELINK_P (rval)))
1506 return NULL_TREE;
1508 return rval;
1511 /* Like lookup_member, except that if we find a non-function member we
1512 return NULL_TREE. */
1514 tree
1515 lookup_fnfields (tree xbasetype, tree name, int protect)
1517 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1518 tf_warning_or_error);
1520 /* Ignore non-functions, but propagate the ambiguity list. */
1521 if (!error_operand_p (rval)
1522 && (rval && !BASELINK_P (rval)))
1523 return NULL_TREE;
1525 return rval;
1528 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1529 corresponding to "operator TYPE ()", or -1 if there is no such
1530 operator. Only CLASS_TYPE itself is searched; this routine does
1531 not scan the base classes of CLASS_TYPE. */
1533 static int
1534 lookup_conversion_operator (tree class_type, tree type)
1536 int tpl_slot = -1;
1538 if (TYPE_HAS_CONVERSION (class_type))
1540 int i;
1541 tree fn;
1542 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1544 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1545 vec_safe_iterate (methods, i, &fn); ++i)
1547 /* All the conversion operators come near the beginning of
1548 the class. Therefore, if FN is not a conversion
1549 operator, there is no matching conversion operator in
1550 CLASS_TYPE. */
1551 fn = OVL_CURRENT (fn);
1552 if (!DECL_CONV_FN_P (fn))
1553 break;
1555 if (TREE_CODE (fn) == TEMPLATE_DECL)
1556 /* All the templated conversion functions are on the same
1557 slot, so remember it. */
1558 tpl_slot = i;
1559 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1560 return i;
1564 return tpl_slot;
1567 /* TYPE is a class type. Return the index of the fields within
1568 the method vector with name NAME, or -1 if no such field exists.
1569 Does not lazily declare implicitly-declared member functions. */
1571 static int
1572 lookup_fnfields_idx_nolazy (tree type, tree name)
1574 vec<tree, va_gc> *method_vec;
1575 tree fn;
1576 tree tmp;
1577 size_t i;
1579 if (!CLASS_TYPE_P (type))
1580 return -1;
1582 method_vec = CLASSTYPE_METHOD_VEC (type);
1583 if (!method_vec)
1584 return -1;
1586 if (GATHER_STATISTICS)
1587 n_calls_lookup_fnfields_1++;
1589 /* Constructors are first... */
1590 if (name == ctor_identifier)
1592 fn = CLASSTYPE_CONSTRUCTORS (type);
1593 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1595 /* and destructors are second. */
1596 if (name == dtor_identifier)
1598 fn = CLASSTYPE_DESTRUCTORS (type);
1599 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1601 if (IDENTIFIER_TYPENAME_P (name))
1602 return lookup_conversion_operator (type, TREE_TYPE (name));
1604 /* Skip the conversion operators. */
1605 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1606 vec_safe_iterate (method_vec, i, &fn);
1607 ++i)
1608 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1609 break;
1611 /* If the type is complete, use binary search. */
1612 if (COMPLETE_TYPE_P (type))
1614 int lo;
1615 int hi;
1617 lo = i;
1618 hi = method_vec->length ();
1619 while (lo < hi)
1621 i = (lo + hi) / 2;
1623 if (GATHER_STATISTICS)
1624 n_outer_fields_searched++;
1626 tmp = (*method_vec)[i];
1627 tmp = DECL_NAME (OVL_CURRENT (tmp));
1628 if (tmp > name)
1629 hi = i;
1630 else if (tmp < name)
1631 lo = i + 1;
1632 else
1633 return i;
1636 else
1637 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1639 if (GATHER_STATISTICS)
1640 n_outer_fields_searched++;
1641 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1642 return i;
1645 return -1;
1648 /* TYPE is a class type. Return the index of the fields within
1649 the method vector with name NAME, or -1 if no such field exists. */
1652 lookup_fnfields_1 (tree type, tree name)
1654 if (!CLASS_TYPE_P (type))
1655 return -1;
1657 if (COMPLETE_TYPE_P (type))
1659 if ((name == ctor_identifier
1660 || name == base_ctor_identifier
1661 || name == complete_ctor_identifier))
1663 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1664 lazily_declare_fn (sfk_constructor, type);
1665 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1666 lazily_declare_fn (sfk_copy_constructor, type);
1667 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1668 lazily_declare_fn (sfk_move_constructor, type);
1670 else if (name == cp_assignment_operator_id (NOP_EXPR))
1672 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1673 lazily_declare_fn (sfk_copy_assignment, type);
1674 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1675 lazily_declare_fn (sfk_move_assignment, type);
1677 else if ((name == dtor_identifier
1678 || name == base_dtor_identifier
1679 || name == complete_dtor_identifier
1680 || name == deleting_dtor_identifier)
1681 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1682 lazily_declare_fn (sfk_destructor, type);
1685 return lookup_fnfields_idx_nolazy (type, name);
1688 /* TYPE is a class type. Return the field within the method vector with
1689 name NAME, or NULL_TREE if no such field exists. */
1691 tree
1692 lookup_fnfields_slot (tree type, tree name)
1694 int ix = lookup_fnfields_1 (complete_type (type), name);
1695 if (ix < 0)
1696 return NULL_TREE;
1697 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1700 /* As above, but avoid lazily declaring functions. */
1702 tree
1703 lookup_fnfields_slot_nolazy (tree type, tree name)
1705 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1706 if (ix < 0)
1707 return NULL_TREE;
1708 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1711 /* Like lookup_fnfields_1, except that the name is extracted from
1712 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1715 class_method_index_for_fn (tree class_type, tree function)
1717 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1719 return lookup_fnfields_1 (class_type,
1720 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1721 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1722 DECL_NAME (function));
1726 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1727 the class or namespace used to qualify the name. CONTEXT_CLASS is
1728 the class corresponding to the object in which DECL will be used.
1729 Return a possibly modified version of DECL that takes into account
1730 the CONTEXT_CLASS.
1732 In particular, consider an expression like `B::m' in the context of
1733 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1734 then the most derived class indicated by the BASELINK_BINFO will be
1735 `B', not `D'. This function makes that adjustment. */
1737 tree
1738 adjust_result_of_qualified_name_lookup (tree decl,
1739 tree qualifying_scope,
1740 tree context_class)
1742 if (context_class && context_class != error_mark_node
1743 && CLASS_TYPE_P (context_class)
1744 && CLASS_TYPE_P (qualifying_scope)
1745 && DERIVED_FROM_P (qualifying_scope, context_class)
1746 && BASELINK_P (decl))
1748 tree base;
1750 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1751 Because we do not yet know which function will be chosen by
1752 overload resolution, we cannot yet check either accessibility
1753 or ambiguity -- in either case, the choice of a static member
1754 function might make the usage valid. */
1755 base = lookup_base (context_class, qualifying_scope,
1756 ba_unique, NULL, tf_none);
1757 if (base && base != error_mark_node)
1759 BASELINK_ACCESS_BINFO (decl) = base;
1760 tree decl_binfo
1761 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1762 ba_unique, NULL, tf_none);
1763 if (decl_binfo && decl_binfo != error_mark_node)
1764 BASELINK_BINFO (decl) = decl_binfo;
1768 if (BASELINK_P (decl))
1769 BASELINK_QUALIFIED_P (decl) = true;
1771 return decl;
1775 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1776 PRE_FN is called in preorder, while POST_FN is called in postorder.
1777 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1778 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1779 that value is immediately returned and the walk is terminated. One
1780 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1781 POST_FN are passed the binfo to examine and the caller's DATA
1782 value. All paths are walked, thus virtual and morally virtual
1783 binfos can be multiply walked. */
1785 tree
1786 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1787 tree (*post_fn) (tree, void *), void *data)
1789 tree rval;
1790 unsigned ix;
1791 tree base_binfo;
1793 /* Call the pre-order walking function. */
1794 if (pre_fn)
1796 rval = pre_fn (binfo, data);
1797 if (rval)
1799 if (rval == dfs_skip_bases)
1800 goto skip_bases;
1801 return rval;
1805 /* Find the next child binfo to walk. */
1806 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1808 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1809 if (rval)
1810 return rval;
1813 skip_bases:
1814 /* Call the post-order walking function. */
1815 if (post_fn)
1817 rval = post_fn (binfo, data);
1818 gcc_assert (rval != dfs_skip_bases);
1819 return rval;
1822 return NULL_TREE;
1825 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1826 that binfos are walked at most once. */
1828 static tree
1829 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1830 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1831 void *data)
1833 tree rval;
1834 unsigned ix;
1835 tree base_binfo;
1837 /* Call the pre-order walking function. */
1838 if (pre_fn)
1840 rval = pre_fn (binfo, data);
1841 if (rval)
1843 if (rval == dfs_skip_bases)
1844 goto skip_bases;
1846 return rval;
1850 /* Find the next child binfo to walk. */
1851 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1853 if (BINFO_VIRTUAL_P (base_binfo))
1854 if (pset->add (base_binfo))
1855 continue;
1857 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1858 if (rval)
1859 return rval;
1862 skip_bases:
1863 /* Call the post-order walking function. */
1864 if (post_fn)
1866 rval = post_fn (binfo, data);
1867 gcc_assert (rval != dfs_skip_bases);
1868 return rval;
1871 return NULL_TREE;
1874 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1875 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1876 For diamond shaped hierarchies we must mark the virtual bases, to
1877 avoid multiple walks. */
1879 tree
1880 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1881 tree (*post_fn) (tree, void *), void *data)
1883 static int active = 0; /* We must not be called recursively. */
1884 tree rval;
1886 gcc_assert (pre_fn || post_fn);
1887 gcc_assert (!active);
1888 active++;
1890 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1891 /* We are not diamond shaped, and therefore cannot encounter the
1892 same binfo twice. */
1893 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1894 else
1896 hash_set<tree> pset;
1897 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1900 active--;
1902 return rval;
1905 /* Worker function for dfs_walk_once_accessible. Behaves like
1906 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1907 access given by the current context should be considered, (b) ONCE
1908 indicates whether bases should be marked during traversal. */
1910 static tree
1911 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1912 tree (*pre_fn) (tree, void *),
1913 tree (*post_fn) (tree, void *), void *data)
1915 tree rval = NULL_TREE;
1916 unsigned ix;
1917 tree base_binfo;
1919 /* Call the pre-order walking function. */
1920 if (pre_fn)
1922 rval = pre_fn (binfo, data);
1923 if (rval)
1925 if (rval == dfs_skip_bases)
1926 goto skip_bases;
1928 return rval;
1932 /* Find the next child binfo to walk. */
1933 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1935 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1937 if (mark && pset->contains (base_binfo))
1938 continue;
1940 /* If the base is inherited via private or protected
1941 inheritance, then we can't see it, unless we are a friend of
1942 the current binfo. */
1943 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1945 tree scope;
1946 if (!friends_p)
1947 continue;
1948 scope = current_scope ();
1949 if (!scope
1950 || TREE_CODE (scope) == NAMESPACE_DECL
1951 || !is_friend (BINFO_TYPE (binfo), scope))
1952 continue;
1955 if (mark)
1956 pset->add (base_binfo);
1958 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1959 pre_fn, post_fn, data);
1960 if (rval)
1961 return rval;
1964 skip_bases:
1965 /* Call the post-order walking function. */
1966 if (post_fn)
1968 rval = post_fn (binfo, data);
1969 gcc_assert (rval != dfs_skip_bases);
1970 return rval;
1973 return NULL_TREE;
1976 /* Like dfs_walk_once except that only accessible bases are walked.
1977 FRIENDS_P indicates whether friendship of the local context
1978 should be considered when determining accessibility. */
1980 static tree
1981 dfs_walk_once_accessible (tree binfo, bool friends_p,
1982 tree (*pre_fn) (tree, void *),
1983 tree (*post_fn) (tree, void *), void *data)
1985 hash_set<tree> *pset = NULL;
1986 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1987 pset = new hash_set<tree>;
1988 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1989 pre_fn, post_fn, data);
1991 if (pset)
1992 delete pset;
1993 return rval;
1996 /* Check that virtual overrider OVERRIDER is acceptable for base function
1997 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1999 static int
2000 check_final_overrider (tree overrider, tree basefn)
2002 tree over_type = TREE_TYPE (overrider);
2003 tree base_type = TREE_TYPE (basefn);
2004 tree over_return = fndecl_declared_return_type (overrider);
2005 tree base_return = fndecl_declared_return_type (basefn);
2006 tree over_throw, base_throw;
2008 int fail = 0;
2010 if (DECL_INVALID_OVERRIDER_P (overrider))
2011 return 0;
2013 if (same_type_p (base_return, over_return))
2014 /* OK */;
2015 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
2016 || (TREE_CODE (base_return) == TREE_CODE (over_return)
2017 && POINTER_TYPE_P (base_return)))
2019 /* Potentially covariant. */
2020 unsigned base_quals, over_quals;
2022 fail = !POINTER_TYPE_P (base_return);
2023 if (!fail)
2025 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
2027 base_return = TREE_TYPE (base_return);
2028 over_return = TREE_TYPE (over_return);
2030 base_quals = cp_type_quals (base_return);
2031 over_quals = cp_type_quals (over_return);
2033 if ((base_quals & over_quals) != over_quals)
2034 fail = 1;
2036 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
2038 /* Strictly speaking, the standard requires the return type to be
2039 complete even if it only differs in cv-quals, but that seems
2040 like a bug in the wording. */
2041 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
2042 over_return))
2044 tree binfo = lookup_base (over_return, base_return,
2045 ba_check, NULL, tf_none);
2047 if (!binfo || binfo == error_mark_node)
2048 fail = 1;
2051 else if (can_convert_standard (TREE_TYPE (base_type),
2052 TREE_TYPE (over_type),
2053 tf_warning_or_error))
2054 /* GNU extension, allow trivial pointer conversions such as
2055 converting to void *, or qualification conversion. */
2057 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
2058 "invalid covariant return type for %q#D", overrider))
2059 inform (DECL_SOURCE_LOCATION (basefn),
2060 " overriding %q#D", basefn);
2062 else
2063 fail = 2;
2065 else
2066 fail = 2;
2067 if (!fail)
2068 /* OK */;
2069 else
2071 if (fail == 1)
2073 error ("invalid covariant return type for %q+#D", overrider);
2074 error (" overriding %q+#D", basefn);
2076 else
2078 error ("conflicting return type specified for %q+#D", overrider);
2079 error (" overriding %q+#D", basefn);
2081 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2082 return 0;
2085 /* Check throw specifier is at least as strict. */
2086 maybe_instantiate_noexcept (basefn);
2087 maybe_instantiate_noexcept (overrider);
2088 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
2089 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
2091 if (!comp_except_specs (base_throw, over_throw, ce_derived))
2093 error ("looser throw specifier for %q+#F", overrider);
2094 error (" overriding %q+#F", basefn);
2095 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2096 return 0;
2099 /* Check for conflicting type attributes. But leave transaction_safe for
2100 set_one_vmethod_tm_attributes. */
2101 if (!comp_type_attributes (over_type, base_type)
2102 && !tx_safe_fn_type_p (base_type)
2103 && !tx_safe_fn_type_p (over_type))
2105 error ("conflicting type attributes specified for %q+#D", overrider);
2106 error (" overriding %q+#D", basefn);
2107 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2108 return 0;
2111 /* A function declared transaction_safe_dynamic that overrides a function
2112 declared transaction_safe (but not transaction_safe_dynamic) is
2113 ill-formed. */
2114 if (tx_safe_fn_type_p (base_type)
2115 && lookup_attribute ("transaction_safe_dynamic",
2116 DECL_ATTRIBUTES (overrider))
2117 && !lookup_attribute ("transaction_safe_dynamic",
2118 DECL_ATTRIBUTES (basefn)))
2120 error_at (DECL_SOURCE_LOCATION (overrider),
2121 "%qD declared %<transaction_safe_dynamic%>", overrider);
2122 inform (DECL_SOURCE_LOCATION (basefn),
2123 "overriding %qD declared %<transaction_safe%>", basefn);
2126 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2128 if (DECL_DELETED_FN (overrider))
2130 error ("deleted function %q+D", overrider);
2131 error ("overriding non-deleted function %q+D", basefn);
2132 maybe_explain_implicit_delete (overrider);
2134 else
2136 error ("non-deleted function %q+D", overrider);
2137 error ("overriding deleted function %q+D", basefn);
2139 return 0;
2141 if (DECL_FINAL_P (basefn))
2143 error ("virtual function %q+D", overrider);
2144 error ("overriding final function %q+D", basefn);
2145 return 0;
2147 return 1;
2150 /* Given a class TYPE, and a function decl FNDECL, look for
2151 virtual functions in TYPE's hierarchy which FNDECL overrides.
2152 We do not look in TYPE itself, only its bases.
2154 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2155 find that it overrides anything.
2157 We check that every function which is overridden, is correctly
2158 overridden. */
2161 look_for_overrides (tree type, tree fndecl)
2163 tree binfo = TYPE_BINFO (type);
2164 tree base_binfo;
2165 int ix;
2166 int found = 0;
2168 /* A constructor for a class T does not override a function T
2169 in a base class. */
2170 if (DECL_CONSTRUCTOR_P (fndecl))
2171 return 0;
2173 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2175 tree basetype = BINFO_TYPE (base_binfo);
2177 if (TYPE_POLYMORPHIC_P (basetype))
2178 found += look_for_overrides_r (basetype, fndecl);
2180 return found;
2183 /* Look in TYPE for virtual functions with the same signature as
2184 FNDECL. */
2186 tree
2187 look_for_overrides_here (tree type, tree fndecl)
2189 int ix;
2191 /* If there are no methods in TYPE (meaning that only implicitly
2192 declared methods will ever be provided for TYPE), then there are
2193 no virtual functions. */
2194 if (!CLASSTYPE_METHOD_VEC (type))
2195 return NULL_TREE;
2197 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2198 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2199 else
2200 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2201 if (ix >= 0)
2203 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
2205 for (; fns; fns = OVL_NEXT (fns))
2207 tree fn = OVL_CURRENT (fns);
2209 if (!DECL_VIRTUAL_P (fn))
2210 /* Not a virtual. */;
2211 else if (DECL_CONTEXT (fn) != type)
2212 /* Introduced with a using declaration. */;
2213 else if (DECL_STATIC_FUNCTION_P (fndecl))
2215 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2216 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2217 if (compparms (TREE_CHAIN (btypes), dtypes))
2218 return fn;
2220 else if (same_signature_p (fndecl, fn))
2221 return fn;
2224 return NULL_TREE;
2227 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2228 TYPE itself and its bases. */
2230 static int
2231 look_for_overrides_r (tree type, tree fndecl)
2233 tree fn = look_for_overrides_here (type, fndecl);
2234 if (fn)
2236 if (DECL_STATIC_FUNCTION_P (fndecl))
2238 /* A static member function cannot match an inherited
2239 virtual member function. */
2240 error ("%q+#D cannot be declared", fndecl);
2241 error (" since %q+#D declared in base class", fn);
2243 else
2245 /* It's definitely virtual, even if not explicitly set. */
2246 DECL_VIRTUAL_P (fndecl) = 1;
2247 check_final_overrider (fndecl, fn);
2249 return 1;
2252 /* We failed to find one declared in this class. Look in its bases. */
2253 return look_for_overrides (type, fndecl);
2256 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2258 static tree
2259 dfs_get_pure_virtuals (tree binfo, void *data)
2261 tree type = (tree) data;
2263 /* We're not interested in primary base classes; the derived class
2264 of which they are a primary base will contain the information we
2265 need. */
2266 if (!BINFO_PRIMARY_P (binfo))
2268 tree virtuals;
2270 for (virtuals = BINFO_VIRTUALS (binfo);
2271 virtuals;
2272 virtuals = TREE_CHAIN (virtuals))
2273 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2274 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2277 return NULL_TREE;
2280 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2282 void
2283 get_pure_virtuals (tree type)
2285 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2286 is going to be overridden. */
2287 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2288 /* Now, run through all the bases which are not primary bases, and
2289 collect the pure virtual functions. We look at the vtable in
2290 each class to determine what pure virtual functions are present.
2291 (A primary base is not interesting because the derived class of
2292 which it is a primary base will contain vtable entries for the
2293 pure virtuals in the base class. */
2294 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2297 /* Debug info for C++ classes can get very large; try to avoid
2298 emitting it everywhere.
2300 Note that this optimization wins even when the target supports
2301 BINCL (if only slightly), and reduces the amount of work for the
2302 linker. */
2304 void
2305 maybe_suppress_debug_info (tree t)
2307 if (write_symbols == NO_DEBUG)
2308 return;
2310 /* We might have set this earlier in cp_finish_decl. */
2311 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2313 /* Always emit the information for each class every time. */
2314 if (flag_emit_class_debug_always)
2315 return;
2317 /* If we already know how we're handling this class, handle debug info
2318 the same way. */
2319 if (CLASSTYPE_INTERFACE_KNOWN (t))
2321 if (CLASSTYPE_INTERFACE_ONLY (t))
2322 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2323 /* else don't set it. */
2325 /* If the class has a vtable, write out the debug info along with
2326 the vtable. */
2327 else if (TYPE_CONTAINS_VPTR_P (t))
2328 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2330 /* Otherwise, just emit the debug info normally. */
2333 /* Note that we want debugging information for a base class of a class
2334 whose vtable is being emitted. Normally, this would happen because
2335 calling the constructor for a derived class implies calling the
2336 constructors for all bases, which involve initializing the
2337 appropriate vptr with the vtable for the base class; but in the
2338 presence of optimization, this initialization may be optimized
2339 away, so we tell finish_vtable_vardecl that we want the debugging
2340 information anyway. */
2342 static tree
2343 dfs_debug_mark (tree binfo, void * /*data*/)
2345 tree t = BINFO_TYPE (binfo);
2347 if (CLASSTYPE_DEBUG_REQUESTED (t))
2348 return dfs_skip_bases;
2350 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2352 return NULL_TREE;
2355 /* Write out the debugging information for TYPE, whose vtable is being
2356 emitted. Also walk through our bases and note that we want to
2357 write out information for them. This avoids the problem of not
2358 writing any debug info for intermediate basetypes whose
2359 constructors, and thus the references to their vtables, and thus
2360 the vtables themselves, were optimized away. */
2362 void
2363 note_debug_info_needed (tree type)
2365 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2367 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2368 rest_of_type_compilation (type, toplevel_bindings_p ());
2371 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2374 void
2375 print_search_statistics (void)
2377 if (! GATHER_STATISTICS)
2379 fprintf (stderr, "no search statistics\n");
2380 return;
2383 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2384 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2385 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2386 n_outer_fields_searched, n_calls_lookup_fnfields);
2387 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2390 void
2391 reinit_search_statistics (void)
2393 n_fields_searched = 0;
2394 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2395 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2396 n_calls_get_base_type = 0;
2397 n_outer_fields_searched = 0;
2398 n_contexts_saved = 0;
2401 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2402 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2403 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2404 bases have been encountered already in the tree walk. PARENT_CONVS
2405 is the list of lists of conversion functions that could hide CONV
2406 and OTHER_CONVS is the list of lists of conversion functions that
2407 could hide or be hidden by CONV, should virtualness be involved in
2408 the hierarchy. Merely checking the conversion op's name is not
2409 enough because two conversion operators to the same type can have
2410 different names. Return nonzero if we are visible. */
2412 static int
2413 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2414 tree to_type, tree parent_convs, tree other_convs)
2416 tree level, probe;
2418 /* See if we are hidden by a parent conversion. */
2419 for (level = parent_convs; level; level = TREE_CHAIN (level))
2420 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2421 if (same_type_p (to_type, TREE_TYPE (probe)))
2422 return 0;
2424 if (virtual_depth || virtualness)
2426 /* In a virtual hierarchy, we could be hidden, or could hide a
2427 conversion function on the other_convs list. */
2428 for (level = other_convs; level; level = TREE_CHAIN (level))
2430 int we_hide_them;
2431 int they_hide_us;
2432 tree *prev, other;
2434 if (!(virtual_depth || TREE_STATIC (level)))
2435 /* Neither is morally virtual, so cannot hide each other. */
2436 continue;
2438 if (!TREE_VALUE (level))
2439 /* They evaporated away already. */
2440 continue;
2442 they_hide_us = (virtual_depth
2443 && original_binfo (binfo, TREE_PURPOSE (level)));
2444 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2445 && original_binfo (TREE_PURPOSE (level), binfo));
2447 if (!(we_hide_them || they_hide_us))
2448 /* Neither is within the other, so no hiding can occur. */
2449 continue;
2451 for (prev = &TREE_VALUE (level), other = *prev; other;)
2453 if (same_type_p (to_type, TREE_TYPE (other)))
2455 if (they_hide_us)
2456 /* We are hidden. */
2457 return 0;
2459 if (we_hide_them)
2461 /* We hide the other one. */
2462 other = TREE_CHAIN (other);
2463 *prev = other;
2464 continue;
2467 prev = &TREE_CHAIN (other);
2468 other = *prev;
2472 return 1;
2475 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2476 of conversion functions, the first slot will be for the current
2477 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2478 of conversion functions from children of the current binfo,
2479 concatenated with conversions from elsewhere in the hierarchy --
2480 that list begins with OTHER_CONVS. Return a single list of lists
2481 containing only conversions from the current binfo and its
2482 children. */
2484 static tree
2485 split_conversions (tree my_convs, tree parent_convs,
2486 tree child_convs, tree other_convs)
2488 tree t;
2489 tree prev;
2491 /* Remove the original other_convs portion from child_convs. */
2492 for (prev = NULL, t = child_convs;
2493 t != other_convs; prev = t, t = TREE_CHAIN (t))
2494 continue;
2496 if (prev)
2497 TREE_CHAIN (prev) = NULL_TREE;
2498 else
2499 child_convs = NULL_TREE;
2501 /* Attach the child convs to any we had at this level. */
2502 if (my_convs)
2504 my_convs = parent_convs;
2505 TREE_CHAIN (my_convs) = child_convs;
2507 else
2508 my_convs = child_convs;
2510 return my_convs;
2513 /* Worker for lookup_conversions. Lookup conversion functions in
2514 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2515 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2516 encountered virtual bases already in the tree walk. PARENT_CONVS &
2517 PARENT_TPL_CONVS are lists of list of conversions within parent
2518 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2519 elsewhere in the tree. Return the conversions found within this
2520 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2521 encountered virtualness. We keep template and non-template
2522 conversions separate, to avoid unnecessary type comparisons.
2524 The located conversion functions are held in lists of lists. The
2525 TREE_VALUE of the outer list is the list of conversion functions
2526 found in a particular binfo. The TREE_PURPOSE of both the outer
2527 and inner lists is the binfo at which those conversions were
2528 found. TREE_STATIC is set for those lists within of morally
2529 virtual binfos. The TREE_VALUE of the inner list is the conversion
2530 function or overload itself. The TREE_TYPE of each inner list node
2531 is the converted-to type. */
2533 static int
2534 lookup_conversions_r (tree binfo,
2535 int virtual_depth, int virtualness,
2536 tree parent_convs, tree parent_tpl_convs,
2537 tree other_convs, tree other_tpl_convs,
2538 tree *convs, tree *tpl_convs)
2540 int my_virtualness = 0;
2541 tree my_convs = NULL_TREE;
2542 tree my_tpl_convs = NULL_TREE;
2543 tree child_convs = NULL_TREE;
2544 tree child_tpl_convs = NULL_TREE;
2545 unsigned i;
2546 tree base_binfo;
2547 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2548 tree conv;
2550 /* If we have no conversion operators, then don't look. */
2551 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2553 *convs = *tpl_convs = NULL_TREE;
2555 return 0;
2558 if (BINFO_VIRTUAL_P (binfo))
2559 virtual_depth++;
2561 /* First, locate the unhidden ones at this level. */
2562 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2563 vec_safe_iterate (method_vec, i, &conv);
2564 ++i)
2566 tree cur = OVL_CURRENT (conv);
2568 if (!DECL_CONV_FN_P (cur))
2569 break;
2571 if (TREE_CODE (cur) == TEMPLATE_DECL)
2573 /* Only template conversions can be overloaded, and we must
2574 flatten them out and check each one individually. */
2575 tree tpls;
2577 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2579 tree tpl = OVL_CURRENT (tpls);
2580 tree type = DECL_CONV_FN_TYPE (tpl);
2582 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2583 type, parent_tpl_convs, other_tpl_convs))
2585 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2586 TREE_TYPE (my_tpl_convs) = type;
2587 if (virtual_depth)
2589 TREE_STATIC (my_tpl_convs) = 1;
2590 my_virtualness = 1;
2595 else
2597 tree name = DECL_NAME (cur);
2599 if (!IDENTIFIER_MARKED (name))
2601 tree type = DECL_CONV_FN_TYPE (cur);
2602 if (type_uses_auto (type))
2604 mark_used (cur);
2605 type = DECL_CONV_FN_TYPE (cur);
2608 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2609 type, parent_convs, other_convs))
2611 my_convs = tree_cons (binfo, conv, my_convs);
2612 TREE_TYPE (my_convs) = type;
2613 if (virtual_depth)
2615 TREE_STATIC (my_convs) = 1;
2616 my_virtualness = 1;
2618 IDENTIFIER_MARKED (name) = 1;
2624 if (my_convs)
2626 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2627 if (virtual_depth)
2628 TREE_STATIC (parent_convs) = 1;
2631 if (my_tpl_convs)
2633 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2634 if (virtual_depth)
2635 TREE_STATIC (parent_tpl_convs) = 1;
2638 child_convs = other_convs;
2639 child_tpl_convs = other_tpl_convs;
2641 /* Now iterate over each base, looking for more conversions. */
2642 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2644 tree base_convs, base_tpl_convs;
2645 unsigned base_virtualness;
2647 base_virtualness = lookup_conversions_r (base_binfo,
2648 virtual_depth, virtualness,
2649 parent_convs, parent_tpl_convs,
2650 child_convs, child_tpl_convs,
2651 &base_convs, &base_tpl_convs);
2652 if (base_virtualness)
2653 my_virtualness = virtualness = 1;
2654 child_convs = chainon (base_convs, child_convs);
2655 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2658 /* Unmark the conversions found at this level */
2659 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2660 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2662 *convs = split_conversions (my_convs, parent_convs,
2663 child_convs, other_convs);
2664 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2665 child_tpl_convs, other_tpl_convs);
2667 return my_virtualness;
2670 /* Return a TREE_LIST containing all the non-hidden user-defined
2671 conversion functions for TYPE (and its base-classes). The
2672 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2673 function. The TREE_PURPOSE is the BINFO from which the conversion
2674 functions in this node were selected. This function is effectively
2675 performing a set of member lookups as lookup_fnfield does, but
2676 using the type being converted to as the unique key, rather than the
2677 field name. */
2679 tree
2680 lookup_conversions (tree type)
2682 tree convs, tpl_convs;
2683 tree list = NULL_TREE;
2685 complete_type (type);
2686 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2687 return NULL_TREE;
2689 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2690 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2691 &convs, &tpl_convs);
2693 /* Flatten the list-of-lists */
2694 for (; convs; convs = TREE_CHAIN (convs))
2696 tree probe, next;
2698 for (probe = TREE_VALUE (convs); probe; probe = next)
2700 next = TREE_CHAIN (probe);
2702 TREE_CHAIN (probe) = list;
2703 list = probe;
2707 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2709 tree probe, next;
2711 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2713 next = TREE_CHAIN (probe);
2715 TREE_CHAIN (probe) = list;
2716 list = probe;
2720 return list;
2723 /* Returns the binfo of the first direct or indirect virtual base derived
2724 from BINFO, or NULL if binfo is not via virtual. */
2726 tree
2727 binfo_from_vbase (tree binfo)
2729 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2731 if (BINFO_VIRTUAL_P (binfo))
2732 return binfo;
2734 return NULL_TREE;
2737 /* Returns the binfo of the first direct or indirect virtual base derived
2738 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2739 via virtual. */
2741 tree
2742 binfo_via_virtual (tree binfo, tree limit)
2744 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2745 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2746 return NULL_TREE;
2748 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2749 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2751 if (BINFO_VIRTUAL_P (binfo))
2752 return binfo;
2754 return NULL_TREE;
2757 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2758 Find the equivalent binfo within whatever graph HERE is located.
2759 This is the inverse of original_binfo. */
2761 tree
2762 copied_binfo (tree binfo, tree here)
2764 tree result = NULL_TREE;
2766 if (BINFO_VIRTUAL_P (binfo))
2768 tree t;
2770 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2771 t = BINFO_INHERITANCE_CHAIN (t))
2772 continue;
2774 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2776 else if (BINFO_INHERITANCE_CHAIN (binfo))
2778 tree cbinfo;
2779 tree base_binfo;
2780 int ix;
2782 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2783 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2784 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2786 result = base_binfo;
2787 break;
2790 else
2792 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2793 result = here;
2796 gcc_assert (result);
2797 return result;
2800 tree
2801 binfo_for_vbase (tree base, tree t)
2803 unsigned ix;
2804 tree binfo;
2805 vec<tree, va_gc> *vbases;
2807 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2808 vec_safe_iterate (vbases, ix, &binfo); ix++)
2809 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2810 return binfo;
2811 return NULL;
2814 /* BINFO is some base binfo of HERE, within some other
2815 hierarchy. Return the equivalent binfo, but in the hierarchy
2816 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2817 is not a base binfo of HERE, returns NULL_TREE. */
2819 tree
2820 original_binfo (tree binfo, tree here)
2822 tree result = NULL;
2824 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2825 result = here;
2826 else if (BINFO_VIRTUAL_P (binfo))
2827 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2828 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2829 : NULL_TREE);
2830 else if (BINFO_INHERITANCE_CHAIN (binfo))
2832 tree base_binfos;
2834 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2835 if (base_binfos)
2837 int ix;
2838 tree base_binfo;
2840 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2841 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2842 BINFO_TYPE (binfo)))
2844 result = base_binfo;
2845 break;
2850 return result;
2853 /* True iff TYPE has any dependent bases (and therefore we can't say
2854 definitively that another class is not a base of an instantiation of
2855 TYPE). */
2857 bool
2858 any_dependent_bases_p (tree type)
2860 if (!type || !CLASS_TYPE_P (type) || !processing_template_decl)
2861 return false;
2863 unsigned i;
2864 tree base_binfo;
2865 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2866 if (BINFO_DEPENDENT_BASE_P (base_binfo))
2867 return true;
2869 return false;