ChangeLog entry:
[official-gcc.git] / gcc / cp / search.c
blob19ef5967a80c8b3130384ecb9ffb1c95742207eb
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011,
5 2012
6 Free Software Foundation, Inc.
7 Contributed by Michael Tiemann (tiemann@cygnus.com)
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3, or (at your option)
14 any later version.
16 GCC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "flags.h"
35 #include "output.h"
36 #include "toplev.h"
37 #include "target.h"
39 static int is_subobject_of_p (tree, tree);
40 static tree dfs_lookup_base (tree, void *);
41 static tree dfs_dcast_hint_pre (tree, void *);
42 static tree dfs_dcast_hint_post (tree, void *);
43 static tree dfs_debug_mark (tree, void *);
44 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
45 tree (*post_fn) (tree, void *), void *data);
46 static void dfs_unmark_r (tree);
47 static int check_hidden_convs (tree, int, int, tree, tree, tree);
48 static tree split_conversions (tree, tree, tree, tree);
49 static int lookup_conversions_r (tree, int, int,
50 tree, tree, tree, tree, tree *, tree *);
51 static int look_for_overrides_r (tree, tree);
52 static tree lookup_field_r (tree, void *);
53 static tree dfs_accessible_post (tree, void *);
54 static tree dfs_walk_once_accessible_r (tree, bool, bool,
55 tree (*pre_fn) (tree, void *),
56 tree (*post_fn) (tree, void *),
57 void *data);
58 static tree dfs_walk_once_accessible (tree, bool,
59 tree (*pre_fn) (tree, void *),
60 tree (*post_fn) (tree, void *),
61 void *data);
62 static tree dfs_access_in_type (tree, void *);
63 static access_kind access_in_type (tree, tree);
64 static int protected_accessible_p (tree, tree, tree);
65 static int friend_accessible_p (tree, tree, tree);
66 static tree dfs_get_pure_virtuals (tree, void *);
69 /* Variables for gathering statistics. */
70 #ifdef GATHER_STATISTICS
71 static int n_fields_searched;
72 static int n_calls_lookup_field, n_calls_lookup_field_1;
73 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
74 static int n_calls_get_base_type;
75 static int n_outer_fields_searched;
76 static int n_contexts_saved;
77 #endif /* GATHER_STATISTICS */
80 /* Data for lookup_base and its workers. */
82 struct lookup_base_data_s
84 tree t; /* type being searched. */
85 tree base; /* The base type we're looking for. */
86 tree binfo; /* Found binfo. */
87 bool via_virtual; /* Found via a virtual path. */
88 bool ambiguous; /* Found multiply ambiguous */
89 bool repeated_base; /* Whether there are repeated bases in the
90 hierarchy. */
91 bool want_any; /* Whether we want any matching binfo. */
94 /* Worker function for lookup_base. See if we've found the desired
95 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
97 static tree
98 dfs_lookup_base (tree binfo, void *data_)
100 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
102 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
104 if (!data->binfo)
106 data->binfo = binfo;
107 data->via_virtual
108 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
110 if (!data->repeated_base)
111 /* If there are no repeated bases, we can stop now. */
112 return binfo;
114 if (data->want_any && !data->via_virtual)
115 /* If this is a non-virtual base, then we can't do
116 better. */
117 return binfo;
119 return dfs_skip_bases;
121 else
123 gcc_assert (binfo != data->binfo);
125 /* We've found more than one matching binfo. */
126 if (!data->want_any)
128 /* This is immediately ambiguous. */
129 data->binfo = NULL_TREE;
130 data->ambiguous = true;
131 return error_mark_node;
134 /* Prefer one via a non-virtual path. */
135 if (!binfo_via_virtual (binfo, data->t))
137 data->binfo = binfo;
138 data->via_virtual = false;
139 return binfo;
142 /* There must be repeated bases, otherwise we'd have stopped
143 on the first base we found. */
144 return dfs_skip_bases;
148 return NULL_TREE;
151 /* Returns true if type BASE is accessible in T. (BASE is known to be
152 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
153 true, consider any special access of the current scope, or access
154 bestowed by friendship. */
156 bool
157 accessible_base_p (tree t, tree base, bool consider_local_p)
159 tree decl;
161 /* [class.access.base]
163 A base class is said to be accessible if an invented public
164 member of the base class is accessible.
166 If BASE is a non-proper base, this condition is trivially
167 true. */
168 if (same_type_p (t, base))
169 return true;
170 /* Rather than inventing a public member, we use the implicit
171 public typedef created in the scope of every class. */
172 decl = TYPE_FIELDS (base);
173 while (!DECL_SELF_REFERENCE_P (decl))
174 decl = DECL_CHAIN (decl);
175 while (ANON_AGGR_TYPE_P (t))
176 t = TYPE_CONTEXT (t);
177 return accessible_p (t, decl, consider_local_p);
180 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
181 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
182 non-NULL, fill with information about what kind of base we
183 discovered.
185 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
186 not set in ACCESS, then an error is issued and error_mark_node is
187 returned. If the ba_quiet bit is set, then no error is issued and
188 NULL_TREE is returned. */
190 tree
191 lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
193 tree binfo;
194 tree t_binfo;
195 base_kind bk;
197 if (t == error_mark_node || base == error_mark_node)
199 if (kind_ptr)
200 *kind_ptr = bk_not_base;
201 return error_mark_node;
203 gcc_assert (TYPE_P (base));
205 if (!TYPE_P (t))
207 t_binfo = t;
208 t = BINFO_TYPE (t);
210 else
212 t = complete_type (TYPE_MAIN_VARIANT (t));
213 t_binfo = TYPE_BINFO (t);
216 base = TYPE_MAIN_VARIANT (base);
218 /* If BASE is incomplete, it can't be a base of T--and instantiating it
219 might cause an error. */
220 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
222 struct lookup_base_data_s data;
224 data.t = t;
225 data.base = base;
226 data.binfo = NULL_TREE;
227 data.ambiguous = data.via_virtual = false;
228 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
229 data.want_any = access == ba_any;
231 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
232 binfo = data.binfo;
234 if (!binfo)
235 bk = data.ambiguous ? bk_ambig : bk_not_base;
236 else if (binfo == t_binfo)
237 bk = bk_same_type;
238 else if (data.via_virtual)
239 bk = bk_via_virtual;
240 else
241 bk = bk_proper_base;
243 else
245 binfo = NULL_TREE;
246 bk = bk_not_base;
249 /* Check that the base is unambiguous and accessible. */
250 if (access != ba_any)
251 switch (bk)
253 case bk_not_base:
254 break;
256 case bk_ambig:
257 if (!(access & ba_quiet))
259 error ("%qT is an ambiguous base of %qT", base, t);
260 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 (!(access & ba_quiet))
277 error ("%qT is an inaccessible base of %qT", base, t);
278 binfo = error_mark_node;
280 else
281 binfo = NULL_TREE;
282 bk = bk_inaccessible;
284 break;
287 if (kind_ptr)
288 *kind_ptr = bk;
290 return binfo;
293 /* Data for dcast_base_hint walker. */
295 struct dcast_data_s
297 tree subtype; /* The base type we're looking for. */
298 int virt_depth; /* Number of virtual bases encountered from most
299 derived. */
300 tree offset; /* Best hint offset discovered so far. */
301 bool repeated_base; /* Whether there are repeated bases in the
302 hierarchy. */
305 /* Worker for dcast_base_hint. Search for the base type being cast
306 from. */
308 static tree
309 dfs_dcast_hint_pre (tree binfo, void *data_)
311 struct dcast_data_s *data = (struct dcast_data_s *) data_;
313 if (BINFO_VIRTUAL_P (binfo))
314 data->virt_depth++;
316 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
318 if (data->virt_depth)
320 data->offset = ssize_int (-1);
321 return data->offset;
323 if (data->offset)
324 data->offset = ssize_int (-3);
325 else
326 data->offset = BINFO_OFFSET (binfo);
328 return data->repeated_base ? dfs_skip_bases : data->offset;
331 return NULL_TREE;
334 /* Worker for dcast_base_hint. Track the virtual depth. */
336 static tree
337 dfs_dcast_hint_post (tree binfo, void *data_)
339 struct dcast_data_s *data = (struct dcast_data_s *) data_;
341 if (BINFO_VIRTUAL_P (binfo))
342 data->virt_depth--;
344 return NULL_TREE;
347 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
348 started from is related to the required TARGET type, in order to optimize
349 the inheritance graph search. This information is independent of the
350 current context, and ignores private paths, hence get_base_distance is
351 inappropriate. Return a TREE specifying the base offset, BOFF.
352 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
353 and there are no public virtual SUBTYPE bases.
354 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
355 BOFF == -2, SUBTYPE is not a public base.
356 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
358 tree
359 dcast_base_hint (tree subtype, tree target)
361 struct dcast_data_s data;
363 data.subtype = subtype;
364 data.virt_depth = 0;
365 data.offset = NULL_TREE;
366 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
368 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
369 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
370 return data.offset ? data.offset : ssize_int (-2);
373 /* Search for a member with name NAME in a multiple inheritance
374 lattice specified by TYPE. If it does not exist, return NULL_TREE.
375 If the member is ambiguously referenced, return `error_mark_node'.
376 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
377 true, type declarations are preferred. */
379 /* Do a 1-level search for NAME as a member of TYPE. The caller must
380 figure out whether it can access this field. (Since it is only one
381 level, this is reasonable.) */
383 tree
384 lookup_field_1 (tree type, tree name, bool want_type)
386 tree field;
388 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
390 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
391 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
392 || TREE_CODE (type) == TYPENAME_TYPE)
393 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
394 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
395 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
396 the code often worked even when we treated the index as a list
397 of fields!)
398 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
399 return NULL_TREE;
401 if (CLASSTYPE_SORTED_FIELDS (type))
403 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
404 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
405 int i;
407 while (lo < hi)
409 i = (lo + hi) / 2;
411 #ifdef GATHER_STATISTICS
412 n_fields_searched++;
413 #endif /* GATHER_STATISTICS */
415 if (DECL_NAME (fields[i]) > name)
416 hi = i;
417 else if (DECL_NAME (fields[i]) < name)
418 lo = i + 1;
419 else
421 field = NULL_TREE;
423 /* We might have a nested class and a field with the
424 same name; we sorted them appropriately via
425 field_decl_cmp, so just look for the first or last
426 field with this name. */
427 if (want_type)
430 field = fields[i--];
431 while (i >= lo && DECL_NAME (fields[i]) == name);
432 if (TREE_CODE (field) != TYPE_DECL
433 && !DECL_TYPE_TEMPLATE_P (field))
434 field = NULL_TREE;
436 else
439 field = fields[i++];
440 while (i < hi && DECL_NAME (fields[i]) == name);
443 if (field)
445 field = strip_using_decl (field);
446 if (is_overloaded_fn (field))
447 field = NULL_TREE;
450 return field;
453 return NULL_TREE;
456 field = TYPE_FIELDS (type);
458 #ifdef GATHER_STATISTICS
459 n_calls_lookup_field_1++;
460 #endif /* GATHER_STATISTICS */
461 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
463 tree decl = field;
465 #ifdef GATHER_STATISTICS
466 n_fields_searched++;
467 #endif /* GATHER_STATISTICS */
468 gcc_assert (DECL_P (field));
469 if (DECL_NAME (field) == NULL_TREE
470 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
472 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
473 if (temp)
474 return temp;
477 if (TREE_CODE (decl) == USING_DECL
478 && DECL_NAME (decl) == name)
480 decl = strip_using_decl (decl);
481 if (is_overloaded_fn (decl))
482 continue;
485 if (DECL_NAME (decl) == name
486 && (!want_type
487 || TREE_CODE (decl) == TYPE_DECL
488 || DECL_TYPE_TEMPLATE_P (decl)))
489 return decl;
491 /* Not found. */
492 if (name == vptr_identifier)
494 /* Give the user what s/he thinks s/he wants. */
495 if (TYPE_POLYMORPHIC_P (type))
496 return TYPE_VFIELD (type);
498 return NULL_TREE;
501 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
502 NAMESPACE_DECL corresponding to the innermost non-block scope. */
504 tree
505 current_scope (void)
507 /* There are a number of cases we need to be aware of here:
508 current_class_type current_function_decl
509 global NULL NULL
510 fn-local NULL SET
511 class-local SET NULL
512 class->fn SET SET
513 fn->class SET SET
515 Those last two make life interesting. If we're in a function which is
516 itself inside a class, we need decls to go into the fn's decls (our
517 second case below). But if we're in a class and the class itself is
518 inside a function, we need decls to go into the decls for the class. To
519 achieve this last goal, we must see if, when both current_class_ptr and
520 current_function_decl are set, the class was declared inside that
521 function. If so, we know to put the decls into the class's scope. */
522 if (current_function_decl && current_class_type
523 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
524 && same_type_p (DECL_CONTEXT (current_function_decl),
525 current_class_type))
526 || (DECL_FRIEND_CONTEXT (current_function_decl)
527 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
528 current_class_type))))
529 return current_function_decl;
530 if (current_class_type)
531 return current_class_type;
532 if (current_function_decl)
533 return current_function_decl;
534 return current_namespace;
537 /* Returns nonzero if we are currently in a function scope. Note
538 that this function returns zero if we are within a local class, but
539 not within a member function body of the local class. */
542 at_function_scope_p (void)
544 tree cs = current_scope ();
545 /* Also check cfun to make sure that we're really compiling
546 this function (as opposed to having set current_function_decl
547 for access checking or some such). */
548 return (cs && TREE_CODE (cs) == FUNCTION_DECL
549 && cfun && cfun->decl == current_function_decl);
552 /* Returns true if the innermost active scope is a class scope. */
554 bool
555 at_class_scope_p (void)
557 tree cs = current_scope ();
558 return cs && TYPE_P (cs);
561 /* Returns true if the innermost active scope is a namespace scope. */
563 bool
564 at_namespace_scope_p (void)
566 tree cs = current_scope ();
567 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
570 /* Return the scope of DECL, as appropriate when doing name-lookup. */
572 tree
573 context_for_name_lookup (tree decl)
575 /* [class.union]
577 For the purposes of name lookup, after the anonymous union
578 definition, the members of the anonymous union are considered to
579 have been defined in the scope in which the anonymous union is
580 declared. */
581 tree context = DECL_CONTEXT (decl);
583 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
584 context = TYPE_CONTEXT (context);
585 if (!context)
586 context = global_namespace;
588 return context;
591 /* The accessibility routines use BINFO_ACCESS for scratch space
592 during the computation of the accessibility of some declaration. */
594 #define BINFO_ACCESS(NODE) \
595 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
597 /* Set the access associated with NODE to ACCESS. */
599 #define SET_BINFO_ACCESS(NODE, ACCESS) \
600 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
601 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
603 /* Called from access_in_type via dfs_walk. Calculate the access to
604 DATA (which is really a DECL) in BINFO. */
606 static tree
607 dfs_access_in_type (tree binfo, void *data)
609 tree decl = (tree) data;
610 tree type = BINFO_TYPE (binfo);
611 access_kind access = ak_none;
613 if (context_for_name_lookup (decl) == type)
615 /* If we have descended to the scope of DECL, just note the
616 appropriate access. */
617 if (TREE_PRIVATE (decl))
618 access = ak_private;
619 else if (TREE_PROTECTED (decl))
620 access = ak_protected;
621 else
622 access = ak_public;
624 else
626 /* First, check for an access-declaration that gives us more
627 access to the DECL. The CONST_DECL for an enumeration
628 constant will not have DECL_LANG_SPECIFIC, and thus no
629 DECL_ACCESS. */
630 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
632 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
634 if (decl_access)
636 decl_access = TREE_VALUE (decl_access);
638 if (decl_access == access_public_node)
639 access = ak_public;
640 else if (decl_access == access_protected_node)
641 access = ak_protected;
642 else if (decl_access == access_private_node)
643 access = ak_private;
644 else
645 gcc_unreachable ();
649 if (!access)
651 int i;
652 tree base_binfo;
653 VEC(tree,gc) *accesses;
655 /* Otherwise, scan our baseclasses, and pick the most favorable
656 access. */
657 accesses = BINFO_BASE_ACCESSES (binfo);
658 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
660 tree base_access = VEC_index (tree, accesses, i);
661 access_kind base_access_now = BINFO_ACCESS (base_binfo);
663 if (base_access_now == ak_none || base_access_now == ak_private)
664 /* If it was not accessible in the base, or only
665 accessible as a private member, we can't access it
666 all. */
667 base_access_now = ak_none;
668 else if (base_access == access_protected_node)
669 /* Public and protected members in the base become
670 protected here. */
671 base_access_now = ak_protected;
672 else if (base_access == access_private_node)
673 /* Public and protected members in the base become
674 private here. */
675 base_access_now = ak_private;
677 /* See if the new access, via this base, gives more
678 access than our previous best access. */
679 if (base_access_now != ak_none
680 && (access == ak_none || base_access_now < access))
682 access = base_access_now;
684 /* If the new access is public, we can't do better. */
685 if (access == ak_public)
686 break;
692 /* Note the access to DECL in TYPE. */
693 SET_BINFO_ACCESS (binfo, access);
695 return NULL_TREE;
698 /* Return the access to DECL in TYPE. */
700 static access_kind
701 access_in_type (tree type, tree decl)
703 tree binfo = TYPE_BINFO (type);
705 /* We must take into account
707 [class.paths]
709 If a name can be reached by several paths through a multiple
710 inheritance graph, the access is that of the path that gives
711 most access.
713 The algorithm we use is to make a post-order depth-first traversal
714 of the base-class hierarchy. As we come up the tree, we annotate
715 each node with the most lenient access. */
716 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
718 return BINFO_ACCESS (binfo);
721 /* Returns nonzero if it is OK to access DECL through an object
722 indicated by BINFO in the context of DERIVED. */
724 static int
725 protected_accessible_p (tree decl, tree derived, tree binfo)
727 access_kind access;
729 /* We're checking this clause from [class.access.base]
731 m as a member of N is protected, and the reference occurs in a
732 member or friend of class N, or in a member or friend of a
733 class P derived from N, where m as a member of P is public, private
734 or protected.
736 Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N. */
738 /* If DERIVED isn't derived from N, then it can't be a P. */
739 if (!DERIVED_FROM_P (BINFO_TYPE (binfo), derived))
740 return 0;
742 access = access_in_type (derived, decl);
744 /* If m is inaccessible in DERIVED, then it's not a P. */
745 if (access == ak_none)
746 return 0;
748 /* [class.protected]
750 When a friend or a member function of a derived class references
751 a protected nonstatic member of a base class, an access check
752 applies in addition to those described earlier in clause
753 _class.access_) Except when forming a pointer to member
754 (_expr.unary.op_), the access must be through a pointer to,
755 reference to, or object of the derived class itself (or any class
756 derived from that class) (_expr.ref_). If the access is to form
757 a pointer to member, the nested-name-specifier shall name the
758 derived class (or any class derived from that class). */
759 if (DECL_NONSTATIC_MEMBER_P (decl))
761 /* We can tell through what the reference is occurring by
762 chasing BINFO up to the root. */
763 tree t = binfo;
764 while (BINFO_INHERITANCE_CHAIN (t))
765 t = BINFO_INHERITANCE_CHAIN (t);
767 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
768 return 0;
771 return 1;
774 /* Returns nonzero if SCOPE is a friend of a type which would be able
775 to access DECL through the object indicated by BINFO. */
777 static int
778 friend_accessible_p (tree scope, tree decl, tree binfo)
780 tree befriending_classes;
781 tree t;
783 if (!scope)
784 return 0;
786 if (TREE_CODE (scope) == FUNCTION_DECL
787 || DECL_FUNCTION_TEMPLATE_P (scope))
788 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
789 else if (TYPE_P (scope))
790 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
791 else
792 return 0;
794 for (t = befriending_classes; t; t = TREE_CHAIN (t))
795 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
796 return 1;
798 /* Nested classes have the same access as their enclosing types, as
799 per DR 45 (this is a change from the standard). */
800 if (TYPE_P (scope))
801 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
802 if (protected_accessible_p (decl, t, binfo))
803 return 1;
805 if (TREE_CODE (scope) == FUNCTION_DECL
806 || DECL_FUNCTION_TEMPLATE_P (scope))
808 /* Perhaps this SCOPE is a member of a class which is a
809 friend. */
810 if (DECL_CLASS_SCOPE_P (scope)
811 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
812 return 1;
814 /* Or an instantiation of something which is a friend. */
815 if (DECL_TEMPLATE_INFO (scope))
817 int ret;
818 /* Increment processing_template_decl to make sure that
819 dependent_type_p works correctly. */
820 ++processing_template_decl;
821 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
822 --processing_template_decl;
823 return ret;
827 return 0;
830 /* Called via dfs_walk_once_accessible from accessible_p */
832 static tree
833 dfs_accessible_post (tree binfo, void *data ATTRIBUTE_UNUSED)
835 if (BINFO_ACCESS (binfo) != ak_none)
837 tree scope = current_scope ();
838 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
839 && is_friend (BINFO_TYPE (binfo), scope))
840 return binfo;
843 return NULL_TREE;
846 /* DECL is a declaration from a base class of TYPE, which was the
847 class used to name DECL. Return nonzero if, in the current
848 context, DECL is accessible. If TYPE is actually a BINFO node,
849 then we can tell in what context the access is occurring by looking
850 at the most derived class along the path indicated by BINFO. If
851 CONSIDER_LOCAL is true, do consider special access the current
852 scope or friendship thereof we might have. */
855 accessible_p (tree type, tree decl, bool consider_local_p)
857 tree binfo;
858 tree scope;
859 access_kind access;
861 /* Nonzero if it's OK to access DECL if it has protected
862 accessibility in TYPE. */
863 int protected_ok = 0;
865 /* If this declaration is in a block or namespace scope, there's no
866 access control. */
867 if (!TYPE_P (context_for_name_lookup (decl)))
868 return 1;
870 /* There is no need to perform access checks inside a thunk. */
871 scope = current_scope ();
872 if (scope && DECL_THUNK_P (scope))
873 return 1;
875 /* In a template declaration, we cannot be sure whether the
876 particular specialization that is instantiated will be a friend
877 or not. Therefore, all access checks are deferred until
878 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
879 parameter list for a template (because we may see dependent types
880 in default arguments for template parameters), and access
881 checking should be performed in the outermost parameter list. */
882 if (processing_template_decl
883 && (!processing_template_parmlist || processing_template_decl > 1))
884 return 1;
886 if (!TYPE_P (type))
888 binfo = type;
889 type = BINFO_TYPE (type);
891 else
892 binfo = TYPE_BINFO (type);
894 /* [class.access.base]
896 A member m is accessible when named in class N if
898 --m as a member of N is public, or
900 --m as a member of N is private, and the reference occurs in a
901 member or friend of class N, or
903 --m as a member of N is protected, and the reference occurs in a
904 member or friend of class N, or in a member or friend of a
905 class P derived from N, where m as a member of P is private or
906 protected, or
908 --there exists a base class B of N that is accessible at the point
909 of reference, and m is accessible when named in class B.
911 We walk the base class hierarchy, checking these conditions. */
913 if (consider_local_p)
915 /* Figure out where the reference is occurring. Check to see if
916 DECL is private or protected in this scope, since that will
917 determine whether protected access is allowed. */
918 if (current_class_type)
919 protected_ok = protected_accessible_p (decl,
920 current_class_type, binfo);
922 /* Now, loop through the classes of which we are a friend. */
923 if (!protected_ok)
924 protected_ok = friend_accessible_p (scope, decl, binfo);
927 /* Standardize the binfo that access_in_type will use. We don't
928 need to know what path was chosen from this point onwards. */
929 binfo = TYPE_BINFO (type);
931 /* Compute the accessibility of DECL in the class hierarchy
932 dominated by type. */
933 access = access_in_type (type, decl);
934 if (access == ak_public
935 || (access == ak_protected && protected_ok))
936 return 1;
938 if (!consider_local_p)
939 return 0;
941 /* Walk the hierarchy again, looking for a base class that allows
942 access. */
943 return dfs_walk_once_accessible (binfo, /*friends=*/true,
944 NULL, dfs_accessible_post, NULL)
945 != NULL_TREE;
948 struct lookup_field_info {
949 /* The type in which we're looking. */
950 tree type;
951 /* The name of the field for which we're looking. */
952 tree name;
953 /* If non-NULL, the current result of the lookup. */
954 tree rval;
955 /* The path to RVAL. */
956 tree rval_binfo;
957 /* If non-NULL, the lookup was ambiguous, and this is a list of the
958 candidates. */
959 tree ambiguous;
960 /* If nonzero, we are looking for types, not data members. */
961 int want_type;
962 /* If something went wrong, a message indicating what. */
963 const char *errstr;
966 /* Nonzero for a class member means that it is shared between all objects
967 of that class.
969 [class.member.lookup]:If the resulting set of declarations are not all
970 from sub-objects of the same type, or the set has a nonstatic member
971 and includes members from distinct sub-objects, there is an ambiguity
972 and the program is ill-formed.
974 This function checks that T contains no nonstatic members. */
977 shared_member_p (tree t)
979 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
980 || TREE_CODE (t) == CONST_DECL)
981 return 1;
982 if (is_overloaded_fn (t))
984 t = get_fns (t);
985 for (; t; t = OVL_NEXT (t))
987 tree fn = OVL_CURRENT (t);
988 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
989 return 0;
991 return 1;
993 return 0;
996 /* Routine to see if the sub-object denoted by the binfo PARENT can be
997 found as a base class and sub-object of the object denoted by
998 BINFO. */
1000 static int
1001 is_subobject_of_p (tree parent, tree binfo)
1003 tree probe;
1005 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1007 if (probe == binfo)
1008 return 1;
1009 if (BINFO_VIRTUAL_P (probe))
1010 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1011 != NULL_TREE);
1013 return 0;
1016 /* DATA is really a struct lookup_field_info. Look for a field with
1017 the name indicated there in BINFO. If this function returns a
1018 non-NULL value it is the result of the lookup. Called from
1019 lookup_field via breadth_first_search. */
1021 static tree
1022 lookup_field_r (tree binfo, void *data)
1024 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1025 tree type = BINFO_TYPE (binfo);
1026 tree nval = NULL_TREE;
1028 /* If this is a dependent base, don't look in it. */
1029 if (BINFO_DEPENDENT_BASE_P (binfo))
1030 return NULL_TREE;
1032 /* If this base class is hidden by the best-known value so far, we
1033 don't need to look. */
1034 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1035 && !BINFO_VIRTUAL_P (binfo))
1036 return dfs_skip_bases;
1038 /* First, look for a function. There can't be a function and a data
1039 member with the same name, and if there's a function and a type
1040 with the same name, the type is hidden by the function. */
1041 if (!lfi->want_type)
1042 nval = lookup_fnfields_slot (type, lfi->name);
1044 if (!nval)
1045 /* Look for a data member or type. */
1046 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1048 /* If there is no declaration with the indicated name in this type,
1049 then there's nothing to do. */
1050 if (!nval)
1051 goto done;
1053 /* If we're looking up a type (as with an elaborated type specifier)
1054 we ignore all non-types we find. */
1055 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1056 && !DECL_TYPE_TEMPLATE_P (nval))
1058 if (lfi->name == TYPE_IDENTIFIER (type))
1060 /* If the aggregate has no user defined constructors, we allow
1061 it to have fields with the same name as the enclosing type.
1062 If we are looking for that name, find the corresponding
1063 TYPE_DECL. */
1064 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1065 if (DECL_NAME (nval) == lfi->name
1066 && TREE_CODE (nval) == TYPE_DECL)
1067 break;
1069 else
1070 nval = NULL_TREE;
1071 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1073 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1074 lfi->name);
1075 if (e != NULL)
1076 nval = TYPE_MAIN_DECL (e->type);
1077 else
1078 goto done;
1082 /* If the lookup already found a match, and the new value doesn't
1083 hide the old one, we might have an ambiguity. */
1084 if (lfi->rval_binfo
1085 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1088 if (nval == lfi->rval && shared_member_p (nval))
1089 /* The two things are really the same. */
1091 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1092 /* The previous value hides the new one. */
1094 else
1096 /* We have a real ambiguity. We keep a chain of all the
1097 candidates. */
1098 if (!lfi->ambiguous && lfi->rval)
1100 /* This is the first time we noticed an ambiguity. Add
1101 what we previously thought was a reasonable candidate
1102 to the list. */
1103 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1104 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1107 /* Add the new value. */
1108 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1109 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1110 lfi->errstr = G_("request for member %qD is ambiguous");
1113 else
1115 lfi->rval = nval;
1116 lfi->rval_binfo = binfo;
1119 done:
1120 /* Don't look for constructors or destructors in base classes. */
1121 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1122 return dfs_skip_bases;
1123 return NULL_TREE;
1126 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1127 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1128 FUNCTIONS, and OPTYPE respectively. */
1130 tree
1131 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1133 tree baselink;
1135 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1136 || TREE_CODE (functions) == TEMPLATE_DECL
1137 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1138 || TREE_CODE (functions) == OVERLOAD);
1139 gcc_assert (!optype || TYPE_P (optype));
1140 gcc_assert (TREE_TYPE (functions));
1142 baselink = make_node (BASELINK);
1143 TREE_TYPE (baselink) = TREE_TYPE (functions);
1144 BASELINK_BINFO (baselink) = binfo;
1145 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1146 BASELINK_FUNCTIONS (baselink) = functions;
1147 BASELINK_OPTYPE (baselink) = optype;
1149 return baselink;
1152 /* Look for a member named NAME in an inheritance lattice dominated by
1153 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1154 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1155 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1156 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1157 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1158 TREE_VALUEs are the list of ambiguous candidates.
1160 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1162 If nothing can be found return NULL_TREE and do not issue an error. */
1164 tree
1165 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1166 tsubst_flags_t complain)
1168 tree rval, rval_binfo = NULL_TREE;
1169 tree type = NULL_TREE, basetype_path = NULL_TREE;
1170 struct lookup_field_info lfi;
1172 /* rval_binfo is the binfo associated with the found member, note,
1173 this can be set with useful information, even when rval is not
1174 set, because it must deal with ALL members, not just non-function
1175 members. It is used for ambiguity checking and the hidden
1176 checks. Whereas rval is only set if a proper (not hidden)
1177 non-function member is found. */
1179 const char *errstr = 0;
1181 if (name == error_mark_node
1182 || xbasetype == NULL_TREE
1183 || xbasetype == error_mark_node)
1184 return NULL_TREE;
1186 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1188 if (TREE_CODE (xbasetype) == TREE_BINFO)
1190 type = BINFO_TYPE (xbasetype);
1191 basetype_path = xbasetype;
1193 else
1195 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1196 return NULL_TREE;
1197 type = xbasetype;
1198 xbasetype = NULL_TREE;
1201 type = complete_type (type);
1202 if (!basetype_path)
1203 basetype_path = TYPE_BINFO (type);
1205 if (!basetype_path)
1206 return NULL_TREE;
1208 #ifdef GATHER_STATISTICS
1209 n_calls_lookup_field++;
1210 #endif /* GATHER_STATISTICS */
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);
1263 if (errstr && protect)
1265 if (complain & tf_error)
1267 error (errstr, name, type);
1268 if (lfi.ambiguous)
1269 print_candidates (lfi.ambiguous);
1271 rval = error_mark_node;
1274 if (rval && is_overloaded_fn (rval))
1275 rval = build_baselink (rval_binfo, basetype_path, rval,
1276 (IDENTIFIER_TYPENAME_P (name)
1277 ? TREE_TYPE (name): NULL_TREE));
1278 return rval;
1281 /* Like lookup_member, except that if we find a function member we
1282 return NULL_TREE. */
1284 tree
1285 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1287 tree rval = lookup_member (xbasetype, name, protect, want_type,
1288 tf_warning_or_error);
1290 /* Ignore functions, but propagate the ambiguity list. */
1291 if (!error_operand_p (rval)
1292 && (rval && BASELINK_P (rval)))
1293 return NULL_TREE;
1295 return rval;
1298 /* Like lookup_member, except that if we find a non-function member we
1299 return NULL_TREE. */
1301 tree
1302 lookup_fnfields (tree xbasetype, tree name, int protect)
1304 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1305 tf_warning_or_error);
1307 /* Ignore non-functions, but propagate the ambiguity list. */
1308 if (!error_operand_p (rval)
1309 && (rval && !BASELINK_P (rval)))
1310 return NULL_TREE;
1312 return rval;
1315 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1316 corresponding to "operator TYPE ()", or -1 if there is no such
1317 operator. Only CLASS_TYPE itself is searched; this routine does
1318 not scan the base classes of CLASS_TYPE. */
1320 static int
1321 lookup_conversion_operator (tree class_type, tree type)
1323 int tpl_slot = -1;
1325 if (TYPE_HAS_CONVERSION (class_type))
1327 int i;
1328 tree fn;
1329 VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (class_type);
1331 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1332 VEC_iterate (tree, methods, i, fn); ++i)
1334 /* All the conversion operators come near the beginning of
1335 the class. Therefore, if FN is not a conversion
1336 operator, there is no matching conversion operator in
1337 CLASS_TYPE. */
1338 fn = OVL_CURRENT (fn);
1339 if (!DECL_CONV_FN_P (fn))
1340 break;
1342 if (TREE_CODE (fn) == TEMPLATE_DECL)
1343 /* All the templated conversion functions are on the same
1344 slot, so remember it. */
1345 tpl_slot = i;
1346 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1347 return i;
1351 return tpl_slot;
1354 /* TYPE is a class type. Return the index of the fields within
1355 the method vector with name NAME, or -1 if no such field exists.
1356 Does not lazily declare implicitly-declared member functions. */
1358 static int
1359 lookup_fnfields_idx_nolazy (tree type, tree name)
1361 VEC(tree,gc) *method_vec;
1362 tree fn;
1363 tree tmp;
1364 size_t i;
1366 if (!CLASS_TYPE_P (type))
1367 return -1;
1369 method_vec = CLASSTYPE_METHOD_VEC (type);
1370 if (!method_vec)
1371 return -1;
1373 #ifdef GATHER_STATISTICS
1374 n_calls_lookup_fnfields_1++;
1375 #endif /* GATHER_STATISTICS */
1377 /* Constructors are first... */
1378 if (name == ctor_identifier)
1380 fn = CLASSTYPE_CONSTRUCTORS (type);
1381 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1383 /* and destructors are second. */
1384 if (name == dtor_identifier)
1386 fn = CLASSTYPE_DESTRUCTORS (type);
1387 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1389 if (IDENTIFIER_TYPENAME_P (name))
1390 return lookup_conversion_operator (type, TREE_TYPE (name));
1392 /* Skip the conversion operators. */
1393 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1394 VEC_iterate (tree, method_vec, i, fn);
1395 ++i)
1396 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1397 break;
1399 /* If the type is complete, use binary search. */
1400 if (COMPLETE_TYPE_P (type))
1402 int lo;
1403 int hi;
1405 lo = i;
1406 hi = VEC_length (tree, method_vec);
1407 while (lo < hi)
1409 i = (lo + hi) / 2;
1411 #ifdef GATHER_STATISTICS
1412 n_outer_fields_searched++;
1413 #endif /* GATHER_STATISTICS */
1415 tmp = VEC_index (tree, 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_iterate (tree, method_vec, i, fn); ++i)
1428 #ifdef GATHER_STATISTICS
1429 n_outer_fields_searched++;
1430 #endif /* GATHER_STATISTICS */
1431 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1432 return i;
1435 return -1;
1438 /* TYPE is a class type. Return the index of the fields within
1439 the method vector with name NAME, or -1 if no such field exists. */
1442 lookup_fnfields_1 (tree type, tree name)
1444 if (!CLASS_TYPE_P (type))
1445 return -1;
1447 if (COMPLETE_TYPE_P (type))
1449 if ((name == ctor_identifier
1450 || name == base_ctor_identifier
1451 || name == complete_ctor_identifier))
1453 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1454 lazily_declare_fn (sfk_constructor, type);
1455 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1456 lazily_declare_fn (sfk_copy_constructor, type);
1457 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
1458 lazily_declare_fn (sfk_move_constructor, type);
1460 else if (name == ansi_assopname (NOP_EXPR))
1462 if (CLASSTYPE_LAZY_COPY_ASSIGN (type))
1463 lazily_declare_fn (sfk_copy_assignment, type);
1464 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type))
1465 lazily_declare_fn (sfk_move_assignment, type);
1467 else if ((name == dtor_identifier
1468 || name == base_dtor_identifier
1469 || name == complete_dtor_identifier
1470 || name == deleting_dtor_identifier)
1471 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1472 lazily_declare_fn (sfk_destructor, type);
1475 return lookup_fnfields_idx_nolazy (type, name);
1478 /* TYPE is a class type. Return the field within the method vector with
1479 name NAME, or NULL_TREE if no such field exists. */
1481 tree
1482 lookup_fnfields_slot (tree type, tree name)
1484 int ix = lookup_fnfields_1 (complete_type (type), name);
1485 if (ix < 0)
1486 return NULL_TREE;
1487 return VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
1490 /* As above, but avoid lazily declaring functions. */
1492 tree
1493 lookup_fnfields_slot_nolazy (tree type, tree name)
1495 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name);
1496 if (ix < 0)
1497 return NULL_TREE;
1498 return VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
1501 /* Like lookup_fnfields_1, except that the name is extracted from
1502 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1505 class_method_index_for_fn (tree class_type, tree function)
1507 gcc_assert (TREE_CODE (function) == FUNCTION_DECL
1508 || DECL_FUNCTION_TEMPLATE_P (function));
1510 return lookup_fnfields_1 (class_type,
1511 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1512 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1513 DECL_NAME (function));
1517 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1518 the class or namespace used to qualify the name. CONTEXT_CLASS is
1519 the class corresponding to the object in which DECL will be used.
1520 Return a possibly modified version of DECL that takes into account
1521 the CONTEXT_CLASS.
1523 In particular, consider an expression like `B::m' in the context of
1524 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1525 then the most derived class indicated by the BASELINK_BINFO will be
1526 `B', not `D'. This function makes that adjustment. */
1528 tree
1529 adjust_result_of_qualified_name_lookup (tree decl,
1530 tree qualifying_scope,
1531 tree context_class)
1533 if (context_class && context_class != error_mark_node
1534 && CLASS_TYPE_P (context_class)
1535 && CLASS_TYPE_P (qualifying_scope)
1536 && DERIVED_FROM_P (qualifying_scope, context_class)
1537 && BASELINK_P (decl))
1539 tree base;
1541 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1542 Because we do not yet know which function will be chosen by
1543 overload resolution, we cannot yet check either accessibility
1544 or ambiguity -- in either case, the choice of a static member
1545 function might make the usage valid. */
1546 base = lookup_base (context_class, qualifying_scope,
1547 ba_unique | ba_quiet, NULL);
1548 if (base)
1550 BASELINK_ACCESS_BINFO (decl) = base;
1551 BASELINK_BINFO (decl)
1552 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1553 ba_unique | ba_quiet,
1554 NULL);
1558 if (BASELINK_P (decl))
1559 BASELINK_QUALIFIED_P (decl) = true;
1561 return decl;
1565 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1566 PRE_FN is called in preorder, while POST_FN is called in postorder.
1567 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1568 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1569 that value is immediately returned and the walk is terminated. One
1570 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1571 POST_FN are passed the binfo to examine and the caller's DATA
1572 value. All paths are walked, thus virtual and morally virtual
1573 binfos can be multiply walked. */
1575 tree
1576 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1577 tree (*post_fn) (tree, void *), void *data)
1579 tree rval;
1580 unsigned ix;
1581 tree base_binfo;
1583 /* Call the pre-order walking function. */
1584 if (pre_fn)
1586 rval = pre_fn (binfo, data);
1587 if (rval)
1589 if (rval == dfs_skip_bases)
1590 goto skip_bases;
1591 return rval;
1595 /* Find the next child binfo to walk. */
1596 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1598 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1599 if (rval)
1600 return rval;
1603 skip_bases:
1604 /* Call the post-order walking function. */
1605 if (post_fn)
1607 rval = post_fn (binfo, data);
1608 gcc_assert (rval != dfs_skip_bases);
1609 return rval;
1612 return NULL_TREE;
1615 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1616 that binfos are walked at most once. */
1618 static tree
1619 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1620 tree (*post_fn) (tree, void *), void *data)
1622 tree rval;
1623 unsigned ix;
1624 tree base_binfo;
1626 /* Call the pre-order walking function. */
1627 if (pre_fn)
1629 rval = pre_fn (binfo, data);
1630 if (rval)
1632 if (rval == dfs_skip_bases)
1633 goto skip_bases;
1635 return rval;
1639 /* Find the next child binfo to walk. */
1640 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1642 if (BINFO_VIRTUAL_P (base_binfo))
1644 if (BINFO_MARKED (base_binfo))
1645 continue;
1646 BINFO_MARKED (base_binfo) = 1;
1649 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1650 if (rval)
1651 return rval;
1654 skip_bases:
1655 /* Call the post-order walking function. */
1656 if (post_fn)
1658 rval = post_fn (binfo, data);
1659 gcc_assert (rval != dfs_skip_bases);
1660 return rval;
1663 return NULL_TREE;
1666 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1667 BINFO. */
1669 static void
1670 dfs_unmark_r (tree binfo)
1672 unsigned ix;
1673 tree base_binfo;
1675 /* Process the basetypes. */
1676 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1678 if (BINFO_VIRTUAL_P (base_binfo))
1680 if (!BINFO_MARKED (base_binfo))
1681 continue;
1682 BINFO_MARKED (base_binfo) = 0;
1684 /* Only walk, if it can contain more virtual bases. */
1685 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1686 dfs_unmark_r (base_binfo);
1690 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1691 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1692 For diamond shaped hierarchies we must mark the virtual bases, to
1693 avoid multiple walks. */
1695 tree
1696 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1697 tree (*post_fn) (tree, void *), void *data)
1699 static int active = 0; /* We must not be called recursively. */
1700 tree rval;
1702 gcc_assert (pre_fn || post_fn);
1703 gcc_assert (!active);
1704 active++;
1706 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1707 /* We are not diamond shaped, and therefore cannot encounter the
1708 same binfo twice. */
1709 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1710 else
1712 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1713 if (!BINFO_INHERITANCE_CHAIN (binfo))
1715 /* We are at the top of the hierarchy, and can use the
1716 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1717 bases. */
1718 VEC(tree,gc) *vbases;
1719 unsigned ix;
1720 tree base_binfo;
1722 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1723 VEC_iterate (tree, vbases, ix, base_binfo); ix++)
1724 BINFO_MARKED (base_binfo) = 0;
1726 else
1727 dfs_unmark_r (binfo);
1730 active--;
1732 return rval;
1735 /* Worker function for dfs_walk_once_accessible. Behaves like
1736 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1737 access given by the current context should be considered, (b) ONCE
1738 indicates whether bases should be marked during traversal. */
1740 static tree
1741 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1742 tree (*pre_fn) (tree, void *),
1743 tree (*post_fn) (tree, void *), void *data)
1745 tree rval = NULL_TREE;
1746 unsigned ix;
1747 tree base_binfo;
1749 /* Call the pre-order walking function. */
1750 if (pre_fn)
1752 rval = pre_fn (binfo, data);
1753 if (rval)
1755 if (rval == dfs_skip_bases)
1756 goto skip_bases;
1758 return rval;
1762 /* Find the next child binfo to walk. */
1763 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1765 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1767 if (mark && BINFO_MARKED (base_binfo))
1768 continue;
1770 /* If the base is inherited via private or protected
1771 inheritance, then we can't see it, unless we are a friend of
1772 the current binfo. */
1773 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1775 tree scope;
1776 if (!friends_p)
1777 continue;
1778 scope = current_scope ();
1779 if (!scope
1780 || TREE_CODE (scope) == NAMESPACE_DECL
1781 || !is_friend (BINFO_TYPE (binfo), scope))
1782 continue;
1785 if (mark)
1786 BINFO_MARKED (base_binfo) = 1;
1788 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1789 pre_fn, post_fn, data);
1790 if (rval)
1791 return rval;
1794 skip_bases:
1795 /* Call the post-order walking function. */
1796 if (post_fn)
1798 rval = post_fn (binfo, data);
1799 gcc_assert (rval != dfs_skip_bases);
1800 return rval;
1803 return NULL_TREE;
1806 /* Like dfs_walk_once except that only accessible bases are walked.
1807 FRIENDS_P indicates whether friendship of the local context
1808 should be considered when determining accessibility. */
1810 static tree
1811 dfs_walk_once_accessible (tree binfo, bool friends_p,
1812 tree (*pre_fn) (tree, void *),
1813 tree (*post_fn) (tree, void *), void *data)
1815 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1816 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1817 pre_fn, post_fn, data);
1819 if (diamond_shaped)
1821 if (!BINFO_INHERITANCE_CHAIN (binfo))
1823 /* We are at the top of the hierarchy, and can use the
1824 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1825 bases. */
1826 VEC(tree,gc) *vbases;
1827 unsigned ix;
1828 tree base_binfo;
1830 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1831 VEC_iterate (tree, vbases, ix, base_binfo); ix++)
1832 BINFO_MARKED (base_binfo) = 0;
1834 else
1835 dfs_unmark_r (binfo);
1837 return rval;
1840 /* Check that virtual overrider OVERRIDER is acceptable for base function
1841 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1843 static int
1844 check_final_overrider (tree overrider, tree basefn)
1846 tree over_type = TREE_TYPE (overrider);
1847 tree base_type = TREE_TYPE (basefn);
1848 tree over_return = TREE_TYPE (over_type);
1849 tree base_return = TREE_TYPE (base_type);
1850 tree over_throw, base_throw;
1852 int fail = 0;
1854 if (DECL_INVALID_OVERRIDER_P (overrider))
1855 return 0;
1857 if (same_type_p (base_return, over_return))
1858 /* OK */;
1859 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1860 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1861 && POINTER_TYPE_P (base_return)))
1863 /* Potentially covariant. */
1864 unsigned base_quals, over_quals;
1866 fail = !POINTER_TYPE_P (base_return);
1867 if (!fail)
1869 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1871 base_return = TREE_TYPE (base_return);
1872 over_return = TREE_TYPE (over_return);
1874 base_quals = cp_type_quals (base_return);
1875 over_quals = cp_type_quals (over_return);
1877 if ((base_quals & over_quals) != over_quals)
1878 fail = 1;
1880 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1882 /* Strictly speaking, the standard requires the return type to be
1883 complete even if it only differs in cv-quals, but that seems
1884 like a bug in the wording. */
1885 if (!same_type_ignoring_top_level_qualifiers_p (base_return, over_return))
1887 tree binfo = lookup_base (over_return, base_return,
1888 ba_check | ba_quiet, NULL);
1890 if (!binfo)
1891 fail = 1;
1894 else if (!pedantic
1895 && can_convert (TREE_TYPE (base_type), TREE_TYPE (over_type),
1896 tf_warning_or_error))
1897 /* GNU extension, allow trivial pointer conversions such as
1898 converting to void *, or qualification conversion. */
1900 /* can_convert will permit user defined conversion from a
1901 (reference to) class type. We must reject them. */
1902 over_return = non_reference (TREE_TYPE (over_type));
1903 if (CLASS_TYPE_P (over_return))
1904 fail = 2;
1905 else
1907 warning (0, "deprecated covariant return type for %q+#D",
1908 overrider);
1909 warning (0, " overriding %q+#D", basefn);
1912 else
1913 fail = 2;
1915 else
1916 fail = 2;
1917 if (!fail)
1918 /* OK */;
1919 else
1921 if (fail == 1)
1923 error ("invalid covariant return type for %q+#D", overrider);
1924 error (" overriding %q+#D", basefn);
1926 else
1928 error ("conflicting return type specified for %q+#D", overrider);
1929 error (" overriding %q+#D", basefn);
1931 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1932 return 0;
1935 /* Check throw specifier is at least as strict. */
1936 maybe_instantiate_noexcept (basefn);
1937 maybe_instantiate_noexcept (overrider);
1938 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1939 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1941 if (!comp_except_specs (base_throw, over_throw, ce_derived))
1943 error ("looser throw specifier for %q+#F", overrider);
1944 error (" overriding %q+#F", basefn);
1945 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1946 return 0;
1949 /* Check for conflicting type attributes. */
1950 if (!comp_type_attributes (over_type, base_type))
1952 error ("conflicting type attributes specified for %q+#D", overrider);
1953 error (" overriding %q+#D", basefn);
1954 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1955 return 0;
1958 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
1960 if (DECL_DELETED_FN (overrider))
1962 error ("deleted function %q+D", overrider);
1963 error ("overriding non-deleted function %q+D", basefn);
1964 maybe_explain_implicit_delete (overrider);
1966 else
1968 error ("non-deleted function %q+D", overrider);
1969 error ("overriding deleted function %q+D", basefn);
1971 return 0;
1973 if (DECL_FINAL_P (basefn))
1975 error ("virtual function %q+D", overrider);
1976 error ("overriding final function %q+D", basefn);
1977 return 0;
1979 return 1;
1982 /* Given a class TYPE, and a function decl FNDECL, look for
1983 virtual functions in TYPE's hierarchy which FNDECL overrides.
1984 We do not look in TYPE itself, only its bases.
1986 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1987 find that it overrides anything.
1989 We check that every function which is overridden, is correctly
1990 overridden. */
1993 look_for_overrides (tree type, tree fndecl)
1995 tree binfo = TYPE_BINFO (type);
1996 tree base_binfo;
1997 int ix;
1998 int found = 0;
2000 /* A constructor for a class T does not override a function T
2001 in a base class. */
2002 if (DECL_CONSTRUCTOR_P (fndecl))
2003 return 0;
2005 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2007 tree basetype = BINFO_TYPE (base_binfo);
2009 if (TYPE_POLYMORPHIC_P (basetype))
2010 found += look_for_overrides_r (basetype, fndecl);
2012 return found;
2015 /* Look in TYPE for virtual functions with the same signature as
2016 FNDECL. */
2018 tree
2019 look_for_overrides_here (tree type, tree fndecl)
2021 int ix;
2023 /* If there are no methods in TYPE (meaning that only implicitly
2024 declared methods will ever be provided for TYPE), then there are
2025 no virtual functions. */
2026 if (!CLASSTYPE_METHOD_VEC (type))
2027 return NULL_TREE;
2029 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
2030 ix = CLASSTYPE_DESTRUCTOR_SLOT;
2031 else
2032 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
2033 if (ix >= 0)
2035 tree fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
2037 for (; fns; fns = OVL_NEXT (fns))
2039 tree fn = OVL_CURRENT (fns);
2041 if (!DECL_VIRTUAL_P (fn))
2042 /* Not a virtual. */;
2043 else if (DECL_CONTEXT (fn) != type)
2044 /* Introduced with a using declaration. */;
2045 else if (DECL_STATIC_FUNCTION_P (fndecl))
2047 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2048 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2049 if (compparms (TREE_CHAIN (btypes), dtypes))
2050 return fn;
2052 else if (same_signature_p (fndecl, fn))
2053 return fn;
2056 return NULL_TREE;
2059 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2060 TYPE itself and its bases. */
2062 static int
2063 look_for_overrides_r (tree type, tree fndecl)
2065 tree fn = look_for_overrides_here (type, fndecl);
2066 if (fn)
2068 if (DECL_STATIC_FUNCTION_P (fndecl))
2070 /* A static member function cannot match an inherited
2071 virtual member function. */
2072 error ("%q+#D cannot be declared", fndecl);
2073 error (" since %q+#D declared in base class", fn);
2075 else
2077 /* It's definitely virtual, even if not explicitly set. */
2078 DECL_VIRTUAL_P (fndecl) = 1;
2079 check_final_overrider (fndecl, fn);
2081 return 1;
2084 /* We failed to find one declared in this class. Look in its bases. */
2085 return look_for_overrides (type, fndecl);
2088 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2090 static tree
2091 dfs_get_pure_virtuals (tree binfo, void *data)
2093 tree type = (tree) data;
2095 /* We're not interested in primary base classes; the derived class
2096 of which they are a primary base will contain the information we
2097 need. */
2098 if (!BINFO_PRIMARY_P (binfo))
2100 tree virtuals;
2102 for (virtuals = BINFO_VIRTUALS (binfo);
2103 virtuals;
2104 virtuals = TREE_CHAIN (virtuals))
2105 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2106 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (type),
2107 BV_FN (virtuals));
2110 return NULL_TREE;
2113 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2115 void
2116 get_pure_virtuals (tree type)
2118 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2119 is going to be overridden. */
2120 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2121 /* Now, run through all the bases which are not primary bases, and
2122 collect the pure virtual functions. We look at the vtable in
2123 each class to determine what pure virtual functions are present.
2124 (A primary base is not interesting because the derived class of
2125 which it is a primary base will contain vtable entries for the
2126 pure virtuals in the base class. */
2127 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2130 /* Debug info for C++ classes can get very large; try to avoid
2131 emitting it everywhere.
2133 Note that this optimization wins even when the target supports
2134 BINCL (if only slightly), and reduces the amount of work for the
2135 linker. */
2137 void
2138 maybe_suppress_debug_info (tree t)
2140 if (write_symbols == NO_DEBUG)
2141 return;
2143 /* We might have set this earlier in cp_finish_decl. */
2144 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2146 /* Always emit the information for each class every time. */
2147 if (flag_emit_class_debug_always)
2148 return;
2150 /* If we already know how we're handling this class, handle debug info
2151 the same way. */
2152 if (CLASSTYPE_INTERFACE_KNOWN (t))
2154 if (CLASSTYPE_INTERFACE_ONLY (t))
2155 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2156 /* else don't set it. */
2158 /* If the class has a vtable, write out the debug info along with
2159 the vtable. */
2160 else if (TYPE_CONTAINS_VPTR_P (t))
2161 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2163 /* Otherwise, just emit the debug info normally. */
2166 /* Note that we want debugging information for a base class of a class
2167 whose vtable is being emitted. Normally, this would happen because
2168 calling the constructor for a derived class implies calling the
2169 constructors for all bases, which involve initializing the
2170 appropriate vptr with the vtable for the base class; but in the
2171 presence of optimization, this initialization may be optimized
2172 away, so we tell finish_vtable_vardecl that we want the debugging
2173 information anyway. */
2175 static tree
2176 dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
2178 tree t = BINFO_TYPE (binfo);
2180 if (CLASSTYPE_DEBUG_REQUESTED (t))
2181 return dfs_skip_bases;
2183 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2185 return NULL_TREE;
2188 /* Write out the debugging information for TYPE, whose vtable is being
2189 emitted. Also walk through our bases and note that we want to
2190 write out information for them. This avoids the problem of not
2191 writing any debug info for intermediate basetypes whose
2192 constructors, and thus the references to their vtables, and thus
2193 the vtables themselves, were optimized away. */
2195 void
2196 note_debug_info_needed (tree type)
2198 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2200 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2201 rest_of_type_compilation (type, toplevel_bindings_p ());
2204 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2207 void
2208 print_search_statistics (void)
2210 #ifdef GATHER_STATISTICS
2211 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2212 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2213 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2214 n_outer_fields_searched, n_calls_lookup_fnfields);
2215 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2216 #else /* GATHER_STATISTICS */
2217 fprintf (stderr, "no search statistics\n");
2218 #endif /* GATHER_STATISTICS */
2221 void
2222 reinit_search_statistics (void)
2224 #ifdef GATHER_STATISTICS
2225 n_fields_searched = 0;
2226 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2227 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2228 n_calls_get_base_type = 0;
2229 n_outer_fields_searched = 0;
2230 n_contexts_saved = 0;
2231 #endif /* GATHER_STATISTICS */
2234 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2235 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2236 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2237 bases have been encountered already in the tree walk. PARENT_CONVS
2238 is the list of lists of conversion functions that could hide CONV
2239 and OTHER_CONVS is the list of lists of conversion functions that
2240 could hide or be hidden by CONV, should virtualness be involved in
2241 the hierarchy. Merely checking the conversion op's name is not
2242 enough because two conversion operators to the same type can have
2243 different names. Return nonzero if we are visible. */
2245 static int
2246 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2247 tree to_type, tree parent_convs, tree other_convs)
2249 tree level, probe;
2251 /* See if we are hidden by a parent conversion. */
2252 for (level = parent_convs; level; level = TREE_CHAIN (level))
2253 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2254 if (same_type_p (to_type, TREE_TYPE (probe)))
2255 return 0;
2257 if (virtual_depth || virtualness)
2259 /* In a virtual hierarchy, we could be hidden, or could hide a
2260 conversion function on the other_convs list. */
2261 for (level = other_convs; level; level = TREE_CHAIN (level))
2263 int we_hide_them;
2264 int they_hide_us;
2265 tree *prev, other;
2267 if (!(virtual_depth || TREE_STATIC (level)))
2268 /* Neither is morally virtual, so cannot hide each other. */
2269 continue;
2271 if (!TREE_VALUE (level))
2272 /* They evaporated away already. */
2273 continue;
2275 they_hide_us = (virtual_depth
2276 && original_binfo (binfo, TREE_PURPOSE (level)));
2277 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2278 && original_binfo (TREE_PURPOSE (level), binfo));
2280 if (!(we_hide_them || they_hide_us))
2281 /* Neither is within the other, so no hiding can occur. */
2282 continue;
2284 for (prev = &TREE_VALUE (level), other = *prev; other;)
2286 if (same_type_p (to_type, TREE_TYPE (other)))
2288 if (they_hide_us)
2289 /* We are hidden. */
2290 return 0;
2292 if (we_hide_them)
2294 /* We hide the other one. */
2295 other = TREE_CHAIN (other);
2296 *prev = other;
2297 continue;
2300 prev = &TREE_CHAIN (other);
2301 other = *prev;
2305 return 1;
2308 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2309 of conversion functions, the first slot will be for the current
2310 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2311 of conversion functions from children of the current binfo,
2312 concatenated with conversions from elsewhere in the hierarchy --
2313 that list begins with OTHER_CONVS. Return a single list of lists
2314 containing only conversions from the current binfo and its
2315 children. */
2317 static tree
2318 split_conversions (tree my_convs, tree parent_convs,
2319 tree child_convs, tree other_convs)
2321 tree t;
2322 tree prev;
2324 /* Remove the original other_convs portion from child_convs. */
2325 for (prev = NULL, t = child_convs;
2326 t != other_convs; prev = t, t = TREE_CHAIN (t))
2327 continue;
2329 if (prev)
2330 TREE_CHAIN (prev) = NULL_TREE;
2331 else
2332 child_convs = NULL_TREE;
2334 /* Attach the child convs to any we had at this level. */
2335 if (my_convs)
2337 my_convs = parent_convs;
2338 TREE_CHAIN (my_convs) = child_convs;
2340 else
2341 my_convs = child_convs;
2343 return my_convs;
2346 /* Worker for lookup_conversions. Lookup conversion functions in
2347 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2348 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2349 encountered virtual bases already in the tree walk. PARENT_CONVS &
2350 PARENT_TPL_CONVS are lists of list of conversions within parent
2351 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2352 elsewhere in the tree. Return the conversions found within this
2353 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2354 encountered virtualness. We keep template and non-template
2355 conversions separate, to avoid unnecessary type comparisons.
2357 The located conversion functions are held in lists of lists. The
2358 TREE_VALUE of the outer list is the list of conversion functions
2359 found in a particular binfo. The TREE_PURPOSE of both the outer
2360 and inner lists is the binfo at which those conversions were
2361 found. TREE_STATIC is set for those lists within of morally
2362 virtual binfos. The TREE_VALUE of the inner list is the conversion
2363 function or overload itself. The TREE_TYPE of each inner list node
2364 is the converted-to type. */
2366 static int
2367 lookup_conversions_r (tree binfo,
2368 int virtual_depth, int virtualness,
2369 tree parent_convs, tree parent_tpl_convs,
2370 tree other_convs, tree other_tpl_convs,
2371 tree *convs, tree *tpl_convs)
2373 int my_virtualness = 0;
2374 tree my_convs = NULL_TREE;
2375 tree my_tpl_convs = NULL_TREE;
2376 tree child_convs = NULL_TREE;
2377 tree child_tpl_convs = NULL_TREE;
2378 unsigned i;
2379 tree base_binfo;
2380 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2381 tree conv;
2383 /* If we have no conversion operators, then don't look. */
2384 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2386 *convs = *tpl_convs = NULL_TREE;
2388 return 0;
2391 if (BINFO_VIRTUAL_P (binfo))
2392 virtual_depth++;
2394 /* First, locate the unhidden ones at this level. */
2395 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2396 VEC_iterate (tree, method_vec, i, conv);
2397 ++i)
2399 tree cur = OVL_CURRENT (conv);
2401 if (!DECL_CONV_FN_P (cur))
2402 break;
2404 if (TREE_CODE (cur) == TEMPLATE_DECL)
2406 /* Only template conversions can be overloaded, and we must
2407 flatten them out and check each one individually. */
2408 tree tpls;
2410 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2412 tree tpl = OVL_CURRENT (tpls);
2413 tree type = DECL_CONV_FN_TYPE (tpl);
2415 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2416 type, parent_tpl_convs, other_tpl_convs))
2418 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2419 TREE_TYPE (my_tpl_convs) = type;
2420 if (virtual_depth)
2422 TREE_STATIC (my_tpl_convs) = 1;
2423 my_virtualness = 1;
2428 else
2430 tree name = DECL_NAME (cur);
2432 if (!IDENTIFIER_MARKED (name))
2434 tree type = DECL_CONV_FN_TYPE (cur);
2435 if (type_uses_auto (type))
2437 mark_used (cur);
2438 type = DECL_CONV_FN_TYPE (cur);
2441 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2442 type, parent_convs, other_convs))
2444 my_convs = tree_cons (binfo, conv, my_convs);
2445 TREE_TYPE (my_convs) = type;
2446 if (virtual_depth)
2448 TREE_STATIC (my_convs) = 1;
2449 my_virtualness = 1;
2451 IDENTIFIER_MARKED (name) = 1;
2457 if (my_convs)
2459 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2460 if (virtual_depth)
2461 TREE_STATIC (parent_convs) = 1;
2464 if (my_tpl_convs)
2466 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2467 if (virtual_depth)
2468 TREE_STATIC (parent_tpl_convs) = 1;
2471 child_convs = other_convs;
2472 child_tpl_convs = other_tpl_convs;
2474 /* Now iterate over each base, looking for more conversions. */
2475 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2477 tree base_convs, base_tpl_convs;
2478 unsigned base_virtualness;
2480 base_virtualness = lookup_conversions_r (base_binfo,
2481 virtual_depth, virtualness,
2482 parent_convs, parent_tpl_convs,
2483 child_convs, child_tpl_convs,
2484 &base_convs, &base_tpl_convs);
2485 if (base_virtualness)
2486 my_virtualness = virtualness = 1;
2487 child_convs = chainon (base_convs, child_convs);
2488 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2491 /* Unmark the conversions found at this level */
2492 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2493 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2495 *convs = split_conversions (my_convs, parent_convs,
2496 child_convs, other_convs);
2497 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2498 child_tpl_convs, other_tpl_convs);
2500 return my_virtualness;
2503 /* Return a TREE_LIST containing all the non-hidden user-defined
2504 conversion functions for TYPE (and its base-classes). The
2505 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2506 function. The TREE_PURPOSE is the BINFO from which the conversion
2507 functions in this node were selected. This function is effectively
2508 performing a set of member lookups as lookup_fnfield does, but
2509 using the type being converted to as the unique key, rather than the
2510 field name. */
2512 tree
2513 lookup_conversions (tree type)
2515 tree convs, tpl_convs;
2516 tree list = NULL_TREE;
2518 complete_type (type);
2519 if (!TYPE_BINFO (type))
2520 return NULL_TREE;
2522 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2523 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2524 &convs, &tpl_convs);
2526 /* Flatten the list-of-lists */
2527 for (; convs; convs = TREE_CHAIN (convs))
2529 tree probe, next;
2531 for (probe = TREE_VALUE (convs); probe; probe = next)
2533 next = TREE_CHAIN (probe);
2535 TREE_CHAIN (probe) = list;
2536 list = probe;
2540 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2542 tree probe, next;
2544 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2546 next = TREE_CHAIN (probe);
2548 TREE_CHAIN (probe) = list;
2549 list = probe;
2553 return list;
2556 /* Returns the binfo of the first direct or indirect virtual base derived
2557 from BINFO, or NULL if binfo is not via virtual. */
2559 tree
2560 binfo_from_vbase (tree binfo)
2562 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2564 if (BINFO_VIRTUAL_P (binfo))
2565 return binfo;
2567 return NULL_TREE;
2570 /* Returns the binfo of the first direct or indirect virtual base derived
2571 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2572 via virtual. */
2574 tree
2575 binfo_via_virtual (tree binfo, tree limit)
2577 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2578 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2579 return NULL_TREE;
2581 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2582 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2584 if (BINFO_VIRTUAL_P (binfo))
2585 return binfo;
2587 return NULL_TREE;
2590 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2591 Find the equivalent binfo within whatever graph HERE is located.
2592 This is the inverse of original_binfo. */
2594 tree
2595 copied_binfo (tree binfo, tree here)
2597 tree result = NULL_TREE;
2599 if (BINFO_VIRTUAL_P (binfo))
2601 tree t;
2603 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2604 t = BINFO_INHERITANCE_CHAIN (t))
2605 continue;
2607 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2609 else if (BINFO_INHERITANCE_CHAIN (binfo))
2611 tree cbinfo;
2612 tree base_binfo;
2613 int ix;
2615 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2616 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2617 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2619 result = base_binfo;
2620 break;
2623 else
2625 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2626 result = here;
2629 gcc_assert (result);
2630 return result;
2633 tree
2634 binfo_for_vbase (tree base, tree t)
2636 unsigned ix;
2637 tree binfo;
2638 VEC(tree,gc) *vbases;
2640 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2641 VEC_iterate (tree, vbases, ix, binfo); ix++)
2642 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2643 return binfo;
2644 return NULL;
2647 /* BINFO is some base binfo of HERE, within some other
2648 hierarchy. Return the equivalent binfo, but in the hierarchy
2649 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2650 is not a base binfo of HERE, returns NULL_TREE. */
2652 tree
2653 original_binfo (tree binfo, tree here)
2655 tree result = NULL;
2657 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2658 result = here;
2659 else if (BINFO_VIRTUAL_P (binfo))
2660 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2661 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2662 : NULL_TREE);
2663 else if (BINFO_INHERITANCE_CHAIN (binfo))
2665 tree base_binfos;
2667 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2668 if (base_binfos)
2670 int ix;
2671 tree base_binfo;
2673 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2674 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2675 BINFO_TYPE (binfo)))
2677 result = base_binfo;
2678 break;
2683 return result;