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