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