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