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