]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/search.c
search.c (binfo_for_vtable): Use CLASSTYPE_VFIELD_PARENT.
[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++.
0dd3962d 3 Copyright (C) 1987, 89, 92-97, 1998, 1999 Free Software Foundation, Inc.
8d08fdba
MS
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba 22
e92cc029 23/* High-level class interface. */
8d08fdba
MS
24
25#include "config.h"
8d052bc7 26#include "system.h"
e7a587ef 27#include "tree.h"
8d08fdba
MS
28#include "cp-tree.h"
29#include "obstack.h"
30#include "flags.h"
43f2999d 31#include "rtl.h"
e8abc66f 32#include "output.h"
54f92bfb 33#include "toplev.h"
8d08fdba
MS
34
35#define obstack_chunk_alloc xmalloc
36#define obstack_chunk_free free
37
8d08fdba
MS
38extern struct obstack *current_obstack;
39
40#include "stack.h"
41
42/* Obstack used for remembering decision points of breadth-first. */
e92cc029 43
8d08fdba
MS
44static struct obstack search_obstack;
45
46/* Methods for pushing and popping objects to and from obstacks. */
e92cc029 47
8d08fdba
MS
48struct stack_level *
49push_stack_level (obstack, tp, size)
50 struct obstack *obstack;
51 char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
52 int size;
53{
54 struct stack_level *stack;
55 obstack_grow (obstack, tp, size);
56 stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
57 obstack_finish (obstack);
58 stack->obstack = obstack;
59 stack->first = (tree *) obstack_base (obstack);
60 stack->limit = obstack_room (obstack) / sizeof (tree *);
61 return stack;
62}
63
64struct stack_level *
65pop_stack_level (stack)
66 struct stack_level *stack;
67{
68 struct stack_level *tem = stack;
69 struct obstack *obstack = tem->obstack;
70 stack = tem->prev;
71 obstack_free (obstack, tem);
72 return stack;
73}
74
75#define search_level stack_level
76static struct search_level *search_stack;
77
49c249e1 78static tree get_abstract_virtuals_1 PROTO((tree, int, tree));
9e0781b5 79static tree next_baselink PROTO((tree));
49c249e1
JM
80static tree get_vbase_1 PROTO((tree, tree, unsigned int *));
81static tree convert_pointer_to_vbase PROTO((tree, tree));
82static tree lookup_field_1 PROTO((tree, tree));
83static tree convert_pointer_to_single_level PROTO((tree, tree));
49c249e1
JM
84static int lookup_fnfields_here PROTO((tree, tree));
85static int is_subobject_of_p PROTO((tree, tree));
86static int hides PROTO((tree, tree));
87static tree virtual_context PROTO((tree, tree, tree));
d6479fe7
MM
88static tree dfs_check_overlap PROTO((tree, void *));
89static tree dfs_no_overlap_yet PROTO((tree, void *));
49c249e1 90static int get_base_distance_recursive
514a1f18 91 PROTO((tree, int, int, int, int *, tree *, tree,
49c249e1
JM
92 int, int *, int, int));
93static void expand_upcast_fixups
94 PROTO((tree, tree, tree, tree, tree, tree, tree *));
95static void fixup_virtual_upcast_offsets
96 PROTO((tree, tree, int, int, tree, tree, tree, tree,
97 tree *));
d6479fe7
MM
98static tree unmarkedp PROTO((tree, void *));
99static tree marked_vtable_pathp PROTO((tree, void *));
100static tree unmarked_vtable_pathp PROTO((tree, void *));
101static tree marked_new_vtablep PROTO((tree, void *));
102static tree unmarked_new_vtablep PROTO((tree, void *));
103static tree marked_pushdecls_p PROTO((tree, void *));
104static tree unmarked_pushdecls_p PROTO((tree, void *));
105static tree dfs_debug_unmarkedp PROTO((tree, void *));
106static tree dfs_debug_mark PROTO((tree, void *));
107static tree dfs_find_vbases PROTO((tree, void *));
108static tree dfs_clear_vbase_slots PROTO((tree, void *));
109static tree dfs_init_vbase_pointers PROTO((tree, void *));
110static tree dfs_get_vbase_types PROTO((tree, void *));
8f032717
MM
111static tree dfs_push_type_decls PROTO((tree, void *));
112static tree dfs_push_decls PROTO((tree, void *));
d6479fe7 113static tree dfs_unuse_fields PROTO((tree, void *));
7d4bdeed 114static tree add_conversions PROTO((tree, void *));
d6479fe7 115static tree get_virtuals_named_this PROTO((tree, tree));
7d4bdeed 116static tree get_virtual_destructor PROTO((tree, void *));
d6479fe7 117static tree tree_has_any_destructor_p PROTO((tree, void *));
c6160f8f 118static int covariant_return_p PROTO((tree, tree));
4cc1d462 119static int check_final_overrider PROTO((tree, tree));
49c249e1
JM
120static struct search_level *push_search_level
121 PROTO((struct stack_level *, struct obstack *));
122static struct search_level *pop_search_level
123 PROTO((struct stack_level *));
d6479fe7
MM
124static tree bfs_walk
125 PROTO((tree, tree (*) (tree, void *), tree (*) (tree, void *),
126 void *));
127static tree lookup_field_queue_p PROTO((tree, void *));
7d4bdeed 128static tree lookup_field_r PROTO((tree, void *));
d6479fe7
MM
129static tree dfs_walk_real PROTO ((tree,
130 tree (*) (tree, void *),
131 tree (*) (tree, void *),
132 tree (*) (tree, void *),
133 void *));
d6479fe7
MM
134static tree get_virtuals_named_this_r PROTO ((tree, void *));
135static tree context_for_name_lookup PROTO ((tree));
136static tree canonical_binfo PROTO ((tree));
137static tree shared_marked_p PROTO ((tree, void *));
138static tree shared_unmarked_p PROTO ((tree, void *));
139static int dependent_base_p PROTO ((tree));
140static tree dfs_accessible_queue_p PROTO ((tree, void *));
141static tree dfs_accessible_p PROTO ((tree, void *));
142static tree dfs_access_in_type PROTO ((tree, void *));
143static tree access_in_type PROTO ((tree, tree));
6cbd257e 144static tree dfs_canonical_queue PROTO ((tree, void *));
1e61a9d9
MM
145static tree dfs_assert_unmarked_p PROTO ((tree, void *));
146static void assert_canonical_unmarked PROTO ((tree));
6a629cac
MM
147static int protected_accessible_p PROTO ((tree, tree, tree, tree));
148static int friend_accessible_p PROTO ((tree, tree, tree, tree));
8f032717
MM
149static void setup_class_bindings PROTO ((tree, int));
150static int template_self_reference_p PROTO ((tree, tree));
8d08fdba
MS
151
152/* Allocate a level of searching. */
e92cc029 153
8d08fdba
MS
154static struct search_level *
155push_search_level (stack, obstack)
156 struct stack_level *stack;
157 struct obstack *obstack;
158{
159 struct search_level tem;
160
161 tem.prev = stack;
162 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
163}
164
165/* Discard a level of search allocation. */
e92cc029 166
8d08fdba
MS
167static struct search_level *
168pop_search_level (obstack)
169 struct stack_level *obstack;
170{
171 register struct search_level *stack = pop_stack_level (obstack);
172
173 return stack;
174}
175\f
8d08fdba 176/* Variables for gathering statistics. */
5566b478 177#ifdef GATHER_STATISTICS
8d08fdba
MS
178static int n_fields_searched;
179static int n_calls_lookup_field, n_calls_lookup_field_1;
180static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
181static int n_calls_get_base_type;
182static int n_outer_fields_searched;
183static int n_contexts_saved;
fc378698 184#endif /* GATHER_STATISTICS */
8d08fdba 185
8d08fdba 186\f
acc9fe20
RK
187/* Get a virtual binfo that is found inside BINFO's hierarchy that is
188 the same type as the type given in PARENT. To be optimal, we want
189 the first one that is found by going through the least number of
514a1f18
JM
190 virtual bases.
191
192 This uses a clever algorithm that updates *depth when we find the vbase,
193 and cuts off other paths of search when they reach that depth. */
e92cc029 194
acc9fe20 195static tree
bd6dd845 196get_vbase_1 (parent, binfo, depth)
acc9fe20
RK
197 tree parent, binfo;
198 unsigned int *depth;
199{
200 tree binfos;
201 int i, n_baselinks;
202 tree rval = NULL_TREE;
203
acc9fe20
RK
204 if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo))
205 {
206 *depth = 0;
207 return binfo;
208 }
209
210 *depth = *depth - 1;
211
212 binfos = BINFO_BASETYPES (binfo);
213 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
214
215 /* Process base types. */
216 for (i = 0; i < n_baselinks; i++)
217 {
218 tree base_binfo = TREE_VEC_ELT (binfos, i);
219 tree nrval;
220
221 if (*depth == 0)
222 break;
223
bd6dd845 224 nrval = get_vbase_1 (parent, base_binfo, depth);
acc9fe20
RK
225 if (nrval)
226 rval = nrval;
227 }
228 *depth = *depth+1;
229 return rval;
230}
231
514a1f18
JM
232/* Return the shortest path to vbase PARENT within BINFO, ignoring
233 access and ambiguity. */
234
bd6dd845
MS
235tree
236get_vbase (parent, binfo)
237 tree parent;
238 tree binfo;
239{
240 unsigned int d = (unsigned int)-1;
241 return get_vbase_1 (parent, binfo, &d);
242}
243
f30432d7
MS
244/* Convert EXPR to a virtual base class of type TYPE. We know that
245 EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that
246 the type of what expr points to has a virtual base of type TYPE. */
e92cc029 247
bd6dd845 248static tree
f30432d7
MS
249convert_pointer_to_vbase (type, expr)
250 tree type;
251 tree expr;
252{
bd6dd845 253 tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))));
f30432d7
MS
254 return convert_pointer_to_real (vb, expr);
255}
256
8d08fdba
MS
257/* Check whether the type given in BINFO is derived from PARENT. If
258 it isn't, return 0. If it is, but the derivation is MI-ambiguous
259 AND protect != 0, emit an error message and return error_mark_node.
260
261 Otherwise, if TYPE is derived from PARENT, return the actual base
262 information, unless a one of the protection violations below
263 occurs, in which case emit an error message and return error_mark_node.
264
265 If PROTECT is 1, then check if access to a public field of PARENT
266 would be private. Also check for ambiguity. */
267
268tree
269get_binfo (parent, binfo, protect)
270 register tree parent, binfo;
271 int protect;
272{
a703fb38 273 tree type = NULL_TREE;
8d08fdba
MS
274 int dist;
275 tree rval = NULL_TREE;
276
277 if (TREE_CODE (parent) == TREE_VEC)
278 parent = BINFO_TYPE (parent);
71851aaa 279 else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
8d08fdba
MS
280 my_friendly_abort (89);
281
282 if (TREE_CODE (binfo) == TREE_VEC)
283 type = BINFO_TYPE (binfo);
71851aaa 284 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
8d08fdba
MS
285 type = binfo;
286 else
287 my_friendly_abort (90);
288
289 dist = get_base_distance (parent, binfo, protect, &rval);
290
291 if (dist == -3)
292 {
8251199e 293 cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
8d08fdba
MS
294 parent, type);
295 return error_mark_node;
296 }
297 else if (dist == -2 && protect)
298 {
8251199e 299 cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
8d08fdba
MS
300 type);
301 return error_mark_node;
302 }
303
304 return rval;
305}
306
307/* This is the newer depth first get_base_distance routine. */
e92cc029 308
8926095f 309static int
5566b478 310get_base_distance_recursive (binfo, depth, is_private, rval,
514a1f18 311 rval_private_ptr, new_binfo_ptr, parent,
6b5fbb55
MS
312 protect, via_virtual_ptr, via_virtual,
313 current_scope_in_chain)
314 tree binfo;
315 int depth, is_private, rval;
316 int *rval_private_ptr;
514a1f18 317 tree *new_binfo_ptr, parent;
6b5fbb55
MS
318 int protect, *via_virtual_ptr, via_virtual;
319 int current_scope_in_chain;
8d08fdba
MS
320{
321 tree binfos;
322 int i, n_baselinks;
323
6b5fbb55
MS
324 if (protect
325 && !current_scope_in_chain
326 && is_friend (BINFO_TYPE (binfo), current_scope ()))
327 current_scope_in_chain = 1;
328
8d08fdba
MS
329 if (BINFO_TYPE (binfo) == parent || binfo == parent)
330 {
514a1f18
JM
331 int better = 0;
332
8d08fdba 333 if (rval == -1)
514a1f18
JM
334 /* This is the first time we've found parent. */
335 better = 1;
336 else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
337 BINFO_OFFSET (binfo))
338 && *via_virtual_ptr && via_virtual)
339 {
340 /* A new path to the same vbase. If this one has better
341 access or is shorter, take it. */
342
343 if (protect)
344 better = *rval_private_ptr - is_private;
345 if (better == 0)
346 better = rval - depth;
347 }
348 else
349 {
350 /* Ambiguous base class. */
351 rval = depth = -2;
352
353 /* If we get an ambiguity between virtual and non-virtual base
354 class, return the non-virtual in case we are ignoring
355 ambiguity. */
356 better = *via_virtual_ptr - via_virtual;
357 }
358
359 if (better > 0)
8d08fdba
MS
360 {
361 rval = depth;
362 *rval_private_ptr = is_private;
363 *new_binfo_ptr = binfo;
364 *via_virtual_ptr = via_virtual;
365 }
514a1f18 366
8d08fdba
MS
367 return rval;
368 }
369
370 binfos = BINFO_BASETYPES (binfo);
371 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
372 depth += 1;
373
374 /* Process base types. */
375 for (i = 0; i < n_baselinks; i++)
376 {
377 tree base_binfo = TREE_VEC_ELT (binfos, i);
378
514a1f18
JM
379 int via_private
380 = (protect
381 && (is_private
382 || (!TREE_VIA_PUBLIC (base_binfo)
383 && !(TREE_VIA_PROTECTED (base_binfo)
384 && current_scope_in_chain)
385 && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
386 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
387
388 rval = get_base_distance_recursive (base_binfo, depth, via_private,
389 rval, rval_private_ptr,
390 new_binfo_ptr, parent,
391 protect, via_virtual_ptr,
392 this_virtual,
393 current_scope_in_chain);
394
395 /* If we've found a non-virtual, ambiguous base class, we don't need
396 to keep searching. */
397 if (rval == -2 && *via_virtual_ptr == 0)
398 return rval;
8d08fdba
MS
399 }
400
401 return rval;
402}
403
404/* Return the number of levels between type PARENT and the type given
405 in BINFO, following the leftmost path to PARENT not found along a
406 virtual path, if there are no real PARENTs (all come from virtual
514a1f18 407 base classes), then follow the shortest public path to PARENT.
8d08fdba
MS
408
409 Return -1 if TYPE is not derived from PARENT.
410 Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
411 non-negative.
412 Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
413
414 If PATH_PTR is non-NULL, then also build the list of types
ddd5a7c1 415 from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
8d08fdba
MS
416 set.
417
418 PARENT can also be a binfo, in which case that exact parent is found
419 and no other. convert_pointer_to_real uses this functionality.
420
39211cd5 421 If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */
8d08fdba
MS
422
423int
424get_base_distance (parent, binfo, protect, path_ptr)
425 register tree parent, binfo;
426 int protect;
427 tree *path_ptr;
428{
8d08fdba 429 int rval;
8d08fdba 430 int rval_private = 0;
a703fb38 431 tree type = NULL_TREE;
8d08fdba
MS
432 tree new_binfo = NULL_TREE;
433 int via_virtual;
434 int watch_access = protect;
435
5566b478 436 /* Should we be completing types here? */
8d08fdba 437 if (TREE_CODE (parent) != TREE_VEC)
5566b478
MS
438 parent = complete_type (TYPE_MAIN_VARIANT (parent));
439 else
440 complete_type (TREE_TYPE (parent));
8d08fdba
MS
441
442 if (TREE_CODE (binfo) == TREE_VEC)
443 type = BINFO_TYPE (binfo);
f0e01782 444 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
8d08fdba 445 {
e92cc029 446 type = complete_type (binfo);
8d08fdba
MS
447 binfo = TYPE_BINFO (type);
448
449 if (path_ptr)
dfbcd65a
JM
450 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE,
451 980827);
8d08fdba
MS
452 }
453 else
454 my_friendly_abort (92);
455
456 if (parent == type || parent == binfo)
457 {
458 /* If the distance is 0, then we don't really need
459 a path pointer, but we shouldn't let garbage go back. */
460 if (path_ptr)
461 *path_ptr = binfo;
462 return 0;
463 }
464
465 if (path_ptr)
466 watch_access = 1;
467
5566b478 468 rval = get_base_distance_recursive (binfo, 0, 0, -1,
8d08fdba 469 &rval_private, &new_binfo, parent,
514a1f18 470 watch_access, &via_virtual, 0,
6b5fbb55 471 0);
8d08fdba 472
8d08fdba
MS
473 /* Access restrictions don't count if we found an ambiguous basetype. */
474 if (rval == -2 && protect >= 0)
475 rval_private = 0;
476
477 if (rval && protect && rval_private)
478 return -3;
479
514a1f18 480 /* If they gave us the real vbase binfo, which isn't in the main binfo
18141e4c
JM
481 tree, deal with it. This happens when we are called from
482 expand_upcast_fixups. */
39211cd5
MS
483 if (rval == -1 && TREE_CODE (parent) == TREE_VEC
484 && parent == binfo_member (BINFO_TYPE (parent),
485 CLASSTYPE_VBASECLASSES (type)))
486 {
dfbcd65a 487 my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent) == binfo, 980827);
39211cd5
MS
488 new_binfo = parent;
489 rval = 1;
490 }
491
8d08fdba
MS
492 if (path_ptr)
493 *path_ptr = new_binfo;
494 return rval;
495}
496
497/* Search for a member with name NAME in a multiple inheritance lattice
498 specified by TYPE. If it does not exist, return NULL_TREE.
499 If the member is ambiguously referenced, return `error_mark_node'.
500 Otherwise, return the FIELD_DECL. */
501
502/* Do a 1-level search for NAME as a member of TYPE. The caller must
503 figure out whether it can access this field. (Since it is only one
504 level, this is reasonable.) */
e92cc029 505
8d08fdba
MS
506static tree
507lookup_field_1 (type, name)
508 tree type, name;
509{
f84b4be9
JM
510 register tree field;
511
512 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
513 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
514 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all;
515 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
516 the code often worked even when we treated the index as a list
517 of fields!) */
518 return NULL_TREE;
519
f90cdf34
MT
520 if (TYPE_NAME (type)
521 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
522 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
523 {
524 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
525 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
526 int i;
527
528 while (lo < hi)
529 {
530 i = (lo + hi) / 2;
531
532#ifdef GATHER_STATISTICS
533 n_fields_searched++;
534#endif /* GATHER_STATISTICS */
535
536 if (DECL_NAME (fields[i]) > name)
537 hi = i;
538 else if (DECL_NAME (fields[i]) < name)
539 lo = i + 1;
540 else
541 return fields[i];
542 }
543 return NULL_TREE;
544 }
545
f84b4be9 546 field = TYPE_FIELDS (type);
8d08fdba
MS
547
548#ifdef GATHER_STATISTICS
549 n_calls_lookup_field_1++;
fc378698 550#endif /* GATHER_STATISTICS */
8d08fdba
MS
551 while (field)
552 {
553#ifdef GATHER_STATISTICS
554 n_fields_searched++;
fc378698 555#endif /* GATHER_STATISTICS */
f84b4be9 556 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field)) == 'd', 0);
8d08fdba 557 if (DECL_NAME (field) == NULL_TREE
6bdb8141 558 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8d08fdba
MS
559 {
560 tree temp = lookup_field_1 (TREE_TYPE (field), name);
561 if (temp)
562 return temp;
563 }
2036a15c
MM
564 if (TREE_CODE (field) == USING_DECL)
565 /* For now, we're just treating member using declarations as
566 old ARM-style access declarations. Thus, there's no reason
567 to return a USING_DECL, and the rest of the compiler can't
568 handle it. Once the class is defined, these are purged
569 from TYPE_FIELDS anyhow; see handle_using_decl. */
570 ;
571 else if (DECL_NAME (field) == name)
8d08fdba
MS
572 {
573 if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
574 && DECL_ASSEMBLER_NAME (field) != NULL)
575 GNU_xref_ref(current_function_decl,
576 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
577 return field;
578 }
579 field = TREE_CHAIN (field);
580 }
581 /* Not found. */
9cd64686 582 if (name == vptr_identifier)
8d08fdba
MS
583 {
584 /* Give the user what s/he thinks s/he wants. */
585 if (TYPE_VIRTUAL_P (type))
586 return CLASSTYPE_VFIELD (type);
587 }
588 return NULL_TREE;
589}
590
7177d104
MS
591/* There are a number of cases we need to be aware of here:
592 current_class_type current_function_decl
e92cc029
MS
593 global NULL NULL
594 fn-local NULL SET
595 class-local SET NULL
596 class->fn SET SET
597 fn->class SET SET
7177d104
MS
598
599 Those last two make life interesting. If we're in a function which is
600 itself inside a class, we need decls to go into the fn's decls (our
601 second case below). But if we're in a class and the class itself is
602 inside a function, we need decls to go into the decls for the class. To
4ac14744 603 achieve this last goal, we must see if, when both current_class_ptr and
7177d104
MS
604 current_function_decl are set, the class was declared inside that
605 function. If so, we know to put the decls into the class's scope. */
606
8d08fdba
MS
607tree
608current_scope ()
609{
610 if (current_function_decl == NULL_TREE)
611 return current_class_type;
612 if (current_class_type == NULL_TREE)
613 return current_function_decl;
614 if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type)
615 return current_function_decl;
616
617 return current_class_type;
618}
619
9188c363
MM
620/* Returns non-zero if we are currently in a function scope. Note
621 that this function returns zero if we are within a local class, but
622 not within a member function body of the local class. */
623
624int
625at_function_scope_p ()
626{
627 tree cs = current_scope ();
628 return cs && TREE_CODE (cs) == FUNCTION_DECL;
629}
630
d6479fe7 631/* Return the scope of DECL, as appropriate when doing name-lookup. */
8d08fdba 632
d6479fe7
MM
633static tree
634context_for_name_lookup (decl)
635 tree decl;
636{
637 /* [class.union]
638
639 For the purposes of name lookup, after the anonymous union
640 definition, the members of the anonymous union are considered to
641 have been defined in the scope in which teh anonymous union is
642 declared. */
643 tree context = DECL_REAL_CONTEXT (decl);
644
6bdb8141 645 while (TYPE_P (context) && ANON_AGGR_TYPE_P (context))
d6479fe7
MM
646 context = TYPE_CONTEXT (context);
647 if (!context)
648 context = global_namespace;
8d08fdba 649
d6479fe7
MM
650 return context;
651}
8d08fdba 652
d6479fe7
MM
653/* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
654 otherwise. */
8d08fdba 655
d6479fe7
MM
656static tree
657canonical_binfo (binfo)
658 tree binfo;
659{
660 return (TREE_VIA_VIRTUAL (binfo)
661 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
662}
8d08fdba 663
6cbd257e
MM
664/* A queue function that simply ensures that we walk into the
665 canonical versions of virtual bases. */
666
667static tree
668dfs_canonical_queue (binfo, data)
669 tree binfo;
670 void *data ATTRIBUTE_UNUSED;
671{
672 return canonical_binfo (binfo);
673}
674
675/* Called via dfs_walk from assert_canonical_unmarked. */
676
677static tree
678dfs_assert_unmarked_p (binfo, data)
679 tree binfo;
680 void *data ATTRIBUTE_UNUSED;
681{
682 my_friendly_assert (!BINFO_MARKED (binfo), 0);
683 return NULL_TREE;
684}
685
686/* Asserts that all the nodes below BINFO (using the canonical
687 versions of virtual bases) are unmarked. */
688
689static void
690assert_canonical_unmarked (binfo)
691 tree binfo;
692{
693 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
694}
695
d6479fe7
MM
696/* If BINFO is marked, return a canonical version of BINFO.
697 Otherwise, return NULL_TREE. */
8d08fdba 698
d6479fe7
MM
699static tree
700shared_marked_p (binfo, data)
701 tree binfo;
702 void *data;
703{
704 binfo = canonical_binfo (binfo);
705 return markedp (binfo, data) ? binfo : NULL_TREE;
706}
8d08fdba 707
d6479fe7
MM
708/* If BINFO is not marked, return a canonical version of BINFO.
709 Otherwise, return NULL_TREE. */
8d08fdba 710
d6479fe7
MM
711static tree
712shared_unmarked_p (binfo, data)
713 tree binfo;
714 void *data;
8d08fdba 715{
d6479fe7
MM
716 binfo = canonical_binfo (binfo);
717 return unmarkedp (binfo, data) ? binfo : NULL_TREE;
718}
8d08fdba 719
d6479fe7
MM
720/* Called from access_in_type via dfs_walk. Calculate the access to
721 DATA (which is really a DECL) in BINFO. */
eae89e04 722
d6479fe7
MM
723static tree
724dfs_access_in_type (binfo, data)
725 tree binfo;
726 void *data;
727{
728 tree decl = (tree) data;
729 tree type = BINFO_TYPE (binfo);
730 tree access = NULL_TREE;
8d08fdba 731
d6479fe7 732 if (context_for_name_lookup (decl) == type)
8d08fdba 733 {
d6479fe7
MM
734 /* If we have desceneded to the scope of DECL, just note the
735 appropriate access. */
736 if (TREE_PRIVATE (decl))
737 access = access_private_node;
738 else if (TREE_PROTECTED (decl))
739 access = access_protected_node;
740 else
741 access = access_public_node;
8d08fdba 742 }
d6479fe7
MM
743 else
744 {
745 /* First, check for an access-declaration that gives us more
746 access to the DECL. The CONST_DECL for an enumeration
747 constant will not have DECL_LANG_SPECIFIC, and thus no
748 DECL_ACCESS. */
749 if (DECL_LANG_SPECIFIC (decl))
750 {
751 access = purpose_member (type, DECL_ACCESS (decl));
752 if (access)
753 access = TREE_VALUE (access);
754 }
755
756 if (!access)
757 {
758 int i;
759 int n_baselinks;
760 tree binfos;
761
762 /* Otherwise, scan our baseclasses, and pick the most favorable
763 access. */
764 binfos = BINFO_BASETYPES (binfo);
765 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
766 for (i = 0; i < n_baselinks; ++i)
767 {
768 tree base_binfo = TREE_VEC_ELT (binfos, i);
769 tree base_access = TREE_CHAIN (canonical_binfo (base_binfo));
770
771 if (!base_access || base_access == access_private_node)
772 /* If it was not accessible in the base, or only
773 accessible as a private member, we can't access it
774 all. */
775 base_access = NULL_TREE;
776 else if (TREE_VIA_PROTECTED (base_binfo))
777 /* Public and protected members in the base are
778 protected here. */
779 base_access = access_protected_node;
780 else if (!TREE_VIA_PUBLIC (base_binfo))
781 /* Public and protected members in the base are
782 private here. */
783 base_access = access_private_node;
784
785 /* See if the new access, via this base, gives more
786 access than our previous best access. */
787 if (base_access &&
788 (base_access == access_public_node
789 || (base_access == access_protected_node
790 && access != access_public_node)
791 || (base_access == access_private_node
792 && !access)))
793 {
794 access = base_access;
8d08fdba 795
d6479fe7
MM
796 /* If the new access is public, we can't do better. */
797 if (access == access_public_node)
798 break;
799 }
800 }
801 }
802 }
faae18ab 803
d6479fe7
MM
804 /* Note the access to DECL in TYPE. */
805 TREE_CHAIN (binfo) = access;
02020185 806
d6479fe7
MM
807 /* Mark TYPE as visited so that if we reach it again we do not
808 duplicate our efforts here. */
809 SET_BINFO_MARKED (binfo);
8d08fdba 810
d6479fe7
MM
811 return NULL_TREE;
812}
8d08fdba 813
d6479fe7 814/* Return the access to DECL in TYPE. */
8d08fdba 815
d6479fe7
MM
816static tree
817access_in_type (type, decl)
818 tree type;
819 tree decl;
820{
821 tree binfo = TYPE_BINFO (type);
8d08fdba 822
d6479fe7 823 /* We must take into account
8d08fdba 824
d6479fe7 825 [class.paths]
8d08fdba 826
d6479fe7
MM
827 If a name can be reached by several paths through a multiple
828 inheritance graph, the access is that of the path that gives
829 most access.
8d08fdba 830
d6479fe7
MM
831 The algorithm we use is to make a post-order depth-first traversal
832 of the base-class hierarchy. As we come up the tree, we annotate
833 each node with the most lenient access. */
834 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
835 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
6cbd257e 836 assert_canonical_unmarked (binfo);
8d08fdba 837
d6479fe7
MM
838 return TREE_CHAIN (binfo);
839}
840
841/* Called from dfs_accessible_p via dfs_walk. */
842
843static tree
844dfs_accessible_queue_p (binfo, data)
845 tree binfo;
846 void *data ATTRIBUTE_UNUSED;
847{
848 if (BINFO_MARKED (binfo))
849 return NULL_TREE;
850
851 /* If this class is inherited via private or protected inheritance,
852 then we can't see it, unless we are a friend of the subclass. */
853 if (!TREE_VIA_PUBLIC (binfo)
854 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
855 current_scope ()))
856 return NULL_TREE;
857
858 return canonical_binfo (binfo);
859}
860
861/* Called from dfs_accessible_p via dfs_walk. */
862
863static tree
864dfs_accessible_p (binfo, data)
865 tree binfo;
866 void *data;
867{
868 int protected_ok = data != 0;
869 tree access;
870
871 /* We marked the binfos while computing the access in each type.
872 So, we unmark as we go now. */
873 SET_BINFO_MARKED (binfo);
874
875 access = TREE_CHAIN (binfo);
876 if (access == access_public_node
877 || (access == access_protected_node && protected_ok))
878 return binfo;
879 else if (access && is_friend (BINFO_TYPE (binfo), current_scope ()))
880 return binfo;
881
882 return NULL_TREE;
883}
884
6a629cac
MM
885/* Returns non-zero if it is OK to access DECL when named in TYPE
886 through an object indiated by BINFO in the context of DERIVED. */
887
888static int
889protected_accessible_p (type, decl, derived, binfo)
890 tree type;
891 tree decl;
892 tree derived;
893 tree binfo;
894{
895 tree access;
896
897 /* We're checking this clause from [class.access.base]
898
899 m as a member of N is protected, and the reference occurs in a
900 member or friend of class N, or in a member or friend of a
901 class P derived from N, where m as a member of P is private or
902 protected.
903
904 If DERIVED isn't derived from TYPE, then it certainly does not
905 apply. */
906 if (!DERIVED_FROM_P (type, derived))
907 return 0;
908
909 access = access_in_type (derived, decl);
910 if (same_type_p (derived, type))
911 {
912 if (access != access_private_node)
913 return 0;
914 }
915 else if (access != access_private_node
916 && access != access_protected_node)
917 return 0;
918
919 /* [class.protected]
920
921 When a friend or a member function of a derived class references
922 a protected nonstatic member of a base class, an access check
923 applies in addition to those described earlier in clause
924 _class.access_.4) Except when forming a pointer to member
925 (_expr.unary.op_), the access must be through a pointer to,
926 reference to, or object of the derived class itself (or any class
927 derived from that class) (_expr.ref_). If the access is to form
928 a pointer to member, the nested-name-specifier shall name the
929 derived class (or any class derived from that class). */
930 if (DECL_NONSTATIC_MEMBER_P (decl))
931 {
932 /* We can tell through what the reference is occurring by
933 chasing BINFO up to the root. */
934 tree t = binfo;
935 while (BINFO_INHERITANCE_CHAIN (t))
936 t = BINFO_INHERITANCE_CHAIN (t);
937
938 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
939 return 0;
940 }
941
942 return 1;
943}
944
945/* Returns non-zero if SCOPE is a friend of a type which would be able
946 to acces DECL, named in TYPE, through the object indicated by
947 BINFO. */
948
949static int
950friend_accessible_p (scope, type, decl, binfo)
951 tree scope;
952 tree type;
953 tree decl;
954 tree binfo;
955{
956 tree befriending_classes;
957 tree t;
958
959 if (!scope)
960 return 0;
961
962 if (TREE_CODE (scope) == FUNCTION_DECL
963 || DECL_FUNCTION_TEMPLATE_P (scope))
964 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
965 else if (TYPE_P (scope))
966 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
967 else
968 return 0;
969
970 for (t = befriending_classes; t; t = TREE_CHAIN (t))
971 if (protected_accessible_p (type, decl, TREE_VALUE (t), binfo))
972 return 1;
973
974 if (TREE_CODE (scope) == FUNCTION_DECL
975 || DECL_FUNCTION_TEMPLATE_P (scope))
976 {
977 /* Perhaps this SCOPE is a member of a class which is a
978 friend. */
979 if (friend_accessible_p (DECL_CLASS_CONTEXT (scope), type,
980 decl, binfo))
981 return 1;
982
983 /* Or an instantiation of something which is a friend. */
984 if (DECL_TEMPLATE_INFO (scope))
985 return friend_accessible_p (DECL_TI_TEMPLATE (scope),
986 type, decl, binfo);
987 }
988 else if (CLASSTYPE_TEMPLATE_INFO (scope))
989 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope),
990 type, decl, binfo);
991
992 return 0;
993}
994
d6479fe7
MM
995/* DECL is a declaration from a base class of TYPE, which was the
996 classs used to name DECL. Return non-zero if, in the current
997 context, DECL is accessible. If TYPE is actually a BINFO node,
8084bf81
MM
998 then we can tell in what context the access is occurring by looking
999 at the most derived class along the path indicated by BINFO. */
d6479fe7
MM
1000
1001int
1002accessible_p (type, decl)
1003 tree type;
1004 tree decl;
1005
1006{
d6479fe7
MM
1007 tree binfo;
1008 tree t;
1009
1010 /* Non-zero if it's OK to access DECL if it has protected
1011 accessibility in TYPE. */
1012 int protected_ok = 0;
1013
1014 /* If we're not checking access, everything is accessible. */
1015 if (!flag_access_control)
1016 return 1;
1017
1018 /* If this declaration is in a block or namespace scope, there's no
1019 access control. */
1020 if (!TYPE_P (context_for_name_lookup (decl)))
1021 return 1;
1022
1023 /* We don't do access control for types yet. */
1024 if (TREE_CODE (decl) == TYPE_DECL)
1025 return 1;
1026
1027 if (!TYPE_P (type))
1028 {
1029 binfo = type;
1030 type = BINFO_TYPE (type);
8d08fdba 1031 }
d6479fe7
MM
1032 else
1033 binfo = TYPE_BINFO (type);
1034
1035 /* [class.access.base]
1036
1037 A member m is accessible when named in class N if
1038
1039 --m as a member of N is public, or
8d08fdba 1040
d6479fe7
MM
1041 --m as a member of N is private, and the reference occurs in a
1042 member or friend of class N, or
8d08fdba 1043
d6479fe7
MM
1044 --m as a member of N is protected, and the reference occurs in a
1045 member or friend of class N, or in a member or friend of a
1046 class P derived from N, where m as a member of P is private or
1047 protected, or
1048
1049 --there exists a base class B of N that is accessible at the point
1050 of reference, and m is accessible when named in class B.
1051
1052 We walk the base class hierarchy, checking these conditions. */
1053
1054 /* Figure out where the reference is occurring. Check to see if
1055 DECL is private or protected in this scope, since that will
1056 determine whether protected access in TYPE allowed. */
6a629cac
MM
1057 if (current_class_type)
1058 protected_ok
1059 = protected_accessible_p (type, decl, current_class_type,
1060 binfo);
8d08fdba 1061
6a629cac
MM
1062 /* Now, loop through the classes of which we are a friend. */
1063 if (!protected_ok)
1064 protected_ok = friend_accessible_p (current_scope (),
1065 type, decl, binfo);
8d08fdba 1066
d6479fe7
MM
1067 /* Standardize on the same that will access_in_type will use. We
1068 don't need to know what path was chosen from this point onwards. */
1069 binfo = TYPE_BINFO (type);
1070
1071 /* Compute the accessibility of DECL in the class hierarchy
1072 dominated by type. */
1073 access_in_type (type, decl);
1074 /* Walk the hierarchy again, looking for a base class that allows
1075 access. */
1076 t = dfs_walk (binfo, dfs_accessible_p,
1077 dfs_accessible_queue_p,
1078 protected_ok ? &protected_ok : 0);
6cbd257e
MM
1079 /* Clear any mark bits. Note that we have to walk the whole tree
1080 here, since we have aborted the previous walk from some point
1081 deep in the tree. */
1082 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1083 assert_canonical_unmarked (binfo);
d6479fe7
MM
1084
1085 return t != NULL_TREE;
8d08fdba
MS
1086}
1087
1088/* Routine to see if the sub-object denoted by the binfo PARENT can be
1089 found as a base class and sub-object of the object denoted by
1090 BINFO. This routine relies upon binfos not being shared, except
1091 for binfos for virtual bases. */
e92cc029 1092
8d08fdba
MS
1093static int
1094is_subobject_of_p (parent, binfo)
1095 tree parent, binfo;
1096{
d2675b98
MM
1097 tree binfos;
1098 int i, n_baselinks;
8d08fdba 1099
86eab6f6
MM
1100 /* We want to canonicalize for comparison purposes. But, when we
1101 iterate through basetypes later, we want the binfos from the
1102 original hierarchy. That's why we have to calculate BINFOS
1103 first, and then canonicalize. */
1104 binfos = BINFO_BASETYPES (binfo);
d2675b98
MM
1105 parent = canonical_binfo (parent);
1106 binfo = canonical_binfo (binfo);
d6479fe7 1107
8d08fdba
MS
1108 if (parent == binfo)
1109 return 1;
1110
d2675b98
MM
1111 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1112
8d08fdba
MS
1113 /* Process and/or queue base types. */
1114 for (i = 0; i < n_baselinks; i++)
1115 {
d2675b98
MM
1116 tree base_binfo = TREE_VEC_ELT (binfos, i);
1117 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo)))
1118 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1119 class there's no way to descend into it. */
1120 continue;
1121
8d08fdba
MS
1122 if (is_subobject_of_p (parent, base_binfo))
1123 return 1;
1124 }
1125 return 0;
1126}
1127
1128/* See if a one FIELD_DECL hides another. This routine is meant to
1129 correspond to ANSI working paper Sept 17, 1992 10p4. The two
1130 binfos given are the binfos corresponding to the particular places
1131 the FIELD_DECLs are found. This routine relies upon binfos not
e92cc029
MS
1132 being shared, except for virtual bases. */
1133
8d08fdba
MS
1134static int
1135hides (hider_binfo, hidee_binfo)
1136 tree hider_binfo, hidee_binfo;
1137{
1138 /* hider hides hidee, if hider has hidee as a base class and
1139 the instance of hidee is a sub-object of hider. The first
1140 part is always true is the second part is true.
1141
1142 When hider and hidee are the same (two ways to get to the exact
e92cc029 1143 same member) we consider either one as hiding the other. */
8d08fdba
MS
1144 return is_subobject_of_p (hidee_binfo, hider_binfo);
1145}
1146
1147/* Very similar to lookup_fnfields_1 but it ensures that at least one
1148 function was declared inside the class given by TYPE. It really should
1149 only return functions that match the given TYPE. */
e92cc029 1150
8d08fdba
MS
1151static int
1152lookup_fnfields_here (type, name)
1153 tree type, name;
1154{
e92cc029 1155 int idx = lookup_fnfields_1 (type, name);
8d08fdba
MS
1156 tree fndecls;
1157
fc378698 1158 /* ctors and dtors are always only in the right class. */
e92cc029
MS
1159 if (idx <= 1)
1160 return idx;
1161 fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
8d08fdba
MS
1162 while (fndecls)
1163 {
2c73f9f5 1164 if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls)))
8d08fdba 1165 == TYPE_MAIN_VARIANT (type))
e92cc029 1166 return idx;
2c73f9f5 1167 fndecls = OVL_CHAIN (fndecls);
8d08fdba
MS
1168 }
1169 return -1;
1170}
1171
7d4bdeed 1172struct lookup_field_info {
d6479fe7
MM
1173 /* The type in which we're looking. */
1174 tree type;
7d4bdeed
MM
1175 /* The name of the field for which we're looking. */
1176 tree name;
1177 /* If non-NULL, the current result of the lookup. */
1178 tree rval;
1179 /* The path to RVAL. */
1180 tree rval_binfo;
d6479fe7
MM
1181 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1182 candidates. */
7d4bdeed 1183 tree ambiguous;
7d4bdeed
MM
1184 /* If non-zero, we are looking for types, not data members. */
1185 int want_type;
d6479fe7
MM
1186 /* If non-zero, RVAL was found by looking through a dependent base. */
1187 int from_dep_base_p;
7d4bdeed 1188 /* If something went wrong, a message indicating what. */
d8e178a0 1189 const char *errstr;
7d4bdeed
MM
1190};
1191
1192/* Returns non-zero if BINFO is not hidden by the value found by the
1193 lookup so far. If BINFO is hidden, then there's no need to look in
1194 it. DATA is really a struct lookup_field_info. Called from
1195 lookup_field via breadth_first_search. */
1196
d6479fe7 1197static tree
7d4bdeed
MM
1198lookup_field_queue_p (binfo, data)
1199 tree binfo;
1200 void *data;
1201{
1202 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
d6479fe7
MM
1203
1204 /* Don't look for constructors or destructors in base classes. */
1205 if (lfi->name == ctor_identifier || lfi->name == dtor_identifier)
1206 return NULL_TREE;
1207
1208 /* If this base class is hidden by the best-known value so far, we
1209 don't need to look. */
1210 if (!lfi->from_dep_base_p && lfi->rval_binfo
1211 && hides (lfi->rval_binfo, binfo))
1212 return NULL_TREE;
1213
1214 if (TREE_VIA_VIRTUAL (binfo))
1215 return binfo_member (BINFO_TYPE (binfo),
1216 CLASSTYPE_VBASECLASSES (lfi->type));
1217 else
1218 return binfo;
7d4bdeed
MM
1219}
1220
9188c363
MM
1221/* Within the scope of a template class, you can refer to the to the
1222 current specialization with the name of the template itself. For
1223 example:
8f032717
MM
1224
1225 template <typename T> struct S { S* sp; }
1226
1227 Returns non-zero if DECL is such a declaration in a class TYPE. */
1228
1229static int
1230template_self_reference_p (type, decl)
1231 tree type;
1232 tree decl;
1233{
1234 return (CLASSTYPE_USE_TEMPLATE (type)
3fc5037b 1235 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
8f032717
MM
1236 && TREE_CODE (decl) == TYPE_DECL
1237 && DECL_ARTIFICIAL (decl)
1238 && DECL_NAME (decl) == constructor_name (type));
1239}
1240
7d4bdeed
MM
1241/* DATA is really a struct lookup_field_info. Look for a field with
1242 the name indicated there in BINFO. If this function returns a
1243 non-NULL value it is the result of the lookup. Called from
1244 lookup_field via breadth_first_search. */
1245
1246static tree
1247lookup_field_r (binfo, data)
1248 tree binfo;
1249 void *data;
1250{
1251 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1252 tree type = BINFO_TYPE (binfo);
4bb0968f 1253 tree nval = NULL_TREE;
d6479fe7 1254 int from_dep_base_p;
7d4bdeed 1255
d6479fe7
MM
1256 /* First, look for a function. There can't be a function and a data
1257 member with the same name, and if there's a function and a type
1258 with the same name, the type is hidden by the function. */
4bb0968f
MM
1259 if (!lfi->want_type)
1260 {
1261 int idx = lookup_fnfields_here (type, lfi->name);
1262 if (idx >= 0)
1263 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1264 }
1265
1266 if (!nval)
d6479fe7
MM
1267 /* Look for a data member or type. */
1268 nval = lookup_field_1 (type, lfi->name);
1269
1270 /* If there is no declaration with the indicated name in this type,
1271 then there's nothing to do. */
7d4bdeed 1272 if (!nval)
d6479fe7 1273 return NULL_TREE;
7d4bdeed 1274
4bb0968f
MM
1275 /* If we're looking up a type (as with an elaborated type specifier)
1276 we ignore all non-types we find. */
1277 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL)
1278 {
1279 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1280 if (nval)
1281 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1282 else
1283 return NULL_TREE;
1284 }
1285
8f032717
MM
1286 /* You must name a template base class with a template-id. */
1287 if (!same_type_p (type, lfi->type)
1288 && template_self_reference_p (type, nval))
1289 return NULL_TREE;
1290
d6479fe7
MM
1291 from_dep_base_p = dependent_base_p (binfo);
1292 if (lfi->from_dep_base_p && !from_dep_base_p)
1293 {
1294 /* If the new declaration is not found via a dependent base, and
1295 the old one was, then we must prefer the new one. We weren't
1296 really supposed to be able to find the old one, so we don't
1297 want to be affected by a specialization. Consider:
1298
1299 struct B { typedef int I; };
1300 template <typename T> struct D1 : virtual public B {};
1301 template <typename T> struct D :
1302 public D1, virtual pubic B { I i; };
1303
1304 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1305 D1 is specialized. */
1306 lfi->from_dep_base_p = 0;
1307 lfi->rval = NULL_TREE;
1308 lfi->rval_binfo = NULL_TREE;
1309 lfi->ambiguous = NULL_TREE;
1310 lfi->errstr = 0;
1311 }
1312 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1313 /* Similarly, if the old declaration was not found via a dependent
1314 base, and the new one is, ignore the new one. */
7d4bdeed
MM
1315 return NULL_TREE;
1316
1317 /* If the lookup already found a match, and the new value doesn't
1318 hide the old one, we might have an ambiguity. */
1319 if (lfi->rval_binfo && !hides (binfo, lfi->rval_binfo))
1320 {
d6479fe7 1321 if (nval == lfi->rval && SHARED_MEMBER_P (nval))
7d4bdeed
MM
1322 /* The two things are really the same. */
1323 ;
1324 else if (hides (lfi->rval_binfo, binfo))
1325 /* The previous value hides the new one. */
1326 ;
1327 else
1328 {
1329 /* We have a real ambiguity. We keep a chain of all the
1330 candidates. */
1331 if (!lfi->ambiguous && lfi->rval)
aa65d1a2
MM
1332 {
1333 /* This is the first time we noticed an ambiguity. Add
1334 what we previously thought was a reasonable candidate
1335 to the list. */
1336 lfi->ambiguous = scratch_tree_cons (NULL_TREE, lfi->rval,
1337 NULL_TREE);
1338 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1339 }
1340
7d4bdeed
MM
1341 /* Add the new value. */
1342 lfi->ambiguous = scratch_tree_cons (NULL_TREE, nval,
1343 lfi->ambiguous);
aa65d1a2 1344 TREE_TYPE (lfi->ambiguous) = error_mark_node;
7d4bdeed
MM
1345 lfi->errstr = "request for member `%D' is ambiguous";
1346 }
1347 }
1348 else
1349 {
4bb0968f
MM
1350 /* If the thing we're looking for is a virtual base class, then
1351 we know we've got what we want at this point; there's no way
1352 to get an ambiguity. */
1353 if (VBASE_NAME_P (lfi->name))
7d4bdeed 1354 {
4bb0968f
MM
1355 lfi->rval = nval;
1356 return nval;
7d4bdeed
MM
1357 }
1358
4bb0968f
MM
1359 if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
1360 /* We need to return a member template class so we can
1361 define partial specializations. Is there a better
1362 way? */
1363 && !DECL_CLASS_TEMPLATE_P (nval))
1364 /* The thing we're looking for isn't a type, so the implicit
1365 typename extension doesn't apply, so we just pretend we
1366 didn't find anything. */
1367 return NULL_TREE;
7d4bdeed 1368
d6479fe7
MM
1369 lfi->rval = nval;
1370 lfi->from_dep_base_p = from_dep_base_p;
7d4bdeed
MM
1371 lfi->rval_binfo = binfo;
1372 }
1373
d6479fe7 1374 return NULL_TREE;
7d4bdeed
MM
1375}
1376
d6479fe7 1377/* Look for a memer named NAME in an inheritance lattice dominated by
8f032717
MM
1378 XBASETYPE. PROTECT is 0 or two, we do not check access. If it is
1379 1, we enforce accessibility. If PROTECT is zero, then, for an
1380 ambiguous lookup, we return NULL. If PROTECT is 1, we issue an
aa65d1a2
MM
1381 error message. If PROTECT is 2, we return a TREE_LIST whose
1382 TREEE_TYPE is error_mark_node and whose TREE_VALUEs are the list of
8f032717 1383 ambiguous candidates.
8d08fdba 1384
8f032717
MM
1385 WANT_TYPE is 1 when we should only return TYPE_DECLs, if no
1386 TYPE_DECL can be found return NULL_TREE. */
e92cc029 1387
8d08fdba 1388tree
d6479fe7 1389lookup_member (xbasetype, name, protect, want_type)
8d08fdba
MS
1390 register tree xbasetype, name;
1391 int protect, want_type;
1392{
7d4bdeed
MM
1393 tree rval, rval_binfo = NULL_TREE;
1394 tree type = NULL_TREE, basetype_path = NULL_TREE;
1395 struct lookup_field_info lfi;
8d08fdba
MS
1396
1397 /* rval_binfo is the binfo associated with the found member, note,
1398 this can be set with useful information, even when rval is not
1399 set, because it must deal with ALL members, not just non-function
1400 members. It is used for ambiguity checking and the hidden
1401 checks. Whereas rval is only set if a proper (not hidden)
1402 non-function member is found. */
1403
d8e178a0 1404 const char *errstr = 0;
8d08fdba 1405
de22184b
MS
1406 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1407 && IDENTIFIER_CLASS_VALUE (name))
1408 {
1409 tree field = IDENTIFIER_CLASS_VALUE (name);
1410 if (TREE_CODE (field) != FUNCTION_DECL
1411 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
aa65d1a2
MM
1412 /* We're in the scope of this class, and the value has already
1413 been looked up. Just return the cached value. */
de22184b
MS
1414 return field;
1415 }
1416
8d08fdba
MS
1417 if (TREE_CODE (xbasetype) == TREE_VEC)
1418 {
8d08fdba 1419 type = BINFO_TYPE (xbasetype);
39211cd5 1420 basetype_path = xbasetype;
8d08fdba
MS
1421 }
1422 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
39211cd5 1423 {
238109cd 1424 type = xbasetype;
5566b478 1425 basetype_path = TYPE_BINFO (type);
dfbcd65a
JM
1426 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1427 980827);
39211cd5 1428 }
238109cd
JM
1429 else
1430 my_friendly_abort (97);
1431
1432 complete_type (type);
8d08fdba 1433
8d08fdba
MS
1434#ifdef GATHER_STATISTICS
1435 n_calls_lookup_field++;
fc378698 1436#endif /* GATHER_STATISTICS */
8d08fdba 1437
d8e178a0 1438 bzero ((PTR) &lfi, sizeof (lfi));
d6479fe7 1439 lfi.type = type;
7d4bdeed 1440 lfi.name = name;
7d4bdeed 1441 lfi.want_type = want_type;
d6479fe7 1442 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
7d4bdeed
MM
1443 rval = lfi.rval;
1444 rval_binfo = lfi.rval_binfo;
1445 if (rval_binfo)
1446 type = BINFO_TYPE (rval_binfo);
1447 errstr = lfi.errstr;
1448
1449 /* If we are not interested in ambiguities, don't report them;
1450 just return NULL_TREE. */
1451 if (!protect && lfi.ambiguous)
1452 return NULL_TREE;
d6479fe7 1453
8f032717
MM
1454 if (protect == 2)
1455 {
1456 if (lfi.ambiguous)
aa65d1a2 1457 return lfi.ambiguous;
8f032717
MM
1458 else
1459 protect = 0;
1460 }
1461
d6479fe7
MM
1462 /* [class.access]
1463
1464 In the case of overloaded function names, access control is
1465 applied to the function selected by overloaded resolution. */
1466 if (rval && protect && !is_overloaded_fn (rval)
d6479fe7
MM
1467 && !enforce_access (xbasetype, rval))
1468 return error_mark_node;
9e9ff709 1469
8251199e 1470 if (errstr && protect)
8d08fdba 1471 {
8251199e 1472 cp_error (errstr, name, type);
7d4bdeed
MM
1473 if (lfi.ambiguous)
1474 print_candidates (lfi.ambiguous);
8d08fdba
MS
1475 rval = error_mark_node;
1476 }
b3709d9b 1477
d6479fe7
MM
1478 /* If the thing we found was found via the implicit typename
1479 extension, build the typename type. */
1480 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1481 rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
1482 name, name,
1483 TREE_TYPE (rval)));
1484
4bb0968f
MM
1485 if (rval && is_overloaded_fn (rval))
1486 {
1487 rval = scratch_tree_cons (basetype_path, rval, NULL_TREE);
1488 SET_BASELINK_P (rval);
1489 }
d6479fe7
MM
1490
1491 return rval;
1492}
1493
1494/* Like lookup_member, except that if we find a function member we
1495 return NULL_TREE. */
1496
1497tree
1498lookup_field (xbasetype, name, protect, want_type)
1499 register tree xbasetype, name;
1500 int protect, want_type;
1501{
1502 tree rval = lookup_member (xbasetype, name, protect, want_type);
1503
1504 /* Ignore functions. */
1505 if (rval && TREE_CODE (rval) == TREE_LIST)
1506 return NULL_TREE;
1507
1508 return rval;
1509}
1510
1511/* Like lookup_member, except that if we find a non-function member we
1512 return NULL_TREE. */
1513
1514tree
1515lookup_fnfields (xbasetype, name, protect)
1516 register tree xbasetype, name;
1517 int protect;
1518{
1519 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1520
1521 /* Ignore non-functions. */
1522 if (rval && TREE_CODE (rval) != TREE_LIST)
1523 return NULL_TREE;
1524
8d08fdba
MS
1525 return rval;
1526}
1527
8d08fdba
MS
1528/* TYPE is a class type. Return the index of the fields within
1529 the method vector with name NAME, or -1 is no such field exists. */
e92cc029 1530
03017874 1531int
8d08fdba
MS
1532lookup_fnfields_1 (type, name)
1533 tree type, name;
1534{
f90cdf34 1535 tree method_vec
7ddedda4 1536 = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE;
8d08fdba
MS
1537
1538 if (method_vec != 0)
1539 {
f90cdf34 1540 register int i;
8d08fdba 1541 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
f90cdf34
MT
1542 int len = TREE_VEC_LENGTH (method_vec);
1543 tree tmp;
8d08fdba
MS
1544
1545#ifdef GATHER_STATISTICS
1546 n_calls_lookup_fnfields_1++;
fc378698
MS
1547#endif /* GATHER_STATISTICS */
1548
1549 /* Constructors are first... */
f90cdf34
MT
1550 if (name == ctor_identifier)
1551 return methods[0] ? 0 : -1;
8d08fdba 1552
fc378698 1553 /* and destructors are second. */
f90cdf34
MT
1554 if (name == dtor_identifier)
1555 return methods[1] ? 1 : -1;
fc378698 1556
f90cdf34 1557 for (i = 2; i < len && methods[i]; ++i)
8d08fdba
MS
1558 {
1559#ifdef GATHER_STATISTICS
1560 n_outer_fields_searched++;
fc378698 1561#endif /* GATHER_STATISTICS */
f90cdf34
MT
1562
1563 tmp = OVL_CURRENT (methods[i]);
1564 if (DECL_NAME (tmp) == name)
1565 return i;
1566
1567 /* If the type is complete and we're past the conversion ops,
1568 switch to binary search. */
1569 if (! DECL_CONV_FN_P (tmp)
1570 && TYPE_SIZE (type))
1571 {
1572 int lo = i + 1, hi = len;
1573
1574 while (lo < hi)
1575 {
1576 i = (lo + hi) / 2;
1577
1578#ifdef GATHER_STATISTICS
1579 n_outer_fields_searched++;
1580#endif /* GATHER_STATISTICS */
1581
1582 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1583
1584 if (tmp > name)
1585 hi = i;
1586 else if (tmp < name)
1587 lo = i + 1;
1588 else
1589 return i;
1590 }
1591 break;
1592 }
8d08fdba 1593 }
98c1c668
JM
1594
1595 /* If we didn't find it, it might have been a template
1596 conversion operator. (Note that we don't look for this case
1597 above so that we will always find specializations first.) */
f90cdf34 1598 if (IDENTIFIER_TYPENAME_P (name))
98c1c668 1599 {
f90cdf34 1600 for (i = 2; i < len && methods[i]; ++i)
98c1c668 1601 {
f90cdf34
MT
1602 tmp = OVL_CURRENT (methods[i]);
1603 if (! DECL_CONV_FN_P (tmp))
61a127b3
MM
1604 {
1605 /* Since all conversion operators come first, we know
1606 there is no such operator. */
d6479fe7 1607 break;
8d08fdba 1608 }
f90cdf34
MT
1609 else if (TREE_CODE (tmp) == TEMPLATE_DECL)
1610 return i;
8d08fdba
MS
1611 }
1612 }
8d08fdba
MS
1613 }
1614
d6479fe7 1615 return -1;
d23a1bb1 1616}
8d08fdba 1617\f
d6479fe7
MM
1618/* Walk the class hierarchy dominated by TYPE. FN is called for each
1619 type in the hierarchy, in a breadth-first preorder traversal. .
1620 If it ever returns a non-NULL value, that value is immediately
1621 returned and the walk is terminated. At each node FN, is passed a
1622 BINFO indicating the path from the curently visited base-class to
1623 TYPE. The TREE_CHAINs of the BINFOs may be used for scratch space;
1624 they are otherwise unused. Before each base-class is walked QFN is
1625 called. If the value returned is non-zero, the base-class is
1626 walked; otherwise it is not. If QFN is NULL, it is treated as a
1627 function which always returns 1. Both FN and QFN are passed the
1628 DATA whenever they are called. */
8d08fdba 1629
72c4a2a6 1630static tree
d6479fe7 1631bfs_walk (binfo, fn, qfn, data)
8d08fdba 1632 tree binfo;
d6479fe7
MM
1633 tree (*fn) PROTO((tree, void *));
1634 tree (*qfn) PROTO((tree, void *));
7d4bdeed 1635 void *data;
8d08fdba 1636{
d6479fe7
MM
1637 size_t head;
1638 size_t tail;
72c4a2a6 1639 tree rval = NULL_TREE;
d6479fe7
MM
1640 /* An array of the base classes of BINFO. These will be built up in
1641 breadth-first order, except where QFN prunes the search. */
1642 varray_type bfs_bases;
8d08fdba 1643
d6479fe7
MM
1644 /* Start with enough room for ten base classes. That will be enough
1645 for most hierarchies. */
1646 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
8d08fdba 1647
d6479fe7
MM
1648 /* Put the first type into the stack. */
1649 VARRAY_TREE (bfs_bases, 0) = binfo;
1650 tail = 1;
72c4a2a6 1651
d6479fe7 1652 for (head = 0; head < tail; ++head)
8d08fdba 1653 {
8d08fdba 1654 int i;
d6479fe7
MM
1655 int n_baselinks;
1656 tree binfos;
8d08fdba 1657
7d4bdeed 1658 /* Pull the next type out of the queue. */
d6479fe7 1659 binfo = VARRAY_TREE (bfs_bases, head);
7d4bdeed
MM
1660
1661 /* If this is the one we're looking for, we're done. */
d6479fe7 1662 rval = (*fn) (binfo, data);
7d4bdeed
MM
1663 if (rval)
1664 break;
1665
1666 /* Queue up the base types. */
1667 binfos = BINFO_BASETYPES (binfo);
1668 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
8d08fdba
MS
1669 for (i = 0; i < n_baselinks; i++)
1670 {
1671 tree base_binfo = TREE_VEC_ELT (binfos, i);
1672
d6479fe7
MM
1673 if (qfn)
1674 base_binfo = (*qfn) (base_binfo, data);
7d4bdeed 1675
d6479fe7 1676 if (base_binfo)
8d08fdba 1677 {
d6479fe7
MM
1678 if (tail == VARRAY_SIZE (bfs_bases))
1679 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1680 VARRAY_TREE (bfs_bases, tail) = base_binfo;
72c4a2a6 1681 ++tail;
8d08fdba
MS
1682 }
1683 }
7d4bdeed 1684 }
8d08fdba 1685
d6479fe7
MM
1686 /* Clean up. */
1687 VARRAY_FREE (bfs_bases);
1688
1689 return rval;
1690}
1691
1692/* Exactly like bfs_walk, except that a depth-first traversal is
1693 performed, and PREFN is called in preorder, while POSTFN is called
1694 in postorder. */
1695
1696static tree
1697dfs_walk_real (binfo, prefn, postfn, qfn, data)
1698 tree binfo;
1699 tree (*prefn) PROTO((tree, void *));
1700 tree (*postfn) PROTO((tree, void *));
1701 tree (*qfn) PROTO((tree, void *));
1702 void *data;
1703{
1704 int i;
1705 int n_baselinks;
1706 tree binfos;
1707 tree rval = NULL_TREE;
1708
1709 /* Call the pre-order walking function. */
1710 if (prefn)
7d4bdeed 1711 {
d6479fe7
MM
1712 rval = (*prefn) (binfo, data);
1713 if (rval)
1714 return rval;
8d08fdba 1715 }
8d08fdba 1716
d6479fe7
MM
1717 /* Process the basetypes. */
1718 binfos = BINFO_BASETYPES (binfo);
1719 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1720 for (i = 0; i < n_baselinks; i++)
1721 {
1722 tree base_binfo = TREE_VEC_ELT (binfos, i);
1723
1724 if (qfn)
1725 base_binfo = (*qfn) (base_binfo, data);
1726
1727 if (base_binfo)
1728 {
1729 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1730 if (rval)
1731 return rval;
1732 }
1733 }
1734
1735 /* Call the post-order walking function. */
1736 if (postfn)
1737 rval = (*postfn) (binfo, data);
1738
8d08fdba
MS
1739 return rval;
1740}
1741
d6479fe7
MM
1742/* Exactly like bfs_walk, except that a depth-first post-order traversal is
1743 performed. */
1744
1745tree
1746dfs_walk (binfo, fn, qfn, data)
1747 tree binfo;
1748 tree (*fn) PROTO((tree, void *));
1749 tree (*qfn) PROTO((tree, void *));
1750 void *data;
1751{
1752 return dfs_walk_real (binfo, 0, fn, qfn, data);
1753}
1754
1755struct gvnt_info
1756{
1757 /* The name of the function we are looking for. */
1758 tree name;
1759 /* The overloaded functions we have found. */
1760 tree fields;
1761};
1762
1763/* Called from get_virtuals_named_this via bfs_walk. */
1764
1765static tree
1766get_virtuals_named_this_r (binfo, data)
1767 tree binfo;
1768 void *data;
1769{
1770 struct gvnt_info *gvnti = (struct gvnt_info *) data;
1771 tree type = BINFO_TYPE (binfo);
1772 int idx;
1773
1774 idx = lookup_fnfields_here (BINFO_TYPE (binfo), gvnti->name);
1775 if (idx >= 0)
1776 gvnti->fields
1777 = scratch_tree_cons (binfo,
1778 TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type),
1779 idx),
1780 gvnti->fields);
1781
1782 return NULL_TREE;
1783}
8d08fdba 1784
d6479fe7
MM
1785/* Return the virtual functions with the indicated NAME in the type
1786 indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE
1787 indicates the base class from which the TREE_VALUE (an OVERLOAD or
1788 just a FUNCTION_DECL) originated. */
8d08fdba
MS
1789
1790static tree
d6479fe7 1791get_virtuals_named_this (binfo, name)
8d08fdba 1792 tree binfo;
d6479fe7 1793 tree name;
8d08fdba 1794{
d6479fe7 1795 struct gvnt_info gvnti;
8d08fdba
MS
1796 tree fields;
1797
d6479fe7
MM
1798 gvnti.name = name;
1799 gvnti.fields = NULL_TREE;
8d08fdba 1800
d6479fe7 1801 bfs_walk (binfo, get_virtuals_named_this_r, 0, &gvnti);
8d08fdba
MS
1802
1803 /* Get to the function decls, and return the first virtual function
1804 with this name, if there is one. */
d6479fe7 1805 for (fields = gvnti.fields; fields; fields = next_baselink (fields))
8d08fdba
MS
1806 {
1807 tree fndecl;
1808
2c73f9f5
ML
1809 for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl))
1810 if (DECL_VINDEX (OVL_CURRENT (fndecl)))
8d08fdba 1811 return fields;
8d08fdba
MS
1812 }
1813 return NULL_TREE;
1814}
1815
fc378698 1816static tree
7d4bdeed 1817get_virtual_destructor (binfo, data)
8d08fdba 1818 tree binfo;
d6479fe7 1819 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
1820{
1821 tree type = BINFO_TYPE (binfo);
8d08fdba 1822 if (TYPE_HAS_DESTRUCTOR (type)
fc378698
MS
1823 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1)))
1824 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1);
8d08fdba
MS
1825 return 0;
1826}
1827
d6479fe7 1828static tree
7d4bdeed 1829tree_has_any_destructor_p (binfo, data)
8d08fdba 1830 tree binfo;
d6479fe7 1831 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
1832{
1833 tree type = BINFO_TYPE (binfo);
d6479fe7 1834 return TYPE_NEEDS_DESTRUCTOR (type) ? binfo : NULL_TREE;
8d08fdba
MS
1835}
1836
cc600f33
JM
1837/* Returns > 0 if a function with type DRETTYPE overriding a function
1838 with type BRETTYPE is covariant, as defined in [class.virtual].
1839
1840 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1841 adjustment), or -1 if pedantically invalid covariance. */
1842
c6160f8f 1843static int
cc600f33
JM
1844covariant_return_p (brettype, drettype)
1845 tree brettype, drettype;
1846{
1847 tree binfo;
1848
1849 if (TREE_CODE (brettype) == FUNCTION_DECL
1850 || TREE_CODE (brettype) == THUNK_DECL)
1851 {
1852 brettype = TREE_TYPE (TREE_TYPE (brettype));
1853 drettype = TREE_TYPE (TREE_TYPE (drettype));
1854 }
1855 else if (TREE_CODE (brettype) == METHOD_TYPE)
1856 {
1857 brettype = TREE_TYPE (brettype);
1858 drettype = TREE_TYPE (drettype);
1859 }
1860
3bfdc719 1861 if (same_type_p (brettype, drettype))
cc600f33
JM
1862 return 0;
1863
1864 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1865 && (TREE_CODE (brettype) == POINTER_TYPE
1866 || TREE_CODE (brettype) == REFERENCE_TYPE)
91063b51 1867 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
cc600f33
JM
1868 return 0;
1869
1870 if (! can_convert (brettype, drettype))
1871 return 0;
1872
1873 brettype = TREE_TYPE (brettype);
1874 drettype = TREE_TYPE (drettype);
1875
1876 /* If not pedantic, allow any standard pointer conversion. */
1877 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
1878 return -1;
1879
e76e4a68
JM
1880 binfo = get_binfo (brettype, drettype, 1);
1881
1882 /* If we get an error_mark_node from get_binfo, it already complained,
1883 so let's just succeed. */
1884 if (binfo == error_mark_node)
1885 return 1;
cc600f33
JM
1886
1887 if (! BINFO_OFFSET_ZEROP (binfo) || TREE_VIA_VIRTUAL (binfo))
1888 return 2;
1889 return 1;
1890}
1891
4cc1d462
NS
1892/* Check that virtual overrider OVERRIDER is acceptable for base function
1893 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1894
9c0758dd 1895static int
4cc1d462
NS
1896check_final_overrider (overrider, basefn)
1897 tree overrider, basefn;
1898{
1899 tree over_type = TREE_TYPE (overrider);
1900 tree base_type = TREE_TYPE (basefn);
1901 tree over_return = TREE_TYPE (over_type);
1902 tree base_return = TREE_TYPE (base_type);
1903 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1904 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1905 int i;
1906
1907 if (same_type_p (base_return, over_return))
1908 /* OK */;
1909 else if ((i = covariant_return_p (base_return, over_return)))
1910 {
1911 if (i == 2)
1912 sorry ("adjusting pointers for covariant returns");
1913
1914 if (pedantic && i == -1)
1915 {
1916 cp_pedwarn_at ("invalid covariant return type for `virtual %#D'", overrider);
1917 cp_pedwarn_at (" overriding `virtual %#D' (must be pointer or reference to class)", basefn);
1918 }
1919 }
1920 else if (IS_AGGR_TYPE_2 (base_return, over_return)
1921 && same_or_base_type_p (base_return, over_return))
1922 {
1923 cp_error_at ("invalid covariant return type for `virtual %#D'", overrider);
1924 cp_error_at (" overriding `virtual %#D' (must use pointer or reference)", basefn);
1925 return 0;
1926 }
1927 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)) == NULL_TREE)
1928 {
1929 cp_error_at ("conflicting return type specified for `virtual %#D'", overrider);
1930 cp_error_at (" overriding `virtual %#D'", basefn);
1931 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1932 DECL_CLASS_CONTEXT (overrider));
1933 return 0;
1934 }
1935
1936 /* Check throw specifier is subset. */
1937 /* XXX At the moment, punt on an overriding artificial function. We
1938 don't generate its exception specifier, so can't check it properly. */
1939 if (! DECL_ARTIFICIAL (overrider)
1940 && !comp_except_specs (base_throw, over_throw, 0))
1941 {
1942 cp_error_at ("looser throw specifier for `virtual %#F'", overrider);
1943 cp_error_at (" overriding `virtual %#F'", basefn);
1944 return 0;
1945 }
1946 return 1;
1947}
1948
7177d104
MS
1949/* Given a class type TYPE, and a function decl FNDECL, look for a
1950 virtual function in TYPE's hierarchy which FNDECL could match as a
1951 virtual function. It doesn't matter which one we find.
8d08fdba
MS
1952
1953 DTORP is nonzero if we are looking for a destructor. Destructors
1954 need special treatment because they do not match by name. */
e92cc029 1955
8d08fdba 1956tree
7177d104 1957get_matching_virtual (binfo, fndecl, dtorp)
8d08fdba
MS
1958 tree binfo, fndecl;
1959 int dtorp;
1960{
1961 tree tmp = NULL_TREE;
1962
5e795528
MM
1963 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
1964 /* In [temp.mem] we have:
1965
1966 A specialization of a member function template does not
1967 override a virtual function from a base class. */
1968 return NULL_TREE;
1969
8d08fdba
MS
1970 /* Breadth first search routines start searching basetypes
1971 of TYPE, so we must perform first ply of search here. */
1972 if (dtorp)
d6479fe7
MM
1973 return bfs_walk (binfo, get_virtual_destructor,
1974 tree_has_any_destructor_p, 0);
8d08fdba
MS
1975 else
1976 {
1977 tree drettype, dtypes, btypes, instptr_type;
8d08fdba 1978 tree baselink, best = NULL_TREE;
d6479fe7 1979 tree declarator = DECL_NAME (fndecl);
8d08fdba
MS
1980 if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
1981 return NULL_TREE;
1982
d6479fe7 1983 baselink = get_virtuals_named_this (binfo, declarator);
a292b002
MS
1984 if (baselink == NULL_TREE)
1985 return NULL_TREE;
1986
8d08fdba
MS
1987 drettype = TREE_TYPE (TREE_TYPE (fndecl));
1988 dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1989 if (DECL_STATIC_FUNCTION_P (fndecl))
1990 instptr_type = NULL_TREE;
1991 else
1992 instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
1993
a292b002 1994 for (; baselink; baselink = next_baselink (baselink))
8d08fdba 1995 {
2c73f9f5
ML
1996 tree tmps;
1997 for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps))
8d08fdba 1998 {
2c73f9f5 1999 tmp = OVL_CURRENT (tmps);
8d08fdba
MS
2000 if (! DECL_VINDEX (tmp))
2001 continue;
2002
2003 btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
2004 if (instptr_type == NULL_TREE)
2005 {
91063b51 2006 if (compparms (TREE_CHAIN (btypes), dtypes))
8d08fdba
MS
2007 /* Caller knows to give error in this case. */
2008 return tmp;
2009 return NULL_TREE;
2010 }
2011
91063b51
MM
2012 if (/* The first parameter is the `this' parameter,
2013 which has POINTER_TYPE, and we can therefore
2014 safely use TYPE_QUALS, rather than
2015 CP_TYPE_QUALS. */
2016 (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes)))
2017 == TYPE_QUALS (instptr_type))
2018 && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes)))
8d08fdba 2019 {
4cc1d462 2020 check_final_overrider (fndecl, tmp);
ed70c426
MM
2021
2022 /* FNDECL overrides this function. We continue to
2023 check all the other functions in order to catch
2024 errors; it might be that in some other baseclass
2025 a virtual function was declared with the same
2026 parameter types, but a different return type. */
2027 best = tmp;
8d08fdba
MS
2028 }
2029 }
8d08fdba 2030 }
8d08fdba 2031
8d08fdba
MS
2032 return best;
2033 }
2034}
2035
7177d104
MS
2036/* Return the list of virtual functions which are abstract in type
2037 TYPE that come from non virtual base classes. See
2038 expand_direct_vtbls_init for the style of search we do. */
e92cc029 2039
8926095f
MS
2040static tree
2041get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
6b5fbb55 2042 tree binfo;
8926095f 2043 int do_self;
6b5fbb55 2044 tree abstract_virtuals;
8d08fdba 2045{
8926095f
MS
2046 tree binfos = BINFO_BASETYPES (binfo);
2047 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8d08fdba 2048
8926095f 2049 for (i = 0; i < n_baselinks; i++)
8d08fdba 2050 {
8926095f 2051 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8
JM
2052 int is_not_base_vtable
2053 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
8926095f
MS
2054 if (! TREE_VIA_VIRTUAL (base_binfo))
2055 abstract_virtuals
2056 = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
2057 abstract_virtuals);
2058 }
2059 /* Should we use something besides CLASSTYPE_VFIELDS? */
2060 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2061 {
f30432d7
MS
2062 tree virtuals = BINFO_VIRTUALS (binfo);
2063
aff08c18 2064 skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
8d08fdba 2065
f30432d7 2066 while (virtuals)
8d08fdba 2067 {
83f2ccf4 2068 tree base_fndecl = TREE_VALUE (virtuals);
8d08fdba 2069 if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
83f2ccf4
MM
2070 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl,
2071 abstract_virtuals);
f30432d7 2072 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
2073 }
2074 }
8926095f
MS
2075 return abstract_virtuals;
2076}
2077
2078/* Return the list of virtual functions which are abstract in type TYPE.
2079 This information is cached, and so must be built on a
2080 non-temporary obstack. */
e92cc029 2081
8926095f
MS
2082tree
2083get_abstract_virtuals (type)
2084 tree type;
2085{
f30432d7 2086 tree vbases;
83f660b7 2087 tree abstract_virtuals = NULL;
8926095f 2088
e92cc029 2089 /* First get all from non-virtual bases. */
8926095f
MS
2090 abstract_virtuals
2091 = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
2092
8d08fdba
MS
2093 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
2094 {
f30432d7
MS
2095 tree virtuals = BINFO_VIRTUALS (vbases);
2096
6d813d4d 2097 skip_rtti_stuff (&virtuals, BINFO_TYPE (vbases));
8d08fdba 2098
f30432d7 2099 while (virtuals)
8d08fdba 2100 {
83f2ccf4 2101 tree base_fndecl = TREE_VALUE (virtuals);
8ebeee52
JM
2102 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2103 cp_error ("`%#D' needs a final overrider", base_fndecl);
2104 else if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
83f2ccf4
MM
2105 abstract_virtuals = tree_cons (NULL_TREE, base_fndecl,
2106 abstract_virtuals);
f30432d7 2107 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
2108 }
2109 }
2110 return nreverse (abstract_virtuals);
2111}
2112
9e0781b5 2113static tree
8d08fdba
MS
2114next_baselink (baselink)
2115 tree baselink;
2116{
2117 tree tmp = TREE_TYPE (baselink);
2118 baselink = TREE_CHAIN (baselink);
2119 while (tmp)
2120 {
2121 /* @@ does not yet add previous base types. */
2122 baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
2123 baselink);
2124 TREE_TYPE (baselink) = TREE_TYPE (tmp);
2125 tmp = TREE_CHAIN (tmp);
2126 }
2127 return baselink;
2128}
2129\f
2130/* DEPTH-FIRST SEARCH ROUTINES. */
2131
8d08fdba
MS
2132/* This routine converts a pointer to be a pointer of an immediate
2133 base class. The normal convert_pointer_to routine would diagnose
2134 the conversion as ambiguous, under MI code that has the base class
e92cc029
MS
2135 as an ambiguous base class. */
2136
8d08fdba
MS
2137static tree
2138convert_pointer_to_single_level (to_type, expr)
2139 tree to_type, expr;
2140{
bfec1a84 2141 tree derived;
8d08fdba 2142 tree binfo_of_derived;
bfec1a84 2143 int i;
8d08fdba 2144
bfec1a84
MM
2145 derived = TREE_TYPE (TREE_TYPE (expr));
2146 binfo_of_derived = TYPE_BINFO (derived);
dfbcd65a
JM
2147 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived) == NULL_TREE,
2148 980827);
bfec1a84
MM
2149 for (i = CLASSTYPE_N_BASECLASSES (derived) - 1; i >= 0; --i)
2150 {
2151 tree binfo = BINFO_BASETYPE (binfo_of_derived, i);
2152 my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == binfo_of_derived,
2153 980827);
2154 if (same_type_p (BINFO_TYPE (binfo), to_type))
2155 return build_vbase_path (PLUS_EXPR,
2156 build_pointer_type (to_type),
2157 expr, binfo, 1);
2158 }
2159
2160 my_friendly_abort (19990607);
2161
2162 /* NOTREACHED */
2163 return NULL_TREE;
8d08fdba
MS
2164}
2165
d6479fe7
MM
2166tree markedp (binfo, data)
2167 tree binfo;
2168 void *data ATTRIBUTE_UNUSED;
2169{
2170 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2171}
2172
2173static tree
2174unmarkedp (binfo, data)
2175 tree binfo;
2176 void *data ATTRIBUTE_UNUSED;
2177{
2178 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2179}
5566b478 2180
d6479fe7
MM
2181static tree
2182marked_vtable_pathp (binfo, data)
2183 tree binfo;
2184 void *data ATTRIBUTE_UNUSED;
2185{
2186 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2187}
2188
2189static tree
2190unmarked_vtable_pathp (binfo, data)
2191 tree binfo;
2192 void *data ATTRIBUTE_UNUSED;
2193{
2194 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2195}
2196
2197static tree
2198marked_new_vtablep (binfo, data)
2199 tree binfo;
2200 void *data ATTRIBUTE_UNUSED;
2201{
2202 return BINFO_NEW_VTABLE_MARKED (binfo) ? binfo : NULL_TREE;
2203}
2204
2205static tree
2206unmarked_new_vtablep (binfo, data)
2207 tree binfo;
2208 void *data ATTRIBUTE_UNUSED;
2209{
2210 return !BINFO_NEW_VTABLE_MARKED (binfo) ? binfo : NULL_TREE;
2211}
2212
2213static tree
2214marked_pushdecls_p (binfo, data)
2215 tree binfo;
2216 void *data ATTRIBUTE_UNUSED;
2217{
856216bb
MM
2218 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2219 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
d6479fe7 2220}
5566b478 2221
d6479fe7
MM
2222static tree
2223unmarked_pushdecls_p (binfo, data)
2224 tree binfo;
2225 void *data ATTRIBUTE_UNUSED;
2226{
856216bb
MM
2227 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2228 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
d6479fe7 2229}
8d08fdba 2230
5566b478 2231#if 0
8d08fdba
MS
2232static int dfs_search_slot_nonempty_p (binfo) tree binfo;
2233{ return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
5566b478 2234#endif
8d08fdba 2235
d6479fe7
MM
2236static tree
2237dfs_debug_unmarkedp (binfo, data)
2238 tree binfo;
2239 void *data ATTRIBUTE_UNUSED;
2240{
2241 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2242 ? binfo : NULL_TREE);
2243}
8d08fdba
MS
2244
2245/* The worker functions for `dfs_walk'. These do not need to
2246 test anything (vis a vis marking) if they are paired with
2247 a predicate function (above). */
2248
5566b478 2249#if 0
8d08fdba
MS
2250static void
2251dfs_mark (binfo) tree binfo;
2252{ SET_BINFO_MARKED (binfo); }
5566b478 2253#endif
8d08fdba 2254
d6479fe7
MM
2255tree
2256dfs_unmark (binfo, data)
2257 tree binfo;
2258 void *data ATTRIBUTE_UNUSED;
2259{
2260 CLEAR_BINFO_MARKED (binfo);
2261 return NULL_TREE;
2262}
8d08fdba 2263
5566b478 2264#if 0
8d08fdba
MS
2265static void
2266dfs_mark_vtable_path (binfo) tree binfo;
2267{ SET_BINFO_VTABLE_PATH_MARKED (binfo); }
2268
2269static void
2270dfs_unmark_vtable_path (binfo) tree binfo;
2271{ CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
2272
2273static void
2274dfs_mark_new_vtable (binfo) tree binfo;
2275{ SET_BINFO_NEW_VTABLE_MARKED (binfo); }
2276
2277static void
2278dfs_unmark_new_vtable (binfo) tree binfo;
2279{ CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
2280
2281static void
2282dfs_clear_search_slot (binfo) tree binfo;
2283{ CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
5566b478 2284#endif
8d08fdba 2285
d6479fe7
MM
2286static tree
2287dfs_debug_mark (binfo, data)
8d08fdba 2288 tree binfo;
d6479fe7 2289 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
2290{
2291 tree t = BINFO_TYPE (binfo);
2292
2293 /* Use heuristic that if there are virtual functions,
2294 ignore until we see a non-inline virtual function. */
2295 tree methods = CLASSTYPE_METHOD_VEC (t);
2296
2297 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2298
9a3b49ac 2299 if (methods == 0)
d6479fe7 2300 return NULL_TREE;
9a3b49ac 2301
9a3b49ac
MS
2302 /* If interface info is known, either we've already emitted the debug
2303 info or we don't need to. */
56ae6d77 2304 if (CLASSTYPE_INTERFACE_KNOWN (t))
d6479fe7 2305 return NULL_TREE;
8d08fdba
MS
2306
2307 /* If debug info is requested from this context for this type, supply it.
2308 If debug info is requested from another context for this type,
2309 see if some third context can supply it. */
2310 if (current_function_decl == NULL_TREE
2311 || DECL_CLASS_CONTEXT (current_function_decl) != t)
2312 {
fc378698
MS
2313 if (TREE_VEC_ELT (methods, 1))
2314 methods = TREE_VEC_ELT (methods, 1);
2315 else if (TREE_VEC_ELT (methods, 0))
8d08fdba
MS
2316 methods = TREE_VEC_ELT (methods, 0);
2317 else
fc378698 2318 methods = TREE_VEC_ELT (methods, 2);
2c73f9f5 2319 methods = OVL_CURRENT (methods);
8d08fdba
MS
2320 while (methods)
2321 {
2322 if (DECL_VINDEX (methods)
44a8d0b3 2323 && DECL_THIS_INLINE (methods) == 0
8d08fdba
MS
2324 && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
2325 {
2326 /* Somebody, somewhere is going to have to define this
2327 virtual function. When they do, they will provide
2328 the debugging info. */
d6479fe7 2329 return NULL_TREE;
8d08fdba
MS
2330 }
2331 methods = TREE_CHAIN (methods);
2332 }
2333 }
2334 /* We cannot rely on some alien method to solve our problems,
2335 so we must write out the debug info ourselves. */
f0e01782 2336 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
2c73f9f5 2337 rest_of_type_compilation (t, toplevel_bindings_p ());
d6479fe7
MM
2338
2339 return NULL_TREE;
8d08fdba
MS
2340}
2341\f
d6479fe7
MM
2342struct vbase_info
2343{
2344 tree decl_ptr;
2345 tree inits;
2346 tree vbase_types;
2347};
7177d104 2348
d6479fe7
MM
2349/* Attach to the type of the virtual base class, the pointer to the
2350 virtual base class. */
e92cc029 2351
d6479fe7
MM
2352static tree
2353dfs_find_vbases (binfo, data)
8d08fdba 2354 tree binfo;
d6479fe7 2355 void *data;
8d08fdba 2356{
d6479fe7 2357 struct vbase_info *vi = (struct vbase_info *) data;
8d08fdba
MS
2358 tree binfos = BINFO_BASETYPES (binfo);
2359 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2360
2361 for (i = n_baselinks-1; i >= 0; i--)
2362 {
2363 tree base_binfo = TREE_VEC_ELT (binfos, i);
2364
2365 if (TREE_VIA_VIRTUAL (base_binfo)
2366 && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
2367 {
2368 tree vbase = BINFO_TYPE (base_binfo);
d6479fe7 2369 tree binfo = binfo_member (vbase, vi->vbase_types);
8d08fdba
MS
2370
2371 CLASSTYPE_SEARCH_SLOT (vbase)
fc378698 2372 = build (PLUS_EXPR, build_pointer_type (vbase),
d6479fe7 2373 vi->decl_ptr, BINFO_OFFSET (binfo));
8d08fdba
MS
2374 }
2375 }
2376 SET_BINFO_VTABLE_PATH_MARKED (binfo);
2377 SET_BINFO_NEW_VTABLE_MARKED (binfo);
d6479fe7
MM
2378
2379 return NULL_TREE;
8d08fdba
MS
2380}
2381
d6479fe7
MM
2382static tree
2383dfs_init_vbase_pointers (binfo, data)
8d08fdba 2384 tree binfo;
d6479fe7 2385 void *data;
8d08fdba 2386{
d6479fe7 2387 struct vbase_info *vi = (struct vbase_info *) data;
8d08fdba
MS
2388 tree type = BINFO_TYPE (binfo);
2389 tree fields = TYPE_FIELDS (type);
8926095f 2390 tree this_vbase_ptr;
8d08fdba
MS
2391
2392 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2393
6b5fbb55
MS
2394#if 0
2395 /* See finish_struct_1 for when we can enable this. */
2396 /* If we have a vtable pointer first, skip it. */
2397 if (VFIELD_NAME_P (DECL_NAME (fields)))
8d08fdba 2398 fields = TREE_CHAIN (fields);
6b5fbb55 2399#endif
8d08fdba 2400
d6479fe7
MM
2401 if (BINFO_INHERITANCE_CHAIN (binfo))
2402 {
2403 this_vbase_ptr = TREE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo));
2404 if (TREE_VIA_VIRTUAL (binfo))
2405 this_vbase_ptr = CLASSTYPE_SEARCH_SLOT (type);
2406 else
2407 this_vbase_ptr = convert_pointer_to_single_level (type,
2408 this_vbase_ptr);
2409 TREE_CHAIN (binfo) = this_vbase_ptr;
2410 }
2411 else
2412 this_vbase_ptr = TREE_CHAIN (binfo);
2413
8d08fdba
MS
2414 if (fields == NULL_TREE
2415 || DECL_NAME (fields) == NULL_TREE
2416 || ! VBASE_NAME_P (DECL_NAME (fields)))
d6479fe7 2417 return NULL_TREE;
8d08fdba 2418
d6479fe7
MM
2419 if (build_pointer_type (type)
2420 != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
8d08fdba
MS
2421 my_friendly_abort (125);
2422
d6479fe7 2423 while (fields && DECL_NAME (fields) && VBASE_NAME_P (DECL_NAME (fields)))
8d08fdba
MS
2424 {
2425 tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
2426 build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
fc378698 2427 tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
d6479fe7
MM
2428 vi->inits = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
2429 vi->vbase_types),
2430 build_modify_expr (ref, NOP_EXPR, init),
2431 vi->inits);
8d08fdba
MS
2432 fields = TREE_CHAIN (fields);
2433 }
d6479fe7
MM
2434
2435 return NULL_TREE;
8d08fdba
MS
2436}
2437
2438/* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other
2439 times, just NEW_VTABLE, but optimizer should make both with equal
2440 efficiency (though it does not currently). */
e92cc029 2441
d6479fe7
MM
2442static tree
2443dfs_clear_vbase_slots (binfo, data)
8d08fdba 2444 tree binfo;
d6479fe7 2445 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
2446{
2447 tree type = BINFO_TYPE (binfo);
2448 CLASSTYPE_SEARCH_SLOT (type) = 0;
2449 CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
2450 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
d6479fe7 2451 return NULL_TREE;
8d08fdba
MS
2452}
2453
2454tree
2455init_vbase_pointers (type, decl_ptr)
2456 tree type;
2457 tree decl_ptr;
2458{
2459 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2460 {
d6479fe7 2461 struct vbase_info vi;
8d08fdba
MS
2462 int old_flag = flag_this_is_variable;
2463 tree binfo = TYPE_BINFO (type);
2464 flag_this_is_variable = -2;
d6479fe7
MM
2465
2466 /* Find all the virtual base classes, marking them for later
2467 initialization. */
2468 vi.decl_ptr = decl_ptr;
2469 vi.vbase_types = CLASSTYPE_VBASECLASSES (type);
2470 vi.inits = NULL_TREE;
2471
2472 dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp, &vi);
2473
2474 /* Build up a list of the initializers. */
2475 TREE_CHAIN (binfo) = decl_ptr;
2476 dfs_walk_real (binfo,
2477 dfs_init_vbase_pointers, 0,
2478 marked_vtable_pathp,
2479 &vi);
2480
2481 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep, 0);
8d08fdba 2482 flag_this_is_variable = old_flag;
d6479fe7 2483 return vi.inits;
8d08fdba
MS
2484 }
2485 return 0;
2486}
2487
43f2999d
MS
2488/* get the virtual context (the vbase that directly contains the
2489 DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
2490 or NULL_TREE if there is none.
2491
2492 FNDECL must come from a virtual table from a virtual base to ensure that
2493 there is only one possible DECL_CLASS_CONTEXT.
2494
2495 We know that if there is more than one place (binfo) the fndecl that the
2496 declared, they all refer to the same binfo. See get_class_offset_1 for
2497 the check that ensures this. */
e92cc029 2498
43f2999d
MS
2499static tree
2500virtual_context (fndecl, t, vbase)
2501 tree fndecl, t, vbase;
2502{
2503 tree path;
2504 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
2505 {
f30432d7
MS
2506 /* DECL_CLASS_CONTEXT can be ambiguous in t. */
2507 if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0)
2508 {
2509 while (path)
2510 {
2511 /* Not sure if checking path == vbase is necessary here, but just in
2512 case it is. */
2513 if (TREE_VIA_VIRTUAL (path) || path == vbase)
2514 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2515 path = BINFO_INHERITANCE_CHAIN (path);
2516 }
2517 }
43f2999d 2518 /* This shouldn't happen, I don't want errors! */
8251199e 2519 warning ("recoverable compiler error, fixups for virtual function");
43f2999d
MS
2520 return vbase;
2521 }
2522 while (path)
2523 {
2524 if (TREE_VIA_VIRTUAL (path))
2525 return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
2526 path = BINFO_INHERITANCE_CHAIN (path);
2527 }
2528 return 0;
2529}
2530
2531/* Fixups upcast offsets for one vtable.
2532 Entries may stay within the VBASE given, or
2533 they may upcast into a direct base, or
2534 they may upcast into a different vbase.
2535
45537677
MS
2536 We only need to do fixups in case 2 and 3. In case 2, we add in
2537 the virtual base offset to effect an upcast, in case 3, we add in
2538 the virtual base offset to effect an upcast, then subtract out the
2539 offset for the other virtual base, to effect a downcast into it.
43f2999d
MS
2540
2541 This routine mirrors fixup_vtable_deltas in functionality, though
2542 this one is runtime based, and the other is compile time based.
2543 Conceivably that routine could be removed entirely, and all fixups
2544 done at runtime.
2545
2546 VBASE_OFFSETS is an association list of virtual bases that contains
45537677
MS
2547 offset information for the virtual bases, so the offsets are only
2548 calculated once. The offsets are computed by where we think the
2549 vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where
e92cc029
MS
2550 the vbase really is. */
2551
43f2999d 2552static void
45537677
MS
2553expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t,
2554 vbase_offsets)
2555 tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets;
43f2999d
MS
2556{
2557 tree virtuals = BINFO_VIRTUALS (binfo);
2558 tree vc;
2559 tree delta;
2560 unsigned HOST_WIDE_INT n;
2561
2562 delta = purpose_member (vbase, *vbase_offsets);
2563 if (! delta)
2564 {
fc378698 2565 delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
45537677 2566 delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr);
43f2999d
MS
2567 delta = save_expr (delta);
2568 delta = tree_cons (vbase, delta, *vbase_offsets);
2569 *vbase_offsets = delta;
2570 }
2571
6d813d4d 2572 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
f30432d7 2573
43f2999d
MS
2574 while (virtuals)
2575 {
2576 tree current_fndecl = TREE_VALUE (virtuals);
83f2ccf4 2577
43f2999d 2578 if (current_fndecl
e8abc66f 2579 && current_fndecl != abort_fndecl
43f2999d
MS
2580 && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
2581 {
e92cc029 2582 /* This may in fact need a runtime fixup. */
de22184b 2583 tree idx = build_int_2 (n, 0);
43f2999d
MS
2584 tree vtbl = BINFO_VTABLE (binfo);
2585 tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
2586 tree aref, ref, naref;
2587 tree old_delta, new_delta;
2588 tree init;
2589
2590 if (nvtbl == NULL_TREE
2591 || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
2592 {
2593 /* Dup it if it isn't in local scope yet. */
8c90d611
JM
2594 nvtbl = build_decl
2595 (VAR_DECL, DECL_NAME (vtbl),
52bf7d5d 2596 TYPE_MAIN_VARIANT (TREE_TYPE (vtbl)));
43f2999d
MS
2597 DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
2598 DECL_ALIGN (nvtbl));
2599 TREE_READONLY (nvtbl) = 0;
6b5fbb55 2600 DECL_ARTIFICIAL (nvtbl) = 1;
43f2999d
MS
2601 nvtbl = pushdecl (nvtbl);
2602 init = NULL_TREE;
8c90d611
JM
2603 cp_finish_decl (nvtbl, init, NULL_TREE, 0,
2604 LOOKUP_ONLYCONVERTING);
2605
2606 /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl
2607 because they wouldn't be useful; everything that wants to
2608 look at the vtable will look at the decl for the normal
2609 vtable. Setting DECL_CONTEXT also screws up
2610 decl_function_context. */
2611
43f2999d
MS
2612 init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
2613 nvtbl, vtbl);
2614 TREE_SIDE_EFFECTS (init) = 1;
2615 expand_expr_stmt (init);
e92cc029 2616 /* Update the vtable pointers as necessary. */
8c90d611
JM
2617 ref = build_vfield_ref
2618 (build_indirect_ref (addr, NULL_PTR),
2619 DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
2620 expand_expr_stmt
a9e6be8e 2621 (build_modify_expr (ref, NOP_EXPR, nvtbl));
43f2999d
MS
2622 }
2623 assemble_external (vtbl);
2624 aref = build_array_ref (vtbl, idx);
2625 naref = build_array_ref (nvtbl, idx);
8c90d611
JM
2626 old_delta = build_component_ref (aref, delta_identifier,
2627 NULL_TREE, 0);
2628 new_delta = build_component_ref (naref, delta_identifier,
2629 NULL_TREE, 0);
45537677
MS
2630
2631 /* This is a upcast, so we have to add the offset for the
2632 virtual base. */
43f2999d 2633 old_delta = build_binary_op (PLUS_EXPR, old_delta,
337c90cc 2634 TREE_VALUE (delta));
43f2999d
MS
2635 if (vc)
2636 {
45537677
MS
2637 /* If this is set, we need to subtract out the delta
2638 adjustments for the other virtual base that we
2639 downcast into. */
43f2999d
MS
2640 tree vc_delta = purpose_member (vc, *vbase_offsets);
2641 if (! vc_delta)
2642 {
2643 tree vc_addr = convert_pointer_to_real (vc, orig_addr);
fc378698 2644 vc_delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
43f2999d 2645 vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
45537677 2646 vc_delta, vc_addr);
43f2999d
MS
2647 vc_delta = save_expr (vc_delta);
2648 *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
2649 }
2650 else
2651 vc_delta = TREE_VALUE (vc_delta);
2652
45537677
MS
2653 /* This is a downcast, so we have to subtract the offset
2654 for the virtual base. */
337c90cc 2655 old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta);
43f2999d
MS
2656 }
2657
2658 TREE_READONLY (new_delta) = 0;
6396c3a5 2659 TREE_TYPE (new_delta) =
91063b51
MM
2660 cp_build_qualified_type (TREE_TYPE (new_delta),
2661 CP_TYPE_QUALS (TREE_TYPE (new_delta))
2662 & ~TYPE_QUAL_CONST);
43f2999d
MS
2663 expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
2664 old_delta));
2665 }
2666 ++n;
2667 virtuals = TREE_CHAIN (virtuals);
2668 }
2669}
2670
2671/* Fixup upcast offsets for all direct vtables. Patterned after
2672 expand_direct_vtbls_init. */
e92cc029 2673
43f2999d
MS
2674static void
2675fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
6b5fbb55 2676 tree real_binfo, binfo;
43f2999d 2677 int init_self, can_elide;
6b5fbb55 2678 tree addr, orig_addr, type, vbase, *vbase_offsets;
43f2999d
MS
2679{
2680 tree real_binfos = BINFO_BASETYPES (real_binfo);
2681 tree binfos = BINFO_BASETYPES (binfo);
2682 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
2683
2684 for (i = 0; i < n_baselinks; i++)
2685 {
2686 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
2687 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8
JM
2688 int is_not_base_vtable
2689 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
43f2999d
MS
2690 if (! TREE_VIA_VIRTUAL (real_base_binfo))
2691 fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
2692 is_not_base_vtable, can_elide, addr,
2693 orig_addr, type, vbase, vbase_offsets);
2694 }
2695#if 0
2696 /* Before turning this on, make sure it is correct. */
2697 if (can_elide && ! BINFO_MODIFIED (binfo))
2698 return;
2699#endif
2700 /* Should we use something besides CLASSTYPE_VFIELDS? */
2701 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
2702 {
45537677
MS
2703 tree new_addr = convert_pointer_to_real (binfo, addr);
2704 expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr,
2705 type, vbase_offsets);
43f2999d
MS
2706 }
2707}
2708
8d08fdba
MS
2709/* Build a COMPOUND_EXPR which when expanded will generate the code
2710 needed to initialize all the virtual function table slots of all
7177d104
MS
2711 the virtual baseclasses. MAIN_BINFO is the binfo which determines
2712 the virtual baseclasses to use; TYPE is the type of the object to
2713 which the initialization applies. TRUE_EXP is the true object we
2714 are initializing, and DECL_PTR is the pointer to the sub-object we
8d08fdba
MS
2715 are initializing.
2716
2717 When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
ddd5a7c1 2718 object was laid out by a top-level constructor and the computed
8d08fdba 2719 offsets are valid to store vtables. When zero, we must store new
d6479fe7 2720 vtables through virtual baseclass pointers. */
8d08fdba 2721
8926095f 2722void
9e9ff709 2723expand_indirect_vtbls_init (binfo, true_exp, decl_ptr)
7177d104 2724 tree binfo;
8d08fdba 2725 tree true_exp, decl_ptr;
8d08fdba 2726{
8d08fdba 2727 tree type = BINFO_TYPE (binfo);
9e9ff709 2728
8a21007c
AM
2729 /* This function executes during the finish_function() segment,
2730 AFTER the auto variables and temporary stack space has been marked
2731 unused...If space is needed for the virtual function tables,
2732 some of them might fit within what the compiler now thinks
2733 are available stack slots... These values are actually initialized at
2734 the beginnning of the function, so when the automatics use their space,
2735 they will overwrite the values that are placed here. Marking all
2736 temporary space as unavailable prevents this from happening. */
2737
2738 mark_all_temps_used();
2739
8d08fdba
MS
2740 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
2741 {
43f2999d 2742 rtx fixup_insns = NULL_RTX;
8d08fdba 2743 tree vbases = CLASSTYPE_VBASECLASSES (type);
d6479fe7
MM
2744 struct vbase_info vi;
2745 vi.decl_ptr = (true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0)
2746 : decl_ptr);
2747 vi.vbase_types = vbases;
8d08fdba 2748
d6479fe7 2749 dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep, &vi);
43f2999d 2750
8d08fdba 2751 /* Initialized with vtables of type TYPE. */
39211cd5 2752 for (; vbases; vbases = TREE_CHAIN (vbases))
8d08fdba 2753 {
7177d104 2754 tree addr;
9e9ff709 2755
d6479fe7 2756 addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vi.decl_ptr);
8d08fdba 2757
e92cc029 2758 /* Do all vtables from this virtual base. */
7177d104 2759 /* This assumes that virtual bases can never serve as parent
956d6950 2760 binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */
7177d104
MS
2761 expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
2762 1, 0, addr);
43f2999d 2763
e92cc029
MS
2764 /* Now we adjust the offsets for virtual functions that
2765 cross virtual boundaries on an implicit upcast on vf call
2766 so that the layout of the most complete type is used,
2767 instead of assuming the layout of the virtual bases from
2768 our current type. */
43f2999d
MS
2769
2770 if (flag_vtable_thunks)
2771 {
5566b478 2772 /* We don't have dynamic thunks yet!
e92cc029 2773 So for now, just fail silently. */
43f2999d
MS
2774 }
2775 else
2776 {
2777 tree vbase_offsets = NULL_TREE;
2778 push_to_sequence (fixup_insns);
2779 fixup_virtual_upcast_offsets (vbases,
2780 TYPE_BINFO (BINFO_TYPE (vbases)),
d6479fe7 2781 1, 0, addr, vi.decl_ptr,
43f2999d
MS
2782 type, vbases, &vbase_offsets);
2783 fixup_insns = get_insns ();
2784 end_sequence ();
2785 }
2786 }
2787
2788 if (fixup_insns)
2789 {
43f2999d
MS
2790 tree in_charge_node = lookup_name (in_charge_identifier, 0);
2791 if (! in_charge_node)
2792 {
8251199e 2793 warning ("recoverable internal compiler error, nobody's in charge!");
43f2999d
MS
2794 in_charge_node = integer_zero_node;
2795 }
337c90cc 2796 in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node);
43f2999d
MS
2797 expand_start_cond (in_charge_node, 0);
2798 emit_insns (fixup_insns);
2799 expand_end_cond ();
8d08fdba
MS
2800 }
2801
d6479fe7 2802 dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep, 0);
8d08fdba 2803 }
8d08fdba
MS
2804}
2805
8d08fdba
MS
2806/* get virtual base class types.
2807 This adds type to the vbase_types list in reverse dfs order.
2808 Ordering is very important, so don't change it. */
2809
d6479fe7
MM
2810static tree
2811dfs_get_vbase_types (binfo, data)
8d08fdba 2812 tree binfo;
d6479fe7 2813 void *data;
8d08fdba 2814{
d6479fe7
MM
2815 tree *vbase_types = (tree *) data;
2816
51c184be 2817 if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
8d08fdba 2818 {
ca107ded
MM
2819 tree new_vbase = make_binfo (integer_zero_node, binfo,
2820 BINFO_VTABLE (binfo),
2821 BINFO_VIRTUALS (binfo));
d6479fe7 2822 TREE_CHAIN (new_vbase) = *vbase_types;
ca107ded 2823 TREE_VIA_VIRTUAL (new_vbase) = 1;
d6479fe7 2824 *vbase_types = new_vbase;
51c184be 2825 SET_BINFO_VBASE_MARKED (binfo);
8d08fdba
MS
2826 }
2827 SET_BINFO_MARKED (binfo);
d6479fe7 2828 return NULL_TREE;
8d08fdba
MS
2829}
2830
d6479fe7
MM
2831/* Return a list of binfos for the virtual base classes for TYPE, in
2832 depth-first search order. The list is freshly allocated, so
2833 no modification is made to the current binfo hierarchy. */
e92cc029 2834
8d08fdba
MS
2835tree
2836get_vbase_types (type)
2837 tree type;
2838{
d6479fe7 2839 tree vbase_types;
8d08fdba 2840 tree vbases;
51c184be
MS
2841 tree binfo;
2842
ca107ded 2843 binfo = TYPE_BINFO (type);
8d08fdba 2844 vbase_types = NULL_TREE;
d6479fe7
MM
2845 dfs_walk (binfo, dfs_get_vbase_types, unmarkedp, &vbase_types);
2846 dfs_walk (binfo, dfs_unmark, markedp, 0);
8d08fdba
MS
2847 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2848 reverse it so that we get normal dfs ordering. */
2849 vbase_types = nreverse (vbase_types);
2850
51c184be
MS
2851 /* unmark marked vbases */
2852 for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
2853 CLEAR_BINFO_VBASE_MARKED (vbases);
8d08fdba 2854
51c184be 2855 return vbase_types;
8d08fdba
MS
2856}
2857\f
8d08fdba
MS
2858/* If we want debug info for a type TYPE, make sure all its base types
2859 are also marked as being potentially interesting. This avoids
2860 the problem of not writing any debug info for intermediate basetypes
a292b002 2861 that have abstract virtual functions. Also mark member types. */
8d08fdba
MS
2862
2863void
2864note_debug_info_needed (type)
2865 tree type;
2866{
a292b002 2867 tree field;
9a3b49ac
MS
2868
2869 if (current_template_parms)
2870 return;
8857f91e
MM
2871
2872 if (TYPE_BEING_DEFINED (type))
2873 /* We can't go looking for the base types and fields just yet. */
2874 return;
9a3b49ac 2875
d2e5ee5c
MS
2876 /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2877 does not support name references between translation units. Well, we
2878 could, but that would mean putting global labels in the debug output
2879 before each exported type and each of its functions and static data
2880 members. */
6f1b4c42
JM
2881 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG
2882 || write_symbols == NO_DEBUG)
d2e5ee5c
MS
2883 return;
2884
d6479fe7 2885 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
a292b002
MS
2886 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2887 {
2888 tree ttype;
2889 if (TREE_CODE (field) == FIELD_DECL
2890 && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
d6479fe7 2891 && dfs_debug_unmarkedp (TYPE_BINFO (ttype), 0))
a292b002
MS
2892 note_debug_info_needed (ttype);
2893 }
8d08fdba
MS
2894}
2895\f
2896/* Subroutines of push_class_decls (). */
2897
c1def683
JM
2898/* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2899 because it (or one of the intermediate bases) depends on template parms. */
2900
2901static int
2902dependent_base_p (binfo)
2903 tree binfo;
2904{
2905 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2906 {
d6479fe7 2907 if (currently_open_class (TREE_TYPE (binfo)))
c1def683
JM
2908 break;
2909 if (uses_template_parms (TREE_TYPE (binfo)))
2910 return 1;
2911 }
2912 return 0;
2913}
2914
8f032717
MM
2915static void
2916setup_class_bindings (name, type_binding_p)
2917 tree name;
2918 int type_binding_p;
8d08fdba 2919{
8f032717
MM
2920 tree type_binding = NULL_TREE;
2921 tree value_binding;
c1def683 2922
8f032717
MM
2923 /* If we've already done the lookup for this declaration, we're
2924 done. */
2925 if (IDENTIFIER_CLASS_VALUE (name))
2926 return;
8d08fdba 2927
8f032717
MM
2928 /* First, deal with the type binding. */
2929 if (type_binding_p)
8d08fdba 2930 {
8f032717
MM
2931 type_binding = lookup_member (current_class_type, name,
2932 /*protect=*/2,
2933 /*want_type=*/1);
2934 if (TREE_CODE (type_binding) == TREE_LIST
aa65d1a2 2935 && TREE_TYPE (type_binding) == error_mark_node)
8f032717 2936 /* NAME is ambiguous. */
aa65d1a2 2937 push_class_level_binding (name, type_binding);
8f032717
MM
2938 else
2939 pushdecl_class_level (type_binding);
8d08fdba
MS
2940 }
2941
8f032717
MM
2942 /* Now, do the value binding. */
2943 value_binding = lookup_member (current_class_type, name,
2944 /*protect=*/2,
2945 /*want_type=*/0);
2946
2947 if (type_binding_p
2948 && (TREE_CODE (value_binding) == TYPE_DECL
2949 || (TREE_CODE (value_binding) == TREE_LIST
aa65d1a2
MM
2950 && TREE_TYPE (value_binding) == error_mark_node
2951 && (TREE_CODE (TREE_VALUE (value_binding))
8f032717
MM
2952 == TYPE_DECL))))
2953 /* We found a type-binding, even when looking for a non-type
2954 binding. This means that we already processed this binding
2955 above. */
2956 my_friendly_assert (type_binding_p, 19990401);
dfe2b0b3 2957 else if (value_binding)
8d08fdba 2958 {
8f032717 2959 if (TREE_CODE (value_binding) == TREE_LIST
aa65d1a2 2960 && TREE_TYPE (value_binding) == error_mark_node)
8f032717 2961 /* NAME is ambiguous. */
aa65d1a2 2962 push_class_level_binding (name, value_binding);
8f032717 2963 else
8d08fdba 2964 {
aa65d1a2 2965 if (BASELINK_P (value_binding))
8f032717
MM
2966 /* NAME is some overloaded functions. */
2967 value_binding = TREE_VALUE (value_binding);
2968 pushdecl_class_level (value_binding);
2969 }
2970 }
2971}
f30432d7 2972
8f032717
MM
2973/* Push class-level declarations for any names appearing in BINFO that
2974 are TYPE_DECLS. */
7ddedda4 2975
8f032717
MM
2976static tree
2977dfs_push_type_decls (binfo, data)
2978 tree binfo;
2979 void *data ATTRIBUTE_UNUSED;
2980{
2981 tree type;
2982 tree fields;
f30432d7 2983
8f032717
MM
2984 type = BINFO_TYPE (binfo);
2985 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2986 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
908c4e83
MM
2987 && !(!same_type_p (type, current_class_type)
2988 && template_self_reference_p (type, fields)))
8f032717 2989 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
0ec57017
JM
2990
2991 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2992 DERIVED_FROM_P, which calls get_base_distance. */
2993 SET_BINFO_PUSHDECLS_MARKED (binfo);
8f032717 2994
d6479fe7 2995 return NULL_TREE;
8d08fdba
MS
2996}
2997
8f032717
MM
2998/* Push class-level declarations for any names appearing in BINFO that
2999 are not TYPE_DECLS. */
e92cc029 3000
d6479fe7 3001static tree
8f032717 3002dfs_push_decls (binfo, data)
8d08fdba 3003 tree binfo;
8f032717 3004 void *data;
8d08fdba 3005{
8f032717
MM
3006 tree type;
3007 tree method_vec;
3008 int dep_base_p;
8d08fdba 3009
8f032717
MM
3010 type = BINFO_TYPE (binfo);
3011 dep_base_p = (processing_template_decl && type != current_class_type
3012 && dependent_base_p (binfo));
3013 if (!dep_base_p)
8d08fdba 3014 {
8f032717
MM
3015 tree fields;
3016 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3017 if (DECL_NAME (fields)
3018 && TREE_CODE (fields) != TYPE_DECL
3019 && TREE_CODE (fields) != USING_DECL)
3020 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
3021 else if (TREE_CODE (fields) == FIELD_DECL
6bdb8141 3022 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
8f032717
MM
3023 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
3024
3025 method_vec = (CLASS_TYPE_P (type)
3026 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
3027 if (method_vec)
8d08fdba 3028 {
8f032717
MM
3029 tree *methods;
3030 tree *end;
3031
3032 /* Farm out constructors and destructors. */
3033 end = TREE_VEC_END (method_vec);
3034
3035 for (methods = &TREE_VEC_ELT (method_vec, 2);
3036 *methods && methods != end;
3037 methods++)
3038 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
3039 /*type_binding_p=*/0);
8d08fdba
MS
3040 }
3041 }
8f032717 3042
0ec57017 3043 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
d6479fe7
MM
3044
3045 return NULL_TREE;
8d08fdba
MS
3046}
3047
3048/* When entering the scope of a class, we cache all of the
3049 fields that that class provides within its inheritance
3050 lattice. Where ambiguities result, we mark them
3051 with `error_mark_node' so that if they are encountered
3052 without explicit qualification, we can emit an error
45537677 3053 message. */
e92cc029 3054
8d08fdba 3055void
45537677 3056push_class_decls (type)
8d08fdba
MS
3057 tree type;
3058{
8d08fdba 3059 struct obstack *ambient_obstack = current_obstack;
8d08fdba
MS
3060 search_stack = push_search_level (search_stack, &search_obstack);
3061
7ddedda4
MM
3062 /* Build up all the relevant bindings and such on the cache
3063 obstack. That way no memory is wasted when we throw away the
3064 cache later. */
3f1892e1 3065 push_cache_obstack ();
7ddedda4 3066
aa65d1a2 3067 /* Enter type declarations and mark. */
8f032717 3068 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
8d08fdba 3069
aa65d1a2 3070 /* Enter non-type declarations and unmark. */
8f032717 3071 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
013bc8af 3072
3f1892e1 3073 /* Undo the call to push_cache_obstack above. */
7ddedda4
MM
3074 pop_obstacks ();
3075
8d08fdba
MS
3076 current_obstack = ambient_obstack;
3077}
3078
3079/* Here's a subroutine we need because C lacks lambdas. */
e92cc029 3080
d6479fe7
MM
3081static tree
3082dfs_unuse_fields (binfo, data)
8d08fdba 3083 tree binfo;
d6479fe7 3084 void *data ATTRIBUTE_UNUSED;
8d08fdba
MS
3085{
3086 tree type = TREE_TYPE (binfo);
3087 tree fields;
3088
3089 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3090 {
3091 if (TREE_CODE (fields) != FIELD_DECL)
3092 continue;
3093
3094 TREE_USED (fields) = 0;
3095 if (DECL_NAME (fields) == NULL_TREE
6bdb8141 3096 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
8d08fdba
MS
3097 unuse_fields (TREE_TYPE (fields));
3098 }
d6479fe7
MM
3099
3100 return NULL_TREE;
8d08fdba
MS
3101}
3102
3103void
3104unuse_fields (type)
3105 tree type;
3106{
d6479fe7 3107 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
8d08fdba
MS
3108}
3109
3110void
5566b478 3111pop_class_decls ()
8d08fdba
MS
3112{
3113 /* We haven't pushed a search level when dealing with cached classes,
3114 so we'd better not try to pop it. */
3115 if (search_stack)
3116 search_stack = pop_search_level (search_stack);
3117}
3118
8d08fdba
MS
3119void
3120print_search_statistics ()
3121{
3122#ifdef GATHER_STATISTICS
8d08fdba
MS
3123 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
3124 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
3125 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
3126 n_outer_fields_searched, n_calls_lookup_fnfields);
3127 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
fc378698 3128#else /* GATHER_STATISTICS */
8d08fdba 3129 fprintf (stderr, "no search statistics\n");
fc378698 3130#endif /* GATHER_STATISTICS */
8d08fdba
MS
3131}
3132
3133void
3134init_search_processing ()
3135{
3136 gcc_obstack_init (&search_obstack);
9cd64686 3137 vptr_identifier = get_identifier ("_vptr");
8d08fdba
MS
3138}
3139
3140void
3141reinit_search_statistics ()
3142{
5566b478 3143#ifdef GATHER_STATISTICS
8d08fdba
MS
3144 n_fields_searched = 0;
3145 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
3146 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
3147 n_calls_get_base_type = 0;
3148 n_outer_fields_searched = 0;
3149 n_contexts_saved = 0;
fc378698 3150#endif /* GATHER_STATISTICS */
8d08fdba 3151}
e1cd6e56 3152
e66d884e
JM
3153#define scratch_tree_cons expr_tree_cons
3154
72c4a2a6 3155static tree
7d4bdeed 3156add_conversions (binfo, data)
e1cd6e56 3157 tree binfo;
7d4bdeed 3158 void *data;
e1cd6e56 3159{
72b7eeff 3160 int i;
fc378698 3161 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
7d4bdeed 3162 tree *conversions = (tree *) data;
72b7eeff 3163
a7a64a77
MM
3164 /* Some builtin types have no method vector, not even an empty one. */
3165 if (!method_vec)
3166 return NULL_TREE;
3167
fc378698 3168 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
72b7eeff 3169 {
fc378698 3170 tree tmp = TREE_VEC_ELT (method_vec, i);
37b6eb34 3171 tree name;
61a127b3 3172
aa45967f 3173 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
72b7eeff 3174 break;
72c4a2a6 3175
37b6eb34 3176 name = DECL_NAME (OVL_CURRENT (tmp));
59e76fc6 3177
72c4a2a6 3178 /* Make sure we don't already have this conversion. */
37b6eb34 3179 if (! IDENTIFIER_MARKED (name))
72c4a2a6 3180 {
7d4bdeed 3181 *conversions = scratch_tree_cons (binfo, tmp, *conversions);
37b6eb34 3182 IDENTIFIER_MARKED (name) = 1;
72c4a2a6 3183 }
72b7eeff 3184 }
72c4a2a6 3185 return NULL_TREE;
e1cd6e56
MS
3186}
3187
27b8d0cd
MM
3188/* Return a TREE_LIST containing all the non-hidden user-defined
3189 conversion functions for TYPE (and its base-classes). The
3190 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
3191 containing the conversion functions. The TREE_PURPOSE is the BINFO
3192 from which the conversion functions in this node were selected. */
3193
e1cd6e56
MS
3194tree
3195lookup_conversions (type)
3196 tree type;
3197{
72c4a2a6 3198 tree t;
7d4bdeed 3199 tree conversions = NULL_TREE;
72c4a2a6 3200
e92cc029 3201 if (TYPE_SIZE (type))
d6479fe7 3202 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
72c4a2a6
JM
3203
3204 for (t = conversions; t; t = TREE_CHAIN (t))
37b6eb34 3205 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
72c4a2a6 3206
e1cd6e56
MS
3207 return conversions;
3208}
6467930b 3209
d6479fe7
MM
3210struct overlap_info
3211{
3212 tree compare_type;
3213 int found_overlap;
3214};
3215
732dcb6f
JM
3216/* Check whether the empty class indicated by EMPTY_BINFO is also present
3217 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
3218
d6479fe7
MM
3219static tree
3220dfs_check_overlap (empty_binfo, data)
732dcb6f 3221 tree empty_binfo;
d6479fe7 3222 void *data;
732dcb6f 3223{
d6479fe7 3224 struct overlap_info *oi = (struct overlap_info *) data;
732dcb6f 3225 tree binfo;
d6479fe7
MM
3226 for (binfo = TYPE_BINFO (oi->compare_type);
3227 ;
3228 binfo = BINFO_BASETYPE (binfo, 0))
732dcb6f
JM
3229 {
3230 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
3231 {
d6479fe7 3232 oi->found_overlap = 1;
732dcb6f
JM
3233 break;
3234 }
3235 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
3236 break;
3237 }
d6479fe7
MM
3238
3239 return NULL_TREE;
732dcb6f
JM
3240}
3241
3242/* Trivial function to stop base traversal when we find something. */
3243
d6479fe7
MM
3244static tree
3245dfs_no_overlap_yet (binfo, data)
3246 tree binfo;
3247 void *data;
732dcb6f 3248{
d6479fe7
MM
3249 struct overlap_info *oi = (struct overlap_info *) data;
3250 return !oi->found_overlap ? binfo : NULL_TREE;
732dcb6f
JM
3251}
3252
3253/* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
3254 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
3255
3256int
3257types_overlap_p (empty_type, next_type)
3258 tree empty_type, next_type;
3259{
d6479fe7
MM
3260 struct overlap_info oi;
3261
732dcb6f
JM
3262 if (! IS_AGGR_TYPE (next_type))
3263 return 0;
d6479fe7
MM
3264 oi.compare_type = next_type;
3265 oi.found_overlap = 0;
3266 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
3267 dfs_no_overlap_yet, &oi);
3268 return oi.found_overlap;
3269}
3270
d6479fe7 3271/* Given a vtable VAR, determine which binfo it comes from. */
a1dd0d36
JM
3272
3273tree
d6479fe7
MM
3274binfo_for_vtable (var)
3275 tree var;
a1dd0d36 3276{
5430acc8
JM
3277 tree binfo = TYPE_BINFO (DECL_CONTEXT (var));
3278 tree binfos;
3279 int i;
d6479fe7 3280
5430acc8
JM
3281 while (1)
3282 {
3283 binfos = BINFO_BASETYPES (binfo);
3284 if (binfos == NULL_TREE)
3285 break;
3286
3287 i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
3288 if (i == -1)
3289 break;
3290
3291 binfo = TREE_VEC_ELT (binfos, i);
3292 }
3293
3294 return binfo;
a1dd0d36 3295}
6ad07332
JM
3296
3297/* Returns 1 iff BINFO is from a direct or indirect virtual base. */
3298
3299int
3300binfo_from_vbase (binfo)
3301 tree binfo;
3302{
3303 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
3304 {
3305 if (TREE_VIA_VIRTUAL (binfo))
3306 return 1;
3307 }
3308 return 0;
3309}
This page took 0.856126 seconds and 5 git commands to generate.