Add C++11 header <cuchar>.
[official-gcc.git] / gcc / cp / class.c
blobf8ba795ddcb6af206344f58acc9e22bcf0c4bc86
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "alias.h"
28 #include "tree.h"
29 #include "options.h"
30 #include "tm.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "attribs.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "convert.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "cgraph.h"
42 #include "dumpfile.h"
43 #include "splay-tree.h"
44 #include "gimplify.h"
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node {
56 /* The name of the class. */
57 tree name;
59 /* The _TYPE node for the class. */
60 tree type;
62 /* The access specifier pending for new declarations in the scope of
63 this class. */
64 tree access;
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used;
69 /* Nonzero if this class is no longer open, because of a call to
70 push_to_top_level. */
71 size_t hidden;
72 }* class_stack_node_t;
74 struct vtbl_init_data
76 /* The base for which we're building initializers. */
77 tree binfo;
78 /* The type of the most-derived type. */
79 tree derived;
80 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
81 unless ctor_vtbl_p is true. */
82 tree rtti_binfo;
83 /* The negative-index vtable initializers built up so far. These
84 are in order from least negative index to most negative index. */
85 vec<constructor_elt, va_gc> *inits;
86 /* The binfo for the virtual base for which we're building
87 vcall offset initializers. */
88 tree vbase;
89 /* The functions in vbase for which we have already provided vcall
90 offsets. */
91 vec<tree, va_gc> *fns;
92 /* The vtable index of the next vcall or vbase offset. */
93 tree index;
94 /* Nonzero if we are building the initializer for the primary
95 vtable. */
96 int primary_vtbl_p;
97 /* Nonzero if we are building the initializer for a construction
98 vtable. */
99 int ctor_vtbl_p;
100 /* True when adding vcall offset entries to the vtable. False when
101 merely computing the indices. */
102 bool generate_vcall_entries;
105 /* The type of a function passed to walk_subobject_offsets. */
106 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
108 /* The stack itself. This is a dynamically resized array. The
109 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
110 static int current_class_stack_size;
111 static class_stack_node_t current_class_stack;
113 /* The size of the largest empty class seen in this translation unit. */
114 static GTY (()) tree sizeof_biggest_empty_class;
116 /* An array of all local classes present in this translation unit, in
117 declaration order. */
118 vec<tree, va_gc> *local_classes;
120 static tree get_vfield_name (tree);
121 static void finish_struct_anon (tree);
122 static tree get_vtable_name (tree);
123 static void get_basefndecls (tree, tree, vec<tree> *);
124 static int build_primary_vtable (tree, tree);
125 static int build_secondary_vtable (tree);
126 static void finish_vtbls (tree);
127 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
128 static void finish_struct_bits (tree);
129 static int alter_access (tree, tree, tree);
130 static void handle_using_decl (tree, tree);
131 static tree dfs_modify_vtables (tree, void *);
132 static tree modify_all_vtables (tree, tree);
133 static void determine_primary_bases (tree);
134 static void finish_struct_methods (tree);
135 static void maybe_warn_about_overly_private_class (tree);
136 static int method_name_cmp (const void *, const void *);
137 static int resort_method_name_cmp (const void *, const void *);
138 static void add_implicitly_declared_members (tree, tree*, int, int);
139 static tree fixed_type_or_null (tree, int *, int *);
140 static tree build_simple_base_path (tree expr, tree binfo);
141 static tree build_vtbl_ref_1 (tree, tree);
142 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
143 vec<constructor_elt, va_gc> **);
144 static int count_fields (tree);
145 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
146 static void insert_into_classtype_sorted_fields (tree, tree, int);
147 static bool check_bitfield_decl (tree);
148 static void check_field_decl (tree, tree, int *, int *, int *);
149 static void check_field_decls (tree, tree *, int *, int *);
150 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
151 static void build_base_fields (record_layout_info, splay_tree, tree *);
152 static void check_methods (tree);
153 static void remove_zero_width_bit_fields (tree);
154 static bool accessible_nvdtor_p (tree);
155 static void check_bases (tree, int *, int *);
156 static void check_bases_and_members (tree);
157 static tree create_vtable_ptr (tree, tree *);
158 static void include_empty_classes (record_layout_info);
159 static void layout_class_type (tree, tree *);
160 static void propagate_binfo_offsets (tree, tree);
161 static void layout_virtual_bases (record_layout_info, splay_tree);
162 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
164 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
165 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
166 static void add_vcall_offset (tree, tree, vtbl_init_data *);
167 static void layout_vtable_decl (tree, int);
168 static tree dfs_find_final_overrider_pre (tree, void *);
169 static tree dfs_find_final_overrider_post (tree, void *);
170 static tree find_final_overrider (tree, tree, tree);
171 static int make_new_vtable (tree, tree);
172 static tree get_primary_binfo (tree);
173 static int maybe_indent_hierarchy (FILE *, int, int);
174 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
175 static void dump_class_hierarchy (tree);
176 static void dump_class_hierarchy_1 (FILE *, int, tree);
177 static void dump_array (FILE *, tree);
178 static void dump_vtable (tree, tree, tree);
179 static void dump_vtt (tree, tree);
180 static void dump_thunk (FILE *, int, tree);
181 static tree build_vtable (tree, tree, tree);
182 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
183 static void layout_nonempty_base_or_field (record_layout_info,
184 tree, tree, splay_tree);
185 static tree end_of_class (tree, int);
186 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
187 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
188 vec<constructor_elt, va_gc> **);
189 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
190 vec<constructor_elt, va_gc> **);
191 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
192 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
193 static void clone_constructors_and_destructors (tree);
194 static tree build_clone (tree, tree);
195 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
196 static void build_ctor_vtbl_group (tree, tree);
197 static void build_vtt (tree);
198 static tree binfo_ctor_vtable (tree);
199 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
200 tree *);
201 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
202 static tree dfs_fixup_binfo_vtbls (tree, void *);
203 static int record_subobject_offset (tree, tree, splay_tree);
204 static int check_subobject_offset (tree, tree, splay_tree);
205 static int walk_subobject_offsets (tree, subobject_offset_fn,
206 tree, splay_tree, tree, int);
207 static void record_subobject_offsets (tree, tree, splay_tree, bool);
208 static int layout_conflict_p (tree, tree, splay_tree, int);
209 static int splay_tree_compare_integer_csts (splay_tree_key k1,
210 splay_tree_key k2);
211 static void warn_about_ambiguous_bases (tree);
212 static bool type_requires_array_cookie (tree);
213 static bool base_derived_from (tree, tree);
214 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
215 static tree end_of_base (tree);
216 static tree get_vcall_index (tree, tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Convert to or from a base subobject. EXPR is an expression of type
229 `A' or `A*', an expression of type `B' or `B*' is returned. To
230 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
231 the B base instance within A. To convert base A to derived B, CODE
232 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
233 In this latter case, A must not be a morally virtual base of B.
234 NONNULL is true if EXPR is known to be non-NULL (this is only
235 needed when EXPR is of pointer type). CV qualifiers are preserved
236 from EXPR. */
238 tree
239 build_base_path (enum tree_code code,
240 tree expr,
241 tree binfo,
242 int nonnull,
243 tsubst_flags_t complain)
245 tree v_binfo = NULL_TREE;
246 tree d_binfo = NULL_TREE;
247 tree probe;
248 tree offset;
249 tree target_type;
250 tree null_test = NULL;
251 tree ptr_target_type;
252 int fixed_type_p;
253 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
254 bool has_empty = false;
255 bool virtual_access;
256 bool rvalue = false;
258 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
259 return error_mark_node;
261 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
263 d_binfo = probe;
264 if (is_empty_class (BINFO_TYPE (probe)))
265 has_empty = true;
266 if (!v_binfo && BINFO_VIRTUAL_P (probe))
267 v_binfo = probe;
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
271 if (want_pointer)
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 if (code == PLUS_EXPR
275 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
277 /* This can happen when adjust_result_of_qualified_name_lookup can't
278 find a unique base binfo in a call to a member function. We
279 couldn't give the diagnostic then since we might have been calling
280 a static member function, so we do it now. */
281 if (complain & tf_error)
283 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
284 ba_unique, NULL, complain);
285 gcc_assert (base == error_mark_node);
287 return error_mark_node;
290 gcc_assert ((code == MINUS_EXPR
291 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
292 || code == PLUS_EXPR);
294 if (binfo == d_binfo)
295 /* Nothing to do. */
296 return expr;
298 if (code == MINUS_EXPR && v_binfo)
300 if (complain & tf_error)
302 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
304 if (want_pointer)
305 error ("cannot convert from pointer to base class %qT to "
306 "pointer to derived class %qT because the base is "
307 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
308 else
309 error ("cannot convert from base class %qT to derived "
310 "class %qT because the base is virtual",
311 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
313 else
315 if (want_pointer)
316 error ("cannot convert from pointer to base class %qT to "
317 "pointer to derived class %qT via virtual base %qT",
318 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
319 BINFO_TYPE (v_binfo));
320 else
321 error ("cannot convert from base class %qT to derived "
322 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
323 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
326 return error_mark_node;
329 if (!want_pointer)
331 rvalue = !real_lvalue_p (expr);
332 /* This must happen before the call to save_expr. */
333 expr = cp_build_addr_expr (expr, complain);
335 else
336 expr = mark_rvalue_use (expr);
338 offset = BINFO_OFFSET (binfo);
339 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
340 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
341 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
342 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
343 expression returned matches the input. */
344 target_type = cp_build_qualified_type
345 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
346 ptr_target_type = build_pointer_type (target_type);
348 /* Do we need to look in the vtable for the real offset? */
349 virtual_access = (v_binfo && fixed_type_p <= 0);
351 /* Don't bother with the calculations inside sizeof; they'll ICE if the
352 source type is incomplete and the pointer value doesn't matter. In a
353 template (even in instantiate_non_dependent_expr), we don't have vtables
354 set up properly yet, and the value doesn't matter there either; we're
355 just interested in the result of overload resolution. */
356 if (cp_unevaluated_operand != 0
357 || in_template_function ())
359 expr = build_nop (ptr_target_type, expr);
360 goto indout;
363 /* If we're in an NSDMI, we don't have the full constructor context yet
364 that we need for converting to a virtual base, so just build a stub
365 CONVERT_EXPR and expand it later in bot_replace. */
366 if (virtual_access && fixed_type_p < 0
367 && current_scope () != current_function_decl)
369 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
370 CONVERT_EXPR_VBASE_PATH (expr) = true;
371 goto indout;
374 /* Do we need to check for a null pointer? */
375 if (want_pointer && !nonnull)
377 /* If we know the conversion will not actually change the value
378 of EXPR, then we can avoid testing the expression for NULL.
379 We have to avoid generating a COMPONENT_REF for a base class
380 field, because other parts of the compiler know that such
381 expressions are always non-NULL. */
382 if (!virtual_access && integer_zerop (offset))
383 return build_nop (ptr_target_type, expr);
384 null_test = error_mark_node;
387 /* Protect against multiple evaluation if necessary. */
388 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
389 expr = save_expr (expr);
391 /* Now that we've saved expr, build the real null test. */
392 if (null_test)
394 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
395 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
396 expr, zero);
399 /* If this is a simple base reference, express it as a COMPONENT_REF. */
400 if (code == PLUS_EXPR && !virtual_access
401 /* We don't build base fields for empty bases, and they aren't very
402 interesting to the optimizers anyway. */
403 && !has_empty)
405 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
406 expr = build_simple_base_path (expr, binfo);
407 if (rvalue)
408 expr = move (expr);
409 if (want_pointer)
410 expr = build_address (expr);
411 target_type = TREE_TYPE (expr);
412 goto out;
415 if (virtual_access)
417 /* Going via virtual base V_BINFO. We need the static offset
418 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
419 V_BINFO. That offset is an entry in D_BINFO's vtable. */
420 tree v_offset;
422 if (fixed_type_p < 0 && in_base_initializer)
424 /* In a base member initializer, we cannot rely on the
425 vtable being set up. We have to indirect via the
426 vtt_parm. */
427 tree t;
429 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
430 t = build_pointer_type (t);
431 v_offset = convert (t, current_vtt_parm);
432 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
434 else
436 tree t = expr;
437 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
439 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
440 probe, expr);
441 if (t == NULL_TREE)
442 t = expr;
444 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
445 complain),
446 TREE_TYPE (TREE_TYPE (expr)));
449 if (v_offset == error_mark_node)
450 return error_mark_node;
452 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
453 v_offset = build1 (NOP_EXPR,
454 build_pointer_type (ptrdiff_type_node),
455 v_offset);
456 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
457 TREE_CONSTANT (v_offset) = 1;
459 offset = convert_to_integer (ptrdiff_type_node,
460 size_diffop_loc (input_location, offset,
461 BINFO_OFFSET (v_binfo)));
463 if (!integer_zerop (offset))
464 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
466 if (fixed_type_p < 0)
467 /* Negative fixed_type_p means this is a constructor or destructor;
468 virtual base layout is fixed in in-charge [cd]tors, but not in
469 base [cd]tors. */
470 offset = build3 (COND_EXPR, ptrdiff_type_node,
471 build2 (EQ_EXPR, boolean_type_node,
472 current_in_charge_parm, integer_zero_node),
473 v_offset,
474 convert_to_integer (ptrdiff_type_node,
475 BINFO_OFFSET (binfo)));
476 else
477 offset = v_offset;
480 if (want_pointer)
481 target_type = ptr_target_type;
483 expr = build1 (NOP_EXPR, ptr_target_type, expr);
485 if (!integer_zerop (offset))
487 offset = fold_convert (sizetype, offset);
488 if (code == MINUS_EXPR)
489 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
490 expr = fold_build_pointer_plus (expr, offset);
492 else
493 null_test = NULL;
495 indout:
496 if (!want_pointer)
498 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
499 if (rvalue)
500 expr = move (expr);
503 out:
504 if (null_test)
505 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
506 build_zero_cst (target_type));
508 return expr;
511 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
512 Perform a derived-to-base conversion by recursively building up a
513 sequence of COMPONENT_REFs to the appropriate base fields. */
515 static tree
516 build_simple_base_path (tree expr, tree binfo)
518 tree type = BINFO_TYPE (binfo);
519 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
520 tree field;
522 if (d_binfo == NULL_TREE)
524 tree temp;
526 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
528 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
529 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
530 an lvalue in the front end; only _DECLs and _REFs are lvalues
531 in the back end. */
532 temp = unary_complex_lvalue (ADDR_EXPR, expr);
533 if (temp)
534 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
536 return expr;
539 /* Recurse. */
540 expr = build_simple_base_path (expr, d_binfo);
542 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
543 field; field = DECL_CHAIN (field))
544 /* Is this the base field created by build_base_field? */
545 if (TREE_CODE (field) == FIELD_DECL
546 && DECL_FIELD_IS_BASE (field)
547 && TREE_TYPE (field) == type
548 /* If we're looking for a field in the most-derived class,
549 also check the field offset; we can have two base fields
550 of the same type if one is an indirect virtual base and one
551 is a direct non-virtual base. */
552 && (BINFO_INHERITANCE_CHAIN (d_binfo)
553 || tree_int_cst_equal (byte_position (field),
554 BINFO_OFFSET (binfo))))
556 /* We don't use build_class_member_access_expr here, as that
557 has unnecessary checks, and more importantly results in
558 recursive calls to dfs_walk_once. */
559 int type_quals = cp_type_quals (TREE_TYPE (expr));
561 expr = build3 (COMPONENT_REF,
562 cp_build_qualified_type (type, type_quals),
563 expr, field, NULL_TREE);
564 expr = fold_if_not_in_template (expr);
566 /* Mark the expression const or volatile, as appropriate.
567 Even though we've dealt with the type above, we still have
568 to mark the expression itself. */
569 if (type_quals & TYPE_QUAL_CONST)
570 TREE_READONLY (expr) = 1;
571 if (type_quals & TYPE_QUAL_VOLATILE)
572 TREE_THIS_VOLATILE (expr) = 1;
574 return expr;
577 /* Didn't find the base field?!? */
578 gcc_unreachable ();
581 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
582 type is a class type or a pointer to a class type. In the former
583 case, TYPE is also a class type; in the latter it is another
584 pointer type. If CHECK_ACCESS is true, an error message is emitted
585 if TYPE is inaccessible. If OBJECT has pointer type, the value is
586 assumed to be non-NULL. */
588 tree
589 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
590 tsubst_flags_t complain)
592 tree binfo;
593 tree object_type;
595 if (TYPE_PTR_P (TREE_TYPE (object)))
597 object_type = TREE_TYPE (TREE_TYPE (object));
598 type = TREE_TYPE (type);
600 else
601 object_type = TREE_TYPE (object);
603 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
604 NULL, complain);
605 if (!binfo || binfo == error_mark_node)
606 return error_mark_node;
608 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
611 /* EXPR is an expression with unqualified class type. BASE is a base
612 binfo of that class type. Returns EXPR, converted to the BASE
613 type. This function assumes that EXPR is the most derived class;
614 therefore virtual bases can be found at their static offsets. */
616 tree
617 convert_to_base_statically (tree expr, tree base)
619 tree expr_type;
621 expr_type = TREE_TYPE (expr);
622 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
624 /* If this is a non-empty base, use a COMPONENT_REF. */
625 if (!is_empty_class (BINFO_TYPE (base)))
626 return build_simple_base_path (expr, base);
628 /* We use fold_build2 and fold_convert below to simplify the trees
629 provided to the optimizers. It is not safe to call these functions
630 when processing a template because they do not handle C++-specific
631 trees. */
632 gcc_assert (!processing_template_decl);
633 expr = cp_build_addr_expr (expr, tf_warning_or_error);
634 if (!integer_zerop (BINFO_OFFSET (base)))
635 expr = fold_build_pointer_plus_loc (input_location,
636 expr, BINFO_OFFSET (base));
637 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
638 expr = build_fold_indirect_ref_loc (input_location, expr);
641 return expr;
645 tree
646 build_vfield_ref (tree datum, tree type)
648 tree vfield, vcontext;
650 if (datum == error_mark_node
651 /* Can happen in case of duplicate base types (c++/59082). */
652 || !TYPE_VFIELD (type))
653 return error_mark_node;
655 /* First, convert to the requested type. */
656 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
657 datum = convert_to_base (datum, type, /*check_access=*/false,
658 /*nonnull=*/true, tf_warning_or_error);
660 /* Second, the requested type may not be the owner of its own vptr.
661 If not, convert to the base class that owns it. We cannot use
662 convert_to_base here, because VCONTEXT may appear more than once
663 in the inheritance hierarchy of TYPE, and thus direct conversion
664 between the types may be ambiguous. Following the path back up
665 one step at a time via primary bases avoids the problem. */
666 vfield = TYPE_VFIELD (type);
667 vcontext = DECL_CONTEXT (vfield);
668 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
670 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
671 type = TREE_TYPE (datum);
674 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
677 /* Given an object INSTANCE, return an expression which yields the
678 vtable element corresponding to INDEX. There are many special
679 cases for INSTANCE which we take care of here, mainly to avoid
680 creating extra tree nodes when we don't have to. */
682 static tree
683 build_vtbl_ref_1 (tree instance, tree idx)
685 tree aref;
686 tree vtbl = NULL_TREE;
688 /* Try to figure out what a reference refers to, and
689 access its virtual function table directly. */
691 int cdtorp = 0;
692 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
694 tree basetype = non_reference (TREE_TYPE (instance));
696 if (fixed_type && !cdtorp)
698 tree binfo = lookup_base (fixed_type, basetype,
699 ba_unique, NULL, tf_none);
700 if (binfo && binfo != error_mark_node)
701 vtbl = unshare_expr (BINFO_VTABLE (binfo));
704 if (!vtbl)
705 vtbl = build_vfield_ref (instance, basetype);
707 aref = build_array_ref (input_location, vtbl, idx);
708 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
710 return aref;
713 tree
714 build_vtbl_ref (tree instance, tree idx)
716 tree aref = build_vtbl_ref_1 (instance, idx);
718 return aref;
721 /* Given a stable object pointer INSTANCE_PTR, return an expression which
722 yields a function pointer corresponding to vtable element INDEX. */
724 tree
725 build_vfn_ref (tree instance_ptr, tree idx)
727 tree aref;
729 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
730 tf_warning_or_error),
731 idx);
733 /* When using function descriptors, the address of the
734 vtable entry is treated as a function pointer. */
735 if (TARGET_VTABLE_USES_DESCRIPTORS)
736 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
737 cp_build_addr_expr (aref, tf_warning_or_error));
739 /* Remember this as a method reference, for later devirtualization. */
740 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
742 return aref;
745 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
746 for the given TYPE. */
748 static tree
749 get_vtable_name (tree type)
751 return mangle_vtbl_for_type (type);
754 /* DECL is an entity associated with TYPE, like a virtual table or an
755 implicitly generated constructor. Determine whether or not DECL
756 should have external or internal linkage at the object file
757 level. This routine does not deal with COMDAT linkage and other
758 similar complexities; it simply sets TREE_PUBLIC if it possible for
759 entities in other translation units to contain copies of DECL, in
760 the abstract. */
762 void
763 set_linkage_according_to_type (tree /*type*/, tree decl)
765 TREE_PUBLIC (decl) = 1;
766 determine_visibility (decl);
769 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
770 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
771 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
773 static tree
774 build_vtable (tree class_type, tree name, tree vtable_type)
776 tree decl;
778 decl = build_lang_decl (VAR_DECL, name, vtable_type);
779 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
780 now to avoid confusion in mangle_decl. */
781 SET_DECL_ASSEMBLER_NAME (decl, name);
782 DECL_CONTEXT (decl) = class_type;
783 DECL_ARTIFICIAL (decl) = 1;
784 TREE_STATIC (decl) = 1;
785 TREE_READONLY (decl) = 1;
786 DECL_VIRTUAL_P (decl) = 1;
787 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
788 DECL_USER_ALIGN (decl) = true;
789 DECL_VTABLE_OR_VTT_P (decl) = 1;
790 set_linkage_according_to_type (class_type, decl);
791 /* The vtable has not been defined -- yet. */
792 DECL_EXTERNAL (decl) = 1;
793 DECL_NOT_REALLY_EXTERN (decl) = 1;
795 /* Mark the VAR_DECL node representing the vtable itself as a
796 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
797 is rather important that such things be ignored because any
798 effort to actually generate DWARF for them will run into
799 trouble when/if we encounter code like:
801 #pragma interface
802 struct S { virtual void member (); };
804 because the artificial declaration of the vtable itself (as
805 manufactured by the g++ front end) will say that the vtable is
806 a static member of `S' but only *after* the debug output for
807 the definition of `S' has already been output. This causes
808 grief because the DWARF entry for the definition of the vtable
809 will try to refer back to an earlier *declaration* of the
810 vtable as a static member of `S' and there won't be one. We
811 might be able to arrange to have the "vtable static member"
812 attached to the member list for `S' before the debug info for
813 `S' get written (which would solve the problem) but that would
814 require more intrusive changes to the g++ front end. */
815 DECL_IGNORED_P (decl) = 1;
817 return decl;
820 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
821 or even complete. If this does not exist, create it. If COMPLETE is
822 nonzero, then complete the definition of it -- that will render it
823 impossible to actually build the vtable, but is useful to get at those
824 which are known to exist in the runtime. */
826 tree
827 get_vtable_decl (tree type, int complete)
829 tree decl;
831 if (CLASSTYPE_VTABLES (type))
832 return CLASSTYPE_VTABLES (type);
834 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
835 CLASSTYPE_VTABLES (type) = decl;
837 if (complete)
839 DECL_EXTERNAL (decl) = 1;
840 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
843 return decl;
846 /* Build the primary virtual function table for TYPE. If BINFO is
847 non-NULL, build the vtable starting with the initial approximation
848 that it is the same as the one which is the head of the association
849 list. Returns a nonzero value if a new vtable is actually
850 created. */
852 static int
853 build_primary_vtable (tree binfo, tree type)
855 tree decl;
856 tree virtuals;
858 decl = get_vtable_decl (type, /*complete=*/0);
860 if (binfo)
862 if (BINFO_NEW_VTABLE_MARKED (binfo))
863 /* We have already created a vtable for this base, so there's
864 no need to do it again. */
865 return 0;
867 virtuals = copy_list (BINFO_VIRTUALS (binfo));
868 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
869 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
870 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
872 else
874 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
875 virtuals = NULL_TREE;
878 if (GATHER_STATISTICS)
880 n_vtables += 1;
881 n_vtable_elems += list_length (virtuals);
884 /* Initialize the association list for this type, based
885 on our first approximation. */
886 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
887 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
888 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
889 return 1;
892 /* Give BINFO a new virtual function table which is initialized
893 with a skeleton-copy of its original initialization. The only
894 entry that changes is the `delta' entry, so we can really
895 share a lot of structure.
897 FOR_TYPE is the most derived type which caused this table to
898 be needed.
900 Returns nonzero if we haven't met BINFO before.
902 The order in which vtables are built (by calling this function) for
903 an object must remain the same, otherwise a binary incompatibility
904 can result. */
906 static int
907 build_secondary_vtable (tree binfo)
909 if (BINFO_NEW_VTABLE_MARKED (binfo))
910 /* We already created a vtable for this base. There's no need to
911 do it again. */
912 return 0;
914 /* Remember that we've created a vtable for this BINFO, so that we
915 don't try to do so again. */
916 SET_BINFO_NEW_VTABLE_MARKED (binfo);
918 /* Make fresh virtual list, so we can smash it later. */
919 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
921 /* Secondary vtables are laid out as part of the same structure as
922 the primary vtable. */
923 BINFO_VTABLE (binfo) = NULL_TREE;
924 return 1;
927 /* Create a new vtable for BINFO which is the hierarchy dominated by
928 T. Return nonzero if we actually created a new vtable. */
930 static int
931 make_new_vtable (tree t, tree binfo)
933 if (binfo == TYPE_BINFO (t))
934 /* In this case, it is *type*'s vtable we are modifying. We start
935 with the approximation that its vtable is that of the
936 immediate base class. */
937 return build_primary_vtable (binfo, t);
938 else
939 /* This is our very own copy of `basetype' to play with. Later,
940 we will fill in all the virtual functions that override the
941 virtual functions in these base classes which are not defined
942 by the current type. */
943 return build_secondary_vtable (binfo);
946 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
947 (which is in the hierarchy dominated by T) list FNDECL as its
948 BV_FN. DELTA is the required constant adjustment from the `this'
949 pointer where the vtable entry appears to the `this' required when
950 the function is actually called. */
952 static void
953 modify_vtable_entry (tree t,
954 tree binfo,
955 tree fndecl,
956 tree delta,
957 tree *virtuals)
959 tree v;
961 v = *virtuals;
963 if (fndecl != BV_FN (v)
964 || !tree_int_cst_equal (delta, BV_DELTA (v)))
966 /* We need a new vtable for BINFO. */
967 if (make_new_vtable (t, binfo))
969 /* If we really did make a new vtable, we also made a copy
970 of the BINFO_VIRTUALS list. Now, we have to find the
971 corresponding entry in that list. */
972 *virtuals = BINFO_VIRTUALS (binfo);
973 while (BV_FN (*virtuals) != BV_FN (v))
974 *virtuals = TREE_CHAIN (*virtuals);
975 v = *virtuals;
978 BV_DELTA (v) = delta;
979 BV_VCALL_INDEX (v) = NULL_TREE;
980 BV_FN (v) = fndecl;
985 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
986 the USING_DECL naming METHOD. Returns true if the method could be
987 added to the method vec. */
989 bool
990 add_method (tree type, tree method, tree using_decl)
992 unsigned slot;
993 tree overload;
994 bool template_conv_p = false;
995 bool conv_p;
996 vec<tree, va_gc> *method_vec;
997 bool complete_p;
998 bool insert_p = false;
999 tree current_fns;
1000 tree fns;
1002 if (method == error_mark_node)
1003 return false;
1005 complete_p = COMPLETE_TYPE_P (type);
1006 conv_p = DECL_CONV_FN_P (method);
1007 if (conv_p)
1008 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1009 && DECL_TEMPLATE_CONV_FN_P (method));
1011 method_vec = CLASSTYPE_METHOD_VEC (type);
1012 if (!method_vec)
1014 /* Make a new method vector. We start with 8 entries. We must
1015 allocate at least two (for constructors and destructors), and
1016 we're going to end up with an assignment operator at some
1017 point as well. */
1018 vec_alloc (method_vec, 8);
1019 /* Create slots for constructors and destructors. */
1020 method_vec->quick_push (NULL_TREE);
1021 method_vec->quick_push (NULL_TREE);
1022 CLASSTYPE_METHOD_VEC (type) = method_vec;
1025 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1026 grok_special_member_properties (method);
1028 /* Constructors and destructors go in special slots. */
1029 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1030 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1031 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1033 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1035 if (TYPE_FOR_JAVA (type))
1037 if (!DECL_ARTIFICIAL (method))
1038 error ("Java class %qT cannot have a destructor", type);
1039 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1040 error ("Java class %qT cannot have an implicit non-trivial "
1041 "destructor",
1042 type);
1045 else
1047 tree m;
1049 insert_p = true;
1050 /* See if we already have an entry with this name. */
1051 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1052 vec_safe_iterate (method_vec, slot, &m);
1053 ++slot)
1055 m = OVL_CURRENT (m);
1056 if (template_conv_p)
1058 if (TREE_CODE (m) == TEMPLATE_DECL
1059 && DECL_TEMPLATE_CONV_FN_P (m))
1060 insert_p = false;
1061 break;
1063 if (conv_p && !DECL_CONV_FN_P (m))
1064 break;
1065 if (DECL_NAME (m) == DECL_NAME (method))
1067 insert_p = false;
1068 break;
1070 if (complete_p
1071 && !DECL_CONV_FN_P (m)
1072 && DECL_NAME (m) > DECL_NAME (method))
1073 break;
1076 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1078 /* Check to see if we've already got this method. */
1079 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1081 tree fn = OVL_CURRENT (fns);
1082 tree fn_type;
1083 tree method_type;
1084 tree parms1;
1085 tree parms2;
1087 if (TREE_CODE (fn) != TREE_CODE (method))
1088 continue;
1090 /* [over.load] Member function declarations with the
1091 same name and the same parameter types cannot be
1092 overloaded if any of them is a static member
1093 function declaration.
1095 [over.load] Member function declarations with the same name and
1096 the same parameter-type-list as well as member function template
1097 declarations with the same name, the same parameter-type-list, and
1098 the same template parameter lists cannot be overloaded if any of
1099 them, but not all, have a ref-qualifier.
1101 [namespace.udecl] When a using-declaration brings names
1102 from a base class into a derived class scope, member
1103 functions in the derived class override and/or hide member
1104 functions with the same name and parameter types in a base
1105 class (rather than conflicting). */
1106 fn_type = TREE_TYPE (fn);
1107 method_type = TREE_TYPE (method);
1108 parms1 = TYPE_ARG_TYPES (fn_type);
1109 parms2 = TYPE_ARG_TYPES (method_type);
1111 /* Compare the quals on the 'this' parm. Don't compare
1112 the whole types, as used functions are treated as
1113 coming from the using class in overload resolution. */
1114 if (! DECL_STATIC_FUNCTION_P (fn)
1115 && ! DECL_STATIC_FUNCTION_P (method)
1116 /* Either both or neither need to be ref-qualified for
1117 differing quals to allow overloading. */
1118 && (FUNCTION_REF_QUALIFIED (fn_type)
1119 == FUNCTION_REF_QUALIFIED (method_type))
1120 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1121 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1122 continue;
1124 /* For templates, the return type and template parameters
1125 must be identical. */
1126 if (TREE_CODE (fn) == TEMPLATE_DECL
1127 && (!same_type_p (TREE_TYPE (fn_type),
1128 TREE_TYPE (method_type))
1129 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1130 DECL_TEMPLATE_PARMS (method))))
1131 continue;
1133 if (! DECL_STATIC_FUNCTION_P (fn))
1134 parms1 = TREE_CHAIN (parms1);
1135 if (! DECL_STATIC_FUNCTION_P (method))
1136 parms2 = TREE_CHAIN (parms2);
1138 if (compparms (parms1, parms2)
1139 && (!DECL_CONV_FN_P (fn)
1140 || same_type_p (TREE_TYPE (fn_type),
1141 TREE_TYPE (method_type)))
1142 && equivalently_constrained (fn, method))
1144 /* For function versions, their parms and types match
1145 but they are not duplicates. Record function versions
1146 as and when they are found. extern "C" functions are
1147 not treated as versions. */
1148 if (TREE_CODE (fn) == FUNCTION_DECL
1149 && TREE_CODE (method) == FUNCTION_DECL
1150 && !DECL_EXTERN_C_P (fn)
1151 && !DECL_EXTERN_C_P (method)
1152 && targetm.target_option.function_versions (fn, method))
1154 /* Mark functions as versions if necessary. Modify the mangled
1155 decl name if necessary. */
1156 if (!DECL_FUNCTION_VERSIONED (fn))
1158 DECL_FUNCTION_VERSIONED (fn) = 1;
1159 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1160 mangle_decl (fn);
1162 if (!DECL_FUNCTION_VERSIONED (method))
1164 DECL_FUNCTION_VERSIONED (method) = 1;
1165 if (DECL_ASSEMBLER_NAME_SET_P (method))
1166 mangle_decl (method);
1168 cgraph_node::record_function_versions (fn, method);
1169 continue;
1171 if (DECL_INHERITED_CTOR_BASE (method))
1173 if (DECL_INHERITED_CTOR_BASE (fn))
1175 error_at (DECL_SOURCE_LOCATION (method),
1176 "%q#D inherited from %qT", method,
1177 DECL_INHERITED_CTOR_BASE (method));
1178 error_at (DECL_SOURCE_LOCATION (fn),
1179 "conflicts with version inherited from %qT",
1180 DECL_INHERITED_CTOR_BASE (fn));
1182 /* Otherwise defer to the other function. */
1183 return false;
1185 if (using_decl)
1187 if (DECL_CONTEXT (fn) == type)
1188 /* Defer to the local function. */
1189 return false;
1191 else
1193 error ("%q+#D cannot be overloaded", method);
1194 error ("with %q+#D", fn);
1197 /* We don't call duplicate_decls here to merge the
1198 declarations because that will confuse things if the
1199 methods have inline definitions. In particular, we
1200 will crash while processing the definitions. */
1201 return false;
1205 /* A class should never have more than one destructor. */
1206 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1207 return false;
1209 /* Add the new binding. */
1210 if (using_decl)
1212 overload = ovl_cons (method, current_fns);
1213 OVL_USED (overload) = true;
1215 else
1216 overload = build_overload (method, current_fns);
1218 if (conv_p)
1219 TYPE_HAS_CONVERSION (type) = 1;
1220 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1221 push_class_level_binding (DECL_NAME (method), overload);
1223 if (insert_p)
1225 bool reallocated;
1227 /* We only expect to add few methods in the COMPLETE_P case, so
1228 just make room for one more method in that case. */
1229 if (complete_p)
1230 reallocated = vec_safe_reserve_exact (method_vec, 1);
1231 else
1232 reallocated = vec_safe_reserve (method_vec, 1);
1233 if (reallocated)
1234 CLASSTYPE_METHOD_VEC (type) = method_vec;
1235 if (slot == method_vec->length ())
1236 method_vec->quick_push (overload);
1237 else
1238 method_vec->quick_insert (slot, overload);
1240 else
1241 /* Replace the current slot. */
1242 (*method_vec)[slot] = overload;
1243 return true;
1246 /* Subroutines of finish_struct. */
1248 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1249 legit, otherwise return 0. */
1251 static int
1252 alter_access (tree t, tree fdecl, tree access)
1254 tree elem;
1256 if (!DECL_LANG_SPECIFIC (fdecl))
1257 retrofit_lang_decl (fdecl);
1259 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1261 elem = purpose_member (t, DECL_ACCESS (fdecl));
1262 if (elem)
1264 if (TREE_VALUE (elem) != access)
1266 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1267 error ("conflicting access specifications for method"
1268 " %q+D, ignored", TREE_TYPE (fdecl));
1269 else
1270 error ("conflicting access specifications for field %qE, ignored",
1271 DECL_NAME (fdecl));
1273 else
1275 /* They're changing the access to the same thing they changed
1276 it to before. That's OK. */
1280 else
1282 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1283 tf_warning_or_error);
1284 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1285 return 1;
1287 return 0;
1290 /* Process the USING_DECL, which is a member of T. */
1292 static void
1293 handle_using_decl (tree using_decl, tree t)
1295 tree decl = USING_DECL_DECLS (using_decl);
1296 tree name = DECL_NAME (using_decl);
1297 tree access
1298 = TREE_PRIVATE (using_decl) ? access_private_node
1299 : TREE_PROTECTED (using_decl) ? access_protected_node
1300 : access_public_node;
1301 tree flist = NULL_TREE;
1302 tree old_value;
1304 gcc_assert (!processing_template_decl && decl);
1306 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1307 tf_warning_or_error);
1308 if (old_value)
1310 if (is_overloaded_fn (old_value))
1311 old_value = OVL_CURRENT (old_value);
1313 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1314 /* OK */;
1315 else
1316 old_value = NULL_TREE;
1319 cp_emit_debug_info_for_using (decl, t);
1321 if (is_overloaded_fn (decl))
1322 flist = decl;
1324 if (! old_value)
1326 else if (is_overloaded_fn (old_value))
1328 if (flist)
1329 /* It's OK to use functions from a base when there are functions with
1330 the same name already present in the current class. */;
1331 else
1333 error ("%q+D invalid in %q#T", using_decl, t);
1334 error (" because of local method %q+#D with same name",
1335 OVL_CURRENT (old_value));
1336 return;
1339 else if (!DECL_ARTIFICIAL (old_value))
1341 error ("%q+D invalid in %q#T", using_decl, t);
1342 error (" because of local member %q+#D with same name", old_value);
1343 return;
1346 /* Make type T see field decl FDECL with access ACCESS. */
1347 if (flist)
1348 for (; flist; flist = OVL_NEXT (flist))
1350 add_method (t, OVL_CURRENT (flist), using_decl);
1351 alter_access (t, OVL_CURRENT (flist), access);
1353 else
1354 alter_access (t, decl, access);
1357 /* Data structure for find_abi_tags_r, below. */
1359 struct abi_tag_data
1361 tree t; // The type that we're checking for missing tags.
1362 tree subob; // The subobject of T that we're getting tags from.
1363 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1366 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1367 in the context of P. TAG can be either an identifier (the DECL_NAME of
1368 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1370 static void
1371 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1373 if (!IDENTIFIER_MARKED (id))
1375 if (p->tags != error_mark_node)
1377 /* We're collecting tags from template arguments or from
1378 the type of a variable or function return type. */
1379 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1381 /* Don't inherit this tag multiple times. */
1382 IDENTIFIER_MARKED (id) = true;
1384 if (TYPE_P (p->t))
1386 /* Tags inherited from type template arguments are only used
1387 to avoid warnings. */
1388 ABI_TAG_IMPLICIT (p->tags) = true;
1389 return;
1391 /* For functions and variables we want to warn, too. */
1394 /* Otherwise we're diagnosing missing tags. */
1395 if (TREE_CODE (p->t) == FUNCTION_DECL)
1397 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1398 "that %qT (used in its return type) has",
1399 p->t, tag, *tp))
1400 inform (location_of (*tp), "%qT declared here", *tp);
1402 else if (VAR_P (p->t))
1404 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1405 "that %qT (used in its type) has", p->t, tag, *tp))
1406 inform (location_of (*tp), "%qT declared here", *tp);
1408 else if (TYPE_P (p->subob))
1410 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1411 "that base %qT has", p->t, tag, p->subob))
1412 inform (location_of (p->subob), "%qT declared here",
1413 p->subob);
1415 else
1417 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1418 "that %qT (used in the type of %qD) has",
1419 p->t, tag, *tp, p->subob))
1421 inform (location_of (p->subob), "%qD declared here",
1422 p->subob);
1423 inform (location_of (*tp), "%qT declared here", *tp);
1429 /* Find all the ABI tags in the attribute list ATTR and either call
1430 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1432 static void
1433 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1435 if (!attr)
1436 return;
1437 for (; (attr = lookup_attribute ("abi_tag", attr));
1438 attr = TREE_CHAIN (attr))
1439 for (tree list = TREE_VALUE (attr); list;
1440 list = TREE_CHAIN (list))
1442 tree tag = TREE_VALUE (list);
1443 tree id = get_identifier (TREE_STRING_POINTER (tag));
1444 if (tp)
1445 check_tag (tag, id, tp, p);
1446 else
1447 IDENTIFIER_MARKED (id) = val;
1451 /* Find all the ABI tags on T and its enclosing scopes and either call
1452 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1454 static void
1455 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1457 while (t != global_namespace)
1459 tree attr;
1460 if (TYPE_P (t))
1462 attr = TYPE_ATTRIBUTES (t);
1463 t = CP_TYPE_CONTEXT (t);
1465 else
1467 attr = DECL_ATTRIBUTES (t);
1468 t = CP_DECL_CONTEXT (t);
1470 mark_or_check_attr_tags (attr, tp, p, val);
1474 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1475 types with ABI tags, add the corresponding identifiers to the VEC in
1476 *DATA and set IDENTIFIER_MARKED. */
1478 static tree
1479 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1481 if (!OVERLOAD_TYPE_P (*tp))
1482 return NULL_TREE;
1484 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1485 anyway, but let's make sure of it. */
1486 *walk_subtrees = false;
1488 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1490 mark_or_check_tags (*tp, tp, p, false);
1492 return NULL_TREE;
1495 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1496 IDENTIFIER_MARKED on its ABI tags. */
1498 static tree
1499 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1501 if (!OVERLOAD_TYPE_P (*tp))
1502 return NULL_TREE;
1504 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1505 anyway, but let's make sure of it. */
1506 *walk_subtrees = false;
1508 bool *valp = static_cast<bool*>(data);
1510 mark_or_check_tags (*tp, NULL, NULL, *valp);
1512 return NULL_TREE;
1515 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1516 scopes. */
1518 static void
1519 mark_abi_tags (tree t, bool val)
1521 mark_or_check_tags (t, NULL, NULL, val);
1522 if (DECL_P (t))
1524 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1525 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1527 /* Template arguments are part of the signature. */
1528 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1529 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1531 tree arg = TREE_VEC_ELT (level, j);
1532 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1535 if (TREE_CODE (t) == FUNCTION_DECL)
1536 /* A function's parameter types are part of the signature, so
1537 we don't need to inherit any tags that are also in them. */
1538 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1539 arg = TREE_CHAIN (arg))
1540 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1541 mark_abi_tags_r, &val);
1545 /* Check that T has all the ABI tags that subobject SUBOB has, or
1546 warn if not. If T is a (variable or function) declaration, also
1547 add any missing tags. */
1549 static void
1550 check_abi_tags (tree t, tree subob)
1552 bool inherit = DECL_P (t);
1554 if (!inherit && !warn_abi_tag)
1555 return;
1557 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1558 if (!TREE_PUBLIC (decl))
1559 /* No need to worry about things local to this TU. */
1560 return;
1562 mark_abi_tags (t, true);
1564 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1565 struct abi_tag_data data = { t, subob, error_mark_node };
1566 if (inherit)
1567 data.tags = NULL_TREE;
1569 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1571 if (inherit && data.tags)
1573 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1574 if (attr)
1575 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1576 else
1577 DECL_ATTRIBUTES (t)
1578 = tree_cons (get_identifier ("abi_tag"), data.tags,
1579 DECL_ATTRIBUTES (t));
1582 mark_abi_tags (t, false);
1585 /* Check that DECL has all the ABI tags that are used in parts of its type
1586 that are not reflected in its mangled name. */
1588 void
1589 check_abi_tags (tree decl)
1591 if (VAR_P (decl))
1592 check_abi_tags (decl, TREE_TYPE (decl));
1593 else if (TREE_CODE (decl) == FUNCTION_DECL
1594 && !mangle_return_type_p (decl))
1595 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1598 void
1599 inherit_targ_abi_tags (tree t)
1601 if (!CLASS_TYPE_P (t)
1602 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1603 return;
1605 mark_abi_tags (t, true);
1607 tree args = CLASSTYPE_TI_ARGS (t);
1608 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1609 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1611 tree level = TMPL_ARGS_LEVEL (args, i+1);
1612 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1614 tree arg = TREE_VEC_ELT (level, j);
1615 data.subob = arg;
1616 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1620 // If we found some tags on our template arguments, add them to our
1621 // abi_tag attribute.
1622 if (data.tags)
1624 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1625 if (attr)
1626 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1627 else
1628 TYPE_ATTRIBUTES (t)
1629 = tree_cons (get_identifier ("abi_tag"), data.tags,
1630 TYPE_ATTRIBUTES (t));
1633 mark_abi_tags (t, false);
1636 /* Return true, iff class T has a non-virtual destructor that is
1637 accessible from outside the class heirarchy (i.e. is public, or
1638 there's a suitable friend. */
1640 static bool
1641 accessible_nvdtor_p (tree t)
1643 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1645 /* An implicitly declared destructor is always public. And,
1646 if it were virtual, we would have created it by now. */
1647 if (!dtor)
1648 return true;
1650 if (DECL_VINDEX (dtor))
1651 return false; /* Virtual */
1653 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1654 return true; /* Public */
1656 if (CLASSTYPE_FRIEND_CLASSES (t)
1657 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1658 return true; /* Has friends */
1660 return false;
1663 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1664 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1665 properties of the bases. */
1667 static void
1668 check_bases (tree t,
1669 int* cant_have_const_ctor_p,
1670 int* no_const_asn_ref_p)
1672 int i;
1673 bool seen_non_virtual_nearly_empty_base_p = 0;
1674 int seen_tm_mask = 0;
1675 tree base_binfo;
1676 tree binfo;
1677 tree field = NULL_TREE;
1679 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1680 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1681 if (TREE_CODE (field) == FIELD_DECL)
1682 break;
1684 for (binfo = TYPE_BINFO (t), i = 0;
1685 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1687 tree basetype = TREE_TYPE (base_binfo);
1689 gcc_assert (COMPLETE_TYPE_P (basetype));
1691 if (CLASSTYPE_FINAL (basetype))
1692 error ("cannot derive from %<final%> base %qT in derived type %qT",
1693 basetype, t);
1695 /* If any base class is non-literal, so is the derived class. */
1696 if (!CLASSTYPE_LITERAL_P (basetype))
1697 CLASSTYPE_LITERAL_P (t) = false;
1699 /* If the base class doesn't have copy constructors or
1700 assignment operators that take const references, then the
1701 derived class cannot have such a member automatically
1702 generated. */
1703 if (TYPE_HAS_COPY_CTOR (basetype)
1704 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1705 *cant_have_const_ctor_p = 1;
1706 if (TYPE_HAS_COPY_ASSIGN (basetype)
1707 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1708 *no_const_asn_ref_p = 1;
1710 if (BINFO_VIRTUAL_P (base_binfo))
1711 /* A virtual base does not effect nearly emptiness. */
1713 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1715 if (seen_non_virtual_nearly_empty_base_p)
1716 /* And if there is more than one nearly empty base, then the
1717 derived class is not nearly empty either. */
1718 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1719 else
1720 /* Remember we've seen one. */
1721 seen_non_virtual_nearly_empty_base_p = 1;
1723 else if (!is_empty_class (basetype))
1724 /* If the base class is not empty or nearly empty, then this
1725 class cannot be nearly empty. */
1726 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1728 /* A lot of properties from the bases also apply to the derived
1729 class. */
1730 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1731 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1732 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1733 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1734 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1735 || !TYPE_HAS_COPY_ASSIGN (basetype));
1736 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1737 || !TYPE_HAS_COPY_CTOR (basetype));
1738 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1739 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1740 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1741 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1742 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1743 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1744 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1745 || TYPE_HAS_COMPLEX_DFLT (basetype));
1746 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1747 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1748 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1749 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1750 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1751 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1753 /* A standard-layout class is a class that:
1755 * has no non-standard-layout base classes, */
1756 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1757 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1759 tree basefield;
1760 /* ...has no base classes of the same type as the first non-static
1761 data member... */
1762 if (field && DECL_CONTEXT (field) == t
1763 && (same_type_ignoring_top_level_qualifiers_p
1764 (TREE_TYPE (field), basetype)))
1765 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1766 else
1767 /* ...either has no non-static data members in the most-derived
1768 class and at most one base class with non-static data
1769 members, or has no base classes with non-static data
1770 members */
1771 for (basefield = TYPE_FIELDS (basetype); basefield;
1772 basefield = DECL_CHAIN (basefield))
1773 if (TREE_CODE (basefield) == FIELD_DECL)
1775 if (field)
1776 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1777 else
1778 field = basefield;
1779 break;
1783 /* Don't bother collecting tm attributes if transactional memory
1784 support is not enabled. */
1785 if (flag_tm)
1787 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1788 if (tm_attr)
1789 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1792 check_abi_tags (t, basetype);
1795 /* If one of the base classes had TM attributes, and the current class
1796 doesn't define its own, then the current class inherits one. */
1797 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1799 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1800 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1804 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1805 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1806 that have had a nearly-empty virtual primary base stolen by some
1807 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1808 T. */
1810 static void
1811 determine_primary_bases (tree t)
1813 unsigned i;
1814 tree primary = NULL_TREE;
1815 tree type_binfo = TYPE_BINFO (t);
1816 tree base_binfo;
1818 /* Determine the primary bases of our bases. */
1819 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1820 base_binfo = TREE_CHAIN (base_binfo))
1822 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1824 /* See if we're the non-virtual primary of our inheritance
1825 chain. */
1826 if (!BINFO_VIRTUAL_P (base_binfo))
1828 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1829 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1831 if (parent_primary
1832 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1833 BINFO_TYPE (parent_primary)))
1834 /* We are the primary binfo. */
1835 BINFO_PRIMARY_P (base_binfo) = 1;
1837 /* Determine if we have a virtual primary base, and mark it so.
1839 if (primary && BINFO_VIRTUAL_P (primary))
1841 tree this_primary = copied_binfo (primary, base_binfo);
1843 if (BINFO_PRIMARY_P (this_primary))
1844 /* Someone already claimed this base. */
1845 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1846 else
1848 tree delta;
1850 BINFO_PRIMARY_P (this_primary) = 1;
1851 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1853 /* A virtual binfo might have been copied from within
1854 another hierarchy. As we're about to use it as a
1855 primary base, make sure the offsets match. */
1856 delta = size_diffop_loc (input_location,
1857 convert (ssizetype,
1858 BINFO_OFFSET (base_binfo)),
1859 convert (ssizetype,
1860 BINFO_OFFSET (this_primary)));
1862 propagate_binfo_offsets (this_primary, delta);
1867 /* First look for a dynamic direct non-virtual base. */
1868 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1870 tree basetype = BINFO_TYPE (base_binfo);
1872 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1874 primary = base_binfo;
1875 goto found;
1879 /* A "nearly-empty" virtual base class can be the primary base
1880 class, if no non-virtual polymorphic base can be found. Look for
1881 a nearly-empty virtual dynamic base that is not already a primary
1882 base of something in the hierarchy. If there is no such base,
1883 just pick the first nearly-empty virtual base. */
1885 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1886 base_binfo = TREE_CHAIN (base_binfo))
1887 if (BINFO_VIRTUAL_P (base_binfo)
1888 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1890 if (!BINFO_PRIMARY_P (base_binfo))
1892 /* Found one that is not primary. */
1893 primary = base_binfo;
1894 goto found;
1896 else if (!primary)
1897 /* Remember the first candidate. */
1898 primary = base_binfo;
1901 found:
1902 /* If we've got a primary base, use it. */
1903 if (primary)
1905 tree basetype = BINFO_TYPE (primary);
1907 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1908 if (BINFO_PRIMARY_P (primary))
1909 /* We are stealing a primary base. */
1910 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1911 BINFO_PRIMARY_P (primary) = 1;
1912 if (BINFO_VIRTUAL_P (primary))
1914 tree delta;
1916 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1917 /* A virtual binfo might have been copied from within
1918 another hierarchy. As we're about to use it as a primary
1919 base, make sure the offsets match. */
1920 delta = size_diffop_loc (input_location, ssize_int (0),
1921 convert (ssizetype, BINFO_OFFSET (primary)));
1923 propagate_binfo_offsets (primary, delta);
1926 primary = TYPE_BINFO (basetype);
1928 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1929 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1930 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1934 /* Update the variant types of T. */
1936 void
1937 fixup_type_variants (tree t)
1939 tree variants;
1941 if (!t)
1942 return;
1944 for (variants = TYPE_NEXT_VARIANT (t);
1945 variants;
1946 variants = TYPE_NEXT_VARIANT (variants))
1948 /* These fields are in the _TYPE part of the node, not in
1949 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1950 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1951 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1952 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1953 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1955 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1957 TYPE_BINFO (variants) = TYPE_BINFO (t);
1959 /* Copy whatever these are holding today. */
1960 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1961 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1965 /* Early variant fixups: we apply attributes at the beginning of the class
1966 definition, and we need to fix up any variants that have already been
1967 made via elaborated-type-specifier so that check_qualified_type works. */
1969 void
1970 fixup_attribute_variants (tree t)
1972 tree variants;
1974 if (!t)
1975 return;
1977 tree attrs = TYPE_ATTRIBUTES (t);
1978 unsigned align = TYPE_ALIGN (t);
1979 bool user_align = TYPE_USER_ALIGN (t);
1981 for (variants = TYPE_NEXT_VARIANT (t);
1982 variants;
1983 variants = TYPE_NEXT_VARIANT (variants))
1985 /* These are the two fields that check_qualified_type looks at and
1986 are affected by attributes. */
1987 TYPE_ATTRIBUTES (variants) = attrs;
1988 unsigned valign = align;
1989 if (TYPE_USER_ALIGN (variants))
1990 valign = MAX (valign, TYPE_ALIGN (variants));
1991 else
1992 TYPE_USER_ALIGN (variants) = user_align;
1993 TYPE_ALIGN (variants) = valign;
1997 /* Set memoizing fields and bits of T (and its variants) for later
1998 use. */
2000 static void
2001 finish_struct_bits (tree t)
2003 /* Fix up variants (if any). */
2004 fixup_type_variants (t);
2006 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2007 /* For a class w/o baseclasses, 'finish_struct' has set
2008 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2009 Similarly for a class whose base classes do not have vtables.
2010 When neither of these is true, we might have removed abstract
2011 virtuals (by providing a definition), added some (by declaring
2012 new ones), or redeclared ones from a base class. We need to
2013 recalculate what's really an abstract virtual at this point (by
2014 looking in the vtables). */
2015 get_pure_virtuals (t);
2017 /* If this type has a copy constructor or a destructor, force its
2018 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2019 nonzero. This will cause it to be passed by invisible reference
2020 and prevent it from being returned in a register. */
2021 if (type_has_nontrivial_copy_init (t)
2022 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2024 tree variants;
2025 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2026 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2028 SET_TYPE_MODE (variants, BLKmode);
2029 TREE_ADDRESSABLE (variants) = 1;
2034 /* Issue warnings about T having private constructors, but no friends,
2035 and so forth.
2037 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2038 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2039 non-private static member functions. */
2041 static void
2042 maybe_warn_about_overly_private_class (tree t)
2044 int has_member_fn = 0;
2045 int has_nonprivate_method = 0;
2046 tree fn;
2048 if (!warn_ctor_dtor_privacy
2049 /* If the class has friends, those entities might create and
2050 access instances, so we should not warn. */
2051 || (CLASSTYPE_FRIEND_CLASSES (t)
2052 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2053 /* We will have warned when the template was declared; there's
2054 no need to warn on every instantiation. */
2055 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2056 /* There's no reason to even consider warning about this
2057 class. */
2058 return;
2060 /* We only issue one warning, if more than one applies, because
2061 otherwise, on code like:
2063 class A {
2064 // Oops - forgot `public:'
2065 A();
2066 A(const A&);
2067 ~A();
2070 we warn several times about essentially the same problem. */
2072 /* Check to see if all (non-constructor, non-destructor) member
2073 functions are private. (Since there are no friends or
2074 non-private statics, we can't ever call any of the private member
2075 functions.) */
2076 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2077 /* We're not interested in compiler-generated methods; they don't
2078 provide any way to call private members. */
2079 if (!DECL_ARTIFICIAL (fn))
2081 if (!TREE_PRIVATE (fn))
2083 if (DECL_STATIC_FUNCTION_P (fn))
2084 /* A non-private static member function is just like a
2085 friend; it can create and invoke private member
2086 functions, and be accessed without a class
2087 instance. */
2088 return;
2090 has_nonprivate_method = 1;
2091 /* Keep searching for a static member function. */
2093 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2094 has_member_fn = 1;
2097 if (!has_nonprivate_method && has_member_fn)
2099 /* There are no non-private methods, and there's at least one
2100 private member function that isn't a constructor or
2101 destructor. (If all the private members are
2102 constructors/destructors we want to use the code below that
2103 issues error messages specifically referring to
2104 constructors/destructors.) */
2105 unsigned i;
2106 tree binfo = TYPE_BINFO (t);
2108 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2109 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2111 has_nonprivate_method = 1;
2112 break;
2114 if (!has_nonprivate_method)
2116 warning (OPT_Wctor_dtor_privacy,
2117 "all member functions in class %qT are private", t);
2118 return;
2122 /* Even if some of the member functions are non-private, the class
2123 won't be useful for much if all the constructors or destructors
2124 are private: such an object can never be created or destroyed. */
2125 fn = CLASSTYPE_DESTRUCTORS (t);
2126 if (fn && TREE_PRIVATE (fn))
2128 warning (OPT_Wctor_dtor_privacy,
2129 "%q#T only defines a private destructor and has no friends",
2131 return;
2134 /* Warn about classes that have private constructors and no friends. */
2135 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2136 /* Implicitly generated constructors are always public. */
2137 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2138 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2140 int nonprivate_ctor = 0;
2142 /* If a non-template class does not define a copy
2143 constructor, one is defined for it, enabling it to avoid
2144 this warning. For a template class, this does not
2145 happen, and so we would normally get a warning on:
2147 template <class T> class C { private: C(); };
2149 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2150 complete non-template or fully instantiated classes have this
2151 flag set. */
2152 if (!TYPE_HAS_COPY_CTOR (t))
2153 nonprivate_ctor = 1;
2154 else
2155 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2157 tree ctor = OVL_CURRENT (fn);
2158 /* Ideally, we wouldn't count copy constructors (or, in
2159 fact, any constructor that takes an argument of the
2160 class type as a parameter) because such things cannot
2161 be used to construct an instance of the class unless
2162 you already have one. But, for now at least, we're
2163 more generous. */
2164 if (! TREE_PRIVATE (ctor))
2166 nonprivate_ctor = 1;
2167 break;
2171 if (nonprivate_ctor == 0)
2173 warning (OPT_Wctor_dtor_privacy,
2174 "%q#T only defines private constructors and has no friends",
2176 return;
2181 static struct {
2182 gt_pointer_operator new_value;
2183 void *cookie;
2184 } resort_data;
2186 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2188 static int
2189 method_name_cmp (const void* m1_p, const void* m2_p)
2191 const tree *const m1 = (const tree *) m1_p;
2192 const tree *const m2 = (const tree *) m2_p;
2194 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2195 return 0;
2196 if (*m1 == NULL_TREE)
2197 return -1;
2198 if (*m2 == NULL_TREE)
2199 return 1;
2200 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2201 return -1;
2202 return 1;
2205 /* This routine compares two fields like method_name_cmp but using the
2206 pointer operator in resort_field_decl_data. */
2208 static int
2209 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2211 const tree *const m1 = (const tree *) m1_p;
2212 const tree *const m2 = (const tree *) m2_p;
2213 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2214 return 0;
2215 if (*m1 == NULL_TREE)
2216 return -1;
2217 if (*m2 == NULL_TREE)
2218 return 1;
2220 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2221 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2222 resort_data.new_value (&d1, resort_data.cookie);
2223 resort_data.new_value (&d2, resort_data.cookie);
2224 if (d1 < d2)
2225 return -1;
2227 return 1;
2230 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2232 void
2233 resort_type_method_vec (void* obj,
2234 void* /*orig_obj*/,
2235 gt_pointer_operator new_value,
2236 void* cookie)
2238 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2239 int len = vec_safe_length (method_vec);
2240 size_t slot;
2241 tree fn;
2243 /* The type conversion ops have to live at the front of the vec, so we
2244 can't sort them. */
2245 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2246 vec_safe_iterate (method_vec, slot, &fn);
2247 ++slot)
2248 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2249 break;
2251 if (len - slot > 1)
2253 resort_data.new_value = new_value;
2254 resort_data.cookie = cookie;
2255 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2256 resort_method_name_cmp);
2260 /* Warn about duplicate methods in fn_fields.
2262 Sort methods that are not special (i.e., constructors, destructors,
2263 and type conversion operators) so that we can find them faster in
2264 search. */
2266 static void
2267 finish_struct_methods (tree t)
2269 tree fn_fields;
2270 vec<tree, va_gc> *method_vec;
2271 int slot, len;
2273 method_vec = CLASSTYPE_METHOD_VEC (t);
2274 if (!method_vec)
2275 return;
2277 len = method_vec->length ();
2279 /* Clear DECL_IN_AGGR_P for all functions. */
2280 for (fn_fields = TYPE_METHODS (t); fn_fields;
2281 fn_fields = DECL_CHAIN (fn_fields))
2282 DECL_IN_AGGR_P (fn_fields) = 0;
2284 /* Issue warnings about private constructors and such. If there are
2285 no methods, then some public defaults are generated. */
2286 maybe_warn_about_overly_private_class (t);
2288 /* The type conversion ops have to live at the front of the vec, so we
2289 can't sort them. */
2290 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2291 method_vec->iterate (slot, &fn_fields);
2292 ++slot)
2293 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2294 break;
2295 if (len - slot > 1)
2296 qsort (method_vec->address () + slot,
2297 len-slot, sizeof (tree), method_name_cmp);
2300 /* Make BINFO's vtable have N entries, including RTTI entries,
2301 vbase and vcall offsets, etc. Set its type and call the back end
2302 to lay it out. */
2304 static void
2305 layout_vtable_decl (tree binfo, int n)
2307 tree atype;
2308 tree vtable;
2310 atype = build_array_of_n_type (vtable_entry_type, n);
2311 layout_type (atype);
2313 /* We may have to grow the vtable. */
2314 vtable = get_vtbl_decl_for_binfo (binfo);
2315 if (!same_type_p (TREE_TYPE (vtable), atype))
2317 TREE_TYPE (vtable) = atype;
2318 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2319 layout_decl (vtable, 0);
2323 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2324 have the same signature. */
2327 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2329 /* One destructor overrides another if they are the same kind of
2330 destructor. */
2331 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2332 && special_function_p (base_fndecl) == special_function_p (fndecl))
2333 return 1;
2334 /* But a non-destructor never overrides a destructor, nor vice
2335 versa, nor do different kinds of destructors override
2336 one-another. For example, a complete object destructor does not
2337 override a deleting destructor. */
2338 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2339 return 0;
2341 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2342 || (DECL_CONV_FN_P (fndecl)
2343 && DECL_CONV_FN_P (base_fndecl)
2344 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2345 DECL_CONV_FN_TYPE (base_fndecl))))
2347 tree fntype = TREE_TYPE (fndecl);
2348 tree base_fntype = TREE_TYPE (base_fndecl);
2349 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2350 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2351 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2352 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2353 return 1;
2355 return 0;
2358 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2359 subobject. */
2361 static bool
2362 base_derived_from (tree derived, tree base)
2364 tree probe;
2366 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2368 if (probe == derived)
2369 return true;
2370 else if (BINFO_VIRTUAL_P (probe))
2371 /* If we meet a virtual base, we can't follow the inheritance
2372 any more. See if the complete type of DERIVED contains
2373 such a virtual base. */
2374 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2375 != NULL_TREE);
2377 return false;
2380 struct find_final_overrider_data {
2381 /* The function for which we are trying to find a final overrider. */
2382 tree fn;
2383 /* The base class in which the function was declared. */
2384 tree declaring_base;
2385 /* The candidate overriders. */
2386 tree candidates;
2387 /* Path to most derived. */
2388 vec<tree> path;
2391 /* Add the overrider along the current path to FFOD->CANDIDATES.
2392 Returns true if an overrider was found; false otherwise. */
2394 static bool
2395 dfs_find_final_overrider_1 (tree binfo,
2396 find_final_overrider_data *ffod,
2397 unsigned depth)
2399 tree method;
2401 /* If BINFO is not the most derived type, try a more derived class.
2402 A definition there will overrider a definition here. */
2403 if (depth)
2405 depth--;
2406 if (dfs_find_final_overrider_1
2407 (ffod->path[depth], ffod, depth))
2408 return true;
2411 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2412 if (method)
2414 tree *candidate = &ffod->candidates;
2416 /* Remove any candidates overridden by this new function. */
2417 while (*candidate)
2419 /* If *CANDIDATE overrides METHOD, then METHOD
2420 cannot override anything else on the list. */
2421 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2422 return true;
2423 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2424 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2425 *candidate = TREE_CHAIN (*candidate);
2426 else
2427 candidate = &TREE_CHAIN (*candidate);
2430 /* Add the new function. */
2431 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2432 return true;
2435 return false;
2438 /* Called from find_final_overrider via dfs_walk. */
2440 static tree
2441 dfs_find_final_overrider_pre (tree binfo, void *data)
2443 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2445 if (binfo == ffod->declaring_base)
2446 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2447 ffod->path.safe_push (binfo);
2449 return NULL_TREE;
2452 static tree
2453 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2455 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2456 ffod->path.pop ();
2458 return NULL_TREE;
2461 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2462 FN and whose TREE_VALUE is the binfo for the base where the
2463 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2464 DERIVED) is the base object in which FN is declared. */
2466 static tree
2467 find_final_overrider (tree derived, tree binfo, tree fn)
2469 find_final_overrider_data ffod;
2471 /* Getting this right is a little tricky. This is valid:
2473 struct S { virtual void f (); };
2474 struct T { virtual void f (); };
2475 struct U : public S, public T { };
2477 even though calling `f' in `U' is ambiguous. But,
2479 struct R { virtual void f(); };
2480 struct S : virtual public R { virtual void f (); };
2481 struct T : virtual public R { virtual void f (); };
2482 struct U : public S, public T { };
2484 is not -- there's no way to decide whether to put `S::f' or
2485 `T::f' in the vtable for `R'.
2487 The solution is to look at all paths to BINFO. If we find
2488 different overriders along any two, then there is a problem. */
2489 if (DECL_THUNK_P (fn))
2490 fn = THUNK_TARGET (fn);
2492 /* Determine the depth of the hierarchy. */
2493 ffod.fn = fn;
2494 ffod.declaring_base = binfo;
2495 ffod.candidates = NULL_TREE;
2496 ffod.path.create (30);
2498 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2499 dfs_find_final_overrider_post, &ffod);
2501 ffod.path.release ();
2503 /* If there was no winner, issue an error message. */
2504 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2505 return error_mark_node;
2507 return ffod.candidates;
2510 /* Return the index of the vcall offset for FN when TYPE is used as a
2511 virtual base. */
2513 static tree
2514 get_vcall_index (tree fn, tree type)
2516 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2517 tree_pair_p p;
2518 unsigned ix;
2520 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2521 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2522 || same_signature_p (fn, p->purpose))
2523 return p->value;
2525 /* There should always be an appropriate index. */
2526 gcc_unreachable ();
2529 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2530 dominated by T. FN is the old function; VIRTUALS points to the
2531 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2532 of that entry in the list. */
2534 static void
2535 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2536 unsigned ix)
2538 tree b;
2539 tree overrider;
2540 tree delta;
2541 tree virtual_base;
2542 tree first_defn;
2543 tree overrider_fn, overrider_target;
2544 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2545 tree over_return, base_return;
2546 bool lost = false;
2548 /* Find the nearest primary base (possibly binfo itself) which defines
2549 this function; this is the class the caller will convert to when
2550 calling FN through BINFO. */
2551 for (b = binfo; ; b = get_primary_binfo (b))
2553 gcc_assert (b);
2554 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2555 break;
2557 /* The nearest definition is from a lost primary. */
2558 if (BINFO_LOST_PRIMARY_P (b))
2559 lost = true;
2561 first_defn = b;
2563 /* Find the final overrider. */
2564 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2565 if (overrider == error_mark_node)
2567 error ("no unique final overrider for %qD in %qT", target_fn, t);
2568 return;
2570 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2572 /* Check for adjusting covariant return types. */
2573 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2574 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2576 if (POINTER_TYPE_P (over_return)
2577 && TREE_CODE (over_return) == TREE_CODE (base_return)
2578 && CLASS_TYPE_P (TREE_TYPE (over_return))
2579 && CLASS_TYPE_P (TREE_TYPE (base_return))
2580 /* If the overrider is invalid, don't even try. */
2581 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2583 /* If FN is a covariant thunk, we must figure out the adjustment
2584 to the final base FN was converting to. As OVERRIDER_TARGET might
2585 also be converting to the return type of FN, we have to
2586 combine the two conversions here. */
2587 tree fixed_offset, virtual_offset;
2589 over_return = TREE_TYPE (over_return);
2590 base_return = TREE_TYPE (base_return);
2592 if (DECL_THUNK_P (fn))
2594 gcc_assert (DECL_RESULT_THUNK_P (fn));
2595 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2596 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2598 else
2599 fixed_offset = virtual_offset = NULL_TREE;
2601 if (virtual_offset)
2602 /* Find the equivalent binfo within the return type of the
2603 overriding function. We will want the vbase offset from
2604 there. */
2605 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2606 over_return);
2607 else if (!same_type_ignoring_top_level_qualifiers_p
2608 (over_return, base_return))
2610 /* There was no existing virtual thunk (which takes
2611 precedence). So find the binfo of the base function's
2612 return type within the overriding function's return type.
2613 We cannot call lookup base here, because we're inside a
2614 dfs_walk, and will therefore clobber the BINFO_MARKED
2615 flags. Fortunately we know the covariancy is valid (it
2616 has already been checked), so we can just iterate along
2617 the binfos, which have been chained in inheritance graph
2618 order. Of course it is lame that we have to repeat the
2619 search here anyway -- we should really be caching pieces
2620 of the vtable and avoiding this repeated work. */
2621 tree thunk_binfo, base_binfo;
2623 /* Find the base binfo within the overriding function's
2624 return type. We will always find a thunk_binfo, except
2625 when the covariancy is invalid (which we will have
2626 already diagnosed). */
2627 for (base_binfo = TYPE_BINFO (base_return),
2628 thunk_binfo = TYPE_BINFO (over_return);
2629 thunk_binfo;
2630 thunk_binfo = TREE_CHAIN (thunk_binfo))
2631 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2632 BINFO_TYPE (base_binfo)))
2633 break;
2635 /* See if virtual inheritance is involved. */
2636 for (virtual_offset = thunk_binfo;
2637 virtual_offset;
2638 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2639 if (BINFO_VIRTUAL_P (virtual_offset))
2640 break;
2642 if (virtual_offset
2643 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2645 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2647 if (virtual_offset)
2649 /* We convert via virtual base. Adjust the fixed
2650 offset to be from there. */
2651 offset =
2652 size_diffop (offset,
2653 convert (ssizetype,
2654 BINFO_OFFSET (virtual_offset)));
2656 if (fixed_offset)
2657 /* There was an existing fixed offset, this must be
2658 from the base just converted to, and the base the
2659 FN was thunking to. */
2660 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2661 else
2662 fixed_offset = offset;
2666 if (fixed_offset || virtual_offset)
2667 /* Replace the overriding function with a covariant thunk. We
2668 will emit the overriding function in its own slot as
2669 well. */
2670 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2671 fixed_offset, virtual_offset);
2673 else
2674 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2675 !DECL_THUNK_P (fn));
2677 /* If we need a covariant thunk, then we may need to adjust first_defn.
2678 The ABI specifies that the thunks emitted with a function are
2679 determined by which bases the function overrides, so we need to be
2680 sure that we're using a thunk for some overridden base; even if we
2681 know that the necessary this adjustment is zero, there may not be an
2682 appropriate zero-this-adjusment thunk for us to use since thunks for
2683 overriding virtual bases always use the vcall offset.
2685 Furthermore, just choosing any base that overrides this function isn't
2686 quite right, as this slot won't be used for calls through a type that
2687 puts a covariant thunk here. Calling the function through such a type
2688 will use a different slot, and that slot is the one that determines
2689 the thunk emitted for that base.
2691 So, keep looking until we find the base that we're really overriding
2692 in this slot: the nearest primary base that doesn't use a covariant
2693 thunk in this slot. */
2694 if (overrider_target != overrider_fn)
2696 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2697 /* We already know that the overrider needs a covariant thunk. */
2698 b = get_primary_binfo (b);
2699 for (; ; b = get_primary_binfo (b))
2701 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2702 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2703 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2704 break;
2705 if (BINFO_LOST_PRIMARY_P (b))
2706 lost = true;
2708 first_defn = b;
2711 /* Assume that we will produce a thunk that convert all the way to
2712 the final overrider, and not to an intermediate virtual base. */
2713 virtual_base = NULL_TREE;
2715 /* See if we can convert to an intermediate virtual base first, and then
2716 use the vcall offset located there to finish the conversion. */
2717 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2719 /* If we find the final overrider, then we can stop
2720 walking. */
2721 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2722 BINFO_TYPE (TREE_VALUE (overrider))))
2723 break;
2725 /* If we find a virtual base, and we haven't yet found the
2726 overrider, then there is a virtual base between the
2727 declaring base (first_defn) and the final overrider. */
2728 if (BINFO_VIRTUAL_P (b))
2730 virtual_base = b;
2731 break;
2735 /* Compute the constant adjustment to the `this' pointer. The
2736 `this' pointer, when this function is called, will point at BINFO
2737 (or one of its primary bases, which are at the same offset). */
2738 if (virtual_base)
2739 /* The `this' pointer needs to be adjusted from the declaration to
2740 the nearest virtual base. */
2741 delta = size_diffop_loc (input_location,
2742 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2743 convert (ssizetype, BINFO_OFFSET (first_defn)));
2744 else if (lost)
2745 /* If the nearest definition is in a lost primary, we don't need an
2746 entry in our vtable. Except possibly in a constructor vtable,
2747 if we happen to get our primary back. In that case, the offset
2748 will be zero, as it will be a primary base. */
2749 delta = size_zero_node;
2750 else
2751 /* The `this' pointer needs to be adjusted from pointing to
2752 BINFO to pointing at the base where the final overrider
2753 appears. */
2754 delta = size_diffop_loc (input_location,
2755 convert (ssizetype,
2756 BINFO_OFFSET (TREE_VALUE (overrider))),
2757 convert (ssizetype, BINFO_OFFSET (binfo)));
2759 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2761 if (virtual_base)
2762 BV_VCALL_INDEX (*virtuals)
2763 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2764 else
2765 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2767 BV_LOST_PRIMARY (*virtuals) = lost;
2770 /* Called from modify_all_vtables via dfs_walk. */
2772 static tree
2773 dfs_modify_vtables (tree binfo, void* data)
2775 tree t = (tree) data;
2776 tree virtuals;
2777 tree old_virtuals;
2778 unsigned ix;
2780 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2781 /* A base without a vtable needs no modification, and its bases
2782 are uninteresting. */
2783 return dfs_skip_bases;
2785 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2786 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2787 /* Don't do the primary vtable, if it's new. */
2788 return NULL_TREE;
2790 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2791 /* There's no need to modify the vtable for a non-virtual primary
2792 base; we're not going to use that vtable anyhow. We do still
2793 need to do this for virtual primary bases, as they could become
2794 non-primary in a construction vtable. */
2795 return NULL_TREE;
2797 make_new_vtable (t, binfo);
2799 /* Now, go through each of the virtual functions in the virtual
2800 function table for BINFO. Find the final overrider, and update
2801 the BINFO_VIRTUALS list appropriately. */
2802 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2803 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2804 virtuals;
2805 ix++, virtuals = TREE_CHAIN (virtuals),
2806 old_virtuals = TREE_CHAIN (old_virtuals))
2807 update_vtable_entry_for_fn (t,
2808 binfo,
2809 BV_FN (old_virtuals),
2810 &virtuals, ix);
2812 return NULL_TREE;
2815 /* Update all of the primary and secondary vtables for T. Create new
2816 vtables as required, and initialize their RTTI information. Each
2817 of the functions in VIRTUALS is declared in T and may override a
2818 virtual function from a base class; find and modify the appropriate
2819 entries to point to the overriding functions. Returns a list, in
2820 declaration order, of the virtual functions that are declared in T,
2821 but do not appear in the primary base class vtable, and which
2822 should therefore be appended to the end of the vtable for T. */
2824 static tree
2825 modify_all_vtables (tree t, tree virtuals)
2827 tree binfo = TYPE_BINFO (t);
2828 tree *fnsp;
2830 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2831 if (TYPE_CONTAINS_VPTR_P (t))
2832 get_vtable_decl (t, false);
2834 /* Update all of the vtables. */
2835 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2837 /* Add virtual functions not already in our primary vtable. These
2838 will be both those introduced by this class, and those overridden
2839 from secondary bases. It does not include virtuals merely
2840 inherited from secondary bases. */
2841 for (fnsp = &virtuals; *fnsp; )
2843 tree fn = TREE_VALUE (*fnsp);
2845 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2846 || DECL_VINDEX (fn) == error_mark_node)
2848 /* We don't need to adjust the `this' pointer when
2849 calling this function. */
2850 BV_DELTA (*fnsp) = integer_zero_node;
2851 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2853 /* This is a function not already in our vtable. Keep it. */
2854 fnsp = &TREE_CHAIN (*fnsp);
2856 else
2857 /* We've already got an entry for this function. Skip it. */
2858 *fnsp = TREE_CHAIN (*fnsp);
2861 return virtuals;
2864 /* Get the base virtual function declarations in T that have the
2865 indicated NAME. */
2867 static void
2868 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2870 tree methods;
2871 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2872 int i;
2874 /* Find virtual functions in T with the indicated NAME. */
2875 i = lookup_fnfields_1 (t, name);
2876 bool found_decls = false;
2877 if (i != -1)
2878 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2879 methods;
2880 methods = OVL_NEXT (methods))
2882 tree method = OVL_CURRENT (methods);
2884 if (TREE_CODE (method) == FUNCTION_DECL
2885 && DECL_VINDEX (method))
2887 base_fndecls->safe_push (method);
2888 found_decls = true;
2892 if (found_decls)
2893 return;
2895 for (i = 0; i < n_baseclasses; i++)
2897 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2898 get_basefndecls (name, basetype, base_fndecls);
2902 /* If this declaration supersedes the declaration of
2903 a method declared virtual in the base class, then
2904 mark this field as being virtual as well. */
2906 void
2907 check_for_override (tree decl, tree ctype)
2909 bool overrides_found = false;
2910 if (TREE_CODE (decl) == TEMPLATE_DECL)
2911 /* In [temp.mem] we have:
2913 A specialization of a member function template does not
2914 override a virtual function from a base class. */
2915 return;
2916 if ((DECL_DESTRUCTOR_P (decl)
2917 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2918 || DECL_CONV_FN_P (decl))
2919 && look_for_overrides (ctype, decl)
2920 && !DECL_STATIC_FUNCTION_P (decl))
2921 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2922 the error_mark_node so that we know it is an overriding
2923 function. */
2925 DECL_VINDEX (decl) = decl;
2926 overrides_found = true;
2927 if (warn_override && !DECL_OVERRIDE_P (decl)
2928 && !DECL_DESTRUCTOR_P (decl))
2929 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2930 "%qD can be marked override", decl);
2933 if (DECL_VIRTUAL_P (decl))
2935 if (!DECL_VINDEX (decl))
2936 DECL_VINDEX (decl) = error_mark_node;
2937 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2938 if (DECL_DESTRUCTOR_P (decl))
2939 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2941 else if (DECL_FINAL_P (decl))
2942 error ("%q+#D marked %<final%>, but is not virtual", decl);
2943 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2944 error ("%q+#D marked %<override%>, but does not override", decl);
2947 /* Warn about hidden virtual functions that are not overridden in t.
2948 We know that constructors and destructors don't apply. */
2950 static void
2951 warn_hidden (tree t)
2953 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2954 tree fns;
2955 size_t i;
2957 /* We go through each separately named virtual function. */
2958 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2959 vec_safe_iterate (method_vec, i, &fns);
2960 ++i)
2962 tree fn;
2963 tree name;
2964 tree fndecl;
2965 tree base_binfo;
2966 tree binfo;
2967 int j;
2969 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2970 have the same name. Figure out what name that is. */
2971 name = DECL_NAME (OVL_CURRENT (fns));
2972 /* There are no possibly hidden functions yet. */
2973 auto_vec<tree, 20> base_fndecls;
2974 /* Iterate through all of the base classes looking for possibly
2975 hidden functions. */
2976 for (binfo = TYPE_BINFO (t), j = 0;
2977 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2979 tree basetype = BINFO_TYPE (base_binfo);
2980 get_basefndecls (name, basetype, &base_fndecls);
2983 /* If there are no functions to hide, continue. */
2984 if (base_fndecls.is_empty ())
2985 continue;
2987 /* Remove any overridden functions. */
2988 for (fn = fns; fn; fn = OVL_NEXT (fn))
2990 fndecl = OVL_CURRENT (fn);
2991 if (TREE_CODE (fndecl) == FUNCTION_DECL
2992 && DECL_VINDEX (fndecl))
2994 /* If the method from the base class has the same
2995 signature as the method from the derived class, it
2996 has been overridden. */
2997 for (size_t k = 0; k < base_fndecls.length (); k++)
2998 if (base_fndecls[k]
2999 && same_signature_p (fndecl, base_fndecls[k]))
3000 base_fndecls[k] = NULL_TREE;
3004 /* Now give a warning for all base functions without overriders,
3005 as they are hidden. */
3006 size_t k;
3007 tree base_fndecl;
3008 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3009 if (base_fndecl)
3011 /* Here we know it is a hider, and no overrider exists. */
3012 warning_at (location_of (base_fndecl),
3013 OPT_Woverloaded_virtual,
3014 "%qD was hidden", base_fndecl);
3015 warning_at (location_of (fns),
3016 OPT_Woverloaded_virtual, " by %qD", fns);
3021 /* Recursive helper for finish_struct_anon. */
3023 static void
3024 finish_struct_anon_r (tree field, bool complain)
3026 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3027 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3028 for (; elt; elt = DECL_CHAIN (elt))
3030 /* We're generally only interested in entities the user
3031 declared, but we also find nested classes by noticing
3032 the TYPE_DECL that we create implicitly. You're
3033 allowed to put one anonymous union inside another,
3034 though, so we explicitly tolerate that. We use
3035 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3036 we also allow unnamed types used for defining fields. */
3037 if (DECL_ARTIFICIAL (elt)
3038 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3039 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
3040 continue;
3042 if (TREE_CODE (elt) != FIELD_DECL)
3044 /* We already complained about static data members in
3045 finish_static_data_member_decl. */
3046 if (complain && !VAR_P (elt))
3048 if (is_union)
3049 permerror (DECL_SOURCE_LOCATION (elt),
3050 "%q#D invalid; an anonymous union can "
3051 "only have non-static data members", elt);
3052 else
3053 permerror (DECL_SOURCE_LOCATION (elt),
3054 "%q#D invalid; an anonymous struct can "
3055 "only have non-static data members", elt);
3057 continue;
3060 if (complain)
3062 if (TREE_PRIVATE (elt))
3064 if (is_union)
3065 permerror (DECL_SOURCE_LOCATION (elt),
3066 "private member %q#D in anonymous union", elt);
3067 else
3068 permerror (DECL_SOURCE_LOCATION (elt),
3069 "private member %q#D in anonymous struct", elt);
3071 else if (TREE_PROTECTED (elt))
3073 if (is_union)
3074 permerror (DECL_SOURCE_LOCATION (elt),
3075 "protected member %q#D in anonymous union", elt);
3076 else
3077 permerror (DECL_SOURCE_LOCATION (elt),
3078 "protected member %q#D in anonymous struct", elt);
3082 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3083 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3085 /* Recurse into the anonymous aggregates to handle correctly
3086 access control (c++/24926):
3088 class A {
3089 union {
3090 union {
3091 int i;
3096 int j=A().i; */
3097 if (DECL_NAME (elt) == NULL_TREE
3098 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3099 finish_struct_anon_r (elt, /*complain=*/false);
3103 /* Check for things that are invalid. There are probably plenty of other
3104 things we should check for also. */
3106 static void
3107 finish_struct_anon (tree t)
3109 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3111 if (TREE_STATIC (field))
3112 continue;
3113 if (TREE_CODE (field) != FIELD_DECL)
3114 continue;
3116 if (DECL_NAME (field) == NULL_TREE
3117 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3118 finish_struct_anon_r (field, /*complain=*/true);
3122 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3123 will be used later during class template instantiation.
3124 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3125 a non-static member data (FIELD_DECL), a member function
3126 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3127 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3128 When FRIEND_P is nonzero, T is either a friend class
3129 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3130 (FUNCTION_DECL, TEMPLATE_DECL). */
3132 void
3133 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3135 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3136 if (CLASSTYPE_TEMPLATE_INFO (type))
3137 CLASSTYPE_DECL_LIST (type)
3138 = tree_cons (friend_p ? NULL_TREE : type,
3139 t, CLASSTYPE_DECL_LIST (type));
3142 /* This function is called from declare_virt_assop_and_dtor via
3143 dfs_walk_all.
3145 DATA is a type that direcly or indirectly inherits the base
3146 represented by BINFO. If BINFO contains a virtual assignment [copy
3147 assignment or move assigment] operator or a virtual constructor,
3148 declare that function in DATA if it hasn't been already declared. */
3150 static tree
3151 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3153 tree bv, fn, t = (tree)data;
3154 tree opname = ansi_assopname (NOP_EXPR);
3156 gcc_assert (t && CLASS_TYPE_P (t));
3157 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3159 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3160 /* A base without a vtable needs no modification, and its bases
3161 are uninteresting. */
3162 return dfs_skip_bases;
3164 if (BINFO_PRIMARY_P (binfo))
3165 /* If this is a primary base, then we have already looked at the
3166 virtual functions of its vtable. */
3167 return NULL_TREE;
3169 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3171 fn = BV_FN (bv);
3173 if (DECL_NAME (fn) == opname)
3175 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3176 lazily_declare_fn (sfk_copy_assignment, t);
3177 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3178 lazily_declare_fn (sfk_move_assignment, t);
3180 else if (DECL_DESTRUCTOR_P (fn)
3181 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3182 lazily_declare_fn (sfk_destructor, t);
3185 return NULL_TREE;
3188 /* If the class type T has a direct or indirect base that contains a
3189 virtual assignment operator or a virtual destructor, declare that
3190 function in T if it hasn't been already declared. */
3192 static void
3193 declare_virt_assop_and_dtor (tree t)
3195 if (!(TYPE_POLYMORPHIC_P (t)
3196 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3197 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3198 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3199 return;
3201 dfs_walk_all (TYPE_BINFO (t),
3202 dfs_declare_virt_assop_and_dtor,
3203 NULL, t);
3206 /* Declare the inheriting constructor for class T inherited from base
3207 constructor CTOR with the parameter array PARMS of size NPARMS. */
3209 static void
3210 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3212 /* We don't declare an inheriting ctor that would be a default,
3213 copy or move ctor for derived or base. */
3214 if (nparms == 0)
3215 return;
3216 if (nparms == 1
3217 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3219 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3220 if (parm == t || parm == DECL_CONTEXT (ctor))
3221 return;
3224 tree parmlist = void_list_node;
3225 for (int i = nparms - 1; i >= 0; i--)
3226 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3227 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3228 t, false, ctor, parmlist);
3229 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3230 if (add_method (t, fn, NULL_TREE))
3232 DECL_CHAIN (fn) = TYPE_METHODS (t);
3233 TYPE_METHODS (t) = fn;
3237 /* Declare all the inheriting constructors for class T inherited from base
3238 constructor CTOR. */
3240 static void
3241 one_inherited_ctor (tree ctor, tree t)
3243 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3245 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3246 int i = 0;
3247 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3249 if (TREE_PURPOSE (parms))
3250 one_inheriting_sig (t, ctor, new_parms, i);
3251 new_parms[i++] = TREE_VALUE (parms);
3253 one_inheriting_sig (t, ctor, new_parms, i);
3254 if (parms == NULL_TREE)
3256 if (warning (OPT_Winherited_variadic_ctor,
3257 "the ellipsis in %qD is not inherited", ctor))
3258 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3262 /* Create default constructors, assignment operators, and so forth for
3263 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3264 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3265 the class cannot have a default constructor, copy constructor
3266 taking a const reference argument, or an assignment operator taking
3267 a const reference, respectively. */
3269 static void
3270 add_implicitly_declared_members (tree t, tree* access_decls,
3271 int cant_have_const_cctor,
3272 int cant_have_const_assignment)
3274 bool move_ok = false;
3276 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3277 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3278 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3279 move_ok = true;
3281 /* Destructor. */
3282 if (!CLASSTYPE_DESTRUCTORS (t))
3284 /* In general, we create destructors lazily. */
3285 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3287 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3288 && TYPE_FOR_JAVA (t))
3289 /* But if this is a Java class, any non-trivial destructor is
3290 invalid, even if compiler-generated. Therefore, if the
3291 destructor is non-trivial we create it now. */
3292 lazily_declare_fn (sfk_destructor, t);
3295 /* [class.ctor]
3297 If there is no user-declared constructor for a class, a default
3298 constructor is implicitly declared. */
3299 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3301 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3302 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3303 if (cxx_dialect >= cxx11)
3304 TYPE_HAS_CONSTEXPR_CTOR (t)
3305 /* This might force the declaration. */
3306 = type_has_constexpr_default_constructor (t);
3309 /* [class.ctor]
3311 If a class definition does not explicitly declare a copy
3312 constructor, one is declared implicitly. */
3313 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3315 TYPE_HAS_COPY_CTOR (t) = 1;
3316 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3317 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3318 if (move_ok)
3319 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3322 /* If there is no assignment operator, one will be created if and
3323 when it is needed. For now, just record whether or not the type
3324 of the parameter to the assignment operator will be a const or
3325 non-const reference. */
3326 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3328 TYPE_HAS_COPY_ASSIGN (t) = 1;
3329 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3330 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3331 if (move_ok && !LAMBDA_TYPE_P (t))
3332 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3335 /* We can't be lazy about declaring functions that might override
3336 a virtual function from a base class. */
3337 declare_virt_assop_and_dtor (t);
3339 while (*access_decls)
3341 tree using_decl = TREE_VALUE (*access_decls);
3342 tree decl = USING_DECL_DECLS (using_decl);
3343 if (DECL_NAME (using_decl) == ctor_identifier)
3345 /* declare, then remove the decl */
3346 tree ctor_list = decl;
3347 location_t loc = input_location;
3348 input_location = DECL_SOURCE_LOCATION (using_decl);
3349 if (ctor_list)
3350 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3351 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3352 *access_decls = TREE_CHAIN (*access_decls);
3353 input_location = loc;
3355 else
3356 access_decls = &TREE_CHAIN (*access_decls);
3360 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3361 count the number of fields in TYPE, including anonymous union
3362 members. */
3364 static int
3365 count_fields (tree fields)
3367 tree x;
3368 int n_fields = 0;
3369 for (x = fields; x; x = DECL_CHAIN (x))
3371 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3372 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3373 else
3374 n_fields += 1;
3376 return n_fields;
3379 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3380 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3381 elts, starting at offset IDX. */
3383 static int
3384 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3386 tree x;
3387 for (x = fields; x; x = DECL_CHAIN (x))
3389 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3390 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3391 else
3392 field_vec->elts[idx++] = x;
3394 return idx;
3397 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3398 starting at offset IDX. */
3400 static int
3401 add_enum_fields_to_record_type (tree enumtype,
3402 struct sorted_fields_type *field_vec,
3403 int idx)
3405 tree values;
3406 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3407 field_vec->elts[idx++] = TREE_VALUE (values);
3408 return idx;
3411 /* FIELD is a bit-field. We are finishing the processing for its
3412 enclosing type. Issue any appropriate messages and set appropriate
3413 flags. Returns false if an error has been diagnosed. */
3415 static bool
3416 check_bitfield_decl (tree field)
3418 tree type = TREE_TYPE (field);
3419 tree w;
3421 /* Extract the declared width of the bitfield, which has been
3422 temporarily stashed in DECL_INITIAL. */
3423 w = DECL_INITIAL (field);
3424 gcc_assert (w != NULL_TREE);
3425 /* Remove the bit-field width indicator so that the rest of the
3426 compiler does not treat that value as an initializer. */
3427 DECL_INITIAL (field) = NULL_TREE;
3429 /* Detect invalid bit-field type. */
3430 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3432 error ("bit-field %q+#D with non-integral type", field);
3433 w = error_mark_node;
3435 else
3437 location_t loc = input_location;
3438 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3439 STRIP_NOPS (w);
3441 /* detect invalid field size. */
3442 input_location = DECL_SOURCE_LOCATION (field);
3443 w = cxx_constant_value (w);
3444 input_location = loc;
3446 if (TREE_CODE (w) != INTEGER_CST)
3448 error ("bit-field %q+D width not an integer constant", field);
3449 w = error_mark_node;
3451 else if (tree_int_cst_sgn (w) < 0)
3453 error ("negative width in bit-field %q+D", field);
3454 w = error_mark_node;
3456 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3458 error ("zero width for bit-field %q+D", field);
3459 w = error_mark_node;
3461 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3462 && TREE_CODE (type) != BOOLEAN_TYPE
3463 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3464 || ((TREE_CODE (type) == ENUMERAL_TYPE
3465 || TREE_CODE (type) == BOOLEAN_TYPE)
3466 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3467 warning_at (DECL_SOURCE_LOCATION (field), 0,
3468 "width of %qD exceeds its type", field);
3469 else if (TREE_CODE (type) == ENUMERAL_TYPE
3470 && (0 > (compare_tree_int
3471 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3472 warning_at (DECL_SOURCE_LOCATION (field), 0,
3473 "%qD is too small to hold all values of %q#T",
3474 field, type);
3477 if (w != error_mark_node)
3479 DECL_SIZE (field) = convert (bitsizetype, w);
3480 DECL_BIT_FIELD (field) = 1;
3481 return true;
3483 else
3485 /* Non-bit-fields are aligned for their type. */
3486 DECL_BIT_FIELD (field) = 0;
3487 CLEAR_DECL_C_BIT_FIELD (field);
3488 return false;
3492 /* FIELD is a non bit-field. We are finishing the processing for its
3493 enclosing type T. Issue any appropriate messages and set appropriate
3494 flags. */
3496 static void
3497 check_field_decl (tree field,
3498 tree t,
3499 int* cant_have_const_ctor,
3500 int* no_const_asn_ref,
3501 int* any_default_members)
3503 tree type = strip_array_types (TREE_TYPE (field));
3505 /* In C++98 an anonymous union cannot contain any fields which would change
3506 the settings of CANT_HAVE_CONST_CTOR and friends. */
3507 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3509 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3510 structs. So, we recurse through their fields here. */
3511 else if (ANON_AGGR_TYPE_P (type))
3513 tree fields;
3515 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3516 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3517 check_field_decl (fields, t, cant_have_const_ctor,
3518 no_const_asn_ref, any_default_members);
3520 /* Check members with class type for constructors, destructors,
3521 etc. */
3522 else if (CLASS_TYPE_P (type))
3524 /* Never let anything with uninheritable virtuals
3525 make it through without complaint. */
3526 abstract_virtuals_error (field, type);
3528 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3530 static bool warned;
3531 int oldcount = errorcount;
3532 if (TYPE_NEEDS_CONSTRUCTING (type))
3533 error ("member %q+#D with constructor not allowed in union",
3534 field);
3535 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3536 error ("member %q+#D with destructor not allowed in union", field);
3537 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3538 error ("member %q+#D with copy assignment operator not allowed in union",
3539 field);
3540 if (!warned && errorcount > oldcount)
3542 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3543 "only available with -std=c++11 or -std=gnu++11");
3544 warned = true;
3547 else
3549 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3550 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3551 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3552 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3553 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3554 || !TYPE_HAS_COPY_ASSIGN (type));
3555 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3556 || !TYPE_HAS_COPY_CTOR (type));
3557 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3558 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3559 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3560 || TYPE_HAS_COMPLEX_DFLT (type));
3563 if (TYPE_HAS_COPY_CTOR (type)
3564 && !TYPE_HAS_CONST_COPY_CTOR (type))
3565 *cant_have_const_ctor = 1;
3567 if (TYPE_HAS_COPY_ASSIGN (type)
3568 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3569 *no_const_asn_ref = 1;
3572 check_abi_tags (t, field);
3574 if (DECL_INITIAL (field) != NULL_TREE)
3576 /* `build_class_init_list' does not recognize
3577 non-FIELD_DECLs. */
3578 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3579 error ("multiple fields in union %qT initialized", t);
3580 *any_default_members = 1;
3584 /* Check the data members (both static and non-static), class-scoped
3585 typedefs, etc., appearing in the declaration of T. Issue
3586 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3587 declaration order) of access declarations; each TREE_VALUE in this
3588 list is a USING_DECL.
3590 In addition, set the following flags:
3592 EMPTY_P
3593 The class is empty, i.e., contains no non-static data members.
3595 CANT_HAVE_CONST_CTOR_P
3596 This class cannot have an implicitly generated copy constructor
3597 taking a const reference.
3599 CANT_HAVE_CONST_ASN_REF
3600 This class cannot have an implicitly generated assignment
3601 operator taking a const reference.
3603 All of these flags should be initialized before calling this
3604 function.
3606 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3607 fields can be added by adding to this chain. */
3609 static void
3610 check_field_decls (tree t, tree *access_decls,
3611 int *cant_have_const_ctor_p,
3612 int *no_const_asn_ref_p)
3614 tree *field;
3615 tree *next;
3616 bool has_pointers;
3617 int any_default_members;
3618 int cant_pack = 0;
3619 int field_access = -1;
3621 /* Assume there are no access declarations. */
3622 *access_decls = NULL_TREE;
3623 /* Assume this class has no pointer members. */
3624 has_pointers = false;
3625 /* Assume none of the members of this class have default
3626 initializations. */
3627 any_default_members = 0;
3629 for (field = &TYPE_FIELDS (t); *field; field = next)
3631 tree x = *field;
3632 tree type = TREE_TYPE (x);
3633 int this_field_access;
3635 next = &DECL_CHAIN (x);
3637 if (TREE_CODE (x) == USING_DECL)
3639 /* Save the access declarations for our caller. */
3640 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3641 continue;
3644 if (TREE_CODE (x) == TYPE_DECL
3645 || TREE_CODE (x) == TEMPLATE_DECL)
3646 continue;
3648 /* If we've gotten this far, it's a data member, possibly static,
3649 or an enumerator. */
3650 if (TREE_CODE (x) != CONST_DECL)
3651 DECL_CONTEXT (x) = t;
3653 /* When this goes into scope, it will be a non-local reference. */
3654 DECL_NONLOCAL (x) = 1;
3656 if (TREE_CODE (t) == UNION_TYPE
3657 && cxx_dialect < cxx11)
3659 /* [class.union] (C++98)
3661 If a union contains a static data member, or a member of
3662 reference type, the program is ill-formed.
3664 In C++11 this limitation doesn't exist anymore. */
3665 if (VAR_P (x))
3667 error ("in C++98 %q+D may not be static because it is "
3668 "a member of a union", x);
3669 continue;
3671 if (TREE_CODE (type) == REFERENCE_TYPE)
3673 error ("in C++98 %q+D may not have reference type %qT "
3674 "because it is a member of a union", x, type);
3675 continue;
3679 /* Perform error checking that did not get done in
3680 grokdeclarator. */
3681 if (TREE_CODE (type) == FUNCTION_TYPE)
3683 error ("field %q+D invalidly declared function type", x);
3684 type = build_pointer_type (type);
3685 TREE_TYPE (x) = type;
3687 else if (TREE_CODE (type) == METHOD_TYPE)
3689 error ("field %q+D invalidly declared method type", x);
3690 type = build_pointer_type (type);
3691 TREE_TYPE (x) = type;
3694 if (type == error_mark_node)
3695 continue;
3697 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3698 continue;
3700 /* Now it can only be a FIELD_DECL. */
3702 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3703 CLASSTYPE_NON_AGGREGATE (t) = 1;
3705 /* If at least one non-static data member is non-literal, the whole
3706 class becomes non-literal. Per Core/1453, volatile non-static
3707 data members and base classes are also not allowed.
3708 Note: if the type is incomplete we will complain later on. */
3709 if (COMPLETE_TYPE_P (type)
3710 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3711 CLASSTYPE_LITERAL_P (t) = false;
3713 /* A standard-layout class is a class that:
3715 has the same access control (Clause 11) for all non-static data members,
3716 ... */
3717 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3718 if (field_access == -1)
3719 field_access = this_field_access;
3720 else if (this_field_access != field_access)
3721 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3723 /* If this is of reference type, check if it needs an init. */
3724 if (TREE_CODE (type) == REFERENCE_TYPE)
3726 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3727 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3728 if (DECL_INITIAL (x) == NULL_TREE)
3729 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3730 if (cxx_dialect < cxx11)
3732 /* ARM $12.6.2: [A member initializer list] (or, for an
3733 aggregate, initialization by a brace-enclosed list) is the
3734 only way to initialize nonstatic const and reference
3735 members. */
3736 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3737 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3741 type = strip_array_types (type);
3743 if (TYPE_PACKED (t))
3745 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3747 warning_at
3748 (DECL_SOURCE_LOCATION (x), 0,
3749 "ignoring packed attribute because of unpacked non-POD field %q#D",
3751 cant_pack = 1;
3753 else if (DECL_C_BIT_FIELD (x)
3754 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3755 DECL_PACKED (x) = 1;
3758 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3759 /* We don't treat zero-width bitfields as making a class
3760 non-empty. */
3762 else
3764 /* The class is non-empty. */
3765 CLASSTYPE_EMPTY_P (t) = 0;
3766 /* The class is not even nearly empty. */
3767 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3768 /* If one of the data members contains an empty class,
3769 so does T. */
3770 if (CLASS_TYPE_P (type)
3771 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3772 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3775 /* This is used by -Weffc++ (see below). Warn only for pointers
3776 to members which might hold dynamic memory. So do not warn
3777 for pointers to functions or pointers to members. */
3778 if (TYPE_PTR_P (type)
3779 && !TYPE_PTRFN_P (type))
3780 has_pointers = true;
3782 if (CLASS_TYPE_P (type))
3784 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3785 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3786 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3787 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3790 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3791 CLASSTYPE_HAS_MUTABLE (t) = 1;
3793 if (DECL_MUTABLE_P (x))
3795 if (CP_TYPE_CONST_P (type))
3797 error ("member %q+D cannot be declared both %<const%> "
3798 "and %<mutable%>", x);
3799 continue;
3801 if (TREE_CODE (type) == REFERENCE_TYPE)
3803 error ("member %q+D cannot be declared as a %<mutable%> "
3804 "reference", x);
3805 continue;
3809 if (! layout_pod_type_p (type))
3810 /* DR 148 now allows pointers to members (which are POD themselves),
3811 to be allowed in POD structs. */
3812 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3814 if (!std_layout_type_p (type))
3815 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3817 if (! zero_init_p (type))
3818 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3820 /* We set DECL_C_BIT_FIELD in grokbitfield.
3821 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3822 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3823 check_field_decl (x, t,
3824 cant_have_const_ctor_p,
3825 no_const_asn_ref_p,
3826 &any_default_members);
3828 /* Now that we've removed bit-field widths from DECL_INITIAL,
3829 anything left in DECL_INITIAL is an NSDMI that makes the class
3830 non-aggregate in C++11. */
3831 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3832 CLASSTYPE_NON_AGGREGATE (t) = true;
3834 /* If any field is const, the structure type is pseudo-const. */
3835 if (CP_TYPE_CONST_P (type))
3837 C_TYPE_FIELDS_READONLY (t) = 1;
3838 if (DECL_INITIAL (x) == NULL_TREE)
3839 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3840 if (cxx_dialect < cxx11)
3842 /* ARM $12.6.2: [A member initializer list] (or, for an
3843 aggregate, initialization by a brace-enclosed list) is the
3844 only way to initialize nonstatic const and reference
3845 members. */
3846 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3847 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3850 /* A field that is pseudo-const makes the structure likewise. */
3851 else if (CLASS_TYPE_P (type))
3853 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3854 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3855 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3856 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3859 /* Core issue 80: A nonstatic data member is required to have a
3860 different name from the class iff the class has a
3861 user-declared constructor. */
3862 if (constructor_name_p (DECL_NAME (x), t)
3863 && TYPE_HAS_USER_CONSTRUCTOR (t))
3864 permerror (DECL_SOURCE_LOCATION (x),
3865 "field %q#D with same name as class", x);
3868 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3869 it should also define a copy constructor and an assignment operator to
3870 implement the correct copy semantic (deep vs shallow, etc.). As it is
3871 not feasible to check whether the constructors do allocate dynamic memory
3872 and store it within members, we approximate the warning like this:
3874 -- Warn only if there are members which are pointers
3875 -- Warn only if there is a non-trivial constructor (otherwise,
3876 there cannot be memory allocated).
3877 -- Warn only if there is a non-trivial destructor. We assume that the
3878 user at least implemented the cleanup correctly, and a destructor
3879 is needed to free dynamic memory.
3881 This seems enough for practical purposes. */
3882 if (warn_ecpp
3883 && has_pointers
3884 && TYPE_HAS_USER_CONSTRUCTOR (t)
3885 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3886 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3888 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3890 if (! TYPE_HAS_COPY_CTOR (t))
3892 warning (OPT_Weffc__,
3893 " but does not override %<%T(const %T&)%>", t, t);
3894 if (!TYPE_HAS_COPY_ASSIGN (t))
3895 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3897 else if (! TYPE_HAS_COPY_ASSIGN (t))
3898 warning (OPT_Weffc__,
3899 " but does not override %<operator=(const %T&)%>", t);
3902 /* Non-static data member initializers make the default constructor
3903 non-trivial. */
3904 if (any_default_members)
3906 TYPE_NEEDS_CONSTRUCTING (t) = true;
3907 TYPE_HAS_COMPLEX_DFLT (t) = true;
3910 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3911 if (cant_pack)
3912 TYPE_PACKED (t) = 0;
3914 /* Check anonymous struct/anonymous union fields. */
3915 finish_struct_anon (t);
3917 /* We've built up the list of access declarations in reverse order.
3918 Fix that now. */
3919 *access_decls = nreverse (*access_decls);
3922 /* If TYPE is an empty class type, records its OFFSET in the table of
3923 OFFSETS. */
3925 static int
3926 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3928 splay_tree_node n;
3930 if (!is_empty_class (type))
3931 return 0;
3933 /* Record the location of this empty object in OFFSETS. */
3934 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3935 if (!n)
3936 n = splay_tree_insert (offsets,
3937 (splay_tree_key) offset,
3938 (splay_tree_value) NULL_TREE);
3939 n->value = ((splay_tree_value)
3940 tree_cons (NULL_TREE,
3941 type,
3942 (tree) n->value));
3944 return 0;
3947 /* Returns nonzero if TYPE is an empty class type and there is
3948 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3950 static int
3951 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3953 splay_tree_node n;
3954 tree t;
3956 if (!is_empty_class (type))
3957 return 0;
3959 /* Record the location of this empty object in OFFSETS. */
3960 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3961 if (!n)
3962 return 0;
3964 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3965 if (same_type_p (TREE_VALUE (t), type))
3966 return 1;
3968 return 0;
3971 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3972 F for every subobject, passing it the type, offset, and table of
3973 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3974 be traversed.
3976 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3977 than MAX_OFFSET will not be walked.
3979 If F returns a nonzero value, the traversal ceases, and that value
3980 is returned. Otherwise, returns zero. */
3982 static int
3983 walk_subobject_offsets (tree type,
3984 subobject_offset_fn f,
3985 tree offset,
3986 splay_tree offsets,
3987 tree max_offset,
3988 int vbases_p)
3990 int r = 0;
3991 tree type_binfo = NULL_TREE;
3993 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3994 stop. */
3995 if (max_offset && tree_int_cst_lt (max_offset, offset))
3996 return 0;
3998 if (type == error_mark_node)
3999 return 0;
4001 if (!TYPE_P (type))
4003 type_binfo = type;
4004 type = BINFO_TYPE (type);
4007 if (CLASS_TYPE_P (type))
4009 tree field;
4010 tree binfo;
4011 int i;
4013 /* Avoid recursing into objects that are not interesting. */
4014 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4015 return 0;
4017 /* Record the location of TYPE. */
4018 r = (*f) (type, offset, offsets);
4019 if (r)
4020 return r;
4022 /* Iterate through the direct base classes of TYPE. */
4023 if (!type_binfo)
4024 type_binfo = TYPE_BINFO (type);
4025 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4027 tree binfo_offset;
4029 if (BINFO_VIRTUAL_P (binfo))
4030 continue;
4032 tree orig_binfo;
4033 /* We cannot rely on BINFO_OFFSET being set for the base
4034 class yet, but the offsets for direct non-virtual
4035 bases can be calculated by going back to the TYPE. */
4036 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4037 binfo_offset = size_binop (PLUS_EXPR,
4038 offset,
4039 BINFO_OFFSET (orig_binfo));
4041 r = walk_subobject_offsets (binfo,
4043 binfo_offset,
4044 offsets,
4045 max_offset,
4046 /*vbases_p=*/0);
4047 if (r)
4048 return r;
4051 if (CLASSTYPE_VBASECLASSES (type))
4053 unsigned ix;
4054 vec<tree, va_gc> *vbases;
4056 /* Iterate through the virtual base classes of TYPE. In G++
4057 3.2, we included virtual bases in the direct base class
4058 loop above, which results in incorrect results; the
4059 correct offsets for virtual bases are only known when
4060 working with the most derived type. */
4061 if (vbases_p)
4062 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4063 vec_safe_iterate (vbases, ix, &binfo); ix++)
4065 r = walk_subobject_offsets (binfo,
4067 size_binop (PLUS_EXPR,
4068 offset,
4069 BINFO_OFFSET (binfo)),
4070 offsets,
4071 max_offset,
4072 /*vbases_p=*/0);
4073 if (r)
4074 return r;
4076 else
4078 /* We still have to walk the primary base, if it is
4079 virtual. (If it is non-virtual, then it was walked
4080 above.) */
4081 tree vbase = get_primary_binfo (type_binfo);
4083 if (vbase && BINFO_VIRTUAL_P (vbase)
4084 && BINFO_PRIMARY_P (vbase)
4085 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4087 r = (walk_subobject_offsets
4088 (vbase, f, offset,
4089 offsets, max_offset, /*vbases_p=*/0));
4090 if (r)
4091 return r;
4096 /* Iterate through the fields of TYPE. */
4097 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4098 if (TREE_CODE (field) == FIELD_DECL
4099 && TREE_TYPE (field) != error_mark_node
4100 && !DECL_ARTIFICIAL (field))
4102 tree field_offset;
4104 field_offset = byte_position (field);
4106 r = walk_subobject_offsets (TREE_TYPE (field),
4108 size_binop (PLUS_EXPR,
4109 offset,
4110 field_offset),
4111 offsets,
4112 max_offset,
4113 /*vbases_p=*/1);
4114 if (r)
4115 return r;
4118 else if (TREE_CODE (type) == ARRAY_TYPE)
4120 tree element_type = strip_array_types (type);
4121 tree domain = TYPE_DOMAIN (type);
4122 tree index;
4124 /* Avoid recursing into objects that are not interesting. */
4125 if (!CLASS_TYPE_P (element_type)
4126 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
4127 return 0;
4129 /* Step through each of the elements in the array. */
4130 for (index = size_zero_node;
4131 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4132 index = size_binop (PLUS_EXPR, index, size_one_node))
4134 r = walk_subobject_offsets (TREE_TYPE (type),
4136 offset,
4137 offsets,
4138 max_offset,
4139 /*vbases_p=*/1);
4140 if (r)
4141 return r;
4142 offset = size_binop (PLUS_EXPR, offset,
4143 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4144 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4145 there's no point in iterating through the remaining
4146 elements of the array. */
4147 if (max_offset && tree_int_cst_lt (max_offset, offset))
4148 break;
4152 return 0;
4155 /* Record all of the empty subobjects of TYPE (either a type or a
4156 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4157 is being placed at OFFSET; otherwise, it is a base class that is
4158 being placed at OFFSET. */
4160 static void
4161 record_subobject_offsets (tree type,
4162 tree offset,
4163 splay_tree offsets,
4164 bool is_data_member)
4166 tree max_offset;
4167 /* If recording subobjects for a non-static data member or a
4168 non-empty base class , we do not need to record offsets beyond
4169 the size of the biggest empty class. Additional data members
4170 will go at the end of the class. Additional base classes will go
4171 either at offset zero (if empty, in which case they cannot
4172 overlap with offsets past the size of the biggest empty class) or
4173 at the end of the class.
4175 However, if we are placing an empty base class, then we must record
4176 all offsets, as either the empty class is at offset zero (where
4177 other empty classes might later be placed) or at the end of the
4178 class (where other objects might then be placed, so other empty
4179 subobjects might later overlap). */
4180 if (is_data_member
4181 || !is_empty_class (BINFO_TYPE (type)))
4182 max_offset = sizeof_biggest_empty_class;
4183 else
4184 max_offset = NULL_TREE;
4185 walk_subobject_offsets (type, record_subobject_offset, offset,
4186 offsets, max_offset, is_data_member);
4189 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4190 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4191 virtual bases of TYPE are examined. */
4193 static int
4194 layout_conflict_p (tree type,
4195 tree offset,
4196 splay_tree offsets,
4197 int vbases_p)
4199 splay_tree_node max_node;
4201 /* Get the node in OFFSETS that indicates the maximum offset where
4202 an empty subobject is located. */
4203 max_node = splay_tree_max (offsets);
4204 /* If there aren't any empty subobjects, then there's no point in
4205 performing this check. */
4206 if (!max_node)
4207 return 0;
4209 return walk_subobject_offsets (type, check_subobject_offset, offset,
4210 offsets, (tree) (max_node->key),
4211 vbases_p);
4214 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4215 non-static data member of the type indicated by RLI. BINFO is the
4216 binfo corresponding to the base subobject, OFFSETS maps offsets to
4217 types already located at those offsets. This function determines
4218 the position of the DECL. */
4220 static void
4221 layout_nonempty_base_or_field (record_layout_info rli,
4222 tree decl,
4223 tree binfo,
4224 splay_tree offsets)
4226 tree offset = NULL_TREE;
4227 bool field_p;
4228 tree type;
4230 if (binfo)
4232 /* For the purposes of determining layout conflicts, we want to
4233 use the class type of BINFO; TREE_TYPE (DECL) will be the
4234 CLASSTYPE_AS_BASE version, which does not contain entries for
4235 zero-sized bases. */
4236 type = TREE_TYPE (binfo);
4237 field_p = false;
4239 else
4241 type = TREE_TYPE (decl);
4242 field_p = true;
4245 /* Try to place the field. It may take more than one try if we have
4246 a hard time placing the field without putting two objects of the
4247 same type at the same address. */
4248 while (1)
4250 struct record_layout_info_s old_rli = *rli;
4252 /* Place this field. */
4253 place_field (rli, decl);
4254 offset = byte_position (decl);
4256 /* We have to check to see whether or not there is already
4257 something of the same type at the offset we're about to use.
4258 For example, consider:
4260 struct S {};
4261 struct T : public S { int i; };
4262 struct U : public S, public T {};
4264 Here, we put S at offset zero in U. Then, we can't put T at
4265 offset zero -- its S component would be at the same address
4266 as the S we already allocated. So, we have to skip ahead.
4267 Since all data members, including those whose type is an
4268 empty class, have nonzero size, any overlap can happen only
4269 with a direct or indirect base-class -- it can't happen with
4270 a data member. */
4271 /* In a union, overlap is permitted; all members are placed at
4272 offset zero. */
4273 if (TREE_CODE (rli->t) == UNION_TYPE)
4274 break;
4275 if (layout_conflict_p (field_p ? type : binfo, offset,
4276 offsets, field_p))
4278 /* Strip off the size allocated to this field. That puts us
4279 at the first place we could have put the field with
4280 proper alignment. */
4281 *rli = old_rli;
4283 /* Bump up by the alignment required for the type. */
4284 rli->bitpos
4285 = size_binop (PLUS_EXPR, rli->bitpos,
4286 bitsize_int (binfo
4287 ? CLASSTYPE_ALIGN (type)
4288 : TYPE_ALIGN (type)));
4289 normalize_rli (rli);
4291 else if (TREE_CODE (type) == NULLPTR_TYPE
4292 && warn_abi && abi_version_crosses (9))
4294 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4295 the offset wasn't aligned like a pointer when we started to
4296 layout this field, that affects its position. */
4297 tree pos = rli_size_unit_so_far (&old_rli);
4298 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4300 if (abi_version_at_least (9))
4301 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4302 "alignment of %qD increased in -fabi-version=9 "
4303 "(GCC 5.2)", decl);
4304 else
4305 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4306 "of %qD will increase in -fabi-version=9", decl);
4308 break;
4310 else
4311 /* There was no conflict. We're done laying out this field. */
4312 break;
4315 /* Now that we know where it will be placed, update its
4316 BINFO_OFFSET. */
4317 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4318 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4319 this point because their BINFO_OFFSET is copied from another
4320 hierarchy. Therefore, we may not need to add the entire
4321 OFFSET. */
4322 propagate_binfo_offsets (binfo,
4323 size_diffop_loc (input_location,
4324 convert (ssizetype, offset),
4325 convert (ssizetype,
4326 BINFO_OFFSET (binfo))));
4329 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4331 static int
4332 empty_base_at_nonzero_offset_p (tree type,
4333 tree offset,
4334 splay_tree /*offsets*/)
4336 return is_empty_class (type) && !integer_zerop (offset);
4339 /* Layout the empty base BINFO. EOC indicates the byte currently just
4340 past the end of the class, and should be correctly aligned for a
4341 class of the type indicated by BINFO; OFFSETS gives the offsets of
4342 the empty bases allocated so far. T is the most derived
4343 type. Return nonzero iff we added it at the end. */
4345 static bool
4346 layout_empty_base (record_layout_info rli, tree binfo,
4347 tree eoc, splay_tree offsets)
4349 tree alignment;
4350 tree basetype = BINFO_TYPE (binfo);
4351 bool atend = false;
4353 /* This routine should only be used for empty classes. */
4354 gcc_assert (is_empty_class (basetype));
4355 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4357 if (!integer_zerop (BINFO_OFFSET (binfo)))
4358 propagate_binfo_offsets
4359 (binfo, size_diffop_loc (input_location,
4360 size_zero_node, BINFO_OFFSET (binfo)));
4362 /* This is an empty base class. We first try to put it at offset
4363 zero. */
4364 if (layout_conflict_p (binfo,
4365 BINFO_OFFSET (binfo),
4366 offsets,
4367 /*vbases_p=*/0))
4369 /* That didn't work. Now, we move forward from the next
4370 available spot in the class. */
4371 atend = true;
4372 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4373 while (1)
4375 if (!layout_conflict_p (binfo,
4376 BINFO_OFFSET (binfo),
4377 offsets,
4378 /*vbases_p=*/0))
4379 /* We finally found a spot where there's no overlap. */
4380 break;
4382 /* There's overlap here, too. Bump along to the next spot. */
4383 propagate_binfo_offsets (binfo, alignment);
4387 if (CLASSTYPE_USER_ALIGN (basetype))
4389 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4390 if (warn_packed)
4391 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4392 TYPE_USER_ALIGN (rli->t) = 1;
4395 return atend;
4398 /* Layout the base given by BINFO in the class indicated by RLI.
4399 *BASE_ALIGN is a running maximum of the alignments of
4400 any base class. OFFSETS gives the location of empty base
4401 subobjects. T is the most derived type. Return nonzero if the new
4402 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4403 *NEXT_FIELD, unless BINFO is for an empty base class.
4405 Returns the location at which the next field should be inserted. */
4407 static tree *
4408 build_base_field (record_layout_info rli, tree binfo,
4409 splay_tree offsets, tree *next_field)
4411 tree t = rli->t;
4412 tree basetype = BINFO_TYPE (binfo);
4414 if (!COMPLETE_TYPE_P (basetype))
4415 /* This error is now reported in xref_tag, thus giving better
4416 location information. */
4417 return next_field;
4419 /* Place the base class. */
4420 if (!is_empty_class (basetype))
4422 tree decl;
4424 /* The containing class is non-empty because it has a non-empty
4425 base class. */
4426 CLASSTYPE_EMPTY_P (t) = 0;
4428 /* Create the FIELD_DECL. */
4429 decl = build_decl (input_location,
4430 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4431 DECL_ARTIFICIAL (decl) = 1;
4432 DECL_IGNORED_P (decl) = 1;
4433 DECL_FIELD_CONTEXT (decl) = t;
4434 if (CLASSTYPE_AS_BASE (basetype))
4436 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4437 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4438 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4439 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4440 DECL_MODE (decl) = TYPE_MODE (basetype);
4441 DECL_FIELD_IS_BASE (decl) = 1;
4443 /* Try to place the field. It may take more than one try if we
4444 have a hard time placing the field without putting two
4445 objects of the same type at the same address. */
4446 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4447 /* Add the new FIELD_DECL to the list of fields for T. */
4448 DECL_CHAIN (decl) = *next_field;
4449 *next_field = decl;
4450 next_field = &DECL_CHAIN (decl);
4453 else
4455 tree eoc;
4456 bool atend;
4458 /* On some platforms (ARM), even empty classes will not be
4459 byte-aligned. */
4460 eoc = round_up_loc (input_location,
4461 rli_size_unit_so_far (rli),
4462 CLASSTYPE_ALIGN_UNIT (basetype));
4463 atend = layout_empty_base (rli, binfo, eoc, offsets);
4464 /* A nearly-empty class "has no proper base class that is empty,
4465 not morally virtual, and at an offset other than zero." */
4466 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4468 if (atend)
4469 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4470 /* The check above (used in G++ 3.2) is insufficient because
4471 an empty class placed at offset zero might itself have an
4472 empty base at a nonzero offset. */
4473 else if (walk_subobject_offsets (basetype,
4474 empty_base_at_nonzero_offset_p,
4475 size_zero_node,
4476 /*offsets=*/NULL,
4477 /*max_offset=*/NULL_TREE,
4478 /*vbases_p=*/true))
4479 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4482 /* We do not create a FIELD_DECL for empty base classes because
4483 it might overlap some other field. We want to be able to
4484 create CONSTRUCTORs for the class by iterating over the
4485 FIELD_DECLs, and the back end does not handle overlapping
4486 FIELD_DECLs. */
4488 /* An empty virtual base causes a class to be non-empty
4489 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4490 here because that was already done when the virtual table
4491 pointer was created. */
4494 /* Record the offsets of BINFO and its base subobjects. */
4495 record_subobject_offsets (binfo,
4496 BINFO_OFFSET (binfo),
4497 offsets,
4498 /*is_data_member=*/false);
4500 return next_field;
4503 /* Layout all of the non-virtual base classes. Record empty
4504 subobjects in OFFSETS. T is the most derived type. Return nonzero
4505 if the type cannot be nearly empty. The fields created
4506 corresponding to the base classes will be inserted at
4507 *NEXT_FIELD. */
4509 static void
4510 build_base_fields (record_layout_info rli,
4511 splay_tree offsets, tree *next_field)
4513 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4514 subobjects. */
4515 tree t = rli->t;
4516 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4517 int i;
4519 /* The primary base class is always allocated first. */
4520 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4521 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4522 offsets, next_field);
4524 /* Now allocate the rest of the bases. */
4525 for (i = 0; i < n_baseclasses; ++i)
4527 tree base_binfo;
4529 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4531 /* The primary base was already allocated above, so we don't
4532 need to allocate it again here. */
4533 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4534 continue;
4536 /* Virtual bases are added at the end (a primary virtual base
4537 will have already been added). */
4538 if (BINFO_VIRTUAL_P (base_binfo))
4539 continue;
4541 next_field = build_base_field (rli, base_binfo,
4542 offsets, next_field);
4546 /* Go through the TYPE_METHODS of T issuing any appropriate
4547 diagnostics, figuring out which methods override which other
4548 methods, and so forth. */
4550 static void
4551 check_methods (tree t)
4553 tree x;
4555 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4557 check_for_override (x, t);
4558 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4559 error ("initializer specified for non-virtual method %q+D", x);
4560 /* The name of the field is the original field name
4561 Save this in auxiliary field for later overloading. */
4562 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4564 TYPE_POLYMORPHIC_P (t) = 1;
4565 if (DECL_PURE_VIRTUAL_P (x))
4566 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4568 /* All user-provided destructors are non-trivial.
4569 Constructors and assignment ops are handled in
4570 grok_special_member_properties. */
4571 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4572 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4576 /* FN is a constructor or destructor. Clone the declaration to create
4577 a specialized in-charge or not-in-charge version, as indicated by
4578 NAME. */
4580 static tree
4581 build_clone (tree fn, tree name)
4583 tree parms;
4584 tree clone;
4586 /* Copy the function. */
4587 clone = copy_decl (fn);
4588 /* Reset the function name. */
4589 DECL_NAME (clone) = name;
4590 /* Remember where this function came from. */
4591 DECL_ABSTRACT_ORIGIN (clone) = fn;
4592 /* Make it easy to find the CLONE given the FN. */
4593 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4594 DECL_CHAIN (fn) = clone;
4596 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4597 if (TREE_CODE (clone) == TEMPLATE_DECL)
4599 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4600 DECL_TEMPLATE_RESULT (clone) = result;
4601 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4602 DECL_TI_TEMPLATE (result) = clone;
4603 TREE_TYPE (clone) = TREE_TYPE (result);
4604 return clone;
4606 else
4608 // Clone constraints.
4609 if (flag_concepts)
4610 if (tree ci = get_constraints (fn))
4611 set_constraints (clone, copy_node (ci));
4615 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4616 DECL_CLONED_FUNCTION (clone) = fn;
4617 /* There's no pending inline data for this function. */
4618 DECL_PENDING_INLINE_INFO (clone) = NULL;
4619 DECL_PENDING_INLINE_P (clone) = 0;
4621 /* The base-class destructor is not virtual. */
4622 if (name == base_dtor_identifier)
4624 DECL_VIRTUAL_P (clone) = 0;
4625 if (TREE_CODE (clone) != TEMPLATE_DECL)
4626 DECL_VINDEX (clone) = NULL_TREE;
4629 /* If there was an in-charge parameter, drop it from the function
4630 type. */
4631 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4633 tree basetype;
4634 tree parmtypes;
4635 tree exceptions;
4637 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4638 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4639 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4640 /* Skip the `this' parameter. */
4641 parmtypes = TREE_CHAIN (parmtypes);
4642 /* Skip the in-charge parameter. */
4643 parmtypes = TREE_CHAIN (parmtypes);
4644 /* And the VTT parm, in a complete [cd]tor. */
4645 if (DECL_HAS_VTT_PARM_P (fn)
4646 && ! DECL_NEEDS_VTT_PARM_P (clone))
4647 parmtypes = TREE_CHAIN (parmtypes);
4648 /* If this is subobject constructor or destructor, add the vtt
4649 parameter. */
4650 TREE_TYPE (clone)
4651 = build_method_type_directly (basetype,
4652 TREE_TYPE (TREE_TYPE (clone)),
4653 parmtypes);
4654 if (exceptions)
4655 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4656 exceptions);
4657 TREE_TYPE (clone)
4658 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4659 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4662 /* Copy the function parameters. */
4663 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4664 /* Remove the in-charge parameter. */
4665 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4667 DECL_CHAIN (DECL_ARGUMENTS (clone))
4668 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4669 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4671 /* And the VTT parm, in a complete [cd]tor. */
4672 if (DECL_HAS_VTT_PARM_P (fn))
4674 if (DECL_NEEDS_VTT_PARM_P (clone))
4675 DECL_HAS_VTT_PARM_P (clone) = 1;
4676 else
4678 DECL_CHAIN (DECL_ARGUMENTS (clone))
4679 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4680 DECL_HAS_VTT_PARM_P (clone) = 0;
4684 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4686 DECL_CONTEXT (parms) = clone;
4687 cxx_dup_lang_specific_decl (parms);
4690 /* Create the RTL for this function. */
4691 SET_DECL_RTL (clone, NULL);
4692 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4694 if (pch_file)
4695 note_decl_for_pch (clone);
4697 return clone;
4700 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4701 not invoke this function directly.
4703 For a non-thunk function, returns the address of the slot for storing
4704 the function it is a clone of. Otherwise returns NULL_TREE.
4706 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4707 cloned_function is unset. This is to support the separate
4708 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4709 on a template makes sense, but not the former. */
4711 tree *
4712 decl_cloned_function_p (const_tree decl, bool just_testing)
4714 tree *ptr;
4715 if (just_testing)
4716 decl = STRIP_TEMPLATE (decl);
4718 if (TREE_CODE (decl) != FUNCTION_DECL
4719 || !DECL_LANG_SPECIFIC (decl)
4720 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4722 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4723 if (!just_testing)
4724 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4725 else
4726 #endif
4727 return NULL;
4730 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4731 if (just_testing && *ptr == NULL_TREE)
4732 return NULL;
4733 else
4734 return ptr;
4737 /* Produce declarations for all appropriate clones of FN. If
4738 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4739 CLASTYPE_METHOD_VEC as well. */
4741 void
4742 clone_function_decl (tree fn, int update_method_vec_p)
4744 tree clone;
4746 /* Avoid inappropriate cloning. */
4747 if (DECL_CHAIN (fn)
4748 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4749 return;
4751 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4753 /* For each constructor, we need two variants: an in-charge version
4754 and a not-in-charge version. */
4755 clone = build_clone (fn, complete_ctor_identifier);
4756 if (update_method_vec_p)
4757 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4758 clone = build_clone (fn, base_ctor_identifier);
4759 if (update_method_vec_p)
4760 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4762 else
4764 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4766 /* For each destructor, we need three variants: an in-charge
4767 version, a not-in-charge version, and an in-charge deleting
4768 version. We clone the deleting version first because that
4769 means it will go second on the TYPE_METHODS list -- and that
4770 corresponds to the correct layout order in the virtual
4771 function table.
4773 For a non-virtual destructor, we do not build a deleting
4774 destructor. */
4775 if (DECL_VIRTUAL_P (fn))
4777 clone = build_clone (fn, deleting_dtor_identifier);
4778 if (update_method_vec_p)
4779 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4781 clone = build_clone (fn, complete_dtor_identifier);
4782 if (update_method_vec_p)
4783 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4784 clone = build_clone (fn, base_dtor_identifier);
4785 if (update_method_vec_p)
4786 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4789 /* Note that this is an abstract function that is never emitted. */
4790 DECL_ABSTRACT_P (fn) = true;
4793 /* DECL is an in charge constructor, which is being defined. This will
4794 have had an in class declaration, from whence clones were
4795 declared. An out-of-class definition can specify additional default
4796 arguments. As it is the clones that are involved in overload
4797 resolution, we must propagate the information from the DECL to its
4798 clones. */
4800 void
4801 adjust_clone_args (tree decl)
4803 tree clone;
4805 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4806 clone = DECL_CHAIN (clone))
4808 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4809 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4810 tree decl_parms, clone_parms;
4812 clone_parms = orig_clone_parms;
4814 /* Skip the 'this' parameter. */
4815 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4816 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4818 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4819 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4820 if (DECL_HAS_VTT_PARM_P (decl))
4821 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4823 clone_parms = orig_clone_parms;
4824 if (DECL_HAS_VTT_PARM_P (clone))
4825 clone_parms = TREE_CHAIN (clone_parms);
4827 for (decl_parms = orig_decl_parms; decl_parms;
4828 decl_parms = TREE_CHAIN (decl_parms),
4829 clone_parms = TREE_CHAIN (clone_parms))
4831 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4832 TREE_TYPE (clone_parms)));
4834 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4836 /* A default parameter has been added. Adjust the
4837 clone's parameters. */
4838 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4839 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4840 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4841 tree type;
4843 clone_parms = orig_decl_parms;
4845 if (DECL_HAS_VTT_PARM_P (clone))
4847 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4848 TREE_VALUE (orig_clone_parms),
4849 clone_parms);
4850 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4852 type = build_method_type_directly (basetype,
4853 TREE_TYPE (TREE_TYPE (clone)),
4854 clone_parms);
4855 if (exceptions)
4856 type = build_exception_variant (type, exceptions);
4857 if (attrs)
4858 type = cp_build_type_attribute_variant (type, attrs);
4859 TREE_TYPE (clone) = type;
4861 clone_parms = NULL_TREE;
4862 break;
4865 gcc_assert (!clone_parms);
4869 /* For each of the constructors and destructors in T, create an
4870 in-charge and not-in-charge variant. */
4872 static void
4873 clone_constructors_and_destructors (tree t)
4875 tree fns;
4877 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4878 out now. */
4879 if (!CLASSTYPE_METHOD_VEC (t))
4880 return;
4882 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4883 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4884 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4885 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4888 /* Deduce noexcept for a destructor DTOR. */
4890 void
4891 deduce_noexcept_on_destructor (tree dtor)
4893 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4895 tree eh_spec = unevaluated_noexcept_spec ();
4896 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4900 /* For each destructor in T, deduce noexcept:
4902 12.4/3: A declaration of a destructor that does not have an
4903 exception-specification is implicitly considered to have the
4904 same exception-specification as an implicit declaration (15.4). */
4906 static void
4907 deduce_noexcept_on_destructors (tree t)
4909 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4910 out now. */
4911 if (!CLASSTYPE_METHOD_VEC (t))
4912 return;
4914 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4915 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4918 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4919 of TYPE for virtual functions which FNDECL overrides. Return a
4920 mask of the tm attributes found therein. */
4922 static int
4923 look_for_tm_attr_overrides (tree type, tree fndecl)
4925 tree binfo = TYPE_BINFO (type);
4926 tree base_binfo;
4927 int ix, found = 0;
4929 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4931 tree o, basetype = BINFO_TYPE (base_binfo);
4933 if (!TYPE_POLYMORPHIC_P (basetype))
4934 continue;
4936 o = look_for_overrides_here (basetype, fndecl);
4937 if (o)
4938 found |= tm_attr_to_mask (find_tm_attribute
4939 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4940 else
4941 found |= look_for_tm_attr_overrides (basetype, fndecl);
4944 return found;
4947 /* Subroutine of set_method_tm_attributes. Handle the checks and
4948 inheritance for one virtual method FNDECL. */
4950 static void
4951 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4953 tree tm_attr;
4954 int found, have;
4956 found = look_for_tm_attr_overrides (type, fndecl);
4958 /* If FNDECL doesn't actually override anything (i.e. T is the
4959 class that first declares FNDECL virtual), then we're done. */
4960 if (found == 0)
4961 return;
4963 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4964 have = tm_attr_to_mask (tm_attr);
4966 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4967 tm_pure must match exactly, otherwise no weakening of
4968 tm_safe > tm_callable > nothing. */
4969 /* ??? The tm_pure attribute didn't make the transition to the
4970 multivendor language spec. */
4971 if (have == TM_ATTR_PURE)
4973 if (found != TM_ATTR_PURE)
4975 found &= -found;
4976 goto err_override;
4979 /* If the overridden function is tm_pure, then FNDECL must be. */
4980 else if (found == TM_ATTR_PURE && tm_attr)
4981 goto err_override;
4982 /* Look for base class combinations that cannot be satisfied. */
4983 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4985 found &= ~TM_ATTR_PURE;
4986 found &= -found;
4987 error_at (DECL_SOURCE_LOCATION (fndecl),
4988 "method overrides both %<transaction_pure%> and %qE methods",
4989 tm_mask_to_attr (found));
4991 /* If FNDECL did not declare an attribute, then inherit the most
4992 restrictive one. */
4993 else if (tm_attr == NULL)
4995 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4997 /* Otherwise validate that we're not weaker than a function
4998 that is being overridden. */
4999 else
5001 found &= -found;
5002 if (found <= TM_ATTR_CALLABLE && have > found)
5003 goto err_override;
5005 return;
5007 err_override:
5008 error_at (DECL_SOURCE_LOCATION (fndecl),
5009 "method declared %qE overriding %qE method",
5010 tm_attr, tm_mask_to_attr (found));
5013 /* For each of the methods in T, propagate a class-level tm attribute. */
5015 static void
5016 set_method_tm_attributes (tree t)
5018 tree class_tm_attr, fndecl;
5020 /* Don't bother collecting tm attributes if transactional memory
5021 support is not enabled. */
5022 if (!flag_tm)
5023 return;
5025 /* Process virtual methods first, as they inherit directly from the
5026 base virtual function and also require validation of new attributes. */
5027 if (TYPE_CONTAINS_VPTR_P (t))
5029 tree vchain;
5030 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5031 vchain = TREE_CHAIN (vchain))
5033 fndecl = BV_FN (vchain);
5034 if (DECL_THUNK_P (fndecl))
5035 fndecl = THUNK_TARGET (fndecl);
5036 set_one_vmethod_tm_attributes (t, fndecl);
5040 /* If the class doesn't have an attribute, nothing more to do. */
5041 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5042 if (class_tm_attr == NULL)
5043 return;
5045 /* Any method that does not yet have a tm attribute inherits
5046 the one from the class. */
5047 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5049 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5050 apply_tm_attr (fndecl, class_tm_attr);
5054 /* Returns true iff class T has a user-defined constructor other than
5055 the default constructor. */
5057 bool
5058 type_has_user_nondefault_constructor (tree t)
5060 tree fns;
5062 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5063 return false;
5065 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5067 tree fn = OVL_CURRENT (fns);
5068 if (!DECL_ARTIFICIAL (fn)
5069 && (TREE_CODE (fn) == TEMPLATE_DECL
5070 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5071 != NULL_TREE)))
5072 return true;
5075 return false;
5078 /* Returns the defaulted constructor if T has one. Otherwise, returns
5079 NULL_TREE. */
5081 tree
5082 in_class_defaulted_default_constructor (tree t)
5084 tree fns, args;
5086 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5087 return NULL_TREE;
5089 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5091 tree fn = OVL_CURRENT (fns);
5093 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5095 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5096 while (args && TREE_PURPOSE (args))
5097 args = TREE_CHAIN (args);
5098 if (!args || args == void_list_node)
5099 return fn;
5103 return NULL_TREE;
5106 /* Returns true iff FN is a user-provided function, i.e. user-declared
5107 and not defaulted at its first declaration; or explicit, private,
5108 protected, or non-const. */
5110 bool
5111 user_provided_p (tree fn)
5113 if (TREE_CODE (fn) == TEMPLATE_DECL)
5114 return true;
5115 else
5116 return (!DECL_ARTIFICIAL (fn)
5117 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5118 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5121 /* Returns true iff class T has a user-provided constructor. */
5123 bool
5124 type_has_user_provided_constructor (tree t)
5126 tree fns;
5128 if (!CLASS_TYPE_P (t))
5129 return false;
5131 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5132 return false;
5134 /* This can happen in error cases; avoid crashing. */
5135 if (!CLASSTYPE_METHOD_VEC (t))
5136 return false;
5138 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5139 if (user_provided_p (OVL_CURRENT (fns)))
5140 return true;
5142 return false;
5145 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5146 declared or explicitly defaulted in the class body) default
5147 constructor. */
5149 bool
5150 type_has_non_user_provided_default_constructor (tree t)
5152 tree fns;
5154 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5155 return false;
5156 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5157 return true;
5159 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5161 tree fn = OVL_CURRENT (fns);
5162 if (TREE_CODE (fn) == FUNCTION_DECL
5163 && !user_provided_p (fn)
5164 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5165 return true;
5168 return false;
5171 /* TYPE is being used as a virtual base, and has a non-trivial move
5172 assignment. Return true if this is due to there being a user-provided
5173 move assignment in TYPE or one of its subobjects; if there isn't, then
5174 multiple move assignment can't cause any harm. */
5176 bool
5177 vbase_has_user_provided_move_assign (tree type)
5179 /* Does the type itself have a user-provided move assignment operator? */
5180 for (tree fns
5181 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5182 fns; fns = OVL_NEXT (fns))
5184 tree fn = OVL_CURRENT (fns);
5185 if (move_fn_p (fn) && user_provided_p (fn))
5186 return true;
5189 /* Do any of its bases? */
5190 tree binfo = TYPE_BINFO (type);
5191 tree base_binfo;
5192 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5193 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5194 return true;
5196 /* Or non-static data members? */
5197 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5199 if (TREE_CODE (field) == FIELD_DECL
5200 && CLASS_TYPE_P (TREE_TYPE (field))
5201 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5202 return true;
5205 /* Seems not. */
5206 return false;
5209 /* If default-initialization leaves part of TYPE uninitialized, returns
5210 a DECL for the field or TYPE itself (DR 253). */
5212 tree
5213 default_init_uninitialized_part (tree type)
5215 tree t, r, binfo;
5216 int i;
5218 type = strip_array_types (type);
5219 if (!CLASS_TYPE_P (type))
5220 return type;
5221 if (!type_has_non_user_provided_default_constructor (type))
5222 return NULL_TREE;
5223 for (binfo = TYPE_BINFO (type), i = 0;
5224 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5226 r = default_init_uninitialized_part (BINFO_TYPE (t));
5227 if (r)
5228 return r;
5230 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5231 if (TREE_CODE (t) == FIELD_DECL
5232 && !DECL_ARTIFICIAL (t)
5233 && !DECL_INITIAL (t))
5235 r = default_init_uninitialized_part (TREE_TYPE (t));
5236 if (r)
5237 return DECL_P (r) ? r : t;
5240 return NULL_TREE;
5243 /* Returns true iff for class T, a trivial synthesized default constructor
5244 would be constexpr. */
5246 bool
5247 trivial_default_constructor_is_constexpr (tree t)
5249 /* A defaulted trivial default constructor is constexpr
5250 if there is nothing to initialize. */
5251 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5252 return is_really_empty_class (t);
5255 /* Returns true iff class T has a constexpr default constructor. */
5257 bool
5258 type_has_constexpr_default_constructor (tree t)
5260 tree fns;
5262 if (!CLASS_TYPE_P (t))
5264 /* The caller should have stripped an enclosing array. */
5265 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5266 return false;
5268 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5270 if (!TYPE_HAS_COMPLEX_DFLT (t))
5271 return trivial_default_constructor_is_constexpr (t);
5272 /* Non-trivial, we need to check subobject constructors. */
5273 lazily_declare_fn (sfk_constructor, t);
5275 fns = locate_ctor (t);
5276 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5279 /* Returns true iff class TYPE has a virtual destructor. */
5281 bool
5282 type_has_virtual_destructor (tree type)
5284 tree dtor;
5286 if (!CLASS_TYPE_P (type))
5287 return false;
5289 gcc_assert (COMPLETE_TYPE_P (type));
5290 dtor = CLASSTYPE_DESTRUCTORS (type);
5291 return (dtor && DECL_VIRTUAL_P (dtor));
5294 /* Returns true iff class T has a move constructor. */
5296 bool
5297 type_has_move_constructor (tree t)
5299 tree fns;
5301 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5303 gcc_assert (COMPLETE_TYPE_P (t));
5304 lazily_declare_fn (sfk_move_constructor, t);
5307 if (!CLASSTYPE_METHOD_VEC (t))
5308 return false;
5310 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5311 if (move_fn_p (OVL_CURRENT (fns)))
5312 return true;
5314 return false;
5317 /* Returns true iff class T has a move assignment operator. */
5319 bool
5320 type_has_move_assign (tree t)
5322 tree fns;
5324 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5326 gcc_assert (COMPLETE_TYPE_P (t));
5327 lazily_declare_fn (sfk_move_assignment, t);
5330 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5331 fns; fns = OVL_NEXT (fns))
5332 if (move_fn_p (OVL_CURRENT (fns)))
5333 return true;
5335 return false;
5338 /* Returns true iff class T has a move constructor that was explicitly
5339 declared in the class body. Note that this is different from
5340 "user-provided", which doesn't include functions that are defaulted in
5341 the class. */
5343 bool
5344 type_has_user_declared_move_constructor (tree t)
5346 tree fns;
5348 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5349 return false;
5351 if (!CLASSTYPE_METHOD_VEC (t))
5352 return false;
5354 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5356 tree fn = OVL_CURRENT (fns);
5357 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5358 return true;
5361 return false;
5364 /* Returns true iff class T has a move assignment operator that was
5365 explicitly declared in the class body. */
5367 bool
5368 type_has_user_declared_move_assign (tree t)
5370 tree fns;
5372 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5373 return false;
5375 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5376 fns; fns = OVL_NEXT (fns))
5378 tree fn = OVL_CURRENT (fns);
5379 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5380 return true;
5383 return false;
5386 /* Nonzero if we need to build up a constructor call when initializing an
5387 object of this class, either because it has a user-declared constructor
5388 or because it doesn't have a default constructor (so we need to give an
5389 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5390 what you care about is whether or not an object can be produced by a
5391 constructor (e.g. so we don't set TREE_READONLY on const variables of
5392 such type); use this function when what you care about is whether or not
5393 to try to call a constructor to create an object. The latter case is
5394 the former plus some cases of constructors that cannot be called. */
5396 bool
5397 type_build_ctor_call (tree t)
5399 tree inner;
5400 if (TYPE_NEEDS_CONSTRUCTING (t))
5401 return true;
5402 inner = strip_array_types (t);
5403 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5404 return false;
5405 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5406 return true;
5407 if (cxx_dialect < cxx11)
5408 return false;
5409 /* A user-declared constructor might be private, and a constructor might
5410 be trivial but deleted. */
5411 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5412 fns; fns = OVL_NEXT (fns))
5414 tree fn = OVL_CURRENT (fns);
5415 if (!DECL_ARTIFICIAL (fn)
5416 || DECL_DELETED_FN (fn))
5417 return true;
5419 return false;
5422 /* Like type_build_ctor_call, but for destructors. */
5424 bool
5425 type_build_dtor_call (tree t)
5427 tree inner;
5428 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5429 return true;
5430 inner = strip_array_types (t);
5431 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5432 || !COMPLETE_TYPE_P (inner))
5433 return false;
5434 if (cxx_dialect < cxx11)
5435 return false;
5436 /* A user-declared destructor might be private, and a destructor might
5437 be trivial but deleted. */
5438 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5439 fns; fns = OVL_NEXT (fns))
5441 tree fn = OVL_CURRENT (fns);
5442 if (!DECL_ARTIFICIAL (fn)
5443 || DECL_DELETED_FN (fn))
5444 return true;
5446 return false;
5449 /* Remove all zero-width bit-fields from T. */
5451 static void
5452 remove_zero_width_bit_fields (tree t)
5454 tree *fieldsp;
5456 fieldsp = &TYPE_FIELDS (t);
5457 while (*fieldsp)
5459 if (TREE_CODE (*fieldsp) == FIELD_DECL
5460 && DECL_C_BIT_FIELD (*fieldsp)
5461 /* We should not be confused by the fact that grokbitfield
5462 temporarily sets the width of the bit field into
5463 DECL_INITIAL (*fieldsp).
5464 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5465 to that width. */
5466 && (DECL_SIZE (*fieldsp) == NULL_TREE
5467 || integer_zerop (DECL_SIZE (*fieldsp))))
5468 *fieldsp = DECL_CHAIN (*fieldsp);
5469 else
5470 fieldsp = &DECL_CHAIN (*fieldsp);
5474 /* Returns TRUE iff we need a cookie when dynamically allocating an
5475 array whose elements have the indicated class TYPE. */
5477 static bool
5478 type_requires_array_cookie (tree type)
5480 tree fns;
5481 bool has_two_argument_delete_p = false;
5483 gcc_assert (CLASS_TYPE_P (type));
5485 /* If there's a non-trivial destructor, we need a cookie. In order
5486 to iterate through the array calling the destructor for each
5487 element, we'll have to know how many elements there are. */
5488 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5489 return true;
5491 /* If the usual deallocation function is a two-argument whose second
5492 argument is of type `size_t', then we have to pass the size of
5493 the array to the deallocation function, so we will need to store
5494 a cookie. */
5495 fns = lookup_fnfields (TYPE_BINFO (type),
5496 ansi_opname (VEC_DELETE_EXPR),
5497 /*protect=*/0);
5498 /* If there are no `operator []' members, or the lookup is
5499 ambiguous, then we don't need a cookie. */
5500 if (!fns || fns == error_mark_node)
5501 return false;
5502 /* Loop through all of the functions. */
5503 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5505 tree fn;
5506 tree second_parm;
5508 /* Select the current function. */
5509 fn = OVL_CURRENT (fns);
5510 /* See if this function is a one-argument delete function. If
5511 it is, then it will be the usual deallocation function. */
5512 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5513 if (second_parm == void_list_node)
5514 return false;
5515 /* Do not consider this function if its second argument is an
5516 ellipsis. */
5517 if (!second_parm)
5518 continue;
5519 /* Otherwise, if we have a two-argument function and the second
5520 argument is `size_t', it will be the usual deallocation
5521 function -- unless there is one-argument function, too. */
5522 if (TREE_CHAIN (second_parm) == void_list_node
5523 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5524 has_two_argument_delete_p = true;
5527 return has_two_argument_delete_p;
5530 /* Finish computing the `literal type' property of class type T.
5532 At this point, we have already processed base classes and
5533 non-static data members. We need to check whether the copy
5534 constructor is trivial, the destructor is trivial, and there
5535 is a trivial default constructor or at least one constexpr
5536 constructor other than the copy constructor. */
5538 static void
5539 finalize_literal_type_property (tree t)
5541 tree fn;
5543 if (cxx_dialect < cxx11
5544 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5545 CLASSTYPE_LITERAL_P (t) = false;
5546 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5547 && CLASSTYPE_NON_AGGREGATE (t)
5548 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5549 CLASSTYPE_LITERAL_P (t) = false;
5551 if (!CLASSTYPE_LITERAL_P (t))
5552 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5553 if (DECL_DECLARED_CONSTEXPR_P (fn)
5554 && TREE_CODE (fn) != TEMPLATE_DECL
5555 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5556 && !DECL_CONSTRUCTOR_P (fn))
5558 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5559 if (!DECL_GENERATED_P (fn))
5561 error ("enclosing class of constexpr non-static member "
5562 "function %q+#D is not a literal type", fn);
5563 explain_non_literal_class (t);
5568 /* T is a non-literal type used in a context which requires a constant
5569 expression. Explain why it isn't literal. */
5571 void
5572 explain_non_literal_class (tree t)
5574 static hash_set<tree> *diagnosed;
5576 if (!CLASS_TYPE_P (t))
5577 return;
5578 t = TYPE_MAIN_VARIANT (t);
5580 if (diagnosed == NULL)
5581 diagnosed = new hash_set<tree>;
5582 if (diagnosed->add (t))
5583 /* Already explained. */
5584 return;
5586 inform (0, "%q+T is not literal because:", t);
5587 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5588 inform (0, " %q+T has a non-trivial destructor", t);
5589 else if (CLASSTYPE_NON_AGGREGATE (t)
5590 && !TYPE_HAS_TRIVIAL_DFLT (t)
5591 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5593 inform (0, " %q+T is not an aggregate, does not have a trivial "
5594 "default constructor, and has no constexpr constructor that "
5595 "is not a copy or move constructor", t);
5596 if (type_has_non_user_provided_default_constructor (t))
5598 /* Note that we can't simply call locate_ctor because when the
5599 constructor is deleted it just returns NULL_TREE. */
5600 tree fns;
5601 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5603 tree fn = OVL_CURRENT (fns);
5604 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5606 parms = skip_artificial_parms_for (fn, parms);
5608 if (sufficient_parms_p (parms))
5610 if (DECL_DELETED_FN (fn))
5611 maybe_explain_implicit_delete (fn);
5612 else
5613 explain_invalid_constexpr_fn (fn);
5614 break;
5619 else
5621 tree binfo, base_binfo, field; int i;
5622 for (binfo = TYPE_BINFO (t), i = 0;
5623 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5625 tree basetype = TREE_TYPE (base_binfo);
5626 if (!CLASSTYPE_LITERAL_P (basetype))
5628 inform (0, " base class %qT of %q+T is non-literal",
5629 basetype, t);
5630 explain_non_literal_class (basetype);
5631 return;
5634 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5636 tree ftype;
5637 if (TREE_CODE (field) != FIELD_DECL)
5638 continue;
5639 ftype = TREE_TYPE (field);
5640 if (!literal_type_p (ftype))
5642 inform (DECL_SOURCE_LOCATION (field),
5643 " non-static data member %qD has non-literal type",
5644 field);
5645 if (CLASS_TYPE_P (ftype))
5646 explain_non_literal_class (ftype);
5648 if (CP_TYPE_VOLATILE_P (ftype))
5649 inform (DECL_SOURCE_LOCATION (field),
5650 " non-static data member %qD has volatile type", field);
5655 /* Check the validity of the bases and members declared in T. Add any
5656 implicitly-generated functions (like copy-constructors and
5657 assignment operators). Compute various flag bits (like
5658 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5659 level: i.e., independently of the ABI in use. */
5661 static void
5662 check_bases_and_members (tree t)
5664 /* Nonzero if the implicitly generated copy constructor should take
5665 a non-const reference argument. */
5666 int cant_have_const_ctor;
5667 /* Nonzero if the implicitly generated assignment operator
5668 should take a non-const reference argument. */
5669 int no_const_asn_ref;
5670 tree access_decls;
5671 bool saved_complex_asn_ref;
5672 bool saved_nontrivial_dtor;
5673 tree fn;
5675 /* By default, we use const reference arguments and generate default
5676 constructors. */
5677 cant_have_const_ctor = 0;
5678 no_const_asn_ref = 0;
5680 /* Check all the base-classes. */
5681 check_bases (t, &cant_have_const_ctor,
5682 &no_const_asn_ref);
5684 /* Deduce noexcept on destructors. This needs to happen after we've set
5685 triviality flags appropriately for our bases. */
5686 if (cxx_dialect >= cxx11)
5687 deduce_noexcept_on_destructors (t);
5689 /* Check all the method declarations. */
5690 check_methods (t);
5692 /* Save the initial values of these flags which only indicate whether
5693 or not the class has user-provided functions. As we analyze the
5694 bases and members we can set these flags for other reasons. */
5695 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5696 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5698 /* Check all the data member declarations. We cannot call
5699 check_field_decls until we have called check_bases check_methods,
5700 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5701 being set appropriately. */
5702 check_field_decls (t, &access_decls,
5703 &cant_have_const_ctor,
5704 &no_const_asn_ref);
5706 /* A nearly-empty class has to be vptr-containing; a nearly empty
5707 class contains just a vptr. */
5708 if (!TYPE_CONTAINS_VPTR_P (t))
5709 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5711 /* Do some bookkeeping that will guide the generation of implicitly
5712 declared member functions. */
5713 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5714 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5715 /* We need to call a constructor for this class if it has a
5716 user-provided constructor, or if the default constructor is going
5717 to initialize the vptr. (This is not an if-and-only-if;
5718 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5719 themselves need constructing.) */
5720 TYPE_NEEDS_CONSTRUCTING (t)
5721 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5722 /* [dcl.init.aggr]
5724 An aggregate is an array or a class with no user-provided
5725 constructors ... and no virtual functions.
5727 Again, other conditions for being an aggregate are checked
5728 elsewhere. */
5729 CLASSTYPE_NON_AGGREGATE (t)
5730 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5731 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5732 retain the old definition internally for ABI reasons. */
5733 CLASSTYPE_NON_LAYOUT_POD_P (t)
5734 |= (CLASSTYPE_NON_AGGREGATE (t)
5735 || saved_nontrivial_dtor || saved_complex_asn_ref);
5736 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5737 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5738 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5739 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5741 /* If the only explicitly declared default constructor is user-provided,
5742 set TYPE_HAS_COMPLEX_DFLT. */
5743 if (!TYPE_HAS_COMPLEX_DFLT (t)
5744 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5745 && !type_has_non_user_provided_default_constructor (t))
5746 TYPE_HAS_COMPLEX_DFLT (t) = true;
5748 /* Warn if a public base of a polymorphic type has an accessible
5749 non-virtual destructor. It is only now that we know the class is
5750 polymorphic. Although a polymorphic base will have a already
5751 been diagnosed during its definition, we warn on use too. */
5752 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5754 tree binfo = TYPE_BINFO (t);
5755 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5756 tree base_binfo;
5757 unsigned i;
5759 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5761 tree basetype = TREE_TYPE (base_binfo);
5763 if ((*accesses)[i] == access_public_node
5764 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5765 && accessible_nvdtor_p (basetype))
5766 warning (OPT_Wnon_virtual_dtor,
5767 "base class %q#T has accessible non-virtual destructor",
5768 basetype);
5772 /* If the class has no user-declared constructor, but does have
5773 non-static const or reference data members that can never be
5774 initialized, issue a warning. */
5775 if (warn_uninitialized
5776 /* Classes with user-declared constructors are presumed to
5777 initialize these members. */
5778 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5779 /* Aggregates can be initialized with brace-enclosed
5780 initializers. */
5781 && CLASSTYPE_NON_AGGREGATE (t))
5783 tree field;
5785 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5787 tree type;
5789 if (TREE_CODE (field) != FIELD_DECL
5790 || DECL_INITIAL (field) != NULL_TREE)
5791 continue;
5793 type = TREE_TYPE (field);
5794 if (TREE_CODE (type) == REFERENCE_TYPE)
5795 warning_at (DECL_SOURCE_LOCATION (field),
5796 OPT_Wuninitialized, "non-static reference %q#D "
5797 "in class without a constructor", field);
5798 else if (CP_TYPE_CONST_P (type)
5799 && (!CLASS_TYPE_P (type)
5800 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5801 warning_at (DECL_SOURCE_LOCATION (field),
5802 OPT_Wuninitialized, "non-static const member %q#D "
5803 "in class without a constructor", field);
5807 /* Synthesize any needed methods. */
5808 add_implicitly_declared_members (t, &access_decls,
5809 cant_have_const_ctor,
5810 no_const_asn_ref);
5812 /* Check defaulted declarations here so we have cant_have_const_ctor
5813 and don't need to worry about clones. */
5814 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5815 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5817 int copy = copy_fn_p (fn);
5818 if (copy > 0)
5820 bool imp_const_p
5821 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5822 : !no_const_asn_ref);
5823 bool fn_const_p = (copy == 2);
5825 if (fn_const_p && !imp_const_p)
5826 /* If the function is defaulted outside the class, we just
5827 give the synthesis error. */
5828 error ("%q+D declared to take const reference, but implicit "
5829 "declaration would take non-const", fn);
5831 defaulted_late_check (fn);
5834 if (LAMBDA_TYPE_P (t))
5836 /* "This class type is not an aggregate." */
5837 CLASSTYPE_NON_AGGREGATE (t) = 1;
5840 /* Compute the 'literal type' property before we
5841 do anything with non-static member functions. */
5842 finalize_literal_type_property (t);
5844 /* Create the in-charge and not-in-charge variants of constructors
5845 and destructors. */
5846 clone_constructors_and_destructors (t);
5848 /* Process the using-declarations. */
5849 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5850 handle_using_decl (TREE_VALUE (access_decls), t);
5852 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5853 finish_struct_methods (t);
5855 /* Figure out whether or not we will need a cookie when dynamically
5856 allocating an array of this type. */
5857 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5858 = type_requires_array_cookie (t);
5861 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5862 accordingly. If a new vfield was created (because T doesn't have a
5863 primary base class), then the newly created field is returned. It
5864 is not added to the TYPE_FIELDS list; it is the caller's
5865 responsibility to do that. Accumulate declared virtual functions
5866 on VIRTUALS_P. */
5868 static tree
5869 create_vtable_ptr (tree t, tree* virtuals_p)
5871 tree fn;
5873 /* Collect the virtual functions declared in T. */
5874 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5875 if (TREE_CODE (fn) == FUNCTION_DECL
5876 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5877 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5879 tree new_virtual = make_node (TREE_LIST);
5881 BV_FN (new_virtual) = fn;
5882 BV_DELTA (new_virtual) = integer_zero_node;
5883 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5885 TREE_CHAIN (new_virtual) = *virtuals_p;
5886 *virtuals_p = new_virtual;
5889 /* If we couldn't find an appropriate base class, create a new field
5890 here. Even if there weren't any new virtual functions, we might need a
5891 new virtual function table if we're supposed to include vptrs in
5892 all classes that need them. */
5893 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5895 /* We build this decl with vtbl_ptr_type_node, which is a
5896 `vtable_entry_type*'. It might seem more precise to use
5897 `vtable_entry_type (*)[N]' where N is the number of virtual
5898 functions. However, that would require the vtable pointer in
5899 base classes to have a different type than the vtable pointer
5900 in derived classes. We could make that happen, but that
5901 still wouldn't solve all the problems. In particular, the
5902 type-based alias analysis code would decide that assignments
5903 to the base class vtable pointer can't alias assignments to
5904 the derived class vtable pointer, since they have different
5905 types. Thus, in a derived class destructor, where the base
5906 class constructor was inlined, we could generate bad code for
5907 setting up the vtable pointer.
5909 Therefore, we use one type for all vtable pointers. We still
5910 use a type-correct type; it's just doesn't indicate the array
5911 bounds. That's better than using `void*' or some such; it's
5912 cleaner, and it let's the alias analysis code know that these
5913 stores cannot alias stores to void*! */
5914 tree field;
5916 field = build_decl (input_location,
5917 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5918 DECL_VIRTUAL_P (field) = 1;
5919 DECL_ARTIFICIAL (field) = 1;
5920 DECL_FIELD_CONTEXT (field) = t;
5921 DECL_FCONTEXT (field) = t;
5922 if (TYPE_PACKED (t))
5923 DECL_PACKED (field) = 1;
5925 TYPE_VFIELD (t) = field;
5927 /* This class is non-empty. */
5928 CLASSTYPE_EMPTY_P (t) = 0;
5930 return field;
5933 return NULL_TREE;
5936 /* Add OFFSET to all base types of BINFO which is a base in the
5937 hierarchy dominated by T.
5939 OFFSET, which is a type offset, is number of bytes. */
5941 static void
5942 propagate_binfo_offsets (tree binfo, tree offset)
5944 int i;
5945 tree primary_binfo;
5946 tree base_binfo;
5948 /* Update BINFO's offset. */
5949 BINFO_OFFSET (binfo)
5950 = convert (sizetype,
5951 size_binop (PLUS_EXPR,
5952 convert (ssizetype, BINFO_OFFSET (binfo)),
5953 offset));
5955 /* Find the primary base class. */
5956 primary_binfo = get_primary_binfo (binfo);
5958 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5959 propagate_binfo_offsets (primary_binfo, offset);
5961 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5962 downwards. */
5963 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5965 /* Don't do the primary base twice. */
5966 if (base_binfo == primary_binfo)
5967 continue;
5969 if (BINFO_VIRTUAL_P (base_binfo))
5970 continue;
5972 propagate_binfo_offsets (base_binfo, offset);
5976 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5977 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5978 empty subobjects of T. */
5980 static void
5981 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5983 tree vbase;
5984 tree t = rli->t;
5985 tree *next_field;
5987 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5988 return;
5990 /* Find the last field. The artificial fields created for virtual
5991 bases will go after the last extant field to date. */
5992 next_field = &TYPE_FIELDS (t);
5993 while (*next_field)
5994 next_field = &DECL_CHAIN (*next_field);
5996 /* Go through the virtual bases, allocating space for each virtual
5997 base that is not already a primary base class. These are
5998 allocated in inheritance graph order. */
5999 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6001 if (!BINFO_VIRTUAL_P (vbase))
6002 continue;
6004 if (!BINFO_PRIMARY_P (vbase))
6006 /* This virtual base is not a primary base of any class in the
6007 hierarchy, so we have to add space for it. */
6008 next_field = build_base_field (rli, vbase,
6009 offsets, next_field);
6014 /* Returns the offset of the byte just past the end of the base class
6015 BINFO. */
6017 static tree
6018 end_of_base (tree binfo)
6020 tree size;
6022 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6023 size = TYPE_SIZE_UNIT (char_type_node);
6024 else if (is_empty_class (BINFO_TYPE (binfo)))
6025 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6026 allocate some space for it. It cannot have virtual bases, so
6027 TYPE_SIZE_UNIT is fine. */
6028 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6029 else
6030 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6032 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6035 /* Returns the offset of the byte just past the end of the base class
6036 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6037 only non-virtual bases are included. */
6039 static tree
6040 end_of_class (tree t, int include_virtuals_p)
6042 tree result = size_zero_node;
6043 vec<tree, va_gc> *vbases;
6044 tree binfo;
6045 tree base_binfo;
6046 tree offset;
6047 int i;
6049 for (binfo = TYPE_BINFO (t), i = 0;
6050 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6052 if (!include_virtuals_p
6053 && BINFO_VIRTUAL_P (base_binfo)
6054 && (!BINFO_PRIMARY_P (base_binfo)
6055 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6056 continue;
6058 offset = end_of_base (base_binfo);
6059 if (tree_int_cst_lt (result, offset))
6060 result = offset;
6063 if (include_virtuals_p)
6064 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6065 vec_safe_iterate (vbases, i, &base_binfo); i++)
6067 offset = end_of_base (base_binfo);
6068 if (tree_int_cst_lt (result, offset))
6069 result = offset;
6072 return result;
6075 /* Warn about bases of T that are inaccessible because they are
6076 ambiguous. For example:
6078 struct S {};
6079 struct T : public S {};
6080 struct U : public S, public T {};
6082 Here, `(S*) new U' is not allowed because there are two `S'
6083 subobjects of U. */
6085 static void
6086 warn_about_ambiguous_bases (tree t)
6088 int i;
6089 vec<tree, va_gc> *vbases;
6090 tree basetype;
6091 tree binfo;
6092 tree base_binfo;
6094 /* If there are no repeated bases, nothing can be ambiguous. */
6095 if (!CLASSTYPE_REPEATED_BASE_P (t))
6096 return;
6098 /* Check direct bases. */
6099 for (binfo = TYPE_BINFO (t), i = 0;
6100 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6102 basetype = BINFO_TYPE (base_binfo);
6104 if (!uniquely_derived_from_p (basetype, t))
6105 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6106 basetype, t);
6109 /* Check for ambiguous virtual bases. */
6110 if (extra_warnings)
6111 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6112 vec_safe_iterate (vbases, i, &binfo); i++)
6114 basetype = BINFO_TYPE (binfo);
6116 if (!uniquely_derived_from_p (basetype, t))
6117 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6118 "to ambiguity", basetype, t);
6122 /* Compare two INTEGER_CSTs K1 and K2. */
6124 static int
6125 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6127 return tree_int_cst_compare ((tree) k1, (tree) k2);
6130 /* Increase the size indicated in RLI to account for empty classes
6131 that are "off the end" of the class. */
6133 static void
6134 include_empty_classes (record_layout_info rli)
6136 tree eoc;
6137 tree rli_size;
6139 /* It might be the case that we grew the class to allocate a
6140 zero-sized base class. That won't be reflected in RLI, yet,
6141 because we are willing to overlay multiple bases at the same
6142 offset. However, now we need to make sure that RLI is big enough
6143 to reflect the entire class. */
6144 eoc = end_of_class (rli->t,
6145 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6146 rli_size = rli_size_unit_so_far (rli);
6147 if (TREE_CODE (rli_size) == INTEGER_CST
6148 && tree_int_cst_lt (rli_size, eoc))
6150 /* The size should have been rounded to a whole byte. */
6151 gcc_assert (tree_int_cst_equal
6152 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6153 rli->bitpos
6154 = size_binop (PLUS_EXPR,
6155 rli->bitpos,
6156 size_binop (MULT_EXPR,
6157 convert (bitsizetype,
6158 size_binop (MINUS_EXPR,
6159 eoc, rli_size)),
6160 bitsize_int (BITS_PER_UNIT)));
6161 normalize_rli (rli);
6165 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6166 BINFO_OFFSETs for all of the base-classes. Position the vtable
6167 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6169 static void
6170 layout_class_type (tree t, tree *virtuals_p)
6172 tree non_static_data_members;
6173 tree field;
6174 tree vptr;
6175 record_layout_info rli;
6176 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6177 types that appear at that offset. */
6178 splay_tree empty_base_offsets;
6179 /* True if the last field laid out was a bit-field. */
6180 bool last_field_was_bitfield = false;
6181 /* The location at which the next field should be inserted. */
6182 tree *next_field;
6183 /* T, as a base class. */
6184 tree base_t;
6186 /* Keep track of the first non-static data member. */
6187 non_static_data_members = TYPE_FIELDS (t);
6189 /* Start laying out the record. */
6190 rli = start_record_layout (t);
6192 /* Mark all the primary bases in the hierarchy. */
6193 determine_primary_bases (t);
6195 /* Create a pointer to our virtual function table. */
6196 vptr = create_vtable_ptr (t, virtuals_p);
6198 /* The vptr is always the first thing in the class. */
6199 if (vptr)
6201 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6202 TYPE_FIELDS (t) = vptr;
6203 next_field = &DECL_CHAIN (vptr);
6204 place_field (rli, vptr);
6206 else
6207 next_field = &TYPE_FIELDS (t);
6209 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6210 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6211 NULL, NULL);
6212 build_base_fields (rli, empty_base_offsets, next_field);
6214 /* Layout the non-static data members. */
6215 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6217 tree type;
6218 tree padding;
6220 /* We still pass things that aren't non-static data members to
6221 the back end, in case it wants to do something with them. */
6222 if (TREE_CODE (field) != FIELD_DECL)
6224 place_field (rli, field);
6225 /* If the static data member has incomplete type, keep track
6226 of it so that it can be completed later. (The handling
6227 of pending statics in finish_record_layout is
6228 insufficient; consider:
6230 struct S1;
6231 struct S2 { static S1 s1; };
6233 At this point, finish_record_layout will be called, but
6234 S1 is still incomplete.) */
6235 if (VAR_P (field))
6237 maybe_register_incomplete_var (field);
6238 /* The visibility of static data members is determined
6239 at their point of declaration, not their point of
6240 definition. */
6241 determine_visibility (field);
6243 continue;
6246 type = TREE_TYPE (field);
6247 if (type == error_mark_node)
6248 continue;
6250 padding = NULL_TREE;
6252 /* If this field is a bit-field whose width is greater than its
6253 type, then there are some special rules for allocating
6254 it. */
6255 if (DECL_C_BIT_FIELD (field)
6256 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6258 unsigned int itk;
6259 tree integer_type;
6260 bool was_unnamed_p = false;
6261 /* We must allocate the bits as if suitably aligned for the
6262 longest integer type that fits in this many bits. type
6263 of the field. Then, we are supposed to use the left over
6264 bits as additional padding. */
6265 for (itk = itk_char; itk != itk_none; ++itk)
6266 if (integer_types[itk] != NULL_TREE
6267 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6268 TYPE_SIZE (integer_types[itk]))
6269 || tree_int_cst_lt (DECL_SIZE (field),
6270 TYPE_SIZE (integer_types[itk]))))
6271 break;
6273 /* ITK now indicates a type that is too large for the
6274 field. We have to back up by one to find the largest
6275 type that fits. */
6278 --itk;
6279 integer_type = integer_types[itk];
6280 } while (itk > 0 && integer_type == NULL_TREE);
6282 /* Figure out how much additional padding is required. */
6283 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6285 if (TREE_CODE (t) == UNION_TYPE)
6286 /* In a union, the padding field must have the full width
6287 of the bit-field; all fields start at offset zero. */
6288 padding = DECL_SIZE (field);
6289 else
6290 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6291 TYPE_SIZE (integer_type));
6294 /* An unnamed bitfield does not normally affect the
6295 alignment of the containing class on a target where
6296 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6297 make any exceptions for unnamed bitfields when the
6298 bitfields are longer than their types. Therefore, we
6299 temporarily give the field a name. */
6300 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6302 was_unnamed_p = true;
6303 DECL_NAME (field) = make_anon_name ();
6306 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6307 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6308 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6309 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6310 empty_base_offsets);
6311 if (was_unnamed_p)
6312 DECL_NAME (field) = NULL_TREE;
6313 /* Now that layout has been performed, set the size of the
6314 field to the size of its declared type; the rest of the
6315 field is effectively invisible. */
6316 DECL_SIZE (field) = TYPE_SIZE (type);
6317 /* We must also reset the DECL_MODE of the field. */
6318 DECL_MODE (field) = TYPE_MODE (type);
6320 else
6321 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6322 empty_base_offsets);
6324 /* Remember the location of any empty classes in FIELD. */
6325 record_subobject_offsets (TREE_TYPE (field),
6326 byte_position(field),
6327 empty_base_offsets,
6328 /*is_data_member=*/true);
6330 /* If a bit-field does not immediately follow another bit-field,
6331 and yet it starts in the middle of a byte, we have failed to
6332 comply with the ABI. */
6333 if (warn_abi
6334 && DECL_C_BIT_FIELD (field)
6335 /* The TREE_NO_WARNING flag gets set by Objective-C when
6336 laying out an Objective-C class. The ObjC ABI differs
6337 from the C++ ABI, and so we do not want a warning
6338 here. */
6339 && !TREE_NO_WARNING (field)
6340 && !last_field_was_bitfield
6341 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6342 DECL_FIELD_BIT_OFFSET (field),
6343 bitsize_unit_node)))
6344 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6345 "offset of %qD is not ABI-compliant and may "
6346 "change in a future version of GCC", field);
6348 /* The middle end uses the type of expressions to determine the
6349 possible range of expression values. In order to optimize
6350 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6351 must be made aware of the width of "i", via its type.
6353 Because C++ does not have integer types of arbitrary width,
6354 we must (for the purposes of the front end) convert from the
6355 type assigned here to the declared type of the bitfield
6356 whenever a bitfield expression is used as an rvalue.
6357 Similarly, when assigning a value to a bitfield, the value
6358 must be converted to the type given the bitfield here. */
6359 if (DECL_C_BIT_FIELD (field))
6361 unsigned HOST_WIDE_INT width;
6362 tree ftype = TREE_TYPE (field);
6363 width = tree_to_uhwi (DECL_SIZE (field));
6364 if (width != TYPE_PRECISION (ftype))
6366 TREE_TYPE (field)
6367 = c_build_bitfield_integer_type (width,
6368 TYPE_UNSIGNED (ftype));
6369 TREE_TYPE (field)
6370 = cp_build_qualified_type (TREE_TYPE (field),
6371 cp_type_quals (ftype));
6375 /* If we needed additional padding after this field, add it
6376 now. */
6377 if (padding)
6379 tree padding_field;
6381 padding_field = build_decl (input_location,
6382 FIELD_DECL,
6383 NULL_TREE,
6384 char_type_node);
6385 DECL_BIT_FIELD (padding_field) = 1;
6386 DECL_SIZE (padding_field) = padding;
6387 DECL_CONTEXT (padding_field) = t;
6388 DECL_ARTIFICIAL (padding_field) = 1;
6389 DECL_IGNORED_P (padding_field) = 1;
6390 layout_nonempty_base_or_field (rli, padding_field,
6391 NULL_TREE,
6392 empty_base_offsets);
6395 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6398 if (!integer_zerop (rli->bitpos))
6400 /* Make sure that we are on a byte boundary so that the size of
6401 the class without virtual bases will always be a round number
6402 of bytes. */
6403 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6404 normalize_rli (rli);
6407 /* Delete all zero-width bit-fields from the list of fields. Now
6408 that the type is laid out they are no longer important. */
6409 remove_zero_width_bit_fields (t);
6411 /* Create the version of T used for virtual bases. We do not use
6412 make_class_type for this version; this is an artificial type. For
6413 a POD type, we just reuse T. */
6414 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6416 base_t = make_node (TREE_CODE (t));
6418 /* Set the size and alignment for the new type. */
6419 tree eoc;
6421 /* If the ABI version is not at least two, and the last
6422 field was a bit-field, RLI may not be on a byte
6423 boundary. In particular, rli_size_unit_so_far might
6424 indicate the last complete byte, while rli_size_so_far
6425 indicates the total number of bits used. Therefore,
6426 rli_size_so_far, rather than rli_size_unit_so_far, is
6427 used to compute TYPE_SIZE_UNIT. */
6428 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6429 TYPE_SIZE_UNIT (base_t)
6430 = size_binop (MAX_EXPR,
6431 convert (sizetype,
6432 size_binop (CEIL_DIV_EXPR,
6433 rli_size_so_far (rli),
6434 bitsize_int (BITS_PER_UNIT))),
6435 eoc);
6436 TYPE_SIZE (base_t)
6437 = size_binop (MAX_EXPR,
6438 rli_size_so_far (rli),
6439 size_binop (MULT_EXPR,
6440 convert (bitsizetype, eoc),
6441 bitsize_int (BITS_PER_UNIT)));
6442 TYPE_ALIGN (base_t) = rli->record_align;
6443 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6445 /* Copy the fields from T. */
6446 next_field = &TYPE_FIELDS (base_t);
6447 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6448 if (TREE_CODE (field) == FIELD_DECL)
6450 *next_field = build_decl (input_location,
6451 FIELD_DECL,
6452 DECL_NAME (field),
6453 TREE_TYPE (field));
6454 DECL_CONTEXT (*next_field) = base_t;
6455 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6456 DECL_FIELD_BIT_OFFSET (*next_field)
6457 = DECL_FIELD_BIT_OFFSET (field);
6458 DECL_SIZE (*next_field) = DECL_SIZE (field);
6459 DECL_MODE (*next_field) = DECL_MODE (field);
6460 next_field = &DECL_CHAIN (*next_field);
6463 /* Record the base version of the type. */
6464 CLASSTYPE_AS_BASE (t) = base_t;
6465 TYPE_CONTEXT (base_t) = t;
6467 else
6468 CLASSTYPE_AS_BASE (t) = t;
6470 /* Every empty class contains an empty class. */
6471 if (CLASSTYPE_EMPTY_P (t))
6472 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6474 /* Set the TYPE_DECL for this type to contain the right
6475 value for DECL_OFFSET, so that we can use it as part
6476 of a COMPONENT_REF for multiple inheritance. */
6477 layout_decl (TYPE_MAIN_DECL (t), 0);
6479 /* Now fix up any virtual base class types that we left lying
6480 around. We must get these done before we try to lay out the
6481 virtual function table. As a side-effect, this will remove the
6482 base subobject fields. */
6483 layout_virtual_bases (rli, empty_base_offsets);
6485 /* Make sure that empty classes are reflected in RLI at this
6486 point. */
6487 include_empty_classes(rli);
6489 /* Make sure not to create any structures with zero size. */
6490 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6491 place_field (rli,
6492 build_decl (input_location,
6493 FIELD_DECL, NULL_TREE, char_type_node));
6495 /* If this is a non-POD, declaring it packed makes a difference to how it
6496 can be used as a field; don't let finalize_record_size undo it. */
6497 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6498 rli->packed_maybe_necessary = true;
6500 /* Let the back end lay out the type. */
6501 finish_record_layout (rli, /*free_p=*/true);
6503 if (TYPE_SIZE_UNIT (t)
6504 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6505 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6506 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6507 error ("type %qT is too large", t);
6509 /* Warn about bases that can't be talked about due to ambiguity. */
6510 warn_about_ambiguous_bases (t);
6512 /* Now that we're done with layout, give the base fields the real types. */
6513 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6514 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6515 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6517 /* Clean up. */
6518 splay_tree_delete (empty_base_offsets);
6520 if (CLASSTYPE_EMPTY_P (t)
6521 && tree_int_cst_lt (sizeof_biggest_empty_class,
6522 TYPE_SIZE_UNIT (t)))
6523 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6526 /* Determine the "key method" for the class type indicated by TYPE,
6527 and set CLASSTYPE_KEY_METHOD accordingly. */
6529 void
6530 determine_key_method (tree type)
6532 tree method;
6534 if (TYPE_FOR_JAVA (type)
6535 || processing_template_decl
6536 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6537 || CLASSTYPE_INTERFACE_KNOWN (type))
6538 return;
6540 /* The key method is the first non-pure virtual function that is not
6541 inline at the point of class definition. On some targets the
6542 key function may not be inline; those targets should not call
6543 this function until the end of the translation unit. */
6544 for (method = TYPE_METHODS (type); method != NULL_TREE;
6545 method = DECL_CHAIN (method))
6546 if (TREE_CODE (method) == FUNCTION_DECL
6547 && DECL_VINDEX (method) != NULL_TREE
6548 && ! DECL_DECLARED_INLINE_P (method)
6549 && ! DECL_PURE_VIRTUAL_P (method))
6551 CLASSTYPE_KEY_METHOD (type) = method;
6552 break;
6555 return;
6559 /* Allocate and return an instance of struct sorted_fields_type with
6560 N fields. */
6562 static struct sorted_fields_type *
6563 sorted_fields_type_new (int n)
6565 struct sorted_fields_type *sft;
6566 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6567 + n * sizeof (tree));
6568 sft->len = n;
6570 return sft;
6574 /* Perform processing required when the definition of T (a class type)
6575 is complete. */
6577 void
6578 finish_struct_1 (tree t)
6580 tree x;
6581 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6582 tree virtuals = NULL_TREE;
6584 if (COMPLETE_TYPE_P (t))
6586 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6587 error ("redefinition of %q#T", t);
6588 popclass ();
6589 return;
6592 /* If this type was previously laid out as a forward reference,
6593 make sure we lay it out again. */
6594 TYPE_SIZE (t) = NULL_TREE;
6595 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6597 /* Make assumptions about the class; we'll reset the flags if
6598 necessary. */
6599 CLASSTYPE_EMPTY_P (t) = 1;
6600 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6601 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6602 CLASSTYPE_LITERAL_P (t) = true;
6604 /* Do end-of-class semantic processing: checking the validity of the
6605 bases and members and add implicitly generated methods. */
6606 check_bases_and_members (t);
6608 /* Find the key method. */
6609 if (TYPE_CONTAINS_VPTR_P (t))
6611 /* The Itanium C++ ABI permits the key method to be chosen when
6612 the class is defined -- even though the key method so
6613 selected may later turn out to be an inline function. On
6614 some systems (such as ARM Symbian OS) the key method cannot
6615 be determined until the end of the translation unit. On such
6616 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6617 will cause the class to be added to KEYED_CLASSES. Then, in
6618 finish_file we will determine the key method. */
6619 if (targetm.cxx.key_method_may_be_inline ())
6620 determine_key_method (t);
6622 /* If a polymorphic class has no key method, we may emit the vtable
6623 in every translation unit where the class definition appears. If
6624 we're devirtualizing, we can look into the vtable even if we
6625 aren't emitting it. */
6626 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6627 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6630 /* Layout the class itself. */
6631 layout_class_type (t, &virtuals);
6632 if (CLASSTYPE_AS_BASE (t) != t)
6633 /* We use the base type for trivial assignments, and hence it
6634 needs a mode. */
6635 compute_record_mode (CLASSTYPE_AS_BASE (t));
6637 virtuals = modify_all_vtables (t, nreverse (virtuals));
6639 /* If necessary, create the primary vtable for this class. */
6640 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6642 /* We must enter these virtuals into the table. */
6643 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6644 build_primary_vtable (NULL_TREE, t);
6645 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6646 /* Here we know enough to change the type of our virtual
6647 function table, but we will wait until later this function. */
6648 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6650 /* If we're warning about ABI tags, check the types of the new
6651 virtual functions. */
6652 if (warn_abi_tag)
6653 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6654 check_abi_tags (t, TREE_VALUE (v));
6657 if (TYPE_CONTAINS_VPTR_P (t))
6659 int vindex;
6660 tree fn;
6662 if (BINFO_VTABLE (TYPE_BINFO (t)))
6663 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6664 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6665 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6667 /* Add entries for virtual functions introduced by this class. */
6668 BINFO_VIRTUALS (TYPE_BINFO (t))
6669 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6671 /* Set DECL_VINDEX for all functions declared in this class. */
6672 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6674 fn = TREE_CHAIN (fn),
6675 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6676 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6678 tree fndecl = BV_FN (fn);
6680 if (DECL_THUNK_P (fndecl))
6681 /* A thunk. We should never be calling this entry directly
6682 from this vtable -- we'd use the entry for the non
6683 thunk base function. */
6684 DECL_VINDEX (fndecl) = NULL_TREE;
6685 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6686 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6690 finish_struct_bits (t);
6691 set_method_tm_attributes (t);
6693 /* Complete the rtl for any static member objects of the type we're
6694 working on. */
6695 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6696 if (VAR_P (x) && TREE_STATIC (x)
6697 && TREE_TYPE (x) != error_mark_node
6698 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6699 DECL_MODE (x) = TYPE_MODE (t);
6701 /* Done with FIELDS...now decide whether to sort these for
6702 faster lookups later.
6704 We use a small number because most searches fail (succeeding
6705 ultimately as the search bores through the inheritance
6706 hierarchy), and we want this failure to occur quickly. */
6708 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6710 /* Complain if one of the field types requires lower visibility. */
6711 constrain_class_visibility (t);
6713 /* Make the rtl for any new vtables we have created, and unmark
6714 the base types we marked. */
6715 finish_vtbls (t);
6717 /* Build the VTT for T. */
6718 build_vtt (t);
6720 /* This warning does not make sense for Java classes, since they
6721 cannot have destructors. */
6722 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
6723 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6724 && !CLASSTYPE_FINAL (t))
6725 warning (OPT_Wnon_virtual_dtor,
6726 "%q#T has virtual functions and accessible"
6727 " non-virtual destructor", t);
6729 complete_vars (t);
6731 if (warn_overloaded_virtual)
6732 warn_hidden (t);
6734 /* Class layout, assignment of virtual table slots, etc., is now
6735 complete. Give the back end a chance to tweak the visibility of
6736 the class or perform any other required target modifications. */
6737 targetm.cxx.adjust_class_at_definition (t);
6739 maybe_suppress_debug_info (t);
6741 if (flag_vtable_verify)
6742 vtv_save_class_info (t);
6744 dump_class_hierarchy (t);
6746 /* Finish debugging output for this type. */
6747 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6749 if (TYPE_TRANSPARENT_AGGR (t))
6751 tree field = first_field (t);
6752 if (field == NULL_TREE || error_operand_p (field))
6754 error ("type transparent %q#T does not have any fields", t);
6755 TYPE_TRANSPARENT_AGGR (t) = 0;
6757 else if (DECL_ARTIFICIAL (field))
6759 if (DECL_FIELD_IS_BASE (field))
6760 error ("type transparent class %qT has base classes", t);
6761 else
6763 gcc_checking_assert (DECL_VIRTUAL_P (field));
6764 error ("type transparent class %qT has virtual functions", t);
6766 TYPE_TRANSPARENT_AGGR (t) = 0;
6768 else if (TYPE_MODE (t) != DECL_MODE (field))
6770 error ("type transparent %q#T cannot be made transparent because "
6771 "the type of the first field has a different ABI from the "
6772 "class overall", t);
6773 TYPE_TRANSPARENT_AGGR (t) = 0;
6778 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6779 equal to THRESHOLD or greater than THRESHOLD. */
6781 static void
6782 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6784 int n_fields = count_fields (fields);
6785 if (n_fields >= threshold)
6787 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6788 add_fields_to_record_type (fields, field_vec, 0);
6789 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6790 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6794 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6796 void
6797 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6799 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6800 if (sorted_fields)
6802 int i;
6803 int n_fields
6804 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6805 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6807 for (i = 0; i < sorted_fields->len; ++i)
6808 field_vec->elts[i] = sorted_fields->elts[i];
6810 add_enum_fields_to_record_type (enumtype, field_vec,
6811 sorted_fields->len);
6812 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6813 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6817 /* When T was built up, the member declarations were added in reverse
6818 order. Rearrange them to declaration order. */
6820 void
6821 unreverse_member_declarations (tree t)
6823 tree next;
6824 tree prev;
6825 tree x;
6827 /* The following lists are all in reverse order. Put them in
6828 declaration order now. */
6829 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6830 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6832 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6833 reverse order, so we can't just use nreverse. */
6834 prev = NULL_TREE;
6835 for (x = TYPE_FIELDS (t);
6836 x && TREE_CODE (x) != TYPE_DECL;
6837 x = next)
6839 next = DECL_CHAIN (x);
6840 DECL_CHAIN (x) = prev;
6841 prev = x;
6843 if (prev)
6845 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6846 if (prev)
6847 TYPE_FIELDS (t) = prev;
6851 tree
6852 finish_struct (tree t, tree attributes)
6854 location_t saved_loc = input_location;
6856 /* Now that we've got all the field declarations, reverse everything
6857 as necessary. */
6858 unreverse_member_declarations (t);
6860 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6861 fixup_attribute_variants (t);
6863 /* Nadger the current location so that diagnostics point to the start of
6864 the struct, not the end. */
6865 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6867 if (processing_template_decl)
6869 tree x;
6871 finish_struct_methods (t);
6872 TYPE_SIZE (t) = bitsize_zero_node;
6873 TYPE_SIZE_UNIT (t) = size_zero_node;
6875 /* We need to emit an error message if this type was used as a parameter
6876 and it is an abstract type, even if it is a template. We construct
6877 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6878 account and we call complete_vars with this type, which will check
6879 the PARM_DECLS. Note that while the type is being defined,
6880 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6881 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6882 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6883 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6884 if (DECL_PURE_VIRTUAL_P (x))
6885 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6886 complete_vars (t);
6887 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6888 an enclosing scope is a template class, so that this function be
6889 found by lookup_fnfields_1 when the using declaration is not
6890 instantiated yet. */
6891 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6892 if (TREE_CODE (x) == USING_DECL)
6894 tree fn = strip_using_decl (x);
6895 if (is_overloaded_fn (fn))
6896 for (; fn; fn = OVL_NEXT (fn))
6897 add_method (t, OVL_CURRENT (fn), x);
6900 /* Remember current #pragma pack value. */
6901 TYPE_PRECISION (t) = maximum_field_alignment;
6903 /* Fix up any variants we've already built. */
6904 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6906 TYPE_SIZE (x) = TYPE_SIZE (t);
6907 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6908 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6909 TYPE_METHODS (x) = TYPE_METHODS (t);
6912 else
6913 finish_struct_1 (t);
6915 if (is_std_init_list (t))
6917 /* People keep complaining that the compiler crashes on an invalid
6918 definition of initializer_list, so I guess we should explicitly
6919 reject it. What the compiler internals care about is that it's a
6920 template and has a pointer field followed by an integer field. */
6921 bool ok = false;
6922 if (processing_template_decl)
6924 tree f = next_initializable_field (TYPE_FIELDS (t));
6925 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
6927 f = next_initializable_field (DECL_CHAIN (f));
6928 if (f && same_type_p (TREE_TYPE (f), size_type_node))
6929 ok = true;
6932 if (!ok)
6933 fatal_error (input_location,
6934 "definition of std::initializer_list does not match "
6935 "#include <initializer_list>");
6938 input_location = saved_loc;
6940 TYPE_BEING_DEFINED (t) = 0;
6942 if (current_class_type)
6943 popclass ();
6944 else
6945 error ("trying to finish struct, but kicked out due to previous parse errors");
6947 if (processing_template_decl && at_function_scope_p ()
6948 /* Lambdas are defined by the LAMBDA_EXPR. */
6949 && !LAMBDA_TYPE_P (t))
6950 add_stmt (build_min (TAG_DEFN, t));
6952 return t;
6955 /* Hash table to avoid endless recursion when handling references. */
6956 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
6958 /* Return the dynamic type of INSTANCE, if known.
6959 Used to determine whether the virtual function table is needed
6960 or not.
6962 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6963 of our knowledge of its type. *NONNULL should be initialized
6964 before this function is called. */
6966 static tree
6967 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6969 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6971 switch (TREE_CODE (instance))
6973 case INDIRECT_REF:
6974 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6975 return NULL_TREE;
6976 else
6977 return RECUR (TREE_OPERAND (instance, 0));
6979 case CALL_EXPR:
6980 /* This is a call to a constructor, hence it's never zero. */
6981 if (TREE_HAS_CONSTRUCTOR (instance))
6983 if (nonnull)
6984 *nonnull = 1;
6985 return TREE_TYPE (instance);
6987 return NULL_TREE;
6989 case SAVE_EXPR:
6990 /* This is a call to a constructor, hence it's never zero. */
6991 if (TREE_HAS_CONSTRUCTOR (instance))
6993 if (nonnull)
6994 *nonnull = 1;
6995 return TREE_TYPE (instance);
6997 return RECUR (TREE_OPERAND (instance, 0));
6999 case POINTER_PLUS_EXPR:
7000 case PLUS_EXPR:
7001 case MINUS_EXPR:
7002 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7003 return RECUR (TREE_OPERAND (instance, 0));
7004 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7005 /* Propagate nonnull. */
7006 return RECUR (TREE_OPERAND (instance, 0));
7008 return NULL_TREE;
7010 CASE_CONVERT:
7011 return RECUR (TREE_OPERAND (instance, 0));
7013 case ADDR_EXPR:
7014 instance = TREE_OPERAND (instance, 0);
7015 if (nonnull)
7017 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7018 with a real object -- given &p->f, p can still be null. */
7019 tree t = get_base_address (instance);
7020 /* ??? Probably should check DECL_WEAK here. */
7021 if (t && DECL_P (t))
7022 *nonnull = 1;
7024 return RECUR (instance);
7026 case COMPONENT_REF:
7027 /* If this component is really a base class reference, then the field
7028 itself isn't definitive. */
7029 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7030 return RECUR (TREE_OPERAND (instance, 0));
7031 return RECUR (TREE_OPERAND (instance, 1));
7033 case VAR_DECL:
7034 case FIELD_DECL:
7035 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7036 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7038 if (nonnull)
7039 *nonnull = 1;
7040 return TREE_TYPE (TREE_TYPE (instance));
7042 /* fall through... */
7043 case TARGET_EXPR:
7044 case PARM_DECL:
7045 case RESULT_DECL:
7046 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7048 if (nonnull)
7049 *nonnull = 1;
7050 return TREE_TYPE (instance);
7052 else if (instance == current_class_ptr)
7054 if (nonnull)
7055 *nonnull = 1;
7057 /* if we're in a ctor or dtor, we know our type. If
7058 current_class_ptr is set but we aren't in a function, we're in
7059 an NSDMI (and therefore a constructor). */
7060 if (current_scope () != current_function_decl
7061 || (DECL_LANG_SPECIFIC (current_function_decl)
7062 && (DECL_CONSTRUCTOR_P (current_function_decl)
7063 || DECL_DESTRUCTOR_P (current_function_decl))))
7065 if (cdtorp)
7066 *cdtorp = 1;
7067 return TREE_TYPE (TREE_TYPE (instance));
7070 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7072 /* We only need one hash table because it is always left empty. */
7073 if (!fixed_type_or_null_ref_ht)
7074 fixed_type_or_null_ref_ht
7075 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7077 /* Reference variables should be references to objects. */
7078 if (nonnull)
7079 *nonnull = 1;
7081 /* Enter the INSTANCE in a table to prevent recursion; a
7082 variable's initializer may refer to the variable
7083 itself. */
7084 if (VAR_P (instance)
7085 && DECL_INITIAL (instance)
7086 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7087 && !fixed_type_or_null_ref_ht->find (instance))
7089 tree type;
7090 tree_node **slot;
7092 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7093 *slot = instance;
7094 type = RECUR (DECL_INITIAL (instance));
7095 fixed_type_or_null_ref_ht->remove_elt (instance);
7097 return type;
7100 return NULL_TREE;
7102 default:
7103 return NULL_TREE;
7105 #undef RECUR
7108 /* Return nonzero if the dynamic type of INSTANCE is known, and
7109 equivalent to the static type. We also handle the case where
7110 INSTANCE is really a pointer. Return negative if this is a
7111 ctor/dtor. There the dynamic type is known, but this might not be
7112 the most derived base of the original object, and hence virtual
7113 bases may not be laid out according to this type.
7115 Used to determine whether the virtual function table is needed
7116 or not.
7118 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7119 of our knowledge of its type. *NONNULL should be initialized
7120 before this function is called. */
7123 resolves_to_fixed_type_p (tree instance, int* nonnull)
7125 tree t = TREE_TYPE (instance);
7126 int cdtorp = 0;
7127 tree fixed;
7129 /* processing_template_decl can be false in a template if we're in
7130 instantiate_non_dependent_expr, but we still want to suppress
7131 this check. */
7132 if (in_template_function ())
7134 /* In a template we only care about the type of the result. */
7135 if (nonnull)
7136 *nonnull = true;
7137 return true;
7140 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7141 if (fixed == NULL_TREE)
7142 return 0;
7143 if (POINTER_TYPE_P (t))
7144 t = TREE_TYPE (t);
7145 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7146 return 0;
7147 return cdtorp ? -1 : 1;
7151 void
7152 init_class_processing (void)
7154 current_class_depth = 0;
7155 current_class_stack_size = 10;
7156 current_class_stack
7157 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7158 vec_alloc (local_classes, 8);
7159 sizeof_biggest_empty_class = size_zero_node;
7161 ridpointers[(int) RID_PUBLIC] = access_public_node;
7162 ridpointers[(int) RID_PRIVATE] = access_private_node;
7163 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7166 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7168 static void
7169 restore_class_cache (void)
7171 tree type;
7173 /* We are re-entering the same class we just left, so we don't
7174 have to search the whole inheritance matrix to find all the
7175 decls to bind again. Instead, we install the cached
7176 class_shadowed list and walk through it binding names. */
7177 push_binding_level (previous_class_level);
7178 class_binding_level = previous_class_level;
7179 /* Restore IDENTIFIER_TYPE_VALUE. */
7180 for (type = class_binding_level->type_shadowed;
7181 type;
7182 type = TREE_CHAIN (type))
7183 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7186 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7187 appropriate for TYPE.
7189 So that we may avoid calls to lookup_name, we cache the _TYPE
7190 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7192 For multiple inheritance, we perform a two-pass depth-first search
7193 of the type lattice. */
7195 void
7196 pushclass (tree type)
7198 class_stack_node_t csn;
7200 type = TYPE_MAIN_VARIANT (type);
7202 /* Make sure there is enough room for the new entry on the stack. */
7203 if (current_class_depth + 1 >= current_class_stack_size)
7205 current_class_stack_size *= 2;
7206 current_class_stack
7207 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7208 current_class_stack_size);
7211 /* Insert a new entry on the class stack. */
7212 csn = current_class_stack + current_class_depth;
7213 csn->name = current_class_name;
7214 csn->type = current_class_type;
7215 csn->access = current_access_specifier;
7216 csn->names_used = 0;
7217 csn->hidden = 0;
7218 current_class_depth++;
7220 /* Now set up the new type. */
7221 current_class_name = TYPE_NAME (type);
7222 if (TREE_CODE (current_class_name) == TYPE_DECL)
7223 current_class_name = DECL_NAME (current_class_name);
7224 current_class_type = type;
7226 /* By default, things in classes are private, while things in
7227 structures or unions are public. */
7228 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7229 ? access_private_node
7230 : access_public_node);
7232 if (previous_class_level
7233 && type != previous_class_level->this_entity
7234 && current_class_depth == 1)
7236 /* Forcibly remove any old class remnants. */
7237 invalidate_class_lookup_cache ();
7240 if (!previous_class_level
7241 || type != previous_class_level->this_entity
7242 || current_class_depth > 1)
7243 pushlevel_class ();
7244 else
7245 restore_class_cache ();
7248 /* When we exit a toplevel class scope, we save its binding level so
7249 that we can restore it quickly. Here, we've entered some other
7250 class, so we must invalidate our cache. */
7252 void
7253 invalidate_class_lookup_cache (void)
7255 previous_class_level = NULL;
7258 /* Get out of the current class scope. If we were in a class scope
7259 previously, that is the one popped to. */
7261 void
7262 popclass (void)
7264 poplevel_class ();
7266 current_class_depth--;
7267 current_class_name = current_class_stack[current_class_depth].name;
7268 current_class_type = current_class_stack[current_class_depth].type;
7269 current_access_specifier = current_class_stack[current_class_depth].access;
7270 if (current_class_stack[current_class_depth].names_used)
7271 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7274 /* Mark the top of the class stack as hidden. */
7276 void
7277 push_class_stack (void)
7279 if (current_class_depth)
7280 ++current_class_stack[current_class_depth - 1].hidden;
7283 /* Mark the top of the class stack as un-hidden. */
7285 void
7286 pop_class_stack (void)
7288 if (current_class_depth)
7289 --current_class_stack[current_class_depth - 1].hidden;
7292 /* Returns 1 if the class type currently being defined is either T or
7293 a nested type of T. Returns the type from the current_class_stack,
7294 which might be equivalent to but not equal to T in case of
7295 constrained partial specializations. */
7297 tree
7298 currently_open_class (tree t)
7300 int i;
7302 if (!CLASS_TYPE_P (t))
7303 return NULL_TREE;
7305 t = TYPE_MAIN_VARIANT (t);
7307 /* We start looking from 1 because entry 0 is from global scope,
7308 and has no type. */
7309 for (i = current_class_depth; i > 0; --i)
7311 tree c;
7312 if (i == current_class_depth)
7313 c = current_class_type;
7314 else
7316 if (current_class_stack[i].hidden)
7317 break;
7318 c = current_class_stack[i].type;
7320 if (!c)
7321 continue;
7322 if (same_type_p (c, t))
7323 return c;
7325 return NULL_TREE;
7328 /* If either current_class_type or one of its enclosing classes are derived
7329 from T, return the appropriate type. Used to determine how we found
7330 something via unqualified lookup. */
7332 tree
7333 currently_open_derived_class (tree t)
7335 int i;
7337 /* The bases of a dependent type are unknown. */
7338 if (dependent_type_p (t))
7339 return NULL_TREE;
7341 if (!current_class_type)
7342 return NULL_TREE;
7344 if (DERIVED_FROM_P (t, current_class_type))
7345 return current_class_type;
7347 for (i = current_class_depth - 1; i > 0; --i)
7349 if (current_class_stack[i].hidden)
7350 break;
7351 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7352 return current_class_stack[i].type;
7355 return NULL_TREE;
7358 /* Return the outermost enclosing class type that is still open, or
7359 NULL_TREE. */
7361 tree
7362 outermost_open_class (void)
7364 if (!current_class_type)
7365 return NULL_TREE;
7366 tree r = NULL_TREE;
7367 if (TYPE_BEING_DEFINED (current_class_type))
7368 r = current_class_type;
7369 for (int i = current_class_depth - 1; i > 0; --i)
7371 if (current_class_stack[i].hidden)
7372 break;
7373 tree t = current_class_stack[i].type;
7374 if (!TYPE_BEING_DEFINED (t))
7375 break;
7376 r = t;
7378 return r;
7381 /* Returns the innermost class type which is not a lambda closure type. */
7383 tree
7384 current_nonlambda_class_type (void)
7386 int i;
7388 /* We start looking from 1 because entry 0 is from global scope,
7389 and has no type. */
7390 for (i = current_class_depth; i > 0; --i)
7392 tree c;
7393 if (i == current_class_depth)
7394 c = current_class_type;
7395 else
7397 if (current_class_stack[i].hidden)
7398 break;
7399 c = current_class_stack[i].type;
7401 if (!c)
7402 continue;
7403 if (!LAMBDA_TYPE_P (c))
7404 return c;
7406 return NULL_TREE;
7409 /* When entering a class scope, all enclosing class scopes' names with
7410 static meaning (static variables, static functions, types and
7411 enumerators) have to be visible. This recursive function calls
7412 pushclass for all enclosing class contexts until global or a local
7413 scope is reached. TYPE is the enclosed class. */
7415 void
7416 push_nested_class (tree type)
7418 /* A namespace might be passed in error cases, like A::B:C. */
7419 if (type == NULL_TREE
7420 || !CLASS_TYPE_P (type))
7421 return;
7423 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7425 pushclass (type);
7428 /* Undoes a push_nested_class call. */
7430 void
7431 pop_nested_class (void)
7433 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7435 popclass ();
7436 if (context && CLASS_TYPE_P (context))
7437 pop_nested_class ();
7440 /* Returns the number of extern "LANG" blocks we are nested within. */
7443 current_lang_depth (void)
7445 return vec_safe_length (current_lang_base);
7448 /* Set global variables CURRENT_LANG_NAME to appropriate value
7449 so that behavior of name-mangling machinery is correct. */
7451 void
7452 push_lang_context (tree name)
7454 vec_safe_push (current_lang_base, current_lang_name);
7456 if (name == lang_name_cplusplus)
7458 current_lang_name = name;
7460 else if (name == lang_name_java)
7462 current_lang_name = name;
7463 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7464 (See record_builtin_java_type in decl.c.) However, that causes
7465 incorrect debug entries if these types are actually used.
7466 So we re-enable debug output after extern "Java". */
7467 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7468 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7469 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7470 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7471 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7472 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7473 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7474 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7476 else if (name == lang_name_c)
7478 current_lang_name = name;
7480 else
7481 error ("language string %<\"%E\"%> not recognized", name);
7484 /* Get out of the current language scope. */
7486 void
7487 pop_lang_context (void)
7489 current_lang_name = current_lang_base->pop ();
7492 /* Type instantiation routines. */
7494 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7495 matches the TARGET_TYPE. If there is no satisfactory match, return
7496 error_mark_node, and issue an error & warning messages under
7497 control of FLAGS. Permit pointers to member function if FLAGS
7498 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7499 a template-id, and EXPLICIT_TARGS are the explicitly provided
7500 template arguments.
7502 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7503 is the base path used to reference those member functions. If
7504 the address is resolved to a member function, access checks will be
7505 performed and errors issued if appropriate. */
7507 static tree
7508 resolve_address_of_overloaded_function (tree target_type,
7509 tree overload,
7510 tsubst_flags_t complain,
7511 bool template_only,
7512 tree explicit_targs,
7513 tree access_path)
7515 /* Here's what the standard says:
7517 [over.over]
7519 If the name is a function template, template argument deduction
7520 is done, and if the argument deduction succeeds, the deduced
7521 arguments are used to generate a single template function, which
7522 is added to the set of overloaded functions considered.
7524 Non-member functions and static member functions match targets of
7525 type "pointer-to-function" or "reference-to-function." Nonstatic
7526 member functions match targets of type "pointer-to-member
7527 function;" the function type of the pointer to member is used to
7528 select the member function from the set of overloaded member
7529 functions. If a nonstatic member function is selected, the
7530 reference to the overloaded function name is required to have the
7531 form of a pointer to member as described in 5.3.1.
7533 If more than one function is selected, any template functions in
7534 the set are eliminated if the set also contains a non-template
7535 function, and any given template function is eliminated if the
7536 set contains a second template function that is more specialized
7537 than the first according to the partial ordering rules 14.5.5.2.
7538 After such eliminations, if any, there shall remain exactly one
7539 selected function. */
7541 int is_ptrmem = 0;
7542 /* We store the matches in a TREE_LIST rooted here. The functions
7543 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7544 interoperability with most_specialized_instantiation. */
7545 tree matches = NULL_TREE;
7546 tree fn;
7547 tree target_fn_type;
7549 /* By the time we get here, we should be seeing only real
7550 pointer-to-member types, not the internal POINTER_TYPE to
7551 METHOD_TYPE representation. */
7552 gcc_assert (!TYPE_PTR_P (target_type)
7553 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7555 gcc_assert (is_overloaded_fn (overload));
7557 /* Check that the TARGET_TYPE is reasonable. */
7558 if (TYPE_PTRFN_P (target_type)
7559 || TYPE_REFFN_P (target_type))
7560 /* This is OK. */;
7561 else if (TYPE_PTRMEMFUNC_P (target_type))
7562 /* This is OK, too. */
7563 is_ptrmem = 1;
7564 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7565 /* This is OK, too. This comes from a conversion to reference
7566 type. */
7567 target_type = build_reference_type (target_type);
7568 else
7570 if (complain & tf_error)
7571 error ("cannot resolve overloaded function %qD based on"
7572 " conversion to type %qT",
7573 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7574 return error_mark_node;
7577 /* Non-member functions and static member functions match targets of type
7578 "pointer-to-function" or "reference-to-function." Nonstatic member
7579 functions match targets of type "pointer-to-member-function;" the
7580 function type of the pointer to member is used to select the member
7581 function from the set of overloaded member functions.
7583 So figure out the FUNCTION_TYPE that we want to match against. */
7584 target_fn_type = static_fn_type (target_type);
7586 /* If we can find a non-template function that matches, we can just
7587 use it. There's no point in generating template instantiations
7588 if we're just going to throw them out anyhow. But, of course, we
7589 can only do this when we don't *need* a template function. */
7590 if (!template_only)
7592 tree fns;
7594 for (fns = overload; fns; fns = OVL_NEXT (fns))
7596 tree fn = OVL_CURRENT (fns);
7598 if (TREE_CODE (fn) == TEMPLATE_DECL)
7599 /* We're not looking for templates just yet. */
7600 continue;
7602 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7603 != is_ptrmem)
7604 /* We're looking for a non-static member, and this isn't
7605 one, or vice versa. */
7606 continue;
7608 /* Ignore functions which haven't been explicitly
7609 declared. */
7610 if (DECL_ANTICIPATED (fn))
7611 continue;
7613 /* See if there's a match. */
7614 if (same_type_p (target_fn_type, static_fn_type (fn)))
7615 matches = tree_cons (fn, NULL_TREE, matches);
7619 /* Now, if we've already got a match (or matches), there's no need
7620 to proceed to the template functions. But, if we don't have a
7621 match we need to look at them, too. */
7622 if (!matches)
7624 tree target_arg_types;
7625 tree target_ret_type;
7626 tree fns;
7627 tree *args;
7628 unsigned int nargs, ia;
7629 tree arg;
7631 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7632 target_ret_type = TREE_TYPE (target_fn_type);
7634 nargs = list_length (target_arg_types);
7635 args = XALLOCAVEC (tree, nargs);
7636 for (arg = target_arg_types, ia = 0;
7637 arg != NULL_TREE && arg != void_list_node;
7638 arg = TREE_CHAIN (arg), ++ia)
7639 args[ia] = TREE_VALUE (arg);
7640 nargs = ia;
7642 for (fns = overload; fns; fns = OVL_NEXT (fns))
7644 tree fn = OVL_CURRENT (fns);
7645 tree instantiation;
7646 tree targs;
7648 if (TREE_CODE (fn) != TEMPLATE_DECL)
7649 /* We're only looking for templates. */
7650 continue;
7652 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7653 != is_ptrmem)
7654 /* We're not looking for a non-static member, and this is
7655 one, or vice versa. */
7656 continue;
7658 tree ret = target_ret_type;
7660 /* If the template has a deduced return type, don't expose it to
7661 template argument deduction. */
7662 if (undeduced_auto_decl (fn))
7663 ret = NULL_TREE;
7665 /* Try to do argument deduction. */
7666 targs = make_tree_vec (DECL_NTPARMS (fn));
7667 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7668 nargs, ret,
7669 DEDUCE_EXACT, LOOKUP_NORMAL,
7670 false, false);
7671 if (instantiation == error_mark_node)
7672 /* Instantiation failed. */
7673 continue;
7675 /* Constraints must be satisfied. This is done before
7676 return type deduction since that instantiates the
7677 function. */
7678 if (flag_concepts && !constraints_satisfied_p (instantiation))
7679 continue;
7681 /* And now force instantiation to do return type deduction. */
7682 if (undeduced_auto_decl (instantiation))
7684 ++function_depth;
7685 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7686 --function_depth;
7688 require_deduced_type (instantiation);
7691 /* See if there's a match. */
7692 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7693 matches = tree_cons (instantiation, fn, matches);
7696 /* Now, remove all but the most specialized of the matches. */
7697 if (matches)
7699 tree match = most_specialized_instantiation (matches);
7701 if (match != error_mark_node)
7702 matches = tree_cons (TREE_PURPOSE (match),
7703 NULL_TREE,
7704 NULL_TREE);
7708 /* Now we should have exactly one function in MATCHES. */
7709 if (matches == NULL_TREE)
7711 /* There were *no* matches. */
7712 if (complain & tf_error)
7714 error ("no matches converting function %qD to type %q#T",
7715 DECL_NAME (OVL_CURRENT (overload)),
7716 target_type);
7718 print_candidates (overload);
7720 return error_mark_node;
7722 else if (TREE_CHAIN (matches))
7724 /* There were too many matches. First check if they're all
7725 the same function. */
7726 tree match = NULL_TREE;
7728 fn = TREE_PURPOSE (matches);
7730 /* For multi-versioned functions, more than one match is just fine and
7731 decls_match will return false as they are different. */
7732 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7733 if (!decls_match (fn, TREE_PURPOSE (match))
7734 && !targetm.target_option.function_versions
7735 (fn, TREE_PURPOSE (match)))
7736 break;
7738 if (match)
7740 if (complain & tf_error)
7742 error ("converting overloaded function %qD to type %q#T is ambiguous",
7743 DECL_NAME (OVL_FUNCTION (overload)),
7744 target_type);
7746 /* Since print_candidates expects the functions in the
7747 TREE_VALUE slot, we flip them here. */
7748 for (match = matches; match; match = TREE_CHAIN (match))
7749 TREE_VALUE (match) = TREE_PURPOSE (match);
7751 print_candidates (matches);
7754 return error_mark_node;
7758 /* Good, exactly one match. Now, convert it to the correct type. */
7759 fn = TREE_PURPOSE (matches);
7761 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7762 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7764 static int explained;
7766 if (!(complain & tf_error))
7767 return error_mark_node;
7769 permerror (input_location, "assuming pointer to member %qD", fn);
7770 if (!explained)
7772 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7773 explained = 1;
7777 /* If a pointer to a function that is multi-versioned is requested, the
7778 pointer to the dispatcher function is returned instead. This works
7779 well because indirectly calling the function will dispatch the right
7780 function version at run-time. */
7781 if (DECL_FUNCTION_VERSIONED (fn))
7783 fn = get_function_version_dispatcher (fn);
7784 if (fn == NULL)
7785 return error_mark_node;
7786 /* Mark all the versions corresponding to the dispatcher as used. */
7787 if (!(complain & tf_conv))
7788 mark_versions_used (fn);
7791 /* If we're doing overload resolution purely for the purpose of
7792 determining conversion sequences, we should not consider the
7793 function used. If this conversion sequence is selected, the
7794 function will be marked as used at this point. */
7795 if (!(complain & tf_conv))
7797 /* Make =delete work with SFINAE. */
7798 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7799 return error_mark_node;
7800 if (!mark_used (fn, complain) && !(complain & tf_error))
7801 return error_mark_node;
7804 /* We could not check access to member functions when this
7805 expression was originally created since we did not know at that
7806 time to which function the expression referred. */
7807 if (DECL_FUNCTION_MEMBER_P (fn))
7809 gcc_assert (access_path);
7810 perform_or_defer_access_check (access_path, fn, fn, complain);
7813 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7814 return cp_build_addr_expr (fn, complain);
7815 else
7817 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7818 will mark the function as addressed, but here we must do it
7819 explicitly. */
7820 cxx_mark_addressable (fn);
7822 return fn;
7826 /* This function will instantiate the type of the expression given in
7827 RHS to match the type of LHSTYPE. If errors exist, then return
7828 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7829 we complain on errors. If we are not complaining, never modify rhs,
7830 as overload resolution wants to try many possible instantiations, in
7831 the hope that at least one will work.
7833 For non-recursive calls, LHSTYPE should be a function, pointer to
7834 function, or a pointer to member function. */
7836 tree
7837 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7839 tsubst_flags_t complain_in = complain;
7840 tree access_path = NULL_TREE;
7842 complain &= ~tf_ptrmem_ok;
7844 if (lhstype == unknown_type_node)
7846 if (complain & tf_error)
7847 error ("not enough type information");
7848 return error_mark_node;
7851 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7853 tree fntype = non_reference (lhstype);
7854 if (same_type_p (fntype, TREE_TYPE (rhs)))
7855 return rhs;
7856 if (flag_ms_extensions
7857 && TYPE_PTRMEMFUNC_P (fntype)
7858 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7859 /* Microsoft allows `A::f' to be resolved to a
7860 pointer-to-member. */
7862 else
7864 if (complain & tf_error)
7865 error ("cannot convert %qE from type %qT to type %qT",
7866 rhs, TREE_TYPE (rhs), fntype);
7867 return error_mark_node;
7871 if (BASELINK_P (rhs))
7873 access_path = BASELINK_ACCESS_BINFO (rhs);
7874 rhs = BASELINK_FUNCTIONS (rhs);
7877 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7878 deduce any type information. */
7879 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7881 if (complain & tf_error)
7882 error ("not enough type information");
7883 return error_mark_node;
7886 /* There only a few kinds of expressions that may have a type
7887 dependent on overload resolution. */
7888 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7889 || TREE_CODE (rhs) == COMPONENT_REF
7890 || is_overloaded_fn (rhs)
7891 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7893 /* This should really only be used when attempting to distinguish
7894 what sort of a pointer to function we have. For now, any
7895 arithmetic operation which is not supported on pointers
7896 is rejected as an error. */
7898 switch (TREE_CODE (rhs))
7900 case COMPONENT_REF:
7902 tree member = TREE_OPERAND (rhs, 1);
7904 member = instantiate_type (lhstype, member, complain);
7905 if (member != error_mark_node
7906 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7907 /* Do not lose object's side effects. */
7908 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7909 TREE_OPERAND (rhs, 0), member);
7910 return member;
7913 case OFFSET_REF:
7914 rhs = TREE_OPERAND (rhs, 1);
7915 if (BASELINK_P (rhs))
7916 return instantiate_type (lhstype, rhs, complain_in);
7918 /* This can happen if we are forming a pointer-to-member for a
7919 member template. */
7920 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7922 /* Fall through. */
7924 case TEMPLATE_ID_EXPR:
7926 tree fns = TREE_OPERAND (rhs, 0);
7927 tree args = TREE_OPERAND (rhs, 1);
7929 return
7930 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
7931 /*template_only=*/true,
7932 args, access_path);
7935 case OVERLOAD:
7936 case FUNCTION_DECL:
7937 return
7938 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
7939 /*template_only=*/false,
7940 /*explicit_targs=*/NULL_TREE,
7941 access_path);
7943 case ADDR_EXPR:
7945 if (PTRMEM_OK_P (rhs))
7946 complain |= tf_ptrmem_ok;
7948 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
7951 case ERROR_MARK:
7952 return error_mark_node;
7954 default:
7955 gcc_unreachable ();
7957 return error_mark_node;
7960 /* Return the name of the virtual function pointer field
7961 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7962 this may have to look back through base types to find the
7963 ultimate field name. (For single inheritance, these could
7964 all be the same name. Who knows for multiple inheritance). */
7966 static tree
7967 get_vfield_name (tree type)
7969 tree binfo, base_binfo;
7970 char *buf;
7972 for (binfo = TYPE_BINFO (type);
7973 BINFO_N_BASE_BINFOS (binfo);
7974 binfo = base_binfo)
7976 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7978 if (BINFO_VIRTUAL_P (base_binfo)
7979 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7980 break;
7983 type = BINFO_TYPE (binfo);
7984 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7985 + TYPE_NAME_LENGTH (type) + 2);
7986 sprintf (buf, VFIELD_NAME_FORMAT,
7987 IDENTIFIER_POINTER (constructor_name (type)));
7988 return get_identifier (buf);
7991 void
7992 print_class_statistics (void)
7994 if (! GATHER_STATISTICS)
7995 return;
7997 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7998 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7999 if (n_vtables)
8001 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8002 n_vtables, n_vtable_searches);
8003 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8004 n_vtable_entries, n_vtable_elems);
8008 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8009 according to [class]:
8010 The class-name is also inserted
8011 into the scope of the class itself. For purposes of access checking,
8012 the inserted class name is treated as if it were a public member name. */
8014 void
8015 build_self_reference (void)
8017 tree name = constructor_name (current_class_type);
8018 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8019 tree saved_cas;
8021 DECL_NONLOCAL (value) = 1;
8022 DECL_CONTEXT (value) = current_class_type;
8023 DECL_ARTIFICIAL (value) = 1;
8024 SET_DECL_SELF_REFERENCE_P (value);
8025 set_underlying_type (value);
8027 if (processing_template_decl)
8028 value = push_template_decl (value);
8030 saved_cas = current_access_specifier;
8031 current_access_specifier = access_public_node;
8032 finish_member_declaration (value);
8033 current_access_specifier = saved_cas;
8036 /* Returns 1 if TYPE contains only padding bytes. */
8039 is_empty_class (tree type)
8041 if (type == error_mark_node)
8042 return 0;
8044 if (! CLASS_TYPE_P (type))
8045 return 0;
8047 return CLASSTYPE_EMPTY_P (type);
8050 /* Returns true if TYPE contains no actual data, just various
8051 possible combinations of empty classes and possibly a vptr. */
8053 bool
8054 is_really_empty_class (tree type)
8056 if (CLASS_TYPE_P (type))
8058 tree field;
8059 tree binfo;
8060 tree base_binfo;
8061 int i;
8063 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8064 out, but we'd like to be able to check this before then. */
8065 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8066 return true;
8068 for (binfo = TYPE_BINFO (type), i = 0;
8069 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8070 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8071 return false;
8072 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8073 if (TREE_CODE (field) == FIELD_DECL
8074 && !DECL_ARTIFICIAL (field)
8075 && !is_really_empty_class (TREE_TYPE (field)))
8076 return false;
8077 return true;
8079 else if (TREE_CODE (type) == ARRAY_TYPE)
8080 return is_really_empty_class (TREE_TYPE (type));
8081 return false;
8084 /* Note that NAME was looked up while the current class was being
8085 defined and that the result of that lookup was DECL. */
8087 void
8088 maybe_note_name_used_in_class (tree name, tree decl)
8090 splay_tree names_used;
8092 /* If we're not defining a class, there's nothing to do. */
8093 if (!(innermost_scope_kind() == sk_class
8094 && TYPE_BEING_DEFINED (current_class_type)
8095 && !LAMBDA_TYPE_P (current_class_type)))
8096 return;
8098 /* If there's already a binding for this NAME, then we don't have
8099 anything to worry about. */
8100 if (lookup_member (current_class_type, name,
8101 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8102 return;
8104 if (!current_class_stack[current_class_depth - 1].names_used)
8105 current_class_stack[current_class_depth - 1].names_used
8106 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8107 names_used = current_class_stack[current_class_depth - 1].names_used;
8109 splay_tree_insert (names_used,
8110 (splay_tree_key) name,
8111 (splay_tree_value) decl);
8114 /* Note that NAME was declared (as DECL) in the current class. Check
8115 to see that the declaration is valid. */
8117 void
8118 note_name_declared_in_class (tree name, tree decl)
8120 splay_tree names_used;
8121 splay_tree_node n;
8123 /* Look to see if we ever used this name. */
8124 names_used
8125 = current_class_stack[current_class_depth - 1].names_used;
8126 if (!names_used)
8127 return;
8128 /* The C language allows members to be declared with a type of the same
8129 name, and the C++ standard says this diagnostic is not required. So
8130 allow it in extern "C" blocks unless predantic is specified.
8131 Allow it in all cases if -ms-extensions is specified. */
8132 if ((!pedantic && current_lang_name == lang_name_c)
8133 || flag_ms_extensions)
8134 return;
8135 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8136 if (n)
8138 /* [basic.scope.class]
8140 A name N used in a class S shall refer to the same declaration
8141 in its context and when re-evaluated in the completed scope of
8142 S. */
8143 permerror (input_location, "declaration of %q#D", decl);
8144 permerror (location_of ((tree) n->value),
8145 "changes meaning of %qD from %q#D",
8146 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8150 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8151 Secondary vtables are merged with primary vtables; this function
8152 will return the VAR_DECL for the primary vtable. */
8154 tree
8155 get_vtbl_decl_for_binfo (tree binfo)
8157 tree decl;
8159 decl = BINFO_VTABLE (binfo);
8160 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8162 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8163 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8165 if (decl)
8166 gcc_assert (VAR_P (decl));
8167 return decl;
8171 /* Returns the binfo for the primary base of BINFO. If the resulting
8172 BINFO is a virtual base, and it is inherited elsewhere in the
8173 hierarchy, then the returned binfo might not be the primary base of
8174 BINFO in the complete object. Check BINFO_PRIMARY_P or
8175 BINFO_LOST_PRIMARY_P to be sure. */
8177 static tree
8178 get_primary_binfo (tree binfo)
8180 tree primary_base;
8182 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8183 if (!primary_base)
8184 return NULL_TREE;
8186 return copied_binfo (primary_base, binfo);
8189 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8191 static int
8192 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8194 if (!indented_p)
8195 fprintf (stream, "%*s", indent, "");
8196 return 1;
8199 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8200 INDENT should be zero when called from the top level; it is
8201 incremented recursively. IGO indicates the next expected BINFO in
8202 inheritance graph ordering. */
8204 static tree
8205 dump_class_hierarchy_r (FILE *stream,
8206 int flags,
8207 tree binfo,
8208 tree igo,
8209 int indent)
8211 int indented = 0;
8212 tree base_binfo;
8213 int i;
8215 indented = maybe_indent_hierarchy (stream, indent, 0);
8216 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8217 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8218 (HOST_WIDE_INT) (uintptr_t) binfo);
8219 if (binfo != igo)
8221 fprintf (stream, "alternative-path\n");
8222 return igo;
8224 igo = TREE_CHAIN (binfo);
8226 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8227 tree_to_shwi (BINFO_OFFSET (binfo)));
8228 if (is_empty_class (BINFO_TYPE (binfo)))
8229 fprintf (stream, " empty");
8230 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8231 fprintf (stream, " nearly-empty");
8232 if (BINFO_VIRTUAL_P (binfo))
8233 fprintf (stream, " virtual");
8234 fprintf (stream, "\n");
8236 indented = 0;
8237 if (BINFO_PRIMARY_P (binfo))
8239 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8240 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8241 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8242 TFF_PLAIN_IDENTIFIER),
8243 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8245 if (BINFO_LOST_PRIMARY_P (binfo))
8247 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8248 fprintf (stream, " lost-primary");
8250 if (indented)
8251 fprintf (stream, "\n");
8253 if (!(flags & TDF_SLIM))
8255 int indented = 0;
8257 if (BINFO_SUBVTT_INDEX (binfo))
8259 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8260 fprintf (stream, " subvttidx=%s",
8261 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8262 TFF_PLAIN_IDENTIFIER));
8264 if (BINFO_VPTR_INDEX (binfo))
8266 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8267 fprintf (stream, " vptridx=%s",
8268 expr_as_string (BINFO_VPTR_INDEX (binfo),
8269 TFF_PLAIN_IDENTIFIER));
8271 if (BINFO_VPTR_FIELD (binfo))
8273 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8274 fprintf (stream, " vbaseoffset=%s",
8275 expr_as_string (BINFO_VPTR_FIELD (binfo),
8276 TFF_PLAIN_IDENTIFIER));
8278 if (BINFO_VTABLE (binfo))
8280 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8281 fprintf (stream, " vptr=%s",
8282 expr_as_string (BINFO_VTABLE (binfo),
8283 TFF_PLAIN_IDENTIFIER));
8286 if (indented)
8287 fprintf (stream, "\n");
8290 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8291 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8293 return igo;
8296 /* Dump the BINFO hierarchy for T. */
8298 static void
8299 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8301 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8302 fprintf (stream, " size=%lu align=%lu\n",
8303 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8304 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8305 fprintf (stream, " base size=%lu base align=%lu\n",
8306 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8307 / BITS_PER_UNIT),
8308 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8309 / BITS_PER_UNIT));
8310 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8311 fprintf (stream, "\n");
8314 /* Debug interface to hierarchy dumping. */
8316 void
8317 debug_class (tree t)
8319 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8322 static void
8323 dump_class_hierarchy (tree t)
8325 int flags;
8326 FILE *stream = get_dump_info (TDI_class, &flags);
8328 if (stream)
8330 dump_class_hierarchy_1 (stream, flags, t);
8334 static void
8335 dump_array (FILE * stream, tree decl)
8337 tree value;
8338 unsigned HOST_WIDE_INT ix;
8339 HOST_WIDE_INT elt;
8340 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8342 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8343 / BITS_PER_UNIT);
8344 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8345 fprintf (stream, " %s entries",
8346 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8347 TFF_PLAIN_IDENTIFIER));
8348 fprintf (stream, "\n");
8350 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8351 ix, value)
8352 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8353 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8356 static void
8357 dump_vtable (tree t, tree binfo, tree vtable)
8359 int flags;
8360 FILE *stream = get_dump_info (TDI_class, &flags);
8362 if (!stream)
8363 return;
8365 if (!(flags & TDF_SLIM))
8367 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8369 fprintf (stream, "%s for %s",
8370 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8371 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8372 if (ctor_vtbl_p)
8374 if (!BINFO_VIRTUAL_P (binfo))
8375 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8376 (HOST_WIDE_INT) (uintptr_t) binfo);
8377 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8379 fprintf (stream, "\n");
8380 dump_array (stream, vtable);
8381 fprintf (stream, "\n");
8385 static void
8386 dump_vtt (tree t, tree vtt)
8388 int flags;
8389 FILE *stream = get_dump_info (TDI_class, &flags);
8391 if (!stream)
8392 return;
8394 if (!(flags & TDF_SLIM))
8396 fprintf (stream, "VTT for %s\n",
8397 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8398 dump_array (stream, vtt);
8399 fprintf (stream, "\n");
8403 /* Dump a function or thunk and its thunkees. */
8405 static void
8406 dump_thunk (FILE *stream, int indent, tree thunk)
8408 static const char spaces[] = " ";
8409 tree name = DECL_NAME (thunk);
8410 tree thunks;
8412 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8413 (void *)thunk,
8414 !DECL_THUNK_P (thunk) ? "function"
8415 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8416 name ? IDENTIFIER_POINTER (name) : "<unset>");
8417 if (DECL_THUNK_P (thunk))
8419 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8420 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8422 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8423 if (!virtual_adjust)
8424 /*NOP*/;
8425 else if (DECL_THIS_THUNK_P (thunk))
8426 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8427 tree_to_shwi (virtual_adjust));
8428 else
8429 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8430 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8431 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8432 if (THUNK_ALIAS (thunk))
8433 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8435 fprintf (stream, "\n");
8436 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8437 dump_thunk (stream, indent + 2, thunks);
8440 /* Dump the thunks for FN. */
8442 void
8443 debug_thunks (tree fn)
8445 dump_thunk (stderr, 0, fn);
8448 /* Virtual function table initialization. */
8450 /* Create all the necessary vtables for T and its base classes. */
8452 static void
8453 finish_vtbls (tree t)
8455 tree vbase;
8456 vec<constructor_elt, va_gc> *v = NULL;
8457 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8459 /* We lay out the primary and secondary vtables in one contiguous
8460 vtable. The primary vtable is first, followed by the non-virtual
8461 secondary vtables in inheritance graph order. */
8462 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8463 vtable, t, &v);
8465 /* Then come the virtual bases, also in inheritance graph order. */
8466 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8468 if (!BINFO_VIRTUAL_P (vbase))
8469 continue;
8470 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8473 if (BINFO_VTABLE (TYPE_BINFO (t)))
8474 initialize_vtable (TYPE_BINFO (t), v);
8477 /* Initialize the vtable for BINFO with the INITS. */
8479 static void
8480 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8482 tree decl;
8484 layout_vtable_decl (binfo, vec_safe_length (inits));
8485 decl = get_vtbl_decl_for_binfo (binfo);
8486 initialize_artificial_var (decl, inits);
8487 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8490 /* Build the VTT (virtual table table) for T.
8491 A class requires a VTT if it has virtual bases.
8493 This holds
8494 1 - primary virtual pointer for complete object T
8495 2 - secondary VTTs for each direct non-virtual base of T which requires a
8497 3 - secondary virtual pointers for each direct or indirect base of T which
8498 has virtual bases or is reachable via a virtual path from T.
8499 4 - secondary VTTs for each direct or indirect virtual base of T.
8501 Secondary VTTs look like complete object VTTs without part 4. */
8503 static void
8504 build_vtt (tree t)
8506 tree type;
8507 tree vtt;
8508 tree index;
8509 vec<constructor_elt, va_gc> *inits;
8511 /* Build up the initializers for the VTT. */
8512 inits = NULL;
8513 index = size_zero_node;
8514 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8516 /* If we didn't need a VTT, we're done. */
8517 if (!inits)
8518 return;
8520 /* Figure out the type of the VTT. */
8521 type = build_array_of_n_type (const_ptr_type_node,
8522 inits->length ());
8524 /* Now, build the VTT object itself. */
8525 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8526 initialize_artificial_var (vtt, inits);
8527 /* Add the VTT to the vtables list. */
8528 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8529 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8531 dump_vtt (t, vtt);
8534 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8535 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8536 and CHAIN the vtable pointer for this binfo after construction is
8537 complete. VALUE can also be another BINFO, in which case we recurse. */
8539 static tree
8540 binfo_ctor_vtable (tree binfo)
8542 tree vt;
8544 while (1)
8546 vt = BINFO_VTABLE (binfo);
8547 if (TREE_CODE (vt) == TREE_LIST)
8548 vt = TREE_VALUE (vt);
8549 if (TREE_CODE (vt) == TREE_BINFO)
8550 binfo = vt;
8551 else
8552 break;
8555 return vt;
8558 /* Data for secondary VTT initialization. */
8559 struct secondary_vptr_vtt_init_data
8561 /* Is this the primary VTT? */
8562 bool top_level_p;
8564 /* Current index into the VTT. */
8565 tree index;
8567 /* Vector of initializers built up. */
8568 vec<constructor_elt, va_gc> *inits;
8570 /* The type being constructed by this secondary VTT. */
8571 tree type_being_constructed;
8574 /* Recursively build the VTT-initializer for BINFO (which is in the
8575 hierarchy dominated by T). INITS points to the end of the initializer
8576 list to date. INDEX is the VTT index where the next element will be
8577 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8578 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8579 for virtual bases of T. When it is not so, we build the constructor
8580 vtables for the BINFO-in-T variant. */
8582 static void
8583 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8584 tree *index)
8586 int i;
8587 tree b;
8588 tree init;
8589 secondary_vptr_vtt_init_data data;
8590 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8592 /* We only need VTTs for subobjects with virtual bases. */
8593 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8594 return;
8596 /* We need to use a construction vtable if this is not the primary
8597 VTT. */
8598 if (!top_level_p)
8600 build_ctor_vtbl_group (binfo, t);
8602 /* Record the offset in the VTT where this sub-VTT can be found. */
8603 BINFO_SUBVTT_INDEX (binfo) = *index;
8606 /* Add the address of the primary vtable for the complete object. */
8607 init = binfo_ctor_vtable (binfo);
8608 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8609 if (top_level_p)
8611 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8612 BINFO_VPTR_INDEX (binfo) = *index;
8614 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8616 /* Recursively add the secondary VTTs for non-virtual bases. */
8617 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8618 if (!BINFO_VIRTUAL_P (b))
8619 build_vtt_inits (b, t, inits, index);
8621 /* Add secondary virtual pointers for all subobjects of BINFO with
8622 either virtual bases or reachable along a virtual path, except
8623 subobjects that are non-virtual primary bases. */
8624 data.top_level_p = top_level_p;
8625 data.index = *index;
8626 data.inits = *inits;
8627 data.type_being_constructed = BINFO_TYPE (binfo);
8629 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8631 *index = data.index;
8633 /* data.inits might have grown as we added secondary virtual pointers.
8634 Make sure our caller knows about the new vector. */
8635 *inits = data.inits;
8637 if (top_level_p)
8638 /* Add the secondary VTTs for virtual bases in inheritance graph
8639 order. */
8640 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8642 if (!BINFO_VIRTUAL_P (b))
8643 continue;
8645 build_vtt_inits (b, t, inits, index);
8647 else
8648 /* Remove the ctor vtables we created. */
8649 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8652 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8653 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8655 static tree
8656 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8658 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8660 /* We don't care about bases that don't have vtables. */
8661 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8662 return dfs_skip_bases;
8664 /* We're only interested in proper subobjects of the type being
8665 constructed. */
8666 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8667 return NULL_TREE;
8669 /* We're only interested in bases with virtual bases or reachable
8670 via a virtual path from the type being constructed. */
8671 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8672 || binfo_via_virtual (binfo, data->type_being_constructed)))
8673 return dfs_skip_bases;
8675 /* We're not interested in non-virtual primary bases. */
8676 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8677 return NULL_TREE;
8679 /* Record the index where this secondary vptr can be found. */
8680 if (data->top_level_p)
8682 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8683 BINFO_VPTR_INDEX (binfo) = data->index;
8685 if (BINFO_VIRTUAL_P (binfo))
8687 /* It's a primary virtual base, and this is not a
8688 construction vtable. Find the base this is primary of in
8689 the inheritance graph, and use that base's vtable
8690 now. */
8691 while (BINFO_PRIMARY_P (binfo))
8692 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8696 /* Add the initializer for the secondary vptr itself. */
8697 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8699 /* Advance the vtt index. */
8700 data->index = size_binop (PLUS_EXPR, data->index,
8701 TYPE_SIZE_UNIT (ptr_type_node));
8703 return NULL_TREE;
8706 /* Called from build_vtt_inits via dfs_walk. After building
8707 constructor vtables and generating the sub-vtt from them, we need
8708 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8709 binfo of the base whose sub vtt was generated. */
8711 static tree
8712 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8714 tree vtable = BINFO_VTABLE (binfo);
8716 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8717 /* If this class has no vtable, none of its bases do. */
8718 return dfs_skip_bases;
8720 if (!vtable)
8721 /* This might be a primary base, so have no vtable in this
8722 hierarchy. */
8723 return NULL_TREE;
8725 /* If we scribbled the construction vtable vptr into BINFO, clear it
8726 out now. */
8727 if (TREE_CODE (vtable) == TREE_LIST
8728 && (TREE_PURPOSE (vtable) == (tree) data))
8729 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8731 return NULL_TREE;
8734 /* Build the construction vtable group for BINFO which is in the
8735 hierarchy dominated by T. */
8737 static void
8738 build_ctor_vtbl_group (tree binfo, tree t)
8740 tree type;
8741 tree vtbl;
8742 tree id;
8743 tree vbase;
8744 vec<constructor_elt, va_gc> *v;
8746 /* See if we've already created this construction vtable group. */
8747 id = mangle_ctor_vtbl_for_type (t, binfo);
8748 if (IDENTIFIER_GLOBAL_VALUE (id))
8749 return;
8751 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8752 /* Build a version of VTBL (with the wrong type) for use in
8753 constructing the addresses of secondary vtables in the
8754 construction vtable group. */
8755 vtbl = build_vtable (t, id, ptr_type_node);
8756 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8757 /* Don't export construction vtables from shared libraries. Even on
8758 targets that don't support hidden visibility, this tells
8759 can_refer_decl_in_current_unit_p not to assume that it's safe to
8760 access from a different compilation unit (bz 54314). */
8761 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8762 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8764 v = NULL;
8765 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8766 binfo, vtbl, t, &v);
8768 /* Add the vtables for each of our virtual bases using the vbase in T
8769 binfo. */
8770 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8771 vbase;
8772 vbase = TREE_CHAIN (vbase))
8774 tree b;
8776 if (!BINFO_VIRTUAL_P (vbase))
8777 continue;
8778 b = copied_binfo (vbase, binfo);
8780 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8783 /* Figure out the type of the construction vtable. */
8784 type = build_array_of_n_type (vtable_entry_type, v->length ());
8785 layout_type (type);
8786 TREE_TYPE (vtbl) = type;
8787 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8788 layout_decl (vtbl, 0);
8790 /* Initialize the construction vtable. */
8791 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8792 initialize_artificial_var (vtbl, v);
8793 dump_vtable (t, binfo, vtbl);
8796 /* Add the vtbl initializers for BINFO (and its bases other than
8797 non-virtual primaries) to the list of INITS. BINFO is in the
8798 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8799 the constructor the vtbl inits should be accumulated for. (If this
8800 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8801 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8802 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8803 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8804 but are not necessarily the same in terms of layout. */
8806 static void
8807 accumulate_vtbl_inits (tree binfo,
8808 tree orig_binfo,
8809 tree rtti_binfo,
8810 tree vtbl,
8811 tree t,
8812 vec<constructor_elt, va_gc> **inits)
8814 int i;
8815 tree base_binfo;
8816 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8818 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8820 /* If it doesn't have a vptr, we don't do anything. */
8821 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8822 return;
8824 /* If we're building a construction vtable, we're not interested in
8825 subobjects that don't require construction vtables. */
8826 if (ctor_vtbl_p
8827 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8828 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8829 return;
8831 /* Build the initializers for the BINFO-in-T vtable. */
8832 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8834 /* Walk the BINFO and its bases. We walk in preorder so that as we
8835 initialize each vtable we can figure out at what offset the
8836 secondary vtable lies from the primary vtable. We can't use
8837 dfs_walk here because we need to iterate through bases of BINFO
8838 and RTTI_BINFO simultaneously. */
8839 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8841 /* Skip virtual bases. */
8842 if (BINFO_VIRTUAL_P (base_binfo))
8843 continue;
8844 accumulate_vtbl_inits (base_binfo,
8845 BINFO_BASE_BINFO (orig_binfo, i),
8846 rtti_binfo, vtbl, t,
8847 inits);
8851 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8852 BINFO vtable to L. */
8854 static void
8855 dfs_accumulate_vtbl_inits (tree binfo,
8856 tree orig_binfo,
8857 tree rtti_binfo,
8858 tree orig_vtbl,
8859 tree t,
8860 vec<constructor_elt, va_gc> **l)
8862 tree vtbl = NULL_TREE;
8863 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8864 int n_inits;
8866 if (ctor_vtbl_p
8867 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8869 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8870 primary virtual base. If it is not the same primary in
8871 the hierarchy of T, we'll need to generate a ctor vtable
8872 for it, to place at its location in T. If it is the same
8873 primary, we still need a VTT entry for the vtable, but it
8874 should point to the ctor vtable for the base it is a
8875 primary for within the sub-hierarchy of RTTI_BINFO.
8877 There are three possible cases:
8879 1) We are in the same place.
8880 2) We are a primary base within a lost primary virtual base of
8881 RTTI_BINFO.
8882 3) We are primary to something not a base of RTTI_BINFO. */
8884 tree b;
8885 tree last = NULL_TREE;
8887 /* First, look through the bases we are primary to for RTTI_BINFO
8888 or a virtual base. */
8889 b = binfo;
8890 while (BINFO_PRIMARY_P (b))
8892 b = BINFO_INHERITANCE_CHAIN (b);
8893 last = b;
8894 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8895 goto found;
8897 /* If we run out of primary links, keep looking down our
8898 inheritance chain; we might be an indirect primary. */
8899 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8900 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8901 break;
8902 found:
8904 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8905 base B and it is a base of RTTI_BINFO, this is case 2. In
8906 either case, we share our vtable with LAST, i.e. the
8907 derived-most base within B of which we are a primary. */
8908 if (b == rtti_binfo
8909 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8910 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8911 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8912 binfo_ctor_vtable after everything's been set up. */
8913 vtbl = last;
8915 /* Otherwise, this is case 3 and we get our own. */
8917 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8918 return;
8920 n_inits = vec_safe_length (*l);
8922 if (!vtbl)
8924 tree index;
8925 int non_fn_entries;
8927 /* Add the initializer for this vtable. */
8928 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8929 &non_fn_entries, l);
8931 /* Figure out the position to which the VPTR should point. */
8932 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8933 index = size_binop (MULT_EXPR,
8934 TYPE_SIZE_UNIT (vtable_entry_type),
8935 size_int (non_fn_entries + n_inits));
8936 vtbl = fold_build_pointer_plus (vtbl, index);
8939 if (ctor_vtbl_p)
8940 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8941 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8942 straighten this out. */
8943 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8944 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8945 /* Throw away any unneeded intializers. */
8946 (*l)->truncate (n_inits);
8947 else
8948 /* For an ordinary vtable, set BINFO_VTABLE. */
8949 BINFO_VTABLE (binfo) = vtbl;
8952 static GTY(()) tree abort_fndecl_addr;
8954 /* Construct the initializer for BINFO's virtual function table. BINFO
8955 is part of the hierarchy dominated by T. If we're building a
8956 construction vtable, the ORIG_BINFO is the binfo we should use to
8957 find the actual function pointers to put in the vtable - but they
8958 can be overridden on the path to most-derived in the graph that
8959 ORIG_BINFO belongs. Otherwise,
8960 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8961 BINFO that should be indicated by the RTTI information in the
8962 vtable; it will be a base class of T, rather than T itself, if we
8963 are building a construction vtable.
8965 The value returned is a TREE_LIST suitable for wrapping in a
8966 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8967 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8968 number of non-function entries in the vtable.
8970 It might seem that this function should never be called with a
8971 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8972 base is always subsumed by a derived class vtable. However, when
8973 we are building construction vtables, we do build vtables for
8974 primary bases; we need these while the primary base is being
8975 constructed. */
8977 static void
8978 build_vtbl_initializer (tree binfo,
8979 tree orig_binfo,
8980 tree t,
8981 tree rtti_binfo,
8982 int* non_fn_entries_p,
8983 vec<constructor_elt, va_gc> **inits)
8985 tree v;
8986 vtbl_init_data vid;
8987 unsigned ix, jx;
8988 tree vbinfo;
8989 vec<tree, va_gc> *vbases;
8990 constructor_elt *e;
8992 /* Initialize VID. */
8993 memset (&vid, 0, sizeof (vid));
8994 vid.binfo = binfo;
8995 vid.derived = t;
8996 vid.rtti_binfo = rtti_binfo;
8997 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8998 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8999 vid.generate_vcall_entries = true;
9000 /* The first vbase or vcall offset is at index -3 in the vtable. */
9001 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9003 /* Add entries to the vtable for RTTI. */
9004 build_rtti_vtbl_entries (binfo, &vid);
9006 /* Create an array for keeping track of the functions we've
9007 processed. When we see multiple functions with the same
9008 signature, we share the vcall offsets. */
9009 vec_alloc (vid.fns, 32);
9010 /* Add the vcall and vbase offset entries. */
9011 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9013 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9014 build_vbase_offset_vtbl_entries. */
9015 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9016 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9017 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9019 /* If the target requires padding between data entries, add that now. */
9020 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9022 int n_entries = vec_safe_length (vid.inits);
9024 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9026 /* Move data entries into their new positions and add padding
9027 after the new positions. Iterate backwards so we don't
9028 overwrite entries that we would need to process later. */
9029 for (ix = n_entries - 1;
9030 vid.inits->iterate (ix, &e);
9031 ix--)
9033 int j;
9034 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9035 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9037 (*vid.inits)[new_position] = *e;
9039 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9041 constructor_elt *f = &(*vid.inits)[new_position - j];
9042 f->index = NULL_TREE;
9043 f->value = build1 (NOP_EXPR, vtable_entry_type,
9044 null_pointer_node);
9049 if (non_fn_entries_p)
9050 *non_fn_entries_p = vec_safe_length (vid.inits);
9052 /* The initializers for virtual functions were built up in reverse
9053 order. Straighten them out and add them to the running list in one
9054 step. */
9055 jx = vec_safe_length (*inits);
9056 vec_safe_grow (*inits, jx + vid.inits->length ());
9058 for (ix = vid.inits->length () - 1;
9059 vid.inits->iterate (ix, &e);
9060 ix--, jx++)
9061 (**inits)[jx] = *e;
9063 /* Go through all the ordinary virtual functions, building up
9064 initializers. */
9065 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9067 tree delta;
9068 tree vcall_index;
9069 tree fn, fn_original;
9070 tree init = NULL_TREE;
9072 fn = BV_FN (v);
9073 fn_original = fn;
9074 if (DECL_THUNK_P (fn))
9076 if (!DECL_NAME (fn))
9077 finish_thunk (fn);
9078 if (THUNK_ALIAS (fn))
9080 fn = THUNK_ALIAS (fn);
9081 BV_FN (v) = fn;
9083 fn_original = THUNK_TARGET (fn);
9086 /* If the only definition of this function signature along our
9087 primary base chain is from a lost primary, this vtable slot will
9088 never be used, so just zero it out. This is important to avoid
9089 requiring extra thunks which cannot be generated with the function.
9091 We first check this in update_vtable_entry_for_fn, so we handle
9092 restored primary bases properly; we also need to do it here so we
9093 zero out unused slots in ctor vtables, rather than filling them
9094 with erroneous values (though harmless, apart from relocation
9095 costs). */
9096 if (BV_LOST_PRIMARY (v))
9097 init = size_zero_node;
9099 if (! init)
9101 /* Pull the offset for `this', and the function to call, out of
9102 the list. */
9103 delta = BV_DELTA (v);
9104 vcall_index = BV_VCALL_INDEX (v);
9106 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9107 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9109 /* You can't call an abstract virtual function; it's abstract.
9110 So, we replace these functions with __pure_virtual. */
9111 if (DECL_PURE_VIRTUAL_P (fn_original))
9113 fn = abort_fndecl;
9114 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9116 if (abort_fndecl_addr == NULL)
9117 abort_fndecl_addr
9118 = fold_convert (vfunc_ptr_type_node,
9119 build_fold_addr_expr (fn));
9120 init = abort_fndecl_addr;
9123 /* Likewise for deleted virtuals. */
9124 else if (DECL_DELETED_FN (fn_original))
9126 fn = get_identifier ("__cxa_deleted_virtual");
9127 if (!get_global_value_if_present (fn, &fn))
9128 fn = push_library_fn (fn, (build_function_type_list
9129 (void_type_node, NULL_TREE)),
9130 NULL_TREE, ECF_NORETURN);
9131 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9132 init = fold_convert (vfunc_ptr_type_node,
9133 build_fold_addr_expr (fn));
9135 else
9137 if (!integer_zerop (delta) || vcall_index)
9139 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9140 if (!DECL_NAME (fn))
9141 finish_thunk (fn);
9143 /* Take the address of the function, considering it to be of an
9144 appropriate generic type. */
9145 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9146 init = fold_convert (vfunc_ptr_type_node,
9147 build_fold_addr_expr (fn));
9148 /* Don't refer to a virtual destructor from a constructor
9149 vtable or a vtable for an abstract class, since destroying
9150 an object under construction is undefined behavior and we
9151 don't want it to be considered a candidate for speculative
9152 devirtualization. But do create the thunk for ABI
9153 compliance. */
9154 if (DECL_DESTRUCTOR_P (fn_original)
9155 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9156 || orig_binfo != binfo))
9157 init = size_zero_node;
9161 /* And add it to the chain of initializers. */
9162 if (TARGET_VTABLE_USES_DESCRIPTORS)
9164 int i;
9165 if (init == size_zero_node)
9166 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9167 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9168 else
9169 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9171 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9172 fn, build_int_cst (NULL_TREE, i));
9173 TREE_CONSTANT (fdesc) = 1;
9175 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9178 else
9179 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9183 /* Adds to vid->inits the initializers for the vbase and vcall
9184 offsets in BINFO, which is in the hierarchy dominated by T. */
9186 static void
9187 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9189 tree b;
9191 /* If this is a derived class, we must first create entries
9192 corresponding to the primary base class. */
9193 b = get_primary_binfo (binfo);
9194 if (b)
9195 build_vcall_and_vbase_vtbl_entries (b, vid);
9197 /* Add the vbase entries for this base. */
9198 build_vbase_offset_vtbl_entries (binfo, vid);
9199 /* Add the vcall entries for this base. */
9200 build_vcall_offset_vtbl_entries (binfo, vid);
9203 /* Returns the initializers for the vbase offset entries in the vtable
9204 for BINFO (which is part of the class hierarchy dominated by T), in
9205 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9206 where the next vbase offset will go. */
9208 static void
9209 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9211 tree vbase;
9212 tree t;
9213 tree non_primary_binfo;
9215 /* If there are no virtual baseclasses, then there is nothing to
9216 do. */
9217 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9218 return;
9220 t = vid->derived;
9222 /* We might be a primary base class. Go up the inheritance hierarchy
9223 until we find the most derived class of which we are a primary base:
9224 it is the offset of that which we need to use. */
9225 non_primary_binfo = binfo;
9226 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9228 tree b;
9230 /* If we have reached a virtual base, then it must be a primary
9231 base (possibly multi-level) of vid->binfo, or we wouldn't
9232 have called build_vcall_and_vbase_vtbl_entries for it. But it
9233 might be a lost primary, so just skip down to vid->binfo. */
9234 if (BINFO_VIRTUAL_P (non_primary_binfo))
9236 non_primary_binfo = vid->binfo;
9237 break;
9240 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9241 if (get_primary_binfo (b) != non_primary_binfo)
9242 break;
9243 non_primary_binfo = b;
9246 /* Go through the virtual bases, adding the offsets. */
9247 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9248 vbase;
9249 vbase = TREE_CHAIN (vbase))
9251 tree b;
9252 tree delta;
9254 if (!BINFO_VIRTUAL_P (vbase))
9255 continue;
9257 /* Find the instance of this virtual base in the complete
9258 object. */
9259 b = copied_binfo (vbase, binfo);
9261 /* If we've already got an offset for this virtual base, we
9262 don't need another one. */
9263 if (BINFO_VTABLE_PATH_MARKED (b))
9264 continue;
9265 BINFO_VTABLE_PATH_MARKED (b) = 1;
9267 /* Figure out where we can find this vbase offset. */
9268 delta = size_binop (MULT_EXPR,
9269 vid->index,
9270 convert (ssizetype,
9271 TYPE_SIZE_UNIT (vtable_entry_type)));
9272 if (vid->primary_vtbl_p)
9273 BINFO_VPTR_FIELD (b) = delta;
9275 if (binfo != TYPE_BINFO (t))
9276 /* The vbase offset had better be the same. */
9277 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9279 /* The next vbase will come at a more negative offset. */
9280 vid->index = size_binop (MINUS_EXPR, vid->index,
9281 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9283 /* The initializer is the delta from BINFO to this virtual base.
9284 The vbase offsets go in reverse inheritance-graph order, and
9285 we are walking in inheritance graph order so these end up in
9286 the right order. */
9287 delta = size_diffop_loc (input_location,
9288 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9290 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9291 fold_build1_loc (input_location, NOP_EXPR,
9292 vtable_entry_type, delta));
9296 /* Adds the initializers for the vcall offset entries in the vtable
9297 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9298 to VID->INITS. */
9300 static void
9301 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9303 /* We only need these entries if this base is a virtual base. We
9304 compute the indices -- but do not add to the vtable -- when
9305 building the main vtable for a class. */
9306 if (binfo == TYPE_BINFO (vid->derived)
9307 || (BINFO_VIRTUAL_P (binfo)
9308 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9309 correspond to VID->DERIVED), we are building a primary
9310 construction virtual table. Since this is a primary
9311 virtual table, we do not need the vcall offsets for
9312 BINFO. */
9313 && binfo != vid->rtti_binfo))
9315 /* We need a vcall offset for each of the virtual functions in this
9316 vtable. For example:
9318 class A { virtual void f (); };
9319 class B1 : virtual public A { virtual void f (); };
9320 class B2 : virtual public A { virtual void f (); };
9321 class C: public B1, public B2 { virtual void f (); };
9323 A C object has a primary base of B1, which has a primary base of A. A
9324 C also has a secondary base of B2, which no longer has a primary base
9325 of A. So the B2-in-C construction vtable needs a secondary vtable for
9326 A, which will adjust the A* to a B2* to call f. We have no way of
9327 knowing what (or even whether) this offset will be when we define B2,
9328 so we store this "vcall offset" in the A sub-vtable and look it up in
9329 a "virtual thunk" for B2::f.
9331 We need entries for all the functions in our primary vtable and
9332 in our non-virtual bases' secondary vtables. */
9333 vid->vbase = binfo;
9334 /* If we are just computing the vcall indices -- but do not need
9335 the actual entries -- not that. */
9336 if (!BINFO_VIRTUAL_P (binfo))
9337 vid->generate_vcall_entries = false;
9338 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9339 add_vcall_offset_vtbl_entries_r (binfo, vid);
9343 /* Build vcall offsets, starting with those for BINFO. */
9345 static void
9346 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9348 int i;
9349 tree primary_binfo;
9350 tree base_binfo;
9352 /* Don't walk into virtual bases -- except, of course, for the
9353 virtual base for which we are building vcall offsets. Any
9354 primary virtual base will have already had its offsets generated
9355 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9356 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9357 return;
9359 /* If BINFO has a primary base, process it first. */
9360 primary_binfo = get_primary_binfo (binfo);
9361 if (primary_binfo)
9362 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9364 /* Add BINFO itself to the list. */
9365 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9367 /* Scan the non-primary bases of BINFO. */
9368 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9369 if (base_binfo != primary_binfo)
9370 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9373 /* Called from build_vcall_offset_vtbl_entries_r. */
9375 static void
9376 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9378 /* Make entries for the rest of the virtuals. */
9379 tree orig_fn;
9381 /* The ABI requires that the methods be processed in declaration
9382 order. */
9383 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9384 orig_fn;
9385 orig_fn = DECL_CHAIN (orig_fn))
9386 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9387 add_vcall_offset (orig_fn, binfo, vid);
9390 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9392 static void
9393 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9395 size_t i;
9396 tree vcall_offset;
9397 tree derived_entry;
9399 /* If there is already an entry for a function with the same
9400 signature as FN, then we do not need a second vcall offset.
9401 Check the list of functions already present in the derived
9402 class vtable. */
9403 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9405 if (same_signature_p (derived_entry, orig_fn)
9406 /* We only use one vcall offset for virtual destructors,
9407 even though there are two virtual table entries. */
9408 || (DECL_DESTRUCTOR_P (derived_entry)
9409 && DECL_DESTRUCTOR_P (orig_fn)))
9410 return;
9413 /* If we are building these vcall offsets as part of building
9414 the vtable for the most derived class, remember the vcall
9415 offset. */
9416 if (vid->binfo == TYPE_BINFO (vid->derived))
9418 tree_pair_s elt = {orig_fn, vid->index};
9419 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9422 /* The next vcall offset will be found at a more negative
9423 offset. */
9424 vid->index = size_binop (MINUS_EXPR, vid->index,
9425 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9427 /* Keep track of this function. */
9428 vec_safe_push (vid->fns, orig_fn);
9430 if (vid->generate_vcall_entries)
9432 tree base;
9433 tree fn;
9435 /* Find the overriding function. */
9436 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9437 if (fn == error_mark_node)
9438 vcall_offset = build_zero_cst (vtable_entry_type);
9439 else
9441 base = TREE_VALUE (fn);
9443 /* The vbase we're working on is a primary base of
9444 vid->binfo. But it might be a lost primary, so its
9445 BINFO_OFFSET might be wrong, so we just use the
9446 BINFO_OFFSET from vid->binfo. */
9447 vcall_offset = size_diffop_loc (input_location,
9448 BINFO_OFFSET (base),
9449 BINFO_OFFSET (vid->binfo));
9450 vcall_offset = fold_build1_loc (input_location,
9451 NOP_EXPR, vtable_entry_type,
9452 vcall_offset);
9454 /* Add the initializer to the vtable. */
9455 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9459 /* Return vtbl initializers for the RTTI entries corresponding to the
9460 BINFO's vtable. The RTTI entries should indicate the object given
9461 by VID->rtti_binfo. */
9463 static void
9464 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9466 tree b;
9467 tree t;
9468 tree offset;
9469 tree decl;
9470 tree init;
9472 t = BINFO_TYPE (vid->rtti_binfo);
9474 /* To find the complete object, we will first convert to our most
9475 primary base, and then add the offset in the vtbl to that value. */
9476 b = binfo;
9477 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9478 && !BINFO_LOST_PRIMARY_P (b))
9480 tree primary_base;
9482 primary_base = get_primary_binfo (b);
9483 gcc_assert (BINFO_PRIMARY_P (primary_base)
9484 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9485 b = primary_base;
9487 offset = size_diffop_loc (input_location,
9488 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9490 /* The second entry is the address of the typeinfo object. */
9491 if (flag_rtti)
9492 decl = build_address (get_tinfo_decl (t));
9493 else
9494 decl = integer_zero_node;
9496 /* Convert the declaration to a type that can be stored in the
9497 vtable. */
9498 init = build_nop (vfunc_ptr_type_node, decl);
9499 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9501 /* Add the offset-to-top entry. It comes earlier in the vtable than
9502 the typeinfo entry. Convert the offset to look like a
9503 function pointer, so that we can put it in the vtable. */
9504 init = build_nop (vfunc_ptr_type_node, offset);
9505 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9508 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9509 accessibility. */
9511 bool
9512 uniquely_derived_from_p (tree parent, tree type)
9514 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9515 return base && base != error_mark_node;
9518 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9520 bool
9521 publicly_uniquely_derived_p (tree parent, tree type)
9523 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9524 NULL, tf_none);
9525 return base && base != error_mark_node;
9528 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9529 class between them, if any. */
9531 tree
9532 common_enclosing_class (tree ctx1, tree ctx2)
9534 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9535 return NULL_TREE;
9536 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9537 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9538 if (ctx1 == ctx2)
9539 return ctx1;
9540 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9541 TYPE_MARKED_P (t) = true;
9542 tree found = NULL_TREE;
9543 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9544 if (TYPE_MARKED_P (t))
9546 found = t;
9547 break;
9549 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9550 TYPE_MARKED_P (t) = false;
9551 return found;
9554 #include "gt-cp-class.h"