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