2005-07-02 Zack Weinberg <zack@codesourcery.com>
[official-gcc/alias-decl.git] / gcc / cp / class.c
blob4af7412405a8a5f247997f3161493419a68f9b94
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, 2004, 2005 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)
11 any later version.
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, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "output.h"
35 #include "toplev.h"
36 #include "target.h"
37 #include "convert.h"
38 #include "cgraph.h"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
62 }* class_stack_node_t;
64 typedef struct vtbl_init_data_s
66 /* The base for which we're building initializers. */
67 tree binfo;
68 /* The type of the most-derived type. */
69 tree derived;
70 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
71 unless ctor_vtbl_p is true. */
72 tree rtti_binfo;
73 /* The negative-index vtable initializers built up so far. These
74 are in order from least negative index to most negative index. */
75 tree inits;
76 /* The last (i.e., most negative) entry in INITS. */
77 tree* last_init;
78 /* The binfo for the virtual base for which we're building
79 vcall offset initializers. */
80 tree vbase;
81 /* The functions in vbase for which we have already provided vcall
82 offsets. */
83 VEC(tree,gc) *fns;
84 /* The vtable index of the next vcall or vbase offset. */
85 tree index;
86 /* Nonzero if we are building the initializer for the primary
87 vtable. */
88 int primary_vtbl_p;
89 /* Nonzero if we are building the initializer for a construction
90 vtable. */
91 int ctor_vtbl_p;
92 /* True when adding vcall offset entries to the vtable. False when
93 merely computing the indices. */
94 bool generate_vcall_entries;
95 } vtbl_init_data;
97 /* The type of a function passed to walk_subobject_offsets. */
98 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
100 /* The stack itself. This is a dynamically resized array. The
101 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
102 static int current_class_stack_size;
103 static class_stack_node_t current_class_stack;
105 /* An array of all local classes present in this translation unit, in
106 declaration order. */
107 VEC(tree,gc) *local_classes;
109 static tree get_vfield_name (tree);
110 static void finish_struct_anon (tree);
111 static tree get_vtable_name (tree);
112 static tree get_basefndecls (tree, tree);
113 static int build_primary_vtable (tree, tree);
114 static int build_secondary_vtable (tree);
115 static void finish_vtbls (tree);
116 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
117 static void finish_struct_bits (tree);
118 static int alter_access (tree, tree, tree);
119 static void handle_using_decl (tree, tree);
120 static tree dfs_modify_vtables (tree, void *);
121 static tree modify_all_vtables (tree, tree);
122 static void determine_primary_bases (tree);
123 static void finish_struct_methods (tree);
124 static void maybe_warn_about_overly_private_class (tree);
125 static int method_name_cmp (const void *, const void *);
126 static int resort_method_name_cmp (const void *, const void *);
127 static void add_implicitly_declared_members (tree, int, int);
128 static tree fixed_type_or_null (tree, int *, int *);
129 static tree resolve_address_of_overloaded_function (tree, tree, tsubst_flags_t,
130 bool, tree);
131 static tree build_simple_base_path (tree expr, tree binfo);
132 static tree build_vtbl_ref_1 (tree, tree);
133 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
134 static int count_fields (tree);
135 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
136 static void check_bitfield_decl (tree);
137 static void check_field_decl (tree, tree, int *, int *, int *);
138 static void check_field_decls (tree, tree *, int *, int *);
139 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
140 static void build_base_fields (record_layout_info, splay_tree, tree *);
141 static void check_methods (tree);
142 static void remove_zero_width_bit_fields (tree);
143 static void check_bases (tree, int *, int *);
144 static void check_bases_and_members (tree);
145 static tree create_vtable_ptr (tree, tree *);
146 static void include_empty_classes (record_layout_info);
147 static void layout_class_type (tree, tree *);
148 static void fixup_pending_inline (tree);
149 static void fixup_inline_methods (tree);
150 static void propagate_binfo_offsets (tree, tree);
151 static void layout_virtual_bases (record_layout_info, splay_tree);
152 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
153 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
154 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
155 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
156 static void add_vcall_offset (tree, tree, vtbl_init_data *);
157 static void layout_vtable_decl (tree, int);
158 static tree dfs_find_final_overrider_pre (tree, void *);
159 static tree dfs_find_final_overrider_post (tree, void *);
160 static tree find_final_overrider (tree, tree, tree);
161 static int make_new_vtable (tree, tree);
162 static int maybe_indent_hierarchy (FILE *, int, int);
163 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
164 static void dump_class_hierarchy (tree);
165 static void dump_class_hierarchy_1 (FILE *, int, tree);
166 static void dump_array (FILE *, tree);
167 static void dump_vtable (tree, tree, tree);
168 static void dump_vtt (tree, tree);
169 static void dump_thunk (FILE *, int, tree);
170 static tree build_vtable (tree, tree, tree);
171 static void initialize_vtable (tree, tree);
172 static void layout_nonempty_base_or_field (record_layout_info,
173 tree, tree, splay_tree);
174 static tree end_of_class (tree, int);
175 static bool layout_empty_base (tree, tree, splay_tree);
176 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
177 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
178 tree);
179 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
180 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
181 static void clone_constructors_and_destructors (tree);
182 static tree build_clone (tree, tree);
183 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
184 static void build_ctor_vtbl_group (tree, tree);
185 static void build_vtt (tree);
186 static tree binfo_ctor_vtable (tree);
187 static tree *build_vtt_inits (tree, tree, tree *, tree *);
188 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
189 static tree dfs_fixup_binfo_vtbls (tree, void *);
190 static int record_subobject_offset (tree, tree, splay_tree);
191 static int check_subobject_offset (tree, tree, splay_tree);
192 static int walk_subobject_offsets (tree, subobject_offset_fn,
193 tree, splay_tree, tree, int);
194 static void record_subobject_offsets (tree, tree, splay_tree, int);
195 static int layout_conflict_p (tree, tree, splay_tree, int);
196 static int splay_tree_compare_integer_csts (splay_tree_key k1,
197 splay_tree_key k2);
198 static void warn_about_ambiguous_bases (tree);
199 static bool type_requires_array_cookie (tree);
200 static bool contains_empty_class_p (tree);
201 static bool base_derived_from (tree, tree);
202 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
203 static tree end_of_base (tree);
204 static tree get_vcall_index (tree, tree);
206 /* Variables shared between class.c and call.c. */
208 #ifdef GATHER_STATISTICS
209 int n_vtables = 0;
210 int n_vtable_entries = 0;
211 int n_vtable_searches = 0;
212 int n_vtable_elems = 0;
213 int n_convert_harshness = 0;
214 int n_compute_conversion_costs = 0;
215 int n_inner_fields_searched = 0;
216 #endif
218 /* Convert to or from a base subobject. EXPR is an expression of type
219 `A' or `A*', an expression of type `B' or `B*' is returned. To
220 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
221 the B base instance within A. To convert base A to derived B, CODE
222 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
223 In this latter case, A must not be a morally virtual base of B.
224 NONNULL is true if EXPR is known to be non-NULL (this is only
225 needed when EXPR is of pointer type). CV qualifiers are preserved
226 from EXPR. */
228 tree
229 build_base_path (enum tree_code code,
230 tree expr,
231 tree binfo,
232 int nonnull)
234 tree v_binfo = NULL_TREE;
235 tree d_binfo = NULL_TREE;
236 tree probe;
237 tree offset;
238 tree target_type;
239 tree null_test = NULL;
240 tree ptr_target_type;
241 int fixed_type_p;
242 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
243 bool has_empty = false;
244 bool virtual_access;
246 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
247 return error_mark_node;
249 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
251 d_binfo = probe;
252 if (is_empty_class (BINFO_TYPE (probe)))
253 has_empty = true;
254 if (!v_binfo && BINFO_VIRTUAL_P (probe))
255 v_binfo = probe;
258 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
259 if (want_pointer)
260 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
262 gcc_assert ((code == MINUS_EXPR
263 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
264 || (code == PLUS_EXPR
265 && SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe)));
267 if (binfo == d_binfo)
268 /* Nothing to do. */
269 return expr;
271 if (code == MINUS_EXPR && v_binfo)
273 error ("cannot convert from base %qT to derived type %qT via virtual base %qT",
274 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
275 return error_mark_node;
278 if (!want_pointer)
279 /* This must happen before the call to save_expr. */
280 expr = build_unary_op (ADDR_EXPR, expr, 0);
282 offset = BINFO_OFFSET (binfo);
283 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
285 /* Do we need to look in the vtable for the real offset? */
286 virtual_access = (v_binfo && fixed_type_p <= 0);
288 /* Do we need to check for a null pointer? */
289 if (want_pointer && !nonnull && (virtual_access || !integer_zerop (offset)))
290 null_test = error_mark_node;
292 /* Protect against multiple evaluation if necessary. */
293 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
294 expr = save_expr (expr);
296 /* Now that we've saved expr, build the real null test. */
297 if (null_test)
299 tree zero = cp_convert (TREE_TYPE (expr), integer_zero_node);
300 null_test = fold_build2 (NE_EXPR, boolean_type_node,
301 expr, zero);
304 /* If this is a simple base reference, express it as a COMPONENT_REF. */
305 if (code == PLUS_EXPR && !virtual_access
306 /* We don't build base fields for empty bases, and they aren't very
307 interesting to the optimizers anyway. */
308 && !has_empty)
310 expr = build_indirect_ref (expr, NULL);
311 expr = build_simple_base_path (expr, binfo);
312 if (want_pointer)
313 expr = build_address (expr);
314 target_type = TREE_TYPE (expr);
315 goto out;
318 if (virtual_access)
320 /* Going via virtual base V_BINFO. We need the static offset
321 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
322 V_BINFO. That offset is an entry in D_BINFO's vtable. */
323 tree v_offset;
325 if (fixed_type_p < 0 && in_base_initializer)
327 /* In a base member initializer, we cannot rely on
328 the vtable being set up. We have to use the vtt_parm. */
329 tree derived = BINFO_INHERITANCE_CHAIN (v_binfo);
330 tree t;
332 t = TREE_TYPE (TYPE_VFIELD (BINFO_TYPE (derived)));
333 t = build_pointer_type (t);
334 v_offset = convert (t, current_vtt_parm);
335 v_offset = build2 (PLUS_EXPR, t, v_offset,
336 BINFO_VPTR_INDEX (derived));
337 v_offset = build_indirect_ref (v_offset, NULL);
339 else
340 v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
341 TREE_TYPE (TREE_TYPE (expr)));
343 v_offset = build2 (PLUS_EXPR, TREE_TYPE (v_offset),
344 v_offset, BINFO_VPTR_FIELD (v_binfo));
345 v_offset = build1 (NOP_EXPR,
346 build_pointer_type (ptrdiff_type_node),
347 v_offset);
348 v_offset = build_indirect_ref (v_offset, NULL);
349 TREE_CONSTANT (v_offset) = 1;
350 TREE_INVARIANT (v_offset) = 1;
352 offset = convert_to_integer (ptrdiff_type_node,
353 size_diffop (offset,
354 BINFO_OFFSET (v_binfo)));
356 if (!integer_zerop (offset))
357 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
359 if (fixed_type_p < 0)
360 /* Negative fixed_type_p means this is a constructor or destructor;
361 virtual base layout is fixed in in-charge [cd]tors, but not in
362 base [cd]tors. */
363 offset = build3 (COND_EXPR, ptrdiff_type_node,
364 build2 (EQ_EXPR, boolean_type_node,
365 current_in_charge_parm, integer_zero_node),
366 v_offset,
367 BINFO_OFFSET (binfo));
368 else
369 offset = v_offset;
372 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
374 target_type = cp_build_qualified_type
375 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
376 ptr_target_type = build_pointer_type (target_type);
377 if (want_pointer)
378 target_type = ptr_target_type;
380 expr = build1 (NOP_EXPR, ptr_target_type, expr);
382 if (!integer_zerop (offset))
383 expr = build2 (code, ptr_target_type, expr, offset);
384 else
385 null_test = NULL;
387 if (!want_pointer)
388 expr = build_indirect_ref (expr, NULL);
390 out:
391 if (null_test)
392 expr = fold_build3 (COND_EXPR, target_type, null_test, expr,
393 fold_build1 (NOP_EXPR, target_type,
394 integer_zero_node));
396 return expr;
399 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
400 Perform a derived-to-base conversion by recursively building up a
401 sequence of COMPONENT_REFs to the appropriate base fields. */
403 static tree
404 build_simple_base_path (tree expr, tree binfo)
406 tree type = BINFO_TYPE (binfo);
407 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
408 tree field;
410 if (d_binfo == NULL_TREE)
412 tree temp;
414 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
416 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
417 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
418 an lvalue in the frontend; only _DECLs and _REFs are lvalues
419 in the backend. */
420 temp = unary_complex_lvalue (ADDR_EXPR, expr);
421 if (temp)
422 expr = build_indirect_ref (temp, NULL);
424 return expr;
427 /* Recurse. */
428 expr = build_simple_base_path (expr, d_binfo);
430 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
431 field; field = TREE_CHAIN (field))
432 /* Is this the base field created by build_base_field? */
433 if (TREE_CODE (field) == FIELD_DECL
434 && DECL_FIELD_IS_BASE (field)
435 && TREE_TYPE (field) == type)
437 /* We don't use build_class_member_access_expr here, as that
438 has unnecessary checks, and more importantly results in
439 recursive calls to dfs_walk_once. */
440 int type_quals = cp_type_quals (TREE_TYPE (expr));
442 expr = build3 (COMPONENT_REF,
443 cp_build_qualified_type (type, type_quals),
444 expr, field, NULL_TREE);
445 expr = fold_if_not_in_template (expr);
447 /* Mark the expression const or volatile, as appropriate.
448 Even though we've dealt with the type above, we still have
449 to mark the expression itself. */
450 if (type_quals & TYPE_QUAL_CONST)
451 TREE_READONLY (expr) = 1;
452 if (type_quals & TYPE_QUAL_VOLATILE)
453 TREE_THIS_VOLATILE (expr) = 1;
455 return expr;
458 /* Didn't find the base field?!? */
459 gcc_unreachable ();
462 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
463 type is a class type or a pointer to a class type. In the former
464 case, TYPE is also a class type; in the latter it is another
465 pointer type. If CHECK_ACCESS is true, an error message is emitted
466 if TYPE is inaccessible. If OBJECT has pointer type, the value is
467 assumed to be non-NULL. */
469 tree
470 convert_to_base (tree object, tree type, bool check_access, bool nonnull)
472 tree binfo;
473 tree object_type;
475 if (TYPE_PTR_P (TREE_TYPE (object)))
477 object_type = TREE_TYPE (TREE_TYPE (object));
478 type = TREE_TYPE (type);
480 else
481 object_type = TREE_TYPE (object);
483 binfo = lookup_base (object_type, type,
484 check_access ? ba_check : ba_unique,
485 NULL);
486 if (!binfo || binfo == error_mark_node)
487 return error_mark_node;
489 return build_base_path (PLUS_EXPR, object, binfo, nonnull);
492 /* EXPR is an expression with unqualified class type. BASE is a base
493 binfo of that class type. Returns EXPR, converted to the BASE
494 type. This function assumes that EXPR is the most derived class;
495 therefore virtual bases can be found at their static offsets. */
497 tree
498 convert_to_base_statically (tree expr, tree base)
500 tree expr_type;
502 expr_type = TREE_TYPE (expr);
503 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
505 tree pointer_type;
507 pointer_type = build_pointer_type (expr_type);
508 expr = build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1);
509 if (!integer_zerop (BINFO_OFFSET (base)))
510 expr = build2 (PLUS_EXPR, pointer_type, expr,
511 build_nop (pointer_type, BINFO_OFFSET (base)));
512 expr = build_nop (build_pointer_type (BINFO_TYPE (base)), expr);
513 expr = build1 (INDIRECT_REF, BINFO_TYPE (base), expr);
516 return expr;
520 tree
521 build_vfield_ref (tree datum, tree type)
523 tree vfield, vcontext;
525 if (datum == error_mark_node)
526 return error_mark_node;
528 /* First, convert to the requested type. */
529 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
530 datum = convert_to_base (datum, type, /*check_access=*/false,
531 /*nonnull=*/true);
533 /* Second, the requested type may not be the owner of its own vptr.
534 If not, convert to the base class that owns it. We cannot use
535 convert_to_base here, because VCONTEXT may appear more than once
536 in the inheritance hierarchy of TYPE, and thus direct conversion
537 between the types may be ambiguous. Following the path back up
538 one step at a time via primary bases avoids the problem. */
539 vfield = TYPE_VFIELD (type);
540 vcontext = DECL_CONTEXT (vfield);
541 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
543 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
544 type = TREE_TYPE (datum);
547 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
550 /* Given an object INSTANCE, return an expression which yields the
551 vtable element corresponding to INDEX. There are many special
552 cases for INSTANCE which we take care of here, mainly to avoid
553 creating extra tree nodes when we don't have to. */
555 static tree
556 build_vtbl_ref_1 (tree instance, tree idx)
558 tree aref;
559 tree vtbl = NULL_TREE;
561 /* Try to figure out what a reference refers to, and
562 access its virtual function table directly. */
564 int cdtorp = 0;
565 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
567 tree basetype = non_reference (TREE_TYPE (instance));
569 if (fixed_type && !cdtorp)
571 tree binfo = lookup_base (fixed_type, basetype,
572 ba_unique | ba_quiet, NULL);
573 if (binfo)
574 vtbl = unshare_expr (BINFO_VTABLE (binfo));
577 if (!vtbl)
578 vtbl = build_vfield_ref (instance, basetype);
580 assemble_external (vtbl);
582 aref = build_array_ref (vtbl, idx);
583 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
584 TREE_INVARIANT (aref) = TREE_CONSTANT (aref);
586 return aref;
589 tree
590 build_vtbl_ref (tree instance, tree idx)
592 tree aref = build_vtbl_ref_1 (instance, idx);
594 return aref;
597 /* Given a stable object pointer INSTANCE_PTR, return an expression which
598 yields a function pointer corresponding to vtable element INDEX. */
600 tree
601 build_vfn_ref (tree instance_ptr, tree idx)
603 tree aref;
605 aref = build_vtbl_ref_1 (build_indirect_ref (instance_ptr, 0), idx);
607 /* When using function descriptors, the address of the
608 vtable entry is treated as a function pointer. */
609 if (TARGET_VTABLE_USES_DESCRIPTORS)
610 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
611 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
613 /* Remember this as a method reference, for later devirtualization. */
614 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
616 return aref;
619 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
620 for the given TYPE. */
622 static tree
623 get_vtable_name (tree type)
625 return mangle_vtbl_for_type (type);
628 /* Return an IDENTIFIER_NODE for the name of the virtual table table
629 for TYPE. */
631 tree
632 get_vtt_name (tree type)
634 return mangle_vtt_for_type (type);
637 /* DECL is an entity associated with TYPE, like a virtual table or an
638 implicitly generated constructor. Determine whether or not DECL
639 should have external or internal linkage at the object file
640 level. This routine does not deal with COMDAT linkage and other
641 similar complexities; it simply sets TREE_PUBLIC if it possible for
642 entities in other translation units to contain copies of DECL, in
643 the abstract. */
645 void
646 set_linkage_according_to_type (tree type, tree decl)
648 /* If TYPE involves a local class in a function with internal
649 linkage, then DECL should have internal linkage too. Other local
650 classes have no linkage -- but if their containing functions
651 have external linkage, it makes sense for DECL to have external
652 linkage too. That will allow template definitions to be merged,
653 for example. */
654 if (no_linkage_check (type, /*relaxed_p=*/true))
656 TREE_PUBLIC (decl) = 0;
657 DECL_INTERFACE_KNOWN (decl) = 1;
659 else
660 TREE_PUBLIC (decl) = 1;
663 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
664 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
665 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
667 static tree
668 build_vtable (tree class_type, tree name, tree vtable_type)
670 tree decl;
672 decl = build_lang_decl (VAR_DECL, name, vtable_type);
673 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
674 now to avoid confusion in mangle_decl. */
675 SET_DECL_ASSEMBLER_NAME (decl, name);
676 DECL_CONTEXT (decl) = class_type;
677 DECL_ARTIFICIAL (decl) = 1;
678 TREE_STATIC (decl) = 1;
679 TREE_READONLY (decl) = 1;
680 DECL_VIRTUAL_P (decl) = 1;
681 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
682 DECL_VTABLE_OR_VTT_P (decl) = 1;
683 /* At one time the vtable info was grabbed 2 words at a time. This
684 fails on sparc unless you have 8-byte alignment. (tiemann) */
685 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
686 DECL_ALIGN (decl));
687 set_linkage_according_to_type (class_type, decl);
688 /* The vtable has not been defined -- yet. */
689 DECL_EXTERNAL (decl) = 1;
690 DECL_NOT_REALLY_EXTERN (decl) = 1;
692 /* Mark the VAR_DECL node representing the vtable itself as a
693 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
694 is rather important that such things be ignored because any
695 effort to actually generate DWARF for them will run into
696 trouble when/if we encounter code like:
698 #pragma interface
699 struct S { virtual void member (); };
701 because the artificial declaration of the vtable itself (as
702 manufactured by the g++ front end) will say that the vtable is
703 a static member of `S' but only *after* the debug output for
704 the definition of `S' has already been output. This causes
705 grief because the DWARF entry for the definition of the vtable
706 will try to refer back to an earlier *declaration* of the
707 vtable as a static member of `S' and there won't be one. We
708 might be able to arrange to have the "vtable static member"
709 attached to the member list for `S' before the debug info for
710 `S' get written (which would solve the problem) but that would
711 require more intrusive changes to the g++ front end. */
712 DECL_IGNORED_P (decl) = 1;
714 return decl;
717 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
718 or even complete. If this does not exist, create it. If COMPLETE is
719 nonzero, then complete the definition of it -- that will render it
720 impossible to actually build the vtable, but is useful to get at those
721 which are known to exist in the runtime. */
723 tree
724 get_vtable_decl (tree type, int complete)
726 tree decl;
728 if (CLASSTYPE_VTABLES (type))
729 return CLASSTYPE_VTABLES (type);
731 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
732 CLASSTYPE_VTABLES (type) = decl;
734 if (complete)
736 DECL_EXTERNAL (decl) = 1;
737 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
740 return decl;
743 /* Build the primary virtual function table for TYPE. If BINFO is
744 non-NULL, build the vtable starting with the initial approximation
745 that it is the same as the one which is the head of the association
746 list. Returns a nonzero value if a new vtable is actually
747 created. */
749 static int
750 build_primary_vtable (tree binfo, tree type)
752 tree decl;
753 tree virtuals;
755 decl = get_vtable_decl (type, /*complete=*/0);
757 if (binfo)
759 if (BINFO_NEW_VTABLE_MARKED (binfo))
760 /* We have already created a vtable for this base, so there's
761 no need to do it again. */
762 return 0;
764 virtuals = copy_list (BINFO_VIRTUALS (binfo));
765 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
766 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
767 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
769 else
771 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
772 virtuals = NULL_TREE;
775 #ifdef GATHER_STATISTICS
776 n_vtables += 1;
777 n_vtable_elems += list_length (virtuals);
778 #endif
780 /* Initialize the association list for this type, based
781 on our first approximation. */
782 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
783 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
784 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
785 return 1;
788 /* Give BINFO a new virtual function table which is initialized
789 with a skeleton-copy of its original initialization. The only
790 entry that changes is the `delta' entry, so we can really
791 share a lot of structure.
793 FOR_TYPE is the most derived type which caused this table to
794 be needed.
796 Returns nonzero if we haven't met BINFO before.
798 The order in which vtables are built (by calling this function) for
799 an object must remain the same, otherwise a binary incompatibility
800 can result. */
802 static int
803 build_secondary_vtable (tree binfo)
805 if (BINFO_NEW_VTABLE_MARKED (binfo))
806 /* We already created a vtable for this base. There's no need to
807 do it again. */
808 return 0;
810 /* Remember that we've created a vtable for this BINFO, so that we
811 don't try to do so again. */
812 SET_BINFO_NEW_VTABLE_MARKED (binfo);
814 /* Make fresh virtual list, so we can smash it later. */
815 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
817 /* Secondary vtables are laid out as part of the same structure as
818 the primary vtable. */
819 BINFO_VTABLE (binfo) = NULL_TREE;
820 return 1;
823 /* Create a new vtable for BINFO which is the hierarchy dominated by
824 T. Return nonzero if we actually created a new vtable. */
826 static int
827 make_new_vtable (tree t, tree binfo)
829 if (binfo == TYPE_BINFO (t))
830 /* In this case, it is *type*'s vtable we are modifying. We start
831 with the approximation that its vtable is that of the
832 immediate base class. */
833 return build_primary_vtable (binfo, t);
834 else
835 /* This is our very own copy of `basetype' to play with. Later,
836 we will fill in all the virtual functions that override the
837 virtual functions in these base classes which are not defined
838 by the current type. */
839 return build_secondary_vtable (binfo);
842 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
843 (which is in the hierarchy dominated by T) list FNDECL as its
844 BV_FN. DELTA is the required constant adjustment from the `this'
845 pointer where the vtable entry appears to the `this' required when
846 the function is actually called. */
848 static void
849 modify_vtable_entry (tree t,
850 tree binfo,
851 tree fndecl,
852 tree delta,
853 tree *virtuals)
855 tree v;
857 v = *virtuals;
859 if (fndecl != BV_FN (v)
860 || !tree_int_cst_equal (delta, BV_DELTA (v)))
862 /* We need a new vtable for BINFO. */
863 if (make_new_vtable (t, binfo))
865 /* If we really did make a new vtable, we also made a copy
866 of the BINFO_VIRTUALS list. Now, we have to find the
867 corresponding entry in that list. */
868 *virtuals = BINFO_VIRTUALS (binfo);
869 while (BV_FN (*virtuals) != BV_FN (v))
870 *virtuals = TREE_CHAIN (*virtuals);
871 v = *virtuals;
874 BV_DELTA (v) = delta;
875 BV_VCALL_INDEX (v) = NULL_TREE;
876 BV_FN (v) = fndecl;
881 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
882 the USING_DECL naming METHOD. */
884 void
885 add_method (tree type, tree method, tree using_decl)
887 unsigned slot;
888 tree overload;
889 bool template_conv_p = false;
890 bool conv_p;
891 VEC(tree,gc) *method_vec;
892 bool complete_p;
893 bool insert_p = false;
894 tree current_fns;
896 if (method == error_mark_node)
897 return;
899 complete_p = COMPLETE_TYPE_P (type);
900 conv_p = DECL_CONV_FN_P (method);
901 if (conv_p)
902 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
903 && DECL_TEMPLATE_CONV_FN_P (method));
905 method_vec = CLASSTYPE_METHOD_VEC (type);
906 if (!method_vec)
908 /* Make a new method vector. We start with 8 entries. We must
909 allocate at least two (for constructors and destructors), and
910 we're going to end up with an assignment operator at some
911 point as well. */
912 method_vec = VEC_alloc (tree, gc, 8);
913 /* Create slots for constructors and destructors. */
914 VEC_quick_push (tree, method_vec, NULL_TREE);
915 VEC_quick_push (tree, method_vec, NULL_TREE);
916 CLASSTYPE_METHOD_VEC (type) = method_vec;
919 /* Constructors and destructors go in special slots. */
920 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
921 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
922 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
924 slot = CLASSTYPE_DESTRUCTOR_SLOT;
926 if (TYPE_FOR_JAVA (type))
928 if (!DECL_ARTIFICIAL (method))
929 error ("Java class %qT cannot have a destructor", type);
930 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
931 error ("Java class %qT cannot have an implicit non-trivial "
932 "destructor",
933 type);
936 else
938 tree m;
940 insert_p = true;
941 /* See if we already have an entry with this name. */
942 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
943 VEC_iterate (tree, method_vec, slot, m);
944 ++slot)
946 m = OVL_CURRENT (m);
947 if (template_conv_p)
949 if (TREE_CODE (m) == TEMPLATE_DECL
950 && DECL_TEMPLATE_CONV_FN_P (m))
951 insert_p = false;
952 break;
954 if (conv_p && !DECL_CONV_FN_P (m))
955 break;
956 if (DECL_NAME (m) == DECL_NAME (method))
958 insert_p = false;
959 break;
961 if (complete_p
962 && !DECL_CONV_FN_P (m)
963 && DECL_NAME (m) > DECL_NAME (method))
964 break;
967 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
969 if (processing_template_decl)
970 /* TYPE is a template class. Don't issue any errors now; wait
971 until instantiation time to complain. */
973 else
975 tree fns;
977 /* Check to see if we've already got this method. */
978 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
980 tree fn = OVL_CURRENT (fns);
981 tree parms1;
982 tree parms2;
983 bool same = 1;
985 if (TREE_CODE (fn) != TREE_CODE (method))
986 continue;
988 /* [over.load] Member function declarations with the
989 same name and the same parameter types cannot be
990 overloaded if any of them is a static member
991 function declaration.
993 [namespace.udecl] When a using-declaration brings names
994 from a base class into a derived class scope, member
995 functions in the derived class override and/or hide member
996 functions with the same name and parameter types in a base
997 class (rather than conflicting). */
998 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
999 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1001 /* Compare the quals on the 'this' parm. Don't compare
1002 the whole types, as used functions are treated as
1003 coming from the using class in overload resolution. */
1004 if (! DECL_STATIC_FUNCTION_P (fn)
1005 && ! DECL_STATIC_FUNCTION_P (method)
1006 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
1007 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
1008 same = 0;
1010 /* For templates, the template parms must be identical. */
1011 if (TREE_CODE (fn) == TEMPLATE_DECL
1012 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1013 DECL_TEMPLATE_PARMS (method)))
1014 same = 0;
1016 if (! DECL_STATIC_FUNCTION_P (fn))
1017 parms1 = TREE_CHAIN (parms1);
1018 if (! DECL_STATIC_FUNCTION_P (method))
1019 parms2 = TREE_CHAIN (parms2);
1021 if (same && compparms (parms1, parms2)
1022 && (!DECL_CONV_FN_P (fn)
1023 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1024 TREE_TYPE (TREE_TYPE (method)))))
1026 if (using_decl)
1028 if (DECL_CONTEXT (fn) == type)
1029 /* Defer to the local function. */
1030 return;
1031 if (DECL_CONTEXT (fn) == DECL_CONTEXT (method))
1032 error ("repeated using declaration %q+D", using_decl);
1033 else
1034 error ("using declaration %q+D conflicts with a previous using declaration",
1035 using_decl);
1037 else
1039 error ("%q+#D cannot be overloaded", method);
1040 error ("with %q+#D", fn);
1043 /* We don't call duplicate_decls here to merge the
1044 declarations because that will confuse things if the
1045 methods have inline definitions. In particular, we
1046 will crash while processing the definitions. */
1047 return;
1052 /* Add the new binding. */
1053 overload = build_overload (method, current_fns);
1055 if (!conv_p && slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1056 push_class_level_binding (DECL_NAME (method), overload);
1058 if (insert_p)
1060 /* We only expect to add few methods in the COMPLETE_P case, so
1061 just make room for one more method in that case. */
1062 if (VEC_reserve (tree, gc, method_vec, complete_p ? -1 : 1))
1063 CLASSTYPE_METHOD_VEC (type) = method_vec;
1064 if (slot == VEC_length (tree, method_vec))
1065 VEC_quick_push (tree, method_vec, overload);
1066 else
1067 VEC_quick_insert (tree, method_vec, slot, overload);
1069 else
1070 /* Replace the current slot. */
1071 VEC_replace (tree, method_vec, slot, overload);
1074 /* Subroutines of finish_struct. */
1076 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1077 legit, otherwise return 0. */
1079 static int
1080 alter_access (tree t, tree fdecl, tree access)
1082 tree elem;
1084 if (!DECL_LANG_SPECIFIC (fdecl))
1085 retrofit_lang_decl (fdecl);
1087 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1089 elem = purpose_member (t, DECL_ACCESS (fdecl));
1090 if (elem)
1092 if (TREE_VALUE (elem) != access)
1094 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1095 error ("conflicting access specifications for method"
1096 " %q+D, ignored", TREE_TYPE (fdecl));
1097 else
1098 error ("conflicting access specifications for field %qE, ignored",
1099 DECL_NAME (fdecl));
1101 else
1103 /* They're changing the access to the same thing they changed
1104 it to before. That's OK. */
1108 else
1110 perform_or_defer_access_check (TYPE_BINFO (t), fdecl);
1111 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1112 return 1;
1114 return 0;
1117 /* Process the USING_DECL, which is a member of T. */
1119 static void
1120 handle_using_decl (tree using_decl, tree t)
1122 tree decl = USING_DECL_DECLS (using_decl);
1123 tree name = DECL_NAME (using_decl);
1124 tree access
1125 = TREE_PRIVATE (using_decl) ? access_private_node
1126 : TREE_PROTECTED (using_decl) ? access_protected_node
1127 : access_public_node;
1128 tree flist = NULL_TREE;
1129 tree old_value;
1131 gcc_assert (!processing_template_decl && decl);
1133 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false);
1134 if (old_value)
1136 if (is_overloaded_fn (old_value))
1137 old_value = OVL_CURRENT (old_value);
1139 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1140 /* OK */;
1141 else
1142 old_value = NULL_TREE;
1145 cp_emit_debug_info_for_using (decl, current_class_type);
1147 if (is_overloaded_fn (decl))
1148 flist = decl;
1150 if (! old_value)
1152 else if (is_overloaded_fn (old_value))
1154 if (flist)
1155 /* It's OK to use functions from a base when there are functions with
1156 the same name already present in the current class. */;
1157 else
1159 error ("%q+D invalid in %q#T", using_decl, t);
1160 error (" because of local method %q+#D with same name",
1161 OVL_CURRENT (old_value));
1162 return;
1165 else if (!DECL_ARTIFICIAL (old_value))
1167 error ("%q+D invalid in %q#T", using_decl, t);
1168 error (" because of local member %q+#D with same name", old_value);
1169 return;
1172 /* Make type T see field decl FDECL with access ACCESS. */
1173 if (flist)
1174 for (; flist; flist = OVL_NEXT (flist))
1176 add_method (t, OVL_CURRENT (flist), using_decl);
1177 alter_access (t, OVL_CURRENT (flist), access);
1179 else
1180 alter_access (t, decl, access);
1183 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1184 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1185 properties of the bases. */
1187 static void
1188 check_bases (tree t,
1189 int* cant_have_const_ctor_p,
1190 int* no_const_asn_ref_p)
1192 int i;
1193 int seen_non_virtual_nearly_empty_base_p;
1194 tree base_binfo;
1195 tree binfo;
1197 seen_non_virtual_nearly_empty_base_p = 0;
1199 for (binfo = TYPE_BINFO (t), i = 0;
1200 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1202 tree basetype = TREE_TYPE (base_binfo);
1204 gcc_assert (COMPLETE_TYPE_P (basetype));
1206 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1207 here because the case of virtual functions but non-virtual
1208 dtor is handled in finish_struct_1. */
1209 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype))
1210 warning (0, "base class %q#T has a non-virtual destructor", basetype);
1212 /* If the base class doesn't have copy constructors or
1213 assignment operators that take const references, then the
1214 derived class cannot have such a member automatically
1215 generated. */
1216 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1217 *cant_have_const_ctor_p = 1;
1218 if (TYPE_HAS_ASSIGN_REF (basetype)
1219 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1220 *no_const_asn_ref_p = 1;
1222 if (BINFO_VIRTUAL_P (base_binfo))
1223 /* A virtual base does not effect nearly emptiness. */
1225 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1227 if (seen_non_virtual_nearly_empty_base_p)
1228 /* And if there is more than one nearly empty base, then the
1229 derived class is not nearly empty either. */
1230 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1231 else
1232 /* Remember we've seen one. */
1233 seen_non_virtual_nearly_empty_base_p = 1;
1235 else if (!is_empty_class (basetype))
1236 /* If the base class is not empty or nearly empty, then this
1237 class cannot be nearly empty. */
1238 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1240 /* A lot of properties from the bases also apply to the derived
1241 class. */
1242 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1243 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1244 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1245 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1246 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1247 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1248 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1249 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1250 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1254 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1255 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1256 that have had a nearly-empty virtual primary base stolen by some
1257 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1258 T. */
1260 static void
1261 determine_primary_bases (tree t)
1263 unsigned i;
1264 tree primary = NULL_TREE;
1265 tree type_binfo = TYPE_BINFO (t);
1266 tree base_binfo;
1268 /* Determine the primary bases of our bases. */
1269 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1270 base_binfo = TREE_CHAIN (base_binfo))
1272 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1274 /* See if we're the non-virtual primary of our inheritance
1275 chain. */
1276 if (!BINFO_VIRTUAL_P (base_binfo))
1278 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1279 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1281 if (parent_primary
1282 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1283 BINFO_TYPE (parent_primary)))
1284 /* We are the primary binfo. */
1285 BINFO_PRIMARY_P (base_binfo) = 1;
1287 /* Determine if we have a virtual primary base, and mark it so.
1289 if (primary && BINFO_VIRTUAL_P (primary))
1291 tree this_primary = copied_binfo (primary, base_binfo);
1293 if (BINFO_PRIMARY_P (this_primary))
1294 /* Someone already claimed this base. */
1295 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1296 else
1298 tree delta;
1300 BINFO_PRIMARY_P (this_primary) = 1;
1301 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1303 /* A virtual binfo might have been copied from within
1304 another hierarchy. As we're about to use it as a
1305 primary base, make sure the offsets match. */
1306 delta = size_diffop (convert (ssizetype,
1307 BINFO_OFFSET (base_binfo)),
1308 convert (ssizetype,
1309 BINFO_OFFSET (this_primary)));
1311 propagate_binfo_offsets (this_primary, delta);
1316 /* First look for a dynamic direct non-virtual base. */
1317 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1319 tree basetype = BINFO_TYPE (base_binfo);
1321 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1323 primary = base_binfo;
1324 goto found;
1328 /* A "nearly-empty" virtual base class can be the primary base
1329 class, if no non-virtual polymorphic base can be found. Look for
1330 a nearly-empty virtual dynamic base that is not already a primary
1331 base of something in the hierarchy. If there is no such base,
1332 just pick the first nearly-empty virtual base. */
1334 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1335 base_binfo = TREE_CHAIN (base_binfo))
1336 if (BINFO_VIRTUAL_P (base_binfo)
1337 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1339 if (!BINFO_PRIMARY_P (base_binfo))
1341 /* Found one that is not primary. */
1342 primary = base_binfo;
1343 goto found;
1345 else if (!primary)
1346 /* Remember the first candidate. */
1347 primary = base_binfo;
1350 found:
1351 /* If we've got a primary base, use it. */
1352 if (primary)
1354 tree basetype = BINFO_TYPE (primary);
1356 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1357 if (BINFO_PRIMARY_P (primary))
1358 /* We are stealing a primary base. */
1359 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1360 BINFO_PRIMARY_P (primary) = 1;
1361 if (BINFO_VIRTUAL_P (primary))
1363 tree delta;
1365 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1366 /* A virtual binfo might have been copied from within
1367 another hierarchy. As we're about to use it as a primary
1368 base, make sure the offsets match. */
1369 delta = size_diffop (ssize_int (0),
1370 convert (ssizetype, BINFO_OFFSET (primary)));
1372 propagate_binfo_offsets (primary, delta);
1375 primary = TYPE_BINFO (basetype);
1377 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1378 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1379 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1383 /* Set memoizing fields and bits of T (and its variants) for later
1384 use. */
1386 static void
1387 finish_struct_bits (tree t)
1389 tree variants;
1391 /* Fix up variants (if any). */
1392 for (variants = TYPE_NEXT_VARIANT (t);
1393 variants;
1394 variants = TYPE_NEXT_VARIANT (variants))
1396 /* These fields are in the _TYPE part of the node, not in
1397 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1398 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1399 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1400 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1401 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1403 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1405 TYPE_BINFO (variants) = TYPE_BINFO (t);
1407 /* Copy whatever these are holding today. */
1408 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1409 TYPE_METHODS (variants) = TYPE_METHODS (t);
1410 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1411 TYPE_SIZE (variants) = TYPE_SIZE (t);
1412 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1415 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1416 /* For a class w/o baseclasses, 'finish_struct' has set
1417 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1418 Similarly for a class whose base classes do not have vtables.
1419 When neither of these is true, we might have removed abstract
1420 virtuals (by providing a definition), added some (by declaring
1421 new ones), or redeclared ones from a base class. We need to
1422 recalculate what's really an abstract virtual at this point (by
1423 looking in the vtables). */
1424 get_pure_virtuals (t);
1426 /* If this type has a copy constructor or a destructor, force its
1427 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1428 nonzero. This will cause it to be passed by invisible reference
1429 and prevent it from being returned in a register. */
1430 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1432 tree variants;
1433 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1434 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1436 TYPE_MODE (variants) = BLKmode;
1437 TREE_ADDRESSABLE (variants) = 1;
1442 /* Issue warnings about T having private constructors, but no friends,
1443 and so forth.
1445 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1446 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1447 non-private static member functions. */
1449 static void
1450 maybe_warn_about_overly_private_class (tree t)
1452 int has_member_fn = 0;
1453 int has_nonprivate_method = 0;
1454 tree fn;
1456 if (!warn_ctor_dtor_privacy
1457 /* If the class has friends, those entities might create and
1458 access instances, so we should not warn. */
1459 || (CLASSTYPE_FRIEND_CLASSES (t)
1460 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1461 /* We will have warned when the template was declared; there's
1462 no need to warn on every instantiation. */
1463 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1464 /* There's no reason to even consider warning about this
1465 class. */
1466 return;
1468 /* We only issue one warning, if more than one applies, because
1469 otherwise, on code like:
1471 class A {
1472 // Oops - forgot `public:'
1473 A();
1474 A(const A&);
1475 ~A();
1478 we warn several times about essentially the same problem. */
1480 /* Check to see if all (non-constructor, non-destructor) member
1481 functions are private. (Since there are no friends or
1482 non-private statics, we can't ever call any of the private member
1483 functions.) */
1484 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1485 /* We're not interested in compiler-generated methods; they don't
1486 provide any way to call private members. */
1487 if (!DECL_ARTIFICIAL (fn))
1489 if (!TREE_PRIVATE (fn))
1491 if (DECL_STATIC_FUNCTION_P (fn))
1492 /* A non-private static member function is just like a
1493 friend; it can create and invoke private member
1494 functions, and be accessed without a class
1495 instance. */
1496 return;
1498 has_nonprivate_method = 1;
1499 /* Keep searching for a static member function. */
1501 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1502 has_member_fn = 1;
1505 if (!has_nonprivate_method && has_member_fn)
1507 /* There are no non-private methods, and there's at least one
1508 private member function that isn't a constructor or
1509 destructor. (If all the private members are
1510 constructors/destructors we want to use the code below that
1511 issues error messages specifically referring to
1512 constructors/destructors.) */
1513 unsigned i;
1514 tree binfo = TYPE_BINFO (t);
1516 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1517 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1519 has_nonprivate_method = 1;
1520 break;
1522 if (!has_nonprivate_method)
1524 warning (0, "all member functions in class %qT are private", t);
1525 return;
1529 /* Even if some of the member functions are non-private, the class
1530 won't be useful for much if all the constructors or destructors
1531 are private: such an object can never be created or destroyed. */
1532 fn = CLASSTYPE_DESTRUCTORS (t);
1533 if (fn && TREE_PRIVATE (fn))
1535 warning (0, "%q#T only defines a private destructor and has no friends",
1537 return;
1540 if (TYPE_HAS_CONSTRUCTOR (t))
1542 int nonprivate_ctor = 0;
1544 /* If a non-template class does not define a copy
1545 constructor, one is defined for it, enabling it to avoid
1546 this warning. For a template class, this does not
1547 happen, and so we would normally get a warning on:
1549 template <class T> class C { private: C(); };
1551 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1552 complete non-template or fully instantiated classes have this
1553 flag set. */
1554 if (!TYPE_HAS_INIT_REF (t))
1555 nonprivate_ctor = 1;
1556 else
1557 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1559 tree ctor = OVL_CURRENT (fn);
1560 /* Ideally, we wouldn't count copy constructors (or, in
1561 fact, any constructor that takes an argument of the
1562 class type as a parameter) because such things cannot
1563 be used to construct an instance of the class unless
1564 you already have one. But, for now at least, we're
1565 more generous. */
1566 if (! TREE_PRIVATE (ctor))
1568 nonprivate_ctor = 1;
1569 break;
1573 if (nonprivate_ctor == 0)
1575 warning (0, "%q#T only defines private constructors and has no friends",
1577 return;
1582 static struct {
1583 gt_pointer_operator new_value;
1584 void *cookie;
1585 } resort_data;
1587 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1589 static int
1590 method_name_cmp (const void* m1_p, const void* m2_p)
1592 const tree *const m1 = m1_p;
1593 const tree *const m2 = m2_p;
1595 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1596 return 0;
1597 if (*m1 == NULL_TREE)
1598 return -1;
1599 if (*m2 == NULL_TREE)
1600 return 1;
1601 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1602 return -1;
1603 return 1;
1606 /* This routine compares two fields like method_name_cmp but using the
1607 pointer operator in resort_field_decl_data. */
1609 static int
1610 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1612 const tree *const m1 = m1_p;
1613 const tree *const m2 = m2_p;
1614 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1615 return 0;
1616 if (*m1 == NULL_TREE)
1617 return -1;
1618 if (*m2 == NULL_TREE)
1619 return 1;
1621 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1622 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1623 resort_data.new_value (&d1, resort_data.cookie);
1624 resort_data.new_value (&d2, resort_data.cookie);
1625 if (d1 < d2)
1626 return -1;
1628 return 1;
1631 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1633 void
1634 resort_type_method_vec (void* obj,
1635 void* orig_obj ATTRIBUTE_UNUSED ,
1636 gt_pointer_operator new_value,
1637 void* cookie)
1639 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1640 int len = VEC_length (tree, method_vec);
1641 size_t slot;
1642 tree fn;
1644 /* The type conversion ops have to live at the front of the vec, so we
1645 can't sort them. */
1646 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1647 VEC_iterate (tree, method_vec, slot, fn);
1648 ++slot)
1649 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1650 break;
1652 if (len - slot > 1)
1654 resort_data.new_value = new_value;
1655 resort_data.cookie = cookie;
1656 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1657 resort_method_name_cmp);
1661 /* Warn about duplicate methods in fn_fields.
1663 Sort methods that are not special (i.e., constructors, destructors,
1664 and type conversion operators) so that we can find them faster in
1665 search. */
1667 static void
1668 finish_struct_methods (tree t)
1670 tree fn_fields;
1671 VEC(tree,gc) *method_vec;
1672 int slot, len;
1674 method_vec = CLASSTYPE_METHOD_VEC (t);
1675 if (!method_vec)
1676 return;
1678 len = VEC_length (tree, method_vec);
1680 /* Clear DECL_IN_AGGR_P for all functions. */
1681 for (fn_fields = TYPE_METHODS (t); fn_fields;
1682 fn_fields = TREE_CHAIN (fn_fields))
1683 DECL_IN_AGGR_P (fn_fields) = 0;
1685 /* Issue warnings about private constructors and such. If there are
1686 no methods, then some public defaults are generated. */
1687 maybe_warn_about_overly_private_class (t);
1689 /* The type conversion ops have to live at the front of the vec, so we
1690 can't sort them. */
1691 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1692 VEC_iterate (tree, method_vec, slot, fn_fields);
1693 ++slot)
1694 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1695 break;
1696 if (len - slot > 1)
1697 qsort (VEC_address (tree, method_vec) + slot,
1698 len-slot, sizeof (tree), method_name_cmp);
1701 /* Make BINFO's vtable have N entries, including RTTI entries,
1702 vbase and vcall offsets, etc. Set its type and call the backend
1703 to lay it out. */
1705 static void
1706 layout_vtable_decl (tree binfo, int n)
1708 tree atype;
1709 tree vtable;
1711 atype = build_cplus_array_type (vtable_entry_type,
1712 build_index_type (size_int (n - 1)));
1713 layout_type (atype);
1715 /* We may have to grow the vtable. */
1716 vtable = get_vtbl_decl_for_binfo (binfo);
1717 if (!same_type_p (TREE_TYPE (vtable), atype))
1719 TREE_TYPE (vtable) = atype;
1720 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1721 layout_decl (vtable, 0);
1725 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1726 have the same signature. */
1729 same_signature_p (tree fndecl, tree base_fndecl)
1731 /* One destructor overrides another if they are the same kind of
1732 destructor. */
1733 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1734 && special_function_p (base_fndecl) == special_function_p (fndecl))
1735 return 1;
1736 /* But a non-destructor never overrides a destructor, nor vice
1737 versa, nor do different kinds of destructors override
1738 one-another. For example, a complete object destructor does not
1739 override a deleting destructor. */
1740 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1741 return 0;
1743 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1744 || (DECL_CONV_FN_P (fndecl)
1745 && DECL_CONV_FN_P (base_fndecl)
1746 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1747 DECL_CONV_FN_TYPE (base_fndecl))))
1749 tree types, base_types;
1750 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1751 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1752 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
1753 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
1754 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1755 return 1;
1757 return 0;
1760 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1761 subobject. */
1763 static bool
1764 base_derived_from (tree derived, tree base)
1766 tree probe;
1768 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1770 if (probe == derived)
1771 return true;
1772 else if (BINFO_VIRTUAL_P (probe))
1773 /* If we meet a virtual base, we can't follow the inheritance
1774 any more. See if the complete type of DERIVED contains
1775 such a virtual base. */
1776 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1777 != NULL_TREE);
1779 return false;
1782 typedef struct find_final_overrider_data_s {
1783 /* The function for which we are trying to find a final overrider. */
1784 tree fn;
1785 /* The base class in which the function was declared. */
1786 tree declaring_base;
1787 /* The candidate overriders. */
1788 tree candidates;
1789 /* Path to most derived. */
1790 VEC(tree,heap) *path;
1791 } find_final_overrider_data;
1793 /* Add the overrider along the current path to FFOD->CANDIDATES.
1794 Returns true if an overrider was found; false otherwise. */
1796 static bool
1797 dfs_find_final_overrider_1 (tree binfo,
1798 find_final_overrider_data *ffod,
1799 unsigned depth)
1801 tree method;
1803 /* If BINFO is not the most derived type, try a more derived class.
1804 A definition there will overrider a definition here. */
1805 if (depth)
1807 depth--;
1808 if (dfs_find_final_overrider_1
1809 (VEC_index (tree, ffod->path, depth), ffod, depth))
1810 return true;
1813 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
1814 if (method)
1816 tree *candidate = &ffod->candidates;
1818 /* Remove any candidates overridden by this new function. */
1819 while (*candidate)
1821 /* If *CANDIDATE overrides METHOD, then METHOD
1822 cannot override anything else on the list. */
1823 if (base_derived_from (TREE_VALUE (*candidate), binfo))
1824 return true;
1825 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
1826 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
1827 *candidate = TREE_CHAIN (*candidate);
1828 else
1829 candidate = &TREE_CHAIN (*candidate);
1832 /* Add the new function. */
1833 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
1834 return true;
1837 return false;
1840 /* Called from find_final_overrider via dfs_walk. */
1842 static tree
1843 dfs_find_final_overrider_pre (tree binfo, void *data)
1845 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1847 if (binfo == ffod->declaring_base)
1848 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
1849 VEC_safe_push (tree, heap, ffod->path, binfo);
1851 return NULL_TREE;
1854 static tree
1855 dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
1857 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1858 VEC_pop (tree, ffod->path);
1860 return NULL_TREE;
1863 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
1864 FN and whose TREE_VALUE is the binfo for the base where the
1865 overriding occurs. BINFO (in the hierarchy dominated by the binfo
1866 DERIVED) is the base object in which FN is declared. */
1868 static tree
1869 find_final_overrider (tree derived, tree binfo, tree fn)
1871 find_final_overrider_data ffod;
1873 /* Getting this right is a little tricky. This is valid:
1875 struct S { virtual void f (); };
1876 struct T { virtual void f (); };
1877 struct U : public S, public T { };
1879 even though calling `f' in `U' is ambiguous. But,
1881 struct R { virtual void f(); };
1882 struct S : virtual public R { virtual void f (); };
1883 struct T : virtual public R { virtual void f (); };
1884 struct U : public S, public T { };
1886 is not -- there's no way to decide whether to put `S::f' or
1887 `T::f' in the vtable for `R'.
1889 The solution is to look at all paths to BINFO. If we find
1890 different overriders along any two, then there is a problem. */
1891 if (DECL_THUNK_P (fn))
1892 fn = THUNK_TARGET (fn);
1894 /* Determine the depth of the hierarchy. */
1895 ffod.fn = fn;
1896 ffod.declaring_base = binfo;
1897 ffod.candidates = NULL_TREE;
1898 ffod.path = VEC_alloc (tree, heap, 30);
1900 dfs_walk_all (derived, dfs_find_final_overrider_pre,
1901 dfs_find_final_overrider_post, &ffod);
1903 VEC_free (tree, heap, ffod.path);
1905 /* If there was no winner, issue an error message. */
1906 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
1908 error ("no unique final overrider for %qD in %qT", fn,
1909 BINFO_TYPE (derived));
1910 return error_mark_node;
1913 return ffod.candidates;
1916 /* Return the index of the vcall offset for FN when TYPE is used as a
1917 virtual base. */
1919 static tree
1920 get_vcall_index (tree fn, tree type)
1922 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
1923 tree_pair_p p;
1924 unsigned ix;
1926 for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++)
1927 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
1928 || same_signature_p (fn, p->purpose))
1929 return p->value;
1931 /* There should always be an appropriate index. */
1932 gcc_unreachable ();
1935 /* Update an entry in the vtable for BINFO, which is in the hierarchy
1936 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
1937 corresponding position in the BINFO_VIRTUALS list. */
1939 static void
1940 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
1941 unsigned ix)
1943 tree b;
1944 tree overrider;
1945 tree delta;
1946 tree virtual_base;
1947 tree first_defn;
1948 tree overrider_fn, overrider_target;
1949 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
1950 tree over_return, base_return;
1951 bool lost = false;
1953 /* Find the nearest primary base (possibly binfo itself) which defines
1954 this function; this is the class the caller will convert to when
1955 calling FN through BINFO. */
1956 for (b = binfo; ; b = get_primary_binfo (b))
1958 gcc_assert (b);
1959 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
1960 break;
1962 /* The nearest definition is from a lost primary. */
1963 if (BINFO_LOST_PRIMARY_P (b))
1964 lost = true;
1966 first_defn = b;
1968 /* Find the final overrider. */
1969 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
1970 if (overrider == error_mark_node)
1971 return;
1972 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
1974 /* Check for adjusting covariant return types. */
1975 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
1976 base_return = TREE_TYPE (TREE_TYPE (target_fn));
1978 if (POINTER_TYPE_P (over_return)
1979 && TREE_CODE (over_return) == TREE_CODE (base_return)
1980 && CLASS_TYPE_P (TREE_TYPE (over_return))
1981 && CLASS_TYPE_P (TREE_TYPE (base_return)))
1983 /* If FN is a covariant thunk, we must figure out the adjustment
1984 to the final base FN was converting to. As OVERRIDER_TARGET might
1985 also be converting to the return type of FN, we have to
1986 combine the two conversions here. */
1987 tree fixed_offset, virtual_offset;
1989 over_return = TREE_TYPE (over_return);
1990 base_return = TREE_TYPE (base_return);
1992 if (DECL_THUNK_P (fn))
1994 gcc_assert (DECL_RESULT_THUNK_P (fn));
1995 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
1996 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
1998 else
1999 fixed_offset = virtual_offset = NULL_TREE;
2001 if (virtual_offset)
2002 /* Find the equivalent binfo within the return type of the
2003 overriding function. We will want the vbase offset from
2004 there. */
2005 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2006 over_return);
2007 else if (!same_type_ignoring_top_level_qualifiers_p
2008 (over_return, base_return))
2010 /* There was no existing virtual thunk (which takes
2011 precedence). So find the binfo of the base function's
2012 return type within the overriding function's return type.
2013 We cannot call lookup base here, because we're inside a
2014 dfs_walk, and will therefore clobber the BINFO_MARKED
2015 flags. Fortunately we know the covariancy is valid (it
2016 has already been checked), so we can just iterate along
2017 the binfos, which have been chained in inheritance graph
2018 order. Of course it is lame that we have to repeat the
2019 search here anyway -- we should really be caching pieces
2020 of the vtable and avoiding this repeated work. */
2021 tree thunk_binfo, base_binfo;
2023 /* Find the base binfo within the overriding function's
2024 return type. We will always find a thunk_binfo, except
2025 when the covariancy is invalid (which we will have
2026 already diagnosed). */
2027 for (base_binfo = TYPE_BINFO (base_return),
2028 thunk_binfo = TYPE_BINFO (over_return);
2029 thunk_binfo;
2030 thunk_binfo = TREE_CHAIN (thunk_binfo))
2031 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2032 BINFO_TYPE (base_binfo)))
2033 break;
2035 /* See if virtual inheritance is involved. */
2036 for (virtual_offset = thunk_binfo;
2037 virtual_offset;
2038 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2039 if (BINFO_VIRTUAL_P (virtual_offset))
2040 break;
2042 if (virtual_offset
2043 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2045 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2047 if (virtual_offset)
2049 /* We convert via virtual base. Adjust the fixed
2050 offset to be from there. */
2051 offset = size_diffop
2052 (offset, convert
2053 (ssizetype, BINFO_OFFSET (virtual_offset)));
2055 if (fixed_offset)
2056 /* There was an existing fixed offset, this must be
2057 from the base just converted to, and the base the
2058 FN was thunking to. */
2059 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2060 else
2061 fixed_offset = offset;
2065 if (fixed_offset || virtual_offset)
2066 /* Replace the overriding function with a covariant thunk. We
2067 will emit the overriding function in its own slot as
2068 well. */
2069 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2070 fixed_offset, virtual_offset);
2072 else
2073 gcc_assert (!DECL_THUNK_P (fn));
2075 /* Assume that we will produce a thunk that convert all the way to
2076 the final overrider, and not to an intermediate virtual base. */
2077 virtual_base = NULL_TREE;
2079 /* See if we can convert to an intermediate virtual base first, and then
2080 use the vcall offset located there to finish the conversion. */
2081 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2083 /* If we find the final overrider, then we can stop
2084 walking. */
2085 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2086 BINFO_TYPE (TREE_VALUE (overrider))))
2087 break;
2089 /* If we find a virtual base, and we haven't yet found the
2090 overrider, then there is a virtual base between the
2091 declaring base (first_defn) and the final overrider. */
2092 if (BINFO_VIRTUAL_P (b))
2094 virtual_base = b;
2095 break;
2099 if (overrider_fn != overrider_target && !virtual_base)
2101 /* The ABI specifies that a covariant thunk includes a mangling
2102 for a this pointer adjustment. This-adjusting thunks that
2103 override a function from a virtual base have a vcall
2104 adjustment. When the virtual base in question is a primary
2105 virtual base, we know the adjustments are zero, (and in the
2106 non-covariant case, we would not use the thunk).
2107 Unfortunately we didn't notice this could happen, when
2108 designing the ABI and so never mandated that such a covariant
2109 thunk should be emitted. Because we must use the ABI mandated
2110 name, we must continue searching from the binfo where we
2111 found the most recent definition of the function, towards the
2112 primary binfo which first introduced the function into the
2113 vtable. If that enters a virtual base, we must use a vcall
2114 this-adjusting thunk. Bleah! */
2115 tree probe = first_defn;
2117 while ((probe = get_primary_binfo (probe))
2118 && (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix)
2119 if (BINFO_VIRTUAL_P (probe))
2120 virtual_base = probe;
2122 if (virtual_base)
2123 /* Even if we find a virtual base, the correct delta is
2124 between the overrider and the binfo we're building a vtable
2125 for. */
2126 goto virtual_covariant;
2129 /* Compute the constant adjustment to the `this' pointer. The
2130 `this' pointer, when this function is called, will point at BINFO
2131 (or one of its primary bases, which are at the same offset). */
2132 if (virtual_base)
2133 /* The `this' pointer needs to be adjusted from the declaration to
2134 the nearest virtual base. */
2135 delta = size_diffop (convert (ssizetype, BINFO_OFFSET (virtual_base)),
2136 convert (ssizetype, BINFO_OFFSET (first_defn)));
2137 else if (lost)
2138 /* If the nearest definition is in a lost primary, we don't need an
2139 entry in our vtable. Except possibly in a constructor vtable,
2140 if we happen to get our primary back. In that case, the offset
2141 will be zero, as it will be a primary base. */
2142 delta = size_zero_node;
2143 else
2144 /* The `this' pointer needs to be adjusted from pointing to
2145 BINFO to pointing at the base where the final overrider
2146 appears. */
2147 virtual_covariant:
2148 delta = size_diffop (convert (ssizetype,
2149 BINFO_OFFSET (TREE_VALUE (overrider))),
2150 convert (ssizetype, BINFO_OFFSET (binfo)));
2152 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2154 if (virtual_base)
2155 BV_VCALL_INDEX (*virtuals)
2156 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2157 else
2158 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2161 /* Called from modify_all_vtables via dfs_walk. */
2163 static tree
2164 dfs_modify_vtables (tree binfo, void* data)
2166 tree t = (tree) data;
2167 tree virtuals;
2168 tree old_virtuals;
2169 unsigned ix;
2171 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2172 /* A base without a vtable needs no modification, and its bases
2173 are uninteresting. */
2174 return dfs_skip_bases;
2176 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2177 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2178 /* Don't do the primary vtable, if it's new. */
2179 return NULL_TREE;
2181 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2182 /* There's no need to modify the vtable for a non-virtual primary
2183 base; we're not going to use that vtable anyhow. We do still
2184 need to do this for virtual primary bases, as they could become
2185 non-primary in a construction vtable. */
2186 return NULL_TREE;
2188 make_new_vtable (t, binfo);
2190 /* Now, go through each of the virtual functions in the virtual
2191 function table for BINFO. Find the final overrider, and update
2192 the BINFO_VIRTUALS list appropriately. */
2193 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2194 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2195 virtuals;
2196 ix++, virtuals = TREE_CHAIN (virtuals),
2197 old_virtuals = TREE_CHAIN (old_virtuals))
2198 update_vtable_entry_for_fn (t,
2199 binfo,
2200 BV_FN (old_virtuals),
2201 &virtuals, ix);
2203 return NULL_TREE;
2206 /* Update all of the primary and secondary vtables for T. Create new
2207 vtables as required, and initialize their RTTI information. Each
2208 of the functions in VIRTUALS is declared in T and may override a
2209 virtual function from a base class; find and modify the appropriate
2210 entries to point to the overriding functions. Returns a list, in
2211 declaration order, of the virtual functions that are declared in T,
2212 but do not appear in the primary base class vtable, and which
2213 should therefore be appended to the end of the vtable for T. */
2215 static tree
2216 modify_all_vtables (tree t, tree virtuals)
2218 tree binfo = TYPE_BINFO (t);
2219 tree *fnsp;
2221 /* Update all of the vtables. */
2222 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2224 /* Add virtual functions not already in our primary vtable. These
2225 will be both those introduced by this class, and those overridden
2226 from secondary bases. It does not include virtuals merely
2227 inherited from secondary bases. */
2228 for (fnsp = &virtuals; *fnsp; )
2230 tree fn = TREE_VALUE (*fnsp);
2232 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2233 || DECL_VINDEX (fn) == error_mark_node)
2235 /* We don't need to adjust the `this' pointer when
2236 calling this function. */
2237 BV_DELTA (*fnsp) = integer_zero_node;
2238 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2240 /* This is a function not already in our vtable. Keep it. */
2241 fnsp = &TREE_CHAIN (*fnsp);
2243 else
2244 /* We've already got an entry for this function. Skip it. */
2245 *fnsp = TREE_CHAIN (*fnsp);
2248 return virtuals;
2251 /* Get the base virtual function declarations in T that have the
2252 indicated NAME. */
2254 static tree
2255 get_basefndecls (tree name, tree t)
2257 tree methods;
2258 tree base_fndecls = NULL_TREE;
2259 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2260 int i;
2262 /* Find virtual functions in T with the indicated NAME. */
2263 i = lookup_fnfields_1 (t, name);
2264 if (i != -1)
2265 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2266 methods;
2267 methods = OVL_NEXT (methods))
2269 tree method = OVL_CURRENT (methods);
2271 if (TREE_CODE (method) == FUNCTION_DECL
2272 && DECL_VINDEX (method))
2273 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2276 if (base_fndecls)
2277 return base_fndecls;
2279 for (i = 0; i < n_baseclasses; i++)
2281 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2282 base_fndecls = chainon (get_basefndecls (name, basetype),
2283 base_fndecls);
2286 return base_fndecls;
2289 /* If this declaration supersedes the declaration of
2290 a method declared virtual in the base class, then
2291 mark this field as being virtual as well. */
2293 void
2294 check_for_override (tree decl, tree ctype)
2296 if (TREE_CODE (decl) == TEMPLATE_DECL)
2297 /* In [temp.mem] we have:
2299 A specialization of a member function template does not
2300 override a virtual function from a base class. */
2301 return;
2302 if ((DECL_DESTRUCTOR_P (decl)
2303 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2304 || DECL_CONV_FN_P (decl))
2305 && look_for_overrides (ctype, decl)
2306 && !DECL_STATIC_FUNCTION_P (decl))
2307 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2308 the error_mark_node so that we know it is an overriding
2309 function. */
2310 DECL_VINDEX (decl) = decl;
2312 if (DECL_VIRTUAL_P (decl))
2314 if (!DECL_VINDEX (decl))
2315 DECL_VINDEX (decl) = error_mark_node;
2316 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2320 /* Warn about hidden virtual functions that are not overridden in t.
2321 We know that constructors and destructors don't apply. */
2323 void
2324 warn_hidden (tree t)
2326 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2327 tree fns;
2328 size_t i;
2330 /* We go through each separately named virtual function. */
2331 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2332 VEC_iterate (tree, method_vec, i, fns);
2333 ++i)
2335 tree fn;
2336 tree name;
2337 tree fndecl;
2338 tree base_fndecls;
2339 tree base_binfo;
2340 tree binfo;
2341 int j;
2343 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2344 have the same name. Figure out what name that is. */
2345 name = DECL_NAME (OVL_CURRENT (fns));
2346 /* There are no possibly hidden functions yet. */
2347 base_fndecls = NULL_TREE;
2348 /* Iterate through all of the base classes looking for possibly
2349 hidden functions. */
2350 for (binfo = TYPE_BINFO (t), j = 0;
2351 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2353 tree basetype = BINFO_TYPE (base_binfo);
2354 base_fndecls = chainon (get_basefndecls (name, basetype),
2355 base_fndecls);
2358 /* If there are no functions to hide, continue. */
2359 if (!base_fndecls)
2360 continue;
2362 /* Remove any overridden functions. */
2363 for (fn = fns; fn; fn = OVL_NEXT (fn))
2365 fndecl = OVL_CURRENT (fn);
2366 if (DECL_VINDEX (fndecl))
2368 tree *prev = &base_fndecls;
2370 while (*prev)
2371 /* If the method from the base class has the same
2372 signature as the method from the derived class, it
2373 has been overridden. */
2374 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2375 *prev = TREE_CHAIN (*prev);
2376 else
2377 prev = &TREE_CHAIN (*prev);
2381 /* Now give a warning for all base functions without overriders,
2382 as they are hidden. */
2383 while (base_fndecls)
2385 /* Here we know it is a hider, and no overrider exists. */
2386 warning (0, "%q+D was hidden", TREE_VALUE (base_fndecls));
2387 warning (0, " by %q+D", fns);
2388 base_fndecls = TREE_CHAIN (base_fndecls);
2393 /* Check for things that are invalid. There are probably plenty of other
2394 things we should check for also. */
2396 static void
2397 finish_struct_anon (tree t)
2399 tree field;
2401 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2403 if (TREE_STATIC (field))
2404 continue;
2405 if (TREE_CODE (field) != FIELD_DECL)
2406 continue;
2408 if (DECL_NAME (field) == NULL_TREE
2409 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2411 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2412 for (; elt; elt = TREE_CHAIN (elt))
2414 /* We're generally only interested in entities the user
2415 declared, but we also find nested classes by noticing
2416 the TYPE_DECL that we create implicitly. You're
2417 allowed to put one anonymous union inside another,
2418 though, so we explicitly tolerate that. We use
2419 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2420 we also allow unnamed types used for defining fields. */
2421 if (DECL_ARTIFICIAL (elt)
2422 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2423 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2424 continue;
2426 if (TREE_CODE (elt) != FIELD_DECL)
2428 pedwarn ("%q+#D invalid; an anonymous union can "
2429 "only have non-static data members", elt);
2430 continue;
2433 if (TREE_PRIVATE (elt))
2434 pedwarn ("private member %q+#D in anonymous union", elt);
2435 else if (TREE_PROTECTED (elt))
2436 pedwarn ("protected member %q+#D in anonymous union", elt);
2438 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2439 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2445 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2446 will be used later during class template instantiation.
2447 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2448 a non-static member data (FIELD_DECL), a member function
2449 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2450 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2451 When FRIEND_P is nonzero, T is either a friend class
2452 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2453 (FUNCTION_DECL, TEMPLATE_DECL). */
2455 void
2456 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2458 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2459 if (CLASSTYPE_TEMPLATE_INFO (type))
2460 CLASSTYPE_DECL_LIST (type)
2461 = tree_cons (friend_p ? NULL_TREE : type,
2462 t, CLASSTYPE_DECL_LIST (type));
2465 /* Create default constructors, assignment operators, and so forth for
2466 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2467 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2468 the class cannot have a default constructor, copy constructor
2469 taking a const reference argument, or an assignment operator taking
2470 a const reference, respectively. */
2472 static void
2473 add_implicitly_declared_members (tree t,
2474 int cant_have_const_cctor,
2475 int cant_have_const_assignment)
2477 /* Destructor. */
2478 if (!CLASSTYPE_DESTRUCTORS (t))
2480 /* In general, we create destructors lazily. */
2481 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2482 /* However, if the implicit destructor is non-trivial
2483 destructor, we sometimes have to create it at this point. */
2484 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2486 bool lazy_p = true;
2488 if (TYPE_FOR_JAVA (t))
2489 /* If this a Java class, any non-trivial destructor is
2490 invalid, even if compiler-generated. Therefore, if the
2491 destructor is non-trivial we create it now. */
2492 lazy_p = false;
2493 else
2495 tree binfo;
2496 tree base_binfo;
2497 int ix;
2499 /* If the implicit destructor will be virtual, then we must
2500 generate it now because (unfortunately) we do not
2501 generate virtual tables lazily. */
2502 binfo = TYPE_BINFO (t);
2503 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2505 tree base_type;
2506 tree dtor;
2508 base_type = BINFO_TYPE (base_binfo);
2509 dtor = CLASSTYPE_DESTRUCTORS (base_type);
2510 if (dtor && DECL_VIRTUAL_P (dtor))
2512 lazy_p = false;
2513 break;
2518 /* If we can't get away with being lazy, generate the destructor
2519 now. */
2520 if (!lazy_p)
2521 lazily_declare_fn (sfk_destructor, t);
2525 /* Default constructor. */
2526 if (! TYPE_HAS_CONSTRUCTOR (t))
2528 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2529 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2532 /* Copy constructor. */
2533 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2535 TYPE_HAS_INIT_REF (t) = 1;
2536 TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor;
2537 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2538 TYPE_HAS_CONSTRUCTOR (t) = 1;
2541 /* If there is no assignment operator, one will be created if and
2542 when it is needed. For now, just record whether or not the type
2543 of the parameter to the assignment operator will be a const or
2544 non-const reference. */
2545 if (!TYPE_HAS_ASSIGN_REF (t) && !TYPE_FOR_JAVA (t))
2547 TYPE_HAS_ASSIGN_REF (t) = 1;
2548 TYPE_HAS_CONST_ASSIGN_REF (t) = !cant_have_const_assignment;
2549 CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 1;
2553 /* Subroutine of finish_struct_1. Recursively count the number of fields
2554 in TYPE, including anonymous union members. */
2556 static int
2557 count_fields (tree fields)
2559 tree x;
2560 int n_fields = 0;
2561 for (x = fields; x; x = TREE_CHAIN (x))
2563 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2564 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2565 else
2566 n_fields += 1;
2568 return n_fields;
2571 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2572 TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */
2574 static int
2575 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2577 tree x;
2578 for (x = fields; x; x = TREE_CHAIN (x))
2580 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2581 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2582 else
2583 field_vec->elts[idx++] = x;
2585 return idx;
2588 /* FIELD is a bit-field. We are finishing the processing for its
2589 enclosing type. Issue any appropriate messages and set appropriate
2590 flags. */
2592 static void
2593 check_bitfield_decl (tree field)
2595 tree type = TREE_TYPE (field);
2596 tree w = NULL_TREE;
2598 /* Detect invalid bit-field type. */
2599 if (DECL_INITIAL (field)
2600 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2602 error ("bit-field %q+#D with non-integral type", field);
2603 w = error_mark_node;
2606 /* Detect and ignore out of range field width. */
2607 if (DECL_INITIAL (field))
2609 w = DECL_INITIAL (field);
2611 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2612 STRIP_NOPS (w);
2614 /* detect invalid field size. */
2615 w = integral_constant_value (w);
2617 if (TREE_CODE (w) != INTEGER_CST)
2619 error ("bit-field %q+D width not an integer constant", field);
2620 w = error_mark_node;
2622 else if (tree_int_cst_sgn (w) < 0)
2624 error ("negative width in bit-field %q+D", field);
2625 w = error_mark_node;
2627 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2629 error ("zero width for bit-field %q+D", field);
2630 w = error_mark_node;
2632 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2633 && TREE_CODE (type) != ENUMERAL_TYPE
2634 && TREE_CODE (type) != BOOLEAN_TYPE)
2635 warning (0, "width of %q+D exceeds its type", field);
2636 else if (TREE_CODE (type) == ENUMERAL_TYPE
2637 && (0 > compare_tree_int (w,
2638 min_precision (TYPE_MIN_VALUE (type),
2639 TYPE_UNSIGNED (type)))
2640 || 0 > compare_tree_int (w,
2641 min_precision
2642 (TYPE_MAX_VALUE (type),
2643 TYPE_UNSIGNED (type)))))
2644 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2647 /* Remove the bit-field width indicator so that the rest of the
2648 compiler does not treat that value as an initializer. */
2649 DECL_INITIAL (field) = NULL_TREE;
2651 if (w != error_mark_node)
2653 DECL_SIZE (field) = convert (bitsizetype, w);
2654 DECL_BIT_FIELD (field) = 1;
2656 else
2658 /* Non-bit-fields are aligned for their type. */
2659 DECL_BIT_FIELD (field) = 0;
2660 CLEAR_DECL_C_BIT_FIELD (field);
2664 /* FIELD is a non bit-field. We are finishing the processing for its
2665 enclosing type T. Issue any appropriate messages and set appropriate
2666 flags. */
2668 static void
2669 check_field_decl (tree field,
2670 tree t,
2671 int* cant_have_const_ctor,
2672 int* no_const_asn_ref,
2673 int* any_default_members)
2675 tree type = strip_array_types (TREE_TYPE (field));
2677 /* An anonymous union cannot contain any fields which would change
2678 the settings of CANT_HAVE_CONST_CTOR and friends. */
2679 if (ANON_UNION_TYPE_P (type))
2681 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
2682 structs. So, we recurse through their fields here. */
2683 else if (ANON_AGGR_TYPE_P (type))
2685 tree fields;
2687 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2688 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2689 check_field_decl (fields, t, cant_have_const_ctor,
2690 no_const_asn_ref, any_default_members);
2692 /* Check members with class type for constructors, destructors,
2693 etc. */
2694 else if (CLASS_TYPE_P (type))
2696 /* Never let anything with uninheritable virtuals
2697 make it through without complaint. */
2698 abstract_virtuals_error (field, type);
2700 if (TREE_CODE (t) == UNION_TYPE)
2702 if (TYPE_NEEDS_CONSTRUCTING (type))
2703 error ("member %q+#D with constructor not allowed in union",
2704 field);
2705 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2706 error ("member %q+#D with destructor not allowed in union", field);
2707 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2708 error ("member %q+#D with copy assignment operator not allowed in union",
2709 field);
2711 else
2713 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2714 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2715 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2716 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
2717 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
2720 if (!TYPE_HAS_CONST_INIT_REF (type))
2721 *cant_have_const_ctor = 1;
2723 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
2724 *no_const_asn_ref = 1;
2726 if (DECL_INITIAL (field) != NULL_TREE)
2728 /* `build_class_init_list' does not recognize
2729 non-FIELD_DECLs. */
2730 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
2731 error ("multiple fields in union %qT initialized", t);
2732 *any_default_members = 1;
2736 /* Check the data members (both static and non-static), class-scoped
2737 typedefs, etc., appearing in the declaration of T. Issue
2738 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
2739 declaration order) of access declarations; each TREE_VALUE in this
2740 list is a USING_DECL.
2742 In addition, set the following flags:
2744 EMPTY_P
2745 The class is empty, i.e., contains no non-static data members.
2747 CANT_HAVE_CONST_CTOR_P
2748 This class cannot have an implicitly generated copy constructor
2749 taking a const reference.
2751 CANT_HAVE_CONST_ASN_REF
2752 This class cannot have an implicitly generated assignment
2753 operator taking a const reference.
2755 All of these flags should be initialized before calling this
2756 function.
2758 Returns a pointer to the end of the TYPE_FIELDs chain; additional
2759 fields can be added by adding to this chain. */
2761 static void
2762 check_field_decls (tree t, tree *access_decls,
2763 int *cant_have_const_ctor_p,
2764 int *no_const_asn_ref_p)
2766 tree *field;
2767 tree *next;
2768 bool has_pointers;
2769 int any_default_members;
2771 /* Assume there are no access declarations. */
2772 *access_decls = NULL_TREE;
2773 /* Assume this class has no pointer members. */
2774 has_pointers = false;
2775 /* Assume none of the members of this class have default
2776 initializations. */
2777 any_default_members = 0;
2779 for (field = &TYPE_FIELDS (t); *field; field = next)
2781 tree x = *field;
2782 tree type = TREE_TYPE (x);
2784 next = &TREE_CHAIN (x);
2786 if (TREE_CODE (x) == FIELD_DECL)
2788 if (TYPE_PACKED (t))
2790 if (!pod_type_p (TREE_TYPE (x)) && !TYPE_PACKED (TREE_TYPE (x)))
2791 warning
2793 "ignoring packed attribute on unpacked non-POD field %q+#D",
2795 else
2796 DECL_PACKED (x) = 1;
2799 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
2800 /* We don't treat zero-width bitfields as making a class
2801 non-empty. */
2803 else
2805 tree element_type;
2807 /* The class is non-empty. */
2808 CLASSTYPE_EMPTY_P (t) = 0;
2809 /* The class is not even nearly empty. */
2810 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2811 /* If one of the data members contains an empty class,
2812 so does T. */
2813 element_type = strip_array_types (type);
2814 if (CLASS_TYPE_P (element_type)
2815 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
2816 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
2820 if (TREE_CODE (x) == USING_DECL)
2822 /* Prune the access declaration from the list of fields. */
2823 *field = TREE_CHAIN (x);
2825 /* Save the access declarations for our caller. */
2826 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
2828 /* Since we've reset *FIELD there's no reason to skip to the
2829 next field. */
2830 next = field;
2831 continue;
2834 if (TREE_CODE (x) == TYPE_DECL
2835 || TREE_CODE (x) == TEMPLATE_DECL)
2836 continue;
2838 /* If we've gotten this far, it's a data member, possibly static,
2839 or an enumerator. */
2840 DECL_CONTEXT (x) = t;
2842 /* When this goes into scope, it will be a non-local reference. */
2843 DECL_NONLOCAL (x) = 1;
2845 if (TREE_CODE (t) == UNION_TYPE)
2847 /* [class.union]
2849 If a union contains a static data member, or a member of
2850 reference type, the program is ill-formed. */
2851 if (TREE_CODE (x) == VAR_DECL)
2853 error ("%q+D may not be static because it is a member of a union", x);
2854 continue;
2856 if (TREE_CODE (type) == REFERENCE_TYPE)
2858 error ("%q+D may not have reference type %qT because"
2859 " it is a member of a union",
2860 x, type);
2861 continue;
2865 /* ``A local class cannot have static data members.'' ARM 9.4 */
2866 if (current_function_decl && TREE_STATIC (x))
2867 error ("field %q+D in local class cannot be static", x);
2869 /* Perform error checking that did not get done in
2870 grokdeclarator. */
2871 if (TREE_CODE (type) == FUNCTION_TYPE)
2873 error ("field %q+D invalidly declared function type", x);
2874 type = build_pointer_type (type);
2875 TREE_TYPE (x) = type;
2877 else if (TREE_CODE (type) == METHOD_TYPE)
2879 error ("field %q+D invalidly declared method type", x);
2880 type = build_pointer_type (type);
2881 TREE_TYPE (x) = type;
2884 if (type == error_mark_node)
2885 continue;
2887 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
2888 continue;
2890 /* Now it can only be a FIELD_DECL. */
2892 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
2893 CLASSTYPE_NON_AGGREGATE (t) = 1;
2895 /* If this is of reference type, check if it needs an init.
2896 Also do a little ANSI jig if necessary. */
2897 if (TREE_CODE (type) == REFERENCE_TYPE)
2899 CLASSTYPE_NON_POD_P (t) = 1;
2900 if (DECL_INITIAL (x) == NULL_TREE)
2901 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
2903 /* ARM $12.6.2: [A member initializer list] (or, for an
2904 aggregate, initialization by a brace-enclosed list) is the
2905 only way to initialize nonstatic const and reference
2906 members. */
2907 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
2909 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
2910 && extra_warnings)
2911 warning (0, "non-static reference %q+#D in class without a constructor", x);
2914 type = strip_array_types (type);
2916 /* This is used by -Weffc++ (see below). Warn only for pointers
2917 to members which might hold dynamic memory. So do not warn
2918 for pointers to functions or pointers to members. */
2919 if (TYPE_PTR_P (type)
2920 && !TYPE_PTRFN_P (type)
2921 && !TYPE_PTR_TO_MEMBER_P (type))
2922 has_pointers = true;
2924 if (CLASS_TYPE_P (type))
2926 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
2927 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
2928 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
2929 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
2932 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
2933 CLASSTYPE_HAS_MUTABLE (t) = 1;
2935 if (! pod_type_p (type))
2936 /* DR 148 now allows pointers to members (which are POD themselves),
2937 to be allowed in POD structs. */
2938 CLASSTYPE_NON_POD_P (t) = 1;
2940 if (! zero_init_p (type))
2941 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
2943 /* If any field is const, the structure type is pseudo-const. */
2944 if (CP_TYPE_CONST_P (type))
2946 C_TYPE_FIELDS_READONLY (t) = 1;
2947 if (DECL_INITIAL (x) == NULL_TREE)
2948 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
2950 /* ARM $12.6.2: [A member initializer list] (or, for an
2951 aggregate, initialization by a brace-enclosed list) is the
2952 only way to initialize nonstatic const and reference
2953 members. */
2954 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
2956 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
2957 && extra_warnings)
2958 warning (0, "non-static const member %q+#D in class without a constructor", x);
2960 /* A field that is pseudo-const makes the structure likewise. */
2961 else if (CLASS_TYPE_P (type))
2963 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
2964 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
2965 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2966 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
2969 /* Core issue 80: A nonstatic data member is required to have a
2970 different name from the class iff the class has a
2971 user-defined constructor. */
2972 if (constructor_name_p (DECL_NAME (x), t) && TYPE_HAS_CONSTRUCTOR (t))
2973 pedwarn ("field %q+#D with same name as class", x);
2975 /* We set DECL_C_BIT_FIELD in grokbitfield.
2976 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
2977 if (DECL_C_BIT_FIELD (x))
2978 check_bitfield_decl (x);
2979 else
2980 check_field_decl (x, t,
2981 cant_have_const_ctor_p,
2982 no_const_asn_ref_p,
2983 &any_default_members);
2986 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
2987 it should also define a copy constructor and an assignment operator to
2988 implement the correct copy semantic (deep vs shallow, etc.). As it is
2989 not feasible to check whether the constructors do allocate dynamic memory
2990 and store it within members, we approximate the warning like this:
2992 -- Warn only if there are members which are pointers
2993 -- Warn only if there is a non-trivial constructor (otherwise,
2994 there cannot be memory allocated).
2995 -- Warn only if there is a non-trivial destructor. We assume that the
2996 user at least implemented the cleanup correctly, and a destructor
2997 is needed to free dynamic memory.
2999 This seems enough for practical purposes. */
3000 if (warn_ecpp
3001 && has_pointers
3002 && TYPE_HAS_CONSTRUCTOR (t)
3003 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3004 && !(TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3006 warning (0, "%q#T has pointer data members", t);
3008 if (! TYPE_HAS_INIT_REF (t))
3010 warning (0, " but does not override %<%T(const %T&)%>", t, t);
3011 if (! TYPE_HAS_ASSIGN_REF (t))
3012 warning (0, " or %<operator=(const %T&)%>", t);
3014 else if (! TYPE_HAS_ASSIGN_REF (t))
3015 warning (0, " but does not override %<operator=(const %T&)%>", t);
3019 /* Check anonymous struct/anonymous union fields. */
3020 finish_struct_anon (t);
3022 /* We've built up the list of access declarations in reverse order.
3023 Fix that now. */
3024 *access_decls = nreverse (*access_decls);
3027 /* If TYPE is an empty class type, records its OFFSET in the table of
3028 OFFSETS. */
3030 static int
3031 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3033 splay_tree_node n;
3035 if (!is_empty_class (type))
3036 return 0;
3038 /* Record the location of this empty object in OFFSETS. */
3039 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3040 if (!n)
3041 n = splay_tree_insert (offsets,
3042 (splay_tree_key) offset,
3043 (splay_tree_value) NULL_TREE);
3044 n->value = ((splay_tree_value)
3045 tree_cons (NULL_TREE,
3046 type,
3047 (tree) n->value));
3049 return 0;
3052 /* Returns nonzero if TYPE is an empty class type and there is
3053 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3055 static int
3056 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3058 splay_tree_node n;
3059 tree t;
3061 if (!is_empty_class (type))
3062 return 0;
3064 /* Record the location of this empty object in OFFSETS. */
3065 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3066 if (!n)
3067 return 0;
3069 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3070 if (same_type_p (TREE_VALUE (t), type))
3071 return 1;
3073 return 0;
3076 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3077 F for every subobject, passing it the type, offset, and table of
3078 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3079 be traversed.
3081 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3082 than MAX_OFFSET will not be walked.
3084 If F returns a nonzero value, the traversal ceases, and that value
3085 is returned. Otherwise, returns zero. */
3087 static int
3088 walk_subobject_offsets (tree type,
3089 subobject_offset_fn f,
3090 tree offset,
3091 splay_tree offsets,
3092 tree max_offset,
3093 int vbases_p)
3095 int r = 0;
3096 tree type_binfo = NULL_TREE;
3098 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3099 stop. */
3100 if (max_offset && INT_CST_LT (max_offset, offset))
3101 return 0;
3103 if (!TYPE_P (type))
3105 if (abi_version_at_least (2))
3106 type_binfo = type;
3107 type = BINFO_TYPE (type);
3110 if (CLASS_TYPE_P (type))
3112 tree field;
3113 tree binfo;
3114 int i;
3116 /* Avoid recursing into objects that are not interesting. */
3117 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3118 return 0;
3120 /* Record the location of TYPE. */
3121 r = (*f) (type, offset, offsets);
3122 if (r)
3123 return r;
3125 /* Iterate through the direct base classes of TYPE. */
3126 if (!type_binfo)
3127 type_binfo = TYPE_BINFO (type);
3128 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3130 tree binfo_offset;
3132 if (abi_version_at_least (2)
3133 && BINFO_VIRTUAL_P (binfo))
3134 continue;
3136 if (!vbases_p
3137 && BINFO_VIRTUAL_P (binfo)
3138 && !BINFO_PRIMARY_P (binfo))
3139 continue;
3141 if (!abi_version_at_least (2))
3142 binfo_offset = size_binop (PLUS_EXPR,
3143 offset,
3144 BINFO_OFFSET (binfo));
3145 else
3147 tree orig_binfo;
3148 /* We cannot rely on BINFO_OFFSET being set for the base
3149 class yet, but the offsets for direct non-virtual
3150 bases can be calculated by going back to the TYPE. */
3151 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3152 binfo_offset = size_binop (PLUS_EXPR,
3153 offset,
3154 BINFO_OFFSET (orig_binfo));
3157 r = walk_subobject_offsets (binfo,
3159 binfo_offset,
3160 offsets,
3161 max_offset,
3162 (abi_version_at_least (2)
3163 ? /*vbases_p=*/0 : vbases_p));
3164 if (r)
3165 return r;
3168 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3170 unsigned ix;
3171 VEC(tree,gc) *vbases;
3173 /* Iterate through the virtual base classes of TYPE. In G++
3174 3.2, we included virtual bases in the direct base class
3175 loop above, which results in incorrect results; the
3176 correct offsets for virtual bases are only known when
3177 working with the most derived type. */
3178 if (vbases_p)
3179 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3180 VEC_iterate (tree, vbases, ix, binfo); ix++)
3182 r = walk_subobject_offsets (binfo,
3184 size_binop (PLUS_EXPR,
3185 offset,
3186 BINFO_OFFSET (binfo)),
3187 offsets,
3188 max_offset,
3189 /*vbases_p=*/0);
3190 if (r)
3191 return r;
3193 else
3195 /* We still have to walk the primary base, if it is
3196 virtual. (If it is non-virtual, then it was walked
3197 above.) */
3198 tree vbase = get_primary_binfo (type_binfo);
3200 if (vbase && BINFO_VIRTUAL_P (vbase)
3201 && BINFO_PRIMARY_P (vbase)
3202 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3204 r = (walk_subobject_offsets
3205 (vbase, f, offset,
3206 offsets, max_offset, /*vbases_p=*/0));
3207 if (r)
3208 return r;
3213 /* Iterate through the fields of TYPE. */
3214 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3215 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3217 tree field_offset;
3219 if (abi_version_at_least (2))
3220 field_offset = byte_position (field);
3221 else
3222 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3223 field_offset = DECL_FIELD_OFFSET (field);
3225 r = walk_subobject_offsets (TREE_TYPE (field),
3227 size_binop (PLUS_EXPR,
3228 offset,
3229 field_offset),
3230 offsets,
3231 max_offset,
3232 /*vbases_p=*/1);
3233 if (r)
3234 return r;
3237 else if (TREE_CODE (type) == ARRAY_TYPE)
3239 tree element_type = strip_array_types (type);
3240 tree domain = TYPE_DOMAIN (type);
3241 tree index;
3243 /* Avoid recursing into objects that are not interesting. */
3244 if (!CLASS_TYPE_P (element_type)
3245 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3246 return 0;
3248 /* Step through each of the elements in the array. */
3249 for (index = size_zero_node;
3250 /* G++ 3.2 had an off-by-one error here. */
3251 (abi_version_at_least (2)
3252 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3253 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3254 index = size_binop (PLUS_EXPR, index, size_one_node))
3256 r = walk_subobject_offsets (TREE_TYPE (type),
3258 offset,
3259 offsets,
3260 max_offset,
3261 /*vbases_p=*/1);
3262 if (r)
3263 return r;
3264 offset = size_binop (PLUS_EXPR, offset,
3265 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3266 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3267 there's no point in iterating through the remaining
3268 elements of the array. */
3269 if (max_offset && INT_CST_LT (max_offset, offset))
3270 break;
3274 return 0;
3277 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3278 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3279 examined. */
3281 static void
3282 record_subobject_offsets (tree type,
3283 tree offset,
3284 splay_tree offsets,
3285 int vbases_p)
3287 walk_subobject_offsets (type, record_subobject_offset, offset,
3288 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3291 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3292 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3293 virtual bases of TYPE are examined. */
3295 static int
3296 layout_conflict_p (tree type,
3297 tree offset,
3298 splay_tree offsets,
3299 int vbases_p)
3301 splay_tree_node max_node;
3303 /* Get the node in OFFSETS that indicates the maximum offset where
3304 an empty subobject is located. */
3305 max_node = splay_tree_max (offsets);
3306 /* If there aren't any empty subobjects, then there's no point in
3307 performing this check. */
3308 if (!max_node)
3309 return 0;
3311 return walk_subobject_offsets (type, check_subobject_offset, offset,
3312 offsets, (tree) (max_node->key),
3313 vbases_p);
3316 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3317 non-static data member of the type indicated by RLI. BINFO is the
3318 binfo corresponding to the base subobject, OFFSETS maps offsets to
3319 types already located at those offsets. This function determines
3320 the position of the DECL. */
3322 static void
3323 layout_nonempty_base_or_field (record_layout_info rli,
3324 tree decl,
3325 tree binfo,
3326 splay_tree offsets)
3328 tree offset = NULL_TREE;
3329 bool field_p;
3330 tree type;
3332 if (binfo)
3334 /* For the purposes of determining layout conflicts, we want to
3335 use the class type of BINFO; TREE_TYPE (DECL) will be the
3336 CLASSTYPE_AS_BASE version, which does not contain entries for
3337 zero-sized bases. */
3338 type = TREE_TYPE (binfo);
3339 field_p = false;
3341 else
3343 type = TREE_TYPE (decl);
3344 field_p = true;
3347 /* Try to place the field. It may take more than one try if we have
3348 a hard time placing the field without putting two objects of the
3349 same type at the same address. */
3350 while (1)
3352 struct record_layout_info_s old_rli = *rli;
3354 /* Place this field. */
3355 place_field (rli, decl);
3356 offset = byte_position (decl);
3358 /* We have to check to see whether or not there is already
3359 something of the same type at the offset we're about to use.
3360 For example, consider:
3362 struct S {};
3363 struct T : public S { int i; };
3364 struct U : public S, public T {};
3366 Here, we put S at offset zero in U. Then, we can't put T at
3367 offset zero -- its S component would be at the same address
3368 as the S we already allocated. So, we have to skip ahead.
3369 Since all data members, including those whose type is an
3370 empty class, have nonzero size, any overlap can happen only
3371 with a direct or indirect base-class -- it can't happen with
3372 a data member. */
3373 /* In a union, overlap is permitted; all members are placed at
3374 offset zero. */
3375 if (TREE_CODE (rli->t) == UNION_TYPE)
3376 break;
3377 /* G++ 3.2 did not check for overlaps when placing a non-empty
3378 virtual base. */
3379 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3380 break;
3381 if (layout_conflict_p (field_p ? type : binfo, offset,
3382 offsets, field_p))
3384 /* Strip off the size allocated to this field. That puts us
3385 at the first place we could have put the field with
3386 proper alignment. */
3387 *rli = old_rli;
3389 /* Bump up by the alignment required for the type. */
3390 rli->bitpos
3391 = size_binop (PLUS_EXPR, rli->bitpos,
3392 bitsize_int (binfo
3393 ? CLASSTYPE_ALIGN (type)
3394 : TYPE_ALIGN (type)));
3395 normalize_rli (rli);
3397 else
3398 /* There was no conflict. We're done laying out this field. */
3399 break;
3402 /* Now that we know where it will be placed, update its
3403 BINFO_OFFSET. */
3404 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3405 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3406 this point because their BINFO_OFFSET is copied from another
3407 hierarchy. Therefore, we may not need to add the entire
3408 OFFSET. */
3409 propagate_binfo_offsets (binfo,
3410 size_diffop (convert (ssizetype, offset),
3411 convert (ssizetype,
3412 BINFO_OFFSET (binfo))));
3415 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3417 static int
3418 empty_base_at_nonzero_offset_p (tree type,
3419 tree offset,
3420 splay_tree offsets ATTRIBUTE_UNUSED)
3422 return is_empty_class (type) && !integer_zerop (offset);
3425 /* Layout the empty base BINFO. EOC indicates the byte currently just
3426 past the end of the class, and should be correctly aligned for a
3427 class of the type indicated by BINFO; OFFSETS gives the offsets of
3428 the empty bases allocated so far. T is the most derived
3429 type. Return nonzero iff we added it at the end. */
3431 static bool
3432 layout_empty_base (tree binfo, tree eoc, splay_tree offsets)
3434 tree alignment;
3435 tree basetype = BINFO_TYPE (binfo);
3436 bool atend = false;
3438 /* This routine should only be used for empty classes. */
3439 gcc_assert (is_empty_class (basetype));
3440 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3442 if (!integer_zerop (BINFO_OFFSET (binfo)))
3444 if (abi_version_at_least (2))
3445 propagate_binfo_offsets
3446 (binfo, size_diffop (size_zero_node, BINFO_OFFSET (binfo)));
3447 else if (warn_abi)
3448 warning (0, "offset of empty base %qT may not be ABI-compliant and may"
3449 "change in a future version of GCC",
3450 BINFO_TYPE (binfo));
3453 /* This is an empty base class. We first try to put it at offset
3454 zero. */
3455 if (layout_conflict_p (binfo,
3456 BINFO_OFFSET (binfo),
3457 offsets,
3458 /*vbases_p=*/0))
3460 /* That didn't work. Now, we move forward from the next
3461 available spot in the class. */
3462 atend = true;
3463 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3464 while (1)
3466 if (!layout_conflict_p (binfo,
3467 BINFO_OFFSET (binfo),
3468 offsets,
3469 /*vbases_p=*/0))
3470 /* We finally found a spot where there's no overlap. */
3471 break;
3473 /* There's overlap here, too. Bump along to the next spot. */
3474 propagate_binfo_offsets (binfo, alignment);
3477 return atend;
3480 /* Layout the base given by BINFO in the class indicated by RLI.
3481 *BASE_ALIGN is a running maximum of the alignments of
3482 any base class. OFFSETS gives the location of empty base
3483 subobjects. T is the most derived type. Return nonzero if the new
3484 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3485 *NEXT_FIELD, unless BINFO is for an empty base class.
3487 Returns the location at which the next field should be inserted. */
3489 static tree *
3490 build_base_field (record_layout_info rli, tree binfo,
3491 splay_tree offsets, tree *next_field)
3493 tree t = rli->t;
3494 tree basetype = BINFO_TYPE (binfo);
3496 if (!COMPLETE_TYPE_P (basetype))
3497 /* This error is now reported in xref_tag, thus giving better
3498 location information. */
3499 return next_field;
3501 /* Place the base class. */
3502 if (!is_empty_class (basetype))
3504 tree decl;
3506 /* The containing class is non-empty because it has a non-empty
3507 base class. */
3508 CLASSTYPE_EMPTY_P (t) = 0;
3510 /* Create the FIELD_DECL. */
3511 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3512 DECL_ARTIFICIAL (decl) = 1;
3513 DECL_IGNORED_P (decl) = 1;
3514 DECL_FIELD_CONTEXT (decl) = t;
3515 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3516 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3517 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3518 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3519 DECL_MODE (decl) = TYPE_MODE (basetype);
3520 DECL_FIELD_IS_BASE (decl) = 1;
3522 /* Try to place the field. It may take more than one try if we
3523 have a hard time placing the field without putting two
3524 objects of the same type at the same address. */
3525 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3526 /* Add the new FIELD_DECL to the list of fields for T. */
3527 TREE_CHAIN (decl) = *next_field;
3528 *next_field = decl;
3529 next_field = &TREE_CHAIN (decl);
3531 else
3533 tree eoc;
3534 bool atend;
3536 /* On some platforms (ARM), even empty classes will not be
3537 byte-aligned. */
3538 eoc = round_up (rli_size_unit_so_far (rli),
3539 CLASSTYPE_ALIGN_UNIT (basetype));
3540 atend = layout_empty_base (binfo, eoc, offsets);
3541 /* A nearly-empty class "has no proper base class that is empty,
3542 not morally virtual, and at an offset other than zero." */
3543 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3545 if (atend)
3546 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3547 /* The check above (used in G++ 3.2) is insufficient because
3548 an empty class placed at offset zero might itself have an
3549 empty base at a nonzero offset. */
3550 else if (walk_subobject_offsets (basetype,
3551 empty_base_at_nonzero_offset_p,
3552 size_zero_node,
3553 /*offsets=*/NULL,
3554 /*max_offset=*/NULL_TREE,
3555 /*vbases_p=*/true))
3557 if (abi_version_at_least (2))
3558 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3559 else if (warn_abi)
3560 warning (0, "class %qT will be considered nearly empty in a "
3561 "future version of GCC", t);
3565 /* We do not create a FIELD_DECL for empty base classes because
3566 it might overlap some other field. We want to be able to
3567 create CONSTRUCTORs for the class by iterating over the
3568 FIELD_DECLs, and the back end does not handle overlapping
3569 FIELD_DECLs. */
3571 /* An empty virtual base causes a class to be non-empty
3572 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3573 here because that was already done when the virtual table
3574 pointer was created. */
3577 /* Record the offsets of BINFO and its base subobjects. */
3578 record_subobject_offsets (binfo,
3579 BINFO_OFFSET (binfo),
3580 offsets,
3581 /*vbases_p=*/0);
3583 return next_field;
3586 /* Layout all of the non-virtual base classes. Record empty
3587 subobjects in OFFSETS. T is the most derived type. Return nonzero
3588 if the type cannot be nearly empty. The fields created
3589 corresponding to the base classes will be inserted at
3590 *NEXT_FIELD. */
3592 static void
3593 build_base_fields (record_layout_info rli,
3594 splay_tree offsets, tree *next_field)
3596 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3597 subobjects. */
3598 tree t = rli->t;
3599 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3600 int i;
3602 /* The primary base class is always allocated first. */
3603 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3604 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3605 offsets, next_field);
3607 /* Now allocate the rest of the bases. */
3608 for (i = 0; i < n_baseclasses; ++i)
3610 tree base_binfo;
3612 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
3614 /* The primary base was already allocated above, so we don't
3615 need to allocate it again here. */
3616 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3617 continue;
3619 /* Virtual bases are added at the end (a primary virtual base
3620 will have already been added). */
3621 if (BINFO_VIRTUAL_P (base_binfo))
3622 continue;
3624 next_field = build_base_field (rli, base_binfo,
3625 offsets, next_field);
3629 /* Go through the TYPE_METHODS of T issuing any appropriate
3630 diagnostics, figuring out which methods override which other
3631 methods, and so forth. */
3633 static void
3634 check_methods (tree t)
3636 tree x;
3638 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3640 check_for_override (x, t);
3641 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3642 error ("initializer specified for non-virtual method %q+D", x);
3643 /* The name of the field is the original field name
3644 Save this in auxiliary field for later overloading. */
3645 if (DECL_VINDEX (x))
3647 TYPE_POLYMORPHIC_P (t) = 1;
3648 if (DECL_PURE_VIRTUAL_P (x))
3649 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
3651 /* All user-declared destructors are non-trivial. */
3652 if (DECL_DESTRUCTOR_P (x))
3653 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
3657 /* FN is a constructor or destructor. Clone the declaration to create
3658 a specialized in-charge or not-in-charge version, as indicated by
3659 NAME. */
3661 static tree
3662 build_clone (tree fn, tree name)
3664 tree parms;
3665 tree clone;
3667 /* Copy the function. */
3668 clone = copy_decl (fn);
3669 /* Remember where this function came from. */
3670 DECL_CLONED_FUNCTION (clone) = fn;
3671 DECL_ABSTRACT_ORIGIN (clone) = fn;
3672 /* Reset the function name. */
3673 DECL_NAME (clone) = name;
3674 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3675 /* There's no pending inline data for this function. */
3676 DECL_PENDING_INLINE_INFO (clone) = NULL;
3677 DECL_PENDING_INLINE_P (clone) = 0;
3678 /* And it hasn't yet been deferred. */
3679 DECL_DEFERRED_FN (clone) = 0;
3681 /* The base-class destructor is not virtual. */
3682 if (name == base_dtor_identifier)
3684 DECL_VIRTUAL_P (clone) = 0;
3685 if (TREE_CODE (clone) != TEMPLATE_DECL)
3686 DECL_VINDEX (clone) = NULL_TREE;
3689 /* If there was an in-charge parameter, drop it from the function
3690 type. */
3691 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3693 tree basetype;
3694 tree parmtypes;
3695 tree exceptions;
3697 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3698 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3699 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3700 /* Skip the `this' parameter. */
3701 parmtypes = TREE_CHAIN (parmtypes);
3702 /* Skip the in-charge parameter. */
3703 parmtypes = TREE_CHAIN (parmtypes);
3704 /* And the VTT parm, in a complete [cd]tor. */
3705 if (DECL_HAS_VTT_PARM_P (fn)
3706 && ! DECL_NEEDS_VTT_PARM_P (clone))
3707 parmtypes = TREE_CHAIN (parmtypes);
3708 /* If this is subobject constructor or destructor, add the vtt
3709 parameter. */
3710 TREE_TYPE (clone)
3711 = build_method_type_directly (basetype,
3712 TREE_TYPE (TREE_TYPE (clone)),
3713 parmtypes);
3714 if (exceptions)
3715 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3716 exceptions);
3717 TREE_TYPE (clone)
3718 = cp_build_type_attribute_variant (TREE_TYPE (clone),
3719 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
3722 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3723 aren't function parameters; those are the template parameters. */
3724 if (TREE_CODE (clone) != TEMPLATE_DECL)
3726 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3727 /* Remove the in-charge parameter. */
3728 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3730 TREE_CHAIN (DECL_ARGUMENTS (clone))
3731 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3732 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3734 /* And the VTT parm, in a complete [cd]tor. */
3735 if (DECL_HAS_VTT_PARM_P (fn))
3737 if (DECL_NEEDS_VTT_PARM_P (clone))
3738 DECL_HAS_VTT_PARM_P (clone) = 1;
3739 else
3741 TREE_CHAIN (DECL_ARGUMENTS (clone))
3742 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3743 DECL_HAS_VTT_PARM_P (clone) = 0;
3747 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3749 DECL_CONTEXT (parms) = clone;
3750 cxx_dup_lang_specific_decl (parms);
3754 /* Create the RTL for this function. */
3755 SET_DECL_RTL (clone, NULL_RTX);
3756 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
3758 /* Make it easy to find the CLONE given the FN. */
3759 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3760 TREE_CHAIN (fn) = clone;
3762 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
3763 if (TREE_CODE (clone) == TEMPLATE_DECL)
3765 tree result;
3767 DECL_TEMPLATE_RESULT (clone)
3768 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3769 result = DECL_TEMPLATE_RESULT (clone);
3770 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3771 DECL_TI_TEMPLATE (result) = clone;
3773 else if (pch_file)
3774 note_decl_for_pch (clone);
3776 return clone;
3779 /* Produce declarations for all appropriate clones of FN. If
3780 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
3781 CLASTYPE_METHOD_VEC as well. */
3783 void
3784 clone_function_decl (tree fn, int update_method_vec_p)
3786 tree clone;
3788 /* Avoid inappropriate cloning. */
3789 if (TREE_CHAIN (fn)
3790 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
3791 return;
3793 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
3795 /* For each constructor, we need two variants: an in-charge version
3796 and a not-in-charge version. */
3797 clone = build_clone (fn, complete_ctor_identifier);
3798 if (update_method_vec_p)
3799 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3800 clone = build_clone (fn, base_ctor_identifier);
3801 if (update_method_vec_p)
3802 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3804 else
3806 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
3808 /* For each destructor, we need three variants: an in-charge
3809 version, a not-in-charge version, and an in-charge deleting
3810 version. We clone the deleting version first because that
3811 means it will go second on the TYPE_METHODS list -- and that
3812 corresponds to the correct layout order in the virtual
3813 function table.
3815 For a non-virtual destructor, we do not build a deleting
3816 destructor. */
3817 if (DECL_VIRTUAL_P (fn))
3819 clone = build_clone (fn, deleting_dtor_identifier);
3820 if (update_method_vec_p)
3821 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3823 clone = build_clone (fn, complete_dtor_identifier);
3824 if (update_method_vec_p)
3825 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3826 clone = build_clone (fn, base_dtor_identifier);
3827 if (update_method_vec_p)
3828 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3831 /* Note that this is an abstract function that is never emitted. */
3832 DECL_ABSTRACT (fn) = 1;
3835 /* DECL is an in charge constructor, which is being defined. This will
3836 have had an in class declaration, from whence clones were
3837 declared. An out-of-class definition can specify additional default
3838 arguments. As it is the clones that are involved in overload
3839 resolution, we must propagate the information from the DECL to its
3840 clones. */
3842 void
3843 adjust_clone_args (tree decl)
3845 tree clone;
3847 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
3848 clone = TREE_CHAIN (clone))
3850 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
3851 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
3852 tree decl_parms, clone_parms;
3854 clone_parms = orig_clone_parms;
3856 /* Skip the 'this' parameter. */
3857 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
3858 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3860 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
3861 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3862 if (DECL_HAS_VTT_PARM_P (decl))
3863 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3865 clone_parms = orig_clone_parms;
3866 if (DECL_HAS_VTT_PARM_P (clone))
3867 clone_parms = TREE_CHAIN (clone_parms);
3869 for (decl_parms = orig_decl_parms; decl_parms;
3870 decl_parms = TREE_CHAIN (decl_parms),
3871 clone_parms = TREE_CHAIN (clone_parms))
3873 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
3874 TREE_TYPE (clone_parms)));
3876 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
3878 /* A default parameter has been added. Adjust the
3879 clone's parameters. */
3880 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3881 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3882 tree type;
3884 clone_parms = orig_decl_parms;
3886 if (DECL_HAS_VTT_PARM_P (clone))
3888 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
3889 TREE_VALUE (orig_clone_parms),
3890 clone_parms);
3891 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
3893 type = build_method_type_directly (basetype,
3894 TREE_TYPE (TREE_TYPE (clone)),
3895 clone_parms);
3896 if (exceptions)
3897 type = build_exception_variant (type, exceptions);
3898 TREE_TYPE (clone) = type;
3900 clone_parms = NULL_TREE;
3901 break;
3904 gcc_assert (!clone_parms);
3908 /* For each of the constructors and destructors in T, create an
3909 in-charge and not-in-charge variant. */
3911 static void
3912 clone_constructors_and_destructors (tree t)
3914 tree fns;
3916 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
3917 out now. */
3918 if (!CLASSTYPE_METHOD_VEC (t))
3919 return;
3921 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
3922 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
3923 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
3924 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
3927 /* Remove all zero-width bit-fields from T. */
3929 static void
3930 remove_zero_width_bit_fields (tree t)
3932 tree *fieldsp;
3934 fieldsp = &TYPE_FIELDS (t);
3935 while (*fieldsp)
3937 if (TREE_CODE (*fieldsp) == FIELD_DECL
3938 && DECL_C_BIT_FIELD (*fieldsp)
3939 && DECL_INITIAL (*fieldsp))
3940 *fieldsp = TREE_CHAIN (*fieldsp);
3941 else
3942 fieldsp = &TREE_CHAIN (*fieldsp);
3946 /* Returns TRUE iff we need a cookie when dynamically allocating an
3947 array whose elements have the indicated class TYPE. */
3949 static bool
3950 type_requires_array_cookie (tree type)
3952 tree fns;
3953 bool has_two_argument_delete_p = false;
3955 gcc_assert (CLASS_TYPE_P (type));
3957 /* If there's a non-trivial destructor, we need a cookie. In order
3958 to iterate through the array calling the destructor for each
3959 element, we'll have to know how many elements there are. */
3960 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3961 return true;
3963 /* If the usual deallocation function is a two-argument whose second
3964 argument is of type `size_t', then we have to pass the size of
3965 the array to the deallocation function, so we will need to store
3966 a cookie. */
3967 fns = lookup_fnfields (TYPE_BINFO (type),
3968 ansi_opname (VEC_DELETE_EXPR),
3969 /*protect=*/0);
3970 /* If there are no `operator []' members, or the lookup is
3971 ambiguous, then we don't need a cookie. */
3972 if (!fns || fns == error_mark_node)
3973 return false;
3974 /* Loop through all of the functions. */
3975 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3977 tree fn;
3978 tree second_parm;
3980 /* Select the current function. */
3981 fn = OVL_CURRENT (fns);
3982 /* See if this function is a one-argument delete function. If
3983 it is, then it will be the usual deallocation function. */
3984 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
3985 if (second_parm == void_list_node)
3986 return false;
3987 /* Otherwise, if we have a two-argument function and the second
3988 argument is `size_t', it will be the usual deallocation
3989 function -- unless there is one-argument function, too. */
3990 if (TREE_CHAIN (second_parm) == void_list_node
3991 && same_type_p (TREE_VALUE (second_parm), sizetype))
3992 has_two_argument_delete_p = true;
3995 return has_two_argument_delete_p;
3998 /* Check the validity of the bases and members declared in T. Add any
3999 implicitly-generated functions (like copy-constructors and
4000 assignment operators). Compute various flag bits (like
4001 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4002 level: i.e., independently of the ABI in use. */
4004 static void
4005 check_bases_and_members (tree t)
4007 /* Nonzero if the implicitly generated copy constructor should take
4008 a non-const reference argument. */
4009 int cant_have_const_ctor;
4010 /* Nonzero if the implicitly generated assignment operator
4011 should take a non-const reference argument. */
4012 int no_const_asn_ref;
4013 tree access_decls;
4015 /* By default, we use const reference arguments and generate default
4016 constructors. */
4017 cant_have_const_ctor = 0;
4018 no_const_asn_ref = 0;
4020 /* Check all the base-classes. */
4021 check_bases (t, &cant_have_const_ctor,
4022 &no_const_asn_ref);
4024 /* Check all the method declarations. */
4025 check_methods (t);
4027 /* Check all the data member declarations. We cannot call
4028 check_field_decls until we have called check_bases check_methods,
4029 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4030 being set appropriately. */
4031 check_field_decls (t, &access_decls,
4032 &cant_have_const_ctor,
4033 &no_const_asn_ref);
4035 /* A nearly-empty class has to be vptr-containing; a nearly empty
4036 class contains just a vptr. */
4037 if (!TYPE_CONTAINS_VPTR_P (t))
4038 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4040 /* Do some bookkeeping that will guide the generation of implicitly
4041 declared member functions. */
4042 TYPE_HAS_COMPLEX_INIT_REF (t)
4043 |= (TYPE_HAS_INIT_REF (t) || TYPE_CONTAINS_VPTR_P (t));
4044 TYPE_NEEDS_CONSTRUCTING (t)
4045 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_CONTAINS_VPTR_P (t));
4046 CLASSTYPE_NON_AGGREGATE (t)
4047 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_POLYMORPHIC_P (t));
4048 CLASSTYPE_NON_POD_P (t)
4049 |= (CLASSTYPE_NON_AGGREGATE (t)
4050 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4051 || TYPE_HAS_ASSIGN_REF (t));
4052 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4053 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4055 /* Synthesize any needed methods. */
4056 add_implicitly_declared_members (t,
4057 cant_have_const_ctor,
4058 no_const_asn_ref);
4060 /* Create the in-charge and not-in-charge variants of constructors
4061 and destructors. */
4062 clone_constructors_and_destructors (t);
4064 /* Process the using-declarations. */
4065 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4066 handle_using_decl (TREE_VALUE (access_decls), t);
4068 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4069 finish_struct_methods (t);
4071 /* Figure out whether or not we will need a cookie when dynamically
4072 allocating an array of this type. */
4073 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4074 = type_requires_array_cookie (t);
4077 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4078 accordingly. If a new vfield was created (because T doesn't have a
4079 primary base class), then the newly created field is returned. It
4080 is not added to the TYPE_FIELDS list; it is the caller's
4081 responsibility to do that. Accumulate declared virtual functions
4082 on VIRTUALS_P. */
4084 static tree
4085 create_vtable_ptr (tree t, tree* virtuals_p)
4087 tree fn;
4089 /* Collect the virtual functions declared in T. */
4090 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4091 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4092 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4094 tree new_virtual = make_node (TREE_LIST);
4096 BV_FN (new_virtual) = fn;
4097 BV_DELTA (new_virtual) = integer_zero_node;
4098 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
4100 TREE_CHAIN (new_virtual) = *virtuals_p;
4101 *virtuals_p = new_virtual;
4104 /* If we couldn't find an appropriate base class, create a new field
4105 here. Even if there weren't any new virtual functions, we might need a
4106 new virtual function table if we're supposed to include vptrs in
4107 all classes that need them. */
4108 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4110 /* We build this decl with vtbl_ptr_type_node, which is a
4111 `vtable_entry_type*'. It might seem more precise to use
4112 `vtable_entry_type (*)[N]' where N is the number of virtual
4113 functions. However, that would require the vtable pointer in
4114 base classes to have a different type than the vtable pointer
4115 in derived classes. We could make that happen, but that
4116 still wouldn't solve all the problems. In particular, the
4117 type-based alias analysis code would decide that assignments
4118 to the base class vtable pointer can't alias assignments to
4119 the derived class vtable pointer, since they have different
4120 types. Thus, in a derived class destructor, where the base
4121 class constructor was inlined, we could generate bad code for
4122 setting up the vtable pointer.
4124 Therefore, we use one type for all vtable pointers. We still
4125 use a type-correct type; it's just doesn't indicate the array
4126 bounds. That's better than using `void*' or some such; it's
4127 cleaner, and it let's the alias analysis code know that these
4128 stores cannot alias stores to void*! */
4129 tree field;
4131 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4132 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4133 DECL_VIRTUAL_P (field) = 1;
4134 DECL_ARTIFICIAL (field) = 1;
4135 DECL_FIELD_CONTEXT (field) = t;
4136 DECL_FCONTEXT (field) = t;
4138 TYPE_VFIELD (t) = field;
4140 /* This class is non-empty. */
4141 CLASSTYPE_EMPTY_P (t) = 0;
4143 return field;
4146 return NULL_TREE;
4149 /* Fixup the inline function given by INFO now that the class is
4150 complete. */
4152 static void
4153 fixup_pending_inline (tree fn)
4155 if (DECL_PENDING_INLINE_INFO (fn))
4157 tree args = DECL_ARGUMENTS (fn);
4158 while (args)
4160 DECL_CONTEXT (args) = fn;
4161 args = TREE_CHAIN (args);
4166 /* Fixup the inline methods and friends in TYPE now that TYPE is
4167 complete. */
4169 static void
4170 fixup_inline_methods (tree type)
4172 tree method = TYPE_METHODS (type);
4173 VEC(tree,gc) *friends;
4174 unsigned ix;
4176 if (method && TREE_CODE (method) == TREE_VEC)
4178 if (TREE_VEC_ELT (method, 1))
4179 method = TREE_VEC_ELT (method, 1);
4180 else if (TREE_VEC_ELT (method, 0))
4181 method = TREE_VEC_ELT (method, 0);
4182 else
4183 method = TREE_VEC_ELT (method, 2);
4186 /* Do inline member functions. */
4187 for (; method; method = TREE_CHAIN (method))
4188 fixup_pending_inline (method);
4190 /* Do friends. */
4191 for (friends = CLASSTYPE_INLINE_FRIENDS (type), ix = 0;
4192 VEC_iterate (tree, friends, ix, method); ix++)
4193 fixup_pending_inline (method);
4194 CLASSTYPE_INLINE_FRIENDS (type) = NULL;
4197 /* Add OFFSET to all base types of BINFO which is a base in the
4198 hierarchy dominated by T.
4200 OFFSET, which is a type offset, is number of bytes. */
4202 static void
4203 propagate_binfo_offsets (tree binfo, tree offset)
4205 int i;
4206 tree primary_binfo;
4207 tree base_binfo;
4209 /* Update BINFO's offset. */
4210 BINFO_OFFSET (binfo)
4211 = convert (sizetype,
4212 size_binop (PLUS_EXPR,
4213 convert (ssizetype, BINFO_OFFSET (binfo)),
4214 offset));
4216 /* Find the primary base class. */
4217 primary_binfo = get_primary_binfo (binfo);
4219 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
4220 propagate_binfo_offsets (primary_binfo, offset);
4222 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4223 downwards. */
4224 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4226 /* Don't do the primary base twice. */
4227 if (base_binfo == primary_binfo)
4228 continue;
4230 if (BINFO_VIRTUAL_P (base_binfo))
4231 continue;
4233 propagate_binfo_offsets (base_binfo, offset);
4237 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4238 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4239 empty subobjects of T. */
4241 static void
4242 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4244 tree vbase;
4245 tree t = rli->t;
4246 bool first_vbase = true;
4247 tree *next_field;
4249 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
4250 return;
4252 if (!abi_version_at_least(2))
4254 /* In G++ 3.2, we incorrectly rounded the size before laying out
4255 the virtual bases. */
4256 finish_record_layout (rli, /*free_p=*/false);
4257 #ifdef STRUCTURE_SIZE_BOUNDARY
4258 /* Packed structures don't need to have minimum size. */
4259 if (! TYPE_PACKED (t))
4260 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4261 #endif
4262 rli->offset = TYPE_SIZE_UNIT (t);
4263 rli->bitpos = bitsize_zero_node;
4264 rli->record_align = TYPE_ALIGN (t);
4267 /* Find the last field. The artificial fields created for virtual
4268 bases will go after the last extant field to date. */
4269 next_field = &TYPE_FIELDS (t);
4270 while (*next_field)
4271 next_field = &TREE_CHAIN (*next_field);
4273 /* Go through the virtual bases, allocating space for each virtual
4274 base that is not already a primary base class. These are
4275 allocated in inheritance graph order. */
4276 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
4278 if (!BINFO_VIRTUAL_P (vbase))
4279 continue;
4281 if (!BINFO_PRIMARY_P (vbase))
4283 tree basetype = TREE_TYPE (vbase);
4285 /* This virtual base is not a primary base of any class in the
4286 hierarchy, so we have to add space for it. */
4287 next_field = build_base_field (rli, vbase,
4288 offsets, next_field);
4290 /* If the first virtual base might have been placed at a
4291 lower address, had we started from CLASSTYPE_SIZE, rather
4292 than TYPE_SIZE, issue a warning. There can be both false
4293 positives and false negatives from this warning in rare
4294 cases; to deal with all the possibilities would probably
4295 require performing both layout algorithms and comparing
4296 the results which is not particularly tractable. */
4297 if (warn_abi
4298 && first_vbase
4299 && (tree_int_cst_lt
4300 (size_binop (CEIL_DIV_EXPR,
4301 round_up (CLASSTYPE_SIZE (t),
4302 CLASSTYPE_ALIGN (basetype)),
4303 bitsize_unit_node),
4304 BINFO_OFFSET (vbase))))
4305 warning (0, "offset of virtual base %qT is not ABI-compliant and "
4306 "may change in a future version of GCC",
4307 basetype);
4309 first_vbase = false;
4314 /* Returns the offset of the byte just past the end of the base class
4315 BINFO. */
4317 static tree
4318 end_of_base (tree binfo)
4320 tree size;
4322 if (is_empty_class (BINFO_TYPE (binfo)))
4323 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4324 allocate some space for it. It cannot have virtual bases, so
4325 TYPE_SIZE_UNIT is fine. */
4326 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4327 else
4328 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4330 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4333 /* Returns the offset of the byte just past the end of the base class
4334 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4335 only non-virtual bases are included. */
4337 static tree
4338 end_of_class (tree t, int include_virtuals_p)
4340 tree result = size_zero_node;
4341 VEC(tree,gc) *vbases;
4342 tree binfo;
4343 tree base_binfo;
4344 tree offset;
4345 int i;
4347 for (binfo = TYPE_BINFO (t), i = 0;
4348 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4350 if (!include_virtuals_p
4351 && BINFO_VIRTUAL_P (base_binfo)
4352 && (!BINFO_PRIMARY_P (base_binfo)
4353 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
4354 continue;
4356 offset = end_of_base (base_binfo);
4357 if (INT_CST_LT_UNSIGNED (result, offset))
4358 result = offset;
4361 /* G++ 3.2 did not check indirect virtual bases. */
4362 if (abi_version_at_least (2) && include_virtuals_p)
4363 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
4364 VEC_iterate (tree, vbases, i, base_binfo); i++)
4366 offset = end_of_base (base_binfo);
4367 if (INT_CST_LT_UNSIGNED (result, offset))
4368 result = offset;
4371 return result;
4374 /* Warn about bases of T that are inaccessible because they are
4375 ambiguous. For example:
4377 struct S {};
4378 struct T : public S {};
4379 struct U : public S, public T {};
4381 Here, `(S*) new U' is not allowed because there are two `S'
4382 subobjects of U. */
4384 static void
4385 warn_about_ambiguous_bases (tree t)
4387 int i;
4388 VEC(tree,gc) *vbases;
4389 tree basetype;
4390 tree binfo;
4391 tree base_binfo;
4393 /* If there are no repeated bases, nothing can be ambiguous. */
4394 if (!CLASSTYPE_REPEATED_BASE_P (t))
4395 return;
4397 /* Check direct bases. */
4398 for (binfo = TYPE_BINFO (t), i = 0;
4399 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4401 basetype = BINFO_TYPE (base_binfo);
4403 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
4404 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
4405 basetype, t);
4408 /* Check for ambiguous virtual bases. */
4409 if (extra_warnings)
4410 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
4411 VEC_iterate (tree, vbases, i, binfo); i++)
4413 basetype = BINFO_TYPE (binfo);
4415 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
4416 warning (0, "virtual base %qT inaccessible in %qT due to ambiguity",
4417 basetype, t);
4421 /* Compare two INTEGER_CSTs K1 and K2. */
4423 static int
4424 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4426 return tree_int_cst_compare ((tree) k1, (tree) k2);
4429 /* Increase the size indicated in RLI to account for empty classes
4430 that are "off the end" of the class. */
4432 static void
4433 include_empty_classes (record_layout_info rli)
4435 tree eoc;
4436 tree rli_size;
4438 /* It might be the case that we grew the class to allocate a
4439 zero-sized base class. That won't be reflected in RLI, yet,
4440 because we are willing to overlay multiple bases at the same
4441 offset. However, now we need to make sure that RLI is big enough
4442 to reflect the entire class. */
4443 eoc = end_of_class (rli->t,
4444 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4445 rli_size = rli_size_unit_so_far (rli);
4446 if (TREE_CODE (rli_size) == INTEGER_CST
4447 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4449 if (!abi_version_at_least (2))
4450 /* In version 1 of the ABI, the size of a class that ends with
4451 a bitfield was not rounded up to a whole multiple of a
4452 byte. Because rli_size_unit_so_far returns only the number
4453 of fully allocated bytes, any extra bits were not included
4454 in the size. */
4455 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
4456 else
4457 /* The size should have been rounded to a whole byte. */
4458 gcc_assert (tree_int_cst_equal
4459 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
4460 rli->bitpos
4461 = size_binop (PLUS_EXPR,
4462 rli->bitpos,
4463 size_binop (MULT_EXPR,
4464 convert (bitsizetype,
4465 size_binop (MINUS_EXPR,
4466 eoc, rli_size)),
4467 bitsize_int (BITS_PER_UNIT)));
4468 normalize_rli (rli);
4472 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4473 BINFO_OFFSETs for all of the base-classes. Position the vtable
4474 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4476 static void
4477 layout_class_type (tree t, tree *virtuals_p)
4479 tree non_static_data_members;
4480 tree field;
4481 tree vptr;
4482 record_layout_info rli;
4483 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4484 types that appear at that offset. */
4485 splay_tree empty_base_offsets;
4486 /* True if the last field layed out was a bit-field. */
4487 bool last_field_was_bitfield = false;
4488 /* The location at which the next field should be inserted. */
4489 tree *next_field;
4490 /* T, as a base class. */
4491 tree base_t;
4493 /* Keep track of the first non-static data member. */
4494 non_static_data_members = TYPE_FIELDS (t);
4496 /* Start laying out the record. */
4497 rli = start_record_layout (t);
4499 /* Mark all the primary bases in the hierarchy. */
4500 determine_primary_bases (t);
4502 /* Create a pointer to our virtual function table. */
4503 vptr = create_vtable_ptr (t, virtuals_p);
4505 /* The vptr is always the first thing in the class. */
4506 if (vptr)
4508 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4509 TYPE_FIELDS (t) = vptr;
4510 next_field = &TREE_CHAIN (vptr);
4511 place_field (rli, vptr);
4513 else
4514 next_field = &TYPE_FIELDS (t);
4516 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4517 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4518 NULL, NULL);
4519 build_base_fields (rli, empty_base_offsets, next_field);
4521 /* Layout the non-static data members. */
4522 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4524 tree type;
4525 tree padding;
4527 /* We still pass things that aren't non-static data members to
4528 the back-end, in case it wants to do something with them. */
4529 if (TREE_CODE (field) != FIELD_DECL)
4531 place_field (rli, field);
4532 /* If the static data member has incomplete type, keep track
4533 of it so that it can be completed later. (The handling
4534 of pending statics in finish_record_layout is
4535 insufficient; consider:
4537 struct S1;
4538 struct S2 { static S1 s1; };
4540 At this point, finish_record_layout will be called, but
4541 S1 is still incomplete.) */
4542 if (TREE_CODE (field) == VAR_DECL)
4544 maybe_register_incomplete_var (field);
4545 /* The visibility of static data members is determined
4546 at their point of declaration, not their point of
4547 definition. */
4548 determine_visibility (field);
4550 continue;
4553 type = TREE_TYPE (field);
4555 padding = NULL_TREE;
4557 /* If this field is a bit-field whose width is greater than its
4558 type, then there are some special rules for allocating
4559 it. */
4560 if (DECL_C_BIT_FIELD (field)
4561 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4563 integer_type_kind itk;
4564 tree integer_type;
4565 bool was_unnamed_p = false;
4566 /* We must allocate the bits as if suitably aligned for the
4567 longest integer type that fits in this many bits. type
4568 of the field. Then, we are supposed to use the left over
4569 bits as additional padding. */
4570 for (itk = itk_char; itk != itk_none; ++itk)
4571 if (INT_CST_LT (DECL_SIZE (field),
4572 TYPE_SIZE (integer_types[itk])))
4573 break;
4575 /* ITK now indicates a type that is too large for the
4576 field. We have to back up by one to find the largest
4577 type that fits. */
4578 integer_type = integer_types[itk - 1];
4580 /* Figure out how much additional padding is required. GCC
4581 3.2 always created a padding field, even if it had zero
4582 width. */
4583 if (!abi_version_at_least (2)
4584 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
4586 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4587 /* In a union, the padding field must have the full width
4588 of the bit-field; all fields start at offset zero. */
4589 padding = DECL_SIZE (field);
4590 else
4592 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
4593 warning (0, "size assigned to %qT may not be "
4594 "ABI-compliant and may change in a future "
4595 "version of GCC",
4597 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4598 TYPE_SIZE (integer_type));
4601 #ifdef PCC_BITFIELD_TYPE_MATTERS
4602 /* An unnamed bitfield does not normally affect the
4603 alignment of the containing class on a target where
4604 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
4605 make any exceptions for unnamed bitfields when the
4606 bitfields are longer than their types. Therefore, we
4607 temporarily give the field a name. */
4608 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
4610 was_unnamed_p = true;
4611 DECL_NAME (field) = make_anon_name ();
4613 #endif
4614 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4615 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4616 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4617 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4618 empty_base_offsets);
4619 if (was_unnamed_p)
4620 DECL_NAME (field) = NULL_TREE;
4621 /* Now that layout has been performed, set the size of the
4622 field to the size of its declared type; the rest of the
4623 field is effectively invisible. */
4624 DECL_SIZE (field) = TYPE_SIZE (type);
4625 /* We must also reset the DECL_MODE of the field. */
4626 if (abi_version_at_least (2))
4627 DECL_MODE (field) = TYPE_MODE (type);
4628 else if (warn_abi
4629 && DECL_MODE (field) != TYPE_MODE (type))
4630 /* Versions of G++ before G++ 3.4 did not reset the
4631 DECL_MODE. */
4632 warning (0, "the offset of %qD may not be ABI-compliant and may "
4633 "change in a future version of GCC", field);
4635 else
4636 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4637 empty_base_offsets);
4639 /* Remember the location of any empty classes in FIELD. */
4640 if (abi_version_at_least (2))
4641 record_subobject_offsets (TREE_TYPE (field),
4642 byte_position(field),
4643 empty_base_offsets,
4644 /*vbases_p=*/1);
4646 /* If a bit-field does not immediately follow another bit-field,
4647 and yet it starts in the middle of a byte, we have failed to
4648 comply with the ABI. */
4649 if (warn_abi
4650 && DECL_C_BIT_FIELD (field)
4651 /* The TREE_NO_WARNING flag gets set by Objective-C when
4652 laying out an Objective-C class. The ObjC ABI differs
4653 from the C++ ABI, and so we do not want a warning
4654 here. */
4655 && !TREE_NO_WARNING (field)
4656 && !last_field_was_bitfield
4657 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4658 DECL_FIELD_BIT_OFFSET (field),
4659 bitsize_unit_node)))
4660 warning (0, "offset of %q+D is not ABI-compliant and may "
4661 "change in a future version of GCC", field);
4663 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
4664 offset of the field. */
4665 if (warn_abi
4666 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
4667 byte_position (field))
4668 && contains_empty_class_p (TREE_TYPE (field)))
4669 warning (0, "%q+D contains empty classes which may cause base "
4670 "classes to be placed at different locations in a "
4671 "future version of GCC", field);
4673 /* If we needed additional padding after this field, add it
4674 now. */
4675 if (padding)
4677 tree padding_field;
4679 padding_field = build_decl (FIELD_DECL,
4680 NULL_TREE,
4681 char_type_node);
4682 DECL_BIT_FIELD (padding_field) = 1;
4683 DECL_SIZE (padding_field) = padding;
4684 DECL_CONTEXT (padding_field) = t;
4685 DECL_ARTIFICIAL (padding_field) = 1;
4686 DECL_IGNORED_P (padding_field) = 1;
4687 layout_nonempty_base_or_field (rli, padding_field,
4688 NULL_TREE,
4689 empty_base_offsets);
4692 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4695 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
4697 /* Make sure that we are on a byte boundary so that the size of
4698 the class without virtual bases will always be a round number
4699 of bytes. */
4700 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4701 normalize_rli (rli);
4704 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
4705 padding. */
4706 if (!abi_version_at_least (2))
4707 include_empty_classes(rli);
4709 /* Delete all zero-width bit-fields from the list of fields. Now
4710 that the type is laid out they are no longer important. */
4711 remove_zero_width_bit_fields (t);
4713 /* Create the version of T used for virtual bases. We do not use
4714 make_aggr_type for this version; this is an artificial type. For
4715 a POD type, we just reuse T. */
4716 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
4718 base_t = make_node (TREE_CODE (t));
4720 /* Set the size and alignment for the new type. In G++ 3.2, all
4721 empty classes were considered to have size zero when used as
4722 base classes. */
4723 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
4725 TYPE_SIZE (base_t) = bitsize_zero_node;
4726 TYPE_SIZE_UNIT (base_t) = size_zero_node;
4727 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
4728 warning (0, "layout of classes derived from empty class %qT "
4729 "may change in a future version of GCC",
4732 else
4734 tree eoc;
4736 /* If the ABI version is not at least two, and the last
4737 field was a bit-field, RLI may not be on a byte
4738 boundary. In particular, rli_size_unit_so_far might
4739 indicate the last complete byte, while rli_size_so_far
4740 indicates the total number of bits used. Therefore,
4741 rli_size_so_far, rather than rli_size_unit_so_far, is
4742 used to compute TYPE_SIZE_UNIT. */
4743 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4744 TYPE_SIZE_UNIT (base_t)
4745 = size_binop (MAX_EXPR,
4746 convert (sizetype,
4747 size_binop (CEIL_DIV_EXPR,
4748 rli_size_so_far (rli),
4749 bitsize_int (BITS_PER_UNIT))),
4750 eoc);
4751 TYPE_SIZE (base_t)
4752 = size_binop (MAX_EXPR,
4753 rli_size_so_far (rli),
4754 size_binop (MULT_EXPR,
4755 convert (bitsizetype, eoc),
4756 bitsize_int (BITS_PER_UNIT)));
4758 TYPE_ALIGN (base_t) = rli->record_align;
4759 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
4761 /* Copy the fields from T. */
4762 next_field = &TYPE_FIELDS (base_t);
4763 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4764 if (TREE_CODE (field) == FIELD_DECL)
4766 *next_field = build_decl (FIELD_DECL,
4767 DECL_NAME (field),
4768 TREE_TYPE (field));
4769 DECL_CONTEXT (*next_field) = base_t;
4770 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
4771 DECL_FIELD_BIT_OFFSET (*next_field)
4772 = DECL_FIELD_BIT_OFFSET (field);
4773 DECL_SIZE (*next_field) = DECL_SIZE (field);
4774 DECL_MODE (*next_field) = DECL_MODE (field);
4775 next_field = &TREE_CHAIN (*next_field);
4778 /* Record the base version of the type. */
4779 CLASSTYPE_AS_BASE (t) = base_t;
4780 TYPE_CONTEXT (base_t) = t;
4782 else
4783 CLASSTYPE_AS_BASE (t) = t;
4785 /* Every empty class contains an empty class. */
4786 if (CLASSTYPE_EMPTY_P (t))
4787 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4789 /* Set the TYPE_DECL for this type to contain the right
4790 value for DECL_OFFSET, so that we can use it as part
4791 of a COMPONENT_REF for multiple inheritance. */
4792 layout_decl (TYPE_MAIN_DECL (t), 0);
4794 /* Now fix up any virtual base class types that we left lying
4795 around. We must get these done before we try to lay out the
4796 virtual function table. As a side-effect, this will remove the
4797 base subobject fields. */
4798 layout_virtual_bases (rli, empty_base_offsets);
4800 /* Make sure that empty classes are reflected in RLI at this
4801 point. */
4802 include_empty_classes(rli);
4804 /* Make sure not to create any structures with zero size. */
4805 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
4806 place_field (rli,
4807 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
4809 /* Let the back-end lay out the type. */
4810 finish_record_layout (rli, /*free_p=*/true);
4812 /* Warn about bases that can't be talked about due to ambiguity. */
4813 warn_about_ambiguous_bases (t);
4815 /* Now that we're done with layout, give the base fields the real types. */
4816 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4817 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
4818 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
4820 /* Clean up. */
4821 splay_tree_delete (empty_base_offsets);
4824 /* Determine the "key method" for the class type indicated by TYPE,
4825 and set CLASSTYPE_KEY_METHOD accordingly. */
4827 void
4828 determine_key_method (tree type)
4830 tree method;
4832 if (TYPE_FOR_JAVA (type)
4833 || processing_template_decl
4834 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
4835 || CLASSTYPE_INTERFACE_KNOWN (type))
4836 return;
4838 /* The key method is the first non-pure virtual function that is not
4839 inline at the point of class definition. On some targets the
4840 key function may not be inline; those targets should not call
4841 this function until the end of the translation unit. */
4842 for (method = TYPE_METHODS (type); method != NULL_TREE;
4843 method = TREE_CHAIN (method))
4844 if (DECL_VINDEX (method) != NULL_TREE
4845 && ! DECL_DECLARED_INLINE_P (method)
4846 && ! DECL_PURE_VIRTUAL_P (method))
4848 CLASSTYPE_KEY_METHOD (type) = method;
4849 break;
4852 return;
4855 /* Perform processing required when the definition of T (a class type)
4856 is complete. */
4858 void
4859 finish_struct_1 (tree t)
4861 tree x;
4862 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
4863 tree virtuals = NULL_TREE;
4864 int n_fields = 0;
4866 if (COMPLETE_TYPE_P (t))
4868 gcc_assert (IS_AGGR_TYPE (t));
4869 error ("redefinition of %q#T", t);
4870 popclass ();
4871 return;
4874 /* If this type was previously laid out as a forward reference,
4875 make sure we lay it out again. */
4876 TYPE_SIZE (t) = NULL_TREE;
4877 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
4879 fixup_inline_methods (t);
4881 /* Make assumptions about the class; we'll reset the flags if
4882 necessary. */
4883 CLASSTYPE_EMPTY_P (t) = 1;
4884 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4885 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
4887 /* Do end-of-class semantic processing: checking the validity of the
4888 bases and members and add implicitly generated methods. */
4889 check_bases_and_members (t);
4891 /* Find the key method. */
4892 if (TYPE_CONTAINS_VPTR_P (t))
4894 /* The Itanium C++ ABI permits the key method to be chosen when
4895 the class is defined -- even though the key method so
4896 selected may later turn out to be an inline function. On
4897 some systems (such as ARM Symbian OS) the key method cannot
4898 be determined until the end of the translation unit. On such
4899 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
4900 will cause the class to be added to KEYED_CLASSES. Then, in
4901 finish_file we will determine the key method. */
4902 if (targetm.cxx.key_method_may_be_inline ())
4903 determine_key_method (t);
4905 /* If a polymorphic class has no key method, we may emit the vtable
4906 in every translation unit where the class definition appears. */
4907 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
4908 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
4911 /* Layout the class itself. */
4912 layout_class_type (t, &virtuals);
4913 if (CLASSTYPE_AS_BASE (t) != t)
4914 /* We use the base type for trivial assignments, and hence it
4915 needs a mode. */
4916 compute_record_mode (CLASSTYPE_AS_BASE (t));
4918 virtuals = modify_all_vtables (t, nreverse (virtuals));
4920 /* If necessary, create the primary vtable for this class. */
4921 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
4923 /* We must enter these virtuals into the table. */
4924 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4925 build_primary_vtable (NULL_TREE, t);
4926 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4927 /* Here we know enough to change the type of our virtual
4928 function table, but we will wait until later this function. */
4929 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4932 if (TYPE_CONTAINS_VPTR_P (t))
4934 int vindex;
4935 tree fn;
4937 if (BINFO_VTABLE (TYPE_BINFO (t)))
4938 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
4939 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4940 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
4942 /* Add entries for virtual functions introduced by this class. */
4943 BINFO_VIRTUALS (TYPE_BINFO (t))
4944 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
4946 /* Set DECL_VINDEX for all functions declared in this class. */
4947 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
4949 fn = TREE_CHAIN (fn),
4950 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
4951 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
4953 tree fndecl = BV_FN (fn);
4955 if (DECL_THUNK_P (fndecl))
4956 /* A thunk. We should never be calling this entry directly
4957 from this vtable -- we'd use the entry for the non
4958 thunk base function. */
4959 DECL_VINDEX (fndecl) = NULL_TREE;
4960 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
4961 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
4965 finish_struct_bits (t);
4967 /* Complete the rtl for any static member objects of the type we're
4968 working on. */
4969 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4970 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4971 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
4972 DECL_MODE (x) = TYPE_MODE (t);
4974 /* Done with FIELDS...now decide whether to sort these for
4975 faster lookups later.
4977 We use a small number because most searches fail (succeeding
4978 ultimately as the search bores through the inheritance
4979 hierarchy), and we want this failure to occur quickly. */
4981 n_fields = count_fields (TYPE_FIELDS (t));
4982 if (n_fields > 7)
4984 struct sorted_fields_type *field_vec = GGC_NEWVAR
4985 (struct sorted_fields_type,
4986 sizeof (struct sorted_fields_type) + n_fields * sizeof (tree));
4987 field_vec->len = n_fields;
4988 add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
4989 qsort (field_vec->elts, n_fields, sizeof (tree),
4990 field_decl_cmp);
4991 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4992 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4993 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4996 /* Make the rtl for any new vtables we have created, and unmark
4997 the base types we marked. */
4998 finish_vtbls (t);
5000 /* Build the VTT for T. */
5001 build_vtt (t);
5003 /* This warning does not make sense for Java classes, since they
5004 cannot have destructors. */
5005 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
5007 tree dtor;
5009 dtor = CLASSTYPE_DESTRUCTORS (t);
5010 /* Warn only if the dtor is non-private or the class has
5011 friends. */
5012 if (/* An implicitly declared destructor is always public. And,
5013 if it were virtual, we would have created it by now. */
5014 !dtor
5015 || (!DECL_VINDEX (dtor)
5016 && (!TREE_PRIVATE (dtor)
5017 || CLASSTYPE_FRIEND_CLASSES (t)
5018 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))
5019 warning (0, "%q#T has virtual functions but non-virtual destructor",
5023 complete_vars (t);
5025 if (warn_overloaded_virtual)
5026 warn_hidden (t);
5028 maybe_suppress_debug_info (t);
5030 dump_class_hierarchy (t);
5032 /* Finish debugging output for this type. */
5033 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5036 /* When T was built up, the member declarations were added in reverse
5037 order. Rearrange them to declaration order. */
5039 void
5040 unreverse_member_declarations (tree t)
5042 tree next;
5043 tree prev;
5044 tree x;
5046 /* The following lists are all in reverse order. Put them in
5047 declaration order now. */
5048 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5049 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5051 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5052 reverse order, so we can't just use nreverse. */
5053 prev = NULL_TREE;
5054 for (x = TYPE_FIELDS (t);
5055 x && TREE_CODE (x) != TYPE_DECL;
5056 x = next)
5058 next = TREE_CHAIN (x);
5059 TREE_CHAIN (x) = prev;
5060 prev = x;
5062 if (prev)
5064 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5065 if (prev)
5066 TYPE_FIELDS (t) = prev;
5070 tree
5071 finish_struct (tree t, tree attributes)
5073 location_t saved_loc = input_location;
5075 /* Now that we've got all the field declarations, reverse everything
5076 as necessary. */
5077 unreverse_member_declarations (t);
5079 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5081 /* Nadger the current location so that diagnostics point to the start of
5082 the struct, not the end. */
5083 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
5085 if (processing_template_decl)
5087 tree x;
5089 finish_struct_methods (t);
5090 TYPE_SIZE (t) = bitsize_zero_node;
5091 TYPE_SIZE_UNIT (t) = size_zero_node;
5093 /* We need to emit an error message if this type was used as a parameter
5094 and it is an abstract type, even if it is a template. We construct
5095 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
5096 account and we call complete_vars with this type, which will check
5097 the PARM_DECLS. Note that while the type is being defined,
5098 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
5099 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
5100 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
5101 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
5102 if (DECL_PURE_VIRTUAL_P (x))
5103 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
5104 complete_vars (t);
5106 else
5107 finish_struct_1 (t);
5109 input_location = saved_loc;
5111 TYPE_BEING_DEFINED (t) = 0;
5113 if (current_class_type)
5114 popclass ();
5115 else
5116 error ("trying to finish struct, but kicked out due to previous parse errors");
5118 if (processing_template_decl && at_function_scope_p ())
5119 add_stmt (build_min (TAG_DEFN, t));
5121 return t;
5124 /* Return the dynamic type of INSTANCE, if known.
5125 Used to determine whether the virtual function table is needed
5126 or not.
5128 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5129 of our knowledge of its type. *NONNULL should be initialized
5130 before this function is called. */
5132 static tree
5133 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5135 switch (TREE_CODE (instance))
5137 case INDIRECT_REF:
5138 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5139 return NULL_TREE;
5140 else
5141 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5142 nonnull, cdtorp);
5144 case CALL_EXPR:
5145 /* This is a call to a constructor, hence it's never zero. */
5146 if (TREE_HAS_CONSTRUCTOR (instance))
5148 if (nonnull)
5149 *nonnull = 1;
5150 return TREE_TYPE (instance);
5152 return NULL_TREE;
5154 case SAVE_EXPR:
5155 /* This is a call to a constructor, hence it's never zero. */
5156 if (TREE_HAS_CONSTRUCTOR (instance))
5158 if (nonnull)
5159 *nonnull = 1;
5160 return TREE_TYPE (instance);
5162 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5164 case PLUS_EXPR:
5165 case MINUS_EXPR:
5166 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5167 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5168 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5169 /* Propagate nonnull. */
5170 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5171 return NULL_TREE;
5173 case NOP_EXPR:
5174 case CONVERT_EXPR:
5175 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5177 case ADDR_EXPR:
5178 instance = TREE_OPERAND (instance, 0);
5179 if (nonnull)
5181 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
5182 with a real object -- given &p->f, p can still be null. */
5183 tree t = get_base_address (instance);
5184 /* ??? Probably should check DECL_WEAK here. */
5185 if (t && DECL_P (t))
5186 *nonnull = 1;
5188 return fixed_type_or_null (instance, nonnull, cdtorp);
5190 case COMPONENT_REF:
5191 /* If this component is really a base class reference, then the field
5192 itself isn't definitive. */
5193 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
5194 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5195 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5197 case VAR_DECL:
5198 case FIELD_DECL:
5199 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5200 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5202 if (nonnull)
5203 *nonnull = 1;
5204 return TREE_TYPE (TREE_TYPE (instance));
5206 /* fall through... */
5207 case TARGET_EXPR:
5208 case PARM_DECL:
5209 case RESULT_DECL:
5210 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5212 if (nonnull)
5213 *nonnull = 1;
5214 return TREE_TYPE (instance);
5216 else if (instance == current_class_ptr)
5218 if (nonnull)
5219 *nonnull = 1;
5221 /* if we're in a ctor or dtor, we know our type. */
5222 if (DECL_LANG_SPECIFIC (current_function_decl)
5223 && (DECL_CONSTRUCTOR_P (current_function_decl)
5224 || DECL_DESTRUCTOR_P (current_function_decl)))
5226 if (cdtorp)
5227 *cdtorp = 1;
5228 return TREE_TYPE (TREE_TYPE (instance));
5231 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5233 /* Reference variables should be references to objects. */
5234 if (nonnull)
5235 *nonnull = 1;
5237 /* DECL_VAR_MARKED_P is used to prevent recursion; a
5238 variable's initializer may refer to the variable
5239 itself. */
5240 if (TREE_CODE (instance) == VAR_DECL
5241 && DECL_INITIAL (instance)
5242 && !DECL_VAR_MARKED_P (instance))
5244 tree type;
5245 DECL_VAR_MARKED_P (instance) = 1;
5246 type = fixed_type_or_null (DECL_INITIAL (instance),
5247 nonnull, cdtorp);
5248 DECL_VAR_MARKED_P (instance) = 0;
5249 return type;
5252 return NULL_TREE;
5254 default:
5255 return NULL_TREE;
5259 /* Return nonzero if the dynamic type of INSTANCE is known, and
5260 equivalent to the static type. We also handle the case where
5261 INSTANCE is really a pointer. Return negative if this is a
5262 ctor/dtor. There the dynamic type is known, but this might not be
5263 the most derived base of the original object, and hence virtual
5264 bases may not be layed out according to this type.
5266 Used to determine whether the virtual function table is needed
5267 or not.
5269 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5270 of our knowledge of its type. *NONNULL should be initialized
5271 before this function is called. */
5274 resolves_to_fixed_type_p (tree instance, int* nonnull)
5276 tree t = TREE_TYPE (instance);
5277 int cdtorp = 0;
5279 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5280 if (fixed == NULL_TREE)
5281 return 0;
5282 if (POINTER_TYPE_P (t))
5283 t = TREE_TYPE (t);
5284 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5285 return 0;
5286 return cdtorp ? -1 : 1;
5290 void
5291 init_class_processing (void)
5293 current_class_depth = 0;
5294 current_class_stack_size = 10;
5295 current_class_stack
5296 = xmalloc (current_class_stack_size * sizeof (struct class_stack_node));
5297 local_classes = VEC_alloc (tree, gc, 8);
5299 ridpointers[(int) RID_PUBLIC] = access_public_node;
5300 ridpointers[(int) RID_PRIVATE] = access_private_node;
5301 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5304 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
5306 static void
5307 restore_class_cache (void)
5309 tree type;
5311 /* We are re-entering the same class we just left, so we don't
5312 have to search the whole inheritance matrix to find all the
5313 decls to bind again. Instead, we install the cached
5314 class_shadowed list and walk through it binding names. */
5315 push_binding_level (previous_class_level);
5316 class_binding_level = previous_class_level;
5317 /* Restore IDENTIFIER_TYPE_VALUE. */
5318 for (type = class_binding_level->type_shadowed;
5319 type;
5320 type = TREE_CHAIN (type))
5321 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
5324 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5325 appropriate for TYPE.
5327 So that we may avoid calls to lookup_name, we cache the _TYPE
5328 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5330 For multiple inheritance, we perform a two-pass depth-first search
5331 of the type lattice. */
5333 void
5334 pushclass (tree type)
5336 type = TYPE_MAIN_VARIANT (type);
5338 /* Make sure there is enough room for the new entry on the stack. */
5339 if (current_class_depth + 1 >= current_class_stack_size)
5341 current_class_stack_size *= 2;
5342 current_class_stack
5343 = xrealloc (current_class_stack,
5344 current_class_stack_size
5345 * sizeof (struct class_stack_node));
5348 /* Insert a new entry on the class stack. */
5349 current_class_stack[current_class_depth].name = current_class_name;
5350 current_class_stack[current_class_depth].type = current_class_type;
5351 current_class_stack[current_class_depth].access = current_access_specifier;
5352 current_class_stack[current_class_depth].names_used = 0;
5353 current_class_depth++;
5355 /* Now set up the new type. */
5356 current_class_name = TYPE_NAME (type);
5357 if (TREE_CODE (current_class_name) == TYPE_DECL)
5358 current_class_name = DECL_NAME (current_class_name);
5359 current_class_type = type;
5361 /* By default, things in classes are private, while things in
5362 structures or unions are public. */
5363 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5364 ? access_private_node
5365 : access_public_node);
5367 if (previous_class_level
5368 && type != previous_class_level->this_entity
5369 && current_class_depth == 1)
5371 /* Forcibly remove any old class remnants. */
5372 invalidate_class_lookup_cache ();
5375 if (!previous_class_level
5376 || type != previous_class_level->this_entity
5377 || current_class_depth > 1)
5378 pushlevel_class ();
5379 else
5380 restore_class_cache ();
5383 /* When we exit a toplevel class scope, we save its binding level so
5384 that we can restore it quickly. Here, we've entered some other
5385 class, so we must invalidate our cache. */
5387 void
5388 invalidate_class_lookup_cache (void)
5390 previous_class_level = NULL;
5393 /* Get out of the current class scope. If we were in a class scope
5394 previously, that is the one popped to. */
5396 void
5397 popclass (void)
5399 poplevel_class ();
5401 current_class_depth--;
5402 current_class_name = current_class_stack[current_class_depth].name;
5403 current_class_type = current_class_stack[current_class_depth].type;
5404 current_access_specifier = current_class_stack[current_class_depth].access;
5405 if (current_class_stack[current_class_depth].names_used)
5406 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5409 /* Returns 1 if current_class_type is either T or a nested type of T.
5410 We start looking from 1 because entry 0 is from global scope, and has
5411 no type. */
5414 currently_open_class (tree t)
5416 int i;
5417 if (current_class_type && same_type_p (t, current_class_type))
5418 return 1;
5419 for (i = 1; i < current_class_depth; ++i)
5420 if (current_class_stack[i].type
5421 && same_type_p (current_class_stack [i].type, t))
5422 return 1;
5423 return 0;
5426 /* If either current_class_type or one of its enclosing classes are derived
5427 from T, return the appropriate type. Used to determine how we found
5428 something via unqualified lookup. */
5430 tree
5431 currently_open_derived_class (tree t)
5433 int i;
5435 /* The bases of a dependent type are unknown. */
5436 if (dependent_type_p (t))
5437 return NULL_TREE;
5439 if (!current_class_type)
5440 return NULL_TREE;
5442 if (DERIVED_FROM_P (t, current_class_type))
5443 return current_class_type;
5445 for (i = current_class_depth - 1; i > 0; --i)
5446 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5447 return current_class_stack[i].type;
5449 return NULL_TREE;
5452 /* When entering a class scope, all enclosing class scopes' names with
5453 static meaning (static variables, static functions, types and
5454 enumerators) have to be visible. This recursive function calls
5455 pushclass for all enclosing class contexts until global or a local
5456 scope is reached. TYPE is the enclosed class. */
5458 void
5459 push_nested_class (tree type)
5461 tree context;
5463 /* A namespace might be passed in error cases, like A::B:C. */
5464 if (type == NULL_TREE
5465 || type == error_mark_node
5466 || TREE_CODE (type) == NAMESPACE_DECL
5467 || ! IS_AGGR_TYPE (type)
5468 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5469 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5470 return;
5472 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5474 if (context && CLASS_TYPE_P (context))
5475 push_nested_class (context);
5476 pushclass (type);
5479 /* Undoes a push_nested_class call. */
5481 void
5482 pop_nested_class (void)
5484 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5486 popclass ();
5487 if (context && CLASS_TYPE_P (context))
5488 pop_nested_class ();
5491 /* Returns the number of extern "LANG" blocks we are nested within. */
5494 current_lang_depth (void)
5496 return VEC_length (tree, current_lang_base);
5499 /* Set global variables CURRENT_LANG_NAME to appropriate value
5500 so that behavior of name-mangling machinery is correct. */
5502 void
5503 push_lang_context (tree name)
5505 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
5507 if (name == lang_name_cplusplus)
5509 current_lang_name = name;
5511 else if (name == lang_name_java)
5513 current_lang_name = name;
5514 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5515 (See record_builtin_java_type in decl.c.) However, that causes
5516 incorrect debug entries if these types are actually used.
5517 So we re-enable debug output after extern "Java". */
5518 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5519 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5520 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5521 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5522 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5523 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5524 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5525 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5527 else if (name == lang_name_c)
5529 current_lang_name = name;
5531 else
5532 error ("language string %<\"%E\"%> not recognized", name);
5535 /* Get out of the current language scope. */
5537 void
5538 pop_lang_context (void)
5540 current_lang_name = VEC_pop (tree, current_lang_base);
5543 /* Type instantiation routines. */
5545 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5546 matches the TARGET_TYPE. If there is no satisfactory match, return
5547 error_mark_node, and issue a error & warning messages under control
5548 of FLAGS. Permit pointers to member function if FLAGS permits. If
5549 TEMPLATE_ONLY, the name of the overloaded function was a
5550 template-id, and EXPLICIT_TARGS are the explicitly provided
5551 template arguments. */
5553 static tree
5554 resolve_address_of_overloaded_function (tree target_type,
5555 tree overload,
5556 tsubst_flags_t flags,
5557 bool template_only,
5558 tree explicit_targs)
5560 /* Here's what the standard says:
5562 [over.over]
5564 If the name is a function template, template argument deduction
5565 is done, and if the argument deduction succeeds, the deduced
5566 arguments are used to generate a single template function, which
5567 is added to the set of overloaded functions considered.
5569 Non-member functions and static member functions match targets of
5570 type "pointer-to-function" or "reference-to-function." Nonstatic
5571 member functions match targets of type "pointer-to-member
5572 function;" the function type of the pointer to member is used to
5573 select the member function from the set of overloaded member
5574 functions. If a nonstatic member function is selected, the
5575 reference to the overloaded function name is required to have the
5576 form of a pointer to member as described in 5.3.1.
5578 If more than one function is selected, any template functions in
5579 the set are eliminated if the set also contains a non-template
5580 function, and any given template function is eliminated if the
5581 set contains a second template function that is more specialized
5582 than the first according to the partial ordering rules 14.5.5.2.
5583 After such eliminations, if any, there shall remain exactly one
5584 selected function. */
5586 int is_ptrmem = 0;
5587 int is_reference = 0;
5588 /* We store the matches in a TREE_LIST rooted here. The functions
5589 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5590 interoperability with most_specialized_instantiation. */
5591 tree matches = NULL_TREE;
5592 tree fn;
5594 /* By the time we get here, we should be seeing only real
5595 pointer-to-member types, not the internal POINTER_TYPE to
5596 METHOD_TYPE representation. */
5597 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
5598 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
5600 gcc_assert (is_overloaded_fn (overload));
5602 /* Check that the TARGET_TYPE is reasonable. */
5603 if (TYPE_PTRFN_P (target_type))
5604 /* This is OK. */;
5605 else if (TYPE_PTRMEMFUNC_P (target_type))
5606 /* This is OK, too. */
5607 is_ptrmem = 1;
5608 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5610 /* This is OK, too. This comes from a conversion to reference
5611 type. */
5612 target_type = build_reference_type (target_type);
5613 is_reference = 1;
5615 else
5617 if (flags & tf_error)
5618 error ("cannot resolve overloaded function %qD based on"
5619 " conversion to type %qT",
5620 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5621 return error_mark_node;
5624 /* If we can find a non-template function that matches, we can just
5625 use it. There's no point in generating template instantiations
5626 if we're just going to throw them out anyhow. But, of course, we
5627 can only do this when we don't *need* a template function. */
5628 if (!template_only)
5630 tree fns;
5632 for (fns = overload; fns; fns = OVL_NEXT (fns))
5634 tree fn = OVL_CURRENT (fns);
5635 tree fntype;
5637 if (TREE_CODE (fn) == TEMPLATE_DECL)
5638 /* We're not looking for templates just yet. */
5639 continue;
5641 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5642 != is_ptrmem)
5643 /* We're looking for a non-static member, and this isn't
5644 one, or vice versa. */
5645 continue;
5647 /* Ignore anticipated decls of undeclared builtins. */
5648 if (DECL_ANTICIPATED (fn))
5649 continue;
5651 /* See if there's a match. */
5652 fntype = TREE_TYPE (fn);
5653 if (is_ptrmem)
5654 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5655 else if (!is_reference)
5656 fntype = build_pointer_type (fntype);
5658 if (can_convert_arg (target_type, fntype, fn))
5659 matches = tree_cons (fn, NULL_TREE, matches);
5663 /* Now, if we've already got a match (or matches), there's no need
5664 to proceed to the template functions. But, if we don't have a
5665 match we need to look at them, too. */
5666 if (!matches)
5668 tree target_fn_type;
5669 tree target_arg_types;
5670 tree target_ret_type;
5671 tree fns;
5673 if (is_ptrmem)
5674 target_fn_type
5675 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5676 else
5677 target_fn_type = TREE_TYPE (target_type);
5678 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5679 target_ret_type = TREE_TYPE (target_fn_type);
5681 /* Never do unification on the 'this' parameter. */
5682 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5683 target_arg_types = TREE_CHAIN (target_arg_types);
5685 for (fns = overload; fns; fns = OVL_NEXT (fns))
5687 tree fn = OVL_CURRENT (fns);
5688 tree instantiation;
5689 tree instantiation_type;
5690 tree targs;
5692 if (TREE_CODE (fn) != TEMPLATE_DECL)
5693 /* We're only looking for templates. */
5694 continue;
5696 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5697 != is_ptrmem)
5698 /* We're not looking for a non-static member, and this is
5699 one, or vice versa. */
5700 continue;
5702 /* Try to do argument deduction. */
5703 targs = make_tree_vec (DECL_NTPARMS (fn));
5704 if (fn_type_unification (fn, explicit_targs, targs,
5705 target_arg_types, target_ret_type,
5706 DEDUCE_EXACT))
5707 /* Argument deduction failed. */
5708 continue;
5710 /* Instantiate the template. */
5711 instantiation = instantiate_template (fn, targs, flags);
5712 if (instantiation == error_mark_node)
5713 /* Instantiation failed. */
5714 continue;
5716 /* See if there's a match. */
5717 instantiation_type = TREE_TYPE (instantiation);
5718 if (is_ptrmem)
5719 instantiation_type =
5720 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5721 else if (!is_reference)
5722 instantiation_type = build_pointer_type (instantiation_type);
5723 if (can_convert_arg (target_type, instantiation_type, instantiation))
5724 matches = tree_cons (instantiation, fn, matches);
5727 /* Now, remove all but the most specialized of the matches. */
5728 if (matches)
5730 tree match = most_specialized_instantiation (matches);
5732 if (match != error_mark_node)
5733 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5737 /* Now we should have exactly one function in MATCHES. */
5738 if (matches == NULL_TREE)
5740 /* There were *no* matches. */
5741 if (flags & tf_error)
5743 error ("no matches converting function %qD to type %q#T",
5744 DECL_NAME (OVL_FUNCTION (overload)),
5745 target_type);
5747 /* print_candidates expects a chain with the functions in
5748 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5749 so why be clever?). */
5750 for (; overload; overload = OVL_NEXT (overload))
5751 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5752 matches);
5754 print_candidates (matches);
5756 return error_mark_node;
5758 else if (TREE_CHAIN (matches))
5760 /* There were too many matches. */
5762 if (flags & tf_error)
5764 tree match;
5766 error ("converting overloaded function %qD to type %q#T is ambiguous",
5767 DECL_NAME (OVL_FUNCTION (overload)),
5768 target_type);
5770 /* Since print_candidates expects the functions in the
5771 TREE_VALUE slot, we flip them here. */
5772 for (match = matches; match; match = TREE_CHAIN (match))
5773 TREE_VALUE (match) = TREE_PURPOSE (match);
5775 print_candidates (matches);
5778 return error_mark_node;
5781 /* Good, exactly one match. Now, convert it to the correct type. */
5782 fn = TREE_PURPOSE (matches);
5784 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5785 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
5787 static int explained;
5789 if (!(flags & tf_error))
5790 return error_mark_node;
5792 pedwarn ("assuming pointer to member %qD", fn);
5793 if (!explained)
5795 pedwarn ("(a pointer to member can only be formed with %<&%E%>)", fn);
5796 explained = 1;
5800 /* If we're doing overload resolution purely for the purpose of
5801 determining conversion sequences, we should not consider the
5802 function used. If this conversion sequence is selected, the
5803 function will be marked as used at this point. */
5804 if (!(flags & tf_conv))
5805 mark_used (fn);
5807 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5808 return build_unary_op (ADDR_EXPR, fn, 0);
5809 else
5811 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5812 will mark the function as addressed, but here we must do it
5813 explicitly. */
5814 cxx_mark_addressable (fn);
5816 return fn;
5820 /* This function will instantiate the type of the expression given in
5821 RHS to match the type of LHSTYPE. If errors exist, then return
5822 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
5823 we complain on errors. If we are not complaining, never modify rhs,
5824 as overload resolution wants to try many possible instantiations, in
5825 the hope that at least one will work.
5827 For non-recursive calls, LHSTYPE should be a function, pointer to
5828 function, or a pointer to member function. */
5830 tree
5831 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
5833 tsubst_flags_t flags_in = flags;
5835 flags &= ~tf_ptrmem_ok;
5837 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5839 if (flags & tf_error)
5840 error ("not enough type information");
5841 return error_mark_node;
5844 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5846 if (same_type_p (lhstype, TREE_TYPE (rhs)))
5847 return rhs;
5848 if (flag_ms_extensions
5849 && TYPE_PTRMEMFUNC_P (lhstype)
5850 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
5851 /* Microsoft allows `A::f' to be resolved to a
5852 pointer-to-member. */
5854 else
5856 if (flags & tf_error)
5857 error ("argument of type %qT does not match %qT",
5858 TREE_TYPE (rhs), lhstype);
5859 return error_mark_node;
5863 if (TREE_CODE (rhs) == BASELINK)
5864 rhs = BASELINK_FUNCTIONS (rhs);
5866 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
5867 deduce any type information. */
5868 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
5870 if (flags & tf_error)
5871 error ("not enough type information");
5872 return error_mark_node;
5875 /* We don't overwrite rhs if it is an overloaded function.
5876 Copying it would destroy the tree link. */
5877 if (TREE_CODE (rhs) != OVERLOAD)
5878 rhs = copy_node (rhs);
5880 /* This should really only be used when attempting to distinguish
5881 what sort of a pointer to function we have. For now, any
5882 arithmetic operation which is not supported on pointers
5883 is rejected as an error. */
5885 switch (TREE_CODE (rhs))
5887 case TYPE_EXPR:
5888 case CONVERT_EXPR:
5889 case SAVE_EXPR:
5890 case CONSTRUCTOR:
5891 gcc_unreachable ();
5893 case INDIRECT_REF:
5894 case ARRAY_REF:
5896 tree new_rhs;
5898 new_rhs = instantiate_type (build_pointer_type (lhstype),
5899 TREE_OPERAND (rhs, 0), flags);
5900 if (new_rhs == error_mark_node)
5901 return error_mark_node;
5903 TREE_TYPE (rhs) = lhstype;
5904 TREE_OPERAND (rhs, 0) = new_rhs;
5905 return rhs;
5908 case NOP_EXPR:
5909 rhs = copy_node (TREE_OPERAND (rhs, 0));
5910 TREE_TYPE (rhs) = unknown_type_node;
5911 return instantiate_type (lhstype, rhs, flags);
5913 case COMPONENT_REF:
5915 tree member = TREE_OPERAND (rhs, 1);
5917 member = instantiate_type (lhstype, member, flags);
5918 if (member != error_mark_node
5919 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
5920 /* Do not lose object's side effects. */
5921 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
5922 TREE_OPERAND (rhs, 0), member);
5923 return member;
5926 case OFFSET_REF:
5927 rhs = TREE_OPERAND (rhs, 1);
5928 if (BASELINK_P (rhs))
5929 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags_in);
5931 /* This can happen if we are forming a pointer-to-member for a
5932 member template. */
5933 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
5935 /* Fall through. */
5937 case TEMPLATE_ID_EXPR:
5939 tree fns = TREE_OPERAND (rhs, 0);
5940 tree args = TREE_OPERAND (rhs, 1);
5942 return
5943 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
5944 /*template_only=*/true,
5945 args);
5948 case OVERLOAD:
5949 case FUNCTION_DECL:
5950 return
5951 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
5952 /*template_only=*/false,
5953 /*explicit_targs=*/NULL_TREE);
5955 case CALL_EXPR:
5956 /* This is too hard for now. */
5957 gcc_unreachable ();
5959 case PLUS_EXPR:
5960 case MINUS_EXPR:
5961 case COMPOUND_EXPR:
5962 TREE_OPERAND (rhs, 0)
5963 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5964 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5965 return error_mark_node;
5966 TREE_OPERAND (rhs, 1)
5967 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5968 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5969 return error_mark_node;
5971 TREE_TYPE (rhs) = lhstype;
5972 return rhs;
5974 case MULT_EXPR:
5975 case TRUNC_DIV_EXPR:
5976 case FLOOR_DIV_EXPR:
5977 case CEIL_DIV_EXPR:
5978 case ROUND_DIV_EXPR:
5979 case RDIV_EXPR:
5980 case TRUNC_MOD_EXPR:
5981 case FLOOR_MOD_EXPR:
5982 case CEIL_MOD_EXPR:
5983 case ROUND_MOD_EXPR:
5984 case FIX_ROUND_EXPR:
5985 case FIX_FLOOR_EXPR:
5986 case FIX_CEIL_EXPR:
5987 case FIX_TRUNC_EXPR:
5988 case FLOAT_EXPR:
5989 case NEGATE_EXPR:
5990 case ABS_EXPR:
5991 case MAX_EXPR:
5992 case MIN_EXPR:
5994 case BIT_AND_EXPR:
5995 case BIT_IOR_EXPR:
5996 case BIT_XOR_EXPR:
5997 case LSHIFT_EXPR:
5998 case RSHIFT_EXPR:
5999 case LROTATE_EXPR:
6000 case RROTATE_EXPR:
6002 case PREINCREMENT_EXPR:
6003 case PREDECREMENT_EXPR:
6004 case POSTINCREMENT_EXPR:
6005 case POSTDECREMENT_EXPR:
6006 if (flags & tf_error)
6007 error ("invalid operation on uninstantiated type");
6008 return error_mark_node;
6010 case TRUTH_AND_EXPR:
6011 case TRUTH_OR_EXPR:
6012 case TRUTH_XOR_EXPR:
6013 case LT_EXPR:
6014 case LE_EXPR:
6015 case GT_EXPR:
6016 case GE_EXPR:
6017 case EQ_EXPR:
6018 case NE_EXPR:
6019 case TRUTH_ANDIF_EXPR:
6020 case TRUTH_ORIF_EXPR:
6021 case TRUTH_NOT_EXPR:
6022 if (flags & tf_error)
6023 error ("not enough type information");
6024 return error_mark_node;
6026 case COND_EXPR:
6027 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6029 if (flags & tf_error)
6030 error ("not enough type information");
6031 return error_mark_node;
6033 TREE_OPERAND (rhs, 1)
6034 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6035 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6036 return error_mark_node;
6037 TREE_OPERAND (rhs, 2)
6038 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6039 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6040 return error_mark_node;
6042 TREE_TYPE (rhs) = lhstype;
6043 return rhs;
6045 case MODIFY_EXPR:
6046 TREE_OPERAND (rhs, 1)
6047 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6048 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6049 return error_mark_node;
6051 TREE_TYPE (rhs) = lhstype;
6052 return rhs;
6054 case ADDR_EXPR:
6056 if (PTRMEM_OK_P (rhs))
6057 flags |= tf_ptrmem_ok;
6059 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6062 case ERROR_MARK:
6063 return error_mark_node;
6065 default:
6066 gcc_unreachable ();
6068 return error_mark_node;
6071 /* Return the name of the virtual function pointer field
6072 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6073 this may have to look back through base types to find the
6074 ultimate field name. (For single inheritance, these could
6075 all be the same name. Who knows for multiple inheritance). */
6077 static tree
6078 get_vfield_name (tree type)
6080 tree binfo, base_binfo;
6081 char *buf;
6083 for (binfo = TYPE_BINFO (type);
6084 BINFO_N_BASE_BINFOS (binfo);
6085 binfo = base_binfo)
6087 base_binfo = BINFO_BASE_BINFO (binfo, 0);
6089 if (BINFO_VIRTUAL_P (base_binfo)
6090 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
6091 break;
6094 type = BINFO_TYPE (binfo);
6095 buf = alloca (sizeof (VFIELD_NAME_FORMAT) + TYPE_NAME_LENGTH (type) + 2);
6096 sprintf (buf, VFIELD_NAME_FORMAT,
6097 IDENTIFIER_POINTER (constructor_name (type)));
6098 return get_identifier (buf);
6101 void
6102 print_class_statistics (void)
6104 #ifdef GATHER_STATISTICS
6105 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6106 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6107 if (n_vtables)
6109 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6110 n_vtables, n_vtable_searches);
6111 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6112 n_vtable_entries, n_vtable_elems);
6114 #endif
6117 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6118 according to [class]:
6119 The class-name is also inserted
6120 into the scope of the class itself. For purposes of access checking,
6121 the inserted class name is treated as if it were a public member name. */
6123 void
6124 build_self_reference (void)
6126 tree name = constructor_name (current_class_type);
6127 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6128 tree saved_cas;
6130 DECL_NONLOCAL (value) = 1;
6131 DECL_CONTEXT (value) = current_class_type;
6132 DECL_ARTIFICIAL (value) = 1;
6133 SET_DECL_SELF_REFERENCE_P (value);
6135 if (processing_template_decl)
6136 value = push_template_decl (value);
6138 saved_cas = current_access_specifier;
6139 current_access_specifier = access_public_node;
6140 finish_member_declaration (value);
6141 current_access_specifier = saved_cas;
6144 /* Returns 1 if TYPE contains only padding bytes. */
6147 is_empty_class (tree type)
6149 if (type == error_mark_node)
6150 return 0;
6152 if (! IS_AGGR_TYPE (type))
6153 return 0;
6155 /* In G++ 3.2, whether or not a class was empty was determined by
6156 looking at its size. */
6157 if (abi_version_at_least (2))
6158 return CLASSTYPE_EMPTY_P (type);
6159 else
6160 return integer_zerop (CLASSTYPE_SIZE (type));
6163 /* Returns true if TYPE contains an empty class. */
6165 static bool
6166 contains_empty_class_p (tree type)
6168 if (is_empty_class (type))
6169 return true;
6170 if (CLASS_TYPE_P (type))
6172 tree field;
6173 tree binfo;
6174 tree base_binfo;
6175 int i;
6177 for (binfo = TYPE_BINFO (type), i = 0;
6178 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6179 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
6180 return true;
6181 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6182 if (TREE_CODE (field) == FIELD_DECL
6183 && !DECL_ARTIFICIAL (field)
6184 && is_empty_class (TREE_TYPE (field)))
6185 return true;
6187 else if (TREE_CODE (type) == ARRAY_TYPE)
6188 return contains_empty_class_p (TREE_TYPE (type));
6189 return false;
6192 /* Note that NAME was looked up while the current class was being
6193 defined and that the result of that lookup was DECL. */
6195 void
6196 maybe_note_name_used_in_class (tree name, tree decl)
6198 splay_tree names_used;
6200 /* If we're not defining a class, there's nothing to do. */
6201 if (!(innermost_scope_kind() == sk_class
6202 && TYPE_BEING_DEFINED (current_class_type)))
6203 return;
6205 /* If there's already a binding for this NAME, then we don't have
6206 anything to worry about. */
6207 if (lookup_member (current_class_type, name,
6208 /*protect=*/0, /*want_type=*/false))
6209 return;
6211 if (!current_class_stack[current_class_depth - 1].names_used)
6212 current_class_stack[current_class_depth - 1].names_used
6213 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6214 names_used = current_class_stack[current_class_depth - 1].names_used;
6216 splay_tree_insert (names_used,
6217 (splay_tree_key) name,
6218 (splay_tree_value) decl);
6221 /* Note that NAME was declared (as DECL) in the current class. Check
6222 to see that the declaration is valid. */
6224 void
6225 note_name_declared_in_class (tree name, tree decl)
6227 splay_tree names_used;
6228 splay_tree_node n;
6230 /* Look to see if we ever used this name. */
6231 names_used
6232 = current_class_stack[current_class_depth - 1].names_used;
6233 if (!names_used)
6234 return;
6236 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6237 if (n)
6239 /* [basic.scope.class]
6241 A name N used in a class S shall refer to the same declaration
6242 in its context and when re-evaluated in the completed scope of
6243 S. */
6244 error ("declaration of %q#D", decl);
6245 error ("changes meaning of %qD from %q+#D",
6246 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
6250 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6251 Secondary vtables are merged with primary vtables; this function
6252 will return the VAR_DECL for the primary vtable. */
6254 tree
6255 get_vtbl_decl_for_binfo (tree binfo)
6257 tree decl;
6259 decl = BINFO_VTABLE (binfo);
6260 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6262 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
6263 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6265 if (decl)
6266 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6267 return decl;
6271 /* Returns the binfo for the primary base of BINFO. If the resulting
6272 BINFO is a virtual base, and it is inherited elsewhere in the
6273 hierarchy, then the returned binfo might not be the primary base of
6274 BINFO in the complete object. Check BINFO_PRIMARY_P or
6275 BINFO_LOST_PRIMARY_P to be sure. */
6277 tree
6278 get_primary_binfo (tree binfo)
6280 tree primary_base;
6281 tree result;
6283 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6284 if (!primary_base)
6285 return NULL_TREE;
6287 result = copied_binfo (primary_base, binfo);
6288 return result;
6291 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6293 static int
6294 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6296 if (!indented_p)
6297 fprintf (stream, "%*s", indent, "");
6298 return 1;
6301 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
6302 INDENT should be zero when called from the top level; it is
6303 incremented recursively. IGO indicates the next expected BINFO in
6304 inheritance graph ordering. */
6306 static tree
6307 dump_class_hierarchy_r (FILE *stream,
6308 int flags,
6309 tree binfo,
6310 tree igo,
6311 int indent)
6313 int indented = 0;
6314 tree base_binfo;
6315 int i;
6317 indented = maybe_indent_hierarchy (stream, indent, 0);
6318 fprintf (stream, "%s (0x%lx) ",
6319 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
6320 (unsigned long) binfo);
6321 if (binfo != igo)
6323 fprintf (stream, "alternative-path\n");
6324 return igo;
6326 igo = TREE_CHAIN (binfo);
6328 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6329 tree_low_cst (BINFO_OFFSET (binfo), 0));
6330 if (is_empty_class (BINFO_TYPE (binfo)))
6331 fprintf (stream, " empty");
6332 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6333 fprintf (stream, " nearly-empty");
6334 if (BINFO_VIRTUAL_P (binfo))
6335 fprintf (stream, " virtual");
6336 fprintf (stream, "\n");
6338 indented = 0;
6339 if (BINFO_PRIMARY_P (binfo))
6341 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6342 fprintf (stream, " primary-for %s (0x%lx)",
6343 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
6344 TFF_PLAIN_IDENTIFIER),
6345 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
6347 if (BINFO_LOST_PRIMARY_P (binfo))
6349 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6350 fprintf (stream, " lost-primary");
6352 if (indented)
6353 fprintf (stream, "\n");
6355 if (!(flags & TDF_SLIM))
6357 int indented = 0;
6359 if (BINFO_SUBVTT_INDEX (binfo))
6361 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6362 fprintf (stream, " subvttidx=%s",
6363 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6364 TFF_PLAIN_IDENTIFIER));
6366 if (BINFO_VPTR_INDEX (binfo))
6368 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6369 fprintf (stream, " vptridx=%s",
6370 expr_as_string (BINFO_VPTR_INDEX (binfo),
6371 TFF_PLAIN_IDENTIFIER));
6373 if (BINFO_VPTR_FIELD (binfo))
6375 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6376 fprintf (stream, " vbaseoffset=%s",
6377 expr_as_string (BINFO_VPTR_FIELD (binfo),
6378 TFF_PLAIN_IDENTIFIER));
6380 if (BINFO_VTABLE (binfo))
6382 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6383 fprintf (stream, " vptr=%s",
6384 expr_as_string (BINFO_VTABLE (binfo),
6385 TFF_PLAIN_IDENTIFIER));
6388 if (indented)
6389 fprintf (stream, "\n");
6392 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6393 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
6395 return igo;
6398 /* Dump the BINFO hierarchy for T. */
6400 static void
6401 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
6403 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6404 fprintf (stream, " size=%lu align=%lu\n",
6405 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6406 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6407 fprintf (stream, " base size=%lu base align=%lu\n",
6408 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
6409 / BITS_PER_UNIT),
6410 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
6411 / BITS_PER_UNIT));
6412 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
6413 fprintf (stream, "\n");
6416 /* Debug interface to hierarchy dumping. */
6418 extern void
6419 debug_class (tree t)
6421 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
6424 static void
6425 dump_class_hierarchy (tree t)
6427 int flags;
6428 FILE *stream = dump_begin (TDI_class, &flags);
6430 if (stream)
6432 dump_class_hierarchy_1 (stream, flags, t);
6433 dump_end (TDI_class, stream);
6437 static void
6438 dump_array (FILE * stream, tree decl)
6440 tree inits;
6441 int ix;
6442 HOST_WIDE_INT elt;
6443 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6445 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6446 / BITS_PER_UNIT);
6447 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6448 fprintf (stream, " %s entries",
6449 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6450 TFF_PLAIN_IDENTIFIER));
6451 fprintf (stream, "\n");
6453 for (ix = 0, inits = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
6454 inits; ix++, inits = TREE_CHAIN (inits))
6455 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6456 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6459 static void
6460 dump_vtable (tree t, tree binfo, tree vtable)
6462 int flags;
6463 FILE *stream = dump_begin (TDI_class, &flags);
6465 if (!stream)
6466 return;
6468 if (!(flags & TDF_SLIM))
6470 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6472 fprintf (stream, "%s for %s",
6473 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6474 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
6475 if (ctor_vtbl_p)
6477 if (!BINFO_VIRTUAL_P (binfo))
6478 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6479 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6481 fprintf (stream, "\n");
6482 dump_array (stream, vtable);
6483 fprintf (stream, "\n");
6486 dump_end (TDI_class, stream);
6489 static void
6490 dump_vtt (tree t, tree vtt)
6492 int flags;
6493 FILE *stream = dump_begin (TDI_class, &flags);
6495 if (!stream)
6496 return;
6498 if (!(flags & TDF_SLIM))
6500 fprintf (stream, "VTT for %s\n",
6501 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6502 dump_array (stream, vtt);
6503 fprintf (stream, "\n");
6506 dump_end (TDI_class, stream);
6509 /* Dump a function or thunk and its thunkees. */
6511 static void
6512 dump_thunk (FILE *stream, int indent, tree thunk)
6514 static const char spaces[] = " ";
6515 tree name = DECL_NAME (thunk);
6516 tree thunks;
6518 fprintf (stream, "%.*s%p %s %s", indent, spaces,
6519 (void *)thunk,
6520 !DECL_THUNK_P (thunk) ? "function"
6521 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
6522 name ? IDENTIFIER_POINTER (name) : "<unset>");
6523 if (DECL_THUNK_P (thunk))
6525 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
6526 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
6528 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
6529 if (!virtual_adjust)
6530 /*NOP*/;
6531 else if (DECL_THIS_THUNK_P (thunk))
6532 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
6533 tree_low_cst (virtual_adjust, 0));
6534 else
6535 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
6536 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
6537 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
6538 if (THUNK_ALIAS (thunk))
6539 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
6541 fprintf (stream, "\n");
6542 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
6543 dump_thunk (stream, indent + 2, thunks);
6546 /* Dump the thunks for FN. */
6548 extern void
6549 debug_thunks (tree fn)
6551 dump_thunk (stderr, 0, fn);
6554 /* Virtual function table initialization. */
6556 /* Create all the necessary vtables for T and its base classes. */
6558 static void
6559 finish_vtbls (tree t)
6561 tree list;
6562 tree vbase;
6564 /* We lay out the primary and secondary vtables in one contiguous
6565 vtable. The primary vtable is first, followed by the non-virtual
6566 secondary vtables in inheritance graph order. */
6567 list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (t)), NULL_TREE);
6568 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6569 TYPE_BINFO (t), t, list);
6571 /* Then come the virtual bases, also in inheritance graph order. */
6572 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6574 if (!BINFO_VIRTUAL_P (vbase))
6575 continue;
6576 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
6579 if (BINFO_VTABLE (TYPE_BINFO (t)))
6580 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6583 /* Initialize the vtable for BINFO with the INITS. */
6585 static void
6586 initialize_vtable (tree binfo, tree inits)
6588 tree decl;
6590 layout_vtable_decl (binfo, list_length (inits));
6591 decl = get_vtbl_decl_for_binfo (binfo);
6592 initialize_artificial_var (decl, inits);
6593 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6596 /* Build the VTT (virtual table table) for T.
6597 A class requires a VTT if it has virtual bases.
6599 This holds
6600 1 - primary virtual pointer for complete object T
6601 2 - secondary VTTs for each direct non-virtual base of T which requires a
6603 3 - secondary virtual pointers for each direct or indirect base of T which
6604 has virtual bases or is reachable via a virtual path from T.
6605 4 - secondary VTTs for each direct or indirect virtual base of T.
6607 Secondary VTTs look like complete object VTTs without part 4. */
6609 static void
6610 build_vtt (tree t)
6612 tree inits;
6613 tree type;
6614 tree vtt;
6615 tree index;
6617 /* Build up the initializers for the VTT. */
6618 inits = NULL_TREE;
6619 index = size_zero_node;
6620 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6622 /* If we didn't need a VTT, we're done. */
6623 if (!inits)
6624 return;
6626 /* Figure out the type of the VTT. */
6627 type = build_index_type (size_int (list_length (inits) - 1));
6628 type = build_cplus_array_type (const_ptr_type_node, type);
6630 /* Now, build the VTT object itself. */
6631 vtt = build_vtable (t, get_vtt_name (t), type);
6632 initialize_artificial_var (vtt, inits);
6633 /* Add the VTT to the vtables list. */
6634 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6635 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
6637 dump_vtt (t, vtt);
6640 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6641 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6642 and CHAIN the vtable pointer for this binfo after construction is
6643 complete. VALUE can also be another BINFO, in which case we recurse. */
6645 static tree
6646 binfo_ctor_vtable (tree binfo)
6648 tree vt;
6650 while (1)
6652 vt = BINFO_VTABLE (binfo);
6653 if (TREE_CODE (vt) == TREE_LIST)
6654 vt = TREE_VALUE (vt);
6655 if (TREE_CODE (vt) == TREE_BINFO)
6656 binfo = vt;
6657 else
6658 break;
6661 return vt;
6664 /* Data for secondary VTT initialization. */
6665 typedef struct secondary_vptr_vtt_init_data_s
6667 /* Is this the primary VTT? */
6668 bool top_level_p;
6670 /* Current index into the VTT. */
6671 tree index;
6673 /* TREE_LIST of initializers built up. */
6674 tree inits;
6676 /* The type being constructed by this secondary VTT. */
6677 tree type_being_constructed;
6678 } secondary_vptr_vtt_init_data;
6680 /* Recursively build the VTT-initializer for BINFO (which is in the
6681 hierarchy dominated by T). INITS points to the end of the initializer
6682 list to date. INDEX is the VTT index where the next element will be
6683 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6684 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6685 for virtual bases of T. When it is not so, we build the constructor
6686 vtables for the BINFO-in-T variant. */
6688 static tree *
6689 build_vtt_inits (tree binfo, tree t, tree *inits, tree *index)
6691 int i;
6692 tree b;
6693 tree init;
6694 tree secondary_vptrs;
6695 secondary_vptr_vtt_init_data data;
6696 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
6698 /* We only need VTTs for subobjects with virtual bases. */
6699 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
6700 return inits;
6702 /* We need to use a construction vtable if this is not the primary
6703 VTT. */
6704 if (!top_level_p)
6706 build_ctor_vtbl_group (binfo, t);
6708 /* Record the offset in the VTT where this sub-VTT can be found. */
6709 BINFO_SUBVTT_INDEX (binfo) = *index;
6712 /* Add the address of the primary vtable for the complete object. */
6713 init = binfo_ctor_vtable (binfo);
6714 *inits = build_tree_list (NULL_TREE, init);
6715 inits = &TREE_CHAIN (*inits);
6716 if (top_level_p)
6718 gcc_assert (!BINFO_VPTR_INDEX (binfo));
6719 BINFO_VPTR_INDEX (binfo) = *index;
6721 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6723 /* Recursively add the secondary VTTs for non-virtual bases. */
6724 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
6725 if (!BINFO_VIRTUAL_P (b))
6726 inits = build_vtt_inits (b, t, inits, index);
6728 /* Add secondary virtual pointers for all subobjects of BINFO with
6729 either virtual bases or reachable along a virtual path, except
6730 subobjects that are non-virtual primary bases. */
6731 data.top_level_p = top_level_p;
6732 data.index = *index;
6733 data.inits = NULL;
6734 data.type_being_constructed = BINFO_TYPE (binfo);
6736 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
6738 *index = data.index;
6740 /* The secondary vptrs come back in reverse order. After we reverse
6741 them, and add the INITS, the last init will be the first element
6742 of the chain. */
6743 secondary_vptrs = data.inits;
6744 if (secondary_vptrs)
6746 *inits = nreverse (secondary_vptrs);
6747 inits = &TREE_CHAIN (secondary_vptrs);
6748 gcc_assert (*inits == NULL_TREE);
6751 if (top_level_p)
6752 /* Add the secondary VTTs for virtual bases in inheritance graph
6753 order. */
6754 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
6756 if (!BINFO_VIRTUAL_P (b))
6757 continue;
6759 inits = build_vtt_inits (b, t, inits, index);
6761 else
6762 /* Remove the ctor vtables we created. */
6763 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
6765 return inits;
6768 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
6769 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
6771 static tree
6772 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
6774 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
6776 /* We don't care about bases that don't have vtables. */
6777 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6778 return dfs_skip_bases;
6780 /* We're only interested in proper subobjects of the type being
6781 constructed. */
6782 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
6783 return NULL_TREE;
6785 /* We're only interested in bases with virtual bases or reachable
6786 via a virtual path from the type being constructed. */
6787 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
6788 || binfo_via_virtual (binfo, data->type_being_constructed)))
6789 return dfs_skip_bases;
6791 /* We're not interested in non-virtual primary bases. */
6792 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
6793 return NULL_TREE;
6795 /* Record the index where this secondary vptr can be found. */
6796 if (data->top_level_p)
6798 gcc_assert (!BINFO_VPTR_INDEX (binfo));
6799 BINFO_VPTR_INDEX (binfo) = data->index;
6801 if (BINFO_VIRTUAL_P (binfo))
6803 /* It's a primary virtual base, and this is not a
6804 construction vtable. Find the base this is primary of in
6805 the inheritance graph, and use that base's vtable
6806 now. */
6807 while (BINFO_PRIMARY_P (binfo))
6808 binfo = BINFO_INHERITANCE_CHAIN (binfo);
6812 /* Add the initializer for the secondary vptr itself. */
6813 data->inits = tree_cons (NULL_TREE, binfo_ctor_vtable (binfo), data->inits);
6815 /* Advance the vtt index. */
6816 data->index = size_binop (PLUS_EXPR, data->index,
6817 TYPE_SIZE_UNIT (ptr_type_node));
6819 return NULL_TREE;
6822 /* Called from build_vtt_inits via dfs_walk. After building
6823 constructor vtables and generating the sub-vtt from them, we need
6824 to restore the BINFO_VTABLES that were scribbled on. DATA is the
6825 binfo of the base whose sub vtt was generated. */
6827 static tree
6828 dfs_fixup_binfo_vtbls (tree binfo, void* data)
6830 tree vtable = BINFO_VTABLE (binfo);
6832 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
6833 /* If this class has no vtable, none of its bases do. */
6834 return dfs_skip_bases;
6836 if (!vtable)
6837 /* This might be a primary base, so have no vtable in this
6838 hierarchy. */
6839 return NULL_TREE;
6841 /* If we scribbled the construction vtable vptr into BINFO, clear it
6842 out now. */
6843 if (TREE_CODE (vtable) == TREE_LIST
6844 && (TREE_PURPOSE (vtable) == (tree) data))
6845 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
6847 return NULL_TREE;
6850 /* Build the construction vtable group for BINFO which is in the
6851 hierarchy dominated by T. */
6853 static void
6854 build_ctor_vtbl_group (tree binfo, tree t)
6856 tree list;
6857 tree type;
6858 tree vtbl;
6859 tree inits;
6860 tree id;
6861 tree vbase;
6863 /* See if we've already created this construction vtable group. */
6864 id = mangle_ctor_vtbl_for_type (t, binfo);
6865 if (IDENTIFIER_GLOBAL_VALUE (id))
6866 return;
6868 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
6869 /* Build a version of VTBL (with the wrong type) for use in
6870 constructing the addresses of secondary vtables in the
6871 construction vtable group. */
6872 vtbl = build_vtable (t, id, ptr_type_node);
6873 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
6874 list = build_tree_list (vtbl, NULL_TREE);
6875 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
6876 binfo, t, list);
6878 /* Add the vtables for each of our virtual bases using the vbase in T
6879 binfo. */
6880 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
6881 vbase;
6882 vbase = TREE_CHAIN (vbase))
6884 tree b;
6886 if (!BINFO_VIRTUAL_P (vbase))
6887 continue;
6888 b = copied_binfo (vbase, binfo);
6890 accumulate_vtbl_inits (b, vbase, binfo, t, list);
6892 inits = TREE_VALUE (list);
6894 /* Figure out the type of the construction vtable. */
6895 type = build_index_type (size_int (list_length (inits) - 1));
6896 type = build_cplus_array_type (vtable_entry_type, type);
6897 TREE_TYPE (vtbl) = type;
6899 /* Initialize the construction vtable. */
6900 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
6901 initialize_artificial_var (vtbl, inits);
6902 dump_vtable (t, binfo, vtbl);
6905 /* Add the vtbl initializers for BINFO (and its bases other than
6906 non-virtual primaries) to the list of INITS. BINFO is in the
6907 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
6908 the constructor the vtbl inits should be accumulated for. (If this
6909 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
6910 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
6911 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
6912 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
6913 but are not necessarily the same in terms of layout. */
6915 static void
6916 accumulate_vtbl_inits (tree binfo,
6917 tree orig_binfo,
6918 tree rtti_binfo,
6919 tree t,
6920 tree inits)
6922 int i;
6923 tree base_binfo;
6924 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
6926 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
6928 /* If it doesn't have a vptr, we don't do anything. */
6929 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
6930 return;
6932 /* If we're building a construction vtable, we're not interested in
6933 subobjects that don't require construction vtables. */
6934 if (ctor_vtbl_p
6935 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
6936 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
6937 return;
6939 /* Build the initializers for the BINFO-in-T vtable. */
6940 TREE_VALUE (inits)
6941 = chainon (TREE_VALUE (inits),
6942 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
6943 rtti_binfo, t, inits));
6945 /* Walk the BINFO and its bases. We walk in preorder so that as we
6946 initialize each vtable we can figure out at what offset the
6947 secondary vtable lies from the primary vtable. We can't use
6948 dfs_walk here because we need to iterate through bases of BINFO
6949 and RTTI_BINFO simultaneously. */
6950 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6952 /* Skip virtual bases. */
6953 if (BINFO_VIRTUAL_P (base_binfo))
6954 continue;
6955 accumulate_vtbl_inits (base_binfo,
6956 BINFO_BASE_BINFO (orig_binfo, i),
6957 rtti_binfo, t,
6958 inits);
6962 /* Called from accumulate_vtbl_inits. Returns the initializers for
6963 the BINFO vtable. */
6965 static tree
6966 dfs_accumulate_vtbl_inits (tree binfo,
6967 tree orig_binfo,
6968 tree rtti_binfo,
6969 tree t,
6970 tree l)
6972 tree inits = NULL_TREE;
6973 tree vtbl = NULL_TREE;
6974 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
6976 if (ctor_vtbl_p
6977 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
6979 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
6980 primary virtual base. If it is not the same primary in
6981 the hierarchy of T, we'll need to generate a ctor vtable
6982 for it, to place at its location in T. If it is the same
6983 primary, we still need a VTT entry for the vtable, but it
6984 should point to the ctor vtable for the base it is a
6985 primary for within the sub-hierarchy of RTTI_BINFO.
6987 There are three possible cases:
6989 1) We are in the same place.
6990 2) We are a primary base within a lost primary virtual base of
6991 RTTI_BINFO.
6992 3) We are primary to something not a base of RTTI_BINFO. */
6994 tree b;
6995 tree last = NULL_TREE;
6997 /* First, look through the bases we are primary to for RTTI_BINFO
6998 or a virtual base. */
6999 b = binfo;
7000 while (BINFO_PRIMARY_P (b))
7002 b = BINFO_INHERITANCE_CHAIN (b);
7003 last = b;
7004 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7005 goto found;
7007 /* If we run out of primary links, keep looking down our
7008 inheritance chain; we might be an indirect primary. */
7009 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7010 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7011 break;
7012 found:
7014 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7015 base B and it is a base of RTTI_BINFO, this is case 2. In
7016 either case, we share our vtable with LAST, i.e. the
7017 derived-most base within B of which we are a primary. */
7018 if (b == rtti_binfo
7019 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
7020 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7021 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7022 binfo_ctor_vtable after everything's been set up. */
7023 vtbl = last;
7025 /* Otherwise, this is case 3 and we get our own. */
7027 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
7028 return inits;
7030 if (!vtbl)
7032 tree index;
7033 int non_fn_entries;
7035 /* Compute the initializer for this vtable. */
7036 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7037 &non_fn_entries);
7039 /* Figure out the position to which the VPTR should point. */
7040 vtbl = TREE_PURPOSE (l);
7041 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl);
7042 index = size_binop (PLUS_EXPR,
7043 size_int (non_fn_entries),
7044 size_int (list_length (TREE_VALUE (l))));
7045 index = size_binop (MULT_EXPR,
7046 TYPE_SIZE_UNIT (vtable_entry_type),
7047 index);
7048 vtbl = build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7051 if (ctor_vtbl_p)
7052 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7053 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7054 straighten this out. */
7055 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7056 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
7057 inits = NULL_TREE;
7058 else
7059 /* For an ordinary vtable, set BINFO_VTABLE. */
7060 BINFO_VTABLE (binfo) = vtbl;
7062 return inits;
7065 static GTY(()) tree abort_fndecl_addr;
7067 /* Construct the initializer for BINFO's virtual function table. BINFO
7068 is part of the hierarchy dominated by T. If we're building a
7069 construction vtable, the ORIG_BINFO is the binfo we should use to
7070 find the actual function pointers to put in the vtable - but they
7071 can be overridden on the path to most-derived in the graph that
7072 ORIG_BINFO belongs. Otherwise,
7073 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7074 BINFO that should be indicated by the RTTI information in the
7075 vtable; it will be a base class of T, rather than T itself, if we
7076 are building a construction vtable.
7078 The value returned is a TREE_LIST suitable for wrapping in a
7079 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7080 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7081 number of non-function entries in the vtable.
7083 It might seem that this function should never be called with a
7084 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7085 base is always subsumed by a derived class vtable. However, when
7086 we are building construction vtables, we do build vtables for
7087 primary bases; we need these while the primary base is being
7088 constructed. */
7090 static tree
7091 build_vtbl_initializer (tree binfo,
7092 tree orig_binfo,
7093 tree t,
7094 tree rtti_binfo,
7095 int* non_fn_entries_p)
7097 tree v, b;
7098 tree vfun_inits;
7099 vtbl_init_data vid;
7100 unsigned ix;
7101 tree vbinfo;
7102 VEC(tree,gc) *vbases;
7104 /* Initialize VID. */
7105 memset (&vid, 0, sizeof (vid));
7106 vid.binfo = binfo;
7107 vid.derived = t;
7108 vid.rtti_binfo = rtti_binfo;
7109 vid.last_init = &vid.inits;
7110 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7111 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
7112 vid.generate_vcall_entries = true;
7113 /* The first vbase or vcall offset is at index -3 in the vtable. */
7114 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7116 /* Add entries to the vtable for RTTI. */
7117 build_rtti_vtbl_entries (binfo, &vid);
7119 /* Create an array for keeping track of the functions we've
7120 processed. When we see multiple functions with the same
7121 signature, we share the vcall offsets. */
7122 vid.fns = VEC_alloc (tree, gc, 32);
7123 /* Add the vcall and vbase offset entries. */
7124 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7126 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7127 build_vbase_offset_vtbl_entries. */
7128 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
7129 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
7130 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
7132 /* If the target requires padding between data entries, add that now. */
7133 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7135 tree cur, *prev;
7137 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7139 tree add = cur;
7140 int i;
7142 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7143 add = tree_cons (NULL_TREE,
7144 build1 (NOP_EXPR, vtable_entry_type,
7145 null_pointer_node),
7146 add);
7147 *prev = add;
7151 if (non_fn_entries_p)
7152 *non_fn_entries_p = list_length (vid.inits);
7154 /* Go through all the ordinary virtual functions, building up
7155 initializers. */
7156 vfun_inits = NULL_TREE;
7157 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7159 tree delta;
7160 tree vcall_index;
7161 tree fn, fn_original;
7162 tree init = NULL_TREE;
7164 fn = BV_FN (v);
7165 fn_original = fn;
7166 if (DECL_THUNK_P (fn))
7168 if (!DECL_NAME (fn))
7169 finish_thunk (fn);
7170 if (THUNK_ALIAS (fn))
7172 fn = THUNK_ALIAS (fn);
7173 BV_FN (v) = fn;
7175 fn_original = THUNK_TARGET (fn);
7178 /* If the only definition of this function signature along our
7179 primary base chain is from a lost primary, this vtable slot will
7180 never be used, so just zero it out. This is important to avoid
7181 requiring extra thunks which cannot be generated with the function.
7183 We first check this in update_vtable_entry_for_fn, so we handle
7184 restored primary bases properly; we also need to do it here so we
7185 zero out unused slots in ctor vtables, rather than filling themff
7186 with erroneous values (though harmless, apart from relocation
7187 costs). */
7188 for (b = binfo; ; b = get_primary_binfo (b))
7190 /* We found a defn before a lost primary; go ahead as normal. */
7191 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7192 break;
7194 /* The nearest definition is from a lost primary; clear the
7195 slot. */
7196 if (BINFO_LOST_PRIMARY_P (b))
7198 init = size_zero_node;
7199 break;
7203 if (! init)
7205 /* Pull the offset for `this', and the function to call, out of
7206 the list. */
7207 delta = BV_DELTA (v);
7208 vcall_index = BV_VCALL_INDEX (v);
7210 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
7211 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7213 /* You can't call an abstract virtual function; it's abstract.
7214 So, we replace these functions with __pure_virtual. */
7215 if (DECL_PURE_VIRTUAL_P (fn_original))
7217 fn = abort_fndecl;
7218 if (abort_fndecl_addr == NULL)
7219 abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7220 init = abort_fndecl_addr;
7222 else
7224 if (!integer_zerop (delta) || vcall_index)
7226 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7227 if (!DECL_NAME (fn))
7228 finish_thunk (fn);
7230 /* Take the address of the function, considering it to be of an
7231 appropriate generic type. */
7232 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7236 /* And add it to the chain of initializers. */
7237 if (TARGET_VTABLE_USES_DESCRIPTORS)
7239 int i;
7240 if (init == size_zero_node)
7241 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7242 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7243 else
7244 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7246 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
7247 TREE_OPERAND (init, 0),
7248 build_int_cst (NULL_TREE, i));
7249 TREE_CONSTANT (fdesc) = 1;
7250 TREE_INVARIANT (fdesc) = 1;
7252 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7255 else
7256 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7259 /* The initializers for virtual functions were built up in reverse
7260 order; straighten them out now. */
7261 vfun_inits = nreverse (vfun_inits);
7263 /* The negative offset initializers are also in reverse order. */
7264 vid.inits = nreverse (vid.inits);
7266 /* Chain the two together. */
7267 return chainon (vid.inits, vfun_inits);
7270 /* Adds to vid->inits the initializers for the vbase and vcall
7271 offsets in BINFO, which is in the hierarchy dominated by T. */
7273 static void
7274 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7276 tree b;
7278 /* If this is a derived class, we must first create entries
7279 corresponding to the primary base class. */
7280 b = get_primary_binfo (binfo);
7281 if (b)
7282 build_vcall_and_vbase_vtbl_entries (b, vid);
7284 /* Add the vbase entries for this base. */
7285 build_vbase_offset_vtbl_entries (binfo, vid);
7286 /* Add the vcall entries for this base. */
7287 build_vcall_offset_vtbl_entries (binfo, vid);
7290 /* Returns the initializers for the vbase offset entries in the vtable
7291 for BINFO (which is part of the class hierarchy dominated by T), in
7292 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7293 where the next vbase offset will go. */
7295 static void
7296 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7298 tree vbase;
7299 tree t;
7300 tree non_primary_binfo;
7302 /* If there are no virtual baseclasses, then there is nothing to
7303 do. */
7304 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7305 return;
7307 t = vid->derived;
7309 /* We might be a primary base class. Go up the inheritance hierarchy
7310 until we find the most derived class of which we are a primary base:
7311 it is the offset of that which we need to use. */
7312 non_primary_binfo = binfo;
7313 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7315 tree b;
7317 /* If we have reached a virtual base, then it must be a primary
7318 base (possibly multi-level) of vid->binfo, or we wouldn't
7319 have called build_vcall_and_vbase_vtbl_entries for it. But it
7320 might be a lost primary, so just skip down to vid->binfo. */
7321 if (BINFO_VIRTUAL_P (non_primary_binfo))
7323 non_primary_binfo = vid->binfo;
7324 break;
7327 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7328 if (get_primary_binfo (b) != non_primary_binfo)
7329 break;
7330 non_primary_binfo = b;
7333 /* Go through the virtual bases, adding the offsets. */
7334 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7335 vbase;
7336 vbase = TREE_CHAIN (vbase))
7338 tree b;
7339 tree delta;
7341 if (!BINFO_VIRTUAL_P (vbase))
7342 continue;
7344 /* Find the instance of this virtual base in the complete
7345 object. */
7346 b = copied_binfo (vbase, binfo);
7348 /* If we've already got an offset for this virtual base, we
7349 don't need another one. */
7350 if (BINFO_VTABLE_PATH_MARKED (b))
7351 continue;
7352 BINFO_VTABLE_PATH_MARKED (b) = 1;
7354 /* Figure out where we can find this vbase offset. */
7355 delta = size_binop (MULT_EXPR,
7356 vid->index,
7357 convert (ssizetype,
7358 TYPE_SIZE_UNIT (vtable_entry_type)));
7359 if (vid->primary_vtbl_p)
7360 BINFO_VPTR_FIELD (b) = delta;
7362 if (binfo != TYPE_BINFO (t))
7363 /* The vbase offset had better be the same. */
7364 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
7366 /* The next vbase will come at a more negative offset. */
7367 vid->index = size_binop (MINUS_EXPR, vid->index,
7368 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7370 /* The initializer is the delta from BINFO to this virtual base.
7371 The vbase offsets go in reverse inheritance-graph order, and
7372 we are walking in inheritance graph order so these end up in
7373 the right order. */
7374 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7376 *vid->last_init
7377 = build_tree_list (NULL_TREE,
7378 fold_build1 (NOP_EXPR,
7379 vtable_entry_type,
7380 delta));
7381 vid->last_init = &TREE_CHAIN (*vid->last_init);
7385 /* Adds the initializers for the vcall offset entries in the vtable
7386 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7387 to VID->INITS. */
7389 static void
7390 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7392 /* We only need these entries if this base is a virtual base. We
7393 compute the indices -- but do not add to the vtable -- when
7394 building the main vtable for a class. */
7395 if (BINFO_VIRTUAL_P (binfo) || binfo == TYPE_BINFO (vid->derived))
7397 /* We need a vcall offset for each of the virtual functions in this
7398 vtable. For example:
7400 class A { virtual void f (); };
7401 class B1 : virtual public A { virtual void f (); };
7402 class B2 : virtual public A { virtual void f (); };
7403 class C: public B1, public B2 { virtual void f (); };
7405 A C object has a primary base of B1, which has a primary base of A. A
7406 C also has a secondary base of B2, which no longer has a primary base
7407 of A. So the B2-in-C construction vtable needs a secondary vtable for
7408 A, which will adjust the A* to a B2* to call f. We have no way of
7409 knowing what (or even whether) this offset will be when we define B2,
7410 so we store this "vcall offset" in the A sub-vtable and look it up in
7411 a "virtual thunk" for B2::f.
7413 We need entries for all the functions in our primary vtable and
7414 in our non-virtual bases' secondary vtables. */
7415 vid->vbase = binfo;
7416 /* If we are just computing the vcall indices -- but do not need
7417 the actual entries -- not that. */
7418 if (!BINFO_VIRTUAL_P (binfo))
7419 vid->generate_vcall_entries = false;
7420 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7421 add_vcall_offset_vtbl_entries_r (binfo, vid);
7425 /* Build vcall offsets, starting with those for BINFO. */
7427 static void
7428 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
7430 int i;
7431 tree primary_binfo;
7432 tree base_binfo;
7434 /* Don't walk into virtual bases -- except, of course, for the
7435 virtual base for which we are building vcall offsets. Any
7436 primary virtual base will have already had its offsets generated
7437 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7438 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
7439 return;
7441 /* If BINFO has a primary base, process it first. */
7442 primary_binfo = get_primary_binfo (binfo);
7443 if (primary_binfo)
7444 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7446 /* Add BINFO itself to the list. */
7447 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7449 /* Scan the non-primary bases of BINFO. */
7450 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7451 if (base_binfo != primary_binfo)
7452 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7455 /* Called from build_vcall_offset_vtbl_entries_r. */
7457 static void
7458 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
7460 /* Make entries for the rest of the virtuals. */
7461 if (abi_version_at_least (2))
7463 tree orig_fn;
7465 /* The ABI requires that the methods be processed in declaration
7466 order. G++ 3.2 used the order in the vtable. */
7467 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
7468 orig_fn;
7469 orig_fn = TREE_CHAIN (orig_fn))
7470 if (DECL_VINDEX (orig_fn))
7471 add_vcall_offset (orig_fn, binfo, vid);
7473 else
7475 tree derived_virtuals;
7476 tree base_virtuals;
7477 tree orig_virtuals;
7478 /* If BINFO is a primary base, the most derived class which has
7479 BINFO as a primary base; otherwise, just BINFO. */
7480 tree non_primary_binfo;
7482 /* We might be a primary base class. Go up the inheritance hierarchy
7483 until we find the most derived class of which we are a primary base:
7484 it is the BINFO_VIRTUALS there that we need to consider. */
7485 non_primary_binfo = binfo;
7486 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7488 tree b;
7490 /* If we have reached a virtual base, then it must be vid->vbase,
7491 because we ignore other virtual bases in
7492 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7493 base (possibly multi-level) of vid->binfo, or we wouldn't
7494 have called build_vcall_and_vbase_vtbl_entries for it. But it
7495 might be a lost primary, so just skip down to vid->binfo. */
7496 if (BINFO_VIRTUAL_P (non_primary_binfo))
7498 gcc_assert (non_primary_binfo == vid->vbase);
7499 non_primary_binfo = vid->binfo;
7500 break;
7503 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7504 if (get_primary_binfo (b) != non_primary_binfo)
7505 break;
7506 non_primary_binfo = b;
7509 if (vid->ctor_vtbl_p)
7510 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7511 where rtti_binfo is the most derived type. */
7512 non_primary_binfo
7513 = original_binfo (non_primary_binfo, vid->rtti_binfo);
7515 for (base_virtuals = BINFO_VIRTUALS (binfo),
7516 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7517 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7518 base_virtuals;
7519 base_virtuals = TREE_CHAIN (base_virtuals),
7520 derived_virtuals = TREE_CHAIN (derived_virtuals),
7521 orig_virtuals = TREE_CHAIN (orig_virtuals))
7523 tree orig_fn;
7525 /* Find the declaration that originally caused this function to
7526 be present in BINFO_TYPE (binfo). */
7527 orig_fn = BV_FN (orig_virtuals);
7529 /* When processing BINFO, we only want to generate vcall slots for
7530 function slots introduced in BINFO. So don't try to generate
7531 one if the function isn't even defined in BINFO. */
7532 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
7533 continue;
7535 add_vcall_offset (orig_fn, binfo, vid);
7540 /* Add a vcall offset entry for ORIG_FN to the vtable. */
7542 static void
7543 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
7545 size_t i;
7546 tree vcall_offset;
7547 tree derived_entry;
7549 /* If there is already an entry for a function with the same
7550 signature as FN, then we do not need a second vcall offset.
7551 Check the list of functions already present in the derived
7552 class vtable. */
7553 for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i)
7555 if (same_signature_p (derived_entry, orig_fn)
7556 /* We only use one vcall offset for virtual destructors,
7557 even though there are two virtual table entries. */
7558 || (DECL_DESTRUCTOR_P (derived_entry)
7559 && DECL_DESTRUCTOR_P (orig_fn)))
7560 return;
7563 /* If we are building these vcall offsets as part of building
7564 the vtable for the most derived class, remember the vcall
7565 offset. */
7566 if (vid->binfo == TYPE_BINFO (vid->derived))
7568 tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
7569 CLASSTYPE_VCALL_INDICES (vid->derived),
7570 NULL);
7571 elt->purpose = orig_fn;
7572 elt->value = vid->index;
7575 /* The next vcall offset will be found at a more negative
7576 offset. */
7577 vid->index = size_binop (MINUS_EXPR, vid->index,
7578 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7580 /* Keep track of this function. */
7581 VEC_safe_push (tree, gc, vid->fns, orig_fn);
7583 if (vid->generate_vcall_entries)
7585 tree base;
7586 tree fn;
7588 /* Find the overriding function. */
7589 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
7590 if (fn == error_mark_node)
7591 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
7592 integer_zero_node);
7593 else
7595 base = TREE_VALUE (fn);
7597 /* The vbase we're working on is a primary base of
7598 vid->binfo. But it might be a lost primary, so its
7599 BINFO_OFFSET might be wrong, so we just use the
7600 BINFO_OFFSET from vid->binfo. */
7601 vcall_offset = size_diffop (BINFO_OFFSET (base),
7602 BINFO_OFFSET (vid->binfo));
7603 vcall_offset = fold_build1 (NOP_EXPR, vtable_entry_type,
7604 vcall_offset);
7606 /* Add the initializer to the vtable. */
7607 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7608 vid->last_init = &TREE_CHAIN (*vid->last_init);
7612 /* Return vtbl initializers for the RTTI entries corresponding to the
7613 BINFO's vtable. The RTTI entries should indicate the object given
7614 by VID->rtti_binfo. */
7616 static void
7617 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
7619 tree b;
7620 tree t;
7621 tree basetype;
7622 tree offset;
7623 tree decl;
7624 tree init;
7626 basetype = BINFO_TYPE (binfo);
7627 t = BINFO_TYPE (vid->rtti_binfo);
7629 /* To find the complete object, we will first convert to our most
7630 primary base, and then add the offset in the vtbl to that value. */
7631 b = binfo;
7632 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7633 && !BINFO_LOST_PRIMARY_P (b))
7635 tree primary_base;
7637 primary_base = get_primary_binfo (b);
7638 gcc_assert (BINFO_PRIMARY_P (primary_base)
7639 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
7640 b = primary_base;
7642 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7644 /* The second entry is the address of the typeinfo object. */
7645 if (flag_rtti)
7646 decl = build_address (get_tinfo_decl (t));
7647 else
7648 decl = integer_zero_node;
7650 /* Convert the declaration to a type that can be stored in the
7651 vtable. */
7652 init = build_nop (vfunc_ptr_type_node, decl);
7653 *vid->last_init = build_tree_list (NULL_TREE, init);
7654 vid->last_init = &TREE_CHAIN (*vid->last_init);
7656 /* Add the offset-to-top entry. It comes earlier in the vtable than
7657 the typeinfo entry. Convert the offset to look like a
7658 function pointer, so that we can put it in the vtable. */
7659 init = build_nop (vfunc_ptr_type_node, offset);
7660 *vid->last_init = build_tree_list (NULL_TREE, init);
7661 vid->last_init = &TREE_CHAIN (*vid->last_init);
7664 /* Fold a OBJ_TYPE_REF expression to the address of a function.
7665 KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */
7667 tree
7668 cp_fold_obj_type_ref (tree ref, tree known_type)
7670 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
7671 HOST_WIDE_INT i = 0;
7672 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
7673 tree fndecl;
7675 while (i != index)
7677 i += (TARGET_VTABLE_USES_DESCRIPTORS
7678 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
7679 v = TREE_CHAIN (v);
7682 fndecl = BV_FN (v);
7684 #ifdef ENABLE_CHECKING
7685 gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref),
7686 DECL_VINDEX (fndecl)));
7687 #endif
7689 cgraph_node (fndecl)->local.vtable_method = true;
7691 return build_address (fndecl);
7694 #include "gt-cp-class.h"