1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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 GCC 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 GCC; 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. */
28 #include "coretypes.h"
39 /* The number of nested classes being processed. If we are not in the
40 scope of any class, this is zero. */
42 int current_class_depth;
44 /* In order to deal with nested classes, we keep a stack of classes.
45 The topmost entry is the innermost class, and is the entry at index
46 CURRENT_CLASS_DEPTH */
48 typedef struct class_stack_node {
49 /* The name of the class. */
52 /* The _TYPE node for the class. */
55 /* The access specifier pending for new declarations in the scope of
59 /* If were defining TYPE, the names used in this class. */
60 splay_tree names_used;
61 }* class_stack_node_t;
63 typedef struct vtbl_init_data_s
65 /* The base for which we're building initializers. */
67 /* The type of the most-derived type. */
69 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
70 unless ctor_vtbl_p is true. */
72 /* The negative-index vtable initializers built up so far. These
73 are in order from least negative index to most negative index. */
75 /* The last (i.e., most negative) entry in INITS. */
77 /* The binfo for the virtual base for which we're building
78 vcall offset initializers. */
80 /* The functions in vbase for which we have already provided vcall
83 /* The vtable index of the next vcall or vbase offset. */
85 /* Nonzero if we are building the initializer for the primary
88 /* Nonzero if we are building the initializer for a construction
91 /* True when adding vcall offset entries to the vtable. False when
92 merely computing the indices. */
93 bool generate_vcall_entries;
96 /* The type of a function passed to walk_subobject_offsets. */
97 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
99 /* The stack itself. This is a dynamically resized array. The
100 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
101 static int current_class_stack_size;
102 static class_stack_node_t current_class_stack;
104 /* An array of all local classes present in this translation unit, in
105 declaration order. */
106 varray_type local_classes;
108 static tree get_vfield_name (tree);
109 static void finish_struct_anon (tree);
110 static tree get_vtable_name (tree);
111 static tree get_basefndecls (tree, tree);
112 static int build_primary_vtable (tree, tree);
113 static int build_secondary_vtable (tree);
114 static void finish_vtbls (tree);
115 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
116 static tree delete_duplicate_fields_1 (tree, tree);
117 static void delete_duplicate_fields (tree);
118 static void finish_struct_bits (tree);
119 static int alter_access (tree, tree, tree);
120 static void handle_using_decl (tree, tree);
121 static void check_for_override (tree, tree);
122 static tree dfs_modify_vtables (tree, void *);
123 static tree modify_all_vtables (tree, tree);
124 static void determine_primary_base (tree);
125 static void finish_struct_methods (tree);
126 static void maybe_warn_about_overly_private_class (tree);
127 static int field_decl_cmp (const void *, const void *);
128 static int resort_field_decl_cmp (const void *, const void *);
129 static int method_name_cmp (const void *, const void *);
130 static int resort_method_name_cmp (const void *, const void *);
131 static void add_implicitly_declared_members (tree, int, int, int);
132 static tree fixed_type_or_null (tree, int *, int *);
133 static tree resolve_address_of_overloaded_function (tree, tree, int,
135 static tree build_vtable_entry_ref (tree, tree, tree);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
138 static int count_fields (tree);
139 static int add_fields_to_vec (tree, tree, int);
140 static void check_bitfield_decl (tree);
141 static void check_field_decl (tree, tree, int *, int *, int *, int *);
142 static void check_field_decls (tree, tree *, int *, int *, int *);
143 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
144 static void build_base_fields (record_layout_info, splay_tree, tree *);
145 static void check_methods (tree);
146 static void remove_zero_width_bit_fields (tree);
147 static void check_bases (tree, int *, int *, int *);
148 static void check_bases_and_members (tree);
149 static tree create_vtable_ptr (tree, tree *);
150 static void include_empty_classes (record_layout_info);
151 static void layout_class_type (tree, tree *);
152 static void fixup_pending_inline (tree);
153 static void fixup_inline_methods (tree);
154 static void set_primary_base (tree, tree);
155 static void propagate_binfo_offsets (tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
160 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset (tree, tree, vtbl_init_data *);
162 static void layout_vtable_decl (tree, int);
163 static tree dfs_find_final_overrider (tree, void *);
164 static tree dfs_find_final_overrider_post (tree, void *);
165 static tree dfs_find_final_overrider_q (tree, int, void *);
166 static tree find_final_overrider (tree, tree, tree);
167 static int make_new_vtable (tree, tree);
168 static int maybe_indent_hierarchy (FILE *, int, int);
169 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
170 static void dump_class_hierarchy (tree);
171 static void dump_array (FILE *, tree);
172 static void dump_vtable (tree, tree, tree);
173 static void dump_vtt (tree, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, tree);
176 static void initialize_array (tree, tree);
177 static void layout_nonempty_base_or_field (record_layout_info,
178 tree, tree, splay_tree);
179 static tree end_of_class (tree, int);
180 static bool layout_empty_base (tree, tree, splay_tree);
181 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
182 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
185 static void build_vcall_and_vbase_vtbl_entries (tree,
187 static void mark_primary_bases (tree);
188 static void clone_constructors_and_destructors (tree);
189 static tree build_clone (tree, tree);
190 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
191 static tree copy_virtuals (tree);
192 static void build_ctor_vtbl_group (tree, tree);
193 static void build_vtt (tree);
194 static tree binfo_ctor_vtable (tree);
195 static tree *build_vtt_inits (tree, tree, tree *, tree *);
196 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
197 static tree dfs_ctor_vtable_bases_queue_p (tree, int, void *data);
198 static tree dfs_fixup_binfo_vtbls (tree, void *);
199 static int record_subobject_offset (tree, tree, splay_tree);
200 static int check_subobject_offset (tree, tree, splay_tree);
201 static int walk_subobject_offsets (tree, subobject_offset_fn,
202 tree, splay_tree, tree, int);
203 static void record_subobject_offsets (tree, tree, splay_tree, int);
204 static int layout_conflict_p (tree, tree, splay_tree, int);
205 static int splay_tree_compare_integer_csts (splay_tree_key k1,
207 static void warn_about_ambiguous_bases (tree);
208 static bool type_requires_array_cookie (tree);
209 static bool contains_empty_class_p (tree);
210 static bool base_derived_from (tree, tree);
211 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
212 static tree end_of_base (tree);
213 static tree get_vcall_index (tree, tree);
215 /* Macros for dfs walking during vtt construction. See
216 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
217 and dfs_fixup_binfo_vtbls. */
218 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
219 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
221 /* Variables shared between class.c and call.c. */
223 #ifdef GATHER_STATISTICS
225 int n_vtable_entries = 0;
226 int n_vtable_searches = 0;
227 int n_vtable_elems = 0;
228 int n_convert_harshness = 0;
229 int n_compute_conversion_costs = 0;
230 int n_build_method_call = 0;
231 int n_inner_fields_searched = 0;
234 /* Convert to or from a base subobject. EXPR is an expression of type
235 `A' or `A*', an expression of type `B' or `B*' is returned. To
236 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
237 the B base instance within A. To convert base A to derived B, CODE
238 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
239 In this latter case, A must not be a morally virtual base of B.
240 NONNULL is true if EXPR is known to be non-NULL (this is only
241 needed when EXPR is of pointer type). CV qualifiers are preserved
245 build_base_path (enum tree_code code,
250 tree v_binfo = NULL_TREE;
251 tree d_binfo = NULL_TREE;
255 tree null_test = NULL;
256 tree ptr_target_type;
258 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
260 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
261 return error_mark_node;
263 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
266 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 my_friendly_assert (code == MINUS_EXPR
275 ? same_type_p (BINFO_TYPE (binfo), probe)
277 ? same_type_p (BINFO_TYPE (d_binfo), probe)
280 if (code == MINUS_EXPR && v_binfo)
282 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
283 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
284 return error_mark_node;
288 /* This must happen before the call to save_expr. */
289 expr = build_unary_op (ADDR_EXPR, expr, 0);
291 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
292 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
293 expr = save_expr (expr);
295 if (want_pointer && !nonnull)
296 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
298 offset = BINFO_OFFSET (binfo);
300 if (v_binfo && fixed_type_p <= 0)
302 /* Going via virtual base V_BINFO. We need the static offset
303 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
304 V_BINFO. That offset is an entry in D_BINFO's vtable. */
307 if (fixed_type_p < 0 && in_base_initializer)
309 /* In a base member initializer, we cannot rely on
310 the vtable being set up. We have to use the vtt_parm. */
311 tree derived = BINFO_INHERITANCE_CHAIN (v_binfo);
313 v_offset = build (PLUS_EXPR, TREE_TYPE (current_vtt_parm),
314 current_vtt_parm, BINFO_VPTR_INDEX (derived));
316 v_offset = build1 (INDIRECT_REF,
317 TREE_TYPE (TYPE_VFIELD (BINFO_TYPE (derived))),
322 v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
323 TREE_TYPE (TREE_TYPE (expr)));
325 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
326 v_offset, BINFO_VPTR_FIELD (v_binfo));
327 v_offset = build1 (NOP_EXPR,
328 build_pointer_type (ptrdiff_type_node),
330 v_offset = build_indirect_ref (v_offset, NULL);
331 TREE_CONSTANT (v_offset) = 1;
333 offset = cp_convert (ptrdiff_type_node,
334 size_diffop (offset, BINFO_OFFSET (v_binfo)));
336 if (!integer_zerop (offset))
337 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
339 if (fixed_type_p < 0)
340 /* Negative fixed_type_p means this is a constructor or destructor;
341 virtual base layout is fixed in in-charge [cd]tors, but not in
343 offset = build (COND_EXPR, ptrdiff_type_node,
344 build (EQ_EXPR, boolean_type_node,
345 current_in_charge_parm, integer_zero_node),
347 BINFO_OFFSET (binfo));
352 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
354 target_type = cp_build_qualified_type
355 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
356 ptr_target_type = build_pointer_type (target_type);
358 target_type = ptr_target_type;
360 expr = build1 (NOP_EXPR, ptr_target_type, expr);
362 if (!integer_zerop (offset))
363 expr = build (code, ptr_target_type, expr, offset);
368 expr = build_indirect_ref (expr, NULL);
371 expr = build (COND_EXPR, target_type, null_test,
372 build1 (NOP_EXPR, target_type, integer_zero_node),
378 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
379 message is emitted if TYPE is inaccessible. OBJECT is assumed to
383 convert_to_base (tree object, tree type, bool check_access)
387 binfo = lookup_base (TREE_TYPE (object), type,
388 check_access ? ba_check : ba_ignore,
390 if (!binfo || binfo == error_mark_node)
391 return error_mark_node;
393 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
397 /* Virtual function things. */
400 build_vtable_entry_ref (tree array_ref, tree instance, tree idx)
402 tree i, i2, vtable, first_fn, basetype;
404 basetype = TREE_TYPE (instance);
405 if (TREE_CODE (basetype) == REFERENCE_TYPE)
406 basetype = TREE_TYPE (basetype);
408 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
409 first_fn = TYPE_BINFO_VTABLE (basetype);
411 i = fold (build_array_ref (first_fn, idx));
412 i = fold (build_c_cast (ptrdiff_type_node,
413 build_unary_op (ADDR_EXPR, i, 0)));
414 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
415 i2 = fold (build_c_cast (ptrdiff_type_node,
416 build_unary_op (ADDR_EXPR, i2, 0)));
417 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
419 if (TREE_CODE (i) != INTEGER_CST)
422 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
425 /* Given an object INSTANCE, return an expression which yields the
426 vtable element corresponding to INDEX. There are many special
427 cases for INSTANCE which we take care of here, mainly to avoid
428 creating extra tree nodes when we don't have to. */
431 build_vtbl_ref_1 (tree instance, tree idx)
434 tree vtbl = NULL_TREE;
436 /* Try to figure out what a reference refers to, and
437 access its virtual function table directly. */
440 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
442 tree basetype = TREE_TYPE (instance);
443 if (TREE_CODE (basetype) == REFERENCE_TYPE)
444 basetype = TREE_TYPE (basetype);
446 if (fixed_type && !cdtorp)
448 tree binfo = lookup_base (fixed_type, basetype,
449 ba_ignore|ba_quiet, NULL);
451 vtbl = BINFO_VTABLE (binfo);
455 vtbl = build_vfield_ref (instance, basetype);
457 assemble_external (vtbl);
459 aref = build_array_ref (vtbl, idx);
460 TREE_CONSTANT (aref) = 1;
466 build_vtbl_ref (tree instance, tree idx)
468 tree aref = build_vtbl_ref_1 (instance, idx);
471 aref = build_vtable_entry_ref (aref, instance, idx);
476 /* Given an object INSTANCE, return an expression which yields a
477 function pointer corresponding to vtable element INDEX. */
480 build_vfn_ref (tree instance, tree idx)
482 tree aref = build_vtbl_ref_1 (instance, idx);
484 /* When using function descriptors, the address of the
485 vtable entry is treated as a function pointer. */
486 if (TARGET_VTABLE_USES_DESCRIPTORS)
487 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
488 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
491 aref = build_vtable_entry_ref (aref, instance, idx);
496 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
497 for the given TYPE. */
500 get_vtable_name (tree type)
502 return mangle_vtbl_for_type (type);
505 /* Return an IDENTIFIER_NODE for the name of the virtual table table
509 get_vtt_name (tree type)
511 return mangle_vtt_for_type (type);
514 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
515 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
516 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
519 build_vtable (tree class_type, tree name, tree vtable_type)
523 decl = build_lang_decl (VAR_DECL, name, vtable_type);
524 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
525 now to avoid confusion in mangle_decl. */
526 SET_DECL_ASSEMBLER_NAME (decl, name);
527 DECL_CONTEXT (decl) = class_type;
528 DECL_ARTIFICIAL (decl) = 1;
529 TREE_STATIC (decl) = 1;
530 TREE_READONLY (decl) = 1;
531 DECL_VIRTUAL_P (decl) = 1;
532 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
534 /* At one time the vtable info was grabbed 2 words at a time. This
535 fails on sparc unless you have 8-byte alignment. (tiemann) */
536 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
539 import_export_vtable (decl, class_type, 0);
544 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
545 or even complete. If this does not exist, create it. If COMPLETE is
546 nonzero, then complete the definition of it -- that will render it
547 impossible to actually build the vtable, but is useful to get at those
548 which are known to exist in the runtime. */
551 get_vtable_decl (tree type, int complete)
555 if (CLASSTYPE_VTABLES (type))
556 return CLASSTYPE_VTABLES (type);
558 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
559 CLASSTYPE_VTABLES (type) = decl;
563 DECL_EXTERNAL (decl) = 1;
564 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
570 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
571 BV_VCALL_INDEX for each entry is cleared. */
574 copy_virtuals (tree binfo)
579 copies = copy_list (BINFO_VIRTUALS (binfo));
580 for (t = copies; t; t = TREE_CHAIN (t))
581 BV_VCALL_INDEX (t) = NULL_TREE;
586 /* Build the primary virtual function table for TYPE. If BINFO is
587 non-NULL, build the vtable starting with the initial approximation
588 that it is the same as the one which is the head of the association
589 list. Returns a nonzero value if a new vtable is actually
593 build_primary_vtable (tree binfo, tree type)
598 decl = get_vtable_decl (type, /*complete=*/0);
602 if (BINFO_NEW_VTABLE_MARKED (binfo))
603 /* We have already created a vtable for this base, so there's
604 no need to do it again. */
607 virtuals = copy_virtuals (binfo);
608 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
609 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
610 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
614 my_friendly_assert (TREE_TYPE (decl) == vtbl_type_node, 20000118);
615 virtuals = NULL_TREE;
618 #ifdef GATHER_STATISTICS
620 n_vtable_elems += list_length (virtuals);
623 /* Initialize the association list for this type, based
624 on our first approximation. */
625 TYPE_BINFO_VTABLE (type) = decl;
626 TYPE_BINFO_VIRTUALS (type) = virtuals;
627 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
631 /* Give BINFO a new virtual function table which is initialized
632 with a skeleton-copy of its original initialization. The only
633 entry that changes is the `delta' entry, so we can really
634 share a lot of structure.
636 FOR_TYPE is the most derived type which caused this table to
639 Returns nonzero if we haven't met BINFO before.
641 The order in which vtables are built (by calling this function) for
642 an object must remain the same, otherwise a binary incompatibility
646 build_secondary_vtable (tree binfo)
648 if (BINFO_NEW_VTABLE_MARKED (binfo))
649 /* We already created a vtable for this base. There's no need to
653 /* Remember that we've created a vtable for this BINFO, so that we
654 don't try to do so again. */
655 SET_BINFO_NEW_VTABLE_MARKED (binfo);
657 /* Make fresh virtual list, so we can smash it later. */
658 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
660 /* Secondary vtables are laid out as part of the same structure as
661 the primary vtable. */
662 BINFO_VTABLE (binfo) = NULL_TREE;
666 /* Create a new vtable for BINFO which is the hierarchy dominated by
667 T. Return nonzero if we actually created a new vtable. */
670 make_new_vtable (tree t, tree binfo)
672 if (binfo == TYPE_BINFO (t))
673 /* In this case, it is *type*'s vtable we are modifying. We start
674 with the approximation that its vtable is that of the
675 immediate base class. */
676 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
677 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
678 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
681 /* This is our very own copy of `basetype' to play with. Later,
682 we will fill in all the virtual functions that override the
683 virtual functions in these base classes which are not defined
684 by the current type. */
685 return build_secondary_vtable (binfo);
688 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
689 (which is in the hierarchy dominated by T) list FNDECL as its
690 BV_FN. DELTA is the required constant adjustment from the `this'
691 pointer where the vtable entry appears to the `this' required when
692 the function is actually called. */
695 modify_vtable_entry (tree t,
705 if (fndecl != BV_FN (v)
706 || !tree_int_cst_equal (delta, BV_DELTA (v)))
708 /* We need a new vtable for BINFO. */
709 if (make_new_vtable (t, binfo))
711 /* If we really did make a new vtable, we also made a copy
712 of the BINFO_VIRTUALS list. Now, we have to find the
713 corresponding entry in that list. */
714 *virtuals = BINFO_VIRTUALS (binfo);
715 while (BV_FN (*virtuals) != BV_FN (v))
716 *virtuals = TREE_CHAIN (*virtuals);
720 BV_DELTA (v) = delta;
721 BV_VCALL_INDEX (v) = NULL_TREE;
727 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
728 the method after the class has already been defined because a
729 declaration for it was seen. (Even though that is erroneous, we
730 add the method for improved error recovery.) */
733 add_method (tree type, tree method, int error_p)
735 int using = (DECL_CONTEXT (method) != type);
739 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
740 && DECL_TEMPLATE_CONV_FN_P (method));
742 if (!CLASSTYPE_METHOD_VEC (type))
743 /* Make a new method vector. We start with 8 entries. We must
744 allocate at least two (for constructors and destructors), and
745 we're going to end up with an assignment operator at some point
748 We could use a TREE_LIST for now, and convert it to a TREE_VEC
749 in finish_struct, but we would probably waste more memory
750 making the links in the list than we would by over-allocating
751 the size of the vector here. Furthermore, we would complicate
752 all the code that expects this to be a vector. */
753 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
755 method_vec = CLASSTYPE_METHOD_VEC (type);
756 len = TREE_VEC_LENGTH (method_vec);
758 /* Constructors and destructors go in special slots. */
759 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
760 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
761 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
762 slot = CLASSTYPE_DESTRUCTOR_SLOT;
765 int have_template_convs_p = 0;
767 /* See if we already have an entry with this name. */
768 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
770 tree m = TREE_VEC_ELT (method_vec, slot);
778 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
779 && DECL_TEMPLATE_CONV_FN_P (m));
781 /* If we need to move things up, see if there's
783 if (!have_template_convs_p)
786 if (TREE_VEC_ELT (method_vec, slot))
791 if (DECL_NAME (m) == DECL_NAME (method))
797 /* We need a bigger method vector. */
801 /* In the non-error case, we are processing a class
802 definition. Double the size of the vector to give room
806 /* In the error case, the vector is already complete. We
807 don't expect many errors, and the rest of the front-end
808 will get confused if there are empty slots in the vector. */
812 new_vec = make_tree_vec (new_len);
813 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
814 len * sizeof (tree));
816 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
819 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
821 /* Type conversion operators have to come before ordinary
822 methods; add_conversions depends on this to speed up
823 looking for conversion operators. So, if necessary, we
824 slide some of the vector elements up. In theory, this
825 makes this algorithm O(N^2) but we don't expect many
826 conversion operators. */
828 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
830 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
832 tree fn = TREE_VEC_ELT (method_vec, slot);
835 /* There are no more entries in the vector, so we
836 can insert the new conversion operator here. */
839 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
840 /* We can insert the new function right at the
845 if (template_conv_p && have_template_convs_p)
847 else if (!TREE_VEC_ELT (method_vec, slot))
848 /* There is nothing in the Ith slot, so we can avoid
853 /* We know the last slot in the vector is empty
854 because we know that at this point there's room
855 for a new function. */
856 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
857 &TREE_VEC_ELT (method_vec, slot),
858 (len - slot - 1) * sizeof (tree));
859 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
864 if (template_class_depth (type))
865 /* TYPE is a template class. Don't issue any errors now; wait
866 until instantiation time to complain. */
872 /* Check to see if we've already got this method. */
873 for (fns = TREE_VEC_ELT (method_vec, slot);
875 fns = OVL_NEXT (fns))
877 tree fn = OVL_CURRENT (fns);
882 if (TREE_CODE (fn) != TREE_CODE (method))
885 /* [over.load] Member function declarations with the
886 same name and the same parameter types cannot be
887 overloaded if any of them is a static member
888 function declaration.
890 [namespace.udecl] When a using-declaration brings names
891 from a base class into a derived class scope, member
892 functions in the derived class override and/or hide member
893 functions with the same name and parameter types in a base
894 class (rather than conflicting). */
895 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
896 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
898 /* Compare the quals on the 'this' parm. Don't compare
899 the whole types, as used functions are treated as
900 coming from the using class in overload resolution. */
901 if (! DECL_STATIC_FUNCTION_P (fn)
902 && ! DECL_STATIC_FUNCTION_P (method)
903 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
904 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
907 /* For templates, the template parms must be identical. */
908 if (TREE_CODE (fn) == TEMPLATE_DECL
909 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
910 DECL_TEMPLATE_PARMS (method)))
913 if (! DECL_STATIC_FUNCTION_P (fn))
914 parms1 = TREE_CHAIN (parms1);
915 if (! DECL_STATIC_FUNCTION_P (method))
916 parms2 = TREE_CHAIN (parms2);
918 if (same && compparms (parms1, parms2)
919 && (!DECL_CONV_FN_P (fn)
920 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
921 TREE_TYPE (TREE_TYPE (method)))))
923 if (using && DECL_CONTEXT (fn) == type)
924 /* Defer to the local function. */
928 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
931 /* We don't call duplicate_decls here to merge
932 the declarations because that will confuse
933 things if the methods have inline
934 definitions. In particular, we will crash
935 while processing the definitions. */
942 /* Actually insert the new method. */
943 TREE_VEC_ELT (method_vec, slot)
944 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
946 /* Add the new binding. */
947 if (!DECL_CONSTRUCTOR_P (method)
948 && !DECL_DESTRUCTOR_P (method))
949 push_class_level_binding (DECL_NAME (method),
950 TREE_VEC_ELT (method_vec, slot));
953 /* Subroutines of finish_struct. */
955 /* Look through the list of fields for this struct, deleting
956 duplicates as we go. This must be recursive to handle
959 FIELD is the field which may not appear anywhere in FIELDS.
960 FIELD_PTR, if non-null, is the starting point at which
961 chained deletions may take place.
962 The value returned is the first acceptable entry found
965 Note that anonymous fields which are not of UNION_TYPE are
966 not duplicates, they are just anonymous fields. This happens
967 when we have unnamed bitfields, for example. */
970 delete_duplicate_fields_1 (tree field, tree fields)
974 if (DECL_NAME (field) == 0)
976 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
979 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
980 fields = delete_duplicate_fields_1 (x, fields);
985 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
987 if (DECL_NAME (x) == 0)
989 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
991 TYPE_FIELDS (TREE_TYPE (x))
992 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
993 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
996 fields = TREE_CHAIN (fields);
998 TREE_CHAIN (prev) = TREE_CHAIN (x);
1001 else if (TREE_CODE (field) == USING_DECL)
1002 /* A using declaration is allowed to appear more than
1003 once. We'll prune these from the field list later, and
1004 handle_using_decl will complain about invalid multiple
1007 else if (DECL_NAME (field) == DECL_NAME (x))
1009 if (TREE_CODE (field) == CONST_DECL
1010 && TREE_CODE (x) == CONST_DECL)
1011 cp_error_at ("duplicate enum value `%D'", x);
1012 else if (TREE_CODE (field) == CONST_DECL
1013 || TREE_CODE (x) == CONST_DECL)
1014 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1016 else if (DECL_DECLARES_TYPE_P (field)
1017 && DECL_DECLARES_TYPE_P (x))
1019 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1021 cp_error_at ("duplicate nested type `%D'", x);
1023 else if (DECL_DECLARES_TYPE_P (field)
1024 || DECL_DECLARES_TYPE_P (x))
1026 /* Hide tag decls. */
1027 if ((TREE_CODE (field) == TYPE_DECL
1028 && DECL_ARTIFICIAL (field))
1029 || (TREE_CODE (x) == TYPE_DECL
1030 && DECL_ARTIFICIAL (x)))
1032 cp_error_at ("duplicate field `%D' (as type and non-type)",
1036 cp_error_at ("duplicate member `%D'", x);
1038 fields = TREE_CHAIN (fields);
1040 TREE_CHAIN (prev) = TREE_CHAIN (x);
1048 delete_duplicate_fields (tree fields)
1051 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1052 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1055 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1056 legit, otherwise return 0. */
1059 alter_access (tree t, tree fdecl, tree access)
1063 if (!DECL_LANG_SPECIFIC (fdecl))
1064 retrofit_lang_decl (fdecl);
1066 if (DECL_DISCRIMINATOR_P (fdecl))
1069 elem = purpose_member (t, DECL_ACCESS (fdecl));
1072 if (TREE_VALUE (elem) != access)
1074 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1075 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1077 error ("conflicting access specifications for field `%s', ignored",
1078 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1082 /* They're changing the access to the same thing they changed
1083 it to before. That's OK. */
1089 enforce_access (t, fdecl);
1090 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1096 /* Process the USING_DECL, which is a member of T. */
1099 handle_using_decl (tree using_decl, tree t)
1101 tree ctype = DECL_INITIAL (using_decl);
1102 tree name = DECL_NAME (using_decl);
1104 = TREE_PRIVATE (using_decl) ? access_private_node
1105 : TREE_PROTECTED (using_decl) ? access_protected_node
1106 : access_public_node;
1108 tree flist = NULL_TREE;
1111 binfo = lookup_base (t, ctype, ba_any, NULL);
1114 error_not_base_type (t, ctype);
1118 if (constructor_name_p (name, ctype))
1120 cp_error_at ("`%D' names constructor", using_decl);
1123 if (constructor_name_p (name, t))
1125 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1129 fdecl = lookup_member (binfo, name, 0, false);
1133 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1137 if (BASELINK_P (fdecl))
1138 /* Ignore base type this came from. */
1139 fdecl = BASELINK_FUNCTIONS (fdecl);
1141 old_value = IDENTIFIER_CLASS_VALUE (name);
1144 if (is_overloaded_fn (old_value))
1145 old_value = OVL_CURRENT (old_value);
1147 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1150 old_value = NULL_TREE;
1153 if (is_overloaded_fn (fdecl))
1158 else if (is_overloaded_fn (old_value))
1161 /* It's OK to use functions from a base when there are functions with
1162 the same name already present in the current class. */;
1165 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1166 cp_error_at (" because of local method `%#D' with same name",
1167 OVL_CURRENT (old_value));
1171 else if (!DECL_ARTIFICIAL (old_value))
1173 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1174 cp_error_at (" because of local member `%#D' with same name", old_value);
1178 /* Make type T see field decl FDECL with access ACCESS.*/
1180 for (; flist; flist = OVL_NEXT (flist))
1182 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1183 alter_access (t, OVL_CURRENT (flist), access);
1186 alter_access (t, fdecl, access);
1189 /* Run through the base clases of T, updating
1190 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1191 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1195 check_bases (tree t,
1196 int* cant_have_default_ctor_p,
1197 int* cant_have_const_ctor_p,
1198 int* no_const_asn_ref_p)
1202 int seen_non_virtual_nearly_empty_base_p;
1205 binfos = TYPE_BINFO_BASETYPES (t);
1206 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1207 seen_non_virtual_nearly_empty_base_p = 0;
1209 /* An aggregate cannot have baseclasses. */
1210 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1212 for (i = 0; i < n_baseclasses; ++i)
1217 /* Figure out what base we're looking at. */
1218 base_binfo = TREE_VEC_ELT (binfos, i);
1219 basetype = TREE_TYPE (base_binfo);
1221 /* If the type of basetype is incomplete, then we already
1222 complained about that fact (and we should have fixed it up as
1224 if (!COMPLETE_TYPE_P (basetype))
1227 /* The base type is of incomplete type. It is
1228 probably best to pretend that it does not
1230 if (i == n_baseclasses-1)
1231 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1232 TREE_VEC_LENGTH (binfos) -= 1;
1234 for (j = i; j+1 < n_baseclasses; j++)
1235 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1239 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1240 here because the case of virtual functions but non-virtual
1241 dtor is handled in finish_struct_1. */
1242 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1243 && TYPE_HAS_DESTRUCTOR (basetype))
1244 warning ("base class `%#T' has a non-virtual destructor",
1247 /* If the base class doesn't have copy constructors or
1248 assignment operators that take const references, then the
1249 derived class cannot have such a member automatically
1251 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1252 *cant_have_const_ctor_p = 1;
1253 if (TYPE_HAS_ASSIGN_REF (basetype)
1254 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1255 *no_const_asn_ref_p = 1;
1256 /* Similarly, if the base class doesn't have a default
1257 constructor, then the derived class won't have an
1258 automatically generated default constructor. */
1259 if (TYPE_HAS_CONSTRUCTOR (basetype)
1260 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1262 *cant_have_default_ctor_p = 1;
1263 if (! TYPE_HAS_CONSTRUCTOR (t))
1264 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1268 if (TREE_VIA_VIRTUAL (base_binfo))
1269 /* A virtual base does not effect nearly emptiness. */
1271 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1273 if (seen_non_virtual_nearly_empty_base_p)
1274 /* And if there is more than one nearly empty base, then the
1275 derived class is not nearly empty either. */
1276 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1278 /* Remember we've seen one. */
1279 seen_non_virtual_nearly_empty_base_p = 1;
1281 else if (!is_empty_class (basetype))
1282 /* If the base class is not empty or nearly empty, then this
1283 class cannot be nearly empty. */
1284 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1286 /* A lot of properties from the bases also apply to the derived
1288 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1289 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1290 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1291 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1292 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1293 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1294 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1295 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1296 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1297 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1298 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1299 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1303 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1304 dominated by TYPE that are primary bases. */
1307 mark_primary_bases (tree type)
1311 /* Walk the bases in inheritance graph order. */
1312 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1314 tree base_binfo = get_primary_binfo (binfo);
1317 /* Not a dynamic base. */;
1318 else if (BINFO_PRIMARY_P (base_binfo))
1319 BINFO_LOST_PRIMARY_P (binfo) = 1;
1322 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1323 /* A virtual binfo might have been copied from within
1324 another hierarchy. As we're about to use it as a primary
1325 base, make sure the offsets match. */
1326 if (TREE_VIA_VIRTUAL (base_binfo))
1328 tree delta = size_diffop (convert (ssizetype,
1329 BINFO_OFFSET (binfo)),
1331 BINFO_OFFSET (base_binfo)));
1333 propagate_binfo_offsets (base_binfo, delta);
1339 /* Make the BINFO the primary base of T. */
1342 set_primary_base (tree t, tree binfo)
1346 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1347 basetype = BINFO_TYPE (binfo);
1348 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1349 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1350 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1353 /* Determine the primary class for T. */
1356 determine_primary_base (tree t)
1358 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1362 /* If there are no baseclasses, there is certainly no primary base. */
1363 if (n_baseclasses == 0)
1366 type_binfo = TYPE_BINFO (t);
1368 for (i = 0; i < n_baseclasses; i++)
1370 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1371 tree basetype = BINFO_TYPE (base_binfo);
1373 if (TYPE_CONTAINS_VPTR_P (basetype))
1375 /* We prefer a non-virtual base, although a virtual one will
1377 if (TREE_VIA_VIRTUAL (base_binfo))
1380 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1382 set_primary_base (t, base_binfo);
1383 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1389 /* Only add unique vfields, and flatten them out as we go. */
1390 for (vfields = CLASSTYPE_VFIELDS (basetype);
1392 vfields = TREE_CHAIN (vfields))
1393 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1394 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1395 CLASSTYPE_VFIELDS (t)
1396 = tree_cons (base_binfo,
1397 VF_BASETYPE_VALUE (vfields),
1398 CLASSTYPE_VFIELDS (t));
1403 if (!TYPE_VFIELD (t))
1404 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1406 /* Find the indirect primary bases - those virtual bases which are primary
1407 bases of something else in this hierarchy. */
1408 for (vbases = CLASSTYPE_VBASECLASSES (t);
1410 vbases = TREE_CHAIN (vbases))
1412 tree vbase_binfo = TREE_VALUE (vbases);
1414 /* See if this virtual base is an indirect primary base. To be so,
1415 it must be a primary base within the hierarchy of one of our
1417 for (i = 0; i < n_baseclasses; ++i)
1419 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1422 for (v = CLASSTYPE_VBASECLASSES (basetype);
1426 tree base_vbase = TREE_VALUE (v);
1428 if (BINFO_PRIMARY_P (base_vbase)
1429 && same_type_p (BINFO_TYPE (base_vbase),
1430 BINFO_TYPE (vbase_binfo)))
1432 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1437 /* If we've discovered that this virtual base is an indirect
1438 primary base, then we can move on to the next virtual
1440 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1445 /* A "nearly-empty" virtual base class can be the primary base
1446 class, if no non-virtual polymorphic base can be found. */
1447 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1449 /* If not NULL, this is the best primary base candidate we have
1451 tree candidate = NULL_TREE;
1454 /* Loop over the baseclasses. */
1455 for (base_binfo = TYPE_BINFO (t);
1457 base_binfo = TREE_CHAIN (base_binfo))
1459 tree basetype = BINFO_TYPE (base_binfo);
1461 if (TREE_VIA_VIRTUAL (base_binfo)
1462 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1464 /* If this is not an indirect primary base, then it's
1465 definitely our primary base. */
1466 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1468 candidate = base_binfo;
1472 /* If this is an indirect primary base, it still could be
1473 our primary base -- unless we later find there's another
1474 nearly-empty virtual base that isn't an indirect
1477 candidate = base_binfo;
1481 /* If we've got a primary base, use it. */
1484 set_primary_base (t, candidate);
1485 CLASSTYPE_VFIELDS (t)
1486 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1490 /* Mark the primary base classes at this point. */
1491 mark_primary_bases (t);
1494 /* Set memoizing fields and bits of T (and its variants) for later
1498 finish_struct_bits (tree t)
1500 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1502 /* Fix up variants (if any). */
1503 tree variants = TYPE_NEXT_VARIANT (t);
1506 /* These fields are in the _TYPE part of the node, not in
1507 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1508 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1509 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1510 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1511 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1512 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1514 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1515 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1516 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1517 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1518 /* Copy whatever these are holding today. */
1519 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1520 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1521 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1522 TYPE_SIZE (variants) = TYPE_SIZE (t);
1523 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1524 variants = TYPE_NEXT_VARIANT (variants);
1527 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1528 /* For a class w/o baseclasses, `finish_struct' has set
1529 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1530 definition). Similarly for a class whose base classes do not
1531 have vtables. When neither of these is true, we might have
1532 removed abstract virtuals (by providing a definition), added
1533 some (by declaring new ones), or redeclared ones from a base
1534 class. We need to recalculate what's really an abstract virtual
1535 at this point (by looking in the vtables). */
1536 get_pure_virtuals (t);
1540 /* Notice whether this class has type conversion functions defined. */
1541 tree binfo = TYPE_BINFO (t);
1542 tree binfos = BINFO_BASETYPES (binfo);
1545 for (i = n_baseclasses-1; i >= 0; i--)
1547 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1549 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1553 /* If this type has a copy constructor or a destructor, force its mode to
1554 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1555 will cause it to be passed by invisible reference and prevent it from
1556 being returned in a register. */
1557 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1560 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1561 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1563 TYPE_MODE (variants) = BLKmode;
1564 TREE_ADDRESSABLE (variants) = 1;
1569 /* Issue warnings about T having private constructors, but no friends,
1572 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1573 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1574 non-private static member functions. */
1577 maybe_warn_about_overly_private_class (tree t)
1579 int has_member_fn = 0;
1580 int has_nonprivate_method = 0;
1583 if (!warn_ctor_dtor_privacy
1584 /* If the class has friends, those entities might create and
1585 access instances, so we should not warn. */
1586 || (CLASSTYPE_FRIEND_CLASSES (t)
1587 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1588 /* We will have warned when the template was declared; there's
1589 no need to warn on every instantiation. */
1590 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1591 /* There's no reason to even consider warning about this
1595 /* We only issue one warning, if more than one applies, because
1596 otherwise, on code like:
1599 // Oops - forgot `public:'
1605 we warn several times about essentially the same problem. */
1607 /* Check to see if all (non-constructor, non-destructor) member
1608 functions are private. (Since there are no friends or
1609 non-private statics, we can't ever call any of the private member
1611 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1612 /* We're not interested in compiler-generated methods; they don't
1613 provide any way to call private members. */
1614 if (!DECL_ARTIFICIAL (fn))
1616 if (!TREE_PRIVATE (fn))
1618 if (DECL_STATIC_FUNCTION_P (fn))
1619 /* A non-private static member function is just like a
1620 friend; it can create and invoke private member
1621 functions, and be accessed without a class
1625 has_nonprivate_method = 1;
1626 /* Keep searching for a static member function. */
1628 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1632 if (!has_nonprivate_method && has_member_fn)
1634 /* There are no non-private methods, and there's at least one
1635 private member function that isn't a constructor or
1636 destructor. (If all the private members are
1637 constructors/destructors we want to use the code below that
1638 issues error messages specifically referring to
1639 constructors/destructors.) */
1641 tree binfo = TYPE_BINFO (t);
1643 for (i = 0; i < BINFO_N_BASETYPES (binfo); i++)
1644 if (BINFO_BASEACCESS (binfo, i) != access_private_node)
1646 has_nonprivate_method = 1;
1649 if (!has_nonprivate_method)
1651 warning ("all member functions in class `%T' are private", t);
1656 /* Even if some of the member functions are non-private, the class
1657 won't be useful for much if all the constructors or destructors
1658 are private: such an object can never be created or destroyed. */
1659 if (TYPE_HAS_DESTRUCTOR (t))
1661 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1663 if (TREE_PRIVATE (dtor))
1665 warning ("`%#T' only defines a private destructor and has no friends",
1671 if (TYPE_HAS_CONSTRUCTOR (t))
1673 int nonprivate_ctor = 0;
1675 /* If a non-template class does not define a copy
1676 constructor, one is defined for it, enabling it to avoid
1677 this warning. For a template class, this does not
1678 happen, and so we would normally get a warning on:
1680 template <class T> class C { private: C(); };
1682 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1683 complete non-template or fully instantiated classes have this
1685 if (!TYPE_HAS_INIT_REF (t))
1686 nonprivate_ctor = 1;
1688 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1692 tree ctor = OVL_CURRENT (fn);
1693 /* Ideally, we wouldn't count copy constructors (or, in
1694 fact, any constructor that takes an argument of the
1695 class type as a parameter) because such things cannot
1696 be used to construct an instance of the class unless
1697 you already have one. But, for now at least, we're
1699 if (! TREE_PRIVATE (ctor))
1701 nonprivate_ctor = 1;
1706 if (nonprivate_ctor == 0)
1708 warning ("`%#T' only defines private constructors and has no friends",
1715 /* Function to help qsort sort FIELD_DECLs by name order. */
1718 field_decl_cmp (const void* x_p, const void* y_p)
1720 const tree *const x = x_p;
1721 const tree *const y = y_p;
1722 if (DECL_NAME (*x) == DECL_NAME (*y))
1723 /* A nontype is "greater" than a type. */
1724 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1725 if (DECL_NAME (*x) == NULL_TREE)
1727 if (DECL_NAME (*y) == NULL_TREE)
1729 if (DECL_NAME (*x) < DECL_NAME (*y))
1735 gt_pointer_operator new_value;
1739 /* This routine compares two fields like field_decl_cmp but using the
1740 pointer operator in resort_data. */
1743 resort_field_decl_cmp (const void* x_p, const void* y_p)
1745 const tree *const x = x_p;
1746 const tree *const y = y_p;
1748 if (DECL_NAME (*x) == DECL_NAME (*y))
1749 /* A nontype is "greater" than a type. */
1750 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1751 if (DECL_NAME (*x) == NULL_TREE)
1753 if (DECL_NAME (*y) == NULL_TREE)
1756 tree d1 = DECL_NAME (*x);
1757 tree d2 = DECL_NAME (*y);
1758 resort_data.new_value (&d1, resort_data.cookie);
1759 resort_data.new_value (&d2, resort_data.cookie);
1766 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
1769 resort_sorted_fields (void* obj,
1770 void* orig_obj ATTRIBUTE_UNUSED ,
1771 gt_pointer_operator new_value,
1775 resort_data.new_value = new_value;
1776 resort_data.cookie = cookie;
1777 qsort (&TREE_VEC_ELT (sf, 0), TREE_VEC_LENGTH (sf), sizeof (tree),
1778 resort_field_decl_cmp);
1781 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1784 method_name_cmp (const void* m1_p, const void* m2_p)
1786 const tree *const m1 = m1_p;
1787 const tree *const m2 = m2_p;
1789 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1791 if (*m1 == NULL_TREE)
1793 if (*m2 == NULL_TREE)
1795 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1800 /* This routine compares two fields like method_name_cmp but using the
1801 pointer operator in resort_field_decl_data. */
1804 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1806 const tree *const m1 = m1_p;
1807 const tree *const m2 = m2_p;
1808 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1810 if (*m1 == NULL_TREE)
1812 if (*m2 == NULL_TREE)
1815 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1816 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1817 resort_data.new_value (&d1, resort_data.cookie);
1818 resort_data.new_value (&d2, resort_data.cookie);
1825 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1828 resort_type_method_vec (void* obj,
1829 void* orig_obj ATTRIBUTE_UNUSED ,
1830 gt_pointer_operator new_value,
1833 tree method_vec = obj;
1834 int len = TREE_VEC_LENGTH (method_vec);
1837 /* The type conversion ops have to live at the front of the vec, so we
1839 for (slot = 2; slot < len; ++slot)
1841 tree fn = TREE_VEC_ELT (method_vec, slot);
1843 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1848 resort_data.new_value = new_value;
1849 resort_data.cookie = cookie;
1850 qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
1851 resort_method_name_cmp);
1855 /* Warn about duplicate methods in fn_fields. Also compact method
1856 lists so that lookup can be made faster.
1858 Data Structure: List of method lists. The outer list is a
1859 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1860 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1861 links the entire list of methods for TYPE_METHODS. Friends are
1862 chained in the same way as member functions (? TREE_CHAIN or
1863 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1864 list. That allows them to be quickly deleted, and requires no
1867 Sort methods that are not special (i.e., constructors, destructors,
1868 and type conversion operators) so that we can find them faster in
1872 finish_struct_methods (tree t)
1878 if (!TYPE_METHODS (t))
1880 /* Clear these for safety; perhaps some parsing error could set
1881 these incorrectly. */
1882 TYPE_HAS_CONSTRUCTOR (t) = 0;
1883 TYPE_HAS_DESTRUCTOR (t) = 0;
1884 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
1888 method_vec = CLASSTYPE_METHOD_VEC (t);
1889 my_friendly_assert (method_vec != NULL_TREE, 19991215);
1890 len = TREE_VEC_LENGTH (method_vec);
1892 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1893 and the next few with type conversion operators (if any). */
1894 for (fn_fields = TYPE_METHODS (t); fn_fields;
1895 fn_fields = TREE_CHAIN (fn_fields))
1896 /* Clear out this flag. */
1897 DECL_IN_AGGR_P (fn_fields) = 0;
1899 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
1900 /* We thought there was a destructor, but there wasn't. Some
1901 parse errors cause this anomalous situation. */
1902 TYPE_HAS_DESTRUCTOR (t) = 0;
1904 /* Issue warnings about private constructors and such. If there are
1905 no methods, then some public defaults are generated. */
1906 maybe_warn_about_overly_private_class (t);
1908 /* Now sort the methods. */
1909 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
1911 TREE_VEC_LENGTH (method_vec) = len;
1913 /* The type conversion ops have to live at the front of the vec, so we
1915 for (slot = 2; slot < len; ++slot)
1917 tree fn = TREE_VEC_ELT (method_vec, slot);
1919 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1923 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
1927 /* Make BINFO's vtable have N entries, including RTTI entries,
1928 vbase and vcall offsets, etc. Set its type and call the backend
1932 layout_vtable_decl (tree binfo, int n)
1937 atype = build_cplus_array_type (vtable_entry_type,
1938 build_index_type (size_int (n - 1)));
1939 layout_type (atype);
1941 /* We may have to grow the vtable. */
1942 vtable = get_vtbl_decl_for_binfo (binfo);
1943 if (!same_type_p (TREE_TYPE (vtable), atype))
1945 TREE_TYPE (vtable) = atype;
1946 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1947 layout_decl (vtable, 0);
1951 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1952 have the same signature. */
1955 same_signature_p (tree fndecl, tree base_fndecl)
1957 /* One destructor overrides another if they are the same kind of
1959 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1960 && special_function_p (base_fndecl) == special_function_p (fndecl))
1962 /* But a non-destructor never overrides a destructor, nor vice
1963 versa, nor do different kinds of destructors override
1964 one-another. For example, a complete object destructor does not
1965 override a deleting destructor. */
1966 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1969 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
1971 tree types, base_types;
1972 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1973 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1974 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
1975 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
1976 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1982 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1986 base_derived_from (tree derived, tree base)
1990 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1992 if (probe == derived)
1994 else if (TREE_VIA_VIRTUAL (probe))
1995 /* If we meet a virtual base, we can't follow the inheritance
1996 any more. See if the complete type of DERIVED contains
1997 such a virtual base. */
1998 return purpose_member (BINFO_TYPE (probe),
1999 CLASSTYPE_VBASECLASSES (BINFO_TYPE (derived)))
2005 typedef struct find_final_overrider_data_s {
2006 /* The function for which we are trying to find a final overrider. */
2008 /* The base class in which the function was declared. */
2009 tree declaring_base;
2010 /* The most derived class in the hierarchy. */
2011 tree most_derived_type;
2012 /* The candidate overriders. */
2014 /* Binfos which inherited virtually on the currrent path. */
2016 } find_final_overrider_data;
2018 /* Called from find_final_overrider via dfs_walk. */
2021 dfs_find_final_overrider (tree binfo, void* data)
2023 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2025 if (binfo == ffod->declaring_base)
2027 /* We've found a path to the declaring base. Walk the path from
2028 derived to base, looking for an overrider for FN. */
2029 tree path, probe, vpath;
2031 /* Build the path, using the inheritance chain and record of
2032 virtual inheritance. */
2033 for (path = NULL_TREE, probe = binfo, vpath = ffod->vpath;;)
2035 path = tree_cons (NULL_TREE, probe, path);
2036 if (same_type_p (BINFO_TYPE (probe), ffod->most_derived_type))
2038 if (TREE_VIA_VIRTUAL (probe))
2040 probe = TREE_VALUE (vpath);
2041 vpath = TREE_CHAIN (vpath);
2044 probe = BINFO_INHERITANCE_CHAIN (probe);
2046 /* Now walk path, looking for overrides. */
2047 for (; path; path = TREE_CHAIN (path))
2049 tree method = look_for_overrides_here
2050 (BINFO_TYPE (TREE_VALUE (path)), ffod->fn);
2054 tree *candidate = &ffod->candidates;
2055 path = TREE_VALUE (path);
2057 /* Remove any candidates overridden by this new function. */
2060 /* If *CANDIDATE overrides METHOD, then METHOD
2061 cannot override anything else on the list. */
2062 if (base_derived_from (TREE_VALUE (*candidate), path))
2064 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2065 if (base_derived_from (path, TREE_VALUE (*candidate)))
2066 *candidate = TREE_CHAIN (*candidate);
2068 candidate = &TREE_CHAIN (*candidate);
2071 /* Add the new function. */
2072 ffod->candidates = tree_cons (method, path, ffod->candidates);
2082 dfs_find_final_overrider_q (tree derived, int ix, void *data)
2084 tree binfo = BINFO_BASETYPE (derived, ix);
2085 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2087 if (TREE_VIA_VIRTUAL (binfo))
2088 ffod->vpath = tree_cons (NULL_TREE, derived, ffod->vpath);
2094 dfs_find_final_overrider_post (tree binfo, void *data)
2096 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2098 if (TREE_VIA_VIRTUAL (binfo) && TREE_CHAIN (ffod->vpath))
2099 ffod->vpath = TREE_CHAIN (ffod->vpath);
2104 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2105 FN and whose TREE_VALUE is the binfo for the base where the
2106 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2107 DERIVED) is the base object in which FN is declared. */
2110 find_final_overrider (tree derived, tree binfo, tree fn)
2112 find_final_overrider_data ffod;
2114 /* Getting this right is a little tricky. This is valid:
2116 struct S { virtual void f (); };
2117 struct T { virtual void f (); };
2118 struct U : public S, public T { };
2120 even though calling `f' in `U' is ambiguous. But,
2122 struct R { virtual void f(); };
2123 struct S : virtual public R { virtual void f (); };
2124 struct T : virtual public R { virtual void f (); };
2125 struct U : public S, public T { };
2127 is not -- there's no way to decide whether to put `S::f' or
2128 `T::f' in the vtable for `R'.
2130 The solution is to look at all paths to BINFO. If we find
2131 different overriders along any two, then there is a problem. */
2132 if (DECL_THUNK_P (fn))
2133 fn = THUNK_TARGET (fn);
2136 ffod.declaring_base = binfo;
2137 ffod.most_derived_type = BINFO_TYPE (derived);
2138 ffod.candidates = NULL_TREE;
2139 ffod.vpath = NULL_TREE;
2141 dfs_walk_real (derived,
2142 dfs_find_final_overrider,
2143 dfs_find_final_overrider_post,
2144 dfs_find_final_overrider_q,
2147 /* If there was no winner, issue an error message. */
2148 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2150 error ("no unique final overrider for `%D' in `%T'", fn,
2151 BINFO_TYPE (derived));
2152 return error_mark_node;
2155 return ffod.candidates;
2158 /* Return the index of the vcall offset for FN when TYPE is used as a
2162 get_vcall_index (tree fn, tree type)
2166 for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2167 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2168 || same_signature_p (fn, TREE_PURPOSE (v)))
2171 /* There should always be an appropriate index. */
2172 my_friendly_assert (v, 20021103);
2174 return TREE_VALUE (v);
2177 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2178 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
2179 corresponding position in the BINFO_VIRTUALS list. */
2182 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2190 tree overrider_fn, overrider_target;
2191 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2192 tree over_return, base_return;
2195 /* Find the nearest primary base (possibly binfo itself) which defines
2196 this function; this is the class the caller will convert to when
2197 calling FN through BINFO. */
2198 for (b = binfo; ; b = get_primary_binfo (b))
2200 my_friendly_assert (b, 20021227);
2201 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2204 /* The nearest definition is from a lost primary. */
2205 if (BINFO_LOST_PRIMARY_P (b))
2210 /* Find the final overrider. */
2211 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2212 if (overrider == error_mark_node)
2214 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2216 /* Check for adjusting covariant return types. */
2217 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2218 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2220 if (POINTER_TYPE_P (over_return)
2221 && TREE_CODE (over_return) == TREE_CODE (base_return)
2222 && CLASS_TYPE_P (TREE_TYPE (over_return))
2223 && CLASS_TYPE_P (TREE_TYPE (base_return)))
2225 /* If FN is a covariant thunk, we must figure out the adjustment
2226 to the final base FN was converting to. As OVERRIDER_TARGET might
2227 also be converting to the return type of FN, we have to
2228 combine the two conversions here. */
2229 tree fixed_offset, virtual_offset;
2231 if (DECL_THUNK_P (fn))
2233 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2234 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2237 fixed_offset = virtual_offset = NULL_TREE;
2239 if (!virtual_offset)
2241 /* There was no existing virtual thunk (which takes
2246 thunk_binfo = lookup_base (TREE_TYPE (over_return),
2247 TREE_TYPE (base_return),
2248 ba_check | ba_quiet, &kind);
2250 if (thunk_binfo && (kind == bk_via_virtual
2251 || !BINFO_OFFSET_ZEROP (thunk_binfo)))
2253 tree offset = BINFO_OFFSET (thunk_binfo);
2255 if (kind == bk_via_virtual)
2257 /* We convert via virtual base. Find the virtual
2258 base and adjust the fixed offset to be from there. */
2259 while (!TREE_VIA_VIRTUAL (thunk_binfo))
2260 thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
2262 virtual_offset = thunk_binfo;
2263 offset = size_binop (MINUS_EXPR, offset,
2264 BINFO_OFFSET (virtual_offset));
2267 /* There was an existing fixed offset, this must be
2268 from the base just converted to, and the base the
2269 FN was thunking to. */
2270 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2272 fixed_offset = offset;
2276 if (fixed_offset || virtual_offset)
2277 /* Replace the overriding function with a covariant thunk. We
2278 will emit the overriding function in its own slot as
2280 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2281 fixed_offset, virtual_offset);
2284 my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
2286 /* Assume that we will produce a thunk that convert all the way to
2287 the final overrider, and not to an intermediate virtual base. */
2288 virtual_base = NULL_TREE;
2290 /* See if we can convert to an intermediate virtual base first, and then
2291 use the vcall offset located there to finish the conversion. */
2292 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2294 /* If we find the final overrider, then we can stop
2296 if (same_type_p (BINFO_TYPE (b),
2297 BINFO_TYPE (TREE_VALUE (overrider))))
2300 /* If we find a virtual base, and we haven't yet found the
2301 overrider, then there is a virtual base between the
2302 declaring base (first_defn) and the final overrider. */
2303 if (TREE_VIA_VIRTUAL (b))
2310 if (overrider_fn != overrider_target && !virtual_base)
2312 /* The ABI specifies that a covariant thunk includes a mangling
2313 for a this pointer adjustment. This-adjusting thunks that
2314 override a function from a virtual base have a vcall
2315 adjustment. When the virtual base in question is a primary
2316 virtual base, we know the adjustments are zero, (and in the
2317 non-covariant case, we would not use the thunk).
2318 Unfortunately we didn't notice this could happen, when
2319 designing the ABI and so never mandated that such a covariant
2320 thunk should be emitted. Because we must use the ABI mandated
2321 name, we must continue searching from the binfo where we
2322 found the most recent definition of the function, towards the
2323 primary binfo which first introduced the function into the
2324 vtable. If that enters a virtual base, we must use a vcall
2325 this-adjusting thunk. Bleah! */
2328 for (probe = first_defn; (probe = get_primary_binfo (probe));)
2330 if (TREE_VIA_VIRTUAL (probe))
2331 virtual_base = probe;
2332 if ((unsigned) list_length (BINFO_VIRTUALS (probe)) <= ix)
2336 /* Even if we find a virtual base, the correct delta is
2337 between the overrider and the binfo we're building a vtable
2339 goto virtual_covariant;
2342 /* Compute the constant adjustment to the `this' pointer. The
2343 `this' pointer, when this function is called, will point at BINFO
2344 (or one of its primary bases, which are at the same offset). */
2346 /* The `this' pointer needs to be adjusted from the declaration to
2347 the nearest virtual base. */
2348 delta = size_diffop (BINFO_OFFSET (virtual_base),
2349 BINFO_OFFSET (first_defn));
2351 /* If the nearest definition is in a lost primary, we don't need an
2352 entry in our vtable. Except possibly in a constructor vtable,
2353 if we happen to get our primary back. In that case, the offset
2354 will be zero, as it will be a primary base. */
2355 delta = size_zero_node;
2357 /* The `this' pointer needs to be adjusted from pointing to
2358 BINFO to pointing at the base where the final overrider
2361 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2362 BINFO_OFFSET (binfo));
2364 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2367 BV_VCALL_INDEX (*virtuals)
2368 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2371 /* Called from modify_all_vtables via dfs_walk. */
2374 dfs_modify_vtables (tree binfo, void* data)
2376 if (/* There's no need to modify the vtable for a non-virtual
2377 primary base; we're not going to use that vtable anyhow.
2378 We do still need to do this for virtual primary bases, as they
2379 could become non-primary in a construction vtable. */
2380 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2381 /* Similarly, a base without a vtable needs no modification. */
2382 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2384 tree t = (tree) data;
2389 make_new_vtable (t, binfo);
2391 /* Now, go through each of the virtual functions in the virtual
2392 function table for BINFO. Find the final overrider, and
2393 update the BINFO_VIRTUALS list appropriately. */
2394 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2395 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2397 ix++, virtuals = TREE_CHAIN (virtuals),
2398 old_virtuals = TREE_CHAIN (old_virtuals))
2399 update_vtable_entry_for_fn (t,
2401 BV_FN (old_virtuals),
2405 BINFO_MARKED (binfo) = 1;
2410 /* Update all of the primary and secondary vtables for T. Create new
2411 vtables as required, and initialize their RTTI information. Each
2412 of the functions in VIRTUALS is declared in T and may override a
2413 virtual function from a base class; find and modify the appropriate
2414 entries to point to the overriding functions. Returns a list, in
2415 declaration order, of the virtual functions that are declared in T,
2416 but do not appear in the primary base class vtable, and which
2417 should therefore be appended to the end of the vtable for T. */
2420 modify_all_vtables (tree t, tree virtuals)
2422 tree binfo = TYPE_BINFO (t);
2425 /* Update all of the vtables. */
2426 dfs_walk (binfo, dfs_modify_vtables, unmarkedp, t);
2427 dfs_walk (binfo, dfs_unmark, markedp, t);
2429 /* Add virtual functions not already in our primary vtable. These
2430 will be both those introduced by this class, and those overridden
2431 from secondary bases. It does not include virtuals merely
2432 inherited from secondary bases. */
2433 for (fnsp = &virtuals; *fnsp; )
2435 tree fn = TREE_VALUE (*fnsp);
2437 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2438 || DECL_VINDEX (fn) == error_mark_node)
2440 /* We don't need to adjust the `this' pointer when
2441 calling this function. */
2442 BV_DELTA (*fnsp) = integer_zero_node;
2443 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2445 /* This is a function not already in our vtable. Keep it. */
2446 fnsp = &TREE_CHAIN (*fnsp);
2449 /* We've already got an entry for this function. Skip it. */
2450 *fnsp = TREE_CHAIN (*fnsp);
2456 /* Get the base virtual function declarations in T that have the
2460 get_basefndecls (tree name, tree t)
2463 tree base_fndecls = NULL_TREE;
2464 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2467 /* Find virtual functions in T with the indicated NAME. */
2468 i = lookup_fnfields_1 (t, name);
2470 for (methods = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), i);
2472 methods = OVL_NEXT (methods))
2474 tree method = OVL_CURRENT (methods);
2476 if (TREE_CODE (method) == FUNCTION_DECL
2477 && DECL_VINDEX (method))
2478 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2482 return base_fndecls;
2484 for (i = 0; i < n_baseclasses; i++)
2486 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2487 base_fndecls = chainon (get_basefndecls (name, basetype),
2491 return base_fndecls;
2494 /* If this declaration supersedes the declaration of
2495 a method declared virtual in the base class, then
2496 mark this field as being virtual as well. */
2499 check_for_override (tree decl, tree ctype)
2501 if (TREE_CODE (decl) == TEMPLATE_DECL)
2502 /* In [temp.mem] we have:
2504 A specialization of a member function template does not
2505 override a virtual function from a base class. */
2507 if ((DECL_DESTRUCTOR_P (decl)
2508 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2509 && look_for_overrides (ctype, decl)
2510 && !DECL_STATIC_FUNCTION_P (decl))
2511 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2512 the error_mark_node so that we know it is an overriding
2514 DECL_VINDEX (decl) = decl;
2516 if (DECL_VIRTUAL_P (decl))
2518 if (!DECL_VINDEX (decl))
2519 DECL_VINDEX (decl) = error_mark_node;
2520 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2524 /* Warn about hidden virtual functions that are not overridden in t.
2525 We know that constructors and destructors don't apply. */
2528 warn_hidden (tree t)
2530 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2531 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2534 /* We go through each separately named virtual function. */
2535 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2543 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2544 have the same name. Figure out what name that is. */
2545 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2546 /* There are no possibly hidden functions yet. */
2547 base_fndecls = NULL_TREE;
2548 /* Iterate through all of the base classes looking for possibly
2549 hidden functions. */
2550 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2552 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2553 base_fndecls = chainon (get_basefndecls (name, basetype),
2557 /* If there are no functions to hide, continue. */
2561 /* Remove any overridden functions. */
2562 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2564 fndecl = OVL_CURRENT (fns);
2565 if (DECL_VINDEX (fndecl))
2567 tree *prev = &base_fndecls;
2570 /* If the method from the base class has the same
2571 signature as the method from the derived class, it
2572 has been overridden. */
2573 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2574 *prev = TREE_CHAIN (*prev);
2576 prev = &TREE_CHAIN (*prev);
2580 /* Now give a warning for all base functions without overriders,
2581 as they are hidden. */
2582 while (base_fndecls)
2584 /* Here we know it is a hider, and no overrider exists. */
2585 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2586 cp_warning_at (" by `%D'",
2587 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2588 base_fndecls = TREE_CHAIN (base_fndecls);
2593 /* Check for things that are invalid. There are probably plenty of other
2594 things we should check for also. */
2597 finish_struct_anon (tree t)
2601 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2603 if (TREE_STATIC (field))
2605 if (TREE_CODE (field) != FIELD_DECL)
2608 if (DECL_NAME (field) == NULL_TREE
2609 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2611 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2612 for (; elt; elt = TREE_CHAIN (elt))
2614 /* We're generally only interested in entities the user
2615 declared, but we also find nested classes by noticing
2616 the TYPE_DECL that we create implicitly. You're
2617 allowed to put one anonymous union inside another,
2618 though, so we explicitly tolerate that. We use
2619 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2620 we also allow unnamed types used for defining fields. */
2621 if (DECL_ARTIFICIAL (elt)
2622 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2623 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2626 if (constructor_name_p (DECL_NAME (elt), t))
2627 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2630 if (TREE_CODE (elt) != FIELD_DECL)
2632 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2637 if (TREE_PRIVATE (elt))
2638 cp_pedwarn_at ("private member `%#D' in anonymous union",
2640 else if (TREE_PROTECTED (elt))
2641 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2644 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2645 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2651 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2652 will be used later during class template instantiation.
2653 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2654 a non-static member data (FIELD_DECL), a member function
2655 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2656 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2657 When FRIEND_P is nonzero, T is either a friend class
2658 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2659 (FUNCTION_DECL, TEMPLATE_DECL). */
2662 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2664 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2665 if (CLASSTYPE_TEMPLATE_INFO (type))
2666 CLASSTYPE_DECL_LIST (type)
2667 = tree_cons (friend_p ? NULL_TREE : type,
2668 t, CLASSTYPE_DECL_LIST (type));
2671 /* Create default constructors, assignment operators, and so forth for
2672 the type indicated by T, if they are needed.
2673 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2674 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2675 class cannot have a default constructor, copy constructor taking a
2676 const reference argument, or an assignment operator taking a const
2677 reference, respectively. If a virtual destructor is created, its
2678 DECL is returned; otherwise the return value is NULL_TREE. */
2681 add_implicitly_declared_members (tree t,
2682 int cant_have_default_ctor,
2683 int cant_have_const_cctor,
2684 int cant_have_const_assignment)
2687 tree implicit_fns = NULL_TREE;
2688 tree virtual_dtor = NULL_TREE;
2691 ++adding_implicit_members;
2694 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2696 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2697 check_for_override (default_fn, t);
2699 /* If we couldn't make it work, then pretend we didn't need it. */
2700 if (default_fn == void_type_node)
2701 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2704 TREE_CHAIN (default_fn) = implicit_fns;
2705 implicit_fns = default_fn;
2707 if (DECL_VINDEX (default_fn))
2708 virtual_dtor = default_fn;
2712 /* Any non-implicit destructor is non-trivial. */
2713 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2715 /* Default constructor. */
2716 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2718 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2719 TREE_CHAIN (default_fn) = implicit_fns;
2720 implicit_fns = default_fn;
2723 /* Copy constructor. */
2724 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2726 /* ARM 12.18: You get either X(X&) or X(const X&), but
2729 = implicitly_declare_fn (sfk_copy_constructor, t,
2730 /*const_p=*/!cant_have_const_cctor);
2731 TREE_CHAIN (default_fn) = implicit_fns;
2732 implicit_fns = default_fn;
2735 /* Assignment operator. */
2736 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2739 = implicitly_declare_fn (sfk_assignment_operator, t,
2740 /*const_p=*/!cant_have_const_assignment);
2741 TREE_CHAIN (default_fn) = implicit_fns;
2742 implicit_fns = default_fn;
2745 /* Now, hook all of the new functions on to TYPE_METHODS,
2746 and add them to the CLASSTYPE_METHOD_VEC. */
2747 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2749 add_method (t, *f, /*error_p=*/0);
2750 maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2752 if (abi_version_at_least (2))
2753 /* G++ 3.2 put the implicit destructor at the *beginning* of the
2754 list, which cause the destructor to be emitted in an incorrect
2755 location in the vtable. */
2756 TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
2759 if (warn_abi && virtual_dtor)
2760 warning ("vtable layout for class `%T' may not be ABI-compliant "
2761 "and may change in a future version of GCC due to implicit "
2762 "virtual destructor",
2764 *f = TYPE_METHODS (t);
2765 TYPE_METHODS (t) = implicit_fns;
2768 --adding_implicit_members;
2771 /* Subroutine of finish_struct_1. Recursively count the number of fields
2772 in TYPE, including anonymous union members. */
2775 count_fields (tree fields)
2779 for (x = fields; x; x = TREE_CHAIN (x))
2781 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2782 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2789 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2790 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2793 add_fields_to_vec (tree fields, tree field_vec, int idx)
2796 for (x = fields; x; x = TREE_CHAIN (x))
2798 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2799 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2801 TREE_VEC_ELT (field_vec, idx++) = x;
2806 /* FIELD is a bit-field. We are finishing the processing for its
2807 enclosing type. Issue any appropriate messages and set appropriate
2811 check_bitfield_decl (tree field)
2813 tree type = TREE_TYPE (field);
2816 /* Detect invalid bit-field type. */
2817 if (DECL_INITIAL (field)
2818 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2820 cp_error_at ("bit-field `%#D' with non-integral type", field);
2821 w = error_mark_node;
2824 /* Detect and ignore out of range field width. */
2825 if (DECL_INITIAL (field))
2827 w = DECL_INITIAL (field);
2829 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2832 /* detect invalid field size. */
2833 if (TREE_CODE (w) == CONST_DECL)
2834 w = DECL_INITIAL (w);
2836 w = decl_constant_value (w);
2838 if (TREE_CODE (w) != INTEGER_CST)
2840 cp_error_at ("bit-field `%D' width not an integer constant",
2842 w = error_mark_node;
2844 else if (tree_int_cst_sgn (w) < 0)
2846 cp_error_at ("negative width in bit-field `%D'", field);
2847 w = error_mark_node;
2849 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2851 cp_error_at ("zero width for bit-field `%D'", field);
2852 w = error_mark_node;
2854 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2855 && TREE_CODE (type) != ENUMERAL_TYPE
2856 && TREE_CODE (type) != BOOLEAN_TYPE)
2857 cp_warning_at ("width of `%D' exceeds its type", field);
2858 else if (TREE_CODE (type) == ENUMERAL_TYPE
2859 && (0 > compare_tree_int (w,
2860 min_precision (TYPE_MIN_VALUE (type),
2861 TREE_UNSIGNED (type)))
2862 || 0 > compare_tree_int (w,
2864 (TYPE_MAX_VALUE (type),
2865 TREE_UNSIGNED (type)))))
2866 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
2870 /* Remove the bit-field width indicator so that the rest of the
2871 compiler does not treat that value as an initializer. */
2872 DECL_INITIAL (field) = NULL_TREE;
2874 if (w != error_mark_node)
2876 DECL_SIZE (field) = convert (bitsizetype, w);
2877 DECL_BIT_FIELD (field) = 1;
2881 /* Non-bit-fields are aligned for their type. */
2882 DECL_BIT_FIELD (field) = 0;
2883 CLEAR_DECL_C_BIT_FIELD (field);
2887 /* FIELD is a non bit-field. We are finishing the processing for its
2888 enclosing type T. Issue any appropriate messages and set appropriate
2892 check_field_decl (tree field,
2894 int* cant_have_const_ctor,
2895 int* cant_have_default_ctor,
2896 int* no_const_asn_ref,
2897 int* any_default_members)
2899 tree type = strip_array_types (TREE_TYPE (field));
2901 /* An anonymous union cannot contain any fields which would change
2902 the settings of CANT_HAVE_CONST_CTOR and friends. */
2903 if (ANON_UNION_TYPE_P (type))
2905 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
2906 structs. So, we recurse through their fields here. */
2907 else if (ANON_AGGR_TYPE_P (type))
2911 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2912 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2913 check_field_decl (fields, t, cant_have_const_ctor,
2914 cant_have_default_ctor, no_const_asn_ref,
2915 any_default_members);
2917 /* Check members with class type for constructors, destructors,
2919 else if (CLASS_TYPE_P (type))
2921 /* Never let anything with uninheritable virtuals
2922 make it through without complaint. */
2923 abstract_virtuals_error (field, type);
2925 if (TREE_CODE (t) == UNION_TYPE)
2927 if (TYPE_NEEDS_CONSTRUCTING (type))
2928 cp_error_at ("member `%#D' with constructor not allowed in union",
2930 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2931 cp_error_at ("member `%#D' with destructor not allowed in union",
2933 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2934 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
2939 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2940 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2941 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2942 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
2943 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
2946 if (!TYPE_HAS_CONST_INIT_REF (type))
2947 *cant_have_const_ctor = 1;
2949 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
2950 *no_const_asn_ref = 1;
2952 if (TYPE_HAS_CONSTRUCTOR (type)
2953 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2954 *cant_have_default_ctor = 1;
2956 if (DECL_INITIAL (field) != NULL_TREE)
2958 /* `build_class_init_list' does not recognize
2960 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
2961 cp_error_at ("multiple fields in union `%T' initialized");
2962 *any_default_members = 1;
2966 /* Check the data members (both static and non-static), class-scoped
2967 typedefs, etc., appearing in the declaration of T. Issue
2968 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
2969 declaration order) of access declarations; each TREE_VALUE in this
2970 list is a USING_DECL.
2972 In addition, set the following flags:
2975 The class is empty, i.e., contains no non-static data members.
2977 CANT_HAVE_DEFAULT_CTOR_P
2978 This class cannot have an implicitly generated default
2981 CANT_HAVE_CONST_CTOR_P
2982 This class cannot have an implicitly generated copy constructor
2983 taking a const reference.
2985 CANT_HAVE_CONST_ASN_REF
2986 This class cannot have an implicitly generated assignment
2987 operator taking a const reference.
2989 All of these flags should be initialized before calling this
2992 Returns a pointer to the end of the TYPE_FIELDs chain; additional
2993 fields can be added by adding to this chain. */
2996 check_field_decls (tree t, tree *access_decls,
2997 int *cant_have_default_ctor_p,
2998 int *cant_have_const_ctor_p,
2999 int *no_const_asn_ref_p)
3004 int any_default_members;
3006 /* First, delete any duplicate fields. */
3007 delete_duplicate_fields (TYPE_FIELDS (t));
3009 /* Assume there are no access declarations. */
3010 *access_decls = NULL_TREE;
3011 /* Assume this class has no pointer members. */
3013 /* Assume none of the members of this class have default
3015 any_default_members = 0;
3017 for (field = &TYPE_FIELDS (t); *field; field = next)
3020 tree type = TREE_TYPE (x);
3022 next = &TREE_CHAIN (x);
3024 if (TREE_CODE (x) == FIELD_DECL)
3026 DECL_PACKED (x) |= TYPE_PACKED (t);
3028 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3029 /* We don't treat zero-width bitfields as making a class
3036 /* The class is non-empty. */
3037 CLASSTYPE_EMPTY_P (t) = 0;
3038 /* The class is not even nearly empty. */
3039 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3040 /* If one of the data members contains an empty class,
3042 element_type = strip_array_types (type);
3043 if (CLASS_TYPE_P (element_type)
3044 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3045 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3049 if (TREE_CODE (x) == USING_DECL)
3051 /* Prune the access declaration from the list of fields. */
3052 *field = TREE_CHAIN (x);
3054 /* Save the access declarations for our caller. */
3055 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3057 /* Since we've reset *FIELD there's no reason to skip to the
3063 if (TREE_CODE (x) == TYPE_DECL
3064 || TREE_CODE (x) == TEMPLATE_DECL)
3067 /* If we've gotten this far, it's a data member, possibly static,
3068 or an enumerator. */
3070 DECL_CONTEXT (x) = t;
3072 /* ``A local class cannot have static data members.'' ARM 9.4 */
3073 if (current_function_decl && TREE_STATIC (x))
3074 cp_error_at ("field `%D' in local class cannot be static", x);
3076 /* Perform error checking that did not get done in
3078 if (TREE_CODE (type) == FUNCTION_TYPE)
3080 cp_error_at ("field `%D' invalidly declared function type",
3082 type = build_pointer_type (type);
3083 TREE_TYPE (x) = type;
3085 else if (TREE_CODE (type) == METHOD_TYPE)
3087 cp_error_at ("field `%D' invalidly declared method type", x);
3088 type = build_pointer_type (type);
3089 TREE_TYPE (x) = type;
3091 else if (TREE_CODE (type) == OFFSET_TYPE)
3093 cp_error_at ("field `%D' invalidly declared offset type", x);
3094 type = build_pointer_type (type);
3095 TREE_TYPE (x) = type;
3098 if (type == error_mark_node)
3101 /* When this goes into scope, it will be a non-local reference. */
3102 DECL_NONLOCAL (x) = 1;
3104 if (TREE_CODE (x) == CONST_DECL)
3107 if (TREE_CODE (x) == VAR_DECL)
3109 if (TREE_CODE (t) == UNION_TYPE)
3110 /* Unions cannot have static members. */
3111 cp_error_at ("field `%D' declared static in union", x);
3116 /* Now it can only be a FIELD_DECL. */
3118 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3119 CLASSTYPE_NON_AGGREGATE (t) = 1;
3121 /* If this is of reference type, check if it needs an init.
3122 Also do a little ANSI jig if necessary. */
3123 if (TREE_CODE (type) == REFERENCE_TYPE)
3125 CLASSTYPE_NON_POD_P (t) = 1;
3126 if (DECL_INITIAL (x) == NULL_TREE)
3127 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3129 /* ARM $12.6.2: [A member initializer list] (or, for an
3130 aggregate, initialization by a brace-enclosed list) is the
3131 only way to initialize nonstatic const and reference
3133 *cant_have_default_ctor_p = 1;
3134 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3136 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3138 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3141 type = strip_array_types (type);
3143 if (TREE_CODE (type) == POINTER_TYPE)
3146 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3147 CLASSTYPE_HAS_MUTABLE (t) = 1;
3149 if (! pod_type_p (type))
3150 /* DR 148 now allows pointers to members (which are POD themselves),
3151 to be allowed in POD structs. */
3152 CLASSTYPE_NON_POD_P (t) = 1;
3154 if (! zero_init_p (type))
3155 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3157 /* If any field is const, the structure type is pseudo-const. */
3158 if (CP_TYPE_CONST_P (type))
3160 C_TYPE_FIELDS_READONLY (t) = 1;
3161 if (DECL_INITIAL (x) == NULL_TREE)
3162 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3164 /* ARM $12.6.2: [A member initializer list] (or, for an
3165 aggregate, initialization by a brace-enclosed list) is the
3166 only way to initialize nonstatic const and reference
3168 *cant_have_default_ctor_p = 1;
3169 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3171 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3173 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3175 /* A field that is pseudo-const makes the structure likewise. */
3176 else if (CLASS_TYPE_P (type))
3178 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3179 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3180 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3181 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3184 /* Core issue 80: A nonstatic data member is required to have a
3185 different name from the class iff the class has a
3186 user-defined constructor. */
3187 if (constructor_name_p (x, t) && TYPE_HAS_CONSTRUCTOR (t))
3188 cp_pedwarn_at ("field `%#D' with same name as class", x);
3190 /* We set DECL_C_BIT_FIELD in grokbitfield.
3191 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3192 if (DECL_C_BIT_FIELD (x))
3193 check_bitfield_decl (x);
3195 check_field_decl (x, t,
3196 cant_have_const_ctor_p,
3197 cant_have_default_ctor_p,
3199 &any_default_members);
3202 /* Effective C++ rule 11. */
3203 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3204 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3206 warning ("`%#T' has pointer data members", t);
3208 if (! TYPE_HAS_INIT_REF (t))
3210 warning (" but does not override `%T(const %T&)'", t, t);
3211 if (! TYPE_HAS_ASSIGN_REF (t))
3212 warning (" or `operator=(const %T&)'", t);
3214 else if (! TYPE_HAS_ASSIGN_REF (t))
3215 warning (" but does not override `operator=(const %T&)'", t);
3219 /* Check anonymous struct/anonymous union fields. */
3220 finish_struct_anon (t);
3222 /* We've built up the list of access declarations in reverse order.
3224 *access_decls = nreverse (*access_decls);
3227 /* If TYPE is an empty class type, records its OFFSET in the table of
3231 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3235 if (!is_empty_class (type))
3238 /* Record the location of this empty object in OFFSETS. */
3239 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3241 n = splay_tree_insert (offsets,
3242 (splay_tree_key) offset,
3243 (splay_tree_value) NULL_TREE);
3244 n->value = ((splay_tree_value)
3245 tree_cons (NULL_TREE,
3252 /* Returns nonzero if TYPE is an empty class type and there is
3253 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3256 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3261 if (!is_empty_class (type))
3264 /* Record the location of this empty object in OFFSETS. */
3265 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3269 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3270 if (same_type_p (TREE_VALUE (t), type))
3276 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3277 F for every subobject, passing it the type, offset, and table of
3278 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3281 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3282 than MAX_OFFSET will not be walked.
3284 If F returns a nonzero value, the traversal ceases, and that value
3285 is returned. Otherwise, returns zero. */
3288 walk_subobject_offsets (tree type,
3289 subobject_offset_fn f,
3296 tree type_binfo = NULL_TREE;
3298 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3300 if (max_offset && INT_CST_LT (max_offset, offset))
3305 if (abi_version_at_least (2))
3307 type = BINFO_TYPE (type);
3310 if (CLASS_TYPE_P (type))
3316 /* Avoid recursing into objects that are not interesting. */
3317 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3320 /* Record the location of TYPE. */
3321 r = (*f) (type, offset, offsets);
3325 /* Iterate through the direct base classes of TYPE. */
3327 type_binfo = TYPE_BINFO (type);
3328 for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
3332 binfo = BINFO_BASETYPE (type_binfo, i);
3334 if (abi_version_at_least (2)
3335 && TREE_VIA_VIRTUAL (binfo))
3339 && TREE_VIA_VIRTUAL (binfo)
3340 && !BINFO_PRIMARY_P (binfo))
3343 if (!abi_version_at_least (2))
3344 binfo_offset = size_binop (PLUS_EXPR,
3346 BINFO_OFFSET (binfo));
3350 /* We cannot rely on BINFO_OFFSET being set for the base
3351 class yet, but the offsets for direct non-virtual
3352 bases can be calculated by going back to the TYPE. */
3353 orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3354 binfo_offset = size_binop (PLUS_EXPR,
3356 BINFO_OFFSET (orig_binfo));
3359 r = walk_subobject_offsets (binfo,
3364 (abi_version_at_least (2)
3365 ? /*vbases_p=*/0 : vbases_p));
3370 if (abi_version_at_least (2))
3374 /* Iterate through the virtual base classes of TYPE. In G++
3375 3.2, we included virtual bases in the direct base class
3376 loop above, which results in incorrect results; the
3377 correct offsets for virtual bases are only known when
3378 working with the most derived type. */
3380 for (vbase = CLASSTYPE_VBASECLASSES (type);
3382 vbase = TREE_CHAIN (vbase))
3384 binfo = TREE_VALUE (vbase);
3385 r = walk_subobject_offsets (binfo,
3387 size_binop (PLUS_EXPR,
3389 BINFO_OFFSET (binfo)),
3398 /* We still have to walk the primary base, if it is
3399 virtual. (If it is non-virtual, then it was walked
3401 vbase = get_primary_binfo (type_binfo);
3402 if (vbase && TREE_VIA_VIRTUAL (vbase)
3403 && BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
3405 r = (walk_subobject_offsets
3407 offsets, max_offset, /*vbases_p=*/0));
3414 /* Iterate through the fields of TYPE. */
3415 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3416 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3420 if (abi_version_at_least (2))
3421 field_offset = byte_position (field);
3423 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3424 field_offset = DECL_FIELD_OFFSET (field);
3426 r = walk_subobject_offsets (TREE_TYPE (field),
3428 size_binop (PLUS_EXPR,
3438 else if (TREE_CODE (type) == ARRAY_TYPE)
3440 tree element_type = strip_array_types (type);
3441 tree domain = TYPE_DOMAIN (type);
3444 /* Avoid recursing into objects that are not interesting. */
3445 if (!CLASS_TYPE_P (element_type)
3446 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3449 /* Step through each of the elements in the array. */
3450 for (index = size_zero_node;
3451 /* G++ 3.2 had an off-by-one error here. */
3452 (abi_version_at_least (2)
3453 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3454 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3455 index = size_binop (PLUS_EXPR, index, size_one_node))
3457 r = walk_subobject_offsets (TREE_TYPE (type),
3465 offset = size_binop (PLUS_EXPR, offset,
3466 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3467 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3468 there's no point in iterating through the remaining
3469 elements of the array. */
3470 if (max_offset && INT_CST_LT (max_offset, offset))
3478 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3479 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3483 record_subobject_offsets (tree type,
3488 walk_subobject_offsets (type, record_subobject_offset, offset,
3489 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3492 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3493 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3494 virtual bases of TYPE are examined. */
3497 layout_conflict_p (tree type,
3502 splay_tree_node max_node;
3504 /* Get the node in OFFSETS that indicates the maximum offset where
3505 an empty subobject is located. */
3506 max_node = splay_tree_max (offsets);
3507 /* If there aren't any empty subobjects, then there's no point in
3508 performing this check. */
3512 return walk_subobject_offsets (type, check_subobject_offset, offset,
3513 offsets, (tree) (max_node->key),
3517 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3518 non-static data member of the type indicated by RLI. BINFO is the
3519 binfo corresponding to the base subobject, OFFSETS maps offsets to
3520 types already located at those offsets. This function determines
3521 the position of the DECL. */
3524 layout_nonempty_base_or_field (record_layout_info rli,
3529 tree offset = NULL_TREE;
3535 /* For the purposes of determining layout conflicts, we want to
3536 use the class type of BINFO; TREE_TYPE (DECL) will be the
3537 CLASSTYPE_AS_BASE version, which does not contain entries for
3538 zero-sized bases. */
3539 type = TREE_TYPE (binfo);
3544 type = TREE_TYPE (decl);
3548 /* Try to place the field. It may take more than one try if we have
3549 a hard time placing the field without putting two objects of the
3550 same type at the same address. */
3553 struct record_layout_info_s old_rli = *rli;
3555 /* Place this field. */
3556 place_field (rli, decl);
3557 offset = byte_position (decl);
3559 /* We have to check to see whether or not there is already
3560 something of the same type at the offset we're about to use.
3564 struct T : public S { int i; };
3565 struct U : public S, public T {};
3567 Here, we put S at offset zero in U. Then, we can't put T at
3568 offset zero -- its S component would be at the same address
3569 as the S we already allocated. So, we have to skip ahead.
3570 Since all data members, including those whose type is an
3571 empty class, have nonzero size, any overlap can happen only
3572 with a direct or indirect base-class -- it can't happen with
3574 /* G++ 3.2 did not check for overlaps when placing a non-empty
3576 if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3578 if (layout_conflict_p (field_p ? type : binfo, offset,
3581 /* Strip off the size allocated to this field. That puts us
3582 at the first place we could have put the field with
3583 proper alignment. */
3586 /* Bump up by the alignment required for the type. */
3588 = size_binop (PLUS_EXPR, rli->bitpos,
3590 ? CLASSTYPE_ALIGN (type)
3591 : TYPE_ALIGN (type)));
3592 normalize_rli (rli);
3595 /* There was no conflict. We're done laying out this field. */
3599 /* Now that we know where it will be placed, update its
3601 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3602 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3603 this point because their BINFO_OFFSET is copied from another
3604 hierarchy. Therefore, we may not need to add the entire
3606 propagate_binfo_offsets (binfo,
3607 size_diffop (convert (ssizetype, offset),
3609 BINFO_OFFSET (binfo))));
3612 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3615 empty_base_at_nonzero_offset_p (tree type,
3617 splay_tree offsets ATTRIBUTE_UNUSED)
3619 return is_empty_class (type) && !integer_zerop (offset);
3622 /* Layout the empty base BINFO. EOC indicates the byte currently just
3623 past the end of the class, and should be correctly aligned for a
3624 class of the type indicated by BINFO; OFFSETS gives the offsets of
3625 the empty bases allocated so far. T is the most derived
3626 type. Return nonzero iff we added it at the end. */
3629 layout_empty_base (tree binfo, tree eoc, splay_tree offsets)
3632 tree basetype = BINFO_TYPE (binfo);
3635 /* This routine should only be used for empty classes. */
3636 my_friendly_assert (is_empty_class (basetype), 20000321);
3637 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3639 if (abi_version_at_least (2))
3640 BINFO_OFFSET (binfo) = size_zero_node;
3641 if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3642 warning ("offset of empty base `%T' may not be ABI-compliant and may"
3643 "change in a future version of GCC",
3644 BINFO_TYPE (binfo));
3646 /* This is an empty base class. We first try to put it at offset
3648 if (layout_conflict_p (binfo,
3649 BINFO_OFFSET (binfo),
3653 /* That didn't work. Now, we move forward from the next
3654 available spot in the class. */
3656 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3659 if (!layout_conflict_p (binfo,
3660 BINFO_OFFSET (binfo),
3663 /* We finally found a spot where there's no overlap. */
3666 /* There's overlap here, too. Bump along to the next spot. */
3667 propagate_binfo_offsets (binfo, alignment);
3673 /* Layout the the base given by BINFO in the class indicated by RLI.
3674 *BASE_ALIGN is a running maximum of the alignments of
3675 any base class. OFFSETS gives the location of empty base
3676 subobjects. T is the most derived type. Return nonzero if the new
3677 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3678 *NEXT_FIELD, unless BINFO is for an empty base class.
3680 Returns the location at which the next field should be inserted. */
3683 build_base_field (record_layout_info rli, tree binfo,
3684 splay_tree offsets, tree *next_field)
3687 tree basetype = BINFO_TYPE (binfo);
3689 if (!COMPLETE_TYPE_P (basetype))
3690 /* This error is now reported in xref_tag, thus giving better
3691 location information. */
3694 /* Place the base class. */
3695 if (!is_empty_class (basetype))
3699 /* The containing class is non-empty because it has a non-empty
3701 CLASSTYPE_EMPTY_P (t) = 0;
3703 /* Create the FIELD_DECL. */
3704 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3705 DECL_ARTIFICIAL (decl) = 1;
3706 DECL_FIELD_CONTEXT (decl) = t;
3707 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3708 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3709 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3710 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3711 DECL_IGNORED_P (decl) = 1;
3713 /* Try to place the field. It may take more than one try if we
3714 have a hard time placing the field without putting two
3715 objects of the same type at the same address. */
3716 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3717 /* Add the new FIELD_DECL to the list of fields for T. */
3718 TREE_CHAIN (decl) = *next_field;
3720 next_field = &TREE_CHAIN (decl);
3727 /* On some platforms (ARM), even empty classes will not be
3729 eoc = round_up (rli_size_unit_so_far (rli),
3730 CLASSTYPE_ALIGN_UNIT (basetype));
3731 atend = layout_empty_base (binfo, eoc, offsets);
3732 /* A nearly-empty class "has no proper base class that is empty,
3733 not morally virtual, and at an offset other than zero." */
3734 if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3737 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3738 /* The check above (used in G++ 3.2) is insufficient because
3739 an empty class placed at offset zero might itself have an
3740 empty base at a nonzero offset. */
3741 else if (walk_subobject_offsets (basetype,
3742 empty_base_at_nonzero_offset_p,
3745 /*max_offset=*/NULL_TREE,
3748 if (abi_version_at_least (2))
3749 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3751 warning ("class `%T' will be considered nearly empty in a "
3752 "future version of GCC", t);
3756 /* We do not create a FIELD_DECL for empty base classes because
3757 it might overlap some other field. We want to be able to
3758 create CONSTRUCTORs for the class by iterating over the
3759 FIELD_DECLs, and the back end does not handle overlapping
3762 /* An empty virtual base causes a class to be non-empty
3763 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3764 here because that was already done when the virtual table
3765 pointer was created. */
3768 /* Record the offsets of BINFO and its base subobjects. */
3769 record_subobject_offsets (binfo,
3770 BINFO_OFFSET (binfo),
3777 /* Layout all of the non-virtual base classes. Record empty
3778 subobjects in OFFSETS. T is the most derived type. Return nonzero
3779 if the type cannot be nearly empty. The fields created
3780 corresponding to the base classes will be inserted at
3784 build_base_fields (record_layout_info rli,
3785 splay_tree offsets, tree *next_field)
3787 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3790 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
3793 /* The primary base class is always allocated first. */
3794 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3795 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3796 offsets, next_field);
3798 /* Now allocate the rest of the bases. */
3799 for (i = 0; i < n_baseclasses; ++i)
3803 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
3805 /* The primary base was already allocated above, so we don't
3806 need to allocate it again here. */
3807 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3810 /* Virtual bases are added at the end (a primary virtual base
3811 will have already been added). */
3812 if (TREE_VIA_VIRTUAL (base_binfo))
3815 next_field = build_base_field (rli, base_binfo,
3816 offsets, next_field);
3820 /* Go through the TYPE_METHODS of T issuing any appropriate
3821 diagnostics, figuring out which methods override which other
3822 methods, and so forth. */
3825 check_methods (tree t)
3829 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3831 /* If this was an evil function, don't keep it in class. */
3832 if (DECL_ASSEMBLER_NAME_SET_P (x)
3833 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3836 check_for_override (x, t);
3837 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3838 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3840 /* The name of the field is the original field name
3841 Save this in auxiliary field for later overloading. */
3842 if (DECL_VINDEX (x))
3844 TYPE_POLYMORPHIC_P (t) = 1;
3845 if (DECL_PURE_VIRTUAL_P (x))
3846 CLASSTYPE_PURE_VIRTUALS (t)
3847 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3852 /* FN is a constructor or destructor. Clone the declaration to create
3853 a specialized in-charge or not-in-charge version, as indicated by
3857 build_clone (tree fn, tree name)
3862 /* Copy the function. */
3863 clone = copy_decl (fn);
3864 /* Remember where this function came from. */
3865 DECL_CLONED_FUNCTION (clone) = fn;
3866 DECL_ABSTRACT_ORIGIN (clone) = fn;
3867 /* Reset the function name. */
3868 DECL_NAME (clone) = name;
3869 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3870 /* There's no pending inline data for this function. */
3871 DECL_PENDING_INLINE_INFO (clone) = NULL;
3872 DECL_PENDING_INLINE_P (clone) = 0;
3873 /* And it hasn't yet been deferred. */
3874 DECL_DEFERRED_FN (clone) = 0;
3876 /* The base-class destructor is not virtual. */
3877 if (name == base_dtor_identifier)
3879 DECL_VIRTUAL_P (clone) = 0;
3880 if (TREE_CODE (clone) != TEMPLATE_DECL)
3881 DECL_VINDEX (clone) = NULL_TREE;
3884 /* If there was an in-charge parameter, drop it from the function
3886 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3892 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3893 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3894 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3895 /* Skip the `this' parameter. */
3896 parmtypes = TREE_CHAIN (parmtypes);
3897 /* Skip the in-charge parameter. */
3898 parmtypes = TREE_CHAIN (parmtypes);
3899 /* And the VTT parm, in a complete [cd]tor. */
3900 if (DECL_HAS_VTT_PARM_P (fn)
3901 && ! DECL_NEEDS_VTT_PARM_P (clone))
3902 parmtypes = TREE_CHAIN (parmtypes);
3903 /* If this is subobject constructor or destructor, add the vtt
3906 = build_cplus_method_type (basetype,
3907 TREE_TYPE (TREE_TYPE (clone)),
3910 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3914 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3915 aren't function parameters; those are the template parameters. */
3916 if (TREE_CODE (clone) != TEMPLATE_DECL)
3918 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3919 /* Remove the in-charge parameter. */
3920 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3922 TREE_CHAIN (DECL_ARGUMENTS (clone))
3923 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3924 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3926 /* And the VTT parm, in a complete [cd]tor. */
3927 if (DECL_HAS_VTT_PARM_P (fn))
3929 if (DECL_NEEDS_VTT_PARM_P (clone))
3930 DECL_HAS_VTT_PARM_P (clone) = 1;
3933 TREE_CHAIN (DECL_ARGUMENTS (clone))
3934 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3935 DECL_HAS_VTT_PARM_P (clone) = 0;
3939 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3941 DECL_CONTEXT (parms) = clone;
3942 cxx_dup_lang_specific_decl (parms);
3946 /* Create the RTL for this function. */
3947 SET_DECL_RTL (clone, NULL_RTX);
3948 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
3950 /* Make it easy to find the CLONE given the FN. */
3951 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3952 TREE_CHAIN (fn) = clone;
3954 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
3955 if (TREE_CODE (clone) == TEMPLATE_DECL)
3959 DECL_TEMPLATE_RESULT (clone)
3960 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3961 result = DECL_TEMPLATE_RESULT (clone);
3962 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3963 DECL_TI_TEMPLATE (result) = clone;
3965 else if (DECL_DEFERRED_FN (fn))
3971 /* Produce declarations for all appropriate clones of FN. If
3972 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
3973 CLASTYPE_METHOD_VEC as well. */
3976 clone_function_decl (tree fn, int update_method_vec_p)
3980 /* Avoid inappropriate cloning. */
3982 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
3985 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
3987 /* For each constructor, we need two variants: an in-charge version
3988 and a not-in-charge version. */
3989 clone = build_clone (fn, complete_ctor_identifier);
3990 if (update_method_vec_p)
3991 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3992 clone = build_clone (fn, base_ctor_identifier);
3993 if (update_method_vec_p)
3994 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3998 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4000 /* For each destructor, we need three variants: an in-charge
4001 version, a not-in-charge version, and an in-charge deleting
4002 version. We clone the deleting version first because that
4003 means it will go second on the TYPE_METHODS list -- and that
4004 corresponds to the correct layout order in the virtual
4007 For a non-virtual destructor, we do not build a deleting
4009 if (DECL_VIRTUAL_P (fn))
4011 clone = build_clone (fn, deleting_dtor_identifier);
4012 if (update_method_vec_p)
4013 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4015 clone = build_clone (fn, complete_dtor_identifier);
4016 if (update_method_vec_p)
4017 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4018 clone = build_clone (fn, base_dtor_identifier);
4019 if (update_method_vec_p)
4020 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4023 /* Note that this is an abstract function that is never emitted. */
4024 DECL_ABSTRACT (fn) = 1;
4027 /* DECL is an in charge constructor, which is being defined. This will
4028 have had an in class declaration, from whence clones were
4029 declared. An out-of-class definition can specify additional default
4030 arguments. As it is the clones that are involved in overload
4031 resolution, we must propagate the information from the DECL to its
4035 adjust_clone_args (tree decl)
4039 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4040 clone = TREE_CHAIN (clone))
4042 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4043 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4044 tree decl_parms, clone_parms;
4046 clone_parms = orig_clone_parms;
4048 /* Skip the 'this' parameter. */
4049 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4050 orig_decl_parms = TREE_CHAIN (orig_decl_parms);