]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/search.c
* pa-linux.h (GLOBAL_ASM_OP): Fix typo.
[gcc.git] / gcc / cp / search.c
CommitLineData
8d08fdba
MS
1/* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
fed3cef0 3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
426b490f 4 1999, 2000, 2002 Free Software Foundation, Inc.
8d08fdba
MS
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
14GNU CC is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
21the Free Software Foundation, 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA. */
8d08fdba 23
e92cc029 24/* High-level class interface. */
8d08fdba
MS
25
26#include "config.h"
8d052bc7 27#include "system.h"
e7a587ef 28#include "tree.h"
8d08fdba
MS
29#include "cp-tree.h"
30#include "obstack.h"
31#include "flags.h"
43f2999d 32#include "rtl.h"
e8abc66f 33#include "output.h"
e2500fed 34#include "ggc.h"
54f92bfb 35#include "toplev.h"
8d08fdba
MS
36#include "stack.h"
37
38/* Obstack used for remembering decision points of breadth-first. */
e92cc029 39
8d08fdba
MS
40static struct obstack search_obstack;
41
42/* Methods for pushing and popping objects to and from obstacks. */
e92cc029 43
8d08fdba
MS
44struct stack_level *
45push_stack_level (obstack, tp, size)
46 struct obstack *obstack;
47 char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
48 int size;
49{
50 struct stack_level *stack;
51 obstack_grow (obstack, tp, size);
52 stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
53 obstack_finish (obstack);
54 stack->obstack = obstack;
55 stack->first = (tree *) obstack_base (obstack);
56 stack->limit = obstack_room (obstack) / sizeof (tree *);
57 return stack;
58}
59
60struct stack_level *
61pop_stack_level (stack)
62 struct stack_level *stack;
63{
64 struct stack_level *tem = stack;
65 struct obstack *obstack = tem->obstack;
66 stack = tem->prev;
67 obstack_free (obstack, tem);
68 return stack;
69}
70
71#define search_level stack_level
72static struct search_level *search_stack;
73
3c9d6359
MM
74struct vbase_info
75{
76 /* The class dominating the hierarchy. */
77 tree type;
cf2e003b 78 /* A pointer to a complete object of the indicated TYPE. */
3c9d6359
MM
79 tree decl_ptr;
80 tree inits;
3c9d6359
MM
81};
82
158991b7 83static tree lookup_field_1 PARAMS ((tree, tree));
0e997e76 84static int is_subobject_of_p PARAMS ((tree, tree, tree));
158991b7
KG
85static tree dfs_check_overlap PARAMS ((tree, void *));
86static tree dfs_no_overlap_yet PARAMS ((tree, void *));
338d90b8 87static base_kind lookup_base_r
2db1ab2d 88 PARAMS ((tree, tree, base_access, int, int, int, tree *));
158991b7 89static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
158991b7
KG
90static tree marked_pushdecls_p PARAMS ((tree, void *));
91static tree unmarked_pushdecls_p PARAMS ((tree, void *));
158991b7
KG
92static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
93static tree dfs_debug_mark PARAMS ((tree, void *));
158991b7
KG
94static tree dfs_get_vbase_types PARAMS ((tree, void *));
95static tree dfs_push_type_decls PARAMS ((tree, void *));
96static tree dfs_push_decls PARAMS ((tree, void *));
97static tree dfs_unuse_fields PARAMS ((tree, void *));
98static tree add_conversions PARAMS ((tree, void *));
158991b7 99static int covariant_return_p PARAMS ((tree, tree));
cbb40945 100static int look_for_overrides_r PARAMS ((tree, tree));
49c249e1 101static struct search_level *push_search_level
158991b7 102 PARAMS ((struct stack_level *, struct obstack *));
49c249e1 103static struct search_level *pop_search_level
158991b7 104 PARAMS ((struct stack_level *));
d6479fe7 105static tree bfs_walk
158991b7 106 PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
d6479fe7 107 void *));
158991b7 108static tree lookup_field_queue_p PARAMS ((tree, void *));
bd0d5d4a 109static int shared_member_p PARAMS ((tree));
158991b7 110static tree lookup_field_r PARAMS ((tree, void *));
158991b7
KG
111static tree canonical_binfo PARAMS ((tree));
112static tree shared_marked_p PARAMS ((tree, void *));
113static tree shared_unmarked_p PARAMS ((tree, void *));
114static int dependent_base_p PARAMS ((tree));
115static tree dfs_accessible_queue_p PARAMS ((tree, void *));
116static tree dfs_accessible_p PARAMS ((tree, void *));
117static tree dfs_access_in_type PARAMS ((tree, void *));
c35cce41 118static access_kind access_in_type PARAMS ((tree, tree));
158991b7
KG
119static tree dfs_canonical_queue PARAMS ((tree, void *));
120static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
121static void assert_canonical_unmarked PARAMS ((tree));
d7cca31e
JM
122static int protected_accessible_p PARAMS ((tree, tree, tree));
123static int friend_accessible_p PARAMS ((tree, tree, tree));
158991b7
KG
124static void setup_class_bindings PARAMS ((tree, int));
125static int template_self_reference_p PARAMS ((tree, tree));
158991b7 126static tree dfs_find_vbase_instance PARAMS ((tree, void *));
70a51bda 127static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
1cea0434 128static tree dfs_build_inheritance_graph_order PARAMS ((tree, void *));
8d08fdba
MS
129
130/* Allocate a level of searching. */
e92cc029 131
8d08fdba
MS
132static struct search_level *
133push_search_level (stack, obstack)
134 struct stack_level *stack;
135 struct obstack *obstack;
136{
137 struct search_level tem;
138
139 tem.prev = stack;
140 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
141}
142
143/* Discard a level of search allocation. */
e92cc029 144
8d08fdba
MS
145static struct search_level *
146pop_search_level (obstack)
147 struct stack_level *obstack;
148{
149 register struct search_level *stack = pop_stack_level (obstack);
150
151 return stack;
152}
153\f
8d08fdba 154/* Variables for gathering statistics. */
5566b478 155#ifdef GATHER_STATISTICS
8d08fdba
MS
156static int n_fields_searched;
157static int n_calls_lookup_field, n_calls_lookup_field_1;
158static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
159static int n_calls_get_base_type;
160static int n_outer_fields_searched;
161static int n_contexts_saved;
fc378698 162#endif /* GATHER_STATISTICS */
8d08fdba 163
8d08fdba 164\f
338d90b8
NS
165/* Worker for lookup_base. BINFO is the binfo we are searching at,
166 BASE is the RECORD_TYPE we are searching for. ACCESS is the
167 required access checks. WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
168 IS_VIRTUAL indicate how BINFO was reached from the start of the
169 search. WITHIN_CURRENT_SCOPE is true if we met the current scope,
170 or friend thereof (this allows us to determine whether a protected
171 base is accessible or not). IS_NON_PUBLIC indicates whether BINFO
172 is accessible and IS_VIRTUAL indicates if it is morally virtual.
173
174 If BINFO is of the required type, then *BINFO_PTR is examined to
175 compare with any other instance of BASE we might have already
176 discovered. *BINFO_PTR is initialized and a base_kind return value
177 indicates what kind of base was located.
178
179 Otherwise BINFO's bases are searched. */
180
181static base_kind
182lookup_base_r (binfo, base, access, within_current_scope,
183 is_non_public, is_virtual, binfo_ptr)
184 tree binfo, base;
185 base_access access;
186 int within_current_scope;
187 int is_non_public; /* inside a non-public part */
188 int is_virtual; /* inside a virtual part */
189 tree *binfo_ptr;
190{
191 int i;
192 tree bases;
193 base_kind found = bk_not_base;
194
195 if (access == ba_check
196 && !within_current_scope
197 && is_friend (BINFO_TYPE (binfo), current_scope ()))
198 {
07f521fc
JM
199 /* Do not clear is_non_public here. If A is a private base of B, A
200 is not allowed to convert a B* to an A*. */
338d90b8 201 within_current_scope = 1;
338d90b8
NS
202 }
203
204 if (same_type_p (BINFO_TYPE (binfo), base))
205 {
206 /* We have found a base. Check against what we have found
207 already. */
208 found = bk_same_type;
209 if (is_virtual)
210 found = bk_via_virtual;
211 if (is_non_public)
212 found = bk_inaccessible;
213
214 if (!*binfo_ptr)
215 *binfo_ptr = binfo;
216 else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
217 BINFO_OFFSET (*binfo_ptr)))
218 {
219 if (access != ba_any)
220 *binfo_ptr = NULL;
2db1ab2d 221 else if (!is_virtual)
338d90b8
NS
222 /* Prefer a non-virtual base. */
223 *binfo_ptr = binfo;
224 found = bk_ambig;
225 }
338d90b8
NS
226
227 return found;
228 }
229
230 bases = BINFO_BASETYPES (binfo);
231 if (!bases)
232 return bk_not_base;
233
234 for (i = TREE_VEC_LENGTH (bases); i--;)
235 {
236 tree base_binfo = TREE_VEC_ELT (bases, i);
237 int this_non_public = is_non_public;
238 int this_virtual = is_virtual;
4bdd26e6 239 base_kind bk;
338d90b8
NS
240
241 if (access <= ba_ignore)
242 ; /* no change */
243 else if (TREE_VIA_PUBLIC (base_binfo))
244 ; /* no change */
245 else if (access == ba_not_special)
246 this_non_public = 1;
247 else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
248 ; /* no change */
249 else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
250 ; /* no change */
251 else
252 this_non_public = 1;
253
254 if (TREE_VIA_VIRTUAL (base_binfo))
255 this_virtual = 1;
256
4bdd26e6
AH
257 bk = lookup_base_r (base_binfo, base,
258 access, within_current_scope,
259 this_non_public, this_virtual,
260 binfo_ptr);
338d90b8
NS
261
262 switch (bk)
263 {
264 case bk_ambig:
265 if (access != ba_any)
266 return bk;
267 found = bk;
268 break;
269
270 case bk_inaccessible:
271 if (found == bk_not_base)
272 found = bk;
273 my_friendly_assert (found == bk_via_virtual
274 || found == bk_inaccessible, 20010723);
275
276 break;
277
278 case bk_same_type:
279 bk = bk_proper_base;
280 /* FALLTHROUGH */
281 case bk_proper_base:
282 my_friendly_assert (found == bk_not_base, 20010723);
283 found = bk;
284 break;
285
286 case bk_via_virtual:
2db1ab2d
NS
287 if (found != bk_ambig)
288 found = bk;
338d90b8
NS
289 break;
290
291 case bk_not_base:
292 break;
293 }
294 }
295 return found;
296}
297
298/* Lookup BASE in the hierarchy dominated by T. Do access checking as
299 ACCESS specifies. Return the binfo we discover (which might not be
300 canonical). If KIND_PTR is non-NULL, fill with information about
2db1ab2d 301 what kind of base we discovered.
338d90b8 302
2db1ab2d
NS
303 If ba_quiet bit is set in ACCESS, then do not issue an error, and
304 return NULL_TREE for failure. */
338d90b8
NS
305
306tree
307lookup_base (t, base, access, kind_ptr)
308 tree t, base;
309 base_access access;
310 base_kind *kind_ptr;
311{
312 tree binfo = NULL; /* The binfo we've found so far. */
313 base_kind bk;
2db1ab2d 314
338d90b8
NS
315 if (t == error_mark_node || base == error_mark_node)
316 {
317 if (kind_ptr)
318 *kind_ptr = bk_not_base;
319 return error_mark_node;
320 }
2db1ab2d 321 my_friendly_assert (TYPE_P (t) && TYPE_P (base), 20011127);
338d90b8 322
2db1ab2d
NS
323 /* Ensure that the types are instantiated. */
324 t = complete_type (TYPE_MAIN_VARIANT (t));
325 base = complete_type (TYPE_MAIN_VARIANT (base));
338d90b8 326
2db1ab2d
NS
327 bk = lookup_base_r (TYPE_BINFO (t), base, access & ~ba_quiet,
328 0, 0, 0, &binfo);
338d90b8
NS
329
330 switch (bk)
331 {
332 case bk_inaccessible:
2db1ab2d
NS
333 binfo = NULL_TREE;
334 if (!(access & ba_quiet))
335 {
33bd39a2 336 error ("`%T' is an inaccessible base of `%T'", base, t);
2db1ab2d
NS
337 binfo = error_mark_node;
338 }
338d90b8
NS
339 break;
340 case bk_ambig:
341 if (access != ba_any)
342 {
2db1ab2d
NS
343 binfo = NULL_TREE;
344 if (!(access & ba_quiet))
345 {
33bd39a2 346 error ("`%T' is an ambiguous base of `%T'", base, t);
2db1ab2d
NS
347 binfo = error_mark_node;
348 }
338d90b8
NS
349 }
350 break;
338d90b8
NS
351 default:;
352 }
353
354 if (kind_ptr)
355 *kind_ptr = bk;
356
357 return binfo;
358}
359
4a9e5c67
NS
360/* Worker function for get_dynamic_cast_base_type. */
361
362static int
363dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
364 tree subtype;
365 tree binfo;
366 int via_virtual;
367 tree *offset_ptr;
368{
369 tree binfos;
370 int i, n_baselinks;
f08dda39 371 int worst = -2;
4a9e5c67
NS
372
373 if (BINFO_TYPE (binfo) == subtype)
374 {
375 if (via_virtual)
f08dda39 376 return -1;
4a9e5c67
NS
377 else
378 {
379 *offset_ptr = BINFO_OFFSET (binfo);
380 return 0;
381 }
382 }
383
384 binfos = BINFO_BASETYPES (binfo);
385 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
386 for (i = 0; i < n_baselinks; i++)
387 {
388 tree base_binfo = TREE_VEC_ELT (binfos, i);
389 int rval;
390
391 if (!TREE_VIA_PUBLIC (base_binfo))
392 continue;
393 rval = dynamic_cast_base_recurse
394 (subtype, base_binfo,
395 via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
f08dda39 396 if (worst == -2)
4a9e5c67
NS
397 worst = rval;
398 else if (rval >= 0)
f08dda39
NS
399 worst = worst >= 0 ? -3 : worst;
400 else if (rval == -1)
401 worst = -1;
402 else if (rval == -3 && worst != -1)
403 worst = -3;
4a9e5c67
NS
404 }
405 return worst;
406}
407
f08dda39
NS
408/* The dynamic cast runtime needs a hint about how the static SUBTYPE type
409 started from is related to the required TARGET type, in order to optimize
306ef644 410 the inheritance graph search. This information is independent of the
4a9e5c67
NS
411 current context, and ignores private paths, hence get_base_distance is
412 inappropriate. Return a TREE specifying the base offset, BOFF.
413 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
414 and there are no public virtual SUBTYPE bases.
f08dda39
NS
415 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
416 BOFF == -2, SUBTYPE is not a public base.
417 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
4a9e5c67
NS
418
419tree
420get_dynamic_cast_base_type (subtype, target)
421 tree subtype;
422 tree target;
423{
424 tree offset = NULL_TREE;
425 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
426 0, &offset);
427
428 if (!boff)
429 return offset;
0b4c1646
R
430 offset = build_int_2 (boff, -1);
431 TREE_TYPE (offset) = ssizetype;
432 return offset;
4a9e5c67
NS
433}
434
8d08fdba
MS
435/* Search for a member with name NAME in a multiple inheritance lattice
436 specified by TYPE. If it does not exist, return NULL_TREE.
437 If the member is ambiguously referenced, return `error_mark_node'.
438 Otherwise, return the FIELD_DECL. */
439
440/* Do a 1-level search for NAME as a member of TYPE. The caller must
441 figure out whether it can access this field. (Since it is only one
442 level, this is reasonable.) */
e92cc029 443
8d08fdba
MS
444static tree
445lookup_field_1 (type, name)
446 tree type, name;
447{
f84b4be9
JM
448 register tree field;
449
450 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
11e74ea6
KL
451 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
452 || TREE_CODE (type) == TYPENAME_TYPE)
453 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
454 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
f84b4be9
JM
455 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
456 the code often worked even when we treated the index as a list
11e74ea6
KL
457 of fields!)
458 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
f84b4be9
JM
459 return NULL_TREE;
460
f90cdf34
MT
461 if (TYPE_NAME (type)
462 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
463 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
464 {
465 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
466 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
467 int i;
468
469 while (lo < hi)
470 {
471 i = (lo + hi) / 2;
472
473#ifdef GATHER_STATISTICS
474 n_fields_searched++;
475#endif /* GATHER_STATISTICS */
476
477 if (DECL_NAME (fields[i]) > name)
478 hi = i;
479 else if (DECL_NAME (fields[i]) < name)
480 lo = i + 1;
481 else
bff3ce71
JM
482 {
483 /* We might have a nested class and a field with the
484 same name; we sorted them appropriately via
485 field_decl_cmp, so just look for the last field with
486 this name. */
487 while (i + 1 < hi
488 && DECL_NAME (fields[i+1]) == name)
489 ++i;
490 return fields[i];
491 }
f90cdf34
MT
492 }
493 return NULL_TREE;
494 }
495
f84b4be9 496 field = TYPE_FIELDS (type);
8d08fdba
MS
497
498#ifdef GATHER_STATISTICS
499 n_calls_lookup_field_1++;
fc378698 500#endif /* GATHER_STATISTICS */
8d08fdba
MS
501 while (field)
502 {
503#ifdef GATHER_STATISTICS
504 n_fields_searched++;
fc378698 505#endif /* GATHER_STATISTICS */
2f939d94 506 my_friendly_assert (DECL_P (field), 0);
8d08fdba 507 if (DECL_NAME (field) == NULL_TREE
6bdb8141 508 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8d08fdba
MS
509 {
510 tree temp = lookup_field_1 (TREE_TYPE (field), name);
511 if (temp)
512 return temp;
513 }
2036a15c
MM
514 if (TREE_CODE (field) == USING_DECL)
515 /* For now, we're just treating member using declarations as
516 old ARM-style access declarations. Thus, there's no reason
517 to return a USING_DECL, and the rest of the compiler can't
518 handle it. Once the class is defined, these are purged
519 from TYPE_FIELDS anyhow; see handle_using_decl. */
520 ;
521 else if (DECL_NAME (field) == name)
65f36ac8 522 return field;
8d08fdba
MS
523 field = TREE_CHAIN (field);
524 }
525 /* Not found. */
9cd64686 526 if (name == vptr_identifier)
8d08fdba
MS
527 {
528 /* Give the user what s/he thinks s/he wants. */
4c6b7393 529 if (TYPE_POLYMORPHIC_P (type))
d3a3fb6a 530 return TYPE_VFIELD (type);
8d08fdba
MS
531 }
532 return NULL_TREE;
533}
534
7177d104
MS
535/* There are a number of cases we need to be aware of here:
536 current_class_type current_function_decl
e92cc029
MS
537 global NULL NULL
538 fn-local NULL SET
539 class-local SET NULL
540 class->fn SET SET
541 fn->class SET SET
7177d104
MS
542
543 Those last two make life interesting. If we're in a function which is
544 itself inside a class, we need decls to go into the fn's decls (our
545 second case below). But if we're in a class and the class itself is
546 inside a function, we need decls to go into the decls for the class. To
4ac14744 547 achieve this last goal, we must see if, when both current_class_ptr and
7177d104
MS
548 current_function_decl are set, the class was declared inside that
549 function. If so, we know to put the decls into the class's scope. */
550
8d08fdba
MS
551tree
552current_scope ()
553{
554 if (current_function_decl == NULL_TREE)
555 return current_class_type;
556 if (current_class_type == NULL_TREE)
557 return current_function_decl;
4f1c5b7d
MM
558 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
559 && same_type_p (DECL_CONTEXT (current_function_decl),
560 current_class_type))
561 || (DECL_FRIEND_CONTEXT (current_function_decl)
562 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
563 current_class_type)))
8d08fdba
MS
564 return current_function_decl;
565
566 return current_class_type;
567}
568
9188c363
MM
569/* Returns non-zero if we are currently in a function scope. Note
570 that this function returns zero if we are within a local class, but
571 not within a member function body of the local class. */
572
573int
574at_function_scope_p ()
575{
576 tree cs = current_scope ();
577 return cs && TREE_CODE (cs) == FUNCTION_DECL;
578}
579
5f261ba9
MM
580/* Returns true if the innermost active scope is a class scope. */
581
582bool
583at_class_scope_p ()
584{
585 tree cs = current_scope ();
586 return cs && TYPE_P (cs);
587}
588
d6479fe7 589/* Return the scope of DECL, as appropriate when doing name-lookup. */
8d08fdba 590
55de1b66 591tree
d6479fe7
MM
592context_for_name_lookup (decl)
593 tree decl;
594{
595 /* [class.union]
596
597 For the purposes of name lookup, after the anonymous union
598 definition, the members of the anonymous union are considered to
834c6dff 599 have been defined in the scope in which the anonymous union is
d6479fe7 600 declared. */
55de1b66 601 tree context = DECL_CONTEXT (decl);
d6479fe7 602
55de1b66 603 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
d6479fe7
MM
604 context = TYPE_CONTEXT (context);
605 if (!context)
606 context = global_namespace;
8d08fdba 607
d6479fe7
MM
608 return context;
609}
8d08fdba 610
d6479fe7
MM
611/* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
612 otherwise. */
8d08fdba 613
d6479fe7
MM
614static tree
615canonical_binfo (binfo)
616 tree binfo;
617{
618 return (TREE_VIA_VIRTUAL (binfo)
619 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
620}
8d08fdba 621
6cbd257e
MM
622/* A queue function that simply ensures that we walk into the
623 canonical versions of virtual bases. */
624
625static tree
626dfs_canonical_queue (binfo, data)
627 tree binfo;
628 void *data ATTRIBUTE_UNUSED;
629{
630 return canonical_binfo (binfo);
631}
632
633/* Called via dfs_walk from assert_canonical_unmarked. */
634
635static tree
636dfs_assert_unmarked_p (binfo, data)
637 tree binfo;
638 void *data ATTRIBUTE_UNUSED;
639{
640 my_friendly_assert (!BINFO_MARKED (binfo), 0);
641 return NULL_TREE;
642}
643
644/* Asserts that all the nodes below BINFO (using the canonical
645 versions of virtual bases) are unmarked. */
646
647static void
648assert_canonical_unmarked (binfo)
649 tree binfo;
650{
651 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
652}
653
d6479fe7
MM
654/* If BINFO is marked, return a canonical version of BINFO.
655 Otherwise, return NULL_TREE. */
8d08fdba 656
d6479fe7
MM
657static tree
658shared_marked_p (binfo, data)
659 tree binfo;
660 void *data;
661{
662 binfo = canonical_binfo (binfo);
8026246f 663 return markedp (binfo, data);
d6479fe7 664}
8d08fdba 665
d6479fe7
MM
666/* If BINFO is not marked, return a canonical version of BINFO.
667 Otherwise, return NULL_TREE. */
8d08fdba 668
d6479fe7
MM
669static tree
670shared_unmarked_p (binfo, data)
671 tree binfo;
672 void *data;
8d08fdba 673{
d6479fe7 674 binfo = canonical_binfo (binfo);
8026246f 675 return unmarkedp (binfo, data);
d6479fe7 676}
8d08fdba 677
c35cce41
MM
678/* The accessibility routines use BINFO_ACCESS for scratch space
679 during the computation of the accssibility of some declaration. */
680
681#define BINFO_ACCESS(NODE) \
682 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
683
684/* Set the access associated with NODE to ACCESS. */
685
686#define SET_BINFO_ACCESS(NODE, ACCESS) \
426b490f
GS
687 ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0), \
688 (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
c35cce41 689
d6479fe7
MM
690/* Called from access_in_type via dfs_walk. Calculate the access to
691 DATA (which is really a DECL) in BINFO. */
eae89e04 692
d6479fe7
MM
693static tree
694dfs_access_in_type (binfo, data)
695 tree binfo;
696 void *data;
697{
698 tree decl = (tree) data;
699 tree type = BINFO_TYPE (binfo);
c35cce41 700 access_kind access = ak_none;
8d08fdba 701
d6479fe7 702 if (context_for_name_lookup (decl) == type)
8d08fdba 703 {
d6479fe7
MM
704 /* If we have desceneded to the scope of DECL, just note the
705 appropriate access. */
706 if (TREE_PRIVATE (decl))
c35cce41 707 access = ak_private;
d6479fe7 708 else if (TREE_PROTECTED (decl))
c35cce41 709 access = ak_protected;
d6479fe7 710 else
c35cce41 711 access = ak_public;
8d08fdba 712 }
d6479fe7
MM
713 else
714 {
715 /* First, check for an access-declaration that gives us more
716 access to the DECL. The CONST_DECL for an enumeration
717 constant will not have DECL_LANG_SPECIFIC, and thus no
718 DECL_ACCESS. */
8e4ce833 719 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
d6479fe7 720 {
c35cce41
MM
721 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
722 if (decl_access)
723 access = ((access_kind)
724 TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
d6479fe7
MM
725 }
726
727 if (!access)
728 {
729 int i;
730 int n_baselinks;
731 tree binfos;
732
733 /* Otherwise, scan our baseclasses, and pick the most favorable
734 access. */
735 binfos = BINFO_BASETYPES (binfo);
736 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
737 for (i = 0; i < n_baselinks; ++i)
738 {
739 tree base_binfo = TREE_VEC_ELT (binfos, i);
c35cce41
MM
740 access_kind base_access
741 = BINFO_ACCESS (canonical_binfo (base_binfo));
d6479fe7 742
c35cce41 743 if (base_access == ak_none || base_access == ak_private)
d6479fe7
MM
744 /* If it was not accessible in the base, or only
745 accessible as a private member, we can't access it
746 all. */
c35cce41 747 base_access = ak_none;
d6479fe7
MM
748 else if (TREE_VIA_PROTECTED (base_binfo))
749 /* Public and protected members in the base are
750 protected here. */
c35cce41 751 base_access = ak_protected;
d6479fe7
MM
752 else if (!TREE_VIA_PUBLIC (base_binfo))
753 /* Public and protected members in the base are
754 private here. */
c35cce41 755 base_access = ak_private;
d6479fe7
MM
756
757 /* See if the new access, via this base, gives more
758 access than our previous best access. */
c35cce41
MM
759 if (base_access != ak_none
760 && (base_access == ak_public
761 || (base_access == ak_protected
762 && access != ak_public)
763 || (base_access == ak_private
764 && access == ak_none)))
d6479fe7
MM
765 {
766 access = base_access;
8d08fdba 767
d6479fe7 768 /* If the new access is public, we can't do better. */
c35cce41 769 if (access == ak_public)
d6479fe7
MM
770 break;
771 }
772 }
773 }
774 }
faae18ab 775
d6479fe7 776 /* Note the access to DECL in TYPE. */
c35cce41 777 SET_BINFO_ACCESS (binfo, access);
02020185 778
d6479fe7
MM
779 /* Mark TYPE as visited so that if we reach it again we do not
780 duplicate our efforts here. */
781 SET_BINFO_MARKED (binfo);
8d08fdba 782
d6479fe7
MM
783 return NULL_TREE;
784}
8d08fdba 785
d6479fe7 786/* Return the access to DECL in TYPE. */
8d08fdba 787
c35cce41 788static access_kind
d6479fe7
MM
789access_in_type (type, decl)
790 tree type;
791 tree decl;
792{
793 tree binfo = TYPE_BINFO (type);
8d08fdba 794
d6479fe7 795 /* We must take into account
8d08fdba 796
d6479fe7 797 [class.paths]
8d08fdba 798
d6479fe7
MM
799 If a name can be reached by several paths through a multiple
800 inheritance graph, the access is that of the path that gives
801 most access.
8d08fdba 802
d6479fe7
MM
803 The algorithm we use is to make a post-order depth-first traversal
804 of the base-class hierarchy. As we come up the tree, we annotate
805 each node with the most lenient access. */
806 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
807 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
6cbd257e 808 assert_canonical_unmarked (binfo);
8d08fdba 809
c35cce41 810 return BINFO_ACCESS (binfo);
d6479fe7
MM
811}
812
813/* Called from dfs_accessible_p via dfs_walk. */
814
815static tree
816dfs_accessible_queue_p (binfo, data)
817 tree binfo;
818 void *data ATTRIBUTE_UNUSED;
819{
820 if (BINFO_MARKED (binfo))
821 return NULL_TREE;
822
823 /* If this class is inherited via private or protected inheritance,
824 then we can't see it, unless we are a friend of the subclass. */
825 if (!TREE_VIA_PUBLIC (binfo)
826 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
827 current_scope ()))
828 return NULL_TREE;
829
830 return canonical_binfo (binfo);
831}
832
833/* Called from dfs_accessible_p via dfs_walk. */
834
835static tree
836dfs_accessible_p (binfo, data)
837 tree binfo;
838 void *data;
839{
840 int protected_ok = data != 0;
c35cce41 841 access_kind access;
d6479fe7 842
d6479fe7 843 SET_BINFO_MARKED (binfo);
c35cce41
MM
844 access = BINFO_ACCESS (binfo);
845 if (access == ak_public || (access == ak_protected && protected_ok))
d6479fe7 846 return binfo;
c35cce41
MM
847 else if (access != ak_none
848 && is_friend (BINFO_TYPE (binfo), current_scope ()))
d6479fe7
MM
849 return binfo;
850
851 return NULL_TREE;
852}
853
d7cca31e
JM
854/* Returns non-zero if it is OK to access DECL through an object
855 indiated by BINFO in the context of DERIVED. */
6a629cac
MM
856
857static int
d7cca31e 858protected_accessible_p (decl, derived, binfo)
6a629cac
MM
859 tree decl;
860 tree derived;
861 tree binfo;
862{
c35cce41 863 access_kind access;
6a629cac
MM
864
865 /* We're checking this clause from [class.access.base]
866
867 m as a member of N is protected, and the reference occurs in a
868 member or friend of class N, or in a member or friend of a
869 class P derived from N, where m as a member of P is private or
870 protected.
871
d7cca31e
JM
872 Here DERIVED is a possible P and DECL is m. accessible_p will
873 iterate over various values of N, but the access to m in DERIVED
874 does not change.
875
876 Note that I believe that the passage above is wrong, and should read
877 "...is private or protected or public"; otherwise you get bizarre results
878 whereby a public using-decl can prevent you from accessing a protected
879 member of a base. (jason 2000/02/28) */
880
881 /* If DERIVED isn't derived from m's class, then it can't be a P. */
e185aa16 882 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
6a629cac
MM
883 return 0;
884
885 access = access_in_type (derived, decl);
d7cca31e
JM
886
887 /* If m is inaccessible in DERIVED, then it's not a P. */
c35cce41 888 if (access == ak_none)
6a629cac
MM
889 return 0;
890
891 /* [class.protected]
892
893 When a friend or a member function of a derived class references
894 a protected nonstatic member of a base class, an access check
895 applies in addition to those described earlier in clause
d7cca31e 896 _class.access_) Except when forming a pointer to member
6a629cac
MM
897 (_expr.unary.op_), the access must be through a pointer to,
898 reference to, or object of the derived class itself (or any class
899 derived from that class) (_expr.ref_). If the access is to form
900 a pointer to member, the nested-name-specifier shall name the
901 derived class (or any class derived from that class). */
902 if (DECL_NONSTATIC_MEMBER_P (decl))
903 {
904 /* We can tell through what the reference is occurring by
905 chasing BINFO up to the root. */
906 tree t = binfo;
907 while (BINFO_INHERITANCE_CHAIN (t))
908 t = BINFO_INHERITANCE_CHAIN (t);
909
910 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
911 return 0;
912 }
913
914 return 1;
915}
916
917/* Returns non-zero if SCOPE is a friend of a type which would be able
d7cca31e 918 to access DECL through the object indicated by BINFO. */
6a629cac
MM
919
920static int
d7cca31e 921friend_accessible_p (scope, decl, binfo)
6a629cac 922 tree scope;
6a629cac
MM
923 tree decl;
924 tree binfo;
925{
926 tree befriending_classes;
927 tree t;
928
929 if (!scope)
930 return 0;
931
932 if (TREE_CODE (scope) == FUNCTION_DECL
933 || DECL_FUNCTION_TEMPLATE_P (scope))
934 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
935 else if (TYPE_P (scope))
936 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
937 else
938 return 0;
939
940 for (t = befriending_classes; t; t = TREE_CHAIN (t))
d7cca31e 941 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
6a629cac
MM
942 return 1;
943
445ab443
JM
944 /* Nested classes are implicitly friends of their enclosing types, as
945 per core issue 45 (this is a change from the standard). */
946 if (TYPE_P (scope))
947 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
d7cca31e 948 if (protected_accessible_p (decl, t, binfo))
445ab443
JM
949 return 1;
950
6a629cac
MM
951 if (TREE_CODE (scope) == FUNCTION_DECL
952 || DECL_FUNCTION_TEMPLATE_P (scope))
953 {
954 /* Perhaps this SCOPE is a member of a class which is a
955 friend. */
4f1c5b7d 956 if (DECL_CLASS_SCOPE_P (decl)
d7cca31e 957 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
6a629cac
MM
958 return 1;
959
960 /* Or an instantiation of something which is a friend. */
961 if (DECL_TEMPLATE_INFO (scope))
d7cca31e 962 return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
6a629cac
MM
963 }
964 else if (CLASSTYPE_TEMPLATE_INFO (scope))
d7cca31e 965 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
6a629cac
MM
966
967 return 0;
968}
70adf8a9 969
0e902d98
KL
970/* Perform access control on TYPE_DECL or TEMPLATE_DECL VAL, which was
971 looked up in TYPE. This is fairly complex, so here's the design:
70adf8a9
JM
972
973 The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
974 start to process a top-level declaration.
975 As we process the decl-specifier-seq for the declaration, any types we
976 see that might need access control are passed to type_access_control,
977 which defers checking by adding them to type_lookups.
978 When we are done with the decl-specifier-seq, we record the lookups we've
979 seen in the lookups field of the typed_declspecs nonterminal.
980 When we process the first declarator, either in parse_decl or
1f51a992
JM
981 begin_function_definition, we call save_type_access_control,
982 which stores the lookups from the decl-specifier-seq in
983 current_type_lookups.
984 As we finish with each declarator, we process everything in type_lookups
985 via decl_type_access_control, which resets type_lookups to the value of
986 current_type_lookups for subsequent declarators.
987 When we enter a function, we set type_lookups to error_mark_node, so all
988 lookups are processed immediately. */
70adf8a9
JM
989
990void
991type_access_control (type, val)
992 tree type, val;
993{
0e902d98
KL
994 if (val == NULL_TREE
995 || (TREE_CODE (val) != TEMPLATE_DECL && TREE_CODE (val) != TYPE_DECL)
70adf8a9
JM
996 || ! DECL_CLASS_SCOPE_P (val))
997 return;
998
999 if (type_lookups == error_mark_node)
1000 enforce_access (type, val);
1001 else if (! accessible_p (type, val))
1002 type_lookups = tree_cons (type, val, type_lookups);
1003}
1004
d6479fe7 1005/* DECL is a declaration from a base class of TYPE, which was the
70adf8a9 1006 class used to name DECL. Return non-zero if, in the current
d6479fe7 1007 context, DECL is accessible. If TYPE is actually a BINFO node,
8084bf81
MM
1008 then we can tell in what context the access is occurring by looking
1009 at the most derived class along the path indicated by BINFO. */
d6479fe7
MM
1010
1011int
1012accessible_p (type, decl)
1013 tree type;
1014 tree decl;
1015
1016{
d6479fe7
MM
1017 tree binfo;
1018 tree t;
1019
1020 /* Non-zero if it's OK to access DECL if it has protected
1021 accessibility in TYPE. */
1022 int protected_ok = 0;
1023
1024 /* If we're not checking access, everything is accessible. */
1025 if (!flag_access_control)
1026 return 1;
1027
1028 /* If this declaration is in a block or namespace scope, there's no
1029 access control. */
1030 if (!TYPE_P (context_for_name_lookup (decl)))
1031 return 1;
1032
d6479fe7
MM
1033 if (!TYPE_P (type))
1034 {
1035 binfo = type;
1036 type = BINFO_TYPE (type);
8d08fdba 1037 }
d6479fe7
MM
1038 else
1039 binfo = TYPE_BINFO (type);
1040
1041 /* [class.access.base]
1042
1043 A member m is accessible when named in class N if
1044
1045 --m as a member of N is public, or
8d08fdba 1046
d6479fe7
MM
1047 --m as a member of N is private, and the reference occurs in a
1048 member or friend of class N, or
8d08fdba 1049
d6479fe7
MM
1050 --m as a member of N is protected, and the reference occurs in a
1051 member or friend of class N, or in a member or friend of a
1052 class P derived from N, where m as a member of P is private or
1053 protected, or
1054
1055 --there exists a base class B of N that is accessible at the point
1056 of reference, and m is accessible when named in class B.
1057
1058 We walk the base class hierarchy, checking these conditions. */
1059
1060 /* Figure out where the reference is occurring. Check to see if
1061 DECL is private or protected in this scope, since that will
d7cca31e 1062 determine whether protected access is allowed. */
6a629cac 1063 if (current_class_type)
d7cca31e 1064 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
8d08fdba 1065
6a629cac
MM
1066 /* Now, loop through the classes of which we are a friend. */
1067 if (!protected_ok)
d7cca31e 1068 protected_ok = friend_accessible_p (current_scope (), decl, binfo);
8d08fdba 1069
70adf8a9
JM
1070 /* Standardize the binfo that access_in_type will use. We don't
1071 need to know what path was chosen from this point onwards. */
d6479fe7
MM
1072 binfo = TYPE_BINFO (type);
1073
1074 /* Compute the accessibility of DECL in the class hierarchy
1075 dominated by type. */
1076 access_in_type (type, decl);
1077 /* Walk the hierarchy again, looking for a base class that allows
1078 access. */
1079 t = dfs_walk (binfo, dfs_accessible_p,
1080 dfs_accessible_queue_p,
1081 protected_ok ? &protected_ok : 0);
6cbd257e
MM
1082 /* Clear any mark bits. Note that we have to walk the whole tree
1083 here, since we have aborted the previous walk from some point
1084 deep in the tree. */
1085 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1086 assert_canonical_unmarked (binfo);
d6479fe7
MM
1087
1088 return t != NULL_TREE;
8d08fdba
MS
1089}
1090
1091/* Routine to see if the sub-object denoted by the binfo PARENT can be
1092 found as a base class and sub-object of the object denoted by
0e997e76
NS
1093 BINFO. MOST_DERIVED is the most derived type of the hierarchy being
1094 searched. */
e92cc029 1095
8d08fdba 1096static int
0e997e76
NS
1097is_subobject_of_p (parent, binfo, most_derived)
1098 tree parent, binfo, most_derived;
8d08fdba 1099{
d2675b98
MM
1100 tree binfos;
1101 int i, n_baselinks;
8d08fdba
MS
1102
1103 if (parent == binfo)
1104 return 1;
1105
0e997e76 1106 binfos = BINFO_BASETYPES (binfo);
d2675b98
MM
1107 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1108
0e997e76 1109 /* Iterate the base types. */
8d08fdba
MS
1110 for (i = 0; i < n_baselinks; i++)
1111 {
d2675b98
MM
1112 tree base_binfo = TREE_VEC_ELT (binfos, i);
1113 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo)))
1114 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1115 class there's no way to descend into it. */
1116 continue;
1117
0e997e76
NS
1118 if (is_subobject_of_p (parent,
1119 CANONICAL_BINFO (base_binfo, most_derived),
1120 most_derived))
8d08fdba
MS
1121 return 1;
1122 }
1123 return 0;
1124}
1125
7d4bdeed 1126struct lookup_field_info {
d6479fe7
MM
1127 /* The type in which we're looking. */
1128 tree type;
7d4bdeed
MM
1129 /* The name of the field for which we're looking. */
1130 tree name;
1131 /* If non-NULL, the current result of the lookup. */
1132 tree rval;
1133 /* The path to RVAL. */
1134 tree rval_binfo;
d6479fe7
MM
1135 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1136 candidates. */
7d4bdeed 1137 tree ambiguous;
7d4bdeed
MM
1138 /* If non-zero, we are looking for types, not data members. */
1139 int want_type;
d6479fe7
MM
1140 /* If non-zero, RVAL was found by looking through a dependent base. */
1141 int from_dep_base_p;
7d4bdeed 1142 /* If something went wrong, a message indicating what. */
d8e178a0 1143 const char *errstr;
7d4bdeed
MM
1144};
1145
1146/* Returns non-zero if BINFO is not hidden by the value found by the
1147 lookup so far. If BINFO is hidden, then there's no need to look in
1148 it. DATA is really a struct lookup_field_info. Called from
1149 lookup_field via breadth_first_search. */
1150
d6479fe7 1151static tree
7d4bdeed
MM
1152lookup_field_queue_p (binfo, data)
1153 tree binfo;
1154 void *data;
1155{
1156 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
d6479fe7
MM
1157
1158 /* Don't look for constructors or destructors in base classes. */
298d6f60 1159 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
d6479fe7
MM
1160 return NULL_TREE;
1161
1162 /* If this base class is hidden by the best-known value so far, we
1163 don't need to look. */
1164 if (!lfi->from_dep_base_p && lfi->rval_binfo
0e997e76 1165 && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
d6479fe7
MM
1166 return NULL_TREE;
1167
a55583e9 1168 return CANONICAL_BINFO (binfo, lfi->type);
7d4bdeed
MM
1169}
1170
9188c363
MM
1171/* Within the scope of a template class, you can refer to the to the
1172 current specialization with the name of the template itself. For
1173 example:
8f032717
MM
1174
1175 template <typename T> struct S { S* sp; }
1176
1177 Returns non-zero if DECL is such a declaration in a class TYPE. */
1178
1179static int
1180template_self_reference_p (type, decl)
1181 tree type;
1182 tree decl;
1183{
1184 return (CLASSTYPE_USE_TEMPLATE (type)
3fc5037b 1185 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
8f032717
MM
1186 && TREE_CODE (decl) == TYPE_DECL
1187 && DECL_ARTIFICIAL (decl)
1188 && DECL_NAME (decl) == constructor_name (type));
1189}
1190
bd0d5d4a
JM
1191
1192/* Nonzero for a class member means that it is shared between all objects
1193 of that class.
1194
1195 [class.member.lookup]:If the resulting set of declarations are not all
1196 from sub-objects of the same type, or the set has a nonstatic member
1197 and includes members from distinct sub-objects, there is an ambiguity
1198 and the program is ill-formed.
1199
1200 This function checks that T contains no nonstatic members. */
1201
1202static int
1203shared_member_p (t)
1204 tree t;
1205{
1206 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
1207 || TREE_CODE (t) == CONST_DECL)
1208 return 1;
1209 if (is_overloaded_fn (t))
1210 {
1211 for (; t; t = OVL_NEXT (t))
1212 {
1213 tree fn = OVL_CURRENT (t);
1214 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1215 return 0;
1216 }
1217 return 1;
1218 }
1219 return 0;
1220}
1221
7d4bdeed
MM
1222/* DATA is really a struct lookup_field_info. Look for a field with
1223 the name indicated there in BINFO. If this function returns a
1224 non-NULL value it is the result of the lookup. Called from
1225 lookup_field via breadth_first_search. */
1226
1227static tree
1228lookup_field_r (binfo, data)
1229 tree binfo;
1230 void *data;
1231{
1232 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1233 tree type = BINFO_TYPE (binfo);
4bb0968f 1234 tree nval = NULL_TREE;
d6479fe7 1235 int from_dep_base_p;
7d4bdeed 1236
d6479fe7
MM
1237 /* First, look for a function. There can't be a function and a data
1238 member with the same name, and if there's a function and a type
1239 with the same name, the type is hidden by the function. */
4bb0968f
MM
1240 if (!lfi->want_type)
1241 {
477f6664 1242 int idx = lookup_fnfields_1 (type, lfi->name);
4bb0968f
MM
1243 if (idx >= 0)
1244 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1245 }
1246
1247 if (!nval)
d6479fe7
MM
1248 /* Look for a data member or type. */
1249 nval = lookup_field_1 (type, lfi->name);
1250
1251 /* If there is no declaration with the indicated name in this type,
1252 then there's nothing to do. */
7d4bdeed 1253 if (!nval)
d6479fe7 1254 return NULL_TREE;
7d4bdeed 1255
4bb0968f
MM
1256 /* If we're looking up a type (as with an elaborated type specifier)
1257 we ignore all non-types we find. */
8a2b77e7
JM
1258 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1259 && !DECL_CLASS_TEMPLATE_P (nval))
4bb0968f 1260 {
881cae05
JJ
1261 if (lfi->name == TYPE_IDENTIFIER (type))
1262 {
1263 /* If the aggregate has no user defined constructors, we allow
1264 it to have fields with the same name as the enclosing type.
1265 If we are looking for that name, find the corresponding
1266 TYPE_DECL. */
1267 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1268 if (DECL_NAME (nval) == lfi->name
1269 && TREE_CODE (nval) == TYPE_DECL)
1270 break;
1271 }
1272 else
1273 nval = NULL_TREE;
1274 if (!nval)
1275 {
1276 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1277 if (nval)
1278 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1279 else
1280 return NULL_TREE;
1281 }
4bb0968f
MM
1282 }
1283
8f032717
MM
1284 /* You must name a template base class with a template-id. */
1285 if (!same_type_p (type, lfi->type)
1286 && template_self_reference_p (type, nval))
1287 return NULL_TREE;
1288
d6479fe7
MM
1289 from_dep_base_p = dependent_base_p (binfo);
1290 if (lfi->from_dep_base_p && !from_dep_base_p)
1291 {
1292 /* If the new declaration is not found via a dependent base, and
1293 the old one was, then we must prefer the new one. We weren't
1294 really supposed to be able to find the old one, so we don't
1295 want to be affected by a specialization. Consider:
1296
1297 struct B { typedef int I; };
1298 template <typename T> struct D1 : virtual public B {};
1299 template <typename T> struct D :
1300 public D1, virtual pubic B { I i; };
1301
1302 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1303 D1 is specialized. */
1304 lfi->from_dep_base_p = 0;
1305 lfi->rval = NULL_TREE;
1306 lfi->rval_binfo = NULL_TREE;
1307 lfi->ambiguous = NULL_TREE;
1308 lfi->errstr = 0;
1309 }
1310 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1311 /* Similarly, if the old declaration was not found via a dependent
1312 base, and the new one is, ignore the new one. */
7d4bdeed
MM
1313 return NULL_TREE;
1314
1315 /* If the lookup already found a match, and the new value doesn't
1316 hide the old one, we might have an ambiguity. */
0e997e76 1317 if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
7d4bdeed 1318 {
bd0d5d4a 1319 if (nval == lfi->rval && shared_member_p (nval))
7d4bdeed
MM
1320 /* The two things are really the same. */
1321 ;
0e997e76 1322 else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
7d4bdeed
MM
1323 /* The previous value hides the new one. */
1324 ;
1325 else
1326 {
1327 /* We have a real ambiguity. We keep a chain of all the
1328 candidates. */
1329 if (!lfi->ambiguous && lfi->rval)
aa65d1a2
MM
1330 {
1331 /* This is the first time we noticed an ambiguity. Add
1332 what we previously thought was a reasonable candidate
1333 to the list. */
e1b3e07d 1334 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
aa65d1a2
MM
1335 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1336 }
1337
7d4bdeed 1338 /* Add the new value. */
e1b3e07d 1339 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
aa65d1a2 1340 TREE_TYPE (lfi->ambiguous) = error_mark_node;
7d4bdeed
MM
1341 lfi->errstr = "request for member `%D' is ambiguous";
1342 }
1343 }
1344 else
1345 {
4bb0968f
MM
1346 if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
1347 /* We need to return a member template class so we can
1348 define partial specializations. Is there a better
1349 way? */
1350 && !DECL_CLASS_TEMPLATE_P (nval))
1351 /* The thing we're looking for isn't a type, so the implicit
1352 typename extension doesn't apply, so we just pretend we
1353 didn't find anything. */
1354 return NULL_TREE;
7d4bdeed 1355
d6479fe7
MM
1356 lfi->rval = nval;
1357 lfi->from_dep_base_p = from_dep_base_p;
7d4bdeed
MM
1358 lfi->rval_binfo = binfo;
1359 }
1360
d6479fe7 1361 return NULL_TREE;
7d4bdeed
MM
1362}
1363
1a03d967
TP
1364/* Look for a member named NAME in an inheritance lattice dominated by
1365 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it is
8f032717
MM
1366 1, we enforce accessibility. If PROTECT is zero, then, for an
1367 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
aa65d1a2 1368 error message. If PROTECT is 2, we return a TREE_LIST whose
70adf8a9 1369 TREE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
8f032717 1370 ambiguous candidates.
8d08fdba 1371
8f032717
MM
1372 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1373 TYPE_DECL can be found return NULL_TREE. */
e92cc029 1374
8d08fdba 1375tree
d6479fe7 1376lookup_member (xbasetype, name, protect, want_type)
8d08fdba
MS
1377 register tree xbasetype, name;
1378 int protect, want_type;
1379{
7d4bdeed
MM
1380 tree rval, rval_binfo = NULL_TREE;
1381 tree type = NULL_TREE, basetype_path = NULL_TREE;
1382 struct lookup_field_info lfi;
8d08fdba
MS
1383
1384 /* rval_binfo is the binfo associated with the found member, note,
1385 this can be set with useful information, even when rval is not
1386 set, because it must deal with ALL members, not just non-function
1387 members. It is used for ambiguity checking and the hidden
1388 checks. Whereas rval is only set if a proper (not hidden)
1389 non-function member is found. */
1390
d8e178a0 1391 const char *errstr = 0;
8d08fdba 1392
de22184b
MS
1393 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1394 && IDENTIFIER_CLASS_VALUE (name))
1395 {
1396 tree field = IDENTIFIER_CLASS_VALUE (name);
1397 if (TREE_CODE (field) != FUNCTION_DECL
1398 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
aa65d1a2
MM
1399 /* We're in the scope of this class, and the value has already
1400 been looked up. Just return the cached value. */
de22184b
MS
1401 return field;
1402 }
1403
8d08fdba
MS
1404 if (TREE_CODE (xbasetype) == TREE_VEC)
1405 {
8d08fdba 1406 type = BINFO_TYPE (xbasetype);
39211cd5 1407 basetype_path = xbasetype;
8d08fdba
MS
1408 }
1409 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
39211cd5 1410 {
238109cd 1411 type = xbasetype;
5566b478 1412 basetype_path = TYPE_BINFO (type);
dfbcd65a
JM
1413 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1414 980827);
39211cd5 1415 }
238109cd 1416 else
a98facb0 1417 abort ();
238109cd
JM
1418
1419 complete_type (type);
8d08fdba 1420
8d08fdba
MS
1421#ifdef GATHER_STATISTICS
1422 n_calls_lookup_field++;
fc378698 1423#endif /* GATHER_STATISTICS */
8d08fdba 1424
961192e1 1425 memset ((PTR) &lfi, 0, sizeof (lfi));
d6479fe7 1426 lfi.type = type;
7d4bdeed 1427 lfi.name = name;
7d4bdeed 1428 lfi.want_type = want_type;
d6479fe7 1429 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
7d4bdeed
MM
1430 rval = lfi.rval;
1431 rval_binfo = lfi.rval_binfo;
1432 if (rval_binfo)
1433 type = BINFO_TYPE (rval_binfo);
1434 errstr = lfi.errstr;
1435
1436 /* If we are not interested in ambiguities, don't report them;
1437 just return NULL_TREE. */
1438 if (!protect && lfi.ambiguous)
1439 return NULL_TREE;
d6479fe7 1440
8f032717
MM
1441 if (protect == 2)
1442 {
1443 if (lfi.ambiguous)
aa65d1a2 1444 return lfi.ambiguous;
8f032717
MM
1445 else
1446 protect = 0;
1447 }
1448
d6479fe7
MM
1449 /* [class.access]
1450
1451 In the case of overloaded function names, access control is
1452 applied to the function selected by overloaded resolution. */
1453 if (rval && protect && !is_overloaded_fn (rval)
d6479fe7
MM
1454 && !enforce_access (xbasetype, rval))
1455 return error_mark_node;
9e9ff709 1456
8251199e 1457 if (errstr && protect)
8d08fdba 1458 {
33bd39a2 1459 error (errstr, name, type);
7d4bdeed
MM
1460 if (lfi.ambiguous)
1461 print_candidates (lfi.ambiguous);
8d08fdba
MS
1462 rval = error_mark_node;
1463 }
b3709d9b 1464
d6479fe7
MM
1465 /* If the thing we found was found via the implicit typename
1466 extension, build the typename type. */
1467 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1468 rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
1469 name, name,
1470 TREE_TYPE (rval)));
1471
4bb0968f
MM
1472 if (rval && is_overloaded_fn (rval))
1473 {
aa52c1ff
JM
1474 /* Note that the binfo we put in the baselink is the binfo where
1475 we found the functions, which we need for overload
1476 resolution, but which should not be passed to enforce_access;
1477 rather, enforce_access wants a binfo which refers to the
1478 scope in which we started looking for the function. This
1479 will generally be the binfo passed into this function as
1480 xbasetype. */
1481
1482 rval = tree_cons (rval_binfo, rval, NULL_TREE);
4bb0968f
MM
1483 SET_BASELINK_P (rval);
1484 }
d6479fe7
MM
1485
1486 return rval;
1487}
1488
1489/* Like lookup_member, except that if we find a function member we
1490 return NULL_TREE. */
1491
1492tree
1493lookup_field (xbasetype, name, protect, want_type)
1494 register tree xbasetype, name;
1495 int protect, want_type;
1496{
1497 tree rval = lookup_member (xbasetype, name, protect, want_type);
1498
1499 /* Ignore functions. */
1500 if (rval && TREE_CODE (rval) == TREE_LIST)
1501 return NULL_TREE;
1502
1503 return rval;
1504}
1505
1506/* Like lookup_member, except that if we find a non-function member we
1507 return NULL_TREE. */
1508
1509tree
1510lookup_fnfields (xbasetype, name, protect)
1511 register tree xbasetype, name;
1512 int protect;
1513{
1514 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1515
1516 /* Ignore non-functions. */
1517 if (rval && TREE_CODE (rval) != TREE_LIST)
1518 return NULL_TREE;
1519
8d08fdba
MS
1520 return rval;
1521}
1522
8d08fdba
MS
1523/* TYPE is a class type. Return the index of the fields within
1524 the method vector with name NAME, or -1 is no such field exists. */
e92cc029 1525
03017874 1526int
8d08fdba
MS
1527lookup_fnfields_1 (type, name)
1528 tree type, name;
1529{
5dd236e2
NS
1530 tree method_vec = (CLASS_TYPE_P (type)
1531 ? CLASSTYPE_METHOD_VEC (type)
1532 : NULL_TREE);
8d08fdba
MS
1533
1534 if (method_vec != 0)
1535 {
f90cdf34 1536 register int i;
8d08fdba 1537 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
f90cdf34
MT
1538 int len = TREE_VEC_LENGTH (method_vec);
1539 tree tmp;
8d08fdba
MS
1540
1541#ifdef GATHER_STATISTICS
1542 n_calls_lookup_fnfields_1++;
fc378698
MS
1543#endif /* GATHER_STATISTICS */
1544
1545 /* Constructors are first... */
f90cdf34 1546 if (name == ctor_identifier)
db9b2174
MM
1547 return (methods[CLASSTYPE_CONSTRUCTOR_SLOT]
1548 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
fc378698 1549 /* and destructors are second. */
f90cdf34 1550 if (name == dtor_identifier)
db9b2174
MM
1551 return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1552 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
fc378698 1553
db9b2174
MM
1554 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1555 i < len && methods[i];
1556 ++i)
8d08fdba
MS
1557 {
1558#ifdef GATHER_STATISTICS
1559 n_outer_fields_searched++;
fc378698 1560#endif /* GATHER_STATISTICS */
f90cdf34
MT
1561
1562 tmp = OVL_CURRENT (methods[i]);
1563 if (DECL_NAME (tmp) == name)
1564 return i;
1565
1566 /* If the type is complete and we're past the conversion ops,
1567 switch to binary search. */
1568 if (! DECL_CONV_FN_P (tmp)
d0f062fb 1569 && COMPLETE_TYPE_P (type))
f90cdf34
MT
1570 {
1571 int lo = i + 1, hi = len;
1572
1573 while (lo < hi)
1574 {
1575 i = (lo + hi) / 2;
1576
1577#ifdef GATHER_STATISTICS
1578 n_outer_fields_searched++;
1579#endif /* GATHER_STATISTICS */
1580
1581 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1582
1583 if (tmp > name)
1584 hi = i;
1585 else if (tmp < name)
1586 lo = i + 1;
1587 else
1588 return i;
1589 }
1590 break;
1591 }
8d08fdba 1592 }
98c1c668
JM
1593
1594 /* If we didn't find it, it might have been a template
5dd236e2
NS
1595 conversion operator to a templated type. If there are any,
1596 such template conversion operators will all be overloaded on
1597 the first conversion slot. (Note that we don't look for this
1598 case above so that we will always find specializations
1599 first.) */
f90cdf34 1600 if (IDENTIFIER_TYPENAME_P (name))
98c1c668 1601 {
5dd236e2
NS
1602 i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1603 if (i < len && methods[i])
98c1c668 1604 {
f90cdf34 1605 tmp = OVL_CURRENT (methods[i]);
5dd236e2
NS
1606 if (TREE_CODE (tmp) == TEMPLATE_DECL
1607 && DECL_TEMPLATE_CONV_FN_P (tmp))
f90cdf34 1608 return i;
8d08fdba
MS
1609 }
1610 }
8d08fdba
MS
1611 }
1612
d6479fe7 1613 return -1;
d23a1bb1 1614}
8d08fdba 1615\f
d6479fe7 1616/* Walk the class hierarchy dominated by TYPE. FN is called for each
8a2b77e7 1617 type in the hierarchy, in a breadth-first preorder traversal.
d6479fe7 1618 If it ever returns a non-NULL value, that value is immediately
8a2b77e7 1619 returned and the walk is terminated. At each node, FN is passed a
d6479fe7 1620 BINFO indicating the path from the curently visited base-class to
c35cce41
MM
1621 TYPE. Before each base-class is walked QFN is called. If the
1622 value returned is non-zero, the base-class is walked; otherwise it
1623 is not. If QFN is NULL, it is treated as a function which always
1624 returns 1. Both FN and QFN are passed the DATA whenever they are
1625 called. */
8d08fdba 1626
72c4a2a6 1627static tree
d6479fe7 1628bfs_walk (binfo, fn, qfn, data)
8d08fdba 1629 tree binfo;
158991b7
KG
1630 tree (*fn) PARAMS ((tree, void *));
1631 tree (*qfn) PARAMS ((tree, void *));
7d4bdeed 1632 void *data;
8d08fdba 1633{
d6479fe7
MM
1634 size_t head;
1635 size_t tail;
72c4a2a6 1636 tree rval = NULL_TREE;
d6479fe7
MM
1637 /* An array of the base classes of BINFO. These will be built up in
1638 breadth-first order, except where QFN prunes the search. */
1639 varray_type bfs_bases;
8d08fdba 1640
d6479fe7
MM
1641 /* Start with enough room for ten base classes. That will be enough
1642 for most hierarchies. */
1643 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
8d08fdba 1644
d6479fe7
MM
1645 /* Put the first type into the stack. */
1646 VARRAY_TREE (bfs_bases, 0) = binfo;
1647 tail = 1;
72c4a2a6 1648
d6479fe7 1649 for (head = 0; head < tail; ++head)
8d08fdba 1650 {
8d08fdba 1651 int i;
d6479fe7
MM
1652 int n_baselinks;
1653 tree binfos;
8d08fdba 1654
7d4bdeed 1655 /* Pull the next type out of the queue. */
d6479fe7 1656 binfo = VARRAY_TREE (bfs_bases, head);
7d4bdeed
MM
1657
1658 /* If this is the one we're looking for, we're done. */
d6479fe7 1659 rval = (*fn) (binfo, data);
7d4bdeed
MM
1660 if (rval)
1661 break;
1662
1663 /* Queue up the base types. */
1664 binfos = BINFO_BASETYPES (binfo);
1665 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
8d08fdba
MS
1666 for (i = 0; i < n_baselinks; i++)
1667 {
1668 tree base_binfo = TREE_VEC_ELT (binfos, i);
1669
d6479fe7
MM
1670 if (qfn)
1671 base_binfo = (*qfn) (base_binfo, data);
7d4bdeed 1672
d6479fe7 1673 if (base_binfo)
8d08fdba 1674 {
d6479fe7
MM
1675 if (tail == VARRAY_SIZE (bfs_bases))
1676 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1677 VARRAY_TREE (bfs_bases, tail) = base_binfo;
72c4a2a6 1678 ++tail;
8d08fdba
MS
1679 }
1680 }
7d4bdeed 1681 }
8d08fdba 1682
d6479fe7
MM
1683 return rval;
1684}
1685
1686/* Exactly like bfs_walk, except that a depth-first traversal is
1687 performed, and PREFN is called in preorder, while POSTFN is called
1688 in postorder. */
1689
bbd15aac 1690tree
d6479fe7
MM
1691dfs_walk_real (binfo, prefn, postfn, qfn, data)
1692 tree binfo;
158991b7
KG
1693 tree (*prefn) PARAMS ((tree, void *));
1694 tree (*postfn) PARAMS ((tree, void *));
1695 tree (*qfn) PARAMS ((tree, void *));
d6479fe7
MM
1696 void *data;
1697{
1698 int i;
1699 int n_baselinks;
1700 tree binfos;
1701 tree rval = NULL_TREE;
1702
1703 /* Call the pre-order walking function. */
1704 if (prefn)
7d4bdeed 1705 {
d6479fe7
MM
1706 rval = (*prefn) (binfo, data);
1707 if (rval)
1708 return rval;
8d08fdba 1709 }
8d08fdba 1710
d6479fe7
MM
1711 /* Process the basetypes. */
1712 binfos = BINFO_BASETYPES (binfo);
23656158 1713 n_baselinks = BINFO_N_BASETYPES (binfo);
d6479fe7
MM
1714 for (i = 0; i < n_baselinks; i++)
1715 {
1716 tree base_binfo = TREE_VEC_ELT (binfos, i);
1717
1718 if (qfn)
1719 base_binfo = (*qfn) (base_binfo, data);
1720
1721 if (base_binfo)
1722 {
1723 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1724 if (rval)
1725 return rval;
1726 }
1727 }
1728
1729 /* Call the post-order walking function. */
1730 if (postfn)
1731 rval = (*postfn) (binfo, data);
1732
8d08fdba
MS
1733 return rval;
1734}
1735
d6479fe7
MM
1736/* Exactly like bfs_walk, except that a depth-first post-order traversal is
1737 performed. */
1738
1739tree
1740dfs_walk (binfo, fn, qfn, data)
1741 tree binfo;
158991b7
KG
1742 tree (*fn) PARAMS ((tree, void *));
1743 tree (*qfn) PARAMS ((tree, void *));
d6479fe7
MM
1744 void *data;
1745{
1746 return dfs_walk_real (binfo, 0, fn, qfn, data);
1747}
1748
cc600f33
JM
1749/* Returns > 0 if a function with type DRETTYPE overriding a function
1750 with type BRETTYPE is covariant, as defined in [class.virtual].
1751
1752 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1753 adjustment), or -1 if pedantically invalid covariance. */
1754
c6160f8f 1755static int
cc600f33
JM
1756covariant_return_p (brettype, drettype)
1757 tree brettype, drettype;
1758{
1759 tree binfo;
2db1ab2d 1760 base_kind kind;
cc600f33 1761
eb68cb58 1762 if (TREE_CODE (brettype) == FUNCTION_DECL)
cc600f33
JM
1763 {
1764 brettype = TREE_TYPE (TREE_TYPE (brettype));
1765 drettype = TREE_TYPE (TREE_TYPE (drettype));
1766 }
1767 else if (TREE_CODE (brettype) == METHOD_TYPE)
1768 {
1769 brettype = TREE_TYPE (brettype);
1770 drettype = TREE_TYPE (drettype);
1771 }
1772
3bfdc719 1773 if (same_type_p (brettype, drettype))
cc600f33
JM
1774 return 0;
1775
1776 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1777 && (TREE_CODE (brettype) == POINTER_TYPE
1778 || TREE_CODE (brettype) == REFERENCE_TYPE)
91063b51 1779 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
cc600f33
JM
1780 return 0;
1781
1782 if (! can_convert (brettype, drettype))
1783 return 0;
1784
1785 brettype = TREE_TYPE (brettype);
1786 drettype = TREE_TYPE (drettype);
1787
1788 /* If not pedantic, allow any standard pointer conversion. */
1789 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
1790 return -1;
1791
2db1ab2d
NS
1792 binfo = lookup_base (drettype, brettype, ba_check | ba_quiet, &kind);
1793
1794 if (!binfo)
1795 return 0;
1796 if (BINFO_OFFSET_ZEROP (binfo) && kind != bk_via_virtual)
e76e4a68 1797 return 1;
2db1ab2d 1798 return 2;
cc600f33
JM
1799}
1800
4cc1d462
NS
1801/* Check that virtual overrider OVERRIDER is acceptable for base function
1802 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1803
8d1f0f67 1804int
4cc1d462
NS
1805check_final_overrider (overrider, basefn)
1806 tree overrider, basefn;
1807{
1808 tree over_type = TREE_TYPE (overrider);
1809 tree base_type = TREE_TYPE (basefn);
1810 tree over_return = TREE_TYPE (over_type);
1811 tree base_return = TREE_TYPE (base_type);
1812 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1813 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1814 int i;
1815
1816 if (same_type_p (base_return, over_return))
1817 /* OK */;
1818 else if ((i = covariant_return_p (base_return, over_return)))
1819 {
1820 if (i == 2)
1821 sorry ("adjusting pointers for covariant returns");
1822
1823 if (pedantic && i == -1)
1824 {
cbb40945
NS
1825 cp_pedwarn_at ("invalid covariant return type for `%#D'", overrider);
1826 cp_pedwarn_at (" overriding `%#D' (must be pointer or reference to class)", basefn);
4cc1d462
NS
1827 }
1828 }
1829 else if (IS_AGGR_TYPE_2 (base_return, over_return)
1830 && same_or_base_type_p (base_return, over_return))
1831 {
cbb40945
NS
1832 cp_error_at ("invalid covariant return type for `%#D'", overrider);
1833 cp_error_at (" overriding `%#D' (must use pointer or reference)", basefn);
4cc1d462
NS
1834 return 0;
1835 }
1836 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)) == NULL_TREE)
1837 {
cbb40945
NS
1838 cp_error_at ("conflicting return type specified for `%#D'", overrider);
1839 cp_error_at (" overriding `%#D'", basefn);
4cc1d462 1840 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
4f1c5b7d 1841 DECL_CONTEXT (overrider));
4cc1d462
NS
1842 return 0;
1843 }
1844
8152c320 1845 /* Check throw specifier is at least as strict. */
03378143 1846 if (!comp_except_specs (base_throw, over_throw, 0))
4cc1d462 1847 {
cbb40945
NS
1848 cp_error_at ("looser throw specifier for `%#F'", overrider);
1849 cp_error_at (" overriding `%#F'", basefn);
4cc1d462
NS
1850 return 0;
1851 }
1852 return 1;
1853}
1854
cbb40945
NS
1855/* Given a class TYPE, and a function decl FNDECL, look for
1856 virtual functions in TYPE's hierarchy which FNDECL overrides.
1857 We do not look in TYPE itself, only its bases.
1858
1859 Returns non-zero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1860 find that it overrides anything.
1861
1862 We check that every function which is overridden, is correctly
1863 overridden. */
e92cc029 1864
cbb40945
NS
1865int
1866look_for_overrides (type, fndecl)
1867 tree type, fndecl;
8d08fdba 1868{
cbb40945
NS
1869 tree binfo = TYPE_BINFO (type);
1870 tree basebinfos = BINFO_BASETYPES (binfo);
1871 int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
1872 int ix;
1873 int found = 0;
8d08fdba 1874
cbb40945
NS
1875 for (ix = 0; ix != nbasebinfos; ix++)
1876 {
1877 tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
1878
1879 if (TYPE_POLYMORPHIC_P (basetype))
1880 found += look_for_overrides_r (basetype, fndecl);
1881 }
1882 return found;
1883}
5e795528 1884
d0cd8b44
JM
1885/* Look in TYPE for virtual functions with the same signature as FNDECL.
1886 This differs from get_matching_virtual in that it will only return
1887 a function from TYPE. */
5e795528 1888
d0cd8b44
JM
1889tree
1890look_for_overrides_here (type, fndecl)
cbb40945
NS
1891 tree type, fndecl;
1892{
1893 int ix;
d0cd8b44
JM
1894
1895 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
cbb40945 1896 ix = CLASSTYPE_DESTRUCTOR_SLOT;
8d08fdba 1897 else
3c505507 1898 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
cbb40945 1899 if (ix >= 0)
8d08fdba 1900 {
cbb40945 1901 tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
cbb40945
NS
1902
1903 for (; fns; fns = OVL_NEXT (fns))
1904 {
1905 tree fn = OVL_CURRENT (fns);
d0cd8b44 1906
cbb40945 1907 if (!DECL_VIRTUAL_P (fn))
d0cd8b44 1908 /* Not a virtual. */;
3c505507 1909 else if (DECL_CONTEXT (fn) != type)
d0cd8b44
JM
1910 /* Introduced with a using declaration. */;
1911 else if (DECL_STATIC_FUNCTION_P (fndecl))
8d08fdba 1912 {
d0cd8b44
JM
1913 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1914 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1915 if (compparms (TREE_CHAIN (btypes), dtypes))
1916 return fn;
cbb40945 1917 }
e0fff4b3 1918 else if (same_signature_p (fndecl, fn))
d0cd8b44
JM
1919 return fn;
1920 }
1921 }
1922 return NULL_TREE;
1923}
e0fff4b3 1924
d0cd8b44
JM
1925/* Look in TYPE for virtual functions overridden by FNDECL. Check both
1926 TYPE itself and its bases. */
1927
1928static int
1929look_for_overrides_r (type, fndecl)
1930 tree type, fndecl;
1931{
1932 tree fn = look_for_overrides_here (type, fndecl);
1933 if (fn)
1934 {
1935 if (DECL_STATIC_FUNCTION_P (fndecl))
1936 {
1937 /* A static member function cannot match an inherited
1938 virtual member function. */
1939 cp_error_at ("`%#D' cannot be declared", fndecl);
1940 cp_error_at (" since `%#D' declared in base class", fn);
1941 }
1942 else
1943 {
1944 /* It's definitely virtual, even if not explicitly set. */
1945 DECL_VIRTUAL_P (fndecl) = 1;
1946 check_final_overrider (fndecl, fn);
8d08fdba 1947 }
d0cd8b44 1948 return 1;
8d08fdba 1949 }
d0cd8b44 1950
cbb40945
NS
1951 /* We failed to find one declared in this class. Look in its bases. */
1952 return look_for_overrides (type, fndecl);
8d08fdba
MS
1953}
1954
6d4c57a0
NS
1955/* A queue function to use with dfs_walk that only walks into
1956 canonical bases. DATA should be the type of the complete object,
1957 or a TREE_LIST whose TREE_PURPOSE is the type of the complete
1958 object. By using this function as a queue function, you will walk
1959 over exactly those BINFOs that actually exist in the complete
1960 object, including those for virtual base classes. If you
1961 SET_BINFO_MARKED for each binfo you process, you are further
1962 guaranteed that you will walk into each virtual base class exactly
1963 once. */
174eceea
MM
1964
1965tree
6d4c57a0 1966dfs_unmarked_real_bases_queue_p (binfo, data)
174eceea 1967 tree binfo;
70ae3201 1968 void *data;
174eceea 1969{
6d4c57a0 1970 if (TREE_VIA_VIRTUAL (binfo))
174eceea 1971 {
70ae3201
MM
1972 tree type = (tree) data;
1973
1974 if (TREE_CODE (type) == TREE_LIST)
1975 type = TREE_PURPOSE (type);
a55583e9 1976 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
174eceea 1977 }
6d4c57a0 1978 return unmarkedp (binfo, NULL);
174eceea
MM
1979}
1980
1981/* Like dfs_unmarked_real_bases_queue_p but walks only into things
1982 that are marked, rather than unmarked. */
1983
1984tree
1985dfs_marked_real_bases_queue_p (binfo, data)
1986 tree binfo;
1987 void *data;
1988{
6d4c57a0
NS
1989 if (TREE_VIA_VIRTUAL (binfo))
1990 {
1991 tree type = (tree) data;
1992
1993 if (TREE_CODE (type) == TREE_LIST)
1994 type = TREE_PURPOSE (type);
1995 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
1996 }
1997 return markedp (binfo, NULL);
99a6c6f4
MM
1998}
1999
dd42e135
MM
2000/* A queue function that skips all virtual bases (and their
2001 bases). */
2002
2003tree
2004dfs_skip_vbases (binfo, data)
2005 tree binfo;
2006 void *data ATTRIBUTE_UNUSED;
2007{
2008 if (TREE_VIA_VIRTUAL (binfo))
2009 return NULL_TREE;
2010
2011 return binfo;
2012}
2013
99a6c6f4
MM
2014/* Called via dfs_walk from dfs_get_pure_virtuals. */
2015
2016static tree
2017dfs_get_pure_virtuals (binfo, data)
2018 tree binfo;
2019 void *data;
2020{
174eceea
MM
2021 tree type = (tree) data;
2022
99a6c6f4
MM
2023 /* We're not interested in primary base classes; the derived class
2024 of which they are a primary base will contain the information we
2025 need. */
9965d119 2026 if (!BINFO_PRIMARY_P (binfo))
8926095f 2027 {
07b7a812 2028 tree virtuals;
99a6c6f4 2029
da3d4dfa 2030 for (virtuals = BINFO_VIRTUALS (binfo);
99a6c6f4
MM
2031 virtuals;
2032 virtuals = TREE_CHAIN (virtuals))
31f8e4f3 2033 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
99a6c6f4 2034 CLASSTYPE_PURE_VIRTUALS (type)
31f8e4f3 2035 = tree_cons (NULL_TREE, BV_FN (virtuals),
99a6c6f4
MM
2036 CLASSTYPE_PURE_VIRTUALS (type));
2037 }
8026246f
MM
2038
2039 SET_BINFO_MARKED (binfo);
8d08fdba 2040
99a6c6f4 2041 return NULL_TREE;
8926095f
MS
2042}
2043
fee7654e 2044/* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
e92cc029 2045
fee7654e
MM
2046void
2047get_pure_virtuals (type)
8926095f
MS
2048 tree type;
2049{
f30432d7 2050 tree vbases;
8926095f 2051
99a6c6f4
MM
2052 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2053 is going to be overridden. */
2054 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
99a6c6f4
MM
2055 /* Now, run through all the bases which are not primary bases, and
2056 collect the pure virtual functions. We look at the vtable in
2057 each class to determine what pure virtual functions are present.
2058 (A primary base is not interesting because the derived class of
2059 which it is a primary base will contain vtable entries for the
2060 pure virtuals in the base class. */
174eceea
MM
2061 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2062 dfs_unmarked_real_bases_queue_p, type);
2063 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2064 dfs_marked_real_bases_queue_p, type);
8026246f 2065
99a6c6f4
MM
2066 /* Put the pure virtuals in dfs order. */
2067 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2068
174eceea
MM
2069 for (vbases = CLASSTYPE_VBASECLASSES (type);
2070 vbases;
2071 vbases = TREE_CHAIN (vbases))
8d08fdba 2072 {
174eceea 2073 tree virtuals;
8d08fdba 2074
a55583e9 2075 for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
174eceea
MM
2076 virtuals;
2077 virtuals = TREE_CHAIN (virtuals))
8d08fdba 2078 {
31f8e4f3 2079 tree base_fndecl = BV_FN (virtuals);
8ebeee52 2080 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
33bd39a2 2081 error ("`%#D' needs a final overrider", base_fndecl);
8d08fdba
MS
2082 }
2083 }
8d08fdba 2084}
8d08fdba
MS
2085\f
2086/* DEPTH-FIRST SEARCH ROUTINES. */
2087
8026246f
MM
2088tree
2089markedp (binfo, data)
d6479fe7
MM
2090 tree binfo;
2091 void *data ATTRIBUTE_UNUSED;
2092{
2093 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2094}
2095
8026246f 2096tree
d6479fe7
MM
2097unmarkedp (binfo, data)
2098 tree binfo;
2099 void *data ATTRIBUTE_UNUSED;
2100{
2101 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2102}
5566b478 2103
da3d4dfa 2104tree
d6479fe7
MM
2105marked_vtable_pathp (binfo, data)
2106 tree binfo;
2107 void *data ATTRIBUTE_UNUSED;
2108{
2109 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2110}
2111
da3d4dfa 2112tree
d6479fe7
MM
2113unmarked_vtable_pathp (binfo, data)
2114 tree binfo;
2115 void *data ATTRIBUTE_UNUSED;
2116{
2117 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2118}
2119
d6479fe7
MM
2120static tree
2121marked_pushdecls_p (binfo, data)
2122 tree binfo;
2123 void *data ATTRIBUTE_UNUSED;
2124{
856216bb
MM
2125 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2126 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
d6479fe7 2127}
5566b478 2128
d6479fe7
MM
2129static tree
2130unmarked_pushdecls_p (binfo, data)
2131 tree binfo;
2132 void *data ATTRIBUTE_UNUSED;
2133{
856216bb
MM
2134 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2135 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
d6479fe7 2136}
8d08fdba 2137
8d08fdba
MS
2138/* The worker functions for `dfs_walk'. These do not need to
2139 test anything (vis a vis marking) if they are paired with
2140 a predicate function (above). */
2141
d6479fe7
MM
2142tree
2143dfs_unmark (binfo, data)
2144 tree binfo;
2145 void *data ATTRIBUTE_UNUSED;
2146{
2147 CLEAR_BINFO_MARKED (binfo);
2148 return NULL_TREE;
2149}
8d08fdba 2150
8d08fdba
MS
2151/* get virtual base class types.
2152 This adds type to the vbase_types list in reverse dfs order.
2153 Ordering is very important, so don't change it. */
2154
d6479fe7
MM
2155static tree
2156dfs_get_vbase_types (binfo, data)
8d08fdba 2157 tree binfo;
d6479fe7 2158 void *data;
8d08fdba 2159{
4b3b5328 2160 tree type = (tree) data;
d6479fe7 2161
c35cce41 2162 if (TREE_VIA_VIRTUAL (binfo))
a55583e9
MM
2163 CLASSTYPE_VBASECLASSES (type)
2164 = tree_cons (BINFO_TYPE (binfo),
2165 binfo,
2166 CLASSTYPE_VBASECLASSES (type));
8d08fdba 2167 SET_BINFO_MARKED (binfo);
d6479fe7 2168 return NULL_TREE;
8d08fdba
MS
2169}
2170
c35cce41
MM
2171/* Called via dfs_walk from mark_primary_bases. Builds the
2172 inheritance graph order list of BINFOs. */
2173
2174static tree
2175dfs_build_inheritance_graph_order (binfo, data)
2176 tree binfo;
2177 void *data;
2178{
2179 tree *last_binfo = (tree *) data;
2180
2181 if (*last_binfo)
2182 TREE_CHAIN (*last_binfo) = binfo;
2183 *last_binfo = binfo;
2184 SET_BINFO_MARKED (binfo);
2185 return NULL_TREE;
2186}
2187
4b3b5328 2188/* Set CLASSTYPE_VBASECLASSES for TYPE. */
e92cc029 2189
23381155 2190void
8d08fdba
MS
2191get_vbase_types (type)
2192 tree type;
2193{
c35cce41
MM
2194 tree last_binfo;
2195
8026246f 2196 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
4b3b5328 2197 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
8d08fdba
MS
2198 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2199 reverse it so that we get normal dfs ordering. */
8026246f 2200 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
c35cce41
MM
2201 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2202 /* Thread the BINFOs in inheritance-graph order. */
2203 last_binfo = NULL;
2204 dfs_walk_real (TYPE_BINFO (type),
2205 dfs_build_inheritance_graph_order,
2206 NULL,
2207 unmarkedp,
2208 &last_binfo);
2209 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
8d08fdba 2210}
dd42e135
MM
2211
2212/* Called from find_vbase_instance via dfs_walk. */
2213
2214static tree
2215dfs_find_vbase_instance (binfo, data)
2216 tree binfo;
2217 void *data;
2218{
2219 tree base = TREE_VALUE ((tree) data);
2220
9965d119 2221 if (BINFO_PRIMARY_P (binfo)
dd42e135
MM
2222 && same_type_p (BINFO_TYPE (binfo), base))
2223 return binfo;
2224
2225 return NULL_TREE;
2226}
2227
2228/* Find the real occurrence of the virtual BASE (a class type) in the
2229 hierarchy dominated by TYPE. */
2230
2231tree
2232find_vbase_instance (base, type)
2233 tree base;
2234 tree type;
2235{
2236 tree instance;
2237
a55583e9 2238 instance = binfo_for_vbase (base, type);
9965d119 2239 if (!BINFO_PRIMARY_P (instance))
dd42e135
MM
2240 return instance;
2241
2242 return dfs_walk (TYPE_BINFO (type),
2243 dfs_find_vbase_instance,
2244 NULL,
2245 build_tree_list (type, base));
2246}
2247
8d08fdba 2248\f
ae673f14
JM
2249/* Debug info for C++ classes can get very large; try to avoid
2250 emitting it everywhere.
2251
50e159f6
JM
2252 Note that this optimization wins even when the target supports
2253 BINCL (if only slightly), and reduces the amount of work for the
2254 linker. */
ae673f14
JM
2255
2256void
2257maybe_suppress_debug_info (t)
2258 tree t;
2259{
50e159f6
JM
2260 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2261 does not support name references between translation units. It supports
2262 symbolic references between translation units, but only within a single
2263 executable or shared library.
2264
2265 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2266 that the type was never defined, so we only get the members we
2267 actually define. */
2268 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
ae673f14
JM
2269 return;
2270
50e159f6
JM
2271 /* We might have set this earlier in cp_finish_decl. */
2272 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2273
ae673f14
JM
2274 /* If we already know how we're handling this class, handle debug info
2275 the same way. */
3ae18eaf
JM
2276 if (CLASSTYPE_INTERFACE_KNOWN (t))
2277 {
2278 if (CLASSTYPE_INTERFACE_ONLY (t))
2279 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2280 /* else don't set it. */
2281 }
bbd15aac
MM
2282 /* If the class has a vtable, write out the debug info along with
2283 the vtable. */
2284 else if (TYPE_CONTAINS_VPTR_P (t))
ae673f14
JM
2285 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2286
2287 /* Otherwise, just emit the debug info normally. */
2288}
2289
6db20143
JM
2290/* Note that we want debugging information for a base class of a class
2291 whose vtable is being emitted. Normally, this would happen because
2292 calling the constructor for a derived class implies calling the
2293 constructors for all bases, which involve initializing the
2294 appropriate vptr with the vtable for the base class; but in the
2295 presence of optimization, this initialization may be optimized
2296 away, so we tell finish_vtable_vardecl that we want the debugging
2297 information anyway. */
2298
2299static tree
2300dfs_debug_mark (binfo, data)
2301 tree binfo;
2302 void *data ATTRIBUTE_UNUSED;
2303{
2304 tree t = BINFO_TYPE (binfo);
2305
2306 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2307
2308 return NULL_TREE;
2309}
2310
2311/* Returns BINFO if we haven't already noted that we want debugging
2312 info for this base class. */
2313
2314static tree
2315dfs_debug_unmarkedp (binfo, data)
2316 tree binfo;
2317 void *data ATTRIBUTE_UNUSED;
2318{
2319 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2320 ? binfo : NULL_TREE);
2321}
ae673f14 2322
6db20143
JM
2323/* Write out the debugging information for TYPE, whose vtable is being
2324 emitted. Also walk through our bases and note that we want to
2325 write out information for them. This avoids the problem of not
2326 writing any debug info for intermediate basetypes whose
2327 constructors, and thus the references to their vtables, and thus
2328 the vtables themselves, were optimized away. */
8d08fdba
MS
2329
2330void
2331note_debug_info_needed (type)
2332 tree type;
2333{
15f1a795
JM
2334 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2335 {
2336 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2337 rest_of_type_compilation (type, toplevel_bindings_p ());
2338 }
d2e5ee5c 2339
d6479fe7 2340 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
8d08fdba
MS
2341}
2342\f
2343/* Subroutines of push_class_decls (). */
2344
c1def683
JM
2345/* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2346 because it (or one of the intermediate bases) depends on template parms. */
2347
2348static int
2349dependent_base_p (binfo)
2350 tree binfo;
2351{
2352 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2353 {
d6479fe7 2354 if (currently_open_class (TREE_TYPE (binfo)))
c1def683
JM
2355 break;
2356 if (uses_template_parms (TREE_TYPE (binfo)))
2357 return 1;
2358 }
2359 return 0;
2360}
2361
8f032717
MM
2362static void
2363setup_class_bindings (name, type_binding_p)
2364 tree name;
2365 int type_binding_p;
8d08fdba 2366{
8f032717
MM
2367 tree type_binding = NULL_TREE;
2368 tree value_binding;
c1def683 2369
8f032717
MM
2370 /* If we've already done the lookup for this declaration, we're
2371 done. */
2372 if (IDENTIFIER_CLASS_VALUE (name))
2373 return;
8d08fdba 2374
8f032717
MM
2375 /* First, deal with the type binding. */
2376 if (type_binding_p)
8d08fdba 2377 {
8f032717
MM
2378 type_binding = lookup_member (current_class_type, name,
2379 /*protect=*/2,
2380 /*want_type=*/1);
2381 if (TREE_CODE (type_binding) == TREE_LIST
aa65d1a2 2382 && TREE_TYPE (type_binding) == error_mark_node)
8f032717 2383 /* NAME is ambiguous. */
aa65d1a2 2384 push_class_level_binding (name, type_binding);
8f032717
MM
2385 else
2386 pushdecl_class_level (type_binding);
8d08fdba
MS
2387 }
2388
8f032717
MM
2389 /* Now, do the value binding. */
2390 value_binding = lookup_member (current_class_type, name,
2391 /*protect=*/2,
2392 /*want_type=*/0);
2393
2394 if (type_binding_p
2395 && (TREE_CODE (value_binding) == TYPE_DECL
8152c320 2396 || DECL_CLASS_TEMPLATE_P (value_binding)
8f032717 2397 || (TREE_CODE (value_binding) == TREE_LIST
aa65d1a2
MM
2398 && TREE_TYPE (value_binding) == error_mark_node
2399 && (TREE_CODE (TREE_VALUE (value_binding))
8f032717
MM
2400 == TYPE_DECL))))
2401 /* We found a type-binding, even when looking for a non-type
2402 binding. This means that we already processed this binding
8152c320 2403 above. */;
dfe2b0b3 2404 else if (value_binding)
8d08fdba 2405 {
8f032717 2406 if (TREE_CODE (value_binding) == TREE_LIST
aa65d1a2 2407 && TREE_TYPE (value_binding) == error_mark_node)
8f032717 2408 /* NAME is ambiguous. */
aa65d1a2 2409 push_class_level_binding (name, value_binding);
8f032717 2410 else
8d08fdba 2411 {
aa65d1a2 2412 if (BASELINK_P (value_binding))
8f032717
MM
2413 /* NAME is some overloaded functions. */
2414 value_binding = TREE_VALUE (value_binding);
2415 pushdecl_class_level (value_binding);
2416 }
2417 }
2418}
f30432d7 2419
8f032717
MM
2420/* Push class-level declarations for any names appearing in BINFO that
2421 are TYPE_DECLS. */
7ddedda4 2422
8f032717
MM
2423static tree
2424dfs_push_type_decls (binfo, data)
2425 tree binfo;
2426 void *data ATTRIBUTE_UNUSED;
2427{
2428 tree type;
2429 tree fields;
f30432d7 2430
8f032717
MM
2431 type = BINFO_TYPE (binfo);
2432 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2433 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
908c4e83
MM
2434 && !(!same_type_p (type, current_class_type)
2435 && template_self_reference_p (type, fields)))
8f032717 2436 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
0ec57017
JM
2437
2438 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2439 DERIVED_FROM_P, which calls get_base_distance. */
2440 SET_BINFO_PUSHDECLS_MARKED (binfo);
8f032717 2441
d6479fe7 2442 return NULL_TREE;
8d08fdba
MS
2443}
2444
8f032717
MM
2445/* Push class-level declarations for any names appearing in BINFO that
2446 are not TYPE_DECLS. */
e92cc029 2447
d6479fe7 2448static tree
8f032717 2449dfs_push_decls (binfo, data)
8d08fdba 2450 tree binfo;
8f032717 2451 void *data;
8d08fdba 2452{
8f032717
MM
2453 tree type;
2454 tree method_vec;
2455 int dep_base_p;
8d08fdba 2456
8f032717
MM
2457 type = BINFO_TYPE (binfo);
2458 dep_base_p = (processing_template_decl && type != current_class_type
2459 && dependent_base_p (binfo));
2460 if (!dep_base_p)
8d08fdba 2461 {
8f032717
MM
2462 tree fields;
2463 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2464 if (DECL_NAME (fields)
2465 && TREE_CODE (fields) != TYPE_DECL
2466 && TREE_CODE (fields) != USING_DECL)
2467 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
2468 else if (TREE_CODE (fields) == FIELD_DECL
6bdb8141 2469 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
8f032717
MM
2470 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
2471
2472 method_vec = (CLASS_TYPE_P (type)
2473 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
2474 if (method_vec)
8d08fdba 2475 {
8f032717
MM
2476 tree *methods;
2477 tree *end;
2478
2479 /* Farm out constructors and destructors. */
2480 end = TREE_VEC_END (method_vec);
2481
2482 for (methods = &TREE_VEC_ELT (method_vec, 2);
2483 *methods && methods != end;
2484 methods++)
2485 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
2486 /*type_binding_p=*/0);
8d08fdba
MS
2487 }
2488 }
8f032717 2489
0ec57017 2490 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
d6479fe7
MM
2491
2492 return NULL_TREE;
8d08fdba
MS
2493}
2494
2495/* When entering the scope of a class, we cache all of the
2496 fields that that class provides within its inheritance
2497 lattice. Where ambiguities result, we mark them
2498 with `error_mark_node' so that if they are encountered
2499 without explicit qualification, we can emit an error
45537677 2500 message. */
e92cc029 2501
8d08fdba 2502void
45537677 2503push_class_decls (type)
8d08fdba
MS
2504 tree type;
2505{
8d08fdba
MS
2506 search_stack = push_search_level (search_stack, &search_obstack);
2507
aa65d1a2 2508 /* Enter type declarations and mark. */
8f032717 2509 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
8d08fdba 2510
aa65d1a2 2511 /* Enter non-type declarations and unmark. */
8f032717 2512 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
8d08fdba
MS
2513}
2514
2515/* Here's a subroutine we need because C lacks lambdas. */
e92cc029 2516
d6479fe7
MM
2517static tree
2518dfs_unuse_fields (binfo, data)
8d08fdba 2519 tree binfo;
d6479fe7 2520 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
2521{
2522 tree type = TREE_TYPE (binfo);
2523 tree fields;
2524
2525 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2526 {
2527 if (TREE_CODE (fields) != FIELD_DECL)
2528 continue;
2529
2530 TREE_USED (fields) = 0;
2531 if (DECL_NAME (fields) == NULL_TREE
6bdb8141 2532 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
8d08fdba
MS
2533 unuse_fields (TREE_TYPE (fields));
2534 }
d6479fe7
MM
2535
2536 return NULL_TREE;
8d08fdba
MS
2537}
2538
2539void
2540unuse_fields (type)
2541 tree type;
2542{
d6479fe7 2543 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
8d08fdba
MS
2544}
2545
2546void
5566b478 2547pop_class_decls ()
8d08fdba
MS
2548{
2549 /* We haven't pushed a search level when dealing with cached classes,
2550 so we'd better not try to pop it. */
2551 if (search_stack)
2552 search_stack = pop_search_level (search_stack);
2553}
2554
8d08fdba
MS
2555void
2556print_search_statistics ()
2557{
2558#ifdef GATHER_STATISTICS
8d08fdba
MS
2559 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2560 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2561 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2562 n_outer_fields_searched, n_calls_lookup_fnfields);
2563 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
fc378698 2564#else /* GATHER_STATISTICS */
8d08fdba 2565 fprintf (stderr, "no search statistics\n");
fc378698 2566#endif /* GATHER_STATISTICS */
8d08fdba
MS
2567}
2568
2569void
2570init_search_processing ()
2571{
2572 gcc_obstack_init (&search_obstack);
8d08fdba
MS
2573}
2574
2575void
2576reinit_search_statistics ()
2577{
5566b478 2578#ifdef GATHER_STATISTICS
8d08fdba
MS
2579 n_fields_searched = 0;
2580 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2581 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2582 n_calls_get_base_type = 0;
2583 n_outer_fields_searched = 0;
2584 n_contexts_saved = 0;
fc378698 2585#endif /* GATHER_STATISTICS */
8d08fdba 2586}
e1cd6e56 2587
72c4a2a6 2588static tree
7d4bdeed 2589add_conversions (binfo, data)
e1cd6e56 2590 tree binfo;
7d4bdeed 2591 void *data;
e1cd6e56 2592{
72b7eeff 2593 int i;
fc378698 2594 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
7d4bdeed 2595 tree *conversions = (tree *) data;
72b7eeff 2596
a7a64a77
MM
2597 /* Some builtin types have no method vector, not even an empty one. */
2598 if (!method_vec)
2599 return NULL_TREE;
2600
fc378698 2601 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
72b7eeff 2602 {
fc378698 2603 tree tmp = TREE_VEC_ELT (method_vec, i);
37b6eb34 2604 tree name;
61a127b3 2605
aa45967f 2606 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
72b7eeff 2607 break;
72c4a2a6 2608
37b6eb34 2609 name = DECL_NAME (OVL_CURRENT (tmp));
59e76fc6 2610
72c4a2a6 2611 /* Make sure we don't already have this conversion. */
37b6eb34 2612 if (! IDENTIFIER_MARKED (name))
72c4a2a6 2613 {
e1b3e07d 2614 *conversions = tree_cons (binfo, tmp, *conversions);
37b6eb34 2615 IDENTIFIER_MARKED (name) = 1;
72c4a2a6 2616 }
72b7eeff 2617 }
72c4a2a6 2618 return NULL_TREE;
e1cd6e56
MS
2619}
2620
27b8d0cd
MM
2621/* Return a TREE_LIST containing all the non-hidden user-defined
2622 conversion functions for TYPE (and its base-classes). The
2623 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2624 containing the conversion functions. The TREE_PURPOSE is the BINFO
2625 from which the conversion functions in this node were selected. */
2626
e1cd6e56
MS
2627tree
2628lookup_conversions (type)
2629 tree type;
2630{
72c4a2a6 2631 tree t;
7d4bdeed 2632 tree conversions = NULL_TREE;
72c4a2a6 2633
d0f062fb 2634 if (COMPLETE_TYPE_P (type))
d6479fe7 2635 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
72c4a2a6
JM
2636
2637 for (t = conversions; t; t = TREE_CHAIN (t))
37b6eb34 2638 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
72c4a2a6 2639
e1cd6e56
MS
2640 return conversions;
2641}
6467930b 2642
d6479fe7
MM
2643struct overlap_info
2644{
2645 tree compare_type;
2646 int found_overlap;
2647};
2648
732dcb6f
JM
2649/* Check whether the empty class indicated by EMPTY_BINFO is also present
2650 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
2651
d6479fe7
MM
2652static tree
2653dfs_check_overlap (empty_binfo, data)
732dcb6f 2654 tree empty_binfo;
d6479fe7 2655 void *data;
732dcb6f 2656{
d6479fe7 2657 struct overlap_info *oi = (struct overlap_info *) data;
732dcb6f 2658 tree binfo;
d6479fe7
MM
2659 for (binfo = TYPE_BINFO (oi->compare_type);
2660 ;
2661 binfo = BINFO_BASETYPE (binfo, 0))
732dcb6f
JM
2662 {
2663 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
2664 {
d6479fe7 2665 oi->found_overlap = 1;
732dcb6f
JM
2666 break;
2667 }
2668 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
2669 break;
2670 }
d6479fe7
MM
2671
2672 return NULL_TREE;
732dcb6f
JM
2673}
2674
2675/* Trivial function to stop base traversal when we find something. */
2676
d6479fe7
MM
2677static tree
2678dfs_no_overlap_yet (binfo, data)
2679 tree binfo;
2680 void *data;
732dcb6f 2681{
d6479fe7
MM
2682 struct overlap_info *oi = (struct overlap_info *) data;
2683 return !oi->found_overlap ? binfo : NULL_TREE;
732dcb6f
JM
2684}
2685
2686/* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2687 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
2688
2689int
2690types_overlap_p (empty_type, next_type)
2691 tree empty_type, next_type;
2692{
d6479fe7
MM
2693 struct overlap_info oi;
2694
732dcb6f
JM
2695 if (! IS_AGGR_TYPE (next_type))
2696 return 0;
d6479fe7
MM
2697 oi.compare_type = next_type;
2698 oi.found_overlap = 0;
2699 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
2700 dfs_no_overlap_yet, &oi);
2701 return oi.found_overlap;
2702}
2703
6f18f7e9
HPN
2704/* Given a vtable VAR, determine which of the inherited classes the vtable
2705 inherits (in a loose sense) functions from.
ae673f14 2706
6f18f7e9 2707 FIXME: This does not work with the new ABI. */
a1dd0d36
JM
2708
2709tree
d6479fe7
MM
2710binfo_for_vtable (var)
2711 tree var;
a1dd0d36 2712{
6f18f7e9
HPN
2713 tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
2714 tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
2715 int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
2716 int i;
2717
2718 for (i = 0; i < n_baseclasses; i++)
2719 {
2720 tree base_binfo = TREE_VEC_ELT (binfos, i);
2721 if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
2722 return base_binfo;
2723 }
d6479fe7 2724
6f18f7e9
HPN
2725 /* If no secondary base classes matched, return the primary base, if
2726 there is one. */
2727 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
2728 return get_primary_binfo (main_binfo);
5430acc8 2729
6f18f7e9 2730 return main_binfo;
a1dd0d36 2731}
6ad07332 2732
9965d119
NS
2733/* Returns the binfo of the first direct or indirect virtual base derived
2734 from BINFO, or NULL if binfo is not via virtual. */
6ad07332 2735
f9825168 2736tree
6ad07332
JM
2737binfo_from_vbase (binfo)
2738 tree binfo;
2739{
2740 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2741 {
2742 if (TREE_VIA_VIRTUAL (binfo))
f9825168 2743 return binfo;
6ad07332 2744 }
f9825168 2745 return NULL_TREE;
6ad07332 2746}
a55583e9 2747
9965d119
NS
2748/* Returns the binfo of the first direct or indirect virtual base derived
2749 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2750 via virtual. */
2751
2752tree
2753binfo_via_virtual (binfo, limit)
2754 tree binfo;
2755 tree limit;
2756{
2757 for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
2758 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2759 {
2760 if (TREE_VIA_VIRTUAL (binfo))
2761 return binfo;
2762 }
2763 return NULL_TREE;
2764}
2765
a55583e9
MM
2766/* Returns the BINFO (if any) for the virtual baseclass T of the class
2767 C from the CLASSTYPE_VBASECLASSES list. */
2768
2769tree
2770binfo_for_vbase (basetype, classtype)
2771 tree basetype;
2772 tree classtype;
2773{
2774 tree binfo;
2775
2776 binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
2777 return binfo ? TREE_VALUE (binfo) : NULL_TREE;
2778}
This page took 1.481317 seconds and 5 git commands to generate.