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