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