gcc/ChangeLog
[official-gcc.git] / gcc / cp / search.c
blob42db122e98c73932bcf5d1591a4296ee2d9e5d50
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "alias.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "intl.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "target.h"
36 static int is_subobject_of_p (tree, tree);
37 static tree dfs_lookup_base (tree, void *);
38 static tree dfs_dcast_hint_pre (tree, void *);
39 static tree dfs_dcast_hint_post (tree, void *);
40 static tree dfs_debug_mark (tree, void *);
41 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
42 tree (*post_fn) (tree, void *), void *data);
43 static void dfs_unmark_r (tree);
44 static int check_hidden_convs (tree, int, int, tree, tree, tree);
45 static tree split_conversions (tree, tree, tree, tree);
46 static int lookup_conversions_r (tree, int, int,
47 tree, tree, tree, tree, tree *, tree *);
48 static int look_for_overrides_r (tree, tree);
49 static tree lookup_field_r (tree, void *);
50 static tree dfs_accessible_post (tree, void *);
51 static tree dfs_walk_once_accessible_r (tree, bool, bool,
52 tree (*pre_fn) (tree, void *),
53 tree (*post_fn) (tree, void *),
54 void *data);
55 static tree dfs_walk_once_accessible (tree, bool,
56 tree (*pre_fn) (tree, void *),
57 tree (*post_fn) (tree, void *),
58 void *data);
59 static tree dfs_access_in_type (tree, void *);
60 static access_kind access_in_type (tree, tree);
61 static tree dfs_get_pure_virtuals (tree, void *);
64 /* Variables for gathering statistics. */
65 static int n_fields_searched;
66 static int n_calls_lookup_field, n_calls_lookup_field_1;
67 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
68 static int n_calls_get_base_type;
69 static int n_outer_fields_searched;
70 static int n_contexts_saved;
73 /* Data for lookup_base and its workers. */
75 struct lookup_base_data_s
77 tree t; /* type being searched. */
78 tree base; /* The base type we're looking for. */
79 tree binfo; /* Found binfo. */
80 bool via_virtual; /* Found via a virtual path. */
81 bool ambiguous; /* Found multiply ambiguous */
82 bool repeated_base; /* Whether there are repeated bases in the
83 hierarchy. */
84 bool want_any; /* Whether we want any matching binfo. */
87 /* Worker function for lookup_base. See if we've found the desired
88 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
90 static tree
91 dfs_lookup_base (tree binfo, void *data_)
93 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
95 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
97 if (!data->binfo)
99 data->binfo = binfo;
100 data->via_virtual
101 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
103 if (!data->repeated_base)
104 /* If there are no repeated bases, we can stop now. */
105 return binfo;
107 if (data->want_any && !data->via_virtual)
108 /* If this is a non-virtual base, then we can't do
109 better. */
110 return binfo;
112 return dfs_skip_bases;
114 else
116 gcc_assert (binfo != data->binfo);
118 /* We've found more than one matching binfo. */
119 if (!data->want_any)
121 /* This is immediately ambiguous. */
122 data->binfo = NULL_TREE;
123 data->ambiguous = true;
124 return error_mark_node;
127 /* Prefer one via a non-virtual path. */
128 if (!binfo_via_virtual (binfo, data->t))
130 data->binfo = binfo;
131 data->via_virtual = false;
132 return binfo;
135 /* There must be repeated bases, otherwise we'd have stopped
136 on the first base we found. */
137 return dfs_skip_bases;
141 return NULL_TREE;
144 /* Returns true if type BASE is accessible in T. (BASE is known to be
145 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
146 true, consider any special access of the current scope, or access
147 bestowed by friendship. */
149 bool
150 accessible_base_p (tree t, tree base, bool consider_local_p)
152 tree decl;
154 /* [class.access.base]
156 A base class is said to be accessible if an invented public
157 member of the base class is accessible.
159 If BASE is a non-proper base, this condition is trivially
160 true. */
161 if (same_type_p (t, base))
162 return true;
163 /* Rather than inventing a public member, we use the implicit
164 public typedef created in the scope of every class. */
165 decl = TYPE_FIELDS (base);
166 while (!DECL_SELF_REFERENCE_P (decl))
167 decl = DECL_CHAIN (decl);
168 while (ANON_AGGR_TYPE_P (t))
169 t = TYPE_CONTEXT (t);
170 return accessible_p (t, decl, consider_local_p);
173 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
174 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
175 non-NULL, fill with information about what kind of base we
176 discovered.
178 If the base is inaccessible, or ambiguous, then error_mark_node is
179 returned. If the tf_error bit of COMPLAIN is not set, no error
180 is issued. */
182 tree
183 lookup_base (tree t, tree base, base_access access,
184 base_kind *kind_ptr, tsubst_flags_t complain)
186 tree binfo;
187 tree t_binfo;
188 base_kind bk;
190 /* "Nothing" is definitely not derived from Base. */
191 if (t == NULL_TREE)
193 if (kind_ptr)
194 *kind_ptr = bk_not_base;
195 return NULL_TREE;
198 if (t == error_mark_node || base == error_mark_node)
200 if (kind_ptr)
201 *kind_ptr = bk_not_base;
202 return error_mark_node;
204 gcc_assert (TYPE_P (base));
206 if (!TYPE_P (t))
208 t_binfo = t;
209 t = BINFO_TYPE (t);
211 else
213 t = complete_type (TYPE_MAIN_VARIANT (t));
214 t_binfo = TYPE_BINFO (t);
217 base = TYPE_MAIN_VARIANT (base);
219 /* If BASE is incomplete, it can't be a base of T--and instantiating it
220 might cause an error. */
221 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
223 struct lookup_base_data_s data;
225 data.t = t;
226 data.base = base;
227 data.binfo = NULL_TREE;
228 data.ambiguous = data.via_virtual = false;
229 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
230 data.want_any = access == ba_any;
232 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
233 binfo = data.binfo;
235 if (!binfo)
236 bk = data.ambiguous ? bk_ambig : bk_not_base;
237 else if (binfo == t_binfo)
238 bk = bk_same_type;
239 else if (data.via_virtual)
240 bk = bk_via_virtual;
241 else
242 bk = bk_proper_base;
244 else
246 binfo = NULL_TREE;
247 bk = bk_not_base;
250 /* Check that the base is unambiguous and accessible. */
251 if (access != ba_any)
252 switch (bk)
254 case bk_not_base:
255 break;
257 case bk_ambig:
258 if (complain & tf_error)
259 error ("%qT is an ambiguous base of %qT", base, t);
260 binfo = error_mark_node;
261 break;
263 default:
264 if ((access & ba_check_bit)
265 /* If BASE is incomplete, then BASE and TYPE are probably
266 the same, in which case BASE is accessible. If they
267 are not the same, then TYPE is invalid. In that case,
268 there's no need to issue another error here, and
269 there's no implicit typedef to use in the code that
270 follows, so we skip the check. */
271 && COMPLETE_TYPE_P (base)
272 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
274 if (complain & tf_error)
275 error ("%qT is an inaccessible base of %qT", base, t);
276 binfo = error_mark_node;
277 bk = bk_inaccessible;
279 break;
282 if (kind_ptr)
283 *kind_ptr = bk;
285 return binfo;
288 /* Data for dcast_base_hint walker. */
290 struct dcast_data_s
292 tree subtype; /* The base type we're looking for. */
293 int virt_depth; /* Number of virtual bases encountered from most
294 derived. */
295 tree offset; /* Best hint offset discovered so far. */
296 bool repeated_base; /* Whether there are repeated bases in the
297 hierarchy. */
300 /* Worker for dcast_base_hint. Search for the base type being cast
301 from. */
303 static tree
304 dfs_dcast_hint_pre (tree binfo, void *data_)
306 struct dcast_data_s *data = (struct dcast_data_s *) data_;
308 if (BINFO_VIRTUAL_P (binfo))
309 data->virt_depth++;
311 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
313 if (data->virt_depth)
315 data->offset = ssize_int (-1);
316 return data->offset;
318 if (data->offset)
319 data->offset = ssize_int (-3);
320 else
321 data->offset = BINFO_OFFSET (binfo);
323 return data->repeated_base ? dfs_skip_bases : data->offset;
326 return NULL_TREE;
329 /* Worker for dcast_base_hint. Track the virtual depth. */
331 static tree
332 dfs_dcast_hint_post (tree binfo, void *data_)
334 struct dcast_data_s *data = (struct dcast_data_s *) data_;
336 if (BINFO_VIRTUAL_P (binfo))
337 data->virt_depth--;
339 return NULL_TREE;
342 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
343 started from is related to the required TARGET type, in order to optimize
344 the inheritance graph search. This information is independent of the
345 current context, and ignores private paths, hence get_base_distance is
346 inappropriate. Return a TREE specifying the base offset, BOFF.
347 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
348 and there are no public virtual SUBTYPE bases.
349 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
350 BOFF == -2, SUBTYPE is not a public base.
351 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
353 tree
354 dcast_base_hint (tree subtype, tree target)
356 struct dcast_data_s data;
358 data.subtype = subtype;
359 data.virt_depth = 0;
360 data.offset = NULL_TREE;
361 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
363 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
364 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
365 return data.offset ? data.offset : ssize_int (-2);
368 /* Search for a member with name NAME in a multiple inheritance
369 lattice specified by TYPE. If it does not exist, return NULL_TREE.
370 If the member is ambiguously referenced, return `error_mark_node'.
371 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
372 true, type declarations are preferred. */
374 /* Do a 1-level search for NAME as a member of TYPE. The caller must
375 figure out whether it can access this field. (Since it is only one
376 level, this is reasonable.) */
378 tree
379 lookup_field_1 (tree type, tree name, bool want_type)
381 tree field;
383 gcc_assert (identifier_p (name));
385 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
386 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
387 || TREE_CODE (type) == TYPENAME_TYPE)
388 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
389 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
390 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
391 the code often worked even when we treated the index as a list
392 of fields!)
393 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
394 return NULL_TREE;
396 if (CLASSTYPE_SORTED_FIELDS (type))
398 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
399 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
400 int i;
402 while (lo < hi)
404 i = (lo + hi) / 2;
406 if (GATHER_STATISTICS)
407 n_fields_searched++;
409 if (DECL_NAME (fields[i]) > name)
410 hi = i;
411 else if (DECL_NAME (fields[i]) < name)
412 lo = i + 1;
413 else
415 field = NULL_TREE;
417 /* We might have a nested class and a field with the
418 same name; we sorted them appropriately via
419 field_decl_cmp, so just look for the first or last
420 field with this name. */
421 if (want_type)
424 field = fields[i--];
425 while (i >= lo && DECL_NAME (fields[i]) == name);
426 if (!DECL_DECLARES_TYPE_P (field))
427 field = NULL_TREE;
429 else
432 field = fields[i++];
433 while (i < hi && DECL_NAME (fields[i]) == name);
436 if (field)
438 field = strip_using_decl (field);
439 if (is_overloaded_fn (field))
440 field = NULL_TREE;
443 return field;
446 return NULL_TREE;
449 field = TYPE_FIELDS (type);
451 if (GATHER_STATISTICS)
452 n_calls_lookup_field_1++;
454 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
456 tree decl = field;
458 if (GATHER_STATISTICS)
459 n_fields_searched++;
461 gcc_assert (DECL_P (field));
462 if (DECL_NAME (field) == NULL_TREE
463 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
465 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
466 if (temp)
467 return temp;
470 if (TREE_CODE (decl) == USING_DECL
471 && DECL_NAME (decl) == name)
473 decl = strip_using_decl (decl);
474 if (is_overloaded_fn (decl))
475 continue;
478 if (DECL_NAME (decl) == name
479 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
480 return decl;
482 /* Not found. */
483 if (name == vptr_identifier)
485 /* Give the user what s/he thinks s/he wants. */
486 if (TYPE_POLYMORPHIC_P (type))
487 return TYPE_VFIELD (type);
489 return NULL_TREE;
492 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
493 NAMESPACE_DECL corresponding to the innermost non-block scope. */
495 tree
496 current_scope (void)
498 /* There are a number of cases we need to be aware of here:
499 current_class_type current_function_decl
500 global NULL NULL
501 fn-local NULL SET
502 class-local SET NULL
503 class->fn SET SET
504 fn->class SET SET
506 Those last two make life interesting. If we're in a function which is
507 itself inside a class, we need decls to go into the fn's decls (our
508 second case below). But if we're in a class and the class itself is
509 inside a function, we need decls to go into the decls for the class. To
510 achieve this last goal, we must see if, when both current_class_ptr and
511 current_function_decl are set, the class was declared inside that
512 function. If so, we know to put the decls into the class's scope. */
513 if (current_function_decl && current_class_type
514 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
515 && same_type_p (DECL_CONTEXT (current_function_decl),
516 current_class_type))
517 || (DECL_FRIEND_CONTEXT (current_function_decl)
518 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
519 current_class_type))))
520 return current_function_decl;
521 if (current_class_type)
522 return current_class_type;
523 if (current_function_decl)
524 return current_function_decl;
525 return current_namespace;
528 /* Returns nonzero if we are currently in a function scope. Note
529 that this function returns zero if we are within a local class, but
530 not within a member function body of the local class. */
533 at_function_scope_p (void)
535 tree cs = current_scope ();
536 /* Also check cfun to make sure that we're really compiling
537 this function (as opposed to having set current_function_decl
538 for access checking or some such). */
539 return (cs && TREE_CODE (cs) == FUNCTION_DECL
540 && cfun && cfun->decl == current_function_decl);
543 /* Returns true if the innermost active scope is a class scope. */
545 bool
546 at_class_scope_p (void)
548 tree cs = current_scope ();
549 return cs && TYPE_P (cs);
552 /* Returns true if the innermost active scope is a namespace scope. */
554 bool
555 at_namespace_scope_p (void)
557 tree cs = current_scope ();
558 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
561 /* Return the scope of DECL, as appropriate when doing name-lookup. */
563 tree
564 context_for_name_lookup (tree decl)
566 /* [class.union]
568 For the purposes of name lookup, after the anonymous union
569 definition, the members of the anonymous union are considered to
570 have been defined in the scope in which the anonymous union is
571 declared. */
572 tree context = DECL_CONTEXT (decl);
574 while (context && TYPE_P (context)
575 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
576 context = TYPE_CONTEXT (context);
577 if (!context)
578 context = global_namespace;
580 return context;
583 /* Returns true iff DECL is declared in TYPE. */
585 static bool
586 member_declared_in_type (tree decl, tree type)
588 /* A normal declaration obviously counts. */
589 if (context_for_name_lookup (decl) == type)
590 return true;
591 /* So does a using or access declaration. */
592 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
593 && purpose_member (type, DECL_ACCESS (decl)))
594 return true;
595 return false;
598 /* The accessibility routines use BINFO_ACCESS for scratch space
599 during the computation of the accessibility of some declaration. */
601 /* Avoid walking up past a declaration of the member. */
603 static tree
604 dfs_access_in_type_pre (tree binfo, void *data)
606 tree decl = (tree) data;
607 tree type = BINFO_TYPE (binfo);
608 if (member_declared_in_type (decl, type))
609 return dfs_skip_bases;
610 return NULL_TREE;
613 #define BINFO_ACCESS(NODE) \
614 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
616 /* Set the access associated with NODE to ACCESS. */
618 #define SET_BINFO_ACCESS(NODE, ACCESS) \
619 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
620 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
622 /* Called from access_in_type via dfs_walk. Calculate the access to
623 DATA (which is really a DECL) in BINFO. */
625 static tree
626 dfs_access_in_type (tree binfo, void *data)
628 tree decl = (tree) data;
629 tree type = BINFO_TYPE (binfo);
630 access_kind access = ak_none;
632 if (context_for_name_lookup (decl) == type)
634 /* If we have descended to the scope of DECL, just note the
635 appropriate access. */
636 if (TREE_PRIVATE (decl))
637 access = ak_private;
638 else if (TREE_PROTECTED (decl))
639 access = ak_protected;
640 else
641 access = ak_public;
643 else
645 /* First, check for an access-declaration that gives us more
646 access to the DECL. */
647 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
649 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
651 if (decl_access)
653 decl_access = TREE_VALUE (decl_access);
655 if (decl_access == access_public_node)
656 access = ak_public;
657 else if (decl_access == access_protected_node)
658 access = ak_protected;
659 else if (decl_access == access_private_node)
660 access = ak_private;
661 else
662 gcc_unreachable ();
666 if (!access)
668 int i;
669 tree base_binfo;
670 vec<tree, va_gc> *accesses;
672 /* Otherwise, scan our baseclasses, and pick the most favorable
673 access. */
674 accesses = BINFO_BASE_ACCESSES (binfo);
675 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
677 tree base_access = (*accesses)[i];
678 access_kind base_access_now = BINFO_ACCESS (base_binfo);
680 if (base_access_now == ak_none || base_access_now == ak_private)
681 /* If it was not accessible in the base, or only
682 accessible as a private member, we can't access it
683 all. */
684 base_access_now = ak_none;
685 else if (base_access == access_protected_node)
686 /* Public and protected members in the base become
687 protected here. */
688 base_access_now = ak_protected;
689 else if (base_access == access_private_node)
690 /* Public and protected members in the base become
691 private here. */
692 base_access_now = ak_private;
694 /* See if the new access, via this base, gives more
695 access than our previous best access. */
696 if (base_access_now != ak_none
697 && (access == ak_none || base_access_now < access))
699 access = base_access_now;
701 /* If the new access is public, we can't do better. */
702 if (access == ak_public)
703 break;
709 /* Note the access to DECL in TYPE. */
710 SET_BINFO_ACCESS (binfo, access);
712 return NULL_TREE;
715 /* Return the access to DECL in TYPE. */
717 static access_kind
718 access_in_type (tree type, tree decl)
720 tree binfo = TYPE_BINFO (type);
722 /* We must take into account
724 [class.paths]
726 If a name can be reached by several paths through a multiple
727 inheritance graph, the access is that of the path that gives
728 most access.
730 The algorithm we use is to make a post-order depth-first traversal
731 of the base-class hierarchy. As we come up the tree, we annotate
732 each node with the most lenient access. */
733 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
735 return BINFO_ACCESS (binfo);
738 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
739 of OTYPE in the context of DERIVED. */
741 static int
742 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
744 /* We're checking this clause from [class.access.base]
746 m as a member of N is protected, and the reference occurs in a
747 member or friend of class N, or in a member or friend of a
748 class P derived from N, where m as a member of P is public, private
749 or protected.
751 Here DERIVED is a possible P, DECL is m and TYPE is N. */
753 /* If DERIVED isn't derived from N, then it can't be a P. */
754 if (!DERIVED_FROM_P (type, derived))
755 return 0;
757 /* [class.protected]
759 When a friend or a member function of a derived class references
760 a protected nonstatic member of a base class, an access check
761 applies in addition to those described earlier in clause
762 _class.access_) Except when forming a pointer to member
763 (_expr.unary.op_), the access must be through a pointer to,
764 reference to, or object of the derived class itself (or any class
765 derived from that class) (_expr.ref_). If the access is to form
766 a pointer to member, the nested-name-specifier shall name the
767 derived class (or any class derived from that class). */
768 if (DECL_NONSTATIC_MEMBER_P (decl)
769 && !DERIVED_FROM_P (derived, otype))
770 return 0;
772 return 1;
775 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
776 to access DECL through TYPE. OTYPE is the type of the object. */
778 static int
779 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
781 /* We're checking this clause from [class.access.base]
783 m as a member of N is protected, and the reference occurs in a
784 member or friend of class N, or in a member or friend of a
785 class P derived from N, where m as a member of P is public, private
786 or protected.
788 Here DECL is m and TYPE is N. SCOPE is the current context,
789 and we check all its possible Ps. */
790 tree befriending_classes;
791 tree t;
793 if (!scope)
794 return 0;
796 /* Is SCOPE itself a suitable P? */
797 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
798 return 1;
800 if (DECL_DECLARES_FUNCTION_P (scope))
801 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
802 else if (TYPE_P (scope))
803 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
804 else
805 return 0;
807 for (t = befriending_classes; t; t = TREE_CHAIN (t))
808 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
809 return 1;
811 /* Nested classes have the same access as their enclosing types, as
812 per DR 45 (this is a change from C++98). */
813 if (TYPE_P (scope))
814 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
815 return 1;
817 if (DECL_DECLARES_FUNCTION_P (scope))
819 /* Perhaps this SCOPE is a member of a class which is a
820 friend. */
821 if (DECL_CLASS_SCOPE_P (scope)
822 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
823 return 1;
826 /* Maybe scope's template is a friend. */
827 if (tree tinfo = get_template_info (scope))
829 tree tmpl = TI_TEMPLATE (tinfo);
830 if (DECL_CLASS_TEMPLATE_P (tmpl))
831 tmpl = TREE_TYPE (tmpl);
832 else
833 tmpl = DECL_TEMPLATE_RESULT (tmpl);
834 if (tmpl != scope)
836 /* Increment processing_template_decl to make sure that
837 dependent_type_p works correctly. */
838 ++processing_template_decl;
839 int ret = friend_accessible_p (tmpl, decl, type, otype);
840 --processing_template_decl;
841 if (ret)
842 return 1;
846 /* If is_friend is true, we should have found a befriending class. */
847 gcc_checking_assert (!is_friend (type, scope));
849 return 0;
852 struct dfs_accessible_data
854 tree decl;
855 tree object_type;
858 /* Avoid walking up past a declaration of the member. */
860 static tree
861 dfs_accessible_pre (tree binfo, void *data)
863 dfs_accessible_data *d = (dfs_accessible_data *)data;
864 tree type = BINFO_TYPE (binfo);
865 if (member_declared_in_type (d->decl, type))
866 return dfs_skip_bases;
867 return NULL_TREE;
870 /* Called via dfs_walk_once_accessible from accessible_p */
872 static tree
873 dfs_accessible_post (tree binfo, void *data)
875 /* access_in_type already set BINFO_ACCESS for us. */
876 access_kind access = BINFO_ACCESS (binfo);
877 tree N = BINFO_TYPE (binfo);
878 dfs_accessible_data *d = (dfs_accessible_data *)data;
879 tree decl = d->decl;
880 tree scope = current_nonlambda_scope ();
882 /* A member m is accessible at the point R when named in class N if */
883 switch (access)
885 case ak_none:
886 return NULL_TREE;
888 case ak_public:
889 /* m as a member of N is public, or */
890 return binfo;
892 case ak_private:
894 /* m as a member of N is private, and R occurs in a member or friend of
895 class N, or */
896 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
897 && is_friend (N, scope))
898 return binfo;
899 return NULL_TREE;
902 case ak_protected:
904 /* m as a member of N is protected, and R occurs in a member or friend
905 of class N, or in a member or friend of a class P derived from N,
906 where m as a member of P is public, private, or protected */
907 if (friend_accessible_p (scope, decl, N, d->object_type))
908 return binfo;
909 return NULL_TREE;
912 default:
913 gcc_unreachable ();
917 /* Like accessible_p below, but within a template returns true iff DECL is
918 accessible in TYPE to all possible instantiations of the template. */
921 accessible_in_template_p (tree type, tree decl)
923 int save_ptd = processing_template_decl;
924 processing_template_decl = 0;
925 int val = accessible_p (type, decl, false);
926 processing_template_decl = save_ptd;
927 return val;
930 /* DECL is a declaration from a base class of TYPE, which was the
931 class used to name DECL. Return nonzero if, in the current
932 context, DECL is accessible. If TYPE is actually a BINFO node,
933 then we can tell in what context the access is occurring by looking
934 at the most derived class along the path indicated by BINFO. If
935 CONSIDER_LOCAL is true, do consider special access the current
936 scope or friendship thereof we might have. */
939 accessible_p (tree type, tree decl, bool consider_local_p)
941 tree binfo;
942 access_kind access;
944 /* If this declaration is in a block or namespace scope, there's no
945 access control. */
946 if (!TYPE_P (context_for_name_lookup (decl)))
947 return 1;
949 /* There is no need to perform access checks inside a thunk. */
950 if (current_function_decl && DECL_THUNK_P (current_function_decl))
951 return 1;
953 /* In a template declaration, we cannot be sure whether the
954 particular specialization that is instantiated will be a friend
955 or not. Therefore, all access checks are deferred until
956 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
957 parameter list for a template (because we may see dependent types
958 in default arguments for template parameters), and access
959 checking should be performed in the outermost parameter list. */
960 if (processing_template_decl
961 && (!processing_template_parmlist || processing_template_decl > 1))
962 return 1;
964 tree otype;
965 if (!TYPE_P (type))
967 /* When accessing a non-static member, the most derived type in the
968 binfo chain is the type of the object; remember that type for
969 protected_accessible_p. */
970 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
971 otype = BINFO_TYPE (b);
972 type = BINFO_TYPE (type);
974 else
975 otype = type;
977 /* [class.access.base]
979 A member m is accessible when named in class N if
981 --m as a member of N is public, or
983 --m as a member of N is private, and the reference occurs in a
984 member or friend of class N, or
986 --m as a member of N is protected, and the reference occurs in a
987 member or friend of class N, or in a member or friend of a
988 class P derived from N, where m as a member of P is public, private or
989 protected, or
991 --there exists a base class B of N that is accessible at the point
992 of reference, and m is accessible when named in class B.
994 We walk the base class hierarchy, checking these conditions. */
996 /* We walk using TYPE_BINFO (type) because access_in_type will set
997 BINFO_ACCESS on it and its bases. */
998 binfo = TYPE_BINFO (type);
1000 /* Compute the accessibility of DECL in the class hierarchy
1001 dominated by type. */
1002 access = access_in_type (type, decl);
1003 if (access == ak_public)
1004 return 1;
1006 /* If we aren't considering the point of reference, only the first bullet
1007 applies. */
1008 if (!consider_local_p)
1009 return 0;
1011 dfs_accessible_data d = { decl, otype };
1013 /* Walk the hierarchy again, looking for a base class that allows
1014 access. */
1015 return dfs_walk_once_accessible (binfo, /*friends=*/true,
1016 dfs_accessible_pre,
1017 dfs_accessible_post, &d)
1018 != NULL_TREE;
1021 struct lookup_field_info {
1022 /* The type in which we're looking. */
1023 tree type;
1024 /* The name of the field for which we're looking. */
1025 tree name;
1026 /* If non-NULL, the current result of the lookup. */
1027 tree rval;
1028 /* The path to RVAL. */
1029 tree rval_binfo;
1030 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1031 candidates. */
1032 tree ambiguous;
1033 /* If nonzero, we are looking for types, not data members. */
1034 int want_type;
1035 /* If something went wrong, a message indicating what. */
1036 const char *errstr;
1039 /* Nonzero for a class member means that it is shared between all objects
1040 of that class.
1042 [class.member.lookup]:If the resulting set of declarations are not all
1043 from sub-objects of the same type, or the set has a nonstatic member
1044 and includes members from distinct sub-objects, there is an ambiguity
1045 and the program is ill-formed.
1047 This function checks that T contains no nonstatic members. */
1050 shared_member_p (tree t)
1052 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
1053 || TREE_CODE (t) == CONST_DECL)
1054 return 1;
1055 if (is_overloaded_fn (t))
1057 t = get_fns (t);
1058 for (; t; t = OVL_NEXT (t))
1060 tree fn = OVL_CURRENT (t);
1061 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1062 return 0;
1064 return 1;
1066 return 0;
1069 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1070 found as a base class and sub-object of the object denoted by
1071 BINFO. */
1073 static int
1074 is_subobject_of_p (tree parent, tree binfo)
1076 tree probe;
1078 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1080 if (probe == binfo)
1081 return 1;
1082 if (BINFO_VIRTUAL_P (probe))
1083 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1084 != NULL_TREE);
1086 return 0;
1089 /* DATA is really a struct lookup_field_info. Look for a field with
1090 the name indicated there in BINFO. If this function returns a
1091 non-NULL value it is the result of the lookup. Called from
1092 lookup_field via breadth_first_search. */
1094 static tree
1095 lookup_field_r (tree binfo, void *data)
1097 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1098 tree type = BINFO_TYPE (binfo);
1099 tree nval = NULL_TREE;
1101 /* If this is a dependent base, don't look in it. */
1102 if (BINFO_DEPENDENT_BASE_P (binfo))
1103 return NULL_TREE;
1105 /* If this base class is hidden by the best-known value so far, we
1106 don't need to look. */
1107 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1108 && !BINFO_VIRTUAL_P (binfo))
1109 return dfs_skip_bases;
1111 /* First, look for a function. There can't be a function and a data
1112 member with the same name, and if there's a function and a type
1113 with the same name, the type is hidden by the function. */
1114 if (!lfi->want_type)
1115 nval = lookup_fnfields_slot (type, lfi->name);
1117 if (!nval)
1118 /* Look for a data member or type. */
1119 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1121 /* If there is no declaration with the indicated name in this type,
1122 then there's nothing to do. */
1123 if (!nval)
1124 goto done;
1126 /* If we're looking up a type (as with an elaborated type specifier)
1127 we ignore all non-types we find. */
1128 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1130 if (lfi->name == TYPE_IDENTIFIER (type))
1132 /* If the aggregate has no user defined constructors, we allow
1133 it to have fields with the same name as the enclosing type.
1134 If we are looking for that name, find the corresponding
1135 TYPE_DECL. */
1136 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1137 if (DECL_NAME (nval) == lfi->name
1138 && TREE_CODE (nval) == TYPE_DECL)
1139 break;
1141 else
1142 nval = NULL_TREE;
1143 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1145 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1146 lfi->name);
1147 if (e != NULL)
1148 nval = TYPE_MAIN_DECL (e->type);
1149 else
1150 goto done;
1154 /* If the lookup already found a match, and the new value doesn't
1155 hide the old one, we might have an ambiguity. */
1156 if (lfi->rval_binfo
1157 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1160 if (nval == lfi->rval && shared_member_p (nval))
1161 /* The two things are really the same. */
1163 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1164 /* The previous value hides the new one. */
1166 else
1168 /* We have a real ambiguity. We keep a chain of all the
1169 candidates. */
1170 if (!lfi->ambiguous && lfi->rval)
1172 /* This is the first time we noticed an ambiguity. Add
1173 what we previously thought was a reasonable candidate
1174 to the list. */
1175 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1176 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1179 /* Add the new value. */
1180 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1181 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1182 lfi->errstr = G_("request for member %qD is ambiguous");
1185 else
1187 lfi->rval = nval;
1188 lfi->rval_binfo = binfo;
1191 done:
1192 /* Don't look for constructors or destructors in base classes. */
1193 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1194 return dfs_skip_bases;
1195 return NULL_TREE;
1198 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1199 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1200 FUNCTIONS, and OPTYPE respectively. */
1202 tree
1203 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1205 tree baselink;
1207 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1208 || TREE_CODE (functions) == TEMPLATE_DECL
1209 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1210 || TREE_CODE (functions) == OVERLOAD);
1211 gcc_assert (!optype || TYPE_P (optype));
1212 gcc_assert (TREE_TYPE (functions));
1214 baselink = make_node (BASELINK);
1215 TREE_TYPE (baselink) = TREE_TYPE (functions);
1216 BASELINK_BINFO (baselink) = binfo;
1217 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1218 BASELINK_FUNCTIONS (baselink) = functions;
1219 BASELINK_OPTYPE (baselink) = optype;
1221 return baselink;
1224 /* Look for a member named NAME in an inheritance lattice dominated by
1225 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1226 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1227 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1228 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1229 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1230 TREE_VALUEs are the list of ambiguous candidates.
1232 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1234 If nothing can be found return NULL_TREE and do not issue an error. */
1236 tree
1237 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1238 tsubst_flags_t complain)
1240 tree rval, rval_binfo = NULL_TREE;
1241 tree type = NULL_TREE, basetype_path = NULL_TREE;
1242 struct lookup_field_info lfi;
1244 /* rval_binfo is the binfo associated with the found member, note,
1245 this can be set with useful information, even when rval is not
1246 set, because it must deal with ALL members, not just non-function
1247 members. It is used for ambiguity checking and the hidden
1248 checks. Whereas rval is only set if a proper (not hidden)
1249 non-function member is found. */
1251 const char *errstr = 0;
1253 if (name == error_mark_node
1254 || xbasetype == NULL_TREE
1255 || xbasetype == error_mark_node)
1256 return NULL_TREE;
1258 gcc_assert (identifier_p (name));
1260 if (TREE_CODE (xbasetype) == TREE_BINFO)
1262 type = BINFO_TYPE (xbasetype);
1263 basetype_path = xbasetype;
1265 else
1267 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1268 return NULL_TREE;
1269 type = xbasetype;
1270 xbasetype = NULL_TREE;
1273 type = complete_type (type);
1275 /* Make sure we're looking for a member of the current instantiation in the
1276 right partial specialization. */
1277 if (flag_concepts && dependent_type_p (type))
1278 type = currently_open_class (type);
1280 if (!basetype_path)
1281 basetype_path = TYPE_BINFO (type);
1283 if (!basetype_path)
1284 return NULL_TREE;
1286 if (GATHER_STATISTICS)
1287 n_calls_lookup_field++;
1289 memset (&lfi, 0, sizeof (lfi));
1290 lfi.type = type;
1291 lfi.name = name;
1292 lfi.want_type = want_type;
1293 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1294 rval = lfi.rval;
1295 rval_binfo = lfi.rval_binfo;
1296 if (rval_binfo)
1297 type = BINFO_TYPE (rval_binfo);
1298 errstr = lfi.errstr;
1300 /* If we are not interested in ambiguities, don't report them;
1301 just return NULL_TREE. */
1302 if (!protect && lfi.ambiguous)
1303 return NULL_TREE;
1305 if (protect == 2)
1307 if (lfi.ambiguous)
1308 return lfi.ambiguous;
1309 else
1310 protect = 0;
1313 /* [class.access]
1315 In the case of overloaded function names, access control is
1316 applied to the function selected by overloaded resolution.
1318 We cannot check here, even if RVAL is only a single non-static
1319 member function, since we do not know what the "this" pointer
1320 will be. For:
1322 class A { protected: void f(); };
1323 class B : public A {
1324 void g(A *p) {
1325 f(); // OK
1326 p->f(); // Not OK.
1330 only the first call to "f" is valid. However, if the function is
1331 static, we can check. */
1332 if (rval && protect
1333 && !really_overloaded_fn (rval))
1335 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1336 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1337 && !perform_or_defer_access_check (basetype_path, decl, decl,
1338 complain))
1339 rval = error_mark_node;
1342 if (errstr && protect)
1344 if (complain & tf_error)
1346 error (errstr, name, type);
1347 if (lfi.ambiguous)
1348 print_candidates (lfi.ambiguous);
1350 rval = error_mark_node;
1353 if (rval && is_overloaded_fn (rval))
1354 rval = build_baselink (rval_binfo, basetype_path, rval,
1355 (IDENTIFIER_TYPENAME_P (name)
1356 ? TREE_TYPE (name): NULL_TREE));
1357 return rval;
1360 /* Like lookup_member, except that if we find a function member we
1361 return NULL_TREE. */
1363 tree
1364 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1366 tree rval = lookup_member (xbasetype, name, protect, want_type,
1367 tf_warning_or_error);
1369 /* Ignore functions, but propagate the ambiguity list. */
1370 if (!error_operand_p (rval)
1371 && (rval && BASELINK_P (rval)))
1372 return NULL_TREE;
1374 return rval;
1377 /* Like lookup_member, except that if we find a non-function member we
1378 return NULL_TREE. */
1380 tree
1381 lookup_fnfields (tree xbasetype, tree name, int protect)
1383 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1384 tf_warning_or_error);
1386 /* Ignore non-functions, but propagate the ambiguity list. */
1387 if (!error_operand_p (rval)
1388 && (rval && !BASELINK_P (rval)))
1389 return NULL_TREE;
1391 return rval;
1394 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1395 corresponding to "operator TYPE ()", or -1 if there is no such
1396 operator. Only CLASS_TYPE itself is searched; this routine does
1397 not scan the base classes of CLASS_TYPE. */
1399 static int
1400 lookup_conversion_operator (tree class_type, tree type)
1402 int tpl_slot = -1;
1404 if (TYPE_HAS_CONVERSION (class_type))
1406 int i;
1407 tree fn;
1408 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1410 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1411 vec_safe_iterate (methods, i, &fn); ++i)
1413 /* All the conversion operators come near the beginning of
1414 the class. Therefore, if FN is not a conversion
1415 operator, there is no matching conversion operator in
1416 CLASS_TYPE. */
1417 fn = OVL_CURRENT (fn);
1418 if (!DECL_CONV_FN_P (fn))
1419 break;
1421 if (TREE_CODE (fn) == TEMPLATE_DECL)
1422 /* All the templated conversion functions are on the same
1423 slot, so remember it. */
1424 tpl_slot = i;
1425 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1426 return i;
1430 return tpl_slot;
1433 /* TYPE is a class type. Return the index of the fields within
1434 the method vector with name NAME, or -1 if no such field exists.
1435 Does not lazily declare implicitly-declared member functions. */
1437 static int
1438 lookup_fnfields_idx_nolazy (tree type, tree name)
1440 vec<tree, va_gc> *method_vec;
1441 tree fn;
1442 tree tmp;
1443 size_t i;
1445 if (!CLASS_TYPE_P (type))
1446 return -1;
1448 method_vec = CLASSTYPE_METHOD_VEC (type);
1449 if (!method_vec)
1450 return -1;
1452 if (GATHER_STATISTICS)
1453 n_calls_lookup_fnfields_1++;
1455 /* Constructors are first... */
1456 if (name == ctor_identifier)
1458 fn = CLASSTYPE_CONSTRUCTORS (type);
1459 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1461 /* and destructors are second. */
1462 if (name == dtor_identifier)
1464 fn = CLASSTYPE_DESTRUCTORS (type);
1465 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1467 if (IDENTIFIER_TYPENAME_P (name))
1468 return lookup_conversion_operator (type, TREE_TYPE (name));
1470 /* Skip the conversion operators. */
1471 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1472 vec_safe_iterate (method_vec, i, &fn);
1473 ++i)
1474 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1475 break;
1477 /* If the type is complete, use binary search. */
1478 if (COMPLETE_TYPE_P (type))
1480 int lo;
1481 int hi;
1483 lo = i;
1484 hi = method_vec->length ();
1485 while (lo < hi)
1487 i = (lo + hi) / 2;
1489 if (GATHER_STATISTICS)
1490 n_outer_fields_searched++;
1492 tmp = (*method_vec)[i];
1493 tmp = DECL_NAME (OVL_CURRENT (tmp));
1494 if (tmp > name)
1495 hi = i;
1496 else if (tmp < name)
1497 lo = i + 1;
1498 else
1499 return i;
1502 else
1503 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1505 if (GATHER_STATISTICS)
1506 n_outer_fields_searched++;
1507 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1508 return i;
1511 return -1;
1514 /* TYPE is a class type. Return the index of the fields within
1515 the method vector with name NAME, or -1 if no such field exists. */
1518 lookup_fnfields_1 (tree type, tree name)
1520 if (!CLASS_TYPE_P (type))
1521 return -1;
1523 if (COMPLETE_TYPE_P (type))
1525 if ((name == ctor_identifier
1526 || name == base_ctor_identifier
1527 || name == complete_ctor_identifier))
1529 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1530 lazily_declare_fn (sfk_constructor, type);
1531 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1532 lazily_declare_fn (sfk_copy_constructor, type);
1533 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1534 lazily_declare_fn (sfk_move_constructor, type);
1536 else if (name == ansi_assopname (NOP_EXPR))
1538 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1539 lazily_declare_fn (sfk_copy_assignment, type);
1540 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1541 lazily_declare_fn (sfk_move_assignment, type);
1543 else if ((name == dtor_identifier
1544 || name == base_dtor_identifier
1545 || name == complete_dtor_identifier
1546 || name == deleting_dtor_identifier)
1547 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1548 lazily_declare_fn (sfk_destructor, type);
1551 return lookup_fnfields_idx_nolazy (type, name);
1554 /* TYPE is a class type. Return the field within the method vector with
1555 name NAME, or NULL_TREE if no such field exists. */
1557 tree
1558 lookup_fnfields_slot (tree type, tree name)
1560 int ix = lookup_fnfields_1 (complete_type (type), name);
1561 if (ix < 0)
1562 return NULL_TREE;
1563 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1566 /* As above, but avoid lazily declaring functions. */
1568 tree
1569 lookup_fnfields_slot_nolazy (tree type, tree name)
1571 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1572 if (ix < 0)
1573 return NULL_TREE;
1574 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1577 /* Like lookup_fnfields_1, except that the name is extracted from
1578 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1581 class_method_index_for_fn (tree class_type, tree function)
1583 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1585 return lookup_fnfields_1 (class_type,
1586 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1587 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1588 DECL_NAME (function));
1592 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1593 the class or namespace used to qualify the name. CONTEXT_CLASS is
1594 the class corresponding to the object in which DECL will be used.
1595 Return a possibly modified version of DECL that takes into account
1596 the CONTEXT_CLASS.
1598 In particular, consider an expression like `B::m' in the context of
1599 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1600 then the most derived class indicated by the BASELINK_BINFO will be
1601 `B', not `D'. This function makes that adjustment. */
1603 tree
1604 adjust_result_of_qualified_name_lookup (tree decl,
1605 tree qualifying_scope,
1606 tree context_class)
1608 if (context_class && context_class != error_mark_node
1609 && CLASS_TYPE_P (context_class)
1610 && CLASS_TYPE_P (qualifying_scope)
1611 && DERIVED_FROM_P (qualifying_scope, context_class)
1612 && BASELINK_P (decl))
1614 tree base;
1616 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1617 Because we do not yet know which function will be chosen by
1618 overload resolution, we cannot yet check either accessibility
1619 or ambiguity -- in either case, the choice of a static member
1620 function might make the usage valid. */
1621 base = lookup_base (context_class, qualifying_scope,
1622 ba_unique, NULL, tf_none);
1623 if (base && base != error_mark_node)
1625 BASELINK_ACCESS_BINFO (decl) = base;
1626 BASELINK_BINFO (decl)
1627 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1628 ba_unique, NULL, tf_none);
1632 if (BASELINK_P (decl))
1633 BASELINK_QUALIFIED_P (decl) = true;
1635 return decl;
1639 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1640 PRE_FN is called in preorder, while POST_FN is called in postorder.
1641 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1642 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1643 that value is immediately returned and the walk is terminated. One
1644 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1645 POST_FN are passed the binfo to examine and the caller's DATA
1646 value. All paths are walked, thus virtual and morally virtual
1647 binfos can be multiply walked. */
1649 tree
1650 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1651 tree (*post_fn) (tree, void *), void *data)
1653 tree rval;
1654 unsigned ix;
1655 tree base_binfo;
1657 /* Call the pre-order walking function. */
1658 if (pre_fn)
1660 rval = pre_fn (binfo, data);
1661 if (rval)
1663 if (rval == dfs_skip_bases)
1664 goto skip_bases;
1665 return rval;
1669 /* Find the next child binfo to walk. */
1670 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1672 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1673 if (rval)
1674 return rval;
1677 skip_bases:
1678 /* Call the post-order walking function. */
1679 if (post_fn)
1681 rval = post_fn (binfo, data);
1682 gcc_assert (rval != dfs_skip_bases);
1683 return rval;
1686 return NULL_TREE;
1689 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1690 that binfos are walked at most once. */
1692 static tree
1693 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1694 tree (*post_fn) (tree, void *), void *data)
1696 tree rval;
1697 unsigned ix;
1698 tree base_binfo;
1700 /* Call the pre-order walking function. */
1701 if (pre_fn)
1703 rval = pre_fn (binfo, data);
1704 if (rval)
1706 if (rval == dfs_skip_bases)
1707 goto skip_bases;
1709 return rval;
1713 /* Find the next child binfo to walk. */
1714 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1716 if (BINFO_VIRTUAL_P (base_binfo))
1718 if (BINFO_MARKED (base_binfo))
1719 continue;
1720 BINFO_MARKED (base_binfo) = 1;
1723 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1724 if (rval)
1725 return rval;
1728 skip_bases:
1729 /* Call the post-order walking function. */
1730 if (post_fn)
1732 rval = post_fn (binfo, data);
1733 gcc_assert (rval != dfs_skip_bases);
1734 return rval;
1737 return NULL_TREE;
1740 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1741 BINFO. */
1743 static void
1744 dfs_unmark_r (tree binfo)
1746 unsigned ix;
1747 tree base_binfo;
1749 /* Process the basetypes. */
1750 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1752 if (BINFO_VIRTUAL_P (base_binfo))
1754 if (!BINFO_MARKED (base_binfo))
1755 continue;
1756 BINFO_MARKED (base_binfo) = 0;
1758 /* Only walk, if it can contain more virtual bases. */
1759 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1760 dfs_unmark_r (base_binfo);
1764 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1765 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1766 For diamond shaped hierarchies we must mark the virtual bases, to
1767 avoid multiple walks. */
1769 tree
1770 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1771 tree (*post_fn) (tree, void *), void *data)
1773 static int active = 0; /* We must not be called recursively. */
1774 tree rval;
1776 gcc_assert (pre_fn || post_fn);
1777 gcc_assert (!active);
1778 active++;
1780 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1781 /* We are not diamond shaped, and therefore cannot encounter the
1782 same binfo twice. */
1783 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1784 else
1786 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1787 if (!BINFO_INHERITANCE_CHAIN (binfo))
1789 /* We are at the top of the hierarchy, and can use the
1790 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1791 bases. */
1792 vec<tree, va_gc> *vbases;
1793 unsigned ix;
1794 tree base_binfo;
1796 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1797 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1798 BINFO_MARKED (base_binfo) = 0;
1800 else
1801 dfs_unmark_r (binfo);
1804 active--;
1806 return rval;
1809 /* Worker function for dfs_walk_once_accessible. Behaves like
1810 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1811 access given by the current context should be considered, (b) ONCE
1812 indicates whether bases should be marked during traversal. */
1814 static tree
1815 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1816 tree (*pre_fn) (tree, void *),
1817 tree (*post_fn) (tree, void *), void *data)
1819 tree rval = NULL_TREE;
1820 unsigned ix;
1821 tree base_binfo;
1823 /* Call the pre-order walking function. */
1824 if (pre_fn)
1826 rval = pre_fn (binfo, data);
1827 if (rval)
1829 if (rval == dfs_skip_bases)
1830 goto skip_bases;
1832 return rval;
1836 /* Find the next child binfo to walk. */
1837 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1839 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1841 if (mark && BINFO_MARKED (base_binfo))
1842 continue;
1844 /* If the base is inherited via private or protected
1845 inheritance, then we can't see it, unless we are a friend of
1846 the current binfo. */
1847 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1849 tree scope;
1850 if (!friends_p)
1851 continue;
1852 scope = current_scope ();
1853 if (!scope
1854 || TREE_CODE (scope) == NAMESPACE_DECL
1855 || !is_friend (BINFO_TYPE (binfo), scope))
1856 continue;
1859 if (mark)
1860 BINFO_MARKED (base_binfo) = 1;
1862 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1863 pre_fn, post_fn, data);
1864 if (rval)
1865 return rval;
1868 skip_bases:
1869 /* Call the post-order walking function. */
1870 if (post_fn)
1872 rval = post_fn (binfo, data);
1873 gcc_assert (rval != dfs_skip_bases);
1874 return rval;
1877 return NULL_TREE;
1880 /* Like dfs_walk_once except that only accessible bases are walked.
1881 FRIENDS_P indicates whether friendship of the local context
1882 should be considered when determining accessibility. */
1884 static tree
1885 dfs_walk_once_accessible (tree binfo, bool friends_p,
1886 tree (*pre_fn) (tree, void *),
1887 tree (*post_fn) (tree, void *), void *data)
1889 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1890 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1891 pre_fn, post_fn, data);
1893 if (diamond_shaped)
1895 if (!BINFO_INHERITANCE_CHAIN (binfo))
1897 /* We are at the top of the hierarchy, and can use the
1898 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1899 bases. */
1900 vec<tree, va_gc> *vbases;
1901 unsigned ix;
1902 tree base_binfo;
1904 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1905 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1906 BINFO_MARKED (base_binfo) = 0;
1908 else
1909 dfs_unmark_r (binfo);
1911 return rval;
1914 /* Check that virtual overrider OVERRIDER is acceptable for base function
1915 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1917 static int
1918 check_final_overrider (tree overrider, tree basefn)
1920 tree over_type = TREE_TYPE (overrider);
1921 tree base_type = TREE_TYPE (basefn);
1922 tree over_return = fndecl_declared_return_type (overrider);
1923 tree base_return = fndecl_declared_return_type (basefn);
1924 tree over_throw, base_throw;
1926 int fail = 0;
1928 if (DECL_INVALID_OVERRIDER_P (overrider))
1929 return 0;
1931 if (same_type_p (base_return, over_return))
1932 /* OK */;
1933 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1934 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1935 && POINTER_TYPE_P (base_return)))
1937 /* Potentially covariant. */
1938 unsigned base_quals, over_quals;
1940 fail = !POINTER_TYPE_P (base_return);
1941 if (!fail)
1943 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1945 base_return = TREE_TYPE (base_return);
1946 over_return = TREE_TYPE (over_return);
1948 base_quals = cp_type_quals (base_return);
1949 over_quals = cp_type_quals (over_return);
1951 if ((base_quals & over_quals) != over_quals)
1952 fail = 1;
1954 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1956 /* Strictly speaking, the standard requires the return type to be
1957 complete even if it only differs in cv-quals, but that seems
1958 like a bug in the wording. */
1959 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1960 over_return))
1962 tree binfo = lookup_base (over_return, base_return,
1963 ba_check, NULL, tf_none);
1965 if (!binfo || binfo == error_mark_node)
1966 fail = 1;
1969 else if (can_convert_standard (TREE_TYPE (base_type),
1970 TREE_TYPE (over_type),
1971 tf_warning_or_error))
1972 /* GNU extension, allow trivial pointer conversions such as
1973 converting to void *, or qualification conversion. */
1975 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1976 "invalid covariant return type for %q#D", overrider))
1977 inform (DECL_SOURCE_LOCATION (basefn),
1978 " overriding %q#D", basefn);
1980 else
1981 fail = 2;
1983 else
1984 fail = 2;
1985 if (!fail)
1986 /* OK */;
1987 else
1989 if (fail == 1)
1991 error ("invalid covariant return type for %q+#D", overrider);
1992 error (" overriding %q+#D", basefn);
1994 else
1996 error ("conflicting return type specified for %q+#D", overrider);
1997 error (" overriding %q+#D", basefn);
1999 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2000 return 0;
2003 /* Check throw specifier is at least as strict. */
2004 maybe_instantiate_noexcept (basefn);
2005 maybe_instantiate_noexcept (overrider);
2006 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
2007 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
2009 if (!comp_except_specs (base_throw, over_throw, ce_derived))
2011 error ("looser throw specifier for %q+#F", overrider);
2012 error (" overriding %q+#F", basefn);
2013 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2014 return 0;
2017 /* Check for conflicting type attributes. */
2018 if (!comp_type_attributes (over_type, base_type))
2020 error ("conflicting type attributes specified for %q+#D", overrider);
2021 error (" overriding %q+#D", basefn);
2022 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2023 return 0;
2026 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2028 if (DECL_DELETED_FN (overrider))
2030 error ("deleted function %q+D", overrider);
2031 error ("overriding non-deleted function %q+D", basefn);
2032 maybe_explain_implicit_delete (overrider);
2034 else
2036 error ("non-deleted function %q+D", overrider);
2037 error ("overriding deleted function %q+D", basefn);
2039 return 0;
2041 if (DECL_FINAL_P (basefn))
2043 error ("virtual function %q+D", overrider);
2044 error ("overriding final function %q+D", basefn);
2045 return 0;
2047 return 1;
2050 /* Given a class TYPE, and a function decl FNDECL, look for
2051 virtual functions in TYPE's hierarchy which FNDECL overrides.
2052 We do not look in TYPE itself, only its bases.
2054 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2055 find that it overrides anything.
2057 We check that every function which is overridden, is correctly
2058 overridden. */
2061 look_for_overrides (tree type, tree fndecl)
2063 tree binfo = TYPE_BINFO (type);
2064 tree base_binfo;
2065 int ix;
2066 int found = 0;
2068 /* A constructor for a class T does not override a function T
2069 in a base class. */
2070 if (DECL_CONSTRUCTOR_P (fndecl))
2071 return 0;
2073 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2075 tree basetype = BINFO_TYPE (base_binfo);
2077 if (TYPE_POLYMORPHIC_P (basetype))
2078 found += look_for_overrides_r (basetype, fndecl);
2080 return found;
2083 /* Look in TYPE for virtual functions with the same signature as
2084 FNDECL. */
2086 tree
2087 look_for_overrides_here (tree type, tree fndecl)
2089 int ix;
2091 /* If there are no methods in TYPE (meaning that only implicitly
2092 declared methods will ever be provided for TYPE), then there are
2093 no virtual functions. */
2094 if (!CLASSTYPE_METHOD_VEC (type))
2095 return NULL_TREE;
2097 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2098 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2099 else
2100 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2101 if (ix >= 0)
2103 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
2105 for (; fns; fns = OVL_NEXT (fns))
2107 tree fn = OVL_CURRENT (fns);
2109 if (!DECL_VIRTUAL_P (fn))
2110 /* Not a virtual. */;
2111 else if (DECL_CONTEXT (fn) != type)
2112 /* Introduced with a using declaration. */;
2113 else if (DECL_STATIC_FUNCTION_P (fndecl))
2115 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2116 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2117 if (compparms (TREE_CHAIN (btypes), dtypes))
2118 return fn;
2120 else if (same_signature_p (fndecl, fn))
2121 return fn;
2124 return NULL_TREE;
2127 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2128 TYPE itself and its bases. */
2130 static int
2131 look_for_overrides_r (tree type, tree fndecl)
2133 tree fn = look_for_overrides_here (type, fndecl);
2134 if (fn)
2136 if (DECL_STATIC_FUNCTION_P (fndecl))
2138 /* A static member function cannot match an inherited
2139 virtual member function. */
2140 error ("%q+#D cannot be declared", fndecl);
2141 error (" since %q+#D declared in base class", fn);
2143 else
2145 /* It's definitely virtual, even if not explicitly set. */
2146 DECL_VIRTUAL_P (fndecl) = 1;
2147 check_final_overrider (fndecl, fn);
2149 return 1;
2152 /* We failed to find one declared in this class. Look in its bases. */
2153 return look_for_overrides (type, fndecl);
2156 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2158 static tree
2159 dfs_get_pure_virtuals (tree binfo, void *data)
2161 tree type = (tree) data;
2163 /* We're not interested in primary base classes; the derived class
2164 of which they are a primary base will contain the information we
2165 need. */
2166 if (!BINFO_PRIMARY_P (binfo))
2168 tree virtuals;
2170 for (virtuals = BINFO_VIRTUALS (binfo);
2171 virtuals;
2172 virtuals = TREE_CHAIN (virtuals))
2173 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2174 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2177 return NULL_TREE;
2180 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2182 void
2183 get_pure_virtuals (tree type)
2185 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2186 is going to be overridden. */
2187 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2188 /* Now, run through all the bases which are not primary bases, and
2189 collect the pure virtual functions. We look at the vtable in
2190 each class to determine what pure virtual functions are present.
2191 (A primary base is not interesting because the derived class of
2192 which it is a primary base will contain vtable entries for the
2193 pure virtuals in the base class. */
2194 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2197 /* Debug info for C++ classes can get very large; try to avoid
2198 emitting it everywhere.
2200 Note that this optimization wins even when the target supports
2201 BINCL (if only slightly), and reduces the amount of work for the
2202 linker. */
2204 void
2205 maybe_suppress_debug_info (tree t)
2207 if (write_symbols == NO_DEBUG)
2208 return;
2210 /* We might have set this earlier in cp_finish_decl. */
2211 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2213 /* Always emit the information for each class every time. */
2214 if (flag_emit_class_debug_always)
2215 return;
2217 /* If we already know how we're handling this class, handle debug info
2218 the same way. */
2219 if (CLASSTYPE_INTERFACE_KNOWN (t))
2221 if (CLASSTYPE_INTERFACE_ONLY (t))
2222 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2223 /* else don't set it. */
2225 /* If the class has a vtable, write out the debug info along with
2226 the vtable. */
2227 else if (TYPE_CONTAINS_VPTR_P (t))
2228 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2230 /* Otherwise, just emit the debug info normally. */
2233 /* Note that we want debugging information for a base class of a class
2234 whose vtable is being emitted. Normally, this would happen because
2235 calling the constructor for a derived class implies calling the
2236 constructors for all bases, which involve initializing the
2237 appropriate vptr with the vtable for the base class; but in the
2238 presence of optimization, this initialization may be optimized
2239 away, so we tell finish_vtable_vardecl that we want the debugging
2240 information anyway. */
2242 static tree
2243 dfs_debug_mark (tree binfo, void * /*data*/)
2245 tree t = BINFO_TYPE (binfo);
2247 if (CLASSTYPE_DEBUG_REQUESTED (t))
2248 return dfs_skip_bases;
2250 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2252 return NULL_TREE;
2255 /* Write out the debugging information for TYPE, whose vtable is being
2256 emitted. Also walk through our bases and note that we want to
2257 write out information for them. This avoids the problem of not
2258 writing any debug info for intermediate basetypes whose
2259 constructors, and thus the references to their vtables, and thus
2260 the vtables themselves, were optimized away. */
2262 void
2263 note_debug_info_needed (tree type)
2265 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2267 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2268 rest_of_type_compilation (type, toplevel_bindings_p ());
2271 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2274 void
2275 print_search_statistics (void)
2277 if (! GATHER_STATISTICS)
2279 fprintf (stderr, "no search statistics\n");
2280 return;
2283 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2284 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2285 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2286 n_outer_fields_searched, n_calls_lookup_fnfields);
2287 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2290 void
2291 reinit_search_statistics (void)
2293 n_fields_searched = 0;
2294 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2295 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2296 n_calls_get_base_type = 0;
2297 n_outer_fields_searched = 0;
2298 n_contexts_saved = 0;
2301 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2302 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2303 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2304 bases have been encountered already in the tree walk. PARENT_CONVS
2305 is the list of lists of conversion functions that could hide CONV
2306 and OTHER_CONVS is the list of lists of conversion functions that
2307 could hide or be hidden by CONV, should virtualness be involved in
2308 the hierarchy. Merely checking the conversion op's name is not
2309 enough because two conversion operators to the same type can have
2310 different names. Return nonzero if we are visible. */
2312 static int
2313 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2314 tree to_type, tree parent_convs, tree other_convs)
2316 tree level, probe;
2318 /* See if we are hidden by a parent conversion. */
2319 for (level = parent_convs; level; level = TREE_CHAIN (level))
2320 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2321 if (same_type_p (to_type, TREE_TYPE (probe)))
2322 return 0;
2324 if (virtual_depth || virtualness)
2326 /* In a virtual hierarchy, we could be hidden, or could hide a
2327 conversion function on the other_convs list. */
2328 for (level = other_convs; level; level = TREE_CHAIN (level))
2330 int we_hide_them;
2331 int they_hide_us;
2332 tree *prev, other;
2334 if (!(virtual_depth || TREE_STATIC (level)))
2335 /* Neither is morally virtual, so cannot hide each other. */
2336 continue;
2338 if (!TREE_VALUE (level))
2339 /* They evaporated away already. */
2340 continue;
2342 they_hide_us = (virtual_depth
2343 && original_binfo (binfo, TREE_PURPOSE (level)));
2344 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2345 && original_binfo (TREE_PURPOSE (level), binfo));
2347 if (!(we_hide_them || they_hide_us))
2348 /* Neither is within the other, so no hiding can occur. */
2349 continue;
2351 for (prev = &TREE_VALUE (level), other = *prev; other;)
2353 if (same_type_p (to_type, TREE_TYPE (other)))
2355 if (they_hide_us)
2356 /* We are hidden. */
2357 return 0;
2359 if (we_hide_them)
2361 /* We hide the other one. */
2362 other = TREE_CHAIN (other);
2363 *prev = other;
2364 continue;
2367 prev = &TREE_CHAIN (other);
2368 other = *prev;
2372 return 1;
2375 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2376 of conversion functions, the first slot will be for the current
2377 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2378 of conversion functions from children of the current binfo,
2379 concatenated with conversions from elsewhere in the hierarchy --
2380 that list begins with OTHER_CONVS. Return a single list of lists
2381 containing only conversions from the current binfo and its
2382 children. */
2384 static tree
2385 split_conversions (tree my_convs, tree parent_convs,
2386 tree child_convs, tree other_convs)
2388 tree t;
2389 tree prev;
2391 /* Remove the original other_convs portion from child_convs. */
2392 for (prev = NULL, t = child_convs;
2393 t != other_convs; prev = t, t = TREE_CHAIN (t))
2394 continue;
2396 if (prev)
2397 TREE_CHAIN (prev) = NULL_TREE;
2398 else
2399 child_convs = NULL_TREE;
2401 /* Attach the child convs to any we had at this level. */
2402 if (my_convs)
2404 my_convs = parent_convs;
2405 TREE_CHAIN (my_convs) = child_convs;
2407 else
2408 my_convs = child_convs;
2410 return my_convs;
2413 /* Worker for lookup_conversions. Lookup conversion functions in
2414 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2415 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2416 encountered virtual bases already in the tree walk. PARENT_CONVS &
2417 PARENT_TPL_CONVS are lists of list of conversions within parent
2418 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2419 elsewhere in the tree. Return the conversions found within this
2420 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2421 encountered virtualness. We keep template and non-template
2422 conversions separate, to avoid unnecessary type comparisons.
2424 The located conversion functions are held in lists of lists. The
2425 TREE_VALUE of the outer list is the list of conversion functions
2426 found in a particular binfo. The TREE_PURPOSE of both the outer
2427 and inner lists is the binfo at which those conversions were
2428 found. TREE_STATIC is set for those lists within of morally
2429 virtual binfos. The TREE_VALUE of the inner list is the conversion
2430 function or overload itself. The TREE_TYPE of each inner list node
2431 is the converted-to type. */
2433 static int
2434 lookup_conversions_r (tree binfo,
2435 int virtual_depth, int virtualness,
2436 tree parent_convs, tree parent_tpl_convs,
2437 tree other_convs, tree other_tpl_convs,
2438 tree *convs, tree *tpl_convs)
2440 int my_virtualness = 0;
2441 tree my_convs = NULL_TREE;
2442 tree my_tpl_convs = NULL_TREE;
2443 tree child_convs = NULL_TREE;
2444 tree child_tpl_convs = NULL_TREE;
2445 unsigned i;
2446 tree base_binfo;
2447 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2448 tree conv;
2450 /* If we have no conversion operators, then don't look. */
2451 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2453 *convs = *tpl_convs = NULL_TREE;
2455 return 0;
2458 if (BINFO_VIRTUAL_P (binfo))
2459 virtual_depth++;
2461 /* First, locate the unhidden ones at this level. */
2462 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2463 vec_safe_iterate (method_vec, i, &conv);
2464 ++i)
2466 tree cur = OVL_CURRENT (conv);
2468 if (!DECL_CONV_FN_P (cur))
2469 break;
2471 if (TREE_CODE (cur) == TEMPLATE_DECL)
2473 /* Only template conversions can be overloaded, and we must
2474 flatten them out and check each one individually. */
2475 tree tpls;
2477 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2479 tree tpl = OVL_CURRENT (tpls);
2480 tree type = DECL_CONV_FN_TYPE (tpl);
2482 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2483 type, parent_tpl_convs, other_tpl_convs))
2485 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2486 TREE_TYPE (my_tpl_convs) = type;
2487 if (virtual_depth)
2489 TREE_STATIC (my_tpl_convs) = 1;
2490 my_virtualness = 1;
2495 else
2497 tree name = DECL_NAME (cur);
2499 if (!IDENTIFIER_MARKED (name))
2501 tree type = DECL_CONV_FN_TYPE (cur);
2502 if (type_uses_auto (type))
2504 mark_used (cur);
2505 type = DECL_CONV_FN_TYPE (cur);
2508 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2509 type, parent_convs, other_convs))
2511 my_convs = tree_cons (binfo, conv, my_convs);
2512 TREE_TYPE (my_convs) = type;
2513 if (virtual_depth)
2515 TREE_STATIC (my_convs) = 1;
2516 my_virtualness = 1;
2518 IDENTIFIER_MARKED (name) = 1;
2524 if (my_convs)
2526 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2527 if (virtual_depth)
2528 TREE_STATIC (parent_convs) = 1;
2531 if (my_tpl_convs)
2533 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2534 if (virtual_depth)
2535 TREE_STATIC (parent_tpl_convs) = 1;
2538 child_convs = other_convs;
2539 child_tpl_convs = other_tpl_convs;
2541 /* Now iterate over each base, looking for more conversions. */
2542 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2544 tree base_convs, base_tpl_convs;
2545 unsigned base_virtualness;
2547 base_virtualness = lookup_conversions_r (base_binfo,
2548 virtual_depth, virtualness,
2549 parent_convs, parent_tpl_convs,
2550 child_convs, child_tpl_convs,
2551 &base_convs, &base_tpl_convs);
2552 if (base_virtualness)
2553 my_virtualness = virtualness = 1;
2554 child_convs = chainon (base_convs, child_convs);
2555 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2558 /* Unmark the conversions found at this level */
2559 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2560 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2562 *convs = split_conversions (my_convs, parent_convs,
2563 child_convs, other_convs);
2564 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2565 child_tpl_convs, other_tpl_convs);
2567 return my_virtualness;
2570 /* Return a TREE_LIST containing all the non-hidden user-defined
2571 conversion functions for TYPE (and its base-classes). The
2572 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2573 function. The TREE_PURPOSE is the BINFO from which the conversion
2574 functions in this node were selected. This function is effectively
2575 performing a set of member lookups as lookup_fnfield does, but
2576 using the type being converted to as the unique key, rather than the
2577 field name. */
2579 tree
2580 lookup_conversions (tree type)
2582 tree convs, tpl_convs;
2583 tree list = NULL_TREE;
2585 complete_type (type);
2586 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2587 return NULL_TREE;
2589 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2590 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2591 &convs, &tpl_convs);
2593 /* Flatten the list-of-lists */
2594 for (; convs; convs = TREE_CHAIN (convs))
2596 tree probe, next;
2598 for (probe = TREE_VALUE (convs); probe; probe = next)
2600 next = TREE_CHAIN (probe);
2602 TREE_CHAIN (probe) = list;
2603 list = probe;
2607 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2609 tree probe, next;
2611 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2613 next = TREE_CHAIN (probe);
2615 TREE_CHAIN (probe) = list;
2616 list = probe;
2620 return list;
2623 /* Returns the binfo of the first direct or indirect virtual base derived
2624 from BINFO, or NULL if binfo is not via virtual. */
2626 tree
2627 binfo_from_vbase (tree binfo)
2629 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2631 if (BINFO_VIRTUAL_P (binfo))
2632 return binfo;
2634 return NULL_TREE;
2637 /* Returns the binfo of the first direct or indirect virtual base derived
2638 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2639 via virtual. */
2641 tree
2642 binfo_via_virtual (tree binfo, tree limit)
2644 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2645 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2646 return NULL_TREE;
2648 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2649 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2651 if (BINFO_VIRTUAL_P (binfo))
2652 return binfo;
2654 return NULL_TREE;
2657 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2658 Find the equivalent binfo within whatever graph HERE is located.
2659 This is the inverse of original_binfo. */
2661 tree
2662 copied_binfo (tree binfo, tree here)
2664 tree result = NULL_TREE;
2666 if (BINFO_VIRTUAL_P (binfo))
2668 tree t;
2670 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2671 t = BINFO_INHERITANCE_CHAIN (t))
2672 continue;
2674 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2676 else if (BINFO_INHERITANCE_CHAIN (binfo))
2678 tree cbinfo;
2679 tree base_binfo;
2680 int ix;
2682 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2683 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2684 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2686 result = base_binfo;
2687 break;
2690 else
2692 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2693 result = here;
2696 gcc_assert (result);
2697 return result;
2700 tree
2701 binfo_for_vbase (tree base, tree t)
2703 unsigned ix;
2704 tree binfo;
2705 vec<tree, va_gc> *vbases;
2707 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2708 vec_safe_iterate (vbases, ix, &binfo); ix++)
2709 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2710 return binfo;
2711 return NULL;
2714 /* BINFO is some base binfo of HERE, within some other
2715 hierarchy. Return the equivalent binfo, but in the hierarchy
2716 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2717 is not a base binfo of HERE, returns NULL_TREE. */
2719 tree
2720 original_binfo (tree binfo, tree here)
2722 tree result = NULL;
2724 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2725 result = here;
2726 else if (BINFO_VIRTUAL_P (binfo))
2727 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2728 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2729 : NULL_TREE);
2730 else if (BINFO_INHERITANCE_CHAIN (binfo))
2732 tree base_binfos;
2734 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2735 if (base_binfos)
2737 int ix;
2738 tree base_binfo;
2740 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2741 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2742 BINFO_TYPE (binfo)))
2744 result = base_binfo;
2745 break;
2750 return result;