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