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