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