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