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