Debian Codegraph @

gcc/cp/search.cc

raw
1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987-2025 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann ([email protected])
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 /* High-level class interface. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "cp-tree.h"
28 #include "intl.h"
29 #include "toplev.h"
30 #include "spellcheck-tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "tree-inline.h"
34
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 int check_hidden_convs (tree, int, int, tree, tree, tree);
41 static tree split_conversions (tree, tree, tree, tree);
42 static int lookup_conversions_r (tree, int, int, tree, tree, tree *);
43 static int look_for_overrides_r (tree, tree);
44 static tree lookup_field_r (tree, void *);
45 static tree dfs_accessible_post (tree, void *);
46 static tree dfs_walk_once_accessible (tree, bool,
47 tree (*pre_fn) (tree, void *),
48 tree (*post_fn) (tree, void *),
49 void *data);
50 static tree dfs_access_in_type (tree, void *);
51 static access_kind access_in_type (tree, tree);
52 static tree dfs_get_pure_virtuals (tree, void *);
53
54
55 /* Data for lookup_base and its workers. */
56
57 struct lookup_base_data_s
58 {
59 HOST_WIDE_INT offset; /* Offset we want, or -1 if any. */
60 tree t; /* type being searched. */
61 tree base; /* The base type we're looking for. */
62 tree binfo; /* Found binfo. */
63 bool via_virtual; /* Found via a virtual path. */
64 bool ambiguous; /* Found multiply ambiguous */
65 bool repeated_base; /* Whether there are repeated bases in the
66 hierarchy. */
67 bool want_any; /* Whether we want any matching binfo. */
68 bool require_virtual; /* Whether we require a virtual path. */
69 };
70
71 /* Worker function for lookup_base. See if we've found the desired
72 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
73
74 static tree
75 dfs_lookup_base (tree binfo, void *data_)
76 {
77 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
78
79 if (data->offset != -1)
80 {
81 /* We're looking for the type at a particular offset. */
82 int comp = compare_tree_int (BINFO_OFFSET (binfo), data->offset);
83 if (comp > 0)
84 /* Don't bother looking into bases laid out later; even if they
85 do virtually inherit from the base we want, we can get there
86 by another path. */
87 return dfs_skip_bases;
88 else if (comp != 0
89 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
90 /* Right type, wrong offset. */
91 return dfs_skip_bases;
92 /* Fall through. */
93 }
94
95 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
96 {
97 const bool via_virtual
98 = binfo_via_virtual (binfo, data->t) != NULL_TREE;
99
100 if (data->require_virtual && !via_virtual)
101 /* Skip this result if we require virtual inheritance
102 and this is not a virtual base. */
103 return dfs_skip_bases;
104
105 if (!data->binfo)
106 {
107 data->binfo = binfo;
108 data->via_virtual = via_virtual;
109
110 if (!data->repeated_base)
111 /* If there are no repeated bases, we can stop now. */
112 return binfo;
113
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;
118
119 return dfs_skip_bases;
120 }
121 else
122 {
123 gcc_assert (binfo != data->binfo);
124
125 /* We've found more than one matching binfo. */
126 if (!data->want_any)
127 {
128 /* This is immediately ambiguous. */
129 data->binfo = NULL_TREE;
130 data->ambiguous = true;
131 return error_mark_node;
132 }
133
134 /* Prefer one via a non-virtual path. */
135 if (!via_virtual)
136 {
137 data->binfo = binfo;
138 data->via_virtual = false;
139 return binfo;
140 }
141
142 /* There must be repeated bases, otherwise we'd have stopped
143 on the first base we found. */
144 return dfs_skip_bases;
145 }
146 }
147
148 return NULL_TREE;
149 }
150
151 /* This deals with bug PR17314.
152
153 DECL is a declaration and BINFO represents a class that has attempted (but
154 failed) to access DECL.
155
156 Examine the parent binfos of BINFO and determine whether any of them had
157 private access to DECL. If they did, return the parent binfo. This helps
158 in figuring out the correct error message to show (if the parents had
159 access, it's their fault for not giving sufficient access to BINFO).
160
161 If no parents had access, return NULL_TREE. */
162
163 tree
164 get_parent_with_private_access (tree decl, tree binfo)
165 {
166 /* Only BINFOs should come through here. */
167 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
168
169 tree base_binfo = NULL_TREE;
170
171 /* Iterate through immediate parent classes.
172 Note that the base list might contain WILDCARD_TYPE_P types, that
173 should be ignored here. */
174 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
175 {
176 tree base_binfo_type = BINFO_TYPE (base_binfo);
177 /* This parent had private access. Therefore that's why BINFO can't
178 access DECL. */
179 if (RECORD_OR_UNION_TYPE_P (base_binfo_type)
180 && access_in_type (base_binfo_type, decl) == ak_private)
181 return base_binfo;
182 }
183
184 /* None of the parents had access. Note: it's impossible for one of the
185 parents to have had public or protected access to DECL, since then
186 BINFO would have been able to access DECL too. */
187 return NULL_TREE;
188 }
189
190 /* Returns true if type BASE is accessible in T. (BASE is known to be
191 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
192 true, consider any special access of the current scope, or access
193 bestowed by friendship. */
194
195 bool
196 accessible_base_p (tree t, tree base, bool consider_local_p)
197 {
198 tree decl;
199
200 /* [class.access.base]
201
202 A base class is said to be accessible if an invented public
203 member of the base class is accessible.
204
205 If BASE is a non-proper base, this condition is trivially
206 true. */
207 if (same_type_p (t, base))
208 return true;
209 /* Rather than inventing a public member, we use the implicit
210 public typedef created in the scope of every class. */
211 decl = TYPE_FIELDS (base);
212 while (!DECL_SELF_REFERENCE_P (decl))
213 decl = DECL_CHAIN (decl);
214 while (ANON_AGGR_TYPE_P (t))
215 t = TYPE_CONTEXT (t);
216 return accessible_p (t, decl, consider_local_p);
217 }
218
219 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
220 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
221 non-NULL, fill with information about what kind of base we
222 discovered. If OFFSET is other than -1, only match at that offset.
223
224 If the base is inaccessible, or ambiguous, then error_mark_node is
225 returned. If the tf_error bit of COMPLAIN is not set, no error
226 is issued. */
227
228 tree
229 lookup_base (tree t, tree base, base_access access,
230 base_kind *kind_ptr, tsubst_flags_t complain,
231 HOST_WIDE_INT offset /* = -1 */)
232 {
233 tree binfo;
234 tree t_binfo;
235 base_kind bk;
236
237 /* "Nothing" is definitely not derived from Base. */
238 if (t == NULL_TREE)
239 {
240 if (kind_ptr)
241 *kind_ptr = bk_not_base;
242 return NULL_TREE;
243 }
244
245 if (t == error_mark_node || base == error_mark_node)
246 {
247 if (kind_ptr)
248 *kind_ptr = bk_not_base;
249 return error_mark_node;
250 }
251 gcc_assert (TYPE_P (base));
252
253 if (!TYPE_P (t))
254 {
255 t_binfo = t;
256 t = BINFO_TYPE (t);
257 }
258 else
259 {
260 t = complete_type (TYPE_MAIN_VARIANT (t));
261 if (dependent_type_p (t))
262 if (tree open = currently_open_class (t))
263 t = open;
264 t_binfo = TYPE_BINFO (t);
265 }
266
267 base = TYPE_MAIN_VARIANT (base);
268
269 /* If BASE is incomplete, it can't be a base of T--and instantiating it
270 might cause an error. */
271 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
272 {
273 struct lookup_base_data_s data;
274
275 data.t = t;
276 data.base = base;
277 data.binfo = NULL_TREE;
278 data.ambiguous = data.via_virtual = false;
279 data.repeated_base = (offset == -1) && CLASSTYPE_REPEATED_BASE_P (t);
280 data.want_any = access == ba_any;
281 data.offset = offset;
282 data.require_virtual = (access & ba_require_virtual);
283
284 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
285 binfo = data.binfo;
286
287 if (!binfo)
288 bk = data.ambiguous ? bk_ambig : bk_not_base;
289 else if (binfo == t_binfo)
290 bk = bk_same_type;
291 else if (data.via_virtual)
292 bk = bk_via_virtual;
293 else
294 bk = bk_proper_base;
295 }
296 else
297 {
298 binfo = NULL_TREE;
299 bk = bk_not_base;
300 }
301
302 /* Check that the base is unambiguous and accessible. */
303 if (access != ba_any)
304 switch (bk)
305 {
306 case bk_not_base:
307 break;
308
309 case bk_ambig:
310 if (complain & tf_error)
311 error ("%qT is an ambiguous base of %qT", base, t);
312 binfo = error_mark_node;
313 break;
314
315 default:
316 if ((access & ba_check_bit)
317 /* If BASE is incomplete, then BASE and TYPE are probably
318 the same, in which case BASE is accessible. If they
319 are not the same, then TYPE is invalid. In that case,
320 there's no need to issue another error here, and
321 there's no implicit typedef to use in the code that
322 follows, so we skip the check. */
323 && COMPLETE_TYPE_P (base)
324 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
325 {
326 if (complain & tf_error)
327 error ("%qT is an inaccessible base of %qT", base, t);
328 binfo = error_mark_node;
329 bk = bk_inaccessible;
330 }
331 break;
332 }
333
334 if (kind_ptr)
335 *kind_ptr = bk;
336
337 return binfo;
338 }
339
340 /* Data for dcast_base_hint walker. */
341
342 struct dcast_data_s
343 {
344 tree subtype; /* The base type we're looking for. */
345 int virt_depth; /* Number of virtual bases encountered from most
346 derived. */
347 tree offset; /* Best hint offset discovered so far. */
348 bool repeated_base; /* Whether there are repeated bases in the
349 hierarchy. */
350 };
351
352 /* Worker for dcast_base_hint. Search for the base type being cast
353 from. */
354
355 static tree
356 dfs_dcast_hint_pre (tree binfo, void *data_)
357 {
358 struct dcast_data_s *data = (struct dcast_data_s *) data_;
359
360 if (BINFO_VIRTUAL_P (binfo))
361 data->virt_depth++;
362
363 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
364 {
365 if (data->virt_depth)
366 {
367 data->offset = ssize_int (-1);
368 return data->offset;
369 }
370 if (data->offset)
371 data->offset = ssize_int (-3);
372 else
373 data->offset = BINFO_OFFSET (binfo);
374
375 return data->repeated_base ? dfs_skip_bases : data->offset;
376 }
377
378 return NULL_TREE;
379 }
380
381 /* Worker for dcast_base_hint. Track the virtual depth. */
382
383 static tree
384 dfs_dcast_hint_post (tree binfo, void *data_)
385 {
386 struct dcast_data_s *data = (struct dcast_data_s *) data_;
387
388 if (BINFO_VIRTUAL_P (binfo))
389 data->virt_depth--;
390
391 return NULL_TREE;
392 }
393
394 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
395 started from is related to the required TARGET type, in order to optimize
396 the inheritance graph search. This information is independent of the
397 current context, and ignores private paths, hence get_base_distance is
398 inappropriate. Return a TREE specifying the base offset, BOFF.
399 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
400 and there are no public virtual SUBTYPE bases.
401 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
402 BOFF == -2, SUBTYPE is not a public base.
403 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
404
405 tree
406 dcast_base_hint (tree subtype, tree target)
407 {
408 struct dcast_data_s data;
409
410 data.subtype = subtype;
411 data.virt_depth = 0;
412 data.offset = NULL_TREE;
413 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
414
415 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
416 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
417 return data.offset ? data.offset : ssize_int (-2);
418 }
419
420 /* Search for a member with name NAME in a multiple inheritance
421 lattice specified by TYPE. If it does not exist, return NULL_TREE.
422 If the member is ambiguously referenced, return `error_mark_node'.
423 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
424 true, type declarations are preferred. */
425
426 /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
427 NAMESPACE_DECL corresponding to the innermost non-block scope. */
428
429 tree
430 current_scope (void)
431 {
432 /* There are a number of cases we need to be aware of here:
433 current_class_type current_function_decl
434 global NULL NULL
435 fn-local NULL SET
436 class-local SET NULL
437 class->fn SET SET
438 fn->class SET SET
439
440 Those last two make life interesting. If we're in a function which is
441 itself inside a class, we need decls to go into the fn's decls (our
442 second case below). But if we're in a class and the class itself is
443 inside a function, we need decls to go into the decls for the class. To
444 achieve this last goal, we must see if, when both current_class_ptr and
445 current_function_decl are set, the class was declared inside that
446 function. If so, we know to put the decls into the class's scope. */
447 if (current_function_decl && current_class_type
448 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
449 && same_type_p (DECL_CONTEXT (current_function_decl),
450 current_class_type))
451 || (DECL_FRIEND_CONTEXT (current_function_decl)
452 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
453 current_class_type))))
454 return current_function_decl;
455
456 if (current_class_type)
457 return current_class_type;
458
459 if (current_function_decl)
460 return current_function_decl;
461
462 return current_namespace;
463 }
464
465 /* Returns nonzero if we are currently in a function scope. Note
466 that this function returns zero if we are within a local class, but
467 not within a member function body of the local class. */
468
469 int
470 at_function_scope_p (void)
471 {
472 tree cs = current_scope ();
473 /* Also check cfun to make sure that we're really compiling
474 this function (as opposed to having set current_function_decl
475 for access checking or some such). */
476 return (cs && TREE_CODE (cs) == FUNCTION_DECL
477 && cfun && cfun->decl == current_function_decl);
478 }
479
480 /* Returns true if the innermost active scope is a class scope. */
481
482 bool
483 at_class_scope_p (void)
484 {
485 tree cs = current_scope ();
486 return cs && TYPE_P (cs);
487 }
488
489 /* Returns true if the innermost active scope is a namespace scope. */
490
491 bool
492 at_namespace_scope_p (void)
493 {
494 tree cs = current_scope ();
495 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
496 }
497
498 /* Return the scope of DECL, as appropriate when doing name-lookup. */
499
500 tree
501 context_for_name_lookup (tree decl)
502 {
503 /* [class.union]
504
505 For the purposes of name lookup, after the anonymous union
506 definition, the members of the anonymous union are considered to
507 have been defined in the scope in which the anonymous union is
508 declared. */
509 tree context = DECL_CONTEXT (decl);
510
511 while (context && TYPE_P (context)
512 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
513 context = TYPE_CONTEXT (context);
514 if (!context)
515 context = global_namespace;
516
517 return context;
518 }
519
520 /* Like the above, but always return a type, because it's simpler for member
521 handling to refer to the anonymous aggr rather than a function. */
522
523 tree
524 type_context_for_name_lookup (tree decl)
525 {
526 tree context = DECL_P (decl) ? DECL_CONTEXT (decl) : decl;
527 gcc_checking_assert (CLASS_TYPE_P (context));
528
529 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
530 {
531 tree next = TYPE_CONTEXT (context);
532 if (!TYPE_P (next))
533 break;
534 context = next;
535 }
536 return context;
537 }
538
539 /* Returns true iff DECL is declared in TYPE. */
540
541 static bool
542 member_declared_in_type (tree decl, tree type)
543 {
544 /* A normal declaration obviously counts. */
545 if (context_for_name_lookup (decl) == type)
546 return true;
547 /* So does a using or access declaration. */
548 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
549 && purpose_member (type, DECL_ACCESS (decl)))
550 return true;
551 return false;
552 }
553
554 /* The accessibility routines use BINFO_ACCESS for scratch space
555 during the computation of the accessibility of some declaration. */
556
557 /* Avoid walking up past a declaration of the member. */
558
559 static tree
560 dfs_access_in_type_pre (tree binfo, void *data)
561 {
562 tree decl = (tree) data;
563 tree type = BINFO_TYPE (binfo);
564 if (member_declared_in_type (decl, type))
565 return dfs_skip_bases;
566 return NULL_TREE;
567 }
568
569 #define BINFO_ACCESS(NODE) \
570 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
571
572 /* Set the access associated with NODE to ACCESS. */
573
574 #define SET_BINFO_ACCESS(NODE, ACCESS) \
575 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
576 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
577
578 /* Called from access_in_type via dfs_walk. Calculate the access to
579 DATA (which is really a DECL) in BINFO. */
580
581 static tree
582 dfs_access_in_type (tree binfo, void *data)
583 {
584 tree decl = (tree) data;
585 tree type = BINFO_TYPE (binfo);
586 access_kind access = ak_none;
587
588 if (context_for_name_lookup (decl) == type)
589 {
590 /* If we have descended to the scope of DECL, just note the
591 appropriate access. */
592 if (TREE_PRIVATE (decl))
593 access = ak_private;
594 else if (TREE_PROTECTED (decl))
595 access = ak_protected;
596 else
597 access = ak_public;
598 }
599 else
600 {
601 /* First, check for an access-declaration that gives us more
602 access to the DECL. */
603 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
604 {
605 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
606
607 if (decl_access)
608 {
609 decl_access = TREE_VALUE (decl_access);
610
611 if (decl_access == access_public_node)
612 access = ak_public;
613 else if (decl_access == access_protected_node)
614 access = ak_protected;
615 else if (decl_access == access_private_node)
616 access = ak_private;
617 else
618 gcc_unreachable ();
619 }
620 }
621
622 if (!access)
623 {
624 int i;
625 tree base_binfo;
626 vec<tree, va_gc> *accesses;
627
628 /* Otherwise, scan our baseclasses, and pick the most favorable
629 access. */
630 accesses = BINFO_BASE_ACCESSES (binfo);
631 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
632 {
633 tree base_access = (*accesses)[i];
634 access_kind base_access_now = BINFO_ACCESS (base_binfo);
635
636 if (base_access_now == ak_none || base_access_now == ak_private)
637 /* If it was not accessible in the base, or only
638 accessible as a private member, we can't access it
639 all. */
640 base_access_now = ak_none;
641 else if (base_access == access_protected_node)
642 /* Public and protected members in the base become
643 protected here. */
644 base_access_now = ak_protected;
645 else if (base_access == access_private_node)
646 /* Public and protected members in the base become
647 private here. */
648 base_access_now = ak_private;
649
650 /* See if the new access, via this base, gives more
651 access than our previous best access. */
652 if (base_access_now != ak_none
653 && (access == ak_none || base_access_now < access))
654 {
655 access = base_access_now;
656
657 /* If the new access is public, we can't do better. */
658 if (access == ak_public)
659 break;
660 }
661 }
662 }
663 }
664
665 /* Note the access to DECL in TYPE. */
666 SET_BINFO_ACCESS (binfo, access);
667
668 return NULL_TREE;
669 }
670
671 /* Return the access to DECL in TYPE. */
672
673 static access_kind
674 access_in_type (tree type, tree decl)
675 {
676 tree binfo = TYPE_BINFO (type);
677
678 /* We must take into account
679
680 [class.paths]
681
682 If a name can be reached by several paths through a multiple
683 inheritance graph, the access is that of the path that gives
684 most access.
685
686 The algorithm we use is to make a post-order depth-first traversal
687 of the base-class hierarchy. As we come up the tree, we annotate
688 each node with the most lenient access. */
689 dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
690
691 return BINFO_ACCESS (binfo);
692 }
693
694 /* Returns nonzero if it is OK to access DECL named in TYPE through an object
695 of OTYPE in the context of DERIVED. */
696
697 static int
698 protected_accessible_p (tree decl, tree derived, tree type, tree otype)
699 {
700 /* We're checking this clause from [class.access.base]
701
702 m as a member of N is protected, and the reference occurs in a
703 member or friend of class N, or in a member or friend of a
704 class P derived from N, where m as a member of P is public, private
705 or protected.
706
707 Here DERIVED is a possible P, DECL is m and TYPE is N. */
708
709 /* If DERIVED isn't derived from N, then it can't be a P. */
710 if (!DERIVED_FROM_P (type, derived))
711 return 0;
712
713 /* DECL_NONSTATIC_MEMBER_P won't work for USING_DECLs. */
714 decl = strip_using_decl (decl);
715 /* We don't expect or support dependent decls. */
716 gcc_assert (TREE_CODE (decl) != USING_DECL);
717
718 /* [class.protected]
719
720 When a friend or a member function of a derived class references
721 a protected non-static member of a base class, an access check
722 applies in addition to those described earlier in clause
723 _class.access_) Except when forming a pointer to member
724 (_expr.unary.op_), the access must be through a pointer to,
725 reference to, or object of the derived class itself (or any class
726 derived from that class) (_expr.ref_). If the access is to form
727 a pointer to member, the nested-name-specifier shall name the
728 derived class (or any class derived from that class). */
729 if (DECL_NONSTATIC_MEMBER_P (decl)
730 && !DERIVED_FROM_P (derived, otype))
731 return 0;
732
733 return 1;
734 }
735
736 /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
737 to access DECL through TYPE. OTYPE is the type of the object. */
738
739 static int
740 friend_accessible_p (tree scope, tree decl, tree type, tree otype)
741 {
742 /* We're checking this clause from [class.access.base]
743
744 m as a member of N is protected, and the reference occurs in a
745 member or friend of class N, or in a member or friend of a
746 class P derived from N, where m as a member of P is public, private
747 or protected.
748
749 Here DECL is m and TYPE is N. SCOPE is the current context,
750 and we check all its possible Ps. */
751 tree befriending_classes;
752 tree t;
753
754 if (!scope)
755 return 0;
756
757 if (is_global_friend (scope))
758 return 1;
759
760 /* Is SCOPE itself a suitable P? */
761 if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
762 return 1;
763
764 if (DECL_DECLARES_FUNCTION_P (scope))
765 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
766 else if (TYPE_P (scope))
767 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
768 else
769 return 0;
770
771 for (t = befriending_classes; t; t = TREE_CHAIN (t))
772 if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
773 return 1;
774
775 /* Nested classes have the same access as their enclosing types, as
776 per DR 45 (this is a change from C++98). */
777 if (TYPE_P (scope))
778 if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
779 return 1;
780
781 if (DECL_DECLARES_FUNCTION_P (scope))
782 {
783 /* Perhaps this SCOPE is a member of a class which is a
784 friend. */
785 if (DECL_CLASS_SCOPE_P (scope)
786 && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
787 return 1;
788 /* Perhaps SCOPE is a friend function defined inside a class from which
789 DECL is accessible. */
790 if (tree fctx = DECL_FRIEND_CONTEXT (scope))
791 if (friend_accessible_p (fctx, decl, type, otype))
792 return 1;
793 }
794
795 /* Maybe scope's template is a friend. */
796 if (tree tinfo = get_template_info (scope))
797 {
798 tree tmpl = TI_TEMPLATE (tinfo);
799 if (DECL_CLASS_TEMPLATE_P (tmpl))
800 tmpl = TREE_TYPE (tmpl);
801 else
802 tmpl = DECL_TEMPLATE_RESULT (tmpl);
803 if (tmpl != scope)
804 {
805 /* Increment processing_template_decl to make sure that
806 dependent_type_p works correctly. */
807 ++processing_template_decl;
808 int ret = friend_accessible_p (tmpl, decl, type, otype);
809 --processing_template_decl;
810 if (ret)
811 return 1;
812 }
813 }
814
815 /* If is_friend is true, we should have found a befriending class. */
816 gcc_checking_assert (!is_friend (type, scope));
817
818 return 0;
819 }
820
821 struct dfs_accessible_data
822 {
823 tree decl;
824 tree object_type;
825 };
826
827 /* Avoid walking up past a declaration of the member. */
828
829 static tree
830 dfs_accessible_pre (tree binfo, void *data)
831 {
832 dfs_accessible_data *d = (dfs_accessible_data *)data;
833 tree type = BINFO_TYPE (binfo);
834 if (member_declared_in_type (d->decl, type))
835 return dfs_skip_bases;
836 return NULL_TREE;
837 }
838
839 /* Called via dfs_walk_once_accessible from accessible_p */
840
841 static tree
842 dfs_accessible_post (tree binfo, void *data)
843 {
844 /* access_in_type already set BINFO_ACCESS for us. */
845 access_kind access = BINFO_ACCESS (binfo);
846 tree N = BINFO_TYPE (binfo);
847 dfs_accessible_data *d = (dfs_accessible_data *)data;
848 tree decl = d->decl;
849 tree scope = current_nonlambda_scope ();
850
851 /* A member m is accessible at the point R when named in class N if */
852 switch (access)
853 {
854 case ak_none:
855 return NULL_TREE;
856
857 case ak_public:
858 /* m as a member of N is public, or */
859 return binfo;
860
861 case ak_private:
862 {
863 /* m as a member of N is private, and R occurs in a member or friend of
864 class N, or */
865 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
866 && is_friend (N, scope))
867 return binfo;
868 return NULL_TREE;
869 }
870
871 case ak_protected:
872 {
873 /* m as a member of N is protected, and R occurs in a member or friend
874 of class N, or in a member or friend of a class P derived from N,
875 where m as a member of P is public, private, or protected */
876 if (friend_accessible_p (scope, decl, N, d->object_type))
877 return binfo;
878 return NULL_TREE;
879 }
880
881 default:
882 gcc_unreachable ();
883 }
884 }
885
886 /* Like accessible_p below, but within a template returns true iff DECL is
887 accessible in TYPE to all possible instantiations of the template. */
888
889 int
890 accessible_in_template_p (tree type, tree decl)
891 {
892 int save_ptd = processing_template_decl;
893 processing_template_decl = 0;
894 int val = accessible_p (type, decl, false);
895 processing_template_decl = save_ptd;
896 return val;
897 }
898
899 /* DECL is a declaration from a base class of TYPE, which was the
900 class used to name DECL. Return nonzero if, in the current
901 context, DECL is accessible. If TYPE is actually a BINFO node,
902 then we can tell in what context the access is occurring by looking
903 at the most derived class along the path indicated by BINFO. If
904 CONSIDER_LOCAL is true, do consider special access the current
905 scope or friendship thereof we might have. */
906
907 int
908 accessible_p (tree type, tree decl, bool consider_local_p)
909 {
910 tree binfo;
911 access_kind access;
912
913 /* If this declaration is in a block or namespace scope, there's no
914 access control. */
915 if (!TYPE_P (context_for_name_lookup (decl)))
916 return 1;
917
918 /* There is no need to perform access checks inside a thunk. */
919 if (current_function_decl && DECL_THUNK_P (current_function_decl))
920 return 1;
921
922 tree otype = NULL_TREE;
923 if (!TYPE_P (type))
924 {
925 /* When accessing a non-static member, the most derived type in the
926 binfo chain is the type of the object; remember that type for
927 protected_accessible_p. */
928 for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
929 otype = BINFO_TYPE (b);
930 type = BINFO_TYPE (type);
931 }
932 else
933 otype = type;
934
935 /* Anonymous unions don't have their own access. */
936 if (ANON_AGGR_TYPE_P (type))
937 type = type_context_for_name_lookup (type);
938
939 /* [class.access.base]
940
941 A member m is accessible when named in class N if
942
943 --m as a member of N is public, or
944
945 --m as a member of N is private, and the reference occurs in a
946 member or friend of class N, or
947
948 --m as a member of N is protected, and the reference occurs in a
949 member or friend of class N, or in a member or friend of a
950 class P derived from N, where m as a member of P is public, private or
951 protected, or
952
953 --there exists a base class B of N that is accessible at the point
954 of reference, and m is accessible when named in class B.
955
956 We walk the base class hierarchy, checking these conditions. */
957
958 /* We walk using TYPE_BINFO (type) because access_in_type will set
959 BINFO_ACCESS on it and its bases. */
960 binfo = TYPE_BINFO (type);
961
962 /* Compute the accessibility of DECL in the class hierarchy
963 dominated by type. */
964 access = access_in_type (type, decl);
965 if (access == ak_public)
966 return 1;
967
968 /* If we aren't considering the point of reference, only the first bullet
969 applies. */
970 if (!consider_local_p)
971 return 0;
972
973 dfs_accessible_data d = { decl, otype };
974
975 /* Walk the hierarchy again, looking for a base class that allows
976 access. */
977 return dfs_walk_once_accessible (binfo, /*friends=*/true,
978 dfs_accessible_pre,
979 dfs_accessible_post, &d)
980 != NULL_TREE;
981 }
982
983 struct lookup_field_info {
984 /* The type in which we're looking. */
985 tree type;
986 /* The name of the field for which we're looking. */
987 tree name;
988 /* If non-NULL, the current result of the lookup. */
989 tree rval;
990 /* The path to RVAL. */
991 tree rval_binfo;
992 /* If non-NULL, the lookup was ambiguous, and this is a list of the
993 candidates. */
994 tree ambiguous;
995 /* If nonzero, we are looking for types, not data members. */
996 int want_type;
997 };
998
999 /* True for a class member means that it is shared between all objects
1000 of that class.
1001
1002 [class.member.lookup]:If the resulting set of declarations are not all
1003 from sub-objects of the same type, or the set has a non-static member
1004 and includes members from distinct sub-objects, there is an ambiguity
1005 and the program is ill-formed.
1006
1007 This function checks that T contains no non-static members. */
1008
1009 bool
1010 shared_member_p (tree t)
1011 {
1012 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL
1013 || TREE_CODE (t) == CONST_DECL)
1014 return true;
1015 if (is_overloaded_fn (t))
1016 {
1017 for (ovl_iterator iter (get_fns (t)); iter; ++iter)
1018 {
1019 tree decl = strip_using_decl (*iter);
1020 if (TREE_CODE (decl) == USING_DECL)
1021 /* Conservatively assume a dependent using-declaration
1022 might resolve to a non-static member. */
1023 return false;
1024 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl))
1025 return false;
1026 }
1027 return true;
1028 }
1029 return false;
1030 }
1031
1032 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1033 found as a base class and sub-object of the object denoted by
1034 BINFO. */
1035
1036 static int
1037 is_subobject_of_p (tree parent, tree binfo)
1038 {
1039 tree probe;
1040
1041 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1042 {
1043 if (probe == binfo)
1044 return 1;
1045 if (BINFO_VIRTUAL_P (probe))
1046 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1047 != NULL_TREE);
1048 }
1049 return 0;
1050 }
1051
1052 /* DATA is really a struct lookup_field_info. Look for a field with
1053 the name indicated there in BINFO. If this function returns a
1054 non-NULL value it is the result of the lookup. Called from
1055 lookup_field via breadth_first_search. */
1056
1057 static tree
1058 lookup_field_r (tree binfo, void *data)
1059 {
1060 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1061 tree type = BINFO_TYPE (binfo);
1062 tree nval = NULL_TREE;
1063
1064 /* If this is a dependent base, don't look in it. */
1065 if (BINFO_DEPENDENT_BASE_P (binfo))
1066 return NULL_TREE;
1067
1068 /* If this base class is hidden by the best-known value so far, we
1069 don't need to look. */
1070 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1071 && !BINFO_VIRTUAL_P (binfo))
1072 return dfs_skip_bases;
1073
1074 nval = get_class_binding (type, lfi->name, lfi->want_type);
1075
1076 /* If there is no declaration with the indicated name in this type,
1077 then there's nothing to do. */
1078 if (!nval)
1079 goto done;
1080
1081 /* If the lookup already found a match, and the new value doesn't
1082 hide the old one, we might have an ambiguity. */
1083 if (lfi->rval_binfo
1084 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1085
1086 {
1087 if (nval == lfi->rval && shared_member_p (nval))
1088 /* The two things are really the same. */
1089 ;
1090 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1091 /* The previous value hides the new one. */
1092 ;
1093 else
1094 {
1095 /* We have a real ambiguity. We keep a chain of all the
1096 candidates. */
1097 if (!lfi->ambiguous && lfi->rval)
1098 {
1099 /* This is the first time we noticed an ambiguity. Add
1100 what we previously thought was a reasonable candidate
1101 to the list. */
1102 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1103 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1104 }
1105
1106 /* Add the new value. */
1107 if (TREE_CODE (nval) == TREE_LIST)
1108 lfi->ambiguous = chainon (nval, lfi->ambiguous);
1109 else
1110 {
1111 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1112 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1113 }
1114 }
1115 }
1116 else
1117 {
1118 if (TREE_CODE (nval) == TREE_LIST)
1119 {
1120 lfi->ambiguous = chainon (nval, lfi->ambiguous);
1121 lfi->rval = TREE_VALUE (nval);
1122 }
1123 else
1124 lfi->rval = nval;
1125 lfi->rval_binfo = binfo;
1126 }
1127
1128 done:
1129 /* Don't look for constructors or destructors in base classes. */
1130 if (IDENTIFIER_CDTOR_P (lfi->name))
1131 return dfs_skip_bases;
1132 return NULL_TREE;
1133 }
1134
1135 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1136 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1137 FUNCTIONS, and OPTYPE respectively. */
1138
1139 tree
1140 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1141 {
1142 tree baselink;
1143
1144 gcc_assert (OVL_P (functions) || TREE_CODE (functions) == TEMPLATE_ID_EXPR);
1145 gcc_assert (!optype || TYPE_P (optype));
1146 gcc_assert (TREE_TYPE (functions));
1147
1148 baselink = make_node (BASELINK);
1149 TREE_TYPE (baselink) = TREE_TYPE (functions);
1150 BASELINK_BINFO (baselink) = binfo;
1151 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1152 BASELINK_FUNCTIONS (baselink) = functions;
1153 BASELINK_OPTYPE (baselink) = optype;
1154
1155 if (binfo == access_binfo
1156 && TYPE_BEING_DEFINED (BINFO_TYPE (access_binfo)))
1157 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
1158
1159 return baselink;
1160 }
1161
1162 /* Look for a member named NAME in an inheritance lattice dominated by
1163 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1164 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1165 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1166 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1167 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1168 TREE_VALUEs are the list of ambiguous candidates.
1169
1170 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1171
1172 If nothing can be found return NULL_TREE and do not issue an error.
1173
1174 If non-NULL, failure information is written back to AFI. */
1175
1176 tree
1177 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
1178 tsubst_flags_t complain, access_failure_info *afi /* = NULL */)
1179 {
1180 tree rval, rval_binfo = NULL_TREE;
1181 tree type = NULL_TREE, basetype_path = NULL_TREE;
1182 struct lookup_field_info lfi;
1183
1184 /* rval_binfo is the binfo associated with the found member, note,
1185 this can be set with useful information, even when rval is not
1186 set, because it must deal with ALL members, not just non-function
1187 members. It is used for ambiguity checking and the hidden
1188 checks. Whereas rval is only set if a proper (not hidden)
1189 non-function member is found. */
1190
1191 if (name == error_mark_node
1192 || xbasetype == NULL_TREE
1193 || xbasetype == error_mark_node)
1194 return NULL_TREE;
1195
1196 gcc_assert (identifier_p (name));
1197
1198 if (TREE_CODE (xbasetype) == TREE_BINFO)
1199 {
1200 type = BINFO_TYPE (xbasetype);
1201 basetype_path = xbasetype;
1202 }
1203 else
1204 {
1205 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1206 return NULL_TREE;
1207 type = xbasetype;
1208 xbasetype = NULL_TREE;
1209 }
1210
1211 type = complete_type (type);
1212
1213 /* Make sure we're looking for a member of the current instantiation in the
1214 right partial specialization. */
1215 if (dependent_type_p (type))
1216 if (tree t = currently_open_class (type))
1217 type = t;
1218
1219 if (!basetype_path)
1220 basetype_path = TYPE_BINFO (type);
1221
1222 if (!basetype_path)
1223 return NULL_TREE;
1224
1225 memset (&lfi, 0, sizeof (lfi));
1226 lfi.type = type;
1227 lfi.name = name;
1228 lfi.want_type = want_type;
1229 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1230 rval = lfi.rval;
1231 rval_binfo = lfi.rval_binfo;
1232 if (rval_binfo)
1233 type = BINFO_TYPE (rval_binfo);
1234
1235 if (lfi.ambiguous)
1236 {
1237 if (protect == 0)
1238 return NULL_TREE;
1239 else if (protect == 1)
1240 {
1241 if (complain & tf_error)
1242 {
1243 auto_diagnostic_group d;
1244 error ("request for member %qD is ambiguous", name);
1245 print_candidates (lfi.ambiguous);
1246 }
1247 return error_mark_node;
1248 }
1249 else if (protect == 2)
1250 return lfi.ambiguous;
1251 }
1252
1253 if (!rval)
1254 return NULL_TREE;
1255
1256 /* [class.access]
1257
1258 In the case of overloaded function names, access control is
1259 applied to the function selected by overloaded resolution.
1260
1261 We cannot check here, even if RVAL is only a single non-static
1262 member function, since we do not know what the "this" pointer
1263 will be. For:
1264
1265 class A { protected: void f(); };
1266 class B : public A {
1267 void g(A *p) {
1268 f(); // OK
1269 p->f(); // Not OK.
1270 }
1271 };
1272
1273 only the first call to "f" is valid. However, if the function is
1274 static, we can check. */
1275 if (protect == 1 && !really_overloaded_fn (rval))
1276 {
1277 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
1278 decl = strip_using_decl (decl);
1279 /* A dependent USING_DECL will be checked after tsubsting. */
1280 if (TREE_CODE (decl) != USING_DECL
1281 && !DECL_IOBJ_MEMBER_FUNCTION_P (decl)
1282 && !perform_or_defer_access_check (basetype_path, decl, decl,
1283 complain, afi))
1284 return error_mark_node;
1285 }
1286
1287 if (is_overloaded_fn (rval)
1288 /* Don't use a BASELINK for class-scope deduction guides since
1289 they're not actually member functions. */
1290 && !dguide_name_p (name))
1291 rval = build_baselink (rval_binfo, basetype_path, rval,
1292 (IDENTIFIER_CONV_OP_P (name)
1293 ? TREE_TYPE (name) : NULL_TREE));
1294 return rval;
1295 }
1296
1297 /* Helper class for lookup_member_fuzzy. */
1298
1299 class lookup_field_fuzzy_info
1300 {
1301 public:
1302 lookup_field_fuzzy_info (bool want_type_p) :
1303 m_want_type_p (want_type_p), m_candidates () {}
1304
1305 void fuzzy_lookup_field (tree type);
1306
1307 /* If true, we are looking for types, not data members. */
1308 bool m_want_type_p;
1309 /* The result: a vec of identifiers. */
1310 auto_vec<tree> m_candidates;
1311 };
1312
1313 /* Locate all fields within TYPE, append them to m_candidates. */
1314
1315 void
1316 lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
1317 {
1318 if (!CLASS_TYPE_P (type))
1319 return;
1320
1321 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1322 {
1323 if (m_want_type_p && !DECL_DECLARES_TYPE_P (field))
1324 continue;
1325
1326 if (!DECL_NAME (field))
1327 continue;
1328
1329 if (is_lambda_ignored_entity (field))
1330 continue;
1331
1332 /* Ignore special identifiers with space at the end like cdtor or
1333 conversion op identifiers. */
1334 if (TREE_CODE (DECL_NAME (field)) == IDENTIFIER_NODE)
1335 if (unsigned int len = IDENTIFIER_LENGTH (DECL_NAME (field)))
1336 if (IDENTIFIER_POINTER (DECL_NAME (field))[len - 1] == ' ')
1337 continue;
1338
1339 m_candidates.safe_push (DECL_NAME (field));
1340 }
1341 }
1342
1343
1344 /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
1345 DATA is really a lookup_field_fuzzy_info. Look for a field with
1346 the name indicated there in BINFO. Gathers pertinent identifiers into
1347 m_candidates. */
1348
1349 static tree
1350 lookup_field_fuzzy_r (tree binfo, void *data)
1351 {
1352 lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
1353 tree type = BINFO_TYPE (binfo);
1354
1355 lffi->fuzzy_lookup_field (type);
1356
1357 return NULL_TREE;
1358 }
1359
1360 /* Like lookup_member, but try to find the closest match for NAME,
1361 rather than an exact match, and return an identifier (or NULL_TREE).
1362 Do not complain. */
1363
1364 tree
1365 lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
1366 {
1367 tree type = NULL_TREE, basetype_path = NULL_TREE;
1368 class lookup_field_fuzzy_info lffi (want_type_p);
1369
1370 /* rval_binfo is the binfo associated with the found member, note,
1371 this can be set with useful information, even when rval is not
1372 set, because it must deal with ALL members, not just non-function
1373 members. It is used for ambiguity checking and the hidden
1374 checks. Whereas rval is only set if a proper (not hidden)
1375 non-function member is found. */
1376
1377 if (name == error_mark_node
1378 || xbasetype == NULL_TREE
1379 || xbasetype == error_mark_node)
1380 return NULL_TREE;
1381
1382 gcc_assert (identifier_p (name));
1383
1384 if (TREE_CODE (xbasetype) == TREE_BINFO)
1385 {
1386 type = BINFO_TYPE (xbasetype);
1387 basetype_path = xbasetype;
1388 }
1389 else
1390 {
1391 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
1392 return NULL_TREE;
1393 type = xbasetype;
1394 xbasetype = NULL_TREE;
1395 }
1396
1397 type = complete_type (type);
1398
1399 /* Make sure we're looking for a member of the current instantiation in the
1400 right partial specialization. */
1401 if (flag_concepts && dependent_type_p (type))
1402 type = currently_open_class (type);
1403
1404 if (!basetype_path)
1405 basetype_path = TYPE_BINFO (type);
1406
1407 if (!basetype_path)
1408 return NULL_TREE;
1409
1410 /* Populate lffi.m_candidates. */
1411 dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
1412
1413 return find_closest_identifier (name, &lffi.m_candidates);
1414 }
1415
1416 /* Like lookup_member, except that if we find a function member we
1417 return NULL_TREE. */
1418
1419 tree
1420 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1421 {
1422 tree rval = lookup_member (xbasetype, name, protect, want_type,
1423 tf_warning_or_error);
1424
1425 /* Ignore functions, but propagate the ambiguity list. */
1426 if (!error_operand_p (rval)
1427 && (rval && BASELINK_P (rval)))
1428 return NULL_TREE;
1429
1430 return rval;
1431 }
1432
1433 /* Like lookup_member, except that if we find a non-function member we
1434 return NULL_TREE. */
1435
1436 tree
1437 lookup_fnfields (tree xbasetype, tree name, int protect,
1438 tsubst_flags_t complain)
1439 {
1440 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
1441 complain);
1442
1443 /* Ignore non-functions, but propagate the ambiguity list. */
1444 if (!error_operand_p (rval)
1445 && (rval && !BASELINK_P (rval)))
1446 return NULL_TREE;
1447
1448 return rval;
1449 }
1450
1451 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1452 the class or namespace used to qualify the name. CONTEXT_CLASS is
1453 the class corresponding to the object in which DECL will be used.
1454 Return a possibly modified version of DECL that takes into account
1455 the CONTEXT_CLASS.
1456
1457 In particular, consider an expression like `B::m' in the context of
1458 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1459 then the most derived class indicated by the BASELINK_BINFO will be
1460 `B', not `D'. This function makes that adjustment. */
1461
1462 tree
1463 adjust_result_of_qualified_name_lookup (tree decl,
1464 tree qualifying_scope,
1465 tree context_class)
1466 {
1467 if (context_class && context_class != error_mark_node
1468 && CLASS_TYPE_P (context_class)
1469 && CLASS_TYPE_P (qualifying_scope)
1470 && DERIVED_FROM_P (qualifying_scope, context_class)
1471 && BASELINK_P (decl))
1472 {
1473 tree base;
1474
1475 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1476 Because we do not yet know which function will be chosen by
1477 overload resolution, we cannot yet check either accessibility
1478 or ambiguity -- in either case, the choice of a static member
1479 function might make the usage valid. */
1480 base = lookup_base (context_class, qualifying_scope,
1481 ba_unique, NULL, tf_none);
1482 if (base && base != error_mark_node)
1483 {
1484 BASELINK_ACCESS_BINFO (decl) = base;
1485 tree decl_binfo
1486 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1487 ba_unique, NULL, tf_none);
1488 if (decl_binfo && decl_binfo != error_mark_node)
1489 BASELINK_BINFO (decl) = decl_binfo;
1490 }
1491 }
1492
1493 if (BASELINK_P (decl))
1494 BASELINK_QUALIFIED_P (decl) = true;
1495
1496 return decl;
1497 }
1498
1499
1500 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1501 PRE_FN is called in preorder, while POST_FN is called in postorder.
1502 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1503 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1504 that value is immediately returned and the walk is terminated. One
1505 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1506 POST_FN are passed the binfo to examine and the caller's DATA
1507 value. All paths are walked, thus virtual and morally virtual
1508 binfos can be multiply walked. */
1509
1510 tree
1511 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1512 tree (*post_fn) (tree, void *), void *data)
1513 {
1514 tree rval;
1515 unsigned ix;
1516 tree base_binfo;
1517
1518 /* Call the pre-order walking function. */
1519 if (pre_fn)
1520 {
1521 rval = pre_fn (binfo, data);
1522 if (rval)
1523 {
1524 if (rval == dfs_skip_bases)
1525 goto skip_bases;
1526 return rval;
1527 }
1528 }
1529
1530 /* Find the next child binfo to walk. */
1531 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1532 {
1533 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1534 if (rval)
1535 return rval;
1536 }
1537
1538 skip_bases:
1539 /* Call the post-order walking function. */
1540 if (post_fn)
1541 {
1542 rval = post_fn (binfo, data);
1543 gcc_assert (rval != dfs_skip_bases);
1544 return rval;
1545 }
1546
1547 return NULL_TREE;
1548 }
1549
1550 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1551 that binfos are walked at most once. */
1552
1553 static tree
1554 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1555 tree (*post_fn) (tree, void *), hash_set<tree> *pset,
1556 void *data)
1557 {
1558 tree rval;
1559 unsigned ix;
1560 tree base_binfo;
1561
1562 /* Call the pre-order walking function. */
1563 if (pre_fn)
1564 {
1565 rval = pre_fn (binfo, data);
1566 if (rval)
1567 {
1568 if (rval == dfs_skip_bases)
1569 goto skip_bases;
1570
1571 return rval;
1572 }
1573 }
1574
1575 /* Find the next child binfo to walk. */
1576 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1577 {
1578 if (BINFO_VIRTUAL_P (base_binfo))
1579 if (pset->add (base_binfo))
1580 continue;
1581
1582 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
1583 if (rval)
1584 return rval;
1585 }
1586
1587 skip_bases:
1588 /* Call the post-order walking function. */
1589 if (post_fn)
1590 {
1591 rval = post_fn (binfo, data);
1592 gcc_assert (rval != dfs_skip_bases);
1593 return rval;
1594 }
1595
1596 return NULL_TREE;
1597 }
1598
1599 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1600 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1601 For diamond shaped hierarchies we must mark the virtual bases, to
1602 avoid multiple walks. */
1603
1604 tree
1605 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1606 tree (*post_fn) (tree, void *), void *data)
1607 {
1608 static int active = 0; /* We must not be called recursively. */
1609 tree rval;
1610
1611 gcc_assert (pre_fn || post_fn);
1612 gcc_assert (!active);
1613 active++;
1614
1615 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1616 /* We are not diamond shaped, and therefore cannot encounter the
1617 same binfo twice. */
1618 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1619 else
1620 {
1621 hash_set<tree> pset;
1622 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
1623 }
1624
1625 active--;
1626
1627 return rval;
1628 }
1629
1630 /* Worker function for dfs_walk_once_accessible. Behaves like
1631 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1632 access given by the current context should be considered, (b) ONCE
1633 indicates whether bases should be marked during traversal. */
1634
1635 static tree
1636 dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
1637 tree (*pre_fn) (tree, void *),
1638 tree (*post_fn) (tree, void *), void *data)
1639 {
1640 tree rval = NULL_TREE;
1641 unsigned ix;
1642 tree base_binfo;
1643
1644 /* Call the pre-order walking function. */
1645 if (pre_fn)
1646 {
1647 rval = pre_fn (binfo, data);
1648 if (rval)
1649 {
1650 if (rval == dfs_skip_bases)
1651 goto skip_bases;
1652
1653 return rval;
1654 }
1655 }
1656
1657 /* Find the next child binfo to walk. */
1658 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1659 {
1660 bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
1661
1662 if (mark && pset->contains (base_binfo))
1663 continue;
1664
1665 /* If the base is inherited via private or protected
1666 inheritance, then we can't see it, unless we are a friend of
1667 the current binfo. */
1668 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1669 {
1670 tree scope;
1671 if (!friends_p)
1672 continue;
1673 scope = current_scope ();
1674 if (!scope
1675 || TREE_CODE (scope) == NAMESPACE_DECL
1676 || !is_friend (BINFO_TYPE (binfo), scope))
1677 continue;
1678 }
1679
1680 if (mark)
1681 pset->add (base_binfo);
1682
1683 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
1684 pre_fn, post_fn, data);
1685 if (rval)
1686 return rval;
1687 }
1688
1689 skip_bases:
1690 /* Call the post-order walking function. */
1691 if (post_fn)
1692 {
1693 rval = post_fn (binfo, data);
1694 gcc_assert (rval != dfs_skip_bases);
1695 return rval;
1696 }
1697
1698 return NULL_TREE;
1699 }
1700
1701 /* Like dfs_walk_once except that only accessible bases are walked.
1702 FRIENDS_P indicates whether friendship of the local context
1703 should be considered when determining accessibility. */
1704
1705 static tree
1706 dfs_walk_once_accessible (tree binfo, bool friends_p,
1707 tree (*pre_fn) (tree, void *),
1708 tree (*post_fn) (tree, void *), void *data)
1709 {
1710 hash_set<tree> *pset = NULL;
1711 if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1712 pset = new hash_set<tree>;
1713 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
1714 pre_fn, post_fn, data);
1715
1716 if (pset)
1717 delete pset;
1718 return rval;
1719 }
1720
1721 /* Return true iff the code of T is CODE, and it has compatible
1722 type with TYPE. */
1723
1724 static bool
1725 matches_code_and_type_p (tree t, enum tree_code code, tree type)
1726 {
1727 if (TREE_CODE (t) != code)
1728 return false;
1729 if (!cxx_types_compatible_p (TREE_TYPE (t), type))
1730 return false;
1731 return true;
1732 }
1733
1734 /* Subroutine of direct_accessor_p and reference_accessor_p.
1735 Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
1736 We expect a tree of the form:
1737 <component_ref:
1738 <indirect_ref:S>
1739 <nop_expr:P*
1740 <parm_decl (this)>
1741 <field_decl (FIELD_DECL)>>>. */
1742
1743 static bool
1744 field_access_p (tree component_ref, tree field_decl, tree field_type)
1745 {
1746 if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
1747 return false;
1748
1749 tree indirect_ref = TREE_OPERAND (component_ref, 0);
1750 if (!INDIRECT_REF_P (indirect_ref))
1751 return false;
1752
1753 tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
1754 if (!is_object_parameter (ptr))
1755 return false;
1756
1757 /* Must access the correct field. */
1758 if (TREE_OPERAND (component_ref, 1) != field_decl)
1759 return false;
1760 return true;
1761 }
1762
1763 /* Subroutine of field_accessor_p.
1764
1765 Assuming that INIT_EXPR has already had its code and type checked,
1766 determine if it is a simple accessor for FIELD_DECL
1767 (of type FIELD_TYPE).
1768
1769 Specifically, a simple accessor within struct S of the form:
1770 T get_field () { return m_field; }
1771 should have a constexpr_fn_retval (saved_tree) of the form:
1772 <init_expr:T
1773 <result_decl:T
1774 <nop_expr:T
1775 <component_ref:
1776 <indirect_ref:S>
1777 <nop_expr:P*
1778 <parm_decl (this)>
1779 <field_decl (FIELD_DECL)>>>>>. */
1780
1781 static bool
1782 direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
1783 {
1784 tree result_decl = TREE_OPERAND (init_expr, 0);
1785 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
1786 return false;
1787
1788 tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1789 if (!field_access_p (component_ref, field_decl, field_type))
1790 return false;
1791
1792 return true;
1793 }
1794
1795 /* Subroutine of field_accessor_p.
1796
1797 Assuming that INIT_EXPR has already had its code and type checked,
1798 determine if it is a "reference" accessor for FIELD_DECL
1799 (of type FIELD_REFERENCE_TYPE).
1800
1801 Specifically, a simple accessor within struct S of the form:
1802 T& get_field () { return m_field; }
1803 should have a constexpr_fn_retval (saved_tree) of the form:
1804 <init_expr:T&
1805 <result_decl:T&
1806 <nop_expr: T&
1807 <addr_expr: T*
1808 <component_ref:T
1809 <indirect_ref:S
1810 <nop_expr
1811 <parm_decl (this)>>
1812 <field (FIELD_DECL)>>>>>>. */
1813 static bool
1814 reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
1815 tree field_reference_type)
1816 {
1817 tree result_decl = TREE_OPERAND (init_expr, 0);
1818 if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
1819 return false;
1820
1821 tree field_pointer_type = build_pointer_type (field_type);
1822 tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
1823 if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
1824 return false;
1825
1826 tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
1827
1828 if (!field_access_p (component_ref, field_decl, field_type))
1829 return false;
1830
1831 return true;
1832 }
1833
1834 /* Return the class of the `this' or explicit object parameter of FN. */
1835
1836 static tree
1837 class_of_object_parm (const_tree fn)
1838 {
1839 tree fntype = TREE_TYPE (fn);
1840 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
1841 return non_reference (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
1842 return class_of_this_parm (fntype);
1843 }
1844
1845 /* Return true if FN is an accessor method for FIELD_DECL.
1846 i.e. a method of the form { return FIELD; }, with no
1847 conversions.
1848
1849 If CONST_P, then additionally require that FN be a const
1850 method. */
1851
1852 static bool
1853 field_accessor_p (tree fn, tree field_decl, bool const_p)
1854 {
1855 if (TREE_CODE (fn) != FUNCTION_DECL)
1856 return false;
1857
1858 /* We don't yet support looking up static data, just fields. */
1859 if (TREE_CODE (field_decl) != FIELD_DECL)
1860 return false;
1861
1862 if (!DECL_OBJECT_MEMBER_FUNCTION_P (fn))
1863 return false;
1864
1865 /* If the field is accessed via a const "this" argument, verify
1866 that the "this" parameter is const. */
1867 if (const_p)
1868 {
1869 tree this_class = class_of_object_parm (fn);
1870 if (!TYPE_READONLY (this_class))
1871 return false;
1872 }
1873
1874 tree saved_tree = DECL_SAVED_TREE (fn);
1875
1876 if (saved_tree == NULL_TREE)
1877 return false;
1878
1879 /* Attempt to extract a single return value from the function,
1880 if it has one. */
1881 tree retval = constexpr_fn_retval (saved_tree);
1882 if (retval == NULL_TREE || retval == error_mark_node)
1883 return false;
1884 /* Require an INIT_EXPR. */
1885 if (TREE_CODE (retval) != INIT_EXPR)
1886 return false;
1887 tree init_expr = retval;
1888
1889 /* Determine if this is a simple accessor within struct S of the form:
1890 T get_field () { return m_field; }. */
1891 tree field_type = TREE_TYPE (field_decl);
1892 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
1893 return direct_accessor_p (init_expr, field_decl, field_type);
1894
1895 /* Failing that, determine if it is an accessor of the form:
1896 T& get_field () { return m_field; }. */
1897 tree field_reference_type = cp_build_reference_type (field_type, false);
1898 if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
1899 return reference_accessor_p (init_expr, field_decl, field_type,
1900 field_reference_type);
1901
1902 return false;
1903 }
1904
1905 /* Callback data for dfs_locate_field_accessor_pre. */
1906
1907 class locate_field_data
1908 {
1909 public:
1910 locate_field_data (tree field_decl_, bool const_p_)
1911 : field_decl (field_decl_), const_p (const_p_) {}
1912
1913 tree field_decl;
1914 bool const_p;
1915 };
1916
1917 /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
1918 callable via binfo, if one exists, otherwise return NULL_TREE.
1919
1920 Callback for dfs_walk_once_accessible for use within
1921 locate_field_accessor. */
1922
1923 static tree
1924 dfs_locate_field_accessor_pre (tree binfo, void *data)
1925 {
1926 locate_field_data *lfd = (locate_field_data *)data;
1927 tree type = BINFO_TYPE (binfo);
1928
1929 vec<tree, va_gc> *member_vec;
1930 tree fn;
1931 size_t i;
1932
1933 if (!CLASS_TYPE_P (type))
1934 return NULL_TREE;
1935
1936 member_vec = CLASSTYPE_MEMBER_VEC (type);
1937 if (!member_vec)
1938 return NULL_TREE;
1939
1940 for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
1941 if (fn)
1942 if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
1943 return fn;
1944
1945 return NULL_TREE;
1946 }
1947
1948 /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
1949 callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE. */
1950
1951 tree
1952 locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
1953 {
1954 if (TREE_CODE (basetype_path) != TREE_BINFO)
1955 return NULL_TREE;
1956
1957 /* Walk the hierarchy, looking for a method of some base class that allows
1958 access to the field. */
1959 locate_field_data lfd (field_decl, const_p);
1960 return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
1961 dfs_locate_field_accessor_pre,
1962 NULL, &lfd);
1963 }
1964
1965 /* Check throw specifier of OVERRIDER is at least as strict as
1966 the one of BASEFN. This is due to [except.spec]: "If a virtual function
1967 has a non-throwing exception specification, all declarations, including
1968 the definition, of any function that overrides that virtual function in
1969 any derived class shall have a non-throwing exception specification,
1970 unless the overriding function is defined as deleted." */
1971
1972 bool
1973 maybe_check_overriding_exception_spec (tree overrider, tree basefn)
1974 {
1975 maybe_instantiate_noexcept (basefn);
1976 maybe_instantiate_noexcept (overrider);
1977 tree base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
1978 tree over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
1979
1980 if (DECL_INVALID_OVERRIDER_P (overrider)
1981 /* CWG 1351 added the "unless the overriding function is defined as
1982 deleted" wording. */
1983 || DECL_DELETED_FN (overrider))
1984 return true;
1985
1986 /* Can't check this yet. Pretend this is fine and let
1987 noexcept_override_late_checks check this later. */
1988 if (UNPARSED_NOEXCEPT_SPEC_P (base_throw)
1989 || UNPARSED_NOEXCEPT_SPEC_P (over_throw))
1990 return true;
1991
1992 /* We also have to defer checking when we're in a template and couldn't
1993 instantiate & evaluate the noexcept to true/false. */
1994 if (processing_template_decl)
1995 if ((base_throw
1996 && base_throw != noexcept_true_spec
1997 && base_throw != noexcept_false_spec)
1998 || (over_throw
1999 && over_throw != noexcept_true_spec
2000 && over_throw != noexcept_false_spec))
2001 return true;
2002
2003 if (!comp_except_specs (base_throw, over_throw, ce_derived))
2004 {
2005 auto_diagnostic_group d;
2006 error ("looser exception specification on overriding virtual function "
2007 "%q+#F", overrider);
2008 inform (DECL_SOURCE_LOCATION (basefn),
2009 "overridden function is %q#F", basefn);
2010 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2011 return false;
2012 }
2013 return true;
2014 }
2015
2016 /* Check that virtual overrider OVERRIDER is acceptable for base function
2017 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
2018
2019 static int
2020 check_final_overrider (tree overrider, tree basefn)
2021 {
2022 tree over_type = TREE_TYPE (overrider);
2023 tree base_type = TREE_TYPE (basefn);
2024 tree over_return = fndecl_declared_return_type (overrider);
2025 tree base_return = fndecl_declared_return_type (basefn);
2026
2027 int fail = 0;
2028
2029 if (DECL_INVALID_OVERRIDER_P (overrider))
2030 return 0;
2031
2032 if (same_type_p (base_return, over_return))
2033 /* OK */;
2034 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
2035 || (TREE_CODE (base_return) == TREE_CODE (over_return)
2036 && INDIRECT_TYPE_P (base_return)))
2037 {
2038 /* Potentially covariant. */
2039 unsigned base_quals, over_quals;
2040
2041 fail = !INDIRECT_TYPE_P (base_return);
2042 if (!fail)
2043 {
2044 if (cp_type_quals (base_return) != cp_type_quals (over_return))
2045 fail = 1;
2046
2047 if (TYPE_REF_P (base_return)
2048 && (TYPE_REF_IS_RVALUE (base_return)
2049 != TYPE_REF_IS_RVALUE (over_return)))
2050 fail = 1;
2051
2052 base_return = TREE_TYPE (base_return);
2053 over_return = TREE_TYPE (over_return);
2054 }
2055 base_quals = cp_type_quals (base_return);
2056 over_quals = cp_type_quals (over_return);
2057
2058 if ((base_quals & over_quals) != over_quals)
2059 fail = 1;
2060
2061 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
2062 {
2063 /* Strictly speaking, the standard requires the return type to be
2064 complete even if it only differs in cv-quals, but that seems
2065 like a bug in the wording. */
2066 if (!same_type_ignoring_top_level_qualifiers_p (base_return,
2067 over_return))
2068 {
2069 tree binfo = lookup_base (over_return, base_return,
2070 ba_check, NULL, tf_none);
2071
2072 if (!binfo || binfo == error_mark_node)
2073 fail = 1;
2074 }
2075 }
2076 else if (can_convert_standard (TREE_TYPE (base_type),
2077 TREE_TYPE (over_type),
2078 tf_warning_or_error))
2079 /* GNU extension, allow trivial pointer conversions such as
2080 converting to void *, or qualification conversion. */
2081 {
2082 auto_diagnostic_group d;
2083 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
2084 "invalid covariant return type for %q#D", overrider))
2085 inform (DECL_SOURCE_LOCATION (basefn),
2086 "overridden function is %q#D", basefn);
2087 }
2088 else
2089 fail = 2;
2090 }
2091 else
2092 fail = 2;
2093 if (!fail)
2094 /* OK */;
2095 else
2096 {
2097 auto_diagnostic_group d;
2098 if (fail == 1)
2099 error ("invalid covariant return type for %q+#D", overrider);
2100 else
2101 error ("conflicting return type specified for %q+#D", overrider);
2102 inform (DECL_SOURCE_LOCATION (basefn),
2103 "overridden function is %q#D", basefn);
2104 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2105 return 0;
2106 }
2107
2108 if (!maybe_check_overriding_exception_spec (overrider, basefn))
2109 return 0;
2110
2111 /* Check for conflicting type attributes. But leave transaction_safe for
2112 set_one_vmethod_tm_attributes. */
2113 if (!comp_type_attributes (over_type, base_type)
2114 && !tx_safe_fn_type_p (base_type)
2115 && !tx_safe_fn_type_p (over_type))
2116 {
2117 auto_diagnostic_group d;
2118 error ("conflicting type attributes specified for %q+#D", overrider);
2119 inform (DECL_SOURCE_LOCATION (basefn),
2120 "overridden function is %q#D", basefn);
2121 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2122 return 0;
2123 }
2124
2125 /* A consteval virtual function shall not override a virtual function that is
2126 not consteval. A consteval virtual function shall not be overridden by a
2127 virtual function that is not consteval. */
2128 if (DECL_IMMEDIATE_FUNCTION_P (overrider)
2129 != DECL_IMMEDIATE_FUNCTION_P (basefn))
2130 {
2131 auto_diagnostic_group d;
2132 if (DECL_IMMEDIATE_FUNCTION_P (overrider))
2133 error ("%<consteval%> function %q+D overriding non-%<consteval%> "
2134 "function", overrider);
2135 else
2136 error ("non-%<consteval%> function %q+D overriding %<consteval%> "
2137 "function", overrider);
2138 inform (DECL_SOURCE_LOCATION (basefn),
2139 "overridden function is %qD", basefn);
2140 DECL_INVALID_OVERRIDER_P (overrider) = 1;
2141 return 0;
2142 }
2143
2144 /* A function declared transaction_safe_dynamic that overrides a function
2145 declared transaction_safe (but not transaction_safe_dynamic) is
2146 ill-formed. */
2147 if (tx_safe_fn_type_p (base_type)
2148 && lookup_attribute ("transaction_safe_dynamic",
2149 DECL_ATTRIBUTES (overrider))
2150 && !lookup_attribute ("transaction_safe_dynamic",
2151 DECL_ATTRIBUTES (basefn)))
2152 {
2153 auto_diagnostic_group d;
2154 error_at (DECL_SOURCE_LOCATION (overrider),
2155 "%qD declared %<transaction_safe_dynamic%>", overrider);
2156 inform (DECL_SOURCE_LOCATION (basefn),
2157 "overriding %qD declared %<transaction_safe%>", basefn);
2158 }
2159
2160 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
2161 {
2162 if (DECL_DELETED_FN (overrider))
2163 {
2164 auto_diagnostic_group d;
2165 error ("deleted function %q+D overriding non-deleted function",
2166 overrider);
2167 inform (DECL_SOURCE_LOCATION (basefn),
2168 "overridden function is %qD", basefn);
2169 maybe_explain_implicit_delete (overrider);
2170 }
2171 else
2172 {
2173 auto_diagnostic_group d;
2174 error ("non-deleted function %q+D overriding deleted function",
2175 overrider);
2176 inform (DECL_SOURCE_LOCATION (basefn),
2177 "overridden function is %qD", basefn);
2178 }
2179 return 0;
2180 }
2181
2182 if (!DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
2183 {
2184 auto_diagnostic_group d;
2185 error ("function with contracts %q+D overriding contractless function",
2186 overrider);
2187 inform (DECL_SOURCE_LOCATION (basefn),
2188 "overridden function is %qD", basefn);
2189 return 0;
2190 }
2191 else if (DECL_HAS_CONTRACTS_P (basefn) && !DECL_HAS_CONTRACTS_P (overrider))
2192 {
2193 /* We're inheriting basefn's contracts; create a copy of them but
2194 replace references to their parms to our parms. */
2195 inherit_base_contracts (overrider, basefn);
2196 }
2197 else if (DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
2198 {
2199 /* We're in the process of completing the overrider's class, which means
2200 our conditions definitely are not parsed so simply chain on the
2201 basefn for later checking.
2202
2203 Note that OVERRIDER's contracts will have been fully parsed at the
2204 point the deferred match is run. */
2205 defer_guarded_contract_match (overrider, basefn, DECL_CONTRACTS (basefn));
2206 }
2207
2208 if (DECL_FINAL_P (basefn))
2209 {
2210 auto_diagnostic_group d;
2211 error ("virtual function %q+D overriding final function", overrider);
2212 inform (DECL_SOURCE_LOCATION (basefn),
2213 "overridden function is %qD", basefn);
2214 return 0;
2215 }
2216 return 1;
2217 }
2218
2219 /* Given a class TYPE, and a function decl FNDECL, look for
2220 virtual functions in TYPE's hierarchy which FNDECL overrides.
2221 We do not look in TYPE itself, only its bases.
2222
2223 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
2224 find that it overrides anything.
2225
2226 We check that every function which is overridden, is correctly
2227 overridden. */
2228
2229 int
2230 look_for_overrides (tree type, tree fndecl)
2231 {
2232 tree binfo = TYPE_BINFO (type);
2233 tree base_binfo;
2234 int ix;
2235 int found = 0;
2236
2237 /* A constructor for a class T does not override a function T
2238 in a base class. */
2239 if (DECL_CONSTRUCTOR_P (fndecl))
2240 return 0;
2241
2242 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2243 {
2244 tree basetype = BINFO_TYPE (base_binfo);
2245
2246 if (TYPE_POLYMORPHIC_P (basetype))
2247 found += look_for_overrides_r (basetype, fndecl);
2248 }
2249 return found;
2250 }
2251
2252 /* Look in TYPE for virtual functions with the same signature as
2253 FNDECL. */
2254
2255 tree
2256 look_for_overrides_here (tree type, tree fndecl)
2257 {
2258 tree ovl = get_class_binding (type, DECL_NAME (fndecl));
2259
2260 for (ovl_iterator iter (ovl); iter; ++iter)
2261 {
2262 tree fn = *iter;
2263
2264 if (!DECL_VIRTUAL_P (fn))
2265 /* Not a virtual. */;
2266 else if (DECL_CONTEXT (fn) != type)
2267 /* Introduced with a using declaration. */;
2268 else if (DECL_STATIC_FUNCTION_P (fndecl)
2269 || DECL_XOBJ_MEMBER_FUNCTION_P (fndecl))
2270 {
2271 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
2272 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2273 dtypes = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl) ? TREE_CHAIN (dtypes)
2274 : dtypes;
2275 if (compparms (TREE_CHAIN (btypes), dtypes))
2276 return fn;
2277 }
2278 else if (same_signature_p (fndecl, fn))
2279 return fn;
2280 }
2281
2282 return NULL_TREE;
2283 }
2284
2285 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
2286 TYPE itself and its bases. */
2287
2288 static int
2289 look_for_overrides_r (tree type, tree fndecl)
2290 {
2291 tree fn = look_for_overrides_here (type, fndecl);
2292 if (fn)
2293 {
2294 if (DECL_STATIC_FUNCTION_P (fndecl))
2295 {
2296 /* A static member function cannot match an inherited
2297 virtual member function. */
2298 auto_diagnostic_group d;
2299 error ("%q+#D cannot be declared", fndecl);
2300 error (" since %q+#D declared in base class", fn);
2301 }
2302 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fndecl))
2303 {
2304 auto_diagnostic_group d;
2305 error_at (DECL_SOURCE_LOCATION (fndecl),
2306 "explicit object member function "
2307 "overrides virtual function");
2308 inform (DECL_SOURCE_LOCATION (fn),
2309 "virtual function declared here");
2310 }
2311 else
2312 {
2313 /* It's definitely virtual, even if not explicitly set. */
2314 DECL_VIRTUAL_P (fndecl) = 1;
2315 check_final_overrider (fndecl, fn);
2316 }
2317 return 1;
2318 }
2319
2320 /* We failed to find one declared in this class. Look in its bases. */
2321 return look_for_overrides (type, fndecl);
2322 }
2323
2324 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2325
2326 static tree
2327 dfs_get_pure_virtuals (tree binfo, void *data)
2328 {
2329 tree type = (tree) data;
2330
2331 /* We're not interested in primary base classes; the derived class
2332 of which they are a primary base will contain the information we
2333 need. */
2334 if (!BINFO_PRIMARY_P (binfo))
2335 {
2336 tree virtuals;
2337
2338 for (virtuals = BINFO_VIRTUALS (binfo);
2339 virtuals;
2340 virtuals = TREE_CHAIN (virtuals))
2341 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2342 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
2343 }
2344
2345 return NULL_TREE;
2346 }
2347
2348 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2349
2350 void
2351 get_pure_virtuals (tree type)
2352 {
2353 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2354 is going to be overridden. */
2355 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
2356 /* Now, run through all the bases which are not primary bases, and
2357 collect the pure virtual functions. We look at the vtable in
2358 each class to determine what pure virtual functions are present.
2359 (A primary base is not interesting because the derived class of
2360 which it is a primary base will contain vtable entries for the
2361 pure virtuals in the base class. */
2362 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
2363 }
2364
2365 /* Debug info for C++ classes can get very large; try to avoid
2366 emitting it everywhere.
2367
2368 Note that this optimization wins even when the target supports
2369 BINCL (if only slightly), and reduces the amount of work for the
2370 linker. */
2371
2372 void
2373 maybe_suppress_debug_info (tree t)
2374 {
2375 if (write_symbols == NO_DEBUG)
2376 return;
2377
2378 /* We might have set this earlier in cp_finish_decl. */
2379 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2380
2381 /* Always emit the information for each class every time. */
2382 if (flag_emit_class_debug_always)
2383 return;
2384
2385 /* If we already know how we're handling this class, handle debug info
2386 the same way. */
2387 if (CLASSTYPE_INTERFACE_KNOWN (t))
2388 {
2389 if (CLASSTYPE_INTERFACE_ONLY (t))
2390 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2391 /* else don't set it. */
2392 }
2393 /* If the class has a vtable, write out the debug info along with
2394 the vtable. */
2395 else if (TYPE_CONTAINS_VPTR_P (t))
2396 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2397
2398 /* Otherwise, just emit the debug info normally. */
2399 }
2400
2401 /* Note that we want debugging information for a base class of a class
2402 whose vtable is being emitted. Normally, this would happen because
2403 calling the constructor for a derived class implies calling the
2404 constructors for all bases, which involve initializing the
2405 appropriate vptr with the vtable for the base class; but in the
2406 presence of optimization, this initialization may be optimized
2407 away, so we tell finish_vtable_vardecl that we want the debugging
2408 information anyway. */
2409
2410 static tree
2411 dfs_debug_mark (tree binfo, void * /*data*/)
2412 {
2413 tree t = BINFO_TYPE (binfo);
2414
2415 if (CLASSTYPE_DEBUG_REQUESTED (t))
2416 return dfs_skip_bases;
2417
2418 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2419
2420 return NULL_TREE;
2421 }
2422
2423 /* Write out the debugging information for TYPE, whose vtable is being
2424 emitted. Also walk through our bases and note that we want to
2425 write out information for them. This avoids the problem of not
2426 writing any debug info for intermediate basetypes whose
2427 constructors, and thus the references to their vtables, and thus
2428 the vtables themselves, were optimized away. */
2429
2430 void
2431 note_debug_info_needed (tree type)
2432 {
2433 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2434 {
2435 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2436 rest_of_type_compilation (type, namespace_bindings_p ());
2437 }
2438
2439 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2440 }
2441
2442 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2443 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2444 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2445 bases have been encountered already in the tree walk. PARENT_CONVS
2446 is the list of lists of conversion functions that could hide CONV
2447 and OTHER_CONVS is the list of lists of conversion functions that
2448 could hide or be hidden by CONV, should virtualness be involved in
2449 the hierarchy. Merely checking the conversion op's name is not
2450 enough because two conversion operators to the same type can have
2451 different names. Return nonzero if we are visible. */
2452
2453 static int
2454 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2455 tree to_type, tree parent_convs, tree other_convs)
2456 {
2457 tree level, probe;
2458
2459 /* See if we are hidden by a parent conversion. */
2460 for (level = parent_convs; level; level = TREE_CHAIN (level))
2461 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2462 if (same_type_p (to_type, TREE_TYPE (probe)))
2463 return 0;
2464
2465 if (virtual_depth || virtualness)
2466 {
2467 /* In a virtual hierarchy, we could be hidden, or could hide a
2468 conversion function on the other_convs list. */
2469 for (level = other_convs; level; level = TREE_CHAIN (level))
2470 {
2471 int we_hide_them;
2472 int they_hide_us;
2473 tree *prev, other;
2474
2475 if (!(virtual_depth || TREE_STATIC (level)))
2476 /* Neither is morally virtual, so cannot hide each other. */
2477 continue;
2478
2479 if (!TREE_VALUE (level))
2480 /* They evaporated away already. */
2481 continue;
2482
2483 they_hide_us = (virtual_depth
2484 && original_binfo (binfo, TREE_PURPOSE (level)));
2485 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2486 && original_binfo (TREE_PURPOSE (level), binfo));
2487
2488 if (!(we_hide_them || they_hide_us))
2489 /* Neither is within the other, so no hiding can occur. */
2490 continue;
2491
2492 for (prev = &TREE_VALUE (level), other = *prev; other;)
2493 {
2494 if (same_type_p (to_type, TREE_TYPE (other)))
2495 {
2496 if (they_hide_us)
2497 /* We are hidden. */
2498 return 0;
2499
2500 if (we_hide_them)
2501 {
2502 /* We hide the other one. */
2503 other = TREE_CHAIN (other);
2504 *prev = other;
2505 continue;
2506 }
2507 }
2508 prev = &TREE_CHAIN (other);
2509 other = *prev;
2510 }
2511 }
2512 }
2513 return 1;
2514 }
2515
2516 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2517 of conversion functions, the first slot will be for the current
2518 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2519 of conversion functions from children of the current binfo,
2520 concatenated with conversions from elsewhere in the hierarchy --
2521 that list begins with OTHER_CONVS. Return a single list of lists
2522 containing only conversions from the current binfo and its
2523 children. */
2524
2525 static tree
2526 split_conversions (tree my_convs, tree parent_convs,
2527 tree child_convs, tree other_convs)
2528 {
2529 tree t;
2530 tree prev;
2531
2532 /* Remove the original other_convs portion from child_convs. */
2533 for (prev = NULL, t = child_convs;
2534 t != other_convs; prev = t, t = TREE_CHAIN (t))
2535 continue;
2536
2537 if (prev)
2538 TREE_CHAIN (prev) = NULL_TREE;
2539 else
2540 child_convs = NULL_TREE;
2541
2542 /* Attach the child convs to any we had at this level. */
2543 if (my_convs)
2544 {
2545 my_convs = parent_convs;
2546 TREE_CHAIN (my_convs) = child_convs;
2547 }
2548 else
2549 my_convs = child_convs;
2550
2551 return my_convs;
2552 }
2553
2554 /* Worker for lookup_conversions. Lookup conversion functions in
2555 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in a
2556 morally virtual base, and VIRTUALNESS is nonzero, if we've
2557 encountered virtual bases already in the tree walk. PARENT_CONVS
2558 is a list of conversions within parent binfos. OTHER_CONVS are
2559 conversions found elsewhere in the tree. Return the conversions
2560 found within this portion of the graph in CONVS. Return nonzero if
2561 we encountered virtualness. We keep template and non-template
2562 conversions separate, to avoid unnecessary type comparisons.
2563
2564 The located conversion functions are held in lists of lists. The
2565 TREE_VALUE of the outer list is the list of conversion functions
2566 found in a particular binfo. The TREE_PURPOSE of both the outer
2567 and inner lists is the binfo at which those conversions were
2568 found. TREE_STATIC is set for those lists within of morally
2569 virtual binfos. The TREE_VALUE of the inner list is the conversion
2570 function or overload itself. The TREE_TYPE of each inner list node
2571 is the converted-to type. */
2572
2573 static int
2574 lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
2575 tree parent_convs, tree other_convs, tree *convs)
2576 {
2577 int my_virtualness = 0;
2578 tree my_convs = NULL_TREE;
2579 tree child_convs = NULL_TREE;
2580
2581 /* If we have no conversion operators, then don't look. */
2582 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2583 {
2584 *convs = NULL_TREE;
2585
2586 return 0;
2587 }
2588
2589 if (BINFO_VIRTUAL_P (binfo))
2590 virtual_depth++;
2591
2592 /* First, locate the unhidden ones at this level. */
2593 if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
2594 for (ovl_iterator iter (conv); iter; ++iter)
2595 {
2596 tree fn = *iter;
2597 tree type = DECL_CONV_FN_TYPE (fn);
2598
2599 if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
2600 {
2601 mark_used (fn);
2602 type = DECL_CONV_FN_TYPE (fn);
2603 }
2604
2605 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2606 type, parent_convs, other_convs))
2607 {
2608 my_convs = tree_cons (binfo, fn, my_convs);
2609 TREE_TYPE (my_convs) = type;
2610 if (virtual_depth)
2611 {
2612 TREE_STATIC (my_convs) = 1;
2613 my_virtualness = 1;
2614 }
2615 }
2616 }
2617
2618 if (my_convs)
2619 {
2620 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2621 if (virtual_depth)
2622 TREE_STATIC (parent_convs) = 1;
2623 }
2624
2625 child_convs = other_convs;
2626
2627 /* Now iterate over each base, looking for more conversions. */
2628 unsigned i;
2629 tree base_binfo;
2630 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2631 {
2632 tree base_convs;
2633 unsigned base_virtualness;
2634
2635 base_virtualness = lookup_conversions_r (base_binfo,
2636 virtual_depth, virtualness,
2637 parent_convs, child_convs,
2638 &base_convs);
2639 if (base_virtualness)
2640 my_virtualness = virtualness = 1;
2641 child_convs = chainon (base_convs, child_convs);
2642 }
2643
2644 *convs = split_conversions (my_convs, parent_convs,
2645 child_convs, other_convs);
2646
2647 return my_virtualness;
2648 }
2649
2650 /* Return a TREE_LIST containing all the non-hidden user-defined
2651 conversion functions for TYPE (and its base-classes). The
2652 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2653 function. The TREE_PURPOSE is the BINFO from which the conversion
2654 functions in this node were selected. This function is effectively
2655 performing a set of member lookups as lookup_fnfield does, but
2656 using the type being converted to as the unique key, rather than the
2657 field name. */
2658
2659 tree
2660 lookup_conversions (tree type)
2661 {
2662 tree convs;
2663
2664 complete_type (type);
2665 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
2666 return NULL_TREE;
2667
2668 lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
2669
2670 tree list = NULL_TREE;
2671
2672 /* Flatten the list-of-lists */
2673 for (; convs; convs = TREE_CHAIN (convs))
2674 {
2675 tree probe, next;
2676
2677 for (probe = TREE_VALUE (convs); probe; probe = next)
2678 {
2679 next = TREE_CHAIN (probe);
2680
2681 TREE_CHAIN (probe) = list;
2682 list = probe;
2683 }
2684 }
2685
2686 return list;
2687 }
2688
2689 /* Returns the binfo of the first direct or indirect virtual base derived
2690 from BINFO, or NULL if binfo is not via virtual. */
2691
2692 tree
2693 binfo_from_vbase (tree binfo)
2694 {
2695 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2696 {
2697 if (BINFO_VIRTUAL_P (binfo))
2698 return binfo;
2699 }
2700 return NULL_TREE;
2701 }
2702
2703 /* Returns the binfo of the first direct or indirect virtual base derived
2704 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2705 via virtual. */
2706
2707 tree
2708 binfo_via_virtual (tree binfo, tree limit)
2709 {
2710 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2711 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2712 return NULL_TREE;
2713
2714 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2715 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2716 {
2717 if (BINFO_VIRTUAL_P (binfo))
2718 return binfo;
2719 }
2720 return NULL_TREE;
2721 }
2722
2723 /* BINFO is for a base class in some hierarchy. Return true iff it is a
2724 direct base. */
2725
2726 bool
2727 binfo_direct_p (tree binfo)
2728 {
2729 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
2730 if (BINFO_INHERITANCE_CHAIN (d_binfo))
2731 /* A second inheritance chain means indirect. */
2732 return false;
2733 if (!BINFO_VIRTUAL_P (binfo))
2734 /* Non-virtual, so only one inheritance chain means direct. */
2735 return true;
2736 /* A virtual base looks like a direct base, so we need to look through the
2737 direct bases to see if it's there. */
2738 tree b_binfo;
2739 for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
2740 if (b_binfo == binfo)
2741 return true;
2742 return false;
2743 }
2744
2745 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2746 Find the equivalent binfo within whatever graph HERE is located.
2747 This is the inverse of original_binfo. */
2748
2749 tree
2750 copied_binfo (tree binfo, tree here)
2751 {
2752 tree result = NULL_TREE;
2753
2754 if (BINFO_VIRTUAL_P (binfo))
2755 {
2756 tree t;
2757
2758 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2759 t = BINFO_INHERITANCE_CHAIN (t))
2760 continue;
2761
2762 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2763 }
2764 else if (BINFO_INHERITANCE_CHAIN (binfo))
2765 {
2766 tree cbinfo;
2767 tree base_binfo;
2768 int ix;
2769
2770 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2771 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2772 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2773 {
2774 result = base_binfo;
2775 break;
2776 }
2777 }
2778 else
2779 {
2780 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2781 result = here;
2782 }
2783
2784 gcc_assert (result);
2785 return result;
2786 }
2787
2788 tree
2789 binfo_for_vbase (tree base, tree t)
2790 {
2791 unsigned ix;
2792 tree binfo;
2793 vec<tree, va_gc> *vbases;
2794
2795 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2796 vec_safe_iterate (vbases, ix, &binfo); ix++)
2797 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2798 return binfo;
2799 return NULL;
2800 }
2801
2802 /* BINFO is some base binfo of HERE, within some other
2803 hierarchy. Return the equivalent binfo, but in the hierarchy
2804 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2805 is not a base binfo of HERE, returns NULL_TREE. */
2806
2807 tree
2808 original_binfo (tree binfo, tree here)
2809 {
2810 tree result = NULL;
2811
2812 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2813 result = here;
2814 else if (BINFO_VIRTUAL_P (binfo))
2815 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2816 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2817 : NULL_TREE);
2818 else if (BINFO_INHERITANCE_CHAIN (binfo))
2819 {
2820 tree base_binfos;
2821
2822 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2823 if (base_binfos)
2824 {
2825 int ix;
2826 tree base_binfo;
2827
2828 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2829 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2830 BINFO_TYPE (binfo)))
2831 {
2832 result = base_binfo;
2833 break;
2834 }
2835 }
2836 }
2837
2838 return result;
2839 }
2840
2841 /* True iff TYPE has any dependent bases (and therefore we can't say
2842 definitively that another class is not a base of an instantiation of
2843 TYPE). */
2844
2845 bool
2846 any_dependent_bases_p (tree type /* = current_nonlambda_class_type () */)
2847 {
2848 if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
2849 return false;
2850
2851 /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
2852 Return false because in this situation we aren't actually looking up names
2853 in the scope of the class, so it doesn't matter whether it has dependent
2854 bases. */
2855 if (!TYPE_BINFO (type))
2856 return false;
2857
2858 unsigned i;
2859 tree base_binfo;
2860 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
2861 if (BINFO_DEPENDENT_BASE_P (base_binfo)
2862 /* Recurse to also consider possibly dependent bases of a base that
2863 is part of the current instantiation. */
2864 || any_dependent_bases_p (BINFO_TYPE (base_binfo)))
2865 return true;
2866
2867 return false;
2868 }