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