re PR rtl-optimization/53589 (ICE in maybe_record_trace_start with asm goto)
[official-gcc.git] / gcc / cp / class.c
blob4bb9dd2c1684a627fe58e3024195717190ab4efc
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, 2007, 2008, 2009, 2010, 2011,
4 2012
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "target.h"
36 #include "convert.h"
37 #include "cgraph.h"
38 #include "tree-dump.h"
39 #include "splay-tree.h"
40 #include "pointer-set.h"
42 /* The number of nested classes being processed. If we are not in the
43 scope of any class, this is zero. */
45 int current_class_depth;
47 /* In order to deal with nested classes, we keep a stack of classes.
48 The topmost entry is the innermost class, and is the entry at index
49 CURRENT_CLASS_DEPTH */
51 typedef struct class_stack_node {
52 /* The name of the class. */
53 tree name;
55 /* The _TYPE node for the class. */
56 tree type;
58 /* The access specifier pending for new declarations in the scope of
59 this class. */
60 tree access;
62 /* If were defining TYPE, the names used in this class. */
63 splay_tree names_used;
65 /* Nonzero if this class is no longer open, because of a call to
66 push_to_top_level. */
67 size_t hidden;
68 }* class_stack_node_t;
70 typedef struct vtbl_init_data_s
72 /* The base for which we're building initializers. */
73 tree binfo;
74 /* The type of the most-derived type. */
75 tree derived;
76 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
77 unless ctor_vtbl_p is true. */
78 tree rtti_binfo;
79 /* The negative-index vtable initializers built up so far. These
80 are in order from least negative index to most negative index. */
81 VEC(constructor_elt,gc) *inits;
82 /* The binfo for the virtual base for which we're building
83 vcall offset initializers. */
84 tree vbase;
85 /* The functions in vbase for which we have already provided vcall
86 offsets. */
87 VEC(tree,gc) *fns;
88 /* The vtable index of the next vcall or vbase offset. */
89 tree index;
90 /* Nonzero if we are building the initializer for the primary
91 vtable. */
92 int primary_vtbl_p;
93 /* Nonzero if we are building the initializer for a construction
94 vtable. */
95 int ctor_vtbl_p;
96 /* True when adding vcall offset entries to the vtable. False when
97 merely computing the indices. */
98 bool generate_vcall_entries;
99 } vtbl_init_data;
101 /* The type of a function passed to walk_subobject_offsets. */
102 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
104 /* The stack itself. This is a dynamically resized array. The
105 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
106 static int current_class_stack_size;
107 static class_stack_node_t current_class_stack;
109 /* The size of the largest empty class seen in this translation unit. */
110 static GTY (()) tree sizeof_biggest_empty_class;
112 /* An array of all local classes present in this translation unit, in
113 declaration order. */
114 VEC(tree,gc) *local_classes;
116 static tree get_vfield_name (tree);
117 static void finish_struct_anon (tree);
118 static tree get_vtable_name (tree);
119 static tree get_basefndecls (tree, tree);
120 static int build_primary_vtable (tree, tree);
121 static int build_secondary_vtable (tree);
122 static void finish_vtbls (tree);
123 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
124 static void finish_struct_bits (tree);
125 static int alter_access (tree, tree, tree);
126 static void handle_using_decl (tree, tree);
127 static tree dfs_modify_vtables (tree, void *);
128 static tree modify_all_vtables (tree, tree);
129 static void determine_primary_bases (tree);
130 static void finish_struct_methods (tree);
131 static void maybe_warn_about_overly_private_class (tree);
132 static int method_name_cmp (const void *, const void *);
133 static int resort_method_name_cmp (const void *, const void *);
134 static void add_implicitly_declared_members (tree, int, int);
135 static tree fixed_type_or_null (tree, int *, int *);
136 static tree build_simple_base_path (tree expr, tree binfo);
137 static tree build_vtbl_ref_1 (tree, tree);
138 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
139 VEC(constructor_elt,gc) **);
140 static int count_fields (tree);
141 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
142 static void insert_into_classtype_sorted_fields (tree, tree, int);
143 static bool check_bitfield_decl (tree);
144 static void check_field_decl (tree, tree, int *, int *, int *);
145 static void check_field_decls (tree, tree *, int *, int *);
146 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
147 static void build_base_fields (record_layout_info, splay_tree, tree *);
148 static void check_methods (tree);
149 static void remove_zero_width_bit_fields (tree);
150 static void check_bases (tree, int *, int *);
151 static void check_bases_and_members (tree);
152 static tree create_vtable_ptr (tree, tree *);
153 static void include_empty_classes (record_layout_info);
154 static void layout_class_type (tree, tree *);
155 static void propagate_binfo_offsets (tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
160 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset (tree, tree, vtbl_init_data *);
162 static void layout_vtable_decl (tree, int);
163 static tree dfs_find_final_overrider_pre (tree, void *);
164 static tree dfs_find_final_overrider_post (tree, void *);
165 static tree find_final_overrider (tree, tree, tree);
166 static int make_new_vtable (tree, tree);
167 static tree get_primary_binfo (tree);
168 static int maybe_indent_hierarchy (FILE *, int, int);
169 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
170 static void dump_class_hierarchy (tree);
171 static void dump_class_hierarchy_1 (FILE *, int, tree);
172 static void dump_array (FILE *, tree);
173 static void dump_vtable (tree, tree, tree);
174 static void dump_vtt (tree, tree);
175 static void dump_thunk (FILE *, int, tree);
176 static tree build_vtable (tree, tree, tree);
177 static void initialize_vtable (tree, VEC(constructor_elt,gc) *);
178 static void layout_nonempty_base_or_field (record_layout_info,
179 tree, tree, splay_tree);
180 static tree end_of_class (tree, int);
181 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
182 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
183 VEC(constructor_elt,gc) **);
184 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
185 VEC(constructor_elt,gc) **);
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 void build_vtt_inits (tree, tree, VEC(constructor_elt,gc) **, 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,
240 tsubst_flags_t complain)
242 tree v_binfo = NULL_TREE;
243 tree d_binfo = NULL_TREE;
244 tree probe;
245 tree offset;
246 tree target_type;
247 tree null_test = NULL;
248 tree ptr_target_type;
249 int fixed_type_p;
250 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
251 bool has_empty = false;
252 bool virtual_access;
254 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
255 return error_mark_node;
257 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
259 d_binfo = probe;
260 if (is_empty_class (BINFO_TYPE (probe)))
261 has_empty = true;
262 if (!v_binfo && BINFO_VIRTUAL_P (probe))
263 v_binfo = probe;
266 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
267 if (want_pointer)
268 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
270 if (code == PLUS_EXPR
271 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
273 /* This can happen when adjust_result_of_qualified_name_lookup can't
274 find a unique base binfo in a call to a member function. We
275 couldn't give the diagnostic then since we might have been calling
276 a static member function, so we do it now. */
277 if (complain & tf_error)
279 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
280 ba_unique, NULL);
281 gcc_assert (base == error_mark_node);
283 return error_mark_node;
286 gcc_assert ((code == MINUS_EXPR
287 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
288 || code == PLUS_EXPR);
290 if (binfo == d_binfo)
291 /* Nothing to do. */
292 return expr;
294 if (code == MINUS_EXPR && v_binfo)
296 if (complain & tf_error)
297 error ("cannot convert from base %qT to derived type %qT via "
298 "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
299 BINFO_TYPE (v_binfo));
300 return error_mark_node;
303 if (!want_pointer)
304 /* This must happen before the call to save_expr. */
305 expr = cp_build_addr_expr (expr, complain);
306 else
307 expr = mark_rvalue_use (expr);
309 offset = BINFO_OFFSET (binfo);
310 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
311 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
312 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
313 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
314 expression returned matches the input. */
315 target_type = cp_build_qualified_type
316 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
317 ptr_target_type = build_pointer_type (target_type);
319 /* Do we need to look in the vtable for the real offset? */
320 virtual_access = (v_binfo && fixed_type_p <= 0);
322 /* Don't bother with the calculations inside sizeof; they'll ICE if the
323 source type is incomplete and the pointer value doesn't matter. In a
324 template (even in fold_non_dependent_expr), we don't have vtables set
325 up properly yet, and the value doesn't matter there either; we're just
326 interested in the result of overload resolution. */
327 if (cp_unevaluated_operand != 0
328 || (current_function_decl
329 && uses_template_parms (current_function_decl)))
331 expr = build_nop (ptr_target_type, expr);
332 if (!want_pointer)
333 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
334 return expr;
337 /* If we're in an NSDMI, we don't have the full constructor context yet
338 that we need for converting to a virtual base, so just build a stub
339 CONVERT_EXPR and expand it later in bot_replace. */
340 if (virtual_access && fixed_type_p < 0
341 && current_scope () != current_function_decl)
343 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
344 CONVERT_EXPR_VBASE_PATH (expr) = true;
345 if (!want_pointer)
346 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
347 return expr;
350 /* Do we need to check for a null pointer? */
351 if (want_pointer && !nonnull)
353 /* If we know the conversion will not actually change the value
354 of EXPR, then we can avoid testing the expression for NULL.
355 We have to avoid generating a COMPONENT_REF for a base class
356 field, because other parts of the compiler know that such
357 expressions are always non-NULL. */
358 if (!virtual_access && integer_zerop (offset))
359 return build_nop (ptr_target_type, expr);
360 null_test = error_mark_node;
363 /* Protect against multiple evaluation if necessary. */
364 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
365 expr = save_expr (expr);
367 /* Now that we've saved expr, build the real null test. */
368 if (null_test)
370 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
371 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
372 expr, zero);
375 /* If this is a simple base reference, express it as a COMPONENT_REF. */
376 if (code == PLUS_EXPR && !virtual_access
377 /* We don't build base fields for empty bases, and they aren't very
378 interesting to the optimizers anyway. */
379 && !has_empty)
381 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
382 expr = build_simple_base_path (expr, binfo);
383 if (want_pointer)
384 expr = build_address (expr);
385 target_type = TREE_TYPE (expr);
386 goto out;
389 if (virtual_access)
391 /* Going via virtual base V_BINFO. We need the static offset
392 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
393 V_BINFO. That offset is an entry in D_BINFO's vtable. */
394 tree v_offset;
396 if (fixed_type_p < 0 && in_base_initializer)
398 /* In a base member initializer, we cannot rely on the
399 vtable being set up. We have to indirect via the
400 vtt_parm. */
401 tree t;
403 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
404 t = build_pointer_type (t);
405 v_offset = convert (t, current_vtt_parm);
406 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
408 else
409 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
410 complain),
411 TREE_TYPE (TREE_TYPE (expr)));
413 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
414 v_offset = build1 (NOP_EXPR,
415 build_pointer_type (ptrdiff_type_node),
416 v_offset);
417 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
418 TREE_CONSTANT (v_offset) = 1;
420 offset = convert_to_integer (ptrdiff_type_node,
421 size_diffop_loc (input_location, offset,
422 BINFO_OFFSET (v_binfo)));
424 if (!integer_zerop (offset))
425 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
427 if (fixed_type_p < 0)
428 /* Negative fixed_type_p means this is a constructor or destructor;
429 virtual base layout is fixed in in-charge [cd]tors, but not in
430 base [cd]tors. */
431 offset = build3 (COND_EXPR, ptrdiff_type_node,
432 build2 (EQ_EXPR, boolean_type_node,
433 current_in_charge_parm, integer_zero_node),
434 v_offset,
435 convert_to_integer (ptrdiff_type_node,
436 BINFO_OFFSET (binfo)));
437 else
438 offset = v_offset;
441 if (want_pointer)
442 target_type = ptr_target_type;
444 expr = build1 (NOP_EXPR, ptr_target_type, expr);
446 if (!integer_zerop (offset))
448 offset = fold_convert (sizetype, offset);
449 if (code == MINUS_EXPR)
450 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
451 expr = fold_build_pointer_plus (expr, offset);
453 else
454 null_test = NULL;
456 if (!want_pointer)
457 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
459 out:
460 if (null_test)
461 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
462 build_zero_cst (target_type));
464 return expr;
467 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
468 Perform a derived-to-base conversion by recursively building up a
469 sequence of COMPONENT_REFs to the appropriate base fields. */
471 static tree
472 build_simple_base_path (tree expr, tree binfo)
474 tree type = BINFO_TYPE (binfo);
475 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
476 tree field;
478 if (d_binfo == NULL_TREE)
480 tree temp;
482 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
484 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
485 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
486 an lvalue in the front end; only _DECLs and _REFs are lvalues
487 in the back end. */
488 temp = unary_complex_lvalue (ADDR_EXPR, expr);
489 if (temp)
490 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
492 return expr;
495 /* Recurse. */
496 expr = build_simple_base_path (expr, d_binfo);
498 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
499 field; field = DECL_CHAIN (field))
500 /* Is this the base field created by build_base_field? */
501 if (TREE_CODE (field) == FIELD_DECL
502 && DECL_FIELD_IS_BASE (field)
503 && TREE_TYPE (field) == type
504 /* If we're looking for a field in the most-derived class,
505 also check the field offset; we can have two base fields
506 of the same type if one is an indirect virtual base and one
507 is a direct non-virtual base. */
508 && (BINFO_INHERITANCE_CHAIN (d_binfo)
509 || tree_int_cst_equal (byte_position (field),
510 BINFO_OFFSET (binfo))))
512 /* We don't use build_class_member_access_expr here, as that
513 has unnecessary checks, and more importantly results in
514 recursive calls to dfs_walk_once. */
515 int type_quals = cp_type_quals (TREE_TYPE (expr));
517 expr = build3 (COMPONENT_REF,
518 cp_build_qualified_type (type, type_quals),
519 expr, field, NULL_TREE);
520 expr = fold_if_not_in_template (expr);
522 /* Mark the expression const or volatile, as appropriate.
523 Even though we've dealt with the type above, we still have
524 to mark the expression itself. */
525 if (type_quals & TYPE_QUAL_CONST)
526 TREE_READONLY (expr) = 1;
527 if (type_quals & TYPE_QUAL_VOLATILE)
528 TREE_THIS_VOLATILE (expr) = 1;
530 return expr;
533 /* Didn't find the base field?!? */
534 gcc_unreachable ();
537 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
538 type is a class type or a pointer to a class type. In the former
539 case, TYPE is also a class type; in the latter it is another
540 pointer type. If CHECK_ACCESS is true, an error message is emitted
541 if TYPE is inaccessible. If OBJECT has pointer type, the value is
542 assumed to be non-NULL. */
544 tree
545 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
546 tsubst_flags_t complain)
548 tree binfo;
549 tree object_type;
550 base_access access;
552 if (TYPE_PTR_P (TREE_TYPE (object)))
554 object_type = TREE_TYPE (TREE_TYPE (object));
555 type = TREE_TYPE (type);
557 else
558 object_type = TREE_TYPE (object);
560 access = check_access ? ba_check : ba_unique;
561 if (!(complain & tf_error))
562 access |= ba_quiet;
563 binfo = lookup_base (object_type, type,
564 access,
565 NULL);
566 if (!binfo || binfo == error_mark_node)
567 return error_mark_node;
569 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
572 /* EXPR is an expression with unqualified class type. BASE is a base
573 binfo of that class type. Returns EXPR, converted to the BASE
574 type. This function assumes that EXPR is the most derived class;
575 therefore virtual bases can be found at their static offsets. */
577 tree
578 convert_to_base_statically (tree expr, tree base)
580 tree expr_type;
582 expr_type = TREE_TYPE (expr);
583 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
585 /* If this is a non-empty base, use a COMPONENT_REF. */
586 if (!is_empty_class (BINFO_TYPE (base)))
587 return build_simple_base_path (expr, base);
589 /* We use fold_build2 and fold_convert below to simplify the trees
590 provided to the optimizers. It is not safe to call these functions
591 when processing a template because they do not handle C++-specific
592 trees. */
593 gcc_assert (!processing_template_decl);
594 expr = cp_build_addr_expr (expr, tf_warning_or_error);
595 if (!integer_zerop (BINFO_OFFSET (base)))
596 expr = fold_build_pointer_plus_loc (input_location,
597 expr, BINFO_OFFSET (base));
598 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
599 expr = build_fold_indirect_ref_loc (input_location, expr);
602 return expr;
606 tree
607 build_vfield_ref (tree datum, tree type)
609 tree vfield, vcontext;
611 if (datum == error_mark_node)
612 return error_mark_node;
614 /* First, convert to the requested type. */
615 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
616 datum = convert_to_base (datum, type, /*check_access=*/false,
617 /*nonnull=*/true, tf_warning_or_error);
619 /* Second, the requested type may not be the owner of its own vptr.
620 If not, convert to the base class that owns it. We cannot use
621 convert_to_base here, because VCONTEXT may appear more than once
622 in the inheritance hierarchy of TYPE, and thus direct conversion
623 between the types may be ambiguous. Following the path back up
624 one step at a time via primary bases avoids the problem. */
625 vfield = TYPE_VFIELD (type);
626 vcontext = DECL_CONTEXT (vfield);
627 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
629 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
630 type = TREE_TYPE (datum);
633 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
636 /* Given an object INSTANCE, return an expression which yields the
637 vtable element corresponding to INDEX. There are many special
638 cases for INSTANCE which we take care of here, mainly to avoid
639 creating extra tree nodes when we don't have to. */
641 static tree
642 build_vtbl_ref_1 (tree instance, tree idx)
644 tree aref;
645 tree vtbl = NULL_TREE;
647 /* Try to figure out what a reference refers to, and
648 access its virtual function table directly. */
650 int cdtorp = 0;
651 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
653 tree basetype = non_reference (TREE_TYPE (instance));
655 if (fixed_type && !cdtorp)
657 tree binfo = lookup_base (fixed_type, basetype,
658 ba_unique | ba_quiet, NULL);
659 if (binfo)
660 vtbl = unshare_expr (BINFO_VTABLE (binfo));
663 if (!vtbl)
664 vtbl = build_vfield_ref (instance, basetype);
666 aref = build_array_ref (input_location, vtbl, idx);
667 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
669 return aref;
672 tree
673 build_vtbl_ref (tree instance, tree idx)
675 tree aref = build_vtbl_ref_1 (instance, idx);
677 return aref;
680 /* Given a stable object pointer INSTANCE_PTR, return an expression which
681 yields a function pointer corresponding to vtable element INDEX. */
683 tree
684 build_vfn_ref (tree instance_ptr, tree idx)
686 tree aref;
688 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
689 tf_warning_or_error),
690 idx);
692 /* When using function descriptors, the address of the
693 vtable entry is treated as a function pointer. */
694 if (TARGET_VTABLE_USES_DESCRIPTORS)
695 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
696 cp_build_addr_expr (aref, tf_warning_or_error));
698 /* Remember this as a method reference, for later devirtualization. */
699 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
701 return aref;
704 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
705 for the given TYPE. */
707 static tree
708 get_vtable_name (tree type)
710 return mangle_vtbl_for_type (type);
713 /* DECL is an entity associated with TYPE, like a virtual table or an
714 implicitly generated constructor. Determine whether or not DECL
715 should have external or internal linkage at the object file
716 level. This routine does not deal with COMDAT linkage and other
717 similar complexities; it simply sets TREE_PUBLIC if it possible for
718 entities in other translation units to contain copies of DECL, in
719 the abstract. */
721 void
722 set_linkage_according_to_type (tree type ATTRIBUTE_UNUSED, tree decl)
724 TREE_PUBLIC (decl) = 1;
725 determine_visibility (decl);
728 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
729 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
730 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
732 static tree
733 build_vtable (tree class_type, tree name, tree vtable_type)
735 tree decl;
737 decl = build_lang_decl (VAR_DECL, name, vtable_type);
738 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
739 now to avoid confusion in mangle_decl. */
740 SET_DECL_ASSEMBLER_NAME (decl, name);
741 DECL_CONTEXT (decl) = class_type;
742 DECL_ARTIFICIAL (decl) = 1;
743 TREE_STATIC (decl) = 1;
744 TREE_READONLY (decl) = 1;
745 DECL_VIRTUAL_P (decl) = 1;
746 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
747 DECL_VTABLE_OR_VTT_P (decl) = 1;
748 /* At one time the vtable info was grabbed 2 words at a time. This
749 fails on sparc unless you have 8-byte alignment. (tiemann) */
750 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
751 DECL_ALIGN (decl));
752 set_linkage_according_to_type (class_type, decl);
753 /* The vtable has not been defined -- yet. */
754 DECL_EXTERNAL (decl) = 1;
755 DECL_NOT_REALLY_EXTERN (decl) = 1;
757 /* Mark the VAR_DECL node representing the vtable itself as a
758 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
759 is rather important that such things be ignored because any
760 effort to actually generate DWARF for them will run into
761 trouble when/if we encounter code like:
763 #pragma interface
764 struct S { virtual void member (); };
766 because the artificial declaration of the vtable itself (as
767 manufactured by the g++ front end) will say that the vtable is
768 a static member of `S' but only *after* the debug output for
769 the definition of `S' has already been output. This causes
770 grief because the DWARF entry for the definition of the vtable
771 will try to refer back to an earlier *declaration* of the
772 vtable as a static member of `S' and there won't be one. We
773 might be able to arrange to have the "vtable static member"
774 attached to the member list for `S' before the debug info for
775 `S' get written (which would solve the problem) but that would
776 require more intrusive changes to the g++ front end. */
777 DECL_IGNORED_P (decl) = 1;
779 return decl;
782 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
783 or even complete. If this does not exist, create it. If COMPLETE is
784 nonzero, then complete the definition of it -- that will render it
785 impossible to actually build the vtable, but is useful to get at those
786 which are known to exist in the runtime. */
788 tree
789 get_vtable_decl (tree type, int complete)
791 tree decl;
793 if (CLASSTYPE_VTABLES (type))
794 return CLASSTYPE_VTABLES (type);
796 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
797 CLASSTYPE_VTABLES (type) = decl;
799 if (complete)
801 DECL_EXTERNAL (decl) = 1;
802 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
805 return decl;
808 /* Build the primary virtual function table for TYPE. If BINFO is
809 non-NULL, build the vtable starting with the initial approximation
810 that it is the same as the one which is the head of the association
811 list. Returns a nonzero value if a new vtable is actually
812 created. */
814 static int
815 build_primary_vtable (tree binfo, tree type)
817 tree decl;
818 tree virtuals;
820 decl = get_vtable_decl (type, /*complete=*/0);
822 if (binfo)
824 if (BINFO_NEW_VTABLE_MARKED (binfo))
825 /* We have already created a vtable for this base, so there's
826 no need to do it again. */
827 return 0;
829 virtuals = copy_list (BINFO_VIRTUALS (binfo));
830 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
831 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
832 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
834 else
836 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
837 virtuals = NULL_TREE;
840 #ifdef GATHER_STATISTICS
841 n_vtables += 1;
842 n_vtable_elems += list_length (virtuals);
843 #endif
845 /* Initialize the association list for this type, based
846 on our first approximation. */
847 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
848 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
849 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
850 return 1;
853 /* Give BINFO a new virtual function table which is initialized
854 with a skeleton-copy of its original initialization. The only
855 entry that changes is the `delta' entry, so we can really
856 share a lot of structure.
858 FOR_TYPE is the most derived type which caused this table to
859 be needed.
861 Returns nonzero if we haven't met BINFO before.
863 The order in which vtables are built (by calling this function) for
864 an object must remain the same, otherwise a binary incompatibility
865 can result. */
867 static int
868 build_secondary_vtable (tree binfo)
870 if (BINFO_NEW_VTABLE_MARKED (binfo))
871 /* We already created a vtable for this base. There's no need to
872 do it again. */
873 return 0;
875 /* Remember that we've created a vtable for this BINFO, so that we
876 don't try to do so again. */
877 SET_BINFO_NEW_VTABLE_MARKED (binfo);
879 /* Make fresh virtual list, so we can smash it later. */
880 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
882 /* Secondary vtables are laid out as part of the same structure as
883 the primary vtable. */
884 BINFO_VTABLE (binfo) = NULL_TREE;
885 return 1;
888 /* Create a new vtable for BINFO which is the hierarchy dominated by
889 T. Return nonzero if we actually created a new vtable. */
891 static int
892 make_new_vtable (tree t, tree binfo)
894 if (binfo == TYPE_BINFO (t))
895 /* In this case, it is *type*'s vtable we are modifying. We start
896 with the approximation that its vtable is that of the
897 immediate base class. */
898 return build_primary_vtable (binfo, t);
899 else
900 /* This is our very own copy of `basetype' to play with. Later,
901 we will fill in all the virtual functions that override the
902 virtual functions in these base classes which are not defined
903 by the current type. */
904 return build_secondary_vtable (binfo);
907 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
908 (which is in the hierarchy dominated by T) list FNDECL as its
909 BV_FN. DELTA is the required constant adjustment from the `this'
910 pointer where the vtable entry appears to the `this' required when
911 the function is actually called. */
913 static void
914 modify_vtable_entry (tree t,
915 tree binfo,
916 tree fndecl,
917 tree delta,
918 tree *virtuals)
920 tree v;
922 v = *virtuals;
924 if (fndecl != BV_FN (v)
925 || !tree_int_cst_equal (delta, BV_DELTA (v)))
927 /* We need a new vtable for BINFO. */
928 if (make_new_vtable (t, binfo))
930 /* If we really did make a new vtable, we also made a copy
931 of the BINFO_VIRTUALS list. Now, we have to find the
932 corresponding entry in that list. */
933 *virtuals = BINFO_VIRTUALS (binfo);
934 while (BV_FN (*virtuals) != BV_FN (v))
935 *virtuals = TREE_CHAIN (*virtuals);
936 v = *virtuals;
939 BV_DELTA (v) = delta;
940 BV_VCALL_INDEX (v) = NULL_TREE;
941 BV_FN (v) = fndecl;
946 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
947 the USING_DECL naming METHOD. Returns true if the method could be
948 added to the method vec. */
950 bool
951 add_method (tree type, tree method, tree using_decl)
953 unsigned slot;
954 tree overload;
955 bool template_conv_p = false;
956 bool conv_p;
957 VEC(tree,gc) *method_vec;
958 bool complete_p;
959 bool insert_p = false;
960 tree current_fns;
961 tree fns;
963 if (method == error_mark_node)
964 return false;
966 complete_p = COMPLETE_TYPE_P (type);
967 conv_p = DECL_CONV_FN_P (method);
968 if (conv_p)
969 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
970 && DECL_TEMPLATE_CONV_FN_P (method));
972 method_vec = CLASSTYPE_METHOD_VEC (type);
973 if (!method_vec)
975 /* Make a new method vector. We start with 8 entries. We must
976 allocate at least two (for constructors and destructors), and
977 we're going to end up with an assignment operator at some
978 point as well. */
979 method_vec = VEC_alloc (tree, gc, 8);
980 /* Create slots for constructors and destructors. */
981 VEC_quick_push (tree, method_vec, NULL_TREE);
982 VEC_quick_push (tree, method_vec, NULL_TREE);
983 CLASSTYPE_METHOD_VEC (type) = method_vec;
986 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
987 grok_special_member_properties (method);
989 /* Constructors and destructors go in special slots. */
990 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
991 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
992 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
994 slot = CLASSTYPE_DESTRUCTOR_SLOT;
996 if (TYPE_FOR_JAVA (type))
998 if (!DECL_ARTIFICIAL (method))
999 error ("Java class %qT cannot have a destructor", type);
1000 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1001 error ("Java class %qT cannot have an implicit non-trivial "
1002 "destructor",
1003 type);
1006 else
1008 tree m;
1010 insert_p = true;
1011 /* See if we already have an entry with this name. */
1012 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1013 VEC_iterate (tree, method_vec, slot, m);
1014 ++slot)
1016 m = OVL_CURRENT (m);
1017 if (template_conv_p)
1019 if (TREE_CODE (m) == TEMPLATE_DECL
1020 && DECL_TEMPLATE_CONV_FN_P (m))
1021 insert_p = false;
1022 break;
1024 if (conv_p && !DECL_CONV_FN_P (m))
1025 break;
1026 if (DECL_NAME (m) == DECL_NAME (method))
1028 insert_p = false;
1029 break;
1031 if (complete_p
1032 && !DECL_CONV_FN_P (m)
1033 && DECL_NAME (m) > DECL_NAME (method))
1034 break;
1037 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
1039 /* Check to see if we've already got this method. */
1040 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1042 tree fn = OVL_CURRENT (fns);
1043 tree fn_type;
1044 tree method_type;
1045 tree parms1;
1046 tree parms2;
1048 if (TREE_CODE (fn) != TREE_CODE (method))
1049 continue;
1051 /* [over.load] Member function declarations with the
1052 same name and the same parameter types cannot be
1053 overloaded if any of them is a static member
1054 function declaration.
1056 [namespace.udecl] When a using-declaration brings names
1057 from a base class into a derived class scope, member
1058 functions in the derived class override and/or hide member
1059 functions with the same name and parameter types in a base
1060 class (rather than conflicting). */
1061 fn_type = TREE_TYPE (fn);
1062 method_type = TREE_TYPE (method);
1063 parms1 = TYPE_ARG_TYPES (fn_type);
1064 parms2 = TYPE_ARG_TYPES (method_type);
1066 /* Compare the quals on the 'this' parm. Don't compare
1067 the whole types, as used functions are treated as
1068 coming from the using class in overload resolution. */
1069 if (! DECL_STATIC_FUNCTION_P (fn)
1070 && ! DECL_STATIC_FUNCTION_P (method)
1071 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1072 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1073 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1074 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1075 continue;
1077 /* For templates, the return type and template parameters
1078 must be identical. */
1079 if (TREE_CODE (fn) == TEMPLATE_DECL
1080 && (!same_type_p (TREE_TYPE (fn_type),
1081 TREE_TYPE (method_type))
1082 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1083 DECL_TEMPLATE_PARMS (method))))
1084 continue;
1086 if (! DECL_STATIC_FUNCTION_P (fn))
1087 parms1 = TREE_CHAIN (parms1);
1088 if (! DECL_STATIC_FUNCTION_P (method))
1089 parms2 = TREE_CHAIN (parms2);
1091 if (compparms (parms1, parms2)
1092 && (!DECL_CONV_FN_P (fn)
1093 || same_type_p (TREE_TYPE (fn_type),
1094 TREE_TYPE (method_type))))
1096 if (using_decl)
1098 if (DECL_CONTEXT (fn) == type)
1099 /* Defer to the local function. */
1100 return false;
1102 else
1104 error ("%q+#D cannot be overloaded", method);
1105 error ("with %q+#D", fn);
1108 /* We don't call duplicate_decls here to merge the
1109 declarations because that will confuse things if the
1110 methods have inline definitions. In particular, we
1111 will crash while processing the definitions. */
1112 return false;
1116 /* A class should never have more than one destructor. */
1117 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1118 return false;
1120 /* Add the new binding. */
1121 if (using_decl)
1123 overload = ovl_cons (method, current_fns);
1124 OVL_USED (overload) = true;
1126 else
1127 overload = build_overload (method, current_fns);
1129 if (conv_p)
1130 TYPE_HAS_CONVERSION (type) = 1;
1131 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1132 push_class_level_binding (DECL_NAME (method), overload);
1134 if (insert_p)
1136 bool reallocated;
1138 /* We only expect to add few methods in the COMPLETE_P case, so
1139 just make room for one more method in that case. */
1140 if (complete_p)
1141 reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
1142 else
1143 reallocated = VEC_reserve (tree, gc, method_vec, 1);
1144 if (reallocated)
1145 CLASSTYPE_METHOD_VEC (type) = method_vec;
1146 if (slot == VEC_length (tree, method_vec))
1147 VEC_quick_push (tree, method_vec, overload);
1148 else
1149 VEC_quick_insert (tree, method_vec, slot, overload);
1151 else
1152 /* Replace the current slot. */
1153 VEC_replace (tree, method_vec, slot, overload);
1154 return true;
1157 /* Subroutines of finish_struct. */
1159 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1160 legit, otherwise return 0. */
1162 static int
1163 alter_access (tree t, tree fdecl, tree access)
1165 tree elem;
1167 if (!DECL_LANG_SPECIFIC (fdecl))
1168 retrofit_lang_decl (fdecl);
1170 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1172 elem = purpose_member (t, DECL_ACCESS (fdecl));
1173 if (elem)
1175 if (TREE_VALUE (elem) != access)
1177 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1178 error ("conflicting access specifications for method"
1179 " %q+D, ignored", TREE_TYPE (fdecl));
1180 else
1181 error ("conflicting access specifications for field %qE, ignored",
1182 DECL_NAME (fdecl));
1184 else
1186 /* They're changing the access to the same thing they changed
1187 it to before. That's OK. */
1191 else
1193 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl);
1194 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1195 return 1;
1197 return 0;
1200 /* Process the USING_DECL, which is a member of T. */
1202 static void
1203 handle_using_decl (tree using_decl, tree t)
1205 tree decl = USING_DECL_DECLS (using_decl);
1206 tree name = DECL_NAME (using_decl);
1207 tree access
1208 = TREE_PRIVATE (using_decl) ? access_private_node
1209 : TREE_PROTECTED (using_decl) ? access_protected_node
1210 : access_public_node;
1211 tree flist = NULL_TREE;
1212 tree old_value;
1214 gcc_assert (!processing_template_decl && decl);
1216 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1217 tf_warning_or_error);
1218 if (old_value)
1220 if (is_overloaded_fn (old_value))
1221 old_value = OVL_CURRENT (old_value);
1223 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1224 /* OK */;
1225 else
1226 old_value = NULL_TREE;
1229 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1231 if (is_overloaded_fn (decl))
1232 flist = decl;
1234 if (! old_value)
1236 else if (is_overloaded_fn (old_value))
1238 if (flist)
1239 /* It's OK to use functions from a base when there are functions with
1240 the same name already present in the current class. */;
1241 else
1243 error ("%q+D invalid in %q#T", using_decl, t);
1244 error (" because of local method %q+#D with same name",
1245 OVL_CURRENT (old_value));
1246 return;
1249 else if (!DECL_ARTIFICIAL (old_value))
1251 error ("%q+D invalid in %q#T", using_decl, t);
1252 error (" because of local member %q+#D with same name", old_value);
1253 return;
1256 /* Make type T see field decl FDECL with access ACCESS. */
1257 if (flist)
1258 for (; flist; flist = OVL_NEXT (flist))
1260 add_method (t, OVL_CURRENT (flist), using_decl);
1261 alter_access (t, OVL_CURRENT (flist), access);
1263 else
1264 alter_access (t, decl, access);
1267 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1268 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1269 properties of the bases. */
1271 static void
1272 check_bases (tree t,
1273 int* cant_have_const_ctor_p,
1274 int* no_const_asn_ref_p)
1276 int i;
1277 bool seen_non_virtual_nearly_empty_base_p = 0;
1278 int seen_tm_mask = 0;
1279 tree base_binfo;
1280 tree binfo;
1281 tree field = NULL_TREE;
1283 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1284 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1285 if (TREE_CODE (field) == FIELD_DECL)
1286 break;
1288 for (binfo = TYPE_BINFO (t), i = 0;
1289 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1291 tree basetype = TREE_TYPE (base_binfo);
1293 gcc_assert (COMPLETE_TYPE_P (basetype));
1295 if (CLASSTYPE_FINAL (basetype))
1296 error ("cannot derive from %<final%> base %qT in derived type %qT",
1297 basetype, t);
1299 /* If any base class is non-literal, so is the derived class. */
1300 if (!CLASSTYPE_LITERAL_P (basetype))
1301 CLASSTYPE_LITERAL_P (t) = false;
1303 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1304 here because the case of virtual functions but non-virtual
1305 dtor is handled in finish_struct_1. */
1306 if (!TYPE_POLYMORPHIC_P (basetype))
1307 warning (OPT_Weffc__,
1308 "base class %q#T has a non-virtual destructor", basetype);
1310 /* If the base class doesn't have copy constructors or
1311 assignment operators that take const references, then the
1312 derived class cannot have such a member automatically
1313 generated. */
1314 if (TYPE_HAS_COPY_CTOR (basetype)
1315 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1316 *cant_have_const_ctor_p = 1;
1317 if (TYPE_HAS_COPY_ASSIGN (basetype)
1318 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1319 *no_const_asn_ref_p = 1;
1321 if (BINFO_VIRTUAL_P (base_binfo))
1322 /* A virtual base does not effect nearly emptiness. */
1324 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1326 if (seen_non_virtual_nearly_empty_base_p)
1327 /* And if there is more than one nearly empty base, then the
1328 derived class is not nearly empty either. */
1329 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1330 else
1331 /* Remember we've seen one. */
1332 seen_non_virtual_nearly_empty_base_p = 1;
1334 else if (!is_empty_class (basetype))
1335 /* If the base class is not empty or nearly empty, then this
1336 class cannot be nearly empty. */
1337 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1339 /* A lot of properties from the bases also apply to the derived
1340 class. */
1341 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1342 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1343 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1344 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1345 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1346 || !TYPE_HAS_COPY_ASSIGN (basetype));
1347 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1348 || !TYPE_HAS_COPY_CTOR (basetype));
1349 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1350 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1351 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1352 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1353 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1354 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1355 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1356 || TYPE_HAS_COMPLEX_DFLT (basetype));
1358 /* A standard-layout class is a class that:
1360 * has no non-standard-layout base classes, */
1361 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1362 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1364 tree basefield;
1365 /* ...has no base classes of the same type as the first non-static
1366 data member... */
1367 if (field && DECL_CONTEXT (field) == t
1368 && (same_type_ignoring_top_level_qualifiers_p
1369 (TREE_TYPE (field), basetype)))
1370 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1371 else
1372 /* ...either has no non-static data members in the most-derived
1373 class and at most one base class with non-static data
1374 members, or has no base classes with non-static data
1375 members */
1376 for (basefield = TYPE_FIELDS (basetype); basefield;
1377 basefield = DECL_CHAIN (basefield))
1378 if (TREE_CODE (basefield) == FIELD_DECL)
1380 if (field)
1381 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1382 else
1383 field = basefield;
1384 break;
1388 /* Don't bother collecting tm attributes if transactional memory
1389 support is not enabled. */
1390 if (flag_tm)
1392 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1393 if (tm_attr)
1394 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1398 /* If one of the base classes had TM attributes, and the current class
1399 doesn't define its own, then the current class inherits one. */
1400 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1402 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1403 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1407 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1408 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1409 that have had a nearly-empty virtual primary base stolen by some
1410 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1411 T. */
1413 static void
1414 determine_primary_bases (tree t)
1416 unsigned i;
1417 tree primary = NULL_TREE;
1418 tree type_binfo = TYPE_BINFO (t);
1419 tree base_binfo;
1421 /* Determine the primary bases of our bases. */
1422 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1423 base_binfo = TREE_CHAIN (base_binfo))
1425 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1427 /* See if we're the non-virtual primary of our inheritance
1428 chain. */
1429 if (!BINFO_VIRTUAL_P (base_binfo))
1431 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1432 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1434 if (parent_primary
1435 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1436 BINFO_TYPE (parent_primary)))
1437 /* We are the primary binfo. */
1438 BINFO_PRIMARY_P (base_binfo) = 1;
1440 /* Determine if we have a virtual primary base, and mark it so.
1442 if (primary && BINFO_VIRTUAL_P (primary))
1444 tree this_primary = copied_binfo (primary, base_binfo);
1446 if (BINFO_PRIMARY_P (this_primary))
1447 /* Someone already claimed this base. */
1448 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1449 else
1451 tree delta;
1453 BINFO_PRIMARY_P (this_primary) = 1;
1454 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1456 /* A virtual binfo might have been copied from within
1457 another hierarchy. As we're about to use it as a
1458 primary base, make sure the offsets match. */
1459 delta = size_diffop_loc (input_location,
1460 convert (ssizetype,
1461 BINFO_OFFSET (base_binfo)),
1462 convert (ssizetype,
1463 BINFO_OFFSET (this_primary)));
1465 propagate_binfo_offsets (this_primary, delta);
1470 /* First look for a dynamic direct non-virtual base. */
1471 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1473 tree basetype = BINFO_TYPE (base_binfo);
1475 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1477 primary = base_binfo;
1478 goto found;
1482 /* A "nearly-empty" virtual base class can be the primary base
1483 class, if no non-virtual polymorphic base can be found. Look for
1484 a nearly-empty virtual dynamic base that is not already a primary
1485 base of something in the hierarchy. If there is no such base,
1486 just pick the first nearly-empty virtual base. */
1488 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1489 base_binfo = TREE_CHAIN (base_binfo))
1490 if (BINFO_VIRTUAL_P (base_binfo)
1491 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1493 if (!BINFO_PRIMARY_P (base_binfo))
1495 /* Found one that is not primary. */
1496 primary = base_binfo;
1497 goto found;
1499 else if (!primary)
1500 /* Remember the first candidate. */
1501 primary = base_binfo;
1504 found:
1505 /* If we've got a primary base, use it. */
1506 if (primary)
1508 tree basetype = BINFO_TYPE (primary);
1510 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1511 if (BINFO_PRIMARY_P (primary))
1512 /* We are stealing a primary base. */
1513 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1514 BINFO_PRIMARY_P (primary) = 1;
1515 if (BINFO_VIRTUAL_P (primary))
1517 tree delta;
1519 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1520 /* A virtual binfo might have been copied from within
1521 another hierarchy. As we're about to use it as a primary
1522 base, make sure the offsets match. */
1523 delta = size_diffop_loc (input_location, ssize_int (0),
1524 convert (ssizetype, BINFO_OFFSET (primary)));
1526 propagate_binfo_offsets (primary, delta);
1529 primary = TYPE_BINFO (basetype);
1531 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1532 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1533 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1537 /* Update the variant types of T. */
1539 void
1540 fixup_type_variants (tree t)
1542 tree variants;
1544 if (!t)
1545 return;
1547 for (variants = TYPE_NEXT_VARIANT (t);
1548 variants;
1549 variants = TYPE_NEXT_VARIANT (variants))
1551 /* These fields are in the _TYPE part of the node, not in
1552 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1553 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1554 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1555 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1556 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1558 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1560 TYPE_BINFO (variants) = TYPE_BINFO (t);
1562 /* Copy whatever these are holding today. */
1563 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1564 TYPE_METHODS (variants) = TYPE_METHODS (t);
1565 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1569 /* Early variant fixups: we apply attributes at the beginning of the class
1570 definition, and we need to fix up any variants that have already been
1571 made via elaborated-type-specifier so that check_qualified_type works. */
1573 void
1574 fixup_attribute_variants (tree t)
1576 tree variants;
1578 if (!t)
1579 return;
1581 for (variants = TYPE_NEXT_VARIANT (t);
1582 variants;
1583 variants = TYPE_NEXT_VARIANT (variants))
1585 /* These are the two fields that check_qualified_type looks at and
1586 are affected by attributes. */
1587 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1588 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1592 /* Set memoizing fields and bits of T (and its variants) for later
1593 use. */
1595 static void
1596 finish_struct_bits (tree t)
1598 /* Fix up variants (if any). */
1599 fixup_type_variants (t);
1601 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1602 /* For a class w/o baseclasses, 'finish_struct' has set
1603 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1604 Similarly for a class whose base classes do not have vtables.
1605 When neither of these is true, we might have removed abstract
1606 virtuals (by providing a definition), added some (by declaring
1607 new ones), or redeclared ones from a base class. We need to
1608 recalculate what's really an abstract virtual at this point (by
1609 looking in the vtables). */
1610 get_pure_virtuals (t);
1612 /* If this type has a copy constructor or a destructor, force its
1613 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1614 nonzero. This will cause it to be passed by invisible reference
1615 and prevent it from being returned in a register. */
1616 if (type_has_nontrivial_copy_init (t)
1617 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1619 tree variants;
1620 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1621 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1623 SET_TYPE_MODE (variants, BLKmode);
1624 TREE_ADDRESSABLE (variants) = 1;
1629 /* Issue warnings about T having private constructors, but no friends,
1630 and so forth.
1632 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1633 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1634 non-private static member functions. */
1636 static void
1637 maybe_warn_about_overly_private_class (tree t)
1639 int has_member_fn = 0;
1640 int has_nonprivate_method = 0;
1641 tree fn;
1643 if (!warn_ctor_dtor_privacy
1644 /* If the class has friends, those entities might create and
1645 access instances, so we should not warn. */
1646 || (CLASSTYPE_FRIEND_CLASSES (t)
1647 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1648 /* We will have warned when the template was declared; there's
1649 no need to warn on every instantiation. */
1650 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1651 /* There's no reason to even consider warning about this
1652 class. */
1653 return;
1655 /* We only issue one warning, if more than one applies, because
1656 otherwise, on code like:
1658 class A {
1659 // Oops - forgot `public:'
1660 A();
1661 A(const A&);
1662 ~A();
1665 we warn several times about essentially the same problem. */
1667 /* Check to see if all (non-constructor, non-destructor) member
1668 functions are private. (Since there are no friends or
1669 non-private statics, we can't ever call any of the private member
1670 functions.) */
1671 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1672 /* We're not interested in compiler-generated methods; they don't
1673 provide any way to call private members. */
1674 if (!DECL_ARTIFICIAL (fn))
1676 if (!TREE_PRIVATE (fn))
1678 if (DECL_STATIC_FUNCTION_P (fn))
1679 /* A non-private static member function is just like a
1680 friend; it can create and invoke private member
1681 functions, and be accessed without a class
1682 instance. */
1683 return;
1685 has_nonprivate_method = 1;
1686 /* Keep searching for a static member function. */
1688 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1689 has_member_fn = 1;
1692 if (!has_nonprivate_method && has_member_fn)
1694 /* There are no non-private methods, and there's at least one
1695 private member function that isn't a constructor or
1696 destructor. (If all the private members are
1697 constructors/destructors we want to use the code below that
1698 issues error messages specifically referring to
1699 constructors/destructors.) */
1700 unsigned i;
1701 tree binfo = TYPE_BINFO (t);
1703 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1704 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1706 has_nonprivate_method = 1;
1707 break;
1709 if (!has_nonprivate_method)
1711 warning (OPT_Wctor_dtor_privacy,
1712 "all member functions in class %qT are private", t);
1713 return;
1717 /* Even if some of the member functions are non-private, the class
1718 won't be useful for much if all the constructors or destructors
1719 are private: such an object can never be created or destroyed. */
1720 fn = CLASSTYPE_DESTRUCTORS (t);
1721 if (fn && TREE_PRIVATE (fn))
1723 warning (OPT_Wctor_dtor_privacy,
1724 "%q#T only defines a private destructor and has no friends",
1726 return;
1729 /* Warn about classes that have private constructors and no friends. */
1730 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1731 /* Implicitly generated constructors are always public. */
1732 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1733 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1735 int nonprivate_ctor = 0;
1737 /* If a non-template class does not define a copy
1738 constructor, one is defined for it, enabling it to avoid
1739 this warning. For a template class, this does not
1740 happen, and so we would normally get a warning on:
1742 template <class T> class C { private: C(); };
1744 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1745 complete non-template or fully instantiated classes have this
1746 flag set. */
1747 if (!TYPE_HAS_COPY_CTOR (t))
1748 nonprivate_ctor = 1;
1749 else
1750 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1752 tree ctor = OVL_CURRENT (fn);
1753 /* Ideally, we wouldn't count copy constructors (or, in
1754 fact, any constructor that takes an argument of the
1755 class type as a parameter) because such things cannot
1756 be used to construct an instance of the class unless
1757 you already have one. But, for now at least, we're
1758 more generous. */
1759 if (! TREE_PRIVATE (ctor))
1761 nonprivate_ctor = 1;
1762 break;
1766 if (nonprivate_ctor == 0)
1768 warning (OPT_Wctor_dtor_privacy,
1769 "%q#T only defines private constructors and has no friends",
1771 return;
1776 static struct {
1777 gt_pointer_operator new_value;
1778 void *cookie;
1779 } resort_data;
1781 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1783 static int
1784 method_name_cmp (const void* m1_p, const void* m2_p)
1786 const tree *const m1 = (const tree *) m1_p;
1787 const tree *const m2 = (const tree *) m2_p;
1789 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1790 return 0;
1791 if (*m1 == NULL_TREE)
1792 return -1;
1793 if (*m2 == NULL_TREE)
1794 return 1;
1795 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1796 return -1;
1797 return 1;
1800 /* This routine compares two fields like method_name_cmp but using the
1801 pointer operator in resort_field_decl_data. */
1803 static int
1804 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1806 const tree *const m1 = (const tree *) m1_p;
1807 const tree *const m2 = (const tree *) m2_p;
1808 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1809 return 0;
1810 if (*m1 == NULL_TREE)
1811 return -1;
1812 if (*m2 == NULL_TREE)
1813 return 1;
1815 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1816 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1817 resort_data.new_value (&d1, resort_data.cookie);
1818 resort_data.new_value (&d2, resort_data.cookie);
1819 if (d1 < d2)
1820 return -1;
1822 return 1;
1825 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1827 void
1828 resort_type_method_vec (void* obj,
1829 void* orig_obj ATTRIBUTE_UNUSED ,
1830 gt_pointer_operator new_value,
1831 void* cookie)
1833 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1834 int len = VEC_length (tree, method_vec);
1835 size_t slot;
1836 tree fn;
1838 /* The type conversion ops have to live at the front of the vec, so we
1839 can't sort them. */
1840 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1841 VEC_iterate (tree, method_vec, slot, fn);
1842 ++slot)
1843 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1844 break;
1846 if (len - slot > 1)
1848 resort_data.new_value = new_value;
1849 resort_data.cookie = cookie;
1850 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1851 resort_method_name_cmp);
1855 /* Warn about duplicate methods in fn_fields.
1857 Sort methods that are not special (i.e., constructors, destructors,
1858 and type conversion operators) so that we can find them faster in
1859 search. */
1861 static void
1862 finish_struct_methods (tree t)
1864 tree fn_fields;
1865 VEC(tree,gc) *method_vec;
1866 int slot, len;
1868 method_vec = CLASSTYPE_METHOD_VEC (t);
1869 if (!method_vec)
1870 return;
1872 len = VEC_length (tree, method_vec);
1874 /* Clear DECL_IN_AGGR_P for all functions. */
1875 for (fn_fields = TYPE_METHODS (t); fn_fields;
1876 fn_fields = DECL_CHAIN (fn_fields))
1877 DECL_IN_AGGR_P (fn_fields) = 0;
1879 /* Issue warnings about private constructors and such. If there are
1880 no methods, then some public defaults are generated. */
1881 maybe_warn_about_overly_private_class (t);
1883 /* The type conversion ops have to live at the front of the vec, so we
1884 can't sort them. */
1885 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1886 VEC_iterate (tree, method_vec, slot, fn_fields);
1887 ++slot)
1888 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1889 break;
1890 if (len - slot > 1)
1891 qsort (VEC_address (tree, method_vec) + slot,
1892 len-slot, sizeof (tree), method_name_cmp);
1895 /* Make BINFO's vtable have N entries, including RTTI entries,
1896 vbase and vcall offsets, etc. Set its type and call the back end
1897 to lay it out. */
1899 static void
1900 layout_vtable_decl (tree binfo, int n)
1902 tree atype;
1903 tree vtable;
1905 atype = build_array_of_n_type (vtable_entry_type, n);
1906 layout_type (atype);
1908 /* We may have to grow the vtable. */
1909 vtable = get_vtbl_decl_for_binfo (binfo);
1910 if (!same_type_p (TREE_TYPE (vtable), atype))
1912 TREE_TYPE (vtable) = atype;
1913 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1914 layout_decl (vtable, 0);
1918 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1919 have the same signature. */
1922 same_signature_p (const_tree fndecl, const_tree base_fndecl)
1924 /* One destructor overrides another if they are the same kind of
1925 destructor. */
1926 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1927 && special_function_p (base_fndecl) == special_function_p (fndecl))
1928 return 1;
1929 /* But a non-destructor never overrides a destructor, nor vice
1930 versa, nor do different kinds of destructors override
1931 one-another. For example, a complete object destructor does not
1932 override a deleting destructor. */
1933 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1934 return 0;
1936 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1937 || (DECL_CONV_FN_P (fndecl)
1938 && DECL_CONV_FN_P (base_fndecl)
1939 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1940 DECL_CONV_FN_TYPE (base_fndecl))))
1942 tree types, base_types;
1943 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1944 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1945 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
1946 == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
1947 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1948 return 1;
1950 return 0;
1953 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1954 subobject. */
1956 static bool
1957 base_derived_from (tree derived, tree base)
1959 tree probe;
1961 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1963 if (probe == derived)
1964 return true;
1965 else if (BINFO_VIRTUAL_P (probe))
1966 /* If we meet a virtual base, we can't follow the inheritance
1967 any more. See if the complete type of DERIVED contains
1968 such a virtual base. */
1969 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1970 != NULL_TREE);
1972 return false;
1975 typedef struct find_final_overrider_data_s {
1976 /* The function for which we are trying to find a final overrider. */
1977 tree fn;
1978 /* The base class in which the function was declared. */
1979 tree declaring_base;
1980 /* The candidate overriders. */
1981 tree candidates;
1982 /* Path to most derived. */
1983 VEC(tree,heap) *path;
1984 } find_final_overrider_data;
1986 /* Add the overrider along the current path to FFOD->CANDIDATES.
1987 Returns true if an overrider was found; false otherwise. */
1989 static bool
1990 dfs_find_final_overrider_1 (tree binfo,
1991 find_final_overrider_data *ffod,
1992 unsigned depth)
1994 tree method;
1996 /* If BINFO is not the most derived type, try a more derived class.
1997 A definition there will overrider a definition here. */
1998 if (depth)
2000 depth--;
2001 if (dfs_find_final_overrider_1
2002 (VEC_index (tree, ffod->path, depth), ffod, depth))
2003 return true;
2006 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2007 if (method)
2009 tree *candidate = &ffod->candidates;
2011 /* Remove any candidates overridden by this new function. */
2012 while (*candidate)
2014 /* If *CANDIDATE overrides METHOD, then METHOD
2015 cannot override anything else on the list. */
2016 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2017 return true;
2018 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2019 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2020 *candidate = TREE_CHAIN (*candidate);
2021 else
2022 candidate = &TREE_CHAIN (*candidate);
2025 /* Add the new function. */
2026 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2027 return true;
2030 return false;
2033 /* Called from find_final_overrider via dfs_walk. */
2035 static tree
2036 dfs_find_final_overrider_pre (tree binfo, void *data)
2038 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2040 if (binfo == ffod->declaring_base)
2041 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
2042 VEC_safe_push (tree, heap, ffod->path, binfo);
2044 return NULL_TREE;
2047 static tree
2048 dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
2050 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2051 VEC_pop (tree, ffod->path);
2053 return NULL_TREE;
2056 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2057 FN and whose TREE_VALUE is the binfo for the base where the
2058 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2059 DERIVED) is the base object in which FN is declared. */
2061 static tree
2062 find_final_overrider (tree derived, tree binfo, tree fn)
2064 find_final_overrider_data ffod;
2066 /* Getting this right is a little tricky. This is valid:
2068 struct S { virtual void f (); };
2069 struct T { virtual void f (); };
2070 struct U : public S, public T { };
2072 even though calling `f' in `U' is ambiguous. But,
2074 struct R { virtual void f(); };
2075 struct S : virtual public R { virtual void f (); };
2076 struct T : virtual public R { virtual void f (); };
2077 struct U : public S, public T { };
2079 is not -- there's no way to decide whether to put `S::f' or
2080 `T::f' in the vtable for `R'.
2082 The solution is to look at all paths to BINFO. If we find
2083 different overriders along any two, then there is a problem. */
2084 if (DECL_THUNK_P (fn))
2085 fn = THUNK_TARGET (fn);
2087 /* Determine the depth of the hierarchy. */
2088 ffod.fn = fn;
2089 ffod.declaring_base = binfo;
2090 ffod.candidates = NULL_TREE;
2091 ffod.path = VEC_alloc (tree, heap, 30);
2093 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2094 dfs_find_final_overrider_post, &ffod);
2096 VEC_free (tree, heap, ffod.path);
2098 /* If there was no winner, issue an error message. */
2099 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2100 return error_mark_node;
2102 return ffod.candidates;
2105 /* Return the index of the vcall offset for FN when TYPE is used as a
2106 virtual base. */
2108 static tree
2109 get_vcall_index (tree fn, tree type)
2111 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
2112 tree_pair_p p;
2113 unsigned ix;
2115 FOR_EACH_VEC_ELT (tree_pair_s, indices, ix, p)
2116 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2117 || same_signature_p (fn, p->purpose))
2118 return p->value;
2120 /* There should always be an appropriate index. */
2121 gcc_unreachable ();
2124 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2125 dominated by T. FN is the old function; VIRTUALS points to the
2126 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2127 of that entry in the list. */
2129 static void
2130 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2131 unsigned ix)
2133 tree b;
2134 tree overrider;
2135 tree delta;
2136 tree virtual_base;
2137 tree first_defn;
2138 tree overrider_fn, overrider_target;
2139 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2140 tree over_return, base_return;
2141 bool lost = false;
2143 /* Find the nearest primary base (possibly binfo itself) which defines
2144 this function; this is the class the caller will convert to when
2145 calling FN through BINFO. */
2146 for (b = binfo; ; b = get_primary_binfo (b))
2148 gcc_assert (b);
2149 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2150 break;
2152 /* The nearest definition is from a lost primary. */
2153 if (BINFO_LOST_PRIMARY_P (b))
2154 lost = true;
2156 first_defn = b;
2158 /* Find the final overrider. */
2159 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2160 if (overrider == error_mark_node)
2162 error ("no unique final overrider for %qD in %qT", target_fn, t);
2163 return;
2165 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2167 /* Check for adjusting covariant return types. */
2168 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2169 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2171 if (POINTER_TYPE_P (over_return)
2172 && TREE_CODE (over_return) == TREE_CODE (base_return)
2173 && CLASS_TYPE_P (TREE_TYPE (over_return))
2174 && CLASS_TYPE_P (TREE_TYPE (base_return))
2175 /* If the overrider is invalid, don't even try. */
2176 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2178 /* If FN is a covariant thunk, we must figure out the adjustment
2179 to the final base FN was converting to. As OVERRIDER_TARGET might
2180 also be converting to the return type of FN, we have to
2181 combine the two conversions here. */
2182 tree fixed_offset, virtual_offset;
2184 over_return = TREE_TYPE (over_return);
2185 base_return = TREE_TYPE (base_return);
2187 if (DECL_THUNK_P (fn))
2189 gcc_assert (DECL_RESULT_THUNK_P (fn));
2190 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2191 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2193 else
2194 fixed_offset = virtual_offset = NULL_TREE;
2196 if (virtual_offset)
2197 /* Find the equivalent binfo within the return type of the
2198 overriding function. We will want the vbase offset from
2199 there. */
2200 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2201 over_return);
2202 else if (!same_type_ignoring_top_level_qualifiers_p
2203 (over_return, base_return))
2205 /* There was no existing virtual thunk (which takes
2206 precedence). So find the binfo of the base function's
2207 return type within the overriding function's return type.
2208 We cannot call lookup base here, because we're inside a
2209 dfs_walk, and will therefore clobber the BINFO_MARKED
2210 flags. Fortunately we know the covariancy is valid (it
2211 has already been checked), so we can just iterate along
2212 the binfos, which have been chained in inheritance graph
2213 order. Of course it is lame that we have to repeat the
2214 search here anyway -- we should really be caching pieces
2215 of the vtable and avoiding this repeated work. */
2216 tree thunk_binfo, base_binfo;
2218 /* Find the base binfo within the overriding function's
2219 return type. We will always find a thunk_binfo, except
2220 when the covariancy is invalid (which we will have
2221 already diagnosed). */
2222 for (base_binfo = TYPE_BINFO (base_return),
2223 thunk_binfo = TYPE_BINFO (over_return);
2224 thunk_binfo;
2225 thunk_binfo = TREE_CHAIN (thunk_binfo))
2226 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2227 BINFO_TYPE (base_binfo)))
2228 break;
2230 /* See if virtual inheritance is involved. */
2231 for (virtual_offset = thunk_binfo;
2232 virtual_offset;
2233 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2234 if (BINFO_VIRTUAL_P (virtual_offset))
2235 break;
2237 if (virtual_offset
2238 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2240 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2242 if (virtual_offset)
2244 /* We convert via virtual base. Adjust the fixed
2245 offset to be from there. */
2246 offset =
2247 size_diffop (offset,
2248 convert (ssizetype,
2249 BINFO_OFFSET (virtual_offset)));
2251 if (fixed_offset)
2252 /* There was an existing fixed offset, this must be
2253 from the base just converted to, and the base the
2254 FN was thunking to. */
2255 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2256 else
2257 fixed_offset = offset;
2261 if (fixed_offset || virtual_offset)
2262 /* Replace the overriding function with a covariant thunk. We
2263 will emit the overriding function in its own slot as
2264 well. */
2265 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2266 fixed_offset, virtual_offset);
2268 else
2269 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2270 !DECL_THUNK_P (fn));
2272 /* If we need a covariant thunk, then we may need to adjust first_defn.
2273 The ABI specifies that the thunks emitted with a function are
2274 determined by which bases the function overrides, so we need to be
2275 sure that we're using a thunk for some overridden base; even if we
2276 know that the necessary this adjustment is zero, there may not be an
2277 appropriate zero-this-adjusment thunk for us to use since thunks for
2278 overriding virtual bases always use the vcall offset.
2280 Furthermore, just choosing any base that overrides this function isn't
2281 quite right, as this slot won't be used for calls through a type that
2282 puts a covariant thunk here. Calling the function through such a type
2283 will use a different slot, and that slot is the one that determines
2284 the thunk emitted for that base.
2286 So, keep looking until we find the base that we're really overriding
2287 in this slot: the nearest primary base that doesn't use a covariant
2288 thunk in this slot. */
2289 if (overrider_target != overrider_fn)
2291 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2292 /* We already know that the overrider needs a covariant thunk. */
2293 b = get_primary_binfo (b);
2294 for (; ; b = get_primary_binfo (b))
2296 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2297 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2298 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2299 break;
2300 if (BINFO_LOST_PRIMARY_P (b))
2301 lost = true;
2303 first_defn = b;
2306 /* Assume that we will produce a thunk that convert all the way to
2307 the final overrider, and not to an intermediate virtual base. */
2308 virtual_base = NULL_TREE;
2310 /* See if we can convert to an intermediate virtual base first, and then
2311 use the vcall offset located there to finish the conversion. */
2312 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2314 /* If we find the final overrider, then we can stop
2315 walking. */
2316 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2317 BINFO_TYPE (TREE_VALUE (overrider))))
2318 break;
2320 /* If we find a virtual base, and we haven't yet found the
2321 overrider, then there is a virtual base between the
2322 declaring base (first_defn) and the final overrider. */
2323 if (BINFO_VIRTUAL_P (b))
2325 virtual_base = b;
2326 break;
2330 /* Compute the constant adjustment to the `this' pointer. The
2331 `this' pointer, when this function is called, will point at BINFO
2332 (or one of its primary bases, which are at the same offset). */
2333 if (virtual_base)
2334 /* The `this' pointer needs to be adjusted from the declaration to
2335 the nearest virtual base. */
2336 delta = size_diffop_loc (input_location,
2337 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2338 convert (ssizetype, BINFO_OFFSET (first_defn)));
2339 else if (lost)
2340 /* If the nearest definition is in a lost primary, we don't need an
2341 entry in our vtable. Except possibly in a constructor vtable,
2342 if we happen to get our primary back. In that case, the offset
2343 will be zero, as it will be a primary base. */
2344 delta = size_zero_node;
2345 else
2346 /* The `this' pointer needs to be adjusted from pointing to
2347 BINFO to pointing at the base where the final overrider
2348 appears. */
2349 delta = size_diffop_loc (input_location,
2350 convert (ssizetype,
2351 BINFO_OFFSET (TREE_VALUE (overrider))),
2352 convert (ssizetype, BINFO_OFFSET (binfo)));
2354 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2356 if (virtual_base)
2357 BV_VCALL_INDEX (*virtuals)
2358 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2359 else
2360 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2362 BV_LOST_PRIMARY (*virtuals) = lost;
2365 /* Called from modify_all_vtables via dfs_walk. */
2367 static tree
2368 dfs_modify_vtables (tree binfo, void* data)
2370 tree t = (tree) data;
2371 tree virtuals;
2372 tree old_virtuals;
2373 unsigned ix;
2375 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2376 /* A base without a vtable needs no modification, and its bases
2377 are uninteresting. */
2378 return dfs_skip_bases;
2380 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2381 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2382 /* Don't do the primary vtable, if it's new. */
2383 return NULL_TREE;
2385 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2386 /* There's no need to modify the vtable for a non-virtual primary
2387 base; we're not going to use that vtable anyhow. We do still
2388 need to do this for virtual primary bases, as they could become
2389 non-primary in a construction vtable. */
2390 return NULL_TREE;
2392 make_new_vtable (t, binfo);
2394 /* Now, go through each of the virtual functions in the virtual
2395 function table for BINFO. Find the final overrider, and update
2396 the BINFO_VIRTUALS list appropriately. */
2397 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2398 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2399 virtuals;
2400 ix++, virtuals = TREE_CHAIN (virtuals),
2401 old_virtuals = TREE_CHAIN (old_virtuals))
2402 update_vtable_entry_for_fn (t,
2403 binfo,
2404 BV_FN (old_virtuals),
2405 &virtuals, ix);
2407 return NULL_TREE;
2410 /* Update all of the primary and secondary vtables for T. Create new
2411 vtables as required, and initialize their RTTI information. Each
2412 of the functions in VIRTUALS is declared in T and may override a
2413 virtual function from a base class; find and modify the appropriate
2414 entries to point to the overriding functions. Returns a list, in
2415 declaration order, of the virtual functions that are declared in T,
2416 but do not appear in the primary base class vtable, and which
2417 should therefore be appended to the end of the vtable for T. */
2419 static tree
2420 modify_all_vtables (tree t, tree virtuals)
2422 tree binfo = TYPE_BINFO (t);
2423 tree *fnsp;
2425 /* Update all of the vtables. */
2426 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2428 /* Add virtual functions not already in our primary vtable. These
2429 will be both those introduced by this class, and those overridden
2430 from secondary bases. It does not include virtuals merely
2431 inherited from secondary bases. */
2432 for (fnsp = &virtuals; *fnsp; )
2434 tree fn = TREE_VALUE (*fnsp);
2436 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2437 || DECL_VINDEX (fn) == error_mark_node)
2439 /* We don't need to adjust the `this' pointer when
2440 calling this function. */
2441 BV_DELTA (*fnsp) = integer_zero_node;
2442 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2444 /* This is a function not already in our vtable. Keep it. */
2445 fnsp = &TREE_CHAIN (*fnsp);
2447 else
2448 /* We've already got an entry for this function. Skip it. */
2449 *fnsp = TREE_CHAIN (*fnsp);
2452 return virtuals;
2455 /* Get the base virtual function declarations in T that have the
2456 indicated NAME. */
2458 static tree
2459 get_basefndecls (tree name, tree t)
2461 tree methods;
2462 tree base_fndecls = NULL_TREE;
2463 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2464 int i;
2466 /* Find virtual functions in T with the indicated NAME. */
2467 i = lookup_fnfields_1 (t, name);
2468 if (i != -1)
2469 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2470 methods;
2471 methods = OVL_NEXT (methods))
2473 tree method = OVL_CURRENT (methods);
2475 if (TREE_CODE (method) == FUNCTION_DECL
2476 && DECL_VINDEX (method))
2477 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2480 if (base_fndecls)
2481 return base_fndecls;
2483 for (i = 0; i < n_baseclasses; i++)
2485 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2486 base_fndecls = chainon (get_basefndecls (name, basetype),
2487 base_fndecls);
2490 return base_fndecls;
2493 /* If this declaration supersedes the declaration of
2494 a method declared virtual in the base class, then
2495 mark this field as being virtual as well. */
2497 void
2498 check_for_override (tree decl, tree ctype)
2500 bool overrides_found = false;
2501 if (TREE_CODE (decl) == TEMPLATE_DECL)
2502 /* In [temp.mem] we have:
2504 A specialization of a member function template does not
2505 override a virtual function from a base class. */
2506 return;
2507 if ((DECL_DESTRUCTOR_P (decl)
2508 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2509 || DECL_CONV_FN_P (decl))
2510 && look_for_overrides (ctype, decl)
2511 && !DECL_STATIC_FUNCTION_P (decl))
2512 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2513 the error_mark_node so that we know it is an overriding
2514 function. */
2516 DECL_VINDEX (decl) = decl;
2517 overrides_found = true;
2520 if (DECL_VIRTUAL_P (decl))
2522 if (!DECL_VINDEX (decl))
2523 DECL_VINDEX (decl) = error_mark_node;
2524 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2525 if (DECL_DESTRUCTOR_P (decl))
2526 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2528 else if (DECL_FINAL_P (decl))
2529 error ("%q+#D marked final, but is not virtual", decl);
2530 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2531 error ("%q+#D marked override, but does not override", decl);
2534 /* Warn about hidden virtual functions that are not overridden in t.
2535 We know that constructors and destructors don't apply. */
2537 static void
2538 warn_hidden (tree t)
2540 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2541 tree fns;
2542 size_t i;
2544 /* We go through each separately named virtual function. */
2545 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2546 VEC_iterate (tree, method_vec, i, fns);
2547 ++i)
2549 tree fn;
2550 tree name;
2551 tree fndecl;
2552 tree base_fndecls;
2553 tree base_binfo;
2554 tree binfo;
2555 int j;
2557 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2558 have the same name. Figure out what name that is. */
2559 name = DECL_NAME (OVL_CURRENT (fns));
2560 /* There are no possibly hidden functions yet. */
2561 base_fndecls = NULL_TREE;
2562 /* Iterate through all of the base classes looking for possibly
2563 hidden functions. */
2564 for (binfo = TYPE_BINFO (t), j = 0;
2565 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2567 tree basetype = BINFO_TYPE (base_binfo);
2568 base_fndecls = chainon (get_basefndecls (name, basetype),
2569 base_fndecls);
2572 /* If there are no functions to hide, continue. */
2573 if (!base_fndecls)
2574 continue;
2576 /* Remove any overridden functions. */
2577 for (fn = fns; fn; fn = OVL_NEXT (fn))
2579 fndecl = OVL_CURRENT (fn);
2580 if (DECL_VINDEX (fndecl))
2582 tree *prev = &base_fndecls;
2584 while (*prev)
2585 /* If the method from the base class has the same
2586 signature as the method from the derived class, it
2587 has been overridden. */
2588 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2589 *prev = TREE_CHAIN (*prev);
2590 else
2591 prev = &TREE_CHAIN (*prev);
2595 /* Now give a warning for all base functions without overriders,
2596 as they are hidden. */
2597 while (base_fndecls)
2599 /* Here we know it is a hider, and no overrider exists. */
2600 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2601 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2602 base_fndecls = TREE_CHAIN (base_fndecls);
2607 /* Check for things that are invalid. There are probably plenty of other
2608 things we should check for also. */
2610 static void
2611 finish_struct_anon (tree t)
2613 tree field;
2615 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2617 if (TREE_STATIC (field))
2618 continue;
2619 if (TREE_CODE (field) != FIELD_DECL)
2620 continue;
2622 if (DECL_NAME (field) == NULL_TREE
2623 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2625 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2626 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2627 for (; elt; elt = DECL_CHAIN (elt))
2629 /* We're generally only interested in entities the user
2630 declared, but we also find nested classes by noticing
2631 the TYPE_DECL that we create implicitly. You're
2632 allowed to put one anonymous union inside another,
2633 though, so we explicitly tolerate that. We use
2634 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2635 we also allow unnamed types used for defining fields. */
2636 if (DECL_ARTIFICIAL (elt)
2637 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2638 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2639 continue;
2641 if (TREE_CODE (elt) != FIELD_DECL)
2643 if (is_union)
2644 permerror (input_location, "%q+#D invalid; an anonymous union can "
2645 "only have non-static data members", elt);
2646 else
2647 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2648 "only have non-static data members", elt);
2649 continue;
2652 if (TREE_PRIVATE (elt))
2654 if (is_union)
2655 permerror (input_location, "private member %q+#D in anonymous union", elt);
2656 else
2657 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2659 else if (TREE_PROTECTED (elt))
2661 if (is_union)
2662 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2663 else
2664 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2667 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2668 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2674 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2675 will be used later during class template instantiation.
2676 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2677 a non-static member data (FIELD_DECL), a member function
2678 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2679 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2680 When FRIEND_P is nonzero, T is either a friend class
2681 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2682 (FUNCTION_DECL, TEMPLATE_DECL). */
2684 void
2685 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2687 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2688 if (CLASSTYPE_TEMPLATE_INFO (type))
2689 CLASSTYPE_DECL_LIST (type)
2690 = tree_cons (friend_p ? NULL_TREE : type,
2691 t, CLASSTYPE_DECL_LIST (type));
2694 /* This function is called from declare_virt_assop_and_dtor via
2695 dfs_walk_all.
2697 DATA is a type that direcly or indirectly inherits the base
2698 represented by BINFO. If BINFO contains a virtual assignment [copy
2699 assignment or move assigment] operator or a virtual constructor,
2700 declare that function in DATA if it hasn't been already declared. */
2702 static tree
2703 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2705 tree bv, fn, t = (tree)data;
2706 tree opname = ansi_assopname (NOP_EXPR);
2708 gcc_assert (t && CLASS_TYPE_P (t));
2709 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2711 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2712 /* A base without a vtable needs no modification, and its bases
2713 are uninteresting. */
2714 return dfs_skip_bases;
2716 if (BINFO_PRIMARY_P (binfo))
2717 /* If this is a primary base, then we have already looked at the
2718 virtual functions of its vtable. */
2719 return NULL_TREE;
2721 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2723 fn = BV_FN (bv);
2725 if (DECL_NAME (fn) == opname)
2727 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2728 lazily_declare_fn (sfk_copy_assignment, t);
2729 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2730 lazily_declare_fn (sfk_move_assignment, t);
2732 else if (DECL_DESTRUCTOR_P (fn)
2733 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2734 lazily_declare_fn (sfk_destructor, t);
2737 return NULL_TREE;
2740 /* If the class type T has a direct or indirect base that contains a
2741 virtual assignment operator or a virtual destructor, declare that
2742 function in T if it hasn't been already declared. */
2744 static void
2745 declare_virt_assop_and_dtor (tree t)
2747 if (!(TYPE_POLYMORPHIC_P (t)
2748 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2749 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2750 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2751 return;
2753 dfs_walk_all (TYPE_BINFO (t),
2754 dfs_declare_virt_assop_and_dtor,
2755 NULL, t);
2758 /* Create default constructors, assignment operators, and so forth for
2759 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2760 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2761 the class cannot have a default constructor, copy constructor
2762 taking a const reference argument, or an assignment operator taking
2763 a const reference, respectively. */
2765 static void
2766 add_implicitly_declared_members (tree t,
2767 int cant_have_const_cctor,
2768 int cant_have_const_assignment)
2770 bool move_ok = false;
2772 if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2773 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2774 && !type_has_move_constructor (t) && !type_has_move_assign (t))
2775 move_ok = true;
2777 /* Destructor. */
2778 if (!CLASSTYPE_DESTRUCTORS (t))
2780 /* In general, we create destructors lazily. */
2781 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2783 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2784 && TYPE_FOR_JAVA (t))
2785 /* But if this is a Java class, any non-trivial destructor is
2786 invalid, even if compiler-generated. Therefore, if the
2787 destructor is non-trivial we create it now. */
2788 lazily_declare_fn (sfk_destructor, t);
2791 /* [class.ctor]
2793 If there is no user-declared constructor for a class, a default
2794 constructor is implicitly declared. */
2795 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2797 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2798 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2799 if (cxx_dialect >= cxx0x)
2800 TYPE_HAS_CONSTEXPR_CTOR (t)
2801 /* This might force the declaration. */
2802 = type_has_constexpr_default_constructor (t);
2805 /* [class.ctor]
2807 If a class definition does not explicitly declare a copy
2808 constructor, one is declared implicitly. */
2809 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
2811 TYPE_HAS_COPY_CTOR (t) = 1;
2812 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2813 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2814 if (move_ok)
2815 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2818 /* If there is no assignment operator, one will be created if and
2819 when it is needed. For now, just record whether or not the type
2820 of the parameter to the assignment operator will be a const or
2821 non-const reference. */
2822 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
2824 TYPE_HAS_COPY_ASSIGN (t) = 1;
2825 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
2826 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
2827 if (move_ok)
2828 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
2831 /* We can't be lazy about declaring functions that might override
2832 a virtual function from a base class. */
2833 declare_virt_assop_and_dtor (t);
2836 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
2837 count the number of fields in TYPE, including anonymous union
2838 members. */
2840 static int
2841 count_fields (tree fields)
2843 tree x;
2844 int n_fields = 0;
2845 for (x = fields; x; x = DECL_CHAIN (x))
2847 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2848 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2849 else
2850 n_fields += 1;
2852 return n_fields;
2855 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
2856 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
2857 elts, starting at offset IDX. */
2859 static int
2860 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2862 tree x;
2863 for (x = fields; x; x = DECL_CHAIN (x))
2865 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2866 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2867 else
2868 field_vec->elts[idx++] = x;
2870 return idx;
2873 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
2874 starting at offset IDX. */
2876 static int
2877 add_enum_fields_to_record_type (tree enumtype,
2878 struct sorted_fields_type *field_vec,
2879 int idx)
2881 tree values;
2882 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
2883 field_vec->elts[idx++] = TREE_VALUE (values);
2884 return idx;
2887 /* FIELD is a bit-field. We are finishing the processing for its
2888 enclosing type. Issue any appropriate messages and set appropriate
2889 flags. Returns false if an error has been diagnosed. */
2891 static bool
2892 check_bitfield_decl (tree field)
2894 tree type = TREE_TYPE (field);
2895 tree w;
2897 /* Extract the declared width of the bitfield, which has been
2898 temporarily stashed in DECL_INITIAL. */
2899 w = DECL_INITIAL (field);
2900 gcc_assert (w != NULL_TREE);
2901 /* Remove the bit-field width indicator so that the rest of the
2902 compiler does not treat that value as an initializer. */
2903 DECL_INITIAL (field) = NULL_TREE;
2905 /* Detect invalid bit-field type. */
2906 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2908 error ("bit-field %q+#D with non-integral type", field);
2909 w = error_mark_node;
2911 else
2913 location_t loc = input_location;
2914 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2915 STRIP_NOPS (w);
2917 /* detect invalid field size. */
2918 input_location = DECL_SOURCE_LOCATION (field);
2919 w = cxx_constant_value (w);
2920 input_location = loc;
2922 if (TREE_CODE (w) != INTEGER_CST)
2924 error ("bit-field %q+D width not an integer constant", field);
2925 w = error_mark_node;
2927 else if (tree_int_cst_sgn (w) < 0)
2929 error ("negative width in bit-field %q+D", field);
2930 w = error_mark_node;
2932 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2934 error ("zero width for bit-field %q+D", field);
2935 w = error_mark_node;
2937 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2938 && TREE_CODE (type) != ENUMERAL_TYPE
2939 && TREE_CODE (type) != BOOLEAN_TYPE)
2940 warning (0, "width of %q+D exceeds its type", field);
2941 else if (TREE_CODE (type) == ENUMERAL_TYPE
2942 && (0 > (compare_tree_int
2943 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
2944 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2947 if (w != error_mark_node)
2949 DECL_SIZE (field) = convert (bitsizetype, w);
2950 DECL_BIT_FIELD (field) = 1;
2951 return true;
2953 else
2955 /* Non-bit-fields are aligned for their type. */
2956 DECL_BIT_FIELD (field) = 0;
2957 CLEAR_DECL_C_BIT_FIELD (field);
2958 return false;
2962 /* FIELD is a non bit-field. We are finishing the processing for its
2963 enclosing type T. Issue any appropriate messages and set appropriate
2964 flags. */
2966 static void
2967 check_field_decl (tree field,
2968 tree t,
2969 int* cant_have_const_ctor,
2970 int* no_const_asn_ref,
2971 int* any_default_members)
2973 tree type = strip_array_types (TREE_TYPE (field));
2975 /* In C++98 an anonymous union cannot contain any fields which would change
2976 the settings of CANT_HAVE_CONST_CTOR and friends. */
2977 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
2979 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
2980 structs. So, we recurse through their fields here. */
2981 else if (ANON_AGGR_TYPE_P (type))
2983 tree fields;
2985 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2986 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2987 check_field_decl (fields, t, cant_have_const_ctor,
2988 no_const_asn_ref, any_default_members);
2990 /* Check members with class type for constructors, destructors,
2991 etc. */
2992 else if (CLASS_TYPE_P (type))
2994 /* Never let anything with uninheritable virtuals
2995 make it through without complaint. */
2996 abstract_virtuals_error (field, type);
2998 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
3000 static bool warned;
3001 int oldcount = errorcount;
3002 if (TYPE_NEEDS_CONSTRUCTING (type))
3003 error ("member %q+#D with constructor not allowed in union",
3004 field);
3005 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3006 error ("member %q+#D with destructor not allowed in union", field);
3007 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3008 error ("member %q+#D with copy assignment operator not allowed in union",
3009 field);
3010 if (!warned && errorcount > oldcount)
3012 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3013 "only available with -std=c++11 or -std=gnu++11");
3014 warned = true;
3017 else
3019 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3020 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3021 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3022 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3023 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3024 || !TYPE_HAS_COPY_ASSIGN (type));
3025 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3026 || !TYPE_HAS_COPY_CTOR (type));
3027 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3028 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3029 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3030 || TYPE_HAS_COMPLEX_DFLT (type));
3033 if (TYPE_HAS_COPY_CTOR (type)
3034 && !TYPE_HAS_CONST_COPY_CTOR (type))
3035 *cant_have_const_ctor = 1;
3037 if (TYPE_HAS_COPY_ASSIGN (type)
3038 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3039 *no_const_asn_ref = 1;
3041 if (DECL_INITIAL (field) != NULL_TREE)
3043 /* `build_class_init_list' does not recognize
3044 non-FIELD_DECLs. */
3045 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3046 error ("multiple fields in union %qT initialized", t);
3047 *any_default_members = 1;
3051 /* Check the data members (both static and non-static), class-scoped
3052 typedefs, etc., appearing in the declaration of T. Issue
3053 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3054 declaration order) of access declarations; each TREE_VALUE in this
3055 list is a USING_DECL.
3057 In addition, set the following flags:
3059 EMPTY_P
3060 The class is empty, i.e., contains no non-static data members.
3062 CANT_HAVE_CONST_CTOR_P
3063 This class cannot have an implicitly generated copy constructor
3064 taking a const reference.
3066 CANT_HAVE_CONST_ASN_REF
3067 This class cannot have an implicitly generated assignment
3068 operator taking a const reference.
3070 All of these flags should be initialized before calling this
3071 function.
3073 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3074 fields can be added by adding to this chain. */
3076 static void
3077 check_field_decls (tree t, tree *access_decls,
3078 int *cant_have_const_ctor_p,
3079 int *no_const_asn_ref_p)
3081 tree *field;
3082 tree *next;
3083 bool has_pointers;
3084 int any_default_members;
3085 int cant_pack = 0;
3086 int field_access = -1;
3088 /* Assume there are no access declarations. */
3089 *access_decls = NULL_TREE;
3090 /* Assume this class has no pointer members. */
3091 has_pointers = false;
3092 /* Assume none of the members of this class have default
3093 initializations. */
3094 any_default_members = 0;
3096 for (field = &TYPE_FIELDS (t); *field; field = next)
3098 tree x = *field;
3099 tree type = TREE_TYPE (x);
3100 int this_field_access;
3102 next = &DECL_CHAIN (x);
3104 if (TREE_CODE (x) == USING_DECL)
3106 /* Save the access declarations for our caller. */
3107 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3108 continue;
3111 if (TREE_CODE (x) == TYPE_DECL
3112 || TREE_CODE (x) == TEMPLATE_DECL)
3113 continue;
3115 /* If we've gotten this far, it's a data member, possibly static,
3116 or an enumerator. */
3117 DECL_CONTEXT (x) = t;
3119 /* When this goes into scope, it will be a non-local reference. */
3120 DECL_NONLOCAL (x) = 1;
3122 if (TREE_CODE (t) == UNION_TYPE)
3124 /* [class.union]
3126 If a union contains a static data member, or a member of
3127 reference type, the program is ill-formed. */
3128 if (TREE_CODE (x) == VAR_DECL)
3130 error ("%q+D may not be static because it is a member of a union", x);
3131 continue;
3133 if (TREE_CODE (type) == REFERENCE_TYPE)
3135 error ("%q+D may not have reference type %qT because"
3136 " it is a member of a union",
3137 x, type);
3138 continue;
3142 /* Perform error checking that did not get done in
3143 grokdeclarator. */
3144 if (TREE_CODE (type) == FUNCTION_TYPE)
3146 error ("field %q+D invalidly declared function type", x);
3147 type = build_pointer_type (type);
3148 TREE_TYPE (x) = type;
3150 else if (TREE_CODE (type) == METHOD_TYPE)
3152 error ("field %q+D invalidly declared method type", x);
3153 type = build_pointer_type (type);
3154 TREE_TYPE (x) = type;
3157 if (type == error_mark_node)
3158 continue;
3160 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3161 continue;
3163 /* Now it can only be a FIELD_DECL. */
3165 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3166 CLASSTYPE_NON_AGGREGATE (t) = 1;
3168 /* If at least one non-static data member is non-literal, the whole
3169 class becomes non-literal. Note: if the type is incomplete we
3170 will complain later on. */
3171 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3172 CLASSTYPE_LITERAL_P (t) = false;
3174 /* A standard-layout class is a class that:
3176 has the same access control (Clause 11) for all non-static data members,
3177 ... */
3178 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3179 if (field_access == -1)
3180 field_access = this_field_access;
3181 else if (this_field_access != field_access)
3182 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3184 /* If this is of reference type, check if it needs an init. */
3185 if (TREE_CODE (type) == REFERENCE_TYPE)
3187 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3188 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3189 if (DECL_INITIAL (x) == NULL_TREE)
3190 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3192 /* ARM $12.6.2: [A member initializer list] (or, for an
3193 aggregate, initialization by a brace-enclosed list) is the
3194 only way to initialize nonstatic const and reference
3195 members. */
3196 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3197 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3200 type = strip_array_types (type);
3202 if (TYPE_PACKED (t))
3204 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3206 warning
3208 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3210 cant_pack = 1;
3212 else if (DECL_C_BIT_FIELD (x)
3213 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3214 DECL_PACKED (x) = 1;
3217 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3218 /* We don't treat zero-width bitfields as making a class
3219 non-empty. */
3221 else
3223 /* The class is non-empty. */
3224 CLASSTYPE_EMPTY_P (t) = 0;
3225 /* The class is not even nearly empty. */
3226 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3227 /* If one of the data members contains an empty class,
3228 so does T. */
3229 if (CLASS_TYPE_P (type)
3230 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3231 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3234 /* This is used by -Weffc++ (see below). Warn only for pointers
3235 to members which might hold dynamic memory. So do not warn
3236 for pointers to functions or pointers to members. */
3237 if (TYPE_PTR_P (type)
3238 && !TYPE_PTRFN_P (type))
3239 has_pointers = true;
3241 if (CLASS_TYPE_P (type))
3243 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3244 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3245 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3246 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3249 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3250 CLASSTYPE_HAS_MUTABLE (t) = 1;
3252 if (! layout_pod_type_p (type))
3253 /* DR 148 now allows pointers to members (which are POD themselves),
3254 to be allowed in POD structs. */
3255 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3257 if (!std_layout_type_p (type))
3258 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3260 if (! zero_init_p (type))
3261 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3263 /* We set DECL_C_BIT_FIELD in grokbitfield.
3264 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3265 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3266 check_field_decl (x, t,
3267 cant_have_const_ctor_p,
3268 no_const_asn_ref_p,
3269 &any_default_members);
3271 /* Now that we've removed bit-field widths from DECL_INITIAL,
3272 anything left in DECL_INITIAL is an NSDMI that makes the class
3273 non-aggregate. */
3274 if (DECL_INITIAL (x))
3275 CLASSTYPE_NON_AGGREGATE (t) = true;
3277 /* If any field is const, the structure type is pseudo-const. */
3278 if (CP_TYPE_CONST_P (type))
3280 C_TYPE_FIELDS_READONLY (t) = 1;
3281 if (DECL_INITIAL (x) == NULL_TREE)
3282 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3284 /* ARM $12.6.2: [A member initializer list] (or, for an
3285 aggregate, initialization by a brace-enclosed list) is the
3286 only way to initialize nonstatic const and reference
3287 members. */
3288 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3289 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3291 /* A field that is pseudo-const makes the structure likewise. */
3292 else if (CLASS_TYPE_P (type))
3294 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3295 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3296 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3297 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3300 /* Core issue 80: A nonstatic data member is required to have a
3301 different name from the class iff the class has a
3302 user-declared constructor. */
3303 if (constructor_name_p (DECL_NAME (x), t)
3304 && TYPE_HAS_USER_CONSTRUCTOR (t))
3305 permerror (input_location, "field %q+#D with same name as class", x);
3308 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3309 it should also define a copy constructor and an assignment operator to
3310 implement the correct copy semantic (deep vs shallow, etc.). As it is
3311 not feasible to check whether the constructors do allocate dynamic memory
3312 and store it within members, we approximate the warning like this:
3314 -- Warn only if there are members which are pointers
3315 -- Warn only if there is a non-trivial constructor (otherwise,
3316 there cannot be memory allocated).
3317 -- Warn only if there is a non-trivial destructor. We assume that the
3318 user at least implemented the cleanup correctly, and a destructor
3319 is needed to free dynamic memory.
3321 This seems enough for practical purposes. */
3322 if (warn_ecpp
3323 && has_pointers
3324 && TYPE_HAS_USER_CONSTRUCTOR (t)
3325 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3326 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3328 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3330 if (! TYPE_HAS_COPY_CTOR (t))
3332 warning (OPT_Weffc__,
3333 " but does not override %<%T(const %T&)%>", t, t);
3334 if (!TYPE_HAS_COPY_ASSIGN (t))
3335 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3337 else if (! TYPE_HAS_COPY_ASSIGN (t))
3338 warning (OPT_Weffc__,
3339 " but does not override %<operator=(const %T&)%>", t);
3342 /* Non-static data member initializers make the default constructor
3343 non-trivial. */
3344 if (any_default_members)
3346 TYPE_NEEDS_CONSTRUCTING (t) = true;
3347 TYPE_HAS_COMPLEX_DFLT (t) = true;
3350 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3351 if (cant_pack)
3352 TYPE_PACKED (t) = 0;
3354 /* Check anonymous struct/anonymous union fields. */
3355 finish_struct_anon (t);
3357 /* We've built up the list of access declarations in reverse order.
3358 Fix that now. */
3359 *access_decls = nreverse (*access_decls);
3362 /* If TYPE is an empty class type, records its OFFSET in the table of
3363 OFFSETS. */
3365 static int
3366 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3368 splay_tree_node n;
3370 if (!is_empty_class (type))
3371 return 0;
3373 /* Record the location of this empty object in OFFSETS. */
3374 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3375 if (!n)
3376 n = splay_tree_insert (offsets,
3377 (splay_tree_key) offset,
3378 (splay_tree_value) NULL_TREE);
3379 n->value = ((splay_tree_value)
3380 tree_cons (NULL_TREE,
3381 type,
3382 (tree) n->value));
3384 return 0;
3387 /* Returns nonzero if TYPE is an empty class type and there is
3388 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3390 static int
3391 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3393 splay_tree_node n;
3394 tree t;
3396 if (!is_empty_class (type))
3397 return 0;
3399 /* Record the location of this empty object in OFFSETS. */
3400 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3401 if (!n)
3402 return 0;
3404 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3405 if (same_type_p (TREE_VALUE (t), type))
3406 return 1;
3408 return 0;
3411 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3412 F for every subobject, passing it the type, offset, and table of
3413 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3414 be traversed.
3416 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3417 than MAX_OFFSET will not be walked.
3419 If F returns a nonzero value, the traversal ceases, and that value
3420 is returned. Otherwise, returns zero. */
3422 static int
3423 walk_subobject_offsets (tree type,
3424 subobject_offset_fn f,
3425 tree offset,
3426 splay_tree offsets,
3427 tree max_offset,
3428 int vbases_p)
3430 int r = 0;
3431 tree type_binfo = NULL_TREE;
3433 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3434 stop. */
3435 if (max_offset && INT_CST_LT (max_offset, offset))
3436 return 0;
3438 if (type == error_mark_node)
3439 return 0;
3441 if (!TYPE_P (type))
3443 if (abi_version_at_least (2))
3444 type_binfo = type;
3445 type = BINFO_TYPE (type);
3448 if (CLASS_TYPE_P (type))
3450 tree field;
3451 tree binfo;
3452 int i;
3454 /* Avoid recursing into objects that are not interesting. */
3455 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3456 return 0;
3458 /* Record the location of TYPE. */
3459 r = (*f) (type, offset, offsets);
3460 if (r)
3461 return r;
3463 /* Iterate through the direct base classes of TYPE. */
3464 if (!type_binfo)
3465 type_binfo = TYPE_BINFO (type);
3466 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3468 tree binfo_offset;
3470 if (abi_version_at_least (2)
3471 && BINFO_VIRTUAL_P (binfo))
3472 continue;
3474 if (!vbases_p
3475 && BINFO_VIRTUAL_P (binfo)
3476 && !BINFO_PRIMARY_P (binfo))
3477 continue;
3479 if (!abi_version_at_least (2))
3480 binfo_offset = size_binop (PLUS_EXPR,
3481 offset,
3482 BINFO_OFFSET (binfo));
3483 else
3485 tree orig_binfo;
3486 /* We cannot rely on BINFO_OFFSET being set for the base
3487 class yet, but the offsets for direct non-virtual
3488 bases can be calculated by going back to the TYPE. */
3489 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3490 binfo_offset = size_binop (PLUS_EXPR,
3491 offset,
3492 BINFO_OFFSET (orig_binfo));
3495 r = walk_subobject_offsets (binfo,
3497 binfo_offset,
3498 offsets,
3499 max_offset,
3500 (abi_version_at_least (2)
3501 ? /*vbases_p=*/0 : vbases_p));
3502 if (r)
3503 return r;
3506 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3508 unsigned ix;
3509 VEC(tree,gc) *vbases;
3511 /* Iterate through the virtual base classes of TYPE. In G++
3512 3.2, we included virtual bases in the direct base class
3513 loop above, which results in incorrect results; the
3514 correct offsets for virtual bases are only known when
3515 working with the most derived type. */
3516 if (vbases_p)
3517 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3518 VEC_iterate (tree, vbases, ix, binfo); ix++)
3520 r = walk_subobject_offsets (binfo,
3522 size_binop (PLUS_EXPR,
3523 offset,
3524 BINFO_OFFSET (binfo)),
3525 offsets,
3526 max_offset,
3527 /*vbases_p=*/0);
3528 if (r)
3529 return r;
3531 else
3533 /* We still have to walk the primary base, if it is
3534 virtual. (If it is non-virtual, then it was walked
3535 above.) */
3536 tree vbase = get_primary_binfo (type_binfo);
3538 if (vbase && BINFO_VIRTUAL_P (vbase)
3539 && BINFO_PRIMARY_P (vbase)
3540 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3542 r = (walk_subobject_offsets
3543 (vbase, f, offset,
3544 offsets, max_offset, /*vbases_p=*/0));
3545 if (r)
3546 return r;
3551 /* Iterate through the fields of TYPE. */
3552 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3553 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3555 tree field_offset;
3557 if (abi_version_at_least (2))
3558 field_offset = byte_position (field);
3559 else
3560 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3561 field_offset = DECL_FIELD_OFFSET (field);
3563 r = walk_subobject_offsets (TREE_TYPE (field),
3565 size_binop (PLUS_EXPR,
3566 offset,
3567 field_offset),
3568 offsets,
3569 max_offset,
3570 /*vbases_p=*/1);
3571 if (r)
3572 return r;
3575 else if (TREE_CODE (type) == ARRAY_TYPE)
3577 tree element_type = strip_array_types (type);
3578 tree domain = TYPE_DOMAIN (type);
3579 tree index;
3581 /* Avoid recursing into objects that are not interesting. */
3582 if (!CLASS_TYPE_P (element_type)
3583 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3584 return 0;
3586 /* Step through each of the elements in the array. */
3587 for (index = size_zero_node;
3588 /* G++ 3.2 had an off-by-one error here. */
3589 (abi_version_at_least (2)
3590 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3591 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3592 index = size_binop (PLUS_EXPR, index, size_one_node))
3594 r = walk_subobject_offsets (TREE_TYPE (type),
3596 offset,
3597 offsets,
3598 max_offset,
3599 /*vbases_p=*/1);
3600 if (r)
3601 return r;
3602 offset = size_binop (PLUS_EXPR, offset,
3603 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3604 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3605 there's no point in iterating through the remaining
3606 elements of the array. */
3607 if (max_offset && INT_CST_LT (max_offset, offset))
3608 break;
3612 return 0;
3615 /* Record all of the empty subobjects of TYPE (either a type or a
3616 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3617 is being placed at OFFSET; otherwise, it is a base class that is
3618 being placed at OFFSET. */
3620 static void
3621 record_subobject_offsets (tree type,
3622 tree offset,
3623 splay_tree offsets,
3624 bool is_data_member)
3626 tree max_offset;
3627 /* If recording subobjects for a non-static data member or a
3628 non-empty base class , we do not need to record offsets beyond
3629 the size of the biggest empty class. Additional data members
3630 will go at the end of the class. Additional base classes will go
3631 either at offset zero (if empty, in which case they cannot
3632 overlap with offsets past the size of the biggest empty class) or
3633 at the end of the class.
3635 However, if we are placing an empty base class, then we must record
3636 all offsets, as either the empty class is at offset zero (where
3637 other empty classes might later be placed) or at the end of the
3638 class (where other objects might then be placed, so other empty
3639 subobjects might later overlap). */
3640 if (is_data_member
3641 || !is_empty_class (BINFO_TYPE (type)))
3642 max_offset = sizeof_biggest_empty_class;
3643 else
3644 max_offset = NULL_TREE;
3645 walk_subobject_offsets (type, record_subobject_offset, offset,
3646 offsets, max_offset, is_data_member);
3649 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3650 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3651 virtual bases of TYPE are examined. */
3653 static int
3654 layout_conflict_p (tree type,
3655 tree offset,
3656 splay_tree offsets,
3657 int vbases_p)
3659 splay_tree_node max_node;
3661 /* Get the node in OFFSETS that indicates the maximum offset where
3662 an empty subobject is located. */
3663 max_node = splay_tree_max (offsets);
3664 /* If there aren't any empty subobjects, then there's no point in
3665 performing this check. */
3666 if (!max_node)
3667 return 0;
3669 return walk_subobject_offsets (type, check_subobject_offset, offset,
3670 offsets, (tree) (max_node->key),
3671 vbases_p);
3674 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3675 non-static data member of the type indicated by RLI. BINFO is the
3676 binfo corresponding to the base subobject, OFFSETS maps offsets to
3677 types already located at those offsets. This function determines
3678 the position of the DECL. */
3680 static void
3681 layout_nonempty_base_or_field (record_layout_info rli,
3682 tree decl,
3683 tree binfo,
3684 splay_tree offsets)
3686 tree offset = NULL_TREE;
3687 bool field_p;
3688 tree type;
3690 if (binfo)
3692 /* For the purposes of determining layout conflicts, we want to
3693 use the class type of BINFO; TREE_TYPE (DECL) will be the
3694 CLASSTYPE_AS_BASE version, which does not contain entries for
3695 zero-sized bases. */
3696 type = TREE_TYPE (binfo);
3697 field_p = false;
3699 else
3701 type = TREE_TYPE (decl);
3702 field_p = true;
3705 /* Try to place the field. It may take more than one try if we have
3706 a hard time placing the field without putting two objects of the
3707 same type at the same address. */
3708 while (1)
3710 struct record_layout_info_s old_rli = *rli;
3712 /* Place this field. */
3713 place_field (rli, decl);
3714 offset = byte_position (decl);
3716 /* We have to check to see whether or not there is already
3717 something of the same type at the offset we're about to use.
3718 For example, consider:
3720 struct S {};
3721 struct T : public S { int i; };
3722 struct U : public S, public T {};
3724 Here, we put S at offset zero in U. Then, we can't put T at
3725 offset zero -- its S component would be at the same address
3726 as the S we already allocated. So, we have to skip ahead.
3727 Since all data members, including those whose type is an
3728 empty class, have nonzero size, any overlap can happen only
3729 with a direct or indirect base-class -- it can't happen with
3730 a data member. */
3731 /* In a union, overlap is permitted; all members are placed at
3732 offset zero. */
3733 if (TREE_CODE (rli->t) == UNION_TYPE)
3734 break;
3735 /* G++ 3.2 did not check for overlaps when placing a non-empty
3736 virtual base. */
3737 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3738 break;
3739 if (layout_conflict_p (field_p ? type : binfo, offset,
3740 offsets, field_p))
3742 /* Strip off the size allocated to this field. That puts us
3743 at the first place we could have put the field with
3744 proper alignment. */
3745 *rli = old_rli;
3747 /* Bump up by the alignment required for the type. */
3748 rli->bitpos
3749 = size_binop (PLUS_EXPR, rli->bitpos,
3750 bitsize_int (binfo
3751 ? CLASSTYPE_ALIGN (type)
3752 : TYPE_ALIGN (type)));
3753 normalize_rli (rli);
3755 else
3756 /* There was no conflict. We're done laying out this field. */
3757 break;
3760 /* Now that we know where it will be placed, update its
3761 BINFO_OFFSET. */
3762 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3763 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3764 this point because their BINFO_OFFSET is copied from another
3765 hierarchy. Therefore, we may not need to add the entire
3766 OFFSET. */
3767 propagate_binfo_offsets (binfo,
3768 size_diffop_loc (input_location,
3769 convert (ssizetype, offset),
3770 convert (ssizetype,
3771 BINFO_OFFSET (binfo))));
3774 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3776 static int
3777 empty_base_at_nonzero_offset_p (tree type,
3778 tree offset,
3779 splay_tree offsets ATTRIBUTE_UNUSED)
3781 return is_empty_class (type) && !integer_zerop (offset);
3784 /* Layout the empty base BINFO. EOC indicates the byte currently just
3785 past the end of the class, and should be correctly aligned for a
3786 class of the type indicated by BINFO; OFFSETS gives the offsets of
3787 the empty bases allocated so far. T is the most derived
3788 type. Return nonzero iff we added it at the end. */
3790 static bool
3791 layout_empty_base (record_layout_info rli, tree binfo,
3792 tree eoc, splay_tree offsets)
3794 tree alignment;
3795 tree basetype = BINFO_TYPE (binfo);
3796 bool atend = false;
3798 /* This routine should only be used for empty classes. */
3799 gcc_assert (is_empty_class (basetype));
3800 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3802 if (!integer_zerop (BINFO_OFFSET (binfo)))
3804 if (abi_version_at_least (2))
3805 propagate_binfo_offsets
3806 (binfo, size_diffop_loc (input_location,
3807 size_zero_node, BINFO_OFFSET (binfo)));
3808 else
3809 warning (OPT_Wabi,
3810 "offset of empty base %qT may not be ABI-compliant and may"
3811 "change in a future version of GCC",
3812 BINFO_TYPE (binfo));
3815 /* This is an empty base class. We first try to put it at offset
3816 zero. */
3817 if (layout_conflict_p (binfo,
3818 BINFO_OFFSET (binfo),
3819 offsets,
3820 /*vbases_p=*/0))
3822 /* That didn't work. Now, we move forward from the next
3823 available spot in the class. */
3824 atend = true;
3825 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3826 while (1)
3828 if (!layout_conflict_p (binfo,
3829 BINFO_OFFSET (binfo),
3830 offsets,
3831 /*vbases_p=*/0))
3832 /* We finally found a spot where there's no overlap. */
3833 break;
3835 /* There's overlap here, too. Bump along to the next spot. */
3836 propagate_binfo_offsets (binfo, alignment);
3840 if (CLASSTYPE_USER_ALIGN (basetype))
3842 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
3843 if (warn_packed)
3844 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
3845 TYPE_USER_ALIGN (rli->t) = 1;
3848 return atend;
3851 /* Layout the base given by BINFO in the class indicated by RLI.
3852 *BASE_ALIGN is a running maximum of the alignments of
3853 any base class. OFFSETS gives the location of empty base
3854 subobjects. T is the most derived type. Return nonzero if the new
3855 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3856 *NEXT_FIELD, unless BINFO is for an empty base class.
3858 Returns the location at which the next field should be inserted. */
3860 static tree *
3861 build_base_field (record_layout_info rli, tree binfo,
3862 splay_tree offsets, tree *next_field)
3864 tree t = rli->t;
3865 tree basetype = BINFO_TYPE (binfo);
3867 if (!COMPLETE_TYPE_P (basetype))
3868 /* This error is now reported in xref_tag, thus giving better
3869 location information. */
3870 return next_field;
3872 /* Place the base class. */
3873 if (!is_empty_class (basetype))
3875 tree decl;
3877 /* The containing class is non-empty because it has a non-empty
3878 base class. */
3879 CLASSTYPE_EMPTY_P (t) = 0;
3881 /* Create the FIELD_DECL. */
3882 decl = build_decl (input_location,
3883 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3884 DECL_ARTIFICIAL (decl) = 1;
3885 DECL_IGNORED_P (decl) = 1;
3886 DECL_FIELD_CONTEXT (decl) = t;
3887 if (CLASSTYPE_AS_BASE (basetype))
3889 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3890 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3891 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3892 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3893 DECL_MODE (decl) = TYPE_MODE (basetype);
3894 DECL_FIELD_IS_BASE (decl) = 1;
3896 /* Try to place the field. It may take more than one try if we
3897 have a hard time placing the field without putting two
3898 objects of the same type at the same address. */
3899 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3900 /* Add the new FIELD_DECL to the list of fields for T. */
3901 DECL_CHAIN (decl) = *next_field;
3902 *next_field = decl;
3903 next_field = &DECL_CHAIN (decl);
3906 else
3908 tree eoc;
3909 bool atend;
3911 /* On some platforms (ARM), even empty classes will not be
3912 byte-aligned. */
3913 eoc = round_up_loc (input_location,
3914 rli_size_unit_so_far (rli),
3915 CLASSTYPE_ALIGN_UNIT (basetype));
3916 atend = layout_empty_base (rli, binfo, eoc, offsets);
3917 /* A nearly-empty class "has no proper base class that is empty,
3918 not morally virtual, and at an offset other than zero." */
3919 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3921 if (atend)
3922 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3923 /* The check above (used in G++ 3.2) is insufficient because
3924 an empty class placed at offset zero might itself have an
3925 empty base at a nonzero offset. */
3926 else if (walk_subobject_offsets (basetype,
3927 empty_base_at_nonzero_offset_p,
3928 size_zero_node,
3929 /*offsets=*/NULL,
3930 /*max_offset=*/NULL_TREE,
3931 /*vbases_p=*/true))
3933 if (abi_version_at_least (2))
3934 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3935 else
3936 warning (OPT_Wabi,
3937 "class %qT will be considered nearly empty in a "
3938 "future version of GCC", t);
3942 /* We do not create a FIELD_DECL for empty base classes because
3943 it might overlap some other field. We want to be able to
3944 create CONSTRUCTORs for the class by iterating over the
3945 FIELD_DECLs, and the back end does not handle overlapping
3946 FIELD_DECLs. */
3948 /* An empty virtual base causes a class to be non-empty
3949 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3950 here because that was already done when the virtual table
3951 pointer was created. */
3954 /* Record the offsets of BINFO and its base subobjects. */
3955 record_subobject_offsets (binfo,
3956 BINFO_OFFSET (binfo),
3957 offsets,
3958 /*is_data_member=*/false);
3960 return next_field;
3963 /* Layout all of the non-virtual base classes. Record empty
3964 subobjects in OFFSETS. T is the most derived type. Return nonzero
3965 if the type cannot be nearly empty. The fields created
3966 corresponding to the base classes will be inserted at
3967 *NEXT_FIELD. */
3969 static void
3970 build_base_fields (record_layout_info rli,
3971 splay_tree offsets, tree *next_field)
3973 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3974 subobjects. */
3975 tree t = rli->t;
3976 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3977 int i;
3979 /* The primary base class is always allocated first. */
3980 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3981 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3982 offsets, next_field);
3984 /* Now allocate the rest of the bases. */
3985 for (i = 0; i < n_baseclasses; ++i)
3987 tree base_binfo;
3989 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
3991 /* The primary base was already allocated above, so we don't
3992 need to allocate it again here. */
3993 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3994 continue;
3996 /* Virtual bases are added at the end (a primary virtual base
3997 will have already been added). */
3998 if (BINFO_VIRTUAL_P (base_binfo))
3999 continue;
4001 next_field = build_base_field (rli, base_binfo,
4002 offsets, next_field);
4006 /* Go through the TYPE_METHODS of T issuing any appropriate
4007 diagnostics, figuring out which methods override which other
4008 methods, and so forth. */
4010 static void
4011 check_methods (tree t)
4013 tree x;
4015 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4017 check_for_override (x, t);
4018 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4019 error ("initializer specified for non-virtual method %q+D", x);
4020 /* The name of the field is the original field name
4021 Save this in auxiliary field for later overloading. */
4022 if (DECL_VINDEX (x))
4024 TYPE_POLYMORPHIC_P (t) = 1;
4025 if (DECL_PURE_VIRTUAL_P (x))
4026 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
4028 /* All user-provided destructors are non-trivial.
4029 Constructors and assignment ops are handled in
4030 grok_special_member_properties. */
4031 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4032 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4036 /* FN is a constructor or destructor. Clone the declaration to create
4037 a specialized in-charge or not-in-charge version, as indicated by
4038 NAME. */
4040 static tree
4041 build_clone (tree fn, tree name)
4043 tree parms;
4044 tree clone;
4046 /* Copy the function. */
4047 clone = copy_decl (fn);
4048 /* Reset the function name. */
4049 DECL_NAME (clone) = name;
4050 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4051 /* Remember where this function came from. */
4052 DECL_ABSTRACT_ORIGIN (clone) = fn;
4053 /* Make it easy to find the CLONE given the FN. */
4054 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4055 DECL_CHAIN (fn) = clone;
4057 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4058 if (TREE_CODE (clone) == TEMPLATE_DECL)
4060 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4061 DECL_TEMPLATE_RESULT (clone) = result;
4062 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4063 DECL_TI_TEMPLATE (result) = clone;
4064 TREE_TYPE (clone) = TREE_TYPE (result);
4065 return clone;
4068 DECL_CLONED_FUNCTION (clone) = fn;
4069 /* There's no pending inline data for this function. */
4070 DECL_PENDING_INLINE_INFO (clone) = NULL;
4071 DECL_PENDING_INLINE_P (clone) = 0;
4073 /* The base-class destructor is not virtual. */
4074 if (name == base_dtor_identifier)
4076 DECL_VIRTUAL_P (clone) = 0;
4077 if (TREE_CODE (clone) != TEMPLATE_DECL)
4078 DECL_VINDEX (clone) = NULL_TREE;
4081 /* If there was an in-charge parameter, drop it from the function
4082 type. */
4083 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4085 tree basetype;
4086 tree parmtypes;
4087 tree exceptions;
4089 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4090 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4091 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4092 /* Skip the `this' parameter. */
4093 parmtypes = TREE_CHAIN (parmtypes);
4094 /* Skip the in-charge parameter. */
4095 parmtypes = TREE_CHAIN (parmtypes);
4096 /* And the VTT parm, in a complete [cd]tor. */
4097 if (DECL_HAS_VTT_PARM_P (fn)
4098 && ! DECL_NEEDS_VTT_PARM_P (clone))
4099 parmtypes = TREE_CHAIN (parmtypes);
4100 /* If this is subobject constructor or destructor, add the vtt
4101 parameter. */
4102 TREE_TYPE (clone)
4103 = build_method_type_directly (basetype,
4104 TREE_TYPE (TREE_TYPE (clone)),
4105 parmtypes);
4106 if (exceptions)
4107 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4108 exceptions);
4109 TREE_TYPE (clone)
4110 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4111 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4114 /* Copy the function parameters. */
4115 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4116 /* Remove the in-charge parameter. */
4117 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4119 DECL_CHAIN (DECL_ARGUMENTS (clone))
4120 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4121 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4123 /* And the VTT parm, in a complete [cd]tor. */
4124 if (DECL_HAS_VTT_PARM_P (fn))
4126 if (DECL_NEEDS_VTT_PARM_P (clone))
4127 DECL_HAS_VTT_PARM_P (clone) = 1;
4128 else
4130 DECL_CHAIN (DECL_ARGUMENTS (clone))
4131 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4132 DECL_HAS_VTT_PARM_P (clone) = 0;
4136 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4138 DECL_CONTEXT (parms) = clone;
4139 cxx_dup_lang_specific_decl (parms);
4142 /* Create the RTL for this function. */
4143 SET_DECL_RTL (clone, NULL);
4144 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4146 if (pch_file)
4147 note_decl_for_pch (clone);
4149 return clone;
4152 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4153 not invoke this function directly.
4155 For a non-thunk function, returns the address of the slot for storing
4156 the function it is a clone of. Otherwise returns NULL_TREE.
4158 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4159 cloned_function is unset. This is to support the separate
4160 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4161 on a template makes sense, but not the former. */
4163 tree *
4164 decl_cloned_function_p (const_tree decl, bool just_testing)
4166 tree *ptr;
4167 if (just_testing)
4168 decl = STRIP_TEMPLATE (decl);
4170 if (TREE_CODE (decl) != FUNCTION_DECL
4171 || !DECL_LANG_SPECIFIC (decl)
4172 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4174 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4175 if (!just_testing)
4176 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4177 else
4178 #endif
4179 return NULL;
4182 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4183 if (just_testing && *ptr == NULL_TREE)
4184 return NULL;
4185 else
4186 return ptr;
4189 /* Produce declarations for all appropriate clones of FN. If
4190 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4191 CLASTYPE_METHOD_VEC as well. */
4193 void
4194 clone_function_decl (tree fn, int update_method_vec_p)
4196 tree clone;
4198 /* Avoid inappropriate cloning. */
4199 if (DECL_CHAIN (fn)
4200 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4201 return;
4203 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4205 /* For each constructor, we need two variants: an in-charge version
4206 and a not-in-charge version. */
4207 clone = build_clone (fn, complete_ctor_identifier);
4208 if (update_method_vec_p)
4209 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4210 clone = build_clone (fn, base_ctor_identifier);
4211 if (update_method_vec_p)
4212 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4214 else
4216 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4218 /* For each destructor, we need three variants: an in-charge
4219 version, a not-in-charge version, and an in-charge deleting
4220 version. We clone the deleting version first because that
4221 means it will go second on the TYPE_METHODS list -- and that
4222 corresponds to the correct layout order in the virtual
4223 function table.
4225 For a non-virtual destructor, we do not build a deleting
4226 destructor. */
4227 if (DECL_VIRTUAL_P (fn))
4229 clone = build_clone (fn, deleting_dtor_identifier);
4230 if (update_method_vec_p)
4231 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4233 clone = build_clone (fn, complete_dtor_identifier);
4234 if (update_method_vec_p)
4235 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4236 clone = build_clone (fn, base_dtor_identifier);
4237 if (update_method_vec_p)
4238 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4241 /* Note that this is an abstract function that is never emitted. */
4242 DECL_ABSTRACT (fn) = 1;
4245 /* DECL is an in charge constructor, which is being defined. This will
4246 have had an in class declaration, from whence clones were
4247 declared. An out-of-class definition can specify additional default
4248 arguments. As it is the clones that are involved in overload
4249 resolution, we must propagate the information from the DECL to its
4250 clones. */
4252 void
4253 adjust_clone_args (tree decl)
4255 tree clone;
4257 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4258 clone = DECL_CHAIN (clone))
4260 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4261 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4262 tree decl_parms, clone_parms;
4264 clone_parms = orig_clone_parms;
4266 /* Skip the 'this' parameter. */
4267 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4268 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4270 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4271 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4272 if (DECL_HAS_VTT_PARM_P (decl))
4273 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4275 clone_parms = orig_clone_parms;
4276 if (DECL_HAS_VTT_PARM_P (clone))
4277 clone_parms = TREE_CHAIN (clone_parms);
4279 for (decl_parms = orig_decl_parms; decl_parms;
4280 decl_parms = TREE_CHAIN (decl_parms),
4281 clone_parms = TREE_CHAIN (clone_parms))
4283 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4284 TREE_TYPE (clone_parms)));
4286 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4288 /* A default parameter has been added. Adjust the
4289 clone's parameters. */
4290 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4291 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4292 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4293 tree type;
4295 clone_parms = orig_decl_parms;
4297 if (DECL_HAS_VTT_PARM_P (clone))
4299 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4300 TREE_VALUE (orig_clone_parms),
4301 clone_parms);
4302 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4304 type = build_method_type_directly (basetype,
4305 TREE_TYPE (TREE_TYPE (clone)),
4306 clone_parms);
4307 if (exceptions)
4308 type = build_exception_variant (type, exceptions);
4309 if (attrs)
4310 type = cp_build_type_attribute_variant (type, attrs);
4311 TREE_TYPE (clone) = type;
4313 clone_parms = NULL_TREE;
4314 break;
4317 gcc_assert (!clone_parms);
4321 /* For each of the constructors and destructors in T, create an
4322 in-charge and not-in-charge variant. */
4324 static void
4325 clone_constructors_and_destructors (tree t)
4327 tree fns;
4329 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4330 out now. */
4331 if (!CLASSTYPE_METHOD_VEC (t))
4332 return;
4334 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4335 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4336 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4337 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4340 /* Deduce noexcept for a destructor DTOR. */
4342 void
4343 deduce_noexcept_on_destructor (tree dtor)
4345 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4347 tree ctx = DECL_CONTEXT (dtor);
4348 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4349 /*const_p=*/false);
4350 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4351 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4355 /* For each destructor in T, deduce noexcept:
4357 12.4/3: A declaration of a destructor that does not have an
4358 exception-specification is implicitly considered to have the
4359 same exception-specification as an implicit declaration (15.4). */
4361 static void
4362 deduce_noexcept_on_destructors (tree t)
4364 tree fns;
4366 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4367 out now. */
4368 if (!CLASSTYPE_METHOD_VEC (t))
4369 return;
4371 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4372 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4375 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4376 of TYPE for virtual functions which FNDECL overrides. Return a
4377 mask of the tm attributes found therein. */
4379 static int
4380 look_for_tm_attr_overrides (tree type, tree fndecl)
4382 tree binfo = TYPE_BINFO (type);
4383 tree base_binfo;
4384 int ix, found = 0;
4386 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4388 tree o, basetype = BINFO_TYPE (base_binfo);
4390 if (!TYPE_POLYMORPHIC_P (basetype))
4391 continue;
4393 o = look_for_overrides_here (basetype, fndecl);
4394 if (o)
4395 found |= tm_attr_to_mask (find_tm_attribute
4396 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4397 else
4398 found |= look_for_tm_attr_overrides (basetype, fndecl);
4401 return found;
4404 /* Subroutine of set_method_tm_attributes. Handle the checks and
4405 inheritance for one virtual method FNDECL. */
4407 static void
4408 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4410 tree tm_attr;
4411 int found, have;
4413 found = look_for_tm_attr_overrides (type, fndecl);
4415 /* If FNDECL doesn't actually override anything (i.e. T is the
4416 class that first declares FNDECL virtual), then we're done. */
4417 if (found == 0)
4418 return;
4420 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4421 have = tm_attr_to_mask (tm_attr);
4423 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4424 tm_pure must match exactly, otherwise no weakening of
4425 tm_safe > tm_callable > nothing. */
4426 /* ??? The tm_pure attribute didn't make the transition to the
4427 multivendor language spec. */
4428 if (have == TM_ATTR_PURE)
4430 if (found != TM_ATTR_PURE)
4432 found &= -found;
4433 goto err_override;
4436 /* If the overridden function is tm_pure, then FNDECL must be. */
4437 else if (found == TM_ATTR_PURE && tm_attr)
4438 goto err_override;
4439 /* Look for base class combinations that cannot be satisfied. */
4440 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4442 found &= ~TM_ATTR_PURE;
4443 found &= -found;
4444 error_at (DECL_SOURCE_LOCATION (fndecl),
4445 "method overrides both %<transaction_pure%> and %qE methods",
4446 tm_mask_to_attr (found));
4448 /* If FNDECL did not declare an attribute, then inherit the most
4449 restrictive one. */
4450 else if (tm_attr == NULL)
4452 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4454 /* Otherwise validate that we're not weaker than a function
4455 that is being overridden. */
4456 else
4458 found &= -found;
4459 if (found <= TM_ATTR_CALLABLE && have > found)
4460 goto err_override;
4462 return;
4464 err_override:
4465 error_at (DECL_SOURCE_LOCATION (fndecl),
4466 "method declared %qE overriding %qE method",
4467 tm_attr, tm_mask_to_attr (found));
4470 /* For each of the methods in T, propagate a class-level tm attribute. */
4472 static void
4473 set_method_tm_attributes (tree t)
4475 tree class_tm_attr, fndecl;
4477 /* Don't bother collecting tm attributes if transactional memory
4478 support is not enabled. */
4479 if (!flag_tm)
4480 return;
4482 /* Process virtual methods first, as they inherit directly from the
4483 base virtual function and also require validation of new attributes. */
4484 if (TYPE_CONTAINS_VPTR_P (t))
4486 tree vchain;
4487 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4488 vchain = TREE_CHAIN (vchain))
4490 fndecl = BV_FN (vchain);
4491 if (DECL_THUNK_P (fndecl))
4492 fndecl = THUNK_TARGET (fndecl);
4493 set_one_vmethod_tm_attributes (t, fndecl);
4497 /* If the class doesn't have an attribute, nothing more to do. */
4498 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4499 if (class_tm_attr == NULL)
4500 return;
4502 /* Any method that does not yet have a tm attribute inherits
4503 the one from the class. */
4504 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4506 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4507 apply_tm_attr (fndecl, class_tm_attr);
4511 /* Returns true iff class T has a user-defined constructor other than
4512 the default constructor. */
4514 bool
4515 type_has_user_nondefault_constructor (tree t)
4517 tree fns;
4519 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4520 return false;
4522 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4524 tree fn = OVL_CURRENT (fns);
4525 if (!DECL_ARTIFICIAL (fn)
4526 && (TREE_CODE (fn) == TEMPLATE_DECL
4527 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4528 != NULL_TREE)))
4529 return true;
4532 return false;
4535 /* Returns the defaulted constructor if T has one. Otherwise, returns
4536 NULL_TREE. */
4538 tree
4539 in_class_defaulted_default_constructor (tree t)
4541 tree fns, args;
4543 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4544 return NULL_TREE;
4546 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4548 tree fn = OVL_CURRENT (fns);
4550 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4552 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4553 while (args && TREE_PURPOSE (args))
4554 args = TREE_CHAIN (args);
4555 if (!args || args == void_list_node)
4556 return fn;
4560 return NULL_TREE;
4563 /* Returns true iff FN is a user-provided function, i.e. user-declared
4564 and not defaulted at its first declaration; or explicit, private,
4565 protected, or non-const. */
4567 bool
4568 user_provided_p (tree fn)
4570 if (TREE_CODE (fn) == TEMPLATE_DECL)
4571 return true;
4572 else
4573 return (!DECL_ARTIFICIAL (fn)
4574 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4577 /* Returns true iff class T has a user-provided constructor. */
4579 bool
4580 type_has_user_provided_constructor (tree t)
4582 tree fns;
4584 if (!CLASS_TYPE_P (t))
4585 return false;
4587 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4588 return false;
4590 /* This can happen in error cases; avoid crashing. */
4591 if (!CLASSTYPE_METHOD_VEC (t))
4592 return false;
4594 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4595 if (user_provided_p (OVL_CURRENT (fns)))
4596 return true;
4598 return false;
4601 /* Returns true iff class T has a user-provided default constructor. */
4603 bool
4604 type_has_user_provided_default_constructor (tree t)
4606 tree fns;
4608 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4609 return false;
4611 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4613 tree fn = OVL_CURRENT (fns);
4614 if (TREE_CODE (fn) == FUNCTION_DECL
4615 && user_provided_p (fn)
4616 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4617 return true;
4620 return false;
4623 /* If default-initialization leaves part of TYPE uninitialized, returns
4624 a DECL for the field or TYPE itself (DR 253). */
4626 tree
4627 default_init_uninitialized_part (tree type)
4629 tree t, r, binfo;
4630 int i;
4632 type = strip_array_types (type);
4633 if (!CLASS_TYPE_P (type))
4634 return type;
4635 if (type_has_user_provided_default_constructor (type))
4636 return NULL_TREE;
4637 for (binfo = TYPE_BINFO (type), i = 0;
4638 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4640 r = default_init_uninitialized_part (BINFO_TYPE (t));
4641 if (r)
4642 return r;
4644 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4645 if (TREE_CODE (t) == FIELD_DECL
4646 && !DECL_ARTIFICIAL (t)
4647 && !DECL_INITIAL (t))
4649 r = default_init_uninitialized_part (TREE_TYPE (t));
4650 if (r)
4651 return DECL_P (r) ? r : t;
4654 return NULL_TREE;
4657 /* Returns true iff for class T, a trivial synthesized default constructor
4658 would be constexpr. */
4660 bool
4661 trivial_default_constructor_is_constexpr (tree t)
4663 /* A defaulted trivial default constructor is constexpr
4664 if there is nothing to initialize. */
4665 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4666 return is_really_empty_class (t);
4669 /* Returns true iff class T has a constexpr default constructor. */
4671 bool
4672 type_has_constexpr_default_constructor (tree t)
4674 tree fns;
4676 if (!CLASS_TYPE_P (t))
4678 /* The caller should have stripped an enclosing array. */
4679 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4680 return false;
4682 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4684 if (!TYPE_HAS_COMPLEX_DFLT (t))
4685 return trivial_default_constructor_is_constexpr (t);
4686 /* Non-trivial, we need to check subobject constructors. */
4687 lazily_declare_fn (sfk_constructor, t);
4689 fns = locate_ctor (t);
4690 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4693 /* Returns true iff class TYPE has a virtual destructor. */
4695 bool
4696 type_has_virtual_destructor (tree type)
4698 tree dtor;
4700 if (!CLASS_TYPE_P (type))
4701 return false;
4703 gcc_assert (COMPLETE_TYPE_P (type));
4704 dtor = CLASSTYPE_DESTRUCTORS (type);
4705 return (dtor && DECL_VIRTUAL_P (dtor));
4708 /* Returns true iff class T has a move constructor. */
4710 bool
4711 type_has_move_constructor (tree t)
4713 tree fns;
4715 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4717 gcc_assert (COMPLETE_TYPE_P (t));
4718 lazily_declare_fn (sfk_move_constructor, t);
4721 if (!CLASSTYPE_METHOD_VEC (t))
4722 return false;
4724 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4725 if (move_fn_p (OVL_CURRENT (fns)))
4726 return true;
4728 return false;
4731 /* Returns true iff class T has a move assignment operator. */
4733 bool
4734 type_has_move_assign (tree t)
4736 tree fns;
4738 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4740 gcc_assert (COMPLETE_TYPE_P (t));
4741 lazily_declare_fn (sfk_move_assignment, t);
4744 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4745 fns; fns = OVL_NEXT (fns))
4746 if (move_fn_p (OVL_CURRENT (fns)))
4747 return true;
4749 return false;
4752 /* Returns true iff class T has a move constructor that was explicitly
4753 declared in the class body. Note that this is different from
4754 "user-provided", which doesn't include functions that are defaulted in
4755 the class. */
4757 bool
4758 type_has_user_declared_move_constructor (tree t)
4760 tree fns;
4762 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4763 return false;
4765 if (!CLASSTYPE_METHOD_VEC (t))
4766 return false;
4768 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4770 tree fn = OVL_CURRENT (fns);
4771 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4772 return true;
4775 return false;
4778 /* Returns true iff class T has a move assignment operator that was
4779 explicitly declared in the class body. */
4781 bool
4782 type_has_user_declared_move_assign (tree t)
4784 tree fns;
4786 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4787 return false;
4789 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4790 fns; fns = OVL_NEXT (fns))
4792 tree fn = OVL_CURRENT (fns);
4793 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4794 return true;
4797 return false;
4800 /* Nonzero if we need to build up a constructor call when initializing an
4801 object of this class, either because it has a user-provided constructor
4802 or because it doesn't have a default constructor (so we need to give an
4803 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
4804 what you care about is whether or not an object can be produced by a
4805 constructor (e.g. so we don't set TREE_READONLY on const variables of
4806 such type); use this function when what you care about is whether or not
4807 to try to call a constructor to create an object. The latter case is
4808 the former plus some cases of constructors that cannot be called. */
4810 bool
4811 type_build_ctor_call (tree t)
4813 tree inner;
4814 if (TYPE_NEEDS_CONSTRUCTING (t))
4815 return true;
4816 inner = strip_array_types (t);
4817 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
4818 && !ANON_AGGR_TYPE_P (inner));
4821 /* Remove all zero-width bit-fields from T. */
4823 static void
4824 remove_zero_width_bit_fields (tree t)
4826 tree *fieldsp;
4828 fieldsp = &TYPE_FIELDS (t);
4829 while (*fieldsp)
4831 if (TREE_CODE (*fieldsp) == FIELD_DECL
4832 && DECL_C_BIT_FIELD (*fieldsp)
4833 /* We should not be confused by the fact that grokbitfield
4834 temporarily sets the width of the bit field into
4835 DECL_INITIAL (*fieldsp).
4836 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
4837 to that width. */
4838 && integer_zerop (DECL_SIZE (*fieldsp)))
4839 *fieldsp = DECL_CHAIN (*fieldsp);
4840 else
4841 fieldsp = &DECL_CHAIN (*fieldsp);
4845 /* Returns TRUE iff we need a cookie when dynamically allocating an
4846 array whose elements have the indicated class TYPE. */
4848 static bool
4849 type_requires_array_cookie (tree type)
4851 tree fns;
4852 bool has_two_argument_delete_p = false;
4854 gcc_assert (CLASS_TYPE_P (type));
4856 /* If there's a non-trivial destructor, we need a cookie. In order
4857 to iterate through the array calling the destructor for each
4858 element, we'll have to know how many elements there are. */
4859 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4860 return true;
4862 /* If the usual deallocation function is a two-argument whose second
4863 argument is of type `size_t', then we have to pass the size of
4864 the array to the deallocation function, so we will need to store
4865 a cookie. */
4866 fns = lookup_fnfields (TYPE_BINFO (type),
4867 ansi_opname (VEC_DELETE_EXPR),
4868 /*protect=*/0);
4869 /* If there are no `operator []' members, or the lookup is
4870 ambiguous, then we don't need a cookie. */
4871 if (!fns || fns == error_mark_node)
4872 return false;
4873 /* Loop through all of the functions. */
4874 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4876 tree fn;
4877 tree second_parm;
4879 /* Select the current function. */
4880 fn = OVL_CURRENT (fns);
4881 /* See if this function is a one-argument delete function. If
4882 it is, then it will be the usual deallocation function. */
4883 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4884 if (second_parm == void_list_node)
4885 return false;
4886 /* Do not consider this function if its second argument is an
4887 ellipsis. */
4888 if (!second_parm)
4889 continue;
4890 /* Otherwise, if we have a two-argument function and the second
4891 argument is `size_t', it will be the usual deallocation
4892 function -- unless there is one-argument function, too. */
4893 if (TREE_CHAIN (second_parm) == void_list_node
4894 && same_type_p (TREE_VALUE (second_parm), size_type_node))
4895 has_two_argument_delete_p = true;
4898 return has_two_argument_delete_p;
4901 /* Finish computing the `literal type' property of class type T.
4903 At this point, we have already processed base classes and
4904 non-static data members. We need to check whether the copy
4905 constructor is trivial, the destructor is trivial, and there
4906 is a trivial default constructor or at least one constexpr
4907 constructor other than the copy constructor. */
4909 static void
4910 finalize_literal_type_property (tree t)
4912 tree fn;
4914 if (cxx_dialect < cxx0x
4915 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4916 CLASSTYPE_LITERAL_P (t) = false;
4917 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
4918 && CLASSTYPE_NON_AGGREGATE (t)
4919 && !TYPE_HAS_CONSTEXPR_CTOR (t))
4920 CLASSTYPE_LITERAL_P (t) = false;
4922 if (!CLASSTYPE_LITERAL_P (t))
4923 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
4924 if (DECL_DECLARED_CONSTEXPR_P (fn)
4925 && TREE_CODE (fn) != TEMPLATE_DECL
4926 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
4927 && !DECL_CONSTRUCTOR_P (fn))
4929 DECL_DECLARED_CONSTEXPR_P (fn) = false;
4930 if (!DECL_GENERATED_P (fn))
4932 error ("enclosing class of constexpr non-static member "
4933 "function %q+#D is not a literal type", fn);
4934 explain_non_literal_class (t);
4939 /* T is a non-literal type used in a context which requires a constant
4940 expression. Explain why it isn't literal. */
4942 void
4943 explain_non_literal_class (tree t)
4945 static struct pointer_set_t *diagnosed;
4947 if (!CLASS_TYPE_P (t))
4948 return;
4949 t = TYPE_MAIN_VARIANT (t);
4951 if (diagnosed == NULL)
4952 diagnosed = pointer_set_create ();
4953 if (pointer_set_insert (diagnosed, t) != 0)
4954 /* Already explained. */
4955 return;
4957 inform (0, "%q+T is not literal because:", t);
4958 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4959 inform (0, " %q+T has a non-trivial destructor", t);
4960 else if (CLASSTYPE_NON_AGGREGATE (t)
4961 && !TYPE_HAS_TRIVIAL_DFLT (t)
4962 && !TYPE_HAS_CONSTEXPR_CTOR (t))
4964 inform (0, " %q+T is not an aggregate, does not have a trivial "
4965 "default constructor, and has no constexpr constructor that "
4966 "is not a copy or move constructor", t);
4967 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
4968 && !type_has_user_provided_default_constructor (t))
4970 /* Note that we can't simply call locate_ctor because when the
4971 constructor is deleted it just returns NULL_TREE. */
4972 tree fns;
4973 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4975 tree fn = OVL_CURRENT (fns);
4976 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
4978 parms = skip_artificial_parms_for (fn, parms);
4980 if (sufficient_parms_p (parms))
4982 if (DECL_DELETED_FN (fn))
4983 maybe_explain_implicit_delete (fn);
4984 else
4985 explain_invalid_constexpr_fn (fn);
4986 break;
4991 else
4993 tree binfo, base_binfo, field; int i;
4994 for (binfo = TYPE_BINFO (t), i = 0;
4995 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4997 tree basetype = TREE_TYPE (base_binfo);
4998 if (!CLASSTYPE_LITERAL_P (basetype))
5000 inform (0, " base class %qT of %q+T is non-literal",
5001 basetype, t);
5002 explain_non_literal_class (basetype);
5003 return;
5006 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5008 tree ftype;
5009 if (TREE_CODE (field) != FIELD_DECL)
5010 continue;
5011 ftype = TREE_TYPE (field);
5012 if (!literal_type_p (ftype))
5014 inform (0, " non-static data member %q+D has "
5015 "non-literal type", field);
5016 if (CLASS_TYPE_P (ftype))
5017 explain_non_literal_class (ftype);
5023 /* Check the validity of the bases and members declared in T. Add any
5024 implicitly-generated functions (like copy-constructors and
5025 assignment operators). Compute various flag bits (like
5026 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5027 level: i.e., independently of the ABI in use. */
5029 static void
5030 check_bases_and_members (tree t)
5032 /* Nonzero if the implicitly generated copy constructor should take
5033 a non-const reference argument. */
5034 int cant_have_const_ctor;
5035 /* Nonzero if the implicitly generated assignment operator
5036 should take a non-const reference argument. */
5037 int no_const_asn_ref;
5038 tree access_decls;
5039 bool saved_complex_asn_ref;
5040 bool saved_nontrivial_dtor;
5041 tree fn;
5043 /* By default, we use const reference arguments and generate default
5044 constructors. */
5045 cant_have_const_ctor = 0;
5046 no_const_asn_ref = 0;
5048 /* Deduce noexcept on destructors. */
5049 if (cxx_dialect >= cxx0x)
5050 deduce_noexcept_on_destructors (t);
5052 /* Check all the base-classes. */
5053 check_bases (t, &cant_have_const_ctor,
5054 &no_const_asn_ref);
5056 /* Check all the method declarations. */
5057 check_methods (t);
5059 /* Save the initial values of these flags which only indicate whether
5060 or not the class has user-provided functions. As we analyze the
5061 bases and members we can set these flags for other reasons. */
5062 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5063 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5065 /* Check all the data member declarations. We cannot call
5066 check_field_decls until we have called check_bases check_methods,
5067 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5068 being set appropriately. */
5069 check_field_decls (t, &access_decls,
5070 &cant_have_const_ctor,
5071 &no_const_asn_ref);
5073 /* A nearly-empty class has to be vptr-containing; a nearly empty
5074 class contains just a vptr. */
5075 if (!TYPE_CONTAINS_VPTR_P (t))
5076 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5078 /* Do some bookkeeping that will guide the generation of implicitly
5079 declared member functions. */
5080 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5081 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5082 /* We need to call a constructor for this class if it has a
5083 user-provided constructor, or if the default constructor is going
5084 to initialize the vptr. (This is not an if-and-only-if;
5085 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5086 themselves need constructing.) */
5087 TYPE_NEEDS_CONSTRUCTING (t)
5088 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5089 /* [dcl.init.aggr]
5091 An aggregate is an array or a class with no user-provided
5092 constructors ... and no virtual functions.
5094 Again, other conditions for being an aggregate are checked
5095 elsewhere. */
5096 CLASSTYPE_NON_AGGREGATE (t)
5097 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5098 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5099 retain the old definition internally for ABI reasons. */
5100 CLASSTYPE_NON_LAYOUT_POD_P (t)
5101 |= (CLASSTYPE_NON_AGGREGATE (t)
5102 || saved_nontrivial_dtor || saved_complex_asn_ref);
5103 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5104 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5105 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5106 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5108 /* If the class has no user-declared constructor, but does have
5109 non-static const or reference data members that can never be
5110 initialized, issue a warning. */
5111 if (warn_uninitialized
5112 /* Classes with user-declared constructors are presumed to
5113 initialize these members. */
5114 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5115 /* Aggregates can be initialized with brace-enclosed
5116 initializers. */
5117 && CLASSTYPE_NON_AGGREGATE (t))
5119 tree field;
5121 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5123 tree type;
5125 if (TREE_CODE (field) != FIELD_DECL)
5126 continue;
5128 type = TREE_TYPE (field);
5129 if (TREE_CODE (type) == REFERENCE_TYPE)
5130 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5131 "in class without a constructor", field);
5132 else if (CP_TYPE_CONST_P (type)
5133 && (!CLASS_TYPE_P (type)
5134 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5135 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5136 "in class without a constructor", field);
5140 /* Synthesize any needed methods. */
5141 add_implicitly_declared_members (t,
5142 cant_have_const_ctor,
5143 no_const_asn_ref);
5145 /* Check defaulted declarations here so we have cant_have_const_ctor
5146 and don't need to worry about clones. */
5147 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5148 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5150 int copy = copy_fn_p (fn);
5151 if (copy > 0)
5153 bool imp_const_p
5154 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5155 : !no_const_asn_ref);
5156 bool fn_const_p = (copy == 2);
5158 if (fn_const_p && !imp_const_p)
5159 /* If the function is defaulted outside the class, we just
5160 give the synthesis error. */
5161 error ("%q+D declared to take const reference, but implicit "
5162 "declaration would take non-const", fn);
5164 defaulted_late_check (fn);
5167 if (LAMBDA_TYPE_P (t))
5169 /* "The closure type associated with a lambda-expression has a deleted
5170 default constructor and a deleted copy assignment operator." */
5171 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5172 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5173 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5174 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5176 /* "This class type is not an aggregate." */
5177 CLASSTYPE_NON_AGGREGATE (t) = 1;
5180 /* Compute the 'literal type' property before we
5181 do anything with non-static member functions. */
5182 finalize_literal_type_property (t);
5184 /* Create the in-charge and not-in-charge variants of constructors
5185 and destructors. */
5186 clone_constructors_and_destructors (t);
5188 /* Process the using-declarations. */
5189 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5190 handle_using_decl (TREE_VALUE (access_decls), t);
5192 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5193 finish_struct_methods (t);
5195 /* Figure out whether or not we will need a cookie when dynamically
5196 allocating an array of this type. */
5197 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5198 = type_requires_array_cookie (t);
5201 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5202 accordingly. If a new vfield was created (because T doesn't have a
5203 primary base class), then the newly created field is returned. It
5204 is not added to the TYPE_FIELDS list; it is the caller's
5205 responsibility to do that. Accumulate declared virtual functions
5206 on VIRTUALS_P. */
5208 static tree
5209 create_vtable_ptr (tree t, tree* virtuals_p)
5211 tree fn;
5213 /* Collect the virtual functions declared in T. */
5214 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5215 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5216 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5218 tree new_virtual = make_node (TREE_LIST);
5220 BV_FN (new_virtual) = fn;
5221 BV_DELTA (new_virtual) = integer_zero_node;
5222 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5224 TREE_CHAIN (new_virtual) = *virtuals_p;
5225 *virtuals_p = new_virtual;
5228 /* If we couldn't find an appropriate base class, create a new field
5229 here. Even if there weren't any new virtual functions, we might need a
5230 new virtual function table if we're supposed to include vptrs in
5231 all classes that need them. */
5232 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5234 /* We build this decl with vtbl_ptr_type_node, which is a
5235 `vtable_entry_type*'. It might seem more precise to use
5236 `vtable_entry_type (*)[N]' where N is the number of virtual
5237 functions. However, that would require the vtable pointer in
5238 base classes to have a different type than the vtable pointer
5239 in derived classes. We could make that happen, but that
5240 still wouldn't solve all the problems. In particular, the
5241 type-based alias analysis code would decide that assignments
5242 to the base class vtable pointer can't alias assignments to
5243 the derived class vtable pointer, since they have different
5244 types. Thus, in a derived class destructor, where the base
5245 class constructor was inlined, we could generate bad code for
5246 setting up the vtable pointer.
5248 Therefore, we use one type for all vtable pointers. We still
5249 use a type-correct type; it's just doesn't indicate the array
5250 bounds. That's better than using `void*' or some such; it's
5251 cleaner, and it let's the alias analysis code know that these
5252 stores cannot alias stores to void*! */
5253 tree field;
5255 field = build_decl (input_location,
5256 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5257 DECL_VIRTUAL_P (field) = 1;
5258 DECL_ARTIFICIAL (field) = 1;
5259 DECL_FIELD_CONTEXT (field) = t;
5260 DECL_FCONTEXT (field) = t;
5261 if (TYPE_PACKED (t))
5262 DECL_PACKED (field) = 1;
5264 TYPE_VFIELD (t) = field;
5266 /* This class is non-empty. */
5267 CLASSTYPE_EMPTY_P (t) = 0;
5269 return field;
5272 return NULL_TREE;
5275 /* Add OFFSET to all base types of BINFO which is a base in the
5276 hierarchy dominated by T.
5278 OFFSET, which is a type offset, is number of bytes. */
5280 static void
5281 propagate_binfo_offsets (tree binfo, tree offset)
5283 int i;
5284 tree primary_binfo;
5285 tree base_binfo;
5287 /* Update BINFO's offset. */
5288 BINFO_OFFSET (binfo)
5289 = convert (sizetype,
5290 size_binop (PLUS_EXPR,
5291 convert (ssizetype, BINFO_OFFSET (binfo)),
5292 offset));
5294 /* Find the primary base class. */
5295 primary_binfo = get_primary_binfo (binfo);
5297 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5298 propagate_binfo_offsets (primary_binfo, offset);
5300 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5301 downwards. */
5302 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5304 /* Don't do the primary base twice. */
5305 if (base_binfo == primary_binfo)
5306 continue;
5308 if (BINFO_VIRTUAL_P (base_binfo))
5309 continue;
5311 propagate_binfo_offsets (base_binfo, offset);
5315 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5316 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5317 empty subobjects of T. */
5319 static void
5320 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5322 tree vbase;
5323 tree t = rli->t;
5324 bool first_vbase = true;
5325 tree *next_field;
5327 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5328 return;
5330 if (!abi_version_at_least(2))
5332 /* In G++ 3.2, we incorrectly rounded the size before laying out
5333 the virtual bases. */
5334 finish_record_layout (rli, /*free_p=*/false);
5335 #ifdef STRUCTURE_SIZE_BOUNDARY
5336 /* Packed structures don't need to have minimum size. */
5337 if (! TYPE_PACKED (t))
5338 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5339 #endif
5340 rli->offset = TYPE_SIZE_UNIT (t);
5341 rli->bitpos = bitsize_zero_node;
5342 rli->record_align = TYPE_ALIGN (t);
5345 /* Find the last field. The artificial fields created for virtual
5346 bases will go after the last extant field to date. */
5347 next_field = &TYPE_FIELDS (t);
5348 while (*next_field)
5349 next_field = &DECL_CHAIN (*next_field);
5351 /* Go through the virtual bases, allocating space for each virtual
5352 base that is not already a primary base class. These are
5353 allocated in inheritance graph order. */
5354 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5356 if (!BINFO_VIRTUAL_P (vbase))
5357 continue;
5359 if (!BINFO_PRIMARY_P (vbase))
5361 tree basetype = TREE_TYPE (vbase);
5363 /* This virtual base is not a primary base of any class in the
5364 hierarchy, so we have to add space for it. */
5365 next_field = build_base_field (rli, vbase,
5366 offsets, next_field);
5368 /* If the first virtual base might have been placed at a
5369 lower address, had we started from CLASSTYPE_SIZE, rather
5370 than TYPE_SIZE, issue a warning. There can be both false
5371 positives and false negatives from this warning in rare
5372 cases; to deal with all the possibilities would probably
5373 require performing both layout algorithms and comparing
5374 the results which is not particularly tractable. */
5375 if (warn_abi
5376 && first_vbase
5377 && (tree_int_cst_lt
5378 (size_binop (CEIL_DIV_EXPR,
5379 round_up_loc (input_location,
5380 CLASSTYPE_SIZE (t),
5381 CLASSTYPE_ALIGN (basetype)),
5382 bitsize_unit_node),
5383 BINFO_OFFSET (vbase))))
5384 warning (OPT_Wabi,
5385 "offset of virtual base %qT is not ABI-compliant and "
5386 "may change in a future version of GCC",
5387 basetype);
5389 first_vbase = false;
5394 /* Returns the offset of the byte just past the end of the base class
5395 BINFO. */
5397 static tree
5398 end_of_base (tree binfo)
5400 tree size;
5402 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5403 size = TYPE_SIZE_UNIT (char_type_node);
5404 else if (is_empty_class (BINFO_TYPE (binfo)))
5405 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5406 allocate some space for it. It cannot have virtual bases, so
5407 TYPE_SIZE_UNIT is fine. */
5408 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5409 else
5410 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5412 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5415 /* Returns the offset of the byte just past the end of the base class
5416 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5417 only non-virtual bases are included. */
5419 static tree
5420 end_of_class (tree t, int include_virtuals_p)
5422 tree result = size_zero_node;
5423 VEC(tree,gc) *vbases;
5424 tree binfo;
5425 tree base_binfo;
5426 tree offset;
5427 int i;
5429 for (binfo = TYPE_BINFO (t), i = 0;
5430 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5432 if (!include_virtuals_p
5433 && BINFO_VIRTUAL_P (base_binfo)
5434 && (!BINFO_PRIMARY_P (base_binfo)
5435 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5436 continue;
5438 offset = end_of_base (base_binfo);
5439 if (INT_CST_LT_UNSIGNED (result, offset))
5440 result = offset;
5443 /* G++ 3.2 did not check indirect virtual bases. */
5444 if (abi_version_at_least (2) && include_virtuals_p)
5445 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5446 VEC_iterate (tree, vbases, i, base_binfo); i++)
5448 offset = end_of_base (base_binfo);
5449 if (INT_CST_LT_UNSIGNED (result, offset))
5450 result = offset;
5453 return result;
5456 /* Warn about bases of T that are inaccessible because they are
5457 ambiguous. For example:
5459 struct S {};
5460 struct T : public S {};
5461 struct U : public S, public T {};
5463 Here, `(S*) new U' is not allowed because there are two `S'
5464 subobjects of U. */
5466 static void
5467 warn_about_ambiguous_bases (tree t)
5469 int i;
5470 VEC(tree,gc) *vbases;
5471 tree basetype;
5472 tree binfo;
5473 tree base_binfo;
5475 /* If there are no repeated bases, nothing can be ambiguous. */
5476 if (!CLASSTYPE_REPEATED_BASE_P (t))
5477 return;
5479 /* Check direct bases. */
5480 for (binfo = TYPE_BINFO (t), i = 0;
5481 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5483 basetype = BINFO_TYPE (base_binfo);
5485 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
5486 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5487 basetype, t);
5490 /* Check for ambiguous virtual bases. */
5491 if (extra_warnings)
5492 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5493 VEC_iterate (tree, vbases, i, binfo); i++)
5495 basetype = BINFO_TYPE (binfo);
5497 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
5498 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity",
5499 basetype, t);
5503 /* Compare two INTEGER_CSTs K1 and K2. */
5505 static int
5506 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5508 return tree_int_cst_compare ((tree) k1, (tree) k2);
5511 /* Increase the size indicated in RLI to account for empty classes
5512 that are "off the end" of the class. */
5514 static void
5515 include_empty_classes (record_layout_info rli)
5517 tree eoc;
5518 tree rli_size;
5520 /* It might be the case that we grew the class to allocate a
5521 zero-sized base class. That won't be reflected in RLI, yet,
5522 because we are willing to overlay multiple bases at the same
5523 offset. However, now we need to make sure that RLI is big enough
5524 to reflect the entire class. */
5525 eoc = end_of_class (rli->t,
5526 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5527 rli_size = rli_size_unit_so_far (rli);
5528 if (TREE_CODE (rli_size) == INTEGER_CST
5529 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5531 if (!abi_version_at_least (2))
5532 /* In version 1 of the ABI, the size of a class that ends with
5533 a bitfield was not rounded up to a whole multiple of a
5534 byte. Because rli_size_unit_so_far returns only the number
5535 of fully allocated bytes, any extra bits were not included
5536 in the size. */
5537 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5538 else
5539 /* The size should have been rounded to a whole byte. */
5540 gcc_assert (tree_int_cst_equal
5541 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5542 rli->bitpos
5543 = size_binop (PLUS_EXPR,
5544 rli->bitpos,
5545 size_binop (MULT_EXPR,
5546 convert (bitsizetype,
5547 size_binop (MINUS_EXPR,
5548 eoc, rli_size)),
5549 bitsize_int (BITS_PER_UNIT)));
5550 normalize_rli (rli);
5554 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5555 BINFO_OFFSETs for all of the base-classes. Position the vtable
5556 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5558 static void
5559 layout_class_type (tree t, tree *virtuals_p)
5561 tree non_static_data_members;
5562 tree field;
5563 tree vptr;
5564 record_layout_info rli;
5565 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5566 types that appear at that offset. */
5567 splay_tree empty_base_offsets;
5568 /* True if the last field layed out was a bit-field. */
5569 bool last_field_was_bitfield = false;
5570 /* The location at which the next field should be inserted. */
5571 tree *next_field;
5572 /* T, as a base class. */
5573 tree base_t;
5575 /* Keep track of the first non-static data member. */
5576 non_static_data_members = TYPE_FIELDS (t);
5578 /* Start laying out the record. */
5579 rli = start_record_layout (t);
5581 /* Mark all the primary bases in the hierarchy. */
5582 determine_primary_bases (t);
5584 /* Create a pointer to our virtual function table. */
5585 vptr = create_vtable_ptr (t, virtuals_p);
5587 /* The vptr is always the first thing in the class. */
5588 if (vptr)
5590 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5591 TYPE_FIELDS (t) = vptr;
5592 next_field = &DECL_CHAIN (vptr);
5593 place_field (rli, vptr);
5595 else
5596 next_field = &TYPE_FIELDS (t);
5598 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5599 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5600 NULL, NULL);
5601 build_base_fields (rli, empty_base_offsets, next_field);
5603 /* Layout the non-static data members. */
5604 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5606 tree type;
5607 tree padding;
5609 /* We still pass things that aren't non-static data members to
5610 the back end, in case it wants to do something with them. */
5611 if (TREE_CODE (field) != FIELD_DECL)
5613 place_field (rli, field);
5614 /* If the static data member has incomplete type, keep track
5615 of it so that it can be completed later. (The handling
5616 of pending statics in finish_record_layout is
5617 insufficient; consider:
5619 struct S1;
5620 struct S2 { static S1 s1; };
5622 At this point, finish_record_layout will be called, but
5623 S1 is still incomplete.) */
5624 if (TREE_CODE (field) == VAR_DECL)
5626 maybe_register_incomplete_var (field);
5627 /* The visibility of static data members is determined
5628 at their point of declaration, not their point of
5629 definition. */
5630 determine_visibility (field);
5632 continue;
5635 type = TREE_TYPE (field);
5636 if (type == error_mark_node)
5637 continue;
5639 padding = NULL_TREE;
5641 /* If this field is a bit-field whose width is greater than its
5642 type, then there are some special rules for allocating
5643 it. */
5644 if (DECL_C_BIT_FIELD (field)
5645 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5647 unsigned int itk;
5648 tree integer_type;
5649 bool was_unnamed_p = false;
5650 /* We must allocate the bits as if suitably aligned for the
5651 longest integer type that fits in this many bits. type
5652 of the field. Then, we are supposed to use the left over
5653 bits as additional padding. */
5654 for (itk = itk_char; itk != itk_none; ++itk)
5655 if (integer_types[itk] != NULL_TREE
5656 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5657 TYPE_SIZE (integer_types[itk]))
5658 || INT_CST_LT (DECL_SIZE (field),
5659 TYPE_SIZE (integer_types[itk]))))
5660 break;
5662 /* ITK now indicates a type that is too large for the
5663 field. We have to back up by one to find the largest
5664 type that fits. */
5667 --itk;
5668 integer_type = integer_types[itk];
5669 } while (itk > 0 && integer_type == NULL_TREE);
5671 /* Figure out how much additional padding is required. GCC
5672 3.2 always created a padding field, even if it had zero
5673 width. */
5674 if (!abi_version_at_least (2)
5675 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5677 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5678 /* In a union, the padding field must have the full width
5679 of the bit-field; all fields start at offset zero. */
5680 padding = DECL_SIZE (field);
5681 else
5683 if (TREE_CODE (t) == UNION_TYPE)
5684 warning (OPT_Wabi, "size assigned to %qT may not be "
5685 "ABI-compliant and may change in a future "
5686 "version of GCC",
5688 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5689 TYPE_SIZE (integer_type));
5692 #ifdef PCC_BITFIELD_TYPE_MATTERS
5693 /* An unnamed bitfield does not normally affect the
5694 alignment of the containing class on a target where
5695 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5696 make any exceptions for unnamed bitfields when the
5697 bitfields are longer than their types. Therefore, we
5698 temporarily give the field a name. */
5699 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5701 was_unnamed_p = true;
5702 DECL_NAME (field) = make_anon_name ();
5704 #endif
5705 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5706 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5707 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5708 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5709 empty_base_offsets);
5710 if (was_unnamed_p)
5711 DECL_NAME (field) = NULL_TREE;
5712 /* Now that layout has been performed, set the size of the
5713 field to the size of its declared type; the rest of the
5714 field is effectively invisible. */
5715 DECL_SIZE (field) = TYPE_SIZE (type);
5716 /* We must also reset the DECL_MODE of the field. */
5717 if (abi_version_at_least (2))
5718 DECL_MODE (field) = TYPE_MODE (type);
5719 else if (warn_abi
5720 && DECL_MODE (field) != TYPE_MODE (type))
5721 /* Versions of G++ before G++ 3.4 did not reset the
5722 DECL_MODE. */
5723 warning (OPT_Wabi,
5724 "the offset of %qD may not be ABI-compliant and may "
5725 "change in a future version of GCC", field);
5727 else
5728 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5729 empty_base_offsets);
5731 /* Remember the location of any empty classes in FIELD. */
5732 if (abi_version_at_least (2))
5733 record_subobject_offsets (TREE_TYPE (field),
5734 byte_position(field),
5735 empty_base_offsets,
5736 /*is_data_member=*/true);
5738 /* If a bit-field does not immediately follow another bit-field,
5739 and yet it starts in the middle of a byte, we have failed to
5740 comply with the ABI. */
5741 if (warn_abi
5742 && DECL_C_BIT_FIELD (field)
5743 /* The TREE_NO_WARNING flag gets set by Objective-C when
5744 laying out an Objective-C class. The ObjC ABI differs
5745 from the C++ ABI, and so we do not want a warning
5746 here. */
5747 && !TREE_NO_WARNING (field)
5748 && !last_field_was_bitfield
5749 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5750 DECL_FIELD_BIT_OFFSET (field),
5751 bitsize_unit_node)))
5752 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5753 "change in a future version of GCC", field);
5755 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5756 offset of the field. */
5757 if (warn_abi
5758 && !abi_version_at_least (2)
5759 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5760 byte_position (field))
5761 && contains_empty_class_p (TREE_TYPE (field)))
5762 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5763 "classes to be placed at different locations in a "
5764 "future version of GCC", field);
5766 /* The middle end uses the type of expressions to determine the
5767 possible range of expression values. In order to optimize
5768 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5769 must be made aware of the width of "i", via its type.
5771 Because C++ does not have integer types of arbitrary width,
5772 we must (for the purposes of the front end) convert from the
5773 type assigned here to the declared type of the bitfield
5774 whenever a bitfield expression is used as an rvalue.
5775 Similarly, when assigning a value to a bitfield, the value
5776 must be converted to the type given the bitfield here. */
5777 if (DECL_C_BIT_FIELD (field))
5779 unsigned HOST_WIDE_INT width;
5780 tree ftype = TREE_TYPE (field);
5781 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5782 if (width != TYPE_PRECISION (ftype))
5784 TREE_TYPE (field)
5785 = c_build_bitfield_integer_type (width,
5786 TYPE_UNSIGNED (ftype));
5787 TREE_TYPE (field)
5788 = cp_build_qualified_type (TREE_TYPE (field),
5789 cp_type_quals (ftype));
5793 /* If we needed additional padding after this field, add it
5794 now. */
5795 if (padding)
5797 tree padding_field;
5799 padding_field = build_decl (input_location,
5800 FIELD_DECL,
5801 NULL_TREE,
5802 char_type_node);
5803 DECL_BIT_FIELD (padding_field) = 1;
5804 DECL_SIZE (padding_field) = padding;
5805 DECL_CONTEXT (padding_field) = t;
5806 DECL_ARTIFICIAL (padding_field) = 1;
5807 DECL_IGNORED_P (padding_field) = 1;
5808 layout_nonempty_base_or_field (rli, padding_field,
5809 NULL_TREE,
5810 empty_base_offsets);
5813 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5816 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5818 /* Make sure that we are on a byte boundary so that the size of
5819 the class without virtual bases will always be a round number
5820 of bytes. */
5821 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
5822 normalize_rli (rli);
5825 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5826 padding. */
5827 if (!abi_version_at_least (2))
5828 include_empty_classes(rli);
5830 /* Delete all zero-width bit-fields from the list of fields. Now
5831 that the type is laid out they are no longer important. */
5832 remove_zero_width_bit_fields (t);
5834 /* Create the version of T used for virtual bases. We do not use
5835 make_class_type for this version; this is an artificial type. For
5836 a POD type, we just reuse T. */
5837 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5839 base_t = make_node (TREE_CODE (t));
5841 /* Set the size and alignment for the new type. In G++ 3.2, all
5842 empty classes were considered to have size zero when used as
5843 base classes. */
5844 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5846 TYPE_SIZE (base_t) = bitsize_zero_node;
5847 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5848 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5849 warning (OPT_Wabi,
5850 "layout of classes derived from empty class %qT "
5851 "may change in a future version of GCC",
5854 else
5856 tree eoc;
5858 /* If the ABI version is not at least two, and the last
5859 field was a bit-field, RLI may not be on a byte
5860 boundary. In particular, rli_size_unit_so_far might
5861 indicate the last complete byte, while rli_size_so_far
5862 indicates the total number of bits used. Therefore,
5863 rli_size_so_far, rather than rli_size_unit_so_far, is
5864 used to compute TYPE_SIZE_UNIT. */
5865 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5866 TYPE_SIZE_UNIT (base_t)
5867 = size_binop (MAX_EXPR,
5868 convert (sizetype,
5869 size_binop (CEIL_DIV_EXPR,
5870 rli_size_so_far (rli),
5871 bitsize_int (BITS_PER_UNIT))),
5872 eoc);
5873 TYPE_SIZE (base_t)
5874 = size_binop (MAX_EXPR,
5875 rli_size_so_far (rli),
5876 size_binop (MULT_EXPR,
5877 convert (bitsizetype, eoc),
5878 bitsize_int (BITS_PER_UNIT)));
5880 TYPE_ALIGN (base_t) = rli->record_align;
5881 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5883 /* Copy the fields from T. */
5884 next_field = &TYPE_FIELDS (base_t);
5885 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5886 if (TREE_CODE (field) == FIELD_DECL)
5888 *next_field = build_decl (input_location,
5889 FIELD_DECL,
5890 DECL_NAME (field),
5891 TREE_TYPE (field));
5892 DECL_CONTEXT (*next_field) = base_t;
5893 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5894 DECL_FIELD_BIT_OFFSET (*next_field)
5895 = DECL_FIELD_BIT_OFFSET (field);
5896 DECL_SIZE (*next_field) = DECL_SIZE (field);
5897 DECL_MODE (*next_field) = DECL_MODE (field);
5898 next_field = &DECL_CHAIN (*next_field);
5901 /* Record the base version of the type. */
5902 CLASSTYPE_AS_BASE (t) = base_t;
5903 TYPE_CONTEXT (base_t) = t;
5905 else
5906 CLASSTYPE_AS_BASE (t) = t;
5908 /* Every empty class contains an empty class. */
5909 if (CLASSTYPE_EMPTY_P (t))
5910 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5912 /* Set the TYPE_DECL for this type to contain the right
5913 value for DECL_OFFSET, so that we can use it as part
5914 of a COMPONENT_REF for multiple inheritance. */
5915 layout_decl (TYPE_MAIN_DECL (t), 0);
5917 /* Now fix up any virtual base class types that we left lying
5918 around. We must get these done before we try to lay out the
5919 virtual function table. As a side-effect, this will remove the
5920 base subobject fields. */
5921 layout_virtual_bases (rli, empty_base_offsets);
5923 /* Make sure that empty classes are reflected in RLI at this
5924 point. */
5925 include_empty_classes(rli);
5927 /* Make sure not to create any structures with zero size. */
5928 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5929 place_field (rli,
5930 build_decl (input_location,
5931 FIELD_DECL, NULL_TREE, char_type_node));
5933 /* If this is a non-POD, declaring it packed makes a difference to how it
5934 can be used as a field; don't let finalize_record_size undo it. */
5935 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
5936 rli->packed_maybe_necessary = true;
5938 /* Let the back end lay out the type. */
5939 finish_record_layout (rli, /*free_p=*/true);
5941 /* Warn about bases that can't be talked about due to ambiguity. */
5942 warn_about_ambiguous_bases (t);
5944 /* Now that we're done with layout, give the base fields the real types. */
5945 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5946 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
5947 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
5949 /* Clean up. */
5950 splay_tree_delete (empty_base_offsets);
5952 if (CLASSTYPE_EMPTY_P (t)
5953 && tree_int_cst_lt (sizeof_biggest_empty_class,
5954 TYPE_SIZE_UNIT (t)))
5955 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
5958 /* Determine the "key method" for the class type indicated by TYPE,
5959 and set CLASSTYPE_KEY_METHOD accordingly. */
5961 void
5962 determine_key_method (tree type)
5964 tree method;
5966 if (TYPE_FOR_JAVA (type)
5967 || processing_template_decl
5968 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5969 || CLASSTYPE_INTERFACE_KNOWN (type))
5970 return;
5972 /* The key method is the first non-pure virtual function that is not
5973 inline at the point of class definition. On some targets the
5974 key function may not be inline; those targets should not call
5975 this function until the end of the translation unit. */
5976 for (method = TYPE_METHODS (type); method != NULL_TREE;
5977 method = DECL_CHAIN (method))
5978 if (DECL_VINDEX (method) != NULL_TREE
5979 && ! DECL_DECLARED_INLINE_P (method)
5980 && ! DECL_PURE_VIRTUAL_P (method))
5982 CLASSTYPE_KEY_METHOD (type) = method;
5983 break;
5986 return;
5990 /* Allocate and return an instance of struct sorted_fields_type with
5991 N fields. */
5993 static struct sorted_fields_type *
5994 sorted_fields_type_new (int n)
5996 struct sorted_fields_type *sft;
5997 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
5998 + n * sizeof (tree));
5999 sft->len = n;
6001 return sft;
6005 /* Perform processing required when the definition of T (a class type)
6006 is complete. */
6008 void
6009 finish_struct_1 (tree t)
6011 tree x;
6012 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6013 tree virtuals = NULL_TREE;
6015 if (COMPLETE_TYPE_P (t))
6017 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6018 error ("redefinition of %q#T", t);
6019 popclass ();
6020 return;
6023 /* If this type was previously laid out as a forward reference,
6024 make sure we lay it out again. */
6025 TYPE_SIZE (t) = NULL_TREE;
6026 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6028 /* Make assumptions about the class; we'll reset the flags if
6029 necessary. */
6030 CLASSTYPE_EMPTY_P (t) = 1;
6031 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6032 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6033 CLASSTYPE_LITERAL_P (t) = true;
6035 /* Do end-of-class semantic processing: checking the validity of the
6036 bases and members and add implicitly generated methods. */
6037 check_bases_and_members (t);
6039 /* Find the key method. */
6040 if (TYPE_CONTAINS_VPTR_P (t))
6042 /* The Itanium C++ ABI permits the key method to be chosen when
6043 the class is defined -- even though the key method so
6044 selected may later turn out to be an inline function. On
6045 some systems (such as ARM Symbian OS) the key method cannot
6046 be determined until the end of the translation unit. On such
6047 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6048 will cause the class to be added to KEYED_CLASSES. Then, in
6049 finish_file we will determine the key method. */
6050 if (targetm.cxx.key_method_may_be_inline ())
6051 determine_key_method (t);
6053 /* If a polymorphic class has no key method, we may emit the vtable
6054 in every translation unit where the class definition appears. */
6055 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6056 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6059 /* Layout the class itself. */
6060 layout_class_type (t, &virtuals);
6061 if (CLASSTYPE_AS_BASE (t) != t)
6062 /* We use the base type for trivial assignments, and hence it
6063 needs a mode. */
6064 compute_record_mode (CLASSTYPE_AS_BASE (t));
6066 virtuals = modify_all_vtables (t, nreverse (virtuals));
6068 /* If necessary, create the primary vtable for this class. */
6069 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6071 /* We must enter these virtuals into the table. */
6072 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6073 build_primary_vtable (NULL_TREE, t);
6074 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6075 /* Here we know enough to change the type of our virtual
6076 function table, but we will wait until later this function. */
6077 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6080 if (TYPE_CONTAINS_VPTR_P (t))
6082 int vindex;
6083 tree fn;
6085 if (BINFO_VTABLE (TYPE_BINFO (t)))
6086 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6087 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6088 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6090 /* Add entries for virtual functions introduced by this class. */
6091 BINFO_VIRTUALS (TYPE_BINFO (t))
6092 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6094 /* Set DECL_VINDEX for all functions declared in this class. */
6095 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6097 fn = TREE_CHAIN (fn),
6098 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6099 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6101 tree fndecl = BV_FN (fn);
6103 if (DECL_THUNK_P (fndecl))
6104 /* A thunk. We should never be calling this entry directly
6105 from this vtable -- we'd use the entry for the non
6106 thunk base function. */
6107 DECL_VINDEX (fndecl) = NULL_TREE;
6108 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6109 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6113 finish_struct_bits (t);
6114 set_method_tm_attributes (t);
6116 /* Complete the rtl for any static member objects of the type we're
6117 working on. */
6118 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6119 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
6120 && TREE_TYPE (x) != error_mark_node
6121 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6122 DECL_MODE (x) = TYPE_MODE (t);
6124 /* Done with FIELDS...now decide whether to sort these for
6125 faster lookups later.
6127 We use a small number because most searches fail (succeeding
6128 ultimately as the search bores through the inheritance
6129 hierarchy), and we want this failure to occur quickly. */
6131 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6133 /* Complain if one of the field types requires lower visibility. */
6134 constrain_class_visibility (t);
6136 /* Make the rtl for any new vtables we have created, and unmark
6137 the base types we marked. */
6138 finish_vtbls (t);
6140 /* Build the VTT for T. */
6141 build_vtt (t);
6143 /* This warning does not make sense for Java classes, since they
6144 cannot have destructors. */
6145 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6147 tree dtor;
6149 dtor = CLASSTYPE_DESTRUCTORS (t);
6150 if (/* An implicitly declared destructor is always public. And,
6151 if it were virtual, we would have created it by now. */
6152 !dtor
6153 || (!DECL_VINDEX (dtor)
6154 && (/* public non-virtual */
6155 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6156 || (/* non-public non-virtual with friends */
6157 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6158 && (CLASSTYPE_FRIEND_CLASSES (t)
6159 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6160 warning (OPT_Wnon_virtual_dtor,
6161 "%q#T has virtual functions and accessible"
6162 " non-virtual destructor", t);
6165 complete_vars (t);
6167 if (warn_overloaded_virtual)
6168 warn_hidden (t);
6170 /* Class layout, assignment of virtual table slots, etc., is now
6171 complete. Give the back end a chance to tweak the visibility of
6172 the class or perform any other required target modifications. */
6173 targetm.cxx.adjust_class_at_definition (t);
6175 maybe_suppress_debug_info (t);
6177 dump_class_hierarchy (t);
6179 /* Finish debugging output for this type. */
6180 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6182 if (TYPE_TRANSPARENT_AGGR (t))
6184 tree field = first_field (t);
6185 if (field == NULL_TREE || error_operand_p (field))
6187 error ("type transparent class %qT does not have any fields", t);
6188 TYPE_TRANSPARENT_AGGR (t) = 0;
6190 else if (DECL_ARTIFICIAL (field))
6192 if (DECL_FIELD_IS_BASE (field))
6193 error ("type transparent class %qT has base classes", t);
6194 else
6196 gcc_checking_assert (DECL_VIRTUAL_P (field));
6197 error ("type transparent class %qT has virtual functions", t);
6199 TYPE_TRANSPARENT_AGGR (t) = 0;
6204 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6205 equal to THRESHOLD or greater than THRESHOLD. */
6207 static void
6208 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6210 int n_fields = count_fields (fields);
6211 if (n_fields >= threshold)
6213 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6214 add_fields_to_record_type (fields, field_vec, 0);
6215 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6216 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6220 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6222 void
6223 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6225 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6226 if (sorted_fields)
6228 int i;
6229 int n_fields
6230 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6231 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6233 for (i = 0; i < sorted_fields->len; ++i)
6234 field_vec->elts[i] = sorted_fields->elts[i];
6236 add_enum_fields_to_record_type (enumtype, field_vec,
6237 sorted_fields->len);
6238 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6239 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6243 /* When T was built up, the member declarations were added in reverse
6244 order. Rearrange them to declaration order. */
6246 void
6247 unreverse_member_declarations (tree t)
6249 tree next;
6250 tree prev;
6251 tree x;
6253 /* The following lists are all in reverse order. Put them in
6254 declaration order now. */
6255 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6256 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6258 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6259 reverse order, so we can't just use nreverse. */
6260 prev = NULL_TREE;
6261 for (x = TYPE_FIELDS (t);
6262 x && TREE_CODE (x) != TYPE_DECL;
6263 x = next)
6265 next = DECL_CHAIN (x);
6266 DECL_CHAIN (x) = prev;
6267 prev = x;
6269 if (prev)
6271 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6272 if (prev)
6273 TYPE_FIELDS (t) = prev;
6277 tree
6278 finish_struct (tree t, tree attributes)
6280 location_t saved_loc = input_location;
6282 /* Now that we've got all the field declarations, reverse everything
6283 as necessary. */
6284 unreverse_member_declarations (t);
6286 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6288 /* Nadger the current location so that diagnostics point to the start of
6289 the struct, not the end. */
6290 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6292 if (processing_template_decl)
6294 tree x;
6296 finish_struct_methods (t);
6297 TYPE_SIZE (t) = bitsize_zero_node;
6298 TYPE_SIZE_UNIT (t) = size_zero_node;
6300 /* We need to emit an error message if this type was used as a parameter
6301 and it is an abstract type, even if it is a template. We construct
6302 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6303 account and we call complete_vars with this type, which will check
6304 the PARM_DECLS. Note that while the type is being defined,
6305 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6306 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6307 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6308 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6309 if (DECL_PURE_VIRTUAL_P (x))
6310 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
6311 complete_vars (t);
6312 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6313 an enclosing scope is a template class, so that this function be
6314 found by lookup_fnfields_1 when the using declaration is not
6315 instantiated yet. */
6316 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6317 if (TREE_CODE (x) == USING_DECL)
6319 tree fn = strip_using_decl (x);
6320 if (is_overloaded_fn (fn))
6321 for (; fn; fn = OVL_NEXT (fn))
6322 add_method (t, OVL_CURRENT (fn), x);
6325 /* Remember current #pragma pack value. */
6326 TYPE_PRECISION (t) = maximum_field_alignment;
6328 else
6329 finish_struct_1 (t);
6331 input_location = saved_loc;
6333 TYPE_BEING_DEFINED (t) = 0;
6335 if (current_class_type)
6336 popclass ();
6337 else
6338 error ("trying to finish struct, but kicked out due to previous parse errors");
6340 if (processing_template_decl && at_function_scope_p ())
6341 add_stmt (build_min (TAG_DEFN, t));
6343 return t;
6346 /* Return the dynamic type of INSTANCE, if known.
6347 Used to determine whether the virtual function table is needed
6348 or not.
6350 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6351 of our knowledge of its type. *NONNULL should be initialized
6352 before this function is called. */
6354 static tree
6355 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6357 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6359 switch (TREE_CODE (instance))
6361 case INDIRECT_REF:
6362 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6363 return NULL_TREE;
6364 else
6365 return RECUR (TREE_OPERAND (instance, 0));
6367 case CALL_EXPR:
6368 /* This is a call to a constructor, hence it's never zero. */
6369 if (TREE_HAS_CONSTRUCTOR (instance))
6371 if (nonnull)
6372 *nonnull = 1;
6373 return TREE_TYPE (instance);
6375 return NULL_TREE;
6377 case SAVE_EXPR:
6378 /* This is a call to a constructor, hence it's never zero. */
6379 if (TREE_HAS_CONSTRUCTOR (instance))
6381 if (nonnull)
6382 *nonnull = 1;
6383 return TREE_TYPE (instance);
6385 return RECUR (TREE_OPERAND (instance, 0));
6387 case POINTER_PLUS_EXPR:
6388 case PLUS_EXPR:
6389 case MINUS_EXPR:
6390 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6391 return RECUR (TREE_OPERAND (instance, 0));
6392 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6393 /* Propagate nonnull. */
6394 return RECUR (TREE_OPERAND (instance, 0));
6396 return NULL_TREE;
6398 CASE_CONVERT:
6399 return RECUR (TREE_OPERAND (instance, 0));
6401 case ADDR_EXPR:
6402 instance = TREE_OPERAND (instance, 0);
6403 if (nonnull)
6405 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6406 with a real object -- given &p->f, p can still be null. */
6407 tree t = get_base_address (instance);
6408 /* ??? Probably should check DECL_WEAK here. */
6409 if (t && DECL_P (t))
6410 *nonnull = 1;
6412 return RECUR (instance);
6414 case COMPONENT_REF:
6415 /* If this component is really a base class reference, then the field
6416 itself isn't definitive. */
6417 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6418 return RECUR (TREE_OPERAND (instance, 0));
6419 return RECUR (TREE_OPERAND (instance, 1));
6421 case VAR_DECL:
6422 case FIELD_DECL:
6423 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6424 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6426 if (nonnull)
6427 *nonnull = 1;
6428 return TREE_TYPE (TREE_TYPE (instance));
6430 /* fall through... */
6431 case TARGET_EXPR:
6432 case PARM_DECL:
6433 case RESULT_DECL:
6434 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6436 if (nonnull)
6437 *nonnull = 1;
6438 return TREE_TYPE (instance);
6440 else if (instance == current_class_ptr)
6442 if (nonnull)
6443 *nonnull = 1;
6445 /* if we're in a ctor or dtor, we know our type. If
6446 current_class_ptr is set but we aren't in a function, we're in
6447 an NSDMI (and therefore a constructor). */
6448 if (current_scope () != current_function_decl
6449 || (DECL_LANG_SPECIFIC (current_function_decl)
6450 && (DECL_CONSTRUCTOR_P (current_function_decl)
6451 || DECL_DESTRUCTOR_P (current_function_decl))))
6453 if (cdtorp)
6454 *cdtorp = 1;
6455 return TREE_TYPE (TREE_TYPE (instance));
6458 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6460 /* We only need one hash table because it is always left empty. */
6461 static htab_t ht;
6462 if (!ht)
6463 ht = htab_create (37,
6464 htab_hash_pointer,
6465 htab_eq_pointer,
6466 /*htab_del=*/NULL);
6468 /* Reference variables should be references to objects. */
6469 if (nonnull)
6470 *nonnull = 1;
6472 /* Enter the INSTANCE in a table to prevent recursion; a
6473 variable's initializer may refer to the variable
6474 itself. */
6475 if (TREE_CODE (instance) == VAR_DECL
6476 && DECL_INITIAL (instance)
6477 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6478 && !htab_find (ht, instance))
6480 tree type;
6481 void **slot;
6483 slot = htab_find_slot (ht, instance, INSERT);
6484 *slot = instance;
6485 type = RECUR (DECL_INITIAL (instance));
6486 htab_remove_elt (ht, instance);
6488 return type;
6491 return NULL_TREE;
6493 default:
6494 return NULL_TREE;
6496 #undef RECUR
6499 /* Return nonzero if the dynamic type of INSTANCE is known, and
6500 equivalent to the static type. We also handle the case where
6501 INSTANCE is really a pointer. Return negative if this is a
6502 ctor/dtor. There the dynamic type is known, but this might not be
6503 the most derived base of the original object, and hence virtual
6504 bases may not be layed out according to this type.
6506 Used to determine whether the virtual function table is needed
6507 or not.
6509 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6510 of our knowledge of its type. *NONNULL should be initialized
6511 before this function is called. */
6514 resolves_to_fixed_type_p (tree instance, int* nonnull)
6516 tree t = TREE_TYPE (instance);
6517 int cdtorp = 0;
6518 tree fixed;
6520 if (processing_template_decl)
6522 /* In a template we only care about the type of the result. */
6523 if (nonnull)
6524 *nonnull = true;
6525 return true;
6528 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6529 if (fixed == NULL_TREE)
6530 return 0;
6531 if (POINTER_TYPE_P (t))
6532 t = TREE_TYPE (t);
6533 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6534 return 0;
6535 return cdtorp ? -1 : 1;
6539 void
6540 init_class_processing (void)
6542 current_class_depth = 0;
6543 current_class_stack_size = 10;
6544 current_class_stack
6545 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6546 local_classes = VEC_alloc (tree, gc, 8);
6547 sizeof_biggest_empty_class = size_zero_node;
6549 ridpointers[(int) RID_PUBLIC] = access_public_node;
6550 ridpointers[(int) RID_PRIVATE] = access_private_node;
6551 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6554 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6556 static void
6557 restore_class_cache (void)
6559 tree type;
6561 /* We are re-entering the same class we just left, so we don't
6562 have to search the whole inheritance matrix to find all the
6563 decls to bind again. Instead, we install the cached
6564 class_shadowed list and walk through it binding names. */
6565 push_binding_level (previous_class_level);
6566 class_binding_level = previous_class_level;
6567 /* Restore IDENTIFIER_TYPE_VALUE. */
6568 for (type = class_binding_level->type_shadowed;
6569 type;
6570 type = TREE_CHAIN (type))
6571 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6574 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6575 appropriate for TYPE.
6577 So that we may avoid calls to lookup_name, we cache the _TYPE
6578 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6580 For multiple inheritance, we perform a two-pass depth-first search
6581 of the type lattice. */
6583 void
6584 pushclass (tree type)
6586 class_stack_node_t csn;
6588 type = TYPE_MAIN_VARIANT (type);
6590 /* Make sure there is enough room for the new entry on the stack. */
6591 if (current_class_depth + 1 >= current_class_stack_size)
6593 current_class_stack_size *= 2;
6594 current_class_stack
6595 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6596 current_class_stack_size);
6599 /* Insert a new entry on the class stack. */
6600 csn = current_class_stack + current_class_depth;
6601 csn->name = current_class_name;
6602 csn->type = current_class_type;
6603 csn->access = current_access_specifier;
6604 csn->names_used = 0;
6605 csn->hidden = 0;
6606 current_class_depth++;
6608 /* Now set up the new type. */
6609 current_class_name = TYPE_NAME (type);
6610 if (TREE_CODE (current_class_name) == TYPE_DECL)
6611 current_class_name = DECL_NAME (current_class_name);
6612 current_class_type = type;
6614 /* By default, things in classes are private, while things in
6615 structures or unions are public. */
6616 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6617 ? access_private_node
6618 : access_public_node);
6620 if (previous_class_level
6621 && type != previous_class_level->this_entity
6622 && current_class_depth == 1)
6624 /* Forcibly remove any old class remnants. */
6625 invalidate_class_lookup_cache ();
6628 if (!previous_class_level
6629 || type != previous_class_level->this_entity
6630 || current_class_depth > 1)
6631 pushlevel_class ();
6632 else
6633 restore_class_cache ();
6636 /* When we exit a toplevel class scope, we save its binding level so
6637 that we can restore it quickly. Here, we've entered some other
6638 class, so we must invalidate our cache. */
6640 void
6641 invalidate_class_lookup_cache (void)
6643 previous_class_level = NULL;
6646 /* Get out of the current class scope. If we were in a class scope
6647 previously, that is the one popped to. */
6649 void
6650 popclass (void)
6652 poplevel_class ();
6654 current_class_depth--;
6655 current_class_name = current_class_stack[current_class_depth].name;
6656 current_class_type = current_class_stack[current_class_depth].type;
6657 current_access_specifier = current_class_stack[current_class_depth].access;
6658 if (current_class_stack[current_class_depth].names_used)
6659 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6662 /* Mark the top of the class stack as hidden. */
6664 void
6665 push_class_stack (void)
6667 if (current_class_depth)
6668 ++current_class_stack[current_class_depth - 1].hidden;
6671 /* Mark the top of the class stack as un-hidden. */
6673 void
6674 pop_class_stack (void)
6676 if (current_class_depth)
6677 --current_class_stack[current_class_depth - 1].hidden;
6680 /* Returns 1 if the class type currently being defined is either T or
6681 a nested type of T. */
6683 bool
6684 currently_open_class (tree t)
6686 int i;
6688 if (!CLASS_TYPE_P (t))
6689 return false;
6691 t = TYPE_MAIN_VARIANT (t);
6693 /* We start looking from 1 because entry 0 is from global scope,
6694 and has no type. */
6695 for (i = current_class_depth; i > 0; --i)
6697 tree c;
6698 if (i == current_class_depth)
6699 c = current_class_type;
6700 else
6702 if (current_class_stack[i].hidden)
6703 break;
6704 c = current_class_stack[i].type;
6706 if (!c)
6707 continue;
6708 if (same_type_p (c, t))
6709 return true;
6711 return false;
6714 /* If either current_class_type or one of its enclosing classes are derived
6715 from T, return the appropriate type. Used to determine how we found
6716 something via unqualified lookup. */
6718 tree
6719 currently_open_derived_class (tree t)
6721 int i;
6723 /* The bases of a dependent type are unknown. */
6724 if (dependent_type_p (t))
6725 return NULL_TREE;
6727 if (!current_class_type)
6728 return NULL_TREE;
6730 if (DERIVED_FROM_P (t, current_class_type))
6731 return current_class_type;
6733 for (i = current_class_depth - 1; i > 0; --i)
6735 if (current_class_stack[i].hidden)
6736 break;
6737 if (DERIVED_FROM_P (t, current_class_stack[i].type))
6738 return current_class_stack[i].type;
6741 return NULL_TREE;
6744 /* Returns the innermost class type which is not a lambda closure type. */
6746 tree
6747 current_nonlambda_class_type (void)
6749 int i;
6751 /* We start looking from 1 because entry 0 is from global scope,
6752 and has no type. */
6753 for (i = current_class_depth; i > 0; --i)
6755 tree c;
6756 if (i == current_class_depth)
6757 c = current_class_type;
6758 else
6760 if (current_class_stack[i].hidden)
6761 break;
6762 c = current_class_stack[i].type;
6764 if (!c)
6765 continue;
6766 if (!LAMBDA_TYPE_P (c))
6767 return c;
6769 return NULL_TREE;
6772 /* When entering a class scope, all enclosing class scopes' names with
6773 static meaning (static variables, static functions, types and
6774 enumerators) have to be visible. This recursive function calls
6775 pushclass for all enclosing class contexts until global or a local
6776 scope is reached. TYPE is the enclosed class. */
6778 void
6779 push_nested_class (tree type)
6781 /* A namespace might be passed in error cases, like A::B:C. */
6782 if (type == NULL_TREE
6783 || !CLASS_TYPE_P (type))
6784 return;
6786 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
6788 pushclass (type);
6791 /* Undoes a push_nested_class call. */
6793 void
6794 pop_nested_class (void)
6796 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6798 popclass ();
6799 if (context && CLASS_TYPE_P (context))
6800 pop_nested_class ();
6803 /* Returns the number of extern "LANG" blocks we are nested within. */
6806 current_lang_depth (void)
6808 return VEC_length (tree, current_lang_base);
6811 /* Set global variables CURRENT_LANG_NAME to appropriate value
6812 so that behavior of name-mangling machinery is correct. */
6814 void
6815 push_lang_context (tree name)
6817 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
6819 if (name == lang_name_cplusplus)
6821 current_lang_name = name;
6823 else if (name == lang_name_java)
6825 current_lang_name = name;
6826 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6827 (See record_builtin_java_type in decl.c.) However, that causes
6828 incorrect debug entries if these types are actually used.
6829 So we re-enable debug output after extern "Java". */
6830 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6831 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6832 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6833 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6834 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6835 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6836 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6837 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6839 else if (name == lang_name_c)
6841 current_lang_name = name;
6843 else
6844 error ("language string %<\"%E\"%> not recognized", name);
6847 /* Get out of the current language scope. */
6849 void
6850 pop_lang_context (void)
6852 current_lang_name = VEC_pop (tree, current_lang_base);
6855 /* Type instantiation routines. */
6857 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6858 matches the TARGET_TYPE. If there is no satisfactory match, return
6859 error_mark_node, and issue an error & warning messages under
6860 control of FLAGS. Permit pointers to member function if FLAGS
6861 permits. If TEMPLATE_ONLY, the name of the overloaded function was
6862 a template-id, and EXPLICIT_TARGS are the explicitly provided
6863 template arguments.
6865 If OVERLOAD is for one or more member functions, then ACCESS_PATH
6866 is the base path used to reference those member functions. If
6867 TF_NO_ACCESS_CONTROL is not set in FLAGS, and the address is
6868 resolved to a member function, access checks will be performed and
6869 errors issued if appropriate. */
6871 static tree
6872 resolve_address_of_overloaded_function (tree target_type,
6873 tree overload,
6874 tsubst_flags_t flags,
6875 bool template_only,
6876 tree explicit_targs,
6877 tree access_path)
6879 /* Here's what the standard says:
6881 [over.over]
6883 If the name is a function template, template argument deduction
6884 is done, and if the argument deduction succeeds, the deduced
6885 arguments are used to generate a single template function, which
6886 is added to the set of overloaded functions considered.
6888 Non-member functions and static member functions match targets of
6889 type "pointer-to-function" or "reference-to-function." Nonstatic
6890 member functions match targets of type "pointer-to-member
6891 function;" the function type of the pointer to member is used to
6892 select the member function from the set of overloaded member
6893 functions. If a nonstatic member function is selected, the
6894 reference to the overloaded function name is required to have the
6895 form of a pointer to member as described in 5.3.1.
6897 If more than one function is selected, any template functions in
6898 the set are eliminated if the set also contains a non-template
6899 function, and any given template function is eliminated if the
6900 set contains a second template function that is more specialized
6901 than the first according to the partial ordering rules 14.5.5.2.
6902 After such eliminations, if any, there shall remain exactly one
6903 selected function. */
6905 int is_ptrmem = 0;
6906 /* We store the matches in a TREE_LIST rooted here. The functions
6907 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6908 interoperability with most_specialized_instantiation. */
6909 tree matches = NULL_TREE;
6910 tree fn;
6911 tree target_fn_type;
6913 /* By the time we get here, we should be seeing only real
6914 pointer-to-member types, not the internal POINTER_TYPE to
6915 METHOD_TYPE representation. */
6916 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
6917 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
6919 gcc_assert (is_overloaded_fn (overload));
6921 /* Check that the TARGET_TYPE is reasonable. */
6922 if (TYPE_PTRFN_P (target_type))
6923 /* This is OK. */;
6924 else if (TYPE_PTRMEMFUNC_P (target_type))
6925 /* This is OK, too. */
6926 is_ptrmem = 1;
6927 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6928 /* This is OK, too. This comes from a conversion to reference
6929 type. */
6930 target_type = build_reference_type (target_type);
6931 else
6933 if (flags & tf_error)
6934 error ("cannot resolve overloaded function %qD based on"
6935 " conversion to type %qT",
6936 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6937 return error_mark_node;
6940 /* Non-member functions and static member functions match targets of type
6941 "pointer-to-function" or "reference-to-function." Nonstatic member
6942 functions match targets of type "pointer-to-member-function;" the
6943 function type of the pointer to member is used to select the member
6944 function from the set of overloaded member functions.
6946 So figure out the FUNCTION_TYPE that we want to match against. */
6947 target_fn_type = static_fn_type (target_type);
6949 /* If we can find a non-template function that matches, we can just
6950 use it. There's no point in generating template instantiations
6951 if we're just going to throw them out anyhow. But, of course, we
6952 can only do this when we don't *need* a template function. */
6953 if (!template_only)
6955 tree fns;
6957 for (fns = overload; fns; fns = OVL_NEXT (fns))
6959 tree fn = OVL_CURRENT (fns);
6961 if (TREE_CODE (fn) == TEMPLATE_DECL)
6962 /* We're not looking for templates just yet. */
6963 continue;
6965 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6966 != is_ptrmem)
6967 /* We're looking for a non-static member, and this isn't
6968 one, or vice versa. */
6969 continue;
6971 /* Ignore functions which haven't been explicitly
6972 declared. */
6973 if (DECL_ANTICIPATED (fn))
6974 continue;
6976 /* See if there's a match. */
6977 if (same_type_p (target_fn_type, static_fn_type (fn)))
6978 matches = tree_cons (fn, NULL_TREE, matches);
6982 /* Now, if we've already got a match (or matches), there's no need
6983 to proceed to the template functions. But, if we don't have a
6984 match we need to look at them, too. */
6985 if (!matches)
6987 tree target_arg_types;
6988 tree target_ret_type;
6989 tree fns;
6990 tree *args;
6991 unsigned int nargs, ia;
6992 tree arg;
6994 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6995 target_ret_type = TREE_TYPE (target_fn_type);
6997 nargs = list_length (target_arg_types);
6998 args = XALLOCAVEC (tree, nargs);
6999 for (arg = target_arg_types, ia = 0;
7000 arg != NULL_TREE && arg != void_list_node;
7001 arg = TREE_CHAIN (arg), ++ia)
7002 args[ia] = TREE_VALUE (arg);
7003 nargs = ia;
7005 for (fns = overload; fns; fns = OVL_NEXT (fns))
7007 tree fn = OVL_CURRENT (fns);
7008 tree instantiation;
7009 tree targs;
7011 if (TREE_CODE (fn) != TEMPLATE_DECL)
7012 /* We're only looking for templates. */
7013 continue;
7015 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7016 != is_ptrmem)
7017 /* We're not looking for a non-static member, and this is
7018 one, or vice versa. */
7019 continue;
7021 /* Try to do argument deduction. */
7022 targs = make_tree_vec (DECL_NTPARMS (fn));
7023 if (fn_type_unification (fn, explicit_targs, targs, args, nargs,
7024 target_ret_type, DEDUCE_EXACT,
7025 LOOKUP_NORMAL, false))
7026 /* Argument deduction failed. */
7027 continue;
7029 /* Instantiate the template. */
7030 instantiation = instantiate_template (fn, targs, flags);
7031 if (instantiation == error_mark_node)
7032 /* Instantiation failed. */
7033 continue;
7035 /* See if there's a match. */
7036 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7037 matches = tree_cons (instantiation, fn, matches);
7040 /* Now, remove all but the most specialized of the matches. */
7041 if (matches)
7043 tree match = most_specialized_instantiation (matches);
7045 if (match != error_mark_node)
7046 matches = tree_cons (TREE_PURPOSE (match),
7047 NULL_TREE,
7048 NULL_TREE);
7052 /* Now we should have exactly one function in MATCHES. */
7053 if (matches == NULL_TREE)
7055 /* There were *no* matches. */
7056 if (flags & tf_error)
7058 error ("no matches converting function %qD to type %q#T",
7059 DECL_NAME (OVL_CURRENT (overload)),
7060 target_type);
7062 print_candidates (overload);
7064 return error_mark_node;
7066 else if (TREE_CHAIN (matches))
7068 /* There were too many matches. First check if they're all
7069 the same function. */
7070 tree match;
7072 fn = TREE_PURPOSE (matches);
7073 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7074 if (!decls_match (fn, TREE_PURPOSE (match)))
7075 break;
7077 if (match)
7079 if (flags & tf_error)
7081 error ("converting overloaded function %qD to type %q#T is ambiguous",
7082 DECL_NAME (OVL_FUNCTION (overload)),
7083 target_type);
7085 /* Since print_candidates expects the functions in the
7086 TREE_VALUE slot, we flip them here. */
7087 for (match = matches; match; match = TREE_CHAIN (match))
7088 TREE_VALUE (match) = TREE_PURPOSE (match);
7090 print_candidates (matches);
7093 return error_mark_node;
7097 /* Good, exactly one match. Now, convert it to the correct type. */
7098 fn = TREE_PURPOSE (matches);
7100 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7101 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7103 static int explained;
7105 if (!(flags & tf_error))
7106 return error_mark_node;
7108 permerror (input_location, "assuming pointer to member %qD", fn);
7109 if (!explained)
7111 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7112 explained = 1;
7116 /* If we're doing overload resolution purely for the purpose of
7117 determining conversion sequences, we should not consider the
7118 function used. If this conversion sequence is selected, the
7119 function will be marked as used at this point. */
7120 if (!(flags & tf_conv))
7122 /* Make =delete work with SFINAE. */
7123 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7124 return error_mark_node;
7126 mark_used (fn);
7129 /* We could not check access to member functions when this
7130 expression was originally created since we did not know at that
7131 time to which function the expression referred. */
7132 if (!(flags & tf_no_access_control)
7133 && DECL_FUNCTION_MEMBER_P (fn))
7135 gcc_assert (access_path);
7136 perform_or_defer_access_check (access_path, fn, fn);
7139 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7140 return cp_build_addr_expr (fn, flags);
7141 else
7143 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7144 will mark the function as addressed, but here we must do it
7145 explicitly. */
7146 cxx_mark_addressable (fn);
7148 return fn;
7152 /* This function will instantiate the type of the expression given in
7153 RHS to match the type of LHSTYPE. If errors exist, then return
7154 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7155 we complain on errors. If we are not complaining, never modify rhs,
7156 as overload resolution wants to try many possible instantiations, in
7157 the hope that at least one will work.
7159 For non-recursive calls, LHSTYPE should be a function, pointer to
7160 function, or a pointer to member function. */
7162 tree
7163 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7165 tsubst_flags_t flags_in = flags;
7166 tree access_path = NULL_TREE;
7168 flags &= ~tf_ptrmem_ok;
7170 if (lhstype == unknown_type_node)
7172 if (flags & tf_error)
7173 error ("not enough type information");
7174 return error_mark_node;
7177 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7179 if (same_type_p (lhstype, TREE_TYPE (rhs)))
7180 return rhs;
7181 if (flag_ms_extensions
7182 && TYPE_PTRMEMFUNC_P (lhstype)
7183 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7184 /* Microsoft allows `A::f' to be resolved to a
7185 pointer-to-member. */
7187 else
7189 if (flags & tf_error)
7190 error ("cannot convert %qE from type %qT to type %qT",
7191 rhs, TREE_TYPE (rhs), lhstype);
7192 return error_mark_node;
7196 if (BASELINK_P (rhs))
7198 access_path = BASELINK_ACCESS_BINFO (rhs);
7199 rhs = BASELINK_FUNCTIONS (rhs);
7202 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7203 deduce any type information. */
7204 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7206 if (flags & tf_error)
7207 error ("not enough type information");
7208 return error_mark_node;
7211 /* There only a few kinds of expressions that may have a type
7212 dependent on overload resolution. */
7213 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7214 || TREE_CODE (rhs) == COMPONENT_REF
7215 || really_overloaded_fn (rhs)
7216 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7218 /* This should really only be used when attempting to distinguish
7219 what sort of a pointer to function we have. For now, any
7220 arithmetic operation which is not supported on pointers
7221 is rejected as an error. */
7223 switch (TREE_CODE (rhs))
7225 case COMPONENT_REF:
7227 tree member = TREE_OPERAND (rhs, 1);
7229 member = instantiate_type (lhstype, member, flags);
7230 if (member != error_mark_node
7231 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7232 /* Do not lose object's side effects. */
7233 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7234 TREE_OPERAND (rhs, 0), member);
7235 return member;
7238 case OFFSET_REF:
7239 rhs = TREE_OPERAND (rhs, 1);
7240 if (BASELINK_P (rhs))
7241 return instantiate_type (lhstype, rhs, flags_in);
7243 /* This can happen if we are forming a pointer-to-member for a
7244 member template. */
7245 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7247 /* Fall through. */
7249 case TEMPLATE_ID_EXPR:
7251 tree fns = TREE_OPERAND (rhs, 0);
7252 tree args = TREE_OPERAND (rhs, 1);
7254 return
7255 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7256 /*template_only=*/true,
7257 args, access_path);
7260 case OVERLOAD:
7261 case FUNCTION_DECL:
7262 return
7263 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7264 /*template_only=*/false,
7265 /*explicit_targs=*/NULL_TREE,
7266 access_path);
7268 case ADDR_EXPR:
7270 if (PTRMEM_OK_P (rhs))
7271 flags |= tf_ptrmem_ok;
7273 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7276 case ERROR_MARK:
7277 return error_mark_node;
7279 default:
7280 gcc_unreachable ();
7282 return error_mark_node;
7285 /* Return the name of the virtual function pointer field
7286 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7287 this may have to look back through base types to find the
7288 ultimate field name. (For single inheritance, these could
7289 all be the same name. Who knows for multiple inheritance). */
7291 static tree
7292 get_vfield_name (tree type)
7294 tree binfo, base_binfo;
7295 char *buf;
7297 for (binfo = TYPE_BINFO (type);
7298 BINFO_N_BASE_BINFOS (binfo);
7299 binfo = base_binfo)
7301 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7303 if (BINFO_VIRTUAL_P (base_binfo)
7304 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7305 break;
7308 type = BINFO_TYPE (binfo);
7309 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7310 + TYPE_NAME_LENGTH (type) + 2);
7311 sprintf (buf, VFIELD_NAME_FORMAT,
7312 IDENTIFIER_POINTER (constructor_name (type)));
7313 return get_identifier (buf);
7316 void
7317 print_class_statistics (void)
7319 #ifdef GATHER_STATISTICS
7320 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7321 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7322 if (n_vtables)
7324 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7325 n_vtables, n_vtable_searches);
7326 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7327 n_vtable_entries, n_vtable_elems);
7329 #endif
7332 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7333 according to [class]:
7334 The class-name is also inserted
7335 into the scope of the class itself. For purposes of access checking,
7336 the inserted class name is treated as if it were a public member name. */
7338 void
7339 build_self_reference (void)
7341 tree name = constructor_name (current_class_type);
7342 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7343 tree saved_cas;
7345 DECL_NONLOCAL (value) = 1;
7346 DECL_CONTEXT (value) = current_class_type;
7347 DECL_ARTIFICIAL (value) = 1;
7348 SET_DECL_SELF_REFERENCE_P (value);
7349 set_underlying_type (value);
7351 if (processing_template_decl)
7352 value = push_template_decl (value);
7354 saved_cas = current_access_specifier;
7355 current_access_specifier = access_public_node;
7356 finish_member_declaration (value);
7357 current_access_specifier = saved_cas;
7360 /* Returns 1 if TYPE contains only padding bytes. */
7363 is_empty_class (tree type)
7365 if (type == error_mark_node)
7366 return 0;
7368 if (! CLASS_TYPE_P (type))
7369 return 0;
7371 /* In G++ 3.2, whether or not a class was empty was determined by
7372 looking at its size. */
7373 if (abi_version_at_least (2))
7374 return CLASSTYPE_EMPTY_P (type);
7375 else
7376 return integer_zerop (CLASSTYPE_SIZE (type));
7379 /* Returns true if TYPE contains an empty class. */
7381 static bool
7382 contains_empty_class_p (tree type)
7384 if (is_empty_class (type))
7385 return true;
7386 if (CLASS_TYPE_P (type))
7388 tree field;
7389 tree binfo;
7390 tree base_binfo;
7391 int i;
7393 for (binfo = TYPE_BINFO (type), i = 0;
7394 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7395 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7396 return true;
7397 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7398 if (TREE_CODE (field) == FIELD_DECL
7399 && !DECL_ARTIFICIAL (field)
7400 && is_empty_class (TREE_TYPE (field)))
7401 return true;
7403 else if (TREE_CODE (type) == ARRAY_TYPE)
7404 return contains_empty_class_p (TREE_TYPE (type));
7405 return false;
7408 /* Returns true if TYPE contains no actual data, just various
7409 possible combinations of empty classes and possibly a vptr. */
7411 bool
7412 is_really_empty_class (tree type)
7414 if (CLASS_TYPE_P (type))
7416 tree field;
7417 tree binfo;
7418 tree base_binfo;
7419 int i;
7421 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7422 out, but we'd like to be able to check this before then. */
7423 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7424 return true;
7426 for (binfo = TYPE_BINFO (type), i = 0;
7427 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7428 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7429 return false;
7430 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7431 if (TREE_CODE (field) == FIELD_DECL
7432 && !DECL_ARTIFICIAL (field)
7433 && !is_really_empty_class (TREE_TYPE (field)))
7434 return false;
7435 return true;
7437 else if (TREE_CODE (type) == ARRAY_TYPE)
7438 return is_really_empty_class (TREE_TYPE (type));
7439 return false;
7442 /* Note that NAME was looked up while the current class was being
7443 defined and that the result of that lookup was DECL. */
7445 void
7446 maybe_note_name_used_in_class (tree name, tree decl)
7448 splay_tree names_used;
7450 /* If we're not defining a class, there's nothing to do. */
7451 if (!(innermost_scope_kind() == sk_class
7452 && TYPE_BEING_DEFINED (current_class_type)
7453 && !LAMBDA_TYPE_P (current_class_type)))
7454 return;
7456 /* If there's already a binding for this NAME, then we don't have
7457 anything to worry about. */
7458 if (lookup_member (current_class_type, name,
7459 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7460 return;
7462 if (!current_class_stack[current_class_depth - 1].names_used)
7463 current_class_stack[current_class_depth - 1].names_used
7464 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7465 names_used = current_class_stack[current_class_depth - 1].names_used;
7467 splay_tree_insert (names_used,
7468 (splay_tree_key) name,
7469 (splay_tree_value) decl);
7472 /* Note that NAME was declared (as DECL) in the current class. Check
7473 to see that the declaration is valid. */
7475 void
7476 note_name_declared_in_class (tree name, tree decl)
7478 splay_tree names_used;
7479 splay_tree_node n;
7481 /* Look to see if we ever used this name. */
7482 names_used
7483 = current_class_stack[current_class_depth - 1].names_used;
7484 if (!names_used)
7485 return;
7486 /* The C language allows members to be declared with a type of the same
7487 name, and the C++ standard says this diagnostic is not required. So
7488 allow it in extern "C" blocks unless predantic is specified.
7489 Allow it in all cases if -ms-extensions is specified. */
7490 if ((!pedantic && current_lang_name == lang_name_c)
7491 || flag_ms_extensions)
7492 return;
7493 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7494 if (n)
7496 /* [basic.scope.class]
7498 A name N used in a class S shall refer to the same declaration
7499 in its context and when re-evaluated in the completed scope of
7500 S. */
7501 permerror (input_location, "declaration of %q#D", decl);
7502 permerror (input_location, "changes meaning of %qD from %q+#D",
7503 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7507 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7508 Secondary vtables are merged with primary vtables; this function
7509 will return the VAR_DECL for the primary vtable. */
7511 tree
7512 get_vtbl_decl_for_binfo (tree binfo)
7514 tree decl;
7516 decl = BINFO_VTABLE (binfo);
7517 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7519 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7520 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7522 if (decl)
7523 gcc_assert (TREE_CODE (decl) == VAR_DECL);
7524 return decl;
7528 /* Returns the binfo for the primary base of BINFO. If the resulting
7529 BINFO is a virtual base, and it is inherited elsewhere in the
7530 hierarchy, then the returned binfo might not be the primary base of
7531 BINFO in the complete object. Check BINFO_PRIMARY_P or
7532 BINFO_LOST_PRIMARY_P to be sure. */
7534 static tree
7535 get_primary_binfo (tree binfo)
7537 tree primary_base;
7539 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7540 if (!primary_base)
7541 return NULL_TREE;
7543 return copied_binfo (primary_base, binfo);
7546 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7548 static int
7549 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7551 if (!indented_p)
7552 fprintf (stream, "%*s", indent, "");
7553 return 1;
7556 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7557 INDENT should be zero when called from the top level; it is
7558 incremented recursively. IGO indicates the next expected BINFO in
7559 inheritance graph ordering. */
7561 static tree
7562 dump_class_hierarchy_r (FILE *stream,
7563 int flags,
7564 tree binfo,
7565 tree igo,
7566 int indent)
7568 int indented = 0;
7569 tree base_binfo;
7570 int i;
7572 indented = maybe_indent_hierarchy (stream, indent, 0);
7573 fprintf (stream, "%s (0x%lx) ",
7574 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7575 (unsigned long) binfo);
7576 if (binfo != igo)
7578 fprintf (stream, "alternative-path\n");
7579 return igo;
7581 igo = TREE_CHAIN (binfo);
7583 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7584 tree_low_cst (BINFO_OFFSET (binfo), 0));
7585 if (is_empty_class (BINFO_TYPE (binfo)))
7586 fprintf (stream, " empty");
7587 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7588 fprintf (stream, " nearly-empty");
7589 if (BINFO_VIRTUAL_P (binfo))
7590 fprintf (stream, " virtual");
7591 fprintf (stream, "\n");
7593 indented = 0;
7594 if (BINFO_PRIMARY_P (binfo))
7596 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7597 fprintf (stream, " primary-for %s (0x%lx)",
7598 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7599 TFF_PLAIN_IDENTIFIER),
7600 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
7602 if (BINFO_LOST_PRIMARY_P (binfo))
7604 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7605 fprintf (stream, " lost-primary");
7607 if (indented)
7608 fprintf (stream, "\n");
7610 if (!(flags & TDF_SLIM))
7612 int indented = 0;
7614 if (BINFO_SUBVTT_INDEX (binfo))
7616 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7617 fprintf (stream, " subvttidx=%s",
7618 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7619 TFF_PLAIN_IDENTIFIER));
7621 if (BINFO_VPTR_INDEX (binfo))
7623 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7624 fprintf (stream, " vptridx=%s",
7625 expr_as_string (BINFO_VPTR_INDEX (binfo),
7626 TFF_PLAIN_IDENTIFIER));
7628 if (BINFO_VPTR_FIELD (binfo))
7630 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7631 fprintf (stream, " vbaseoffset=%s",
7632 expr_as_string (BINFO_VPTR_FIELD (binfo),
7633 TFF_PLAIN_IDENTIFIER));
7635 if (BINFO_VTABLE (binfo))
7637 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7638 fprintf (stream, " vptr=%s",
7639 expr_as_string (BINFO_VTABLE (binfo),
7640 TFF_PLAIN_IDENTIFIER));
7643 if (indented)
7644 fprintf (stream, "\n");
7647 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7648 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7650 return igo;
7653 /* Dump the BINFO hierarchy for T. */
7655 static void
7656 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7658 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7659 fprintf (stream, " size=%lu align=%lu\n",
7660 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7661 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7662 fprintf (stream, " base size=%lu base align=%lu\n",
7663 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7664 / BITS_PER_UNIT),
7665 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7666 / BITS_PER_UNIT));
7667 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7668 fprintf (stream, "\n");
7671 /* Debug interface to hierarchy dumping. */
7673 void
7674 debug_class (tree t)
7676 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7679 static void
7680 dump_class_hierarchy (tree t)
7682 int flags;
7683 FILE *stream = dump_begin (TDI_class, &flags);
7685 if (stream)
7687 dump_class_hierarchy_1 (stream, flags, t);
7688 dump_end (TDI_class, stream);
7692 static void
7693 dump_array (FILE * stream, tree decl)
7695 tree value;
7696 unsigned HOST_WIDE_INT ix;
7697 HOST_WIDE_INT elt;
7698 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7700 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7701 / BITS_PER_UNIT);
7702 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7703 fprintf (stream, " %s entries",
7704 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7705 TFF_PLAIN_IDENTIFIER));
7706 fprintf (stream, "\n");
7708 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7709 ix, value)
7710 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
7711 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7714 static void
7715 dump_vtable (tree t, tree binfo, tree vtable)
7717 int flags;
7718 FILE *stream = dump_begin (TDI_class, &flags);
7720 if (!stream)
7721 return;
7723 if (!(flags & TDF_SLIM))
7725 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7727 fprintf (stream, "%s for %s",
7728 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7729 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7730 if (ctor_vtbl_p)
7732 if (!BINFO_VIRTUAL_P (binfo))
7733 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7734 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7736 fprintf (stream, "\n");
7737 dump_array (stream, vtable);
7738 fprintf (stream, "\n");
7741 dump_end (TDI_class, stream);
7744 static void
7745 dump_vtt (tree t, tree vtt)
7747 int flags;
7748 FILE *stream = dump_begin (TDI_class, &flags);
7750 if (!stream)
7751 return;
7753 if (!(flags & TDF_SLIM))
7755 fprintf (stream, "VTT for %s\n",
7756 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7757 dump_array (stream, vtt);
7758 fprintf (stream, "\n");
7761 dump_end (TDI_class, stream);
7764 /* Dump a function or thunk and its thunkees. */
7766 static void
7767 dump_thunk (FILE *stream, int indent, tree thunk)
7769 static const char spaces[] = " ";
7770 tree name = DECL_NAME (thunk);
7771 tree thunks;
7773 fprintf (stream, "%.*s%p %s %s", indent, spaces,
7774 (void *)thunk,
7775 !DECL_THUNK_P (thunk) ? "function"
7776 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
7777 name ? IDENTIFIER_POINTER (name) : "<unset>");
7778 if (DECL_THUNK_P (thunk))
7780 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
7781 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
7783 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
7784 if (!virtual_adjust)
7785 /*NOP*/;
7786 else if (DECL_THIS_THUNK_P (thunk))
7787 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
7788 tree_low_cst (virtual_adjust, 0));
7789 else
7790 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
7791 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
7792 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
7793 if (THUNK_ALIAS (thunk))
7794 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
7796 fprintf (stream, "\n");
7797 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
7798 dump_thunk (stream, indent + 2, thunks);
7801 /* Dump the thunks for FN. */
7803 void
7804 debug_thunks (tree fn)
7806 dump_thunk (stderr, 0, fn);
7809 /* Virtual function table initialization. */
7811 /* Create all the necessary vtables for T and its base classes. */
7813 static void
7814 finish_vtbls (tree t)
7816 tree vbase;
7817 VEC(constructor_elt,gc) *v = NULL;
7818 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
7820 /* We lay out the primary and secondary vtables in one contiguous
7821 vtable. The primary vtable is first, followed by the non-virtual
7822 secondary vtables in inheritance graph order. */
7823 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
7824 vtable, t, &v);
7826 /* Then come the virtual bases, also in inheritance graph order. */
7827 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7829 if (!BINFO_VIRTUAL_P (vbase))
7830 continue;
7831 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
7834 if (BINFO_VTABLE (TYPE_BINFO (t)))
7835 initialize_vtable (TYPE_BINFO (t), v);
7838 /* Initialize the vtable for BINFO with the INITS. */
7840 static void
7841 initialize_vtable (tree binfo, VEC(constructor_elt,gc) *inits)
7843 tree decl;
7845 layout_vtable_decl (binfo, VEC_length (constructor_elt, inits));
7846 decl = get_vtbl_decl_for_binfo (binfo);
7847 initialize_artificial_var (decl, inits);
7848 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7851 /* Build the VTT (virtual table table) for T.
7852 A class requires a VTT if it has virtual bases.
7854 This holds
7855 1 - primary virtual pointer for complete object T
7856 2 - secondary VTTs for each direct non-virtual base of T which requires a
7858 3 - secondary virtual pointers for each direct or indirect base of T which
7859 has virtual bases or is reachable via a virtual path from T.
7860 4 - secondary VTTs for each direct or indirect virtual base of T.
7862 Secondary VTTs look like complete object VTTs without part 4. */
7864 static void
7865 build_vtt (tree t)
7867 tree type;
7868 tree vtt;
7869 tree index;
7870 VEC(constructor_elt,gc) *inits;
7872 /* Build up the initializers for the VTT. */
7873 inits = NULL;
7874 index = size_zero_node;
7875 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7877 /* If we didn't need a VTT, we're done. */
7878 if (!inits)
7879 return;
7881 /* Figure out the type of the VTT. */
7882 type = build_array_of_n_type (const_ptr_type_node,
7883 VEC_length (constructor_elt, inits));
7885 /* Now, build the VTT object itself. */
7886 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
7887 initialize_artificial_var (vtt, inits);
7888 /* Add the VTT to the vtables list. */
7889 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
7890 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7892 dump_vtt (t, vtt);
7895 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7896 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7897 and CHAIN the vtable pointer for this binfo after construction is
7898 complete. VALUE can also be another BINFO, in which case we recurse. */
7900 static tree
7901 binfo_ctor_vtable (tree binfo)
7903 tree vt;
7905 while (1)
7907 vt = BINFO_VTABLE (binfo);
7908 if (TREE_CODE (vt) == TREE_LIST)
7909 vt = TREE_VALUE (vt);
7910 if (TREE_CODE (vt) == TREE_BINFO)
7911 binfo = vt;
7912 else
7913 break;
7916 return vt;
7919 /* Data for secondary VTT initialization. */
7920 typedef struct secondary_vptr_vtt_init_data_s
7922 /* Is this the primary VTT? */
7923 bool top_level_p;
7925 /* Current index into the VTT. */
7926 tree index;
7928 /* Vector of initializers built up. */
7929 VEC(constructor_elt,gc) *inits;
7931 /* The type being constructed by this secondary VTT. */
7932 tree type_being_constructed;
7933 } secondary_vptr_vtt_init_data;
7935 /* Recursively build the VTT-initializer for BINFO (which is in the
7936 hierarchy dominated by T). INITS points to the end of the initializer
7937 list to date. INDEX is the VTT index where the next element will be
7938 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7939 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7940 for virtual bases of T. When it is not so, we build the constructor
7941 vtables for the BINFO-in-T variant. */
7943 static void
7944 build_vtt_inits (tree binfo, tree t, VEC(constructor_elt,gc) **inits, tree *index)
7946 int i;
7947 tree b;
7948 tree init;
7949 secondary_vptr_vtt_init_data data;
7950 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7952 /* We only need VTTs for subobjects with virtual bases. */
7953 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7954 return;
7956 /* We need to use a construction vtable if this is not the primary
7957 VTT. */
7958 if (!top_level_p)
7960 build_ctor_vtbl_group (binfo, t);
7962 /* Record the offset in the VTT where this sub-VTT can be found. */
7963 BINFO_SUBVTT_INDEX (binfo) = *index;
7966 /* Add the address of the primary vtable for the complete object. */
7967 init = binfo_ctor_vtable (binfo);
7968 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
7969 if (top_level_p)
7971 gcc_assert (!BINFO_VPTR_INDEX (binfo));
7972 BINFO_VPTR_INDEX (binfo) = *index;
7974 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7976 /* Recursively add the secondary VTTs for non-virtual bases. */
7977 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
7978 if (!BINFO_VIRTUAL_P (b))
7979 build_vtt_inits (b, t, inits, index);
7981 /* Add secondary virtual pointers for all subobjects of BINFO with
7982 either virtual bases or reachable along a virtual path, except
7983 subobjects that are non-virtual primary bases. */
7984 data.top_level_p = top_level_p;
7985 data.index = *index;
7986 data.inits = *inits;
7987 data.type_being_constructed = BINFO_TYPE (binfo);
7989 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
7991 *index = data.index;
7993 /* data.inits might have grown as we added secondary virtual pointers.
7994 Make sure our caller knows about the new vector. */
7995 *inits = data.inits;
7997 if (top_level_p)
7998 /* Add the secondary VTTs for virtual bases in inheritance graph
7999 order. */
8000 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8002 if (!BINFO_VIRTUAL_P (b))
8003 continue;
8005 build_vtt_inits (b, t, inits, index);
8007 else
8008 /* Remove the ctor vtables we created. */
8009 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8012 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8013 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8015 static tree
8016 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8018 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8020 /* We don't care about bases that don't have vtables. */
8021 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8022 return dfs_skip_bases;
8024 /* We're only interested in proper subobjects of the type being
8025 constructed. */
8026 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8027 return NULL_TREE;
8029 /* We're only interested in bases with virtual bases or reachable
8030 via a virtual path from the type being constructed. */
8031 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8032 || binfo_via_virtual (binfo, data->type_being_constructed)))
8033 return dfs_skip_bases;
8035 /* We're not interested in non-virtual primary bases. */
8036 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8037 return NULL_TREE;
8039 /* Record the index where this secondary vptr can be found. */
8040 if (data->top_level_p)
8042 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8043 BINFO_VPTR_INDEX (binfo) = data->index;
8045 if (BINFO_VIRTUAL_P (binfo))
8047 /* It's a primary virtual base, and this is not a
8048 construction vtable. Find the base this is primary of in
8049 the inheritance graph, and use that base's vtable
8050 now. */
8051 while (BINFO_PRIMARY_P (binfo))
8052 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8056 /* Add the initializer for the secondary vptr itself. */
8057 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8059 /* Advance the vtt index. */
8060 data->index = size_binop (PLUS_EXPR, data->index,
8061 TYPE_SIZE_UNIT (ptr_type_node));
8063 return NULL_TREE;
8066 /* Called from build_vtt_inits via dfs_walk. After building
8067 constructor vtables and generating the sub-vtt from them, we need
8068 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8069 binfo of the base whose sub vtt was generated. */
8071 static tree
8072 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8074 tree vtable = BINFO_VTABLE (binfo);
8076 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8077 /* If this class has no vtable, none of its bases do. */
8078 return dfs_skip_bases;
8080 if (!vtable)
8081 /* This might be a primary base, so have no vtable in this
8082 hierarchy. */
8083 return NULL_TREE;
8085 /* If we scribbled the construction vtable vptr into BINFO, clear it
8086 out now. */
8087 if (TREE_CODE (vtable) == TREE_LIST
8088 && (TREE_PURPOSE (vtable) == (tree) data))
8089 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8091 return NULL_TREE;
8094 /* Build the construction vtable group for BINFO which is in the
8095 hierarchy dominated by T. */
8097 static void
8098 build_ctor_vtbl_group (tree binfo, tree t)
8100 tree type;
8101 tree vtbl;
8102 tree id;
8103 tree vbase;
8104 VEC(constructor_elt,gc) *v;
8106 /* See if we've already created this construction vtable group. */
8107 id = mangle_ctor_vtbl_for_type (t, binfo);
8108 if (IDENTIFIER_GLOBAL_VALUE (id))
8109 return;
8111 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8112 /* Build a version of VTBL (with the wrong type) for use in
8113 constructing the addresses of secondary vtables in the
8114 construction vtable group. */
8115 vtbl = build_vtable (t, id, ptr_type_node);
8116 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8118 v = NULL;
8119 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8120 binfo, vtbl, t, &v);
8122 /* Add the vtables for each of our virtual bases using the vbase in T
8123 binfo. */
8124 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8125 vbase;
8126 vbase = TREE_CHAIN (vbase))
8128 tree b;
8130 if (!BINFO_VIRTUAL_P (vbase))
8131 continue;
8132 b = copied_binfo (vbase, binfo);
8134 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8137 /* Figure out the type of the construction vtable. */
8138 type = build_array_of_n_type (vtable_entry_type,
8139 VEC_length (constructor_elt, v));
8140 layout_type (type);
8141 TREE_TYPE (vtbl) = type;
8142 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8143 layout_decl (vtbl, 0);
8145 /* Initialize the construction vtable. */
8146 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8147 initialize_artificial_var (vtbl, v);
8148 dump_vtable (t, binfo, vtbl);
8151 /* Add the vtbl initializers for BINFO (and its bases other than
8152 non-virtual primaries) to the list of INITS. BINFO is in the
8153 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8154 the constructor the vtbl inits should be accumulated for. (If this
8155 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8156 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8157 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8158 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8159 but are not necessarily the same in terms of layout. */
8161 static void
8162 accumulate_vtbl_inits (tree binfo,
8163 tree orig_binfo,
8164 tree rtti_binfo,
8165 tree vtbl,
8166 tree t,
8167 VEC(constructor_elt,gc) **inits)
8169 int i;
8170 tree base_binfo;
8171 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8173 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8175 /* If it doesn't have a vptr, we don't do anything. */
8176 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8177 return;
8179 /* If we're building a construction vtable, we're not interested in
8180 subobjects that don't require construction vtables. */
8181 if (ctor_vtbl_p
8182 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8183 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8184 return;
8186 /* Build the initializers for the BINFO-in-T vtable. */
8187 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8189 /* Walk the BINFO and its bases. We walk in preorder so that as we
8190 initialize each vtable we can figure out at what offset the
8191 secondary vtable lies from the primary vtable. We can't use
8192 dfs_walk here because we need to iterate through bases of BINFO
8193 and RTTI_BINFO simultaneously. */
8194 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8196 /* Skip virtual bases. */
8197 if (BINFO_VIRTUAL_P (base_binfo))
8198 continue;
8199 accumulate_vtbl_inits (base_binfo,
8200 BINFO_BASE_BINFO (orig_binfo, i),
8201 rtti_binfo, vtbl, t,
8202 inits);
8206 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8207 BINFO vtable to L. */
8209 static void
8210 dfs_accumulate_vtbl_inits (tree binfo,
8211 tree orig_binfo,
8212 tree rtti_binfo,
8213 tree orig_vtbl,
8214 tree t,
8215 VEC(constructor_elt,gc) **l)
8217 tree vtbl = NULL_TREE;
8218 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8219 int n_inits;
8221 if (ctor_vtbl_p
8222 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8224 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8225 primary virtual base. If it is not the same primary in
8226 the hierarchy of T, we'll need to generate a ctor vtable
8227 for it, to place at its location in T. If it is the same
8228 primary, we still need a VTT entry for the vtable, but it
8229 should point to the ctor vtable for the base it is a
8230 primary for within the sub-hierarchy of RTTI_BINFO.
8232 There are three possible cases:
8234 1) We are in the same place.
8235 2) We are a primary base within a lost primary virtual base of
8236 RTTI_BINFO.
8237 3) We are primary to something not a base of RTTI_BINFO. */
8239 tree b;
8240 tree last = NULL_TREE;
8242 /* First, look through the bases we are primary to for RTTI_BINFO
8243 or a virtual base. */
8244 b = binfo;
8245 while (BINFO_PRIMARY_P (b))
8247 b = BINFO_INHERITANCE_CHAIN (b);
8248 last = b;
8249 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8250 goto found;
8252 /* If we run out of primary links, keep looking down our
8253 inheritance chain; we might be an indirect primary. */
8254 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8255 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8256 break;
8257 found:
8259 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8260 base B and it is a base of RTTI_BINFO, this is case 2. In
8261 either case, we share our vtable with LAST, i.e. the
8262 derived-most base within B of which we are a primary. */
8263 if (b == rtti_binfo
8264 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8265 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8266 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8267 binfo_ctor_vtable after everything's been set up. */
8268 vtbl = last;
8270 /* Otherwise, this is case 3 and we get our own. */
8272 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8273 return;
8275 n_inits = VEC_length (constructor_elt, *l);
8277 if (!vtbl)
8279 tree index;
8280 int non_fn_entries;
8282 /* Add the initializer for this vtable. */
8283 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8284 &non_fn_entries, l);
8286 /* Figure out the position to which the VPTR should point. */
8287 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8288 index = size_binop (MULT_EXPR,
8289 TYPE_SIZE_UNIT (vtable_entry_type),
8290 size_int (non_fn_entries + n_inits));
8291 vtbl = fold_build_pointer_plus (vtbl, index);
8294 if (ctor_vtbl_p)
8295 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8296 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8297 straighten this out. */
8298 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8299 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8300 /* Throw away any unneeded intializers. */
8301 VEC_truncate (constructor_elt, *l, n_inits);
8302 else
8303 /* For an ordinary vtable, set BINFO_VTABLE. */
8304 BINFO_VTABLE (binfo) = vtbl;
8307 static GTY(()) tree abort_fndecl_addr;
8309 /* Construct the initializer for BINFO's virtual function table. BINFO
8310 is part of the hierarchy dominated by T. If we're building a
8311 construction vtable, the ORIG_BINFO is the binfo we should use to
8312 find the actual function pointers to put in the vtable - but they
8313 can be overridden on the path to most-derived in the graph that
8314 ORIG_BINFO belongs. Otherwise,
8315 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8316 BINFO that should be indicated by the RTTI information in the
8317 vtable; it will be a base class of T, rather than T itself, if we
8318 are building a construction vtable.
8320 The value returned is a TREE_LIST suitable for wrapping in a
8321 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8322 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8323 number of non-function entries in the vtable.
8325 It might seem that this function should never be called with a
8326 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8327 base is always subsumed by a derived class vtable. However, when
8328 we are building construction vtables, we do build vtables for
8329 primary bases; we need these while the primary base is being
8330 constructed. */
8332 static void
8333 build_vtbl_initializer (tree binfo,
8334 tree orig_binfo,
8335 tree t,
8336 tree rtti_binfo,
8337 int* non_fn_entries_p,
8338 VEC(constructor_elt,gc) **inits)
8340 tree v;
8341 vtbl_init_data vid;
8342 unsigned ix, jx;
8343 tree vbinfo;
8344 VEC(tree,gc) *vbases;
8345 constructor_elt *e;
8347 /* Initialize VID. */
8348 memset (&vid, 0, sizeof (vid));
8349 vid.binfo = binfo;
8350 vid.derived = t;
8351 vid.rtti_binfo = rtti_binfo;
8352 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8353 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8354 vid.generate_vcall_entries = true;
8355 /* The first vbase or vcall offset is at index -3 in the vtable. */
8356 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8358 /* Add entries to the vtable for RTTI. */
8359 build_rtti_vtbl_entries (binfo, &vid);
8361 /* Create an array for keeping track of the functions we've
8362 processed. When we see multiple functions with the same
8363 signature, we share the vcall offsets. */
8364 vid.fns = VEC_alloc (tree, gc, 32);
8365 /* Add the vcall and vbase offset entries. */
8366 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8368 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8369 build_vbase_offset_vtbl_entries. */
8370 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8371 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
8372 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8374 /* If the target requires padding between data entries, add that now. */
8375 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8377 int n_entries = VEC_length (constructor_elt, vid.inits);
8379 VEC_safe_grow (constructor_elt, gc, vid.inits,
8380 TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8382 /* Move data entries into their new positions and add padding
8383 after the new positions. Iterate backwards so we don't
8384 overwrite entries that we would need to process later. */
8385 for (ix = n_entries - 1;
8386 VEC_iterate (constructor_elt, vid.inits, ix, e);
8387 ix--)
8389 int j;
8390 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8391 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8393 VEC_replace (constructor_elt, vid.inits, new_position, e);
8395 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8397 constructor_elt *f = VEC_index (constructor_elt, vid.inits,
8398 new_position - j);
8399 f->index = NULL_TREE;
8400 f->value = build1 (NOP_EXPR, vtable_entry_type,
8401 null_pointer_node);
8406 if (non_fn_entries_p)
8407 *non_fn_entries_p = VEC_length (constructor_elt, vid.inits);
8409 /* The initializers for virtual functions were built up in reverse
8410 order. Straighten them out and add them to the running list in one
8411 step. */
8412 jx = VEC_length (constructor_elt, *inits);
8413 VEC_safe_grow (constructor_elt, gc, *inits,
8414 (jx + VEC_length (constructor_elt, vid.inits)));
8416 for (ix = VEC_length (constructor_elt, vid.inits) - 1;
8417 VEC_iterate (constructor_elt, vid.inits, ix, e);
8418 ix--, jx++)
8419 VEC_replace (constructor_elt, *inits, jx, e);
8421 /* Go through all the ordinary virtual functions, building up
8422 initializers. */
8423 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8425 tree delta;
8426 tree vcall_index;
8427 tree fn, fn_original;
8428 tree init = NULL_TREE;
8430 fn = BV_FN (v);
8431 fn_original = fn;
8432 if (DECL_THUNK_P (fn))
8434 if (!DECL_NAME (fn))
8435 finish_thunk (fn);
8436 if (THUNK_ALIAS (fn))
8438 fn = THUNK_ALIAS (fn);
8439 BV_FN (v) = fn;
8441 fn_original = THUNK_TARGET (fn);
8444 /* If the only definition of this function signature along our
8445 primary base chain is from a lost primary, this vtable slot will
8446 never be used, so just zero it out. This is important to avoid
8447 requiring extra thunks which cannot be generated with the function.
8449 We first check this in update_vtable_entry_for_fn, so we handle
8450 restored primary bases properly; we also need to do it here so we
8451 zero out unused slots in ctor vtables, rather than filling them
8452 with erroneous values (though harmless, apart from relocation
8453 costs). */
8454 if (BV_LOST_PRIMARY (v))
8455 init = size_zero_node;
8457 if (! init)
8459 /* Pull the offset for `this', and the function to call, out of
8460 the list. */
8461 delta = BV_DELTA (v);
8462 vcall_index = BV_VCALL_INDEX (v);
8464 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8465 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8467 /* You can't call an abstract virtual function; it's abstract.
8468 So, we replace these functions with __pure_virtual. */
8469 if (DECL_PURE_VIRTUAL_P (fn_original))
8471 fn = abort_fndecl;
8472 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8474 if (abort_fndecl_addr == NULL)
8475 abort_fndecl_addr
8476 = fold_convert (vfunc_ptr_type_node,
8477 build_fold_addr_expr (fn));
8478 init = abort_fndecl_addr;
8481 /* Likewise for deleted virtuals. */
8482 else if (DECL_DELETED_FN (fn_original))
8484 fn = get_identifier ("__cxa_deleted_virtual");
8485 if (!get_global_value_if_present (fn, &fn))
8486 fn = push_library_fn (fn, (build_function_type_list
8487 (void_type_node, NULL_TREE)),
8488 NULL_TREE);
8489 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8490 init = fold_convert (vfunc_ptr_type_node,
8491 build_fold_addr_expr (fn));
8493 else
8495 if (!integer_zerop (delta) || vcall_index)
8497 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8498 if (!DECL_NAME (fn))
8499 finish_thunk (fn);
8501 /* Take the address of the function, considering it to be of an
8502 appropriate generic type. */
8503 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8504 init = fold_convert (vfunc_ptr_type_node,
8505 build_fold_addr_expr (fn));
8509 /* And add it to the chain of initializers. */
8510 if (TARGET_VTABLE_USES_DESCRIPTORS)
8512 int i;
8513 if (init == size_zero_node)
8514 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8515 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8516 else
8517 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8519 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8520 fn, build_int_cst (NULL_TREE, i));
8521 TREE_CONSTANT (fdesc) = 1;
8523 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8526 else
8527 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8531 /* Adds to vid->inits the initializers for the vbase and vcall
8532 offsets in BINFO, which is in the hierarchy dominated by T. */
8534 static void
8535 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8537 tree b;
8539 /* If this is a derived class, we must first create entries
8540 corresponding to the primary base class. */
8541 b = get_primary_binfo (binfo);
8542 if (b)
8543 build_vcall_and_vbase_vtbl_entries (b, vid);
8545 /* Add the vbase entries for this base. */
8546 build_vbase_offset_vtbl_entries (binfo, vid);
8547 /* Add the vcall entries for this base. */
8548 build_vcall_offset_vtbl_entries (binfo, vid);
8551 /* Returns the initializers for the vbase offset entries in the vtable
8552 for BINFO (which is part of the class hierarchy dominated by T), in
8553 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8554 where the next vbase offset will go. */
8556 static void
8557 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8559 tree vbase;
8560 tree t;
8561 tree non_primary_binfo;
8563 /* If there are no virtual baseclasses, then there is nothing to
8564 do. */
8565 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8566 return;
8568 t = vid->derived;
8570 /* We might be a primary base class. Go up the inheritance hierarchy
8571 until we find the most derived class of which we are a primary base:
8572 it is the offset of that which we need to use. */
8573 non_primary_binfo = binfo;
8574 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8576 tree b;
8578 /* If we have reached a virtual base, then it must be a primary
8579 base (possibly multi-level) of vid->binfo, or we wouldn't
8580 have called build_vcall_and_vbase_vtbl_entries for it. But it
8581 might be a lost primary, so just skip down to vid->binfo. */
8582 if (BINFO_VIRTUAL_P (non_primary_binfo))
8584 non_primary_binfo = vid->binfo;
8585 break;
8588 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8589 if (get_primary_binfo (b) != non_primary_binfo)
8590 break;
8591 non_primary_binfo = b;
8594 /* Go through the virtual bases, adding the offsets. */
8595 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8596 vbase;
8597 vbase = TREE_CHAIN (vbase))
8599 tree b;
8600 tree delta;
8602 if (!BINFO_VIRTUAL_P (vbase))
8603 continue;
8605 /* Find the instance of this virtual base in the complete
8606 object. */
8607 b = copied_binfo (vbase, binfo);
8609 /* If we've already got an offset for this virtual base, we
8610 don't need another one. */
8611 if (BINFO_VTABLE_PATH_MARKED (b))
8612 continue;
8613 BINFO_VTABLE_PATH_MARKED (b) = 1;
8615 /* Figure out where we can find this vbase offset. */
8616 delta = size_binop (MULT_EXPR,
8617 vid->index,
8618 convert (ssizetype,
8619 TYPE_SIZE_UNIT (vtable_entry_type)));
8620 if (vid->primary_vtbl_p)
8621 BINFO_VPTR_FIELD (b) = delta;
8623 if (binfo != TYPE_BINFO (t))
8624 /* The vbase offset had better be the same. */
8625 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8627 /* The next vbase will come at a more negative offset. */
8628 vid->index = size_binop (MINUS_EXPR, vid->index,
8629 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8631 /* The initializer is the delta from BINFO to this virtual base.
8632 The vbase offsets go in reverse inheritance-graph order, and
8633 we are walking in inheritance graph order so these end up in
8634 the right order. */
8635 delta = size_diffop_loc (input_location,
8636 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8638 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8639 fold_build1_loc (input_location, NOP_EXPR,
8640 vtable_entry_type, delta));
8644 /* Adds the initializers for the vcall offset entries in the vtable
8645 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8646 to VID->INITS. */
8648 static void
8649 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8651 /* We only need these entries if this base is a virtual base. We
8652 compute the indices -- but do not add to the vtable -- when
8653 building the main vtable for a class. */
8654 if (binfo == TYPE_BINFO (vid->derived)
8655 || (BINFO_VIRTUAL_P (binfo)
8656 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8657 correspond to VID->DERIVED), we are building a primary
8658 construction virtual table. Since this is a primary
8659 virtual table, we do not need the vcall offsets for
8660 BINFO. */
8661 && binfo != vid->rtti_binfo))
8663 /* We need a vcall offset for each of the virtual functions in this
8664 vtable. For example:
8666 class A { virtual void f (); };
8667 class B1 : virtual public A { virtual void f (); };
8668 class B2 : virtual public A { virtual void f (); };
8669 class C: public B1, public B2 { virtual void f (); };
8671 A C object has a primary base of B1, which has a primary base of A. A
8672 C also has a secondary base of B2, which no longer has a primary base
8673 of A. So the B2-in-C construction vtable needs a secondary vtable for
8674 A, which will adjust the A* to a B2* to call f. We have no way of
8675 knowing what (or even whether) this offset will be when we define B2,
8676 so we store this "vcall offset" in the A sub-vtable and look it up in
8677 a "virtual thunk" for B2::f.
8679 We need entries for all the functions in our primary vtable and
8680 in our non-virtual bases' secondary vtables. */
8681 vid->vbase = binfo;
8682 /* If we are just computing the vcall indices -- but do not need
8683 the actual entries -- not that. */
8684 if (!BINFO_VIRTUAL_P (binfo))
8685 vid->generate_vcall_entries = false;
8686 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8687 add_vcall_offset_vtbl_entries_r (binfo, vid);
8691 /* Build vcall offsets, starting with those for BINFO. */
8693 static void
8694 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8696 int i;
8697 tree primary_binfo;
8698 tree base_binfo;
8700 /* Don't walk into virtual bases -- except, of course, for the
8701 virtual base for which we are building vcall offsets. Any
8702 primary virtual base will have already had its offsets generated
8703 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8704 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8705 return;
8707 /* If BINFO has a primary base, process it first. */
8708 primary_binfo = get_primary_binfo (binfo);
8709 if (primary_binfo)
8710 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8712 /* Add BINFO itself to the list. */
8713 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8715 /* Scan the non-primary bases of BINFO. */
8716 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8717 if (base_binfo != primary_binfo)
8718 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8721 /* Called from build_vcall_offset_vtbl_entries_r. */
8723 static void
8724 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8726 /* Make entries for the rest of the virtuals. */
8727 if (abi_version_at_least (2))
8729 tree orig_fn;
8731 /* The ABI requires that the methods be processed in declaration
8732 order. G++ 3.2 used the order in the vtable. */
8733 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8734 orig_fn;
8735 orig_fn = DECL_CHAIN (orig_fn))
8736 if (DECL_VINDEX (orig_fn))
8737 add_vcall_offset (orig_fn, binfo, vid);
8739 else
8741 tree derived_virtuals;
8742 tree base_virtuals;
8743 tree orig_virtuals;
8744 /* If BINFO is a primary base, the most derived class which has
8745 BINFO as a primary base; otherwise, just BINFO. */
8746 tree non_primary_binfo;
8748 /* We might be a primary base class. Go up the inheritance hierarchy
8749 until we find the most derived class of which we are a primary base:
8750 it is the BINFO_VIRTUALS there that we need to consider. */
8751 non_primary_binfo = binfo;
8752 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8754 tree b;
8756 /* If we have reached a virtual base, then it must be vid->vbase,
8757 because we ignore other virtual bases in
8758 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8759 base (possibly multi-level) of vid->binfo, or we wouldn't
8760 have called build_vcall_and_vbase_vtbl_entries for it. But it
8761 might be a lost primary, so just skip down to vid->binfo. */
8762 if (BINFO_VIRTUAL_P (non_primary_binfo))
8764 gcc_assert (non_primary_binfo == vid->vbase);
8765 non_primary_binfo = vid->binfo;
8766 break;
8769 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8770 if (get_primary_binfo (b) != non_primary_binfo)
8771 break;
8772 non_primary_binfo = b;
8775 if (vid->ctor_vtbl_p)
8776 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8777 where rtti_binfo is the most derived type. */
8778 non_primary_binfo
8779 = original_binfo (non_primary_binfo, vid->rtti_binfo);
8781 for (base_virtuals = BINFO_VIRTUALS (binfo),
8782 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8783 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8784 base_virtuals;
8785 base_virtuals = TREE_CHAIN (base_virtuals),
8786 derived_virtuals = TREE_CHAIN (derived_virtuals),
8787 orig_virtuals = TREE_CHAIN (orig_virtuals))
8789 tree orig_fn;
8791 /* Find the declaration that originally caused this function to
8792 be present in BINFO_TYPE (binfo). */
8793 orig_fn = BV_FN (orig_virtuals);
8795 /* When processing BINFO, we only want to generate vcall slots for
8796 function slots introduced in BINFO. So don't try to generate
8797 one if the function isn't even defined in BINFO. */
8798 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
8799 continue;
8801 add_vcall_offset (orig_fn, binfo, vid);
8806 /* Add a vcall offset entry for ORIG_FN to the vtable. */
8808 static void
8809 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8811 size_t i;
8812 tree vcall_offset;
8813 tree derived_entry;
8815 /* If there is already an entry for a function with the same
8816 signature as FN, then we do not need a second vcall offset.
8817 Check the list of functions already present in the derived
8818 class vtable. */
8819 FOR_EACH_VEC_ELT (tree, vid->fns, i, derived_entry)
8821 if (same_signature_p (derived_entry, orig_fn)
8822 /* We only use one vcall offset for virtual destructors,
8823 even though there are two virtual table entries. */
8824 || (DECL_DESTRUCTOR_P (derived_entry)
8825 && DECL_DESTRUCTOR_P (orig_fn)))
8826 return;
8829 /* If we are building these vcall offsets as part of building
8830 the vtable for the most derived class, remember the vcall
8831 offset. */
8832 if (vid->binfo == TYPE_BINFO (vid->derived))
8834 tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
8835 CLASSTYPE_VCALL_INDICES (vid->derived),
8836 NULL);
8837 elt->purpose = orig_fn;
8838 elt->value = vid->index;
8841 /* The next vcall offset will be found at a more negative
8842 offset. */
8843 vid->index = size_binop (MINUS_EXPR, vid->index,
8844 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8846 /* Keep track of this function. */
8847 VEC_safe_push (tree, gc, vid->fns, orig_fn);
8849 if (vid->generate_vcall_entries)
8851 tree base;
8852 tree fn;
8854 /* Find the overriding function. */
8855 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8856 if (fn == error_mark_node)
8857 vcall_offset = build_zero_cst (vtable_entry_type);
8858 else
8860 base = TREE_VALUE (fn);
8862 /* The vbase we're working on is a primary base of
8863 vid->binfo. But it might be a lost primary, so its
8864 BINFO_OFFSET might be wrong, so we just use the
8865 BINFO_OFFSET from vid->binfo. */
8866 vcall_offset = size_diffop_loc (input_location,
8867 BINFO_OFFSET (base),
8868 BINFO_OFFSET (vid->binfo));
8869 vcall_offset = fold_build1_loc (input_location,
8870 NOP_EXPR, vtable_entry_type,
8871 vcall_offset);
8873 /* Add the initializer to the vtable. */
8874 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
8878 /* Return vtbl initializers for the RTTI entries corresponding to the
8879 BINFO's vtable. The RTTI entries should indicate the object given
8880 by VID->rtti_binfo. */
8882 static void
8883 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8885 tree b;
8886 tree t;
8887 tree offset;
8888 tree decl;
8889 tree init;
8891 t = BINFO_TYPE (vid->rtti_binfo);
8893 /* To find the complete object, we will first convert to our most
8894 primary base, and then add the offset in the vtbl to that value. */
8895 b = binfo;
8896 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8897 && !BINFO_LOST_PRIMARY_P (b))
8899 tree primary_base;
8901 primary_base = get_primary_binfo (b);
8902 gcc_assert (BINFO_PRIMARY_P (primary_base)
8903 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8904 b = primary_base;
8906 offset = size_diffop_loc (input_location,
8907 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8909 /* The second entry is the address of the typeinfo object. */
8910 if (flag_rtti)
8911 decl = build_address (get_tinfo_decl (t));
8912 else
8913 decl = integer_zero_node;
8915 /* Convert the declaration to a type that can be stored in the
8916 vtable. */
8917 init = build_nop (vfunc_ptr_type_node, decl);
8918 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8920 /* Add the offset-to-top entry. It comes earlier in the vtable than
8921 the typeinfo entry. Convert the offset to look like a
8922 function pointer, so that we can put it in the vtable. */
8923 init = build_nop (vfunc_ptr_type_node, offset);
8924 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8927 #include "gt-cp-class.h"