PR c++/28588
[official-gcc.git] / gcc / cp / class.c
blob673b07fb3d00ceb869079a17322dd361be92cfe7
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"
39 #include "tree-dump.h"
41 /* The number of nested classes being processed. If we are not in the
42 scope of any class, this is zero. */
44 int current_class_depth;
46 /* In order to deal with nested classes, we keep a stack of classes.
47 The topmost entry is the innermost class, and is the entry at index
48 CURRENT_CLASS_DEPTH */
50 typedef struct class_stack_node {
51 /* The name of the class. */
52 tree name;
54 /* The _TYPE node for the class. */
55 tree type;
57 /* The access specifier pending for new declarations in the scope of
58 this class. */
59 tree access;
61 /* If were defining TYPE, the names used in this class. */
62 splay_tree names_used;
64 /* Nonzero if this class is no longer open, because of a call to
65 push_to_top_level. */
66 size_t hidden;
67 }* class_stack_node_t;
69 typedef struct vtbl_init_data_s
71 /* The base for which we're building initializers. */
72 tree binfo;
73 /* The type of the most-derived type. */
74 tree derived;
75 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
76 unless ctor_vtbl_p is true. */
77 tree rtti_binfo;
78 /* The negative-index vtable initializers built up so far. These
79 are in order from least negative index to most negative index. */
80 tree inits;
81 /* The last (i.e., most negative) entry in INITS. */
82 tree* last_init;
83 /* The binfo for the virtual base for which we're building
84 vcall offset initializers. */
85 tree vbase;
86 /* The functions in vbase for which we have already provided vcall
87 offsets. */
88 VEC(tree,gc) *fns;
89 /* The vtable index of the next vcall or vbase offset. */
90 tree index;
91 /* Nonzero if we are building the initializer for the primary
92 vtable. */
93 int primary_vtbl_p;
94 /* Nonzero if we are building the initializer for a construction
95 vtable. */
96 int ctor_vtbl_p;
97 /* True when adding vcall offset entries to the vtable. False when
98 merely computing the indices. */
99 bool generate_vcall_entries;
100 } vtbl_init_data;
102 /* The type of a function passed to walk_subobject_offsets. */
103 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105 /* The stack itself. This is a dynamically resized array. The
106 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
107 static int current_class_stack_size;
108 static class_stack_node_t current_class_stack;
110 /* The size of the largest empty class seen in this translation unit. */
111 static GTY (()) tree sizeof_biggest_empty_class;
113 /* An array of all local classes present in this translation unit, in
114 declaration order. */
115 VEC(tree,gc) *local_classes;
117 static tree get_vfield_name (tree);
118 static void finish_struct_anon (tree);
119 static tree get_vtable_name (tree);
120 static tree get_basefndecls (tree, tree);
121 static int build_primary_vtable (tree, tree);
122 static int build_secondary_vtable (tree);
123 static void finish_vtbls (tree);
124 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
125 static void finish_struct_bits (tree);
126 static int alter_access (tree, tree, tree);
127 static void handle_using_decl (tree, tree);
128 static tree dfs_modify_vtables (tree, void *);
129 static tree modify_all_vtables (tree, tree);
130 static void determine_primary_bases (tree);
131 static void finish_struct_methods (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static int method_name_cmp (const void *, const void *);
134 static int resort_method_name_cmp (const void *, const void *);
135 static void add_implicitly_declared_members (tree, int, int);
136 static tree fixed_type_or_null (tree, int *, int *);
137 static tree build_simple_base_path (tree expr, tree binfo);
138 static tree build_vtbl_ref_1 (tree, tree);
139 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
140 static int count_fields (tree);
141 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
142 static void check_bitfield_decl (tree);
143 static void check_field_decl (tree, tree, int *, int *, int *);
144 static void check_field_decls (tree, tree *, int *, int *);
145 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
146 static void build_base_fields (record_layout_info, splay_tree, tree *);
147 static void check_methods (tree);
148 static void remove_zero_width_bit_fields (tree);
149 static void check_bases (tree, int *, int *);
150 static void check_bases_and_members (tree);
151 static tree create_vtable_ptr (tree, tree *);
152 static void include_empty_classes (record_layout_info);
153 static void layout_class_type (tree, tree *);
154 static void fixup_pending_inline (tree);
155 static void fixup_inline_methods (tree);
156 static void propagate_binfo_offsets (tree, tree);
157 static void layout_virtual_bases (record_layout_info, splay_tree);
158 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
160 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
161 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset (tree, tree, vtbl_init_data *);
163 static void layout_vtable_decl (tree, int);
164 static tree dfs_find_final_overrider_pre (tree, void *);
165 static tree dfs_find_final_overrider_post (tree, void *);
166 static tree find_final_overrider (tree, tree, tree);
167 static int make_new_vtable (tree, tree);
168 static tree get_primary_binfo (tree);
169 static int maybe_indent_hierarchy (FILE *, int, int);
170 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
171 static void dump_class_hierarchy (tree);
172 static void dump_class_hierarchy_1 (FILE *, int, tree);
173 static void dump_array (FILE *, tree);
174 static void dump_vtable (tree, tree, tree);
175 static void dump_vtt (tree, tree);
176 static void dump_thunk (FILE *, int, tree);
177 static tree build_vtable (tree, tree, tree);
178 static void initialize_vtable (tree, tree);
179 static void layout_nonempty_base_or_field (record_layout_info,
180 tree, tree, splay_tree);
181 static tree end_of_class (tree, int);
182 static bool layout_empty_base (tree, tree, splay_tree);
183 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
184 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
185 tree);
186 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
187 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
188 static void clone_constructors_and_destructors (tree);
189 static tree build_clone (tree, tree);
190 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
191 static void build_ctor_vtbl_group (tree, tree);
192 static void build_vtt (tree);
193 static tree binfo_ctor_vtable (tree);
194 static tree *build_vtt_inits (tree, tree, tree *, tree *);
195 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
196 static tree dfs_fixup_binfo_vtbls (tree, void *);
197 static int record_subobject_offset (tree, tree, splay_tree);
198 static int check_subobject_offset (tree, tree, splay_tree);
199 static int walk_subobject_offsets (tree, subobject_offset_fn,
200 tree, splay_tree, tree, int);
201 static void record_subobject_offsets (tree, tree, splay_tree, bool);
202 static int layout_conflict_p (tree, tree, splay_tree, int);
203 static int splay_tree_compare_integer_csts (splay_tree_key k1,
204 splay_tree_key k2);
205 static void warn_about_ambiguous_bases (tree);
206 static bool type_requires_array_cookie (tree);
207 static bool contains_empty_class_p (tree);
208 static bool base_derived_from (tree, tree);
209 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
210 static tree end_of_base (tree);
211 static tree get_vcall_index (tree, tree);
213 /* Variables shared between class.c and call.c. */
215 #ifdef GATHER_STATISTICS
216 int n_vtables = 0;
217 int n_vtable_entries = 0;
218 int n_vtable_searches = 0;
219 int n_vtable_elems = 0;
220 int n_convert_harshness = 0;
221 int n_compute_conversion_costs = 0;
222 int n_inner_fields_searched = 0;
223 #endif
225 /* Convert to or from a base subobject. EXPR is an expression of type
226 `A' or `A*', an expression of type `B' or `B*' is returned. To
227 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
228 the B base instance within A. To convert base A to derived B, CODE
229 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
230 In this latter case, A must not be a morally virtual base of B.
231 NONNULL is true if EXPR is known to be non-NULL (this is only
232 needed when EXPR is of pointer type). CV qualifiers are preserved
233 from EXPR. */
235 tree
236 build_base_path (enum tree_code code,
237 tree expr,
238 tree binfo,
239 int nonnull)
241 tree v_binfo = NULL_TREE;
242 tree d_binfo = NULL_TREE;
243 tree probe;
244 tree offset;
245 tree target_type;
246 tree null_test = NULL;
247 tree ptr_target_type;
248 int fixed_type_p;
249 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
250 bool has_empty = false;
251 bool virtual_access;
253 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
254 return error_mark_node;
256 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
258 d_binfo = probe;
259 if (is_empty_class (BINFO_TYPE (probe)))
260 has_empty = true;
261 if (!v_binfo && BINFO_VIRTUAL_P (probe))
262 v_binfo = probe;
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
266 if (want_pointer)
267 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
269 gcc_assert ((code == MINUS_EXPR
270 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
271 || (code == PLUS_EXPR
272 && SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe)));
274 if (binfo == d_binfo)
275 /* Nothing to do. */
276 return expr;
278 if (code == MINUS_EXPR && v_binfo)
280 error ("cannot convert from base %qT to derived type %qT via virtual base %qT",
281 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
282 return error_mark_node;
285 if (!want_pointer)
286 /* This must happen before the call to save_expr. */
287 expr = build_unary_op (ADDR_EXPR, expr, 0);
289 offset = BINFO_OFFSET (binfo);
290 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
291 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
293 /* Do we need to look in the vtable for the real offset? */
294 virtual_access = (v_binfo && fixed_type_p <= 0);
296 /* Do we need to check for a null pointer? */
297 if (want_pointer && !nonnull)
299 /* If we know the conversion will not actually change the value
300 of EXPR, then we can avoid testing the expression for NULL.
301 We have to avoid generating a COMPONENT_REF for a base class
302 field, because other parts of the compiler know that such
303 expressions are always non-NULL. */
304 if (!virtual_access && integer_zerop (offset))
305 return build_nop (build_pointer_type (target_type), expr);
306 null_test = error_mark_node;
309 /* Protect against multiple evaluation if necessary. */
310 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
311 expr = save_expr (expr);
313 /* Now that we've saved expr, build the real null test. */
314 if (null_test)
316 tree zero = cp_convert (TREE_TYPE (expr), integer_zero_node);
317 null_test = fold_build2 (NE_EXPR, boolean_type_node,
318 expr, zero);
321 /* If this is a simple base reference, express it as a COMPONENT_REF. */
322 if (code == PLUS_EXPR && !virtual_access
323 /* We don't build base fields for empty bases, and they aren't very
324 interesting to the optimizers anyway. */
325 && !has_empty)
327 expr = build_indirect_ref (expr, NULL);
328 expr = build_simple_base_path (expr, binfo);
329 if (want_pointer)
330 expr = build_address (expr);
331 target_type = TREE_TYPE (expr);
332 goto out;
335 if (virtual_access)
337 /* Going via virtual base V_BINFO. We need the static offset
338 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
339 V_BINFO. That offset is an entry in D_BINFO's vtable. */
340 tree v_offset;
342 if (fixed_type_p < 0 && in_base_initializer)
344 /* In a base member initializer, we cannot rely on the
345 vtable being set up. We have to indirect via the
346 vtt_parm. */
347 tree t;
349 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
350 t = build_pointer_type (t);
351 v_offset = convert (t, current_vtt_parm);
352 v_offset = build_indirect_ref (v_offset, NULL);
354 else
355 v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
356 TREE_TYPE (TREE_TYPE (expr)));
358 v_offset = build2 (PLUS_EXPR, TREE_TYPE (v_offset),
359 v_offset, BINFO_VPTR_FIELD (v_binfo));
360 v_offset = build1 (NOP_EXPR,
361 build_pointer_type (ptrdiff_type_node),
362 v_offset);
363 v_offset = build_indirect_ref (v_offset, NULL);
364 TREE_CONSTANT (v_offset) = 1;
365 TREE_INVARIANT (v_offset) = 1;
367 offset = convert_to_integer (ptrdiff_type_node,
368 size_diffop (offset,
369 BINFO_OFFSET (v_binfo)));
371 if (!integer_zerop (offset))
372 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
374 if (fixed_type_p < 0)
375 /* Negative fixed_type_p means this is a constructor or destructor;
376 virtual base layout is fixed in in-charge [cd]tors, but not in
377 base [cd]tors. */
378 offset = build3 (COND_EXPR, ptrdiff_type_node,
379 build2 (EQ_EXPR, boolean_type_node,
380 current_in_charge_parm, integer_zero_node),
381 v_offset,
382 convert_to_integer (ptrdiff_type_node,
383 BINFO_OFFSET (binfo)));
384 else
385 offset = v_offset;
388 target_type = cp_build_qualified_type
389 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
390 ptr_target_type = build_pointer_type (target_type);
391 if (want_pointer)
392 target_type = ptr_target_type;
394 expr = build1 (NOP_EXPR, ptr_target_type, expr);
396 if (!integer_zerop (offset))
397 expr = build2 (code, ptr_target_type, expr, offset);
398 else
399 null_test = NULL;
401 if (!want_pointer)
402 expr = build_indirect_ref (expr, NULL);
404 out:
405 if (null_test)
406 expr = fold_build3 (COND_EXPR, target_type, null_test, expr,
407 fold_build1 (NOP_EXPR, target_type,
408 integer_zero_node));
410 return expr;
413 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
414 Perform a derived-to-base conversion by recursively building up a
415 sequence of COMPONENT_REFs to the appropriate base fields. */
417 static tree
418 build_simple_base_path (tree expr, tree binfo)
420 tree type = BINFO_TYPE (binfo);
421 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
422 tree field;
424 if (d_binfo == NULL_TREE)
426 tree temp;
428 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
430 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
431 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
432 an lvalue in the frontend; only _DECLs and _REFs are lvalues
433 in the backend. */
434 temp = unary_complex_lvalue (ADDR_EXPR, expr);
435 if (temp)
436 expr = build_indirect_ref (temp, NULL);
438 return expr;
441 /* Recurse. */
442 expr = build_simple_base_path (expr, d_binfo);
444 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
445 field; field = TREE_CHAIN (field))
446 /* Is this the base field created by build_base_field? */
447 if (TREE_CODE (field) == FIELD_DECL
448 && DECL_FIELD_IS_BASE (field)
449 && TREE_TYPE (field) == type)
451 /* We don't use build_class_member_access_expr here, as that
452 has unnecessary checks, and more importantly results in
453 recursive calls to dfs_walk_once. */
454 int type_quals = cp_type_quals (TREE_TYPE (expr));
456 expr = build3 (COMPONENT_REF,
457 cp_build_qualified_type (type, type_quals),
458 expr, field, NULL_TREE);
459 expr = fold_if_not_in_template (expr);
461 /* Mark the expression const or volatile, as appropriate.
462 Even though we've dealt with the type above, we still have
463 to mark the expression itself. */
464 if (type_quals & TYPE_QUAL_CONST)
465 TREE_READONLY (expr) = 1;
466 if (type_quals & TYPE_QUAL_VOLATILE)
467 TREE_THIS_VOLATILE (expr) = 1;
469 return expr;
472 /* Didn't find the base field?!? */
473 gcc_unreachable ();
476 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
477 type is a class type or a pointer to a class type. In the former
478 case, TYPE is also a class type; in the latter it is another
479 pointer type. If CHECK_ACCESS is true, an error message is emitted
480 if TYPE is inaccessible. If OBJECT has pointer type, the value is
481 assumed to be non-NULL. */
483 tree
484 convert_to_base (tree object, tree type, bool check_access, bool nonnull)
486 tree binfo;
487 tree object_type;
489 if (TYPE_PTR_P (TREE_TYPE (object)))
491 object_type = TREE_TYPE (TREE_TYPE (object));
492 type = TREE_TYPE (type);
494 else
495 object_type = TREE_TYPE (object);
497 binfo = lookup_base (object_type, type,
498 check_access ? ba_check : ba_unique,
499 NULL);
500 if (!binfo || binfo == error_mark_node)
501 return error_mark_node;
503 return build_base_path (PLUS_EXPR, object, binfo, nonnull);
506 /* EXPR is an expression with unqualified class type. BASE is a base
507 binfo of that class type. Returns EXPR, converted to the BASE
508 type. This function assumes that EXPR is the most derived class;
509 therefore virtual bases can be found at their static offsets. */
511 tree
512 convert_to_base_statically (tree expr, tree base)
514 tree expr_type;
516 expr_type = TREE_TYPE (expr);
517 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
519 tree pointer_type;
521 pointer_type = build_pointer_type (expr_type);
522 expr = build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1);
523 if (!integer_zerop (BINFO_OFFSET (base)))
524 expr = build2 (PLUS_EXPR, pointer_type, expr,
525 build_nop (pointer_type, BINFO_OFFSET (base)));
526 expr = build_nop (build_pointer_type (BINFO_TYPE (base)), expr);
527 expr = build1 (INDIRECT_REF, BINFO_TYPE (base), expr);
530 return expr;
534 tree
535 build_vfield_ref (tree datum, tree type)
537 tree vfield, vcontext;
539 if (datum == error_mark_node)
540 return error_mark_node;
542 /* First, convert to the requested type. */
543 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
544 datum = convert_to_base (datum, type, /*check_access=*/false,
545 /*nonnull=*/true);
547 /* Second, the requested type may not be the owner of its own vptr.
548 If not, convert to the base class that owns it. We cannot use
549 convert_to_base here, because VCONTEXT may appear more than once
550 in the inheritance hierarchy of TYPE, and thus direct conversion
551 between the types may be ambiguous. Following the path back up
552 one step at a time via primary bases avoids the problem. */
553 vfield = TYPE_VFIELD (type);
554 vcontext = DECL_CONTEXT (vfield);
555 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
557 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
558 type = TREE_TYPE (datum);
561 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
564 /* Given an object INSTANCE, return an expression which yields the
565 vtable element corresponding to INDEX. There are many special
566 cases for INSTANCE which we take care of here, mainly to avoid
567 creating extra tree nodes when we don't have to. */
569 static tree
570 build_vtbl_ref_1 (tree instance, tree idx)
572 tree aref;
573 tree vtbl = NULL_TREE;
575 /* Try to figure out what a reference refers to, and
576 access its virtual function table directly. */
578 int cdtorp = 0;
579 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
581 tree basetype = non_reference (TREE_TYPE (instance));
583 if (fixed_type && !cdtorp)
585 tree binfo = lookup_base (fixed_type, basetype,
586 ba_unique | ba_quiet, NULL);
587 if (binfo)
588 vtbl = unshare_expr (BINFO_VTABLE (binfo));
591 if (!vtbl)
592 vtbl = build_vfield_ref (instance, basetype);
594 assemble_external (vtbl);
596 aref = build_array_ref (vtbl, idx);
597 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
598 TREE_INVARIANT (aref) = TREE_CONSTANT (aref);
600 return aref;
603 tree
604 build_vtbl_ref (tree instance, tree idx)
606 tree aref = build_vtbl_ref_1 (instance, idx);
608 return aref;
611 /* Given a stable object pointer INSTANCE_PTR, return an expression which
612 yields a function pointer corresponding to vtable element INDEX. */
614 tree
615 build_vfn_ref (tree instance_ptr, tree idx)
617 tree aref;
619 aref = build_vtbl_ref_1 (build_indirect_ref (instance_ptr, 0), idx);
621 /* When using function descriptors, the address of the
622 vtable entry is treated as a function pointer. */
623 if (TARGET_VTABLE_USES_DESCRIPTORS)
624 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
625 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
627 /* Remember this as a method reference, for later devirtualization. */
628 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
630 return aref;
633 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
634 for the given TYPE. */
636 static tree
637 get_vtable_name (tree type)
639 return mangle_vtbl_for_type (type);
642 /* DECL is an entity associated with TYPE, like a virtual table or an
643 implicitly generated constructor. Determine whether or not DECL
644 should have external or internal linkage at the object file
645 level. This routine does not deal with COMDAT linkage and other
646 similar complexities; it simply sets TREE_PUBLIC if it possible for
647 entities in other translation units to contain copies of DECL, in
648 the abstract. */
650 void
651 set_linkage_according_to_type (tree type, tree decl)
653 /* If TYPE involves a local class in a function with internal
654 linkage, then DECL should have internal linkage too. Other local
655 classes have no linkage -- but if their containing functions
656 have external linkage, it makes sense for DECL to have external
657 linkage too. That will allow template definitions to be merged,
658 for example. */
659 if (no_linkage_check (type, /*relaxed_p=*/true))
661 TREE_PUBLIC (decl) = 0;
662 DECL_INTERFACE_KNOWN (decl) = 1;
664 else
665 TREE_PUBLIC (decl) = 1;
668 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
669 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
670 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
672 static tree
673 build_vtable (tree class_type, tree name, tree vtable_type)
675 tree decl;
677 decl = build_lang_decl (VAR_DECL, name, vtable_type);
678 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
679 now to avoid confusion in mangle_decl. */
680 SET_DECL_ASSEMBLER_NAME (decl, name);
681 DECL_CONTEXT (decl) = class_type;
682 DECL_ARTIFICIAL (decl) = 1;
683 TREE_STATIC (decl) = 1;
684 TREE_READONLY (decl) = 1;
685 DECL_VIRTUAL_P (decl) = 1;
686 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
687 DECL_VTABLE_OR_VTT_P (decl) = 1;
688 /* At one time the vtable info was grabbed 2 words at a time. This
689 fails on sparc unless you have 8-byte alignment. (tiemann) */
690 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
691 DECL_ALIGN (decl));
692 set_linkage_according_to_type (class_type, decl);
693 /* The vtable has not been defined -- yet. */
694 DECL_EXTERNAL (decl) = 1;
695 DECL_NOT_REALLY_EXTERN (decl) = 1;
697 /* Mark the VAR_DECL node representing the vtable itself as a
698 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
699 is rather important that such things be ignored because any
700 effort to actually generate DWARF for them will run into
701 trouble when/if we encounter code like:
703 #pragma interface
704 struct S { virtual void member (); };
706 because the artificial declaration of the vtable itself (as
707 manufactured by the g++ front end) will say that the vtable is
708 a static member of `S' but only *after* the debug output for
709 the definition of `S' has already been output. This causes
710 grief because the DWARF entry for the definition of the vtable
711 will try to refer back to an earlier *declaration* of the
712 vtable as a static member of `S' and there won't be one. We
713 might be able to arrange to have the "vtable static member"
714 attached to the member list for `S' before the debug info for
715 `S' get written (which would solve the problem) but that would
716 require more intrusive changes to the g++ front end. */
717 DECL_IGNORED_P (decl) = 1;
719 return decl;
722 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
723 or even complete. If this does not exist, create it. If COMPLETE is
724 nonzero, then complete the definition of it -- that will render it
725 impossible to actually build the vtable, but is useful to get at those
726 which are known to exist in the runtime. */
728 tree
729 get_vtable_decl (tree type, int complete)
731 tree decl;
733 if (CLASSTYPE_VTABLES (type))
734 return CLASSTYPE_VTABLES (type);
736 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
737 CLASSTYPE_VTABLES (type) = decl;
739 if (complete)
741 DECL_EXTERNAL (decl) = 1;
742 finish_decl (decl, NULL_TREE, NULL_TREE);
745 return decl;
748 /* Build the primary virtual function table for TYPE. If BINFO is
749 non-NULL, build the vtable starting with the initial approximation
750 that it is the same as the one which is the head of the association
751 list. Returns a nonzero value if a new vtable is actually
752 created. */
754 static int
755 build_primary_vtable (tree binfo, tree type)
757 tree decl;
758 tree virtuals;
760 decl = get_vtable_decl (type, /*complete=*/0);
762 if (binfo)
764 if (BINFO_NEW_VTABLE_MARKED (binfo))
765 /* We have already created a vtable for this base, so there's
766 no need to do it again. */
767 return 0;
769 virtuals = copy_list (BINFO_VIRTUALS (binfo));
770 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
771 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
772 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
774 else
776 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
777 virtuals = NULL_TREE;
780 #ifdef GATHER_STATISTICS
781 n_vtables += 1;
782 n_vtable_elems += list_length (virtuals);
783 #endif
785 /* Initialize the association list for this type, based
786 on our first approximation. */
787 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
788 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
789 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
790 return 1;
793 /* Give BINFO a new virtual function table which is initialized
794 with a skeleton-copy of its original initialization. The only
795 entry that changes is the `delta' entry, so we can really
796 share a lot of structure.
798 FOR_TYPE is the most derived type which caused this table to
799 be needed.
801 Returns nonzero if we haven't met BINFO before.
803 The order in which vtables are built (by calling this function) for
804 an object must remain the same, otherwise a binary incompatibility
805 can result. */
807 static int
808 build_secondary_vtable (tree binfo)
810 if (BINFO_NEW_VTABLE_MARKED (binfo))
811 /* We already created a vtable for this base. There's no need to
812 do it again. */
813 return 0;
815 /* Remember that we've created a vtable for this BINFO, so that we
816 don't try to do so again. */
817 SET_BINFO_NEW_VTABLE_MARKED (binfo);
819 /* Make fresh virtual list, so we can smash it later. */
820 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
822 /* Secondary vtables are laid out as part of the same structure as
823 the primary vtable. */
824 BINFO_VTABLE (binfo) = NULL_TREE;
825 return 1;
828 /* Create a new vtable for BINFO which is the hierarchy dominated by
829 T. Return nonzero if we actually created a new vtable. */
831 static int
832 make_new_vtable (tree t, tree binfo)
834 if (binfo == TYPE_BINFO (t))
835 /* In this case, it is *type*'s vtable we are modifying. We start
836 with the approximation that its vtable is that of the
837 immediate base class. */
838 return build_primary_vtable (binfo, t);
839 else
840 /* This is our very own copy of `basetype' to play with. Later,
841 we will fill in all the virtual functions that override the
842 virtual functions in these base classes which are not defined
843 by the current type. */
844 return build_secondary_vtable (binfo);
847 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
848 (which is in the hierarchy dominated by T) list FNDECL as its
849 BV_FN. DELTA is the required constant adjustment from the `this'
850 pointer where the vtable entry appears to the `this' required when
851 the function is actually called. */
853 static void
854 modify_vtable_entry (tree t,
855 tree binfo,
856 tree fndecl,
857 tree delta,
858 tree *virtuals)
860 tree v;
862 v = *virtuals;
864 if (fndecl != BV_FN (v)
865 || !tree_int_cst_equal (delta, BV_DELTA (v)))
867 /* We need a new vtable for BINFO. */
868 if (make_new_vtable (t, binfo))
870 /* If we really did make a new vtable, we also made a copy
871 of the BINFO_VIRTUALS list. Now, we have to find the
872 corresponding entry in that list. */
873 *virtuals = BINFO_VIRTUALS (binfo);
874 while (BV_FN (*virtuals) != BV_FN (v))
875 *virtuals = TREE_CHAIN (*virtuals);
876 v = *virtuals;
879 BV_DELTA (v) = delta;
880 BV_VCALL_INDEX (v) = NULL_TREE;
881 BV_FN (v) = fndecl;
886 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
887 the USING_DECL naming METHOD. Returns true if the method could be
888 added to the method vec. */
890 bool
891 add_method (tree type, tree method, tree using_decl)
893 unsigned slot;
894 tree overload;
895 bool template_conv_p = false;
896 bool conv_p;
897 VEC(tree,gc) *method_vec;
898 bool complete_p;
899 bool insert_p = false;
900 tree current_fns;
902 if (method == error_mark_node)
903 return false;
905 complete_p = COMPLETE_TYPE_P (type);
906 conv_p = DECL_CONV_FN_P (method);
907 if (conv_p)
908 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
909 && DECL_TEMPLATE_CONV_FN_P (method));
911 method_vec = CLASSTYPE_METHOD_VEC (type);
912 if (!method_vec)
914 /* Make a new method vector. We start with 8 entries. We must
915 allocate at least two (for constructors and destructors), and
916 we're going to end up with an assignment operator at some
917 point as well. */
918 method_vec = VEC_alloc (tree, gc, 8);
919 /* Create slots for constructors and destructors. */
920 VEC_quick_push (tree, method_vec, NULL_TREE);
921 VEC_quick_push (tree, method_vec, NULL_TREE);
922 CLASSTYPE_METHOD_VEC (type) = method_vec;
925 /* Maintain TYPE_HAS_CONSTRUCTOR, etc. */
926 grok_special_member_properties (method);
928 /* Constructors and destructors go in special slots. */
929 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
930 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
931 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
933 slot = CLASSTYPE_DESTRUCTOR_SLOT;
935 if (TYPE_FOR_JAVA (type))
937 if (!DECL_ARTIFICIAL (method))
938 error ("Java class %qT cannot have a destructor", type);
939 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
940 error ("Java class %qT cannot have an implicit non-trivial "
941 "destructor",
942 type);
945 else
947 tree m;
949 insert_p = true;
950 /* See if we already have an entry with this name. */
951 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
952 VEC_iterate (tree, method_vec, slot, m);
953 ++slot)
955 m = OVL_CURRENT (m);
956 if (template_conv_p)
958 if (TREE_CODE (m) == TEMPLATE_DECL
959 && DECL_TEMPLATE_CONV_FN_P (m))
960 insert_p = false;
961 break;
963 if (conv_p && !DECL_CONV_FN_P (m))
964 break;
965 if (DECL_NAME (m) == DECL_NAME (method))
967 insert_p = false;
968 break;
970 if (complete_p
971 && !DECL_CONV_FN_P (m)
972 && DECL_NAME (m) > DECL_NAME (method))
973 break;
976 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
978 if (processing_template_decl)
979 /* TYPE is a template class. Don't issue any errors now; wait
980 until instantiation time to complain. */
982 else
984 tree fns;
986 /* Check to see if we've already got this method. */
987 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
989 tree fn = OVL_CURRENT (fns);
990 tree fn_type;
991 tree method_type;
992 tree parms1;
993 tree parms2;
995 if (TREE_CODE (fn) != TREE_CODE (method))
996 continue;
998 /* [over.load] Member function declarations with the
999 same name and the same parameter types cannot be
1000 overloaded if any of them is a static member
1001 function declaration.
1003 [namespace.udecl] When a using-declaration brings names
1004 from a base class into a derived class scope, member
1005 functions in the derived class override and/or hide member
1006 functions with the same name and parameter types in a base
1007 class (rather than conflicting). */
1008 fn_type = TREE_TYPE (fn);
1009 method_type = TREE_TYPE (method);
1010 parms1 = TYPE_ARG_TYPES (fn_type);
1011 parms2 = TYPE_ARG_TYPES (method_type);
1013 /* Compare the quals on the 'this' parm. Don't compare
1014 the whole types, as used functions are treated as
1015 coming from the using class in overload resolution. */
1016 if (! DECL_STATIC_FUNCTION_P (fn)
1017 && ! DECL_STATIC_FUNCTION_P (method)
1018 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
1019 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
1020 continue;
1022 /* For templates, the return type and template parameters
1023 must be identical. */
1024 if (TREE_CODE (fn) == TEMPLATE_DECL
1025 && (!same_type_p (TREE_TYPE (fn_type),
1026 TREE_TYPE (method_type))
1027 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1028 DECL_TEMPLATE_PARMS (method))))
1029 continue;
1031 if (! DECL_STATIC_FUNCTION_P (fn))
1032 parms1 = TREE_CHAIN (parms1);
1033 if (! DECL_STATIC_FUNCTION_P (method))
1034 parms2 = TREE_CHAIN (parms2);
1036 if (compparms (parms1, parms2)
1037 && (!DECL_CONV_FN_P (fn)
1038 || same_type_p (TREE_TYPE (fn_type),
1039 TREE_TYPE (method_type))))
1041 if (using_decl)
1043 if (DECL_CONTEXT (fn) == type)
1044 /* Defer to the local function. */
1045 return false;
1046 if (DECL_CONTEXT (fn) == DECL_CONTEXT (method))
1047 error ("repeated using declaration %q+D", using_decl);
1048 else
1049 error ("using declaration %q+D conflicts with a previous using declaration",
1050 using_decl);
1052 else
1054 error ("%q+#D cannot be overloaded", method);
1055 error ("with %q+#D", fn);
1058 /* We don't call duplicate_decls here to merge the
1059 declarations because that will confuse things if the
1060 methods have inline definitions. In particular, we
1061 will crash while processing the definitions. */
1062 return false;
1067 /* A class should never have more than one destructor. */
1068 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1069 return false;
1071 /* Add the new binding. */
1072 overload = build_overload (method, current_fns);
1074 if (conv_p)
1075 TYPE_HAS_CONVERSION (type) = 1;
1076 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1077 push_class_level_binding (DECL_NAME (method), overload);
1079 if (insert_p)
1081 /* We only expect to add few methods in the COMPLETE_P case, so
1082 just make room for one more method in that case. */
1083 if (VEC_reserve (tree, gc, method_vec, complete_p ? -1 : 1))
1084 CLASSTYPE_METHOD_VEC (type) = method_vec;
1085 if (slot == VEC_length (tree, method_vec))
1086 VEC_quick_push (tree, method_vec, overload);
1087 else
1088 VEC_quick_insert (tree, method_vec, slot, overload);
1090 else
1091 /* Replace the current slot. */
1092 VEC_replace (tree, method_vec, slot, overload);
1093 return true;
1096 /* Subroutines of finish_struct. */
1098 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1099 legit, otherwise return 0. */
1101 static int
1102 alter_access (tree t, tree fdecl, tree access)
1104 tree elem;
1106 if (!DECL_LANG_SPECIFIC (fdecl))
1107 retrofit_lang_decl (fdecl);
1109 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1111 elem = purpose_member (t, DECL_ACCESS (fdecl));
1112 if (elem)
1114 if (TREE_VALUE (elem) != access)
1116 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1117 error ("conflicting access specifications for method"
1118 " %q+D, ignored", TREE_TYPE (fdecl));
1119 else
1120 error ("conflicting access specifications for field %qE, ignored",
1121 DECL_NAME (fdecl));
1123 else
1125 /* They're changing the access to the same thing they changed
1126 it to before. That's OK. */
1130 else
1132 perform_or_defer_access_check (TYPE_BINFO (t), fdecl);
1133 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1134 return 1;
1136 return 0;
1139 /* Process the USING_DECL, which is a member of T. */
1141 static void
1142 handle_using_decl (tree using_decl, tree t)
1144 tree decl = USING_DECL_DECLS (using_decl);
1145 tree name = DECL_NAME (using_decl);
1146 tree access
1147 = TREE_PRIVATE (using_decl) ? access_private_node
1148 : TREE_PROTECTED (using_decl) ? access_protected_node
1149 : access_public_node;
1150 tree flist = NULL_TREE;
1151 tree old_value;
1153 gcc_assert (!processing_template_decl && decl);
1155 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false);
1156 if (old_value)
1158 if (is_overloaded_fn (old_value))
1159 old_value = OVL_CURRENT (old_value);
1161 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1162 /* OK */;
1163 else
1164 old_value = NULL_TREE;
1167 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1169 if (is_overloaded_fn (decl))
1170 flist = decl;
1172 if (! old_value)
1174 else if (is_overloaded_fn (old_value))
1176 if (flist)
1177 /* It's OK to use functions from a base when there are functions with
1178 the same name already present in the current class. */;
1179 else
1181 error ("%q+D invalid in %q#T", using_decl, t);
1182 error (" because of local method %q+#D with same name",
1183 OVL_CURRENT (old_value));
1184 return;
1187 else if (!DECL_ARTIFICIAL (old_value))
1189 error ("%q+D invalid in %q#T", using_decl, t);
1190 error (" because of local member %q+#D with same name", old_value);
1191 return;
1194 /* Make type T see field decl FDECL with access ACCESS. */
1195 if (flist)
1196 for (; flist; flist = OVL_NEXT (flist))
1198 add_method (t, OVL_CURRENT (flist), using_decl);
1199 alter_access (t, OVL_CURRENT (flist), access);
1201 else
1202 alter_access (t, decl, access);
1205 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1206 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1207 properties of the bases. */
1209 static void
1210 check_bases (tree t,
1211 int* cant_have_const_ctor_p,
1212 int* no_const_asn_ref_p)
1214 int i;
1215 int seen_non_virtual_nearly_empty_base_p;
1216 tree base_binfo;
1217 tree binfo;
1219 seen_non_virtual_nearly_empty_base_p = 0;
1221 for (binfo = TYPE_BINFO (t), i = 0;
1222 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1224 tree basetype = TREE_TYPE (base_binfo);
1226 gcc_assert (COMPLETE_TYPE_P (basetype));
1228 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1229 here because the case of virtual functions but non-virtual
1230 dtor is handled in finish_struct_1. */
1231 if (!TYPE_POLYMORPHIC_P (basetype))
1232 warning (OPT_Weffc__,
1233 "base class %q#T has a non-virtual destructor", basetype);
1235 /* If the base class doesn't have copy constructors or
1236 assignment operators that take const references, then the
1237 derived class cannot have such a member automatically
1238 generated. */
1239 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1240 *cant_have_const_ctor_p = 1;
1241 if (TYPE_HAS_ASSIGN_REF (basetype)
1242 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1243 *no_const_asn_ref_p = 1;
1245 if (BINFO_VIRTUAL_P (base_binfo))
1246 /* A virtual base does not effect nearly emptiness. */
1248 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1250 if (seen_non_virtual_nearly_empty_base_p)
1251 /* And if there is more than one nearly empty base, then the
1252 derived class is not nearly empty either. */
1253 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1254 else
1255 /* Remember we've seen one. */
1256 seen_non_virtual_nearly_empty_base_p = 1;
1258 else if (!is_empty_class (basetype))
1259 /* If the base class is not empty or nearly empty, then this
1260 class cannot be nearly empty. */
1261 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1263 /* A lot of properties from the bases also apply to the derived
1264 class. */
1265 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1266 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1267 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1268 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1269 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1270 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1271 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1272 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1273 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1277 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1278 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1279 that have had a nearly-empty virtual primary base stolen by some
1280 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1281 T. */
1283 static void
1284 determine_primary_bases (tree t)
1286 unsigned i;
1287 tree primary = NULL_TREE;
1288 tree type_binfo = TYPE_BINFO (t);
1289 tree base_binfo;
1291 /* Determine the primary bases of our bases. */
1292 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1293 base_binfo = TREE_CHAIN (base_binfo))
1295 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1297 /* See if we're the non-virtual primary of our inheritance
1298 chain. */
1299 if (!BINFO_VIRTUAL_P (base_binfo))
1301 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1302 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1304 if (parent_primary
1305 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1306 BINFO_TYPE (parent_primary)))
1307 /* We are the primary binfo. */
1308 BINFO_PRIMARY_P (base_binfo) = 1;
1310 /* Determine if we have a virtual primary base, and mark it so.
1312 if (primary && BINFO_VIRTUAL_P (primary))
1314 tree this_primary = copied_binfo (primary, base_binfo);
1316 if (BINFO_PRIMARY_P (this_primary))
1317 /* Someone already claimed this base. */
1318 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1319 else
1321 tree delta;
1323 BINFO_PRIMARY_P (this_primary) = 1;
1324 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1326 /* A virtual binfo might have been copied from within
1327 another hierarchy. As we're about to use it as a
1328 primary base, make sure the offsets match. */
1329 delta = size_diffop (convert (ssizetype,
1330 BINFO_OFFSET (base_binfo)),
1331 convert (ssizetype,
1332 BINFO_OFFSET (this_primary)));
1334 propagate_binfo_offsets (this_primary, delta);
1339 /* First look for a dynamic direct non-virtual base. */
1340 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1342 tree basetype = BINFO_TYPE (base_binfo);
1344 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1346 primary = base_binfo;
1347 goto found;
1351 /* A "nearly-empty" virtual base class can be the primary base
1352 class, if no non-virtual polymorphic base can be found. Look for
1353 a nearly-empty virtual dynamic base that is not already a primary
1354 base of something in the hierarchy. If there is no such base,
1355 just pick the first nearly-empty virtual base. */
1357 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1358 base_binfo = TREE_CHAIN (base_binfo))
1359 if (BINFO_VIRTUAL_P (base_binfo)
1360 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1362 if (!BINFO_PRIMARY_P (base_binfo))
1364 /* Found one that is not primary. */
1365 primary = base_binfo;
1366 goto found;
1368 else if (!primary)
1369 /* Remember the first candidate. */
1370 primary = base_binfo;
1373 found:
1374 /* If we've got a primary base, use it. */
1375 if (primary)
1377 tree basetype = BINFO_TYPE (primary);
1379 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1380 if (BINFO_PRIMARY_P (primary))
1381 /* We are stealing a primary base. */
1382 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1383 BINFO_PRIMARY_P (primary) = 1;
1384 if (BINFO_VIRTUAL_P (primary))
1386 tree delta;
1388 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1389 /* A virtual binfo might have been copied from within
1390 another hierarchy. As we're about to use it as a primary
1391 base, make sure the offsets match. */
1392 delta = size_diffop (ssize_int (0),
1393 convert (ssizetype, BINFO_OFFSET (primary)));
1395 propagate_binfo_offsets (primary, delta);
1398 primary = TYPE_BINFO (basetype);
1400 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1401 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1402 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1406 /* Set memoizing fields and bits of T (and its variants) for later
1407 use. */
1409 static void
1410 finish_struct_bits (tree t)
1412 tree variants;
1414 /* Fix up variants (if any). */
1415 for (variants = TYPE_NEXT_VARIANT (t);
1416 variants;
1417 variants = TYPE_NEXT_VARIANT (variants))
1419 /* These fields are in the _TYPE part of the node, not in
1420 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1421 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1422 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1423 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1424 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1426 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1428 TYPE_BINFO (variants) = TYPE_BINFO (t);
1430 /* Copy whatever these are holding today. */
1431 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1432 TYPE_METHODS (variants) = TYPE_METHODS (t);
1433 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1436 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1437 /* For a class w/o baseclasses, 'finish_struct' has set
1438 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1439 Similarly for a class whose base classes do not have vtables.
1440 When neither of these is true, we might have removed abstract
1441 virtuals (by providing a definition), added some (by declaring
1442 new ones), or redeclared ones from a base class. We need to
1443 recalculate what's really an abstract virtual at this point (by
1444 looking in the vtables). */
1445 get_pure_virtuals (t);
1447 /* If this type has a copy constructor or a destructor, force its
1448 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1449 nonzero. This will cause it to be passed by invisible reference
1450 and prevent it from being returned in a register. */
1451 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1453 tree variants;
1454 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1455 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1457 TYPE_MODE (variants) = BLKmode;
1458 TREE_ADDRESSABLE (variants) = 1;
1463 /* Issue warnings about T having private constructors, but no friends,
1464 and so forth.
1466 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1467 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1468 non-private static member functions. */
1470 static void
1471 maybe_warn_about_overly_private_class (tree t)
1473 int has_member_fn = 0;
1474 int has_nonprivate_method = 0;
1475 tree fn;
1477 if (!warn_ctor_dtor_privacy
1478 /* If the class has friends, those entities might create and
1479 access instances, so we should not warn. */
1480 || (CLASSTYPE_FRIEND_CLASSES (t)
1481 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1482 /* We will have warned when the template was declared; there's
1483 no need to warn on every instantiation. */
1484 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1485 /* There's no reason to even consider warning about this
1486 class. */
1487 return;
1489 /* We only issue one warning, if more than one applies, because
1490 otherwise, on code like:
1492 class A {
1493 // Oops - forgot `public:'
1494 A();
1495 A(const A&);
1496 ~A();
1499 we warn several times about essentially the same problem. */
1501 /* Check to see if all (non-constructor, non-destructor) member
1502 functions are private. (Since there are no friends or
1503 non-private statics, we can't ever call any of the private member
1504 functions.) */
1505 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1506 /* We're not interested in compiler-generated methods; they don't
1507 provide any way to call private members. */
1508 if (!DECL_ARTIFICIAL (fn))
1510 if (!TREE_PRIVATE (fn))
1512 if (DECL_STATIC_FUNCTION_P (fn))
1513 /* A non-private static member function is just like a
1514 friend; it can create and invoke private member
1515 functions, and be accessed without a class
1516 instance. */
1517 return;
1519 has_nonprivate_method = 1;
1520 /* Keep searching for a static member function. */
1522 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1523 has_member_fn = 1;
1526 if (!has_nonprivate_method && has_member_fn)
1528 /* There are no non-private methods, and there's at least one
1529 private member function that isn't a constructor or
1530 destructor. (If all the private members are
1531 constructors/destructors we want to use the code below that
1532 issues error messages specifically referring to
1533 constructors/destructors.) */
1534 unsigned i;
1535 tree binfo = TYPE_BINFO (t);
1537 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1538 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1540 has_nonprivate_method = 1;
1541 break;
1543 if (!has_nonprivate_method)
1545 warning (OPT_Wctor_dtor_privacy,
1546 "all member functions in class %qT are private", t);
1547 return;
1551 /* Even if some of the member functions are non-private, the class
1552 won't be useful for much if all the constructors or destructors
1553 are private: such an object can never be created or destroyed. */
1554 fn = CLASSTYPE_DESTRUCTORS (t);
1555 if (fn && TREE_PRIVATE (fn))
1557 warning (OPT_Wctor_dtor_privacy,
1558 "%q#T only defines a private destructor and has no friends",
1560 return;
1563 if (TYPE_HAS_CONSTRUCTOR (t)
1564 /* Implicitly generated constructors are always public. */
1565 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1566 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1568 int nonprivate_ctor = 0;
1570 /* If a non-template class does not define a copy
1571 constructor, one is defined for it, enabling it to avoid
1572 this warning. For a template class, this does not
1573 happen, and so we would normally get a warning on:
1575 template <class T> class C { private: C(); };
1577 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1578 complete non-template or fully instantiated classes have this
1579 flag set. */
1580 if (!TYPE_HAS_INIT_REF (t))
1581 nonprivate_ctor = 1;
1582 else
1583 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1585 tree ctor = OVL_CURRENT (fn);
1586 /* Ideally, we wouldn't count copy constructors (or, in
1587 fact, any constructor that takes an argument of the
1588 class type as a parameter) because such things cannot
1589 be used to construct an instance of the class unless
1590 you already have one. But, for now at least, we're
1591 more generous. */
1592 if (! TREE_PRIVATE (ctor))
1594 nonprivate_ctor = 1;
1595 break;
1599 if (nonprivate_ctor == 0)
1601 warning (OPT_Wctor_dtor_privacy,
1602 "%q#T only defines private constructors and has no friends",
1604 return;
1609 static struct {
1610 gt_pointer_operator new_value;
1611 void *cookie;
1612 } resort_data;
1614 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1616 static int
1617 method_name_cmp (const void* m1_p, const void* m2_p)
1619 const tree *const m1 = (const tree *) m1_p;
1620 const tree *const m2 = (const tree *) m2_p;
1622 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1623 return 0;
1624 if (*m1 == NULL_TREE)
1625 return -1;
1626 if (*m2 == NULL_TREE)
1627 return 1;
1628 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1629 return -1;
1630 return 1;
1633 /* This routine compares two fields like method_name_cmp but using the
1634 pointer operator in resort_field_decl_data. */
1636 static int
1637 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1639 const tree *const m1 = (const tree *) m1_p;
1640 const tree *const m2 = (const tree *) m2_p;
1641 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1642 return 0;
1643 if (*m1 == NULL_TREE)
1644 return -1;
1645 if (*m2 == NULL_TREE)
1646 return 1;
1648 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1649 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1650 resort_data.new_value (&d1, resort_data.cookie);
1651 resort_data.new_value (&d2, resort_data.cookie);
1652 if (d1 < d2)
1653 return -1;
1655 return 1;
1658 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1660 void
1661 resort_type_method_vec (void* obj,
1662 void* orig_obj ATTRIBUTE_UNUSED ,
1663 gt_pointer_operator new_value,
1664 void* cookie)
1666 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1667 int len = VEC_length (tree, method_vec);
1668 size_t slot;
1669 tree fn;
1671 /* The type conversion ops have to live at the front of the vec, so we
1672 can't sort them. */
1673 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1674 VEC_iterate (tree, method_vec, slot, fn);
1675 ++slot)
1676 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1677 break;
1679 if (len - slot > 1)
1681 resort_data.new_value = new_value;
1682 resort_data.cookie = cookie;
1683 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1684 resort_method_name_cmp);
1688 /* Warn about duplicate methods in fn_fields.
1690 Sort methods that are not special (i.e., constructors, destructors,
1691 and type conversion operators) so that we can find them faster in
1692 search. */
1694 static void
1695 finish_struct_methods (tree t)
1697 tree fn_fields;
1698 VEC(tree,gc) *method_vec;
1699 int slot, len;
1701 method_vec = CLASSTYPE_METHOD_VEC (t);
1702 if (!method_vec)
1703 return;
1705 len = VEC_length (tree, method_vec);
1707 /* Clear DECL_IN_AGGR_P for all functions. */
1708 for (fn_fields = TYPE_METHODS (t); fn_fields;
1709 fn_fields = TREE_CHAIN (fn_fields))
1710 DECL_IN_AGGR_P (fn_fields) = 0;
1712 /* Issue warnings about private constructors and such. If there are
1713 no methods, then some public defaults are generated. */
1714 maybe_warn_about_overly_private_class (t);
1716 /* The type conversion ops have to live at the front of the vec, so we
1717 can't sort them. */
1718 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1719 VEC_iterate (tree, method_vec, slot, fn_fields);
1720 ++slot)
1721 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1722 break;
1723 if (len - slot > 1)
1724 qsort (VEC_address (tree, method_vec) + slot,
1725 len-slot, sizeof (tree), method_name_cmp);
1728 /* Make BINFO's vtable have N entries, including RTTI entries,
1729 vbase and vcall offsets, etc. Set its type and call the backend
1730 to lay it out. */
1732 static void
1733 layout_vtable_decl (tree binfo, int n)
1735 tree atype;
1736 tree vtable;
1738 atype = build_cplus_array_type (vtable_entry_type,
1739 build_index_type (size_int (n - 1)));
1740 layout_type (atype);
1742 /* We may have to grow the vtable. */
1743 vtable = get_vtbl_decl_for_binfo (binfo);
1744 if (!same_type_p (TREE_TYPE (vtable), atype))
1746 TREE_TYPE (vtable) = atype;
1747 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1748 layout_decl (vtable, 0);
1752 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1753 have the same signature. */
1756 same_signature_p (tree fndecl, tree base_fndecl)
1758 /* One destructor overrides another if they are the same kind of
1759 destructor. */
1760 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1761 && special_function_p (base_fndecl) == special_function_p (fndecl))
1762 return 1;
1763 /* But a non-destructor never overrides a destructor, nor vice
1764 versa, nor do different kinds of destructors override
1765 one-another. For example, a complete object destructor does not
1766 override a deleting destructor. */
1767 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1768 return 0;
1770 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1771 || (DECL_CONV_FN_P (fndecl)
1772 && DECL_CONV_FN_P (base_fndecl)
1773 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1774 DECL_CONV_FN_TYPE (base_fndecl))))
1776 tree types, base_types;
1777 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1778 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1779 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
1780 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
1781 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1782 return 1;
1784 return 0;
1787 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1788 subobject. */
1790 static bool
1791 base_derived_from (tree derived, tree base)
1793 tree probe;
1795 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1797 if (probe == derived)
1798 return true;
1799 else if (BINFO_VIRTUAL_P (probe))
1800 /* If we meet a virtual base, we can't follow the inheritance
1801 any more. See if the complete type of DERIVED contains
1802 such a virtual base. */
1803 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1804 != NULL_TREE);
1806 return false;
1809 typedef struct find_final_overrider_data_s {
1810 /* The function for which we are trying to find a final overrider. */
1811 tree fn;
1812 /* The base class in which the function was declared. */
1813 tree declaring_base;
1814 /* The candidate overriders. */
1815 tree candidates;
1816 /* Path to most derived. */
1817 VEC(tree,heap) *path;
1818 } find_final_overrider_data;
1820 /* Add the overrider along the current path to FFOD->CANDIDATES.
1821 Returns true if an overrider was found; false otherwise. */
1823 static bool
1824 dfs_find_final_overrider_1 (tree binfo,
1825 find_final_overrider_data *ffod,
1826 unsigned depth)
1828 tree method;
1830 /* If BINFO is not the most derived type, try a more derived class.
1831 A definition there will overrider a definition here. */
1832 if (depth)
1834 depth--;
1835 if (dfs_find_final_overrider_1
1836 (VEC_index (tree, ffod->path, depth), ffod, depth))
1837 return true;
1840 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
1841 if (method)
1843 tree *candidate = &ffod->candidates;
1845 /* Remove any candidates overridden by this new function. */
1846 while (*candidate)
1848 /* If *CANDIDATE overrides METHOD, then METHOD
1849 cannot override anything else on the list. */
1850 if (base_derived_from (TREE_VALUE (*candidate), binfo))
1851 return true;
1852 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
1853 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
1854 *candidate = TREE_CHAIN (*candidate);
1855 else
1856 candidate = &TREE_CHAIN (*candidate);
1859 /* Add the new function. */
1860 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
1861 return true;
1864 return false;
1867 /* Called from find_final_overrider via dfs_walk. */
1869 static tree
1870 dfs_find_final_overrider_pre (tree binfo, void *data)
1872 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1874 if (binfo == ffod->declaring_base)
1875 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
1876 VEC_safe_push (tree, heap, ffod->path, binfo);
1878 return NULL_TREE;
1881 static tree
1882 dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
1884 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1885 VEC_pop (tree, ffod->path);
1887 return NULL_TREE;
1890 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
1891 FN and whose TREE_VALUE is the binfo for the base where the
1892 overriding occurs. BINFO (in the hierarchy dominated by the binfo
1893 DERIVED) is the base object in which FN is declared. */
1895 static tree
1896 find_final_overrider (tree derived, tree binfo, tree fn)
1898 find_final_overrider_data ffod;
1900 /* Getting this right is a little tricky. This is valid:
1902 struct S { virtual void f (); };
1903 struct T { virtual void f (); };
1904 struct U : public S, public T { };
1906 even though calling `f' in `U' is ambiguous. But,
1908 struct R { virtual void f(); };
1909 struct S : virtual public R { virtual void f (); };
1910 struct T : virtual public R { virtual void f (); };
1911 struct U : public S, public T { };
1913 is not -- there's no way to decide whether to put `S::f' or
1914 `T::f' in the vtable for `R'.
1916 The solution is to look at all paths to BINFO. If we find
1917 different overriders along any two, then there is a problem. */
1918 if (DECL_THUNK_P (fn))
1919 fn = THUNK_TARGET (fn);
1921 /* Determine the depth of the hierarchy. */
1922 ffod.fn = fn;
1923 ffod.declaring_base = binfo;
1924 ffod.candidates = NULL_TREE;
1925 ffod.path = VEC_alloc (tree, heap, 30);
1927 dfs_walk_all (derived, dfs_find_final_overrider_pre,
1928 dfs_find_final_overrider_post, &ffod);
1930 VEC_free (tree, heap, ffod.path);
1932 /* If there was no winner, issue an error message. */
1933 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
1934 return error_mark_node;
1936 return ffod.candidates;
1939 /* Return the index of the vcall offset for FN when TYPE is used as a
1940 virtual base. */
1942 static tree
1943 get_vcall_index (tree fn, tree type)
1945 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
1946 tree_pair_p p;
1947 unsigned ix;
1949 for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++)
1950 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
1951 || same_signature_p (fn, p->purpose))
1952 return p->value;
1954 /* There should always be an appropriate index. */
1955 gcc_unreachable ();
1958 /* Update an entry in the vtable for BINFO, which is in the hierarchy
1959 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
1960 corresponding position in the BINFO_VIRTUALS list. */
1962 static void
1963 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
1964 unsigned ix)
1966 tree b;
1967 tree overrider;
1968 tree delta;
1969 tree virtual_base;
1970 tree first_defn;
1971 tree overrider_fn, overrider_target;
1972 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
1973 tree over_return, base_return;
1974 bool lost = false;
1976 /* Find the nearest primary base (possibly binfo itself) which defines
1977 this function; this is the class the caller will convert to when
1978 calling FN through BINFO. */
1979 for (b = binfo; ; b = get_primary_binfo (b))
1981 gcc_assert (b);
1982 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
1983 break;
1985 /* The nearest definition is from a lost primary. */
1986 if (BINFO_LOST_PRIMARY_P (b))
1987 lost = true;
1989 first_defn = b;
1991 /* Find the final overrider. */
1992 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
1993 if (overrider == error_mark_node)
1995 error ("no unique final overrider for %qD in %qT", target_fn, t);
1996 return;
1998 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2000 /* Check for adjusting covariant return types. */
2001 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2002 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2004 if (POINTER_TYPE_P (over_return)
2005 && TREE_CODE (over_return) == TREE_CODE (base_return)
2006 && CLASS_TYPE_P (TREE_TYPE (over_return))
2007 && CLASS_TYPE_P (TREE_TYPE (base_return))
2008 /* If the overrider is invalid, don't even try. */
2009 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2011 /* If FN is a covariant thunk, we must figure out the adjustment
2012 to the final base FN was converting to. As OVERRIDER_TARGET might
2013 also be converting to the return type of FN, we have to
2014 combine the two conversions here. */
2015 tree fixed_offset, virtual_offset;
2017 over_return = TREE_TYPE (over_return);
2018 base_return = TREE_TYPE (base_return);
2020 if (DECL_THUNK_P (fn))
2022 gcc_assert (DECL_RESULT_THUNK_P (fn));
2023 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2024 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2026 else
2027 fixed_offset = virtual_offset = NULL_TREE;
2029 if (virtual_offset)
2030 /* Find the equivalent binfo within the return type of the
2031 overriding function. We will want the vbase offset from
2032 there. */
2033 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2034 over_return);
2035 else if (!same_type_ignoring_top_level_qualifiers_p
2036 (over_return, base_return))
2038 /* There was no existing virtual thunk (which takes
2039 precedence). So find the binfo of the base function's
2040 return type within the overriding function's return type.
2041 We cannot call lookup base here, because we're inside a
2042 dfs_walk, and will therefore clobber the BINFO_MARKED
2043 flags. Fortunately we know the covariancy is valid (it
2044 has already been checked), so we can just iterate along
2045 the binfos, which have been chained in inheritance graph
2046 order. Of course it is lame that we have to repeat the
2047 search here anyway -- we should really be caching pieces
2048 of the vtable and avoiding this repeated work. */
2049 tree thunk_binfo, base_binfo;
2051 /* Find the base binfo within the overriding function's
2052 return type. We will always find a thunk_binfo, except
2053 when the covariancy is invalid (which we will have
2054 already diagnosed). */
2055 for (base_binfo = TYPE_BINFO (base_return),
2056 thunk_binfo = TYPE_BINFO (over_return);
2057 thunk_binfo;
2058 thunk_binfo = TREE_CHAIN (thunk_binfo))
2059 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2060 BINFO_TYPE (base_binfo)))
2061 break;
2063 /* See if virtual inheritance is involved. */
2064 for (virtual_offset = thunk_binfo;
2065 virtual_offset;
2066 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2067 if (BINFO_VIRTUAL_P (virtual_offset))
2068 break;
2070 if (virtual_offset
2071 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2073 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2075 if (virtual_offset)
2077 /* We convert via virtual base. Adjust the fixed
2078 offset to be from there. */
2079 offset = size_diffop
2080 (offset, convert
2081 (ssizetype, BINFO_OFFSET (virtual_offset)));
2083 if (fixed_offset)
2084 /* There was an existing fixed offset, this must be
2085 from the base just converted to, and the base the
2086 FN was thunking to. */
2087 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2088 else
2089 fixed_offset = offset;
2093 if (fixed_offset || virtual_offset)
2094 /* Replace the overriding function with a covariant thunk. We
2095 will emit the overriding function in its own slot as
2096 well. */
2097 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2098 fixed_offset, virtual_offset);
2100 else
2101 gcc_assert (!DECL_THUNK_P (fn));
2103 /* Assume that we will produce a thunk that convert all the way to
2104 the final overrider, and not to an intermediate virtual base. */
2105 virtual_base = NULL_TREE;
2107 /* See if we can convert to an intermediate virtual base first, and then
2108 use the vcall offset located there to finish the conversion. */
2109 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2111 /* If we find the final overrider, then we can stop
2112 walking. */
2113 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2114 BINFO_TYPE (TREE_VALUE (overrider))))
2115 break;
2117 /* If we find a virtual base, and we haven't yet found the
2118 overrider, then there is a virtual base between the
2119 declaring base (first_defn) and the final overrider. */
2120 if (BINFO_VIRTUAL_P (b))
2122 virtual_base = b;
2123 break;
2127 if (overrider_fn != overrider_target && !virtual_base)
2129 /* The ABI specifies that a covariant thunk includes a mangling
2130 for a this pointer adjustment. This-adjusting thunks that
2131 override a function from a virtual base have a vcall
2132 adjustment. When the virtual base in question is a primary
2133 virtual base, we know the adjustments are zero, (and in the
2134 non-covariant case, we would not use the thunk).
2135 Unfortunately we didn't notice this could happen, when
2136 designing the ABI and so never mandated that such a covariant
2137 thunk should be emitted. Because we must use the ABI mandated
2138 name, we must continue searching from the binfo where we
2139 found the most recent definition of the function, towards the
2140 primary binfo which first introduced the function into the
2141 vtable. If that enters a virtual base, we must use a vcall
2142 this-adjusting thunk. Bleah! */
2143 tree probe = first_defn;
2145 while ((probe = get_primary_binfo (probe))
2146 && (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix)
2147 if (BINFO_VIRTUAL_P (probe))
2148 virtual_base = probe;
2150 if (virtual_base)
2151 /* Even if we find a virtual base, the correct delta is
2152 between the overrider and the binfo we're building a vtable
2153 for. */
2154 goto virtual_covariant;
2157 /* Compute the constant adjustment to the `this' pointer. The
2158 `this' pointer, when this function is called, will point at BINFO
2159 (or one of its primary bases, which are at the same offset). */
2160 if (virtual_base)
2161 /* The `this' pointer needs to be adjusted from the declaration to
2162 the nearest virtual base. */
2163 delta = size_diffop (convert (ssizetype, BINFO_OFFSET (virtual_base)),
2164 convert (ssizetype, BINFO_OFFSET (first_defn)));
2165 else if (lost)
2166 /* If the nearest definition is in a lost primary, we don't need an
2167 entry in our vtable. Except possibly in a constructor vtable,
2168 if we happen to get our primary back. In that case, the offset
2169 will be zero, as it will be a primary base. */
2170 delta = size_zero_node;
2171 else
2172 /* The `this' pointer needs to be adjusted from pointing to
2173 BINFO to pointing at the base where the final overrider
2174 appears. */
2175 virtual_covariant:
2176 delta = size_diffop (convert (ssizetype,
2177 BINFO_OFFSET (TREE_VALUE (overrider))),
2178 convert (ssizetype, BINFO_OFFSET (binfo)));
2180 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2182 if (virtual_base)
2183 BV_VCALL_INDEX (*virtuals)
2184 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2185 else
2186 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2189 /* Called from modify_all_vtables via dfs_walk. */
2191 static tree
2192 dfs_modify_vtables (tree binfo, void* data)
2194 tree t = (tree) data;
2195 tree virtuals;
2196 tree old_virtuals;
2197 unsigned ix;
2199 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2200 /* A base without a vtable needs no modification, and its bases
2201 are uninteresting. */
2202 return dfs_skip_bases;
2204 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2205 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2206 /* Don't do the primary vtable, if it's new. */
2207 return NULL_TREE;
2209 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2210 /* There's no need to modify the vtable for a non-virtual primary
2211 base; we're not going to use that vtable anyhow. We do still
2212 need to do this for virtual primary bases, as they could become
2213 non-primary in a construction vtable. */
2214 return NULL_TREE;
2216 make_new_vtable (t, binfo);
2218 /* Now, go through each of the virtual functions in the virtual
2219 function table for BINFO. Find the final overrider, and update
2220 the BINFO_VIRTUALS list appropriately. */
2221 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2222 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2223 virtuals;
2224 ix++, virtuals = TREE_CHAIN (virtuals),
2225 old_virtuals = TREE_CHAIN (old_virtuals))
2226 update_vtable_entry_for_fn (t,
2227 binfo,
2228 BV_FN (old_virtuals),
2229 &virtuals, ix);
2231 return NULL_TREE;
2234 /* Update all of the primary and secondary vtables for T. Create new
2235 vtables as required, and initialize their RTTI information. Each
2236 of the functions in VIRTUALS is declared in T and may override a
2237 virtual function from a base class; find and modify the appropriate
2238 entries to point to the overriding functions. Returns a list, in
2239 declaration order, of the virtual functions that are declared in T,
2240 but do not appear in the primary base class vtable, and which
2241 should therefore be appended to the end of the vtable for T. */
2243 static tree
2244 modify_all_vtables (tree t, tree virtuals)
2246 tree binfo = TYPE_BINFO (t);
2247 tree *fnsp;
2249 /* Update all of the vtables. */
2250 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2252 /* Add virtual functions not already in our primary vtable. These
2253 will be both those introduced by this class, and those overridden
2254 from secondary bases. It does not include virtuals merely
2255 inherited from secondary bases. */
2256 for (fnsp = &virtuals; *fnsp; )
2258 tree fn = TREE_VALUE (*fnsp);
2260 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2261 || DECL_VINDEX (fn) == error_mark_node)
2263 /* We don't need to adjust the `this' pointer when
2264 calling this function. */
2265 BV_DELTA (*fnsp) = integer_zero_node;
2266 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2268 /* This is a function not already in our vtable. Keep it. */
2269 fnsp = &TREE_CHAIN (*fnsp);
2271 else
2272 /* We've already got an entry for this function. Skip it. */
2273 *fnsp = TREE_CHAIN (*fnsp);
2276 return virtuals;
2279 /* Get the base virtual function declarations in T that have the
2280 indicated NAME. */
2282 static tree
2283 get_basefndecls (tree name, tree t)
2285 tree methods;
2286 tree base_fndecls = NULL_TREE;
2287 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2288 int i;
2290 /* Find virtual functions in T with the indicated NAME. */
2291 i = lookup_fnfields_1 (t, name);
2292 if (i != -1)
2293 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2294 methods;
2295 methods = OVL_NEXT (methods))
2297 tree method = OVL_CURRENT (methods);
2299 if (TREE_CODE (method) == FUNCTION_DECL
2300 && DECL_VINDEX (method))
2301 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2304 if (base_fndecls)
2305 return base_fndecls;
2307 for (i = 0; i < n_baseclasses; i++)
2309 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2310 base_fndecls = chainon (get_basefndecls (name, basetype),
2311 base_fndecls);
2314 return base_fndecls;
2317 /* If this declaration supersedes the declaration of
2318 a method declared virtual in the base class, then
2319 mark this field as being virtual as well. */
2321 void
2322 check_for_override (tree decl, tree ctype)
2324 if (TREE_CODE (decl) == TEMPLATE_DECL)
2325 /* In [temp.mem] we have:
2327 A specialization of a member function template does not
2328 override a virtual function from a base class. */
2329 return;
2330 if ((DECL_DESTRUCTOR_P (decl)
2331 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2332 || DECL_CONV_FN_P (decl))
2333 && look_for_overrides (ctype, decl)
2334 && !DECL_STATIC_FUNCTION_P (decl))
2335 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2336 the error_mark_node so that we know it is an overriding
2337 function. */
2338 DECL_VINDEX (decl) = decl;
2340 if (DECL_VIRTUAL_P (decl))
2342 if (!DECL_VINDEX (decl))
2343 DECL_VINDEX (decl) = error_mark_node;
2344 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2348 /* Warn about hidden virtual functions that are not overridden in t.
2349 We know that constructors and destructors don't apply. */
2351 static void
2352 warn_hidden (tree t)
2354 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2355 tree fns;
2356 size_t i;
2358 /* We go through each separately named virtual function. */
2359 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2360 VEC_iterate (tree, method_vec, i, fns);
2361 ++i)
2363 tree fn;
2364 tree name;
2365 tree fndecl;
2366 tree base_fndecls;
2367 tree base_binfo;
2368 tree binfo;
2369 int j;
2371 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2372 have the same name. Figure out what name that is. */
2373 name = DECL_NAME (OVL_CURRENT (fns));
2374 /* There are no possibly hidden functions yet. */
2375 base_fndecls = NULL_TREE;
2376 /* Iterate through all of the base classes looking for possibly
2377 hidden functions. */
2378 for (binfo = TYPE_BINFO (t), j = 0;
2379 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2381 tree basetype = BINFO_TYPE (base_binfo);
2382 base_fndecls = chainon (get_basefndecls (name, basetype),
2383 base_fndecls);
2386 /* If there are no functions to hide, continue. */
2387 if (!base_fndecls)
2388 continue;
2390 /* Remove any overridden functions. */
2391 for (fn = fns; fn; fn = OVL_NEXT (fn))
2393 fndecl = OVL_CURRENT (fn);
2394 if (DECL_VINDEX (fndecl))
2396 tree *prev = &base_fndecls;
2398 while (*prev)
2399 /* If the method from the base class has the same
2400 signature as the method from the derived class, it
2401 has been overridden. */
2402 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2403 *prev = TREE_CHAIN (*prev);
2404 else
2405 prev = &TREE_CHAIN (*prev);
2409 /* Now give a warning for all base functions without overriders,
2410 as they are hidden. */
2411 while (base_fndecls)
2413 /* Here we know it is a hider, and no overrider exists. */
2414 warning (0, "%q+D was hidden", TREE_VALUE (base_fndecls));
2415 warning (0, " by %q+D", fns);
2416 base_fndecls = TREE_CHAIN (base_fndecls);
2421 /* Check for things that are invalid. There are probably plenty of other
2422 things we should check for also. */
2424 static void
2425 finish_struct_anon (tree t)
2427 tree field;
2429 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2431 if (TREE_STATIC (field))
2432 continue;
2433 if (TREE_CODE (field) != FIELD_DECL)
2434 continue;
2436 if (DECL_NAME (field) == NULL_TREE
2437 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2439 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2440 for (; elt; elt = TREE_CHAIN (elt))
2442 /* We're generally only interested in entities the user
2443 declared, but we also find nested classes by noticing
2444 the TYPE_DECL that we create implicitly. You're
2445 allowed to put one anonymous union inside another,
2446 though, so we explicitly tolerate that. We use
2447 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2448 we also allow unnamed types used for defining fields. */
2449 if (DECL_ARTIFICIAL (elt)
2450 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2451 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2452 continue;
2454 if (TREE_CODE (elt) != FIELD_DECL)
2456 pedwarn ("%q+#D invalid; an anonymous union can "
2457 "only have non-static data members", elt);
2458 continue;
2461 if (TREE_PRIVATE (elt))
2462 pedwarn ("private member %q+#D in anonymous union", elt);
2463 else if (TREE_PROTECTED (elt))
2464 pedwarn ("protected member %q+#D in anonymous union", elt);
2466 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2467 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2473 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2474 will be used later during class template instantiation.
2475 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2476 a non-static member data (FIELD_DECL), a member function
2477 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2478 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2479 When FRIEND_P is nonzero, T is either a friend class
2480 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2481 (FUNCTION_DECL, TEMPLATE_DECL). */
2483 void
2484 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2486 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2487 if (CLASSTYPE_TEMPLATE_INFO (type))
2488 CLASSTYPE_DECL_LIST (type)
2489 = tree_cons (friend_p ? NULL_TREE : type,
2490 t, CLASSTYPE_DECL_LIST (type));
2493 /* Create default constructors, assignment operators, and so forth for
2494 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2495 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2496 the class cannot have a default constructor, copy constructor
2497 taking a const reference argument, or an assignment operator taking
2498 a const reference, respectively. */
2500 static void
2501 add_implicitly_declared_members (tree t,
2502 int cant_have_const_cctor,
2503 int cant_have_const_assignment)
2505 /* Destructor. */
2506 if (!CLASSTYPE_DESTRUCTORS (t))
2508 /* In general, we create destructors lazily. */
2509 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2510 /* However, if the implicit destructor is non-trivial
2511 destructor, we sometimes have to create it at this point. */
2512 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2514 bool lazy_p = true;
2516 if (TYPE_FOR_JAVA (t))
2517 /* If this a Java class, any non-trivial destructor is
2518 invalid, even if compiler-generated. Therefore, if the
2519 destructor is non-trivial we create it now. */
2520 lazy_p = false;
2521 else
2523 tree binfo;
2524 tree base_binfo;
2525 int ix;
2527 /* If the implicit destructor will be virtual, then we must
2528 generate it now because (unfortunately) we do not
2529 generate virtual tables lazily. */
2530 binfo = TYPE_BINFO (t);
2531 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2533 tree base_type;
2534 tree dtor;
2536 base_type = BINFO_TYPE (base_binfo);
2537 dtor = CLASSTYPE_DESTRUCTORS (base_type);
2538 if (dtor && DECL_VIRTUAL_P (dtor))
2540 lazy_p = false;
2541 break;
2546 /* If we can't get away with being lazy, generate the destructor
2547 now. */
2548 if (!lazy_p)
2549 lazily_declare_fn (sfk_destructor, t);
2553 /* Default constructor. */
2554 if (! TYPE_HAS_CONSTRUCTOR (t))
2556 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2557 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2560 /* Copy constructor. */
2561 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2563 TYPE_HAS_INIT_REF (t) = 1;
2564 TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor;
2565 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2566 TYPE_HAS_CONSTRUCTOR (t) = 1;
2569 /* If there is no assignment operator, one will be created if and
2570 when it is needed. For now, just record whether or not the type
2571 of the parameter to the assignment operator will be a const or
2572 non-const reference. */
2573 if (!TYPE_HAS_ASSIGN_REF (t) && !TYPE_FOR_JAVA (t))
2575 TYPE_HAS_ASSIGN_REF (t) = 1;
2576 TYPE_HAS_CONST_ASSIGN_REF (t) = !cant_have_const_assignment;
2577 CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 1;
2581 /* Subroutine of finish_struct_1. Recursively count the number of fields
2582 in TYPE, including anonymous union members. */
2584 static int
2585 count_fields (tree fields)
2587 tree x;
2588 int n_fields = 0;
2589 for (x = fields; x; x = TREE_CHAIN (x))
2591 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2592 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2593 else
2594 n_fields += 1;
2596 return n_fields;
2599 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2600 TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */
2602 static int
2603 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2605 tree x;
2606 for (x = fields; x; x = TREE_CHAIN (x))
2608 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2609 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2610 else
2611 field_vec->elts[idx++] = x;
2613 return idx;
2616 /* FIELD is a bit-field. We are finishing the processing for its
2617 enclosing type. Issue any appropriate messages and set appropriate
2618 flags. */
2620 static void
2621 check_bitfield_decl (tree field)
2623 tree type = TREE_TYPE (field);
2624 tree w;
2626 /* Extract the declared width of the bitfield, which has been
2627 temporarily stashed in DECL_INITIAL. */
2628 w = DECL_INITIAL (field);
2629 gcc_assert (w != NULL_TREE);
2630 /* Remove the bit-field width indicator so that the rest of the
2631 compiler does not treat that value as an initializer. */
2632 DECL_INITIAL (field) = NULL_TREE;
2634 /* Detect invalid bit-field type. */
2635 if (!INTEGRAL_TYPE_P (type))
2637 error ("bit-field %q+#D with non-integral type", field);
2638 TREE_TYPE (field) = error_mark_node;
2639 w = error_mark_node;
2641 else
2643 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2644 STRIP_NOPS (w);
2646 /* detect invalid field size. */
2647 w = integral_constant_value (w);
2649 if (TREE_CODE (w) != INTEGER_CST)
2651 error ("bit-field %q+D width not an integer constant", field);
2652 w = error_mark_node;
2654 else if (tree_int_cst_sgn (w) < 0)
2656 error ("negative width in bit-field %q+D", field);
2657 w = error_mark_node;
2659 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2661 error ("zero width for bit-field %q+D", field);
2662 w = error_mark_node;
2664 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2665 && TREE_CODE (type) != ENUMERAL_TYPE
2666 && TREE_CODE (type) != BOOLEAN_TYPE)
2667 warning (0, "width of %q+D exceeds its type", field);
2668 else if (TREE_CODE (type) == ENUMERAL_TYPE
2669 && (0 > compare_tree_int (w,
2670 min_precision (TYPE_MIN_VALUE (type),
2671 TYPE_UNSIGNED (type)))
2672 || 0 > compare_tree_int (w,
2673 min_precision
2674 (TYPE_MAX_VALUE (type),
2675 TYPE_UNSIGNED (type)))))
2676 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2679 if (w != error_mark_node)
2681 DECL_SIZE (field) = convert (bitsizetype, w);
2682 DECL_BIT_FIELD (field) = 1;
2684 else
2686 /* Non-bit-fields are aligned for their type. */
2687 DECL_BIT_FIELD (field) = 0;
2688 CLEAR_DECL_C_BIT_FIELD (field);
2692 /* FIELD is a non bit-field. We are finishing the processing for its
2693 enclosing type T. Issue any appropriate messages and set appropriate
2694 flags. */
2696 static void
2697 check_field_decl (tree field,
2698 tree t,
2699 int* cant_have_const_ctor,
2700 int* no_const_asn_ref,
2701 int* any_default_members)
2703 tree type = strip_array_types (TREE_TYPE (field));
2705 /* An anonymous union cannot contain any fields which would change
2706 the settings of CANT_HAVE_CONST_CTOR and friends. */
2707 if (ANON_UNION_TYPE_P (type))
2709 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
2710 structs. So, we recurse through their fields here. */
2711 else if (ANON_AGGR_TYPE_P (type))
2713 tree fields;
2715 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2716 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2717 check_field_decl (fields, t, cant_have_const_ctor,
2718 no_const_asn_ref, any_default_members);
2720 /* Check members with class type for constructors, destructors,
2721 etc. */
2722 else if (CLASS_TYPE_P (type))
2724 /* Never let anything with uninheritable virtuals
2725 make it through without complaint. */
2726 abstract_virtuals_error (field, type);
2728 if (TREE_CODE (t) == UNION_TYPE)
2730 if (TYPE_NEEDS_CONSTRUCTING (type))
2731 error ("member %q+#D with constructor not allowed in union",
2732 field);
2733 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2734 error ("member %q+#D with destructor not allowed in union", field);
2735 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2736 error ("member %q+#D with copy assignment operator not allowed in union",
2737 field);
2739 else
2741 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2742 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2743 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2744 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
2745 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
2748 if (!TYPE_HAS_CONST_INIT_REF (type))
2749 *cant_have_const_ctor = 1;
2751 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
2752 *no_const_asn_ref = 1;
2754 if (DECL_INITIAL (field) != NULL_TREE)
2756 /* `build_class_init_list' does not recognize
2757 non-FIELD_DECLs. */
2758 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
2759 error ("multiple fields in union %qT initialized", t);
2760 *any_default_members = 1;
2764 /* Check the data members (both static and non-static), class-scoped
2765 typedefs, etc., appearing in the declaration of T. Issue
2766 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
2767 declaration order) of access declarations; each TREE_VALUE in this
2768 list is a USING_DECL.
2770 In addition, set the following flags:
2772 EMPTY_P
2773 The class is empty, i.e., contains no non-static data members.
2775 CANT_HAVE_CONST_CTOR_P
2776 This class cannot have an implicitly generated copy constructor
2777 taking a const reference.
2779 CANT_HAVE_CONST_ASN_REF
2780 This class cannot have an implicitly generated assignment
2781 operator taking a const reference.
2783 All of these flags should be initialized before calling this
2784 function.
2786 Returns a pointer to the end of the TYPE_FIELDs chain; additional
2787 fields can be added by adding to this chain. */
2789 static void
2790 check_field_decls (tree t, tree *access_decls,
2791 int *cant_have_const_ctor_p,
2792 int *no_const_asn_ref_p)
2794 tree *field;
2795 tree *next;
2796 bool has_pointers;
2797 int any_default_members;
2799 /* Assume there are no access declarations. */
2800 *access_decls = NULL_TREE;
2801 /* Assume this class has no pointer members. */
2802 has_pointers = false;
2803 /* Assume none of the members of this class have default
2804 initializations. */
2805 any_default_members = 0;
2807 for (field = &TYPE_FIELDS (t); *field; field = next)
2809 tree x = *field;
2810 tree type = TREE_TYPE (x);
2812 next = &TREE_CHAIN (x);
2814 if (TREE_CODE (x) == USING_DECL)
2816 /* Prune the access declaration from the list of fields. */
2817 *field = TREE_CHAIN (x);
2819 /* Save the access declarations for our caller. */
2820 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
2822 /* Since we've reset *FIELD there's no reason to skip to the
2823 next field. */
2824 next = field;
2825 continue;
2828 if (TREE_CODE (x) == TYPE_DECL
2829 || TREE_CODE (x) == TEMPLATE_DECL)
2830 continue;
2832 /* If we've gotten this far, it's a data member, possibly static,
2833 or an enumerator. */
2834 DECL_CONTEXT (x) = t;
2836 /* When this goes into scope, it will be a non-local reference. */
2837 DECL_NONLOCAL (x) = 1;
2839 if (TREE_CODE (t) == UNION_TYPE)
2841 /* [class.union]
2843 If a union contains a static data member, or a member of
2844 reference type, the program is ill-formed. */
2845 if (TREE_CODE (x) == VAR_DECL)
2847 error ("%q+D may not be static because it is a member of a union", x);
2848 continue;
2850 if (TREE_CODE (type) == REFERENCE_TYPE)
2852 error ("%q+D may not have reference type %qT because"
2853 " it is a member of a union",
2854 x, type);
2855 continue;
2859 /* ``A local class cannot have static data members.'' ARM 9.4 */
2860 if (current_function_decl && TREE_STATIC (x))
2861 error ("field %q+D in local class cannot be static", x);
2863 /* Perform error checking that did not get done in
2864 grokdeclarator. */
2865 if (TREE_CODE (type) == FUNCTION_TYPE)
2867 error ("field %q+D invalidly declared function type", x);
2868 type = build_pointer_type (type);
2869 TREE_TYPE (x) = type;
2871 else if (TREE_CODE (type) == METHOD_TYPE)
2873 error ("field %q+D invalidly declared method type", x);
2874 type = build_pointer_type (type);
2875 TREE_TYPE (x) = type;
2878 if (type == error_mark_node)
2879 continue;
2881 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
2882 continue;
2884 /* Now it can only be a FIELD_DECL. */
2886 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
2887 CLASSTYPE_NON_AGGREGATE (t) = 1;
2889 /* If this is of reference type, check if it needs an init.
2890 Also do a little ANSI jig if necessary. */
2891 if (TREE_CODE (type) == REFERENCE_TYPE)
2893 CLASSTYPE_NON_POD_P (t) = 1;
2894 if (DECL_INITIAL (x) == NULL_TREE)
2895 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
2897 /* ARM $12.6.2: [A member initializer list] (or, for an
2898 aggregate, initialization by a brace-enclosed list) is the
2899 only way to initialize nonstatic const and reference
2900 members. */
2901 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
2903 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
2904 && extra_warnings)
2905 warning (OPT_Wextra, "non-static reference %q+#D in class without a constructor", x);
2908 type = strip_array_types (type);
2910 if (TYPE_PACKED (t))
2912 if (!pod_type_p (type) && !TYPE_PACKED (type))
2913 warning
2915 "ignoring packed attribute on unpacked non-POD field %q+#D",
2917 else if (TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
2918 DECL_PACKED (x) = 1;
2921 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
2922 /* We don't treat zero-width bitfields as making a class
2923 non-empty. */
2925 else
2927 /* The class is non-empty. */
2928 CLASSTYPE_EMPTY_P (t) = 0;
2929 /* The class is not even nearly empty. */
2930 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2931 /* If one of the data members contains an empty class,
2932 so does T. */
2933 if (CLASS_TYPE_P (type)
2934 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
2935 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
2938 /* This is used by -Weffc++ (see below). Warn only for pointers
2939 to members which might hold dynamic memory. So do not warn
2940 for pointers to functions or pointers to members. */
2941 if (TYPE_PTR_P (type)
2942 && !TYPE_PTRFN_P (type)
2943 && !TYPE_PTR_TO_MEMBER_P (type))
2944 has_pointers = true;
2946 if (CLASS_TYPE_P (type))
2948 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
2949 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
2950 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
2951 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
2954 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
2955 CLASSTYPE_HAS_MUTABLE (t) = 1;
2957 if (! pod_type_p (type))
2958 /* DR 148 now allows pointers to members (which are POD themselves),
2959 to be allowed in POD structs. */
2960 CLASSTYPE_NON_POD_P (t) = 1;
2962 if (! zero_init_p (type))
2963 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
2965 /* If any field is const, the structure type is pseudo-const. */
2966 if (CP_TYPE_CONST_P (type))
2968 C_TYPE_FIELDS_READONLY (t) = 1;
2969 if (DECL_INITIAL (x) == NULL_TREE)
2970 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
2972 /* ARM $12.6.2: [A member initializer list] (or, for an
2973 aggregate, initialization by a brace-enclosed list) is the
2974 only way to initialize nonstatic const and reference
2975 members. */
2976 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
2978 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
2979 && extra_warnings)
2980 warning (OPT_Wextra, "non-static const member %q+#D in class without a constructor", x);
2982 /* A field that is pseudo-const makes the structure likewise. */
2983 else if (CLASS_TYPE_P (type))
2985 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
2986 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
2987 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2988 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
2991 /* Core issue 80: A nonstatic data member is required to have a
2992 different name from the class iff the class has a
2993 user-defined constructor. */
2994 if (constructor_name_p (DECL_NAME (x), t) && TYPE_HAS_CONSTRUCTOR (t))
2995 pedwarn ("field %q+#D with same name as class", x);
2997 /* We set DECL_C_BIT_FIELD in grokbitfield.
2998 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
2999 if (DECL_C_BIT_FIELD (x))
3000 check_bitfield_decl (x);
3001 else
3002 check_field_decl (x, t,
3003 cant_have_const_ctor_p,
3004 no_const_asn_ref_p,
3005 &any_default_members);
3008 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3009 it should also define a copy constructor and an assignment operator to
3010 implement the correct copy semantic (deep vs shallow, etc.). As it is
3011 not feasible to check whether the constructors do allocate dynamic memory
3012 and store it within members, we approximate the warning like this:
3014 -- Warn only if there are members which are pointers
3015 -- Warn only if there is a non-trivial constructor (otherwise,
3016 there cannot be memory allocated).
3017 -- Warn only if there is a non-trivial destructor. We assume that the
3018 user at least implemented the cleanup correctly, and a destructor
3019 is needed to free dynamic memory.
3021 This seems enough for practical purposes. */
3022 if (warn_ecpp
3023 && has_pointers
3024 && TYPE_HAS_CONSTRUCTOR (t)
3025 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3026 && !(TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3028 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3030 if (! TYPE_HAS_INIT_REF (t))
3032 warning (OPT_Weffc__,
3033 " but does not override %<%T(const %T&)%>", t, t);
3034 if (!TYPE_HAS_ASSIGN_REF (t))
3035 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3037 else if (! TYPE_HAS_ASSIGN_REF (t))
3038 warning (OPT_Weffc__,
3039 " but does not override %<operator=(const %T&)%>", t);
3043 /* Check anonymous struct/anonymous union fields. */
3044 finish_struct_anon (t);
3046 /* We've built up the list of access declarations in reverse order.
3047 Fix that now. */
3048 *access_decls = nreverse (*access_decls);
3051 /* If TYPE is an empty class type, records its OFFSET in the table of
3052 OFFSETS. */
3054 static int
3055 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3057 splay_tree_node n;
3059 if (!is_empty_class (type))
3060 return 0;
3062 /* Record the location of this empty object in OFFSETS. */
3063 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3064 if (!n)
3065 n = splay_tree_insert (offsets,
3066 (splay_tree_key) offset,
3067 (splay_tree_value) NULL_TREE);
3068 n->value = ((splay_tree_value)
3069 tree_cons (NULL_TREE,
3070 type,
3071 (tree) n->value));
3073 return 0;
3076 /* Returns nonzero if TYPE is an empty class type and there is
3077 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3079 static int
3080 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3082 splay_tree_node n;
3083 tree t;
3085 if (!is_empty_class (type))
3086 return 0;
3088 /* Record the location of this empty object in OFFSETS. */
3089 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3090 if (!n)
3091 return 0;
3093 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3094 if (same_type_p (TREE_VALUE (t), type))
3095 return 1;
3097 return 0;
3100 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3101 F for every subobject, passing it the type, offset, and table of
3102 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3103 be traversed.
3105 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3106 than MAX_OFFSET will not be walked.
3108 If F returns a nonzero value, the traversal ceases, and that value
3109 is returned. Otherwise, returns zero. */
3111 static int
3112 walk_subobject_offsets (tree type,
3113 subobject_offset_fn f,
3114 tree offset,
3115 splay_tree offsets,
3116 tree max_offset,
3117 int vbases_p)
3119 int r = 0;
3120 tree type_binfo = NULL_TREE;
3122 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3123 stop. */
3124 if (max_offset && INT_CST_LT (max_offset, offset))
3125 return 0;
3127 if (type == error_mark_node)
3128 return 0;
3130 if (!TYPE_P (type))
3132 if (abi_version_at_least (2))
3133 type_binfo = type;
3134 type = BINFO_TYPE (type);
3137 if (CLASS_TYPE_P (type))
3139 tree field;
3140 tree binfo;
3141 int i;
3143 /* Avoid recursing into objects that are not interesting. */
3144 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3145 return 0;
3147 /* Record the location of TYPE. */
3148 r = (*f) (type, offset, offsets);
3149 if (r)
3150 return r;
3152 /* Iterate through the direct base classes of TYPE. */
3153 if (!type_binfo)
3154 type_binfo = TYPE_BINFO (type);
3155 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3157 tree binfo_offset;
3159 if (abi_version_at_least (2)
3160 && BINFO_VIRTUAL_P (binfo))
3161 continue;
3163 if (!vbases_p
3164 && BINFO_VIRTUAL_P (binfo)
3165 && !BINFO_PRIMARY_P (binfo))
3166 continue;
3168 if (!abi_version_at_least (2))
3169 binfo_offset = size_binop (PLUS_EXPR,
3170 offset,
3171 BINFO_OFFSET (binfo));
3172 else
3174 tree orig_binfo;
3175 /* We cannot rely on BINFO_OFFSET being set for the base
3176 class yet, but the offsets for direct non-virtual
3177 bases can be calculated by going back to the TYPE. */
3178 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3179 binfo_offset = size_binop (PLUS_EXPR,
3180 offset,
3181 BINFO_OFFSET (orig_binfo));
3184 r = walk_subobject_offsets (binfo,
3186 binfo_offset,
3187 offsets,
3188 max_offset,
3189 (abi_version_at_least (2)
3190 ? /*vbases_p=*/0 : vbases_p));
3191 if (r)
3192 return r;
3195 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3197 unsigned ix;
3198 VEC(tree,gc) *vbases;
3200 /* Iterate through the virtual base classes of TYPE. In G++
3201 3.2, we included virtual bases in the direct base class
3202 loop above, which results in incorrect results; the
3203 correct offsets for virtual bases are only known when
3204 working with the most derived type. */
3205 if (vbases_p)
3206 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3207 VEC_iterate (tree, vbases, ix, binfo); ix++)
3209 r = walk_subobject_offsets (binfo,
3211 size_binop (PLUS_EXPR,
3212 offset,
3213 BINFO_OFFSET (binfo)),
3214 offsets,
3215 max_offset,
3216 /*vbases_p=*/0);
3217 if (r)
3218 return r;
3220 else
3222 /* We still have to walk the primary base, if it is
3223 virtual. (If it is non-virtual, then it was walked
3224 above.) */
3225 tree vbase = get_primary_binfo (type_binfo);
3227 if (vbase && BINFO_VIRTUAL_P (vbase)
3228 && BINFO_PRIMARY_P (vbase)
3229 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3231 r = (walk_subobject_offsets
3232 (vbase, f, offset,
3233 offsets, max_offset, /*vbases_p=*/0));
3234 if (r)
3235 return r;
3240 /* Iterate through the fields of TYPE. */
3241 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3242 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3244 tree field_offset;
3246 if (abi_version_at_least (2))
3247 field_offset = byte_position (field);
3248 else
3249 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3250 field_offset = DECL_FIELD_OFFSET (field);
3252 r = walk_subobject_offsets (TREE_TYPE (field),
3254 size_binop (PLUS_EXPR,
3255 offset,
3256 field_offset),
3257 offsets,
3258 max_offset,
3259 /*vbases_p=*/1);
3260 if (r)
3261 return r;
3264 else if (TREE_CODE (type) == ARRAY_TYPE)
3266 tree element_type = strip_array_types (type);
3267 tree domain = TYPE_DOMAIN (type);
3268 tree index;
3270 /* Avoid recursing into objects that are not interesting. */
3271 if (!CLASS_TYPE_P (element_type)
3272 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3273 return 0;
3275 /* Step through each of the elements in the array. */
3276 for (index = size_zero_node;
3277 /* G++ 3.2 had an off-by-one error here. */
3278 (abi_version_at_least (2)
3279 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3280 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3281 index = size_binop (PLUS_EXPR, index, size_one_node))
3283 r = walk_subobject_offsets (TREE_TYPE (type),
3285 offset,
3286 offsets,
3287 max_offset,
3288 /*vbases_p=*/1);
3289 if (r)
3290 return r;
3291 offset = size_binop (PLUS_EXPR, offset,
3292 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3293 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3294 there's no point in iterating through the remaining
3295 elements of the array. */
3296 if (max_offset && INT_CST_LT (max_offset, offset))
3297 break;
3301 return 0;
3304 /* Record all of the empty subobjects of TYPE (either a type or a
3305 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3306 is being placed at OFFSET; otherwise, it is a base class that is
3307 being placed at OFFSET. */
3309 static void
3310 record_subobject_offsets (tree type,
3311 tree offset,
3312 splay_tree offsets,
3313 bool is_data_member)
3315 tree max_offset;
3316 /* If recording subobjects for a non-static data member or a
3317 non-empty base class , we do not need to record offsets beyond
3318 the size of the biggest empty class. Additional data members
3319 will go at the end of the class. Additional base classes will go
3320 either at offset zero (if empty, in which case they cannot
3321 overlap with offsets past the size of the biggest empty class) or
3322 at the end of the class.
3324 However, if we are placing an empty base class, then we must record
3325 all offsets, as either the empty class is at offset zero (where
3326 other empty classes might later be placed) or at the end of the
3327 class (where other objects might then be placed, so other empty
3328 subobjects might later overlap). */
3329 if (is_data_member
3330 || !is_empty_class (BINFO_TYPE (type)))
3331 max_offset = sizeof_biggest_empty_class;
3332 else
3333 max_offset = NULL_TREE;
3334 walk_subobject_offsets (type, record_subobject_offset, offset,
3335 offsets, max_offset, is_data_member);
3338 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3339 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3340 virtual bases of TYPE are examined. */
3342 static int
3343 layout_conflict_p (tree type,
3344 tree offset,
3345 splay_tree offsets,
3346 int vbases_p)
3348 splay_tree_node max_node;
3350 /* Get the node in OFFSETS that indicates the maximum offset where
3351 an empty subobject is located. */
3352 max_node = splay_tree_max (offsets);
3353 /* If there aren't any empty subobjects, then there's no point in
3354 performing this check. */
3355 if (!max_node)
3356 return 0;
3358 return walk_subobject_offsets (type, check_subobject_offset, offset,
3359 offsets, (tree) (max_node->key),
3360 vbases_p);
3363 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3364 non-static data member of the type indicated by RLI. BINFO is the
3365 binfo corresponding to the base subobject, OFFSETS maps offsets to
3366 types already located at those offsets. This function determines
3367 the position of the DECL. */
3369 static void
3370 layout_nonempty_base_or_field (record_layout_info rli,
3371 tree decl,
3372 tree binfo,
3373 splay_tree offsets)
3375 tree offset = NULL_TREE;
3376 bool field_p;
3377 tree type;
3379 if (binfo)
3381 /* For the purposes of determining layout conflicts, we want to
3382 use the class type of BINFO; TREE_TYPE (DECL) will be the
3383 CLASSTYPE_AS_BASE version, which does not contain entries for
3384 zero-sized bases. */
3385 type = TREE_TYPE (binfo);
3386 field_p = false;
3388 else
3390 type = TREE_TYPE (decl);
3391 field_p = true;
3394 /* Try to place the field. It may take more than one try if we have
3395 a hard time placing the field without putting two objects of the
3396 same type at the same address. */
3397 while (1)
3399 struct record_layout_info_s old_rli = *rli;
3401 /* Place this field. */
3402 place_field (rli, decl);
3403 offset = byte_position (decl);
3405 /* We have to check to see whether or not there is already
3406 something of the same type at the offset we're about to use.
3407 For example, consider:
3409 struct S {};
3410 struct T : public S { int i; };
3411 struct U : public S, public T {};
3413 Here, we put S at offset zero in U. Then, we can't put T at
3414 offset zero -- its S component would be at the same address
3415 as the S we already allocated. So, we have to skip ahead.
3416 Since all data members, including those whose type is an
3417 empty class, have nonzero size, any overlap can happen only
3418 with a direct or indirect base-class -- it can't happen with
3419 a data member. */
3420 /* In a union, overlap is permitted; all members are placed at
3421 offset zero. */
3422 if (TREE_CODE (rli->t) == UNION_TYPE)
3423 break;
3424 /* G++ 3.2 did not check for overlaps when placing a non-empty
3425 virtual base. */
3426 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3427 break;
3428 if (layout_conflict_p (field_p ? type : binfo, offset,
3429 offsets, field_p))
3431 /* Strip off the size allocated to this field. That puts us
3432 at the first place we could have put the field with
3433 proper alignment. */
3434 *rli = old_rli;
3436 /* Bump up by the alignment required for the type. */
3437 rli->bitpos
3438 = size_binop (PLUS_EXPR, rli->bitpos,
3439 bitsize_int (binfo
3440 ? CLASSTYPE_ALIGN (type)
3441 : TYPE_ALIGN (type)));
3442 normalize_rli (rli);
3444 else
3445 /* There was no conflict. We're done laying out this field. */
3446 break;
3449 /* Now that we know where it will be placed, update its
3450 BINFO_OFFSET. */
3451 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3452 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3453 this point because their BINFO_OFFSET is copied from another
3454 hierarchy. Therefore, we may not need to add the entire
3455 OFFSET. */
3456 propagate_binfo_offsets (binfo,
3457 size_diffop (convert (ssizetype, offset),
3458 convert (ssizetype,
3459 BINFO_OFFSET (binfo))));
3462 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3464 static int
3465 empty_base_at_nonzero_offset_p (tree type,
3466 tree offset,
3467 splay_tree offsets ATTRIBUTE_UNUSED)
3469 return is_empty_class (type) && !integer_zerop (offset);
3472 /* Layout the empty base BINFO. EOC indicates the byte currently just
3473 past the end of the class, and should be correctly aligned for a
3474 class of the type indicated by BINFO; OFFSETS gives the offsets of
3475 the empty bases allocated so far. T is the most derived
3476 type. Return nonzero iff we added it at the end. */
3478 static bool
3479 layout_empty_base (tree binfo, tree eoc, splay_tree offsets)
3481 tree alignment;
3482 tree basetype = BINFO_TYPE (binfo);
3483 bool atend = false;
3485 /* This routine should only be used for empty classes. */
3486 gcc_assert (is_empty_class (basetype));
3487 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3489 if (!integer_zerop (BINFO_OFFSET (binfo)))
3491 if (abi_version_at_least (2))
3492 propagate_binfo_offsets
3493 (binfo, size_diffop (size_zero_node, BINFO_OFFSET (binfo)));
3494 else
3495 warning (OPT_Wabi,
3496 "offset of empty base %qT may not be ABI-compliant and may"
3497 "change in a future version of GCC",
3498 BINFO_TYPE (binfo));
3501 /* This is an empty base class. We first try to put it at offset
3502 zero. */
3503 if (layout_conflict_p (binfo,
3504 BINFO_OFFSET (binfo),
3505 offsets,
3506 /*vbases_p=*/0))
3508 /* That didn't work. Now, we move forward from the next
3509 available spot in the class. */
3510 atend = true;
3511 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3512 while (1)
3514 if (!layout_conflict_p (binfo,
3515 BINFO_OFFSET (binfo),
3516 offsets,
3517 /*vbases_p=*/0))
3518 /* We finally found a spot where there's no overlap. */
3519 break;
3521 /* There's overlap here, too. Bump along to the next spot. */
3522 propagate_binfo_offsets (binfo, alignment);
3525 return atend;
3528 /* Layout the base given by BINFO in the class indicated by RLI.
3529 *BASE_ALIGN is a running maximum of the alignments of
3530 any base class. OFFSETS gives the location of empty base
3531 subobjects. T is the most derived type. Return nonzero if the new
3532 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3533 *NEXT_FIELD, unless BINFO is for an empty base class.
3535 Returns the location at which the next field should be inserted. */
3537 static tree *
3538 build_base_field (record_layout_info rli, tree binfo,
3539 splay_tree offsets, tree *next_field)
3541 tree t = rli->t;
3542 tree basetype = BINFO_TYPE (binfo);
3544 if (!COMPLETE_TYPE_P (basetype))
3545 /* This error is now reported in xref_tag, thus giving better
3546 location information. */
3547 return next_field;
3549 /* Place the base class. */
3550 if (!is_empty_class (basetype))
3552 tree decl;
3554 /* The containing class is non-empty because it has a non-empty
3555 base class. */
3556 CLASSTYPE_EMPTY_P (t) = 0;
3558 /* Create the FIELD_DECL. */
3559 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3560 DECL_ARTIFICIAL (decl) = 1;
3561 DECL_IGNORED_P (decl) = 1;
3562 DECL_FIELD_CONTEXT (decl) = t;
3563 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3564 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3565 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3566 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3567 DECL_MODE (decl) = TYPE_MODE (basetype);
3568 DECL_FIELD_IS_BASE (decl) = 1;
3570 /* Try to place the field. It may take more than one try if we
3571 have a hard time placing the field without putting two
3572 objects of the same type at the same address. */
3573 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3574 /* Add the new FIELD_DECL to the list of fields for T. */
3575 TREE_CHAIN (decl) = *next_field;
3576 *next_field = decl;
3577 next_field = &TREE_CHAIN (decl);
3579 else
3581 tree eoc;
3582 bool atend;
3584 /* On some platforms (ARM), even empty classes will not be
3585 byte-aligned. */
3586 eoc = round_up (rli_size_unit_so_far (rli),
3587 CLASSTYPE_ALIGN_UNIT (basetype));
3588 atend = layout_empty_base (binfo, eoc, offsets);
3589 /* A nearly-empty class "has no proper base class that is empty,
3590 not morally virtual, and at an offset other than zero." */
3591 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3593 if (atend)
3594 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3595 /* The check above (used in G++ 3.2) is insufficient because
3596 an empty class placed at offset zero might itself have an
3597 empty base at a nonzero offset. */
3598 else if (walk_subobject_offsets (basetype,
3599 empty_base_at_nonzero_offset_p,
3600 size_zero_node,
3601 /*offsets=*/NULL,
3602 /*max_offset=*/NULL_TREE,
3603 /*vbases_p=*/true))
3605 if (abi_version_at_least (2))
3606 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3607 else
3608 warning (OPT_Wabi,
3609 "class %qT will be considered nearly empty in a "
3610 "future version of GCC", t);
3614 /* We do not create a FIELD_DECL for empty base classes because
3615 it might overlap some other field. We want to be able to
3616 create CONSTRUCTORs for the class by iterating over the
3617 FIELD_DECLs, and the back end does not handle overlapping
3618 FIELD_DECLs. */
3620 /* An empty virtual base causes a class to be non-empty
3621 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3622 here because that was already done when the virtual table
3623 pointer was created. */
3626 /* Record the offsets of BINFO and its base subobjects. */
3627 record_subobject_offsets (binfo,
3628 BINFO_OFFSET (binfo),
3629 offsets,
3630 /*is_data_member=*/false);
3632 return next_field;
3635 /* Layout all of the non-virtual base classes. Record empty
3636 subobjects in OFFSETS. T is the most derived type. Return nonzero
3637 if the type cannot be nearly empty. The fields created
3638 corresponding to the base classes will be inserted at
3639 *NEXT_FIELD. */
3641 static void
3642 build_base_fields (record_layout_info rli,
3643 splay_tree offsets, tree *next_field)
3645 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3646 subobjects. */
3647 tree t = rli->t;
3648 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3649 int i;
3651 /* The primary base class is always allocated first. */
3652 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3653 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3654 offsets, next_field);
3656 /* Now allocate the rest of the bases. */
3657 for (i = 0; i < n_baseclasses; ++i)
3659 tree base_binfo;
3661 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
3663 /* The primary base was already allocated above, so we don't
3664 need to allocate it again here. */
3665 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3666 continue;
3668 /* Virtual bases are added at the end (a primary virtual base
3669 will have already been added). */
3670 if (BINFO_VIRTUAL_P (base_binfo))
3671 continue;
3673 next_field = build_base_field (rli, base_binfo,
3674 offsets, next_field);
3678 /* Go through the TYPE_METHODS of T issuing any appropriate
3679 diagnostics, figuring out which methods override which other
3680 methods, and so forth. */
3682 static void
3683 check_methods (tree t)
3685 tree x;
3687 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3689 check_for_override (x, t);
3690 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3691 error ("initializer specified for non-virtual method %q+D", x);
3692 /* The name of the field is the original field name
3693 Save this in auxiliary field for later overloading. */
3694 if (DECL_VINDEX (x))
3696 TYPE_POLYMORPHIC_P (t) = 1;
3697 if (DECL_PURE_VIRTUAL_P (x))
3698 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
3700 /* All user-declared destructors are non-trivial. */
3701 if (DECL_DESTRUCTOR_P (x))
3702 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
3706 /* FN is a constructor or destructor. Clone the declaration to create
3707 a specialized in-charge or not-in-charge version, as indicated by
3708 NAME. */
3710 static tree
3711 build_clone (tree fn, tree name)
3713 tree parms;
3714 tree clone;
3716 /* Copy the function. */
3717 clone = copy_decl (fn);
3718 /* Remember where this function came from. */
3719 DECL_CLONED_FUNCTION (clone) = fn;
3720 DECL_ABSTRACT_ORIGIN (clone) = fn;
3721 /* Reset the function name. */
3722 DECL_NAME (clone) = name;
3723 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3724 /* There's no pending inline data for this function. */
3725 DECL_PENDING_INLINE_INFO (clone) = NULL;
3726 DECL_PENDING_INLINE_P (clone) = 0;
3727 /* And it hasn't yet been deferred. */
3728 DECL_DEFERRED_FN (clone) = 0;
3730 /* The base-class destructor is not virtual. */
3731 if (name == base_dtor_identifier)
3733 DECL_VIRTUAL_P (clone) = 0;
3734 if (TREE_CODE (clone) != TEMPLATE_DECL)
3735 DECL_VINDEX (clone) = NULL_TREE;
3738 /* If there was an in-charge parameter, drop it from the function
3739 type. */
3740 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3742 tree basetype;
3743 tree parmtypes;
3744 tree exceptions;
3746 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3747 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3748 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3749 /* Skip the `this' parameter. */
3750 parmtypes = TREE_CHAIN (parmtypes);
3751 /* Skip the in-charge parameter. */
3752 parmtypes = TREE_CHAIN (parmtypes);
3753 /* And the VTT parm, in a complete [cd]tor. */
3754 if (DECL_HAS_VTT_PARM_P (fn)
3755 && ! DECL_NEEDS_VTT_PARM_P (clone))
3756 parmtypes = TREE_CHAIN (parmtypes);
3757 /* If this is subobject constructor or destructor, add the vtt
3758 parameter. */
3759 TREE_TYPE (clone)
3760 = build_method_type_directly (basetype,
3761 TREE_TYPE (TREE_TYPE (clone)),
3762 parmtypes);
3763 if (exceptions)
3764 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3765 exceptions);
3766 TREE_TYPE (clone)
3767 = cp_build_type_attribute_variant (TREE_TYPE (clone),
3768 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
3771 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3772 aren't function parameters; those are the template parameters. */
3773 if (TREE_CODE (clone) != TEMPLATE_DECL)
3775 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3776 /* Remove the in-charge parameter. */
3777 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3779 TREE_CHAIN (DECL_ARGUMENTS (clone))
3780 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3781 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3783 /* And the VTT parm, in a complete [cd]tor. */
3784 if (DECL_HAS_VTT_PARM_P (fn))
3786 if (DECL_NEEDS_VTT_PARM_P (clone))
3787 DECL_HAS_VTT_PARM_P (clone) = 1;
3788 else
3790 TREE_CHAIN (DECL_ARGUMENTS (clone))
3791 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3792 DECL_HAS_VTT_PARM_P (clone) = 0;
3796 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3798 DECL_CONTEXT (parms) = clone;
3799 cxx_dup_lang_specific_decl (parms);
3803 /* Create the RTL for this function. */
3804 SET_DECL_RTL (clone, NULL_RTX);
3805 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
3807 /* Make it easy to find the CLONE given the FN. */
3808 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3809 TREE_CHAIN (fn) = clone;
3811 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
3812 if (TREE_CODE (clone) == TEMPLATE_DECL)
3814 tree result;
3816 DECL_TEMPLATE_RESULT (clone)
3817 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3818 result = DECL_TEMPLATE_RESULT (clone);
3819 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3820 DECL_TI_TEMPLATE (result) = clone;
3822 else if (pch_file)
3823 note_decl_for_pch (clone);
3825 return clone;
3828 /* Produce declarations for all appropriate clones of FN. If
3829 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
3830 CLASTYPE_METHOD_VEC as well. */
3832 void
3833 clone_function_decl (tree fn, int update_method_vec_p)
3835 tree clone;
3837 /* Avoid inappropriate cloning. */
3838 if (TREE_CHAIN (fn)
3839 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
3840 return;
3842 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
3844 /* For each constructor, we need two variants: an in-charge version
3845 and a not-in-charge version. */
3846 clone = build_clone (fn, complete_ctor_identifier);
3847 if (update_method_vec_p)
3848 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3849 clone = build_clone (fn, base_ctor_identifier);
3850 if (update_method_vec_p)
3851 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3853 else
3855 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
3857 /* For each destructor, we need three variants: an in-charge
3858 version, a not-in-charge version, and an in-charge deleting
3859 version. We clone the deleting version first because that
3860 means it will go second on the TYPE_METHODS list -- and that
3861 corresponds to the correct layout order in the virtual
3862 function table.
3864 For a non-virtual destructor, we do not build a deleting
3865 destructor. */
3866 if (DECL_VIRTUAL_P (fn))
3868 clone = build_clone (fn, deleting_dtor_identifier);
3869 if (update_method_vec_p)
3870 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3872 clone = build_clone (fn, complete_dtor_identifier);
3873 if (update_method_vec_p)
3874 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3875 clone = build_clone (fn, base_dtor_identifier);
3876 if (update_method_vec_p)
3877 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
3880 /* Note that this is an abstract function that is never emitted. */
3881 DECL_ABSTRACT (fn) = 1;
3884 /* DECL is an in charge constructor, which is being defined. This will
3885 have had an in class declaration, from whence clones were
3886 declared. An out-of-class definition can specify additional default
3887 arguments. As it is the clones that are involved in overload
3888 resolution, we must propagate the information from the DECL to its
3889 clones. */
3891 void
3892 adjust_clone_args (tree decl)
3894 tree clone;
3896 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
3897 clone = TREE_CHAIN (clone))
3899 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
3900 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
3901 tree decl_parms, clone_parms;
3903 clone_parms = orig_clone_parms;
3905 /* Skip the 'this' parameter. */
3906 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
3907 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3909 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
3910 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3911 if (DECL_HAS_VTT_PARM_P (decl))
3912 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3914 clone_parms = orig_clone_parms;
3915 if (DECL_HAS_VTT_PARM_P (clone))
3916 clone_parms = TREE_CHAIN (clone_parms);
3918 for (decl_parms = orig_decl_parms; decl_parms;
3919 decl_parms = TREE_CHAIN (decl_parms),
3920 clone_parms = TREE_CHAIN (clone_parms))
3922 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
3923 TREE_TYPE (clone_parms)));
3925 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
3927 /* A default parameter has been added. Adjust the
3928 clone's parameters. */
3929 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3930 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3931 tree type;
3933 clone_parms = orig_decl_parms;
3935 if (DECL_HAS_VTT_PARM_P (clone))
3937 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
3938 TREE_VALUE (orig_clone_parms),
3939 clone_parms);
3940 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
3942 type = build_method_type_directly (basetype,
3943 TREE_TYPE (TREE_TYPE (clone)),
3944 clone_parms);
3945 if (exceptions)
3946 type = build_exception_variant (type, exceptions);
3947 TREE_TYPE (clone) = type;
3949 clone_parms = NULL_TREE;
3950 break;
3953 gcc_assert (!clone_parms);
3957 /* For each of the constructors and destructors in T, create an
3958 in-charge and not-in-charge variant. */
3960 static void
3961 clone_constructors_and_destructors (tree t)
3963 tree fns;
3965 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
3966 out now. */
3967 if (!CLASSTYPE_METHOD_VEC (t))
3968 return;
3970 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
3971 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
3972 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
3973 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
3976 /* Remove all zero-width bit-fields from T. */
3978 static void
3979 remove_zero_width_bit_fields (tree t)
3981 tree *fieldsp;
3983 fieldsp = &TYPE_FIELDS (t);
3984 while (*fieldsp)
3986 if (TREE_CODE (*fieldsp) == FIELD_DECL
3987 && DECL_C_BIT_FIELD (*fieldsp)
3988 && DECL_INITIAL (*fieldsp))
3989 *fieldsp = TREE_CHAIN (*fieldsp);
3990 else
3991 fieldsp = &TREE_CHAIN (*fieldsp);
3995 /* Returns TRUE iff we need a cookie when dynamically allocating an
3996 array whose elements have the indicated class TYPE. */
3998 static bool
3999 type_requires_array_cookie (tree type)
4001 tree fns;
4002 bool has_two_argument_delete_p = false;
4004 gcc_assert (CLASS_TYPE_P (type));
4006 /* If there's a non-trivial destructor, we need a cookie. In order
4007 to iterate through the array calling the destructor for each
4008 element, we'll have to know how many elements there are. */
4009 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4010 return true;
4012 /* If the usual deallocation function is a two-argument whose second
4013 argument is of type `size_t', then we have to pass the size of
4014 the array to the deallocation function, so we will need to store
4015 a cookie. */
4016 fns = lookup_fnfields (TYPE_BINFO (type),
4017 ansi_opname (VEC_DELETE_EXPR),
4018 /*protect=*/0);
4019 /* If there are no `operator []' members, or the lookup is
4020 ambiguous, then we don't need a cookie. */
4021 if (!fns || fns == error_mark_node)
4022 return false;
4023 /* Loop through all of the functions. */
4024 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4026 tree fn;
4027 tree second_parm;
4029 /* Select the current function. */
4030 fn = OVL_CURRENT (fns);
4031 /* See if this function is a one-argument delete function. If
4032 it is, then it will be the usual deallocation function. */
4033 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4034 if (second_parm == void_list_node)
4035 return false;
4036 /* Otherwise, if we have a two-argument function and the second
4037 argument is `size_t', it will be the usual deallocation
4038 function -- unless there is one-argument function, too. */
4039 if (TREE_CHAIN (second_parm) == void_list_node
4040 && same_type_p (TREE_VALUE (second_parm), sizetype))
4041 has_two_argument_delete_p = true;
4044 return has_two_argument_delete_p;
4047 /* Check the validity of the bases and members declared in T. Add any
4048 implicitly-generated functions (like copy-constructors and
4049 assignment operators). Compute various flag bits (like
4050 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4051 level: i.e., independently of the ABI in use. */
4053 static void
4054 check_bases_and_members (tree t)
4056 /* Nonzero if the implicitly generated copy constructor should take
4057 a non-const reference argument. */
4058 int cant_have_const_ctor;
4059 /* Nonzero if the implicitly generated assignment operator
4060 should take a non-const reference argument. */
4061 int no_const_asn_ref;
4062 tree access_decls;
4064 /* By default, we use const reference arguments and generate default
4065 constructors. */
4066 cant_have_const_ctor = 0;
4067 no_const_asn_ref = 0;
4069 /* Check all the base-classes. */
4070 check_bases (t, &cant_have_const_ctor,
4071 &no_const_asn_ref);
4073 /* Check all the method declarations. */
4074 check_methods (t);
4076 /* Check all the data member declarations. We cannot call
4077 check_field_decls until we have called check_bases check_methods,
4078 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4079 being set appropriately. */
4080 check_field_decls (t, &access_decls,
4081 &cant_have_const_ctor,
4082 &no_const_asn_ref);
4084 /* A nearly-empty class has to be vptr-containing; a nearly empty
4085 class contains just a vptr. */
4086 if (!TYPE_CONTAINS_VPTR_P (t))
4087 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4089 /* Do some bookkeeping that will guide the generation of implicitly
4090 declared member functions. */
4091 TYPE_HAS_COMPLEX_INIT_REF (t)
4092 |= (TYPE_HAS_INIT_REF (t) || TYPE_CONTAINS_VPTR_P (t));
4093 TYPE_NEEDS_CONSTRUCTING (t)
4094 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_CONTAINS_VPTR_P (t));
4095 CLASSTYPE_NON_AGGREGATE (t)
4096 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_POLYMORPHIC_P (t));
4097 CLASSTYPE_NON_POD_P (t)
4098 |= (CLASSTYPE_NON_AGGREGATE (t)
4099 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4100 || TYPE_HAS_ASSIGN_REF (t));
4101 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4102 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4104 /* Synthesize any needed methods. */
4105 add_implicitly_declared_members (t,
4106 cant_have_const_ctor,
4107 no_const_asn_ref);
4109 /* Create the in-charge and not-in-charge variants of constructors
4110 and destructors. */
4111 clone_constructors_and_destructors (t);
4113 /* Process the using-declarations. */
4114 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4115 handle_using_decl (TREE_VALUE (access_decls), t);
4117 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4118 finish_struct_methods (t);
4120 /* Figure out whether or not we will need a cookie when dynamically
4121 allocating an array of this type. */
4122 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4123 = type_requires_array_cookie (t);
4126 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4127 accordingly. If a new vfield was created (because T doesn't have a
4128 primary base class), then the newly created field is returned. It
4129 is not added to the TYPE_FIELDS list; it is the caller's
4130 responsibility to do that. Accumulate declared virtual functions
4131 on VIRTUALS_P. */
4133 static tree
4134 create_vtable_ptr (tree t, tree* virtuals_p)
4136 tree fn;
4138 /* Collect the virtual functions declared in T. */
4139 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4140 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4141 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4143 tree new_virtual = make_node (TREE_LIST);
4145 BV_FN (new_virtual) = fn;
4146 BV_DELTA (new_virtual) = integer_zero_node;
4147 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
4149 TREE_CHAIN (new_virtual) = *virtuals_p;
4150 *virtuals_p = new_virtual;
4153 /* If we couldn't find an appropriate base class, create a new field
4154 here. Even if there weren't any new virtual functions, we might need a
4155 new virtual function table if we're supposed to include vptrs in
4156 all classes that need them. */
4157 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4159 /* We build this decl with vtbl_ptr_type_node, which is a
4160 `vtable_entry_type*'. It might seem more precise to use
4161 `vtable_entry_type (*)[N]' where N is the number of virtual
4162 functions. However, that would require the vtable pointer in
4163 base classes to have a different type than the vtable pointer
4164 in derived classes. We could make that happen, but that
4165 still wouldn't solve all the problems. In particular, the
4166 type-based alias analysis code would decide that assignments
4167 to the base class vtable pointer can't alias assignments to
4168 the derived class vtable pointer, since they have different
4169 types. Thus, in a derived class destructor, where the base
4170 class constructor was inlined, we could generate bad code for
4171 setting up the vtable pointer.
4173 Therefore, we use one type for all vtable pointers. We still
4174 use a type-correct type; it's just doesn't indicate the array
4175 bounds. That's better than using `void*' or some such; it's
4176 cleaner, and it let's the alias analysis code know that these
4177 stores cannot alias stores to void*! */
4178 tree field;
4180 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4181 DECL_VIRTUAL_P (field) = 1;
4182 DECL_ARTIFICIAL (field) = 1;
4183 DECL_FIELD_CONTEXT (field) = t;
4184 DECL_FCONTEXT (field) = t;
4186 TYPE_VFIELD (t) = field;
4188 /* This class is non-empty. */
4189 CLASSTYPE_EMPTY_P (t) = 0;
4191 return field;
4194 return NULL_TREE;
4197 /* Fixup the inline function given by INFO now that the class is
4198 complete. */
4200 static void
4201 fixup_pending_inline (tree fn)
4203 if (DECL_PENDING_INLINE_INFO (fn))
4205 tree args = DECL_ARGUMENTS (fn);
4206 while (args)
4208 DECL_CONTEXT (args) = fn;
4209 args = TREE_CHAIN (args);
4214 /* Fixup the inline methods and friends in TYPE now that TYPE is
4215 complete. */
4217 static void
4218 fixup_inline_methods (tree type)
4220 tree method = TYPE_METHODS (type);
4221 VEC(tree,gc) *friends;
4222 unsigned ix;
4224 if (method && TREE_CODE (method) == TREE_VEC)
4226 if (TREE_VEC_ELT (method, 1))
4227 method = TREE_VEC_ELT (method, 1);
4228 else if (TREE_VEC_ELT (method, 0))
4229 method = TREE_VEC_ELT (method, 0);
4230 else
4231 method = TREE_VEC_ELT (method, 2);
4234 /* Do inline member functions. */
4235 for (; method; method = TREE_CHAIN (method))
4236 fixup_pending_inline (method);
4238 /* Do friends. */
4239 for (friends = CLASSTYPE_INLINE_FRIENDS (type), ix = 0;
4240 VEC_iterate (tree, friends, ix, method); ix++)
4241 fixup_pending_inline (method);
4242 CLASSTYPE_INLINE_FRIENDS (type) = NULL;
4245 /* Add OFFSET to all base types of BINFO which is a base in the
4246 hierarchy dominated by T.
4248 OFFSET, which is a type offset, is number of bytes. */
4250 static void
4251 propagate_binfo_offsets (tree binfo, tree offset)
4253 int i;
4254 tree primary_binfo;
4255 tree base_binfo;
4257 /* Update BINFO's offset. */
4258 BINFO_OFFSET (binfo)
4259 = convert (sizetype,
4260 size_binop (PLUS_EXPR,
4261 convert (ssizetype, BINFO_OFFSET (binfo)),
4262 offset));
4264 /* Find the primary base class. */
4265 primary_binfo = get_primary_binfo (binfo);
4267 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
4268 propagate_binfo_offsets (primary_binfo, offset);
4270 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4271 downwards. */
4272 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4274 /* Don't do the primary base twice. */
4275 if (base_binfo == primary_binfo)
4276 continue;
4278 if (BINFO_VIRTUAL_P (base_binfo))
4279 continue;
4281 propagate_binfo_offsets (base_binfo, offset);
4285 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4286 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4287 empty subobjects of T. */
4289 static void
4290 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4292 tree vbase;
4293 tree t = rli->t;
4294 bool first_vbase = true;
4295 tree *next_field;
4297 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
4298 return;
4300 if (!abi_version_at_least(2))
4302 /* In G++ 3.2, we incorrectly rounded the size before laying out
4303 the virtual bases. */
4304 finish_record_layout (rli, /*free_p=*/false);
4305 #ifdef STRUCTURE_SIZE_BOUNDARY
4306 /* Packed structures don't need to have minimum size. */
4307 if (! TYPE_PACKED (t))
4308 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4309 #endif
4310 rli->offset = TYPE_SIZE_UNIT (t);
4311 rli->bitpos = bitsize_zero_node;
4312 rli->record_align = TYPE_ALIGN (t);
4315 /* Find the last field. The artificial fields created for virtual
4316 bases will go after the last extant field to date. */
4317 next_field = &TYPE_FIELDS (t);
4318 while (*next_field)
4319 next_field = &TREE_CHAIN (*next_field);
4321 /* Go through the virtual bases, allocating space for each virtual
4322 base that is not already a primary base class. These are
4323 allocated in inheritance graph order. */
4324 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
4326 if (!BINFO_VIRTUAL_P (vbase))
4327 continue;
4329 if (!BINFO_PRIMARY_P (vbase))
4331 tree basetype = TREE_TYPE (vbase);
4333 /* This virtual base is not a primary base of any class in the
4334 hierarchy, so we have to add space for it. */
4335 next_field = build_base_field (rli, vbase,
4336 offsets, next_field);
4338 /* If the first virtual base might have been placed at a
4339 lower address, had we started from CLASSTYPE_SIZE, rather
4340 than TYPE_SIZE, issue a warning. There can be both false
4341 positives and false negatives from this warning in rare
4342 cases; to deal with all the possibilities would probably
4343 require performing both layout algorithms and comparing
4344 the results which is not particularly tractable. */
4345 if (warn_abi
4346 && first_vbase
4347 && (tree_int_cst_lt
4348 (size_binop (CEIL_DIV_EXPR,
4349 round_up (CLASSTYPE_SIZE (t),
4350 CLASSTYPE_ALIGN (basetype)),
4351 bitsize_unit_node),
4352 BINFO_OFFSET (vbase))))
4353 warning (OPT_Wabi,
4354 "offset of virtual base %qT is not ABI-compliant and "
4355 "may change in a future version of GCC",
4356 basetype);
4358 first_vbase = false;
4363 /* Returns the offset of the byte just past the end of the base class
4364 BINFO. */
4366 static tree
4367 end_of_base (tree binfo)
4369 tree size;
4371 if (is_empty_class (BINFO_TYPE (binfo)))
4372 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4373 allocate some space for it. It cannot have virtual bases, so
4374 TYPE_SIZE_UNIT is fine. */
4375 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4376 else
4377 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4379 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4382 /* Returns the offset of the byte just past the end of the base class
4383 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4384 only non-virtual bases are included. */
4386 static tree
4387 end_of_class (tree t, int include_virtuals_p)
4389 tree result = size_zero_node;
4390 VEC(tree,gc) *vbases;
4391 tree binfo;
4392 tree base_binfo;
4393 tree offset;
4394 int i;
4396 for (binfo = TYPE_BINFO (t), i = 0;
4397 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4399 if (!include_virtuals_p
4400 && BINFO_VIRTUAL_P (base_binfo)
4401 && (!BINFO_PRIMARY_P (base_binfo)
4402 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
4403 continue;
4405 offset = end_of_base (base_binfo);
4406 if (INT_CST_LT_UNSIGNED (result, offset))
4407 result = offset;
4410 /* G++ 3.2 did not check indirect virtual bases. */
4411 if (abi_version_at_least (2) && include_virtuals_p)
4412 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
4413 VEC_iterate (tree, vbases, i, base_binfo); i++)
4415 offset = end_of_base (base_binfo);
4416 if (INT_CST_LT_UNSIGNED (result, offset))
4417 result = offset;
4420 return result;
4423 /* Warn about bases of T that are inaccessible because they are
4424 ambiguous. For example:
4426 struct S {};
4427 struct T : public S {};
4428 struct U : public S, public T {};
4430 Here, `(S*) new U' is not allowed because there are two `S'
4431 subobjects of U. */
4433 static void
4434 warn_about_ambiguous_bases (tree t)
4436 int i;
4437 VEC(tree,gc) *vbases;
4438 tree basetype;
4439 tree binfo;
4440 tree base_binfo;
4442 /* If there are no repeated bases, nothing can be ambiguous. */
4443 if (!CLASSTYPE_REPEATED_BASE_P (t))
4444 return;
4446 /* Check direct bases. */
4447 for (binfo = TYPE_BINFO (t), i = 0;
4448 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4450 basetype = BINFO_TYPE (base_binfo);
4452 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
4453 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
4454 basetype, t);
4457 /* Check for ambiguous virtual bases. */
4458 if (extra_warnings)
4459 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
4460 VEC_iterate (tree, vbases, i, binfo); i++)
4462 basetype = BINFO_TYPE (binfo);
4464 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
4465 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity",
4466 basetype, t);
4470 /* Compare two INTEGER_CSTs K1 and K2. */
4472 static int
4473 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4475 return tree_int_cst_compare ((tree) k1, (tree) k2);
4478 /* Increase the size indicated in RLI to account for empty classes
4479 that are "off the end" of the class. */
4481 static void
4482 include_empty_classes (record_layout_info rli)
4484 tree eoc;
4485 tree rli_size;
4487 /* It might be the case that we grew the class to allocate a
4488 zero-sized base class. That won't be reflected in RLI, yet,
4489 because we are willing to overlay multiple bases at the same
4490 offset. However, now we need to make sure that RLI is big enough
4491 to reflect the entire class. */
4492 eoc = end_of_class (rli->t,
4493 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4494 rli_size = rli_size_unit_so_far (rli);
4495 if (TREE_CODE (rli_size) == INTEGER_CST
4496 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4498 if (!abi_version_at_least (2))
4499 /* In version 1 of the ABI, the size of a class that ends with
4500 a bitfield was not rounded up to a whole multiple of a
4501 byte. Because rli_size_unit_so_far returns only the number
4502 of fully allocated bytes, any extra bits were not included
4503 in the size. */
4504 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
4505 else
4506 /* The size should have been rounded to a whole byte. */
4507 gcc_assert (tree_int_cst_equal
4508 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
4509 rli->bitpos
4510 = size_binop (PLUS_EXPR,
4511 rli->bitpos,
4512 size_binop (MULT_EXPR,
4513 convert (bitsizetype,
4514 size_binop (MINUS_EXPR,
4515 eoc, rli_size)),
4516 bitsize_int (BITS_PER_UNIT)));
4517 normalize_rli (rli);
4521 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4522 BINFO_OFFSETs for all of the base-classes. Position the vtable
4523 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4525 static void
4526 layout_class_type (tree t, tree *virtuals_p)
4528 tree non_static_data_members;
4529 tree field;
4530 tree vptr;
4531 record_layout_info rli;
4532 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4533 types that appear at that offset. */
4534 splay_tree empty_base_offsets;
4535 /* True if the last field layed out was a bit-field. */
4536 bool last_field_was_bitfield = false;
4537 /* The location at which the next field should be inserted. */
4538 tree *next_field;
4539 /* T, as a base class. */
4540 tree base_t;
4542 /* Keep track of the first non-static data member. */
4543 non_static_data_members = TYPE_FIELDS (t);
4545 /* Start laying out the record. */
4546 rli = start_record_layout (t);
4548 /* Mark all the primary bases in the hierarchy. */
4549 determine_primary_bases (t);
4551 /* Create a pointer to our virtual function table. */
4552 vptr = create_vtable_ptr (t, virtuals_p);
4554 /* The vptr is always the first thing in the class. */
4555 if (vptr)
4557 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4558 TYPE_FIELDS (t) = vptr;
4559 next_field = &TREE_CHAIN (vptr);
4560 place_field (rli, vptr);
4562 else
4563 next_field = &TYPE_FIELDS (t);
4565 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4566 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4567 NULL, NULL);
4568 build_base_fields (rli, empty_base_offsets, next_field);
4570 /* Layout the non-static data members. */
4571 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4573 tree type;
4574 tree padding;
4576 /* We still pass things that aren't non-static data members to
4577 the back-end, in case it wants to do something with them. */
4578 if (TREE_CODE (field) != FIELD_DECL)
4580 place_field (rli, field);
4581 /* If the static data member has incomplete type, keep track
4582 of it so that it can be completed later. (The handling
4583 of pending statics in finish_record_layout is
4584 insufficient; consider:
4586 struct S1;
4587 struct S2 { static S1 s1; };
4589 At this point, finish_record_layout will be called, but
4590 S1 is still incomplete.) */
4591 if (TREE_CODE (field) == VAR_DECL)
4593 maybe_register_incomplete_var (field);
4594 /* The visibility of static data members is determined
4595 at their point of declaration, not their point of
4596 definition. */
4597 determine_visibility (field);
4599 continue;
4602 type = TREE_TYPE (field);
4603 if (type == error_mark_node)
4604 continue;
4606 padding = NULL_TREE;
4608 /* If this field is a bit-field whose width is greater than its
4609 type, then there are some special rules for allocating
4610 it. */
4611 if (DECL_C_BIT_FIELD (field)
4612 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4614 integer_type_kind itk;
4615 tree integer_type;
4616 bool was_unnamed_p = false;
4617 /* We must allocate the bits as if suitably aligned for the
4618 longest integer type that fits in this many bits. type
4619 of the field. Then, we are supposed to use the left over
4620 bits as additional padding. */
4621 for (itk = itk_char; itk != itk_none; ++itk)
4622 if (INT_CST_LT (DECL_SIZE (field),
4623 TYPE_SIZE (integer_types[itk])))
4624 break;
4626 /* ITK now indicates a type that is too large for the
4627 field. We have to back up by one to find the largest
4628 type that fits. */
4629 integer_type = integer_types[itk - 1];
4631 /* Figure out how much additional padding is required. GCC
4632 3.2 always created a padding field, even if it had zero
4633 width. */
4634 if (!abi_version_at_least (2)
4635 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
4637 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4638 /* In a union, the padding field must have the full width
4639 of the bit-field; all fields start at offset zero. */
4640 padding = DECL_SIZE (field);
4641 else
4643 if (TREE_CODE (t) == UNION_TYPE)
4644 warning (OPT_Wabi, "size assigned to %qT may not be "
4645 "ABI-compliant and may change in a future "
4646 "version of GCC",
4648 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4649 TYPE_SIZE (integer_type));
4652 #ifdef PCC_BITFIELD_TYPE_MATTERS
4653 /* An unnamed bitfield does not normally affect the
4654 alignment of the containing class on a target where
4655 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
4656 make any exceptions for unnamed bitfields when the
4657 bitfields are longer than their types. Therefore, we
4658 temporarily give the field a name. */
4659 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
4661 was_unnamed_p = true;
4662 DECL_NAME (field) = make_anon_name ();
4664 #endif
4665 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4666 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4667 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4668 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4669 empty_base_offsets);
4670 if (was_unnamed_p)
4671 DECL_NAME (field) = NULL_TREE;
4672 /* Now that layout has been performed, set the size of the
4673 field to the size of its declared type; the rest of the
4674 field is effectively invisible. */
4675 DECL_SIZE (field) = TYPE_SIZE (type);
4676 /* We must also reset the DECL_MODE of the field. */
4677 if (abi_version_at_least (2))
4678 DECL_MODE (field) = TYPE_MODE (type);
4679 else if (warn_abi
4680 && DECL_MODE (field) != TYPE_MODE (type))
4681 /* Versions of G++ before G++ 3.4 did not reset the
4682 DECL_MODE. */
4683 warning (OPT_Wabi,
4684 "the offset of %qD may not be ABI-compliant and may "
4685 "change in a future version of GCC", field);
4687 else
4688 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4689 empty_base_offsets);
4691 /* Remember the location of any empty classes in FIELD. */
4692 if (abi_version_at_least (2))
4693 record_subobject_offsets (TREE_TYPE (field),
4694 byte_position(field),
4695 empty_base_offsets,
4696 /*is_data_member=*/true);
4698 /* If a bit-field does not immediately follow another bit-field,
4699 and yet it starts in the middle of a byte, we have failed to
4700 comply with the ABI. */
4701 if (warn_abi
4702 && DECL_C_BIT_FIELD (field)
4703 /* The TREE_NO_WARNING flag gets set by Objective-C when
4704 laying out an Objective-C class. The ObjC ABI differs
4705 from the C++ ABI, and so we do not want a warning
4706 here. */
4707 && !TREE_NO_WARNING (field)
4708 && !last_field_was_bitfield
4709 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4710 DECL_FIELD_BIT_OFFSET (field),
4711 bitsize_unit_node)))
4712 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
4713 "change in a future version of GCC", field);
4715 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
4716 offset of the field. */
4717 if (warn_abi
4718 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
4719 byte_position (field))
4720 && contains_empty_class_p (TREE_TYPE (field)))
4721 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
4722 "classes to be placed at different locations in a "
4723 "future version of GCC", field);
4725 /* The middle end uses the type of expressions to determine the
4726 possible range of expression values. In order to optimize
4727 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
4728 must be made aware of the width of "i", via its type.
4730 Because C++ does not have integer types of arbitrary width,
4731 we must (for the purposes of the front end) convert from the
4732 type assigned here to the declared type of the bitfield
4733 whenever a bitfield expression is used as an rvalue.
4734 Similarly, when assigning a value to a bitfield, the value
4735 must be converted to the type given the bitfield here. */
4736 if (DECL_C_BIT_FIELD (field))
4738 tree ftype;
4739 unsigned HOST_WIDE_INT width;
4740 ftype = TREE_TYPE (field);
4741 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
4742 if (width != TYPE_PRECISION (ftype))
4743 TREE_TYPE (field)
4744 = c_build_bitfield_integer_type (width,
4745 TYPE_UNSIGNED (ftype));
4748 /* If we needed additional padding after this field, add it
4749 now. */
4750 if (padding)
4752 tree padding_field;
4754 padding_field = build_decl (FIELD_DECL,
4755 NULL_TREE,
4756 char_type_node);
4757 DECL_BIT_FIELD (padding_field) = 1;
4758 DECL_SIZE (padding_field) = padding;
4759 DECL_CONTEXT (padding_field) = t;
4760 DECL_ARTIFICIAL (padding_field) = 1;
4761 DECL_IGNORED_P (padding_field) = 1;
4762 layout_nonempty_base_or_field (rli, padding_field,
4763 NULL_TREE,
4764 empty_base_offsets);
4767 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4770 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
4772 /* Make sure that we are on a byte boundary so that the size of
4773 the class without virtual bases will always be a round number
4774 of bytes. */
4775 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4776 normalize_rli (rli);
4779 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
4780 padding. */
4781 if (!abi_version_at_least (2))
4782 include_empty_classes(rli);
4784 /* Delete all zero-width bit-fields from the list of fields. Now
4785 that the type is laid out they are no longer important. */
4786 remove_zero_width_bit_fields (t);
4788 /* Create the version of T used for virtual bases. We do not use
4789 make_aggr_type for this version; this is an artificial type. For
4790 a POD type, we just reuse T. */
4791 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
4793 base_t = make_node (TREE_CODE (t));
4795 /* Set the size and alignment for the new type. In G++ 3.2, all
4796 empty classes were considered to have size zero when used as
4797 base classes. */
4798 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
4800 TYPE_SIZE (base_t) = bitsize_zero_node;
4801 TYPE_SIZE_UNIT (base_t) = size_zero_node;
4802 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
4803 warning (OPT_Wabi,
4804 "layout of classes derived from empty class %qT "
4805 "may change in a future version of GCC",
4808 else
4810 tree eoc;
4812 /* If the ABI version is not at least two, and the last
4813 field was a bit-field, RLI may not be on a byte
4814 boundary. In particular, rli_size_unit_so_far might
4815 indicate the last complete byte, while rli_size_so_far
4816 indicates the total number of bits used. Therefore,
4817 rli_size_so_far, rather than rli_size_unit_so_far, is
4818 used to compute TYPE_SIZE_UNIT. */
4819 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4820 TYPE_SIZE_UNIT (base_t)
4821 = size_binop (MAX_EXPR,
4822 convert (sizetype,
4823 size_binop (CEIL_DIV_EXPR,
4824 rli_size_so_far (rli),
4825 bitsize_int (BITS_PER_UNIT))),
4826 eoc);
4827 TYPE_SIZE (base_t)
4828 = size_binop (MAX_EXPR,
4829 rli_size_so_far (rli),
4830 size_binop (MULT_EXPR,
4831 convert (bitsizetype, eoc),
4832 bitsize_int (BITS_PER_UNIT)));
4834 TYPE_ALIGN (base_t) = rli->record_align;
4835 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
4837 /* Copy the fields from T. */
4838 next_field = &TYPE_FIELDS (base_t);
4839 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4840 if (TREE_CODE (field) == FIELD_DECL)
4842 *next_field = build_decl (FIELD_DECL,
4843 DECL_NAME (field),
4844 TREE_TYPE (field));
4845 DECL_CONTEXT (*next_field) = base_t;
4846 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
4847 DECL_FIELD_BIT_OFFSET (*next_field)
4848 = DECL_FIELD_BIT_OFFSET (field);
4849 DECL_SIZE (*next_field) = DECL_SIZE (field);
4850 DECL_MODE (*next_field) = DECL_MODE (field);
4851 next_field = &TREE_CHAIN (*next_field);
4854 /* Record the base version of the type. */
4855 CLASSTYPE_AS_BASE (t) = base_t;
4856 TYPE_CONTEXT (base_t) = t;
4858 else
4859 CLASSTYPE_AS_BASE (t) = t;
4861 /* Every empty class contains an empty class. */
4862 if (CLASSTYPE_EMPTY_P (t))
4863 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4865 /* Set the TYPE_DECL for this type to contain the right
4866 value for DECL_OFFSET, so that we can use it as part
4867 of a COMPONENT_REF for multiple inheritance. */
4868 layout_decl (TYPE_MAIN_DECL (t), 0);
4870 /* Now fix up any virtual base class types that we left lying
4871 around. We must get these done before we try to lay out the
4872 virtual function table. As a side-effect, this will remove the
4873 base subobject fields. */
4874 layout_virtual_bases (rli, empty_base_offsets);
4876 /* Make sure that empty classes are reflected in RLI at this
4877 point. */
4878 include_empty_classes(rli);
4880 /* Make sure not to create any structures with zero size. */
4881 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
4882 place_field (rli,
4883 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
4885 /* Let the back-end lay out the type. */
4886 finish_record_layout (rli, /*free_p=*/true);
4888 /* Warn about bases that can't be talked about due to ambiguity. */
4889 warn_about_ambiguous_bases (t);
4891 /* Now that we're done with layout, give the base fields the real types. */
4892 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4893 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
4894 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
4896 /* Clean up. */
4897 splay_tree_delete (empty_base_offsets);
4899 if (CLASSTYPE_EMPTY_P (t)
4900 && tree_int_cst_lt (sizeof_biggest_empty_class,
4901 TYPE_SIZE_UNIT (t)))
4902 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
4905 /* Determine the "key method" for the class type indicated by TYPE,
4906 and set CLASSTYPE_KEY_METHOD accordingly. */
4908 void
4909 determine_key_method (tree type)
4911 tree method;
4913 if (TYPE_FOR_JAVA (type)
4914 || processing_template_decl
4915 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
4916 || CLASSTYPE_INTERFACE_KNOWN (type))
4917 return;
4919 /* The key method is the first non-pure virtual function that is not
4920 inline at the point of class definition. On some targets the
4921 key function may not be inline; those targets should not call
4922 this function until the end of the translation unit. */
4923 for (method = TYPE_METHODS (type); method != NULL_TREE;
4924 method = TREE_CHAIN (method))
4925 if (DECL_VINDEX (method) != NULL_TREE
4926 && ! DECL_DECLARED_INLINE_P (method)
4927 && ! DECL_PURE_VIRTUAL_P (method))
4929 CLASSTYPE_KEY_METHOD (type) = method;
4930 break;
4933 return;
4936 /* Perform processing required when the definition of T (a class type)
4937 is complete. */
4939 void
4940 finish_struct_1 (tree t)
4942 tree x;
4943 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
4944 tree virtuals = NULL_TREE;
4945 int n_fields = 0;
4947 if (COMPLETE_TYPE_P (t))
4949 gcc_assert (IS_AGGR_TYPE (t));
4950 error ("redefinition of %q#T", t);
4951 popclass ();
4952 return;
4955 /* If this type was previously laid out as a forward reference,
4956 make sure we lay it out again. */
4957 TYPE_SIZE (t) = NULL_TREE;
4958 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
4960 fixup_inline_methods (t);
4962 /* Make assumptions about the class; we'll reset the flags if
4963 necessary. */
4964 CLASSTYPE_EMPTY_P (t) = 1;
4965 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4966 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
4968 /* Do end-of-class semantic processing: checking the validity of the
4969 bases and members and add implicitly generated methods. */
4970 check_bases_and_members (t);
4972 /* Find the key method. */
4973 if (TYPE_CONTAINS_VPTR_P (t))
4975 /* The Itanium C++ ABI permits the key method to be chosen when
4976 the class is defined -- even though the key method so
4977 selected may later turn out to be an inline function. On
4978 some systems (such as ARM Symbian OS) the key method cannot
4979 be determined until the end of the translation unit. On such
4980 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
4981 will cause the class to be added to KEYED_CLASSES. Then, in
4982 finish_file we will determine the key method. */
4983 if (targetm.cxx.key_method_may_be_inline ())
4984 determine_key_method (t);
4986 /* If a polymorphic class has no key method, we may emit the vtable
4987 in every translation unit where the class definition appears. */
4988 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
4989 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
4992 /* Layout the class itself. */
4993 layout_class_type (t, &virtuals);
4994 if (CLASSTYPE_AS_BASE (t) != t)
4995 /* We use the base type for trivial assignments, and hence it
4996 needs a mode. */
4997 compute_record_mode (CLASSTYPE_AS_BASE (t));
4999 virtuals = modify_all_vtables (t, nreverse (virtuals));
5001 /* If necessary, create the primary vtable for this class. */
5002 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5004 /* We must enter these virtuals into the table. */
5005 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5006 build_primary_vtable (NULL_TREE, t);
5007 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5008 /* Here we know enough to change the type of our virtual
5009 function table, but we will wait until later this function. */
5010 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5013 if (TYPE_CONTAINS_VPTR_P (t))
5015 int vindex;
5016 tree fn;
5018 if (BINFO_VTABLE (TYPE_BINFO (t)))
5019 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
5020 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5021 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
5023 /* Add entries for virtual functions introduced by this class. */
5024 BINFO_VIRTUALS (TYPE_BINFO (t))
5025 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
5027 /* Set DECL_VINDEX for all functions declared in this class. */
5028 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5030 fn = TREE_CHAIN (fn),
5031 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5032 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5034 tree fndecl = BV_FN (fn);
5036 if (DECL_THUNK_P (fndecl))
5037 /* A thunk. We should never be calling this entry directly
5038 from this vtable -- we'd use the entry for the non
5039 thunk base function. */
5040 DECL_VINDEX (fndecl) = NULL_TREE;
5041 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5042 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
5046 finish_struct_bits (t);
5048 /* Complete the rtl for any static member objects of the type we're
5049 working on. */
5050 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5051 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5052 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5053 DECL_MODE (x) = TYPE_MODE (t);
5055 /* Done with FIELDS...now decide whether to sort these for
5056 faster lookups later.
5058 We use a small number because most searches fail (succeeding
5059 ultimately as the search bores through the inheritance
5060 hierarchy), and we want this failure to occur quickly. */
5062 n_fields = count_fields (TYPE_FIELDS (t));
5063 if (n_fields > 7)
5065 struct sorted_fields_type *field_vec = GGC_NEWVAR
5066 (struct sorted_fields_type,
5067 sizeof (struct sorted_fields_type) + n_fields * sizeof (tree));
5068 field_vec->len = n_fields;
5069 add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
5070 qsort (field_vec->elts, n_fields, sizeof (tree),
5071 field_decl_cmp);
5072 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5073 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5074 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5077 /* Complain if one of the field types requires lower visibility. */
5078 constrain_class_visibility (t);
5080 /* Make the rtl for any new vtables we have created, and unmark
5081 the base types we marked. */
5082 finish_vtbls (t);
5084 /* Build the VTT for T. */
5085 build_vtt (t);
5087 /* This warning does not make sense for Java classes, since they
5088 cannot have destructors. */
5089 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
5091 tree dtor;
5093 dtor = CLASSTYPE_DESTRUCTORS (t);
5094 /* Warn only if the dtor is non-private or the class has
5095 friends. */
5096 if (/* An implicitly declared destructor is always public. And,
5097 if it were virtual, we would have created it by now. */
5098 !dtor
5099 || (!DECL_VINDEX (dtor)
5100 && (!TREE_PRIVATE (dtor)
5101 || CLASSTYPE_FRIEND_CLASSES (t)
5102 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))
5103 warning (0, "%q#T has virtual functions but non-virtual destructor",
5107 complete_vars (t);
5109 if (warn_overloaded_virtual)
5110 warn_hidden (t);
5112 /* Class layout, assignment of virtual table slots, etc., is now
5113 complete. Give the back end a chance to tweak the visibility of
5114 the class or perform any other required target modifications. */
5115 targetm.cxx.adjust_class_at_definition (t);
5117 maybe_suppress_debug_info (t);
5119 dump_class_hierarchy (t);
5121 /* Finish debugging output for this type. */
5122 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5125 /* When T was built up, the member declarations were added in reverse
5126 order. Rearrange them to declaration order. */
5128 void
5129 unreverse_member_declarations (tree t)
5131 tree next;
5132 tree prev;
5133 tree x;
5135 /* The following lists are all in reverse order. Put them in
5136 declaration order now. */
5137 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5138 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5140 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5141 reverse order, so we can't just use nreverse. */
5142 prev = NULL_TREE;
5143 for (x = TYPE_FIELDS (t);
5144 x && TREE_CODE (x) != TYPE_DECL;
5145 x = next)
5147 next = TREE_CHAIN (x);
5148 TREE_CHAIN (x) = prev;
5149 prev = x;
5151 if (prev)
5153 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5154 if (prev)
5155 TYPE_FIELDS (t) = prev;
5159 tree
5160 finish_struct (tree t, tree attributes)
5162 location_t saved_loc = input_location;
5164 /* Now that we've got all the field declarations, reverse everything
5165 as necessary. */
5166 unreverse_member_declarations (t);
5168 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5170 /* Nadger the current location so that diagnostics point to the start of
5171 the struct, not the end. */
5172 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
5174 if (processing_template_decl)
5176 tree x;
5178 finish_struct_methods (t);
5179 TYPE_SIZE (t) = bitsize_zero_node;
5180 TYPE_SIZE_UNIT (t) = size_zero_node;
5182 /* We need to emit an error message if this type was used as a parameter
5183 and it is an abstract type, even if it is a template. We construct
5184 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
5185 account and we call complete_vars with this type, which will check
5186 the PARM_DECLS. Note that while the type is being defined,
5187 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
5188 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
5189 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
5190 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
5191 if (DECL_PURE_VIRTUAL_P (x))
5192 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
5193 complete_vars (t);
5195 else
5196 finish_struct_1 (t);
5198 input_location = saved_loc;
5200 TYPE_BEING_DEFINED (t) = 0;
5202 if (current_class_type)
5203 popclass ();
5204 else
5205 error ("trying to finish struct, but kicked out due to previous parse errors");
5207 if (processing_template_decl && at_function_scope_p ())
5208 add_stmt (build_min (TAG_DEFN, t));
5210 return t;
5213 /* Return the dynamic type of INSTANCE, if known.
5214 Used to determine whether the virtual function table is needed
5215 or not.
5217 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5218 of our knowledge of its type. *NONNULL should be initialized
5219 before this function is called. */
5221 static tree
5222 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5224 switch (TREE_CODE (instance))
5226 case INDIRECT_REF:
5227 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5228 return NULL_TREE;
5229 else
5230 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5231 nonnull, cdtorp);
5233 case CALL_EXPR:
5234 /* This is a call to a constructor, hence it's never zero. */
5235 if (TREE_HAS_CONSTRUCTOR (instance))
5237 if (nonnull)
5238 *nonnull = 1;
5239 return TREE_TYPE (instance);
5241 return NULL_TREE;
5243 case SAVE_EXPR:
5244 /* This is a call to a constructor, hence it's never zero. */
5245 if (TREE_HAS_CONSTRUCTOR (instance))
5247 if (nonnull)
5248 *nonnull = 1;
5249 return TREE_TYPE (instance);
5251 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5253 case PLUS_EXPR:
5254 case MINUS_EXPR:
5255 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5256 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5257 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5258 /* Propagate nonnull. */
5259 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5260 return NULL_TREE;
5262 case NOP_EXPR:
5263 case CONVERT_EXPR:
5264 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5266 case ADDR_EXPR:
5267 instance = TREE_OPERAND (instance, 0);
5268 if (nonnull)
5270 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
5271 with a real object -- given &p->f, p can still be null. */
5272 tree t = get_base_address (instance);
5273 /* ??? Probably should check DECL_WEAK here. */
5274 if (t && DECL_P (t))
5275 *nonnull = 1;
5277 return fixed_type_or_null (instance, nonnull, cdtorp);
5279 case COMPONENT_REF:
5280 /* If this component is really a base class reference, then the field
5281 itself isn't definitive. */
5282 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
5283 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5284 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5286 case VAR_DECL:
5287 case FIELD_DECL:
5288 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5289 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5291 if (nonnull)
5292 *nonnull = 1;
5293 return TREE_TYPE (TREE_TYPE (instance));
5295 /* fall through... */
5296 case TARGET_EXPR:
5297 case PARM_DECL:
5298 case RESULT_DECL:
5299 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5301 if (nonnull)
5302 *nonnull = 1;
5303 return TREE_TYPE (instance);
5305 else if (instance == current_class_ptr)
5307 if (nonnull)
5308 *nonnull = 1;
5310 /* if we're in a ctor or dtor, we know our type. */
5311 if (DECL_LANG_SPECIFIC (current_function_decl)
5312 && (DECL_CONSTRUCTOR_P (current_function_decl)
5313 || DECL_DESTRUCTOR_P (current_function_decl)))
5315 if (cdtorp)
5316 *cdtorp = 1;
5317 return TREE_TYPE (TREE_TYPE (instance));
5320 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5322 /* Reference variables should be references to objects. */
5323 if (nonnull)
5324 *nonnull = 1;
5326 /* DECL_VAR_MARKED_P is used to prevent recursion; a
5327 variable's initializer may refer to the variable
5328 itself. */
5329 if (TREE_CODE (instance) == VAR_DECL
5330 && DECL_INITIAL (instance)
5331 && !DECL_VAR_MARKED_P (instance))
5333 tree type;
5334 DECL_VAR_MARKED_P (instance) = 1;
5335 type = fixed_type_or_null (DECL_INITIAL (instance),
5336 nonnull, cdtorp);
5337 DECL_VAR_MARKED_P (instance) = 0;
5338 return type;
5341 return NULL_TREE;
5343 default:
5344 return NULL_TREE;
5348 /* Return nonzero if the dynamic type of INSTANCE is known, and
5349 equivalent to the static type. We also handle the case where
5350 INSTANCE is really a pointer. Return negative if this is a
5351 ctor/dtor. There the dynamic type is known, but this might not be
5352 the most derived base of the original object, and hence virtual
5353 bases may not be layed out according to this type.
5355 Used to determine whether the virtual function table is needed
5356 or not.
5358 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5359 of our knowledge of its type. *NONNULL should be initialized
5360 before this function is called. */
5363 resolves_to_fixed_type_p (tree instance, int* nonnull)
5365 tree t = TREE_TYPE (instance);
5366 int cdtorp = 0;
5368 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5369 if (fixed == NULL_TREE)
5370 return 0;
5371 if (POINTER_TYPE_P (t))
5372 t = TREE_TYPE (t);
5373 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5374 return 0;
5375 return cdtorp ? -1 : 1;
5379 void
5380 init_class_processing (void)
5382 current_class_depth = 0;
5383 current_class_stack_size = 10;
5384 current_class_stack
5385 = XNEWVEC (struct class_stack_node, current_class_stack_size);
5386 local_classes = VEC_alloc (tree, gc, 8);
5387 sizeof_biggest_empty_class = size_zero_node;
5389 ridpointers[(int) RID_PUBLIC] = access_public_node;
5390 ridpointers[(int) RID_PRIVATE] = access_private_node;
5391 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5394 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
5396 static void
5397 restore_class_cache (void)
5399 tree type;
5401 /* We are re-entering the same class we just left, so we don't
5402 have to search the whole inheritance matrix to find all the
5403 decls to bind again. Instead, we install the cached
5404 class_shadowed list and walk through it binding names. */
5405 push_binding_level (previous_class_level);
5406 class_binding_level = previous_class_level;
5407 /* Restore IDENTIFIER_TYPE_VALUE. */
5408 for (type = class_binding_level->type_shadowed;
5409 type;
5410 type = TREE_CHAIN (type))
5411 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
5414 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5415 appropriate for TYPE.
5417 So that we may avoid calls to lookup_name, we cache the _TYPE
5418 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5420 For multiple inheritance, we perform a two-pass depth-first search
5421 of the type lattice. */
5423 void
5424 pushclass (tree type)
5426 class_stack_node_t csn;
5428 type = TYPE_MAIN_VARIANT (type);
5430 /* Make sure there is enough room for the new entry on the stack. */
5431 if (current_class_depth + 1 >= current_class_stack_size)
5433 current_class_stack_size *= 2;
5434 current_class_stack
5435 = XRESIZEVEC (struct class_stack_node, current_class_stack,
5436 current_class_stack_size);
5439 /* Insert a new entry on the class stack. */
5440 csn = current_class_stack + current_class_depth;
5441 csn->name = current_class_name;
5442 csn->type = current_class_type;
5443 csn->access = current_access_specifier;
5444 csn->names_used = 0;
5445 csn->hidden = 0;
5446 current_class_depth++;
5448 /* Now set up the new type. */
5449 current_class_name = TYPE_NAME (type);
5450 if (TREE_CODE (current_class_name) == TYPE_DECL)
5451 current_class_name = DECL_NAME (current_class_name);
5452 current_class_type = type;
5454 /* By default, things in classes are private, while things in
5455 structures or unions are public. */
5456 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5457 ? access_private_node
5458 : access_public_node);
5460 if (previous_class_level
5461 && type != previous_class_level->this_entity
5462 && current_class_depth == 1)
5464 /* Forcibly remove any old class remnants. */
5465 invalidate_class_lookup_cache ();
5468 if (!previous_class_level
5469 || type != previous_class_level->this_entity
5470 || current_class_depth > 1)
5471 pushlevel_class ();
5472 else
5473 restore_class_cache ();
5476 /* When we exit a toplevel class scope, we save its binding level so
5477 that we can restore it quickly. Here, we've entered some other
5478 class, so we must invalidate our cache. */
5480 void
5481 invalidate_class_lookup_cache (void)
5483 previous_class_level = NULL;
5486 /* Get out of the current class scope. If we were in a class scope
5487 previously, that is the one popped to. */
5489 void
5490 popclass (void)
5492 poplevel_class ();
5494 current_class_depth--;
5495 current_class_name = current_class_stack[current_class_depth].name;
5496 current_class_type = current_class_stack[current_class_depth].type;
5497 current_access_specifier = current_class_stack[current_class_depth].access;
5498 if (current_class_stack[current_class_depth].names_used)
5499 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5502 /* Mark the top of the class stack as hidden. */
5504 void
5505 push_class_stack (void)
5507 if (current_class_depth)
5508 ++current_class_stack[current_class_depth - 1].hidden;
5511 /* Mark the top of the class stack as un-hidden. */
5513 void
5514 pop_class_stack (void)
5516 if (current_class_depth)
5517 --current_class_stack[current_class_depth - 1].hidden;
5520 /* Returns 1 if the class type currently being defined is either T or
5521 a nested type of T. */
5523 bool
5524 currently_open_class (tree t)
5526 int i;
5528 /* We start looking from 1 because entry 0 is from global scope,
5529 and has no type. */
5530 for (i = current_class_depth; i > 0; --i)
5532 tree c;
5533 if (i == current_class_depth)
5534 c = current_class_type;
5535 else
5537 if (current_class_stack[i].hidden)
5538 break;
5539 c = current_class_stack[i].type;
5541 if (!c)
5542 continue;
5543 if (same_type_p (c, t))
5544 return true;
5546 return false;
5549 /* If either current_class_type or one of its enclosing classes are derived
5550 from T, return the appropriate type. Used to determine how we found
5551 something via unqualified lookup. */
5553 tree
5554 currently_open_derived_class (tree t)
5556 int i;
5558 /* The bases of a dependent type are unknown. */
5559 if (dependent_type_p (t))
5560 return NULL_TREE;
5562 if (!current_class_type)
5563 return NULL_TREE;
5565 if (DERIVED_FROM_P (t, current_class_type))
5566 return current_class_type;
5568 for (i = current_class_depth - 1; i > 0; --i)
5570 if (current_class_stack[i].hidden)
5571 break;
5572 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5573 return current_class_stack[i].type;
5576 return NULL_TREE;
5579 /* When entering a class scope, all enclosing class scopes' names with
5580 static meaning (static variables, static functions, types and
5581 enumerators) have to be visible. This recursive function calls
5582 pushclass for all enclosing class contexts until global or a local
5583 scope is reached. TYPE is the enclosed class. */
5585 void
5586 push_nested_class (tree type)
5588 tree context;
5590 /* A namespace might be passed in error cases, like A::B:C. */
5591 if (type == NULL_TREE
5592 || type == error_mark_node
5593 || TREE_CODE (type) == NAMESPACE_DECL
5594 || ! IS_AGGR_TYPE (type)
5595 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5596 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5597 return;
5599 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5601 if (context && CLASS_TYPE_P (context))
5602 push_nested_class (context);
5603 pushclass (type);
5606 /* Undoes a push_nested_class call. */
5608 void
5609 pop_nested_class (void)
5611 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5613 popclass ();
5614 if (context && CLASS_TYPE_P (context))
5615 pop_nested_class ();
5618 /* Returns the number of extern "LANG" blocks we are nested within. */
5621 current_lang_depth (void)
5623 return VEC_length (tree, current_lang_base);
5626 /* Set global variables CURRENT_LANG_NAME to appropriate value
5627 so that behavior of name-mangling machinery is correct. */
5629 void
5630 push_lang_context (tree name)
5632 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
5634 if (name == lang_name_cplusplus)
5636 current_lang_name = name;
5638 else if (name == lang_name_java)
5640 current_lang_name = name;
5641 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5642 (See record_builtin_java_type in decl.c.) However, that causes
5643 incorrect debug entries if these types are actually used.
5644 So we re-enable debug output after extern "Java". */
5645 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5646 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5647 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5648 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5649 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5650 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5651 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5652 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5654 else if (name == lang_name_c)
5656 current_lang_name = name;
5658 else
5659 error ("language string %<\"%E\"%> not recognized", name);
5662 /* Get out of the current language scope. */
5664 void
5665 pop_lang_context (void)
5667 current_lang_name = VEC_pop (tree, current_lang_base);
5670 /* Type instantiation routines. */
5672 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5673 matches the TARGET_TYPE. If there is no satisfactory match, return
5674 error_mark_node, and issue an error & warning messages under
5675 control of FLAGS. Permit pointers to member function if FLAGS
5676 permits. If TEMPLATE_ONLY, the name of the overloaded function was
5677 a template-id, and EXPLICIT_TARGS are the explicitly provided
5678 template arguments. If OVERLOAD is for one or more member
5679 functions, then ACCESS_PATH is the base path used to reference
5680 those member functions. */
5682 static tree
5683 resolve_address_of_overloaded_function (tree target_type,
5684 tree overload,
5685 tsubst_flags_t flags,
5686 bool template_only,
5687 tree explicit_targs,
5688 tree access_path)
5690 /* Here's what the standard says:
5692 [over.over]
5694 If the name is a function template, template argument deduction
5695 is done, and if the argument deduction succeeds, the deduced
5696 arguments are used to generate a single template function, which
5697 is added to the set of overloaded functions considered.
5699 Non-member functions and static member functions match targets of
5700 type "pointer-to-function" or "reference-to-function." Nonstatic
5701 member functions match targets of type "pointer-to-member
5702 function;" the function type of the pointer to member is used to
5703 select the member function from the set of overloaded member
5704 functions. If a nonstatic member function is selected, the
5705 reference to the overloaded function name is required to have the
5706 form of a pointer to member as described in 5.3.1.
5708 If more than one function is selected, any template functions in
5709 the set are eliminated if the set also contains a non-template
5710 function, and any given template function is eliminated if the
5711 set contains a second template function that is more specialized
5712 than the first according to the partial ordering rules 14.5.5.2.
5713 After such eliminations, if any, there shall remain exactly one
5714 selected function. */
5716 int is_ptrmem = 0;
5717 int is_reference = 0;
5718 /* We store the matches in a TREE_LIST rooted here. The functions
5719 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5720 interoperability with most_specialized_instantiation. */
5721 tree matches = NULL_TREE;
5722 tree fn;
5724 /* By the time we get here, we should be seeing only real
5725 pointer-to-member types, not the internal POINTER_TYPE to
5726 METHOD_TYPE representation. */
5727 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
5728 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
5730 gcc_assert (is_overloaded_fn (overload));
5732 /* Check that the TARGET_TYPE is reasonable. */
5733 if (TYPE_PTRFN_P (target_type))
5734 /* This is OK. */;
5735 else if (TYPE_PTRMEMFUNC_P (target_type))
5736 /* This is OK, too. */
5737 is_ptrmem = 1;
5738 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5740 /* This is OK, too. This comes from a conversion to reference
5741 type. */
5742 target_type = build_reference_type (target_type);
5743 is_reference = 1;
5745 else
5747 if (flags & tf_error)
5748 error ("cannot resolve overloaded function %qD based on"
5749 " conversion to type %qT",
5750 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5751 return error_mark_node;
5754 /* If we can find a non-template function that matches, we can just
5755 use it. There's no point in generating template instantiations
5756 if we're just going to throw them out anyhow. But, of course, we
5757 can only do this when we don't *need* a template function. */
5758 if (!template_only)
5760 tree fns;
5762 for (fns = overload; fns; fns = OVL_NEXT (fns))
5764 tree fn = OVL_CURRENT (fns);
5765 tree fntype;
5767 if (TREE_CODE (fn) == TEMPLATE_DECL)
5768 /* We're not looking for templates just yet. */
5769 continue;
5771 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5772 != is_ptrmem)
5773 /* We're looking for a non-static member, and this isn't
5774 one, or vice versa. */
5775 continue;
5777 /* Ignore functions which haven't been explicitly
5778 declared. */
5779 if (DECL_ANTICIPATED (fn))
5780 continue;
5782 /* See if there's a match. */
5783 fntype = TREE_TYPE (fn);
5784 if (is_ptrmem)
5785 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5786 else if (!is_reference)
5787 fntype = build_pointer_type (fntype);
5789 if (can_convert_arg (target_type, fntype, fn, LOOKUP_NORMAL))
5790 matches = tree_cons (fn, NULL_TREE, matches);
5794 /* Now, if we've already got a match (or matches), there's no need
5795 to proceed to the template functions. But, if we don't have a
5796 match we need to look at them, too. */
5797 if (!matches)
5799 tree target_fn_type;
5800 tree target_arg_types;
5801 tree target_ret_type;
5802 tree fns;
5804 if (is_ptrmem)
5805 target_fn_type
5806 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5807 else
5808 target_fn_type = TREE_TYPE (target_type);
5809 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5810 target_ret_type = TREE_TYPE (target_fn_type);
5812 /* Never do unification on the 'this' parameter. */
5813 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5814 target_arg_types = TREE_CHAIN (target_arg_types);
5816 for (fns = overload; fns; fns = OVL_NEXT (fns))
5818 tree fn = OVL_CURRENT (fns);
5819 tree instantiation;
5820 tree instantiation_type;
5821 tree targs;
5823 if (TREE_CODE (fn) != TEMPLATE_DECL)
5824 /* We're only looking for templates. */
5825 continue;
5827 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5828 != is_ptrmem)
5829 /* We're not looking for a non-static member, and this is
5830 one, or vice versa. */
5831 continue;
5833 /* Try to do argument deduction. */
5834 targs = make_tree_vec (DECL_NTPARMS (fn));
5835 if (fn_type_unification (fn, explicit_targs, targs,
5836 target_arg_types, target_ret_type,
5837 DEDUCE_EXACT, LOOKUP_NORMAL))
5838 /* Argument deduction failed. */
5839 continue;
5841 /* Instantiate the template. */
5842 instantiation = instantiate_template (fn, targs, flags);
5843 if (instantiation == error_mark_node)
5844 /* Instantiation failed. */
5845 continue;
5847 /* See if there's a match. */
5848 instantiation_type = TREE_TYPE (instantiation);
5849 if (is_ptrmem)
5850 instantiation_type =
5851 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5852 else if (!is_reference)
5853 instantiation_type = build_pointer_type (instantiation_type);
5854 if (can_convert_arg (target_type, instantiation_type, instantiation,
5855 LOOKUP_NORMAL))
5856 matches = tree_cons (instantiation, fn, matches);
5859 /* Now, remove all but the most specialized of the matches. */
5860 if (matches)
5862 tree match = most_specialized_instantiation (matches);
5864 if (match != error_mark_node)
5865 matches = tree_cons (TREE_PURPOSE (match),
5866 NULL_TREE,
5867 NULL_TREE);
5871 /* Now we should have exactly one function in MATCHES. */
5872 if (matches == NULL_TREE)
5874 /* There were *no* matches. */
5875 if (flags & tf_error)
5877 error ("no matches converting function %qD to type %q#T",
5878 DECL_NAME (OVL_FUNCTION (overload)),
5879 target_type);
5881 /* print_candidates expects a chain with the functions in
5882 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5883 so why be clever?). */
5884 for (; overload; overload = OVL_NEXT (overload))
5885 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5886 matches);
5888 print_candidates (matches);
5890 return error_mark_node;
5892 else if (TREE_CHAIN (matches))
5894 /* There were too many matches. */
5896 if (flags & tf_error)
5898 tree match;
5900 error ("converting overloaded function %qD to type %q#T is ambiguous",
5901 DECL_NAME (OVL_FUNCTION (overload)),
5902 target_type);
5904 /* Since print_candidates expects the functions in the
5905 TREE_VALUE slot, we flip them here. */
5906 for (match = matches; match; match = TREE_CHAIN (match))
5907 TREE_VALUE (match) = TREE_PURPOSE (match);
5909 print_candidates (matches);
5912 return error_mark_node;
5915 /* Good, exactly one match. Now, convert it to the correct type. */
5916 fn = TREE_PURPOSE (matches);
5918 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5919 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
5921 static int explained;
5923 if (!(flags & tf_error))
5924 return error_mark_node;
5926 pedwarn ("assuming pointer to member %qD", fn);
5927 if (!explained)
5929 pedwarn ("(a pointer to member can only be formed with %<&%E%>)", fn);
5930 explained = 1;
5934 /* If we're doing overload resolution purely for the purpose of
5935 determining conversion sequences, we should not consider the
5936 function used. If this conversion sequence is selected, the
5937 function will be marked as used at this point. */
5938 if (!(flags & tf_conv))
5940 mark_used (fn);
5941 /* We could not check access when this expression was originally
5942 created since we did not know at that time to which function
5943 the expression referred. */
5944 if (DECL_FUNCTION_MEMBER_P (fn))
5946 gcc_assert (access_path);
5947 perform_or_defer_access_check (access_path, fn);
5951 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5952 return build_unary_op (ADDR_EXPR, fn, 0);
5953 else
5955 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5956 will mark the function as addressed, but here we must do it
5957 explicitly. */
5958 cxx_mark_addressable (fn);
5960 return fn;
5964 /* This function will instantiate the type of the expression given in
5965 RHS to match the type of LHSTYPE. If errors exist, then return
5966 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
5967 we complain on errors. If we are not complaining, never modify rhs,
5968 as overload resolution wants to try many possible instantiations, in
5969 the hope that at least one will work.
5971 For non-recursive calls, LHSTYPE should be a function, pointer to
5972 function, or a pointer to member function. */
5974 tree
5975 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
5977 tsubst_flags_t flags_in = flags;
5978 tree access_path = NULL_TREE;
5980 flags &= ~tf_ptrmem_ok;
5982 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5984 if (flags & tf_error)
5985 error ("not enough type information");
5986 return error_mark_node;
5989 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5991 if (same_type_p (lhstype, TREE_TYPE (rhs)))
5992 return rhs;
5993 if (flag_ms_extensions
5994 && TYPE_PTRMEMFUNC_P (lhstype)
5995 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
5996 /* Microsoft allows `A::f' to be resolved to a
5997 pointer-to-member. */
5999 else
6001 if (flags & tf_error)
6002 error ("argument of type %qT does not match %qT",
6003 TREE_TYPE (rhs), lhstype);
6004 return error_mark_node;
6008 if (TREE_CODE (rhs) == BASELINK)
6010 access_path = BASELINK_ACCESS_BINFO (rhs);
6011 rhs = BASELINK_FUNCTIONS (rhs);
6014 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
6015 deduce any type information. */
6016 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
6018 if (flags & tf_error)
6019 error ("not enough type information");
6020 return error_mark_node;
6023 /* There only a few kinds of expressions that may have a type
6024 dependent on overload resolution. */
6025 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
6026 || TREE_CODE (rhs) == COMPONENT_REF
6027 || TREE_CODE (rhs) == COMPOUND_EXPR
6028 || really_overloaded_fn (rhs));
6030 /* We don't overwrite rhs if it is an overloaded function.
6031 Copying it would destroy the tree link. */
6032 if (TREE_CODE (rhs) != OVERLOAD)
6033 rhs = copy_node (rhs);
6035 /* This should really only be used when attempting to distinguish
6036 what sort of a pointer to function we have. For now, any
6037 arithmetic operation which is not supported on pointers
6038 is rejected as an error. */
6040 switch (TREE_CODE (rhs))
6042 case COMPONENT_REF:
6044 tree member = TREE_OPERAND (rhs, 1);
6046 member = instantiate_type (lhstype, member, flags);
6047 if (member != error_mark_node
6048 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
6049 /* Do not lose object's side effects. */
6050 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
6051 TREE_OPERAND (rhs, 0), member);
6052 return member;
6055 case OFFSET_REF:
6056 rhs = TREE_OPERAND (rhs, 1);
6057 if (BASELINK_P (rhs))
6058 return instantiate_type (lhstype, rhs, flags_in);
6060 /* This can happen if we are forming a pointer-to-member for a
6061 member template. */
6062 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
6064 /* Fall through. */
6066 case TEMPLATE_ID_EXPR:
6068 tree fns = TREE_OPERAND (rhs, 0);
6069 tree args = TREE_OPERAND (rhs, 1);
6071 return
6072 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
6073 /*template_only=*/true,
6074 args, access_path);
6077 case OVERLOAD:
6078 case FUNCTION_DECL:
6079 return
6080 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
6081 /*template_only=*/false,
6082 /*explicit_targs=*/NULL_TREE,
6083 access_path);
6085 case COMPOUND_EXPR:
6086 TREE_OPERAND (rhs, 0)
6087 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6088 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6089 return error_mark_node;
6090 TREE_OPERAND (rhs, 1)
6091 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6092 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6093 return error_mark_node;
6095 TREE_TYPE (rhs) = lhstype;
6096 return rhs;
6098 case ADDR_EXPR:
6100 if (PTRMEM_OK_P (rhs))
6101 flags |= tf_ptrmem_ok;
6103 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6106 case ERROR_MARK:
6107 return error_mark_node;
6109 default:
6110 gcc_unreachable ();
6112 return error_mark_node;
6115 /* Return the name of the virtual function pointer field
6116 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6117 this may have to look back through base types to find the
6118 ultimate field name. (For single inheritance, these could
6119 all be the same name. Who knows for multiple inheritance). */
6121 static tree
6122 get_vfield_name (tree type)
6124 tree binfo, base_binfo;
6125 char *buf;
6127 for (binfo = TYPE_BINFO (type);
6128 BINFO_N_BASE_BINFOS (binfo);
6129 binfo = base_binfo)
6131 base_binfo = BINFO_BASE_BINFO (binfo, 0);
6133 if (BINFO_VIRTUAL_P (base_binfo)
6134 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
6135 break;
6138 type = BINFO_TYPE (binfo);
6139 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6140 + TYPE_NAME_LENGTH (type) + 2);
6141 sprintf (buf, VFIELD_NAME_FORMAT,
6142 IDENTIFIER_POINTER (constructor_name (type)));
6143 return get_identifier (buf);
6146 void
6147 print_class_statistics (void)
6149 #ifdef GATHER_STATISTICS
6150 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6151 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6152 if (n_vtables)
6154 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6155 n_vtables, n_vtable_searches);
6156 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6157 n_vtable_entries, n_vtable_elems);
6159 #endif
6162 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6163 according to [class]:
6164 The class-name is also inserted
6165 into the scope of the class itself. For purposes of access checking,
6166 the inserted class name is treated as if it were a public member name. */
6168 void
6169 build_self_reference (void)
6171 tree name = constructor_name (current_class_type);
6172 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6173 tree saved_cas;
6175 DECL_NONLOCAL (value) = 1;
6176 DECL_CONTEXT (value) = current_class_type;
6177 DECL_ARTIFICIAL (value) = 1;
6178 SET_DECL_SELF_REFERENCE_P (value);
6180 if (processing_template_decl)
6181 value = push_template_decl (value);
6183 saved_cas = current_access_specifier;
6184 current_access_specifier = access_public_node;
6185 finish_member_declaration (value);
6186 current_access_specifier = saved_cas;
6189 /* Returns 1 if TYPE contains only padding bytes. */
6192 is_empty_class (tree type)
6194 if (type == error_mark_node)
6195 return 0;
6197 if (! IS_AGGR_TYPE (type))
6198 return 0;
6200 /* In G++ 3.2, whether or not a class was empty was determined by
6201 looking at its size. */
6202 if (abi_version_at_least (2))
6203 return CLASSTYPE_EMPTY_P (type);
6204 else
6205 return integer_zerop (CLASSTYPE_SIZE (type));
6208 /* Returns true if TYPE contains an empty class. */
6210 static bool
6211 contains_empty_class_p (tree type)
6213 if (is_empty_class (type))
6214 return true;
6215 if (CLASS_TYPE_P (type))
6217 tree field;
6218 tree binfo;
6219 tree base_binfo;
6220 int i;
6222 for (binfo = TYPE_BINFO (type), i = 0;
6223 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6224 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
6225 return true;
6226 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6227 if (TREE_CODE (field) == FIELD_DECL
6228 && !DECL_ARTIFICIAL (field)
6229 && is_empty_class (TREE_TYPE (field)))
6230 return true;
6232 else if (TREE_CODE (type) == ARRAY_TYPE)
6233 return contains_empty_class_p (TREE_TYPE (type));
6234 return false;
6237 /* Note that NAME was looked up while the current class was being
6238 defined and that the result of that lookup was DECL. */
6240 void
6241 maybe_note_name_used_in_class (tree name, tree decl)
6243 splay_tree names_used;
6245 /* If we're not defining a class, there's nothing to do. */
6246 if (!(innermost_scope_kind() == sk_class
6247 && TYPE_BEING_DEFINED (current_class_type)))
6248 return;
6250 /* If there's already a binding for this NAME, then we don't have
6251 anything to worry about. */
6252 if (lookup_member (current_class_type, name,
6253 /*protect=*/0, /*want_type=*/false))
6254 return;
6256 if (!current_class_stack[current_class_depth - 1].names_used)
6257 current_class_stack[current_class_depth - 1].names_used
6258 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6259 names_used = current_class_stack[current_class_depth - 1].names_used;
6261 splay_tree_insert (names_used,
6262 (splay_tree_key) name,
6263 (splay_tree_value) decl);
6266 /* Note that NAME was declared (as DECL) in the current class. Check
6267 to see that the declaration is valid. */
6269 void
6270 note_name_declared_in_class (tree name, tree decl)
6272 splay_tree names_used;
6273 splay_tree_node n;
6275 /* Look to see if we ever used this name. */
6276 names_used
6277 = current_class_stack[current_class_depth - 1].names_used;
6278 if (!names_used)
6279 return;
6281 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6282 if (n)
6284 /* [basic.scope.class]
6286 A name N used in a class S shall refer to the same declaration
6287 in its context and when re-evaluated in the completed scope of
6288 S. */
6289 error ("declaration of %q#D", decl);
6290 error ("changes meaning of %qD from %q+#D",
6291 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
6295 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6296 Secondary vtables are merged with primary vtables; this function
6297 will return the VAR_DECL for the primary vtable. */
6299 tree
6300 get_vtbl_decl_for_binfo (tree binfo)
6302 tree decl;
6304 decl = BINFO_VTABLE (binfo);
6305 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6307 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
6308 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6310 if (decl)
6311 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6312 return decl;
6316 /* Returns the binfo for the primary base of BINFO. If the resulting
6317 BINFO is a virtual base, and it is inherited elsewhere in the
6318 hierarchy, then the returned binfo might not be the primary base of
6319 BINFO in the complete object. Check BINFO_PRIMARY_P or
6320 BINFO_LOST_PRIMARY_P to be sure. */
6322 static tree
6323 get_primary_binfo (tree binfo)
6325 tree primary_base;
6327 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6328 if (!primary_base)
6329 return NULL_TREE;
6331 return copied_binfo (primary_base, binfo);
6334 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6336 static int
6337 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6339 if (!indented_p)
6340 fprintf (stream, "%*s", indent, "");
6341 return 1;
6344 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
6345 INDENT should be zero when called from the top level; it is
6346 incremented recursively. IGO indicates the next expected BINFO in
6347 inheritance graph ordering. */
6349 static tree
6350 dump_class_hierarchy_r (FILE *stream,
6351 int flags,
6352 tree binfo,
6353 tree igo,
6354 int indent)
6356 int indented = 0;
6357 tree base_binfo;
6358 int i;
6360 indented = maybe_indent_hierarchy (stream, indent, 0);
6361 fprintf (stream, "%s (0x%lx) ",
6362 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
6363 (unsigned long) binfo);
6364 if (binfo != igo)
6366 fprintf (stream, "alternative-path\n");
6367 return igo;
6369 igo = TREE_CHAIN (binfo);
6371 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6372 tree_low_cst (BINFO_OFFSET (binfo), 0));
6373 if (is_empty_class (BINFO_TYPE (binfo)))
6374 fprintf (stream, " empty");
6375 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6376 fprintf (stream, " nearly-empty");
6377 if (BINFO_VIRTUAL_P (binfo))
6378 fprintf (stream, " virtual");
6379 fprintf (stream, "\n");
6381 indented = 0;
6382 if (BINFO_PRIMARY_P (binfo))
6384 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6385 fprintf (stream, " primary-for %s (0x%lx)",
6386 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
6387 TFF_PLAIN_IDENTIFIER),
6388 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
6390 if (BINFO_LOST_PRIMARY_P (binfo))
6392 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6393 fprintf (stream, " lost-primary");
6395 if (indented)
6396 fprintf (stream, "\n");
6398 if (!(flags & TDF_SLIM))
6400 int indented = 0;
6402 if (BINFO_SUBVTT_INDEX (binfo))
6404 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6405 fprintf (stream, " subvttidx=%s",
6406 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6407 TFF_PLAIN_IDENTIFIER));
6409 if (BINFO_VPTR_INDEX (binfo))
6411 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6412 fprintf (stream, " vptridx=%s",
6413 expr_as_string (BINFO_VPTR_INDEX (binfo),
6414 TFF_PLAIN_IDENTIFIER));
6416 if (BINFO_VPTR_FIELD (binfo))
6418 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6419 fprintf (stream, " vbaseoffset=%s",
6420 expr_as_string (BINFO_VPTR_FIELD (binfo),
6421 TFF_PLAIN_IDENTIFIER));
6423 if (BINFO_VTABLE (binfo))
6425 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6426 fprintf (stream, " vptr=%s",
6427 expr_as_string (BINFO_VTABLE (binfo),
6428 TFF_PLAIN_IDENTIFIER));
6431 if (indented)
6432 fprintf (stream, "\n");
6435 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6436 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
6438 return igo;
6441 /* Dump the BINFO hierarchy for T. */
6443 static void
6444 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
6446 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6447 fprintf (stream, " size=%lu align=%lu\n",
6448 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6449 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6450 fprintf (stream, " base size=%lu base align=%lu\n",
6451 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
6452 / BITS_PER_UNIT),
6453 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
6454 / BITS_PER_UNIT));
6455 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
6456 fprintf (stream, "\n");
6459 /* Debug interface to hierarchy dumping. */
6461 void
6462 debug_class (tree t)
6464 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
6467 static void
6468 dump_class_hierarchy (tree t)
6470 int flags;
6471 FILE *stream = dump_begin (TDI_class, &flags);
6473 if (stream)
6475 dump_class_hierarchy_1 (stream, flags, t);
6476 dump_end (TDI_class, stream);
6480 static void
6481 dump_array (FILE * stream, tree decl)
6483 tree value;
6484 unsigned HOST_WIDE_INT ix;
6485 HOST_WIDE_INT elt;
6486 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6488 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6489 / BITS_PER_UNIT);
6490 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6491 fprintf (stream, " %s entries",
6492 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6493 TFF_PLAIN_IDENTIFIER));
6494 fprintf (stream, "\n");
6496 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
6497 ix, value)
6498 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6499 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
6502 static void
6503 dump_vtable (tree t, tree binfo, tree vtable)
6505 int flags;
6506 FILE *stream = dump_begin (TDI_class, &flags);
6508 if (!stream)
6509 return;
6511 if (!(flags & TDF_SLIM))
6513 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6515 fprintf (stream, "%s for %s",
6516 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6517 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
6518 if (ctor_vtbl_p)
6520 if (!BINFO_VIRTUAL_P (binfo))
6521 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6522 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6524 fprintf (stream, "\n");
6525 dump_array (stream, vtable);
6526 fprintf (stream, "\n");
6529 dump_end (TDI_class, stream);
6532 static void
6533 dump_vtt (tree t, tree vtt)
6535 int flags;
6536 FILE *stream = dump_begin (TDI_class, &flags);
6538 if (!stream)
6539 return;
6541 if (!(flags & TDF_SLIM))
6543 fprintf (stream, "VTT for %s\n",
6544 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6545 dump_array (stream, vtt);
6546 fprintf (stream, "\n");
6549 dump_end (TDI_class, stream);
6552 /* Dump a function or thunk and its thunkees. */
6554 static void
6555 dump_thunk (FILE *stream, int indent, tree thunk)
6557 static const char spaces[] = " ";
6558 tree name = DECL_NAME (thunk);
6559 tree thunks;
6561 fprintf (stream, "%.*s%p %s %s", indent, spaces,
6562 (void *)thunk,
6563 !DECL_THUNK_P (thunk) ? "function"
6564 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
6565 name ? IDENTIFIER_POINTER (name) : "<unset>");
6566 if (DECL_THUNK_P (thunk))
6568 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
6569 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
6571 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
6572 if (!virtual_adjust)
6573 /*NOP*/;
6574 else if (DECL_THIS_THUNK_P (thunk))
6575 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
6576 tree_low_cst (virtual_adjust, 0));
6577 else
6578 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
6579 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
6580 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
6581 if (THUNK_ALIAS (thunk))
6582 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
6584 fprintf (stream, "\n");
6585 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
6586 dump_thunk (stream, indent + 2, thunks);
6589 /* Dump the thunks for FN. */
6591 void
6592 debug_thunks (tree fn)
6594 dump_thunk (stderr, 0, fn);
6597 /* Virtual function table initialization. */
6599 /* Create all the necessary vtables for T and its base classes. */
6601 static void
6602 finish_vtbls (tree t)
6604 tree list;
6605 tree vbase;
6607 /* We lay out the primary and secondary vtables in one contiguous
6608 vtable. The primary vtable is first, followed by the non-virtual
6609 secondary vtables in inheritance graph order. */
6610 list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (t)), NULL_TREE);
6611 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6612 TYPE_BINFO (t), t, list);
6614 /* Then come the virtual bases, also in inheritance graph order. */
6615 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6617 if (!BINFO_VIRTUAL_P (vbase))
6618 continue;
6619 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
6622 if (BINFO_VTABLE (TYPE_BINFO (t)))
6623 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6626 /* Initialize the vtable for BINFO with the INITS. */
6628 static void
6629 initialize_vtable (tree binfo, tree inits)
6631 tree decl;
6633 layout_vtable_decl (binfo, list_length (inits));
6634 decl = get_vtbl_decl_for_binfo (binfo);
6635 initialize_artificial_var (decl, inits);
6636 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6639 /* Build the VTT (virtual table table) for T.
6640 A class requires a VTT if it has virtual bases.
6642 This holds
6643 1 - primary virtual pointer for complete object T
6644 2 - secondary VTTs for each direct non-virtual base of T which requires a
6646 3 - secondary virtual pointers for each direct or indirect base of T which
6647 has virtual bases or is reachable via a virtual path from T.
6648 4 - secondary VTTs for each direct or indirect virtual base of T.
6650 Secondary VTTs look like complete object VTTs without part 4. */
6652 static void
6653 build_vtt (tree t)
6655 tree inits;
6656 tree type;
6657 tree vtt;
6658 tree index;
6660 /* Build up the initializers for the VTT. */
6661 inits = NULL_TREE;
6662 index = size_zero_node;
6663 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6665 /* If we didn't need a VTT, we're done. */
6666 if (!inits)
6667 return;
6669 /* Figure out the type of the VTT. */
6670 type = build_index_type (size_int (list_length (inits) - 1));
6671 type = build_cplus_array_type (const_ptr_type_node, type);
6673 /* Now, build the VTT object itself. */
6674 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
6675 initialize_artificial_var (vtt, inits);
6676 /* Add the VTT to the vtables list. */
6677 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6678 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
6680 dump_vtt (t, vtt);
6683 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6684 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6685 and CHAIN the vtable pointer for this binfo after construction is
6686 complete. VALUE can also be another BINFO, in which case we recurse. */
6688 static tree
6689 binfo_ctor_vtable (tree binfo)
6691 tree vt;
6693 while (1)
6695 vt = BINFO_VTABLE (binfo);
6696 if (TREE_CODE (vt) == TREE_LIST)
6697 vt = TREE_VALUE (vt);
6698 if (TREE_CODE (vt) == TREE_BINFO)
6699 binfo = vt;
6700 else
6701 break;
6704 return vt;
6707 /* Data for secondary VTT initialization. */
6708 typedef struct secondary_vptr_vtt_init_data_s
6710 /* Is this the primary VTT? */
6711 bool top_level_p;
6713 /* Current index into the VTT. */
6714 tree index;
6716 /* TREE_LIST of initializers built up. */
6717 tree inits;
6719 /* The type being constructed by this secondary VTT. */
6720 tree type_being_constructed;
6721 } secondary_vptr_vtt_init_data;
6723 /* Recursively build the VTT-initializer for BINFO (which is in the
6724 hierarchy dominated by T). INITS points to the end of the initializer
6725 list to date. INDEX is the VTT index where the next element will be
6726 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6727 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6728 for virtual bases of T. When it is not so, we build the constructor
6729 vtables for the BINFO-in-T variant. */
6731 static tree *
6732 build_vtt_inits (tree binfo, tree t, tree *inits, tree *index)
6734 int i;
6735 tree b;
6736 tree init;
6737 tree secondary_vptrs;
6738 secondary_vptr_vtt_init_data data;
6739 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
6741 /* We only need VTTs for subobjects with virtual bases. */
6742 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
6743 return inits;
6745 /* We need to use a construction vtable if this is not the primary
6746 VTT. */
6747 if (!top_level_p)
6749 build_ctor_vtbl_group (binfo, t);
6751 /* Record the offset in the VTT where this sub-VTT can be found. */
6752 BINFO_SUBVTT_INDEX (binfo) = *index;
6755 /* Add the address of the primary vtable for the complete object. */
6756 init = binfo_ctor_vtable (binfo);
6757 *inits = build_tree_list (NULL_TREE, init);
6758 inits = &TREE_CHAIN (*inits);
6759 if (top_level_p)
6761 gcc_assert (!BINFO_VPTR_INDEX (binfo));
6762 BINFO_VPTR_INDEX (binfo) = *index;
6764 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6766 /* Recursively add the secondary VTTs for non-virtual bases. */
6767 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
6768 if (!BINFO_VIRTUAL_P (b))
6769 inits = build_vtt_inits (b, t, inits, index);
6771 /* Add secondary virtual pointers for all subobjects of BINFO with
6772 either virtual bases or reachable along a virtual path, except
6773 subobjects that are non-virtual primary bases. */
6774 data.top_level_p = top_level_p;
6775 data.index = *index;
6776 data.inits = NULL;
6777 data.type_being_constructed = BINFO_TYPE (binfo);
6779 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
6781 *index = data.index;
6783 /* The secondary vptrs come back in reverse order. After we reverse
6784 them, and add the INITS, the last init will be the first element
6785 of the chain. */
6786 secondary_vptrs = data.inits;
6787 if (secondary_vptrs)
6789 *inits = nreverse (secondary_vptrs);
6790 inits = &TREE_CHAIN (secondary_vptrs);
6791 gcc_assert (*inits == NULL_TREE);
6794 if (top_level_p)
6795 /* Add the secondary VTTs for virtual bases in inheritance graph
6796 order. */
6797 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
6799 if (!BINFO_VIRTUAL_P (b))
6800 continue;
6802 inits = build_vtt_inits (b, t, inits, index);
6804 else
6805 /* Remove the ctor vtables we created. */
6806 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
6808 return inits;
6811 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
6812 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
6814 static tree
6815 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
6817 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
6819 /* We don't care about bases that don't have vtables. */
6820 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6821 return dfs_skip_bases;
6823 /* We're only interested in proper subobjects of the type being
6824 constructed. */
6825 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
6826 return NULL_TREE;
6828 /* We're only interested in bases with virtual bases or reachable
6829 via a virtual path from the type being constructed. */
6830 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
6831 || binfo_via_virtual (binfo, data->type_being_constructed)))
6832 return dfs_skip_bases;
6834 /* We're not interested in non-virtual primary bases. */
6835 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
6836 return NULL_TREE;
6838 /* Record the index where this secondary vptr can be found. */
6839 if (data->top_level_p)
6841 gcc_assert (!BINFO_VPTR_INDEX (binfo));
6842 BINFO_VPTR_INDEX (binfo) = data->index;
6844 if (BINFO_VIRTUAL_P (binfo))
6846 /* It's a primary virtual base, and this is not a
6847 construction vtable. Find the base this is primary of in
6848 the inheritance graph, and use that base's vtable
6849 now. */
6850 while (BINFO_PRIMARY_P (binfo))
6851 binfo = BINFO_INHERITANCE_CHAIN (binfo);
6855 /* Add the initializer for the secondary vptr itself. */
6856 data->inits = tree_cons (NULL_TREE, binfo_ctor_vtable (binfo), data->inits);
6858 /* Advance the vtt index. */
6859 data->index = size_binop (PLUS_EXPR, data->index,
6860 TYPE_SIZE_UNIT (ptr_type_node));
6862 return NULL_TREE;
6865 /* Called from build_vtt_inits via dfs_walk. After building
6866 constructor vtables and generating the sub-vtt from them, we need
6867 to restore the BINFO_VTABLES that were scribbled on. DATA is the
6868 binfo of the base whose sub vtt was generated. */
6870 static tree
6871 dfs_fixup_binfo_vtbls (tree binfo, void* data)
6873 tree vtable = BINFO_VTABLE (binfo);
6875 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
6876 /* If this class has no vtable, none of its bases do. */
6877 return dfs_skip_bases;
6879 if (!vtable)
6880 /* This might be a primary base, so have no vtable in this
6881 hierarchy. */
6882 return NULL_TREE;
6884 /* If we scribbled the construction vtable vptr into BINFO, clear it
6885 out now. */
6886 if (TREE_CODE (vtable) == TREE_LIST
6887 && (TREE_PURPOSE (vtable) == (tree) data))
6888 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
6890 return NULL_TREE;
6893 /* Build the construction vtable group for BINFO which is in the
6894 hierarchy dominated by T. */
6896 static void
6897 build_ctor_vtbl_group (tree binfo, tree t)
6899 tree list;
6900 tree type;
6901 tree vtbl;
6902 tree inits;
6903 tree id;
6904 tree vbase;
6906 /* See if we've already created this construction vtable group. */
6907 id = mangle_ctor_vtbl_for_type (t, binfo);
6908 if (IDENTIFIER_GLOBAL_VALUE (id))
6909 return;
6911 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
6912 /* Build a version of VTBL (with the wrong type) for use in
6913 constructing the addresses of secondary vtables in the
6914 construction vtable group. */
6915 vtbl = build_vtable (t, id, ptr_type_node);
6916 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
6917 list = build_tree_list (vtbl, NULL_TREE);
6918 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
6919 binfo, t, list);
6921 /* Add the vtables for each of our virtual bases using the vbase in T
6922 binfo. */
6923 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
6924 vbase;
6925 vbase = TREE_CHAIN (vbase))
6927 tree b;
6929 if (!BINFO_VIRTUAL_P (vbase))
6930 continue;
6931 b = copied_binfo (vbase, binfo);
6933 accumulate_vtbl_inits (b, vbase, binfo, t, list);
6935 inits = TREE_VALUE (list);
6937 /* Figure out the type of the construction vtable. */
6938 type = build_index_type (size_int (list_length (inits) - 1));
6939 type = build_cplus_array_type (vtable_entry_type, type);
6940 TREE_TYPE (vtbl) = type;
6942 /* Initialize the construction vtable. */
6943 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
6944 initialize_artificial_var (vtbl, inits);
6945 dump_vtable (t, binfo, vtbl);
6948 /* Add the vtbl initializers for BINFO (and its bases other than
6949 non-virtual primaries) to the list of INITS. BINFO is in the
6950 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
6951 the constructor the vtbl inits should be accumulated for. (If this
6952 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
6953 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
6954 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
6955 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
6956 but are not necessarily the same in terms of layout. */
6958 static void
6959 accumulate_vtbl_inits (tree binfo,
6960 tree orig_binfo,
6961 tree rtti_binfo,
6962 tree t,
6963 tree inits)
6965 int i;
6966 tree base_binfo;
6967 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
6969 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
6971 /* If it doesn't have a vptr, we don't do anything. */
6972 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
6973 return;
6975 /* If we're building a construction vtable, we're not interested in
6976 subobjects that don't require construction vtables. */
6977 if (ctor_vtbl_p
6978 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
6979 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
6980 return;
6982 /* Build the initializers for the BINFO-in-T vtable. */
6983 TREE_VALUE (inits)
6984 = chainon (TREE_VALUE (inits),
6985 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
6986 rtti_binfo, t, inits));
6988 /* Walk the BINFO and its bases. We walk in preorder so that as we
6989 initialize each vtable we can figure out at what offset the
6990 secondary vtable lies from the primary vtable. We can't use
6991 dfs_walk here because we need to iterate through bases of BINFO
6992 and RTTI_BINFO simultaneously. */
6993 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6995 /* Skip virtual bases. */
6996 if (BINFO_VIRTUAL_P (base_binfo))
6997 continue;
6998 accumulate_vtbl_inits (base_binfo,
6999 BINFO_BASE_BINFO (orig_binfo, i),
7000 rtti_binfo, t,
7001 inits);
7005 /* Called from accumulate_vtbl_inits. Returns the initializers for
7006 the BINFO vtable. */
7008 static tree
7009 dfs_accumulate_vtbl_inits (tree binfo,
7010 tree orig_binfo,
7011 tree rtti_binfo,
7012 tree t,
7013 tree l)
7015 tree inits = NULL_TREE;
7016 tree vtbl = NULL_TREE;
7017 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
7019 if (ctor_vtbl_p
7020 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7022 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7023 primary virtual base. If it is not the same primary in
7024 the hierarchy of T, we'll need to generate a ctor vtable
7025 for it, to place at its location in T. If it is the same
7026 primary, we still need a VTT entry for the vtable, but it
7027 should point to the ctor vtable for the base it is a
7028 primary for within the sub-hierarchy of RTTI_BINFO.
7030 There are three possible cases:
7032 1) We are in the same place.
7033 2) We are a primary base within a lost primary virtual base of
7034 RTTI_BINFO.
7035 3) We are primary to something not a base of RTTI_BINFO. */
7037 tree b;
7038 tree last = NULL_TREE;
7040 /* First, look through the bases we are primary to for RTTI_BINFO
7041 or a virtual base. */
7042 b = binfo;
7043 while (BINFO_PRIMARY_P (b))
7045 b = BINFO_INHERITANCE_CHAIN (b);
7046 last = b;
7047 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7048 goto found;
7050 /* If we run out of primary links, keep looking down our
7051 inheritance chain; we might be an indirect primary. */
7052 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7053 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7054 break;
7055 found:
7057 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7058 base B and it is a base of RTTI_BINFO, this is case 2. In
7059 either case, we share our vtable with LAST, i.e. the
7060 derived-most base within B of which we are a primary. */
7061 if (b == rtti_binfo
7062 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
7063 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7064 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7065 binfo_ctor_vtable after everything's been set up. */
7066 vtbl = last;
7068 /* Otherwise, this is case 3 and we get our own. */
7070 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
7071 return inits;
7073 if (!vtbl)
7075 tree index;
7076 int non_fn_entries;
7078 /* Compute the initializer for this vtable. */
7079 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7080 &non_fn_entries);
7082 /* Figure out the position to which the VPTR should point. */
7083 vtbl = TREE_PURPOSE (l);
7084 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl);
7085 index = size_binop (PLUS_EXPR,
7086 size_int (non_fn_entries),
7087 size_int (list_length (TREE_VALUE (l))));
7088 index = size_binop (MULT_EXPR,
7089 TYPE_SIZE_UNIT (vtable_entry_type),
7090 index);
7091 vtbl = build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7094 if (ctor_vtbl_p)
7095 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7096 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7097 straighten this out. */
7098 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7099 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
7100 inits = NULL_TREE;
7101 else
7102 /* For an ordinary vtable, set BINFO_VTABLE. */
7103 BINFO_VTABLE (binfo) = vtbl;
7105 return inits;
7108 static GTY(()) tree abort_fndecl_addr;
7110 /* Construct the initializer for BINFO's virtual function table. BINFO
7111 is part of the hierarchy dominated by T. If we're building a
7112 construction vtable, the ORIG_BINFO is the binfo we should use to
7113 find the actual function pointers to put in the vtable - but they
7114 can be overridden on the path to most-derived in the graph that
7115 ORIG_BINFO belongs. Otherwise,
7116 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7117 BINFO that should be indicated by the RTTI information in the
7118 vtable; it will be a base class of T, rather than T itself, if we
7119 are building a construction vtable.
7121 The value returned is a TREE_LIST suitable for wrapping in a
7122 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7123 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7124 number of non-function entries in the vtable.
7126 It might seem that this function should never be called with a
7127 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7128 base is always subsumed by a derived class vtable. However, when
7129 we are building construction vtables, we do build vtables for
7130 primary bases; we need these while the primary base is being
7131 constructed. */
7133 static tree
7134 build_vtbl_initializer (tree binfo,
7135 tree orig_binfo,
7136 tree t,
7137 tree rtti_binfo,
7138 int* non_fn_entries_p)
7140 tree v, b;
7141 tree vfun_inits;
7142 vtbl_init_data vid;
7143 unsigned ix;
7144 tree vbinfo;
7145 VEC(tree,gc) *vbases;
7147 /* Initialize VID. */
7148 memset (&vid, 0, sizeof (vid));
7149 vid.binfo = binfo;
7150 vid.derived = t;
7151 vid.rtti_binfo = rtti_binfo;
7152 vid.last_init = &vid.inits;
7153 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7154 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
7155 vid.generate_vcall_entries = true;
7156 /* The first vbase or vcall offset is at index -3 in the vtable. */
7157 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7159 /* Add entries to the vtable for RTTI. */
7160 build_rtti_vtbl_entries (binfo, &vid);
7162 /* Create an array for keeping track of the functions we've
7163 processed. When we see multiple functions with the same
7164 signature, we share the vcall offsets. */
7165 vid.fns = VEC_alloc (tree, gc, 32);
7166 /* Add the vcall and vbase offset entries. */
7167 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7169 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7170 build_vbase_offset_vtbl_entries. */
7171 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
7172 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
7173 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
7175 /* If the target requires padding between data entries, add that now. */
7176 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7178 tree cur, *prev;
7180 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7182 tree add = cur;
7183 int i;
7185 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7186 add = tree_cons (NULL_TREE,
7187 build1 (NOP_EXPR, vtable_entry_type,
7188 null_pointer_node),
7189 add);
7190 *prev = add;
7194 if (non_fn_entries_p)
7195 *non_fn_entries_p = list_length (vid.inits);
7197 /* Go through all the ordinary virtual functions, building up
7198 initializers. */
7199 vfun_inits = NULL_TREE;
7200 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7202 tree delta;
7203 tree vcall_index;
7204 tree fn, fn_original;
7205 tree init = NULL_TREE;
7207 fn = BV_FN (v);
7208 fn_original = fn;
7209 if (DECL_THUNK_P (fn))
7211 if (!DECL_NAME (fn))
7212 finish_thunk (fn);
7213 if (THUNK_ALIAS (fn))
7215 fn = THUNK_ALIAS (fn);
7216 BV_FN (v) = fn;
7218 fn_original = THUNK_TARGET (fn);
7221 /* If the only definition of this function signature along our
7222 primary base chain is from a lost primary, this vtable slot will
7223 never be used, so just zero it out. This is important to avoid
7224 requiring extra thunks which cannot be generated with the function.
7226 We first check this in update_vtable_entry_for_fn, so we handle
7227 restored primary bases properly; we also need to do it here so we
7228 zero out unused slots in ctor vtables, rather than filling themff
7229 with erroneous values (though harmless, apart from relocation
7230 costs). */
7231 for (b = binfo; ; b = get_primary_binfo (b))
7233 /* We found a defn before a lost primary; go ahead as normal. */
7234 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7235 break;
7237 /* The nearest definition is from a lost primary; clear the
7238 slot. */
7239 if (BINFO_LOST_PRIMARY_P (b))
7241 init = size_zero_node;
7242 break;
7246 if (! init)
7248 /* Pull the offset for `this', and the function to call, out of
7249 the list. */
7250 delta = BV_DELTA (v);
7251 vcall_index = BV_VCALL_INDEX (v);
7253 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
7254 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7256 /* You can't call an abstract virtual function; it's abstract.
7257 So, we replace these functions with __pure_virtual. */
7258 if (DECL_PURE_VIRTUAL_P (fn_original))
7260 fn = abort_fndecl;
7261 if (abort_fndecl_addr == NULL)
7262 abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7263 init = abort_fndecl_addr;
7265 else
7267 if (!integer_zerop (delta) || vcall_index)
7269 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7270 if (!DECL_NAME (fn))
7271 finish_thunk (fn);
7273 /* Take the address of the function, considering it to be of an
7274 appropriate generic type. */
7275 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7279 /* And add it to the chain of initializers. */
7280 if (TARGET_VTABLE_USES_DESCRIPTORS)
7282 int i;
7283 if (init == size_zero_node)
7284 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7285 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7286 else
7287 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7289 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
7290 TREE_OPERAND (init, 0),
7291 build_int_cst (NULL_TREE, i));
7292 TREE_CONSTANT (fdesc) = 1;
7293 TREE_INVARIANT (fdesc) = 1;
7295 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7298 else
7299 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7302 /* The initializers for virtual functions were built up in reverse
7303 order; straighten them out now. */
7304 vfun_inits = nreverse (vfun_inits);
7306 /* The negative offset initializers are also in reverse order. */
7307 vid.inits = nreverse (vid.inits);
7309 /* Chain the two together. */
7310 return chainon (vid.inits, vfun_inits);
7313 /* Adds to vid->inits the initializers for the vbase and vcall
7314 offsets in BINFO, which is in the hierarchy dominated by T. */
7316 static void
7317 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7319 tree b;
7321 /* If this is a derived class, we must first create entries
7322 corresponding to the primary base class. */
7323 b = get_primary_binfo (binfo);
7324 if (b)
7325 build_vcall_and_vbase_vtbl_entries (b, vid);
7327 /* Add the vbase entries for this base. */
7328 build_vbase_offset_vtbl_entries (binfo, vid);
7329 /* Add the vcall entries for this base. */
7330 build_vcall_offset_vtbl_entries (binfo, vid);
7333 /* Returns the initializers for the vbase offset entries in the vtable
7334 for BINFO (which is part of the class hierarchy dominated by T), in
7335 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7336 where the next vbase offset will go. */
7338 static void
7339 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7341 tree vbase;
7342 tree t;
7343 tree non_primary_binfo;
7345 /* If there are no virtual baseclasses, then there is nothing to
7346 do. */
7347 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7348 return;
7350 t = vid->derived;
7352 /* We might be a primary base class. Go up the inheritance hierarchy
7353 until we find the most derived class of which we are a primary base:
7354 it is the offset of that which we need to use. */
7355 non_primary_binfo = binfo;
7356 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7358 tree b;
7360 /* If we have reached a virtual base, then it must be a primary
7361 base (possibly multi-level) of vid->binfo, or we wouldn't
7362 have called build_vcall_and_vbase_vtbl_entries for it. But it
7363 might be a lost primary, so just skip down to vid->binfo. */
7364 if (BINFO_VIRTUAL_P (non_primary_binfo))
7366 non_primary_binfo = vid->binfo;
7367 break;
7370 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7371 if (get_primary_binfo (b) != non_primary_binfo)
7372 break;
7373 non_primary_binfo = b;
7376 /* Go through the virtual bases, adding the offsets. */
7377 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7378 vbase;
7379 vbase = TREE_CHAIN (vbase))
7381 tree b;
7382 tree delta;
7384 if (!BINFO_VIRTUAL_P (vbase))
7385 continue;
7387 /* Find the instance of this virtual base in the complete
7388 object. */
7389 b = copied_binfo (vbase, binfo);
7391 /* If we've already got an offset for this virtual base, we
7392 don't need another one. */
7393 if (BINFO_VTABLE_PATH_MARKED (b))
7394 continue;
7395 BINFO_VTABLE_PATH_MARKED (b) = 1;
7397 /* Figure out where we can find this vbase offset. */
7398 delta = size_binop (MULT_EXPR,
7399 vid->index,
7400 convert (ssizetype,
7401 TYPE_SIZE_UNIT (vtable_entry_type)));
7402 if (vid->primary_vtbl_p)
7403 BINFO_VPTR_FIELD (b) = delta;
7405 if (binfo != TYPE_BINFO (t))
7406 /* The vbase offset had better be the same. */
7407 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
7409 /* The next vbase will come at a more negative offset. */
7410 vid->index = size_binop (MINUS_EXPR, vid->index,
7411 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7413 /* The initializer is the delta from BINFO to this virtual base.
7414 The vbase offsets go in reverse inheritance-graph order, and
7415 we are walking in inheritance graph order so these end up in
7416 the right order. */
7417 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7419 *vid->last_init
7420 = build_tree_list (NULL_TREE,
7421 fold_build1 (NOP_EXPR,
7422 vtable_entry_type,
7423 delta));
7424 vid->last_init = &TREE_CHAIN (*vid->last_init);
7428 /* Adds the initializers for the vcall offset entries in the vtable
7429 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7430 to VID->INITS. */
7432 static void
7433 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7435 /* We only need these entries if this base is a virtual base. We
7436 compute the indices -- but do not add to the vtable -- when
7437 building the main vtable for a class. */
7438 if (BINFO_VIRTUAL_P (binfo) || binfo == TYPE_BINFO (vid->derived))
7440 /* We need a vcall offset for each of the virtual functions in this
7441 vtable. For example:
7443 class A { virtual void f (); };
7444 class B1 : virtual public A { virtual void f (); };
7445 class B2 : virtual public A { virtual void f (); };
7446 class C: public B1, public B2 { virtual void f (); };
7448 A C object has a primary base of B1, which has a primary base of A. A
7449 C also has a secondary base of B2, which no longer has a primary base
7450 of A. So the B2-in-C construction vtable needs a secondary vtable for
7451 A, which will adjust the A* to a B2* to call f. We have no way of
7452 knowing what (or even whether) this offset will be when we define B2,
7453 so we store this "vcall offset" in the A sub-vtable and look it up in
7454 a "virtual thunk" for B2::f.
7456 We need entries for all the functions in our primary vtable and
7457 in our non-virtual bases' secondary vtables. */
7458 vid->vbase = binfo;
7459 /* If we are just computing the vcall indices -- but do not need
7460 the actual entries -- not that. */
7461 if (!BINFO_VIRTUAL_P (binfo))
7462 vid->generate_vcall_entries = false;
7463 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7464 add_vcall_offset_vtbl_entries_r (binfo, vid);
7468 /* Build vcall offsets, starting with those for BINFO. */
7470 static void
7471 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
7473 int i;
7474 tree primary_binfo;
7475 tree base_binfo;
7477 /* Don't walk into virtual bases -- except, of course, for the
7478 virtual base for which we are building vcall offsets. Any
7479 primary virtual base will have already had its offsets generated
7480 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7481 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
7482 return;
7484 /* If BINFO has a primary base, process it first. */
7485 primary_binfo = get_primary_binfo (binfo);
7486 if (primary_binfo)
7487 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7489 /* Add BINFO itself to the list. */
7490 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7492 /* Scan the non-primary bases of BINFO. */
7493 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7494 if (base_binfo != primary_binfo)
7495 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7498 /* Called from build_vcall_offset_vtbl_entries_r. */
7500 static void
7501 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
7503 /* Make entries for the rest of the virtuals. */
7504 if (abi_version_at_least (2))
7506 tree orig_fn;
7508 /* The ABI requires that the methods be processed in declaration
7509 order. G++ 3.2 used the order in the vtable. */
7510 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
7511 orig_fn;
7512 orig_fn = TREE_CHAIN (orig_fn))
7513 if (DECL_VINDEX (orig_fn))
7514 add_vcall_offset (orig_fn, binfo, vid);
7516 else
7518 tree derived_virtuals;
7519 tree base_virtuals;
7520 tree orig_virtuals;
7521 /* If BINFO is a primary base, the most derived class which has
7522 BINFO as a primary base; otherwise, just BINFO. */
7523 tree non_primary_binfo;
7525 /* We might be a primary base class. Go up the inheritance hierarchy
7526 until we find the most derived class of which we are a primary base:
7527 it is the BINFO_VIRTUALS there that we need to consider. */
7528 non_primary_binfo = binfo;
7529 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7531 tree b;
7533 /* If we have reached a virtual base, then it must be vid->vbase,
7534 because we ignore other virtual bases in
7535 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7536 base (possibly multi-level) of vid->binfo, or we wouldn't
7537 have called build_vcall_and_vbase_vtbl_entries for it. But it
7538 might be a lost primary, so just skip down to vid->binfo. */
7539 if (BINFO_VIRTUAL_P (non_primary_binfo))
7541 gcc_assert (non_primary_binfo == vid->vbase);
7542 non_primary_binfo = vid->binfo;
7543 break;
7546 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7547 if (get_primary_binfo (b) != non_primary_binfo)
7548 break;
7549 non_primary_binfo = b;
7552 if (vid->ctor_vtbl_p)
7553 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7554 where rtti_binfo is the most derived type. */
7555 non_primary_binfo
7556 = original_binfo (non_primary_binfo, vid->rtti_binfo);
7558 for (base_virtuals = BINFO_VIRTUALS (binfo),
7559 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7560 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7561 base_virtuals;
7562 base_virtuals = TREE_CHAIN (base_virtuals),
7563 derived_virtuals = TREE_CHAIN (derived_virtuals),
7564 orig_virtuals = TREE_CHAIN (orig_virtuals))
7566 tree orig_fn;
7568 /* Find the declaration that originally caused this function to
7569 be present in BINFO_TYPE (binfo). */
7570 orig_fn = BV_FN (orig_virtuals);
7572 /* When processing BINFO, we only want to generate vcall slots for
7573 function slots introduced in BINFO. So don't try to generate
7574 one if the function isn't even defined in BINFO. */
7575 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
7576 continue;
7578 add_vcall_offset (orig_fn, binfo, vid);
7583 /* Add a vcall offset entry for ORIG_FN to the vtable. */
7585 static void
7586 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
7588 size_t i;
7589 tree vcall_offset;
7590 tree derived_entry;
7592 /* If there is already an entry for a function with the same
7593 signature as FN, then we do not need a second vcall offset.
7594 Check the list of functions already present in the derived
7595 class vtable. */
7596 for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i)
7598 if (same_signature_p (derived_entry, orig_fn)
7599 /* We only use one vcall offset for virtual destructors,
7600 even though there are two virtual table entries. */
7601 || (DECL_DESTRUCTOR_P (derived_entry)
7602 && DECL_DESTRUCTOR_P (orig_fn)))
7603 return;
7606 /* If we are building these vcall offsets as part of building
7607 the vtable for the most derived class, remember the vcall
7608 offset. */
7609 if (vid->binfo == TYPE_BINFO (vid->derived))
7611 tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
7612 CLASSTYPE_VCALL_INDICES (vid->derived),
7613 NULL);
7614 elt->purpose = orig_fn;
7615 elt->value = vid->index;
7618 /* The next vcall offset will be found at a more negative
7619 offset. */
7620 vid->index = size_binop (MINUS_EXPR, vid->index,
7621 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7623 /* Keep track of this function. */
7624 VEC_safe_push (tree, gc, vid->fns, orig_fn);
7626 if (vid->generate_vcall_entries)
7628 tree base;
7629 tree fn;
7631 /* Find the overriding function. */
7632 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
7633 if (fn == error_mark_node)
7634 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
7635 integer_zero_node);
7636 else
7638 base = TREE_VALUE (fn);
7640 /* The vbase we're working on is a primary base of
7641 vid->binfo. But it might be a lost primary, so its
7642 BINFO_OFFSET might be wrong, so we just use the
7643 BINFO_OFFSET from vid->binfo. */
7644 vcall_offset = size_diffop (BINFO_OFFSET (base),
7645 BINFO_OFFSET (vid->binfo));
7646 vcall_offset = fold_build1 (NOP_EXPR, vtable_entry_type,
7647 vcall_offset);
7649 /* Add the initializer to the vtable. */
7650 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7651 vid->last_init = &TREE_CHAIN (*vid->last_init);
7655 /* Return vtbl initializers for the RTTI entries corresponding to the
7656 BINFO's vtable. The RTTI entries should indicate the object given
7657 by VID->rtti_binfo. */
7659 static void
7660 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
7662 tree b;
7663 tree t;
7664 tree basetype;
7665 tree offset;
7666 tree decl;
7667 tree init;
7669 basetype = BINFO_TYPE (binfo);
7670 t = BINFO_TYPE (vid->rtti_binfo);
7672 /* To find the complete object, we will first convert to our most
7673 primary base, and then add the offset in the vtbl to that value. */
7674 b = binfo;
7675 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7676 && !BINFO_LOST_PRIMARY_P (b))
7678 tree primary_base;
7680 primary_base = get_primary_binfo (b);
7681 gcc_assert (BINFO_PRIMARY_P (primary_base)
7682 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
7683 b = primary_base;
7685 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7687 /* The second entry is the address of the typeinfo object. */
7688 if (flag_rtti)
7689 decl = build_address (get_tinfo_decl (t));
7690 else
7691 decl = integer_zero_node;
7693 /* Convert the declaration to a type that can be stored in the
7694 vtable. */
7695 init = build_nop (vfunc_ptr_type_node, decl);
7696 *vid->last_init = build_tree_list (NULL_TREE, init);
7697 vid->last_init = &TREE_CHAIN (*vid->last_init);
7699 /* Add the offset-to-top entry. It comes earlier in the vtable than
7700 the typeinfo entry. Convert the offset to look like a
7701 function pointer, so that we can put it in the vtable. */
7702 init = build_nop (vfunc_ptr_type_node, offset);
7703 *vid->last_init = build_tree_list (NULL_TREE, init);
7704 vid->last_init = &TREE_CHAIN (*vid->last_init);
7707 /* Fold a OBJ_TYPE_REF expression to the address of a function.
7708 KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */
7710 tree
7711 cp_fold_obj_type_ref (tree ref, tree known_type)
7713 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
7714 HOST_WIDE_INT i = 0;
7715 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
7716 tree fndecl;
7718 while (i != index)
7720 i += (TARGET_VTABLE_USES_DESCRIPTORS
7721 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
7722 v = TREE_CHAIN (v);
7725 fndecl = BV_FN (v);
7727 #ifdef ENABLE_CHECKING
7728 gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref),
7729 DECL_VINDEX (fndecl)));
7730 #endif
7732 cgraph_node (fndecl)->local.vtable_method = true;
7734 return build_address (fndecl);
7737 #include "gt-cp-class.h"