Add execution tests of ARM EXT intrinsics
[official-gcc.git] / gcc / cp / search.c
blobc3eed90f6c3632b5a9ade786e0c09e3a2444131d
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "target.h"
35 static int is_subobject_of_p (tree, tree);
36 static tree dfs_lookup_base (tree, void *);
37 static tree dfs_dcast_hint_pre (tree, void *);
38 static tree dfs_dcast_hint_post (tree, void *);
39 static tree dfs_debug_mark (tree, void *);
40 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
41 tree (*post_fn) (tree, void *), void *data);
42 static void dfs_unmark_r (tree);
43 static int check_hidden_convs (tree, int, int, tree, tree, tree);
44 static tree split_conversions (tree, tree, tree, tree);
45 static int lookup_conversions_r (tree, int, int,
46 tree, tree, tree, tree, tree *, tree *);
47 static int look_for_overrides_r (tree, tree);
48 static tree lookup_field_r (tree, void *);
49 static tree dfs_accessible_post (tree, void *);
50 static tree dfs_walk_once_accessible_r (tree, bool, bool,
51 tree (*pre_fn) (tree, void *),
52 tree (*post_fn) (tree, void *),
53 void *data);
54 static tree dfs_walk_once_accessible (tree, bool,
55 tree (*pre_fn) (tree, void *),
56 tree (*post_fn) (tree, void *),
57 void *data);
58 static tree dfs_access_in_type (tree, void *);
59 static access_kind access_in_type (tree, tree);
60 static int protected_accessible_p (tree, tree, tree);
61 static int friend_accessible_p (tree, tree, tree);
62 static tree dfs_get_pure_virtuals (tree, void *);
65 /* Variables for gathering statistics. */
66 static int n_fields_searched;
67 static int n_calls_lookup_field, n_calls_lookup_field_1;
68 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
69 static int n_calls_get_base_type;
70 static int n_outer_fields_searched;
71 static int n_contexts_saved;
74 /* Data for lookup_base and its workers. */
76 struct lookup_base_data_s
78 tree t; /* type being searched. */
79 tree base; /* The base type we're looking for. */
80 tree binfo; /* Found binfo. */
81 bool via_virtual; /* Found via a virtual path. */
82 bool ambiguous; /* Found multiply ambiguous */
83 bool repeated_base; /* Whether there are repeated bases in the
84 hierarchy. */
85 bool want_any; /* Whether we want any matching binfo. */
88 /* Worker function for lookup_base. See if we've found the desired
89 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
91 static tree
92 dfs_lookup_base (tree binfo, void *data_)
94 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
96 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
98 if (!data->binfo)
100 data->binfo = binfo;
101 data->via_virtual
102 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
104 if (!data->repeated_base)
105 /* If there are no repeated bases, we can stop now. */
106 return binfo;
108 if (data->want_any && !data->via_virtual)
109 /* If this is a non-virtual base, then we can't do
110 better. */
111 return binfo;
113 return dfs_skip_bases;
115 else
117 gcc_assert (binfo != data->binfo);
119 /* We've found more than one matching binfo. */
120 if (!data->want_any)
122 /* This is immediately ambiguous. */
123 data->binfo = NULL_TREE;
124 data->ambiguous = true;
125 return error_mark_node;
128 /* Prefer one via a non-virtual path. */
129 if (!binfo_via_virtual (binfo, data->t))
131 data->binfo = binfo;
132 data->via_virtual = false;
133 return binfo;
136 /* There must be repeated bases, otherwise we'd have stopped
137 on the first base we found. */
138 return dfs_skip_bases;
142 return NULL_TREE;
145 /* Returns true if type BASE is accessible in T. (BASE is known to be
146 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
147 true, consider any special access of the current scope, or access
148 bestowed by friendship. */
150 bool
151 accessible_base_p (tree t, tree base, bool consider_local_p)
153 tree decl;
155 /* [class.access.base]
157 A base class is said to be accessible if an invented public
158 member of the base class is accessible.
160 If BASE is a non-proper base, this condition is trivially
161 true. */
162 if (same_type_p (t, base))
163 return true;
164 /* Rather than inventing a public member, we use the implicit
165 public typedef created in the scope of every class. */
166 decl = TYPE_FIELDS (base);
167 while (!DECL_SELF_REFERENCE_P (decl))
168 decl = DECL_CHAIN (decl);
169 while (ANON_AGGR_TYPE_P (t))
170 t = TYPE_CONTEXT (t);
171 return accessible_p (t, decl, consider_local_p);
174 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
175 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
176 non-NULL, fill with information about what kind of base we
177 discovered.
179 If the base is inaccessible, or ambiguous, then error_mark_node is
180 returned. If the tf_error bit of COMPLAIN is not set, no error
181 is issued. */
183 tree
184 lookup_base (tree t, tree base, base_access access,
185 base_kind *kind_ptr, tsubst_flags_t complain)
187 tree binfo;
188 tree t_binfo;
189 base_kind bk;
191 /* "Nothing" is definitely not derived from Base. */
192 if (t == NULL_TREE)
194 if (kind_ptr)
195 *kind_ptr = bk_not_base;
196 return NULL_TREE;
199 if (t == error_mark_node || base == error_mark_node)
201 if (kind_ptr)
202 *kind_ptr = bk_not_base;
203 return error_mark_node;
205 gcc_assert (TYPE_P (base));
207 if (!TYPE_P (t))
209 t_binfo = t;
210 t = BINFO_TYPE (t);
212 else
214 t = complete_type (TYPE_MAIN_VARIANT (t));
215 t_binfo = TYPE_BINFO (t);
218 base = TYPE_MAIN_VARIANT (base);
220 /* If BASE is incomplete, it can't be a base of T--and instantiating it
221 might cause an error. */
222 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
224 struct lookup_base_data_s data;
226 data.t = t;
227 data.base = base;
228 data.binfo = NULL_TREE;
229 data.ambiguous = data.via_virtual = false;
230 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
231 data.want_any = access == ba_any;
233 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
234 binfo = data.binfo;
236 if (!binfo)
237 bk = data.ambiguous ? bk_ambig : bk_not_base;
238 else if (binfo == t_binfo)
239 bk = bk_same_type;
240 else if (data.via_virtual)
241 bk = bk_via_virtual;
242 else
243 bk = bk_proper_base;
245 else
247 binfo = NULL_TREE;
248 bk = bk_not_base;
251 /* Check that the base is unambiguous and accessible. */
252 if (access != ba_any)
253 switch (bk)
255 case bk_not_base:
256 break;
258 case bk_ambig:
259 if (complain & tf_error)
260 error ("%qT is an ambiguous base of %qT", base, t);
261 binfo = error_mark_node;
262 break;
264 default:
265 if ((access & ba_check_bit)
266 /* If BASE is incomplete, then BASE and TYPE are probably
267 the same, in which case BASE is accessible. If they
268 are not the same, then TYPE is invalid. In that case,
269 there's no need to issue another error here, and
270 there's no implicit typedef to use in the code that
271 follows, so we skip the check. */
272 && COMPLETE_TYPE_P (base)
273 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
275 if (complain & tf_error)
276 error ("%qT is an inaccessible base of %qT", base, t);
277 binfo = error_mark_node;
278 bk = bk_inaccessible;
280 break;
283 if (kind_ptr)
284 *kind_ptr = bk;
286 return binfo;
289 /* Data for dcast_base_hint walker. */
291 struct dcast_data_s
293 tree subtype; /* The base type we're looking for. */
294 int virt_depth; /* Number of virtual bases encountered from most
295 derived. */
296 tree offset; /* Best hint offset discovered so far. */
297 bool repeated_base; /* Whether there are repeated bases in the
298 hierarchy. */
301 /* Worker for dcast_base_hint. Search for the base type being cast
302 from. */
304 static tree
305 dfs_dcast_hint_pre (tree binfo, void *data_)
307 struct dcast_data_s *data = (struct dcast_data_s *) data_;
309 if (BINFO_VIRTUAL_P (binfo))
310 data->virt_depth++;
312 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
314 if (data->virt_depth)
316 data->offset = ssize_int (-1);
317 return data->offset;
319 if (data->offset)
320 data->offset = ssize_int (-3);
321 else
322 data->offset = BINFO_OFFSET (binfo);
324 return data->repeated_base ? dfs_skip_bases : data->offset;
327 return NULL_TREE;
330 /* Worker for dcast_base_hint. Track the virtual depth. */
332 static tree
333 dfs_dcast_hint_post (tree binfo, void *data_)
335 struct dcast_data_s *data = (struct dcast_data_s *) data_;
337 if (BINFO_VIRTUAL_P (binfo))
338 data->virt_depth--;
340 return NULL_TREE;
343 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
344 started from is related to the required TARGET type, in order to optimize
345 the inheritance graph search. This information is independent of the
346 current context, and ignores private paths, hence get_base_distance is
347 inappropriate. Return a TREE specifying the base offset, BOFF.
348 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
349 and there are no public virtual SUBTYPE bases.
350 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
351 BOFF == -2, SUBTYPE is not a public base.
352 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
354 tree
355 dcast_base_hint (tree subtype, tree target)
357 struct dcast_data_s data;
359 data.subtype = subtype;
360 data.virt_depth = 0;
361 data.offset = NULL_TREE;
362 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
364 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
365 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
366 return data.offset ? data.offset : ssize_int (-2);
369 /* Search for a member with name NAME in a multiple inheritance
370 lattice specified by TYPE. If it does not exist, return NULL_TREE.
371 If the member is ambiguously referenced, return `error_mark_node'.
372 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
373 true, type declarations are preferred. */
375 /* Do a 1-level search for NAME as a member of TYPE. The caller must
376 figure out whether it can access this field. (Since it is only one
377 level, this is reasonable.) */
379 tree
380 lookup_field_1 (tree type, tree name, bool want_type)
382 tree field;
384 gcc_assert (identifier_p (name));
386 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
387 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
388 || TREE_CODE (type) == TYPENAME_TYPE)
389 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
390 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
391 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
392 the code often worked even when we treated the index as a list
393 of fields!)
394 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
395 return NULL_TREE;
397 if (CLASSTYPE_SORTED_FIELDS (type))
399 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
400 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
401 int i;
403 while (lo < hi)
405 i = (lo + hi) / 2;
407 if (GATHER_STATISTICS)
408 n_fields_searched++;
410 if (DECL_NAME (fields[i]) > name)
411 hi = i;
412 else if (DECL_NAME (fields[i]) < name)
413 lo = i + 1;
414 else
416 field = NULL_TREE;
418 /* We might have a nested class and a field with the
419 same name; we sorted them appropriately via
420 field_decl_cmp, so just look for the first or last
421 field with this name. */
422 if (want_type)
425 field = fields[i--];
426 while (i >= lo && DECL_NAME (fields[i]) == name);
427 if (!DECL_DECLARES_TYPE_P (field))
428 field = NULL_TREE;
430 else
433 field = fields[i++];
434 while (i < hi && DECL_NAME (fields[i]) == name);
437 if (field)
439 field = strip_using_decl (field);
440 if (is_overloaded_fn (field))
441 field = NULL_TREE;
444 return field;
447 return NULL_TREE;
450 field = TYPE_FIELDS (type);
452 if (GATHER_STATISTICS)
453 n_calls_lookup_field_1++;
455 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
457 tree decl = field;
459 if (GATHER_STATISTICS)
460 n_fields_searched++;
462 gcc_assert (DECL_P (field));
463 if (DECL_NAME (field) == NULL_TREE
464 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
466 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
467 if (temp)
468 return temp;
471 if (TREE_CODE (decl) == USING_DECL
472 && DECL_NAME (decl) == name)
474 decl = strip_using_decl (decl);
475 if (is_overloaded_fn (decl))
476 continue;
479 if (DECL_NAME (decl) == name
480 && (!want_type || DECL_DECLARES_TYPE_P (decl)))
481 return decl;
483 /* Not found. */
484 if (name == vptr_identifier)
486 /* Give the user what s/he thinks s/he wants. */
487 if (TYPE_POLYMORPHIC_P (type))
488 return TYPE_VFIELD (type);
490 return NULL_TREE;
493 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
494 NAMESPACE_DECL corresponding to the innermost non-block scope. */
496 tree
497 current_scope (void)
499 /* There are a number of cases we need to be aware of here:
500 current_class_type current_function_decl
501 global NULL NULL
502 fn-local NULL SET
503 class-local SET NULL
504 class->fn SET SET
505 fn->class SET SET
507 Those last two make life interesting. If we're in a function which is
508 itself inside a class, we need decls to go into the fn's decls (our
509 second case below). But if we're in a class and the class itself is
510 inside a function, we need decls to go into the decls for the class. To
511 achieve this last goal, we must see if, when both current_class_ptr and
512 current_function_decl are set, the class was declared inside that
513 function. If so, we know to put the decls into the class's scope. */
514 if (current_function_decl && current_class_type
515 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
516 && same_type_p (DECL_CONTEXT (current_function_decl),
517 current_class_type))
518 || (DECL_FRIEND_CONTEXT (current_function_decl)
519 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
520 current_class_type))))
521 return current_function_decl;
522 if (current_class_type)
523 return current_class_type;
524 if (current_function_decl)
525 return current_function_decl;
526 return current_namespace;
529 /* Returns nonzero if we are currently in a function scope. Note
530 that this function returns zero if we are within a local class, but
531 not within a member function body of the local class. */
534 at_function_scope_p (void)
536 tree cs = current_scope ();
537 /* Also check cfun to make sure that we're really compiling
538 this function (as opposed to having set current_function_decl
539 for access checking or some such). */
540 return (cs && TREE_CODE (cs) == FUNCTION_DECL
541 && cfun && cfun->decl == current_function_decl);
544 /* Returns true if the innermost active scope is a class scope. */
546 bool
547 at_class_scope_p (void)
549 tree cs = current_scope ();
550 return cs && TYPE_P (cs);
553 /* Returns true if the innermost active scope is a namespace scope. */
555 bool
556 at_namespace_scope_p (void)
558 tree cs = current_scope ();
559 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
562 /* Return the scope of DECL, as appropriate when doing name-lookup. */
564 tree
565 context_for_name_lookup (tree decl)
567 /* [class.union]
569 For the purposes of name lookup, after the anonymous union
570 definition, the members of the anonymous union are considered to
571 have been defined in the scope in which the anonymous union is
572 declared. */
573 tree context = DECL_CONTEXT (decl);
575 while (context && TYPE_P (context)
576 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
577 context = TYPE_CONTEXT (context);
578 if (!context)
579 context = global_namespace;
581 return context;
584 /* The accessibility routines use BINFO_ACCESS for scratch space
585 during the computation of the accessibility of some declaration. */
587 #define BINFO_ACCESS(NODE) \
588 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
590 /* Set the access associated with NODE to ACCESS. */
592 #define SET_BINFO_ACCESS(NODE, ACCESS) \
593 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
594 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
596 /* Called from access_in_type via dfs_walk. Calculate the access to
597 DATA (which is really a DECL) in BINFO. */
599 static tree
600 dfs_access_in_type (tree binfo, void *data)
602 tree decl = (tree) data;
603 tree type = BINFO_TYPE (binfo);
604 access_kind access = ak_none;
606 if (context_for_name_lookup (decl) == type)
608 /* If we have descended to the scope of DECL, just note the
609 appropriate access. */
610 if (TREE_PRIVATE (decl))
611 access = ak_private;
612 else if (TREE_PROTECTED (decl))
613 access = ak_protected;
614 else
615 access = ak_public;
617 else
619 /* First, check for an access-declaration that gives us more
620 access to the DECL. */
621 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
623 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
625 if (decl_access)
627 decl_access = TREE_VALUE (decl_access);
629 if (decl_access == access_public_node)
630 access = ak_public;
631 else if (decl_access == access_protected_node)
632 access = ak_protected;
633 else if (decl_access == access_private_node)
634 access = ak_private;
635 else
636 gcc_unreachable ();
640 if (!access)
642 int i;
643 tree base_binfo;
644 vec<tree, va_gc> *accesses;
646 /* Otherwise, scan our baseclasses, and pick the most favorable
647 access. */
648 accesses = BINFO_BASE_ACCESSES (binfo);
649 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
651 tree base_access = (*accesses)[i];
652 access_kind base_access_now = BINFO_ACCESS (base_binfo);
654 if (base_access_now == ak_none || base_access_now == ak_private)
655 /* If it was not accessible in the base, or only
656 accessible as a private member, we can't access it
657 all. */
658 base_access_now = ak_none;
659 else if (base_access == access_protected_node)
660 /* Public and protected members in the base become
661 protected here. */
662 base_access_now = ak_protected;
663 else if (base_access == access_private_node)
664 /* Public and protected members in the base become
665 private here. */
666 base_access_now = ak_private;
668 /* See if the new access, via this base, gives more
669 access than our previous best access. */
670 if (base_access_now != ak_none
671 && (access == ak_none || base_access_now < access))
673 access = base_access_now;
675 /* If the new access is public, we can't do better. */
676 if (access == ak_public)
677 break;
683 /* Note the access to DECL in TYPE. */
684 SET_BINFO_ACCESS (binfo, access);
686 return NULL_TREE;
689 /* Return the access to DECL in TYPE. */
691 static access_kind
692 access_in_type (tree type, tree decl)
694 tree binfo = TYPE_BINFO (type);
696 /* We must take into account
698 [class.paths]
700 If a name can be reached by several paths through a multiple
701 inheritance graph, the access is that of the path that gives
702 most access.
704 The algorithm we use is to make a post-order depth-first traversal
705 of the base-class hierarchy. As we come up the tree, we annotate
706 each node with the most lenient access. */
707 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
709 return BINFO_ACCESS (binfo);
712 /* Returns nonzero if it is OK to access DECL through an object
713 indicated by BINFO in the context of DERIVED. */
715 static int
716 protected_accessible_p (tree decl, tree derived, tree binfo)
718 access_kind access;
720 /* We're checking this clause from [class.access.base]
722 m as a member of N is protected, and the reference occurs in a
723 member or friend of class N, or in a member or friend of a
724 class P derived from N, where m as a member of P is public, private
725 or protected.
727 Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N. */
729 /* If DERIVED isn't derived from N, then it can't be a P. */
730 if (!DERIVED_FROM_P (BINFO_TYPE (binfo), derived))
731 return 0;
733 access = access_in_type (derived, decl);
735 /* If m is inaccessible in DERIVED, then it's not a P. */
736 if (access == ak_none)
737 return 0;
739 /* [class.protected]
741 When a friend or a member function of a derived class references
742 a protected nonstatic member of a base class, an access check
743 applies in addition to those described earlier in clause
744 _class.access_) Except when forming a pointer to member
745 (_expr.unary.op_), the access must be through a pointer to,
746 reference to, or object of the derived class itself (or any class
747 derived from that class) (_expr.ref_). If the access is to form
748 a pointer to member, the nested-name-specifier shall name the
749 derived class (or any class derived from that class). */
750 if (DECL_NONSTATIC_MEMBER_P (decl))
752 /* We can tell through what the reference is occurring by
753 chasing BINFO up to the root. */
754 tree t = binfo;
755 while (BINFO_INHERITANCE_CHAIN (t))
756 t = BINFO_INHERITANCE_CHAIN (t);
758 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
759 return 0;
762 return 1;
765 /* Returns nonzero if SCOPE is a friend of a type which would be able
766 to access DECL through the object indicated by BINFO. */
768 static int
769 friend_accessible_p (tree scope, tree decl, tree binfo)
771 tree befriending_classes;
772 tree t;
774 if (!scope)
775 return 0;
777 if (DECL_DECLARES_FUNCTION_P (scope))
778 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
779 else if (TYPE_P (scope))
780 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
781 else
782 return 0;
784 for (t = befriending_classes; t; t = TREE_CHAIN (t))
785 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
786 return 1;
788 /* Nested classes have the same access as their enclosing types, as
789 per DR 45 (this is a change from the standard). */
790 if (TYPE_P (scope))
791 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
792 if (protected_accessible_p (decl, t, binfo))
793 return 1;
795 if (DECL_DECLARES_FUNCTION_P (scope))
797 /* Perhaps this SCOPE is a member of a class which is a
798 friend. */
799 if (DECL_CLASS_SCOPE_P (scope)
800 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
801 return 1;
803 /* Or an instantiation of something which is a friend. */
804 if (DECL_TEMPLATE_INFO (scope))
806 int ret;
807 /* Increment processing_template_decl to make sure that
808 dependent_type_p works correctly. */
809 ++processing_template_decl;
810 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
811 --processing_template_decl;
812 return ret;
816 return 0;
819 /* Called via dfs_walk_once_accessible from accessible_p */
821 static tree
822 dfs_accessible_post (tree binfo, void * /*data*/)
824 if (BINFO_ACCESS (binfo) != ak_none)
826 tree scope = current_scope ();
827 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
828 && is_friend (BINFO_TYPE (binfo), scope))
829 return binfo;
832 return NULL_TREE;
835 /* Like accessible_p below, but within a template returns true iff DECL is
836 accessible in TYPE to all possible instantiations of the template. */
839 accessible_in_template_p (tree type, tree decl)
841 int save_ptd = processing_template_decl;
842 processing_template_decl = 0;
843 int val = accessible_p (type, decl, false);
844 processing_template_decl = save_ptd;
845 return val;
848 /* DECL is a declaration from a base class of TYPE, which was the
849 class used to name DECL. Return nonzero if, in the current
850 context, DECL is accessible. If TYPE is actually a BINFO node,
851 then we can tell in what context the access is occurring by looking
852 at the most derived class along the path indicated by BINFO. If
853 CONSIDER_LOCAL is true, do consider special access the current
854 scope or friendship thereof we might have. */
857 accessible_p (tree type, tree decl, bool consider_local_p)
859 tree binfo;
860 tree scope;
861 access_kind access;
863 /* Nonzero if it's OK to access DECL if it has protected
864 accessibility in TYPE. */
865 int protected_ok = 0;
867 /* If this declaration is in a block or namespace scope, there's no
868 access control. */
869 if (!TYPE_P (context_for_name_lookup (decl)))
870 return 1;
872 /* There is no need to perform access checks inside a thunk. */
873 scope = current_scope ();
874 if (scope && DECL_THUNK_P (scope))
875 return 1;
877 /* In a template declaration, we cannot be sure whether the
878 particular specialization that is instantiated will be a friend
879 or not. Therefore, all access checks are deferred until
880 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
881 parameter list for a template (because we may see dependent types
882 in default arguments for template parameters), and access
883 checking should be performed in the outermost parameter list. */
884 if (processing_template_decl
885 && (!processing_template_parmlist || processing_template_decl > 1))
886 return 1;
888 if (!TYPE_P (type))
890 binfo = type;
891 type = BINFO_TYPE (type);
893 else
894 binfo = TYPE_BINFO (type);
896 /* [class.access.base]
898 A member m is accessible when named in class N if
900 --m as a member of N is public, or
902 --m as a member of N is private, and the reference occurs in a
903 member or friend of class N, or
905 --m as a member of N is protected, and the reference occurs in a
906 member or friend of class N, or in a member or friend of a
907 class P derived from N, where m as a member of P is private or
908 protected, or
910 --there exists a base class B of N that is accessible at the point
911 of reference, and m is accessible when named in class B.
913 We walk the base class hierarchy, checking these conditions. */
915 if (consider_local_p)
917 /* Figure out where the reference is occurring. Check to see if
918 DECL is private or protected in this scope, since that will
919 determine whether protected access is allowed. */
920 if (current_class_type)
921 protected_ok = protected_accessible_p (decl,
922 current_class_type, binfo);
924 /* Now, loop through the classes of which we are a friend. */
925 if (!protected_ok)
926 protected_ok = friend_accessible_p (scope, decl, binfo);
929 /* Standardize the binfo that access_in_type will use. We don't
930 need to know what path was chosen from this point onwards. */
931 binfo = TYPE_BINFO (type);
933 /* Compute the accessibility of DECL in the class hierarchy
934 dominated by type. */
935 access = access_in_type (type, decl);
936 if (access == ak_public
937 || (access == ak_protected && protected_ok))
938 return 1;
940 if (!consider_local_p)
941 return 0;
943 /* Walk the hierarchy again, looking for a base class that allows
944 access. */
945 return dfs_walk_once_accessible (binfo, /*friends=*/true,
946 NULL, dfs_accessible_post, NULL)
947 != NULL_TREE;
950 struct lookup_field_info {
951 /* The type in which we're looking. */
952 tree type;
953 /* The name of the field for which we're looking. */
954 tree name;
955 /* If non-NULL, the current result of the lookup. */
956 tree rval;
957 /* The path to RVAL. */
958 tree rval_binfo;
959 /* If non-NULL, the lookup was ambiguous, and this is a list of the
960 candidates. */
961 tree ambiguous;
962 /* If nonzero, we are looking for types, not data members. */
963 int want_type;
964 /* If something went wrong, a message indicating what. */
965 const char *errstr;
968 /* Nonzero for a class member means that it is shared between all objects
969 of that class.
971 [class.member.lookup]:If the resulting set of declarations are not all
972 from sub-objects of the same type, or the set has a nonstatic member
973 and includes members from distinct sub-objects, there is an ambiguity
974 and the program is ill-formed.
976 This function checks that T contains no nonstatic members. */
979 shared_member_p (tree t)
981 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \
982 || TREE_CODE (t) == CONST_DECL)
983 return 1;
984 if (is_overloaded_fn (t))
986 t = get_fns (t);
987 for (; t; t = OVL_NEXT (t))
989 tree fn = OVL_CURRENT (t);
990 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
991 return 0;
993 return 1;
995 return 0;
998 /* Routine to see if the sub-object denoted by the binfo PARENT can be
999 found as a base class and sub-object of the object denoted by
1000 BINFO. */
1002 static int
1003 is_subobject_of_p (tree parent, tree binfo)
1005 tree probe;
1007 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1009 if (probe == binfo)
1010 return 1;
1011 if (BINFO_VIRTUAL_P (probe))
1012 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1013 != NULL_TREE);
1015 return 0;
1018 /* DATA is really a struct lookup_field_info. Look for a field with
1019 the name indicated there in BINFO. If this function returns a
1020 non-NULL value it is the result of the lookup. Called from
1021 lookup_field via breadth_first_search. */
1023 static tree
1024 lookup_field_r (tree binfo, void *data)
1026 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1027 tree type = BINFO_TYPE (binfo);
1028 tree nval = NULL_TREE;
1030 /* If this is a dependent base, don't look in it. */
1031 if (BINFO_DEPENDENT_BASE_P (binfo))
1032 return NULL_TREE;
1034 /* If this base class is hidden by the best-known value so far, we
1035 don't need to look. */
1036 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1037 && !BINFO_VIRTUAL_P (binfo))
1038 return dfs_skip_bases;
1040 /* First, look for a function. There can't be a function and a data
1041 member with the same name, and if there's a function and a type
1042 with the same name, the type is hidden by the function. */
1043 if (!lfi->want_type)
1044 nval = lookup_fnfields_slot (type, lfi->name);
1046 if (!nval)
1047 /* Look for a data member or type. */
1048 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1050 /* If there is no declaration with the indicated name in this type,
1051 then there's nothing to do. */
1052 if (!nval)
1053 goto done;
1055 /* If we're looking up a type (as with an elaborated type specifier)
1056 we ignore all non-types we find. */
1057 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval))
1059 if (lfi->name == TYPE_IDENTIFIER (type))
1061 /* If the aggregate has no user defined constructors, we allow
1062 it to have fields with the same name as the enclosing type.
1063 If we are looking for that name, find the corresponding
1064 TYPE_DECL. */
1065 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1066 if (DECL_NAME (nval) == lfi->name
1067 && TREE_CODE (nval) == TYPE_DECL)
1068 break;
1070 else
1071 nval = NULL_TREE;
1072 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1074 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1075 lfi->name);
1076 if (e != NULL)
1077 nval = TYPE_MAIN_DECL (e->type);
1078 else
1079 goto done;
1083 /* If the lookup already found a match, and the new value doesn't
1084 hide the old one, we might have an ambiguity. */
1085 if (lfi->rval_binfo
1086 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1089 if (nval == lfi->rval && shared_member_p (nval))
1090 /* The two things are really the same. */
1092 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1093 /* The previous value hides the new one. */
1095 else
1097 /* We have a real ambiguity. We keep a chain of all the
1098 candidates. */
1099 if (!lfi->ambiguous && lfi->rval)
1101 /* This is the first time we noticed an ambiguity. Add
1102 what we previously thought was a reasonable candidate
1103 to the list. */
1104 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1105 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1108 /* Add the new value. */
1109 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1110 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1111 lfi->errstr = G_("request for member %qD is ambiguous");
1114 else
1116 lfi->rval = nval;
1117 lfi->rval_binfo = binfo;
1120 done:
1121 /* Don't look for constructors or destructors in base classes. */
1122 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1123 return dfs_skip_bases;
1124 return NULL_TREE;
1127 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1128 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1129 FUNCTIONS, and OPTYPE respectively. */
1131 tree
1132 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1134 tree baselink;
1136 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1137 || TREE_CODE (functions) == TEMPLATE_DECL
1138 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1139 || TREE_CODE (functions) == OVERLOAD);
1140 gcc_assert (!optype || TYPE_P (optype));
1141 gcc_assert (TREE_TYPE (functions));
1143 baselink = make_node (BASELINK);
1144 TREE_TYPE (baselink) = TREE_TYPE (functions);
1145 BASELINK_BINFO (baselink) = binfo;
1146 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1147 BASELINK_FUNCTIONS (baselink) = functions;
1148 BASELINK_OPTYPE (baselink) = optype;
1150 return baselink;
1153 /* Look for a member named NAME in an inheritance lattice dominated by
1154 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1155 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1156 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1157 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1158 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1159 TREE_VALUEs are the list of ambiguous candidates.
1161 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1163 If nothing can be found return NULL_TREE and do not issue an error. */
1165 tree
1166 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1167 tsubst_flags_t complain)
1169 tree rval, rval_binfo = NULL_TREE;
1170 tree type = NULL_TREE, basetype_path = NULL_TREE;
1171 struct lookup_field_info lfi;
1173 /* rval_binfo is the binfo associated with the found member, note,
1174 this can be set with useful information, even when rval is not
1175 set, because it must deal with ALL members, not just non-function
1176 members. It is used for ambiguity checking and the hidden
1177 checks. Whereas rval is only set if a proper (not hidden)
1178 non-function member is found. */
1180 const char *errstr = 0;
1182 if (name == error_mark_node
1183 || xbasetype == NULL_TREE
1184 || xbasetype == error_mark_node)
1185 return NULL_TREE;
1187 gcc_assert (identifier_p (name));
1189 if (TREE_CODE (xbasetype) == TREE_BINFO)
1191 type = BINFO_TYPE (xbasetype);
1192 basetype_path = xbasetype;
1194 else
1196 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1197 return NULL_TREE;
1198 type = xbasetype;
1199 xbasetype = NULL_TREE;
1202 type = complete_type (type);
1203 if (!basetype_path)
1204 basetype_path = TYPE_BINFO (type);
1206 if (!basetype_path)
1207 return NULL_TREE;
1209 if (GATHER_STATISTICS)
1210 n_calls_lookup_field++;
1212 memset (&lfi, 0, sizeof (lfi));
1213 lfi.type = type;
1214 lfi.name = name;
1215 lfi.want_type = want_type;
1216 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1217 rval = lfi.rval;
1218 rval_binfo = lfi.rval_binfo;
1219 if (rval_binfo)
1220 type = BINFO_TYPE (rval_binfo);
1221 errstr = lfi.errstr;
1223 /* If we are not interested in ambiguities, don't report them;
1224 just return NULL_TREE. */
1225 if (!protect && lfi.ambiguous)
1226 return NULL_TREE;
1228 if (protect == 2)
1230 if (lfi.ambiguous)
1231 return lfi.ambiguous;
1232 else
1233 protect = 0;
1236 /* [class.access]
1238 In the case of overloaded function names, access control is
1239 applied to the function selected by overloaded resolution.
1241 We cannot check here, even if RVAL is only a single non-static
1242 member function, since we do not know what the "this" pointer
1243 will be. For:
1245 class A { protected: void f(); };
1246 class B : public A {
1247 void g(A *p) {
1248 f(); // OK
1249 p->f(); // Not OK.
1253 only the first call to "f" is valid. However, if the function is
1254 static, we can check. */
1255 if (rval && protect
1256 && !really_overloaded_fn (rval))
1258 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1259 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
1260 && !perform_or_defer_access_check (basetype_path, decl, decl,
1261 complain))
1262 rval = error_mark_node;
1265 if (errstr && protect)
1267 if (complain & tf_error)
1269 error (errstr, name, type);
1270 if (lfi.ambiguous)
1271 print_candidates (lfi.ambiguous);
1273 rval = error_mark_node;
1276 if (rval && is_overloaded_fn (rval))
1277 rval = build_baselink (rval_binfo, basetype_path, rval,
1278 (IDENTIFIER_TYPENAME_P (name)
1279 ? TREE_TYPE (name): NULL_TREE));
1280 return rval;
1283 /* Like lookup_member, except that if we find a function member we
1284 return NULL_TREE. */
1286 tree
1287 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1289 tree rval = lookup_member (xbasetype, name, protect, want_type,
1290 tf_warning_or_error);
1292 /* Ignore functions, but propagate the ambiguity list. */
1293 if (!error_operand_p (rval)
1294 && (rval && BASELINK_P (rval)))
1295 return NULL_TREE;
1297 return rval;
1300 /* Like lookup_member, except that if we find a non-function member we
1301 return NULL_TREE. */
1303 tree
1304 lookup_fnfields (tree xbasetype, tree name, int protect)
1306 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1307 tf_warning_or_error);
1309 /* Ignore non-functions, but propagate the ambiguity list. */
1310 if (!error_operand_p (rval)
1311 && (rval && !BASELINK_P (rval)))
1312 return NULL_TREE;
1314 return rval;
1317 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1318 corresponding to "operator TYPE ()", or -1 if there is no such
1319 operator. Only CLASS_TYPE itself is searched; this routine does
1320 not scan the base classes of CLASS_TYPE. */
1322 static int
1323 lookup_conversion_operator (tree class_type, tree type)
1325 int tpl_slot = -1;
1327 if (TYPE_HAS_CONVERSION (class_type))
1329 int i;
1330 tree fn;
1331 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type);
1333 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1334 vec_safe_iterate (methods, i, &fn); ++i)
1336 /* All the conversion operators come near the beginning of
1337 the class. Therefore, if FN is not a conversion
1338 operator, there is no matching conversion operator in
1339 CLASS_TYPE. */
1340 fn = OVL_CURRENT (fn);
1341 if (!DECL_CONV_FN_P (fn))
1342 break;
1344 if (TREE_CODE (fn) == TEMPLATE_DECL)
1345 /* All the templated conversion functions are on the same
1346 slot, so remember it. */
1347 tpl_slot = i;
1348 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1349 return i;
1353 return tpl_slot;
1356 /* TYPE is a class type. Return the index of the fields within
1357 the method vector with name NAME, or -1 if no such field exists.
1358 Does not lazily declare implicitly-declared member functions. */
1360 static int
1361 lookup_fnfields_idx_nolazy (tree type, tree name)
1363 vec<tree, va_gc> *method_vec;
1364 tree fn;
1365 tree tmp;
1366 size_t i;
1368 if (!CLASS_TYPE_P (type))
1369 return -1;
1371 method_vec = CLASSTYPE_METHOD_VEC (type);
1372 if (!method_vec)
1373 return -1;
1375 if (GATHER_STATISTICS)
1376 n_calls_lookup_fnfields_1++;
1378 /* Constructors are first... */
1379 if (name == ctor_identifier)
1381 fn = CLASSTYPE_CONSTRUCTORS (type);
1382 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1384 /* and destructors are second. */
1385 if (name == dtor_identifier)
1387 fn = CLASSTYPE_DESTRUCTORS (type);
1388 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1390 if (IDENTIFIER_TYPENAME_P (name))
1391 return lookup_conversion_operator (type, TREE_TYPE (name));
1393 /* Skip the conversion operators. */
1394 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1395 vec_safe_iterate (method_vec, i, &fn);
1396 ++i)
1397 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1398 break;
1400 /* If the type is complete, use binary search. */
1401 if (COMPLETE_TYPE_P (type))
1403 int lo;
1404 int hi;
1406 lo = i;
1407 hi = method_vec->length ();
1408 while (lo < hi)
1410 i = (lo + hi) / 2;
1412 if (GATHER_STATISTICS)
1413 n_outer_fields_searched++;
1415 tmp = (*method_vec)[i];
1416 tmp = DECL_NAME (OVL_CURRENT (tmp));
1417 if (tmp > name)
1418 hi = i;
1419 else if (tmp < name)
1420 lo = i + 1;
1421 else
1422 return i;
1425 else
1426 for (; vec_safe_iterate (method_vec, i, &fn); ++i)
1428 if (GATHER_STATISTICS)
1429 n_outer_fields_searched++;
1430 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1431 return i;
1434 return -1;
1437 /* TYPE is a class type. Return the index of the fields within
1438 the method vector with name NAME, or -1 if no such field exists. */
1441 lookup_fnfields_1 (tree type, tree name)
1443 if (!CLASS_TYPE_P (type))
1444 return -1;
1446 if (COMPLETE_TYPE_P (type))
1448 if ((name == ctor_identifier
1449 || name == base_ctor_identifier
1450 || name == complete_ctor_identifier))
1452 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1453 lazily_declare_fn (sfk_constructor, type);
1454 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1455 lazily_declare_fn (sfk_copy_constructor, type);
1456 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1457 lazily_declare_fn (sfk_move_constructor, type);
1459 else if (name == ansi_assopname (NOP_EXPR))
1461 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1462 lazily_declare_fn (sfk_copy_assignment, type);
1463 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1464 lazily_declare_fn (sfk_move_assignment, type);
1466 else if ((name == dtor_identifier
1467 || name == base_dtor_identifier
1468 || name == complete_dtor_identifier
1469 || name == deleting_dtor_identifier)
1470 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1471 lazily_declare_fn (sfk_destructor, type);
1474 return lookup_fnfields_idx_nolazy (type, name);
1477 /* TYPE is a class type. Return the field within the method vector with
1478 name NAME, or NULL_TREE if no such field exists. */
1480 tree
1481 lookup_fnfields_slot (tree type, tree name)
1483 int ix = lookup_fnfields_1 (complete_type (type), name);
1484 if (ix < 0)
1485 return NULL_TREE;
1486 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1489 /* As above, but avoid lazily declaring functions. */
1491 tree
1492 lookup_fnfields_slot_nolazy (tree type, tree name)
1494 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1495 if (ix < 0)
1496 return NULL_TREE;
1497 return (*CLASSTYPE_METHOD_VEC (type))[ix];
1500 /* Like lookup_fnfields_1, except that the name is extracted from
1501 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1504 class_method_index_for_fn (tree class_type, tree function)
1506 gcc_assert (DECL_DECLARES_FUNCTION_P (function));
1508 return lookup_fnfields_1 (class_type,
1509 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1510 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1511 DECL_NAME (function));
1515 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1516 the class or namespace used to qualify the name. CONTEXT_CLASS is
1517 the class corresponding to the object in which DECL will be used.
1518 Return a possibly modified version of DECL that takes into account
1519 the CONTEXT_CLASS.
1521 In particular, consider an expression like `B::m' in the context of
1522 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1523 then the most derived class indicated by the BASELINK_BINFO will be
1524 `B', not `D'. This function makes that adjustment. */
1526 tree
1527 adjust_result_of_qualified_name_lookup (tree decl,
1528 tree qualifying_scope,
1529 tree context_class)
1531 if (context_class && context_class != error_mark_node
1532 && CLASS_TYPE_P (context_class)
1533 && CLASS_TYPE_P (qualifying_scope)
1534 && DERIVED_FROM_P (qualifying_scope, context_class)
1535 && BASELINK_P (decl))
1537 tree base;
1539 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1540 Because we do not yet know which function will be chosen by
1541 overload resolution, we cannot yet check either accessibility
1542 or ambiguity -- in either case, the choice of a static member
1543 function might make the usage valid. */
1544 base = lookup_base (context_class, qualifying_scope,
1545 ba_unique, NULL, tf_none);
1546 if (base && base != error_mark_node)
1548 BASELINK_ACCESS_BINFO (decl) = base;
1549 BASELINK_BINFO (decl)
1550 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1551 ba_unique, NULL, tf_none);
1555 if (BASELINK_P (decl))
1556 BASELINK_QUALIFIED_P (decl) = true;
1558 return decl;
1562 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1563 PRE_FN is called in preorder, while POST_FN is called in postorder.
1564 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1565 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1566 that value is immediately returned and the walk is terminated. One
1567 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1568 POST_FN are passed the binfo to examine and the caller's DATA
1569 value. All paths are walked, thus virtual and morally virtual
1570 binfos can be multiply walked. */
1572 tree
1573 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1574 tree (*post_fn) (tree, void *), void *data)
1576 tree rval;
1577 unsigned ix;
1578 tree base_binfo;
1580 /* Call the pre-order walking function. */
1581 if (pre_fn)
1583 rval = pre_fn (binfo, data);
1584 if (rval)
1586 if (rval == dfs_skip_bases)
1587 goto skip_bases;
1588 return rval;
1592 /* Find the next child binfo to walk. */
1593 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1595 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1596 if (rval)
1597 return rval;
1600 skip_bases:
1601 /* Call the post-order walking function. */
1602 if (post_fn)
1604 rval = post_fn (binfo, data);
1605 gcc_assert (rval != dfs_skip_bases);
1606 return rval;
1609 return NULL_TREE;
1612 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1613 that binfos are walked at most once. */
1615 static tree
1616 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1617 tree (*post_fn) (tree, void *), void *data)
1619 tree rval;
1620 unsigned ix;
1621 tree base_binfo;
1623 /* Call the pre-order walking function. */
1624 if (pre_fn)
1626 rval = pre_fn (binfo, data);
1627 if (rval)
1629 if (rval == dfs_skip_bases)
1630 goto skip_bases;
1632 return rval;
1636 /* Find the next child binfo to walk. */
1637 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1639 if (BINFO_VIRTUAL_P (base_binfo))
1641 if (BINFO_MARKED (base_binfo))
1642 continue;
1643 BINFO_MARKED (base_binfo) = 1;
1646 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1647 if (rval)
1648 return rval;
1651 skip_bases:
1652 /* Call the post-order walking function. */
1653 if (post_fn)
1655 rval = post_fn (binfo, data);
1656 gcc_assert (rval != dfs_skip_bases);
1657 return rval;
1660 return NULL_TREE;
1663 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1664 BINFO. */
1666 static void
1667 dfs_unmark_r (tree binfo)
1669 unsigned ix;
1670 tree base_binfo;
1672 /* Process the basetypes. */
1673 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1675 if (BINFO_VIRTUAL_P (base_binfo))
1677 if (!BINFO_MARKED (base_binfo))
1678 continue;
1679 BINFO_MARKED (base_binfo) = 0;
1681 /* Only walk, if it can contain more virtual bases. */
1682 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1683 dfs_unmark_r (base_binfo);
1687 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1688 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1689 For diamond shaped hierarchies we must mark the virtual bases, to
1690 avoid multiple walks. */
1692 tree
1693 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1694 tree (*post_fn) (tree, void *), void *data)
1696 static int active = 0; /* We must not be called recursively. */
1697 tree rval;
1699 gcc_assert (pre_fn || post_fn);
1700 gcc_assert (!active);
1701 active++;
1703 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1704 /* We are not diamond shaped, and therefore cannot encounter the
1705 same binfo twice. */
1706 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1707 else
1709 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1710 if (!BINFO_INHERITANCE_CHAIN (binfo))
1712 /* We are at the top of the hierarchy, and can use the
1713 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1714 bases. */
1715 vec<tree, va_gc> *vbases;
1716 unsigned ix;
1717 tree base_binfo;
1719 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1720 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1721 BINFO_MARKED (base_binfo) = 0;
1723 else
1724 dfs_unmark_r (binfo);
1727 active--;
1729 return rval;
1732 /* Worker function for dfs_walk_once_accessible. Behaves like
1733 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1734 access given by the current context should be considered, (b) ONCE
1735 indicates whether bases should be marked during traversal. */
1737 static tree
1738 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1739 tree (*pre_fn) (tree, void *),
1740 tree (*post_fn) (tree, void *), void *data)
1742 tree rval = NULL_TREE;
1743 unsigned ix;
1744 tree base_binfo;
1746 /* Call the pre-order walking function. */
1747 if (pre_fn)
1749 rval = pre_fn (binfo, data);
1750 if (rval)
1752 if (rval == dfs_skip_bases)
1753 goto skip_bases;
1755 return rval;
1759 /* Find the next child binfo to walk. */
1760 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1762 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1764 if (mark && BINFO_MARKED (base_binfo))
1765 continue;
1767 /* If the base is inherited via private or protected
1768 inheritance, then we can't see it, unless we are a friend of
1769 the current binfo. */
1770 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1772 tree scope;
1773 if (!friends_p)
1774 continue;
1775 scope = current_scope ();
1776 if (!scope
1777 || TREE_CODE (scope) == NAMESPACE_DECL
1778 || !is_friend (BINFO_TYPE (binfo), scope))
1779 continue;
1782 if (mark)
1783 BINFO_MARKED (base_binfo) = 1;
1785 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1786 pre_fn, post_fn, data);
1787 if (rval)
1788 return rval;
1791 skip_bases:
1792 /* Call the post-order walking function. */
1793 if (post_fn)
1795 rval = post_fn (binfo, data);
1796 gcc_assert (rval != dfs_skip_bases);
1797 return rval;
1800 return NULL_TREE;
1803 /* Like dfs_walk_once except that only accessible bases are walked.
1804 FRIENDS_P indicates whether friendship of the local context
1805 should be considered when determining accessibility. */
1807 static tree
1808 dfs_walk_once_accessible (tree binfo, bool friends_p,
1809 tree (*pre_fn) (tree, void *),
1810 tree (*post_fn) (tree, void *), void *data)
1812 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1813 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1814 pre_fn, post_fn, data);
1816 if (diamond_shaped)
1818 if (!BINFO_INHERITANCE_CHAIN (binfo))
1820 /* We are at the top of the hierarchy, and can use the
1821 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1822 bases. */
1823 vec<tree, va_gc> *vbases;
1824 unsigned ix;
1825 tree base_binfo;
1827 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1828 vec_safe_iterate (vbases, ix, &base_binfo); ix++)
1829 BINFO_MARKED (base_binfo) = 0;
1831 else
1832 dfs_unmark_r (binfo);
1834 return rval;
1837 /* Check that virtual overrider OVERRIDER is acceptable for base function
1838 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1840 static int
1841 check_final_overrider (tree overrider, tree basefn)
1843 tree over_type = TREE_TYPE (overrider);
1844 tree base_type = TREE_TYPE (basefn);
1845 tree over_return = fndecl_declared_return_type (overrider);
1846 tree base_return = fndecl_declared_return_type (basefn);
1847 tree over_throw, base_throw;
1849 int fail = 0;
1851 if (DECL_INVALID_OVERRIDER_P (overrider))
1852 return 0;
1854 if (same_type_p (base_return, over_return))
1855 /* OK */;
1856 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1857 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1858 && POINTER_TYPE_P (base_return)))
1860 /* Potentially covariant. */
1861 unsigned base_quals, over_quals;
1863 fail = !POINTER_TYPE_P (base_return);
1864 if (!fail)
1866 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1868 base_return = TREE_TYPE (base_return);
1869 over_return = TREE_TYPE (over_return);
1871 base_quals = cp_type_quals (base_return);
1872 over_quals = cp_type_quals (over_return);
1874 if ((base_quals & over_quals) != over_quals)
1875 fail = 1;
1877 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1879 /* Strictly speaking, the standard requires the return type to be
1880 complete even if it only differs in cv-quals, but that seems
1881 like a bug in the wording. */
1882 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
1883 over_return))
1885 tree binfo = lookup_base (over_return, base_return,
1886 ba_check, NULL, tf_none);
1888 if (!binfo || binfo == error_mark_node)
1889 fail = 1;
1892 else if (can_convert_standard (TREE_TYPE (base_type),
1893 TREE_TYPE (over_type),
1894 tf_warning_or_error))
1895 /* GNU extension, allow trivial pointer conversions such as
1896 converting to void *, or qualification conversion. */
1898 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
1899 "invalid covariant return type for %q#D", overrider))
1900 inform (DECL_SOURCE_LOCATION (basefn),
1901 " overriding %q+#D", basefn);
1903 else
1904 fail = 2;
1906 else
1907 fail = 2;
1908 if (!fail)
1909 /* OK */;
1910 else
1912 if (fail == 1)
1914 error ("invalid covariant return type for %q+#D", overrider);
1915 error (" overriding %q+#D", basefn);
1917 else
1919 error ("conflicting return type specified for %q+#D", overrider);
1920 error (" overriding %q+#D", basefn);
1922 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1923 return 0;
1926 /* Check throw specifier is at least as strict. */
1927 maybe_instantiate_noexcept (basefn);
1928 maybe_instantiate_noexcept (overrider);
1929 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1930 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1932 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1934 error ("looser throw specifier for %q+#F", overrider);
1935 error (" overriding %q+#F", basefn);
1936 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1937 return 0;
1940 /* Check for conflicting type attributes. */
1941 if (!comp_type_attributes (over_type, base_type))
1943 error ("conflicting type attributes specified for %q+#D", overrider);
1944 error (" overriding %q+#D", basefn);
1945 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1946 return 0;
1949 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1951 if (DECL_DELETED_FN (overrider))
1953 error ("deleted function %q+D", overrider);
1954 error ("overriding non-deleted function %q+D", basefn);
1955 maybe_explain_implicit_delete (overrider);
1957 else
1959 error ("non-deleted function %q+D", overrider);
1960 error ("overriding deleted function %q+D", basefn);
1962 return 0;
1964 if (DECL_FINAL_P (basefn))
1966 error ("virtual function %q+D", overrider);
1967 error ("overriding final function %q+D", basefn);
1968 return 0;
1970 return 1;
1973 /* Given a class TYPE, and a function decl FNDECL, look for
1974 virtual functions in TYPE's hierarchy which FNDECL overrides.
1975 We do not look in TYPE itself, only its bases.
1977 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1978 find that it overrides anything.
1980 We check that every function which is overridden, is correctly
1981 overridden. */
1984 look_for_overrides (tree type, tree fndecl)
1986 tree binfo = TYPE_BINFO (type);
1987 tree base_binfo;
1988 int ix;
1989 int found = 0;
1991 /* A constructor for a class T does not override a function T
1992 in a base class. */
1993 if (DECL_CONSTRUCTOR_P (fndecl))
1994 return 0;
1996 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1998 tree basetype = BINFO_TYPE (base_binfo);
2000 if (TYPE_POLYMORPHIC_P (basetype))
2001 found += look_for_overrides_r (basetype, fndecl);
2003 return found;
2006 /* Look in TYPE for virtual functions with the same signature as
2007 FNDECL. */
2009 tree
2010 look_for_overrides_here (tree type, tree fndecl)
2012 int ix;
2014 /* If there are no methods in TYPE (meaning that only implicitly
2015 declared methods will ever be provided for TYPE), then there are
2016 no virtual functions. */
2017 if (!CLASSTYPE_METHOD_VEC (type))
2018 return NULL_TREE;
2020 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2021 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2022 else
2023 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2024 if (ix >= 0)
2026 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
2028 for (; fns; fns = OVL_NEXT (fns))
2030 tree fn = OVL_CURRENT (fns);
2032 if (!DECL_VIRTUAL_P (fn))
2033 /* Not a virtual. */;
2034 else if (DECL_CONTEXT (fn) != type)
2035 /* Introduced with a using declaration. */;
2036 else if (DECL_STATIC_FUNCTION_P (fndecl))
2038 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2039 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2040 if (compparms (TREE_CHAIN (btypes), dtypes))
2041 return fn;
2043 else if (same_signature_p (fndecl, fn))
2044 return fn;
2047 return NULL_TREE;
2050 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2051 TYPE itself and its bases. */
2053 static int
2054 look_for_overrides_r (tree type, tree fndecl)
2056 tree fn = look_for_overrides_here (type, fndecl);
2057 if (fn)
2059 if (DECL_STATIC_FUNCTION_P (fndecl))
2061 /* A static member function cannot match an inherited
2062 virtual member function. */
2063 error ("%q+#D cannot be declared", fndecl);
2064 error (" since %q+#D declared in base class", fn);
2066 else
2068 /* It's definitely virtual, even if not explicitly set. */
2069 DECL_VIRTUAL_P (fndecl) = 1;
2070 check_final_overrider (fndecl, fn);
2072 return 1;
2075 /* We failed to find one declared in this class. Look in its bases. */
2076 return look_for_overrides (type, fndecl);
2079 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2081 static tree
2082 dfs_get_pure_virtuals (tree binfo, void *data)
2084 tree type = (tree) data;
2086 /* We're not interested in primary base classes; the derived class
2087 of which they are a primary base will contain the information we
2088 need. */
2089 if (!BINFO_PRIMARY_P (binfo))
2091 tree virtuals;
2093 for (virtuals = BINFO_VIRTUALS (binfo);
2094 virtuals;
2095 virtuals = TREE_CHAIN (virtuals))
2096 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2097 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2100 return NULL_TREE;
2103 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2105 void
2106 get_pure_virtuals (tree type)
2108 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2109 is going to be overridden. */
2110 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2111 /* Now, run through all the bases which are not primary bases, and
2112 collect the pure virtual functions. We look at the vtable in
2113 each class to determine what pure virtual functions are present.
2114 (A primary base is not interesting because the derived class of
2115 which it is a primary base will contain vtable entries for the
2116 pure virtuals in the base class. */
2117 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2120 /* Debug info for C++ classes can get very large; try to avoid
2121 emitting it everywhere.
2123 Note that this optimization wins even when the target supports
2124 BINCL (if only slightly), and reduces the amount of work for the
2125 linker. */
2127 void
2128 maybe_suppress_debug_info (tree t)
2130 if (write_symbols == NO_DEBUG)
2131 return;
2133 /* We might have set this earlier in cp_finish_decl. */
2134 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2136 /* Always emit the information for each class every time. */
2137 if (flag_emit_class_debug_always)
2138 return;
2140 /* If we already know how we're handling this class, handle debug info
2141 the same way. */
2142 if (CLASSTYPE_INTERFACE_KNOWN (t))
2144 if (CLASSTYPE_INTERFACE_ONLY (t))
2145 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2146 /* else don't set it. */
2148 /* If the class has a vtable, write out the debug info along with
2149 the vtable. */
2150 else if (TYPE_CONTAINS_VPTR_P (t))
2151 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2153 /* Otherwise, just emit the debug info normally. */
2156 /* Note that we want debugging information for a base class of a class
2157 whose vtable is being emitted. Normally, this would happen because
2158 calling the constructor for a derived class implies calling the
2159 constructors for all bases, which involve initializing the
2160 appropriate vptr with the vtable for the base class; but in the
2161 presence of optimization, this initialization may be optimized
2162 away, so we tell finish_vtable_vardecl that we want the debugging
2163 information anyway. */
2165 static tree
2166 dfs_debug_mark (tree binfo, void * /*data*/)
2168 tree t = BINFO_TYPE (binfo);
2170 if (CLASSTYPE_DEBUG_REQUESTED (t))
2171 return dfs_skip_bases;
2173 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2175 return NULL_TREE;
2178 /* Write out the debugging information for TYPE, whose vtable is being
2179 emitted. Also walk through our bases and note that we want to
2180 write out information for them. This avoids the problem of not
2181 writing any debug info for intermediate basetypes whose
2182 constructors, and thus the references to their vtables, and thus
2183 the vtables themselves, were optimized away. */
2185 void
2186 note_debug_info_needed (tree type)
2188 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2190 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2191 rest_of_type_compilation (type, toplevel_bindings_p ());
2194 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2197 void
2198 print_search_statistics (void)
2200 if (! GATHER_STATISTICS)
2202 fprintf (stderr, "no search statistics\n");
2203 return;
2206 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2207 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2208 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2209 n_outer_fields_searched, n_calls_lookup_fnfields);
2210 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2213 void
2214 reinit_search_statistics (void)
2216 n_fields_searched = 0;
2217 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2218 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2219 n_calls_get_base_type = 0;
2220 n_outer_fields_searched = 0;
2221 n_contexts_saved = 0;
2224 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2225 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2226 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2227 bases have been encountered already in the tree walk. PARENT_CONVS
2228 is the list of lists of conversion functions that could hide CONV
2229 and OTHER_CONVS is the list of lists of conversion functions that
2230 could hide or be hidden by CONV, should virtualness be involved in
2231 the hierarchy. Merely checking the conversion op's name is not
2232 enough because two conversion operators to the same type can have
2233 different names. Return nonzero if we are visible. */
2235 static int
2236 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2237 tree to_type, tree parent_convs, tree other_convs)
2239 tree level, probe;
2241 /* See if we are hidden by a parent conversion. */
2242 for (level = parent_convs; level; level = TREE_CHAIN (level))
2243 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2244 if (same_type_p (to_type, TREE_TYPE (probe)))
2245 return 0;
2247 if (virtual_depth || virtualness)
2249 /* In a virtual hierarchy, we could be hidden, or could hide a
2250 conversion function on the other_convs list. */
2251 for (level = other_convs; level; level = TREE_CHAIN (level))
2253 int we_hide_them;
2254 int they_hide_us;
2255 tree *prev, other;
2257 if (!(virtual_depth || TREE_STATIC (level)))
2258 /* Neither is morally virtual, so cannot hide each other. */
2259 continue;
2261 if (!TREE_VALUE (level))
2262 /* They evaporated away already. */
2263 continue;
2265 they_hide_us = (virtual_depth
2266 && original_binfo (binfo, TREE_PURPOSE (level)));
2267 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2268 && original_binfo (TREE_PURPOSE (level), binfo));
2270 if (!(we_hide_them || they_hide_us))
2271 /* Neither is within the other, so no hiding can occur. */
2272 continue;
2274 for (prev = &TREE_VALUE (level), other = *prev; other;)
2276 if (same_type_p (to_type, TREE_TYPE (other)))
2278 if (they_hide_us)
2279 /* We are hidden. */
2280 return 0;
2282 if (we_hide_them)
2284 /* We hide the other one. */
2285 other = TREE_CHAIN (other);
2286 *prev = other;
2287 continue;
2290 prev = &TREE_CHAIN (other);
2291 other = *prev;
2295 return 1;
2298 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2299 of conversion functions, the first slot will be for the current
2300 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2301 of conversion functions from children of the current binfo,
2302 concatenated with conversions from elsewhere in the hierarchy --
2303 that list begins with OTHER_CONVS. Return a single list of lists
2304 containing only conversions from the current binfo and its
2305 children. */
2307 static tree
2308 split_conversions (tree my_convs, tree parent_convs,
2309 tree child_convs, tree other_convs)
2311 tree t;
2312 tree prev;
2314 /* Remove the original other_convs portion from child_convs. */
2315 for (prev = NULL, t = child_convs;
2316 t != other_convs; prev = t, t = TREE_CHAIN (t))
2317 continue;
2319 if (prev)
2320 TREE_CHAIN (prev) = NULL_TREE;
2321 else
2322 child_convs = NULL_TREE;
2324 /* Attach the child convs to any we had at this level. */
2325 if (my_convs)
2327 my_convs = parent_convs;
2328 TREE_CHAIN (my_convs) = child_convs;
2330 else
2331 my_convs = child_convs;
2333 return my_convs;
2336 /* Worker for lookup_conversions. Lookup conversion functions in
2337 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2338 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2339 encountered virtual bases already in the tree walk. PARENT_CONVS &
2340 PARENT_TPL_CONVS are lists of list of conversions within parent
2341 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2342 elsewhere in the tree. Return the conversions found within this
2343 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2344 encountered virtualness. We keep template and non-template
2345 conversions separate, to avoid unnecessary type comparisons.
2347 The located conversion functions are held in lists of lists. The
2348 TREE_VALUE of the outer list is the list of conversion functions
2349 found in a particular binfo. The TREE_PURPOSE of both the outer
2350 and inner lists is the binfo at which those conversions were
2351 found. TREE_STATIC is set for those lists within of morally
2352 virtual binfos. The TREE_VALUE of the inner list is the conversion
2353 function or overload itself. The TREE_TYPE of each inner list node
2354 is the converted-to type. */
2356 static int
2357 lookup_conversions_r (tree binfo,
2358 int virtual_depth, int virtualness,
2359 tree parent_convs, tree parent_tpl_convs,
2360 tree other_convs, tree other_tpl_convs,
2361 tree *convs, tree *tpl_convs)
2363 int my_virtualness = 0;
2364 tree my_convs = NULL_TREE;
2365 tree my_tpl_convs = NULL_TREE;
2366 tree child_convs = NULL_TREE;
2367 tree child_tpl_convs = NULL_TREE;
2368 unsigned i;
2369 tree base_binfo;
2370 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2371 tree conv;
2373 /* If we have no conversion operators, then don't look. */
2374 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2376 *convs = *tpl_convs = NULL_TREE;
2378 return 0;
2381 if (BINFO_VIRTUAL_P (binfo))
2382 virtual_depth++;
2384 /* First, locate the unhidden ones at this level. */
2385 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2386 vec_safe_iterate (method_vec, i, &conv);
2387 ++i)
2389 tree cur = OVL_CURRENT (conv);
2391 if (!DECL_CONV_FN_P (cur))
2392 break;
2394 if (TREE_CODE (cur) == TEMPLATE_DECL)
2396 /* Only template conversions can be overloaded, and we must
2397 flatten them out and check each one individually. */
2398 tree tpls;
2400 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2402 tree tpl = OVL_CURRENT (tpls);
2403 tree type = DECL_CONV_FN_TYPE (tpl);
2405 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2406 type, parent_tpl_convs, other_tpl_convs))
2408 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2409 TREE_TYPE (my_tpl_convs) = type;
2410 if (virtual_depth)
2412 TREE_STATIC (my_tpl_convs) = 1;
2413 my_virtualness = 1;
2418 else
2420 tree name = DECL_NAME (cur);
2422 if (!IDENTIFIER_MARKED (name))
2424 tree type = DECL_CONV_FN_TYPE (cur);
2425 if (type_uses_auto (type))
2427 mark_used (cur);
2428 type = DECL_CONV_FN_TYPE (cur);
2431 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2432 type, parent_convs, other_convs))
2434 my_convs = tree_cons (binfo, conv, my_convs);
2435 TREE_TYPE (my_convs) = type;
2436 if (virtual_depth)
2438 TREE_STATIC (my_convs) = 1;
2439 my_virtualness = 1;
2441 IDENTIFIER_MARKED (name) = 1;
2447 if (my_convs)
2449 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2450 if (virtual_depth)
2451 TREE_STATIC (parent_convs) = 1;
2454 if (my_tpl_convs)
2456 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2457 if (virtual_depth)
2458 TREE_STATIC (parent_tpl_convs) = 1;
2461 child_convs = other_convs;
2462 child_tpl_convs = other_tpl_convs;
2464 /* Now iterate over each base, looking for more conversions. */
2465 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2467 tree base_convs, base_tpl_convs;
2468 unsigned base_virtualness;
2470 base_virtualness = lookup_conversions_r (base_binfo,
2471 virtual_depth, virtualness,
2472 parent_convs, parent_tpl_convs,
2473 child_convs, child_tpl_convs,
2474 &base_convs, &base_tpl_convs);
2475 if (base_virtualness)
2476 my_virtualness = virtualness = 1;
2477 child_convs = chainon (base_convs, child_convs);
2478 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2481 /* Unmark the conversions found at this level */
2482 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2483 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2485 *convs = split_conversions (my_convs, parent_convs,
2486 child_convs, other_convs);
2487 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2488 child_tpl_convs, other_tpl_convs);
2490 return my_virtualness;
2493 /* Return a TREE_LIST containing all the non-hidden user-defined
2494 conversion functions for TYPE (and its base-classes). The
2495 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2496 function. The TREE_PURPOSE is the BINFO from which the conversion
2497 functions in this node were selected. This function is effectively
2498 performing a set of member lookups as lookup_fnfield does, but
2499 using the type being converted to as the unique key, rather than the
2500 field name. */
2502 tree
2503 lookup_conversions (tree type)
2505 tree convs, tpl_convs;
2506 tree list = NULL_TREE;
2508 complete_type (type);
2509 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2510 return NULL_TREE;
2512 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2513 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2514 &convs, &tpl_convs);
2516 /* Flatten the list-of-lists */
2517 for (; convs; convs = TREE_CHAIN (convs))
2519 tree probe, next;
2521 for (probe = TREE_VALUE (convs); probe; probe = next)
2523 next = TREE_CHAIN (probe);
2525 TREE_CHAIN (probe) = list;
2526 list = probe;
2530 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2532 tree probe, next;
2534 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2536 next = TREE_CHAIN (probe);
2538 TREE_CHAIN (probe) = list;
2539 list = probe;
2543 return list;
2546 /* Returns the binfo of the first direct or indirect virtual base derived
2547 from BINFO, or NULL if binfo is not via virtual. */
2549 tree
2550 binfo_from_vbase (tree binfo)
2552 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2554 if (BINFO_VIRTUAL_P (binfo))
2555 return binfo;
2557 return NULL_TREE;
2560 /* Returns the binfo of the first direct or indirect virtual base derived
2561 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2562 via virtual. */
2564 tree
2565 binfo_via_virtual (tree binfo, tree limit)
2567 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2568 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2569 return NULL_TREE;
2571 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2572 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2574 if (BINFO_VIRTUAL_P (binfo))
2575 return binfo;
2577 return NULL_TREE;
2580 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2581 Find the equivalent binfo within whatever graph HERE is located.
2582 This is the inverse of original_binfo. */
2584 tree
2585 copied_binfo (tree binfo, tree here)
2587 tree result = NULL_TREE;
2589 if (BINFO_VIRTUAL_P (binfo))
2591 tree t;
2593 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2594 t = BINFO_INHERITANCE_CHAIN (t))
2595 continue;
2597 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2599 else if (BINFO_INHERITANCE_CHAIN (binfo))
2601 tree cbinfo;
2602 tree base_binfo;
2603 int ix;
2605 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2606 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2607 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2609 result = base_binfo;
2610 break;
2613 else
2615 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2616 result = here;
2619 gcc_assert (result);
2620 return result;
2623 tree
2624 binfo_for_vbase (tree base, tree t)
2626 unsigned ix;
2627 tree binfo;
2628 vec<tree, va_gc> *vbases;
2630 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2631 vec_safe_iterate (vbases, ix, &binfo); ix++)
2632 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2633 return binfo;
2634 return NULL;
2637 /* BINFO is some base binfo of HERE, within some other
2638 hierarchy. Return the equivalent binfo, but in the hierarchy
2639 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2640 is not a base binfo of HERE, returns NULL_TREE. */
2642 tree
2643 original_binfo (tree binfo, tree here)
2645 tree result = NULL;
2647 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2648 result = here;
2649 else if (BINFO_VIRTUAL_P (binfo))
2650 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2651 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2652 : NULL_TREE);
2653 else if (BINFO_INHERITANCE_CHAIN (binfo))
2655 tree base_binfos;
2657 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2658 if (base_binfos)
2660 int ix;
2661 tree base_binfo;
2663 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2664 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2665 BINFO_TYPE (binfo)))
2667 result = base_binfo;
2668 break;
2673 return result;