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