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