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