1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
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)
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.
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. */
24 /* High-level class interface. */
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* This is how we tell when two virtual member functions are really the
43 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
45 extern void set_class_shadows PARAMS ((tree));
47 /* The number of nested classes being processed. If we are not in the
48 scope of any class, this is zero. */
50 int current_class_depth;
52 /* In order to deal with nested classes, we keep a stack of classes.
53 The topmost entry is the innermost class, and is the entry at index
54 CURRENT_CLASS_DEPTH */
56 typedef struct class_stack_node {
57 /* The name of the class. */
60 /* The _TYPE node for the class. */
63 /* The access specifier pending for new declarations in the scope of
67 /* If were defining TYPE, the names used in this class. */
68 splay_tree names_used;
69 }* class_stack_node_t;
71 /* The stack itself. This is an dynamically resized array. The
72 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
73 static int current_class_stack_size;
74 static class_stack_node_t current_class_stack;
76 static tree get_vfield_name PARAMS ((tree));
77 static void finish_struct_anon PARAMS ((tree));
78 static tree build_vbase_pointer PARAMS ((tree, tree));
79 static tree build_vtable_entry PARAMS ((tree, tree, tree));
80 static tree get_vtable_name PARAMS ((tree));
81 static tree get_derived_offset PARAMS ((tree, tree));
82 static tree get_basefndecls PARAMS ((tree, tree));
83 static void set_rtti_entry PARAMS ((tree, tree, tree));
84 static int build_primary_vtable PARAMS ((tree, tree));
85 static int build_secondary_vtable PARAMS ((tree, tree));
86 static tree dfs_finish_vtbls PARAMS ((tree, void *));
87 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
88 static void finish_vtbls PARAMS ((tree));
89 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
90 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
91 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
92 static void delete_duplicate_fields PARAMS ((tree));
93 static void finish_struct_bits PARAMS ((tree));
94 static int alter_access PARAMS ((tree, tree, tree));
95 static void handle_using_decl PARAMS ((tree, tree));
96 static int overrides PARAMS ((tree, tree));
97 static int strictly_overrides PARAMS ((tree, tree));
98 static void mark_overriders PARAMS ((tree, tree));
99 static void check_for_override PARAMS ((tree, tree));
100 static tree dfs_modify_vtables PARAMS ((tree, void *));
101 static tree modify_all_vtables PARAMS ((tree, int *, tree));
102 static void determine_primary_base PARAMS ((tree, int *));
103 static void finish_struct_methods PARAMS ((tree));
104 static void maybe_warn_about_overly_private_class PARAMS ((tree));
105 static int field_decl_cmp PARAMS ((const tree *, const tree *));
106 static int method_name_cmp PARAMS ((const tree *, const tree *));
107 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
108 static tree fixed_type_or_null PARAMS ((tree, int *));
109 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
111 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
112 static tree build_vtbl_initializer PARAMS ((tree, tree));
113 static int count_fields PARAMS ((tree));
114 static int add_fields_to_vec PARAMS ((tree, tree, int));
115 static void check_bitfield_decl PARAMS ((tree));
116 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
117 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
119 static int avoid_overlap PARAMS ((tree, tree, int *));
120 static tree build_base_field PARAMS ((tree, tree, int *, int *, unsigned int *));
121 static tree build_base_fields PARAMS ((tree, int *));
122 static tree build_vbase_pointer_fields PARAMS ((tree, int *));
123 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
125 static void check_methods PARAMS ((tree));
126 static void remove_zero_width_bit_fields PARAMS ((tree));
127 static void check_bases PARAMS ((tree, int *, int *, int *));
128 static void check_bases_and_members PARAMS ((tree, int *));
129 static void create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
130 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
131 static void fixup_pending_inline PARAMS ((struct pending_inline *));
132 static void fixup_inline_methods PARAMS ((tree));
133 static void set_primary_base PARAMS ((tree, int, int *));
134 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
135 static void propagate_binfo_offsets PARAMS ((tree, tree));
136 static void layout_basetypes PARAMS ((tree));
137 static void layout_virtual_bases PARAMS ((tree));
138 static void remove_base_field PARAMS ((tree, tree, tree *));
139 static void remove_base_fields PARAMS ((tree));
140 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
141 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
142 static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
143 static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
144 static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
145 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
146 static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
147 static tree dfs_count_virtuals PARAMS ((tree, void *));
148 static void start_vtable PARAMS ((tree, int *));
149 static void layout_vtable_decl PARAMS ((tree, int));
150 static int num_vfun_entries PARAMS ((tree));
151 static tree dfs_find_final_overrider PARAMS ((tree, void *));
152 static tree find_final_overrider PARAMS ((tree, tree, tree));
153 static tree dfs_find_base PARAMS ((tree, void *));
154 static int make_new_vtable PARAMS ((tree, tree));
155 extern void dump_class_hierarchy PARAMS ((tree, int));
156 static tree build_vtable PARAMS ((tree, tree, tree));
157 static void initialize_vtable PARAMS ((tree, tree));
159 /* Variables shared between class.c and call.c. */
161 #ifdef GATHER_STATISTICS
163 int n_vtable_entries = 0;
164 int n_vtable_searches = 0;
165 int n_vtable_elems = 0;
166 int n_convert_harshness = 0;
167 int n_compute_conversion_costs = 0;
168 int n_build_method_call = 0;
169 int n_inner_fields_searched = 0;
172 /* Virtual base class layout. */
174 /* Returns a list of virtual base class pointers as a chain of
178 build_vbase_pointer_fields (rec, empty_p)
182 /* Chain to hold all the new FIELD_DECLs which point at virtual
184 tree vbase_decls = NULL_TREE;
185 tree binfos = TYPE_BINFO_BASETYPES (rec);
186 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
190 /* Under the new ABI, there are no vbase pointers in the object.
191 Instead, the offsets are stored in the vtable. */
192 if (vbase_offsets_in_vtable_p ())
195 /* Loop over the baseclasses, adding vbase pointers as needed. */
196 for (i = 0; i < n_baseclasses; i++)
198 register tree base_binfo = TREE_VEC_ELT (binfos, i);
199 register tree basetype = BINFO_TYPE (base_binfo);
201 if (TYPE_SIZE (basetype) == 0)
202 /* This error is now reported in xref_tag, thus giving better
203 location information. */
206 /* All basetypes are recorded in the association list of the
209 if (TREE_VIA_VIRTUAL (base_binfo))
214 /* The offset for a virtual base class is only used in computing
215 virtual function tables and for initializing virtual base
216 pointers. It is built once `get_vbase_types' is called. */
218 /* If this basetype can come from another vbase pointer
219 without an additional indirection, we will share
220 that pointer. If an indirection is involved, we
221 make our own pointer. */
222 for (j = 0; j < n_baseclasses; j++)
224 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
225 if (! TREE_VIA_VIRTUAL (other_base_binfo)
226 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
229 FORMAT_VBASE_NAME (name, basetype);
230 decl = build_vtbl_or_vbase_field (get_identifier (name),
231 get_identifier (VTABLE_BASE),
232 build_pointer_type (basetype),
236 BINFO_VPTR_FIELD (base_binfo) = decl;
237 TREE_CHAIN (decl) = vbase_decls;
242 /* The space this decl occupies has already been accounted for. */
250 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
253 dfs_build_vbase_offset_vtbl_entries (binfo, data)
257 tree list = (tree) data;
259 if (TREE_TYPE (list) == binfo)
260 /* The TREE_TYPE of LIST is the base class from which we started
261 walking. If that BINFO is virtual it's not a virtual baseclass
264 else if (TREE_VIA_VIRTUAL (binfo))
269 /* Remember the index to the vbase offset for this virtual
271 vbase = BINFO_FOR_VBASE (TREE_TYPE (binfo), TREE_PURPOSE (list));
272 if (!TREE_VALUE (list))
273 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
276 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
277 BINFO_VPTR_FIELD (vbase)
278 = fold (build (MINUS_EXPR, integer_type_node,
279 BINFO_VPTR_FIELD (vbase), integer_one_node));
282 /* And record the offset at which this virtual base lies in the
284 init = BINFO_OFFSET (binfo);
285 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
286 init, TREE_VALUE (list));
289 SET_BINFO_VTABLE_PATH_MARKED (binfo);
294 /* Returns the initializers for the vbase offset entries in the vtable
295 for BINFO (which is part of the class hierarchy dominated by T), in
299 build_vbase_offset_vtbl_entries (binfo, t)
307 /* Under the old ABI, pointers to virtual bases are stored in each
309 if (!vbase_offsets_in_vtable_p ())
312 /* If there are no virtual baseclasses, then there is nothing to
314 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
319 /* The offsets are allocated in the reverse order of a
320 depth-first left-to-right traversal of the hierarchy. We use
321 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
322 dfs_walk, and so BINFO_MARKED is already in use. */
323 list = build_tree_list (t, NULL_TREE);
324 TREE_TYPE (list) = binfo;
326 dfs_build_vbase_offset_vtbl_entries,
327 dfs_vtable_path_unmarked_real_bases_queue_p,
330 dfs_vtable_path_unmark,
331 dfs_vtable_path_marked_real_bases_queue_p,
333 inits = nreverse (TREE_VALUE (list));
335 /* We've now got offsets in the right order. However, the offsets
336 we've stored are offsets from the beginning of the complete
337 object, and we need offsets from this BINFO. */
338 for (init = inits; init; init = TREE_CHAIN (init))
340 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
341 TREE_PURPOSE to scribble in. But, we need to clear it now so
342 that the values are not perceived as labeled initializers. */
343 TREE_PURPOSE (init) = NULL_TREE;
345 = fold (build1 (NOP_EXPR, vtable_entry_type,
346 size_diffop (TREE_VALUE (init),
347 BINFO_OFFSET (binfo))));
353 typedef struct vcall_offset_data_s
355 /* The binfo for the most-derived type. */
357 /* The binfo for the virtual base for which we're building
360 /* The vcall offset initializers built up so far. */
362 /* The number of vcall offsets accumulated. */
366 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
369 dfs_vcall_offset_queue_p (binfo, data)
373 vcall_offset_data* vod = (vcall_offset_data *) data;
375 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
378 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
381 dfs_build_vcall_offset_vtbl_entries (binfo, data)
385 vcall_offset_data* vod;
389 /* Primary bases are not interesting; all of the virtual
390 function table entries have been overridden. */
391 if (BINFO_PRIMARY_MARKED_P (binfo))
394 vod = (vcall_offset_data *) data;
395 binfo_inits = NULL_TREE;
397 /* We chain the offsets on in reverse order. That's correct --
398 build_vtbl_initializer will straighten them out. */
399 for (virtuals = skip_rtti_stuff (binfo,
403 virtuals = TREE_CHAIN (virtuals))
405 /* Figure out what function we're looking at. */
406 tree fn = TREE_VALUE (virtuals);
407 tree base = DECL_CONTEXT (fn);
408 /* The FN comes from BASE. So, we must caculate the adjustment
409 from the virtual base that derived from BINFO to BASE. */
410 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
413 = tree_cons (NULL_TREE,
414 fold (build1 (NOP_EXPR, vtable_entry_type,
415 size_diffop (BINFO_OFFSET (base_binfo),
416 BINFO_OFFSET (vod->vbase)))),
420 /* Now add the initializers we've just created to the list that will
421 be returned to our caller. */
422 vod->inits = chainon (vod->inits, binfo_inits);
427 /* Returns the initializers for the vcall offset entries in the vtable
428 for BINFO (which is part of the class hierarchy dominated by T), in
432 build_vcall_offset_vtbl_entries (binfo, t)
436 vcall_offset_data vod;
438 /* Under the old ABI, the adjustments to the `this' pointer were made
440 if (!vcall_offsets_in_vtable_p ())
443 /* We only need these entries if this base is a virtual base. */
444 if (!TREE_VIA_VIRTUAL (binfo))
447 /* We need a vcall offset for each of the virtual functions in this
450 class A { virtual void f (); };
451 class B : virtual public A { };
452 class C: virtual public A, public B {};
459 The location of `A' is not at a fixed offset relative to `B'; the
460 offset depends on the complete object derived from `B'. So,
461 `B' vtable contains an entry for `f' that indicates by what
462 amount the `this' pointer for `B' needs to be adjusted to arrive
465 We need entries for all the functions in our primary vtable and
466 in our non-virtual bases vtables. For each base, the entries
467 appear in the same order as in the base; but the bases themselves
468 appear in reverse depth-first, left-to-right order. */
471 vod.inits = NULL_TREE;
473 dfs_build_vcall_offset_vtbl_entries,
474 dfs_vcall_offset_queue_p,
480 /* Returns a pointer to the virtual base class of EXP that has the
481 indicated TYPE. EXP is of class type, not a pointer type. */
484 build_vbase_pointer (exp, type)
487 if (vbase_offsets_in_vtable_p ())
492 /* Find the shared copy of TYPE; that's where the vtable offset
494 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
495 /* Find the virtual function table pointer. */
496 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
497 /* Compute the location where the offset will lie. */
498 vbase_ptr = build_binary_op (PLUS_EXPR,
500 BINFO_VPTR_FIELD (vbase));
501 vbase_ptr = build1 (NOP_EXPR,
502 build_pointer_type (ptrdiff_type_node),
504 /* Add the contents of this location to EXP. */
505 return build (PLUS_EXPR,
506 build_pointer_type (type),
507 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
508 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
513 FORMAT_VBASE_NAME (name, type);
514 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
518 /* Build multi-level access to EXPR using hierarchy path PATH.
519 CODE is PLUS_EXPR if we are going with the grain,
520 and MINUS_EXPR if we are not (in which case, we cannot traverse
521 virtual baseclass links).
523 TYPE is the type we want this path to have on exit.
525 NONNULL is non-zero if we know (for any reason) that EXPR is
526 not, in fact, zero. */
529 build_vbase_path (code, type, expr, path, nonnull)
531 tree type, expr, path;
534 register int changed = 0;
535 tree last = NULL_TREE, last_virtual = NULL_TREE;
537 tree null_expr = 0, nonnull_expr;
539 tree offset = integer_zero_node;
541 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
542 return build1 (NOP_EXPR, type, expr);
544 /* We could do better if we had additional logic to convert back to the
545 unconverted type (the static type of the complete object), and then
546 convert back to the type we want. Until that is done, we only optimize
547 if the complete type is the same type as expr has. */
548 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
550 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
551 expr = save_expr (expr);
554 path = reverse_path (path);
556 basetype = BINFO_TYPE (path);
560 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
562 last_virtual = BINFO_TYPE (TREE_VALUE (path));
563 if (code == PLUS_EXPR)
565 changed = ! fixed_type_p;
571 /* We already check for ambiguous things in the caller, just
575 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
576 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
578 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
579 nonnull_expr = build_vbase_pointer (ind, last_virtual);
581 && TREE_CODE (type) == POINTER_TYPE
582 && null_expr == NULL_TREE)
584 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
585 expr = build (COND_EXPR, build_pointer_type (last_virtual),
586 build (EQ_EXPR, boolean_type_node, expr,
588 null_expr, nonnull_expr);
591 /* else we'll figure out the offset below. */
593 /* Happens in the case of parse errors. */
594 if (nonnull_expr == error_mark_node)
595 return error_mark_node;
599 cp_error ("cannot cast up from virtual baseclass `%T'",
601 return error_mark_node;
604 last = TREE_VALUE (path);
605 path = TREE_CHAIN (path);
607 /* LAST is now the last basetype assoc on the path. */
609 /* A pointer to a virtual base member of a non-null object
610 is non-null. Therefore, we only need to test for zeroness once.
611 Make EXPR the canonical expression to deal with here. */
614 TREE_OPERAND (expr, 2) = nonnull_expr;
615 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
616 = TREE_TYPE (nonnull_expr);
621 /* If we go through any virtual base pointers, make sure that
622 casts to BASETYPE from the last virtual base class use
623 the right value for BASETYPE. */
626 tree intype = TREE_TYPE (TREE_TYPE (expr));
628 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
630 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
633 offset = BINFO_OFFSET (last);
635 if (TREE_INT_CST_LOW (offset))
637 /* Bash types to make the backend happy. */
638 offset = cp_convert (type, offset);
640 /* If expr might be 0, we need to preserve that zeroness. */
644 TREE_TYPE (null_expr) = type;
646 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
647 if (TREE_SIDE_EFFECTS (expr))
648 expr = save_expr (expr);
650 return build (COND_EXPR, type,
651 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
653 build (code, type, expr, offset));
655 else return build (code, type, expr, offset);
658 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
659 be used multiple times in initialization of multiple inheritance. */
662 TREE_TYPE (expr) = type;
666 return build1 (NOP_EXPR, type, expr);
670 /* Virtual function things. */
672 /* Build an entry in the virtual function table. DELTA is the offset
673 for the `this' pointer. VCALL_INDEX is the vtable index containing
674 the vcall offset; zero if none. ENTRY is the virtual function
675 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
676 but it may not actually be a virtual function table pointer. (For
677 example, it might be the address of the RTTI object, under the new
681 build_vtable_entry (delta, vcall_index, entry)
686 if (flag_vtable_thunks)
688 HOST_WIDE_INT idelta;
689 HOST_WIDE_INT ivindex;
691 idelta = TREE_INT_CST_LOW (delta);
692 ivindex = TREE_INT_CST_LOW (vcall_index);
693 if ((idelta || ivindex)
694 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry, 0)))
696 entry = make_thunk (entry, idelta, ivindex);
697 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
698 TREE_READONLY (entry) = 1;
699 TREE_CONSTANT (entry) = 1;
701 #ifdef GATHER_STATISTICS
702 n_vtable_entries += 1;
708 extern int flag_huge_objects;
709 tree elems = tree_cons (NULL_TREE, delta,
710 tree_cons (NULL_TREE, integer_zero_node,
711 build_tree_list (NULL_TREE, entry)));
712 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
714 /* We don't use vcall offsets when not using vtable thunks. */
715 my_friendly_assert (integer_zerop (vcall_index), 20000125);
717 /* DELTA used to be constructed by `size_int' and/or size_binop,
718 which caused overflow problems when it was negative. That should
721 if (! int_fits_type_p (delta, delta_type_node))
723 if (flag_huge_objects)
724 sorry ("object size exceeds built-in limit for virtual function table implementation");
726 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
729 TREE_CONSTANT (entry) = 1;
730 TREE_STATIC (entry) = 1;
731 TREE_READONLY (entry) = 1;
733 #ifdef GATHER_STATISTICS
734 n_vtable_entries += 1;
741 /* We want to give the assembler the vtable identifier as well as
742 the offset to the function pointer. So we generate
744 __asm__ __volatile__ (".vtable_entry %c0, %c1"
745 : : "s"(&class_vtable),
746 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
749 build_vtable_entry_ref (basetype, vtbl, idx)
750 tree basetype, vtbl, idx;
752 static char asm_stmt[] = ".vtable_entry %c0, %c1";
755 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
756 s = build_tree_list (build_string (1, "s"), s);
758 i = build_array_ref (vtbl, idx);
759 if (!flag_vtable_thunks)
760 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
761 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
762 i2 = build_array_ref (vtbl, build_int_2(0,0));
763 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
764 i = build_binary_op (MINUS_EXPR, i, i2);
765 i = build_tree_list (build_string (1, "i"), i);
767 finish_asm_stmt (ridpointers[RID_VOLATILE],
768 build_string (sizeof(asm_stmt)-1, asm_stmt),
769 NULL_TREE, chainon (s, i), NULL_TREE);
772 /* Given an object INSTANCE, return an expression which yields the
773 virtual function vtable element corresponding to INDEX. There are
774 many special cases for INSTANCE which we take care of here, mainly
775 to avoid creating extra tree nodes when we don't have to. */
778 build_vtbl_ref (instance, idx)
782 tree basetype = TREE_TYPE (instance);
784 if (TREE_CODE (basetype) == REFERENCE_TYPE)
785 basetype = TREE_TYPE (basetype);
787 if (instance == current_class_ref)
788 vtbl = build_vfield_ref (instance, basetype);
793 /* Try to figure out what a reference refers to, and
794 access its virtual function table directly. */
795 tree ref = NULL_TREE;
797 if (TREE_CODE (instance) == INDIRECT_REF
798 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
799 ref = TREE_OPERAND (instance, 0);
800 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
803 if (ref && TREE_CODE (ref) == VAR_DECL
804 && DECL_INITIAL (ref))
806 tree init = DECL_INITIAL (ref);
808 while (TREE_CODE (init) == NOP_EXPR
809 || TREE_CODE (init) == NON_LVALUE_EXPR)
810 init = TREE_OPERAND (init, 0);
811 if (TREE_CODE (init) == ADDR_EXPR)
813 init = TREE_OPERAND (init, 0);
814 if (IS_AGGR_TYPE (TREE_TYPE (init))
815 && (TREE_CODE (init) == PARM_DECL
816 || TREE_CODE (init) == VAR_DECL))
822 if (IS_AGGR_TYPE (TREE_TYPE (instance))
823 && (TREE_CODE (instance) == RESULT_DECL
824 || TREE_CODE (instance) == PARM_DECL
825 || TREE_CODE (instance) == VAR_DECL))
826 vtbl = TYPE_BINFO_VTABLE (basetype);
828 vtbl = build_vfield_ref (instance, basetype);
831 assemble_external (vtbl);
834 build_vtable_entry_ref (basetype, vtbl, idx);
836 aref = build_array_ref (vtbl, idx);
841 /* Given an object INSTANCE, return an expression which yields the
842 virtual function corresponding to INDEX. There are many special
843 cases for INSTANCE which we take care of here, mainly to avoid
844 creating extra tree nodes when we don't have to. */
847 build_vfn_ref (ptr_to_instptr, instance, idx)
848 tree *ptr_to_instptr, instance;
851 tree aref = build_vtbl_ref (instance, idx);
853 /* When using thunks, there is no extra delta, and we get the pfn
855 if (flag_vtable_thunks)
860 /* Save the intermediate result in a SAVE_EXPR so we don't have to
861 compute each component of the virtual function pointer twice. */
862 if (TREE_CODE (aref) == INDIRECT_REF)
863 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
866 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
868 cp_convert (ptrdiff_type_node,
869 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
872 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
875 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
876 for the given TYPE. */
879 get_vtable_name (type)
882 tree type_id = build_typename_overload (type);
883 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
884 + IDENTIFIER_LENGTH (type_id) + 2);
885 const char *ptr = IDENTIFIER_POINTER (type_id);
887 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
889 /* We don't take off the numbers; build_secondary_vtable uses the
890 DECL_ASSEMBLER_NAME for the type, which includes the number
891 in `3foo'. If we were to pull them off here, we'd end up with
892 something like `_vt.foo.3bar', instead of a uniform definition. */
893 while (ptr[i] >= '0' && ptr[i] <= '9')
896 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
897 return get_identifier (buf);
900 /* Return the offset to the main vtable for a given base BINFO. */
903 get_vfield_offset (binfo)
907 = size_binop (FLOOR_DIV_EXPR,
908 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
909 bitsize_int (BITS_PER_UNIT));
911 return size_binop (PLUS_EXPR, convert (sizetype, tmp),
912 BINFO_OFFSET (binfo));
915 /* Get the offset to the start of the original binfo that we derived
916 this binfo from. If we find TYPE first, return the offset only
917 that far. The shortened search is useful because the this pointer
918 on method calling is expected to point to a DECL_CONTEXT (fndecl)
919 object, and not a baseclass of it. */
923 get_derived_offset (binfo, type)
926 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
930 while (BINFO_BASETYPES (binfo)
931 && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
933 tree binfos = BINFO_BASETYPES (binfo);
934 if (BINFO_TYPE (binfo) == type)
936 binfo = TREE_VEC_ELT (binfos, i);
939 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
940 return size_binop (MINUS_EXPR, offset1, offset2);
943 /* Update the rtti info for this class. */
946 set_rtti_entry (virtuals, offset, type)
947 tree virtuals, offset, type;
951 if (CLASSTYPE_COM_INTERFACE (type))
955 decl = get_tinfo_decl (type);
956 else if (!new_abi_rtti_p ())
957 /* If someone tries to get RTTI information for a type compiled
958 without RTTI, they're out of luck. By calling __pure_virtual
959 in this case, we give a small clue as to what went wrong. We
960 could consider having a __no_typeinfo function as well, for a
961 more specific hint. */
964 /* For the new-abi, we just point to the type_info object. */
967 if (flag_vtable_thunks)
969 /* The first slot holds the offset. */
970 BV_DELTA (virtuals) = offset;
971 BV_VCALL_INDEX (virtuals) = integer_zero_node;
973 /* The next node holds the decl. */
974 virtuals = TREE_CHAIN (virtuals);
975 offset = integer_zero_node;
978 /* This slot holds the function to call. */
979 BV_DELTA (virtuals) = offset;
980 BV_VCALL_INDEX (virtuals) = integer_zero_node;
981 BV_FN (virtuals) = decl;
984 /* Create a VAR_DECL for a primary or secondary vtable for
985 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
989 build_vtable (class_type, name, vtable_type)
996 decl = build_lang_decl (VAR_DECL, name, vtable_type);
997 DECL_CONTEXT (decl) = class_type;
998 DECL_ARTIFICIAL (decl) = 1;
999 TREE_STATIC (decl) = 1;
1000 #ifndef WRITABLE_VTABLES
1001 /* Make them READONLY by default. (mrs) */
1002 TREE_READONLY (decl) = 1;
1004 DECL_VIRTUAL_P (decl) = 1;
1005 import_export_vtable (decl, class_type, 0);
1010 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1011 or even complete. If this does not exist, create it. If COMPLETE is
1012 non-zero, then complete the definition of it -- that will render it
1013 impossible to actually build the vtable, but is useful to get at those
1014 which are known to exist in the runtime. */
1017 get_vtable_decl (type, complete)
1021 tree name = get_vtable_name (type);
1022 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1026 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1027 && DECL_VIRTUAL_P (decl), 20000118);
1031 decl = build_vtable (type, name, void_type_node);
1032 decl = pushdecl_top_level (decl);
1033 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1035 /* At one time the vtable info was grabbed 2 words at a time. This
1036 fails on sparc unless you have 8-byte alignment. (tiemann) */
1037 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1041 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1046 /* Build the primary virtual function table for TYPE. If BINFO is
1047 non-NULL, build the vtable starting with the initial approximation
1048 that it is the same as the one which is the head of the association
1049 list. Returns a non-zero value if a new vtable is actually
1053 build_primary_vtable (binfo, type)
1056 tree virtuals, decl;
1058 decl = get_vtable_decl (type, /*complete=*/0);
1064 if (BINFO_NEW_VTABLE_MARKED (binfo))
1065 /* We have already created a vtable for this base, so there's
1066 no need to do it again. */
1069 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1070 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1071 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
1072 DECL_SIZE_UNIT (decl)
1073 = TYPE_SIZE_UNIT (TREE_TYPE (BINFO_VTABLE (binfo)));
1075 /* Now do rtti stuff. */
1076 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
1077 offset = size_diffop (size_zero_node, offset);
1078 set_rtti_entry (virtuals, offset, type);
1082 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1084 virtuals = NULL_TREE;
1087 #ifdef GATHER_STATISTICS
1089 n_vtable_elems += list_length (virtuals);
1092 /* Initialize the association list for this type, based
1093 on our first approximation. */
1094 TYPE_BINFO_VTABLE (type) = decl;
1095 TYPE_BINFO_VIRTUALS (type) = virtuals;
1097 binfo = TYPE_BINFO (type);
1098 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1102 /* Give TYPE a new virtual function table which is initialized
1103 with a skeleton-copy of its original initialization. The only
1104 entry that changes is the `delta' entry, so we can really
1105 share a lot of structure.
1107 FOR_TYPE is the derived type which caused this table to
1110 BINFO is the type association which provided TYPE for FOR_TYPE.
1112 The order in which vtables are built (by calling this function) for
1113 an object must remain the same, otherwise a binary incompatibility
1117 build_secondary_vtable (binfo, for_type)
1118 tree binfo, for_type;
1121 tree orig_decl = BINFO_VTABLE (binfo);
1134 if (TREE_VIA_VIRTUAL (binfo))
1135 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1136 current_class_type),
1139 if (BINFO_NEW_VTABLE_MARKED (binfo))
1140 /* We already created a vtable for this base. There's no need to
1144 /* Remember that we've created a vtable for this BINFO, so that we
1145 don't try to do so again. */
1146 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1148 /* Make fresh virtual list, so we can smash it later. */
1149 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1151 if (TREE_VIA_VIRTUAL (binfo))
1153 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1155 /* XXX - This should never happen, if it does, the caller should
1156 ensure that the binfo is from for_type's binfos, not from any
1157 base type's. We can remove all this code after a while. */
1158 if (binfo1 != binfo)
1159 warning ("internal inconsistency: binfo offset error for rtti");
1161 offset = BINFO_OFFSET (binfo1);
1164 offset = BINFO_OFFSET (binfo);
1166 set_rtti_entry (BINFO_VIRTUALS (binfo),
1167 size_diffop (size_zero_node, offset),
1170 /* In the new ABI, secondary vtables are laid out as part of the
1171 same structure as the primary vtable. */
1172 if (merge_primary_and_secondary_vtables_p ())
1174 BINFO_VTABLE (binfo) = NULL_TREE;
1178 /* Create the declaration for the secondary vtable. */
1179 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
1180 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1181 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1183 /* We know that the vtable that we are going to create doesn't exist
1184 yet in the global namespace, and when we finish, it will be
1185 pushed into the global namespace. In complex MI hierarchies, we
1186 have to loop while the name we are thinking of adding is globally
1187 defined, adding more name components to the vtable name as we
1188 loop, until the name is unique. This is because in complex MI
1189 cases, we might have the same base more than once. This means
1190 that the order in which this function is called for vtables must
1191 remain the same, otherwise binary compatibility can be
1196 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1200 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1202 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1203 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1204 name = get_identifier (buf);
1206 /* If this name doesn't clash, then we can use it, otherwise
1207 we add more to the name until it is unique. */
1209 if (! IDENTIFIER_GLOBAL_VALUE (name))
1212 /* Set values for next loop through, if the name isn't unique. */
1214 path = BINFO_INHERITANCE_CHAIN (path);
1216 /* We better not run out of stuff to make it unique. */
1217 my_friendly_assert (path != NULL_TREE, 368);
1219 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1221 if (for_type == basetype)
1223 /* If we run out of basetypes in the path, we have already
1224 found created a vtable with that name before, we now
1225 resort to tacking on _%d to distinguish them. */
1227 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1228 buf1 = (char *) alloca (i);
1230 sprintf (buf1, "%s%c%s%c%d",
1231 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1233 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1234 + strlen (buf1) + 1);
1235 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1236 name = get_identifier (buf);
1238 /* If this name doesn't clash, then we can use it,
1239 otherwise we add something different to the name until
1241 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1243 /* Hey, they really like MI don't they? Increase the 3
1244 above to 6, and the 999 to 999999. :-) */
1245 my_friendly_assert (j <= 999, 369);
1250 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1251 new_buf2 = (char *) alloca (i);
1252 sprintf (new_buf2, "%s%c%s",
1253 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1257 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
1258 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1259 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1261 #ifdef GATHER_STATISTICS
1263 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1269 /* Create a new vtable for BINFO which is the hierarchy dominated by
1273 make_new_vtable (t, binfo)
1277 if (binfo == TYPE_BINFO (t))
1278 /* In this case, it is *type*'s vtable we are modifying. We start
1279 with the approximation that it's vtable is that of the
1280 immediate base class. */
1281 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1284 /* This is our very own copy of `basetype' to play with. Later,
1285 we will fill in all the virtual functions that override the
1286 virtual functions in these base classes which are not defined
1287 by the current type. */
1288 return build_secondary_vtable (binfo, t);
1291 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1292 (which is in the hierarchy dominated by T) list FNDECL as its
1293 BV_FN. DELTA is the required adjustment from the `this' pointer
1294 where the vtable entry appears to the `this' required when the
1295 function is actually called. */
1298 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1309 vcall_index = integer_zero_node;
1311 if (fndecl != BV_FN (v)
1312 || !tree_int_cst_equal (delta, BV_DELTA (v))
1313 || !tree_int_cst_equal (vcall_index, BV_VCALL_INDEX (v)))
1317 /* We need a new vtable for BINFO. */
1318 if (make_new_vtable (t, binfo))
1320 /* If we really did make a new vtable, we also made a copy
1321 of the BINFO_VIRTUALS list. Now, we have to find the
1322 corresponding entry in that list. */
1323 *virtuals = BINFO_VIRTUALS (binfo);
1324 while (BV_FN (*virtuals) != BV_FN (v))
1325 *virtuals = TREE_CHAIN (*virtuals);
1329 base_fndecl = BV_FN (v);
1330 BV_DELTA (v) = delta;
1331 BV_VCALL_INDEX (v) = vcall_index;
1334 /* Now assign virtual dispatch information, if unset. We can
1335 dispatch this, through any overridden base function. */
1336 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1338 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1339 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1344 /* Call this function whenever its known that a vtable for T is going
1345 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1346 is initialized to the number of slots that are reserved at the
1347 beginning of the vtable for RTTI information. */
1350 start_vtable (t, has_virtual_p)
1354 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1356 /* If we are using thunks, use two slots at the front, one
1357 for the offset pointer, one for the tdesc pointer.
1358 For ARM-style vtables, use the same slot for both. */
1359 if (flag_vtable_thunks)
1366 /* Add a virtual function to all the appropriate vtables for the class
1367 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1368 allocate a new slot in our table. If it is error_mark_node, we
1369 know that no other function from another vtable is overridden by X.
1370 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1371 vtable for the type, and we build upon the NEW_VIRTUALS list
1375 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1376 has_virtual, fndecl, t)
1377 tree *new_virtuals_p;
1378 tree *overridden_virtuals_p;
1381 tree t; /* Structure type. */
1385 /* If this function doesn't override anything from a base class, we
1386 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1387 override something, we keep it around and assign its DECL_VINDEX
1388 later, in modify_all_vtables. */
1389 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1390 /* We've already dealt with this function. */
1393 new_virtual = build_tree_list (integer_zero_node, fndecl);
1394 BV_VCALL_INDEX (new_virtual) = integer_zero_node;
1396 if (DECL_VINDEX (fndecl) == error_mark_node)
1398 /* FNDECL is a new virtual function; it doesn't override any
1399 virtual function in a base class. */
1401 /* We remember that this was the base sub-object for rtti. */
1402 CLASSTYPE_RTTI (t) = t;
1404 start_vtable (t, has_virtual);
1406 /* Now assign virtual dispatch information. */
1407 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1408 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1410 /* Save the state we've computed on the NEW_VIRTUALS list. */
1411 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1412 *new_virtuals_p = new_virtual;
1416 /* FNDECL overrides a function from a base class. */
1417 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1418 *overridden_virtuals_p = new_virtual;
1422 extern struct obstack *current_obstack;
1424 /* Add method METHOD to class TYPE.
1426 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1427 the class type where the method should be added. */
1430 add_method (type, fields, method)
1431 tree type, *fields, method;
1433 int using = (DECL_CONTEXT (method) != type);
1435 if (fields && *fields)
1436 *fields = build_overload (method, *fields);
1443 if (!CLASSTYPE_METHOD_VEC (type))
1444 /* Make a new method vector. We start with 8 entries. We must
1445 allocate at least two (for constructors and destructors), and
1446 we're going to end up with an assignment operator at some
1449 We could use a TREE_LIST for now, and convert it to a
1450 TREE_VEC in finish_struct, but we would probably waste more
1451 memory making the links in the list than we would by
1452 over-allocating the size of the vector here. Furthermore,
1453 we would complicate all the code that expects this to be a
1455 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1457 method_vec = CLASSTYPE_METHOD_VEC (type);
1458 len = TREE_VEC_LENGTH (method_vec);
1460 if (DECL_NAME (method) == constructor_name (type))
1461 /* A new constructor or destructor. Constructors go in
1462 slot 0; destructors go in slot 1. */
1463 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1466 /* See if we already have an entry with this name. */
1467 for (slot = 2; slot < len; ++slot)
1468 if (!TREE_VEC_ELT (method_vec, slot)
1469 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1471 == DECL_NAME (method)))
1476 /* We need a bigger method vector. */
1477 tree new_vec = make_tree_vec (2 * len);
1478 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1479 (PTR) &TREE_VEC_ELT (new_vec, 0),
1480 len * sizeof (tree));
1482 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1485 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1487 /* Type conversion operators have to come before
1488 ordinary methods; add_conversions depends on this to
1489 speed up looking for conversion operators. So, if
1490 necessary, we slide some of the vector elements up.
1491 In theory, this makes this algorithm O(N^2) but we
1492 don't expect many conversion operators. */
1493 for (slot = 2; slot < len; ++slot)
1495 tree fn = TREE_VEC_ELT (method_vec, slot);
1498 /* There are no more entries in the vector, so we
1499 can insert the new conversion operator here. */
1502 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1503 /* We can insert the new function right at the
1508 if (!TREE_VEC_ELT (method_vec, slot))
1509 /* There is nothing in the Ith slot, so we can avoid
1514 /* We know the last slot in the vector is empty
1515 because we know that at this point there's room
1516 for a new function. */
1517 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1518 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1519 (len - slot - 1) * sizeof (tree));
1520 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1525 if (template_class_depth (type))
1526 /* TYPE is a template class. Don't issue any errors now; wait
1527 until instantiation time to complain. */
1533 /* Check to see if we've already got this method. */
1534 for (fns = TREE_VEC_ELT (method_vec, slot);
1536 fns = OVL_NEXT (fns))
1538 tree fn = OVL_CURRENT (fns);
1540 if (TREE_CODE (fn) != TREE_CODE (method))
1543 if (TREE_CODE (method) != TEMPLATE_DECL)
1545 /* [over.load] Member function declarations with the
1546 same name and the same parameter types cannot be
1547 overloaded if any of them is a static member
1548 function declaration. */
1549 if ((DECL_STATIC_FUNCTION_P (fn)
1550 != DECL_STATIC_FUNCTION_P (method))
1553 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1554 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1556 if (! DECL_STATIC_FUNCTION_P (fn))
1557 parms1 = TREE_CHAIN (parms1);
1558 if (! DECL_STATIC_FUNCTION_P (method))
1559 parms2 = TREE_CHAIN (parms2);
1561 if (compparms (parms1, parms2))
1564 /* Defer to the local function. */
1567 cp_error ("`%#D' and `%#D' cannot be overloaded",
1572 /* Since this is an ordinary function in a
1573 non-template class, it's mangled name can be used
1574 as a unique identifier. This technique is only
1575 an optimization; we would get the same results if
1576 we just used decls_match here. */
1577 if (DECL_ASSEMBLER_NAME (fn)
1578 != DECL_ASSEMBLER_NAME (method))
1581 else if (!decls_match (fn, method))
1584 /* There has already been a declaration of this method
1585 or member template. */
1586 cp_error_at ("`%D' has already been declared in `%T'",
1589 /* We don't call duplicate_decls here to merge the
1590 declarations because that will confuse things if the
1591 methods have inline definitions. In particular, we
1592 will crash while processing the definitions. */
1597 /* Actually insert the new method. */
1598 TREE_VEC_ELT (method_vec, slot)
1599 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1601 /* Add the new binding. */
1602 if (!DECL_CONSTRUCTOR_P (method)
1603 && !DECL_DESTRUCTOR_P (method))
1604 push_class_level_binding (DECL_NAME (method),
1605 TREE_VEC_ELT (method_vec, slot));
1609 /* Subroutines of finish_struct. */
1611 /* Look through the list of fields for this struct, deleting
1612 duplicates as we go. This must be recursive to handle
1615 FIELD is the field which may not appear anywhere in FIELDS.
1616 FIELD_PTR, if non-null, is the starting point at which
1617 chained deletions may take place.
1618 The value returned is the first acceptable entry found
1621 Note that anonymous fields which are not of UNION_TYPE are
1622 not duplicates, they are just anonymous fields. This happens
1623 when we have unnamed bitfields, for example. */
1626 delete_duplicate_fields_1 (field, fields)
1631 if (DECL_NAME (field) == 0)
1633 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1636 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1637 fields = delete_duplicate_fields_1 (x, fields);
1642 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1644 if (DECL_NAME (x) == 0)
1646 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1648 TYPE_FIELDS (TREE_TYPE (x))
1649 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1650 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1653 fields = TREE_CHAIN (fields);
1655 TREE_CHAIN (prev) = TREE_CHAIN (x);
1658 else if (TREE_CODE (field) == USING_DECL)
1659 /* A using declaration may is allowed to appear more than
1660 once. We'll prune these from the field list later, and
1661 handle_using_decl will complain about invalid multiple
1664 else if (DECL_NAME (field) == DECL_NAME (x))
1666 if (TREE_CODE (field) == CONST_DECL
1667 && TREE_CODE (x) == CONST_DECL)
1668 cp_error_at ("duplicate enum value `%D'", x);
1669 else if (TREE_CODE (field) == CONST_DECL
1670 || TREE_CODE (x) == CONST_DECL)
1671 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1673 else if (DECL_DECLARES_TYPE_P (field)
1674 && DECL_DECLARES_TYPE_P (x))
1676 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1678 cp_error_at ("duplicate nested type `%D'", x);
1680 else if (DECL_DECLARES_TYPE_P (field)
1681 || DECL_DECLARES_TYPE_P (x))
1683 /* Hide tag decls. */
1684 if ((TREE_CODE (field) == TYPE_DECL
1685 && DECL_ARTIFICIAL (field))
1686 || (TREE_CODE (x) == TYPE_DECL
1687 && DECL_ARTIFICIAL (x)))
1689 cp_error_at ("duplicate field `%D' (as type and non-type)",
1693 cp_error_at ("duplicate member `%D'", x);
1695 fields = TREE_CHAIN (fields);
1697 TREE_CHAIN (prev) = TREE_CHAIN (x);
1705 delete_duplicate_fields (fields)
1709 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1710 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1713 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1714 legit, otherwise return 0. */
1717 alter_access (t, fdecl, access)
1722 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1725 if (TREE_VALUE (elem) != access)
1727 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1728 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1730 error ("conflicting access specifications for field `%s', ignored",
1731 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1735 /* They're changing the access to the same thing they changed
1736 it to before. That's OK. */
1742 enforce_access (t, fdecl);
1743 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1749 /* Process the USING_DECL, which is a member of T. */
1752 handle_using_decl (using_decl, t)
1756 tree ctype = DECL_INITIAL (using_decl);
1757 tree name = DECL_NAME (using_decl);
1759 = TREE_PRIVATE (using_decl) ? access_private_node
1760 : TREE_PROTECTED (using_decl) ? access_protected_node
1761 : access_public_node;
1763 tree flist = NULL_TREE;
1766 binfo = binfo_or_else (ctype, t);
1770 if (name == constructor_name (ctype)
1771 || name == constructor_name_full (ctype))
1773 cp_error_at ("using-declaration for constructor", using_decl);
1777 fdecl = lookup_member (binfo, name, 0, 0);
1781 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1785 if (BASELINK_P (fdecl))
1786 /* Ignore base type this came from. */
1787 fdecl = TREE_VALUE (fdecl);
1789 old_value = IDENTIFIER_CLASS_VALUE (name);
1792 if (is_overloaded_fn (old_value))
1793 old_value = OVL_CURRENT (old_value);
1795 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1798 old_value = NULL_TREE;
1801 if (is_overloaded_fn (fdecl))
1803 else if (! DECL_LANG_SPECIFIC (fdecl))
1804 my_friendly_abort (20000221);
1808 else if (is_overloaded_fn (old_value))
1811 /* It's OK to use functions from a base when there are functions with
1812 the same name already present in the current class. */;
1815 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1816 cp_error_at (" because of local method `%#D' with same name",
1817 OVL_CURRENT (old_value));
1823 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1824 cp_error_at (" because of local field `%#D' with same name", old_value);
1828 /* Make type T see field decl FDECL with access ACCESS.*/
1830 for (; flist; flist = OVL_NEXT (flist))
1832 add_method (t, 0, OVL_CURRENT (flist));
1833 alter_access (t, OVL_CURRENT (flist), access);
1836 alter_access (t, fdecl, access);
1839 /* Run through the base clases of T, updating
1840 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1841 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1845 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1848 int *cant_have_default_ctor_p;
1849 int *cant_have_const_ctor_p;
1850 int *no_const_asn_ref_p;
1854 int seen_nearly_empty_base_p;
1857 binfos = TYPE_BINFO_BASETYPES (t);
1858 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1859 seen_nearly_empty_base_p = 0;
1861 /* An aggregate cannot have baseclasses. */
1862 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1864 for (i = 0; i < n_baseclasses; ++i)
1869 /* Figure out what base we're looking at. */
1870 base_binfo = TREE_VEC_ELT (binfos, i);
1871 basetype = TREE_TYPE (base_binfo);
1873 /* If the type of basetype is incomplete, then we already
1874 complained about that fact (and we should have fixed it up as
1876 if (TYPE_SIZE (basetype) == 0)
1879 /* The base type is of incomplete type. It is
1880 probably best to pretend that it does not
1882 if (i == n_baseclasses-1)
1883 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1884 TREE_VEC_LENGTH (binfos) -= 1;
1886 for (j = i; j+1 < n_baseclasses; j++)
1887 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1891 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1892 here because the case of virtual functions but non-virtual
1893 dtor is handled in finish_struct_1. */
1894 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1895 && TYPE_HAS_DESTRUCTOR (basetype))
1896 cp_warning ("base class `%#T' has a non-virtual destructor",
1899 /* If the base class doesn't have copy constructors or
1900 assignment operators that take const references, then the
1901 derived class cannot have such a member automatically
1903 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1904 *cant_have_const_ctor_p = 1;
1905 if (TYPE_HAS_ASSIGN_REF (basetype)
1906 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1907 *no_const_asn_ref_p = 1;
1908 /* Similarly, if the base class doesn't have a default
1909 constructor, then the derived class won't have an
1910 automatically generated default constructor. */
1911 if (TYPE_HAS_CONSTRUCTOR (basetype)
1912 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1914 *cant_have_default_ctor_p = 1;
1915 if (! TYPE_HAS_CONSTRUCTOR (t))
1916 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1920 /* If the base class is not empty or nearly empty, then this
1921 class cannot be nearly empty. */
1922 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1923 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1924 /* And if there is more than one nearly empty base, then the
1925 derived class is not nearly empty either. */
1926 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1927 && seen_nearly_empty_base_p)
1928 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1929 /* If this is the first nearly empty base class, then remember
1931 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1932 seen_nearly_empty_base_p = 1;
1934 /* A lot of properties from the bases also apply to the derived
1936 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1937 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1938 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1939 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1940 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1941 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1942 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1943 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1944 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1946 /* Derived classes can implicitly become COMified if their bases
1948 if (CLASSTYPE_COM_INTERFACE (basetype))
1949 CLASSTYPE_COM_INTERFACE (t) = 1;
1950 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1953 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1955 CLASSTYPE_COM_INTERFACE (t) = 0;
1960 /* Make the Ith baseclass of T its primary base. */
1963 set_primary_base (t, i, has_virtual_p)
1970 CLASSTYPE_VFIELD_PARENT (t) = i;
1971 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1972 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1973 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1974 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1975 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1976 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1979 /* Determine the primary class for T. */
1982 determine_primary_base (t, has_virtual_p)
1986 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1988 /* If there are no baseclasses, there is certainly no primary base. */
1989 if (n_baseclasses == 0)
1994 for (i = 0; i < n_baseclasses; i++)
1996 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1997 tree basetype = BINFO_TYPE (base_binfo);
1999 if (TYPE_CONTAINS_VPTR_P (basetype))
2001 /* Even a virtual baseclass can contain our RTTI
2002 information. But, we prefer a non-virtual polymorphic
2004 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2005 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
2007 /* A virtual baseclass can't be the primary base under the
2008 old ABI. And under the new ABI we still prefer a
2009 non-virtual base. */
2010 if (TREE_VIA_VIRTUAL (base_binfo))
2013 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2015 set_primary_base (t, i, has_virtual_p);
2016 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2022 /* Only add unique vfields, and flatten them out as we go. */
2023 for (vfields = CLASSTYPE_VFIELDS (basetype);
2025 vfields = TREE_CHAIN (vfields))
2026 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2027 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2028 CLASSTYPE_VFIELDS (t)
2029 = tree_cons (base_binfo,
2030 VF_BASETYPE_VALUE (vfields),
2031 CLASSTYPE_VFIELDS (t));
2033 if (*has_virtual_p == 0)
2034 set_primary_base (t, i, has_virtual_p);
2039 if (!TYPE_VFIELD (t))
2040 CLASSTYPE_VFIELD_PARENT (t) = -1;
2042 /* The new ABI allows for the use of a "nearly-empty" virtual base
2043 class as the primary base class if no non-virtual polymorphic
2044 base can be found. */
2045 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2046 for (i = 0; i < n_baseclasses; ++i)
2048 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2049 tree basetype = BINFO_TYPE (base_binfo);
2051 if (TREE_VIA_VIRTUAL (base_binfo)
2052 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2054 set_primary_base (t, i, has_virtual_p);
2055 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2060 /* Mark the primary base classes at this point. */
2061 mark_primary_bases (t);
2064 /* Set memoizing fields and bits of T (and its variants) for later
2068 finish_struct_bits (t)
2071 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2073 /* Fix up variants (if any). */
2074 tree variants = TYPE_NEXT_VARIANT (t);
2077 /* These fields are in the _TYPE part of the node, not in
2078 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2079 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2080 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2081 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2082 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
2084 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2085 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2086 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2087 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2088 /* Copy whatever these are holding today. */
2089 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2090 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2091 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2092 TYPE_SIZE (variants) = TYPE_SIZE (t);
2093 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2094 variants = TYPE_NEXT_VARIANT (variants);
2097 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2098 /* For a class w/o baseclasses, `finish_struct' has set
2099 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2100 definition). Similarly for a class whose base classes do not
2101 have vtables. When neither of these is true, we might have
2102 removed abstract virtuals (by providing a definition), added
2103 some (by declaring new ones), or redeclared ones from a base
2104 class. We need to recalculate what's really an abstract virtual
2105 at this point (by looking in the vtables). */
2106 get_pure_virtuals (t);
2110 /* Notice whether this class has type conversion functions defined. */
2111 tree binfo = TYPE_BINFO (t);
2112 tree binfos = BINFO_BASETYPES (binfo);
2115 for (i = n_baseclasses-1; i >= 0; i--)
2117 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2119 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2123 /* If this type has a copy constructor, force its mode to be BLKmode, and
2124 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2125 be passed by invisible reference and prevent it from being returned in
2128 Also do this if the class has BLKmode but can still be returned in
2129 registers, since function_cannot_inline_p won't let us inline
2130 functions returning such a type. This affects the HP-PA. */
2131 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2132 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2133 && CLASSTYPE_NON_AGGREGATE (t)))
2136 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2137 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2139 TYPE_MODE (variants) = BLKmode;
2140 TREE_ADDRESSABLE (variants) = 1;
2145 /* Issue warnings about T having private constructors, but no friends,
2148 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2149 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2150 non-private static member functions. */
2153 maybe_warn_about_overly_private_class (t)
2156 int has_member_fn = 0;
2157 int has_nonprivate_method = 0;
2160 if (!warn_ctor_dtor_privacy
2161 /* If the class has friends, those entities might create and
2162 access instances, so we should not warn. */
2163 || (CLASSTYPE_FRIEND_CLASSES (t)
2164 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2165 /* We will have warned when the template was declared; there's
2166 no need to warn on every instantiation. */
2167 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2168 /* There's no reason to even consider warning about this
2172 /* We only issue one warning, if more than one applies, because
2173 otherwise, on code like:
2176 // Oops - forgot `public:'
2182 we warn several times about essentially the same problem. */
2184 /* Check to see if all (non-constructor, non-destructor) member
2185 functions are private. (Since there are no friends or
2186 non-private statics, we can't ever call any of the private member
2188 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2189 /* We're not interested in compiler-generated methods; they don't
2190 provide any way to call private members. */
2191 if (!DECL_ARTIFICIAL (fn))
2193 if (!TREE_PRIVATE (fn))
2195 if (DECL_STATIC_FUNCTION_P (fn))
2196 /* A non-private static member function is just like a
2197 friend; it can create and invoke private member
2198 functions, and be accessed without a class
2202 has_nonprivate_method = 1;
2205 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2209 if (!has_nonprivate_method && has_member_fn)
2211 /* There are no non-private methods, and there's at least one
2212 private member function that isn't a constructor or
2213 destructor. (If all the private members are
2214 constructors/destructors we want to use the code below that
2215 issues error messages specifically referring to
2216 constructors/destructors.) */
2218 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2219 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2220 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2221 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2223 has_nonprivate_method = 1;
2226 if (!has_nonprivate_method)
2228 cp_warning ("all member functions in class `%T' are private", t);
2233 /* Even if some of the member functions are non-private, the class
2234 won't be useful for much if all the constructors or destructors
2235 are private: such an object can never be created or destroyed. */
2236 if (TYPE_HAS_DESTRUCTOR (t))
2238 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2240 if (TREE_PRIVATE (dtor))
2242 cp_warning ("`%#T' only defines a private destructor and has no friends",
2248 if (TYPE_HAS_CONSTRUCTOR (t))
2250 int nonprivate_ctor = 0;
2252 /* If a non-template class does not define a copy
2253 constructor, one is defined for it, enabling it to avoid
2254 this warning. For a template class, this does not
2255 happen, and so we would normally get a warning on:
2257 template <class T> class C { private: C(); };
2259 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2260 complete non-template or fully instantiated classes have this
2262 if (!TYPE_HAS_INIT_REF (t))
2263 nonprivate_ctor = 1;
2265 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2269 tree ctor = OVL_CURRENT (fn);
2270 /* Ideally, we wouldn't count copy constructors (or, in
2271 fact, any constructor that takes an argument of the
2272 class type as a parameter) because such things cannot
2273 be used to construct an instance of the class unless
2274 you already have one. But, for now at least, we're
2276 if (! TREE_PRIVATE (ctor))
2278 nonprivate_ctor = 1;
2283 if (nonprivate_ctor == 0)
2285 cp_warning ("`%#T' only defines private constructors and has no friends",
2292 /* Function to help qsort sort FIELD_DECLs by name order. */
2295 field_decl_cmp (x, y)
2298 if (DECL_NAME (*x) == DECL_NAME (*y))
2299 /* A nontype is "greater" than a type. */
2300 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2301 if (DECL_NAME (*x) == NULL_TREE)
2303 if (DECL_NAME (*y) == NULL_TREE)
2305 if (DECL_NAME (*x) < DECL_NAME (*y))
2310 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2313 method_name_cmp (m1, m2)
2314 const tree *m1, *m2;
2316 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2318 if (*m1 == NULL_TREE)
2320 if (*m2 == NULL_TREE)
2322 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2327 /* Warn about duplicate methods in fn_fields. Also compact method
2328 lists so that lookup can be made faster.
2330 Data Structure: List of method lists. The outer list is a
2331 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2332 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2333 links the entire list of methods for TYPE_METHODS. Friends are
2334 chained in the same way as member functions (? TREE_CHAIN or
2335 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2336 list. That allows them to be quickly deleted, and requires no
2339 If there are any constructors/destructors, they are moved to the
2340 front of the list. This makes pushclass more efficient.
2342 @@ The above comment is obsolete. It mostly describes what add_method
2343 @@ and add_implicitly_declared_members do.
2345 Sort methods that are not special (i.e., constructors, destructors, and
2346 type conversion operators) so that we can find them faster in search. */
2349 finish_struct_methods (t)
2354 tree ctor_name = constructor_name (t);
2357 if (!TYPE_METHODS (t))
2359 /* Clear these for safety; perhaps some parsing error could set
2360 these incorrectly. */
2361 TYPE_HAS_CONSTRUCTOR (t) = 0;
2362 TYPE_HAS_DESTRUCTOR (t) = 0;
2363 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2367 method_vec = CLASSTYPE_METHOD_VEC (t);
2368 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2369 len = TREE_VEC_LENGTH (method_vec);
2371 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2372 and the next few with type conversion operators (if any). */
2373 for (fn_fields = TYPE_METHODS (t); fn_fields;
2374 fn_fields = TREE_CHAIN (fn_fields))
2376 tree fn_name = DECL_NAME (fn_fields);
2378 /* Clear out this flag.
2380 @@ Doug may figure out how to break
2381 @@ this with nested classes and friends. */
2382 DECL_IN_AGGR_P (fn_fields) = 0;
2384 /* Note here that a copy ctor is private, so we don't dare generate
2385 a default copy constructor for a class that has a member
2386 of this type without making sure they have access to it. */
2387 if (fn_name == ctor_name)
2389 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2390 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2392 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2393 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2395 if (TREE_CHAIN (parmtypes) == NULL_TREE
2396 || TREE_CHAIN (parmtypes) == void_list_node
2397 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2399 if (TREE_PROTECTED (fn_fields))
2400 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2401 else if (TREE_PRIVATE (fn_fields))
2402 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2406 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2408 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2410 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2412 if (TREE_PROTECTED (fn_fields))
2413 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2414 else if (TREE_PRIVATE (fn_fields))
2415 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2420 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2421 /* We thought there was a destructor, but there wasn't. Some
2422 parse errors cause this anomalous situation. */
2423 TYPE_HAS_DESTRUCTOR (t) = 0;
2425 /* Issue warnings about private constructors and such. If there are
2426 no methods, then some public defaults are generated. */
2427 maybe_warn_about_overly_private_class (t);
2429 /* Now sort the methods. */
2430 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2432 TREE_VEC_LENGTH (method_vec) = len;
2434 /* The type conversion ops have to live at the front of the vec, so we
2436 for (slot = 2; slot < len; ++slot)
2438 tree fn = TREE_VEC_ELT (method_vec, slot);
2440 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2444 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2445 (int (*)(const void *, const void *))method_name_cmp);
2448 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2451 duplicate_tag_error (t)
2454 cp_error ("redefinition of `%#T'", t);
2455 cp_error_at ("previous definition here", t);
2457 /* Pretend we haven't defined this type. */
2459 /* All of the component_decl's were TREE_CHAINed together in the parser.
2460 finish_struct_methods walks these chains and assembles all methods with
2461 the same base name into DECL_CHAINs. Now we don't need the parser chains
2462 anymore, so we unravel them. */
2464 /* This used to be in finish_struct, but it turns out that the
2465 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2467 if (CLASSTYPE_METHOD_VEC (t))
2469 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2470 int i, len = TREE_VEC_LENGTH (method_vec);
2471 for (i = 0; i < len; i++)
2473 tree unchain = TREE_VEC_ELT (method_vec, i);
2474 while (unchain != NULL_TREE)
2476 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2477 unchain = OVL_NEXT (unchain);
2482 if (TYPE_LANG_SPECIFIC (t))
2484 tree binfo = TYPE_BINFO (t);
2485 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2486 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2487 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2488 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2490 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2491 BINFO_BASETYPES(binfo) = NULL_TREE;
2493 TYPE_BINFO (t) = binfo;
2494 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2495 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2496 TYPE_REDEFINED (t) = 1;
2497 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2498 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2500 TYPE_SIZE (t) = NULL_TREE;
2501 TYPE_MODE (t) = VOIDmode;
2502 TYPE_FIELDS (t) = NULL_TREE;
2503 TYPE_METHODS (t) = NULL_TREE;
2504 TYPE_VFIELD (t) = NULL_TREE;
2505 TYPE_CONTEXT (t) = NULL_TREE;
2506 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2509 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2510 vbase and vcall offsets, etc. Set its type and call the backend
2514 layout_vtable_decl (binfo, n)
2521 itype = size_int (n);
2522 atype = build_cplus_array_type (vtable_entry_type,
2523 build_index_type (itype));
2524 layout_type (atype);
2526 /* We may have to grow the vtable. */
2527 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2529 tree vtable = BINFO_VTABLE (binfo);
2531 TREE_TYPE (vtable) = atype;
2532 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = 0;
2533 layout_decl (vtable, 0);
2535 /* At one time the vtable info was grabbed 2 words at a time. This
2536 fails on Sparc unless you have 8-byte alignment. */
2537 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2538 DECL_ALIGN (vtable));
2542 /* Returns the number of virtual function table entries (excluding
2543 RTTI information, vbase and vcall offests, etc.) in the vtable for
2547 num_vfun_entries (binfo)
2550 return list_length (skip_rtti_stuff (binfo,
2555 /* Called from num_extra_vtbl_entries via dfs_walk. */
2558 dfs_count_virtuals (binfo, data)
2562 /* Non-primary bases are not interesting; all of the virtual
2563 function table entries have been overridden. */
2564 if (!BINFO_PRIMARY_MARKED_P (binfo))
2565 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2570 /* Returns the number of extra entries (at negative indices) required
2571 for BINFO's vtable. */
2574 num_extra_vtbl_entries (binfo)
2580 type = BINFO_TYPE (binfo);
2583 /* There is an entry for the offset to each virtual base. */
2584 if (vbase_offsets_in_vtable_p ())
2585 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2587 /* If this is a virtual base, there are entries for each virtual
2588 function defined in this class or its bases. */
2589 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2591 vcall_offset_data vod;
2597 dfs_vcall_offset_queue_p,
2599 entries += vod.offsets;
2602 return entries ? size_int (entries) : size_zero_node;
2605 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2606 where the vptr should actually point. */
2609 size_extra_vtbl_entries (binfo)
2612 tree offset = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (vtable_entry_type),
2613 num_extra_vtbl_entries (binfo));
2614 return fold (offset);
2617 /* Construct the initializer for BINFOs virtual function table. BINFO
2618 is part of the hierarchy dominated by T. The value returned is a
2619 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
2620 DECL_INITIAL for a vtable. */
2623 build_vtbl_initializer (binfo, t)
2627 tree v = BINFO_VIRTUALS (binfo);
2628 tree inits = NULL_TREE;
2629 tree type = BINFO_TYPE (binfo);
2631 /* Add entries to the vtable that indicate how to adjust the this
2632 pointer when calling a virtual function in this class. */
2633 inits = build_vcall_offset_vtbl_entries (binfo, t);
2635 /* Add entries to the vtable for offsets to our virtual bases. */
2636 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2639 /* Process the RTTI stuff at the head of the list. If we're not
2640 using vtable thunks, then the RTTI entry is just an ordinary
2641 function, and we can process it just like the other virtual
2642 function entries. */
2643 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2648 /* The first entry is an offset. */
2649 offset = TREE_PURPOSE (v);
2650 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2653 /* Convert the offset to look like a function pointer, so that
2654 we can put it in the vtable. */
2655 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2656 TREE_CONSTANT (init) = 1;
2657 inits = tree_cons (NULL_TREE, init, inits);
2661 if (new_abi_rtti_p ())
2663 tree decl = TREE_VALUE (v);
2666 decl = build_unary_op (ADDR_EXPR, decl, 0);
2668 decl = integer_zero_node;
2669 decl = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
2670 TREE_CONSTANT (decl) = 1;
2671 decl = build_vtable_entry (integer_zero_node, integer_zero_node,
2673 inits = tree_cons (NULL_TREE, decl, inits);
2677 /* In the old abi the second entry (the tdesc pointer) is
2678 just an ordinary function, so it can be dealt with like the
2679 virtual functions. */
2682 /* Go through all the ordinary virtual functions, building up
2692 /* Pull the offset for `this', and the function to call, out of
2694 delta = BV_DELTA (v);
2695 vcall_index = BV_VCALL_INDEX (v);
2697 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2698 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2700 /* You can't call an abstract virtual function; it's abstract.
2701 So, we replace these functions with __pure_virtual. */
2702 if (DECL_PURE_VIRTUAL_P (fn))
2705 /* Take the address of the function, considering it to be of an
2706 appropriate generic type. */
2707 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
2708 /* The address of a function can't change. */
2709 TREE_CONSTANT (pfn) = 1;
2710 /* Enter it in the vtable. */
2711 init = build_vtable_entry (delta, vcall_index, pfn);
2712 /* And add it to the chain of initializers. */
2713 inits = tree_cons (NULL_TREE, init, inits);
2719 /* The initializers were built up in reverse order; straighten them
2721 return nreverse (inits);
2724 /* Initialize the vtable for BINFO with the INITS. */
2727 initialize_vtable (binfo, inits)
2734 layout_vtable_decl (binfo, list_length (inits));
2735 decl = BINFO_VTABLE (binfo);
2736 context = DECL_CONTEXT (decl);
2737 DECL_CONTEXT (decl) = 0;
2738 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
2739 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2740 DECL_CONTEXT (decl) = context;
2743 /* Called from finish_vtbls via dfs_walk. */
2746 dfs_finish_vtbls (binfo, data)
2750 if (!BINFO_PRIMARY_MARKED_P (binfo)
2751 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2752 && BINFO_NEW_VTABLE_MARKED (binfo))
2753 initialize_vtable (binfo,
2754 build_vtbl_initializer (binfo, (tree) data));
2756 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2757 SET_BINFO_MARKED (binfo);
2762 /* Called from finish_vtbls via dfs_walk when using the new ABI.
2763 Accumulates the vtable initializers for all of the vtables into
2764 TREE_VALUE (DATA). */
2767 dfs_accumulate_vtbl_inits (binfo, data)
2771 if (!BINFO_PRIMARY_MARKED_P (binfo)
2772 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2773 && BINFO_NEW_VTABLE_MARKED (binfo))
2779 t = TREE_PURPOSE (l);
2781 /* If this is a secondary vtable, record its location. */
2782 if (binfo != TYPE_BINFO (t))
2786 vtbl = TYPE_BINFO_VTABLE (t);
2787 vtbl = build1 (ADDR_EXPR,
2788 build_pointer_type (TREE_TYPE (vtbl)),
2790 BINFO_VTABLE (binfo)
2791 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
2792 size_binop (MULT_EXPR,
2793 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
2794 size_int (list_length (TREE_VALUE (l)))));
2797 /* Add the initializers for this vtable to the initailizers for
2798 the other vtables we've already got. */
2800 = chainon (TREE_VALUE (l),
2801 build_vtbl_initializer (binfo, t));
2804 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2805 SET_BINFO_MARKED (binfo);
2810 /* Create all the necessary vtables for T and its base classes. */
2816 if (merge_primary_and_secondary_vtables_p ())
2820 /* Under the new ABI, we lay out the primary and secondary
2821 vtables in one contiguous vtable. The primary vtable is
2822 first, followed by the secondary vtables as encountered in a
2823 pre-order depth-first left-to-right traversal. */
2824 list = build_tree_list (t, NULL_TREE);
2825 dfs_walk_real (TYPE_BINFO (t),
2826 dfs_accumulate_vtbl_inits,
2828 dfs_unmarked_real_bases_queue_p,
2830 if (TYPE_BINFO_VTABLE (t))
2831 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
2834 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2835 dfs_unmarked_real_bases_queue_p, t);
2837 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2838 dfs_marked_real_bases_queue_p, t);
2841 /* True if we should override the given BASE_FNDECL with the given
2845 overrides (fndecl, base_fndecl)
2846 tree fndecl, base_fndecl;
2848 /* Destructors have special names. */
2849 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2850 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2852 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2853 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2855 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2857 tree types, base_types;
2859 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2860 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2862 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2863 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2864 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2865 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2866 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2872 typedef struct find_final_overrider_data_s {
2873 /* The function for which we are trying to find a final overrider. */
2875 /* The base class in which the function was declared. */
2876 tree declaring_base;
2877 /* The most derived class in the hierarchy. */
2878 tree most_derived_type;
2879 /* The final overriding function. */
2881 /* The BINFO for the class in which the final overriding function
2883 tree overriding_base;
2884 } find_final_overrider_data;
2886 /* Called from find_final_overrider via dfs_walk. */
2889 dfs_find_final_overrider (binfo, data)
2893 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2895 if (same_type_p (BINFO_TYPE (binfo),
2896 BINFO_TYPE (ffod->declaring_base))
2897 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2898 BINFO_OFFSET (ffod->declaring_base)))
2903 /* We've found a path to the declaring base. Walk down the path
2904 looking for an overrider for FN. */
2905 for (path = reverse_path (binfo);
2907 path = TREE_CHAIN (path))
2909 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2911 method = TREE_CHAIN (method))
2912 if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2919 /* If we found an overrider, record the overriding function, and
2920 the base from which it came. */
2923 if (ffod->overriding_fn && ffod->overriding_fn != method)
2925 /* We've found a different overrider along a different
2926 path. That can be OK if the new one overrides the
2929 struct S { virtual void f(); };
2930 struct T : public virtual S { virtual void f(); };
2931 struct U : public virtual S, public virtual T {};
2933 Here `T::f' is the final overrider for `S::f'. */
2934 if (strictly_overrides (method, ffod->overriding_fn))
2936 ffod->overriding_fn = method;
2937 ffod->overriding_base = TREE_VALUE (path);
2939 else if (!strictly_overrides (ffod->overriding_fn, method))
2941 cp_error ("no unique final overrider for `%D' in `%T'",
2942 ffod->most_derived_type,
2944 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2945 cp_error (" `%#D'", method);
2946 return error_mark_node;
2949 else if (ffod->overriding_base
2950 && (!tree_int_cst_equal
2951 (BINFO_OFFSET (TREE_VALUE (path)),
2952 BINFO_OFFSET (ffod->overriding_base))))
2954 /* We've found two instances of the same base that
2955 provide overriders. */
2956 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2958 BINFO_TYPE (ffod->overriding_base),
2959 ffod->most_derived_type);
2960 return error_mark_node;
2964 ffod->overriding_fn = method;
2965 ffod->overriding_base = TREE_VALUE (path);
2973 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2974 FN and whose TREE_VALUE is the binfo for the base where the
2975 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2976 base object in which FN is declared. */
2979 find_final_overrider (t, binfo, fn)
2984 find_final_overrider_data ffod;
2986 /* Getting this right is a little tricky. This is legal:
2988 struct S { virtual void f (); };
2989 struct T { virtual void f (); };
2990 struct U : public S, public T { };
2992 even though calling `f' in `U' is ambiguous. But,
2994 struct R { virtual void f(); };
2995 struct S : virtual public R { virtual void f (); };
2996 struct T : virtual public R { virtual void f (); };
2997 struct U : public S, public T { };
2999 is not -- there's no way to decide whether to put `S::f' or
3000 `T::f' in the vtable for `R'.
3002 The solution is to look at all paths to BINFO. If we find
3003 different overriders along any two, then there is a problem. */
3005 ffod.declaring_base = binfo;
3006 ffod.most_derived_type = t;
3007 ffod.overriding_fn = NULL_TREE;
3008 ffod.overriding_base = NULL_TREE;
3010 if (dfs_walk (TYPE_BINFO (t),
3011 dfs_find_final_overrider,
3014 return error_mark_node;
3016 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
3019 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
3020 the front. If non-NULL, N is set to the number of entries
3024 skip_rtti_stuff (binfo, t, n)
3031 if (CLASSTYPE_COM_INTERFACE (t))
3036 virtuals = BINFO_VIRTUALS (binfo);
3039 /* We always reserve a slot for the offset/tdesc entry. */
3042 virtuals = TREE_CHAIN (virtuals);
3044 if (flag_vtable_thunks && virtuals)
3046 /* The second slot is reserved for the tdesc pointer when thunks
3050 virtuals = TREE_CHAIN (virtuals);
3056 /* Called via dfs_walk. Returns BINFO if BINFO has the same type as
3057 DATA (which is really an _TYPE node). */
3060 dfs_find_base (binfo, data)
3064 return (same_type_p (BINFO_TYPE (binfo), (tree) data)
3065 ? binfo : NULL_TREE);
3068 /* Called from modify_all_vtables via dfs_walk. */
3071 dfs_modify_vtables (binfo, data)
3075 if (/* There's no need to modify the vtable for a primary base;
3076 we're not going to use that vtable anyhow. */
3077 !BINFO_PRIMARY_MARKED_P (binfo)
3078 /* Similarly, a base without a vtable needs no modification. */
3079 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3087 /* If we're supporting RTTI then we always need a new vtable to
3088 point to the RTTI information. Under the new ABI we may need
3089 a new vtable to contain vcall and vbase offsets. */
3090 if (flag_rtti || flag_new_abi)
3091 make_new_vtable (t, binfo);
3093 /* Now, go through each of the virtual functions in the virtual
3094 function table for BINFO. Find the final overrider, and
3095 update the BINFO_VIRTUALS list appropriately. */
3096 for (virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL),
3097 old_virtuals = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (binfo)),
3101 virtuals = TREE_CHAIN (virtuals),
3102 old_virtuals = TREE_CHAIN (old_virtuals))
3111 /* Find the function which originally caused this vtable
3112 entry to be present. */
3113 fn = BV_FN (old_virtuals);
3114 vindex = DECL_VINDEX (fn);
3115 b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
3116 fn = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (b)),
3119 while (i < TREE_INT_CST_LOW (vindex))
3121 fn = TREE_CHAIN (fn);
3126 /* Handle the case of a virtual function defined in BINFO
3128 overrider = find_final_overrider (t, b, fn);
3129 if (overrider == error_mark_node)
3132 /* The `this' pointer needs to be adjusted from pointing to
3133 BINFO to pointing at the base where the final overrider
3135 delta = size_binop (PLUS_EXPR,
3136 get_derived_offset (binfo,
3137 DECL_VIRTUAL_CONTEXT (fn)),
3138 BINFO_OFFSET (binfo));
3139 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
3141 modify_vtable_entry (t,
3143 TREE_PURPOSE (overrider),
3149 SET_BINFO_MARKED (binfo);
3154 /* Update all of the primary and secondary vtables for T. Create new
3155 vtables as required, and initialize their RTTI information. Each
3156 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3157 function from a base class; find and modify the appropriate entries
3158 to point to the overriding functions. Returns a list, in
3159 declaration order, of the functions that are overridden in this
3160 class, but do not appear in the primary base class vtable, and
3161 which should therefore be appended to the end of the vtable for T. */
3164 modify_all_vtables (t, has_virtual_p, overridden_virtuals)
3167 tree overridden_virtuals;
3171 binfo = TYPE_BINFO (t);
3173 /* Update all of the vtables. */
3176 dfs_unmarked_real_bases_queue_p,
3178 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
3180 /* If we should include overriding functions for secondary vtables
3181 in our primary vtable, add them now. */
3182 if (all_overridden_vfuns_in_vtables_p ())
3184 tree *fnsp = &overridden_virtuals;
3188 tree fn = TREE_VALUE (*fnsp);
3190 if (BINFO_VIRTUALS (binfo)
3191 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3193 /* We know we need a vtable for this class now. */
3194 start_vtable (t, has_virtual_p);
3195 /* Set the vtable index. */
3197 = build_shared_int_cst ((*has_virtual_p)++);
3198 /* We don't need to convert to a base class when calling
3200 DECL_VIRTUAL_CONTEXT (fn) = t;
3202 /* We don't need to adjust the `this' pointer when
3203 calling this function. */
3204 BV_DELTA (*fnsp) = integer_zero_node;
3205 BV_VCALL_INDEX (*fnsp) = integer_zero_node;
3207 /* This is an overridden function not already in our
3209 fnsp = &TREE_CHAIN (*fnsp);
3212 /* We've already got an entry for this function. Skip
3214 *fnsp = TREE_CHAIN (*fnsp);
3218 overridden_virtuals = NULL_TREE;
3220 return overridden_virtuals;
3223 /* Here, we already know that they match in every respect.
3224 All we have to check is where they had their declarations. */
3227 strictly_overrides (fndecl1, fndecl2)
3228 tree fndecl1, fndecl2;
3230 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
3231 DECL_CONTEXT (fndecl1),
3233 if (distance == -2 || distance > 0)
3238 /* Get the base virtual function declarations in T that are either
3239 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3240 the overrider/hider. */
3243 get_basefndecls (fndecl, t)
3246 tree methods = TYPE_METHODS (t);
3247 tree base_fndecls = NULL_TREE;
3248 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3249 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3253 if (TREE_CODE (methods) == FUNCTION_DECL
3254 && DECL_VINDEX (methods) != NULL_TREE
3255 && DECL_NAME (fndecl) == DECL_NAME (methods))
3256 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3258 methods = TREE_CHAIN (methods);
3262 return base_fndecls;
3264 for (i = 0; i < n_baseclasses; i++)
3266 tree base_binfo = TREE_VEC_ELT (binfos, i);
3267 tree basetype = BINFO_TYPE (base_binfo);
3269 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3273 return base_fndecls;
3276 /* Mark the functions that have been hidden with their overriders.
3277 Since we start out with all functions already marked with a hider,
3278 no need to mark functions that are just hidden.
3280 Subroutine of warn_hidden. */
3283 mark_overriders (fndecl, base_fndecls)
3284 tree fndecl, base_fndecls;
3286 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3288 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3289 TREE_PURPOSE (base_fndecls) = fndecl;
3293 /* If this declaration supersedes the declaration of
3294 a method declared virtual in the base class, then
3295 mark this field as being virtual as well. */
3298 check_for_override (decl, ctype)
3301 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3302 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3303 int virtualp = DECL_VIRTUAL_P (decl);
3304 int found_overriden_fn = 0;
3306 for (i = 0; i < n_baselinks; i++)
3308 tree base_binfo = TREE_VEC_ELT (binfos, i);
3309 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3311 tree tmp = get_matching_virtual
3313 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3315 if (tmp && !found_overriden_fn)
3317 /* If this function overrides some virtual in some base
3318 class, then the function itself is also necessarily
3319 virtual, even if the user didn't explicitly say so. */
3320 DECL_VIRTUAL_P (decl) = 1;
3322 /* The TMP we really want is the one from the deepest
3323 baseclass on this path, taking care not to
3324 duplicate if we have already found it (via another
3325 path to its virtual baseclass. */
3326 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3328 cp_error_at ("`static %#D' cannot be declared", decl);
3329 cp_error_at (" since `virtual %#D' declared in base class",
3335 /* Set DECL_VINDEX to a value that is neither an
3336 INTEGER_CST nor the error_mark_node so that
3337 add_virtual_function will realize this is an
3338 overridden function. */
3340 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3342 /* We now know that DECL overrides something,
3343 which is all that is important. But, we must
3344 continue to iterate through all the base-classes
3345 in order to allow get_matching_virtual to check for
3346 various illegal overrides. */
3347 found_overriden_fn = 1;
3353 if (DECL_VINDEX (decl) == NULL_TREE)
3354 DECL_VINDEX (decl) = error_mark_node;
3355 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3359 /* Warn about hidden virtual functions that are not overridden in t.
3360 We know that constructors and destructors don't apply. */
3366 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3367 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3370 /* We go through each separately named virtual function. */
3371 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3373 tree fns = TREE_VEC_ELT (method_vec, i);
3374 tree fndecl = NULL_TREE;
3376 tree base_fndecls = NULL_TREE;
3377 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3378 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3380 /* First see if we have any virtual functions in this batch. */
3381 for (; fns; fns = OVL_NEXT (fns))
3383 fndecl = OVL_CURRENT (fns);
3384 if (DECL_VINDEX (fndecl))
3388 if (fns == NULL_TREE)
3391 /* First we get a list of all possible functions that might be
3392 hidden from each base class. */
3393 for (i = 0; i < n_baseclasses; i++)
3395 tree base_binfo = TREE_VEC_ELT (binfos, i);
3396 tree basetype = BINFO_TYPE (base_binfo);
3398 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3402 fns = OVL_NEXT (fns);
3404 /* ...then mark up all the base functions with overriders, preferring
3405 overriders to hiders. */
3407 for (; fns; fns = OVL_NEXT (fns))
3409 fndecl = OVL_CURRENT (fns);
3410 if (DECL_VINDEX (fndecl))
3411 mark_overriders (fndecl, base_fndecls);
3414 /* Now give a warning for all base functions without overriders,
3415 as they are hidden. */
3416 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3418 if (! overrides (TREE_PURPOSE (base_fndecls),
3419 TREE_VALUE (base_fndecls)))
3421 /* Here we know it is a hider, and no overrider exists. */
3422 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3423 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3429 /* Check for things that are invalid. There are probably plenty of other
3430 things we should check for also. */
3433 finish_struct_anon (t)
3438 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3440 if (TREE_STATIC (field))
3442 if (TREE_CODE (field) != FIELD_DECL)
3445 if (DECL_NAME (field) == NULL_TREE
3446 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3448 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3449 for (; elt; elt = TREE_CHAIN (elt))
3451 if (DECL_ARTIFICIAL (elt))
3454 if (DECL_NAME (elt) == constructor_name (t))
3455 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3458 if (TREE_CODE (elt) != FIELD_DECL)
3460 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3465 if (TREE_PRIVATE (elt))
3466 cp_pedwarn_at ("private member `%#D' in anonymous union",
3468 else if (TREE_PROTECTED (elt))
3469 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3472 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3473 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3479 extern int interface_only, interface_unknown;
3481 /* Create default constructors, assignment operators, and so forth for
3482 the type indicated by T, if they are needed.
3483 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3484 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3485 cannot have a default constructor, copy constructor taking a const
3486 reference argument, or an assignment operator, respectively. If a
3487 virtual destructor is created, its DECL is returned; otherwise the
3488 return value is NULL_TREE. */
3491 add_implicitly_declared_members (t, cant_have_default_ctor,
3492 cant_have_const_cctor,
3493 cant_have_assignment)
3495 int cant_have_default_ctor;
3496 int cant_have_const_cctor;
3497 int cant_have_assignment;
3500 tree implicit_fns = NULL_TREE;
3501 tree name = TYPE_IDENTIFIER (t);
3502 tree virtual_dtor = NULL_TREE;
3506 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3508 default_fn = cons_up_default_function (t, name, 0);
3509 check_for_override (default_fn, t);
3511 /* If we couldn't make it work, then pretend we didn't need it. */
3512 if (default_fn == void_type_node)
3513 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3516 TREE_CHAIN (default_fn) = implicit_fns;
3517 implicit_fns = default_fn;
3519 if (DECL_VINDEX (default_fn))
3520 virtual_dtor = default_fn;
3523 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3525 /* Default constructor. */
3526 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3528 default_fn = cons_up_default_function (t, name, 2);
3529 TREE_CHAIN (default_fn) = implicit_fns;
3530 implicit_fns = default_fn;
3533 /* Copy constructor. */
3534 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3536 /* ARM 12.18: You get either X(X&) or X(const X&), but
3538 default_fn = cons_up_default_function (t, name,
3539 3 + cant_have_const_cctor);
3540 TREE_CHAIN (default_fn) = implicit_fns;
3541 implicit_fns = default_fn;
3544 /* Assignment operator. */
3545 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3547 default_fn = cons_up_default_function (t, name,
3548 5 + cant_have_assignment);
3549 TREE_CHAIN (default_fn) = implicit_fns;
3550 implicit_fns = default_fn;
3553 /* Now, hook all of the new functions on to TYPE_METHODS,
3554 and add them to the CLASSTYPE_METHOD_VEC. */
3555 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3556 add_method (t, 0, *f);
3557 *f = TYPE_METHODS (t);
3558 TYPE_METHODS (t) = implicit_fns;
3560 return virtual_dtor;
3563 /* Subroutine of finish_struct_1. Recursively count the number of fields
3564 in TYPE, including anonymous union members. */
3567 count_fields (fields)
3572 for (x = fields; x; x = TREE_CHAIN (x))
3574 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3575 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3582 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3583 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3586 add_fields_to_vec (fields, field_vec, idx)
3587 tree fields, field_vec;
3591 for (x = fields; x; x = TREE_CHAIN (x))
3593 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3594 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3596 TREE_VEC_ELT (field_vec, idx++) = x;
3601 /* FIELD is a bit-field. We are finishing the processing for its
3602 enclosing type. Issue any appropriate messages and set appropriate
3606 check_bitfield_decl (field)
3609 tree type = TREE_TYPE (field);
3611 /* Invalid bit-field size done by grokfield. */
3612 /* Detect invalid bit-field type. Simply checking if TYPE is
3613 integral is insufficient, as that is the array core of the field
3614 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3615 if (DECL_INITIAL (field)
3616 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3618 cp_error_at ("bit-field `%#D' with non-integral type", field);
3619 DECL_INITIAL (field) = NULL;
3622 /* Detect and ignore out of range field width. */
3623 if (DECL_INITIAL (field))
3625 tree w = DECL_INITIAL (field);
3626 register int width = 0;
3628 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3631 /* detect invalid field size. */
3632 if (TREE_CODE (w) == CONST_DECL)
3633 w = DECL_INITIAL (w);
3634 else if (TREE_READONLY_DECL_P (w))
3635 w = decl_constant_value (w);
3637 if (TREE_CODE (w) != INTEGER_CST)
3639 cp_error_at ("bit-field `%D' width not an integer constant",
3641 DECL_INITIAL (field) = NULL_TREE;
3643 else if (width = TREE_INT_CST_LOW (w),
3646 DECL_INITIAL (field) = NULL;
3647 cp_error_at ("negative width in bit-field `%D'", field);
3649 else if (width == 0 && DECL_NAME (field) != 0)
3651 DECL_INITIAL (field) = NULL;
3652 cp_error_at ("zero width for bit-field `%D'", field);
3655 > TYPE_PRECISION (long_long_unsigned_type_node))
3657 /* The backend will dump if you try to use something too
3659 DECL_INITIAL (field) = NULL;
3660 sorry ("bit-fields larger than %d bits",
3661 TYPE_PRECISION (long_long_unsigned_type_node));
3662 cp_error_at (" in declaration of `%D'", field);
3664 else if (width > TYPE_PRECISION (type)
3665 && TREE_CODE (type) != ENUMERAL_TYPE
3666 && TREE_CODE (type) != BOOLEAN_TYPE)
3667 cp_warning_at ("width of `%D' exceeds its type", field);
3668 else if (TREE_CODE (type) == ENUMERAL_TYPE
3669 && ((min_precision (TYPE_MIN_VALUE (type),
3670 TREE_UNSIGNED (type)) > width)
3671 || (min_precision (TYPE_MAX_VALUE (type),
3672 TREE_UNSIGNED (type)) > width)))
3673 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3676 if (DECL_INITIAL (field))
3678 DECL_INITIAL (field) = NULL_TREE;
3679 DECL_SIZE (field) = bitsize_int (width);
3680 DECL_BIT_FIELD (field) = 1;
3684 #ifdef EMPTY_FIELD_BOUNDARY
3685 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3686 EMPTY_FIELD_BOUNDARY);
3688 #ifdef PCC_BITFIELD_TYPE_MATTERS
3689 if (PCC_BITFIELD_TYPE_MATTERS)
3690 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3697 /* Non-bit-fields are aligned for their type. */
3698 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3701 /* FIELD is a non bit-field. We are finishing the processing for its
3702 enclosing type T. Issue any appropriate messages and set appropriate
3706 check_field_decl (field, t, cant_have_const_ctor,
3707 cant_have_default_ctor, no_const_asn_ref,
3708 any_default_members)
3711 int *cant_have_const_ctor;
3712 int *cant_have_default_ctor;
3713 int *no_const_asn_ref;
3714 int *any_default_members;
3716 tree type = strip_array_types (TREE_TYPE (field));
3718 /* An anonymous union cannot contain any fields which would change
3719 the settings of CANT_HAVE_CONST_CTOR and friends. */
3720 if (ANON_UNION_TYPE_P (type))
3722 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3723 structs. So, we recurse through their fields here. */
3724 else if (ANON_AGGR_TYPE_P (type))
3728 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3729 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3730 check_field_decl (fields, t, cant_have_const_ctor,
3731 cant_have_default_ctor, no_const_asn_ref,
3732 any_default_members);
3734 /* Check members with class type for constructors, destructors,
3736 else if (CLASS_TYPE_P (type))
3738 /* Never let anything with uninheritable virtuals
3739 make it through without complaint. */
3740 abstract_virtuals_error (field, type);
3742 if (TREE_CODE (t) == UNION_TYPE)
3744 if (TYPE_NEEDS_CONSTRUCTING (type))
3745 cp_error_at ("member `%#D' with constructor not allowed in union",
3747 if (TYPE_NEEDS_DESTRUCTOR (type))
3748 cp_error_at ("member `%#D' with destructor not allowed in union",
3750 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3751 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3756 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3757 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3758 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3759 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3762 if (!TYPE_HAS_CONST_INIT_REF (type))
3763 *cant_have_const_ctor = 1;
3765 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3766 *no_const_asn_ref = 1;
3768 if (TYPE_HAS_CONSTRUCTOR (type)
3769 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3770 *cant_have_default_ctor = 1;
3772 if (DECL_INITIAL (field) != NULL_TREE)
3774 /* `build_class_init_list' does not recognize
3776 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3777 cp_error_at ("multiple fields in union `%T' initialized");
3778 *any_default_members = 1;
3781 /* Non-bit-fields are aligned for their type, except packed fields
3782 which require only BITS_PER_UNIT alignment. */
3783 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3784 (DECL_PACKED (field)
3786 : TYPE_ALIGN (TREE_TYPE (field))));
3789 /* Check the data members (both static and non-static), class-scoped
3790 typedefs, etc., appearing in the declaration of T. Issue
3791 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3792 declaration order) of access declarations; each TREE_VALUE in this
3793 list is a USING_DECL.
3795 In addition, set the following flags:
3798 The class is empty, i.e., contains no non-static data members.
3800 CANT_HAVE_DEFAULT_CTOR_P
3801 This class cannot have an implicitly generated default
3804 CANT_HAVE_CONST_CTOR_P
3805 This class cannot have an implicitly generated copy constructor
3806 taking a const reference.
3808 CANT_HAVE_CONST_ASN_REF
3809 This class cannot have an implicitly generated assignment
3810 operator taking a const reference.
3812 All of these flags should be initialized before calling this
3815 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3816 fields can be added by adding to this chain. */
3819 check_field_decls (t, access_decls, empty_p,
3820 cant_have_default_ctor_p, cant_have_const_ctor_p,
3825 int *cant_have_default_ctor_p;
3826 int *cant_have_const_ctor_p;
3827 int *no_const_asn_ref_p;
3832 int any_default_members;
3834 /* First, delete any duplicate fields. */
3835 delete_duplicate_fields (TYPE_FIELDS (t));
3837 /* Assume there are no access declarations. */
3838 *access_decls = NULL_TREE;
3839 /* Assume this class has no pointer members. */
3841 /* Assume none of the members of this class have default
3843 any_default_members = 0;
3845 for (field = &TYPE_FIELDS (t); *field; field = next)
3848 tree type = TREE_TYPE (x);
3850 GNU_xref_member (current_class_name, x);
3852 next = &TREE_CHAIN (x);
3854 if (TREE_CODE (x) == FIELD_DECL)
3856 DECL_PACKED (x) |= TYPE_PACKED (t);
3858 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3859 /* We don't treat zero-width bitfields as making a class
3864 /* The class is non-empty. */
3866 /* The class is not even nearly empty. */
3867 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3871 if (TREE_CODE (x) == USING_DECL)
3873 /* Prune the access declaration from the list of fields. */
3874 *field = TREE_CHAIN (x);
3876 /* Save the access declarations for our caller. */
3877 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3879 /* Since we've reset *FIELD there's no reason to skip to the
3885 if (TREE_CODE (x) == TYPE_DECL
3886 || TREE_CODE (x) == TEMPLATE_DECL)
3889 /* If we've gotten this far, it's a data member, possibly static,
3890 or an enumerator. */
3892 DECL_FIELD_CONTEXT (x) = t;
3894 /* ``A local class cannot have static data members.'' ARM 9.4 */
3895 if (current_function_decl && TREE_STATIC (x))
3896 cp_error_at ("field `%D' in local class cannot be static", x);
3898 /* Perform error checking that did not get done in
3900 if (TREE_CODE (type) == FUNCTION_TYPE)
3902 cp_error_at ("field `%D' invalidly declared function type",
3904 type = build_pointer_type (type);
3905 TREE_TYPE (x) = type;
3907 else if (TREE_CODE (type) == METHOD_TYPE)
3909 cp_error_at ("field `%D' invalidly declared method type", x);
3910 type = build_pointer_type (type);
3911 TREE_TYPE (x) = type;
3913 else if (TREE_CODE (type) == OFFSET_TYPE)
3915 cp_error_at ("field `%D' invalidly declared offset type", x);
3916 type = build_pointer_type (type);
3917 TREE_TYPE (x) = type;
3920 if (type == error_mark_node)
3923 DECL_SAVED_INSNS (x) = 0;
3925 /* When this goes into scope, it will be a non-local reference. */
3926 DECL_NONLOCAL (x) = 1;
3928 if (TREE_CODE (x) == CONST_DECL)
3931 if (TREE_CODE (x) == VAR_DECL)
3933 if (TREE_CODE (t) == UNION_TYPE)
3934 /* Unions cannot have static members. */
3935 cp_error_at ("field `%D' declared static in union", x);
3940 /* Now it can only be a FIELD_DECL. */
3942 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3943 CLASSTYPE_NON_AGGREGATE (t) = 1;
3945 /* If this is of reference type, check if it needs an init.
3946 Also do a little ANSI jig if necessary. */
3947 if (TREE_CODE (type) == REFERENCE_TYPE)
3949 CLASSTYPE_NON_POD_P (t) = 1;
3950 if (DECL_INITIAL (x) == NULL_TREE)
3951 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3953 /* ARM $12.6.2: [A member initializer list] (or, for an
3954 aggregate, initialization by a brace-enclosed list) is the
3955 only way to initialize nonstatic const and reference
3957 *cant_have_default_ctor_p = 1;
3958 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3960 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3963 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3965 cp_warning_at ("non-static reference in class without a constructor", x);
3969 type = strip_array_types (type);
3971 if (TREE_CODE (type) == POINTER_TYPE)
3974 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3975 CLASSTYPE_HAS_MUTABLE (t) = 1;
3977 if (! pod_type_p (type)
3978 /* For some reason, pointers to members are POD types themselves,
3979 but are not allowed in POD structs. Silly. */
3980 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3981 CLASSTYPE_NON_POD_P (t) = 1;
3983 /* If any field is const, the structure type is pseudo-const. */
3984 if (CP_TYPE_CONST_P (type))
3986 C_TYPE_FIELDS_READONLY (t) = 1;
3987 if (DECL_INITIAL (x) == NULL_TREE)
3988 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3990 /* ARM $12.6.2: [A member initializer list] (or, for an
3991 aggregate, initialization by a brace-enclosed list) is the
3992 only way to initialize nonstatic const and reference
3994 *cant_have_default_ctor_p = 1;
3995 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3997 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4000 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
4002 cp_warning_at ("non-static const member in class without a constructor", x);
4005 /* A field that is pseudo-const makes the structure likewise. */
4006 else if (IS_AGGR_TYPE (type))
4008 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4009 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4010 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
4013 /* We set DECL_C_BIT_FIELD in grokbitfield.
4014 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4015 if (DECL_C_BIT_FIELD (x))
4016 check_bitfield_decl (x);
4018 check_field_decl (x, t,
4019 cant_have_const_ctor_p,
4020 cant_have_default_ctor_p,
4022 &any_default_members);
4025 /* Effective C++ rule 11. */
4026 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
4027 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4029 cp_warning ("`%#T' has pointer data members", t);
4031 if (! TYPE_HAS_INIT_REF (t))
4033 cp_warning (" but does not override `%T(const %T&)'", t, t);
4034 if (! TYPE_HAS_ASSIGN_REF (t))
4035 cp_warning (" or `operator=(const %T&)'", t);
4037 else if (! TYPE_HAS_ASSIGN_REF (t))
4038 cp_warning (" but does not override `operator=(const %T&)'", t);
4042 /* Check anonymous struct/anonymous union fields. */
4043 finish_struct_anon (t);
4045 /* We've built up the list of access declarations in reverse order.
4047 *access_decls = nreverse (*access_decls);
4050 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4051 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4052 field are as indicated. The CLASS_TYPE in which this field occurs
4053 is also indicated. FCONTEXT is the type that is needed for the debug
4054 info output routines. *EMPTY_P is set to a non-zero value by this
4055 function to indicate that a class containing this field is
4059 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
4062 tree assembler_name;
4070 /* This class is non-empty. */
4073 /* Build the FIELD_DECL. */
4074 field = build_lang_decl (FIELD_DECL, name, type);
4075 DECL_ASSEMBLER_NAME (field) = assembler_name;
4076 DECL_VIRTUAL_P (field) = 1;
4077 DECL_ARTIFICIAL (field) = 1;
4078 DECL_FIELD_CONTEXT (field) = class_type;
4079 DECL_FCONTEXT (field) = fcontext;
4080 DECL_SAVED_INSNS (field) = 0;
4081 DECL_ALIGN (field) = TYPE_ALIGN (type);
4087 /* If the empty base field in DECL overlaps with a base of the same type in
4088 NEWDECL, which is either another base field or the first data field of
4089 the class, pad the base just before NEWDECL and return 1. Otherwise,
4093 avoid_overlap (decl, newdecl, empty_p)
4099 if (newdecl == NULL_TREE
4100 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
4103 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
4104 field = TREE_CHAIN (field))
4107 DECL_SIZE (field) = bitsize_int (1);
4108 DECL_SIZE_UNIT (field) = 0;
4109 /* The containing class cannot be empty; this field takes up space. */
4115 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
4116 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
4117 *BASE_ALIGN is a running maximum of the alignments of any base
4121 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
4126 unsigned int *base_align;
4128 tree basetype = BINFO_TYPE (binfo);
4131 if (TYPE_SIZE (basetype) == 0)
4132 /* This error is now reported in xref_tag, thus giving better
4133 location information. */
4136 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4137 DECL_ARTIFICIAL (decl) = 1;
4138 DECL_FIELD_CONTEXT (decl) = t;
4139 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4140 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4141 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4143 if (flag_new_abi && integer_zerop (DECL_SIZE (decl)))
4149 /* The containing class is non-empty because it has a non-empty base
4155 /* Brain damage for backwards compatibility. For no good
4156 reason, the old layout_basetypes made every base at least
4157 as large as the alignment for the bases up to that point,
4158 gratuitously wasting space. So we do the same thing
4160 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4162 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
4163 (int) (*base_align)));
4164 DECL_SIZE_UNIT (decl)
4165 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE_UNIT (decl)),
4166 (int) *base_align / BITS_PER_UNIT));
4172 /* Returns a list of fields to stand in for the base class subobjects
4173 of REC. These fields are later removed by layout_basetypes. */
4176 build_base_fields (rec, empty_p)
4180 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4182 tree base_decls = NULL_TREE;
4183 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4184 tree decl, nextdecl;
4185 int i, saw_empty = 0;
4186 unsigned int base_align = 0;
4188 /* Under the new ABI, the primary base class is always allocated
4190 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4194 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
4195 base_decls = chainon (build_base_field (rec,
4203 /* Now allocate the rest of the bases. */
4204 for (i = 0; i < n_baseclasses; ++i)
4208 /* Under the new ABI, the primary base was already allocated
4209 above, so we don't need to allocate it again here. */
4210 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4213 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4215 /* A primary virtual base class is allocated just like any other
4216 base class, but a non-primary virtual base is allocated
4217 later, in layout_basetypes. */
4218 if (TREE_VIA_VIRTUAL (base_binfo)
4219 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4222 base_decls = chainon (build_base_field (rec, base_binfo,
4229 /* Reverse the list of fields so we allocate the bases in the proper
4231 base_decls = nreverse (base_decls);
4233 /* In the presence of empty base classes, we run the risk of allocating
4234 two objects of the same class on top of one another. Avoid that. */
4235 if (flag_new_abi && saw_empty)
4236 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
4238 if (integer_zerop (DECL_SIZE (decl)))
4240 /* First step through the following bases until we find
4241 an overlap or a non-empty base. */
4242 for (nextdecl = TREE_CHAIN (decl); nextdecl;
4243 nextdecl = TREE_CHAIN (nextdecl))
4244 if (avoid_overlap (decl, nextdecl, empty_p)
4245 || ! integer_zerop (DECL_SIZE (nextdecl)))
4248 /* If we're still looking, also check against the first
4250 for (nextdecl = TYPE_FIELDS (rec);
4251 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
4252 nextdecl = TREE_CHAIN (nextdecl))
4254 avoid_overlap (decl, nextdecl, empty_p);
4262 /* Go through the TYPE_METHODS of T issuing any appropriate
4263 diagnostics, figuring out which methods override which other
4264 methods, and so forth. */
4272 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4274 GNU_xref_member (current_class_name, x);
4276 /* If this was an evil function, don't keep it in class. */
4277 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4280 DECL_SAVED_INSNS (x) = 0;
4281 check_for_override (x, t);
4282 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4283 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4285 /* The name of the field is the original field name
4286 Save this in auxiliary field for later overloading. */
4287 if (DECL_VINDEX (x))
4289 TYPE_POLYMORPHIC_P (t) = 1;
4290 if (DECL_PURE_VIRTUAL_P (x))
4291 CLASSTYPE_PURE_VIRTUALS (t)
4292 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4297 /* Remove all zero-width bit-fields from T. */
4300 remove_zero_width_bit_fields (t)
4305 fieldsp = &TYPE_FIELDS (t);
4308 if (TREE_CODE (*fieldsp) == FIELD_DECL
4309 && DECL_C_BIT_FIELD (*fieldsp)
4310 && DECL_INITIAL (*fieldsp))
4311 *fieldsp = TREE_CHAIN (*fieldsp);
4313 fieldsp = &TREE_CHAIN (*fieldsp);
4317 /* Check the validity of the bases and members declared in T. Add any
4318 implicitly-generated functions (like copy-constructors and
4319 assignment operators). Compute various flag bits (like
4320 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4321 level: i.e., independently of the ABI in use. */
4324 check_bases_and_members (t, empty_p)
4328 /* Nonzero if we are not allowed to generate a default constructor
4330 int cant_have_default_ctor;
4331 /* Nonzero if the implicitly generated copy constructor should take
4332 a non-const reference argument. */
4333 int cant_have_const_ctor;
4334 /* Nonzero if the the implicitly generated assignment operator
4335 should take a non-const reference argument. */
4336 int no_const_asn_ref;
4339 /* By default, we use const reference arguments and generate default
4341 cant_have_default_ctor = 0;
4342 cant_have_const_ctor = 0;
4343 no_const_asn_ref = 0;
4345 /* Assume that the class is nearly empty; we'll clear this flag if
4346 it turns out not to be nearly empty. */
4347 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4349 /* Check all the base-classes. */
4350 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4353 /* Check all the data member declarations. */
4354 check_field_decls (t, &access_decls, empty_p,
4355 &cant_have_default_ctor,
4356 &cant_have_const_ctor,
4359 /* Check all the method declarations. */
4362 /* A nearly-empty class has to be vptr-containing; a nearly empty
4363 class contains just a vptr. */
4364 if (!TYPE_CONTAINS_VPTR_P (t))
4365 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4367 /* Do some bookkeeping that will guide the generation of implicitly
4368 declared member functions. */
4369 TYPE_HAS_COMPLEX_INIT_REF (t)
4370 |= (TYPE_HAS_INIT_REF (t)
4371 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4372 || TYPE_POLYMORPHIC_P (t));
4373 TYPE_NEEDS_CONSTRUCTING (t)
4374 |= (TYPE_HAS_CONSTRUCTOR (t)
4375 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4376 || TYPE_POLYMORPHIC_P (t));
4377 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4378 || TYPE_POLYMORPHIC_P (t));
4379 CLASSTYPE_NON_POD_P (t)
4380 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4381 || TYPE_HAS_ASSIGN_REF (t));
4382 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4383 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4384 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4386 /* Synthesize any needed methods. Note that methods will be synthesized
4387 for anonymous unions; grok_x_components undoes that. */
4388 add_implicitly_declared_members (t, cant_have_default_ctor,
4389 cant_have_const_ctor,
4392 /* Process the using-declarations. */
4393 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4394 handle_using_decl (TREE_VALUE (access_decls), t);
4396 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4397 finish_struct_methods (t);
4400 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4401 accordingly, and, if necessary, add the TYPE_VFIELD to the
4402 TYPE_FIELDS list. */
4405 create_vtable_ptr (t, empty_p, has_virtual_p,
4406 new_virtuals_p, overridden_virtuals_p)
4410 tree *new_virtuals_p;
4411 tree *overridden_virtuals_p;
4415 /* Loop over the virtual functions, adding them to our various
4417 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4418 if (DECL_VINDEX (fn))
4419 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4420 has_virtual_p, fn, t);
4422 /* Even if there weren't any new virtual functions, we might need a
4423 new virtual function table if we're supposed to include vptrs in
4424 all classes that need them. */
4425 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4426 start_vtable (t, has_virtual_p);
4428 /* If we couldn't find an appropriate base class, create a new field
4430 if (*has_virtual_p && !TYPE_VFIELD (t))
4432 /* We build this decl with vtbl_ptr_type_node, which is a
4433 `vtable_entry_type*'. It might seem more precise to use
4434 `vtable_entry_type (*)[N]' where N is the number of firtual
4435 functions. However, that would require the vtable pointer in
4436 base classes to have a different type than the vtable pointer
4437 in derived classes. We could make that happen, but that
4438 still wouldn't solve all the problems. In particular, the
4439 type-based alias analysis code would decide that assignments
4440 to the base class vtable pointer can't alias assignments to
4441 the derived class vtable pointer, since they have different
4442 types. Thus, in an derived class destructor, where the base
4443 class constructor was inlined, we could generate bad code for
4444 setting up the vtable pointer.
4446 Therefore, we use one type for all vtable pointers. We still
4447 use a type-correct type; it's just doesn't indicate the array
4448 bounds. That's better than using `void*' or some such; it's
4449 cleaner, and it let's the alias analysis code know that these
4450 stores cannot alias stores to void*! */
4452 = build_vtbl_or_vbase_field (get_vfield_name (t),
4453 get_identifier (VFIELD_BASE),
4459 /* Add the new field to the list of fields in this class. */
4461 /* In the old ABI, the vtable pointer goes at the end of the
4463 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4466 /* But in the new ABI, the vtable pointer is the first thing
4468 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4469 /* If there were any baseclasses, they can't possibly be at
4470 offset zero any more, because that's where the vtable
4471 pointer is. So, converting to a base class is going to
4473 if (CLASSTYPE_N_BASECLASSES (t))
4474 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4477 /* We can't yet add this new field to the list of all virtual
4478 function table pointers in this class. The
4479 modify_all_vtables function depends on this not being done.
4480 So, it is done later, in finish_struct_1. */
4484 /* Fixup the inline function given by INFO now that the class is
4488 fixup_pending_inline (info)
4489 struct pending_inline *info;
4494 tree fn = info->fndecl;
4496 args = DECL_ARGUMENTS (fn);
4499 DECL_CONTEXT (args) = fn;
4500 args = TREE_CHAIN (args);
4505 /* Fixup the inline methods and friends in TYPE now that TYPE is
4509 fixup_inline_methods (type)
4512 tree method = TYPE_METHODS (type);
4514 if (method && TREE_CODE (method) == TREE_VEC)
4516 if (TREE_VEC_ELT (method, 1))
4517 method = TREE_VEC_ELT (method, 1);
4518 else if (TREE_VEC_ELT (method, 0))
4519 method = TREE_VEC_ELT (method, 0);
4521 method = TREE_VEC_ELT (method, 2);
4524 /* Do inline member functions. */
4525 for (; method; method = TREE_CHAIN (method))
4526 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4529 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4531 method = TREE_CHAIN (method))
4532 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4533 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4536 /* Called from propagate_binfo_offsets via dfs_walk. */
4539 dfs_propagate_binfo_offsets (binfo, data)
4543 tree offset = (tree) data;
4545 /* Update the BINFO_OFFSET for this base. */
4546 BINFO_OFFSET (binfo) = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4548 SET_BINFO_MARKED (binfo);
4553 /* Add OFFSET to all base types of BINFO which is a base in the
4554 hierarchy dominated by T.
4556 OFFSET, which is a type offset, is number of bytes.
4558 Note that we don't have to worry about having two paths to the
4559 same base type, since this type owns its association list. */
4562 propagate_binfo_offsets (binfo, offset)
4567 dfs_propagate_binfo_offsets,
4568 dfs_skip_nonprimary_vbases_unmarkedp,
4572 dfs_skip_nonprimary_vbases_markedp,
4576 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4577 the field list for T. */
4580 remove_base_field (t, binfo, field)
4585 tree basetype = BINFO_TYPE (binfo);
4588 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4590 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4591 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4595 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4597 propagate_binfo_offsets (binfo, offset);
4599 /* Remove this field. */
4600 *field = TREE_CHAIN (*field);
4603 /* Remove the FIELD_DECLs created for T's base classes in
4604 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4605 bases, except for non-primary virtual baseclasses. */
4608 remove_base_fields (t)
4614 /* Now propagate offset information throughout the lattice.
4615 Simultaneously, remove the temporary FIELD_DECLS we created in
4616 build_base_fields to refer to base types. */
4617 field = &TYPE_FIELDS (t);
4618 if (TYPE_VFIELD (t) == *field)
4620 /* If this class did not have a primary base, we create a
4621 virtual function table pointer. It will be the first thing
4622 in the class, under the new ABI. Skip it; the base fields
4624 my_friendly_assert (flag_new_abi
4625 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4627 field = &TREE_CHAIN (*field);
4630 /* Under the new ABI, the primary base is always allocated first. */
4631 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4632 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4634 /* Now remove the rest of the bases. */
4635 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4639 /* Under the new ABI, we've already removed the primary base
4641 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4644 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4646 /* We treat a primary virtual base class just like an ordinary base
4647 class. But, non-primary virtual bases are laid out later. */
4648 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4651 remove_base_field (t, binfo, field);
4655 /* Called via dfs_walk from layout_virtual bases. */
4658 dfs_set_offset_for_shared_vbases (binfo, data)
4662 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4664 /* Update the shared copy. */
4667 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4668 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4674 /* Called via dfs_walk from layout_virtual bases. */
4677 dfs_set_offset_for_unshared_vbases (binfo, data)
4681 /* If this is a virtual base, make sure it has the same offset as
4682 the shared copy. If it's a primary base, then we know it's
4684 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4686 tree t = (tree) data;
4690 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4691 offset = size_binop (MINUS_EXPR,
4692 BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4693 propagate_binfo_offsets (binfo, offset);
4699 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4700 TYPE_ALIGN and TYPE_SIZE for T. */
4703 layout_virtual_bases (t)
4709 /* DSIZE is the size of the class without the virtual bases. */
4710 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4711 /* Make every class have alignment of at least one. */
4712 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4714 /* Go through the virtual bases, allocating space for each virtual
4715 base that is not already a primary base class. */
4716 for (vbase = CLASSTYPE_VBASECLASSES (t);
4718 vbase = TREE_CHAIN (vbase))
4719 if (!BINFO_VBASE_PRIMARY_P (vbase))
4721 /* This virtual base is not a primary base of any class in the
4722 hierarchy, so we have to add space for it. */
4724 unsigned int desired_align;
4726 basetype = BINFO_TYPE (vbase);
4727 desired_align = TYPE_ALIGN (basetype);
4728 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4730 /* Add padding so that we can put the virtual base class at an
4731 appropriately aligned offset. */
4732 dsize = CEIL (dsize, desired_align) * desired_align;
4733 /* And compute the offset of the virtual base. */
4734 propagate_binfo_offsets (vbase,
4735 size_int (CEIL (dsize, BITS_PER_UNIT)));
4736 /* Every virtual baseclass takes a least a UNIT, so that we can
4737 take it's address and get something different for each base. */
4738 dsize += MAX (BITS_PER_UNIT,
4739 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4742 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4743 BINFO_OFFSET set correctly. Those we just allocated certainly
4744 will. The others are primary baseclasses; we walk the hierarchy
4745 to find the primary copies and update the shared copy. */
4746 dfs_walk (TYPE_BINFO (t),
4747 dfs_set_offset_for_shared_vbases,
4748 dfs_unmarked_real_bases_queue_p,
4751 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4752 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4753 bases and their direct and indirect bases. The ambiguity checks
4754 in get_base_distance depend on the BINFO_OFFSETs being set
4756 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4757 for (vbase = CLASSTYPE_VBASECLASSES (t);
4759 vbase = TREE_CHAIN (vbase))
4760 dfs_walk (vbase, dfs_set_offset_for_unshared_vbases, NULL, t);
4762 /* Now, make sure that the total size of the type is a multiple of
4764 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4765 TYPE_SIZE (t) = bitsize_int (dsize);
4766 TYPE_SIZE_UNIT (t) = convert (sizetype,
4767 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4768 bitsize_int (BITS_PER_UNIT)));
4771 /* Finish the work of layout_record, now taking virtual bases into account.
4772 Also compute the actual offsets that our base classes will have.
4773 This must be performed after the fields are laid out, since virtual
4774 baseclasses must lay down at the end of the record. */
4777 layout_basetypes (rec)
4782 #ifdef STRUCTURE_SIZE_BOUNDARY
4783 /* Packed structures don't need to have minimum size. */
4784 if (! TYPE_PACKED (rec))
4785 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4788 /* Remove the FIELD_DECLs we created for baseclasses in
4789 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4790 everything in the hierarcy except non-primary virtual bases. */
4791 remove_base_fields (rec);
4793 /* Allocate the virtual base classes. */
4794 layout_virtual_bases (rec);
4796 /* Get all the virtual base types that this type uses. The
4797 TREE_VALUE slot holds the virtual baseclass type. Note that
4798 get_vbase_types makes copies of the virtual base BINFOs, so that
4799 the vbase_types are unshared. */
4800 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4801 vbase_types = TREE_CHAIN (vbase_types))
4804 tree basetype = BINFO_TYPE (vbase_types);
4805 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4806 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4811 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4812 BINFO_OFFSETs for all of the base-classes. Position the vtable
4816 layout_class_type (t, empty_p, has_virtual_p,
4817 new_virtuals_p, overridden_virtuals_p)
4821 tree *new_virtuals_p;
4822 tree *overridden_virtuals_p;
4824 tree padding = NULL_TREE;
4826 /* If possible, we reuse the virtual function table pointer from one
4827 of our base classes. */
4828 determine_primary_base (t, has_virtual_p);
4830 /* Add pointers to all of our virtual base-classes. */
4831 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4833 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4834 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4837 /* Create a pointer to our virtual function table. */
4838 create_vtable_ptr (t, empty_p, has_virtual_p,
4839 new_virtuals_p, overridden_virtuals_p);
4841 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4842 we have to save this before we start modifying
4843 TYPE_NONCOPIED_PARTS. */
4844 fixup_inline_methods (t);
4846 /* We make all structures have at least one element, so that they
4847 have non-zero size. The field that we add here is fake, in the
4848 sense that, for example, we don't want people to be able to
4849 initialize it later. So, we add it just long enough to let the
4850 back-end lay out the type, and then remove it. In the new ABI,
4851 the class may be empty even if it has basetypes. Therefore, we
4852 add the fake field at the end of the fields list; if there are
4853 already FIELD_DECLs on the list, their offsets will not be
4857 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
4858 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), padding);
4859 TYPE_NONCOPIED_PARTS (t)
4860 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4861 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4864 /* Let the back-end lay out the type. Note that at this point we
4865 have only included non-virtual base-classes; we will lay out the
4866 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4867 this call are not necessarily correct; they are just the size and
4868 alignment when no virtual base clases are used. */
4871 /* If we added an extra field to make this class non-empty, remove
4877 declp = &TYPE_FIELDS (t);
4878 while (*declp != padding)
4879 declp = &TREE_CHAIN (*declp);
4880 *declp = TREE_CHAIN (*declp);
4883 /* Delete all zero-width bit-fields from the list of fields. Now
4884 that the type is laid out they are no longer important. */
4885 remove_zero_width_bit_fields (t);
4887 /* Remember the size and alignment of the class before adding
4888 the virtual bases. */
4889 if (*empty_p && flag_new_abi)
4891 CLASSTYPE_SIZE (t) = bitsize_int (0);
4892 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4894 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4895 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4897 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4898 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4902 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4903 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4906 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4908 /* Set the TYPE_DECL for this type to contain the right
4909 value for DECL_OFFSET, so that we can use it as part
4910 of a COMPONENT_REF for multiple inheritance. */
4911 layout_decl (TYPE_MAIN_DECL (t), 0);
4913 /* Now fix up any virtual base class types that we left lying
4914 around. We must get these done before we try to lay out the
4915 virtual function table. */
4916 if (CLASSTYPE_N_BASECLASSES (t))
4917 /* layout_basetypes will remove the base subobject fields. */
4918 layout_basetypes (t);
4921 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4922 (or C++ class declaration).
4924 For C++, we must handle the building of derived classes.
4925 Also, C++ allows static class members. The way that this is
4926 handled is to keep the field name where it is (as the DECL_NAME
4927 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4928 of the field. layout_record and layout_union will know about this.
4930 More C++ hair: inline functions have text in their
4931 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4932 meaningful tree structure. After the struct has been laid out, set
4933 things up so that this can happen.
4935 And still more: virtual functions. In the case of single inheritance,
4936 when a new virtual function is seen which redefines a virtual function
4937 from the base class, the new virtual function is placed into
4938 the virtual function table at exactly the same address that
4939 it had in the base class. When this is extended to multiple
4940 inheritance, the same thing happens, except that multiple virtual
4941 function tables must be maintained. The first virtual function
4942 table is treated in exactly the same way as in the case of single
4943 inheritance. Additional virtual function tables have different
4944 DELTAs, which tell how to adjust `this' to point to the right thing.
4946 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4954 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4955 a FUNCTION_DECL. Each of these functions is a virtual function
4956 declared in T that does not override any virtual function from a
4958 tree new_virtuals = NULL_TREE;
4959 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
4960 except that each declaration here overrides the declaration from
4962 tree overridden_virtuals = NULL_TREE;
4969 if (IS_AGGR_TYPE (t))
4970 cp_error ("redefinition of `%#T'", t);
4972 my_friendly_abort (172);
4977 GNU_xref_decl (current_function_decl, t);
4979 /* If this type was previously laid out as a forward reference,
4980 make sure we lay it out again. */
4981 TYPE_SIZE (t) = NULL_TREE;
4982 CLASSTYPE_GOT_SEMICOLON (t) = 0;
4983 CLASSTYPE_VFIELD_PARENT (t) = -1;
4985 CLASSTYPE_RTTI (t) = NULL_TREE;
4987 /* Do end-of-class semantic processing: checking the validity of the
4988 bases and members and add implicitly generated methods. */
4989 check_bases_and_members (t, &empty);
4991 /* Layout the class itself. */
4992 layout_class_type (t, &empty, &has_virtual,
4993 &new_virtuals, &overridden_virtuals);
4995 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4996 might need to know it for setting up the offsets in the vtable
4997 (or in thunks) below. */
4998 vfield = TYPE_VFIELD (t);
4999 if (vfield != NULL_TREE
5000 && DECL_FIELD_CONTEXT (vfield) != t)
5002 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5003 tree offset = convert (bitsizetype, BINFO_OFFSET (binfo));
5005 vfield = copy_node (vfield);
5006 copy_lang_decl (vfield);
5008 if (! integer_zerop (offset))
5009 offset = size_binop (MULT_EXPR, offset, bitsize_int (BITS_PER_UNIT));
5011 DECL_FIELD_CONTEXT (vfield) = t;
5012 DECL_FIELD_BITPOS (vfield)
5013 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
5014 TYPE_VFIELD (t) = vfield;
5018 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
5020 /* If necessary, create the primary vtable for this class. */
5022 || overridden_virtuals
5023 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5025 new_virtuals = nreverse (new_virtuals);
5026 /* We must enter these virtuals into the table. */
5027 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5029 if (! CLASSTYPE_COM_INTERFACE (t))
5031 /* The second slot is for the tdesc pointer when thunks
5033 if (flag_vtable_thunks)
5034 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5036 /* The first slot is for the rtti offset. */
5037 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5039 set_rtti_entry (new_virtuals,
5040 convert (ssizetype, integer_zero_node), t);
5042 build_primary_vtable (NULL_TREE, t);
5044 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5045 /* Here we know enough to change the type of our virtual
5046 function table, but we will wait until later this function. */
5047 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5049 /* If this type has basetypes with constructors, then those
5050 constructors might clobber the virtual function table. But
5051 they don't if the derived class shares the exact vtable of the base
5054 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5056 /* If we didn't need a new vtable, see if we should copy one from
5058 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5060 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5062 /* This class contributes nothing new to the virtual function
5063 table. However, it may have declared functions which
5064 went into the virtual function table "inherited" from the
5065 base class. If so, we grab a copy of those updated functions,
5066 and pretend they are ours. */
5068 /* See if we should steal the virtual info from base class. */
5069 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5070 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5071 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5072 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5073 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5074 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5077 if (TYPE_CONTAINS_VPTR_P (t))
5079 if (TYPE_BINFO_VTABLE (t))
5080 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5082 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5083 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5086 CLASSTYPE_VSIZE (t) = has_virtual;
5087 /* Entries for virtual functions defined in the primary base are
5088 followed by entries for new functions unique to this class. */
5089 TYPE_BINFO_VIRTUALS (t)
5090 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5091 /* Finally, add entries for functions that override virtuals
5092 from non-primary bases. */
5093 TYPE_BINFO_VIRTUALS (t)
5094 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5097 /* If we created a new vtbl pointer for this class, add it to the
5099 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5100 CLASSTYPE_VFIELDS (t)
5101 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5103 finish_struct_bits (t);
5105 /* Complete the rtl for any static member objects of the type we're
5107 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5109 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5110 && TREE_TYPE (x) == t)
5112 DECL_MODE (x) = TYPE_MODE (t);
5113 make_decl_rtl (x, NULL, 0);
5117 /* Done with FIELDS...now decide whether to sort these for
5118 faster lookups later.
5120 The C front-end only does this when n_fields > 15. We use
5121 a smaller number because most searches fail (succeeding
5122 ultimately as the search bores through the inheritance
5123 hierarchy), and we want this failure to occur quickly. */
5125 n_fields = count_fields (TYPE_FIELDS (t));
5128 tree field_vec = make_tree_vec (n_fields);
5129 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5130 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5131 (int (*)(const void *, const void *))field_decl_cmp);
5132 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5133 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5134 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5137 if (TYPE_HAS_CONSTRUCTOR (t))
5139 tree vfields = CLASSTYPE_VFIELDS (t);
5143 /* Mark the fact that constructor for T
5144 could affect anybody inheriting from T
5145 who wants to initialize vtables for VFIELDS's type. */
5146 if (VF_DERIVED_VALUE (vfields))
5147 TREE_ADDRESSABLE (vfields) = 1;
5148 vfields = TREE_CHAIN (vfields);
5152 /* Make the rtl for any new vtables we have created, and unmark
5153 the base types we marked. */
5156 if (CLASSTYPE_VSIZE (t) != 0)
5158 /* In addition to this one, all the other vfields should be listed. */
5159 /* Before that can be done, we have to have FIELD_DECLs for them, and
5160 a place to find them. */
5161 TYPE_NONCOPIED_PARTS (t)
5162 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5163 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5165 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5166 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5167 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5171 hack_incomplete_structures (t);
5173 if (warn_overloaded_virtual)
5176 maybe_suppress_debug_info (t);
5178 /* Finish debugging output for this type. */
5179 rest_of_type_compilation (t, toplevel_bindings_p ());
5182 /* When T was built up, the member declarations were added in reverse
5183 order. Rearrange them to declaration order. */
5186 unreverse_member_declarations (t)
5193 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5194 reverse order. Put them in declaration order now. */
5195 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5196 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5198 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5199 reverse order, so we can't just use nreverse. */
5201 for (x = TYPE_FIELDS (t);
5202 x && TREE_CODE (x) != TYPE_DECL;
5205 next = TREE_CHAIN (x);
5206 TREE_CHAIN (x) = prev;
5211 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5213 TYPE_FIELDS (t) = prev;
5218 finish_struct (t, attributes)
5221 /* Now that we've got all the field declarations, reverse everything
5223 unreverse_member_declarations (t);
5225 cplus_decl_attributes (t, attributes, NULL_TREE);
5227 if (processing_template_decl)
5229 finish_struct_methods (t);
5230 TYPE_SIZE (t) = integer_zero_node;
5233 finish_struct_1 (t);
5235 TYPE_BEING_DEFINED (t) = 0;
5237 if (current_class_type)
5240 error ("trying to finish struct, but kicked out due to previous parse errors.");
5242 if (processing_template_decl)
5244 tree scope = current_scope ();
5245 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5246 add_tree (build_min (TAG_DEFN, t));
5252 /* Return the dynamic type of INSTANCE, if known.
5253 Used to determine whether the virtual function table is needed
5256 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5257 of our knowledge of its type. *NONNULL should be initialized
5258 before this function is called. */
5261 fixed_type_or_null (instance, nonnull)
5265 switch (TREE_CODE (instance))
5268 /* Check that we are not going through a cast of some sort. */
5269 if (TREE_TYPE (instance)
5270 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5271 instance = TREE_OPERAND (instance, 0);
5272 /* fall through... */
5274 /* This is a call to a constructor, hence it's never zero. */
5275 if (TREE_HAS_CONSTRUCTOR (instance))
5279 return TREE_TYPE (instance);
5284 /* This is a call to a constructor, hence it's never zero. */
5285 if (TREE_HAS_CONSTRUCTOR (instance))
5289 return TREE_TYPE (instance);
5291 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5298 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5299 /* Propagate nonnull. */
5300 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5301 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5302 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5307 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5312 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5315 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5319 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5320 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5324 return TREE_TYPE (TREE_TYPE (instance));
5326 /* fall through... */
5329 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5333 return TREE_TYPE (instance);
5337 if (instance == current_class_ptr
5338 && flag_this_is_variable <= 0)
5340 /* Normally, 'this' must be non-null. */
5341 if (flag_this_is_variable == 0)
5344 /* <0 means we're in a constructor and we know our type. */
5345 if (flag_this_is_variable < 0)
5346 return TREE_TYPE (TREE_TYPE (instance));
5348 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5349 /* Reference variables should be references to objects. */
5359 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5360 to the static type. We also handle the case where INSTANCE is really
5363 Used to determine whether the virtual function table is needed
5366 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5367 of our knowledge of its type. *NONNULL should be initialized
5368 before this function is called. */
5371 resolves_to_fixed_type_p (instance, nonnull)
5375 tree t = TREE_TYPE (instance);
5376 tree fixed = fixed_type_or_null (instance, nonnull);
5377 if (fixed == NULL_TREE)
5379 if (POINTER_TYPE_P (t))
5381 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5386 init_class_processing ()
5388 current_class_depth = 0;
5389 current_class_stack_size = 10;
5391 = (class_stack_node_t) xmalloc (current_class_stack_size
5392 * sizeof (struct class_stack_node));
5394 access_default_node = build_int_2 (0, 0);
5395 access_public_node = build_int_2 (1, 0);
5396 access_protected_node = build_int_2 (2, 0);
5397 access_private_node = build_int_2 (3, 0);
5398 access_default_virtual_node = build_int_2 (4, 0);
5399 access_public_virtual_node = build_int_2 (5, 0);
5400 access_protected_virtual_node = build_int_2 (6, 0);
5401 access_private_virtual_node = build_int_2 (7, 0);
5404 /* Set current scope to NAME. CODE tells us if this is a
5405 STRUCT, UNION, or ENUM environment.
5407 NAME may end up being NULL_TREE if this is an anonymous or
5408 late-bound struct (as in "struct { ... } foo;") */
5410 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5411 appropriate values, found by looking up the type definition of
5414 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5415 which can be seen locally to the class. They are shadowed by
5416 any subsequent local declaration (including parameter names).
5418 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5419 which have static meaning (i.e., static members, static
5420 member functions, enum declarations, etc).
5422 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5423 which can be seen locally to the class (as in 1), but
5424 know that we are doing this for declaration purposes
5425 (i.e. friend foo::bar (int)).
5427 So that we may avoid calls to lookup_name, we cache the _TYPE
5428 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5430 For multiple inheritance, we perform a two-pass depth-first search
5431 of the type lattice. The first pass performs a pre-order search,
5432 marking types after the type has had its fields installed in
5433 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5434 unmarks the marked types. If a field or member function name
5435 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5436 that name becomes `error_mark_node'. */
5439 pushclass (type, modify)
5443 type = TYPE_MAIN_VARIANT (type);
5445 /* Make sure there is enough room for the new entry on the stack. */
5446 if (current_class_depth + 1 >= current_class_stack_size)
5448 current_class_stack_size *= 2;
5450 = (class_stack_node_t) xrealloc (current_class_stack,
5451 current_class_stack_size
5452 * sizeof (struct class_stack_node));
5455 /* Insert a new entry on the class stack. */
5456 current_class_stack[current_class_depth].name = current_class_name;
5457 current_class_stack[current_class_depth].type = current_class_type;
5458 current_class_stack[current_class_depth].access = current_access_specifier;
5459 current_class_stack[current_class_depth].names_used = 0;
5460 current_class_depth++;
5462 /* Now set up the new type. */
5463 current_class_name = TYPE_NAME (type);
5464 if (TREE_CODE (current_class_name) == TYPE_DECL)
5465 current_class_name = DECL_NAME (current_class_name);
5466 current_class_type = type;
5468 /* By default, things in classes are private, while things in
5469 structures or unions are public. */
5470 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5471 ? access_private_node
5472 : access_public_node);
5474 if (previous_class_type != NULL_TREE
5475 && (type != previous_class_type
5476 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5477 && current_class_depth == 1)
5479 /* Forcibly remove any old class remnants. */
5480 invalidate_class_lookup_cache ();
5483 /* If we're about to enter a nested class, clear
5484 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5485 if (modify && current_class_depth > 1)
5486 clear_identifier_class_values ();
5491 if (CLASSTYPE_TEMPLATE_INFO (type))
5492 overload_template_name (type);
5497 if (type != previous_class_type || current_class_depth > 1)
5498 push_class_decls (type);
5503 /* We are re-entering the same class we just left, so we
5504 don't have to search the whole inheritance matrix to find
5505 all the decls to bind again. Instead, we install the
5506 cached class_shadowed list, and walk through it binding
5507 names and setting up IDENTIFIER_TYPE_VALUEs. */
5508 set_class_shadows (previous_class_values);
5509 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5511 tree id = TREE_PURPOSE (item);
5512 tree decl = TREE_TYPE (item);
5514 push_class_binding (id, decl);
5515 if (TREE_CODE (decl) == TYPE_DECL)
5516 set_identifier_type_value (id, TREE_TYPE (decl));
5518 unuse_fields (type);
5521 storetags (CLASSTYPE_TAGS (type));
5525 /* When we exit a toplevel class scope, we save the
5526 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5527 reenter the class. Here, we've entered some other class, so we
5528 must invalidate our cache. */
5531 invalidate_class_lookup_cache ()
5535 /* This code can be seen as a cache miss. When we've cached a
5536 class' scope's bindings and we can't use them, we need to reset
5537 them. This is it! */
5538 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5539 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5541 previous_class_type = NULL_TREE;
5544 /* Get out of the current class scope. If we were in a class scope
5545 previously, that is the one popped to. */
5551 /* Since poplevel_class does the popping of class decls nowadays,
5552 this really only frees the obstack used for these decls. */
5555 current_class_depth--;
5556 current_class_name = current_class_stack[current_class_depth].name;
5557 current_class_type = current_class_stack[current_class_depth].type;
5558 current_access_specifier = current_class_stack[current_class_depth].access;
5559 if (current_class_stack[current_class_depth].names_used)
5560 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5563 /* Returns 1 if current_class_type is either T or a nested type of T.
5564 We start looking from 1 because entry 0 is from global scope, and has
5568 currently_open_class (t)
5572 if (t == current_class_type)
5574 for (i = 1; i < current_class_depth; ++i)
5575 if (current_class_stack [i].type == t)
5580 /* If either current_class_type or one of its enclosing classes are derived
5581 from T, return the appropriate type. Used to determine how we found
5582 something via unqualified lookup. */
5585 currently_open_derived_class (t)
5590 if (DERIVED_FROM_P (t, current_class_type))
5591 return current_class_type;
5593 for (i = current_class_depth - 1; i > 0; --i)
5594 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5595 return current_class_stack[i].type;
5600 /* When entering a class scope, all enclosing class scopes' names with
5601 static meaning (static variables, static functions, types and enumerators)
5602 have to be visible. This recursive function calls pushclass for all
5603 enclosing class contexts until global or a local scope is reached.
5604 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5605 formal of the same name. */
5608 push_nested_class (type, modify)
5614 /* A namespace might be passed in error cases, like A::B:C. */
5615 if (type == NULL_TREE
5616 || type == error_mark_node
5617 || TREE_CODE (type) == NAMESPACE_DECL
5618 || ! IS_AGGR_TYPE (type)
5619 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5620 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5623 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5625 if (context && CLASS_TYPE_P (context))
5626 push_nested_class (context, 2);
5627 pushclass (type, modify);
5630 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5635 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5638 if (context && CLASS_TYPE_P (context))
5639 pop_nested_class ();
5642 /* Set global variables CURRENT_LANG_NAME to appropriate value
5643 so that behavior of name-mangling machinery is correct. */
5646 push_lang_context (name)
5649 *current_lang_stack++ = current_lang_name;
5650 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5651 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5653 size_t old_size = VARRAY_SIZE (current_lang_base);
5655 VARRAY_GROW (current_lang_base, old_size + 10);
5656 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5659 if (name == lang_name_cplusplus)
5661 strict_prototype = strict_prototypes_lang_cplusplus;
5662 current_lang_name = name;
5664 else if (name == lang_name_java)
5666 strict_prototype = strict_prototypes_lang_cplusplus;
5667 current_lang_name = name;
5668 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5669 (See record_builtin_java_type in decl.c.) However, that causes
5670 incorrect debug entries if these types are actually used.
5671 So we re-enable debug output after extern "Java". */
5672 DECL_IGNORED_P (java_byte_type_node) = 0;
5673 DECL_IGNORED_P (java_short_type_node) = 0;
5674 DECL_IGNORED_P (java_int_type_node) = 0;
5675 DECL_IGNORED_P (java_long_type_node) = 0;
5676 DECL_IGNORED_P (java_float_type_node) = 0;
5677 DECL_IGNORED_P (java_double_type_node) = 0;
5678 DECL_IGNORED_P (java_char_type_node) = 0;
5679 DECL_IGNORED_P (java_boolean_type_node) = 0;
5681 else if (name == lang_name_c)
5683 strict_prototype = strict_prototypes_lang_c;
5684 current_lang_name = name;
5687 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5690 /* Get out of the current language scope. */
5695 /* Clear the current entry so that garbage collector won't hold on
5697 *current_lang_stack = NULL_TREE;
5698 current_lang_name = *--current_lang_stack;
5699 if (current_lang_name == lang_name_cplusplus
5700 || current_lang_name == lang_name_java)
5701 strict_prototype = strict_prototypes_lang_cplusplus;
5702 else if (current_lang_name == lang_name_c)
5703 strict_prototype = strict_prototypes_lang_c;
5706 /* Type instantiation routines. */
5708 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5709 matches the TARGET_TYPE. If there is no satisfactory match, return
5710 error_mark_node, and issue an error message if COMPLAIN is
5711 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5712 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5713 template arguments. */
5716 resolve_address_of_overloaded_function (target_type,
5725 tree explicit_targs;
5727 /* Here's what the standard says:
5731 If the name is a function template, template argument deduction
5732 is done, and if the argument deduction succeeds, the deduced
5733 arguments are used to generate a single template function, which
5734 is added to the set of overloaded functions considered.
5736 Non-member functions and static member functions match targets of
5737 type "pointer-to-function" or "reference-to-function." Nonstatic
5738 member functions match targets of type "pointer-to-member
5739 function;" the function type of the pointer to member is used to
5740 select the member function from the set of overloaded member
5741 functions. If a nonstatic member function is selected, the
5742 reference to the overloaded function name is required to have the
5743 form of a pointer to member as described in 5.3.1.
5745 If more than one function is selected, any template functions in
5746 the set are eliminated if the set also contains a non-template
5747 function, and any given template function is eliminated if the
5748 set contains a second template function that is more specialized
5749 than the first according to the partial ordering rules 14.5.5.2.
5750 After such eliminations, if any, there shall remain exactly one
5751 selected function. */
5754 int is_reference = 0;
5755 /* We store the matches in a TREE_LIST rooted here. The functions
5756 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5757 interoperability with most_specialized_instantiation. */
5758 tree matches = NULL_TREE;
5761 /* By the time we get here, we should be seeing only real
5762 pointer-to-member types, not the internal POINTER_TYPE to
5763 METHOD_TYPE representation. */
5764 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5765 && (TREE_CODE (TREE_TYPE (target_type))
5766 == METHOD_TYPE)), 0);
5768 /* Check that the TARGET_TYPE is reasonable. */
5769 if (TYPE_PTRFN_P (target_type))
5772 else if (TYPE_PTRMEMFUNC_P (target_type))
5773 /* This is OK, too. */
5775 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5777 /* This is OK, too. This comes from a conversion to reference
5779 target_type = build_reference_type (target_type);
5785 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5786 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5787 return error_mark_node;
5790 /* If we can find a non-template function that matches, we can just
5791 use it. There's no point in generating template instantiations
5792 if we're just going to throw them out anyhow. But, of course, we
5793 can only do this when we don't *need* a template function. */
5798 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5800 tree fn = OVL_FUNCTION (fns);
5803 if (TREE_CODE (fn) == TEMPLATE_DECL)
5804 /* We're not looking for templates just yet. */
5807 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5809 /* We're looking for a non-static member, and this isn't
5810 one, or vice versa. */
5813 /* See if there's a match. */
5814 fntype = TREE_TYPE (fn);
5816 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5817 else if (!is_reference)
5818 fntype = build_pointer_type (fntype);
5820 if (can_convert_arg (target_type, fntype, fn))
5821 matches = tree_cons (fn, NULL_TREE, matches);
5825 /* Now, if we've already got a match (or matches), there's no need
5826 to proceed to the template functions. But, if we don't have a
5827 match we need to look at them, too. */
5830 tree target_fn_type;
5831 tree target_arg_types;
5836 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5838 target_fn_type = TREE_TYPE (target_type);
5839 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5841 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5843 tree fn = OVL_FUNCTION (fns);
5845 tree instantiation_type;
5848 if (TREE_CODE (fn) != TEMPLATE_DECL)
5849 /* We're only looking for templates. */
5852 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5854 /* We're not looking for a non-static member, and this is
5855 one, or vice versa. */
5858 /* Try to do argument deduction. */
5859 targs = make_tree_vec (DECL_NTPARMS (fn));
5860 if (fn_type_unification (fn, explicit_targs, targs,
5861 target_arg_types, NULL_TREE,
5863 /* Argument deduction failed. */
5866 /* Instantiate the template. */
5867 instantiation = instantiate_template (fn, targs);
5868 if (instantiation == error_mark_node)
5869 /* Instantiation failed. */
5872 /* See if there's a match. */
5873 instantiation_type = TREE_TYPE (instantiation);
5875 instantiation_type =
5876 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5877 else if (!is_reference)
5878 instantiation_type = build_pointer_type (instantiation_type);
5879 if (can_convert_arg (target_type, instantiation_type, instantiation))
5880 matches = tree_cons (instantiation, fn, matches);
5883 /* Now, remove all but the most specialized of the matches. */
5886 tree match = most_specialized_instantiation (matches,
5889 if (match != error_mark_node)
5890 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5894 /* Now we should have exactly one function in MATCHES. */
5895 if (matches == NULL_TREE)
5897 /* There were *no* matches. */
5900 cp_error ("no matches converting function `%D' to type `%#T'",
5901 DECL_NAME (OVL_FUNCTION (overload)),
5904 /* print_candidates expects a chain with the functions in
5905 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5906 so why be clever?). */
5907 for (; overload; overload = OVL_NEXT (overload))
5908 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5911 print_candidates (matches);
5913 return error_mark_node;
5915 else if (TREE_CHAIN (matches))
5917 /* There were too many matches. */
5923 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5924 DECL_NAME (OVL_FUNCTION (overload)),
5927 /* Since print_candidates expects the functions in the
5928 TREE_VALUE slot, we flip them here. */
5929 for (match = matches; match; match = TREE_CHAIN (match))
5930 TREE_VALUE (match) = TREE_PURPOSE (match);
5932 print_candidates (matches);
5935 return error_mark_node;
5938 /* Good, exactly one match. Now, convert it to the correct type. */
5939 fn = TREE_PURPOSE (matches);
5943 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5944 return build_unary_op (ADDR_EXPR, fn, 0);
5947 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5948 will mark the function as addressed, but here we must do it
5950 mark_addressable (fn);
5956 /* This function will instantiate the type of the expression given in
5957 RHS to match the type of LHSTYPE. If errors exist, then return
5958 error_mark_node. We only complain is COMPLAIN is set. If we are
5959 not complaining, never modify rhs, as overload resolution wants to
5960 try many possible instantiations, in hopes that at least one will
5963 FLAGS is a bitmask, as we see at the top of the function.
5965 For non-recursive calls, LHSTYPE should be a function, pointer to
5966 function, or a pointer to member function. */
5969 instantiate_type (lhstype, rhs, flags)
5973 int complain = (flags & 1);
5974 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5976 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5979 error ("not enough type information");
5980 return error_mark_node;
5983 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5985 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5988 cp_error ("argument of type `%T' does not match `%T'",
5989 TREE_TYPE (rhs), lhstype);
5990 return error_mark_node;
5993 /* We don't overwrite rhs if it is an overloaded function.
5994 Copying it would destroy the tree link. */
5995 if (TREE_CODE (rhs) != OVERLOAD)
5996 rhs = copy_node (rhs);
5998 /* This should really only be used when attempting to distinguish
5999 what sort of a pointer to function we have. For now, any
6000 arithmetic operation which is not supported on pointers
6001 is rejected as an error. */
6003 switch (TREE_CODE (rhs))
6010 my_friendly_abort (177);
6011 return error_mark_node;
6018 new_rhs = instantiate_type (build_pointer_type (lhstype),
6019 TREE_OPERAND (rhs, 0), flags);
6020 if (new_rhs == error_mark_node)
6021 return error_mark_node;
6023 TREE_TYPE (rhs) = lhstype;
6024 TREE_OPERAND (rhs, 0) = new_rhs;
6029 rhs = copy_node (TREE_OPERAND (rhs, 0));
6030 TREE_TYPE (rhs) = unknown_type_node;
6031 return instantiate_type (lhstype, rhs, flags);
6035 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6037 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6038 && complain && !flag_ms_extensions)
6040 /* Note: we check this after the recursive call to avoid
6041 complaining about cases where overload resolution fails. */
6043 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6044 tree fn = PTRMEM_CST_MEMBER (r);
6046 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6049 ("object-dependent reference to `%E' can only be used in a call",
6052 (" to form a pointer to member function, say `&%T::%E'",
6060 rhs = TREE_OPERAND (rhs, 1);
6061 if (BASELINK_P (rhs))
6062 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6064 /* This can happen if we are forming a pointer-to-member for a
6066 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6070 case TEMPLATE_ID_EXPR:
6072 resolve_address_of_overloaded_function (lhstype,
6073 TREE_OPERAND (rhs, 0),
6075 /*template_only=*/1,
6076 TREE_OPERAND (rhs, 1));
6080 resolve_address_of_overloaded_function (lhstype,
6083 /*template_only=*/0,
6084 /*explicit_targs=*/NULL_TREE);
6087 /* Now we should have a baselink. */
6088 my_friendly_assert (BASELINK_P (rhs), 990412);
6090 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6093 /* This is too hard for now. */
6094 my_friendly_abort (183);
6095 return error_mark_node;
6100 TREE_OPERAND (rhs, 0)
6101 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6102 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6103 return error_mark_node;
6104 TREE_OPERAND (rhs, 1)
6105 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6106 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6107 return error_mark_node;
6109 TREE_TYPE (rhs) = lhstype;
6113 case TRUNC_DIV_EXPR:
6114 case FLOOR_DIV_EXPR:
6116 case ROUND_DIV_EXPR:
6118 case TRUNC_MOD_EXPR:
6119 case FLOOR_MOD_EXPR:
6121 case ROUND_MOD_EXPR:
6122 case FIX_ROUND_EXPR:
6123 case FIX_FLOOR_EXPR:
6125 case FIX_TRUNC_EXPR:
6141 case PREINCREMENT_EXPR:
6142 case PREDECREMENT_EXPR:
6143 case POSTINCREMENT_EXPR:
6144 case POSTDECREMENT_EXPR:
6146 error ("invalid operation on uninstantiated type");
6147 return error_mark_node;
6149 case TRUTH_AND_EXPR:
6151 case TRUTH_XOR_EXPR:
6158 case TRUTH_ANDIF_EXPR:
6159 case TRUTH_ORIF_EXPR:
6160 case TRUTH_NOT_EXPR:
6162 error ("not enough type information");
6163 return error_mark_node;
6166 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6169 error ("not enough type information");
6170 return error_mark_node;
6172 TREE_OPERAND (rhs, 1)
6173 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6174 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6175 return error_mark_node;
6176 TREE_OPERAND (rhs, 2)
6177 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6178 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6179 return error_mark_node;
6181 TREE_TYPE (rhs) = lhstype;
6185 TREE_OPERAND (rhs, 1)
6186 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6187 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6188 return error_mark_node;
6190 TREE_TYPE (rhs) = lhstype;
6194 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6196 case ENTRY_VALUE_EXPR:
6197 my_friendly_abort (184);
6198 return error_mark_node;
6201 return error_mark_node;
6204 my_friendly_abort (185);
6205 return error_mark_node;
6209 /* Return the name of the virtual function pointer field
6210 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6211 this may have to look back through base types to find the
6212 ultimate field name. (For single inheritance, these could
6213 all be the same name. Who knows for multiple inheritance). */
6216 get_vfield_name (type)
6219 tree binfo = TYPE_BINFO (type);
6222 while (BINFO_BASETYPES (binfo)
6223 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6224 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6225 binfo = BINFO_BASETYPE (binfo, 0);
6227 type = BINFO_TYPE (binfo);
6228 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6229 + TYPE_NAME_LENGTH (type) + 2);
6230 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6231 return get_identifier (buf);
6235 print_class_statistics ()
6237 #ifdef GATHER_STATISTICS
6238 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6239 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6240 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6241 n_build_method_call, n_inner_fields_searched);
6244 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6245 n_vtables, n_vtable_searches);
6246 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6247 n_vtable_entries, n_vtable_elems);
6252 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6253 according to [class]:
6254 The class-name is also inserted
6255 into the scope of the class itself. For purposes of access checking,
6256 the inserted class name is treated as if it were a public member name. */
6259 build_self_reference ()
6261 tree name = constructor_name (current_class_type);
6262 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6265 DECL_NONLOCAL (value) = 1;
6266 DECL_CONTEXT (value) = current_class_type;
6267 DECL_ARTIFICIAL (value) = 1;
6269 if (processing_template_decl)
6270 value = push_template_decl (value);
6272 saved_cas = current_access_specifier;
6273 current_access_specifier = access_public_node;
6274 finish_member_declaration (value);
6275 current_access_specifier = saved_cas;
6278 /* Returns 1 if TYPE contains only padding bytes. */
6281 is_empty_class (type)
6286 if (type == error_mark_node)
6289 if (! IS_AGGR_TYPE (type))
6293 return integer_zerop (CLASSTYPE_SIZE (type));
6295 if (TYPE_BINFO_BASETYPES (type))
6297 t = TYPE_FIELDS (type);
6298 while (t && TREE_CODE (t) != FIELD_DECL)
6300 return (t == NULL_TREE);
6303 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6304 a *_TYPE node. NODE can also be a local class. */
6307 get_enclosing_class (type)
6312 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6314 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6317 node = DECL_CONTEXT (node);
6323 node = TYPE_CONTEXT (node);
6327 my_friendly_abort (0);
6333 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6336 is_base_of_enclosing_class (base, type)
6341 if (get_binfo (base, type, 0))
6344 type = get_enclosing_class (type);
6349 /* Note that NAME was looked up while the current class was being
6350 defined and that the result of that lookup was DECL. */
6353 maybe_note_name_used_in_class (name, decl)
6357 splay_tree names_used;
6359 /* If we're not defining a class, there's nothing to do. */
6360 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6363 /* If there's already a binding for this NAME, then we don't have
6364 anything to worry about. */
6365 if (IDENTIFIER_CLASS_VALUE (name))
6368 if (!current_class_stack[current_class_depth - 1].names_used)
6369 current_class_stack[current_class_depth - 1].names_used
6370 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6371 names_used = current_class_stack[current_class_depth - 1].names_used;
6373 splay_tree_insert (names_used,
6374 (splay_tree_key) name,
6375 (splay_tree_value) decl);
6378 /* Note that NAME was declared (as DECL) in the current class. Check
6379 to see that the declaration is legal. */
6382 note_name_declared_in_class (name, decl)
6386 splay_tree names_used;
6389 /* Look to see if we ever used this name. */
6391 = current_class_stack[current_class_depth - 1].names_used;
6395 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6398 /* [basic.scope.class]
6400 A name N used in a class S shall refer to the same declaration
6401 in its context and when re-evaluated in the completed scope of
6403 cp_error ("declaration of `%#D'", decl);
6404 cp_error_at ("changes meaning of `%s' from `%+#D'",
6405 IDENTIFIER_POINTER (DECL_NAME (decl)),
6410 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6411 INDENT should be zero when called from the top level; it is
6412 incremented recursively. */
6415 dump_class_hierarchy (binfo, indent)
6421 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6422 (unsigned long) binfo,
6423 type_as_string (binfo, TS_PLAIN));
6424 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6425 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)));
6426 fprintf (stderr, " %s\n",
6427 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6429 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6430 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);