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 int build_primary_vtable PARAMS ((tree, tree));
84 static int build_secondary_vtable PARAMS ((tree, tree));
85 static tree dfs_finish_vtbls PARAMS ((tree, void *));
86 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
87 static void finish_vtbls PARAMS ((tree));
88 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
89 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
90 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
91 static void delete_duplicate_fields PARAMS ((tree));
92 static void finish_struct_bits PARAMS ((tree));
93 static int alter_access PARAMS ((tree, tree, tree));
94 static void handle_using_decl PARAMS ((tree, tree));
95 static int overrides PARAMS ((tree, tree));
96 static int strictly_overrides PARAMS ((tree, tree));
97 static void mark_overriders PARAMS ((tree, tree));
98 static void check_for_override PARAMS ((tree, tree));
99 static tree dfs_modify_vtables PARAMS ((tree, void *));
100 static tree modify_all_vtables PARAMS ((tree, int *, tree));
101 static void determine_primary_base PARAMS ((tree, int *));
102 static void finish_struct_methods PARAMS ((tree));
103 static void maybe_warn_about_overly_private_class PARAMS ((tree));
104 static int field_decl_cmp PARAMS ((const tree *, const tree *));
105 static int method_name_cmp PARAMS ((const tree *, const tree *));
106 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
107 static tree fixed_type_or_null PARAMS ((tree, int *));
108 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
110 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
111 static tree build_vtbl_initializer PARAMS ((tree, tree));
112 static int count_fields PARAMS ((tree));
113 static int add_fields_to_vec PARAMS ((tree, tree, int));
114 static void check_bitfield_decl PARAMS ((tree));
115 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
116 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
118 static void build_base_field PARAMS ((record_layout_info, tree, int *,
119 unsigned int *, varray_type *));
120 static varray_type build_base_fields PARAMS ((record_layout_info, int *));
121 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
122 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
124 static void check_methods PARAMS ((tree));
125 static void remove_zero_width_bit_fields PARAMS ((tree));
126 static void check_bases PARAMS ((tree, int *, int *, int *));
127 static void check_bases_and_members PARAMS ((tree, int *));
128 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
129 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
130 static void fixup_pending_inline PARAMS ((struct pending_inline *));
131 static void fixup_inline_methods PARAMS ((tree));
132 static void set_primary_base PARAMS ((tree, int, int *));
133 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
134 static void propagate_binfo_offsets PARAMS ((tree, tree));
135 static void layout_virtual_bases PARAMS ((tree, varray_type *));
136 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
137 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
138 static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
139 static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
140 static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
141 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
142 static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
143 static tree dfs_count_virtuals PARAMS ((tree, void *));
144 static void layout_vtable_decl PARAMS ((tree, int));
145 static int num_vfun_entries PARAMS ((tree));
146 static tree dfs_find_final_overrider PARAMS ((tree, void *));
147 static tree find_final_overrider PARAMS ((tree, tree, tree));
148 static tree dfs_find_base PARAMS ((tree, void *));
149 static int make_new_vtable PARAMS ((tree, tree));
150 extern void dump_class_hierarchy PARAMS ((tree, int));
151 static tree build_vtable PARAMS ((tree, tree, tree));
152 static void initialize_vtable PARAMS ((tree, tree));
153 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
156 static tree dfs_record_base_offsets PARAMS ((tree, void *));
157 static void record_base_offsets PARAMS ((tree, varray_type *));
158 static tree dfs_search_base_offsets PARAMS ((tree, void *));
159 static int layout_conflict_p PARAMS ((tree, varray_type));
160 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
161 static void layout_empty_base PARAMS ((tree, tree, varray_type));
162 static void accumulate_vtbl_inits PARAMS ((tree, tree));
163 static void set_vindex PARAMS ((tree, tree, int *));
164 static tree build_rtti_vtbl_entries PARAMS ((tree, tree));
166 /* Variables shared between class.c and call.c. */
168 #ifdef GATHER_STATISTICS
170 int n_vtable_entries = 0;
171 int n_vtable_searches = 0;
172 int n_vtable_elems = 0;
173 int n_convert_harshness = 0;
174 int n_compute_conversion_costs = 0;
175 int n_build_method_call = 0;
176 int n_inner_fields_searched = 0;
179 /* Virtual base class layout. */
181 /* Returns a list of virtual base class pointers as a chain of
185 build_vbase_pointer_fields (rli, empty_p)
186 record_layout_info rli;
189 /* Chain to hold all the new FIELD_DECLs which point at virtual
192 tree vbase_decls = NULL_TREE;
193 tree binfos = TYPE_BINFO_BASETYPES (rec);
194 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
198 /* Under the new ABI, there are no vbase pointers in the object.
199 Instead, the offsets are stored in the vtable. */
200 if (vbase_offsets_in_vtable_p ())
203 /* Loop over the baseclasses, adding vbase pointers as needed. */
204 for (i = 0; i < n_baseclasses; i++)
206 register tree base_binfo = TREE_VEC_ELT (binfos, i);
207 register tree basetype = BINFO_TYPE (base_binfo);
209 if (!COMPLETE_TYPE_P (basetype))
210 /* This error is now reported in xref_tag, thus giving better
211 location information. */
214 /* All basetypes are recorded in the association list of the
217 if (TREE_VIA_VIRTUAL (base_binfo))
222 /* The offset for a virtual base class is only used in computing
223 virtual function tables and for initializing virtual base
224 pointers. It is built once `get_vbase_types' is called. */
226 /* If this basetype can come from another vbase pointer
227 without an additional indirection, we will share
228 that pointer. If an indirection is involved, we
229 make our own pointer. */
230 for (j = 0; j < n_baseclasses; j++)
232 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
233 if (! TREE_VIA_VIRTUAL (other_base_binfo)
234 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
237 FORMAT_VBASE_NAME (name, basetype);
238 decl = build_vtbl_or_vbase_field (get_identifier (name),
239 get_identifier (VTABLE_BASE),
240 build_pointer_type (basetype),
244 BINFO_VPTR_FIELD (base_binfo) = decl;
245 TREE_CHAIN (decl) = vbase_decls;
246 place_field (rli, decl);
251 /* The space this decl occupies has already been accounted for. */
259 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
262 dfs_build_vbase_offset_vtbl_entries (binfo, data)
266 tree list = (tree) data;
268 if (TREE_TYPE (list) == binfo)
269 /* The TREE_TYPE of LIST is the base class from which we started
270 walking. If that BINFO is virtual it's not a virtual baseclass
273 else if (TREE_VIA_VIRTUAL (binfo))
278 /* Remember the index to the vbase offset for this virtual
280 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), TREE_PURPOSE (list));
281 if (!TREE_VALUE (list))
282 BINFO_VPTR_FIELD (vbase) = build_int_2 (-3, 0);
285 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
286 BINFO_VPTR_FIELD (vbase)
287 = fold (build (MINUS_EXPR, integer_type_node,
288 BINFO_VPTR_FIELD (vbase), integer_one_node));
291 /* And record the offset at which this virtual base lies in the
293 init = BINFO_OFFSET (binfo);
294 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
295 init, TREE_VALUE (list));
298 SET_BINFO_VTABLE_PATH_MARKED (binfo);
303 /* Returns the initializers for the vbase offset entries in the vtable
304 for BINFO (which is part of the class hierarchy dominated by T), in
308 build_vbase_offset_vtbl_entries (binfo, t)
316 /* Under the old ABI, pointers to virtual bases are stored in each
318 if (!vbase_offsets_in_vtable_p ())
321 /* If there are no virtual baseclasses, then there is nothing to
323 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
328 /* The offsets are allocated in the reverse order of a
329 depth-first left-to-right traversal of the hierarchy. We use
330 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
331 dfs_walk, and so BINFO_MARKED is already in use. */
332 list = build_tree_list (t, NULL_TREE);
333 TREE_TYPE (list) = binfo;
335 dfs_build_vbase_offset_vtbl_entries,
336 unmarked_vtable_pathp,
339 dfs_vtable_path_unmark,
342 inits = nreverse (TREE_VALUE (list));
344 /* We've now got offsets in the right order. However, the offsets
345 we've stored are offsets from the beginning of the complete
346 object, and we need offsets from this BINFO. */
347 for (init = inits; init; init = TREE_CHAIN (init))
349 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
350 TREE_PURPOSE to scribble in. But, we need to clear it now so
351 that the values are not perceived as labeled initializers. */
352 TREE_PURPOSE (init) = NULL_TREE;
354 = fold (build1 (NOP_EXPR, vtable_entry_type,
355 size_diffop (TREE_VALUE (init),
356 BINFO_OFFSET (binfo))));
362 typedef struct vcall_offset_data_s
364 /* The binfo for the most-derived type. */
366 /* The binfo for the virtual base for which we're building
369 /* The vcall offset initializers built up so far. */
371 /* The number of vcall offsets accumulated. */
375 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
378 dfs_vcall_offset_queue_p (binfo, data)
382 vcall_offset_data* vod = (vcall_offset_data *) data;
384 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
387 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
390 dfs_build_vcall_offset_vtbl_entries (binfo, data)
394 vcall_offset_data* vod;
398 /* Primary bases are not interesting; all of the virtual
399 function table entries have been overridden. */
400 if (BINFO_PRIMARY_MARKED_P (binfo))
403 vod = (vcall_offset_data *) data;
404 binfo_inits = NULL_TREE;
406 /* We chain the offsets on in reverse order. That's correct --
407 build_vtbl_initializer will straighten them out. */
408 for (virtuals = BINFO_VIRTUALS (binfo);
410 virtuals = TREE_CHAIN (virtuals))
412 /* Figure out what function we're looking at. */
413 tree fn = TREE_VALUE (virtuals);
414 tree base = DECL_CONTEXT (fn);
415 /* The FN comes from BASE. So, we must caculate the adjustment
416 from the virtual base that derived from BINFO to BASE. */
417 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
420 = tree_cons (NULL_TREE,
421 fold (build1 (NOP_EXPR, vtable_entry_type,
422 size_diffop (BINFO_OFFSET (base_binfo),
423 BINFO_OFFSET (vod->vbase)))),
427 /* Now add the initializers we've just created to the list that will
428 be returned to our caller. */
429 vod->inits = chainon (vod->inits, binfo_inits);
434 /* Returns the initializers for the vcall offset entries in the vtable
435 for BINFO (which is part of the class hierarchy dominated by T), in
439 build_vcall_offset_vtbl_entries (binfo, t)
443 vcall_offset_data vod;
445 /* Under the old ABI, the adjustments to the `this' pointer were made
447 if (!vcall_offsets_in_vtable_p ())
450 /* We only need these entries if this base is a virtual base. */
451 if (!TREE_VIA_VIRTUAL (binfo))
454 /* We need a vcall offset for each of the virtual functions in this
457 class A { virtual void f (); };
458 class B : virtual public A { };
459 class C: virtual public A, public B {};
466 The location of `A' is not at a fixed offset relative to `B'; the
467 offset depends on the complete object derived from `B'. So,
468 `B' vtable contains an entry for `f' that indicates by what
469 amount the `this' pointer for `B' needs to be adjusted to arrive
472 We need entries for all the functions in our primary vtable and
473 in our non-virtual bases vtables. For each base, the entries
474 appear in the same order as in the base; but the bases themselves
475 appear in reverse depth-first, left-to-right order. */
478 vod.inits = NULL_TREE;
480 dfs_build_vcall_offset_vtbl_entries,
481 dfs_vcall_offset_queue_p,
487 /* Return vtbl initializers for the RTTI entries coresponding to the
488 BINFO's vtable. BINFO is a part of the hierarchy dominated by
492 build_rtti_vtbl_entries (binfo, t)
503 basetype = BINFO_TYPE (binfo);
506 /* For a COM object there is no RTTI entry. */
507 if (CLASSTYPE_COM_INTERFACE (basetype))
510 /* To find the complete object, we will first convert to our most
511 primary base, and then add the offset in the vtbl to that value. */
513 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)))
514 b = BINFO_BASETYPE (b,
515 CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (b)));
516 offset = size_diffop (size_zero_node, BINFO_OFFSET (b));
518 /* Add the offset-to-top entry. */
519 if (flag_vtable_thunks)
521 /* Convert the offset to look like a function pointer, so that
522 we can put it in the vtable. */
523 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
524 TREE_CONSTANT (init) = 1;
525 inits = tree_cons (NULL_TREE, init, inits);
528 /* The second entry is, in the case of the new ABI, the address of
529 the typeinfo object, or, in the case of the old ABI, a function
530 which returns a typeinfo object. */
531 if (new_abi_rtti_p ())
534 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
536 decl = integer_zero_node;
538 /* Convert the declaration to a type that can be stored in the
540 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
541 TREE_CONSTANT (init) = 1;
546 decl = get_tinfo_decl (t);
550 /* Convert the declaration to a type that can be stored in the
552 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, decl);
553 TREE_CONSTANT (init) = 1;
554 init = build_vtable_entry (offset, integer_zero_node, init);
557 /* Hook the RTTI declaration onto the list. */
558 inits = tree_cons (NULL_TREE, init, inits);
563 /* Returns a pointer to the virtual base class of EXP that has the
564 indicated TYPE. EXP is of class type, not a pointer type. */
567 build_vbase_pointer (exp, type)
570 if (vbase_offsets_in_vtable_p ())
575 /* Find the shared copy of TYPE; that's where the vtable offset
577 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
578 /* Find the virtual function table pointer. */
579 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
580 /* Compute the location where the offset will lie. */
581 vbase_ptr = build_binary_op (PLUS_EXPR,
583 BINFO_VPTR_FIELD (vbase));
584 vbase_ptr = build1 (NOP_EXPR,
585 build_pointer_type (ptrdiff_type_node),
587 /* Add the contents of this location to EXP. */
588 return build (PLUS_EXPR,
589 build_pointer_type (type),
590 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
591 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
596 FORMAT_VBASE_NAME (name, type);
597 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
601 /* Build multi-level access to EXPR using hierarchy path PATH.
602 CODE is PLUS_EXPR if we are going with the grain,
603 and MINUS_EXPR if we are not (in which case, we cannot traverse
604 virtual baseclass links).
606 TYPE is the type we want this path to have on exit.
608 NONNULL is non-zero if we know (for any reason) that EXPR is
609 not, in fact, zero. */
612 build_vbase_path (code, type, expr, path, nonnull)
614 tree type, expr, path;
617 register int changed = 0;
618 tree last = NULL_TREE, last_virtual = NULL_TREE;
620 tree null_expr = 0, nonnull_expr;
622 tree offset = integer_zero_node;
624 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
625 return build1 (NOP_EXPR, type, expr);
627 /* We could do better if we had additional logic to convert back to the
628 unconverted type (the static type of the complete object), and then
629 convert back to the type we want. Until that is done, we only optimize
630 if the complete type is the same type as expr has. */
631 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
633 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
634 expr = save_expr (expr);
637 path = reverse_path (path);
639 basetype = BINFO_TYPE (path);
643 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
645 last_virtual = BINFO_TYPE (TREE_VALUE (path));
646 if (code == PLUS_EXPR)
648 changed = ! fixed_type_p;
654 /* We already check for ambiguous things in the caller, just
658 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
659 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
661 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
662 nonnull_expr = build_vbase_pointer (ind, last_virtual);
664 && TREE_CODE (type) == POINTER_TYPE
665 && null_expr == NULL_TREE)
667 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
668 expr = build (COND_EXPR, build_pointer_type (last_virtual),
669 build (EQ_EXPR, boolean_type_node, expr,
671 null_expr, nonnull_expr);
674 /* else we'll figure out the offset below. */
676 /* Happens in the case of parse errors. */
677 if (nonnull_expr == error_mark_node)
678 return error_mark_node;
682 cp_error ("cannot cast up from virtual baseclass `%T'",
684 return error_mark_node;
687 last = TREE_VALUE (path);
688 path = TREE_CHAIN (path);
690 /* LAST is now the last basetype assoc on the path. */
692 /* A pointer to a virtual base member of a non-null object
693 is non-null. Therefore, we only need to test for zeroness once.
694 Make EXPR the canonical expression to deal with here. */
697 TREE_OPERAND (expr, 2) = nonnull_expr;
698 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
699 = TREE_TYPE (nonnull_expr);
704 /* If we go through any virtual base pointers, make sure that
705 casts to BASETYPE from the last virtual base class use
706 the right value for BASETYPE. */
709 tree intype = TREE_TYPE (TREE_TYPE (expr));
711 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
713 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
716 offset = BINFO_OFFSET (last);
718 if (! integer_zerop (offset))
720 /* Bash types to make the backend happy. */
721 offset = cp_convert (type, offset);
723 /* If expr might be 0, we need to preserve that zeroness. */
727 TREE_TYPE (null_expr) = type;
729 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
730 if (TREE_SIDE_EFFECTS (expr))
731 expr = save_expr (expr);
733 return build (COND_EXPR, type,
734 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
736 build (code, type, expr, offset));
738 else return build (code, type, expr, offset);
741 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
742 be used multiple times in initialization of multiple inheritance. */
745 TREE_TYPE (expr) = type;
749 return build1 (NOP_EXPR, type, expr);
753 /* Virtual function things. */
755 /* Build an entry in the virtual function table. DELTA is the offset
756 for the `this' pointer. VCALL_INDEX is the vtable index containing
757 the vcall offset; zero if none. ENTRY is the virtual function
758 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
759 but it may not actually be a virtual function table pointer. (For
760 example, it might be the address of the RTTI object, under the new
764 build_vtable_entry (delta, vcall_index, entry)
769 if (flag_vtable_thunks)
771 HOST_WIDE_INT idelta;
772 HOST_WIDE_INT ivindex;
774 idelta = tree_low_cst (delta, 0);
775 ivindex = tree_low_cst (vcall_index, 0);
776 if ((idelta || ivindex)
777 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry, 0)))
779 entry = make_thunk (entry, idelta, ivindex);
780 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
781 TREE_READONLY (entry) = 1;
782 TREE_CONSTANT (entry) = 1;
784 #ifdef GATHER_STATISTICS
785 n_vtable_entries += 1;
791 extern int flag_huge_objects;
792 tree elems = tree_cons (NULL_TREE, delta,
793 tree_cons (NULL_TREE, integer_zero_node,
794 build_tree_list (NULL_TREE, entry)));
795 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
797 /* We don't use vcall offsets when not using vtable thunks. */
798 my_friendly_assert (integer_zerop (vcall_index), 20000125);
800 /* DELTA used to be constructed by `size_int' and/or size_binop,
801 which caused overflow problems when it was negative. That should
804 if (! int_fits_type_p (delta, delta_type_node))
806 if (flag_huge_objects)
807 sorry ("object size exceeds built-in limit for virtual function table implementation");
809 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
812 TREE_CONSTANT (entry) = 1;
813 TREE_STATIC (entry) = 1;
814 TREE_READONLY (entry) = 1;
816 #ifdef GATHER_STATISTICS
817 n_vtable_entries += 1;
824 /* We want to give the assembler the vtable identifier as well as
825 the offset to the function pointer. So we generate
827 __asm__ __volatile__ (".vtable_entry %c0, %c1"
828 : : "s"(&class_vtable),
829 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
832 build_vtable_entry_ref (basetype, vtbl, idx)
833 tree basetype, vtbl, idx;
835 static char asm_stmt[] = ".vtable_entry %c0, %c1";
838 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
839 s = build_tree_list (build_string (1, "s"), s);
841 i = build_array_ref (vtbl, idx);
842 if (!flag_vtable_thunks)
843 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
844 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
845 i2 = build_array_ref (vtbl, build_int_2(0,0));
846 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
847 i = build_binary_op (MINUS_EXPR, i, i2);
848 i = build_tree_list (build_string (1, "i"), i);
850 finish_asm_stmt (ridpointers[RID_VOLATILE],
851 build_string (sizeof(asm_stmt)-1, asm_stmt),
852 NULL_TREE, chainon (s, i), NULL_TREE);
855 /* Given an object INSTANCE, return an expression which yields the
856 virtual function vtable element corresponding to INDEX. There are
857 many special cases for INSTANCE which we take care of here, mainly
858 to avoid creating extra tree nodes when we don't have to. */
861 build_vtbl_ref (instance, idx)
865 tree basetype = TREE_TYPE (instance);
867 if (TREE_CODE (basetype) == REFERENCE_TYPE)
868 basetype = TREE_TYPE (basetype);
870 if (instance == current_class_ref)
871 vtbl = build_vfield_ref (instance, basetype);
876 /* Try to figure out what a reference refers to, and
877 access its virtual function table directly. */
878 tree ref = NULL_TREE;
880 if (TREE_CODE (instance) == INDIRECT_REF
881 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
882 ref = TREE_OPERAND (instance, 0);
883 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
886 if (ref && TREE_CODE (ref) == VAR_DECL
887 && DECL_INITIAL (ref))
889 tree init = DECL_INITIAL (ref);
891 while (TREE_CODE (init) == NOP_EXPR
892 || TREE_CODE (init) == NON_LVALUE_EXPR)
893 init = TREE_OPERAND (init, 0);
894 if (TREE_CODE (init) == ADDR_EXPR)
896 init = TREE_OPERAND (init, 0);
897 if (IS_AGGR_TYPE (TREE_TYPE (init))
898 && (TREE_CODE (init) == PARM_DECL
899 || TREE_CODE (init) == VAR_DECL))
905 if (IS_AGGR_TYPE (TREE_TYPE (instance))
906 && (TREE_CODE (instance) == RESULT_DECL
907 || TREE_CODE (instance) == PARM_DECL
908 || TREE_CODE (instance) == VAR_DECL))
909 vtbl = TYPE_BINFO_VTABLE (basetype);
911 vtbl = build_vfield_ref (instance, basetype);
914 assemble_external (vtbl);
917 build_vtable_entry_ref (basetype, vtbl, idx);
919 aref = build_array_ref (vtbl, idx);
924 /* Given an object INSTANCE, return an expression which yields the
925 virtual function corresponding to INDEX. There are many special
926 cases for INSTANCE which we take care of here, mainly to avoid
927 creating extra tree nodes when we don't have to. */
930 build_vfn_ref (ptr_to_instptr, instance, idx)
931 tree *ptr_to_instptr, instance;
934 tree aref = build_vtbl_ref (instance, idx);
936 /* When using thunks, there is no extra delta, and we get the pfn
938 if (flag_vtable_thunks)
943 /* Save the intermediate result in a SAVE_EXPR so we don't have to
944 compute each component of the virtual function pointer twice. */
945 if (TREE_CODE (aref) == INDIRECT_REF)
946 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
949 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
951 cp_convert (ptrdiff_type_node,
952 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
955 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
958 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
959 for the given TYPE. */
962 get_vtable_name (type)
965 tree type_id = build_typename_overload (type);
966 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
967 + IDENTIFIER_LENGTH (type_id) + 2);
968 const char *ptr = IDENTIFIER_POINTER (type_id);
970 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
972 /* We don't take off the numbers; build_secondary_vtable uses the
973 DECL_ASSEMBLER_NAME for the type, which includes the number
974 in `3foo'. If we were to pull them off here, we'd end up with
975 something like `_vt.foo.3bar', instead of a uniform definition. */
976 while (ptr[i] >= '0' && ptr[i] <= '9')
979 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
980 return get_identifier (buf);
983 /* Return the offset to the main vtable for a given base BINFO. */
986 get_vfield_offset (binfo)
990 size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
991 BINFO_OFFSET (binfo));
994 /* Get the offset to the start of the original binfo that we derived
995 this binfo from. If we find TYPE first, return the offset only
996 that far. The shortened search is useful because the this pointer
997 on method calling is expected to point to a DECL_CONTEXT (fndecl)
998 object, and not a baseclass of it. */
1002 get_derived_offset (binfo, type)
1005 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
1009 while (BINFO_BASETYPES (binfo)
1010 && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
1012 tree binfos = BINFO_BASETYPES (binfo);
1013 if (BINFO_TYPE (binfo) == type)
1015 binfo = TREE_VEC_ELT (binfos, i);
1018 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
1019 return size_binop (MINUS_EXPR, offset1, offset2);
1022 /* Create a VAR_DECL for a primary or secondary vtable for
1023 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
1027 build_vtable (class_type, name, vtable_type)
1034 decl = build_lang_decl (VAR_DECL, name, vtable_type);
1035 DECL_CONTEXT (decl) = class_type;
1036 DECL_ARTIFICIAL (decl) = 1;
1037 TREE_STATIC (decl) = 1;
1038 #ifndef WRITABLE_VTABLES
1039 /* Make them READONLY by default. (mrs) */
1040 TREE_READONLY (decl) = 1;
1042 DECL_VIRTUAL_P (decl) = 1;
1043 import_export_vtable (decl, class_type, 0);
1048 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1049 or even complete. If this does not exist, create it. If COMPLETE is
1050 non-zero, then complete the definition of it -- that will render it
1051 impossible to actually build the vtable, but is useful to get at those
1052 which are known to exist in the runtime. */
1055 get_vtable_decl (type, complete)
1059 tree name = get_vtable_name (type);
1060 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1064 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1065 && DECL_VIRTUAL_P (decl), 20000118);
1069 decl = build_vtable (type, name, void_type_node);
1070 decl = pushdecl_top_level (decl);
1071 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1073 /* At one time the vtable info was grabbed 2 words at a time. This
1074 fails on sparc unless you have 8-byte alignment. (tiemann) */
1075 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1080 DECL_EXTERNAL (decl) = 1;
1081 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1087 /* Build the primary virtual function table for TYPE. If BINFO is
1088 non-NULL, build the vtable starting with the initial approximation
1089 that it is the same as the one which is the head of the association
1090 list. Returns a non-zero value if a new vtable is actually
1094 build_primary_vtable (binfo, type)
1097 tree virtuals, decl;
1099 decl = get_vtable_decl (type, /*complete=*/0);
1103 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
1104 /* We have already created a vtable for this base, so there's
1105 no need to do it again. */
1108 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1109 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1110 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
1111 DECL_SIZE_UNIT (decl)
1112 = TYPE_SIZE_UNIT (TREE_TYPE (BINFO_VTABLE (binfo)));
1116 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1118 virtuals = NULL_TREE;
1121 #ifdef GATHER_STATISTICS
1123 n_vtable_elems += list_length (virtuals);
1126 /* Initialize the association list for this type, based
1127 on our first approximation. */
1128 TYPE_BINFO_VTABLE (type) = decl;
1129 TYPE_BINFO_VIRTUALS (type) = virtuals;
1131 binfo = TYPE_BINFO (type);
1132 SET_BINFO_NEW_VTABLE_MARKED (binfo, type);
1136 /* Give TYPE a new virtual function table which is initialized
1137 with a skeleton-copy of its original initialization. The only
1138 entry that changes is the `delta' entry, so we can really
1139 share a lot of structure.
1141 FOR_TYPE is the derived type which caused this table to
1144 BINFO is the type association which provided TYPE for FOR_TYPE.
1146 The order in which vtables are built (by calling this function) for
1147 an object must remain the same, otherwise a binary incompatibility
1151 build_secondary_vtable (binfo, for_type)
1152 tree binfo, for_type;
1155 tree orig_decl = BINFO_VTABLE (binfo);
1168 if (TREE_VIA_VIRTUAL (binfo))
1169 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1170 current_class_type),
1173 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
1174 /* We already created a vtable for this base. There's no need to
1178 /* Remember that we've created a vtable for this BINFO, so that we
1179 don't try to do so again. */
1180 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
1182 /* Make fresh virtual list, so we can smash it later. */
1183 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1185 if (TREE_VIA_VIRTUAL (binfo))
1187 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1189 /* XXX - This should never happen, if it does, the caller should
1190 ensure that the binfo is from for_type's binfos, not from any
1191 base type's. We can remove all this code after a while. */
1192 if (binfo1 != binfo)
1193 warning ("internal inconsistency: binfo offset error for rtti");
1195 offset = BINFO_OFFSET (binfo1);
1198 offset = BINFO_OFFSET (binfo);
1200 /* In the new ABI, secondary vtables are laid out as part of the
1201 same structure as the primary vtable. */
1202 if (merge_primary_and_secondary_vtables_p ())
1204 BINFO_VTABLE (binfo) = NULL_TREE;
1208 /* Create the declaration for the secondary vtable. */
1209 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
1210 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1211 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1213 /* We know that the vtable that we are going to create doesn't exist
1214 yet in the global namespace, and when we finish, it will be
1215 pushed into the global namespace. In complex MI hierarchies, we
1216 have to loop while the name we are thinking of adding is globally
1217 defined, adding more name components to the vtable name as we
1218 loop, until the name is unique. This is because in complex MI
1219 cases, we might have the same base more than once. This means
1220 that the order in which this function is called for vtables must
1221 remain the same, otherwise binary compatibility can be
1226 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1230 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1232 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1233 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1234 name = get_identifier (buf);
1236 /* If this name doesn't clash, then we can use it, otherwise
1237 we add more to the name until it is unique. */
1239 if (! IDENTIFIER_GLOBAL_VALUE (name))
1242 /* Set values for next loop through, if the name isn't unique. */
1244 path = BINFO_INHERITANCE_CHAIN (path);
1246 /* We better not run out of stuff to make it unique. */
1247 my_friendly_assert (path != NULL_TREE, 368);
1249 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1251 if (for_type == basetype)
1253 /* If we run out of basetypes in the path, we have already
1254 found created a vtable with that name before, we now
1255 resort to tacking on _%d to distinguish them. */
1257 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1258 buf1 = (char *) alloca (i);
1260 sprintf (buf1, "%s%c%s%c%d",
1261 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1263 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1264 + strlen (buf1) + 1);
1265 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1266 name = get_identifier (buf);
1268 /* If this name doesn't clash, then we can use it,
1269 otherwise we add something different to the name until
1271 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1273 /* Hey, they really like MI don't they? Increase the 3
1274 above to 6, and the 999 to 999999. :-) */
1275 my_friendly_assert (j <= 999, 369);
1280 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1281 new_buf2 = (char *) alloca (i);
1282 sprintf (new_buf2, "%s%c%s",
1283 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1287 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
1288 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1289 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1291 #ifdef GATHER_STATISTICS
1293 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1299 /* Create a new vtable for BINFO which is the hierarchy dominated by
1303 make_new_vtable (t, binfo)
1307 if (binfo == TYPE_BINFO (t))
1308 /* In this case, it is *type*'s vtable we are modifying. We start
1309 with the approximation that it's vtable is that of the
1310 immediate base class. */
1311 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1314 /* This is our very own copy of `basetype' to play with. Later,
1315 we will fill in all the virtual functions that override the
1316 virtual functions in these base classes which are not defined
1317 by the current type. */
1318 return build_secondary_vtable (binfo, t);
1321 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1322 (which is in the hierarchy dominated by T) list FNDECL as its
1323 BV_FN. DELTA is the required adjustment from the `this' pointer
1324 where the vtable entry appears to the `this' required when the
1325 function is actually called. */
1328 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1339 vcall_index = integer_zero_node;
1341 if (fndecl != BV_FN (v)
1342 || !tree_int_cst_equal (delta, BV_DELTA (v))
1343 || !tree_int_cst_equal (vcall_index, BV_VCALL_INDEX (v)))
1347 /* We need a new vtable for BINFO. */
1348 if (make_new_vtable (t, binfo))
1350 /* If we really did make a new vtable, we also made a copy
1351 of the BINFO_VIRTUALS list. Now, we have to find the
1352 corresponding entry in that list. */
1353 *virtuals = BINFO_VIRTUALS (binfo);
1354 while (BV_FN (*virtuals) != BV_FN (v))
1355 *virtuals = TREE_CHAIN (*virtuals);
1359 base_fndecl = BV_FN (v);
1360 BV_DELTA (v) = delta;
1361 BV_VCALL_INDEX (v) = vcall_index;
1364 /* Now assign virtual dispatch information, if unset. We can
1365 dispatch this, through any overridden base function. */
1366 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1368 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1369 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1374 /* Return the index (in the virtual function table) of the first
1375 virtual function. */
1378 first_vfun_index (t)
1381 /* Under the old ABI, the offset-to-top and RTTI entries are at
1382 indices zero and one; under the new ABI, the first virtual
1383 function is at index zero. */
1384 if (!CLASSTYPE_COM_INTERFACE (t) && !flag_new_abi)
1385 return flag_vtable_thunks ? 2 : 1;
1390 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1391 functions present in the vtable so far. */
1394 set_vindex (t, decl, vfuns_p)
1401 vindex = (*vfuns_p)++;
1402 vindex += first_vfun_index (t);
1403 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1406 /* Add a virtual function to all the appropriate vtables for the class
1407 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1408 allocate a new slot in our table. If it is error_mark_node, we
1409 know that no other function from another vtable is overridden by X.
1410 VFUNS_P keeps track of how many virtuals there are in our
1411 main vtable for the type, and we build upon the NEW_VIRTUALS list
1415 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1417 tree *new_virtuals_p;
1418 tree *overridden_virtuals_p;
1421 tree t; /* Structure type. */
1425 /* If this function doesn't override anything from a base class, we
1426 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1427 override something, we keep it around and assign its DECL_VINDEX
1428 later, in modify_all_vtables. */
1429 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1430 /* We've already dealt with this function. */
1433 new_virtual = build_tree_list (integer_zero_node, fndecl);
1434 BV_VCALL_INDEX (new_virtual) = integer_zero_node;
1436 if (DECL_VINDEX (fndecl) == error_mark_node)
1438 /* FNDECL is a new virtual function; it doesn't override any
1439 virtual function in a base class. */
1441 /* We remember that this was the base sub-object for rtti. */
1442 CLASSTYPE_RTTI (t) = t;
1444 /* Now assign virtual dispatch information. */
1445 set_vindex (t, fndecl, vfuns_p);
1446 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1448 /* Save the state we've computed on the NEW_VIRTUALS list. */
1449 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1450 *new_virtuals_p = new_virtual;
1454 /* FNDECL overrides a function from a base class. */
1455 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1456 *overridden_virtuals_p = new_virtual;
1460 extern struct obstack *current_obstack;
1462 /* Add method METHOD to class TYPE.
1464 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1465 the class type where the method should be added. */
1468 add_method (type, fields, method)
1469 tree type, *fields, method;
1471 int using = (DECL_CONTEXT (method) != type);
1473 if (fields && *fields)
1474 *fields = build_overload (method, *fields);
1481 if (!CLASSTYPE_METHOD_VEC (type))
1482 /* Make a new method vector. We start with 8 entries. We must
1483 allocate at least two (for constructors and destructors), and
1484 we're going to end up with an assignment operator at some
1487 We could use a TREE_LIST for now, and convert it to a
1488 TREE_VEC in finish_struct, but we would probably waste more
1489 memory making the links in the list than we would by
1490 over-allocating the size of the vector here. Furthermore,
1491 we would complicate all the code that expects this to be a
1493 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1495 method_vec = CLASSTYPE_METHOD_VEC (type);
1496 len = TREE_VEC_LENGTH (method_vec);
1498 if (DECL_NAME (method) == constructor_name (type))
1499 /* A new constructor or destructor. Constructors go in
1500 slot 0; destructors go in slot 1. */
1501 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1504 /* See if we already have an entry with this name. */
1505 for (slot = 2; slot < len; ++slot)
1506 if (!TREE_VEC_ELT (method_vec, slot)
1507 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1509 == DECL_NAME (method)))
1514 /* We need a bigger method vector. */
1515 tree new_vec = make_tree_vec (2 * len);
1516 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1517 (PTR) &TREE_VEC_ELT (new_vec, 0),
1518 len * sizeof (tree));
1520 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1523 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1525 /* Type conversion operators have to come before
1526 ordinary methods; add_conversions depends on this to
1527 speed up looking for conversion operators. So, if
1528 necessary, we slide some of the vector elements up.
1529 In theory, this makes this algorithm O(N^2) but we
1530 don't expect many conversion operators. */
1531 for (slot = 2; slot < len; ++slot)
1533 tree fn = TREE_VEC_ELT (method_vec, slot);
1536 /* There are no more entries in the vector, so we
1537 can insert the new conversion operator here. */
1540 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1541 /* We can insert the new function right at the
1546 if (!TREE_VEC_ELT (method_vec, slot))
1547 /* There is nothing in the Ith slot, so we can avoid
1552 /* We know the last slot in the vector is empty
1553 because we know that at this point there's room
1554 for a new function. */
1555 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1556 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1557 (len - slot - 1) * sizeof (tree));
1558 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1563 if (template_class_depth (type))
1564 /* TYPE is a template class. Don't issue any errors now; wait
1565 until instantiation time to complain. */
1571 /* Check to see if we've already got this method. */
1572 for (fns = TREE_VEC_ELT (method_vec, slot);
1574 fns = OVL_NEXT (fns))
1576 tree fn = OVL_CURRENT (fns);
1578 if (TREE_CODE (fn) != TREE_CODE (method))
1581 if (TREE_CODE (method) != TEMPLATE_DECL)
1583 /* [over.load] Member function declarations with the
1584 same name and the same parameter types cannot be
1585 overloaded if any of them is a static member
1586 function declaration. */
1587 if ((DECL_STATIC_FUNCTION_P (fn)
1588 != DECL_STATIC_FUNCTION_P (method))
1591 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1592 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1594 if (! DECL_STATIC_FUNCTION_P (fn))
1595 parms1 = TREE_CHAIN (parms1);
1596 if (! DECL_STATIC_FUNCTION_P (method))
1597 parms2 = TREE_CHAIN (parms2);
1599 if (compparms (parms1, parms2))
1602 /* Defer to the local function. */
1605 cp_error ("`%#D' and `%#D' cannot be overloaded",
1610 /* Since this is an ordinary function in a
1611 non-template class, it's mangled name can be used
1612 as a unique identifier. This technique is only
1613 an optimization; we would get the same results if
1614 we just used decls_match here. */
1615 if (DECL_ASSEMBLER_NAME (fn)
1616 != DECL_ASSEMBLER_NAME (method))
1619 else if (!decls_match (fn, method))
1622 /* There has already been a declaration of this method
1623 or member template. */
1624 cp_error_at ("`%D' has already been declared in `%T'",
1627 /* We don't call duplicate_decls here to merge the
1628 declarations because that will confuse things if the
1629 methods have inline definitions. In particular, we
1630 will crash while processing the definitions. */
1635 /* Actually insert the new method. */
1636 TREE_VEC_ELT (method_vec, slot)
1637 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1639 /* Add the new binding. */
1640 if (!DECL_CONSTRUCTOR_P (method)
1641 && !DECL_DESTRUCTOR_P (method))
1642 push_class_level_binding (DECL_NAME (method),
1643 TREE_VEC_ELT (method_vec, slot));
1647 /* Subroutines of finish_struct. */
1649 /* Look through the list of fields for this struct, deleting
1650 duplicates as we go. This must be recursive to handle
1653 FIELD is the field which may not appear anywhere in FIELDS.
1654 FIELD_PTR, if non-null, is the starting point at which
1655 chained deletions may take place.
1656 The value returned is the first acceptable entry found
1659 Note that anonymous fields which are not of UNION_TYPE are
1660 not duplicates, they are just anonymous fields. This happens
1661 when we have unnamed bitfields, for example. */
1664 delete_duplicate_fields_1 (field, fields)
1669 if (DECL_NAME (field) == 0)
1671 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1674 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1675 fields = delete_duplicate_fields_1 (x, fields);
1680 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1682 if (DECL_NAME (x) == 0)
1684 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1686 TYPE_FIELDS (TREE_TYPE (x))
1687 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1688 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1691 fields = TREE_CHAIN (fields);
1693 TREE_CHAIN (prev) = TREE_CHAIN (x);
1696 else if (TREE_CODE (field) == USING_DECL)
1697 /* A using declaration may is allowed to appear more than
1698 once. We'll prune these from the field list later, and
1699 handle_using_decl will complain about invalid multiple
1702 else if (DECL_NAME (field) == DECL_NAME (x))
1704 if (TREE_CODE (field) == CONST_DECL
1705 && TREE_CODE (x) == CONST_DECL)
1706 cp_error_at ("duplicate enum value `%D'", x);
1707 else if (TREE_CODE (field) == CONST_DECL
1708 || TREE_CODE (x) == CONST_DECL)
1709 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1711 else if (DECL_DECLARES_TYPE_P (field)
1712 && DECL_DECLARES_TYPE_P (x))
1714 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1716 cp_error_at ("duplicate nested type `%D'", x);
1718 else if (DECL_DECLARES_TYPE_P (field)
1719 || DECL_DECLARES_TYPE_P (x))
1721 /* Hide tag decls. */
1722 if ((TREE_CODE (field) == TYPE_DECL
1723 && DECL_ARTIFICIAL (field))
1724 || (TREE_CODE (x) == TYPE_DECL
1725 && DECL_ARTIFICIAL (x)))
1727 cp_error_at ("duplicate field `%D' (as type and non-type)",
1731 cp_error_at ("duplicate member `%D'", x);
1733 fields = TREE_CHAIN (fields);
1735 TREE_CHAIN (prev) = TREE_CHAIN (x);
1743 delete_duplicate_fields (fields)
1747 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1748 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1751 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1752 legit, otherwise return 0. */
1755 alter_access (t, fdecl, access)
1760 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1763 if (TREE_VALUE (elem) != access)
1765 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1766 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1768 error ("conflicting access specifications for field `%s', ignored",
1769 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1773 /* They're changing the access to the same thing they changed
1774 it to before. That's OK. */
1780 enforce_access (t, fdecl);
1781 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1787 /* Process the USING_DECL, which is a member of T. */
1790 handle_using_decl (using_decl, t)
1794 tree ctype = DECL_INITIAL (using_decl);
1795 tree name = DECL_NAME (using_decl);
1797 = TREE_PRIVATE (using_decl) ? access_private_node
1798 : TREE_PROTECTED (using_decl) ? access_protected_node
1799 : access_public_node;
1801 tree flist = NULL_TREE;
1804 binfo = binfo_or_else (ctype, t);
1808 if (name == constructor_name (ctype)
1809 || name == constructor_name_full (ctype))
1811 cp_error_at ("using-declaration for constructor", using_decl);
1815 fdecl = lookup_member (binfo, name, 0, 0);
1819 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1823 if (BASELINK_P (fdecl))
1824 /* Ignore base type this came from. */
1825 fdecl = TREE_VALUE (fdecl);
1827 old_value = IDENTIFIER_CLASS_VALUE (name);
1830 if (is_overloaded_fn (old_value))
1831 old_value = OVL_CURRENT (old_value);
1833 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1836 old_value = NULL_TREE;
1839 if (is_overloaded_fn (fdecl))
1841 else if (! DECL_LANG_SPECIFIC (fdecl))
1842 my_friendly_abort (20000221);
1846 else if (is_overloaded_fn (old_value))
1849 /* It's OK to use functions from a base when there are functions with
1850 the same name already present in the current class. */;
1853 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1854 cp_error_at (" because of local method `%#D' with same name",
1855 OVL_CURRENT (old_value));
1861 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1862 cp_error_at (" because of local field `%#D' with same name", old_value);
1866 /* Make type T see field decl FDECL with access ACCESS.*/
1868 for (; flist; flist = OVL_NEXT (flist))
1870 add_method (t, 0, OVL_CURRENT (flist));
1871 alter_access (t, OVL_CURRENT (flist), access);
1874 alter_access (t, fdecl, access);
1877 /* Run through the base clases of T, updating
1878 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1879 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1883 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1886 int *cant_have_default_ctor_p;
1887 int *cant_have_const_ctor_p;
1888 int *no_const_asn_ref_p;
1892 int seen_nearly_empty_base_p;
1895 binfos = TYPE_BINFO_BASETYPES (t);
1896 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1897 seen_nearly_empty_base_p = 0;
1899 /* An aggregate cannot have baseclasses. */
1900 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1902 for (i = 0; i < n_baseclasses; ++i)
1907 /* Figure out what base we're looking at. */
1908 base_binfo = TREE_VEC_ELT (binfos, i);
1909 basetype = TREE_TYPE (base_binfo);
1911 /* If the type of basetype is incomplete, then we already
1912 complained about that fact (and we should have fixed it up as
1914 if (!COMPLETE_TYPE_P (basetype))
1917 /* The base type is of incomplete type. It is
1918 probably best to pretend that it does not
1920 if (i == n_baseclasses-1)
1921 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1922 TREE_VEC_LENGTH (binfos) -= 1;
1924 for (j = i; j+1 < n_baseclasses; j++)
1925 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1929 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1930 here because the case of virtual functions but non-virtual
1931 dtor is handled in finish_struct_1. */
1932 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1933 && TYPE_HAS_DESTRUCTOR (basetype))
1934 cp_warning ("base class `%#T' has a non-virtual destructor",
1937 /* If the base class doesn't have copy constructors or
1938 assignment operators that take const references, then the
1939 derived class cannot have such a member automatically
1941 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1942 *cant_have_const_ctor_p = 1;
1943 if (TYPE_HAS_ASSIGN_REF (basetype)
1944 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1945 *no_const_asn_ref_p = 1;
1946 /* Similarly, if the base class doesn't have a default
1947 constructor, then the derived class won't have an
1948 automatically generated default constructor. */
1949 if (TYPE_HAS_CONSTRUCTOR (basetype)
1950 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1952 *cant_have_default_ctor_p = 1;
1953 if (! TYPE_HAS_CONSTRUCTOR (t))
1954 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1958 /* If the base class is not empty or nearly empty, then this
1959 class cannot be nearly empty. */
1960 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1961 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1962 /* And if there is more than one nearly empty base, then the
1963 derived class is not nearly empty either. */
1964 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1965 && seen_nearly_empty_base_p)
1966 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1967 /* If this is the first nearly empty base class, then remember
1969 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1970 seen_nearly_empty_base_p = 1;
1972 /* A lot of properties from the bases also apply to the derived
1974 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1975 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1976 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1977 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1978 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1979 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1980 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1981 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1982 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1983 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1985 /* Derived classes can implicitly become COMified if their bases
1987 if (CLASSTYPE_COM_INTERFACE (basetype))
1988 CLASSTYPE_COM_INTERFACE (t) = 1;
1989 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1992 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1994 CLASSTYPE_COM_INTERFACE (t) = 0;
1999 /* Make the Ith baseclass of T its primary base. */
2002 set_primary_base (t, i, vfuns_p)
2009 CLASSTYPE_VFIELD_PARENT (t) = i;
2010 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
2011 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
2012 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
2013 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2014 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
2015 *vfuns_p = CLASSTYPE_VSIZE (basetype);
2018 /* Determine the primary class for T. */
2021 determine_primary_base (t, vfuns_p)
2025 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2027 /* If there are no baseclasses, there is certainly no primary base. */
2028 if (n_baseclasses == 0)
2033 for (i = 0; i < n_baseclasses; i++)
2035 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2036 tree basetype = BINFO_TYPE (base_binfo);
2038 if (TYPE_CONTAINS_VPTR_P (basetype))
2040 /* Even a virtual baseclass can contain our RTTI
2041 information. But, we prefer a non-virtual polymorphic
2043 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2044 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
2046 /* A virtual baseclass can't be the primary base under the
2047 old ABI. And under the new ABI we still prefer a
2048 non-virtual base. */
2049 if (TREE_VIA_VIRTUAL (base_binfo))
2052 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2054 set_primary_base (t, i, vfuns_p);
2055 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2061 /* Only add unique vfields, and flatten them out as we go. */
2062 for (vfields = CLASSTYPE_VFIELDS (basetype);
2064 vfields = TREE_CHAIN (vfields))
2065 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2066 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2067 CLASSTYPE_VFIELDS (t)
2068 = tree_cons (base_binfo,
2069 VF_BASETYPE_VALUE (vfields),
2070 CLASSTYPE_VFIELDS (t));
2073 set_primary_base (t, i, vfuns_p);
2078 if (!TYPE_VFIELD (t))
2079 CLASSTYPE_VFIELD_PARENT (t) = -1;
2081 /* The new ABI allows for the use of a "nearly-empty" virtual base
2082 class as the primary base class if no non-virtual polymorphic
2083 base can be found. */
2084 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2085 for (i = 0; i < n_baseclasses; ++i)
2087 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2088 tree basetype = BINFO_TYPE (base_binfo);
2090 if (TREE_VIA_VIRTUAL (base_binfo)
2091 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2093 set_primary_base (t, i, vfuns_p);
2094 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2099 /* Mark the primary base classes at this point. */
2100 mark_primary_bases (t);
2103 /* Set memoizing fields and bits of T (and its variants) for later
2107 finish_struct_bits (t)
2110 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2112 /* Fix up variants (if any). */
2113 tree variants = TYPE_NEXT_VARIANT (t);
2116 /* These fields are in the _TYPE part of the node, not in
2117 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2118 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2119 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2120 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2121 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2122 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2124 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2125 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2126 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2127 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2128 /* Copy whatever these are holding today. */
2129 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2130 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2131 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2132 TYPE_SIZE (variants) = TYPE_SIZE (t);
2133 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2134 variants = TYPE_NEXT_VARIANT (variants);
2137 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2138 /* For a class w/o baseclasses, `finish_struct' has set
2139 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2140 definition). Similarly for a class whose base classes do not
2141 have vtables. When neither of these is true, we might have
2142 removed abstract virtuals (by providing a definition), added
2143 some (by declaring new ones), or redeclared ones from a base
2144 class. We need to recalculate what's really an abstract virtual
2145 at this point (by looking in the vtables). */
2146 get_pure_virtuals (t);
2150 /* Notice whether this class has type conversion functions defined. */
2151 tree binfo = TYPE_BINFO (t);
2152 tree binfos = BINFO_BASETYPES (binfo);
2155 for (i = n_baseclasses-1; i >= 0; i--)
2157 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2159 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2163 /* If this type has a copy constructor, force its mode to be BLKmode, and
2164 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2165 be passed by invisible reference and prevent it from being returned in
2168 Also do this if the class has BLKmode but can still be returned in
2169 registers, since function_cannot_inline_p won't let us inline
2170 functions returning such a type. This affects the HP-PA. */
2171 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2172 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2173 && CLASSTYPE_NON_AGGREGATE (t)))
2176 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2177 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2179 TYPE_MODE (variants) = BLKmode;
2180 TREE_ADDRESSABLE (variants) = 1;
2185 /* Issue warnings about T having private constructors, but no friends,
2188 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2189 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2190 non-private static member functions. */
2193 maybe_warn_about_overly_private_class (t)
2196 int has_member_fn = 0;
2197 int has_nonprivate_method = 0;
2200 if (!warn_ctor_dtor_privacy
2201 /* If the class has friends, those entities might create and
2202 access instances, so we should not warn. */
2203 || (CLASSTYPE_FRIEND_CLASSES (t)
2204 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2205 /* We will have warned when the template was declared; there's
2206 no need to warn on every instantiation. */
2207 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2208 /* There's no reason to even consider warning about this
2212 /* We only issue one warning, if more than one applies, because
2213 otherwise, on code like:
2216 // Oops - forgot `public:'
2222 we warn several times about essentially the same problem. */
2224 /* Check to see if all (non-constructor, non-destructor) member
2225 functions are private. (Since there are no friends or
2226 non-private statics, we can't ever call any of the private member
2228 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2229 /* We're not interested in compiler-generated methods; they don't
2230 provide any way to call private members. */
2231 if (!DECL_ARTIFICIAL (fn))
2233 if (!TREE_PRIVATE (fn))
2235 if (DECL_STATIC_FUNCTION_P (fn))
2236 /* A non-private static member function is just like a
2237 friend; it can create and invoke private member
2238 functions, and be accessed without a class
2242 has_nonprivate_method = 1;
2245 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2249 if (!has_nonprivate_method && has_member_fn)
2251 /* There are no non-private methods, and there's at least one
2252 private member function that isn't a constructor or
2253 destructor. (If all the private members are
2254 constructors/destructors we want to use the code below that
2255 issues error messages specifically referring to
2256 constructors/destructors.) */
2258 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2259 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2260 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2261 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2263 has_nonprivate_method = 1;
2266 if (!has_nonprivate_method)
2268 cp_warning ("all member functions in class `%T' are private", t);
2273 /* Even if some of the member functions are non-private, the class
2274 won't be useful for much if all the constructors or destructors
2275 are private: such an object can never be created or destroyed. */
2276 if (TYPE_HAS_DESTRUCTOR (t))
2278 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2280 if (TREE_PRIVATE (dtor))
2282 cp_warning ("`%#T' only defines a private destructor and has no friends",
2288 if (TYPE_HAS_CONSTRUCTOR (t))
2290 int nonprivate_ctor = 0;
2292 /* If a non-template class does not define a copy
2293 constructor, one is defined for it, enabling it to avoid
2294 this warning. For a template class, this does not
2295 happen, and so we would normally get a warning on:
2297 template <class T> class C { private: C(); };
2299 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2300 complete non-template or fully instantiated classes have this
2302 if (!TYPE_HAS_INIT_REF (t))
2303 nonprivate_ctor = 1;
2305 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2309 tree ctor = OVL_CURRENT (fn);
2310 /* Ideally, we wouldn't count copy constructors (or, in
2311 fact, any constructor that takes an argument of the
2312 class type as a parameter) because such things cannot
2313 be used to construct an instance of the class unless
2314 you already have one. But, for now at least, we're
2316 if (! TREE_PRIVATE (ctor))
2318 nonprivate_ctor = 1;
2323 if (nonprivate_ctor == 0)
2325 cp_warning ("`%#T' only defines private constructors and has no friends",
2332 /* Function to help qsort sort FIELD_DECLs by name order. */
2335 field_decl_cmp (x, y)
2338 if (DECL_NAME (*x) == DECL_NAME (*y))
2339 /* A nontype is "greater" than a type. */
2340 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2341 if (DECL_NAME (*x) == NULL_TREE)
2343 if (DECL_NAME (*y) == NULL_TREE)
2345 if (DECL_NAME (*x) < DECL_NAME (*y))
2350 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2353 method_name_cmp (m1, m2)
2354 const tree *m1, *m2;
2356 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2358 if (*m1 == NULL_TREE)
2360 if (*m2 == NULL_TREE)
2362 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2367 /* Warn about duplicate methods in fn_fields. Also compact method
2368 lists so that lookup can be made faster.
2370 Data Structure: List of method lists. The outer list is a
2371 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2372 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2373 links the entire list of methods for TYPE_METHODS. Friends are
2374 chained in the same way as member functions (? TREE_CHAIN or
2375 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2376 list. That allows them to be quickly deleted, and requires no
2379 If there are any constructors/destructors, they are moved to the
2380 front of the list. This makes pushclass more efficient.
2382 @@ The above comment is obsolete. It mostly describes what add_method
2383 @@ and add_implicitly_declared_members do.
2385 Sort methods that are not special (i.e., constructors, destructors, and
2386 type conversion operators) so that we can find them faster in search. */
2389 finish_struct_methods (t)
2394 tree ctor_name = constructor_name (t);
2397 if (!TYPE_METHODS (t))
2399 /* Clear these for safety; perhaps some parsing error could set
2400 these incorrectly. */
2401 TYPE_HAS_CONSTRUCTOR (t) = 0;
2402 TYPE_HAS_DESTRUCTOR (t) = 0;
2403 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2407 method_vec = CLASSTYPE_METHOD_VEC (t);
2408 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2409 len = TREE_VEC_LENGTH (method_vec);
2411 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2412 and the next few with type conversion operators (if any). */
2413 for (fn_fields = TYPE_METHODS (t); fn_fields;
2414 fn_fields = TREE_CHAIN (fn_fields))
2416 tree fn_name = DECL_NAME (fn_fields);
2418 /* Clear out this flag.
2420 @@ Doug may figure out how to break
2421 @@ this with nested classes and friends. */
2422 DECL_IN_AGGR_P (fn_fields) = 0;
2424 /* Note here that a copy ctor is private, so we don't dare generate
2425 a default copy constructor for a class that has a member
2426 of this type without making sure they have access to it. */
2427 if (fn_name == ctor_name)
2429 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2430 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2432 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2433 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2435 if (TREE_CHAIN (parmtypes) == NULL_TREE
2436 || TREE_CHAIN (parmtypes) == void_list_node
2437 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2439 if (TREE_PROTECTED (fn_fields))
2440 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2441 else if (TREE_PRIVATE (fn_fields))
2442 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2446 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2448 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2450 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2452 if (TREE_PROTECTED (fn_fields))
2453 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2454 else if (TREE_PRIVATE (fn_fields))
2455 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2460 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2461 /* We thought there was a destructor, but there wasn't. Some
2462 parse errors cause this anomalous situation. */
2463 TYPE_HAS_DESTRUCTOR (t) = 0;
2465 /* Issue warnings about private constructors and such. If there are
2466 no methods, then some public defaults are generated. */
2467 maybe_warn_about_overly_private_class (t);
2469 /* Now sort the methods. */
2470 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2472 TREE_VEC_LENGTH (method_vec) = len;
2474 /* The type conversion ops have to live at the front of the vec, so we
2476 for (slot = 2; slot < len; ++slot)
2478 tree fn = TREE_VEC_ELT (method_vec, slot);
2480 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2484 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2485 (int (*)(const void *, const void *))method_name_cmp);
2488 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2491 duplicate_tag_error (t)
2494 cp_error ("redefinition of `%#T'", t);
2495 cp_error_at ("previous definition here", t);
2497 /* Pretend we haven't defined this type. */
2499 /* All of the component_decl's were TREE_CHAINed together in the parser.
2500 finish_struct_methods walks these chains and assembles all methods with
2501 the same base name into DECL_CHAINs. Now we don't need the parser chains
2502 anymore, so we unravel them. */
2504 /* This used to be in finish_struct, but it turns out that the
2505 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2507 if (CLASSTYPE_METHOD_VEC (t))
2509 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2510 int i, len = TREE_VEC_LENGTH (method_vec);
2511 for (i = 0; i < len; i++)
2513 tree unchain = TREE_VEC_ELT (method_vec, i);
2514 while (unchain != NULL_TREE)
2516 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2517 unchain = OVL_NEXT (unchain);
2522 if (TYPE_LANG_SPECIFIC (t))
2524 tree binfo = TYPE_BINFO (t);
2525 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2526 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2527 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2528 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2530 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2531 BINFO_BASETYPES(binfo) = NULL_TREE;
2533 TYPE_BINFO (t) = binfo;
2534 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2535 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2536 TYPE_REDEFINED (t) = 1;
2537 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2538 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2540 TYPE_SIZE (t) = NULL_TREE;
2541 TYPE_MODE (t) = VOIDmode;
2542 TYPE_FIELDS (t) = NULL_TREE;
2543 TYPE_METHODS (t) = NULL_TREE;
2544 TYPE_VFIELD (t) = NULL_TREE;
2545 TYPE_CONTEXT (t) = NULL_TREE;
2546 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2549 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2550 vbase and vcall offsets, etc. Set its type and call the backend
2554 layout_vtable_decl (binfo, n)
2561 itype = size_int (n);
2562 atype = build_cplus_array_type (vtable_entry_type,
2563 build_index_type (itype));
2564 layout_type (atype);
2566 /* We may have to grow the vtable. */
2567 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2569 tree vtable = BINFO_VTABLE (binfo);
2571 TREE_TYPE (vtable) = atype;
2572 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = 0;
2573 layout_decl (vtable, 0);
2575 /* At one time the vtable info was grabbed 2 words at a time. This
2576 fails on Sparc unless you have 8-byte alignment. */
2577 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2578 DECL_ALIGN (vtable));
2582 /* Returns the number of virtual function table entries (excluding
2583 RTTI information, vbase and vcall offests, etc.) in the vtable for
2587 num_vfun_entries (binfo)
2590 return list_length (BINFO_VIRTUALS (binfo));
2593 /* Called from num_extra_vtbl_entries via dfs_walk. */
2596 dfs_count_virtuals (binfo, data)
2600 /* Non-primary bases are not interesting; all of the virtual
2601 function table entries have been overridden. */
2602 if (!BINFO_PRIMARY_MARKED_P (binfo))
2603 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2608 /* Returns the number of extra entries (at negative indices) required
2609 for BINFO's vtable. */
2612 num_extra_vtbl_entries (binfo)
2618 type = BINFO_TYPE (binfo);
2621 /* There is an entry for the offset to each virtual base. */
2622 if (vbase_offsets_in_vtable_p ())
2623 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2625 /* If this is a virtual base, there are entries for each virtual
2626 function defined in this class or its bases. */
2627 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2629 vcall_offset_data vod;
2635 dfs_vcall_offset_queue_p,
2637 entries += vod.offsets;
2640 /* When laying out COM-compatible classes, there are no RTTI
2642 if (CLASSTYPE_COM_INTERFACE (type))
2644 /* When using vtable thunks, there are two RTTI entries: the "offset
2645 to top" value and the RTTI entry itself. */
2646 else if (flag_vtable_thunks)
2648 /* When not using vtable thunks there is only a single entry. */
2652 return size_int (entries);
2655 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2656 where the vptr should actually point. */
2659 size_extra_vtbl_entries (binfo)
2662 tree offset = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (vtable_entry_type),
2663 num_extra_vtbl_entries (binfo));
2664 return fold (offset);
2667 /* Construct the initializer for BINFOs virtual function table. BINFO
2668 is part of the hierarchy dominated by T. The value returned is a
2669 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
2670 DECL_INITIAL for a vtable. */
2673 build_vtbl_initializer (binfo, t)
2677 tree v = BINFO_VIRTUALS (binfo);
2678 tree inits = NULL_TREE;
2680 /* Add entries to the vtable that indicate how to adjust the this
2681 pointer when calling a virtual function in this class. */
2682 inits = build_vcall_offset_vtbl_entries (binfo, t);
2684 /* Add entries to the vtable for offsets to our virtual bases. */
2685 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2688 /* Add entries to the vtable for RTTI. */
2689 inits = chainon (build_rtti_vtbl_entries (binfo, t), inits);
2691 /* Go through all the ordinary virtual functions, building up
2701 /* Pull the offset for `this', and the function to call, out of
2703 delta = BV_DELTA (v);
2704 vcall_index = BV_VCALL_INDEX (v);
2706 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2707 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2709 /* You can't call an abstract virtual function; it's abstract.
2710 So, we replace these functions with __pure_virtual. */
2711 if (DECL_PURE_VIRTUAL_P (fn))
2714 /* Take the address of the function, considering it to be of an
2715 appropriate generic type. */
2716 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
2717 /* The address of a function can't change. */
2718 TREE_CONSTANT (pfn) = 1;
2719 /* Enter it in the vtable. */
2720 init = build_vtable_entry (delta, vcall_index, pfn);
2721 /* And add it to the chain of initializers. */
2722 inits = tree_cons (NULL_TREE, init, inits);
2728 /* The initializers were built up in reverse order; straighten them
2730 return nreverse (inits);
2733 /* Initialize the vtable for BINFO with the INITS. */
2736 initialize_vtable (binfo, inits)
2743 layout_vtable_decl (binfo, list_length (inits));
2744 decl = BINFO_VTABLE (binfo);
2745 context = DECL_CONTEXT (decl);
2746 DECL_CONTEXT (decl) = 0;
2747 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
2748 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2749 DECL_CONTEXT (decl) = context;
2752 /* Called from finish_vtbls via dfs_walk. */
2755 dfs_finish_vtbls (binfo, data)
2759 tree t = (tree) data;
2761 if (!BINFO_PRIMARY_MARKED_P (binfo)
2762 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2763 && BINFO_NEW_VTABLE_MARKED (binfo, t))
2764 initialize_vtable (binfo,
2765 build_vtbl_initializer (binfo, t));
2767 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
2768 SET_BINFO_MARKED (binfo);
2773 /* Called from finish_vtbls via dfs_walk when using the new ABI.
2774 Accumulates the vtable initializers for all of the vtables into
2775 TREE_VALUE (DATA). */
2778 dfs_accumulate_vtbl_inits (binfo, data)
2786 t = TREE_PURPOSE (l);
2788 if (!BINFO_PRIMARY_MARKED_P (binfo)
2789 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2790 && BINFO_NEW_VTABLE_MARKED (binfo, t))
2792 /* If this is a secondary vtable, record its location. */
2793 if (binfo != TYPE_BINFO (t))
2797 vtbl = TYPE_BINFO_VTABLE (t);
2798 vtbl = build1 (ADDR_EXPR,
2799 build_pointer_type (TREE_TYPE (vtbl)),
2801 BINFO_VTABLE (binfo)
2802 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
2803 size_binop (MULT_EXPR,
2804 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
2805 size_int (list_length (TREE_VALUE (l)))));
2808 /* Add the initializers for this vtable to the initializers for
2809 the other vtables we've already got. */
2811 = chainon (TREE_VALUE (l),
2812 build_vtbl_initializer (binfo, t));
2815 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
2820 /* Add the vtbl initializers for BINFO (and its non-primary,
2821 non-virtual bases) to the list of INITS. */
2824 accumulate_vtbl_inits (binfo, inits)
2828 /* Walk the BINFO and its bases. */
2829 dfs_walk_real (binfo,
2830 dfs_accumulate_vtbl_inits,
2836 /* Create all the necessary vtables for T and its base classes. */
2842 if (merge_primary_and_secondary_vtables_p ())
2847 /* Under the new ABI, we lay out the primary and secondary
2848 vtables in one contiguous vtable. The primary vtable is
2849 first, followed by the non-virtual secondary vtables in
2850 inheritance graph order. */
2851 list = build_tree_list (t, NULL_TREE);
2852 accumulate_vtbl_inits (TYPE_BINFO (t), list);
2853 /* Then come the virtual bases, also in inheritance graph
2855 for (vbase = CLASSTYPE_VBASECLASSES (t);
2857 vbase = TREE_CHAIN (vbase))
2858 accumulate_vtbl_inits (vbase, list);
2860 if (TYPE_BINFO_VTABLE (t))
2861 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
2865 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2866 dfs_unmarked_real_bases_queue_p, t);
2867 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2868 dfs_marked_real_bases_queue_p, t);
2872 /* True if we should override the given BASE_FNDECL with the given
2876 overrides (fndecl, base_fndecl)
2877 tree fndecl, base_fndecl;
2879 /* Destructors have special names. */
2880 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2881 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2883 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2884 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2886 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2888 tree types, base_types;
2890 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2891 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2893 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2894 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2895 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2896 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2897 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2903 typedef struct find_final_overrider_data_s {
2904 /* The function for which we are trying to find a final overrider. */
2906 /* The base class in which the function was declared. */
2907 tree declaring_base;
2908 /* The most derived class in the hierarchy. */
2909 tree most_derived_type;
2910 /* The final overriding function. */
2912 /* The BINFO for the class in which the final overriding function
2914 tree overriding_base;
2915 } find_final_overrider_data;
2917 /* Called from find_final_overrider via dfs_walk. */
2920 dfs_find_final_overrider (binfo, data)
2924 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2926 if (same_type_p (BINFO_TYPE (binfo),
2927 BINFO_TYPE (ffod->declaring_base))
2928 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2929 BINFO_OFFSET (ffod->declaring_base)))
2934 /* We've found a path to the declaring base. Walk down the path
2935 looking for an overrider for FN. */
2936 for (path = reverse_path (binfo);
2938 path = TREE_CHAIN (path))
2940 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2942 method = TREE_CHAIN (method))
2943 if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2950 /* If we found an overrider, record the overriding function, and
2951 the base from which it came. */
2954 if (ffod->overriding_fn && ffod->overriding_fn != method)
2956 /* We've found a different overrider along a different
2957 path. That can be OK if the new one overrides the
2960 struct S { virtual void f(); };
2961 struct T : public virtual S { virtual void f(); };
2962 struct U : public virtual S, public virtual T {};
2964 Here `T::f' is the final overrider for `S::f'. */
2965 if (strictly_overrides (method, ffod->overriding_fn))
2967 ffod->overriding_fn = method;
2968 ffod->overriding_base = TREE_VALUE (path);
2970 else if (!strictly_overrides (ffod->overriding_fn, method))
2972 cp_error ("no unique final overrider for `%D' in `%T'",
2973 ffod->most_derived_type,
2975 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2976 cp_error (" `%#D'", method);
2977 return error_mark_node;
2980 else if (ffod->overriding_base
2981 && (!tree_int_cst_equal
2982 (BINFO_OFFSET (TREE_VALUE (path)),
2983 BINFO_OFFSET (ffod->overriding_base))))
2985 /* We've found two instances of the same base that
2986 provide overriders. */
2987 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2989 BINFO_TYPE (ffod->overriding_base),
2990 ffod->most_derived_type);
2991 return error_mark_node;
2995 ffod->overriding_fn = method;
2996 ffod->overriding_base = TREE_VALUE (path);
3004 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
3005 FN and whose TREE_VALUE is the binfo for the base where the
3006 overriding occurs. BINFO (in the hierarchy dominated by T) is the
3007 base object in which FN is declared. */
3010 find_final_overrider (t, binfo, fn)
3015 find_final_overrider_data ffod;
3017 /* Getting this right is a little tricky. This is legal:
3019 struct S { virtual void f (); };
3020 struct T { virtual void f (); };
3021 struct U : public S, public T { };
3023 even though calling `f' in `U' is ambiguous. But,
3025 struct R { virtual void f(); };
3026 struct S : virtual public R { virtual void f (); };
3027 struct T : virtual public R { virtual void f (); };
3028 struct U : public S, public T { };
3030 is not -- there's no way to decide whether to put `S::f' or
3031 `T::f' in the vtable for `R'.
3033 The solution is to look at all paths to BINFO. If we find
3034 different overriders along any two, then there is a problem. */
3036 ffod.declaring_base = binfo;
3037 ffod.most_derived_type = t;
3038 ffod.overriding_fn = NULL_TREE;
3039 ffod.overriding_base = NULL_TREE;
3041 if (dfs_walk (TYPE_BINFO (t),
3042 dfs_find_final_overrider,
3045 return error_mark_node;
3047 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
3050 /* Called via dfs_walk. Returns BINFO if BINFO has the same type as
3051 DATA (which is really an _TYPE node). */
3054 dfs_find_base (binfo, data)
3058 return (same_type_p (BINFO_TYPE (binfo), (tree) data)
3059 ? binfo : NULL_TREE);
3062 /* Called from modify_all_vtables via dfs_walk. */
3065 dfs_modify_vtables (binfo, data)
3069 if (/* There's no need to modify the vtable for a primary base;
3070 we're not going to use that vtable anyhow. */
3071 !BINFO_PRIMARY_MARKED_P (binfo)
3072 /* Similarly, a base without a vtable needs no modification. */
3073 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3081 /* If we're supporting RTTI then we always need a new vtable to
3082 point to the RTTI information. Under the new ABI we may need
3083 a new vtable to contain vcall and vbase offsets. */
3084 if (flag_rtti || flag_new_abi)
3085 make_new_vtable (t, binfo);
3087 /* Now, go through each of the virtual functions in the virtual
3088 function table for BINFO. Find the final overrider, and
3089 update the BINFO_VIRTUALS list appropriately. */
3090 for (virtuals = BINFO_VIRTUALS (binfo),
3091 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3093 virtuals = TREE_CHAIN (virtuals),
3094 old_virtuals = TREE_CHAIN (old_virtuals))
3101 HOST_WIDE_INT vindex_val;
3104 /* Find the function which originally caused this vtable
3105 entry to be present. */
3106 fn = BV_FN (old_virtuals);
3107 vindex = DECL_VINDEX (fn);
3108 b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
3109 fn = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (b)));
3110 i = first_vfun_index (BINFO_TYPE (b));
3111 vindex_val = tree_low_cst (vindex, 0);
3112 while (i < vindex_val)
3114 fn = TREE_CHAIN (fn);
3119 /* Handle the case of a virtual function defined in BINFO
3121 overrider = find_final_overrider (t, b, fn);
3122 if (overrider == error_mark_node)
3125 /* The `this' pointer needs to be adjusted from pointing to
3126 BINFO to pointing at the base where the final overrider
3128 delta = size_binop (PLUS_EXPR,
3129 get_derived_offset (binfo,
3130 DECL_VIRTUAL_CONTEXT (fn)),
3131 BINFO_OFFSET (binfo));
3132 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
3134 modify_vtable_entry (t,
3136 TREE_PURPOSE (overrider),
3142 SET_BINFO_MARKED (binfo);
3147 /* Update all of the primary and secondary vtables for T. Create new
3148 vtables as required, and initialize their RTTI information. Each
3149 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3150 function from a base class; find and modify the appropriate entries
3151 to point to the overriding functions. Returns a list, in
3152 declaration order, of the functions that are overridden in this
3153 class, but do not appear in the primary base class vtable, and
3154 which should therefore be appended to the end of the vtable for T. */
3157 modify_all_vtables (t, vfuns_p, overridden_virtuals)
3160 tree overridden_virtuals;
3164 binfo = TYPE_BINFO (t);
3166 /* Update all of the vtables. */
3169 dfs_unmarked_real_bases_queue_p,
3171 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
3173 /* If we should include overriding functions for secondary vtables
3174 in our primary vtable, add them now. */
3175 if (all_overridden_vfuns_in_vtables_p ())
3177 tree *fnsp = &overridden_virtuals;
3181 tree fn = TREE_VALUE (*fnsp);
3183 if (BINFO_VIRTUALS (binfo)
3184 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3186 /* Set the vtable index. */
3187 set_vindex (t, fn, vfuns_p);
3188 /* We don't need to convert to a base class when calling
3190 DECL_VIRTUAL_CONTEXT (fn) = t;
3192 /* We don't need to adjust the `this' pointer when
3193 calling this function. */
3194 BV_DELTA (*fnsp) = integer_zero_node;
3195 BV_VCALL_INDEX (*fnsp) = integer_zero_node;
3197 /* This is an overridden function not already in our
3199 fnsp = &TREE_CHAIN (*fnsp);
3202 /* We've already got an entry for this function. Skip
3204 *fnsp = TREE_CHAIN (*fnsp);
3208 overridden_virtuals = NULL_TREE;
3210 return overridden_virtuals;
3213 /* Here, we already know that they match in every respect.
3214 All we have to check is where they had their declarations. */
3217 strictly_overrides (fndecl1, fndecl2)
3218 tree fndecl1, fndecl2;
3220 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
3221 DECL_CONTEXT (fndecl1),
3223 if (distance == -2 || distance > 0)
3228 /* Get the base virtual function declarations in T that are either
3229 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3230 the overrider/hider. */
3233 get_basefndecls (fndecl, t)
3236 tree methods = TYPE_METHODS (t);
3237 tree base_fndecls = NULL_TREE;
3238 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3239 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3243 if (TREE_CODE (methods) == FUNCTION_DECL
3244 && DECL_VINDEX (methods) != NULL_TREE
3245 && DECL_NAME (fndecl) == DECL_NAME (methods))
3246 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3248 methods = TREE_CHAIN (methods);
3252 return base_fndecls;
3254 for (i = 0; i < n_baseclasses; i++)
3256 tree base_binfo = TREE_VEC_ELT (binfos, i);
3257 tree basetype = BINFO_TYPE (base_binfo);
3259 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3263 return base_fndecls;
3266 /* Mark the functions that have been hidden with their overriders.
3267 Since we start out with all functions already marked with a hider,
3268 no need to mark functions that are just hidden.
3270 Subroutine of warn_hidden. */
3273 mark_overriders (fndecl, base_fndecls)
3274 tree fndecl, base_fndecls;
3276 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3278 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3279 TREE_PURPOSE (base_fndecls) = fndecl;
3283 /* If this declaration supersedes the declaration of
3284 a method declared virtual in the base class, then
3285 mark this field as being virtual as well. */
3288 check_for_override (decl, ctype)
3291 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3292 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3293 int virtualp = DECL_VIRTUAL_P (decl);
3294 int found_overriden_fn = 0;
3296 for (i = 0; i < n_baselinks; i++)
3298 tree base_binfo = TREE_VEC_ELT (binfos, i);
3299 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3301 tree tmp = get_matching_virtual
3303 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3305 if (tmp && !found_overriden_fn)
3307 /* If this function overrides some virtual in some base
3308 class, then the function itself is also necessarily
3309 virtual, even if the user didn't explicitly say so. */
3310 DECL_VIRTUAL_P (decl) = 1;
3312 /* The TMP we really want is the one from the deepest
3313 baseclass on this path, taking care not to
3314 duplicate if we have already found it (via another
3315 path to its virtual baseclass. */
3316 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3318 cp_error_at ("`static %#D' cannot be declared", decl);
3319 cp_error_at (" since `virtual %#D' declared in base class",
3325 /* Set DECL_VINDEX to a value that is neither an
3326 INTEGER_CST nor the error_mark_node so that
3327 add_virtual_function will realize this is an
3328 overridden function. */
3330 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3332 /* We now know that DECL overrides something,
3333 which is all that is important. But, we must
3334 continue to iterate through all the base-classes
3335 in order to allow get_matching_virtual to check for
3336 various illegal overrides. */
3337 found_overriden_fn = 1;
3343 if (DECL_VINDEX (decl) == NULL_TREE)
3344 DECL_VINDEX (decl) = error_mark_node;
3345 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3349 /* Warn about hidden virtual functions that are not overridden in t.
3350 We know that constructors and destructors don't apply. */
3356 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3357 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3360 /* We go through each separately named virtual function. */
3361 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3363 tree fns = TREE_VEC_ELT (method_vec, i);
3364 tree fndecl = NULL_TREE;
3366 tree base_fndecls = NULL_TREE;
3367 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3368 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3370 /* First see if we have any virtual functions in this batch. */
3371 for (; fns; fns = OVL_NEXT (fns))
3373 fndecl = OVL_CURRENT (fns);
3374 if (DECL_VINDEX (fndecl))
3378 if (fns == NULL_TREE)
3381 /* First we get a list of all possible functions that might be
3382 hidden from each base class. */
3383 for (i = 0; i < n_baseclasses; i++)
3385 tree base_binfo = TREE_VEC_ELT (binfos, i);
3386 tree basetype = BINFO_TYPE (base_binfo);
3388 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3392 fns = OVL_NEXT (fns);
3394 /* ...then mark up all the base functions with overriders, preferring
3395 overriders to hiders. */
3397 for (; fns; fns = OVL_NEXT (fns))
3399 fndecl = OVL_CURRENT (fns);
3400 if (DECL_VINDEX (fndecl))
3401 mark_overriders (fndecl, base_fndecls);
3404 /* Now give a warning for all base functions without overriders,
3405 as they are hidden. */
3406 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3408 if (! overrides (TREE_PURPOSE (base_fndecls),
3409 TREE_VALUE (base_fndecls)))
3411 /* Here we know it is a hider, and no overrider exists. */
3412 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3413 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3419 /* Check for things that are invalid. There are probably plenty of other
3420 things we should check for also. */
3423 finish_struct_anon (t)
3428 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3430 if (TREE_STATIC (field))
3432 if (TREE_CODE (field) != FIELD_DECL)
3435 if (DECL_NAME (field) == NULL_TREE
3436 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3438 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3439 for (; elt; elt = TREE_CHAIN (elt))
3441 if (DECL_ARTIFICIAL (elt))
3444 if (DECL_NAME (elt) == constructor_name (t))
3445 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3448 if (TREE_CODE (elt) != FIELD_DECL)
3450 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3455 if (TREE_PRIVATE (elt))
3456 cp_pedwarn_at ("private member `%#D' in anonymous union",
3458 else if (TREE_PROTECTED (elt))
3459 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3462 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3463 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3469 extern int interface_only, interface_unknown;
3471 /* Create default constructors, assignment operators, and so forth for
3472 the type indicated by T, if they are needed.
3473 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3474 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3475 cannot have a default constructor, copy constructor taking a const
3476 reference argument, or an assignment operator, respectively. If a
3477 virtual destructor is created, its DECL is returned; otherwise the
3478 return value is NULL_TREE. */
3481 add_implicitly_declared_members (t, cant_have_default_ctor,
3482 cant_have_const_cctor,
3483 cant_have_assignment)
3485 int cant_have_default_ctor;
3486 int cant_have_const_cctor;
3487 int cant_have_assignment;
3490 tree implicit_fns = NULL_TREE;
3491 tree name = TYPE_IDENTIFIER (t);
3492 tree virtual_dtor = NULL_TREE;
3496 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3498 default_fn = cons_up_default_function (t, name, 0);
3499 check_for_override (default_fn, t);
3501 /* If we couldn't make it work, then pretend we didn't need it. */
3502 if (default_fn == void_type_node)
3503 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3506 TREE_CHAIN (default_fn) = implicit_fns;
3507 implicit_fns = default_fn;
3509 if (DECL_VINDEX (default_fn))
3510 virtual_dtor = default_fn;
3514 /* Any non-implicit destructor is non-trivial. */
3515 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3517 /* Default constructor. */
3518 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3520 default_fn = cons_up_default_function (t, name, 2);
3521 TREE_CHAIN (default_fn) = implicit_fns;
3522 implicit_fns = default_fn;
3525 /* Copy constructor. */
3526 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3528 /* ARM 12.18: You get either X(X&) or X(const X&), but
3530 default_fn = cons_up_default_function (t, name,
3531 3 + cant_have_const_cctor);
3532 TREE_CHAIN (default_fn) = implicit_fns;
3533 implicit_fns = default_fn;
3536 /* Assignment operator. */
3537 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3539 default_fn = cons_up_default_function (t, name,
3540 5 + cant_have_assignment);
3541 TREE_CHAIN (default_fn) = implicit_fns;
3542 implicit_fns = default_fn;
3545 /* Now, hook all of the new functions on to TYPE_METHODS,
3546 and add them to the CLASSTYPE_METHOD_VEC. */
3547 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3548 add_method (t, 0, *f);
3549 *f = TYPE_METHODS (t);
3550 TYPE_METHODS (t) = implicit_fns;
3552 return virtual_dtor;
3555 /* Subroutine of finish_struct_1. Recursively count the number of fields
3556 in TYPE, including anonymous union members. */
3559 count_fields (fields)
3564 for (x = fields; x; x = TREE_CHAIN (x))
3566 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3567 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3574 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3575 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3578 add_fields_to_vec (fields, field_vec, idx)
3579 tree fields, field_vec;
3583 for (x = fields; x; x = TREE_CHAIN (x))
3585 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3586 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3588 TREE_VEC_ELT (field_vec, idx++) = x;
3593 /* FIELD is a bit-field. We are finishing the processing for its
3594 enclosing type. Issue any appropriate messages and set appropriate
3598 check_bitfield_decl (field)
3601 tree type = TREE_TYPE (field);
3604 /* Detect invalid bit-field type. */
3605 if (DECL_INITIAL (field)
3606 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3608 cp_error_at ("bit-field `%#D' with non-integral type", field);
3609 w = error_mark_node;
3612 /* Detect and ignore out of range field width. */
3613 if (DECL_INITIAL (field))
3615 w = DECL_INITIAL (field);
3617 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3620 /* detect invalid field size. */
3621 if (TREE_CODE (w) == CONST_DECL)
3622 w = DECL_INITIAL (w);
3623 else if (TREE_READONLY_DECL_P (w))
3624 w = decl_constant_value (w);
3626 if (TREE_CODE (w) != INTEGER_CST)
3628 cp_error_at ("bit-field `%D' width not an integer constant",
3630 w = error_mark_node;
3632 else if (tree_int_cst_sgn (w) < 0)
3634 cp_error_at ("negative width in bit-field `%D'", field);
3635 w = error_mark_node;
3637 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3639 cp_error_at ("zero width for bit-field `%D'", field);
3640 w = error_mark_node;
3642 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3643 && TREE_CODE (type) != ENUMERAL_TYPE
3644 && TREE_CODE (type) != BOOLEAN_TYPE)
3645 cp_warning_at ("width of `%D' exceeds its type", field);
3646 else if (TREE_CODE (type) == ENUMERAL_TYPE
3647 && (0 > compare_tree_int (w,
3648 min_precision (TYPE_MIN_VALUE (type),
3649 TREE_UNSIGNED (type)))
3650 || 0 > compare_tree_int (w,
3652 (TYPE_MAX_VALUE (type),
3653 TREE_UNSIGNED (type)))))
3654 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3658 /* Remove the bit-field width indicator so that the rest of the
3659 compiler does not treat that value as an initializer. */
3660 DECL_INITIAL (field) = NULL_TREE;
3662 if (w != error_mark_node)
3664 DECL_SIZE (field) = convert (bitsizetype, w);
3665 DECL_BIT_FIELD (field) = 1;
3667 if (integer_zerop (w))
3669 #ifdef EMPTY_FIELD_BOUNDARY
3670 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3671 EMPTY_FIELD_BOUNDARY);
3673 #ifdef PCC_BITFIELD_TYPE_MATTERS
3674 if (PCC_BITFIELD_TYPE_MATTERS)
3675 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3682 /* Non-bit-fields are aligned for their type. */
3683 DECL_BIT_FIELD (field) = 0;
3684 CLEAR_DECL_C_BIT_FIELD (field);
3685 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3689 /* FIELD is a non bit-field. We are finishing the processing for its
3690 enclosing type T. Issue any appropriate messages and set appropriate
3694 check_field_decl (field, t, cant_have_const_ctor,
3695 cant_have_default_ctor, no_const_asn_ref,
3696 any_default_members)
3699 int *cant_have_const_ctor;
3700 int *cant_have_default_ctor;
3701 int *no_const_asn_ref;
3702 int *any_default_members;
3704 tree type = strip_array_types (TREE_TYPE (field));
3706 /* An anonymous union cannot contain any fields which would change
3707 the settings of CANT_HAVE_CONST_CTOR and friends. */
3708 if (ANON_UNION_TYPE_P (type))
3710 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3711 structs. So, we recurse through their fields here. */
3712 else if (ANON_AGGR_TYPE_P (type))
3716 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3717 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3718 check_field_decl (fields, t, cant_have_const_ctor,
3719 cant_have_default_ctor, no_const_asn_ref,
3720 any_default_members);
3722 /* Check members with class type for constructors, destructors,
3724 else if (CLASS_TYPE_P (type))
3726 /* Never let anything with uninheritable virtuals
3727 make it through without complaint. */
3728 abstract_virtuals_error (field, type);
3730 if (TREE_CODE (t) == UNION_TYPE)
3732 if (TYPE_NEEDS_CONSTRUCTING (type))
3733 cp_error_at ("member `%#D' with constructor not allowed in union",
3735 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3736 cp_error_at ("member `%#D' with destructor not allowed in union",
3738 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3739 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3744 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3745 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3746 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3747 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3748 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3751 if (!TYPE_HAS_CONST_INIT_REF (type))
3752 *cant_have_const_ctor = 1;
3754 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3755 *no_const_asn_ref = 1;
3757 if (TYPE_HAS_CONSTRUCTOR (type)
3758 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3759 *cant_have_default_ctor = 1;
3761 if (DECL_INITIAL (field) != NULL_TREE)
3763 /* `build_class_init_list' does not recognize
3765 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3766 cp_error_at ("multiple fields in union `%T' initialized");
3767 *any_default_members = 1;
3770 /* Non-bit-fields are aligned for their type, except packed fields
3771 which require only BITS_PER_UNIT alignment. */
3772 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3773 (DECL_PACKED (field)
3775 : TYPE_ALIGN (TREE_TYPE (field))));
3778 /* Check the data members (both static and non-static), class-scoped
3779 typedefs, etc., appearing in the declaration of T. Issue
3780 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3781 declaration order) of access declarations; each TREE_VALUE in this
3782 list is a USING_DECL.
3784 In addition, set the following flags:
3787 The class is empty, i.e., contains no non-static data members.
3789 CANT_HAVE_DEFAULT_CTOR_P
3790 This class cannot have an implicitly generated default
3793 CANT_HAVE_CONST_CTOR_P
3794 This class cannot have an implicitly generated copy constructor
3795 taking a const reference.
3797 CANT_HAVE_CONST_ASN_REF
3798 This class cannot have an implicitly generated assignment
3799 operator taking a const reference.
3801 All of these flags should be initialized before calling this
3804 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3805 fields can be added by adding to this chain. */
3808 check_field_decls (t, access_decls, empty_p,
3809 cant_have_default_ctor_p, cant_have_const_ctor_p,
3814 int *cant_have_default_ctor_p;
3815 int *cant_have_const_ctor_p;
3816 int *no_const_asn_ref_p;
3821 int any_default_members;
3823 /* First, delete any duplicate fields. */
3824 delete_duplicate_fields (TYPE_FIELDS (t));
3826 /* Assume there are no access declarations. */
3827 *access_decls = NULL_TREE;
3828 /* Assume this class has no pointer members. */
3830 /* Assume none of the members of this class have default
3832 any_default_members = 0;
3834 for (field = &TYPE_FIELDS (t); *field; field = next)
3837 tree type = TREE_TYPE (x);
3839 GNU_xref_member (current_class_name, x);
3841 next = &TREE_CHAIN (x);
3843 if (TREE_CODE (x) == FIELD_DECL)
3845 DECL_PACKED (x) |= TYPE_PACKED (t);
3847 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3848 /* We don't treat zero-width bitfields as making a class
3853 /* The class is non-empty. */
3855 /* The class is not even nearly empty. */
3856 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3860 if (TREE_CODE (x) == USING_DECL)
3862 /* Prune the access declaration from the list of fields. */
3863 *field = TREE_CHAIN (x);
3865 /* Save the access declarations for our caller. */
3866 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3868 /* Since we've reset *FIELD there's no reason to skip to the
3874 if (TREE_CODE (x) == TYPE_DECL
3875 || TREE_CODE (x) == TEMPLATE_DECL)
3878 /* If we've gotten this far, it's a data member, possibly static,
3879 or an enumerator. */
3881 DECL_CONTEXT (x) = t;
3883 /* ``A local class cannot have static data members.'' ARM 9.4 */
3884 if (current_function_decl && TREE_STATIC (x))
3885 cp_error_at ("field `%D' in local class cannot be static", x);
3887 /* Perform error checking that did not get done in
3889 if (TREE_CODE (type) == FUNCTION_TYPE)
3891 cp_error_at ("field `%D' invalidly declared function type",
3893 type = build_pointer_type (type);
3894 TREE_TYPE (x) = type;
3896 else if (TREE_CODE (type) == METHOD_TYPE)
3898 cp_error_at ("field `%D' invalidly declared method type", x);
3899 type = build_pointer_type (type);
3900 TREE_TYPE (x) = type;
3902 else if (TREE_CODE (type) == OFFSET_TYPE)
3904 cp_error_at ("field `%D' invalidly declared offset type", x);
3905 type = build_pointer_type (type);
3906 TREE_TYPE (x) = type;
3909 if (type == error_mark_node)
3912 /* When this goes into scope, it will be a non-local reference. */
3913 DECL_NONLOCAL (x) = 1;
3915 if (TREE_CODE (x) == CONST_DECL)
3918 if (TREE_CODE (x) == VAR_DECL)
3920 if (TREE_CODE (t) == UNION_TYPE)
3921 /* Unions cannot have static members. */
3922 cp_error_at ("field `%D' declared static in union", x);
3927 /* Now it can only be a FIELD_DECL. */
3929 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3930 CLASSTYPE_NON_AGGREGATE (t) = 1;
3932 /* If this is of reference type, check if it needs an init.
3933 Also do a little ANSI jig if necessary. */
3934 if (TREE_CODE (type) == REFERENCE_TYPE)
3936 CLASSTYPE_NON_POD_P (t) = 1;
3937 if (DECL_INITIAL (x) == NULL_TREE)
3938 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3940 /* ARM $12.6.2: [A member initializer list] (or, for an
3941 aggregate, initialization by a brace-enclosed list) is the
3942 only way to initialize nonstatic const and reference
3944 *cant_have_default_ctor_p = 1;
3945 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3947 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3950 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3952 cp_warning_at ("non-static reference in class without a constructor", x);
3956 type = strip_array_types (type);
3958 if (TREE_CODE (type) == POINTER_TYPE)
3961 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3962 CLASSTYPE_HAS_MUTABLE (t) = 1;
3964 if (! pod_type_p (type)
3965 /* For some reason, pointers to members are POD types themselves,
3966 but are not allowed in POD structs. Silly. */
3967 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3968 CLASSTYPE_NON_POD_P (t) = 1;
3970 /* If any field is const, the structure type is pseudo-const. */
3971 if (CP_TYPE_CONST_P (type))
3973 C_TYPE_FIELDS_READONLY (t) = 1;
3974 if (DECL_INITIAL (x) == NULL_TREE)
3975 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3977 /* ARM $12.6.2: [A member initializer list] (or, for an
3978 aggregate, initialization by a brace-enclosed list) is the
3979 only way to initialize nonstatic const and reference
3981 *cant_have_default_ctor_p = 1;
3982 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3984 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3987 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3989 cp_warning_at ("non-static const member in class without a constructor", x);
3992 /* A field that is pseudo-const makes the structure likewise. */
3993 else if (IS_AGGR_TYPE (type))
3995 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3996 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3997 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
4000 /* We set DECL_C_BIT_FIELD in grokbitfield.
4001 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4002 if (DECL_C_BIT_FIELD (x))
4003 check_bitfield_decl (x);
4005 check_field_decl (x, t,
4006 cant_have_const_ctor_p,
4007 cant_have_default_ctor_p,
4009 &any_default_members);
4012 /* Effective C++ rule 11. */
4013 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
4014 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4016 cp_warning ("`%#T' has pointer data members", t);
4018 if (! TYPE_HAS_INIT_REF (t))
4020 cp_warning (" but does not override `%T(const %T&)'", t, t);
4021 if (! TYPE_HAS_ASSIGN_REF (t))
4022 cp_warning (" or `operator=(const %T&)'", t);
4024 else if (! TYPE_HAS_ASSIGN_REF (t))
4025 cp_warning (" but does not override `operator=(const %T&)'", t);
4029 /* Check anonymous struct/anonymous union fields. */
4030 finish_struct_anon (t);
4032 /* We've built up the list of access declarations in reverse order.
4034 *access_decls = nreverse (*access_decls);
4037 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4038 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4039 field are as indicated. The CLASS_TYPE in which this field occurs
4040 is also indicated. FCONTEXT is the type that is needed for the debug
4041 info output routines. *EMPTY_P is set to a non-zero value by this
4042 function to indicate that a class containing this field is
4046 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
4049 tree assembler_name;
4057 /* This class is non-empty. */
4060 /* Build the FIELD_DECL. */
4061 field = build_lang_decl (FIELD_DECL, name, type);
4062 DECL_ASSEMBLER_NAME (field) = assembler_name;
4063 DECL_VIRTUAL_P (field) = 1;
4064 DECL_ARTIFICIAL (field) = 1;
4065 DECL_FIELD_CONTEXT (field) = class_type;
4066 DECL_FCONTEXT (field) = fcontext;
4067 DECL_ALIGN (field) = TYPE_ALIGN (type);
4073 /* Record the type of BINFO in the slot in DATA (which is really a
4074 `varray_type *') corresponding to the BINFO_OFFSET. */
4077 dfs_record_base_offsets (binfo, data)
4082 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
4084 v = (varray_type *) data;
4085 while (VARRAY_SIZE (*v) <= offset)
4086 VARRAY_GROW (*v, 2 * VARRAY_SIZE (*v));
4087 VARRAY_TREE (*v, offset) = tree_cons (NULL_TREE,
4089 VARRAY_TREE (*v, offset));
4094 /* Add the offset of BINFO and its bases to BASE_OFFSETS. */
4097 record_base_offsets (binfo, base_offsets)
4099 varray_type *base_offsets;
4102 dfs_record_base_offsets,
4107 /* Returns non-NULL if there is already an entry in DATA (which is
4108 really a `varray_type') indicating that an object with the same
4109 type of BINFO is already at the BINFO_OFFSET for BINFO. */
4112 dfs_search_base_offsets (binfo, data)
4116 if (is_empty_class (BINFO_TYPE (binfo)))
4118 varray_type v = (varray_type) data;
4119 /* Find the offset for this BINFO. */
4120 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
4123 /* If we haven't yet encountered any objects at offsets that
4124 big, then there's no conflict. */
4125 if (VARRAY_SIZE (v) <= offset)
4127 /* Otherwise, go through the objects already allocated at this
4129 for (t = VARRAY_TREE (v, offset); t; t = TREE_CHAIN (t))
4130 if (same_type_p (TREE_VALUE (t), BINFO_TYPE (binfo)))
4137 /* Returns non-zero if there's a conflict between BINFO and a base
4138 already mentioned in BASE_OFFSETS if BINFO is placed at its current
4142 layout_conflict_p (binfo, base_offsets)
4144 varray_type base_offsets;
4146 return dfs_walk (binfo, dfs_search_base_offsets, dfs_skip_vbases,
4147 base_offsets) != NULL_TREE;
4150 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4151 non-static data member of the type indicated by RLI. BINFO is the
4152 binfo corresponding to the base subobject, or, if this is a
4153 non-static data-member, a dummy BINFO for the type of the data
4154 member. BINFO may be NULL if checks to see if the field overlaps
4155 an existing field with the same type are not required. V maps
4156 offsets to types already located at those offsets. This function
4157 determines the position of the DECL. */
4160 layout_nonempty_base_or_field (rli, decl, binfo, v)
4161 record_layout_info rli;
4166 /* Try to place the field. It may take more than one try if we have
4167 a hard time placing the field without putting two objects of the
4168 same type at the same address. */
4172 struct record_layout_info old_rli = *rli;
4174 /* Place this field. */
4175 place_field (rli, decl);
4177 /* Now that we know where it wil be placed, update its
4179 offset = byte_position (decl);
4181 propagate_binfo_offsets (binfo,
4182 convert (ssizetype, offset));
4184 /* We have to check to see whether or not there is already
4185 something of the same type at the offset we're about to use.
4189 struct T : public S { int i; };
4190 struct U : public S, public T {};
4192 Here, we put S at offset zero in U. Then, we can't put T at
4193 offset zero -- its S component would be at the same address
4194 as the S we already allocated. So, we have to skip ahead.
4195 Since all data members, including those whose type is an
4196 empty class, have non-zero size, any overlap can happen only
4197 with a direct or indirect base-class -- it can't happen with
4199 if (binfo && flag_new_abi && layout_conflict_p (binfo, v))
4201 /* Undo the propogate_binfo_offsets call. */
4202 offset = size_diffop (size_zero_node, offset);
4203 propagate_binfo_offsets (binfo, convert (ssizetype, offset));
4205 /* Strip off the size allocated to this field. That puts us
4206 at the first place we could have put the field with
4207 proper alignment. */
4210 /* Bump up by the alignment required for the type, without
4211 virtual base classes. */
4213 = size_binop (PLUS_EXPR, rli->bitpos,
4214 bitsize_int (CLASSTYPE_ALIGN (BINFO_TYPE (binfo))));
4215 normalize_rli (rli);
4218 /* There was no conflict. We're done laying out this field. */
4223 /* Layout the empty base BINFO. EOC indicates the byte currently just
4224 past the end of the class, and should be correctly aligned for a
4225 class of the type indicated by BINFO; BINFO_OFFSETS gives the
4226 offsets of the other bases allocated so far. */
4229 layout_empty_base (binfo, eoc, binfo_offsets)
4232 varray_type binfo_offsets;
4235 tree basetype = BINFO_TYPE (binfo);
4237 /* This routine should only be used for empty classes. */
4238 my_friendly_assert (is_empty_class (basetype), 20000321);
4239 alignment = ssize_int (CLASSTYPE_ALIGN (basetype));
4241 /* This is an empty base class. We first try to put it at offset
4243 if (layout_conflict_p (binfo, binfo_offsets))
4245 /* That didn't work. Now, we move forward from the next
4246 available spot in the class. */
4247 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4250 if (!layout_conflict_p (binfo, binfo_offsets))
4251 /* We finally found a spot where there's no overlap. */
4254 /* There's overlap here, too. Bump along to the next spot. */
4255 propagate_binfo_offsets (binfo, alignment);
4260 /* Build a FIELD_DECL for the base given by BINFO in the class
4261 *indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
4262 *BASE_ALIGN is a running maximum of the alignments of any base
4266 build_base_field (rli, binfo, empty_p, base_align, v)
4267 record_layout_info rli;
4270 unsigned int *base_align;
4273 tree basetype = BINFO_TYPE (binfo);
4276 if (!COMPLETE_TYPE_P (basetype))
4277 /* This error is now reported in xref_tag, thus giving better
4278 location information. */
4281 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4282 DECL_ARTIFICIAL (decl) = 1;
4283 DECL_FIELD_CONTEXT (decl) = rli->t;
4284 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4285 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4286 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4290 /* Brain damage for backwards compatibility. For no good
4291 reason, the old basetype layout made every base have at least
4292 as large as the alignment for the bases up to that point,
4293 gratuitously wasting space. So we do the same thing here. */
4294 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4296 = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
4297 DECL_SIZE_UNIT (decl)
4298 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
4299 size_int (*base_align / BITS_PER_UNIT));
4302 if (!integer_zerop (DECL_SIZE (decl)))
4304 /* The containing class is non-empty because it has a non-empty
4308 /* Try to place the field. It may take more than one try if we
4309 have a hard time placing the field without putting two
4310 objects of the same type at the same address. */
4311 layout_nonempty_base_or_field (rli, decl, binfo, *v);
4315 unsigned HOST_WIDE_INT eoc;
4317 /* On some platforms (ARM), even empty classes will not be
4319 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
4320 eoc = CEIL (eoc, DECL_ALIGN (decl)) * DECL_ALIGN (decl);
4321 layout_empty_base (binfo, size_int (eoc), *v);
4324 /* Check for inaccessible base classes. If the same base class
4325 appears more than once in the hierarchy, but isn't virtual, then
4327 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
4328 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4331 /* Record the offsets of BINFO and its base subobjects. */
4332 record_base_offsets (binfo, v);
4335 /* Layout all of the non-virtual base classes. Returns a map from
4336 offsets to types present at those offsets. */
4339 build_base_fields (rli, empty_p)
4340 record_layout_info rli;
4343 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4346 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4349 unsigned int base_align = 0;
4351 /* Create the table mapping offsets to empty base classes. */
4352 VARRAY_TREE_INIT (v, 32, "v");
4354 /* Under the new ABI, the primary base class is always allocated
4356 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4357 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4358 empty_p, &base_align, &v);
4360 /* Now allocate the rest of the bases. */
4361 for (i = 0; i < n_baseclasses; ++i)
4365 /* Under the new ABI, the primary base was already allocated
4366 above, so we don't need to allocate it again here. */
4367 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4370 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4372 /* A primary virtual base class is allocated just like any other
4373 base class, but a non-primary virtual base is allocated
4374 later, in layout_virtual_bases. */
4375 if (TREE_VIA_VIRTUAL (base_binfo)
4376 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4379 build_base_field (rli, base_binfo, empty_p, &base_align, &v);
4385 /* Go through the TYPE_METHODS of T issuing any appropriate
4386 diagnostics, figuring out which methods override which other
4387 methods, and so forth. */
4394 int seen_one_arg_array_delete_p = 0;
4396 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4398 GNU_xref_member (current_class_name, x);
4400 /* If this was an evil function, don't keep it in class. */
4401 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4404 check_for_override (x, t);
4405 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4406 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4408 /* The name of the field is the original field name
4409 Save this in auxiliary field for later overloading. */
4410 if (DECL_VINDEX (x))
4412 TYPE_POLYMORPHIC_P (t) = 1;
4413 if (DECL_PURE_VIRTUAL_P (x))
4414 CLASSTYPE_PURE_VIRTUALS (t)
4415 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4418 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4422 /* When dynamically allocating an array of this type, we
4423 need a "cookie" to record how many elements we allocated,
4424 even if the array elements have no non-trivial
4425 destructor, if the usual array deallocation function
4426 takes a second argument of type size_t. The standard (in
4427 [class.free]) requires that the second argument be set
4429 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4430 /* This is overly conservative, but we must maintain this
4431 behavior for backwards compatibility. */
4432 if (!flag_new_abi && second_parm != void_list_node)
4433 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4434 /* Under the new ABI, we choose only those function that are
4435 explicitly declared as `operator delete[] (void *,
4437 else if (flag_new_abi
4438 && !seen_one_arg_array_delete_p
4440 && TREE_CHAIN (second_parm) == void_list_node
4441 && same_type_p (TREE_VALUE (second_parm), sizetype))
4442 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4443 /* If there's no second parameter, then this is the usual
4444 deallocation function. */
4445 else if (second_parm == void_list_node)
4446 seen_one_arg_array_delete_p = 1;
4451 /* Remove all zero-width bit-fields from T. */
4454 remove_zero_width_bit_fields (t)
4459 fieldsp = &TYPE_FIELDS (t);
4462 if (TREE_CODE (*fieldsp) == FIELD_DECL
4463 && DECL_C_BIT_FIELD (*fieldsp)
4464 && DECL_INITIAL (*fieldsp))
4465 *fieldsp = TREE_CHAIN (*fieldsp);
4467 fieldsp = &TREE_CHAIN (*fieldsp);
4471 /* Check the validity of the bases and members declared in T. Add any
4472 implicitly-generated functions (like copy-constructors and
4473 assignment operators). Compute various flag bits (like
4474 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4475 level: i.e., independently of the ABI in use. */
4478 check_bases_and_members (t, empty_p)
4482 /* Nonzero if we are not allowed to generate a default constructor
4484 int cant_have_default_ctor;
4485 /* Nonzero if the implicitly generated copy constructor should take
4486 a non-const reference argument. */
4487 int cant_have_const_ctor;
4488 /* Nonzero if the the implicitly generated assignment operator
4489 should take a non-const reference argument. */
4490 int no_const_asn_ref;
4493 /* By default, we use const reference arguments and generate default
4495 cant_have_default_ctor = 0;
4496 cant_have_const_ctor = 0;
4497 no_const_asn_ref = 0;
4499 /* Assume that the class is nearly empty; we'll clear this flag if
4500 it turns out not to be nearly empty. */
4501 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4503 /* Check all the base-classes. */
4504 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4507 /* Check all the data member declarations. */
4508 check_field_decls (t, &access_decls, empty_p,
4509 &cant_have_default_ctor,
4510 &cant_have_const_ctor,
4513 /* Check all the method declarations. */
4516 /* A nearly-empty class has to be vptr-containing; a nearly empty
4517 class contains just a vptr. */
4518 if (!TYPE_CONTAINS_VPTR_P (t))
4519 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4521 /* Do some bookkeeping that will guide the generation of implicitly
4522 declared member functions. */
4523 TYPE_HAS_COMPLEX_INIT_REF (t)
4524 |= (TYPE_HAS_INIT_REF (t)
4525 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4526 || TYPE_POLYMORPHIC_P (t));
4527 TYPE_NEEDS_CONSTRUCTING (t)
4528 |= (TYPE_HAS_CONSTRUCTOR (t)
4529 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4530 || TYPE_POLYMORPHIC_P (t));
4531 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4532 || TYPE_POLYMORPHIC_P (t));
4533 CLASSTYPE_NON_POD_P (t)
4534 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4535 || TYPE_HAS_ASSIGN_REF (t));
4536 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4537 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4538 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4540 /* Synthesize any needed methods. Note that methods will be synthesized
4541 for anonymous unions; grok_x_components undoes that. */
4542 add_implicitly_declared_members (t, cant_have_default_ctor,
4543 cant_have_const_ctor,
4546 /* Process the using-declarations. */
4547 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4548 handle_using_decl (TREE_VALUE (access_decls), t);
4550 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4551 finish_struct_methods (t);
4554 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4555 accordingly. If a new vfield was created (because T doesn't have a
4556 primary base class), then the newly created field is returned. It
4557 is not added to the TYPE_FIELDS list; it is the callers
4558 responsibility to do that. */
4561 create_vtable_ptr (t, empty_p, vfuns_p,
4562 new_virtuals_p, overridden_virtuals_p)
4566 tree *new_virtuals_p;
4567 tree *overridden_virtuals_p;
4571 /* Loop over the virtual functions, adding them to our various
4573 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4574 if (DECL_VINDEX (fn))
4575 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4578 /* If we couldn't find an appropriate base class, create a new field
4579 here. Even if there weren't any new virtual functions, we might need a
4580 new virtual function table if we're supposed to include vptrs in
4581 all classes that need them. */
4582 if (!TYPE_VFIELD (t)
4584 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4586 /* We build this decl with vtbl_ptr_type_node, which is a
4587 `vtable_entry_type*'. It might seem more precise to use
4588 `vtable_entry_type (*)[N]' where N is the number of firtual
4589 functions. However, that would require the vtable pointer in
4590 base classes to have a different type than the vtable pointer
4591 in derived classes. We could make that happen, but that
4592 still wouldn't solve all the problems. In particular, the
4593 type-based alias analysis code would decide that assignments
4594 to the base class vtable pointer can't alias assignments to
4595 the derived class vtable pointer, since they have different
4596 types. Thus, in an derived class destructor, where the base
4597 class constructor was inlined, we could generate bad code for
4598 setting up the vtable pointer.
4600 Therefore, we use one type for all vtable pointers. We still
4601 use a type-correct type; it's just doesn't indicate the array
4602 bounds. That's better than using `void*' or some such; it's
4603 cleaner, and it let's the alias analysis code know that these
4604 stores cannot alias stores to void*! */
4606 = build_vtbl_or_vbase_field (get_vfield_name (t),
4607 get_identifier (VFIELD_BASE),
4613 if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4614 /* If there were any baseclasses, they can't possibly be at
4615 offset zero any more, because that's where the vtable
4616 pointer is. So, converting to a base class is going to
4618 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4620 return TYPE_VFIELD (t);
4626 /* Fixup the inline function given by INFO now that the class is
4630 fixup_pending_inline (info)
4631 struct pending_inline *info;
4636 tree fn = info->fndecl;
4638 args = DECL_ARGUMENTS (fn);
4641 DECL_CONTEXT (args) = fn;
4642 args = TREE_CHAIN (args);
4647 /* Fixup the inline methods and friends in TYPE now that TYPE is
4651 fixup_inline_methods (type)
4654 tree method = TYPE_METHODS (type);
4656 if (method && TREE_CODE (method) == TREE_VEC)
4658 if (TREE_VEC_ELT (method, 1))
4659 method = TREE_VEC_ELT (method, 1);
4660 else if (TREE_VEC_ELT (method, 0))
4661 method = TREE_VEC_ELT (method, 0);
4663 method = TREE_VEC_ELT (method, 2);
4666 /* Do inline member functions. */
4667 for (; method; method = TREE_CHAIN (method))
4668 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4671 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4673 method = TREE_CHAIN (method))
4674 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4675 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4678 /* Called from propagate_binfo_offsets via dfs_walk. */
4681 dfs_propagate_binfo_offsets (binfo, data)
4685 tree offset = (tree) data;
4687 /* Update the BINFO_OFFSET for this base. Allow for the case where it
4688 might be negative. */
4689 BINFO_OFFSET (binfo)
4690 = convert (sizetype, size_binop (PLUS_EXPR,
4691 convert (ssizetype, BINFO_OFFSET (binfo)),
4693 SET_BINFO_MARKED (binfo);
4698 /* Add OFFSET to all base types of BINFO which is a base in the
4699 hierarchy dominated by T.
4701 OFFSET, which is a type offset, is number of bytes.
4703 Note that we don't have to worry about having two paths to the
4704 same base type, since this type owns its association list. */
4707 propagate_binfo_offsets (binfo, offset)
4712 dfs_propagate_binfo_offsets,
4713 dfs_skip_nonprimary_vbases_unmarkedp,
4717 dfs_skip_nonprimary_vbases_markedp,
4721 /* Called via dfs_walk from layout_virtual bases. */
4724 dfs_set_offset_for_shared_vbases (binfo, data)
4728 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4730 /* Update the shared copy. */
4733 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4734 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4740 /* Called via dfs_walk from layout_virtual bases. */
4743 dfs_set_offset_for_unshared_vbases (binfo, data)
4747 /* If this is a virtual base, make sure it has the same offset as
4748 the shared copy. If it's a primary base, then we know it's
4750 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4752 tree t = (tree) data;
4756 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4757 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4758 propagate_binfo_offsets (binfo, offset);
4764 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4765 TYPE_ALIGN and TYPE_SIZE for T. BASE_OFFSETS is a varray mapping
4766 offsets to the types at those offsets. */
4769 layout_virtual_bases (t, base_offsets)
4771 varray_type *base_offsets;
4774 unsigned HOST_WIDE_INT dsize;
4775 unsigned HOST_WIDE_INT eoc;
4777 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4780 #ifdef STRUCTURE_SIZE_BOUNDARY
4781 /* Packed structures don't need to have minimum size. */
4782 if (! TYPE_PACKED (t))
4783 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4786 /* DSIZE is the size of the class without the virtual bases. */
4787 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4789 /* Make every class have alignment of at least one. */
4790 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4792 /* Go through the virtual bases, allocating space for each virtual
4793 base that is not already a primary base class. */
4794 for (vbase = CLASSTYPE_VBASECLASSES (t);
4796 vbase = TREE_CHAIN (vbase))
4797 if (!BINFO_VBASE_PRIMARY_P (vbase))
4799 /* This virtual base is not a primary base of any class in the
4800 hierarchy, so we have to add space for it. */
4802 unsigned int desired_align;
4804 basetype = BINFO_TYPE (vbase);
4807 desired_align = CLASSTYPE_ALIGN (basetype);
4809 /* Under the old ABI, virtual bases were aligned as for the
4810 entire base object (including its virtual bases). That's
4811 wasteful, in general. */
4812 desired_align = TYPE_ALIGN (basetype);
4813 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4815 /* Add padding so that we can put the virtual base class at an
4816 appropriately aligned offset. */
4817 dsize = CEIL (dsize, desired_align) * desired_align;
4819 /* Under the new ABI, we try to squish empty virtual bases in
4820 just like ordinary empty bases. */
4821 if (flag_new_abi && is_empty_class (basetype))
4822 layout_empty_base (vbase,
4823 size_int (CEIL (dsize, BITS_PER_UNIT)),
4827 /* And compute the offset of the virtual base. */
4828 propagate_binfo_offsets (vbase,
4829 ssize_int (CEIL (dsize, BITS_PER_UNIT)));
4830 /* Every virtual baseclass takes a least a UNIT, so that
4831 we can take it's address and get something different
4833 dsize += MAX (BITS_PER_UNIT,
4834 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4837 /* Keep track of the offsets assigned to this virtual base. */
4838 record_base_offsets (vbase, base_offsets);
4841 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4842 BINFO_OFFSET set correctly. Those we just allocated certainly
4843 will. The others are primary baseclasses; we walk the hierarchy
4844 to find the primary copies and update the shared copy. */
4845 dfs_walk (TYPE_BINFO (t),
4846 dfs_set_offset_for_shared_vbases,
4847 dfs_unmarked_real_bases_queue_p,
4850 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4851 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4852 bases and their direct and indirect bases. The ambiguity checks
4853 in get_base_distance depend on the BINFO_OFFSETs being set
4855 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4856 for (vbase = CLASSTYPE_VBASECLASSES (t);
4858 vbase = TREE_CHAIN (vbase))
4859 dfs_walk (vbase, dfs_set_offset_for_unshared_vbases, NULL, t);
4861 /* If we had empty base classes that protruded beyond the end of the
4862 class, we didn't update DSIZE above; we were hoping to overlay
4863 multiple such bases at the same location. */
4864 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4865 if (eoc * BITS_PER_UNIT > dsize)
4866 dsize = (eoc + 1) * BITS_PER_UNIT;
4868 /* Now, make sure that the total size of the type is a multiple of
4870 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4871 TYPE_SIZE (t) = bitsize_int (dsize);
4872 TYPE_SIZE_UNIT (t) = convert (sizetype,
4873 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4874 bitsize_unit_node));
4876 /* Check for ambiguous virtual bases. */
4878 for (vbase = CLASSTYPE_VBASECLASSES (t);
4880 vbase = TREE_CHAIN (vbase))
4882 tree basetype = BINFO_TYPE (vbase);
4883 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4884 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4889 /* Returns the offset of the byte just past the end of the base class
4890 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4891 only non-virtual bases are included. */
4893 static unsigned HOST_WIDE_INT
4894 end_of_class (t, include_virtuals_p)
4896 int include_virtuals_p;
4898 unsigned HOST_WIDE_INT result = 0;
4901 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4905 unsigned HOST_WIDE_INT end_of_base;
4907 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4909 if (!include_virtuals_p
4910 && TREE_VIA_VIRTUAL (base_binfo)
4911 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4914 offset = size_binop (PLUS_EXPR,
4915 BINFO_OFFSET (base_binfo),
4916 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4917 end_of_base = tree_low_cst (offset, /*pos=*/1);
4918 if (end_of_base > result)
4919 result = end_of_base;
4925 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4926 BINFO_OFFSETs for all of the base-classes. Position the vtable
4930 layout_class_type (t, empty_p, vfuns_p,
4931 new_virtuals_p, overridden_virtuals_p)
4935 tree *new_virtuals_p;
4936 tree *overridden_virtuals_p;
4938 tree non_static_data_members;
4941 record_layout_info rli;
4943 unsigned HOST_WIDE_INT eoc;
4945 /* Keep track of the first non-static data member. */
4946 non_static_data_members = TYPE_FIELDS (t);
4948 /* Start laying out the record. */
4949 rli = start_record_layout (t);
4951 /* If possible, we reuse the virtual function table pointer from one
4952 of our base classes. */
4953 determine_primary_base (t, vfuns_p);
4955 /* Create a pointer to our virtual function table. */
4956 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4957 new_virtuals_p, overridden_virtuals_p);
4959 /* Under the new ABI, the vptr is always the first thing in the
4961 if (flag_new_abi && vptr)
4963 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4964 place_field (rli, vptr);
4967 /* Add pointers to all of our virtual base-classes. */
4968 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
4970 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4971 v = build_base_fields (rli, empty_p);
4973 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4974 we have to save this before we start modifying
4975 TYPE_NONCOPIED_PARTS. */
4976 fixup_inline_methods (t);
4978 /* Layout the non-static data members. */
4979 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4985 /* We still pass things that aren't non-static data members to
4986 the back-end, in case it wants to do something with them. */
4987 if (TREE_CODE (field) != FIELD_DECL)
4989 place_field (rli, field);
4993 type = TREE_TYPE (field);
4995 /* If this field is a bit-field whose width is greater than its
4996 type, then there are some special rules for allocating it
4997 under the new ABI. Under the old ABI, there were no special
4998 rules, but the back-end can't handle bitfields longer than a
4999 `long long', so we use the same mechanism. */
5000 if (DECL_C_BIT_FIELD (field)
5002 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5004 && 0 < compare_tree_int (DECL_SIZE (field),
5006 (long_long_unsigned_type_node)))))
5008 integer_type_kind itk;
5011 /* We must allocate the bits as if suitably aligned for the
5012 longest integer type that fits in this many bits. type
5013 of the field. Then, we are supposed to use the left over
5014 bits as additional padding. */
5015 for (itk = itk_char; itk != itk_none; ++itk)
5016 if (INT_CST_LT (DECL_SIZE (field),
5017 TYPE_SIZE (integer_types[itk])))
5020 /* ITK now indicates a type that is too large for the
5021 field. We have to back up by one to find the largest
5023 integer_type = integer_types[itk - 1];
5024 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5025 TYPE_SIZE (integer_type));
5026 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5027 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5030 padding = NULL_TREE;
5032 /* Create a dummy BINFO corresponding to this field. */
5033 binfo = make_binfo (size_zero_node, type, NULL_TREE, NULL_TREE);
5034 unshare_base_binfos (binfo);
5035 layout_nonempty_base_or_field (rli, field, binfo, v);
5037 /* If we needed additional padding after this field, add it
5043 padding_field = build_decl (FIELD_DECL,
5046 DECL_BIT_FIELD (padding_field) = 1;
5047 DECL_SIZE (padding_field) = padding;
5048 DECL_ALIGN (padding_field) = 1;
5049 layout_nonempty_base_or_field (rli, padding_field, NULL_TREE, v);
5053 /* It might be the case that we grew the class to allocate a
5054 zero-sized base class. That won't be reflected in RLI, yet,
5055 because we are willing to overlay multiple bases at the same
5056 offset. However, now we need to make sure that RLI is big enough
5057 to reflect the entire class. */
5058 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5059 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
5060 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
5062 /* We don't handle zero-sized base classes specially under the
5063 old ABI, so if we get here, we had better be operating under
5064 the new ABI rules. */
5065 my_friendly_assert (flag_new_abi, 20000321);
5066 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
5067 rli->bitpos = bitsize_zero_node;
5070 /* We make all structures have at least one element, so that they
5071 have non-zero size. In the new ABI, the class may be empty even
5072 if it has basetypes. Therefore, we add the fake field after all
5073 the other fields; if there are already FIELD_DECLs on the list,
5074 their offsets will not be disturbed. */
5079 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
5080 place_field (rli, padding);
5081 TYPE_NONCOPIED_PARTS (t)
5082 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
5083 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
5086 /* Under the old ABI, the vptr comes at the very end of the
5088 if (!flag_new_abi && vptr)
5090 place_field (rli, vptr);
5091 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
5094 /* Let the back-end lay out the type. Note that at this point we
5095 have only included non-virtual base-classes; we will lay out the
5096 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5097 this call are not necessarily correct; they are just the size and
5098 alignment when no virtual base clases are used. */
5099 finish_record_layout (rli);
5101 /* Delete all zero-width bit-fields from the list of fields. Now
5102 that the type is laid out they are no longer important. */
5103 remove_zero_width_bit_fields (t);
5105 /* Remember the size and alignment of the class before adding
5106 the virtual bases. */
5107 if (*empty_p && flag_new_abi)
5109 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5110 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5112 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
5113 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
5115 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5116 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5120 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
5121 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
5124 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5126 /* Set the TYPE_DECL for this type to contain the right
5127 value for DECL_OFFSET, so that we can use it as part
5128 of a COMPONENT_REF for multiple inheritance. */
5129 layout_decl (TYPE_MAIN_DECL (t), 0);
5131 /* Now fix up any virtual base class types that we left lying
5132 around. We must get these done before we try to lay out the
5133 virtual function table. As a side-effect, this will remove the
5134 base subobject fields. */
5135 layout_virtual_bases (t, &v);
5141 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5142 (or C++ class declaration).
5144 For C++, we must handle the building of derived classes.
5145 Also, C++ allows static class members. The way that this is
5146 handled is to keep the field name where it is (as the DECL_NAME
5147 of the field), and place the overloaded decl in the bit position
5148 of the field. layout_record and layout_union will know about this.
5150 More C++ hair: inline functions have text in their
5151 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5152 meaningful tree structure. After the struct has been laid out, set
5153 things up so that this can happen.
5155 And still more: virtual functions. In the case of single inheritance,
5156 when a new virtual function is seen which redefines a virtual function
5157 from the base class, the new virtual function is placed into
5158 the virtual function table at exactly the same address that
5159 it had in the base class. When this is extended to multiple
5160 inheritance, the same thing happens, except that multiple virtual
5161 function tables must be maintained. The first virtual function
5162 table is treated in exactly the same way as in the case of single
5163 inheritance. Additional virtual function tables have different
5164 DELTAs, which tell how to adjust `this' to point to the right thing.
5166 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5174 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5175 a FUNCTION_DECL. Each of these functions is a virtual function
5176 declared in T that does not override any virtual function from a
5178 tree new_virtuals = NULL_TREE;
5179 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5180 except that each declaration here overrides the declaration from
5182 tree overridden_virtuals = NULL_TREE;
5187 if (COMPLETE_TYPE_P (t))
5189 if (IS_AGGR_TYPE (t))
5190 cp_error ("redefinition of `%#T'", t);
5192 my_friendly_abort (172);
5197 GNU_xref_decl (current_function_decl, t);
5199 /* If this type was previously laid out as a forward reference,
5200 make sure we lay it out again. */
5201 TYPE_SIZE (t) = NULL_TREE;
5202 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5203 CLASSTYPE_VFIELD_PARENT (t) = -1;
5205 CLASSTYPE_RTTI (t) = NULL_TREE;
5207 /* Do end-of-class semantic processing: checking the validity of the
5208 bases and members and add implicitly generated methods. */
5209 check_bases_and_members (t, &empty);
5211 /* Layout the class itself. */
5212 layout_class_type (t, &empty, &vfuns,
5213 &new_virtuals, &overridden_virtuals);
5215 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5216 might need to know it for setting up the offsets in the vtable
5217 (or in thunks) below. */
5218 vfield = TYPE_VFIELD (t);
5219 if (vfield != NULL_TREE
5220 && DECL_FIELD_CONTEXT (vfield) != t)
5222 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5224 vfield = copy_node (vfield);
5225 copy_lang_decl (vfield);
5227 DECL_FIELD_CONTEXT (vfield) = t;
5228 DECL_FIELD_OFFSET (vfield)
5229 = size_binop (PLUS_EXPR,
5230 BINFO_OFFSET (binfo),
5231 DECL_FIELD_OFFSET (vfield));
5232 TYPE_VFIELD (t) = vfield;
5236 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5238 /* If necessary, create the primary vtable for this class. */
5240 || overridden_virtuals
5241 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5243 new_virtuals = nreverse (new_virtuals);
5244 /* We must enter these virtuals into the table. */
5245 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5246 build_primary_vtable (NULL_TREE, t);
5247 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5248 /* Here we know enough to change the type of our virtual
5249 function table, but we will wait until later this function. */
5250 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5252 /* If this type has basetypes with constructors, then those
5253 constructors might clobber the virtual function table. But
5254 they don't if the derived class shares the exact vtable of the base
5257 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5259 /* If we didn't need a new vtable, see if we should copy one from
5261 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5263 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5265 /* This class contributes nothing new to the virtual function
5266 table. However, it may have declared functions which
5267 went into the virtual function table "inherited" from the
5268 base class. If so, we grab a copy of those updated functions,
5269 and pretend they are ours. */
5271 /* See if we should steal the virtual info from base class. */
5272 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5273 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5274 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5275 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5276 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5277 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5280 if (TYPE_CONTAINS_VPTR_P (t))
5282 if (TYPE_BINFO_VTABLE (t))
5283 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5285 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5286 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5289 CLASSTYPE_VSIZE (t) = vfuns;
5290 /* Entries for virtual functions defined in the primary base are
5291 followed by entries for new functions unique to this class. */
5292 TYPE_BINFO_VIRTUALS (t)
5293 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5294 /* Finally, add entries for functions that override virtuals
5295 from non-primary bases. */
5296 TYPE_BINFO_VIRTUALS (t)
5297 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5300 /* If we created a new vtbl pointer for this class, add it to the
5302 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5303 CLASSTYPE_VFIELDS (t)
5304 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5306 finish_struct_bits (t);
5308 /* Complete the rtl for any static member objects of the type we're
5310 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5312 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5313 && TREE_TYPE (x) == t)
5315 DECL_MODE (x) = TYPE_MODE (t);
5316 make_decl_rtl (x, NULL, 0);
5320 /* Done with FIELDS...now decide whether to sort these for
5321 faster lookups later.
5323 The C front-end only does this when n_fields > 15. We use
5324 a smaller number because most searches fail (succeeding
5325 ultimately as the search bores through the inheritance
5326 hierarchy), and we want this failure to occur quickly. */
5328 n_fields = count_fields (TYPE_FIELDS (t));
5331 tree field_vec = make_tree_vec (n_fields);
5332 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5333 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5334 (int (*)(const void *, const void *))field_decl_cmp);
5335 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5336 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5337 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5340 if (TYPE_HAS_CONSTRUCTOR (t))
5342 tree vfields = CLASSTYPE_VFIELDS (t);
5346 /* Mark the fact that constructor for T
5347 could affect anybody inheriting from T
5348 who wants to initialize vtables for VFIELDS's type. */
5349 if (VF_DERIVED_VALUE (vfields))
5350 TREE_ADDRESSABLE (vfields) = 1;
5351 vfields = TREE_CHAIN (vfields);
5355 /* Make the rtl for any new vtables we have created, and unmark
5356 the base types we marked. */
5359 if (CLASSTYPE_VSIZE (t) != 0)
5361 /* In addition to this one, all the other vfields should be listed. */
5362 /* Before that can be done, we have to have FIELD_DECLs for them, and
5363 a place to find them. */
5364 TYPE_NONCOPIED_PARTS (t)
5365 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5366 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5368 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5369 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5370 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5374 hack_incomplete_structures (t);
5376 if (warn_overloaded_virtual)
5379 maybe_suppress_debug_info (t);
5381 /* Finish debugging output for this type. */
5382 rest_of_type_compilation (t, toplevel_bindings_p ());
5385 /* When T was built up, the member declarations were added in reverse
5386 order. Rearrange them to declaration order. */
5389 unreverse_member_declarations (t)
5396 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5397 reverse order. Put them in declaration order now. */
5398 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5399 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5401 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5402 reverse order, so we can't just use nreverse. */
5404 for (x = TYPE_FIELDS (t);
5405 x && TREE_CODE (x) != TYPE_DECL;
5408 next = TREE_CHAIN (x);
5409 TREE_CHAIN (x) = prev;
5414 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5416 TYPE_FIELDS (t) = prev;
5421 finish_struct (t, attributes)
5424 /* Now that we've got all the field declarations, reverse everything
5426 unreverse_member_declarations (t);
5428 cplus_decl_attributes (t, attributes, NULL_TREE);
5430 if (processing_template_decl)
5432 finish_struct_methods (t);
5433 TYPE_SIZE (t) = bitsize_zero_node;
5436 finish_struct_1 (t);
5438 TYPE_BEING_DEFINED (t) = 0;
5440 if (current_class_type)
5443 error ("trying to finish struct, but kicked out due to previous parse errors.");
5445 if (processing_template_decl)
5447 tree scope = current_scope ();
5448 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5449 add_tree (build_min (TAG_DEFN, t));
5455 /* Return the dynamic type of INSTANCE, if known.
5456 Used to determine whether the virtual function table is needed
5459 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5460 of our knowledge of its type. *NONNULL should be initialized
5461 before this function is called. */
5464 fixed_type_or_null (instance, nonnull)
5468 switch (TREE_CODE (instance))
5471 /* Check that we are not going through a cast of some sort. */
5472 if (TREE_TYPE (instance)
5473 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5474 instance = TREE_OPERAND (instance, 0);
5475 /* fall through... */
5477 /* This is a call to a constructor, hence it's never zero. */
5478 if (TREE_HAS_CONSTRUCTOR (instance))
5482 return TREE_TYPE (instance);
5487 /* This is a call to a constructor, hence it's never zero. */
5488 if (TREE_HAS_CONSTRUCTOR (instance))
5492 return TREE_TYPE (instance);
5494 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5501 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5502 /* Propagate nonnull. */
5503 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5504 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5505 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5510 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5515 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5518 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5522 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5523 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5527 return TREE_TYPE (TREE_TYPE (instance));
5529 /* fall through... */
5532 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5536 return TREE_TYPE (instance);
5540 if (instance == current_class_ptr
5541 && flag_this_is_variable <= 0)
5543 /* Normally, 'this' must be non-null. */
5544 if (flag_this_is_variable == 0)
5547 /* <0 means we're in a constructor and we know our type. */
5548 if (flag_this_is_variable < 0)
5549 return TREE_TYPE (TREE_TYPE (instance));
5551 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5552 /* Reference variables should be references to objects. */
5562 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5563 to the static type. We also handle the case where INSTANCE is really
5566 Used to determine whether the virtual function table is needed
5569 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5570 of our knowledge of its type. *NONNULL should be initialized
5571 before this function is called. */
5574 resolves_to_fixed_type_p (instance, nonnull)
5578 tree t = TREE_TYPE (instance);
5579 tree fixed = fixed_type_or_null (instance, nonnull);
5580 if (fixed == NULL_TREE)
5582 if (POINTER_TYPE_P (t))
5584 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5589 init_class_processing ()
5591 current_class_depth = 0;
5592 current_class_stack_size = 10;
5594 = (class_stack_node_t) xmalloc (current_class_stack_size
5595 * sizeof (struct class_stack_node));
5597 access_default_node = build_int_2 (0, 0);
5598 access_public_node = build_int_2 (1, 0);
5599 access_protected_node = build_int_2 (2, 0);
5600 access_private_node = build_int_2 (3, 0);
5601 access_default_virtual_node = build_int_2 (4, 0);
5602 access_public_virtual_node = build_int_2 (5, 0);
5603 access_protected_virtual_node = build_int_2 (6, 0);
5604 access_private_virtual_node = build_int_2 (7, 0);
5607 /* Set current scope to NAME. CODE tells us if this is a
5608 STRUCT, UNION, or ENUM environment.
5610 NAME may end up being NULL_TREE if this is an anonymous or
5611 late-bound struct (as in "struct { ... } foo;") */
5613 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5614 appropriate values, found by looking up the type definition of
5617 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5618 which can be seen locally to the class. They are shadowed by
5619 any subsequent local declaration (including parameter names).
5621 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5622 which have static meaning (i.e., static members, static
5623 member functions, enum declarations, etc).
5625 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5626 which can be seen locally to the class (as in 1), but
5627 know that we are doing this for declaration purposes
5628 (i.e. friend foo::bar (int)).
5630 So that we may avoid calls to lookup_name, we cache the _TYPE
5631 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5633 For multiple inheritance, we perform a two-pass depth-first search
5634 of the type lattice. The first pass performs a pre-order search,
5635 marking types after the type has had its fields installed in
5636 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5637 unmarks the marked types. If a field or member function name
5638 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5639 that name becomes `error_mark_node'. */
5642 pushclass (type, modify)
5646 type = TYPE_MAIN_VARIANT (type);
5648 /* Make sure there is enough room for the new entry on the stack. */
5649 if (current_class_depth + 1 >= current_class_stack_size)
5651 current_class_stack_size *= 2;
5653 = (class_stack_node_t) xrealloc (current_class_stack,
5654 current_class_stack_size
5655 * sizeof (struct class_stack_node));
5658 /* Insert a new entry on the class stack. */
5659 current_class_stack[current_class_depth].name = current_class_name;
5660 current_class_stack[current_class_depth].type = current_class_type;
5661 current_class_stack[current_class_depth].access = current_access_specifier;
5662 current_class_stack[current_class_depth].names_used = 0;
5663 current_class_depth++;
5665 /* Now set up the new type. */
5666 current_class_name = TYPE_NAME (type);
5667 if (TREE_CODE (current_class_name) == TYPE_DECL)
5668 current_class_name = DECL_NAME (current_class_name);
5669 current_class_type = type;
5671 /* By default, things in classes are private, while things in
5672 structures or unions are public. */
5673 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5674 ? access_private_node
5675 : access_public_node);
5677 if (previous_class_type != NULL_TREE
5678 && (type != previous_class_type
5679 || !COMPLETE_TYPE_P (previous_class_type))
5680 && current_class_depth == 1)
5682 /* Forcibly remove any old class remnants. */
5683 invalidate_class_lookup_cache ();
5686 /* If we're about to enter a nested class, clear
5687 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5688 if (modify && current_class_depth > 1)
5689 clear_identifier_class_values ();
5694 if (CLASSTYPE_TEMPLATE_INFO (type))
5695 overload_template_name (type);
5700 if (type != previous_class_type || current_class_depth > 1)
5701 push_class_decls (type);
5706 /* We are re-entering the same class we just left, so we
5707 don't have to search the whole inheritance matrix to find
5708 all the decls to bind again. Instead, we install the
5709 cached class_shadowed list, and walk through it binding
5710 names and setting up IDENTIFIER_TYPE_VALUEs. */
5711 set_class_shadows (previous_class_values);
5712 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5714 tree id = TREE_PURPOSE (item);
5715 tree decl = TREE_TYPE (item);
5717 push_class_binding (id, decl);
5718 if (TREE_CODE (decl) == TYPE_DECL)
5719 set_identifier_type_value (id, TREE_TYPE (decl));
5721 unuse_fields (type);
5724 storetags (CLASSTYPE_TAGS (type));
5728 /* When we exit a toplevel class scope, we save the
5729 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5730 reenter the class. Here, we've entered some other class, so we
5731 must invalidate our cache. */
5734 invalidate_class_lookup_cache ()
5738 /* This code can be seen as a cache miss. When we've cached a
5739 class' scope's bindings and we can't use them, we need to reset
5740 them. This is it! */
5741 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5742 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5744 previous_class_type = NULL_TREE;
5747 /* Get out of the current class scope. If we were in a class scope
5748 previously, that is the one popped to. */
5754 /* Since poplevel_class does the popping of class decls nowadays,
5755 this really only frees the obstack used for these decls. */
5758 current_class_depth--;
5759 current_class_name = current_class_stack[current_class_depth].name;
5760 current_class_type = current_class_stack[current_class_depth].type;
5761 current_access_specifier = current_class_stack[current_class_depth].access;
5762 if (current_class_stack[current_class_depth].names_used)
5763 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5766 /* Returns 1 if current_class_type is either T or a nested type of T.
5767 We start looking from 1 because entry 0 is from global scope, and has
5771 currently_open_class (t)
5775 if (t == current_class_type)
5777 for (i = 1; i < current_class_depth; ++i)
5778 if (current_class_stack [i].type == t)
5783 /* If either current_class_type or one of its enclosing classes are derived
5784 from T, return the appropriate type. Used to determine how we found
5785 something via unqualified lookup. */
5788 currently_open_derived_class (t)
5793 if (DERIVED_FROM_P (t, current_class_type))
5794 return current_class_type;
5796 for (i = current_class_depth - 1; i > 0; --i)
5797 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5798 return current_class_stack[i].type;
5803 /* When entering a class scope, all enclosing class scopes' names with
5804 static meaning (static variables, static functions, types and enumerators)
5805 have to be visible. This recursive function calls pushclass for all
5806 enclosing class contexts until global or a local scope is reached.
5807 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5808 formal of the same name. */
5811 push_nested_class (type, modify)
5817 /* A namespace might be passed in error cases, like A::B:C. */
5818 if (type == NULL_TREE
5819 || type == error_mark_node
5820 || TREE_CODE (type) == NAMESPACE_DECL
5821 || ! IS_AGGR_TYPE (type)
5822 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5823 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5826 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5828 if (context && CLASS_TYPE_P (context))
5829 push_nested_class (context, 2);
5830 pushclass (type, modify);
5833 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5838 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5841 if (context && CLASS_TYPE_P (context))
5842 pop_nested_class ();
5845 /* Set global variables CURRENT_LANG_NAME to appropriate value
5846 so that behavior of name-mangling machinery is correct. */
5849 push_lang_context (name)
5852 *current_lang_stack++ = current_lang_name;
5853 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5854 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5856 size_t old_size = VARRAY_SIZE (current_lang_base);
5858 VARRAY_GROW (current_lang_base, old_size + 10);
5859 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5862 if (name == lang_name_cplusplus)
5864 strict_prototype = strict_prototypes_lang_cplusplus;
5865 current_lang_name = name;
5867 else if (name == lang_name_java)
5869 strict_prototype = strict_prototypes_lang_cplusplus;
5870 current_lang_name = name;
5871 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5872 (See record_builtin_java_type in decl.c.) However, that causes
5873 incorrect debug entries if these types are actually used.
5874 So we re-enable debug output after extern "Java". */
5875 DECL_IGNORED_P (java_byte_type_node) = 0;
5876 DECL_IGNORED_P (java_short_type_node) = 0;
5877 DECL_IGNORED_P (java_int_type_node) = 0;
5878 DECL_IGNORED_P (java_long_type_node) = 0;
5879 DECL_IGNORED_P (java_float_type_node) = 0;
5880 DECL_IGNORED_P (java_double_type_node) = 0;
5881 DECL_IGNORED_P (java_char_type_node) = 0;
5882 DECL_IGNORED_P (java_boolean_type_node) = 0;
5884 else if (name == lang_name_c)
5886 strict_prototype = strict_prototypes_lang_c;
5887 current_lang_name = name;
5890 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5893 /* Get out of the current language scope. */
5898 /* Clear the current entry so that garbage collector won't hold on
5900 *current_lang_stack = NULL_TREE;
5901 current_lang_name = *--current_lang_stack;
5902 if (current_lang_name == lang_name_cplusplus
5903 || current_lang_name == lang_name_java)
5904 strict_prototype = strict_prototypes_lang_cplusplus;
5905 else if (current_lang_name == lang_name_c)
5906 strict_prototype = strict_prototypes_lang_c;
5909 /* Type instantiation routines. */
5911 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5912 matches the TARGET_TYPE. If there is no satisfactory match, return
5913 error_mark_node, and issue an error message if COMPLAIN is
5914 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5915 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5916 template arguments. */
5919 resolve_address_of_overloaded_function (target_type,
5928 tree explicit_targs;
5930 /* Here's what the standard says:
5934 If the name is a function template, template argument deduction
5935 is done, and if the argument deduction succeeds, the deduced
5936 arguments are used to generate a single template function, which
5937 is added to the set of overloaded functions considered.
5939 Non-member functions and static member functions match targets of
5940 type "pointer-to-function" or "reference-to-function." Nonstatic
5941 member functions match targets of type "pointer-to-member
5942 function;" the function type of the pointer to member is used to
5943 select the member function from the set of overloaded member
5944 functions. If a nonstatic member function is selected, the
5945 reference to the overloaded function name is required to have the
5946 form of a pointer to member as described in 5.3.1.
5948 If more than one function is selected, any template functions in
5949 the set are eliminated if the set also contains a non-template
5950 function, and any given template function is eliminated if the
5951 set contains a second template function that is more specialized
5952 than the first according to the partial ordering rules 14.5.5.2.
5953 After such eliminations, if any, there shall remain exactly one
5954 selected function. */
5957 int is_reference = 0;
5958 /* We store the matches in a TREE_LIST rooted here. The functions
5959 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5960 interoperability with most_specialized_instantiation. */
5961 tree matches = NULL_TREE;
5964 /* By the time we get here, we should be seeing only real
5965 pointer-to-member types, not the internal POINTER_TYPE to
5966 METHOD_TYPE representation. */
5967 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5968 && (TREE_CODE (TREE_TYPE (target_type))
5969 == METHOD_TYPE)), 0);
5971 /* Check that the TARGET_TYPE is reasonable. */
5972 if (TYPE_PTRFN_P (target_type))
5975 else if (TYPE_PTRMEMFUNC_P (target_type))
5976 /* This is OK, too. */
5978 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5980 /* This is OK, too. This comes from a conversion to reference
5982 target_type = build_reference_type (target_type);
5988 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5989 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5990 return error_mark_node;
5993 /* If we can find a non-template function that matches, we can just
5994 use it. There's no point in generating template instantiations
5995 if we're just going to throw them out anyhow. But, of course, we
5996 can only do this when we don't *need* a template function. */
6001 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6003 tree fn = OVL_FUNCTION (fns);
6006 if (TREE_CODE (fn) == TEMPLATE_DECL)
6007 /* We're not looking for templates just yet. */
6010 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6012 /* We're looking for a non-static member, and this isn't
6013 one, or vice versa. */
6016 /* See if there's a match. */
6017 fntype = TREE_TYPE (fn);
6019 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
6020 else if (!is_reference)
6021 fntype = build_pointer_type (fntype);
6023 if (can_convert_arg (target_type, fntype, fn))
6024 matches = tree_cons (fn, NULL_TREE, matches);
6028 /* Now, if we've already got a match (or matches), there's no need
6029 to proceed to the template functions. But, if we don't have a
6030 match we need to look at them, too. */
6033 tree target_fn_type;
6034 tree target_arg_types;
6035 tree target_ret_type;
6040 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
6042 target_fn_type = TREE_TYPE (target_type);
6043 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6044 target_ret_type = TREE_TYPE (target_fn_type);
6046 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6048 tree fn = OVL_FUNCTION (fns);
6050 tree instantiation_type;
6053 if (TREE_CODE (fn) != TEMPLATE_DECL)
6054 /* We're only looking for templates. */
6057 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6059 /* We're not looking for a non-static member, and this is
6060 one, or vice versa. */
6063 /* Try to do argument deduction. */
6064 targs = make_tree_vec (DECL_NTPARMS (fn));
6065 if (fn_type_unification (fn, explicit_targs, targs,
6066 target_arg_types, target_ret_type,
6068 /* Argument deduction failed. */
6071 /* Instantiate the template. */
6072 instantiation = instantiate_template (fn, targs);
6073 if (instantiation == error_mark_node)
6074 /* Instantiation failed. */
6077 /* See if there's a match. */
6078 instantiation_type = TREE_TYPE (instantiation);
6080 instantiation_type =
6081 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6082 else if (!is_reference)
6083 instantiation_type = build_pointer_type (instantiation_type);
6084 if (can_convert_arg (target_type, instantiation_type, instantiation))
6085 matches = tree_cons (instantiation, fn, matches);
6088 /* Now, remove all but the most specialized of the matches. */
6091 tree match = most_specialized_instantiation (matches,
6094 if (match != error_mark_node)
6095 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6099 /* Now we should have exactly one function in MATCHES. */
6100 if (matches == NULL_TREE)
6102 /* There were *no* matches. */
6105 cp_error ("no matches converting function `%D' to type `%#T'",
6106 DECL_NAME (OVL_FUNCTION (overload)),
6109 /* print_candidates expects a chain with the functions in
6110 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6111 so why be clever?). */
6112 for (; overload; overload = OVL_NEXT (overload))
6113 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6116 print_candidates (matches);
6118 return error_mark_node;
6120 else if (TREE_CHAIN (matches))
6122 /* There were too many matches. */
6128 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6129 DECL_NAME (OVL_FUNCTION (overload)),
6132 /* Since print_candidates expects the functions in the
6133 TREE_VALUE slot, we flip them here. */
6134 for (match = matches; match; match = TREE_CHAIN (match))
6135 TREE_VALUE (match) = TREE_PURPOSE (match);
6137 print_candidates (matches);
6140 return error_mark_node;
6143 /* Good, exactly one match. Now, convert it to the correct type. */
6144 fn = TREE_PURPOSE (matches);
6148 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6149 return build_unary_op (ADDR_EXPR, fn, 0);
6152 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6153 will mark the function as addressed, but here we must do it
6155 mark_addressable (fn);
6161 /* This function will instantiate the type of the expression given in
6162 RHS to match the type of LHSTYPE. If errors exist, then return
6163 error_mark_node. We only complain is COMPLAIN is set. If we are
6164 not complaining, never modify rhs, as overload resolution wants to
6165 try many possible instantiations, in hopes that at least one will
6168 FLAGS is a bitmask, as we see at the top of the function.
6170 For non-recursive calls, LHSTYPE should be a function, pointer to
6171 function, or a pointer to member function. */
6174 instantiate_type (lhstype, rhs, flags)
6178 int complain = (flags & 1);
6179 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6181 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6184 error ("not enough type information");
6185 return error_mark_node;
6188 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6190 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6193 cp_error ("argument of type `%T' does not match `%T'",
6194 TREE_TYPE (rhs), lhstype);
6195 return error_mark_node;
6198 /* We don't overwrite rhs if it is an overloaded function.
6199 Copying it would destroy the tree link. */
6200 if (TREE_CODE (rhs) != OVERLOAD)
6201 rhs = copy_node (rhs);
6203 /* This should really only be used when attempting to distinguish
6204 what sort of a pointer to function we have. For now, any
6205 arithmetic operation which is not supported on pointers
6206 is rejected as an error. */
6208 switch (TREE_CODE (rhs))
6215 my_friendly_abort (177);
6216 return error_mark_node;
6223 new_rhs = instantiate_type (build_pointer_type (lhstype),
6224 TREE_OPERAND (rhs, 0), flags);
6225 if (new_rhs == error_mark_node)
6226 return error_mark_node;
6228 TREE_TYPE (rhs) = lhstype;
6229 TREE_OPERAND (rhs, 0) = new_rhs;
6234 rhs = copy_node (TREE_OPERAND (rhs, 0));
6235 TREE_TYPE (rhs) = unknown_type_node;
6236 return instantiate_type (lhstype, rhs, flags);
6240 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6242 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6243 && complain && !flag_ms_extensions)
6245 /* Note: we check this after the recursive call to avoid
6246 complaining about cases where overload resolution fails. */
6248 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6249 tree fn = PTRMEM_CST_MEMBER (r);
6251 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6254 ("object-dependent reference to `%E' can only be used in a call",
6257 (" to form a pointer to member function, say `&%T::%E'",
6265 rhs = TREE_OPERAND (rhs, 1);
6266 if (BASELINK_P (rhs))
6267 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6269 /* This can happen if we are forming a pointer-to-member for a
6271 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6275 case TEMPLATE_ID_EXPR:
6277 resolve_address_of_overloaded_function (lhstype,
6278 TREE_OPERAND (rhs, 0),
6280 /*template_only=*/1,
6281 TREE_OPERAND (rhs, 1));
6285 resolve_address_of_overloaded_function (lhstype,
6288 /*template_only=*/0,
6289 /*explicit_targs=*/NULL_TREE);
6292 /* Now we should have a baselink. */
6293 my_friendly_assert (BASELINK_P (rhs), 990412);
6295 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6298 /* This is too hard for now. */
6299 my_friendly_abort (183);
6300 return error_mark_node;
6305 TREE_OPERAND (rhs, 0)
6306 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6307 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6308 return error_mark_node;
6309 TREE_OPERAND (rhs, 1)
6310 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6311 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6312 return error_mark_node;
6314 TREE_TYPE (rhs) = lhstype;
6318 case TRUNC_DIV_EXPR:
6319 case FLOOR_DIV_EXPR:
6321 case ROUND_DIV_EXPR:
6323 case TRUNC_MOD_EXPR:
6324 case FLOOR_MOD_EXPR:
6326 case ROUND_MOD_EXPR:
6327 case FIX_ROUND_EXPR:
6328 case FIX_FLOOR_EXPR:
6330 case FIX_TRUNC_EXPR:
6346 case PREINCREMENT_EXPR:
6347 case PREDECREMENT_EXPR:
6348 case POSTINCREMENT_EXPR:
6349 case POSTDECREMENT_EXPR:
6351 error ("invalid operation on uninstantiated type");
6352 return error_mark_node;
6354 case TRUTH_AND_EXPR:
6356 case TRUTH_XOR_EXPR:
6363 case TRUTH_ANDIF_EXPR:
6364 case TRUTH_ORIF_EXPR:
6365 case TRUTH_NOT_EXPR:
6367 error ("not enough type information");
6368 return error_mark_node;
6371 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6374 error ("not enough type information");
6375 return error_mark_node;
6377 TREE_OPERAND (rhs, 1)
6378 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6379 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6380 return error_mark_node;
6381 TREE_OPERAND (rhs, 2)
6382 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6383 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6384 return error_mark_node;
6386 TREE_TYPE (rhs) = lhstype;
6390 TREE_OPERAND (rhs, 1)
6391 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6392 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6393 return error_mark_node;
6395 TREE_TYPE (rhs) = lhstype;
6399 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6401 case ENTRY_VALUE_EXPR:
6402 my_friendly_abort (184);
6403 return error_mark_node;
6406 return error_mark_node;
6409 my_friendly_abort (185);
6410 return error_mark_node;
6414 /* Return the name of the virtual function pointer field
6415 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6416 this may have to look back through base types to find the
6417 ultimate field name. (For single inheritance, these could
6418 all be the same name. Who knows for multiple inheritance). */
6421 get_vfield_name (type)
6424 tree binfo = TYPE_BINFO (type);
6427 while (BINFO_BASETYPES (binfo)
6428 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6429 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6430 binfo = BINFO_BASETYPE (binfo, 0);
6432 type = BINFO_TYPE (binfo);
6433 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6434 + TYPE_NAME_LENGTH (type) + 2);
6435 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6436 return get_identifier (buf);
6440 print_class_statistics ()
6442 #ifdef GATHER_STATISTICS
6443 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6444 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6445 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6446 n_build_method_call, n_inner_fields_searched);
6449 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6450 n_vtables, n_vtable_searches);
6451 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6452 n_vtable_entries, n_vtable_elems);
6457 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6458 according to [class]:
6459 The class-name is also inserted
6460 into the scope of the class itself. For purposes of access checking,
6461 the inserted class name is treated as if it were a public member name. */
6464 build_self_reference ()
6466 tree name = constructor_name (current_class_type);
6467 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6470 DECL_NONLOCAL (value) = 1;
6471 DECL_CONTEXT (value) = current_class_type;
6472 DECL_ARTIFICIAL (value) = 1;
6474 if (processing_template_decl)
6475 value = push_template_decl (value);
6477 saved_cas = current_access_specifier;
6478 current_access_specifier = access_public_node;
6479 finish_member_declaration (value);
6480 current_access_specifier = saved_cas;
6483 /* Returns 1 if TYPE contains only padding bytes. */
6486 is_empty_class (type)
6491 if (type == error_mark_node)
6494 if (! IS_AGGR_TYPE (type))
6498 return integer_zerop (CLASSTYPE_SIZE (type));
6500 if (TYPE_BINFO_BASETYPES (type))
6502 t = TYPE_FIELDS (type);
6503 while (t && TREE_CODE (t) != FIELD_DECL)
6505 return (t == NULL_TREE);
6508 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6509 a *_TYPE node. NODE can also be a local class. */
6512 get_enclosing_class (type)
6517 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6519 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6522 node = DECL_CONTEXT (node);
6528 node = TYPE_CONTEXT (node);
6532 my_friendly_abort (0);
6538 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6541 is_base_of_enclosing_class (base, type)
6546 if (get_binfo (base, type, 0))
6549 type = get_enclosing_class (type);
6554 /* Note that NAME was looked up while the current class was being
6555 defined and that the result of that lookup was DECL. */
6558 maybe_note_name_used_in_class (name, decl)
6562 splay_tree names_used;
6564 /* If we're not defining a class, there's nothing to do. */
6565 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6568 /* If there's already a binding for this NAME, then we don't have
6569 anything to worry about. */
6570 if (IDENTIFIER_CLASS_VALUE (name))
6573 if (!current_class_stack[current_class_depth - 1].names_used)
6574 current_class_stack[current_class_depth - 1].names_used
6575 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6576 names_used = current_class_stack[current_class_depth - 1].names_used;
6578 splay_tree_insert (names_used,
6579 (splay_tree_key) name,
6580 (splay_tree_value) decl);
6583 /* Note that NAME was declared (as DECL) in the current class. Check
6584 to see that the declaration is legal. */
6587 note_name_declared_in_class (name, decl)
6591 splay_tree names_used;
6594 /* Look to see if we ever used this name. */
6596 = current_class_stack[current_class_depth - 1].names_used;
6600 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6603 /* [basic.scope.class]
6605 A name N used in a class S shall refer to the same declaration
6606 in its context and when re-evaluated in the completed scope of
6608 cp_error ("declaration of `%#D'", decl);
6609 cp_error_at ("changes meaning of `%s' from `%+#D'",
6610 IDENTIFIER_POINTER (DECL_NAME (OVL_CURRENT (decl))),
6615 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6616 INDENT should be zero when called from the top level; it is
6617 incremented recursively. */
6620 dump_class_hierarchy (binfo, indent)
6626 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6627 (unsigned long) binfo,
6628 type_as_string (binfo, TS_PLAIN));
6629 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6630 tree_low_cst (BINFO_OFFSET (binfo), 0));
6631 fprintf (stderr, " %s\n",
6632 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6634 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6635 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);