[RS6000] biarch test fail
[official-gcc.git] / gcc / cp / class.c
blob26f996b7f4bff5f02639646e58a562911ca29a24
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2020 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 "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static void get_basefndecls (tree, tree, vec<tree> *);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void maybe_warn_about_overly_private_class (tree);
129 static void add_implicitly_declared_members (tree, tree*, int, int);
130 static tree fixed_type_or_null (tree, int *, int *);
131 static tree build_simple_base_path (tree expr, tree binfo);
132 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
133 vec<constructor_elt, va_gc> **);
134 static bool check_bitfield_decl (tree);
135 static bool check_field_decl (tree, tree, int *, int *);
136 static void check_field_decls (tree, tree *, int *, int *);
137 static void build_base_fields (record_layout_info, splay_tree, tree *);
138 static void check_methods (tree);
139 static void remove_zero_width_bit_fields (tree);
140 static bool accessible_nvdtor_p (tree);
142 /* Used by find_flexarrays and related functions. */
143 struct flexmems_t;
144 static void diagnose_flexarrays (tree, const flexmems_t *);
145 static void find_flexarrays (tree, flexmems_t *, bool = false,
146 tree = NULL_TREE, tree = NULL_TREE);
147 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
148 static void check_bases (tree, int *, int *);
149 static void check_bases_and_members (tree);
150 static tree create_vtable_ptr (tree, tree *);
151 static void include_empty_classes (record_layout_info);
152 static void layout_class_type (tree, tree *);
153 static void propagate_binfo_offsets (tree, tree);
154 static void layout_virtual_bases (record_layout_info, splay_tree);
155 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
156 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
157 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
158 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset (tree, tree, vtbl_init_data *);
160 static void layout_vtable_decl (tree, int);
161 static tree dfs_find_final_overrider_pre (tree, void *);
162 static tree dfs_find_final_overrider_post (tree, void *);
163 static tree find_final_overrider (tree, tree, tree);
164 static int make_new_vtable (tree, tree);
165 static tree get_primary_binfo (tree);
166 static int maybe_indent_hierarchy (FILE *, int, int);
167 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
168 static void dump_class_hierarchy (tree);
169 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static void dump_thunk (FILE *, int, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
176 static void layout_nonempty_base_or_field (record_layout_info,
177 tree, tree, splay_tree);
178 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
179 vec<constructor_elt, va_gc> **);
180 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
181 vec<constructor_elt, va_gc> **);
182 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
183 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
184 static void clone_constructors_and_destructors (tree);
185 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
186 static void build_ctor_vtbl_group (tree, tree);
187 static void build_vtt (tree);
188 static tree binfo_ctor_vtable (tree);
189 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
190 tree *);
191 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
192 static tree dfs_fixup_binfo_vtbls (tree, void *);
193 static int record_subobject_offset (tree, tree, splay_tree);
194 static int check_subobject_offset (tree, tree, splay_tree);
195 static int walk_subobject_offsets (tree, subobject_offset_fn,
196 tree, splay_tree, tree, int);
197 static int layout_conflict_p (tree, tree, splay_tree, int);
198 static int splay_tree_compare_integer_csts (splay_tree_key k1,
199 splay_tree_key k2);
200 static void maybe_warn_about_inaccessible_bases (tree);
201 static bool type_requires_array_cookie (tree);
202 static bool base_derived_from (tree, tree);
203 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
204 static tree end_of_base (tree);
205 static tree get_vcall_index (tree, tree);
206 static bool type_maybe_constexpr_default_constructor (tree);
207 static bool type_maybe_constexpr_destructor (tree);
208 static bool field_poverlapping_p (tree);
210 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
211 'structor is in charge of 'structing virtual bases, or FALSE_STMT
212 otherwise. */
214 tree
215 build_if_in_charge (tree true_stmt, tree false_stmt)
217 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
218 tree cmp = build2 (NE_EXPR, boolean_type_node,
219 current_in_charge_parm, integer_zero_node);
220 tree type = unlowered_expr_type (true_stmt);
221 if (VOID_TYPE_P (type))
222 type = unlowered_expr_type (false_stmt);
223 tree cond = build3 (COND_EXPR, type,
224 cmp, true_stmt, false_stmt);
225 return cond;
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));
273 if (dependent_type_p (probe))
274 if (tree open = currently_open_class (probe))
275 probe = open;
277 if (code == PLUS_EXPR
278 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
280 /* This can happen when adjust_result_of_qualified_name_lookup can't
281 find a unique base binfo in a call to a member function. We
282 couldn't give the diagnostic then since we might have been calling
283 a static member function, so we do it now. In other cases, eg.
284 during error recovery (c++/71979), we may not have a base at all. */
285 if (complain & tf_error)
287 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
288 ba_unique, NULL, complain);
289 gcc_assert (base == error_mark_node || !base);
291 return error_mark_node;
294 gcc_assert ((code == MINUS_EXPR
295 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
296 || code == PLUS_EXPR);
298 if (binfo == d_binfo)
299 /* Nothing to do. */
300 return expr;
302 if (code == MINUS_EXPR && v_binfo)
304 if (complain & tf_error)
306 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
308 if (want_pointer)
309 error ("cannot convert from pointer to base class %qT to "
310 "pointer to derived class %qT because the base is "
311 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
312 else
313 error ("cannot convert from base class %qT to derived "
314 "class %qT because the base is virtual",
315 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
317 else
319 if (want_pointer)
320 error ("cannot convert from pointer to base class %qT to "
321 "pointer to derived class %qT via virtual base %qT",
322 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
323 BINFO_TYPE (v_binfo));
324 else
325 error ("cannot convert from base class %qT to derived "
326 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
327 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
330 return error_mark_node;
333 if (!want_pointer)
335 rvalue = !lvalue_p (expr);
336 /* This must happen before the call to save_expr. */
337 expr = cp_build_addr_expr (expr, complain);
339 else
340 expr = mark_rvalue_use (expr);
342 offset = BINFO_OFFSET (binfo);
343 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
344 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
345 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
346 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
347 expression returned matches the input. */
348 target_type = cp_build_qualified_type
349 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
350 ptr_target_type = build_pointer_type (target_type);
352 /* Do we need to look in the vtable for the real offset? */
353 virtual_access = (v_binfo && fixed_type_p <= 0);
355 /* Don't bother with the calculations inside sizeof; they'll ICE if the
356 source type is incomplete and the pointer value doesn't matter. In a
357 template (even in instantiate_non_dependent_expr), we don't have vtables
358 set up properly yet, and the value doesn't matter there either; we're
359 just interested in the result of overload resolution. */
360 if (cp_unevaluated_operand != 0
361 || processing_template_decl
362 || in_template_function ())
364 expr = build_nop (ptr_target_type, expr);
365 goto indout;
368 if (!COMPLETE_TYPE_P (probe))
370 if (complain & tf_error)
371 error ("cannot convert from %qT to base class %qT because %qT is "
372 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
373 BINFO_TYPE (d_binfo));
374 return error_mark_node;
377 /* If we're in an NSDMI, we don't have the full constructor context yet
378 that we need for converting to a virtual base, so just build a stub
379 CONVERT_EXPR and expand it later in bot_replace. */
380 if (virtual_access && fixed_type_p < 0
381 && current_scope () != current_function_decl)
383 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
384 CONVERT_EXPR_VBASE_PATH (expr) = true;
385 goto indout;
388 /* Do we need to check for a null pointer? */
389 if (want_pointer && !nonnull)
391 /* If we know the conversion will not actually change the value
392 of EXPR, then we can avoid testing the expression for NULL.
393 We have to avoid generating a COMPONENT_REF for a base class
394 field, because other parts of the compiler know that such
395 expressions are always non-NULL. */
396 if (!virtual_access && integer_zerop (offset))
397 return build_nop (ptr_target_type, expr);
398 null_test = error_mark_node;
401 /* Protect against multiple evaluation if necessary. */
402 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
403 expr = save_expr (expr);
405 /* Now that we've saved expr, build the real null test. */
406 if (null_test)
408 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
409 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
410 expr, zero);
411 /* This is a compiler generated comparison, don't emit
412 e.g. -Wnonnull-compare warning for it. */
413 TREE_NO_WARNING (null_test) = 1;
416 /* If this is a simple base reference, express it as a COMPONENT_REF. */
417 if (code == PLUS_EXPR && !virtual_access
418 /* We don't build base fields for empty bases, and they aren't very
419 interesting to the optimizers anyway. */
420 && !has_empty)
422 expr = cp_build_fold_indirect_ref (expr);
423 expr = build_simple_base_path (expr, binfo);
424 if (rvalue && lvalue_p (expr))
425 expr = move (expr);
426 if (want_pointer)
427 expr = build_address (expr);
428 target_type = TREE_TYPE (expr);
429 goto out;
432 if (virtual_access)
434 /* Going via virtual base V_BINFO. We need the static offset
435 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
436 V_BINFO. That offset is an entry in D_BINFO's vtable. */
437 tree v_offset;
439 if (fixed_type_p < 0 && in_base_initializer)
441 /* In a base member initializer, we cannot rely on the
442 vtable being set up. We have to indirect via the
443 vtt_parm. */
444 tree t;
446 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
447 t = build_pointer_type (t);
448 v_offset = fold_convert (t, current_vtt_parm);
449 v_offset = cp_build_fold_indirect_ref (v_offset);
451 else
453 tree t = expr;
454 if (sanitize_flags_p (SANITIZE_VPTR)
455 && fixed_type_p == 0)
457 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
458 probe, expr);
459 if (t == NULL_TREE)
460 t = expr;
462 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
463 TREE_TYPE (TREE_TYPE (expr)));
466 if (v_offset == error_mark_node)
467 return error_mark_node;
469 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
470 v_offset = build1 (NOP_EXPR,
471 build_pointer_type (ptrdiff_type_node),
472 v_offset);
473 v_offset = cp_build_fold_indirect_ref (v_offset);
474 TREE_CONSTANT (v_offset) = 1;
476 offset = convert_to_integer (ptrdiff_type_node,
477 size_diffop_loc (input_location, offset,
478 BINFO_OFFSET (v_binfo)));
480 if (!integer_zerop (offset))
481 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
483 if (fixed_type_p < 0)
484 /* Negative fixed_type_p means this is a constructor or destructor;
485 virtual base layout is fixed in in-charge [cd]tors, but not in
486 base [cd]tors. */
487 offset = build_if_in_charge
488 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
489 v_offset);
490 else
491 offset = v_offset;
494 if (want_pointer)
495 target_type = ptr_target_type;
497 expr = build1 (NOP_EXPR, ptr_target_type, expr);
499 if (!integer_zerop (offset))
501 offset = fold_convert (sizetype, offset);
502 if (code == MINUS_EXPR)
503 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
504 expr = fold_build_pointer_plus (expr, offset);
506 else
507 null_test = NULL;
509 indout:
510 if (!want_pointer)
512 expr = cp_build_fold_indirect_ref (expr);
513 if (rvalue)
514 expr = move (expr);
517 out:
518 if (null_test)
520 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test,
521 expr, build_zero_cst (target_type));
522 /* Avoid warning for the whole conditional expression (in addition
523 to NULL_TEST itself -- see above) in case the result is used in
524 a nonnull context that the front end -Wnonnull checks. */
525 TREE_NO_WARNING (expr) = 1;
528 return expr;
531 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
532 Perform a derived-to-base conversion by recursively building up a
533 sequence of COMPONENT_REFs to the appropriate base fields. */
535 static tree
536 build_simple_base_path (tree expr, tree binfo)
538 tree type = BINFO_TYPE (binfo);
539 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
540 tree field;
542 if (d_binfo == NULL_TREE)
544 tree temp;
546 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
548 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
549 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
550 an lvalue in the front end; only _DECLs and _REFs are lvalues
551 in the back end. */
552 temp = unary_complex_lvalue (ADDR_EXPR, expr);
553 if (temp)
554 expr = cp_build_fold_indirect_ref (temp);
556 return expr;
559 /* Recurse. */
560 expr = build_simple_base_path (expr, d_binfo);
562 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
563 field; field = DECL_CHAIN (field))
564 /* Is this the base field created by build_base_field? */
565 if (TREE_CODE (field) == FIELD_DECL
566 && DECL_FIELD_IS_BASE (field)
567 && TREE_TYPE (field) == type
568 /* If we're looking for a field in the most-derived class,
569 also check the field offset; we can have two base fields
570 of the same type if one is an indirect virtual base and one
571 is a direct non-virtual base. */
572 && (BINFO_INHERITANCE_CHAIN (d_binfo)
573 || tree_int_cst_equal (byte_position (field),
574 BINFO_OFFSET (binfo))))
576 /* We don't use build_class_member_access_expr here, as that
577 has unnecessary checks, and more importantly results in
578 recursive calls to dfs_walk_once. */
579 int type_quals = cp_type_quals (TREE_TYPE (expr));
581 expr = build3 (COMPONENT_REF,
582 cp_build_qualified_type (type, type_quals),
583 expr, field, NULL_TREE);
584 /* Mark the expression const or volatile, as appropriate.
585 Even though we've dealt with the type above, we still have
586 to mark the expression itself. */
587 if (type_quals & TYPE_QUAL_CONST)
588 TREE_READONLY (expr) = 1;
589 if (type_quals & TYPE_QUAL_VOLATILE)
590 TREE_THIS_VOLATILE (expr) = 1;
592 return expr;
595 /* Didn't find the base field?!? */
596 gcc_unreachable ();
599 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
600 type is a class type or a pointer to a class type. In the former
601 case, TYPE is also a class type; in the latter it is another
602 pointer type. If CHECK_ACCESS is true, an error message is emitted
603 if TYPE is inaccessible. If OBJECT has pointer type, the value is
604 assumed to be non-NULL. */
606 tree
607 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
608 tsubst_flags_t complain)
610 tree binfo;
611 tree object_type;
613 if (TYPE_PTR_P (TREE_TYPE (object)))
615 object_type = TREE_TYPE (TREE_TYPE (object));
616 type = TREE_TYPE (type);
618 else
619 object_type = TREE_TYPE (object);
621 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
622 NULL, complain);
623 if (!binfo || binfo == error_mark_node)
624 return error_mark_node;
626 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
629 /* EXPR is an expression with unqualified class type. BASE is a base
630 binfo of that class type. Returns EXPR, converted to the BASE
631 type. This function assumes that EXPR is the most derived class;
632 therefore virtual bases can be found at their static offsets. */
634 tree
635 convert_to_base_statically (tree expr, tree base)
637 tree expr_type;
639 expr_type = TREE_TYPE (expr);
640 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
642 /* If this is a non-empty base, use a COMPONENT_REF. */
643 if (!is_empty_class (BINFO_TYPE (base)))
644 return build_simple_base_path (expr, base);
646 /* We use fold_build2 and fold_convert below to simplify the trees
647 provided to the optimizers. It is not safe to call these functions
648 when processing a template because they do not handle C++-specific
649 trees. */
650 gcc_assert (!processing_template_decl);
651 expr = cp_build_addr_expr (expr, tf_warning_or_error);
652 if (!integer_zerop (BINFO_OFFSET (base)))
653 expr = fold_build_pointer_plus_loc (input_location,
654 expr, BINFO_OFFSET (base));
655 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
656 expr = build_fold_indirect_ref_loc (input_location, expr);
659 return expr;
663 tree
664 build_vfield_ref (tree datum, tree type)
666 tree vfield, vcontext;
668 if (datum == error_mark_node
669 /* Can happen in case of duplicate base types (c++/59082). */
670 || !TYPE_VFIELD (type))
671 return error_mark_node;
673 /* First, convert to the requested type. */
674 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
675 datum = convert_to_base (datum, type, /*check_access=*/false,
676 /*nonnull=*/true, tf_warning_or_error);
678 /* Second, the requested type may not be the owner of its own vptr.
679 If not, convert to the base class that owns it. We cannot use
680 convert_to_base here, because VCONTEXT may appear more than once
681 in the inheritance hierarchy of TYPE, and thus direct conversion
682 between the types may be ambiguous. Following the path back up
683 one step at a time via primary bases avoids the problem. */
684 vfield = TYPE_VFIELD (type);
685 vcontext = DECL_CONTEXT (vfield);
686 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
688 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
689 type = TREE_TYPE (datum);
692 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
695 /* Given an object INSTANCE, return an expression which yields the
696 vtable element corresponding to INDEX. There are many special
697 cases for INSTANCE which we take care of here, mainly to avoid
698 creating extra tree nodes when we don't have to. */
700 tree
701 build_vtbl_ref (tree instance, tree idx)
703 tree aref;
704 tree vtbl = NULL_TREE;
706 /* Try to figure out what a reference refers to, and
707 access its virtual function table directly. */
709 int cdtorp = 0;
710 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
712 tree basetype = non_reference (TREE_TYPE (instance));
714 if (fixed_type && !cdtorp)
716 tree binfo = lookup_base (fixed_type, basetype,
717 ba_unique, NULL, tf_none);
718 if (binfo && binfo != error_mark_node)
719 vtbl = unshare_expr (BINFO_VTABLE (binfo));
722 if (!vtbl)
723 vtbl = build_vfield_ref (instance, basetype);
725 aref = build_array_ref (input_location, vtbl, idx);
726 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
728 return aref;
731 /* Given a stable object pointer INSTANCE_PTR, return an expression which
732 yields a function pointer corresponding to vtable element INDEX. */
734 tree
735 build_vfn_ref (tree instance_ptr, tree idx)
737 tree aref;
739 aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
741 /* When using function descriptors, the address of the
742 vtable entry is treated as a function pointer. */
743 if (TARGET_VTABLE_USES_DESCRIPTORS)
744 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
745 cp_build_addr_expr (aref, tf_warning_or_error));
747 /* Remember this as a method reference, for later devirtualization. */
748 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
750 return aref;
753 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
754 for the given TYPE. */
756 static tree
757 get_vtable_name (tree type)
759 return mangle_vtbl_for_type (type);
762 /* DECL is an entity associated with TYPE, like a virtual table or an
763 implicitly generated constructor. Determine whether or not DECL
764 should have external or internal linkage at the object file
765 level. This routine does not deal with COMDAT linkage and other
766 similar complexities; it simply sets TREE_PUBLIC if it possible for
767 entities in other translation units to contain copies of DECL, in
768 the abstract. */
770 void
771 set_linkage_according_to_type (tree /*type*/, tree decl)
773 TREE_PUBLIC (decl) = 1;
774 determine_visibility (decl);
777 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
778 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
779 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
781 static tree
782 build_vtable (tree class_type, tree name, tree vtable_type)
784 tree decl;
786 decl = build_lang_decl (VAR_DECL, name, vtable_type);
787 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
788 now to avoid confusion in mangle_decl. */
789 SET_DECL_ASSEMBLER_NAME (decl, name);
790 DECL_CONTEXT (decl) = class_type;
791 DECL_ARTIFICIAL (decl) = 1;
792 TREE_STATIC (decl) = 1;
793 TREE_READONLY (decl) = 1;
794 DECL_VIRTUAL_P (decl) = 1;
795 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
796 DECL_USER_ALIGN (decl) = true;
797 DECL_VTABLE_OR_VTT_P (decl) = 1;
798 set_linkage_according_to_type (class_type, decl);
799 /* The vtable has not been defined -- yet. */
800 DECL_EXTERNAL (decl) = 1;
801 DECL_NOT_REALLY_EXTERN (decl) = 1;
803 /* Mark the VAR_DECL node representing the vtable itself as a
804 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
805 is rather important that such things be ignored because any
806 effort to actually generate DWARF for them will run into
807 trouble when/if we encounter code like:
809 #pragma interface
810 struct S { virtual void member (); };
812 because the artificial declaration of the vtable itself (as
813 manufactured by the g++ front end) will say that the vtable is
814 a static member of `S' but only *after* the debug output for
815 the definition of `S' has already been output. This causes
816 grief because the DWARF entry for the definition of the vtable
817 will try to refer back to an earlier *declaration* of the
818 vtable as a static member of `S' and there won't be one. We
819 might be able to arrange to have the "vtable static member"
820 attached to the member list for `S' before the debug info for
821 `S' get written (which would solve the problem) but that would
822 require more intrusive changes to the g++ front end. */
823 DECL_IGNORED_P (decl) = 1;
825 return decl;
828 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
829 or even complete. If this does not exist, create it. If COMPLETE is
830 nonzero, then complete the definition of it -- that will render it
831 impossible to actually build the vtable, but is useful to get at those
832 which are known to exist in the runtime. */
834 tree
835 get_vtable_decl (tree type, int complete)
837 tree decl;
839 if (CLASSTYPE_VTABLES (type))
840 return CLASSTYPE_VTABLES (type);
842 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
843 CLASSTYPE_VTABLES (type) = decl;
845 if (complete)
847 DECL_EXTERNAL (decl) = 1;
848 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
851 return decl;
854 /* Build the primary virtual function table for TYPE. If BINFO is
855 non-NULL, build the vtable starting with the initial approximation
856 that it is the same as the one which is the head of the association
857 list. Returns a nonzero value if a new vtable is actually
858 created. */
860 static int
861 build_primary_vtable (tree binfo, tree type)
863 tree decl;
864 tree virtuals;
866 decl = get_vtable_decl (type, /*complete=*/0);
868 if (binfo)
870 if (BINFO_NEW_VTABLE_MARKED (binfo))
871 /* We have already created a vtable for this base, so there's
872 no need to do it again. */
873 return 0;
875 virtuals = copy_list (BINFO_VIRTUALS (binfo));
876 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
877 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
878 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
880 else
882 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
883 virtuals = NULL_TREE;
886 /* Initialize the association list for this type, based
887 on our first approximation. */
888 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
889 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
890 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
891 return 1;
894 /* Give BINFO a new virtual function table which is initialized
895 with a skeleton-copy of its original initialization. The only
896 entry that changes is the `delta' entry, so we can really
897 share a lot of structure.
899 FOR_TYPE is the most derived type which caused this table to
900 be needed.
902 Returns nonzero if we haven't met BINFO before.
904 The order in which vtables are built (by calling this function) for
905 an object must remain the same, otherwise a binary incompatibility
906 can result. */
908 static int
909 build_secondary_vtable (tree binfo)
911 if (BINFO_NEW_VTABLE_MARKED (binfo))
912 /* We already created a vtable for this base. There's no need to
913 do it again. */
914 return 0;
916 /* Remember that we've created a vtable for this BINFO, so that we
917 don't try to do so again. */
918 SET_BINFO_NEW_VTABLE_MARKED (binfo);
920 /* Make fresh virtual list, so we can smash it later. */
921 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
923 /* Secondary vtables are laid out as part of the same structure as
924 the primary vtable. */
925 BINFO_VTABLE (binfo) = NULL_TREE;
926 return 1;
929 /* Create a new vtable for BINFO which is the hierarchy dominated by
930 T. Return nonzero if we actually created a new vtable. */
932 static int
933 make_new_vtable (tree t, tree binfo)
935 if (binfo == TYPE_BINFO (t))
936 /* In this case, it is *type*'s vtable we are modifying. We start
937 with the approximation that its vtable is that of the
938 immediate base class. */
939 return build_primary_vtable (binfo, t);
940 else
941 /* This is our very own copy of `basetype' to play with. Later,
942 we will fill in all the virtual functions that override the
943 virtual functions in these base classes which are not defined
944 by the current type. */
945 return build_secondary_vtable (binfo);
948 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
949 (which is in the hierarchy dominated by T) list FNDECL as its
950 BV_FN. DELTA is the required constant adjustment from the `this'
951 pointer where the vtable entry appears to the `this' required when
952 the function is actually called. */
954 static void
955 modify_vtable_entry (tree t,
956 tree binfo,
957 tree fndecl,
958 tree delta,
959 tree *virtuals)
961 tree v;
963 v = *virtuals;
965 if (fndecl != BV_FN (v)
966 || !tree_int_cst_equal (delta, BV_DELTA (v)))
968 /* We need a new vtable for BINFO. */
969 if (make_new_vtable (t, binfo))
971 /* If we really did make a new vtable, we also made a copy
972 of the BINFO_VIRTUALS list. Now, we have to find the
973 corresponding entry in that list. */
974 *virtuals = BINFO_VIRTUALS (binfo);
975 while (BV_FN (*virtuals) != BV_FN (v))
976 *virtuals = TREE_CHAIN (*virtuals);
977 v = *virtuals;
980 BV_DELTA (v) = delta;
981 BV_VCALL_INDEX (v) = NULL_TREE;
982 BV_FN (v) = fndecl;
987 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
988 METHOD is being injected via a using_decl. Returns true if the
989 method could be added to the method vec. */
991 bool
992 add_method (tree type, tree method, bool via_using)
994 if (method == error_mark_node)
995 return false;
997 gcc_assert (!DECL_EXTERN_C_P (method));
999 tree *slot = find_member_slot (type, DECL_NAME (method));
1000 tree current_fns = slot ? *slot : NULL_TREE;
1002 /* See below. */
1003 int losem = -1;
1005 /* Check to see if we've already got this method. */
1006 for (ovl_iterator iter (current_fns); iter; ++iter)
1008 tree fn = *iter;
1010 if (TREE_CODE (fn) != TREE_CODE (method))
1011 continue;
1013 /* Two using-declarations can coexist, we'll complain about ambiguity in
1014 overload resolution. */
1015 if (via_using && iter.using_p ()
1016 /* Except handle inherited constructors specially. */
1017 && ! DECL_CONSTRUCTOR_P (fn))
1018 continue;
1020 /* [over.load] Member function declarations with the
1021 same name and the same parameter types cannot be
1022 overloaded if any of them is a static member
1023 function declaration.
1025 [over.load] Member function declarations with the same name and
1026 the same parameter-type-list as well as member function template
1027 declarations with the same name, the same parameter-type-list, and
1028 the same template parameter lists cannot be overloaded if any of
1029 them, but not all, have a ref-qualifier.
1031 [namespace.udecl] When a using-declaration brings names
1032 from a base class into a derived class scope, member
1033 functions in the derived class override and/or hide member
1034 functions with the same name and parameter types in a base
1035 class (rather than conflicting). */
1036 tree fn_type = TREE_TYPE (fn);
1037 tree method_type = TREE_TYPE (method);
1039 /* Compare the quals on the 'this' parm. Don't compare
1040 the whole types, as used functions are treated as
1041 coming from the using class in overload resolution. */
1042 if (! DECL_STATIC_FUNCTION_P (fn)
1043 && ! DECL_STATIC_FUNCTION_P (method)
1044 /* Either both or neither need to be ref-qualified for
1045 differing quals to allow overloading. */
1046 && (FUNCTION_REF_QUALIFIED (fn_type)
1047 == FUNCTION_REF_QUALIFIED (method_type))
1048 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1049 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1050 continue;
1052 tree real_fn = fn;
1053 tree real_method = method;
1055 /* Templates and conversion ops must match return types. */
1056 if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1057 && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1058 continue;
1060 /* For templates, the template parameters must be identical. */
1061 if (TREE_CODE (fn) == TEMPLATE_DECL)
1063 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1064 DECL_TEMPLATE_PARMS (method)))
1065 continue;
1067 real_fn = DECL_TEMPLATE_RESULT (fn);
1068 real_method = DECL_TEMPLATE_RESULT (method);
1071 tree parms1 = TYPE_ARG_TYPES (fn_type);
1072 tree parms2 = TYPE_ARG_TYPES (method_type);
1073 if (! DECL_STATIC_FUNCTION_P (real_fn))
1074 parms1 = TREE_CHAIN (parms1);
1075 if (! DECL_STATIC_FUNCTION_P (real_method))
1076 parms2 = TREE_CHAIN (parms2);
1078 /* Bring back parameters omitted from an inherited ctor. The
1079 method and the function can have different omittedness. */
1080 if (ctor_omit_inherited_parms (real_fn))
1081 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1082 if (ctor_omit_inherited_parms (real_method))
1083 parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1084 (DECL_CLONED_FUNCTION (real_method)));
1086 if (!compparms (parms1, parms2))
1087 continue;
1089 if (!equivalently_constrained (fn, method))
1091 if (processing_template_decl)
1092 /* We can't check satisfaction in dependent context, wait until
1093 the class is instantiated. */
1094 continue;
1096 special_function_kind sfk = special_memfn_p (method);
1098 if (sfk == sfk_none
1099 || DECL_INHERITED_CTOR (fn)
1100 || TREE_CODE (fn) == TEMPLATE_DECL)
1101 /* Member function templates and non-special member functions
1102 coexist if they are not equivalently constrained. A member
1103 function is not hidden by an inherited constructor. */
1104 continue;
1106 /* P0848: For special member functions, deleted, unsatisfied, or
1107 less constrained overloads are ineligible. We implement this
1108 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1109 use the notion of eligibility, and the selected destructor can
1110 be deleted, but removing unsatisfied or less constrained
1111 overloads has the same effect as overload resolution. */
1112 bool dtor = (sfk == sfk_destructor);
1113 if (losem == -1)
1114 losem = ((!dtor && DECL_DELETED_FN (method))
1115 || !constraints_satisfied_p (method));
1116 bool losef = ((!dtor && DECL_DELETED_FN (fn))
1117 || !constraints_satisfied_p (fn));
1118 int win;
1119 if (losem || losef)
1120 win = losem - losef;
1121 else
1122 win = more_constrained (fn, method);
1123 if (win > 0)
1124 /* Leave FN in the method vec, discard METHOD. */
1125 return false;
1126 else if (win < 0)
1128 /* Remove FN, add METHOD. */
1129 current_fns = iter.remove_node (current_fns);
1130 continue;
1132 else
1133 /* Let them coexist for now. */
1134 continue;
1137 /* If these are versions of the same function, process and
1138 move on. */
1139 if (TREE_CODE (fn) == FUNCTION_DECL
1140 && maybe_version_functions (method, fn, true))
1141 continue;
1143 if (DECL_INHERITED_CTOR (method))
1145 if (!DECL_INHERITED_CTOR (fn))
1146 /* Defer to the other function. */
1147 return false;
1149 tree basem = DECL_INHERITED_CTOR_BASE (method);
1150 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1151 if (flag_new_inheriting_ctors)
1153 if (basem == basef)
1155 /* Inheriting the same constructor along different
1156 paths, combine them. */
1157 SET_DECL_INHERITED_CTOR
1158 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1159 DECL_INHERITED_CTOR (fn)));
1160 /* And discard the new one. */
1161 return false;
1163 else
1164 /* Inherited ctors can coexist until overload
1165 resolution. */
1166 continue;
1169 error_at (DECL_SOURCE_LOCATION (method),
1170 "%q#D conflicts with version inherited from %qT",
1171 method, basef);
1172 inform (DECL_SOURCE_LOCATION (fn),
1173 "version inherited from %qT declared here",
1174 basef);
1175 return false;
1178 if (via_using)
1179 /* Defer to the local function. */
1180 return false;
1181 else if (flag_new_inheriting_ctors
1182 && DECL_INHERITED_CTOR (fn))
1184 /* Remove the inherited constructor. */
1185 current_fns = iter.remove_node (current_fns);
1186 continue;
1188 else
1190 error_at (DECL_SOURCE_LOCATION (method),
1191 "%q#D cannot be overloaded with %q#D", method, fn);
1192 inform (DECL_SOURCE_LOCATION (fn),
1193 "previous declaration %q#D", fn);
1194 return false;
1198 current_fns = ovl_insert (method, current_fns, via_using);
1200 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1201 && !push_class_level_binding (DECL_NAME (method), current_fns))
1202 return false;
1204 if (!slot)
1205 slot = add_member_slot (type, DECL_NAME (method));
1207 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1208 grok_special_member_properties (method);
1210 *slot = current_fns;
1212 return true;
1215 /* Subroutines of finish_struct. */
1217 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1218 legit, otherwise return 0. */
1220 static int
1221 alter_access (tree t, tree fdecl, tree access)
1223 tree elem;
1225 retrofit_lang_decl (fdecl);
1227 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1229 elem = purpose_member (t, DECL_ACCESS (fdecl));
1230 if (elem)
1232 if (TREE_VALUE (elem) != access)
1234 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1235 error ("conflicting access specifications for method"
1236 " %q+D, ignored", TREE_TYPE (fdecl));
1237 else
1238 error ("conflicting access specifications for field %qE, ignored",
1239 DECL_NAME (fdecl));
1241 else
1243 /* They're changing the access to the same thing they changed
1244 it to before. That's OK. */
1248 else
1250 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1251 tf_warning_or_error);
1252 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1253 return 1;
1255 return 0;
1258 /* Return the access node for DECL's access in its enclosing class. */
1260 tree
1261 declared_access (tree decl)
1263 return (TREE_PRIVATE (decl) ? access_private_node
1264 : TREE_PROTECTED (decl) ? access_protected_node
1265 : access_public_node);
1268 /* Process the USING_DECL, which is a member of T. */
1270 static void
1271 handle_using_decl (tree using_decl, tree t)
1273 tree decl = USING_DECL_DECLS (using_decl);
1274 tree name = DECL_NAME (using_decl);
1275 tree access = declared_access (using_decl);
1276 tree flist = NULL_TREE;
1277 tree old_value;
1279 gcc_assert (!processing_template_decl && decl);
1281 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1282 tf_warning_or_error);
1283 if (old_value)
1285 old_value = OVL_FIRST (old_value);
1287 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1288 /* OK */;
1289 else
1290 old_value = NULL_TREE;
1293 cp_emit_debug_info_for_using (decl, t);
1295 if (is_overloaded_fn (decl))
1296 flist = decl;
1298 if (! old_value)
1300 else if (is_overloaded_fn (old_value))
1302 if (flist)
1303 /* It's OK to use functions from a base when there are functions with
1304 the same name already present in the current class. */;
1305 else
1307 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1308 "because of local method %q#D with same name",
1309 using_decl, t, old_value);
1310 inform (DECL_SOURCE_LOCATION (old_value),
1311 "local method %q#D declared here", old_value);
1312 return;
1315 else if (!DECL_ARTIFICIAL (old_value))
1317 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1318 "because of local member %q#D with same name",
1319 using_decl, t, old_value);
1320 inform (DECL_SOURCE_LOCATION (old_value),
1321 "local member %q#D declared here", old_value);
1322 return;
1325 /* Make type T see field decl FDECL with access ACCESS. */
1326 if (flist)
1327 for (ovl_iterator iter (flist); iter; ++iter)
1329 add_method (t, *iter, true);
1330 alter_access (t, *iter, access);
1332 else
1333 alter_access (t, decl, access);
1336 /* Data structure for find_abi_tags_r, below. */
1338 struct abi_tag_data
1340 tree t; // The type that we're checking for missing tags.
1341 tree subob; // The subobject of T that we're getting tags from.
1342 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1345 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1346 in the context of P. TAG can be either an identifier (the DECL_NAME of
1347 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1349 static void
1350 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1352 if (!IDENTIFIER_MARKED (id))
1354 if (p->tags != error_mark_node)
1356 /* We're collecting tags from template arguments or from
1357 the type of a variable or function return type. */
1358 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1360 /* Don't inherit this tag multiple times. */
1361 IDENTIFIER_MARKED (id) = true;
1363 if (TYPE_P (p->t))
1365 /* Tags inherited from type template arguments are only used
1366 to avoid warnings. */
1367 ABI_TAG_IMPLICIT (p->tags) = true;
1368 return;
1370 /* For functions and variables we want to warn, too. */
1373 /* Otherwise we're diagnosing missing tags. */
1374 if (TREE_CODE (p->t) == FUNCTION_DECL)
1376 auto_diagnostic_group d;
1377 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1378 "that %qT (used in its return type) has",
1379 p->t, tag, *tp))
1380 inform (location_of (*tp), "%qT declared here", *tp);
1382 else if (VAR_P (p->t))
1384 auto_diagnostic_group d;
1385 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1386 "that %qT (used in its type) has", p->t, tag, *tp))
1387 inform (location_of (*tp), "%qT declared here", *tp);
1389 else if (TYPE_P (p->subob))
1391 auto_diagnostic_group d;
1392 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1393 "that base %qT has", p->t, tag, p->subob))
1394 inform (location_of (p->subob), "%qT declared here",
1395 p->subob);
1397 else
1399 auto_diagnostic_group d;
1400 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1401 "that %qT (used in the type of %qD) has",
1402 p->t, tag, *tp, p->subob))
1404 inform (location_of (p->subob), "%qD declared here",
1405 p->subob);
1406 inform (location_of (*tp), "%qT declared here", *tp);
1412 /* Find all the ABI tags in the attribute list ATTR and either call
1413 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1415 static void
1416 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1418 if (!attr)
1419 return;
1420 for (; (attr = lookup_attribute ("abi_tag", attr));
1421 attr = TREE_CHAIN (attr))
1422 for (tree list = TREE_VALUE (attr); list;
1423 list = TREE_CHAIN (list))
1425 tree tag = TREE_VALUE (list);
1426 tree id = get_identifier (TREE_STRING_POINTER (tag));
1427 if (tp)
1428 check_tag (tag, id, tp, p);
1429 else
1430 IDENTIFIER_MARKED (id) = val;
1434 /* Find all the ABI tags on T and its enclosing scopes and either call
1435 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1437 static void
1438 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1440 while (t != global_namespace)
1442 tree attr;
1443 if (TYPE_P (t))
1445 attr = TYPE_ATTRIBUTES (t);
1446 t = CP_TYPE_CONTEXT (t);
1448 else
1450 attr = DECL_ATTRIBUTES (t);
1451 t = CP_DECL_CONTEXT (t);
1453 mark_or_check_attr_tags (attr, tp, p, val);
1457 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1458 types with ABI tags, add the corresponding identifiers to the VEC in
1459 *DATA and set IDENTIFIER_MARKED. */
1461 static tree
1462 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1464 if (!OVERLOAD_TYPE_P (*tp))
1465 return NULL_TREE;
1467 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1468 anyway, but let's make sure of it. */
1469 *walk_subtrees = false;
1471 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1473 mark_or_check_tags (*tp, tp, p, false);
1475 return NULL_TREE;
1478 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1479 IDENTIFIER_MARKED on its ABI tags. */
1481 static tree
1482 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1484 if (!OVERLOAD_TYPE_P (*tp))
1485 return NULL_TREE;
1487 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1488 anyway, but let's make sure of it. */
1489 *walk_subtrees = false;
1491 bool *valp = static_cast<bool*>(data);
1493 mark_or_check_tags (*tp, NULL, NULL, *valp);
1495 return NULL_TREE;
1498 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1499 scopes. */
1501 static void
1502 mark_abi_tags (tree t, bool val)
1504 mark_or_check_tags (t, NULL, NULL, val);
1505 if (DECL_P (t))
1507 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1508 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1510 /* Template arguments are part of the signature. */
1511 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1512 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1514 tree arg = TREE_VEC_ELT (level, j);
1515 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1518 if (TREE_CODE (t) == FUNCTION_DECL)
1519 /* A function's parameter types are part of the signature, so
1520 we don't need to inherit any tags that are also in them. */
1521 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1522 arg = TREE_CHAIN (arg))
1523 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1524 mark_abi_tags_r, &val);
1528 /* Check that T has all the ABI tags that subobject SUBOB has, or
1529 warn if not. If T is a (variable or function) declaration, also
1530 return any missing tags, and add them to T if JUST_CHECKING is false. */
1532 static tree
1533 check_abi_tags (tree t, tree subob, bool just_checking = false)
1535 bool inherit = DECL_P (t);
1537 if (!inherit && !warn_abi_tag)
1538 return NULL_TREE;
1540 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1541 if (!TREE_PUBLIC (decl))
1542 /* No need to worry about things local to this TU. */
1543 return NULL_TREE;
1545 mark_abi_tags (t, true);
1547 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1548 struct abi_tag_data data = { t, subob, error_mark_node };
1549 if (inherit)
1550 data.tags = NULL_TREE;
1552 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1554 if (!(inherit && data.tags))
1555 /* We don't need to do anything with data.tags. */;
1556 else if (just_checking)
1557 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1559 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1560 IDENTIFIER_MARKED (id) = false;
1562 else
1564 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1565 if (attr)
1566 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1567 else
1568 DECL_ATTRIBUTES (t)
1569 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1572 mark_abi_tags (t, false);
1574 return data.tags;
1577 /* Check that DECL has all the ABI tags that are used in parts of its type
1578 that are not reflected in its mangled name. */
1580 void
1581 check_abi_tags (tree decl)
1583 if (VAR_P (decl))
1584 check_abi_tags (decl, TREE_TYPE (decl));
1585 else if (TREE_CODE (decl) == FUNCTION_DECL
1586 && !DECL_CONV_FN_P (decl)
1587 && !mangle_return_type_p (decl))
1588 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1591 /* Return any ABI tags that are used in parts of the type of DECL
1592 that are not reflected in its mangled name. This function is only
1593 used in backward-compatible mangling for ABI <11. */
1595 tree
1596 missing_abi_tags (tree decl)
1598 if (VAR_P (decl))
1599 return check_abi_tags (decl, TREE_TYPE (decl), true);
1600 else if (TREE_CODE (decl) == FUNCTION_DECL
1601 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1602 that we can use this function for setting need_abi_warning
1603 regardless of the current flag_abi_version. */
1604 && !mangle_return_type_p (decl))
1605 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1606 else
1607 return NULL_TREE;
1610 void
1611 inherit_targ_abi_tags (tree t)
1613 if (!CLASS_TYPE_P (t)
1614 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1615 return;
1617 mark_abi_tags (t, true);
1619 tree args = CLASSTYPE_TI_ARGS (t);
1620 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1621 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1623 tree level = TMPL_ARGS_LEVEL (args, i+1);
1624 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1626 tree arg = TREE_VEC_ELT (level, j);
1627 data.subob = arg;
1628 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1632 // If we found some tags on our template arguments, add them to our
1633 // abi_tag attribute.
1634 if (data.tags)
1636 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1637 if (attr)
1638 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1639 else
1640 TYPE_ATTRIBUTES (t)
1641 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1644 mark_abi_tags (t, false);
1647 /* Return true, iff class T has a non-virtual destructor that is
1648 accessible from outside the class heirarchy (i.e. is public, or
1649 there's a suitable friend. */
1651 static bool
1652 accessible_nvdtor_p (tree t)
1654 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1656 /* An implicitly declared destructor is always public. And,
1657 if it were virtual, we would have created it by now. */
1658 if (!dtor)
1659 return true;
1661 if (DECL_VINDEX (dtor))
1662 return false; /* Virtual */
1664 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1665 return true; /* Public */
1667 if (CLASSTYPE_FRIEND_CLASSES (t)
1668 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1669 return true; /* Has friends */
1671 return false;
1674 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1675 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1676 properties of the bases. */
1678 static void
1679 check_bases (tree t,
1680 int* cant_have_const_ctor_p,
1681 int* no_const_asn_ref_p)
1683 int i;
1684 bool seen_non_virtual_nearly_empty_base_p = 0;
1685 int seen_tm_mask = 0;
1686 tree base_binfo;
1687 tree binfo;
1688 tree field = NULL_TREE;
1690 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1691 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1692 if (TREE_CODE (field) == FIELD_DECL)
1693 break;
1695 for (binfo = TYPE_BINFO (t), i = 0;
1696 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1698 tree basetype = TREE_TYPE (base_binfo);
1700 gcc_assert (COMPLETE_TYPE_P (basetype));
1702 if (CLASSTYPE_FINAL (basetype))
1703 error ("cannot derive from %<final%> base %qT in derived type %qT",
1704 basetype, t);
1706 /* If any base class is non-literal, so is the derived class. */
1707 if (!CLASSTYPE_LITERAL_P (basetype))
1708 CLASSTYPE_LITERAL_P (t) = false;
1710 /* If the base class doesn't have copy constructors or
1711 assignment operators that take const references, then the
1712 derived class cannot have such a member automatically
1713 generated. */
1714 if (TYPE_HAS_COPY_CTOR (basetype)
1715 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1716 *cant_have_const_ctor_p = 1;
1717 if (TYPE_HAS_COPY_ASSIGN (basetype)
1718 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1719 *no_const_asn_ref_p = 1;
1721 if (BINFO_VIRTUAL_P (base_binfo))
1722 /* A virtual base does not effect nearly emptiness. */
1724 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1726 if (seen_non_virtual_nearly_empty_base_p)
1727 /* And if there is more than one nearly empty base, then the
1728 derived class is not nearly empty either. */
1729 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1730 else
1731 /* Remember we've seen one. */
1732 seen_non_virtual_nearly_empty_base_p = 1;
1734 else if (!is_empty_class (basetype))
1735 /* If the base class is not empty or nearly empty, then this
1736 class cannot be nearly empty. */
1737 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1739 /* A lot of properties from the bases also apply to the derived
1740 class. */
1741 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1742 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1743 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1744 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1745 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1746 || !TYPE_HAS_COPY_ASSIGN (basetype));
1747 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1748 || !TYPE_HAS_COPY_CTOR (basetype));
1749 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1750 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1751 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1752 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1753 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1754 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1755 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1756 || TYPE_HAS_COMPLEX_DFLT (basetype));
1757 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1758 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1759 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1760 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1761 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1762 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1763 if (TYPE_HAS_MUTABLE_P (basetype))
1764 CLASSTYPE_HAS_MUTABLE (t) = 1;
1766 /* A standard-layout class is a class that:
1768 * has no non-standard-layout base classes, */
1769 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1770 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1772 tree basefield;
1773 /* ...has no base classes of the same type as the first non-static
1774 data member... */
1775 if (field && DECL_CONTEXT (field) == t
1776 && (same_type_ignoring_top_level_qualifiers_p
1777 (TREE_TYPE (field), basetype)))
1778 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1779 /* DR 1813:
1780 ...has at most one base class subobject of any given type... */
1781 else if (CLASSTYPE_REPEATED_BASE_P (t))
1782 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1783 else
1784 /* ...either has no non-static data members in the most-derived
1785 class and at most one base class with non-static data
1786 members, or has no base classes with non-static data
1787 members. FIXME This was reworded in DR 1813. */
1788 for (basefield = TYPE_FIELDS (basetype); basefield;
1789 basefield = DECL_CHAIN (basefield))
1790 if (TREE_CODE (basefield) == FIELD_DECL
1791 && !(DECL_FIELD_IS_BASE (basefield)
1792 && integer_zerop (DECL_SIZE (basefield))))
1794 if (field)
1795 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1796 else
1797 field = basefield;
1798 break;
1802 /* Don't bother collecting tm attributes if transactional memory
1803 support is not enabled. */
1804 if (flag_tm)
1806 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1807 if (tm_attr)
1808 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1811 check_abi_tags (t, basetype);
1814 /* If one of the base classes had TM attributes, and the current class
1815 doesn't define its own, then the current class inherits one. */
1816 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1818 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1819 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1823 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1824 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1825 that have had a nearly-empty virtual primary base stolen by some
1826 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1827 T. */
1829 static void
1830 determine_primary_bases (tree t)
1832 unsigned i;
1833 tree primary = NULL_TREE;
1834 tree type_binfo = TYPE_BINFO (t);
1835 tree base_binfo;
1837 /* Determine the primary bases of our bases. */
1838 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1839 base_binfo = TREE_CHAIN (base_binfo))
1841 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1843 /* See if we're the non-virtual primary of our inheritance
1844 chain. */
1845 if (!BINFO_VIRTUAL_P (base_binfo))
1847 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1848 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1850 if (parent_primary
1851 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1852 BINFO_TYPE (parent_primary)))
1853 /* We are the primary binfo. */
1854 BINFO_PRIMARY_P (base_binfo) = 1;
1856 /* Determine if we have a virtual primary base, and mark it so.
1858 if (primary && BINFO_VIRTUAL_P (primary))
1860 tree this_primary = copied_binfo (primary, base_binfo);
1862 if (BINFO_PRIMARY_P (this_primary))
1863 /* Someone already claimed this base. */
1864 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1865 else
1867 tree delta;
1869 BINFO_PRIMARY_P (this_primary) = 1;
1870 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1872 /* A virtual binfo might have been copied from within
1873 another hierarchy. As we're about to use it as a
1874 primary base, make sure the offsets match. */
1875 delta = size_diffop_loc (input_location,
1876 fold_convert (ssizetype,
1877 BINFO_OFFSET (base_binfo)),
1878 fold_convert (ssizetype,
1879 BINFO_OFFSET (this_primary)));
1881 propagate_binfo_offsets (this_primary, delta);
1886 /* First look for a dynamic direct non-virtual base. */
1887 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1889 tree basetype = BINFO_TYPE (base_binfo);
1891 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1893 primary = base_binfo;
1894 goto found;
1898 /* A "nearly-empty" virtual base class can be the primary base
1899 class, if no non-virtual polymorphic base can be found. Look for
1900 a nearly-empty virtual dynamic base that is not already a primary
1901 base of something in the hierarchy. If there is no such base,
1902 just pick the first nearly-empty virtual base. */
1904 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1905 base_binfo = TREE_CHAIN (base_binfo))
1906 if (BINFO_VIRTUAL_P (base_binfo)
1907 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1909 if (!BINFO_PRIMARY_P (base_binfo))
1911 /* Found one that is not primary. */
1912 primary = base_binfo;
1913 goto found;
1915 else if (!primary)
1916 /* Remember the first candidate. */
1917 primary = base_binfo;
1920 found:
1921 /* If we've got a primary base, use it. */
1922 if (primary)
1924 tree basetype = BINFO_TYPE (primary);
1926 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1927 if (BINFO_PRIMARY_P (primary))
1928 /* We are stealing a primary base. */
1929 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1930 BINFO_PRIMARY_P (primary) = 1;
1931 if (BINFO_VIRTUAL_P (primary))
1933 tree delta;
1935 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1936 /* A virtual binfo might have been copied from within
1937 another hierarchy. As we're about to use it as a primary
1938 base, make sure the offsets match. */
1939 delta = size_diffop_loc (input_location, ssize_int (0),
1940 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1942 propagate_binfo_offsets (primary, delta);
1945 primary = TYPE_BINFO (basetype);
1947 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1948 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1949 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1953 /* Update the variant types of T. */
1955 void
1956 fixup_type_variants (tree t)
1958 tree variants;
1960 if (!t)
1961 return;
1963 for (variants = TYPE_NEXT_VARIANT (t);
1964 variants;
1965 variants = TYPE_NEXT_VARIANT (variants))
1967 /* These fields are in the _TYPE part of the node, not in
1968 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1969 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1970 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1971 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1972 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1974 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1975 CLASSTYPE_FINAL (variants) = CLASSTYPE_FINAL (t);
1977 TYPE_BINFO (variants) = TYPE_BINFO (t);
1979 /* Copy whatever these are holding today. */
1980 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1981 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1983 TYPE_SIZE (variants) = TYPE_SIZE (t);
1984 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1988 /* KLASS is a class that we're applying may_alias to after the body is
1989 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1990 canonical type(s) will be implicitly updated. */
1992 static void
1993 fixup_may_alias (tree klass)
1995 tree t, v;
1997 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1998 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1999 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2000 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2001 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2002 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2005 /* Early variant fixups: we apply attributes at the beginning of the class
2006 definition, and we need to fix up any variants that have already been
2007 made via elaborated-type-specifier so that check_qualified_type works. */
2009 void
2010 fixup_attribute_variants (tree t)
2012 tree variants;
2014 if (!t)
2015 return;
2017 tree attrs = TYPE_ATTRIBUTES (t);
2018 unsigned align = TYPE_ALIGN (t);
2019 bool user_align = TYPE_USER_ALIGN (t);
2020 bool may_alias = lookup_attribute ("may_alias", attrs);
2021 bool packed = TYPE_PACKED (t);
2023 if (may_alias)
2024 fixup_may_alias (t);
2026 for (variants = TYPE_NEXT_VARIANT (t);
2027 variants;
2028 variants = TYPE_NEXT_VARIANT (variants))
2030 /* These are the two fields that check_qualified_type looks at and
2031 are affected by attributes. */
2032 TYPE_ATTRIBUTES (variants) = attrs;
2033 unsigned valign = align;
2034 if (TYPE_USER_ALIGN (variants))
2035 valign = MAX (valign, TYPE_ALIGN (variants));
2036 else
2037 TYPE_USER_ALIGN (variants) = user_align;
2038 SET_TYPE_ALIGN (variants, valign);
2039 TYPE_PACKED (variants) = packed;
2040 if (may_alias)
2041 fixup_may_alias (variants);
2045 /* Set memoizing fields and bits of T (and its variants) for later
2046 use. */
2048 static void
2049 finish_struct_bits (tree t)
2051 /* Fix up variants (if any). */
2052 fixup_type_variants (t);
2054 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2055 /* For a class w/o baseclasses, 'finish_struct' has set
2056 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2057 Similarly for a class whose base classes do not have vtables.
2058 When neither of these is true, we might have removed abstract
2059 virtuals (by providing a definition), added some (by declaring
2060 new ones), or redeclared ones from a base class. We need to
2061 recalculate what's really an abstract virtual at this point (by
2062 looking in the vtables). */
2063 get_pure_virtuals (t);
2065 /* If this type has a copy constructor or a destructor, force its
2066 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2067 nonzero. This will cause it to be passed by invisible reference
2068 and prevent it from being returned in a register. */
2069 if (type_has_nontrivial_copy_init (t)
2070 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2072 tree variants;
2073 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2074 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2076 SET_TYPE_MODE (variants, BLKmode);
2077 TREE_ADDRESSABLE (variants) = 1;
2082 /* Issue warnings about T having private constructors, but no friends,
2083 and so forth.
2085 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2086 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2087 non-private static member functions. */
2089 static void
2090 maybe_warn_about_overly_private_class (tree t)
2092 int has_member_fn = 0;
2093 int has_nonprivate_method = 0;
2094 bool nonprivate_ctor = false;
2096 if (!warn_ctor_dtor_privacy
2097 /* If the class has friends, those entities might create and
2098 access instances, so we should not warn. */
2099 || (CLASSTYPE_FRIEND_CLASSES (t)
2100 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2101 /* We will have warned when the template was declared; there's
2102 no need to warn on every instantiation. */
2103 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2104 /* There's no reason to even consider warning about this
2105 class. */
2106 return;
2108 /* We only issue one warning, if more than one applies, because
2109 otherwise, on code like:
2111 class A {
2112 // Oops - forgot `public:'
2113 A();
2114 A(const A&);
2115 ~A();
2118 we warn several times about essentially the same problem. */
2120 /* Check to see if all (non-constructor, non-destructor) member
2121 functions are private. (Since there are no friends or
2122 non-private statics, we can't ever call any of the private member
2123 functions.) */
2124 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2125 if (TREE_CODE (fn) == USING_DECL
2126 && DECL_NAME (fn) == ctor_identifier
2127 && !TREE_PRIVATE (fn))
2128 nonprivate_ctor = true;
2129 else if (!DECL_DECLARES_FUNCTION_P (fn))
2130 /* Not a function. */;
2131 else if (DECL_ARTIFICIAL (fn))
2132 /* We're not interested in compiler-generated methods; they don't
2133 provide any way to call private members. */;
2134 else if (!TREE_PRIVATE (fn))
2136 if (DECL_STATIC_FUNCTION_P (fn))
2137 /* A non-private static member function is just like a
2138 friend; it can create and invoke private member
2139 functions, and be accessed without a class
2140 instance. */
2141 return;
2143 has_nonprivate_method = 1;
2144 /* Keep searching for a static member function. */
2146 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2147 has_member_fn = 1;
2149 if (!has_nonprivate_method && has_member_fn)
2151 /* There are no non-private methods, and there's at least one
2152 private member function that isn't a constructor or
2153 destructor. (If all the private members are
2154 constructors/destructors we want to use the code below that
2155 issues error messages specifically referring to
2156 constructors/destructors.) */
2157 unsigned i;
2158 tree binfo = TYPE_BINFO (t);
2160 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2161 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2163 has_nonprivate_method = 1;
2164 break;
2166 if (!has_nonprivate_method)
2168 warning (OPT_Wctor_dtor_privacy,
2169 "all member functions in class %qT are private", t);
2170 return;
2174 /* Even if some of the member functions are non-private, the class
2175 won't be useful for much if all the constructors or destructors
2176 are private: such an object can never be created or destroyed. */
2177 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2178 if (TREE_PRIVATE (dtor))
2180 warning (OPT_Wctor_dtor_privacy,
2181 "%q#T only defines a private destructor and has no friends",
2183 return;
2186 /* Warn about classes that have private constructors and no friends. */
2187 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2188 /* Implicitly generated constructors are always public. */
2189 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2191 tree copy_or_move = NULL_TREE;
2193 /* If a non-template class does not define a copy
2194 constructor, one is defined for it, enabling it to avoid
2195 this warning. For a template class, this does not
2196 happen, and so we would normally get a warning on:
2198 template <class T> class C { private: C(); };
2200 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2201 complete non-template or fully instantiated classes have this
2202 flag set. */
2203 if (!TYPE_HAS_COPY_CTOR (t))
2204 nonprivate_ctor = true;
2205 else
2206 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2207 !nonprivate_ctor && iter; ++iter)
2208 if (TREE_PRIVATE (*iter))
2209 continue;
2210 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2211 /* Ideally, we wouldn't count any constructor that takes
2212 an argument of the class type as a parameter, because
2213 such things cannot be used to construct an instance of
2214 the class unless you already have one. */
2215 copy_or_move = *iter;
2216 else
2217 nonprivate_ctor = true;
2219 if (!nonprivate_ctor)
2221 bool w = warning (OPT_Wctor_dtor_privacy,
2222 "%q#T only defines private constructors and has "
2223 "no friends", t);
2224 if (w && copy_or_move)
2225 inform (DECL_SOURCE_LOCATION (copy_or_move),
2226 "%q#D is public, but requires an existing %q#T object",
2227 copy_or_move, t);
2228 return;
2233 /* Make BINFO's vtable have N entries, including RTTI entries,
2234 vbase and vcall offsets, etc. Set its type and call the back end
2235 to lay it out. */
2237 static void
2238 layout_vtable_decl (tree binfo, int n)
2240 tree atype;
2241 tree vtable;
2243 atype = build_array_of_n_type (vtable_entry_type, n);
2244 layout_type (atype);
2246 /* We may have to grow the vtable. */
2247 vtable = get_vtbl_decl_for_binfo (binfo);
2248 if (!same_type_p (TREE_TYPE (vtable), atype))
2250 TREE_TYPE (vtable) = atype;
2251 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2252 layout_decl (vtable, 0);
2256 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2257 have the same signature. */
2260 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2262 /* One destructor overrides another if they are the same kind of
2263 destructor. */
2264 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2265 && special_function_p (base_fndecl) == special_function_p (fndecl))
2266 return 1;
2267 /* But a non-destructor never overrides a destructor, nor vice
2268 versa, nor do different kinds of destructors override
2269 one-another. For example, a complete object destructor does not
2270 override a deleting destructor. */
2271 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2272 return 0;
2274 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2275 || (DECL_CONV_FN_P (fndecl)
2276 && DECL_CONV_FN_P (base_fndecl)
2277 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2278 DECL_CONV_FN_TYPE (base_fndecl))))
2280 tree fntype = TREE_TYPE (fndecl);
2281 tree base_fntype = TREE_TYPE (base_fndecl);
2282 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2283 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2284 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2285 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2286 return 1;
2288 return 0;
2291 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2292 subobject. */
2294 static bool
2295 base_derived_from (tree derived, tree base)
2297 tree probe;
2299 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2301 if (probe == derived)
2302 return true;
2303 else if (BINFO_VIRTUAL_P (probe))
2304 /* If we meet a virtual base, we can't follow the inheritance
2305 any more. See if the complete type of DERIVED contains
2306 such a virtual base. */
2307 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2308 != NULL_TREE);
2310 return false;
2313 struct find_final_overrider_data {
2314 /* The function for which we are trying to find a final overrider. */
2315 tree fn;
2316 /* The base class in which the function was declared. */
2317 tree declaring_base;
2318 /* The candidate overriders. */
2319 tree candidates;
2320 /* Path to most derived. */
2321 vec<tree> path;
2324 /* Add the overrider along the current path to FFOD->CANDIDATES.
2325 Returns true if an overrider was found; false otherwise. */
2327 static bool
2328 dfs_find_final_overrider_1 (tree binfo,
2329 find_final_overrider_data *ffod,
2330 unsigned depth)
2332 tree method;
2334 /* If BINFO is not the most derived type, try a more derived class.
2335 A definition there will overrider a definition here. */
2336 if (depth)
2338 depth--;
2339 if (dfs_find_final_overrider_1
2340 (ffod->path[depth], ffod, depth))
2341 return true;
2344 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2345 if (method)
2347 tree *candidate = &ffod->candidates;
2349 /* Remove any candidates overridden by this new function. */
2350 while (*candidate)
2352 /* If *CANDIDATE overrides METHOD, then METHOD
2353 cannot override anything else on the list. */
2354 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2355 return true;
2356 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2357 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2358 *candidate = TREE_CHAIN (*candidate);
2359 else
2360 candidate = &TREE_CHAIN (*candidate);
2363 /* Add the new function. */
2364 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2365 return true;
2368 return false;
2371 /* Called from find_final_overrider via dfs_walk. */
2373 static tree
2374 dfs_find_final_overrider_pre (tree binfo, void *data)
2376 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2378 if (binfo == ffod->declaring_base)
2379 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2380 ffod->path.safe_push (binfo);
2382 return NULL_TREE;
2385 static tree
2386 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2388 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2389 ffod->path.pop ();
2391 return NULL_TREE;
2394 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2395 FN and whose TREE_VALUE is the binfo for the base where the
2396 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2397 DERIVED) is the base object in which FN is declared. */
2399 static tree
2400 find_final_overrider (tree derived, tree binfo, tree fn)
2402 find_final_overrider_data ffod;
2404 /* Getting this right is a little tricky. This is valid:
2406 struct S { virtual void f (); };
2407 struct T { virtual void f (); };
2408 struct U : public S, public T { };
2410 even though calling `f' in `U' is ambiguous. But,
2412 struct R { virtual void f(); };
2413 struct S : virtual public R { virtual void f (); };
2414 struct T : virtual public R { virtual void f (); };
2415 struct U : public S, public T { };
2417 is not -- there's no way to decide whether to put `S::f' or
2418 `T::f' in the vtable for `R'.
2420 The solution is to look at all paths to BINFO. If we find
2421 different overriders along any two, then there is a problem. */
2422 if (DECL_THUNK_P (fn))
2423 fn = THUNK_TARGET (fn);
2425 /* Determine the depth of the hierarchy. */
2426 ffod.fn = fn;
2427 ffod.declaring_base = binfo;
2428 ffod.candidates = NULL_TREE;
2429 ffod.path.create (30);
2431 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2432 dfs_find_final_overrider_post, &ffod);
2434 ffod.path.release ();
2436 /* If there was no winner, issue an error message. */
2437 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2438 return error_mark_node;
2440 return ffod.candidates;
2443 /* Return the index of the vcall offset for FN when TYPE is used as a
2444 virtual base. */
2446 static tree
2447 get_vcall_index (tree fn, tree type)
2449 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2450 tree_pair_p p;
2451 unsigned ix;
2453 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2454 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2455 || same_signature_p (fn, p->purpose))
2456 return p->value;
2458 /* There should always be an appropriate index. */
2459 gcc_unreachable ();
2462 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2463 overrider at that index in the vtable. This should only be used when we
2464 know that BINFO is correct for the dynamic type of the object. */
2466 tree
2467 lookup_vfn_in_binfo (tree idx, tree binfo)
2469 int ix = tree_to_shwi (idx);
2470 if (TARGET_VTABLE_USES_DESCRIPTORS)
2471 ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2472 while (BINFO_PRIMARY_P (binfo))
2473 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2474 class that actually owns the vtable. */
2475 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2476 tree virtuals = BINFO_VIRTUALS (binfo);
2477 return TREE_VALUE (chain_index (ix, virtuals));
2480 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2481 dominated by T. FN is the old function; VIRTUALS points to the
2482 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2483 of that entry in the list. */
2485 static void
2486 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2487 unsigned ix)
2489 tree b;
2490 tree overrider;
2491 tree delta;
2492 tree virtual_base;
2493 tree first_defn;
2494 tree overrider_fn, overrider_target;
2495 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2496 tree over_return, base_return;
2497 bool lost = false;
2499 /* Find the nearest primary base (possibly binfo itself) which defines
2500 this function; this is the class the caller will convert to when
2501 calling FN through BINFO. */
2502 for (b = binfo; ; b = get_primary_binfo (b))
2504 gcc_assert (b);
2505 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2506 break;
2508 /* The nearest definition is from a lost primary. */
2509 if (BINFO_LOST_PRIMARY_P (b))
2510 lost = true;
2512 first_defn = b;
2514 /* Find the final overrider. */
2515 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2516 if (overrider == error_mark_node)
2518 error ("no unique final overrider for %qD in %qT", target_fn, t);
2519 return;
2521 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2523 /* Check for adjusting covariant return types. */
2524 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2525 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2527 if (INDIRECT_TYPE_P (over_return)
2528 && TREE_CODE (over_return) == TREE_CODE (base_return)
2529 && CLASS_TYPE_P (TREE_TYPE (over_return))
2530 && CLASS_TYPE_P (TREE_TYPE (base_return))
2531 /* If the overrider is invalid, don't even try. */
2532 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2534 /* If FN is a covariant thunk, we must figure out the adjustment
2535 to the final base FN was converting to. As OVERRIDER_TARGET might
2536 also be converting to the return type of FN, we have to
2537 combine the two conversions here. */
2538 tree fixed_offset, virtual_offset;
2540 over_return = TREE_TYPE (over_return);
2541 base_return = TREE_TYPE (base_return);
2543 if (DECL_THUNK_P (fn))
2545 gcc_assert (DECL_RESULT_THUNK_P (fn));
2546 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2547 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2549 else
2550 fixed_offset = virtual_offset = NULL_TREE;
2552 if (virtual_offset)
2553 /* Find the equivalent binfo within the return type of the
2554 overriding function. We will want the vbase offset from
2555 there. */
2556 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2557 over_return);
2558 else if (!same_type_ignoring_top_level_qualifiers_p
2559 (over_return, base_return))
2561 /* There was no existing virtual thunk (which takes
2562 precedence). So find the binfo of the base function's
2563 return type within the overriding function's return type.
2564 Fortunately we know the covariancy is valid (it
2565 has already been checked), so we can just iterate along
2566 the binfos, which have been chained in inheritance graph
2567 order. Of course it is lame that we have to repeat the
2568 search here anyway -- we should really be caching pieces
2569 of the vtable and avoiding this repeated work. */
2570 tree thunk_binfo = NULL_TREE;
2571 tree base_binfo = TYPE_BINFO (base_return);
2573 /* Find the base binfo within the overriding function's
2574 return type. We will always find a thunk_binfo, except
2575 when the covariancy is invalid (which we will have
2576 already diagnosed). */
2577 if (base_binfo)
2578 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2579 thunk_binfo = TREE_CHAIN (thunk_binfo))
2580 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2581 BINFO_TYPE (base_binfo)))
2582 break;
2583 gcc_assert (thunk_binfo || errorcount);
2585 /* See if virtual inheritance is involved. */
2586 for (virtual_offset = thunk_binfo;
2587 virtual_offset;
2588 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2589 if (BINFO_VIRTUAL_P (virtual_offset))
2590 break;
2592 if (virtual_offset
2593 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2595 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2597 if (virtual_offset)
2599 /* We convert via virtual base. Adjust the fixed
2600 offset to be from there. */
2601 offset =
2602 size_diffop (offset,
2603 fold_convert (ssizetype,
2604 BINFO_OFFSET (virtual_offset)));
2606 if (fixed_offset)
2607 /* There was an existing fixed offset, this must be
2608 from the base just converted to, and the base the
2609 FN was thunking to. */
2610 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2611 else
2612 fixed_offset = offset;
2616 if (fixed_offset || virtual_offset)
2617 /* Replace the overriding function with a covariant thunk. We
2618 will emit the overriding function in its own slot as
2619 well. */
2620 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2621 fixed_offset, virtual_offset);
2623 else
2624 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2625 !DECL_THUNK_P (fn));
2627 /* If we need a covariant thunk, then we may need to adjust first_defn.
2628 The ABI specifies that the thunks emitted with a function are
2629 determined by which bases the function overrides, so we need to be
2630 sure that we're using a thunk for some overridden base; even if we
2631 know that the necessary this adjustment is zero, there may not be an
2632 appropriate zero-this-adjustment thunk for us to use since thunks for
2633 overriding virtual bases always use the vcall offset.
2635 Furthermore, just choosing any base that overrides this function isn't
2636 quite right, as this slot won't be used for calls through a type that
2637 puts a covariant thunk here. Calling the function through such a type
2638 will use a different slot, and that slot is the one that determines
2639 the thunk emitted for that base.
2641 So, keep looking until we find the base that we're really overriding
2642 in this slot: the nearest primary base that doesn't use a covariant
2643 thunk in this slot. */
2644 if (overrider_target != overrider_fn)
2646 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2647 /* We already know that the overrider needs a covariant thunk. */
2648 b = get_primary_binfo (b);
2649 for (; ; b = get_primary_binfo (b))
2651 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2652 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2653 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2654 break;
2655 if (BINFO_LOST_PRIMARY_P (b))
2656 lost = true;
2658 first_defn = b;
2661 /* Assume that we will produce a thunk that convert all the way to
2662 the final overrider, and not to an intermediate virtual base. */
2663 virtual_base = NULL_TREE;
2665 /* See if we can convert to an intermediate virtual base first, and then
2666 use the vcall offset located there to finish the conversion. */
2667 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2669 /* If we find the final overrider, then we can stop
2670 walking. */
2671 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2672 BINFO_TYPE (TREE_VALUE (overrider))))
2673 break;
2675 /* If we find a virtual base, and we haven't yet found the
2676 overrider, then there is a virtual base between the
2677 declaring base (first_defn) and the final overrider. */
2678 if (BINFO_VIRTUAL_P (b))
2680 virtual_base = b;
2681 break;
2685 /* Compute the constant adjustment to the `this' pointer. The
2686 `this' pointer, when this function is called, will point at BINFO
2687 (or one of its primary bases, which are at the same offset). */
2688 if (virtual_base)
2689 /* The `this' pointer needs to be adjusted from the declaration to
2690 the nearest virtual base. */
2691 delta = size_diffop_loc (input_location,
2692 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2693 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2694 else if (lost)
2695 /* If the nearest definition is in a lost primary, we don't need an
2696 entry in our vtable. Except possibly in a constructor vtable,
2697 if we happen to get our primary back. In that case, the offset
2698 will be zero, as it will be a primary base. */
2699 delta = size_zero_node;
2700 else
2701 /* The `this' pointer needs to be adjusted from pointing to
2702 BINFO to pointing at the base where the final overrider
2703 appears. */
2704 delta = size_diffop_loc (input_location,
2705 fold_convert (ssizetype,
2706 BINFO_OFFSET (TREE_VALUE (overrider))),
2707 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2709 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2711 if (virtual_base)
2712 BV_VCALL_INDEX (*virtuals)
2713 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2714 else
2715 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2717 BV_LOST_PRIMARY (*virtuals) = lost;
2720 /* Called from modify_all_vtables via dfs_walk. */
2722 static tree
2723 dfs_modify_vtables (tree binfo, void* data)
2725 tree t = (tree) data;
2726 tree virtuals;
2727 tree old_virtuals;
2728 unsigned ix;
2730 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2731 /* A base without a vtable needs no modification, and its bases
2732 are uninteresting. */
2733 return dfs_skip_bases;
2735 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2736 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2737 /* Don't do the primary vtable, if it's new. */
2738 return NULL_TREE;
2740 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2741 /* There's no need to modify the vtable for a non-virtual primary
2742 base; we're not going to use that vtable anyhow. We do still
2743 need to do this for virtual primary bases, as they could become
2744 non-primary in a construction vtable. */
2745 return NULL_TREE;
2747 make_new_vtable (t, binfo);
2749 /* Now, go through each of the virtual functions in the virtual
2750 function table for BINFO. Find the final overrider, and update
2751 the BINFO_VIRTUALS list appropriately. */
2752 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2753 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2754 virtuals;
2755 ix++, virtuals = TREE_CHAIN (virtuals),
2756 old_virtuals = TREE_CHAIN (old_virtuals))
2757 update_vtable_entry_for_fn (t,
2758 binfo,
2759 BV_FN (old_virtuals),
2760 &virtuals, ix);
2762 return NULL_TREE;
2765 /* Update all of the primary and secondary vtables for T. Create new
2766 vtables as required, and initialize their RTTI information. Each
2767 of the functions in VIRTUALS is declared in T and may override a
2768 virtual function from a base class; find and modify the appropriate
2769 entries to point to the overriding functions. Returns a list, in
2770 declaration order, of the virtual functions that are declared in T,
2771 but do not appear in the primary base class vtable, and which
2772 should therefore be appended to the end of the vtable for T. */
2774 static tree
2775 modify_all_vtables (tree t, tree virtuals)
2777 tree binfo = TYPE_BINFO (t);
2778 tree *fnsp;
2780 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2781 if (TYPE_CONTAINS_VPTR_P (t))
2782 get_vtable_decl (t, false);
2784 /* Update all of the vtables. */
2785 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2787 /* Add virtual functions not already in our primary vtable. These
2788 will be both those introduced by this class, and those overridden
2789 from secondary bases. It does not include virtuals merely
2790 inherited from secondary bases. */
2791 for (fnsp = &virtuals; *fnsp; )
2793 tree fn = TREE_VALUE (*fnsp);
2795 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2796 || DECL_VINDEX (fn) == error_mark_node)
2798 /* We don't need to adjust the `this' pointer when
2799 calling this function. */
2800 BV_DELTA (*fnsp) = integer_zero_node;
2801 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2803 /* This is a function not already in our vtable. Keep it. */
2804 fnsp = &TREE_CHAIN (*fnsp);
2806 else
2807 /* We've already got an entry for this function. Skip it. */
2808 *fnsp = TREE_CHAIN (*fnsp);
2811 return virtuals;
2814 /* Get the base virtual function declarations in T that have the
2815 indicated NAME. */
2817 static void
2818 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2820 bool found_decls = false;
2822 /* Find virtual functions in T with the indicated NAME. */
2823 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2825 tree method = *iter;
2827 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2829 base_fndecls->safe_push (method);
2830 found_decls = true;
2834 if (found_decls)
2835 return;
2837 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2838 for (int i = 0; i < n_baseclasses; i++)
2840 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2841 get_basefndecls (name, basetype, base_fndecls);
2845 /* If this method overrides a virtual method from a base, then mark
2846 this member function as being virtual as well. Do 'final' and
2847 'override' checks too. */
2849 void
2850 check_for_override (tree decl, tree ctype)
2852 if (TREE_CODE (decl) == TEMPLATE_DECL)
2853 /* In [temp.mem] we have:
2855 A specialization of a member function template does not
2856 override a virtual function from a base class. */
2857 return;
2859 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2860 used for a vfunc. That avoids the expensive look_for_overrides
2861 call that when we know there's nothing to find. As conversion
2862 operators for the same type can have distinct identifiers, we
2863 cannot optimize those in that way. */
2864 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2865 || DECL_CONV_FN_P (decl))
2866 && look_for_overrides (ctype, decl)
2867 /* Check staticness after we've checked if we 'override'. */
2868 && !DECL_STATIC_FUNCTION_P (decl))
2870 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2871 the error_mark_node so that we know it is an overriding
2872 function. */
2873 DECL_VINDEX (decl) = decl;
2875 if (warn_override
2876 && !DECL_OVERRIDE_P (decl)
2877 && !DECL_FINAL_P (decl)
2878 && !DECL_DESTRUCTOR_P (decl))
2879 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2880 "%qD can be marked override", decl);
2882 else if (DECL_OVERRIDE_P (decl))
2883 error ("%q+#D marked %<override%>, but does not override", decl);
2885 if (DECL_VIRTUAL_P (decl))
2887 /* Remember this identifier is virtual name. */
2888 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
2890 if (!DECL_VINDEX (decl))
2891 /* It's a new vfunc. */
2892 DECL_VINDEX (decl) = error_mark_node;
2894 if (DECL_DESTRUCTOR_P (decl))
2895 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2897 else if (DECL_FINAL_P (decl))
2898 error ("%q+#D marked %<final%>, but is not virtual", decl);
2901 /* Warn about hidden virtual functions that are not overridden in t.
2902 We know that constructors and destructors don't apply. */
2904 static void
2905 warn_hidden (tree t)
2907 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2908 for (unsigned ix = member_vec->length (); ix--;)
2910 tree fns = (*member_vec)[ix];
2912 if (!OVL_P (fns))
2913 continue;
2915 tree name = OVL_NAME (fns);
2916 auto_vec<tree, 20> base_fndecls;
2917 tree base_binfo;
2918 tree binfo;
2919 unsigned j;
2921 /* Iterate through all of the base classes looking for possibly
2922 hidden functions. */
2923 for (binfo = TYPE_BINFO (t), j = 0;
2924 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2926 tree basetype = BINFO_TYPE (base_binfo);
2927 get_basefndecls (name, basetype, &base_fndecls);
2930 /* If there are no functions to hide, continue. */
2931 if (base_fndecls.is_empty ())
2932 continue;
2934 /* Remove any overridden functions. */
2935 for (ovl_iterator iter (fns); iter; ++iter)
2937 tree fndecl = *iter;
2938 if (TREE_CODE (fndecl) == FUNCTION_DECL
2939 && DECL_VINDEX (fndecl))
2941 /* If the method from the base class has the same
2942 signature as the method from the derived class, it
2943 has been overridden. */
2944 for (size_t k = 0; k < base_fndecls.length (); k++)
2945 if (base_fndecls[k]
2946 && same_signature_p (fndecl, base_fndecls[k]))
2947 base_fndecls[k] = NULL_TREE;
2951 /* Now give a warning for all base functions without overriders,
2952 as they are hidden. */
2953 tree base_fndecl;
2954 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2955 if (base_fndecl)
2957 auto_diagnostic_group d;
2958 /* Here we know it is a hider, and no overrider exists. */
2959 if (warning_at (location_of (base_fndecl),
2960 OPT_Woverloaded_virtual,
2961 "%qD was hidden", base_fndecl))
2962 inform (location_of (fns), " by %qD", fns);
2967 /* Recursive helper for finish_struct_anon. */
2969 static void
2970 finish_struct_anon_r (tree field, bool complain)
2972 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
2974 /* We're generally only interested in entities the user
2975 declared, but we also find nested classes by noticing
2976 the TYPE_DECL that we create implicitly. You're
2977 allowed to put one anonymous union inside another,
2978 though, so we explicitly tolerate that. We use
2979 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2980 we also allow unnamed types used for defining fields. */
2981 if (DECL_ARTIFICIAL (elt)
2982 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2983 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2984 continue;
2986 if (complain
2987 && (TREE_CODE (elt) != FIELD_DECL
2988 || (TREE_PRIVATE (elt) || TREE_PROTECTED (elt))))
2990 /* We already complained about static data members in
2991 finish_static_data_member_decl. */
2992 if (!VAR_P (elt))
2994 auto_diagnostic_group d;
2995 if (permerror (DECL_SOURCE_LOCATION (elt),
2996 TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
2997 ? "%q#D invalid; an anonymous union may "
2998 "only have public non-static data members"
2999 : "%q#D invalid; an anonymous struct may "
3000 "only have public non-static data members", elt))
3002 static bool hint;
3003 if (flag_permissive && !hint)
3005 hint = true;
3006 inform (DECL_SOURCE_LOCATION (elt),
3007 "this flexibility is deprecated and will be "
3008 "removed");
3014 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3015 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3017 /* Recurse into the anonymous aggregates to correctly handle
3018 access control (c++/24926):
3020 class A {
3021 union {
3022 union {
3023 int i;
3028 int j=A().i; */
3029 if (DECL_NAME (elt) == NULL_TREE
3030 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3031 finish_struct_anon_r (elt, /*complain=*/false);
3035 /* Check for things that are invalid. There are probably plenty of other
3036 things we should check for also. */
3038 static void
3039 finish_struct_anon (tree t)
3041 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3043 if (TREE_STATIC (field))
3044 continue;
3045 if (TREE_CODE (field) != FIELD_DECL)
3046 continue;
3048 if (DECL_NAME (field) == NULL_TREE
3049 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3050 finish_struct_anon_r (field, /*complain=*/true);
3054 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3055 will be used later during class template instantiation.
3056 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3057 a non-static member data (FIELD_DECL), a member function
3058 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3059 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3060 When FRIEND_P is nonzero, T is either a friend class
3061 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3062 (FUNCTION_DECL, TEMPLATE_DECL). */
3064 void
3065 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3067 if (CLASSTYPE_TEMPLATE_INFO (type)
3068 && TREE_CODE (t) != CONST_DECL)
3070 tree purpose = friend_p ? NULL_TREE : type;
3072 CLASSTYPE_DECL_LIST (type)
3073 = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3077 /* This function is called from declare_virt_assop_and_dtor via
3078 dfs_walk_all.
3080 DATA is a type that direcly or indirectly inherits the base
3081 represented by BINFO. If BINFO contains a virtual assignment [copy
3082 assignment or move assigment] operator or a virtual constructor,
3083 declare that function in DATA if it hasn't been already declared. */
3085 static tree
3086 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3088 tree bv, fn, t = (tree)data;
3089 tree opname = assign_op_identifier;
3091 gcc_assert (t && CLASS_TYPE_P (t));
3092 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3094 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3095 /* A base without a vtable needs no modification, and its bases
3096 are uninteresting. */
3097 return dfs_skip_bases;
3099 if (BINFO_PRIMARY_P (binfo))
3100 /* If this is a primary base, then we have already looked at the
3101 virtual functions of its vtable. */
3102 return NULL_TREE;
3104 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3106 fn = BV_FN (bv);
3108 if (DECL_NAME (fn) == opname)
3110 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3111 lazily_declare_fn (sfk_copy_assignment, t);
3112 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3113 lazily_declare_fn (sfk_move_assignment, t);
3115 else if (DECL_DESTRUCTOR_P (fn)
3116 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3117 lazily_declare_fn (sfk_destructor, t);
3120 return NULL_TREE;
3123 /* If the class type T has a direct or indirect base that contains a
3124 virtual assignment operator or a virtual destructor, declare that
3125 function in T if it hasn't been already declared. */
3127 static void
3128 declare_virt_assop_and_dtor (tree t)
3130 if (!(TYPE_POLYMORPHIC_P (t)
3131 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3132 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3133 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3134 return;
3136 dfs_walk_all (TYPE_BINFO (t),
3137 dfs_declare_virt_assop_and_dtor,
3138 NULL, t);
3141 /* Declare the inheriting constructor for class T inherited from base
3142 constructor CTOR with the parameter array PARMS of size NPARMS. */
3144 static void
3145 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3147 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3149 /* We don't declare an inheriting ctor that would be a default,
3150 copy or move ctor for derived or base. */
3151 if (nparms == 0)
3152 return;
3153 if (nparms == 1
3154 && TYPE_REF_P (parms[0]))
3156 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3157 if (parm == t || parm == DECL_CONTEXT (ctor))
3158 return;
3161 tree parmlist = void_list_node;
3162 for (int i = nparms - 1; i >= 0; i--)
3163 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3164 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3165 t, false, ctor, parmlist);
3167 if (add_method (t, fn, false))
3169 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3170 TYPE_FIELDS (t) = fn;
3174 /* Declare all the inheriting constructors for class T inherited from base
3175 constructor CTOR. */
3177 static void
3178 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3180 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3182 if (flag_new_inheriting_ctors)
3184 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3185 t, /*const*/false, ctor, parms);
3186 add_method (t, ctor, using_decl != NULL_TREE);
3187 return;
3190 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3191 int i = 0;
3192 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3194 if (TREE_PURPOSE (parms))
3195 one_inheriting_sig (t, ctor, new_parms, i);
3196 new_parms[i++] = TREE_VALUE (parms);
3198 one_inheriting_sig (t, ctor, new_parms, i);
3199 if (parms == NULL_TREE)
3201 auto_diagnostic_group d;
3202 if (warning (OPT_Winherited_variadic_ctor,
3203 "the ellipsis in %qD is not inherited", ctor))
3204 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3208 /* Create default constructors, assignment operators, and so forth for
3209 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3210 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3211 the class cannot have a default constructor, copy constructor
3212 taking a const reference argument, or an assignment operator taking
3213 a const reference, respectively. */
3215 static void
3216 add_implicitly_declared_members (tree t, tree* access_decls,
3217 int cant_have_const_cctor,
3218 int cant_have_const_assignment)
3220 /* Destructor. */
3221 if (!CLASSTYPE_DESTRUCTOR (t))
3222 /* In general, we create destructors lazily. */
3223 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3225 bool move_ok = false;
3226 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3227 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3228 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3229 move_ok = true;
3231 /* [class.ctor]
3233 If there is no user-declared constructor for a class, a default
3234 constructor is implicitly declared. */
3235 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3237 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3238 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3239 if (cxx_dialect >= cxx11)
3240 TYPE_HAS_CONSTEXPR_CTOR (t)
3241 /* Don't force the declaration to get a hard answer; if the
3242 definition would have made the class non-literal, it will still be
3243 non-literal because of the base or member in question, and that
3244 gives a better diagnostic. */
3245 = type_maybe_constexpr_default_constructor (t);
3248 /* [class.ctor]
3250 If a class definition does not explicitly declare a copy
3251 constructor, one is declared implicitly. */
3252 if (! TYPE_HAS_COPY_CTOR (t))
3254 TYPE_HAS_COPY_CTOR (t) = 1;
3255 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3256 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3257 if (move_ok)
3258 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3261 /* If there is no assignment operator, one will be created if and
3262 when it is needed. For now, just record whether or not the type
3263 of the parameter to the assignment operator will be a const or
3264 non-const reference. */
3265 if (!TYPE_HAS_COPY_ASSIGN (t))
3267 TYPE_HAS_COPY_ASSIGN (t) = 1;
3268 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3269 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3270 if (move_ok && !LAMBDA_TYPE_P (t))
3271 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3274 /* We can't be lazy about declaring functions that might override
3275 a virtual function from a base class. */
3276 declare_virt_assop_and_dtor (t);
3278 /* If the class definition does not explicitly declare an == operator
3279 function, but declares a defaulted three-way comparison operator function,
3280 an == operator function is declared implicitly. */
3281 if (!classtype_has_op (t, EQ_EXPR))
3282 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3284 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3285 NULL_TREE);
3286 bool is_friend = DECL_CONTEXT (space) != t;
3287 if (is_friend)
3288 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3289 NULL_TREE, NO_SPECIAL, true);
3290 else
3292 add_method (t, eq, false);
3293 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3294 TYPE_FIELDS (t) = eq;
3296 maybe_add_class_template_decl_list (t, eq, is_friend);
3299 while (*access_decls)
3301 tree using_decl = TREE_VALUE (*access_decls);
3302 tree decl = USING_DECL_DECLS (using_decl);
3303 if (DECL_NAME (using_decl) == ctor_identifier)
3305 /* declare, then remove the decl */
3306 tree ctor_list = decl;
3307 location_t loc = input_location;
3308 input_location = DECL_SOURCE_LOCATION (using_decl);
3309 for (ovl_iterator iter (ctor_list); iter; ++iter)
3310 one_inherited_ctor (*iter, t, using_decl);
3311 *access_decls = TREE_CHAIN (*access_decls);
3312 input_location = loc;
3314 else
3315 access_decls = &TREE_CHAIN (*access_decls);
3319 /* Cache of enum_min_precision values. */
3320 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3322 /* Return the minimum precision of a bit-field needed to store all
3323 enumerators of ENUMERAL_TYPE TYPE. */
3325 static int
3326 enum_min_precision (tree type)
3328 type = TYPE_MAIN_VARIANT (type);
3329 /* For unscoped enums without fixed underlying type and without mode
3330 attribute we can just use precision of the underlying type. */
3331 if (UNSCOPED_ENUM_P (type)
3332 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3333 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3334 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3336 if (enum_to_min_precision == NULL)
3337 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3339 bool existed;
3340 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3341 if (existed)
3342 return prec;
3344 tree minnode, maxnode;
3345 if (TYPE_VALUES (type))
3347 minnode = maxnode = NULL_TREE;
3348 for (tree values = TYPE_VALUES (type);
3349 values; values = TREE_CHAIN (values))
3351 tree decl = TREE_VALUE (values);
3352 tree value = DECL_INITIAL (decl);
3353 if (value == error_mark_node)
3354 value = integer_zero_node;
3355 if (!minnode)
3356 minnode = maxnode = value;
3357 else if (tree_int_cst_lt (maxnode, value))
3358 maxnode = value;
3359 else if (tree_int_cst_lt (value, minnode))
3360 minnode = value;
3363 else
3364 minnode = maxnode = integer_zero_node;
3366 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3367 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3368 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3369 prec = MAX (lowprec, highprec);
3370 return prec;
3373 /* FIELD is a bit-field. We are finishing the processing for its
3374 enclosing type. Issue any appropriate messages and set appropriate
3375 flags. Returns false if an error has been diagnosed. */
3377 static bool
3378 check_bitfield_decl (tree field)
3380 tree type = TREE_TYPE (field);
3381 tree w;
3383 /* Extract the declared width of the bitfield, which has been
3384 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3385 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3386 gcc_assert (w != NULL_TREE);
3387 /* Remove the bit-field width indicator so that the rest of the
3388 compiler does not treat that value as a qualifier. */
3389 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3391 /* Detect invalid bit-field type. */
3392 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3394 error_at (DECL_SOURCE_LOCATION (field),
3395 "bit-field %q#D with non-integral type %qT", field, type);
3396 w = error_mark_node;
3398 else
3400 location_t loc = input_location;
3401 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3402 STRIP_NOPS (w);
3404 /* detect invalid field size. */
3405 input_location = DECL_SOURCE_LOCATION (field);
3406 w = cxx_constant_value (w);
3407 input_location = loc;
3409 if (TREE_CODE (w) != INTEGER_CST)
3411 error ("bit-field %q+D width not an integer constant", field);
3412 w = error_mark_node;
3414 else if (tree_int_cst_sgn (w) < 0)
3416 error ("negative width in bit-field %q+D", field);
3417 w = error_mark_node;
3419 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3421 error ("zero width for bit-field %q+D", field);
3422 w = error_mark_node;
3424 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3425 && TREE_CODE (type) != BOOLEAN_TYPE
3426 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3427 || ((TREE_CODE (type) == ENUMERAL_TYPE
3428 || TREE_CODE (type) == BOOLEAN_TYPE)
3429 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3430 warning_at (DECL_SOURCE_LOCATION (field), 0,
3431 "width of %qD exceeds its type", field);
3432 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3434 int prec = enum_min_precision (type);
3435 if (compare_tree_int (w, prec) < 0)
3436 warning_at (DECL_SOURCE_LOCATION (field), 0,
3437 "%qD is too small to hold all values of %q#T",
3438 field, type);
3442 if (w != error_mark_node)
3444 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3445 DECL_BIT_FIELD (field) = 1;
3446 return true;
3448 else
3450 /* Non-bit-fields are aligned for their type. */
3451 DECL_BIT_FIELD (field) = 0;
3452 CLEAR_DECL_C_BIT_FIELD (field);
3453 return false;
3457 /* FIELD is a non bit-field. We are finishing the processing for its
3458 enclosing type T. Issue any appropriate messages and set appropriate
3459 flags. */
3461 static bool
3462 check_field_decl (tree field,
3463 tree t,
3464 int* cant_have_const_ctor,
3465 int* no_const_asn_ref)
3467 tree type = strip_array_types (TREE_TYPE (field));
3468 bool any_default_members = false;
3470 /* In C++98 an anonymous union cannot contain any fields which would change
3471 the settings of CANT_HAVE_CONST_CTOR and friends. */
3472 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3474 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3475 structs. So, we recurse through their fields here. */
3476 else if (ANON_AGGR_TYPE_P (type))
3478 for (tree fields = TYPE_FIELDS (type); fields;
3479 fields = DECL_CHAIN (fields))
3480 if (TREE_CODE (fields) == FIELD_DECL)
3481 any_default_members |= check_field_decl (fields, t,
3482 cant_have_const_ctor,
3483 no_const_asn_ref);
3485 /* Check members with class type for constructors, destructors,
3486 etc. */
3487 else if (CLASS_TYPE_P (type))
3489 /* Never let anything with uninheritable virtuals
3490 make it through without complaint. */
3491 abstract_virtuals_error (field, type);
3493 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3495 static bool warned;
3496 int oldcount = errorcount;
3497 if (TYPE_NEEDS_CONSTRUCTING (type))
3498 error ("member %q+#D with constructor not allowed in union",
3499 field);
3500 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3501 error ("member %q+#D with destructor not allowed in union", field);
3502 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3503 error ("member %q+#D with copy assignment operator not allowed in union",
3504 field);
3505 if (!warned && errorcount > oldcount)
3507 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3508 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3509 warned = true;
3512 else
3514 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3515 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3516 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3517 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3518 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3519 || !TYPE_HAS_COPY_ASSIGN (type));
3520 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3521 || !TYPE_HAS_COPY_CTOR (type));
3522 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3523 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3524 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3525 || TYPE_HAS_COMPLEX_DFLT (type));
3528 if (TYPE_HAS_COPY_CTOR (type)
3529 && !TYPE_HAS_CONST_COPY_CTOR (type))
3530 *cant_have_const_ctor = 1;
3532 if (TYPE_HAS_COPY_ASSIGN (type)
3533 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3534 *no_const_asn_ref = 1;
3537 check_abi_tags (t, field);
3539 if (DECL_INITIAL (field) != NULL_TREE)
3540 /* `build_class_init_list' does not recognize
3541 non-FIELD_DECLs. */
3542 any_default_members = true;
3544 return any_default_members;
3547 /* Check the data members (both static and non-static), class-scoped
3548 typedefs, etc., appearing in the declaration of T. Issue
3549 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3550 declaration order) of access declarations; each TREE_VALUE in this
3551 list is a USING_DECL.
3553 In addition, set the following flags:
3555 EMPTY_P
3556 The class is empty, i.e., contains no non-static data members.
3558 CANT_HAVE_CONST_CTOR_P
3559 This class cannot have an implicitly generated copy constructor
3560 taking a const reference.
3562 CANT_HAVE_CONST_ASN_REF
3563 This class cannot have an implicitly generated assignment
3564 operator taking a const reference.
3566 All of these flags should be initialized before calling this
3567 function. */
3569 static void
3570 check_field_decls (tree t, tree *access_decls,
3571 int *cant_have_const_ctor_p,
3572 int *no_const_asn_ref_p)
3574 int cant_pack = 0;
3576 /* Assume there are no access declarations. */
3577 *access_decls = NULL_TREE;
3578 /* Effective C has things to say about classes with pointer members. */
3579 tree pointer_member = NULL_TREE;
3580 /* Default initialized members affect the whole class. */
3581 tree default_init_member = NULL_TREE;
3582 /* Lack of any non-static data member of non-volatile literal
3583 type affects a union. */
3584 bool found_nv_literal_p = false;
3585 /* Standard layout requires all FIELDS have same access. */
3586 int field_access = -1;
3588 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3590 tree type = TREE_TYPE (field);
3592 switch (TREE_CODE (field))
3594 default:
3595 gcc_unreachable ();
3597 case USING_DECL:
3598 /* Save the access declarations for our caller. */
3599 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3600 break;
3602 case TYPE_DECL:
3603 case TEMPLATE_DECL:
3604 break;
3606 case FUNCTION_DECL:
3607 /* FIXME: We should fold in the checking from check_methods. */
3608 break;
3610 case CONST_DECL:
3611 DECL_NONLOCAL (field) = 1;
3612 break;
3614 case VAR_DECL:
3615 if (TREE_CODE (t) == UNION_TYPE
3616 && cxx_dialect < cxx11)
3618 /* [class.union]
3620 (C++98) If a union contains a static data member,
3621 ... the program is ill-formed. */
3622 if (cxx_dialect < cxx11)
3623 error ("in C++98 %q+D may not be static because it is "
3624 "a member of a union", field);
3626 goto data_member;
3628 case FIELD_DECL:
3629 if (TREE_CODE (t) == UNION_TYPE)
3631 /* [class.union]
3633 If a union contains ... or a [non-static data] member
3634 of reference type, the program is ill-formed. */
3635 if (TYPE_REF_P (type))
3636 error ("non-static data member %q+D in a union may not "
3637 "have reference type %qT", field, type);
3640 data_member:
3641 /* Common VAR_DECL & FIELD_DECL processing. */
3642 DECL_CONTEXT (field) = t;
3643 DECL_NONLOCAL (field) = 1;
3645 /* Template instantiation can cause this. Perhaps this
3646 should be a specific instantiation check? */
3647 if (TREE_CODE (type) == FUNCTION_TYPE)
3649 error ("data member %q+D invalidly declared function type", field);
3650 type = build_pointer_type (type);
3651 TREE_TYPE (field) = type;
3653 else if (TREE_CODE (type) == METHOD_TYPE)
3655 error ("data member %q+D invalidly declared method type", field);
3656 type = build_pointer_type (type);
3657 TREE_TYPE (field) = type;
3660 break;
3663 if (TREE_CODE (field) != FIELD_DECL)
3664 continue;
3666 if (type == error_mark_node)
3667 continue;
3669 /* If it is not a union and at least one non-static data member is
3670 non-literal, the whole class becomes non-literal. Per Core/1453,
3671 volatile non-static data members and base classes are also not allowed.
3672 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3673 members.
3674 Note: if the type is incomplete we will complain later on. */
3675 if (COMPLETE_TYPE_P (type))
3677 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
3678 CLASSTYPE_LITERAL_P (t) = false;
3679 else
3680 found_nv_literal_p = true;
3683 int this_field_access = (TREE_PROTECTED (field) ? 1
3684 : TREE_PRIVATE (field) ? 2 : 0);
3685 if (field_access != this_field_access)
3687 /* A standard-layout class is a class that:
3689 ... has the same access control (Clause 11) for all
3690 non-static data members, */
3691 if (field_access < 0)
3692 field_access = this_field_access;
3693 else
3694 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3696 /* Aggregates must be public. */
3697 if (this_field_access)
3698 CLASSTYPE_NON_AGGREGATE (t) = 1;
3701 /* If this is of reference type, check if it needs an init. */
3702 if (TYPE_REF_P (type))
3704 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3705 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3706 if (DECL_INITIAL (field) == NULL_TREE)
3707 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3708 if (cxx_dialect < cxx11)
3710 /* ARM $12.6.2: [A member initializer list] (or, for an
3711 aggregate, initialization by a brace-enclosed list) is the
3712 only way to initialize non-static const and reference
3713 members. */
3714 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3715 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3719 type = strip_array_types (type);
3721 if (TYPE_PACKED (t))
3723 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3725 warning_at (DECL_SOURCE_LOCATION (field), 0,
3726 "ignoring packed attribute because of"
3727 " unpacked non-POD field %q#D", field);
3728 cant_pack = 1;
3730 else if (DECL_C_BIT_FIELD (field)
3731 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
3732 DECL_PACKED (field) = 1;
3735 if (DECL_C_BIT_FIELD (field)
3736 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
3737 /* We don't treat zero-width bitfields as making a class
3738 non-empty. */
3740 else if (field_poverlapping_p (field)
3741 && is_empty_class (TREE_TYPE (field)))
3742 /* Empty data members also don't make a class non-empty. */
3743 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3744 else
3746 /* The class is non-empty. */
3747 CLASSTYPE_EMPTY_P (t) = 0;
3748 /* The class is not even nearly empty. */
3749 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3750 /* If one of the data members contains an empty class, so
3751 does T. */
3752 if (CLASS_TYPE_P (type)
3753 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3754 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3757 /* This is used by -Weffc++ (see below). Warn only for pointers
3758 to members which might hold dynamic memory. So do not warn
3759 for pointers to functions or pointers to members. */
3760 if (TYPE_PTR_P (type)
3761 && !TYPE_PTRFN_P (type))
3762 pointer_member = field;
3764 if (CLASS_TYPE_P (type))
3766 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3767 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3768 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3769 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3772 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
3773 CLASSTYPE_HAS_MUTABLE (t) = 1;
3775 if (DECL_MUTABLE_P (field))
3777 if (TYPE_REF_P (type))
3778 error ("member %q+D cannot be declared as a %<mutable%> "
3779 "reference", field);
3780 else if (CP_TYPE_CONST_P (type))
3781 error ("member %q+D cannot be declared both %<const%> "
3782 "and %<mutable%>", field);
3785 if (! layout_pod_type_p (type))
3786 /* DR 148 now allows pointers to members (which are POD themselves),
3787 to be allowed in POD structs. */
3788 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3790 if (field_poverlapping_p (field))
3791 /* A potentially-overlapping non-static data member makes the class
3792 non-layout-POD. */
3793 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3795 if (!std_layout_type_p (type))
3796 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3798 if (! zero_init_p (type))
3799 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3801 /* We set DECL_C_BIT_FIELD in grokbitfield.
3802 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3803 if (DECL_C_BIT_FIELD (field))
3804 check_bitfield_decl (field);
3806 if (check_field_decl (field, t,
3807 cant_have_const_ctor_p, no_const_asn_ref_p))
3809 if (default_init_member
3810 && TREE_CODE (t) == UNION_TYPE)
3812 error ("multiple fields in union %qT initialized", t);
3813 inform (DECL_SOURCE_LOCATION (default_init_member),
3814 "initialized member %q+D declared here",
3815 default_init_member);
3817 default_init_member = field;
3820 /* Now that we've removed bit-field widths from DECL_INITIAL,
3821 anything left in DECL_INITIAL is an NSDMI that makes the class
3822 non-aggregate in C++11. */
3823 if (DECL_INITIAL (field) && cxx_dialect < cxx14)
3824 CLASSTYPE_NON_AGGREGATE (t) = true;
3826 if (CP_TYPE_CONST_P (type))
3828 /* If any field is const, the structure type is pseudo-const. */
3829 C_TYPE_FIELDS_READONLY (t) = 1;
3830 if (DECL_INITIAL (field) == NULL_TREE)
3831 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3832 if (cxx_dialect < cxx11)
3834 /* ARM $12.6.2: [A member initializer list] (or, for an
3835 aggregate, initialization by a brace-enclosed list) is the
3836 only way to initialize non-static const and reference
3837 members. */
3838 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3839 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3842 /* A field that is pseudo-const makes the structure likewise. */
3843 else if (CLASS_TYPE_P (type))
3845 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3846 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3847 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3848 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3851 /* Core issue 80: A non-static data member is required to have a
3852 different name from the class iff the class has a
3853 user-declared constructor. */
3854 if (constructor_name_p (DECL_NAME (field), t)
3855 && TYPE_HAS_USER_CONSTRUCTOR (t))
3856 permerror (DECL_SOURCE_LOCATION (field),
3857 "field %q#D with same name as class", field);
3860 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3861 one of its non-static data members is of non-volatile literal type. */
3862 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
3863 CLASSTYPE_LITERAL_P (t) = true;
3865 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3866 it should also define a copy constructor and an assignment operator to
3867 implement the correct copy semantic (deep vs shallow, etc.). As it is
3868 not feasible to check whether the constructors do allocate dynamic memory
3869 and store it within members, we approximate the warning like this:
3871 -- Warn only if there are members which are pointers
3872 -- Warn only if there is a non-trivial constructor (otherwise,
3873 there cannot be memory allocated).
3874 -- Warn only if there is a non-trivial destructor. We assume that the
3875 user at least implemented the cleanup correctly, and a destructor
3876 is needed to free dynamic memory.
3878 This seems enough for practical purposes. */
3879 if (warn_ecpp
3880 && pointer_member
3881 && TYPE_HAS_USER_CONSTRUCTOR (t)
3882 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3883 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3885 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
3887 if (! TYPE_HAS_COPY_CTOR (t))
3889 warning (OPT_Weffc__,
3890 " but does not declare %<%T(const %T&)%>", t, t);
3891 if (!TYPE_HAS_COPY_ASSIGN (t))
3892 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3894 else if (! TYPE_HAS_COPY_ASSIGN (t))
3895 warning (OPT_Weffc__,
3896 " but does not declare %<operator=(const %T&)%>", t);
3897 inform (DECL_SOURCE_LOCATION (pointer_member),
3898 "pointer member %q+D declared here", pointer_member);
3902 /* Non-static data member initializers make the default constructor
3903 non-trivial. */
3904 if (default_init_member)
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 || !domain
4128 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4129 return 0;
4131 /* Step through each of the elements in the array. */
4132 for (index = size_zero_node;
4133 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4134 index = size_binop (PLUS_EXPR, index, size_one_node))
4136 r = walk_subobject_offsets (TREE_TYPE (type),
4138 offset,
4139 offsets,
4140 max_offset,
4141 /*vbases_p=*/1);
4142 if (r)
4143 return r;
4144 offset = size_binop (PLUS_EXPR, offset,
4145 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4146 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4147 there's no point in iterating through the remaining
4148 elements of the array. */
4149 if (max_offset && tree_int_cst_lt (max_offset, offset))
4150 break;
4154 return 0;
4157 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4159 static bool
4160 field_poverlapping_p (tree decl)
4162 /* Base fields are actually potentially overlapping, but C++ bases go through
4163 a different code path based on binfos, and ObjC++ base fields are laid out
4164 in objc-act, so we don't want layout_class_type to mess with them. */
4165 if (DECL_FIELD_IS_BASE (decl))
4167 gcc_checking_assert (c_dialect_objc ());
4168 return false;
4171 return lookup_attribute ("no_unique_address",
4172 DECL_ATTRIBUTES (decl));
4175 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4177 static void
4178 record_subobject_offsets (tree decl_or_binfo,
4179 splay_tree offsets)
4181 tree type, offset;
4182 bool overlapping, vbases_p;
4184 if (DECL_P (decl_or_binfo))
4186 tree decl = decl_or_binfo;
4187 type = TREE_TYPE (decl);
4188 offset = byte_position (decl);
4189 overlapping = field_poverlapping_p (decl);
4190 vbases_p = true;
4192 else
4194 type = BINFO_TYPE (decl_or_binfo);
4195 offset = BINFO_OFFSET (decl_or_binfo);
4196 overlapping = true;
4197 vbases_p = false;
4200 tree max_offset;
4201 /* If recording subobjects for a non-static data member or a
4202 non-empty base class, we do not need to record offsets beyond
4203 the size of the biggest empty class. Additional data members
4204 will go at the end of the class. Additional base classes will go
4205 either at offset zero (if empty, in which case they cannot
4206 overlap with offsets past the size of the biggest empty class) or
4207 at the end of the class.
4209 However, if we are placing an empty base class, then we must record
4210 all offsets, as either the empty class is at offset zero (where
4211 other empty classes might later be placed) or at the end of the
4212 class (where other objects might then be placed, so other empty
4213 subobjects might later overlap). */
4214 if (!overlapping
4215 || !is_empty_class (type))
4216 max_offset = sizeof_biggest_empty_class;
4217 else
4218 max_offset = NULL_TREE;
4219 walk_subobject_offsets (type, record_subobject_offset, offset,
4220 offsets, max_offset, vbases_p);
4223 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4224 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4225 virtual bases of TYPE are examined. */
4227 static int
4228 layout_conflict_p (tree type,
4229 tree offset,
4230 splay_tree offsets,
4231 int vbases_p)
4233 splay_tree_node max_node;
4235 /* Get the node in OFFSETS that indicates the maximum offset where
4236 an empty subobject is located. */
4237 max_node = splay_tree_max (offsets);
4238 /* If there aren't any empty subobjects, then there's no point in
4239 performing this check. */
4240 if (!max_node)
4241 return 0;
4243 return walk_subobject_offsets (type, check_subobject_offset, offset,
4244 offsets, (tree) (max_node->key),
4245 vbases_p);
4248 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4249 non-static data member of the type indicated by RLI. BINFO is the
4250 binfo corresponding to the base subobject, OFFSETS maps offsets to
4251 types already located at those offsets. This function determines
4252 the position of the DECL. */
4254 static void
4255 layout_nonempty_base_or_field (record_layout_info rli,
4256 tree decl,
4257 tree binfo,
4258 splay_tree offsets)
4260 tree offset = NULL_TREE;
4261 bool field_p;
4262 tree type;
4264 if (binfo)
4266 /* For the purposes of determining layout conflicts, we want to
4267 use the class type of BINFO; TREE_TYPE (DECL) will be the
4268 CLASSTYPE_AS_BASE version, which does not contain entries for
4269 zero-sized bases. */
4270 type = TREE_TYPE (binfo);
4271 field_p = false;
4273 else
4275 type = TREE_TYPE (decl);
4276 field_p = true;
4279 /* Try to place the field. It may take more than one try if we have
4280 a hard time placing the field without putting two objects of the
4281 same type at the same address. */
4282 while (1)
4284 struct record_layout_info_s old_rli = *rli;
4286 /* Place this field. */
4287 place_field (rli, decl);
4288 offset = byte_position (decl);
4290 /* We have to check to see whether or not there is already
4291 something of the same type at the offset we're about to use.
4292 For example, consider:
4294 struct S {};
4295 struct T : public S { int i; };
4296 struct U : public S, public T {};
4298 Here, we put S at offset zero in U. Then, we can't put T at
4299 offset zero -- its S component would be at the same address
4300 as the S we already allocated. So, we have to skip ahead.
4301 Since all data members, including those whose type is an
4302 empty class, have nonzero size, any overlap can happen only
4303 with a direct or indirect base-class -- it can't happen with
4304 a data member. */
4305 /* In a union, overlap is permitted; all members are placed at
4306 offset zero. */
4307 if (TREE_CODE (rli->t) == UNION_TYPE)
4308 break;
4309 if (layout_conflict_p (field_p ? type : binfo, offset,
4310 offsets, field_p))
4312 /* Strip off the size allocated to this field. That puts us
4313 at the first place we could have put the field with
4314 proper alignment. */
4315 *rli = old_rli;
4317 /* Bump up by the alignment required for the type. */
4318 rli->bitpos
4319 = size_binop (PLUS_EXPR, rli->bitpos,
4320 bitsize_int (binfo
4321 ? CLASSTYPE_ALIGN (type)
4322 : TYPE_ALIGN (type)));
4323 normalize_rli (rli);
4325 else if (TREE_CODE (type) == NULLPTR_TYPE
4326 && warn_abi && abi_version_crosses (9))
4328 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4329 the offset wasn't aligned like a pointer when we started to
4330 layout this field, that affects its position. */
4331 tree pos = rli_size_unit_so_far (&old_rli);
4332 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4334 if (abi_version_at_least (9))
4335 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4336 "alignment of %qD increased in %<-fabi-version=9%> "
4337 "(GCC 5.2)", decl);
4338 else
4339 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4340 "of %qD will increase in %<-fabi-version=9%>",
4341 decl);
4343 break;
4345 else
4346 /* There was no conflict. We're done laying out this field. */
4347 break;
4350 /* Now that we know where it will be placed, update its
4351 BINFO_OFFSET. */
4352 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4353 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4354 this point because their BINFO_OFFSET is copied from another
4355 hierarchy. Therefore, we may not need to add the entire
4356 OFFSET. */
4357 propagate_binfo_offsets (binfo,
4358 size_diffop_loc (input_location,
4359 fold_convert (ssizetype, offset),
4360 fold_convert (ssizetype,
4361 BINFO_OFFSET (binfo))));
4364 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4366 static int
4367 empty_base_at_nonzero_offset_p (tree type,
4368 tree offset,
4369 splay_tree /*offsets*/)
4371 return is_empty_class (type) && !integer_zerop (offset);
4374 /* Layout the empty base BINFO. EOC indicates the byte currently just
4375 past the end of the class, and should be correctly aligned for a
4376 class of the type indicated by BINFO; OFFSETS gives the offsets of
4377 the empty bases allocated so far. T is the most derived
4378 type. Return nonzero iff we added it at the end. */
4380 static bool
4381 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4382 splay_tree offsets)
4384 tree alignment;
4385 bool atend = false;
4386 tree binfo = NULL_TREE;
4387 tree decl = NULL_TREE;
4388 tree type;
4389 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4391 binfo = binfo_or_decl;
4392 type = BINFO_TYPE (binfo);
4394 else
4396 decl = binfo_or_decl;
4397 type = TREE_TYPE (decl);
4400 /* On some platforms (ARM), even empty classes will not be
4401 byte-aligned. */
4402 tree eoc = round_up_loc (input_location,
4403 rli_size_unit_so_far (rli),
4404 CLASSTYPE_ALIGN_UNIT (type));
4406 /* This routine should only be used for empty classes. */
4407 gcc_assert (is_empty_class (type));
4409 if (decl && DECL_USER_ALIGN (decl))
4410 alignment = size_int (DECL_ALIGN_UNIT (decl));
4411 else
4412 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4414 /* This is an empty base class. We first try to put it at offset
4415 zero. */
4416 tree offset = size_zero_node;
4417 if (TREE_CODE (rli->t) != UNION_TYPE
4418 && layout_conflict_p (type,
4419 offset,
4420 offsets,
4421 /*vbases_p=*/0))
4423 /* That didn't work. Now, we move forward from the next
4424 available spot in the class. */
4425 atend = true;
4426 offset = eoc;
4427 while (1)
4429 if (!layout_conflict_p (type,
4430 offset,
4431 offsets,
4432 /*vbases_p=*/0))
4433 /* We finally found a spot where there's no overlap. */
4434 break;
4436 /* There's overlap here, too. Bump along to the next spot. */
4437 offset = size_binop (PLUS_EXPR, offset, alignment);
4441 if (decl && DECL_USER_ALIGN (decl))
4443 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4444 if (warn_packed)
4445 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4446 TYPE_USER_ALIGN (rli->t) = 1;
4448 else if (CLASSTYPE_USER_ALIGN (type))
4450 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4451 if (warn_packed)
4452 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4453 TYPE_USER_ALIGN (rli->t) = 1;
4456 if (binfo)
4457 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4458 propagate_binfo_offsets (binfo,
4459 size_diffop (offset, BINFO_OFFSET (binfo)));
4460 else
4462 DECL_FIELD_OFFSET (decl) = offset;
4463 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4464 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4467 return atend;
4470 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4471 fields at NEXT_FIELD, and return it. */
4473 static tree
4474 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4476 /* Create the FIELD_DECL. */
4477 tree basetype = BINFO_TYPE (binfo);
4478 tree as_base = CLASSTYPE_AS_BASE (basetype);
4479 gcc_assert (as_base);
4480 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4482 DECL_ARTIFICIAL (decl) = 1;
4483 DECL_IGNORED_P (decl) = 1;
4484 DECL_FIELD_CONTEXT (decl) = t;
4485 if (is_empty_class (basetype))
4486 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4487 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4488 else
4490 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4491 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4493 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4494 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4495 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4496 DECL_FIELD_IS_BASE (decl) = 1;
4498 if (access == access_private_node)
4499 TREE_PRIVATE (decl) = true;
4500 else if (access == access_protected_node)
4501 TREE_PROTECTED (decl) = true;
4503 /* Add the new FIELD_DECL to the list of fields for T. */
4504 DECL_CHAIN (decl) = *next_field;
4505 *next_field = decl;
4506 next_field = &DECL_CHAIN (decl);
4508 return decl;
4511 /* Layout the base given by BINFO in the class indicated by RLI.
4512 *BASE_ALIGN is a running maximum of the alignments of
4513 any base class. OFFSETS gives the location of empty base
4514 subobjects. T is the most derived type. Return nonzero if the new
4515 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4516 *NEXT_FIELD, unless BINFO is for an empty base class.
4518 Returns the location at which the next field should be inserted. */
4520 static tree *
4521 build_base_field (record_layout_info rli, tree binfo, tree access,
4522 splay_tree offsets, tree *next_field)
4524 tree t = rli->t;
4525 tree basetype = BINFO_TYPE (binfo);
4527 if (!COMPLETE_TYPE_P (basetype))
4528 /* This error is now reported in xref_tag, thus giving better
4529 location information. */
4530 return next_field;
4532 /* Place the base class. */
4533 if (!is_empty_class (basetype))
4535 tree decl;
4537 /* The containing class is non-empty because it has a non-empty
4538 base class. */
4539 CLASSTYPE_EMPTY_P (t) = 0;
4541 /* Create the FIELD_DECL. */
4542 decl = build_base_field_1 (t, binfo, access, next_field);
4544 /* Try to place the field. It may take more than one try if we
4545 have a hard time placing the field without putting two
4546 objects of the same type at the same address. */
4547 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4549 else
4551 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4552 /* A nearly-empty class "has no proper base class that is empty,
4553 not morally virtual, and at an offset other than zero." */
4554 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4556 if (atend)
4557 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4558 /* The check above (used in G++ 3.2) is insufficient because
4559 an empty class placed at offset zero might itself have an
4560 empty base at a nonzero offset. */
4561 else if (walk_subobject_offsets (basetype,
4562 empty_base_at_nonzero_offset_p,
4563 size_zero_node,
4564 /*offsets=*/NULL,
4565 /*max_offset=*/NULL_TREE,
4566 /*vbases_p=*/true))
4567 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4570 /* We used to not create a FIELD_DECL for empty base classes because of
4571 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4572 be a problem anymore. We need them to handle initialization of C++17
4573 aggregate bases. */
4574 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4576 tree decl = build_base_field_1 (t, binfo, access, next_field);
4577 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4578 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4579 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4580 DECL_FIELD_ABI_IGNORED (decl) = 1;
4583 /* An empty virtual base causes a class to be non-empty
4584 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4585 here because that was already done when the virtual table
4586 pointer was created. */
4589 /* Record the offsets of BINFO and its base subobjects. */
4590 record_subobject_offsets (binfo, offsets);
4592 return next_field;
4595 /* Layout all of the non-virtual base classes. Record empty
4596 subobjects in OFFSETS. T is the most derived type. Return nonzero
4597 if the type cannot be nearly empty. The fields created
4598 corresponding to the base classes will be inserted at
4599 *NEXT_FIELD. */
4601 static void
4602 build_base_fields (record_layout_info rli,
4603 splay_tree offsets, tree *next_field)
4605 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4606 subobjects. */
4607 tree t = rli->t;
4608 tree binfo = TYPE_BINFO (t);
4609 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4611 /* The primary base class is always allocated first. */
4612 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
4613 if (primary_binfo)
4615 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4616 base, if it is direct. Indirect base fields are private. */
4617 tree primary_access = access_private_node;
4618 for (int i = 0; i < n_baseclasses; ++i)
4620 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4621 if (base_binfo == primary_binfo)
4623 primary_access = BINFO_BASE_ACCESS (binfo, i);
4624 break;
4627 next_field = build_base_field (rli, primary_binfo,
4628 primary_access,
4629 offsets, next_field);
4632 /* Now allocate the rest of the bases. */
4633 for (int i = 0; i < n_baseclasses; ++i)
4635 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4637 /* The primary base was already allocated above, so we don't
4638 need to allocate it again here. */
4639 if (base_binfo == primary_binfo)
4640 continue;
4642 /* Virtual bases are added at the end (a primary virtual base
4643 will have already been added). */
4644 if (BINFO_VIRTUAL_P (base_binfo))
4645 continue;
4647 next_field = build_base_field (rli, base_binfo,
4648 BINFO_BASE_ACCESS (binfo, i),
4649 offsets, next_field);
4653 /* Go through the TYPE_FIELDS of T issuing any appropriate
4654 diagnostics, figuring out which methods override which other
4655 methods, and so forth. */
4657 static void
4658 check_methods (tree t)
4660 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4661 if (DECL_DECLARES_FUNCTION_P (x))
4663 check_for_override (x, t);
4665 if (DECL_PURE_VIRTUAL_P (x)
4666 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4667 error ("initializer specified for non-virtual method %q+D", x);
4668 /* The name of the field is the original field name
4669 Save this in auxiliary field for later overloading. */
4670 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4672 TYPE_POLYMORPHIC_P (t) = 1;
4673 if (DECL_PURE_VIRTUAL_P (x))
4674 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4677 if (!DECL_VIRTUAL_P (x)
4678 && lookup_attribute ("transaction_safe_dynamic",
4679 DECL_ATTRIBUTES (x)))
4680 error_at (DECL_SOURCE_LOCATION (x),
4681 "%<transaction_safe_dynamic%> may only be specified for "
4682 "a virtual function");
4685 /* Check whether the eligible special member functions (P0848) are
4686 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4687 has the eligible ones; TYPE_FIELDS also contains ineligible overloads,
4688 which is why this needs to be separate from the loop above. */
4690 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4692 if (TREE_CODE (dtor) == OVERLOAD)
4694 /* P0848: At the end of the definition of a class, overload
4695 resolution is performed among the prospective destructors declared
4696 in that class with an empty argument list to select the destructor
4697 for the class, also known as the selected destructor. The program
4698 is ill-formed if overload resolution fails. */
4699 auto_diagnostic_group d;
4700 error_at (location_of (t), "destructor for %qT is ambiguous", t);
4701 print_candidates (dtor);
4703 else if (user_provided_p (dtor))
4704 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
4707 for (ovl_iterator i (CLASSTYPE_CONSTRUCTORS (t)); i; ++i)
4709 tree fn = *i;
4710 if (!user_provided_p (fn))
4711 /* Might be trivial. */;
4712 else if (copy_fn_p (fn))
4713 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
4714 else if (move_fn_p (fn))
4715 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
4718 for (ovl_iterator i (get_class_binding_direct (t, assign_op_identifier));
4719 i; ++i)
4721 tree fn = *i;
4722 if (!user_provided_p (fn))
4723 /* Might be trivial. */;
4724 else if (copy_fn_p (fn))
4725 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
4726 else if (move_fn_p (fn))
4727 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
4731 /* FN is constructor, destructor or operator function. Clone the
4732 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4733 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4735 static tree
4736 copy_fndecl_with_name (tree fn, tree name, tree_code code,
4737 bool need_vtt_parm_p, bool omit_inherited_parms_p)
4739 /* Copy the function. */
4740 tree clone = copy_decl (fn);
4741 /* Reset the function name. */
4742 DECL_NAME (clone) = name;
4744 if (flag_concepts)
4745 /* Clone constraints. */
4746 if (tree ci = get_constraints (fn))
4747 set_constraints (clone, copy_node (ci));
4749 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4750 /* There's no pending inline data for this function. */
4751 DECL_PENDING_INLINE_INFO (clone) = NULL;
4752 DECL_PENDING_INLINE_P (clone) = 0;
4754 if (name == base_dtor_identifier)
4756 /* The base-class destructor is not virtual. */
4757 DECL_VIRTUAL_P (clone) = 0;
4758 DECL_VINDEX (clone) = NULL_TREE;
4760 else if (code != ERROR_MARK)
4762 /* Set the operator code. */
4763 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
4764 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
4766 /* The operator could be virtual. */
4767 if (DECL_VIRTUAL_P (clone))
4768 IDENTIFIER_VIRTUAL_P (name) = true;
4771 if (omit_inherited_parms_p)
4772 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4774 /* If there was an in-charge parameter, drop it from the function
4775 type. */
4776 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4778 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4779 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4780 /* Skip the `this' parameter. */
4781 parmtypes = TREE_CHAIN (parmtypes);
4782 /* Skip the in-charge parameter. */
4783 parmtypes = TREE_CHAIN (parmtypes);
4784 /* And the VTT parm, in a complete [cd]tor. */
4785 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
4786 parmtypes = TREE_CHAIN (parmtypes);
4787 if (omit_inherited_parms_p)
4789 /* If we're omitting inherited parms, that just leaves the VTT. */
4790 gcc_assert (need_vtt_parm_p);
4791 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4793 TREE_TYPE (clone)
4794 = build_method_type_directly (basetype,
4795 TREE_TYPE (TREE_TYPE (clone)),
4796 parmtypes);
4797 TREE_TYPE (clone)
4798 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4799 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4800 TREE_TYPE (clone)
4801 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4804 /* Copy the function parameters. */
4805 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4807 /* Remove the in-charge parameter. */
4808 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4810 DECL_CHAIN (DECL_ARGUMENTS (clone))
4811 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4812 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4815 /* And the VTT parm, in a complete [cd]tor. */
4816 if (DECL_HAS_VTT_PARM_P (fn))
4818 if (need_vtt_parm_p)
4819 DECL_HAS_VTT_PARM_P (clone) = 1;
4820 else
4822 DECL_CHAIN (DECL_ARGUMENTS (clone))
4823 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4824 DECL_HAS_VTT_PARM_P (clone) = 0;
4828 /* A base constructor inheriting from a virtual base doesn't get the
4829 arguments. */
4830 if (omit_inherited_parms_p)
4831 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4833 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4835 DECL_CONTEXT (parms) = clone;
4836 cxx_dup_lang_specific_decl (parms);
4839 /* Create the RTL for this function. */
4840 SET_DECL_RTL (clone, NULL);
4841 rest_of_decl_compilation (clone, namespace_bindings_p (), at_eof);
4843 return clone;
4846 /* FN is an operator function, create a variant for CODE. */
4848 tree
4849 copy_operator_fn (tree fn, tree_code code)
4851 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
4852 code, false, false);
4855 /* FN is a constructor or destructor. Clone the declaration to create
4856 a specialized in-charge or not-in-charge version, as indicated by
4857 NAME. */
4859 static tree
4860 build_clone (tree fn, tree name, bool need_vtt_parm_p,
4861 bool omit_inherited_parms_p)
4863 tree clone;
4865 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4866 if (TREE_CODE (fn) == TEMPLATE_DECL)
4868 clone = copy_decl (fn);
4869 DECL_NAME (clone) = name;
4871 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
4872 need_vtt_parm_p, omit_inherited_parms_p);
4873 DECL_TEMPLATE_RESULT (clone) = result;
4875 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4876 DECL_TI_TEMPLATE (result) = clone;
4878 TREE_TYPE (clone) = TREE_TYPE (result);
4880 else
4882 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
4883 need_vtt_parm_p, omit_inherited_parms_p);
4884 DECL_CLONED_FUNCTION (clone) = fn;
4887 /* Remember where this function came from. */
4888 DECL_ABSTRACT_ORIGIN (clone) = fn;
4890 /* Make it easy to find the CLONE given the FN. Note the
4891 template_result of a template will be chained this way too. */
4892 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4893 DECL_CHAIN (fn) = clone;
4895 return clone;
4898 /* Build the clones of FN, return the number of clones built. These
4899 will be inserted onto DECL_CHAIN of FN. */
4901 static unsigned
4902 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p)
4904 unsigned count = 0;
4906 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4908 /* For each constructor, we need two variants: an in-charge version
4909 and a not-in-charge version. */
4910 build_clone (fn, complete_ctor_identifier, false, false);
4911 build_clone (fn, base_ctor_identifier, needs_vtt_p,
4912 base_omits_inherited_p);
4913 count += 2;
4915 else
4917 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4919 /* For each destructor, we need three variants: an in-charge
4920 version, a not-in-charge version, and an in-charge deleting
4921 version. We clone the deleting version first because that
4922 means it will go second on the TYPE_FIELDS list -- and that
4923 corresponds to the correct layout order in the virtual
4924 function table.
4926 For a non-virtual destructor, we do not build a deleting
4927 destructor. */
4928 if (DECL_VIRTUAL_P (fn))
4930 build_clone (fn, deleting_dtor_identifier, false, false);
4931 count++;
4933 build_clone (fn, complete_dtor_identifier, false, false);
4934 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
4935 count += 2;
4938 return count;
4941 /* Produce declarations for all appropriate clones of FN. If
4942 UPDATE_METHODS is true, the clones are added to the
4943 CLASSTYPE_MEMBER_VEC. */
4945 void
4946 clone_cdtor (tree fn, bool update_methods)
4948 /* Avoid inappropriate cloning. */
4949 if (DECL_CHAIN (fn)
4950 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4951 return;
4953 /* Base cdtors need a vtt parm if there are virtual bases. */
4954 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
4956 /* Base ctor omits inherited parms it needs a vttparm and inherited
4957 from a virtual nase ctor. */
4958 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
4959 && base_ctor_omit_inherited_parms (fn));
4961 unsigned count = build_cdtor_clones (fn, vtt, base_omits_inherited);
4963 /* Note that this is an abstract function that is never emitted. */
4964 DECL_ABSTRACT_P (fn) = true;
4966 if (update_methods)
4967 for (tree clone = fn; count--;)
4969 clone = DECL_CHAIN (clone);
4970 add_method (DECL_CONTEXT (clone), clone, false);
4974 /* DECL is an in charge constructor, which is being defined. This will
4975 have had an in class declaration, from whence clones were
4976 declared. An out-of-class definition can specify additional default
4977 arguments. As it is the clones that are involved in overload
4978 resolution, we must propagate the information from the DECL to its
4979 clones. */
4981 void
4982 adjust_clone_args (tree decl)
4984 tree clone;
4986 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4987 clone = DECL_CHAIN (clone))
4989 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4990 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4991 tree decl_parms, clone_parms;
4993 /* Skip the 'this' parameter. */
4994 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4995 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4997 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4998 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4999 if (DECL_HAS_VTT_PARM_P (decl))
5000 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5002 clone_parms = orig_clone_parms;
5003 if (DECL_HAS_VTT_PARM_P (clone))
5004 clone_parms = TREE_CHAIN (clone_parms);
5006 for (decl_parms = orig_decl_parms; decl_parms;
5007 decl_parms = TREE_CHAIN (decl_parms),
5008 clone_parms = TREE_CHAIN (clone_parms))
5010 if (clone_parms == void_list_node)
5012 gcc_assert (decl_parms == clone_parms
5013 || ctor_omit_inherited_parms (clone));
5014 break;
5017 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5018 TREE_VALUE (clone_parms)));
5020 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5022 /* A default parameter has been added. Adjust the
5023 clone's parameters. */
5024 clone_parms = orig_decl_parms;
5026 if (DECL_HAS_VTT_PARM_P (clone))
5028 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5029 TREE_VALUE (orig_clone_parms),
5030 clone_parms);
5031 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5034 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5035 tree type
5036 = build_method_type_directly (basetype,
5037 TREE_TYPE (TREE_TYPE (clone)),
5038 clone_parms);
5039 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5040 type = cp_build_type_attribute_variant (type, attrs);
5041 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5042 TREE_TYPE (clone) = type;
5044 clone_parms = NULL_TREE;
5045 break;
5048 gcc_assert (!clone_parms || clone_parms == void_list_node);
5052 /* For each of the constructors and destructors in T, create an
5053 in-charge and not-in-charge variant. */
5055 static void
5056 clone_constructors_and_destructors (tree t)
5058 /* While constructors can be via a using declaration, at this point
5059 we no longer need to know that. */
5060 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5061 clone_cdtor (*iter, /*update_methods=*/true);
5063 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5064 clone_cdtor (dtor, /*update_methods=*/true);
5067 /* Deduce noexcept for a destructor DTOR. */
5069 void
5070 deduce_noexcept_on_destructor (tree dtor)
5072 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5073 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5074 noexcept_deferred_spec);
5077 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5078 of TYPE for virtual functions which FNDECL overrides. Return a
5079 mask of the tm attributes found therein. */
5081 static int
5082 look_for_tm_attr_overrides (tree type, tree fndecl)
5084 tree binfo = TYPE_BINFO (type);
5085 tree base_binfo;
5086 int ix, found = 0;
5088 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5090 tree o, basetype = BINFO_TYPE (base_binfo);
5092 if (!TYPE_POLYMORPHIC_P (basetype))
5093 continue;
5095 o = look_for_overrides_here (basetype, fndecl);
5096 if (o)
5098 if (lookup_attribute ("transaction_safe_dynamic",
5099 DECL_ATTRIBUTES (o)))
5100 /* transaction_safe_dynamic is not inherited. */;
5101 else
5102 found |= tm_attr_to_mask (find_tm_attribute
5103 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5105 else
5106 found |= look_for_tm_attr_overrides (basetype, fndecl);
5109 return found;
5112 /* Subroutine of set_method_tm_attributes. Handle the checks and
5113 inheritance for one virtual method FNDECL. */
5115 static void
5116 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5118 tree tm_attr;
5119 int found, have;
5121 found = look_for_tm_attr_overrides (type, fndecl);
5123 /* If FNDECL doesn't actually override anything (i.e. T is the
5124 class that first declares FNDECL virtual), then we're done. */
5125 if (found == 0)
5126 return;
5128 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5129 have = tm_attr_to_mask (tm_attr);
5131 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5132 tm_pure must match exactly, otherwise no weakening of
5133 tm_safe > tm_callable > nothing. */
5134 /* ??? The tm_pure attribute didn't make the transition to the
5135 multivendor language spec. */
5136 if (have == TM_ATTR_PURE)
5138 if (found != TM_ATTR_PURE)
5140 found &= -found;
5141 goto err_override;
5144 /* If the overridden function is tm_pure, then FNDECL must be. */
5145 else if (found == TM_ATTR_PURE && tm_attr)
5146 goto err_override;
5147 /* Look for base class combinations that cannot be satisfied. */
5148 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5150 found &= ~TM_ATTR_PURE;
5151 found &= -found;
5152 error_at (DECL_SOURCE_LOCATION (fndecl),
5153 "method overrides both %<transaction_pure%> and %qE methods",
5154 tm_mask_to_attr (found));
5156 /* If FNDECL did not declare an attribute, then inherit the most
5157 restrictive one. */
5158 else if (tm_attr == NULL)
5160 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5162 /* Otherwise validate that we're not weaker than a function
5163 that is being overridden. */
5164 else
5166 found &= -found;
5167 if (found <= TM_ATTR_CALLABLE && have > found)
5168 goto err_override;
5170 return;
5172 err_override:
5173 error_at (DECL_SOURCE_LOCATION (fndecl),
5174 "method declared %qE overriding %qE method",
5175 tm_attr, tm_mask_to_attr (found));
5178 /* For each of the methods in T, propagate a class-level tm attribute. */
5180 static void
5181 set_method_tm_attributes (tree t)
5183 tree class_tm_attr, fndecl;
5185 /* Don't bother collecting tm attributes if transactional memory
5186 support is not enabled. */
5187 if (!flag_tm)
5188 return;
5190 /* Process virtual methods first, as they inherit directly from the
5191 base virtual function and also require validation of new attributes. */
5192 if (TYPE_CONTAINS_VPTR_P (t))
5194 tree vchain;
5195 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5196 vchain = TREE_CHAIN (vchain))
5198 fndecl = BV_FN (vchain);
5199 if (DECL_THUNK_P (fndecl))
5200 fndecl = THUNK_TARGET (fndecl);
5201 set_one_vmethod_tm_attributes (t, fndecl);
5205 /* If the class doesn't have an attribute, nothing more to do. */
5206 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5207 if (class_tm_attr == NULL)
5208 return;
5210 /* Any method that does not yet have a tm attribute inherits
5211 the one from the class. */
5212 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5213 if (DECL_DECLARES_FUNCTION_P (fndecl)
5214 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5215 apply_tm_attr (fndecl, class_tm_attr);
5218 /* Returns true if FN is a default constructor. */
5220 bool
5221 default_ctor_p (const_tree fn)
5223 return (DECL_CONSTRUCTOR_P (fn)
5224 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5227 /* Returns true iff class T has a user-provided constructor that can be called
5228 with more than zero arguments. */
5230 bool
5231 type_has_user_nondefault_constructor (tree t)
5233 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5234 return false;
5236 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5238 tree fn = *iter;
5239 if (user_provided_p (fn)
5240 && (TREE_CODE (fn) == TEMPLATE_DECL
5241 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5242 != NULL_TREE)))
5243 return true;
5246 return false;
5249 /* Returns the defaulted constructor if T has one. Otherwise, returns
5250 NULL_TREE. */
5252 tree
5253 in_class_defaulted_default_constructor (tree t)
5255 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5256 return NULL_TREE;
5258 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5260 tree fn = *iter;
5262 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5263 && default_ctor_p (fn))
5264 return fn;
5267 return NULL_TREE;
5270 /* Returns true iff FN is a user-provided function, i.e. user-declared
5271 and not defaulted at its first declaration. */
5273 bool
5274 user_provided_p (tree fn)
5276 fn = STRIP_TEMPLATE (fn);
5277 return (!DECL_ARTIFICIAL (fn)
5278 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5279 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5282 /* Returns true iff class T has a user-provided constructor. */
5284 bool
5285 type_has_user_provided_constructor (tree t)
5287 if (!CLASS_TYPE_P (t))
5288 return false;
5290 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5291 return false;
5293 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5294 if (user_provided_p (*iter))
5295 return true;
5297 return false;
5300 /* Returns true iff class T has a user-provided or explicit constructor. */
5302 bool
5303 type_has_user_provided_or_explicit_constructor (tree t)
5305 if (!CLASS_TYPE_P (t))
5306 return false;
5308 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5309 return false;
5311 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5313 tree fn = *iter;
5314 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5315 return true;
5318 return false;
5321 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5322 declared or explicitly defaulted in the class body) default
5323 constructor. */
5325 bool
5326 type_has_non_user_provided_default_constructor (tree t)
5328 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5329 return false;
5330 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5331 return true;
5333 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5335 tree fn = *iter;
5336 if (TREE_CODE (fn) == FUNCTION_DECL
5337 && default_ctor_p (fn)
5338 && !user_provided_p (fn))
5339 return true;
5342 return false;
5345 /* TYPE is being used as a virtual base, and has a non-trivial move
5346 assignment. Return true if this is due to there being a user-provided
5347 move assignment in TYPE or one of its subobjects; if there isn't, then
5348 multiple move assignment can't cause any harm. */
5350 bool
5351 vbase_has_user_provided_move_assign (tree type)
5353 /* Does the type itself have a user-provided move assignment operator? */
5354 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5355 for (ovl_iterator iter (get_class_binding_direct
5356 (type, assign_op_identifier));
5357 iter; ++iter)
5358 if (user_provided_p (*iter) && move_fn_p (*iter))
5359 return true;
5361 /* Do any of its bases? */
5362 tree binfo = TYPE_BINFO (type);
5363 tree base_binfo;
5364 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5365 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5366 return true;
5368 /* Or non-static data members? */
5369 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5371 if (TREE_CODE (field) == FIELD_DECL
5372 && CLASS_TYPE_P (TREE_TYPE (field))
5373 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5374 return true;
5377 /* Seems not. */
5378 return false;
5381 /* If default-initialization leaves part of TYPE uninitialized, returns
5382 a DECL for the field or TYPE itself (DR 253). */
5384 tree
5385 default_init_uninitialized_part (tree type)
5387 tree t, r, binfo;
5388 int i;
5390 type = strip_array_types (type);
5391 if (!CLASS_TYPE_P (type))
5392 return type;
5393 if (!type_has_non_user_provided_default_constructor (type))
5394 return NULL_TREE;
5395 for (binfo = TYPE_BINFO (type), i = 0;
5396 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5398 r = default_init_uninitialized_part (BINFO_TYPE (t));
5399 if (r)
5400 return r;
5402 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5403 if (TREE_CODE (t) == FIELD_DECL
5404 && !DECL_ARTIFICIAL (t)
5405 && !DECL_INITIAL (t))
5407 r = default_init_uninitialized_part (TREE_TYPE (t));
5408 if (r)
5409 return DECL_P (r) ? r : t;
5412 return NULL_TREE;
5415 /* Returns true iff for class T, a trivial synthesized default constructor
5416 would be constexpr. */
5418 bool
5419 trivial_default_constructor_is_constexpr (tree t)
5421 /* A defaulted trivial default constructor is constexpr
5422 if there is nothing to initialize. */
5423 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5424 /* A class with a vptr doesn't have a trivial default ctor.
5425 In C++20, a class can have transient uninitialized members, e.g.:
5427 struct S { int i; constexpr S() = default; };
5429 should work. */
5430 return (cxx_dialect >= cxx20
5431 || is_really_empty_class (t, /*ignore_vptr*/true));
5434 /* Returns true iff class T has a constexpr default constructor. */
5436 bool
5437 type_has_constexpr_default_constructor (tree t)
5439 tree fns;
5441 if (!CLASS_TYPE_P (t))
5443 /* The caller should have stripped an enclosing array. */
5444 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5445 return false;
5447 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5449 if (!TYPE_HAS_COMPLEX_DFLT (t))
5450 return trivial_default_constructor_is_constexpr (t);
5451 /* Non-trivial, we need to check subobject constructors. */
5452 lazily_declare_fn (sfk_constructor, t);
5454 fns = locate_ctor (t);
5455 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5458 /* Returns true iff class T has a constexpr default constructor or has an
5459 implicitly declared default constructor that we can't tell if it's constexpr
5460 without forcing a lazy declaration (which might cause undesired
5461 instantiations). */
5463 static bool
5464 type_maybe_constexpr_default_constructor (tree t)
5466 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5467 && TYPE_HAS_COMPLEX_DFLT (t))
5468 /* Assume it's constexpr. */
5469 return true;
5470 return type_has_constexpr_default_constructor (t);
5473 /* Returns true iff class T has a constexpr destructor. */
5475 bool
5476 type_has_constexpr_destructor (tree t)
5478 tree fns;
5480 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5481 /* Non-trivial, we need to check subobject destructors. */
5482 lazily_declare_fn (sfk_destructor, t);
5483 fns = CLASSTYPE_DESTRUCTOR (t);
5484 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5487 /* Returns true iff class T has a constexpr destructor or has an
5488 implicitly declared destructor that we can't tell if it's constexpr
5489 without forcing a lazy declaration (which might cause undesired
5490 instantiations). */
5492 static bool
5493 type_maybe_constexpr_destructor (tree t)
5495 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5496 /* Assume it's constexpr. */
5497 return true;
5498 return type_has_constexpr_destructor (t);
5501 /* Returns true iff class TYPE has a virtual destructor. */
5503 bool
5504 type_has_virtual_destructor (tree type)
5506 tree dtor;
5508 if (!CLASS_TYPE_P (type))
5509 return false;
5511 gcc_assert (COMPLETE_TYPE_P (type));
5512 dtor = CLASSTYPE_DESTRUCTOR (type);
5513 return (dtor && DECL_VIRTUAL_P (dtor));
5516 /* Returns true iff T, a class, has a move-assignment or
5517 move-constructor. Does not lazily declare either.
5518 If USER_P is false, any move function will do. If it is true, the
5519 move function must be user-declared.
5521 Note that user-declared here is different from "user-provided",
5522 which doesn't include functions that are defaulted in the
5523 class. */
5525 bool
5526 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5528 gcc_assert (user_p
5529 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5530 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5532 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5533 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5534 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5535 return true;
5537 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5538 for (ovl_iterator iter (get_class_binding_direct
5539 (t, assign_op_identifier));
5540 iter; ++iter)
5541 if ((!user_p || !DECL_ARTIFICIAL (*iter))
5542 && DECL_CONTEXT (*iter) == t
5543 && move_fn_p (*iter))
5544 return true;
5546 return false;
5549 /* True iff T has a move constructor that is not deleted. */
5551 bool
5552 classtype_has_non_deleted_move_ctor (tree t)
5554 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5555 lazily_declare_fn (sfk_move_constructor, t);
5556 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5557 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5558 return true;
5559 return false;
5562 /* True iff T has a copy constructor that is not deleted. */
5564 bool
5565 classtype_has_non_deleted_copy_ctor (tree t)
5567 if (CLASSTYPE_LAZY_COPY_CTOR (t))
5568 lazily_declare_fn (sfk_copy_constructor, t);
5569 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5570 if (copy_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5571 return true;
5572 return false;
5575 /* If T, a class, has a user-provided copy constructor, copy assignment
5576 operator, or destructor, returns that function. Otherwise, null. */
5578 tree
5579 classtype_has_depr_implicit_copy (tree t)
5581 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5582 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5584 tree fn = *iter;
5585 if (user_provided_p (fn) && copy_fn_p (fn))
5586 return fn;
5589 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5590 for (ovl_iterator iter (get_class_binding_direct
5591 (t, assign_op_identifier));
5592 iter; ++iter)
5594 tree fn = *iter;
5595 if (user_provided_p (fn) && copy_fn_p (fn))
5596 return fn;
5599 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5601 tree fn = CLASSTYPE_DESTRUCTOR (t);
5602 if (user_provided_p (fn))
5603 return fn;
5606 return NULL_TREE;
5609 /* True iff T has a member or friend declaration of operator OP. */
5611 bool
5612 classtype_has_op (tree t, tree_code op)
5614 tree name = ovl_op_identifier (op);
5615 if (get_class_binding (t, name))
5616 return true;
5617 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5618 if (FRIEND_NAME (f) == name)
5619 return true;
5620 return false;
5624 /* If T has a defaulted member or friend declaration of OP, return it. */
5626 tree
5627 classtype_has_defaulted_op (tree t, tree_code op)
5629 tree name = ovl_op_identifier (op);
5630 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
5632 tree fn = *oi;
5633 if (DECL_DEFAULTED_FN (fn))
5634 return fn;
5636 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5637 if (FRIEND_NAME (f) == name)
5638 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
5640 tree fn = TREE_VALUE (l);
5641 if (DECL_DEFAULTED_FN (fn))
5642 return fn;
5644 return NULL_TREE;
5647 /* Nonzero if we need to build up a constructor call when initializing an
5648 object of this class, either because it has a user-declared constructor
5649 or because it doesn't have a default constructor (so we need to give an
5650 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5651 what you care about is whether or not an object can be produced by a
5652 constructor (e.g. so we don't set TREE_READONLY on const variables of
5653 such type); use this function when what you care about is whether or not
5654 to try to call a constructor to create an object. The latter case is
5655 the former plus some cases of constructors that cannot be called. */
5657 bool
5658 type_build_ctor_call (tree t)
5660 tree inner;
5661 if (TYPE_NEEDS_CONSTRUCTING (t))
5662 return true;
5663 inner = strip_array_types (t);
5664 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5665 return false;
5666 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5667 return true;
5668 if (cxx_dialect < cxx11)
5669 return false;
5670 /* A user-declared constructor might be private, and a constructor might
5671 be trivial but deleted. */
5672 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5673 iter; ++iter)
5675 tree fn = *iter;
5676 if (!DECL_ARTIFICIAL (fn)
5677 || TREE_DEPRECATED (fn)
5678 || DECL_DELETED_FN (fn))
5679 return true;
5681 return false;
5684 /* Like type_build_ctor_call, but for destructors. */
5686 bool
5687 type_build_dtor_call (tree t)
5689 tree inner;
5690 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5691 return true;
5692 inner = strip_array_types (t);
5693 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5694 || !COMPLETE_TYPE_P (inner))
5695 return false;
5696 if (cxx_dialect < cxx11)
5697 return false;
5698 /* A user-declared destructor might be private, and a destructor might
5699 be trivial but deleted. */
5700 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5701 iter; ++iter)
5703 tree fn = *iter;
5704 if (!DECL_ARTIFICIAL (fn)
5705 || TREE_DEPRECATED (fn)
5706 || DECL_DELETED_FN (fn))
5707 return true;
5709 return false;
5712 /* Remove all zero-width bit-fields from T. */
5714 static void
5715 remove_zero_width_bit_fields (tree t)
5717 tree *fieldsp;
5719 fieldsp = &TYPE_FIELDS (t);
5720 while (*fieldsp)
5722 if (TREE_CODE (*fieldsp) == FIELD_DECL
5723 && DECL_C_BIT_FIELD (*fieldsp)
5724 /* We should not be confused by the fact that grokbitfield
5725 temporarily sets the width of the bit field into
5726 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5727 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5728 to that width. */
5729 && (DECL_SIZE (*fieldsp) == NULL_TREE
5730 || integer_zerop (DECL_SIZE (*fieldsp))))
5731 *fieldsp = DECL_CHAIN (*fieldsp);
5732 else
5733 fieldsp = &DECL_CHAIN (*fieldsp);
5737 /* Returns TRUE iff we need a cookie when dynamically allocating an
5738 array whose elements have the indicated class TYPE. */
5740 static bool
5741 type_requires_array_cookie (tree type)
5743 tree fns;
5744 bool has_two_argument_delete_p = false;
5746 gcc_assert (CLASS_TYPE_P (type));
5748 /* If there's a non-trivial destructor, we need a cookie. In order
5749 to iterate through the array calling the destructor for each
5750 element, we'll have to know how many elements there are. */
5751 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5752 return true;
5754 /* If the usual deallocation function is a two-argument whose second
5755 argument is of type `size_t', then we have to pass the size of
5756 the array to the deallocation function, so we will need to store
5757 a cookie. */
5758 fns = lookup_fnfields (TYPE_BINFO (type),
5759 ovl_op_identifier (false, VEC_DELETE_EXPR),
5760 /*protect=*/0, tf_warning_or_error);
5761 /* If there are no `operator []' members, or the lookup is
5762 ambiguous, then we don't need a cookie. */
5763 if (!fns || fns == error_mark_node)
5764 return false;
5765 /* Loop through all of the functions. */
5766 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5768 tree fn = *iter;
5770 /* See if this function is a one-argument delete function. If
5771 it is, then it will be the usual deallocation function. */
5772 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5773 if (second_parm == void_list_node)
5774 return false;
5775 /* Do not consider this function if its second argument is an
5776 ellipsis. */
5777 if (!second_parm)
5778 continue;
5779 /* Otherwise, if we have a two-argument function and the second
5780 argument is `size_t', it will be the usual deallocation
5781 function -- unless there is one-argument function, too. */
5782 if (TREE_CHAIN (second_parm) == void_list_node
5783 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5784 has_two_argument_delete_p = true;
5787 return has_two_argument_delete_p;
5790 /* Finish computing the `literal type' property of class type T.
5792 At this point, we have already processed base classes and
5793 non-static data members. We need to check whether the copy
5794 constructor is trivial, the destructor is trivial, and there
5795 is a trivial default constructor or at least one constexpr
5796 constructor other than the copy constructor. */
5798 static void
5799 finalize_literal_type_property (tree t)
5801 tree fn;
5803 if (cxx_dialect < cxx11)
5804 CLASSTYPE_LITERAL_P (t) = false;
5805 else if (CLASSTYPE_LITERAL_P (t)
5806 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5807 && (cxx_dialect < cxx20 || !type_maybe_constexpr_destructor (t)))
5808 CLASSTYPE_LITERAL_P (t) = false;
5809 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5810 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5811 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5812 && CLASSTYPE_NON_AGGREGATE (t)
5813 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5814 CLASSTYPE_LITERAL_P (t) = false;
5816 /* C++14 DR 1684 removed this restriction. */
5817 if (cxx_dialect < cxx14
5818 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5819 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5820 if (TREE_CODE (fn) == FUNCTION_DECL
5821 && DECL_DECLARED_CONSTEXPR_P (fn)
5822 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5823 && !DECL_CONSTRUCTOR_P (fn))
5825 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5826 if (!DECL_GENERATED_P (fn))
5828 auto_diagnostic_group d;
5829 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5830 "enclosing class of %<constexpr%> non-static "
5831 "member function %q+#D is not a literal type", fn))
5832 explain_non_literal_class (t);
5837 /* T is a non-literal type used in a context which requires a constant
5838 expression. Explain why it isn't literal. */
5840 void
5841 explain_non_literal_class (tree t)
5843 static hash_set<tree> *diagnosed;
5845 if (!CLASS_TYPE_P (t))
5846 return;
5847 t = TYPE_MAIN_VARIANT (t);
5849 if (diagnosed == NULL)
5850 diagnosed = new hash_set<tree>;
5851 if (diagnosed->add (t))
5852 /* Already explained. */
5853 return;
5855 auto_diagnostic_group d;
5856 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5857 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5858 inform (UNKNOWN_LOCATION,
5859 " %qT is a closure type, which is only literal in "
5860 "C++17 and later", t);
5861 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5862 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5863 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5864 && !type_maybe_constexpr_destructor (t))
5865 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
5867 else if (CLASSTYPE_NON_AGGREGATE (t)
5868 && !TYPE_HAS_TRIVIAL_DFLT (t)
5869 && !LAMBDA_TYPE_P (t)
5870 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5872 inform (UNKNOWN_LOCATION,
5873 " %q+T is not an aggregate, does not have a trivial "
5874 "default constructor, and has no %<constexpr%> constructor that "
5875 "is not a copy or move constructor", t);
5876 if (type_has_non_user_provided_default_constructor (t))
5877 /* Note that we can't simply call locate_ctor because when the
5878 constructor is deleted it just returns NULL_TREE. */
5879 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5881 tree fn = *iter;
5882 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5884 parms = skip_artificial_parms_for (fn, parms);
5886 if (sufficient_parms_p (parms))
5888 if (DECL_DELETED_FN (fn))
5889 maybe_explain_implicit_delete (fn);
5890 else
5891 explain_invalid_constexpr_fn (fn);
5892 break;
5896 else
5898 tree binfo, base_binfo, field; int i;
5899 for (binfo = TYPE_BINFO (t), i = 0;
5900 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5902 tree basetype = TREE_TYPE (base_binfo);
5903 if (!CLASSTYPE_LITERAL_P (basetype))
5905 inform (UNKNOWN_LOCATION,
5906 " base class %qT of %q+T is non-literal",
5907 basetype, t);
5908 explain_non_literal_class (basetype);
5909 return;
5912 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5914 tree ftype;
5915 if (TREE_CODE (field) != FIELD_DECL)
5916 continue;
5917 ftype = TREE_TYPE (field);
5918 if (!literal_type_p (ftype))
5920 inform (DECL_SOURCE_LOCATION (field),
5921 " non-static data member %qD has non-literal type",
5922 field);
5923 if (CLASS_TYPE_P (ftype))
5924 explain_non_literal_class (ftype);
5926 if (CP_TYPE_VOLATILE_P (ftype))
5927 inform (DECL_SOURCE_LOCATION (field),
5928 " non-static data member %qD has volatile type", field);
5933 /* Check the validity of the bases and members declared in T. Add any
5934 implicitly-generated functions (like copy-constructors and
5935 assignment operators). Compute various flag bits (like
5936 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5937 level: i.e., independently of the ABI in use. */
5939 static void
5940 check_bases_and_members (tree t)
5942 /* Nonzero if the implicitly generated copy constructor should take
5943 a non-const reference argument. */
5944 int cant_have_const_ctor;
5945 /* Nonzero if the implicitly generated assignment operator
5946 should take a non-const reference argument. */
5947 int no_const_asn_ref;
5948 tree access_decls;
5949 bool saved_complex_asn_ref;
5950 bool saved_nontrivial_dtor;
5951 tree fn;
5953 /* By default, we use const reference arguments and generate default
5954 constructors. */
5955 cant_have_const_ctor = 0;
5956 no_const_asn_ref = 0;
5958 /* Check all the base-classes and set FMEM members to point to arrays
5959 of potential interest. */
5960 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5962 /* Deduce noexcept on destructor. This needs to happen after we've set
5963 triviality flags appropriately for our bases. */
5964 if (cxx_dialect >= cxx11)
5965 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5966 deduce_noexcept_on_destructor (dtor);
5968 /* Check all the method declarations. */
5969 check_methods (t);
5971 /* Save the initial values of these flags which only indicate whether
5972 or not the class has user-provided functions. As we analyze the
5973 bases and members we can set these flags for other reasons. */
5974 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5975 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5977 /* Check all the data member declarations. We cannot call
5978 check_field_decls until we have called check_bases check_methods,
5979 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5980 being set appropriately. */
5981 check_field_decls (t, &access_decls,
5982 &cant_have_const_ctor,
5983 &no_const_asn_ref);
5985 /* A nearly-empty class has to be vptr-containing; a nearly empty
5986 class contains just a vptr. */
5987 if (!TYPE_CONTAINS_VPTR_P (t))
5988 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5990 /* Do some bookkeeping that will guide the generation of implicitly
5991 declared member functions. */
5992 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5993 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5994 /* We need to call a constructor for this class if it has a
5995 user-provided constructor, or if the default constructor is going
5996 to initialize the vptr. (This is not an if-and-only-if;
5997 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5998 themselves need constructing.) */
5999 TYPE_NEEDS_CONSTRUCTING (t)
6000 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6001 /* [dcl.init.aggr]
6003 An aggregate is an array or a class with no user-provided
6004 constructors ... and no virtual functions.
6006 Again, other conditions for being an aggregate are checked
6007 elsewhere. */
6008 CLASSTYPE_NON_AGGREGATE (t)
6009 |= ((cxx_dialect < cxx20
6010 ? type_has_user_provided_or_explicit_constructor (t)
6011 : TYPE_HAS_USER_CONSTRUCTOR (t))
6012 || TYPE_POLYMORPHIC_P (t));
6013 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6014 retain the old definition internally for ABI reasons. */
6015 CLASSTYPE_NON_LAYOUT_POD_P (t)
6016 |= (CLASSTYPE_NON_AGGREGATE (t)
6017 || saved_nontrivial_dtor || saved_complex_asn_ref);
6018 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6019 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6020 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6021 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6023 /* If the only explicitly declared default constructor is user-provided,
6024 set TYPE_HAS_COMPLEX_DFLT. */
6025 if (!TYPE_HAS_COMPLEX_DFLT (t)
6026 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6027 && !type_has_non_user_provided_default_constructor (t))
6028 TYPE_HAS_COMPLEX_DFLT (t) = true;
6030 /* Warn if a public base of a polymorphic type has an accessible
6031 non-virtual destructor. It is only now that we know the class is
6032 polymorphic. Although a polymorphic base will have a already
6033 been diagnosed during its definition, we warn on use too. */
6034 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6036 tree binfo = TYPE_BINFO (t);
6037 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6038 tree base_binfo;
6039 unsigned i;
6041 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6043 tree basetype = TREE_TYPE (base_binfo);
6045 if ((*accesses)[i] == access_public_node
6046 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6047 && accessible_nvdtor_p (basetype))
6048 warning (OPT_Wnon_virtual_dtor,
6049 "base class %q#T has accessible non-virtual destructor",
6050 basetype);
6054 /* If the class has no user-declared constructor, but does have
6055 non-static const or reference data members that can never be
6056 initialized, issue a warning. */
6057 if (warn_uninitialized
6058 /* Classes with user-declared constructors are presumed to
6059 initialize these members. */
6060 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6061 /* Aggregates can be initialized with brace-enclosed
6062 initializers. */
6063 && CLASSTYPE_NON_AGGREGATE (t))
6065 tree field;
6067 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6069 tree type;
6071 if (TREE_CODE (field) != FIELD_DECL
6072 || DECL_INITIAL (field) != NULL_TREE)
6073 continue;
6075 type = TREE_TYPE (field);
6076 if (TYPE_REF_P (type))
6077 warning_at (DECL_SOURCE_LOCATION (field),
6078 OPT_Wuninitialized, "non-static reference %q#D "
6079 "in class without a constructor", field);
6080 else if (CP_TYPE_CONST_P (type)
6081 && (!CLASS_TYPE_P (type)
6082 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6083 warning_at (DECL_SOURCE_LOCATION (field),
6084 OPT_Wuninitialized, "non-static const member %q#D "
6085 "in class without a constructor", field);
6089 /* Synthesize any needed methods. */
6090 add_implicitly_declared_members (t, &access_decls,
6091 cant_have_const_ctor,
6092 no_const_asn_ref);
6094 /* Check defaulted declarations here so we have cant_have_const_ctor
6095 and don't need to worry about clones. */
6096 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6097 if (DECL_DECLARES_FUNCTION_P (fn)
6098 && !DECL_ARTIFICIAL (fn)
6099 && DECL_DEFAULTED_IN_CLASS_P (fn))
6101 int copy = copy_fn_p (fn);
6102 if (copy > 0)
6104 bool imp_const_p
6105 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6106 : !no_const_asn_ref);
6107 bool fn_const_p = (copy == 2);
6109 if (fn_const_p && !imp_const_p)
6110 /* If the function is defaulted outside the class, we just
6111 give the synthesis error. Core Issue #1331 says this is
6112 no longer ill-formed, it is defined as deleted instead. */
6113 DECL_DELETED_FN (fn) = true;
6115 defaulted_late_check (fn);
6118 if (LAMBDA_TYPE_P (t))
6119 /* "This class type is not an aggregate." */
6120 CLASSTYPE_NON_AGGREGATE (t) = 1;
6122 /* Compute the 'literal type' property before we
6123 do anything with non-static member functions. */
6124 finalize_literal_type_property (t);
6126 /* Create the in-charge and not-in-charge variants of constructors
6127 and destructors. */
6128 clone_constructors_and_destructors (t);
6130 /* Process the using-declarations. */
6131 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6132 handle_using_decl (TREE_VALUE (access_decls), t);
6134 /* Figure out whether or not we will need a cookie when dynamically
6135 allocating an array of this type. */
6136 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6137 = type_requires_array_cookie (t);
6140 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6141 accordingly. If a new vfield was created (because T doesn't have a
6142 primary base class), then the newly created field is returned. It
6143 is not added to the TYPE_FIELDS list; it is the caller's
6144 responsibility to do that. Accumulate declared virtual functions
6145 on VIRTUALS_P. */
6147 static tree
6148 create_vtable_ptr (tree t, tree* virtuals_p)
6150 tree fn;
6152 /* Collect the virtual functions declared in T. */
6153 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6154 if (TREE_CODE (fn) == FUNCTION_DECL
6155 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6156 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6158 tree new_virtual = make_node (TREE_LIST);
6160 BV_FN (new_virtual) = fn;
6161 BV_DELTA (new_virtual) = integer_zero_node;
6162 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6164 TREE_CHAIN (new_virtual) = *virtuals_p;
6165 *virtuals_p = new_virtual;
6168 /* If we couldn't find an appropriate base class, create a new field
6169 here. Even if there weren't any new virtual functions, we might need a
6170 new virtual function table if we're supposed to include vptrs in
6171 all classes that need them. */
6172 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6174 /* We build this decl with vtbl_ptr_type_node, which is a
6175 `vtable_entry_type*'. It might seem more precise to use
6176 `vtable_entry_type (*)[N]' where N is the number of virtual
6177 functions. However, that would require the vtable pointer in
6178 base classes to have a different type than the vtable pointer
6179 in derived classes. We could make that happen, but that
6180 still wouldn't solve all the problems. In particular, the
6181 type-based alias analysis code would decide that assignments
6182 to the base class vtable pointer can't alias assignments to
6183 the derived class vtable pointer, since they have different
6184 types. Thus, in a derived class destructor, where the base
6185 class constructor was inlined, we could generate bad code for
6186 setting up the vtable pointer.
6188 Therefore, we use one type for all vtable pointers. We still
6189 use a type-correct type; it's just doesn't indicate the array
6190 bounds. That's better than using `void*' or some such; it's
6191 cleaner, and it let's the alias analysis code know that these
6192 stores cannot alias stores to void*! */
6193 tree field;
6195 field = build_decl (input_location,
6196 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6197 DECL_VIRTUAL_P (field) = 1;
6198 DECL_ARTIFICIAL (field) = 1;
6199 DECL_FIELD_CONTEXT (field) = t;
6200 DECL_FCONTEXT (field) = t;
6201 if (TYPE_PACKED (t))
6202 DECL_PACKED (field) = 1;
6204 TYPE_VFIELD (t) = field;
6206 /* This class is non-empty. */
6207 CLASSTYPE_EMPTY_P (t) = 0;
6209 return field;
6212 return NULL_TREE;
6215 /* Add OFFSET to all base types of BINFO which is a base in the
6216 hierarchy dominated by T.
6218 OFFSET, which is a type offset, is number of bytes. */
6220 static void
6221 propagate_binfo_offsets (tree binfo, tree offset)
6223 int i;
6224 tree primary_binfo;
6225 tree base_binfo;
6227 /* Update BINFO's offset. */
6228 BINFO_OFFSET (binfo)
6229 = fold_convert (sizetype,
6230 size_binop (PLUS_EXPR,
6231 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6232 offset));
6234 /* Find the primary base class. */
6235 primary_binfo = get_primary_binfo (binfo);
6237 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6238 propagate_binfo_offsets (primary_binfo, offset);
6240 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6241 downwards. */
6242 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6244 /* Don't do the primary base twice. */
6245 if (base_binfo == primary_binfo)
6246 continue;
6248 if (BINFO_VIRTUAL_P (base_binfo))
6249 continue;
6251 propagate_binfo_offsets (base_binfo, offset);
6255 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6256 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6257 empty subobjects of T. */
6259 static void
6260 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6262 tree vbase;
6263 tree t = rli->t;
6264 tree *next_field;
6266 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6267 return;
6269 /* Find the last field. The artificial fields created for virtual
6270 bases will go after the last extant field to date. */
6271 next_field = &TYPE_FIELDS (t);
6272 while (*next_field)
6273 next_field = &DECL_CHAIN (*next_field);
6275 /* Go through the virtual bases, allocating space for each virtual
6276 base that is not already a primary base class. These are
6277 allocated in inheritance graph order. */
6278 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6280 if (!BINFO_VIRTUAL_P (vbase))
6281 continue;
6283 if (!BINFO_PRIMARY_P (vbase))
6285 /* This virtual base is not a primary base of any class in the
6286 hierarchy, so we have to add space for it. */
6287 next_field = build_base_field (rli, vbase,
6288 access_private_node,
6289 offsets, next_field);
6294 /* Returns the offset of the byte just past the end of the base class
6295 BINFO. */
6297 static tree
6298 end_of_base (tree binfo)
6300 tree size;
6302 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6303 size = TYPE_SIZE_UNIT (char_type_node);
6304 else if (is_empty_class (BINFO_TYPE (binfo)))
6305 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6306 allocate some space for it. It cannot have virtual bases, so
6307 TYPE_SIZE_UNIT is fine. */
6308 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6309 else
6310 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6312 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6315 /* Returns the offset of the byte just past the end of the base class or empty
6316 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
6317 then only non-virtual bases are included. */
6319 static tree
6320 end_of_class (tree t, bool include_virtuals_p)
6322 tree result = size_zero_node;
6323 vec<tree, va_gc> *vbases;
6324 tree binfo;
6325 tree base_binfo;
6326 tree offset;
6327 int i;
6329 for (binfo = TYPE_BINFO (t), i = 0;
6330 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6332 if (!include_virtuals_p
6333 && BINFO_VIRTUAL_P (base_binfo)
6334 && (!BINFO_PRIMARY_P (base_binfo)
6335 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6336 continue;
6338 offset = end_of_base (base_binfo);
6339 if (tree_int_cst_lt (result, offset))
6340 result = offset;
6343 /* Also consider empty data members. */
6344 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6345 if (TREE_CODE (field) == FIELD_DECL
6346 && !DECL_ARTIFICIAL (field)
6347 && field_poverlapping_p (field)
6348 && is_empty_class (TREE_TYPE (field)))
6350 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
6351 offset = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (field),
6352 TYPE_SIZE_UNIT (TREE_TYPE (field)));
6353 if (tree_int_cst_lt (result, offset))
6354 result = offset;
6357 if (include_virtuals_p)
6358 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6359 vec_safe_iterate (vbases, i, &base_binfo); i++)
6361 offset = end_of_base (base_binfo);
6362 if (tree_int_cst_lt (result, offset))
6363 result = offset;
6366 return result;
6369 /* Warn about bases of T that are inaccessible because they are
6370 ambiguous. For example:
6372 struct S {};
6373 struct T : public S {};
6374 struct U : public S, public T {};
6376 Here, `(S*) new U' is not allowed because there are two `S'
6377 subobjects of U. */
6379 static void
6380 maybe_warn_about_inaccessible_bases (tree t)
6382 int i;
6383 vec<tree, va_gc> *vbases;
6384 tree basetype;
6385 tree binfo;
6386 tree base_binfo;
6388 /* If not checking for warning then return early. */
6389 if (!warn_inaccessible_base)
6390 return;
6392 /* If there are no repeated bases, nothing can be ambiguous. */
6393 if (!CLASSTYPE_REPEATED_BASE_P (t))
6394 return;
6396 /* Check direct bases. */
6397 for (binfo = TYPE_BINFO (t), i = 0;
6398 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6400 basetype = BINFO_TYPE (base_binfo);
6402 if (!uniquely_derived_from_p (basetype, t))
6403 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6404 "in %qT due to ambiguity", basetype, t);
6407 /* Check for ambiguous virtual bases. */
6408 if (extra_warnings)
6409 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6410 vec_safe_iterate (vbases, i, &binfo); i++)
6412 basetype = BINFO_TYPE (binfo);
6414 if (!uniquely_derived_from_p (basetype, t))
6415 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6416 "%qT due to ambiguity", basetype, t);
6420 /* Compare two INTEGER_CSTs K1 and K2. */
6422 static int
6423 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6425 return tree_int_cst_compare ((tree) k1, (tree) k2);
6428 /* Increase the size indicated in RLI to account for empty classes
6429 that are "off the end" of the class. */
6431 static void
6432 include_empty_classes (record_layout_info rli)
6434 tree eoc;
6435 tree rli_size;
6437 /* It might be the case that we grew the class to allocate a
6438 zero-sized base class. That won't be reflected in RLI, yet,
6439 because we are willing to overlay multiple bases at the same
6440 offset. However, now we need to make sure that RLI is big enough
6441 to reflect the entire class. */
6442 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6443 rli_size = rli_size_unit_so_far (rli);
6444 if (TREE_CODE (rli_size) == INTEGER_CST
6445 && tree_int_cst_lt (rli_size, eoc))
6447 /* The size should have been rounded to a whole byte. */
6448 gcc_assert (tree_int_cst_equal
6449 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6450 rli->bitpos
6451 = size_binop (PLUS_EXPR,
6452 rli->bitpos,
6453 size_binop (MULT_EXPR,
6454 fold_convert (bitsizetype,
6455 size_binop (MINUS_EXPR,
6456 eoc, rli_size)),
6457 bitsize_int (BITS_PER_UNIT)));
6458 normalize_rli (rli);
6462 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6463 BINFO_OFFSETs for all of the base-classes. Position the vtable
6464 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6466 static void
6467 layout_class_type (tree t, tree *virtuals_p)
6469 tree non_static_data_members;
6470 tree field;
6471 tree vptr;
6472 record_layout_info rli;
6473 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6474 types that appear at that offset. */
6475 splay_tree empty_base_offsets;
6476 /* True if the last field laid out was a bit-field. */
6477 bool last_field_was_bitfield = false;
6478 /* The location at which the next field should be inserted. */
6479 tree *next_field;
6481 /* Keep track of the first non-static data member. */
6482 non_static_data_members = TYPE_FIELDS (t);
6484 /* Start laying out the record. */
6485 rli = start_record_layout (t);
6487 /* Mark all the primary bases in the hierarchy. */
6488 determine_primary_bases (t);
6490 /* Create a pointer to our virtual function table. */
6491 vptr = create_vtable_ptr (t, virtuals_p);
6493 /* The vptr is always the first thing in the class. */
6494 if (vptr)
6496 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6497 TYPE_FIELDS (t) = vptr;
6498 next_field = &DECL_CHAIN (vptr);
6499 place_field (rli, vptr);
6501 else
6502 next_field = &TYPE_FIELDS (t);
6504 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6505 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6506 NULL, NULL);
6507 build_base_fields (rli, empty_base_offsets, next_field);
6509 /* Layout the non-static data members. */
6510 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6512 tree type;
6513 tree padding;
6515 /* We still pass things that aren't non-static data members to
6516 the back end, in case it wants to do something with them. */
6517 if (TREE_CODE (field) != FIELD_DECL)
6519 place_field (rli, field);
6520 /* If the static data member has incomplete type, keep track
6521 of it so that it can be completed later. (The handling
6522 of pending statics in finish_record_layout is
6523 insufficient; consider:
6525 struct S1;
6526 struct S2 { static S1 s1; };
6528 At this point, finish_record_layout will be called, but
6529 S1 is still incomplete.) */
6530 if (VAR_P (field))
6532 maybe_register_incomplete_var (field);
6533 /* The visibility of static data members is determined
6534 at their point of declaration, not their point of
6535 definition. */
6536 determine_visibility (field);
6538 continue;
6541 type = TREE_TYPE (field);
6542 if (type == error_mark_node)
6543 continue;
6545 padding = NULL_TREE;
6547 bool might_overlap = field_poverlapping_p (field);
6549 if (might_overlap && CLASS_TYPE_P (type)
6550 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
6552 /* if D is a potentially-overlapping data member, update sizeof(C) to
6553 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6554 tree nvsize = CLASSTYPE_SIZE_UNIT (type);
6555 /* end_of_class doesn't always give dsize, but it does in the case of
6556 a class with virtual bases, which is when dsize > nvsize. */
6557 tree dsize = end_of_class (type, /*vbases*/true);
6558 if (tree_int_cst_le (dsize, nvsize))
6560 DECL_SIZE_UNIT (field) = nvsize;
6561 DECL_SIZE (field) = CLASSTYPE_SIZE (type);
6563 else
6565 DECL_SIZE_UNIT (field) = dsize;
6566 DECL_SIZE (field) = bit_from_pos (dsize, bitsize_zero_node);
6570 /* If this field is a bit-field whose width is greater than its
6571 type, then there are some special rules for allocating
6572 it. */
6573 if (DECL_C_BIT_FIELD (field)
6574 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6576 bool was_unnamed_p = false;
6577 /* We must allocate the bits as if suitably aligned for the
6578 longest integer type that fits in this many bits. Then,
6579 we are supposed to use the left over bits as additional
6580 padding. */
6582 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6583 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6584 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6585 limit = DECL_SIZE (field);
6587 tree integer_type = integer_types[itk_char];
6588 for (unsigned itk = itk_char; itk != itk_none; itk++)
6589 if (tree next = integer_types[itk])
6591 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6592 /* Too big, so our current guess is what we want. */
6593 break;
6594 /* Not bigger than limit, ok */
6595 integer_type = next;
6598 /* Figure out how much additional padding is required. */
6599 if (TREE_CODE (t) == UNION_TYPE)
6600 /* In a union, the padding field must have the full width
6601 of the bit-field; all fields start at offset zero. */
6602 padding = DECL_SIZE (field);
6603 else
6604 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6605 TYPE_SIZE (integer_type));
6607 if (integer_zerop (padding))
6608 padding = NULL_TREE;
6610 /* An unnamed bitfield does not normally affect the
6611 alignment of the containing class on a target where
6612 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6613 make any exceptions for unnamed bitfields when the
6614 bitfields are longer than their types. Therefore, we
6615 temporarily give the field a name. */
6616 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6618 was_unnamed_p = true;
6619 DECL_NAME (field) = make_anon_name ();
6622 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6623 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6624 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6625 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6626 empty_base_offsets);
6627 if (was_unnamed_p)
6628 DECL_NAME (field) = NULL_TREE;
6629 /* Now that layout has been performed, set the size of the
6630 field to the size of its declared type; the rest of the
6631 field is effectively invisible. */
6632 DECL_SIZE (field) = TYPE_SIZE (type);
6633 /* We must also reset the DECL_MODE of the field. */
6634 SET_DECL_MODE (field, TYPE_MODE (type));
6636 else if (might_overlap && is_empty_class (type))
6638 DECL_FIELD_ABI_IGNORED (field) = 1;
6639 layout_empty_base_or_field (rli, field, empty_base_offsets);
6641 else
6642 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6643 empty_base_offsets);
6645 /* Remember the location of any empty classes in FIELD. */
6646 record_subobject_offsets (field, empty_base_offsets);
6648 /* If a bit-field does not immediately follow another bit-field,
6649 and yet it starts in the middle of a byte, we have failed to
6650 comply with the ABI. */
6651 if (warn_abi
6652 && DECL_C_BIT_FIELD (field)
6653 /* The TREE_NO_WARNING flag gets set by Objective-C when
6654 laying out an Objective-C class. The ObjC ABI differs
6655 from the C++ ABI, and so we do not want a warning
6656 here. */
6657 && !TREE_NO_WARNING (field)
6658 && !last_field_was_bitfield
6659 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6660 DECL_FIELD_BIT_OFFSET (field),
6661 bitsize_unit_node)))
6662 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6663 "offset of %qD is not ABI-compliant and may "
6664 "change in a future version of GCC", field);
6666 /* The middle end uses the type of expressions to determine the
6667 possible range of expression values. In order to optimize
6668 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6669 must be made aware of the width of "i", via its type.
6671 Because C++ does not have integer types of arbitrary width,
6672 we must (for the purposes of the front end) convert from the
6673 type assigned here to the declared type of the bitfield
6674 whenever a bitfield expression is used as an rvalue.
6675 Similarly, when assigning a value to a bitfield, the value
6676 must be converted to the type given the bitfield here. */
6677 if (DECL_C_BIT_FIELD (field))
6679 unsigned HOST_WIDE_INT width;
6680 tree ftype = TREE_TYPE (field);
6681 width = tree_to_uhwi (DECL_SIZE (field));
6682 if (width != TYPE_PRECISION (ftype))
6684 TREE_TYPE (field)
6685 = c_build_bitfield_integer_type (width,
6686 TYPE_UNSIGNED (ftype));
6687 TREE_TYPE (field)
6688 = cp_build_qualified_type (TREE_TYPE (field),
6689 cp_type_quals (ftype));
6693 /* If we needed additional padding after this field, add it
6694 now. */
6695 if (padding)
6697 tree padding_field;
6699 padding_field = build_decl (input_location,
6700 FIELD_DECL,
6701 NULL_TREE,
6702 char_type_node);
6703 DECL_BIT_FIELD (padding_field) = 1;
6704 DECL_SIZE (padding_field) = padding;
6705 DECL_CONTEXT (padding_field) = t;
6706 DECL_ARTIFICIAL (padding_field) = 1;
6707 DECL_IGNORED_P (padding_field) = 1;
6708 DECL_PADDING_P (padding_field) = 1;
6709 layout_nonempty_base_or_field (rli, padding_field,
6710 NULL_TREE,
6711 empty_base_offsets);
6714 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6717 if (!integer_zerop (rli->bitpos))
6719 /* Make sure that we are on a byte boundary so that the size of
6720 the class without virtual bases will always be a round number
6721 of bytes. */
6722 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6723 normalize_rli (rli);
6726 /* Delete all zero-width bit-fields from the list of fields. Now
6727 that the type is laid out they are no longer important. */
6728 remove_zero_width_bit_fields (t);
6730 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6732 /* T needs a different layout as a base (eliding virtual bases
6733 or whatever). Create that version. */
6734 tree base_t = make_node (TREE_CODE (t));
6735 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
6737 TYPE_CONTEXT (base_t) = t;
6738 DECL_CONTEXT (base_d) = t;
6740 /* If the ABI version is not at least two, and the last
6741 field was a bit-field, RLI may not be on a byte
6742 boundary. In particular, rli_size_unit_so_far might
6743 indicate the last complete byte, while rli_size_so_far
6744 indicates the total number of bits used. Therefore,
6745 rli_size_so_far, rather than rli_size_unit_so_far, is
6746 used to compute TYPE_SIZE_UNIT. */
6748 /* Set the size and alignment for the new type. */
6749 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6750 TYPE_SIZE_UNIT (base_t)
6751 = size_binop (MAX_EXPR,
6752 fold_convert (sizetype,
6753 size_binop (CEIL_DIV_EXPR,
6754 rli_size_so_far (rli),
6755 bitsize_int (BITS_PER_UNIT))),
6756 eoc);
6757 TYPE_SIZE (base_t)
6758 = size_binop (MAX_EXPR,
6759 rli_size_so_far (rli),
6760 size_binop (MULT_EXPR,
6761 fold_convert (bitsizetype, eoc),
6762 bitsize_int (BITS_PER_UNIT)));
6763 SET_TYPE_ALIGN (base_t, rli->record_align);
6764 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6765 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6766 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
6768 /* Copy the non-static data members of T. This will include its
6769 direct non-virtual bases & vtable. */
6770 next_field = &TYPE_FIELDS (base_t);
6771 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6772 if (TREE_CODE (field) == FIELD_DECL)
6774 *next_field = copy_node (field);
6775 /* Zap any NSDMI, it's not needed and might be a deferred
6776 parse. */
6777 DECL_INITIAL (*next_field) = NULL_TREE;
6778 DECL_CONTEXT (*next_field) = base_t;
6779 next_field = &DECL_CHAIN (*next_field);
6781 *next_field = NULL_TREE;
6783 /* We use the base type for trivial assignments, and hence it
6784 needs a mode. */
6785 compute_record_mode (base_t);
6787 /* Record the base version of the type. */
6788 CLASSTYPE_AS_BASE (t) = base_t;
6790 else
6791 CLASSTYPE_AS_BASE (t) = t;
6793 /* Every empty class contains an empty class. */
6794 if (CLASSTYPE_EMPTY_P (t))
6795 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6797 /* Set the TYPE_DECL for this type to contain the right
6798 value for DECL_OFFSET, so that we can use it as part
6799 of a COMPONENT_REF for multiple inheritance. */
6800 layout_decl (TYPE_MAIN_DECL (t), 0);
6802 /* Now fix up any virtual base class types that we left lying
6803 around. We must get these done before we try to lay out the
6804 virtual function table. As a side-effect, this will remove the
6805 base subobject fields. */
6806 layout_virtual_bases (rli, empty_base_offsets);
6808 /* Make sure that empty classes are reflected in RLI at this
6809 point. */
6810 include_empty_classes (rli);
6812 /* Make sure not to create any structures with zero size. */
6813 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6814 place_field (rli,
6815 build_decl (input_location,
6816 FIELD_DECL, NULL_TREE, char_type_node));
6818 /* If this is a non-POD, declaring it packed makes a difference to how it
6819 can be used as a field; don't let finalize_record_size undo it. */
6820 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6821 rli->packed_maybe_necessary = true;
6823 /* Let the back end lay out the type. */
6824 finish_record_layout (rli, /*free_p=*/true);
6826 /* If we didn't end up needing an as-base type, don't use it. */
6827 if (CLASSTYPE_AS_BASE (t) != t
6828 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
6829 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
6830 causing us to lose the user-specified alignment as in PR94050. */
6831 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
6832 && tree_int_cst_equal (TYPE_SIZE (t),
6833 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
6834 CLASSTYPE_AS_BASE (t) = t;
6836 if (TYPE_SIZE_UNIT (t)
6837 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6838 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6839 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6840 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6842 /* Warn about bases that can't be talked about due to ambiguity. */
6843 maybe_warn_about_inaccessible_bases (t);
6845 /* Now that we're done with layout, give the base fields the real types. */
6846 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6847 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6848 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6850 /* Clean up. */
6851 splay_tree_delete (empty_base_offsets);
6853 if (CLASSTYPE_EMPTY_P (t)
6854 && tree_int_cst_lt (sizeof_biggest_empty_class,
6855 TYPE_SIZE_UNIT (t)))
6856 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6859 /* Determine the "key method" for the class type indicated by TYPE,
6860 and set CLASSTYPE_KEY_METHOD accordingly. */
6862 void
6863 determine_key_method (tree type)
6865 tree method;
6867 if (processing_template_decl
6868 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6869 || CLASSTYPE_INTERFACE_KNOWN (type))
6870 return;
6872 /* The key method is the first non-pure virtual function that is not
6873 inline at the point of class definition. On some targets the
6874 key function may not be inline; those targets should not call
6875 this function until the end of the translation unit. */
6876 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6877 if (TREE_CODE (method) == FUNCTION_DECL
6878 && DECL_VINDEX (method) != NULL_TREE
6879 && ! DECL_DECLARED_INLINE_P (method)
6880 && ! DECL_PURE_VIRTUAL_P (method))
6882 CLASSTYPE_KEY_METHOD (type) = method;
6883 break;
6886 return;
6889 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6890 class data member of non-zero size, otherwise false. */
6892 static inline bool
6893 field_nonempty_p (const_tree fld)
6895 if (TREE_CODE (fld) == ERROR_MARK)
6896 return false;
6898 tree type = TREE_TYPE (fld);
6899 if (TREE_CODE (fld) == FIELD_DECL
6900 && TREE_CODE (type) != ERROR_MARK
6901 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6903 return TYPE_SIZE (type)
6904 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6905 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6908 return false;
6911 /* Used by find_flexarrays and related functions. */
6913 struct flexmems_t
6915 /* The first flexible array member or non-zero array member found
6916 in the order of layout. */
6917 tree array;
6918 /* First non-static non-empty data member in the class or its bases. */
6919 tree first;
6920 /* The first non-static non-empty data member following either
6921 the flexible array member, if found, or the zero-length array member
6922 otherwise. AFTER[1] refers to the first such data member of a union
6923 of which the struct containing the flexible array member or zero-length
6924 array is a member, or NULL when no such union exists. This element is
6925 only used during searching, not for diagnosing problems. AFTER[0]
6926 refers to the first such data member that is not a member of such
6927 a union. */
6928 tree after[2];
6930 /* Refers to a struct (not union) in which the struct of which the flexible
6931 array is member is defined. Used to diagnose strictly (according to C)
6932 invalid uses of the latter structs. */
6933 tree enclosing;
6936 /* Find either the first flexible array member or the first zero-length
6937 array, in that order of preference, among members of class T (but not
6938 its base classes), and set members of FMEM accordingly.
6939 BASE_P is true if T is a base class of another class.
6940 PUN is set to the outermost union in which the flexible array member
6941 (or zero-length array) is defined if one such union exists, otherwise
6942 to NULL.
6943 Similarly, PSTR is set to a data member of the outermost struct of
6944 which the flexible array is a member if one such struct exists,
6945 otherwise to NULL. */
6947 static void
6948 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6949 tree pun /* = NULL_TREE */,
6950 tree pstr /* = NULL_TREE */)
6952 /* Set the "pointer" to the outermost enclosing union if not set
6953 yet and maintain it for the remainder of the recursion. */
6954 if (!pun && TREE_CODE (t) == UNION_TYPE)
6955 pun = t;
6957 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6959 if (fld == error_mark_node)
6960 return;
6962 /* Is FLD a typedef for an anonymous struct? */
6964 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6965 handled elsewhere so that errors like the following are detected
6966 as well:
6967 typedef struct { int i, a[], j; } S; // bug c++/72753
6968 S s [2]; // bug c++/68489
6970 if (TREE_CODE (fld) == TYPE_DECL
6971 && DECL_IMPLICIT_TYPEDEF_P (fld)
6972 && CLASS_TYPE_P (TREE_TYPE (fld))
6973 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
6975 /* Check the nested unnamed type referenced via a typedef
6976 independently of FMEM (since it's not a data member of
6977 the enclosing class). */
6978 check_flexarrays (TREE_TYPE (fld));
6979 continue;
6982 /* Skip anything that's GCC-generated or not a (non-static) data
6983 member. */
6984 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6985 continue;
6987 /* Type of the member. */
6988 tree fldtype = TREE_TYPE (fld);
6989 if (fldtype == error_mark_node)
6990 return;
6992 /* Determine the type of the array element or object referenced
6993 by the member so that it can be checked for flexible array
6994 members if it hasn't been yet. */
6995 tree eltype = fldtype;
6996 while (TREE_CODE (eltype) == ARRAY_TYPE
6997 || INDIRECT_TYPE_P (eltype))
6998 eltype = TREE_TYPE (eltype);
7000 if (RECORD_OR_UNION_TYPE_P (eltype))
7002 if (fmem->array && !fmem->after[bool (pun)])
7004 /* Once the member after the flexible array has been found
7005 we're done. */
7006 fmem->after[bool (pun)] = fld;
7007 break;
7010 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7012 /* Descend into the non-static member struct or union and try
7013 to find a flexible array member or zero-length array among
7014 its members. This is only necessary for anonymous types
7015 and types in whose context the current type T has not been
7016 defined (the latter must not be checked again because they
7017 are already in the process of being checked by one of the
7018 recursive calls). */
7020 tree first = fmem->first;
7021 tree array = fmem->array;
7023 /* If this member isn't anonymous and a prior non-flexible array
7024 member has been seen in one of the enclosing structs, clear
7025 the FIRST member since it doesn't contribute to the flexible
7026 array struct's members. */
7027 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7028 fmem->first = NULL_TREE;
7030 find_flexarrays (eltype, fmem, false, pun,
7031 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7033 if (fmem->array != array)
7034 continue;
7036 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7038 /* Restore the FIRST member reset above if no flexible
7039 array member has been found in this member's struct. */
7040 fmem->first = first;
7043 /* If the member struct contains the first flexible array
7044 member, or if this member is a base class, continue to
7045 the next member and avoid setting the FMEM->NEXT pointer
7046 to point to it. */
7047 if (base_p)
7048 continue;
7052 if (field_nonempty_p (fld))
7054 /* Remember the first non-static data member. */
7055 if (!fmem->first)
7056 fmem->first = fld;
7058 /* Remember the first non-static data member after the flexible
7059 array member, if one has been found, or the zero-length array
7060 if it has been found. */
7061 if (fmem->array && !fmem->after[bool (pun)])
7062 fmem->after[bool (pun)] = fld;
7065 /* Skip non-arrays. */
7066 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7067 continue;
7069 /* Determine the upper bound of the array if it has one. */
7070 if (TYPE_DOMAIN (fldtype))
7072 if (fmem->array)
7074 /* Make a record of the zero-length array if either one
7075 such field or a flexible array member has been seen to
7076 handle the pathological and unlikely case of multiple
7077 such members. */
7078 if (!fmem->after[bool (pun)])
7079 fmem->after[bool (pun)] = fld;
7081 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7083 /* Remember the first zero-length array unless a flexible array
7084 member has already been seen. */
7085 fmem->array = fld;
7086 fmem->enclosing = pstr;
7089 else
7091 /* Flexible array members have no upper bound. */
7092 if (fmem->array)
7094 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7096 /* Replace the zero-length array if it's been stored and
7097 reset the after pointer. */
7098 fmem->after[bool (pun)] = NULL_TREE;
7099 fmem->array = fld;
7100 fmem->enclosing = pstr;
7102 else if (!fmem->after[bool (pun)])
7103 /* Make a record of another flexible array member. */
7104 fmem->after[bool (pun)] = fld;
7106 else
7108 fmem->array = fld;
7109 fmem->enclosing = pstr;
7115 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7116 a flexible array member (or the zero-length array extension). */
7118 static void
7119 diagnose_invalid_flexarray (const flexmems_t *fmem)
7121 if (fmem->array && fmem->enclosing)
7123 auto_diagnostic_group d;
7124 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7125 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7126 ? G_("invalid use of %q#T with a zero-size array "
7127 "in %q#D")
7128 : G_("invalid use of %q#T with a flexible array member "
7129 "in %q#T"),
7130 DECL_CONTEXT (fmem->array),
7131 DECL_CONTEXT (fmem->enclosing)))
7132 inform (DECL_SOURCE_LOCATION (fmem->array),
7133 "array member %q#D declared here", fmem->array);
7137 /* Issue diagnostics for invalid flexible array members or zero-length
7138 arrays that are not the last elements of the containing class or its
7139 base classes or that are its sole members. */
7141 static void
7142 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7144 if (!fmem->array)
7145 return;
7147 if (fmem->first && !fmem->after[0])
7149 diagnose_invalid_flexarray (fmem);
7150 return;
7153 /* Has a diagnostic been issued? */
7154 bool diagd = false;
7156 const char *msg = 0;
7158 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7160 if (fmem->after[0])
7161 msg = G_("zero-size array member %qD not at end of %q#T");
7162 else if (!fmem->first)
7163 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7165 if (msg)
7167 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7169 auto_diagnostic_group d;
7170 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7172 inform (location_of (t), "in the definition of %q#T", t);
7173 diagd = true;
7177 else
7179 if (fmem->after[0])
7180 msg = G_("flexible array member %qD not at end of %q#T");
7181 else if (!fmem->first)
7182 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7184 if (msg)
7186 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7187 diagd = true;
7189 auto_diagnostic_group d;
7190 error_at (loc, msg, fmem->array, t);
7192 /* In the unlikely event that the member following the flexible
7193 array member is declared in a different class, or the member
7194 overlaps another member of a common union, point to it.
7195 Otherwise it should be obvious. */
7196 if (fmem->after[0]
7197 && ((DECL_CONTEXT (fmem->after[0])
7198 != DECL_CONTEXT (fmem->array))))
7200 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7201 "next member %q#D declared here",
7202 fmem->after[0]);
7203 inform (location_of (t), "in the definition of %q#T", t);
7208 if (!diagd && fmem->array && fmem->enclosing)
7209 diagnose_invalid_flexarray (fmem);
7213 /* Recursively check to make sure that any flexible array or zero-length
7214 array members of class T or its bases are valid (i.e., not the sole
7215 non-static data member of T and, if one exists, that it is the last
7216 non-static data member of T and its base classes. FMEM is expected
7217 to be initially null and is used internally by recursive calls to
7218 the function. Issue the appropriate diagnostics for the array member
7219 that fails the checks. */
7221 static void
7222 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7223 bool base_p /* = false */)
7225 /* Initialize the result of a search for flexible array and zero-length
7226 array members. Avoid doing any work if the most interesting FMEM data
7227 have already been populated. */
7228 flexmems_t flexmems = flexmems_t ();
7229 if (!fmem)
7230 fmem = &flexmems;
7231 else if (fmem->array && fmem->first && fmem->after[0])
7232 return;
7234 tree fam = fmem->array;
7236 /* Recursively check the primary base class first. */
7237 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7239 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7240 check_flexarrays (basetype, fmem, true);
7243 /* Recursively check the base classes. */
7244 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7245 for (int i = 0; i < nbases; ++i)
7247 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7249 /* The primary base class was already checked above. */
7250 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7251 continue;
7253 /* Virtual base classes are at the end. */
7254 if (BINFO_VIRTUAL_P (base_binfo))
7255 continue;
7257 /* Check the base class. */
7258 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7261 if (fmem == &flexmems)
7263 /* Check virtual base classes only once per derived class.
7264 I.e., this check is not performed recursively for base
7265 classes. */
7266 int i;
7267 tree base_binfo;
7268 vec<tree, va_gc> *vbases;
7269 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7270 vec_safe_iterate (vbases, i, &base_binfo); i++)
7272 /* Check the virtual base class. */
7273 tree basetype = TREE_TYPE (base_binfo);
7275 check_flexarrays (basetype, fmem, /*base_p=*/true);
7279 /* Is the type unnamed (and therefore a member of it potentially
7280 an anonymous struct or union)? */
7281 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7282 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7283 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7285 /* Search the members of the current (possibly derived) class, skipping
7286 unnamed structs and unions since those could be anonymous. */
7287 if (fmem != &flexmems || !maybe_anon_p)
7288 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7290 if (fmem == &flexmems && !maybe_anon_p)
7292 /* Issue diagnostics for invalid flexible and zero-length array
7293 members found in base classes or among the members of the current
7294 class. Ignore anonymous structs and unions whose members are
7295 considered to be members of the enclosing class and thus will
7296 be diagnosed when checking it. */
7297 diagnose_flexarrays (t, fmem);
7301 /* Perform processing required when the definition of T (a class type)
7302 is complete. Diagnose invalid definitions of flexible array members
7303 and zero-size arrays. */
7305 void
7306 finish_struct_1 (tree t)
7308 tree x;
7309 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7310 tree virtuals = NULL_TREE;
7312 if (COMPLETE_TYPE_P (t))
7314 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7315 error ("redefinition of %q#T", t);
7316 popclass ();
7317 return;
7320 /* If this type was previously laid out as a forward reference,
7321 make sure we lay it out again. */
7322 TYPE_SIZE (t) = NULL_TREE;
7323 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7325 /* Make assumptions about the class; we'll reset the flags if
7326 necessary. */
7327 CLASSTYPE_EMPTY_P (t) = 1;
7328 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7329 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7330 CLASSTYPE_LITERAL_P (t) = true;
7332 /* Do end-of-class semantic processing: checking the validity of the
7333 bases and members and add implicitly generated methods. */
7334 check_bases_and_members (t);
7336 /* Find the key method. */
7337 if (TYPE_CONTAINS_VPTR_P (t))
7339 /* The Itanium C++ ABI permits the key method to be chosen when
7340 the class is defined -- even though the key method so
7341 selected may later turn out to be an inline function. On
7342 some systems (such as ARM Symbian OS) the key method cannot
7343 be determined until the end of the translation unit. On such
7344 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7345 will cause the class to be added to KEYED_CLASSES. Then, in
7346 finish_file we will determine the key method. */
7347 if (targetm.cxx.key_method_may_be_inline ())
7348 determine_key_method (t);
7350 /* If a polymorphic class has no key method, we may emit the vtable
7351 in every translation unit where the class definition appears. If
7352 we're devirtualizing, we can look into the vtable even if we
7353 aren't emitting it. */
7354 if (!CLASSTYPE_KEY_METHOD (t))
7355 vec_safe_push (keyed_classes, t);
7358 /* Layout the class itself. */
7359 layout_class_type (t, &virtuals);
7360 /* COMPLETE_TYPE_P is now true. */
7362 set_class_bindings (t);
7364 /* With the layout complete, check for flexible array members and
7365 zero-length arrays that might overlap other members in the final
7366 layout. */
7367 check_flexarrays (t);
7369 virtuals = modify_all_vtables (t, nreverse (virtuals));
7371 /* If necessary, create the primary vtable for this class. */
7372 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7374 /* We must enter these virtuals into the table. */
7375 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7376 build_primary_vtable (NULL_TREE, t);
7377 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7378 /* Here we know enough to change the type of our virtual
7379 function table, but we will wait until later this function. */
7380 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7382 /* If we're warning about ABI tags, check the types of the new
7383 virtual functions. */
7384 if (warn_abi_tag)
7385 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7386 check_abi_tags (t, TREE_VALUE (v));
7389 if (TYPE_CONTAINS_VPTR_P (t))
7391 int vindex;
7392 tree fn;
7394 if (BINFO_VTABLE (TYPE_BINFO (t)))
7395 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7396 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7397 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7399 /* Add entries for virtual functions introduced by this class. */
7400 BINFO_VIRTUALS (TYPE_BINFO (t))
7401 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7403 /* Set DECL_VINDEX for all functions declared in this class. */
7404 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7406 fn = TREE_CHAIN (fn),
7407 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7408 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7410 tree fndecl = BV_FN (fn);
7412 if (DECL_THUNK_P (fndecl))
7413 /* A thunk. We should never be calling this entry directly
7414 from this vtable -- we'd use the entry for the non
7415 thunk base function. */
7416 DECL_VINDEX (fndecl) = NULL_TREE;
7417 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7418 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7422 finish_struct_bits (t);
7424 set_method_tm_attributes (t);
7425 if (flag_openmp || flag_openmp_simd)
7426 finish_omp_declare_simd_methods (t);
7428 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7429 for any static member objects of the type we're working on. */
7430 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7431 if (DECL_DECLARES_FUNCTION_P (x))
7432 DECL_IN_AGGR_P (x) = false;
7433 else if (VAR_P (x) && TREE_STATIC (x)
7434 && TREE_TYPE (x) != error_mark_node
7435 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7436 SET_DECL_MODE (x, TYPE_MODE (t));
7438 /* Complain if one of the field types requires lower visibility. */
7439 constrain_class_visibility (t);
7441 /* Make the rtl for any new vtables we have created, and unmark
7442 the base types we marked. */
7443 finish_vtbls (t);
7445 /* Build the VTT for T. */
7446 build_vtt (t);
7448 if (warn_nonvdtor
7449 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7450 && !CLASSTYPE_FINAL (t))
7451 warning (OPT_Wnon_virtual_dtor,
7452 "%q#T has virtual functions and accessible"
7453 " non-virtual destructor", t);
7455 complete_vars (t);
7457 if (warn_overloaded_virtual)
7458 warn_hidden (t);
7460 /* Class layout, assignment of virtual table slots, etc., is now
7461 complete. Give the back end a chance to tweak the visibility of
7462 the class or perform any other required target modifications. */
7463 targetm.cxx.adjust_class_at_definition (t);
7465 maybe_suppress_debug_info (t);
7467 if (flag_vtable_verify)
7468 vtv_save_class_info (t);
7470 dump_class_hierarchy (t);
7472 /* Finish debugging output for this type. */
7473 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7475 /* Recalculate satisfaction that might depend on completeness. */
7476 clear_satisfaction_cache ();
7478 if (TYPE_TRANSPARENT_AGGR (t))
7480 tree field = first_field (t);
7481 if (field == NULL_TREE || error_operand_p (field))
7483 error ("type transparent %q#T does not have any fields", t);
7484 TYPE_TRANSPARENT_AGGR (t) = 0;
7486 else if (DECL_ARTIFICIAL (field))
7488 if (DECL_FIELD_IS_BASE (field))
7489 error ("type transparent class %qT has base classes", t);
7490 else
7492 gcc_checking_assert (DECL_VIRTUAL_P (field));
7493 error ("type transparent class %qT has virtual functions", t);
7495 TYPE_TRANSPARENT_AGGR (t) = 0;
7497 else if (TYPE_MODE (t) != DECL_MODE (field))
7499 error ("type transparent %q#T cannot be made transparent because "
7500 "the type of the first field has a different ABI from the "
7501 "class overall", t);
7502 TYPE_TRANSPARENT_AGGR (t) = 0;
7507 /* When T was built up, the member declarations were added in reverse
7508 order. Rearrange them to declaration order. */
7510 void
7511 unreverse_member_declarations (tree t)
7513 tree next;
7514 tree prev;
7515 tree x;
7517 /* The following lists are all in reverse order. Put them in
7518 declaration order now. */
7519 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7521 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7522 order, so we can't just use nreverse. Due to stat_hack
7523 chicanery in finish_member_declaration. */
7524 prev = NULL_TREE;
7525 for (x = TYPE_FIELDS (t);
7526 x && TREE_CODE (x) != TYPE_DECL;
7527 x = next)
7529 next = DECL_CHAIN (x);
7530 DECL_CHAIN (x) = prev;
7531 prev = x;
7534 if (prev)
7536 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7537 TYPE_FIELDS (t) = prev;
7541 tree
7542 finish_struct (tree t, tree attributes)
7544 location_t saved_loc = input_location;
7546 /* Now that we've got all the field declarations, reverse everything
7547 as necessary. */
7548 unreverse_member_declarations (t);
7550 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7551 fixup_attribute_variants (t);
7553 /* Nadger the current location so that diagnostics point to the start of
7554 the struct, not the end. */
7555 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7557 if (processing_template_decl)
7559 tree x;
7561 /* We need to add the target functions of USING_DECLS, so that
7562 they can be found when the using declaration is not
7563 instantiated yet. */
7564 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7565 if (TREE_CODE (x) == USING_DECL)
7567 tree fn = strip_using_decl (x);
7568 if (OVL_P (fn))
7569 for (lkp_iterator iter (fn); iter; ++iter)
7570 add_method (t, *iter, true);
7572 else if (DECL_DECLARES_FUNCTION_P (x))
7574 DECL_IN_AGGR_P (x) = false;
7575 if (DECL_VIRTUAL_P (x))
7576 CLASSTYPE_NON_AGGREGATE (t) = true;
7578 else if (TREE_CODE (x) == FIELD_DECL)
7580 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
7581 CLASSTYPE_NON_AGGREGATE (t) = true;
7584 /* Also add a USING_DECL for operator=. We know there'll be (at
7585 least) one, but we don't know the signature(s). We want name
7586 lookup not to fail or recurse into bases. This isn't added
7587 to the template decl list so we drop this at instantiation
7588 time. */
7589 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
7590 NULL_TREE);
7591 DECL_CONTEXT (ass_op) = t;
7592 USING_DECL_SCOPE (ass_op) = t;
7593 DECL_DEPENDENT_P (ass_op) = true;
7594 DECL_ARTIFICIAL (ass_op) = true;
7595 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
7596 TYPE_FIELDS (t) = ass_op;
7598 TYPE_SIZE (t) = bitsize_zero_node;
7599 TYPE_SIZE_UNIT (t) = size_zero_node;
7600 /* COMPLETE_TYPE_P is now true. */
7602 set_class_bindings (t);
7604 /* We need to emit an error message if this type was used as a parameter
7605 and it is an abstract type, even if it is a template. We construct
7606 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7607 account and we call complete_vars with this type, which will check
7608 the PARM_DECLS. Note that while the type is being defined,
7609 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7610 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7611 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7612 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7613 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7614 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7615 complete_vars (t);
7617 /* Remember current #pragma pack value. */
7618 TYPE_PRECISION (t) = maximum_field_alignment;
7620 if (cxx_dialect < cxx20)
7622 if (!CLASSTYPE_NON_AGGREGATE (t)
7623 && type_has_user_provided_or_explicit_constructor (t))
7624 CLASSTYPE_NON_AGGREGATE (t) = 1;
7626 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
7627 CLASSTYPE_NON_AGGREGATE (t) = 1;
7629 /* Fix up any variants we've already built. */
7630 fixup_type_variants (t);
7632 else
7633 finish_struct_1 (t);
7634 /* COMPLETE_TYPE_P is now true. */
7636 maybe_warn_about_overly_private_class (t);
7638 if (is_std_init_list (t))
7640 /* People keep complaining that the compiler crashes on an invalid
7641 definition of initializer_list, so I guess we should explicitly
7642 reject it. What the compiler internals care about is that it's a
7643 template and has a pointer field followed by size_type field. */
7644 bool ok = false;
7645 if (processing_template_decl)
7647 tree f = next_initializable_field (TYPE_FIELDS (t));
7648 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7650 f = next_initializable_field (DECL_CHAIN (f));
7651 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7652 ok = true;
7655 if (!ok)
7656 fatal_error (input_location, "definition of %qD does not match "
7657 "%<#include <initializer_list>%>", TYPE_NAME (t));
7660 input_location = saved_loc;
7662 TYPE_BEING_DEFINED (t) = 0;
7664 if (current_class_type)
7665 popclass ();
7666 else
7667 error ("trying to finish struct, but kicked out due to previous parse errors");
7669 if (flag_openmp)
7670 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
7671 if (TREE_CODE (decl) == FUNCTION_DECL
7672 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7673 if (tree attr = lookup_attribute ("omp declare variant base",
7674 DECL_ATTRIBUTES (decl)))
7675 omp_declare_variant_finalize (decl, attr);
7677 if (processing_template_decl && at_function_scope_p ()
7678 /* Lambdas are defined by the LAMBDA_EXPR. */
7679 && !LAMBDA_TYPE_P (t))
7680 add_stmt (build_min (TAG_DEFN, t));
7682 return t;
7685 /* Hash table to avoid endless recursion when handling references. */
7686 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7688 /* Return the dynamic type of INSTANCE, if known.
7689 Used to determine whether the virtual function table is needed
7690 or not.
7692 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7693 of our knowledge of its type. *NONNULL should be initialized
7694 before this function is called. */
7696 static tree
7697 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7699 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7701 switch (TREE_CODE (instance))
7703 case INDIRECT_REF:
7704 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7705 return NULL_TREE;
7706 else
7707 return RECUR (TREE_OPERAND (instance, 0));
7709 case CALL_EXPR:
7710 /* This is a call to a constructor, hence it's never zero. */
7711 if (CALL_EXPR_FN (instance)
7712 && TREE_HAS_CONSTRUCTOR (instance))
7714 if (nonnull)
7715 *nonnull = 1;
7716 return TREE_TYPE (instance);
7718 return NULL_TREE;
7720 case SAVE_EXPR:
7721 /* This is a call to a constructor, hence it's never zero. */
7722 if (TREE_HAS_CONSTRUCTOR (instance))
7724 if (nonnull)
7725 *nonnull = 1;
7726 return TREE_TYPE (instance);
7728 return RECUR (TREE_OPERAND (instance, 0));
7730 case POINTER_PLUS_EXPR:
7731 case PLUS_EXPR:
7732 case MINUS_EXPR:
7733 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7734 return RECUR (TREE_OPERAND (instance, 0));
7735 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7736 /* Propagate nonnull. */
7737 return RECUR (TREE_OPERAND (instance, 0));
7739 return NULL_TREE;
7741 CASE_CONVERT:
7742 return RECUR (TREE_OPERAND (instance, 0));
7744 case ADDR_EXPR:
7745 instance = TREE_OPERAND (instance, 0);
7746 if (nonnull)
7748 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7749 with a real object -- given &p->f, p can still be null. */
7750 tree t = get_base_address (instance);
7751 /* ??? Probably should check DECL_WEAK here. */
7752 if (t && DECL_P (t))
7753 *nonnull = 1;
7755 return RECUR (instance);
7757 case COMPONENT_REF:
7758 /* If this component is really a base class reference, then the field
7759 itself isn't definitive. */
7760 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7761 return RECUR (TREE_OPERAND (instance, 0));
7762 return RECUR (TREE_OPERAND (instance, 1));
7764 case VAR_DECL:
7765 case FIELD_DECL:
7766 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7767 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7769 if (nonnull)
7770 *nonnull = 1;
7771 return TREE_TYPE (TREE_TYPE (instance));
7773 /* fall through. */
7774 case TARGET_EXPR:
7775 case PARM_DECL:
7776 case RESULT_DECL:
7777 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7779 if (nonnull)
7780 *nonnull = 1;
7781 return TREE_TYPE (instance);
7783 else if (instance == current_class_ptr)
7785 if (nonnull)
7786 *nonnull = 1;
7788 /* if we're in a ctor or dtor, we know our type. If
7789 current_class_ptr is set but we aren't in a function, we're in
7790 an NSDMI (and therefore a constructor). */
7791 if (current_scope () != current_function_decl
7792 || (DECL_LANG_SPECIFIC (current_function_decl)
7793 && (DECL_CONSTRUCTOR_P (current_function_decl)
7794 || DECL_DESTRUCTOR_P (current_function_decl))))
7796 if (cdtorp)
7797 *cdtorp = 1;
7798 return TREE_TYPE (TREE_TYPE (instance));
7801 else if (TYPE_REF_P (TREE_TYPE (instance)))
7803 /* We only need one hash table because it is always left empty. */
7804 if (!fixed_type_or_null_ref_ht)
7805 fixed_type_or_null_ref_ht
7806 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7808 /* Reference variables should be references to objects. */
7809 if (nonnull)
7810 *nonnull = 1;
7812 /* Enter the INSTANCE in a table to prevent recursion; a
7813 variable's initializer may refer to the variable
7814 itself. */
7815 if (VAR_P (instance)
7816 && DECL_INITIAL (instance)
7817 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7818 && !fixed_type_or_null_ref_ht->find (instance))
7820 tree type;
7821 tree_node **slot;
7823 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7824 *slot = instance;
7825 type = RECUR (DECL_INITIAL (instance));
7826 fixed_type_or_null_ref_ht->remove_elt (instance);
7828 return type;
7831 return NULL_TREE;
7833 case VIEW_CONVERT_EXPR:
7834 if (location_wrapper_p (instance))
7835 return RECUR (TREE_OPERAND (instance, 0));
7836 else
7837 /* TODO: Recursion may be correct for some non-location-wrapper
7838 uses of VIEW_CONVERT_EXPR. */
7839 return NULL_TREE;
7841 default:
7842 return NULL_TREE;
7844 #undef RECUR
7847 /* Return nonzero if the dynamic type of INSTANCE is known, and
7848 equivalent to the static type. We also handle the case where
7849 INSTANCE is really a pointer. Return negative if this is a
7850 ctor/dtor. There the dynamic type is known, but this might not be
7851 the most derived base of the original object, and hence virtual
7852 bases may not be laid out according to this type.
7854 Used to determine whether the virtual function table is needed
7855 or not.
7857 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7858 of our knowledge of its type. *NONNULL should be initialized
7859 before this function is called. */
7862 resolves_to_fixed_type_p (tree instance, int* nonnull)
7864 tree t = TREE_TYPE (instance);
7865 int cdtorp = 0;
7866 tree fixed;
7868 /* processing_template_decl can be false in a template if we're in
7869 instantiate_non_dependent_expr, but we still want to suppress
7870 this check. */
7871 if (in_template_function ())
7873 /* In a template we only care about the type of the result. */
7874 if (nonnull)
7875 *nonnull = true;
7876 return true;
7879 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7880 if (INDIRECT_TYPE_P (t))
7881 t = TREE_TYPE (t);
7882 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
7883 return 1;
7884 if (fixed == NULL_TREE)
7885 return 0;
7886 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7887 return 0;
7888 return cdtorp ? -1 : 1;
7892 void
7893 init_class_processing (void)
7895 current_class_depth = 0;
7896 current_class_stack_size = 10;
7897 current_class_stack
7898 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7899 sizeof_biggest_empty_class = size_zero_node;
7901 ridpointers[(int) RID_PUBLIC] = access_public_node;
7902 ridpointers[(int) RID_PRIVATE] = access_private_node;
7903 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7906 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7908 static void
7909 restore_class_cache (void)
7911 tree type;
7913 /* We are re-entering the same class we just left, so we don't
7914 have to search the whole inheritance matrix to find all the
7915 decls to bind again. Instead, we install the cached
7916 class_shadowed list and walk through it binding names. */
7917 push_binding_level (previous_class_level);
7918 class_binding_level = previous_class_level;
7919 /* Restore IDENTIFIER_TYPE_VALUE. */
7920 for (type = class_binding_level->type_shadowed;
7921 type;
7922 type = TREE_CHAIN (type))
7923 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7926 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7927 appropriate for TYPE.
7929 So that we may avoid calls to lookup_name, we cache the _TYPE
7930 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7932 For multiple inheritance, we perform a two-pass depth-first search
7933 of the type lattice. */
7935 void
7936 pushclass (tree type)
7938 class_stack_node_t csn;
7940 type = TYPE_MAIN_VARIANT (type);
7942 /* Make sure there is enough room for the new entry on the stack. */
7943 if (current_class_depth + 1 >= current_class_stack_size)
7945 current_class_stack_size *= 2;
7946 current_class_stack
7947 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7948 current_class_stack_size);
7951 /* Insert a new entry on the class stack. */
7952 csn = current_class_stack + current_class_depth;
7953 csn->name = current_class_name;
7954 csn->type = current_class_type;
7955 csn->access = current_access_specifier;
7956 csn->names_used = 0;
7957 csn->hidden = 0;
7958 current_class_depth++;
7960 /* Now set up the new type. */
7961 current_class_name = TYPE_NAME (type);
7962 if (TREE_CODE (current_class_name) == TYPE_DECL)
7963 current_class_name = DECL_NAME (current_class_name);
7964 current_class_type = type;
7966 /* By default, things in classes are private, while things in
7967 structures or unions are public. */
7968 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7969 ? access_private_node
7970 : access_public_node);
7972 if (previous_class_level
7973 && type != previous_class_level->this_entity
7974 && current_class_depth == 1)
7976 /* Forcibly remove any old class remnants. */
7977 invalidate_class_lookup_cache ();
7980 if (!previous_class_level
7981 || type != previous_class_level->this_entity
7982 || current_class_depth > 1)
7983 pushlevel_class ();
7984 else
7985 restore_class_cache ();
7988 /* Get out of the current class scope. If we were in a class scope
7989 previously, that is the one popped to. */
7991 void
7992 popclass (void)
7994 poplevel_class ();
7996 current_class_depth--;
7997 current_class_name = current_class_stack[current_class_depth].name;
7998 current_class_type = current_class_stack[current_class_depth].type;
7999 current_access_specifier = current_class_stack[current_class_depth].access;
8000 if (current_class_stack[current_class_depth].names_used)
8001 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8004 /* Mark the top of the class stack as hidden. */
8006 void
8007 push_class_stack (void)
8009 if (current_class_depth)
8010 ++current_class_stack[current_class_depth - 1].hidden;
8013 /* Mark the top of the class stack as un-hidden. */
8015 void
8016 pop_class_stack (void)
8018 if (current_class_depth)
8019 --current_class_stack[current_class_depth - 1].hidden;
8022 /* If the class type currently being defined is either T or
8023 a nested type of T, returns the type from the current_class_stack,
8024 which might be equivalent to but not equal to T in case of
8025 constrained partial specializations. */
8027 tree
8028 currently_open_class (tree t)
8030 int i;
8032 if (!CLASS_TYPE_P (t))
8033 return NULL_TREE;
8035 t = TYPE_MAIN_VARIANT (t);
8037 /* We start looking from 1 because entry 0 is from global scope,
8038 and has no type. */
8039 for (i = current_class_depth; i > 0; --i)
8041 tree c;
8042 if (i == current_class_depth)
8043 c = current_class_type;
8044 else
8046 if (current_class_stack[i].hidden)
8047 break;
8048 c = current_class_stack[i].type;
8050 if (!c)
8051 continue;
8052 if (same_type_p (c, t))
8053 return c;
8055 return NULL_TREE;
8058 /* If either current_class_type or one of its enclosing classes are derived
8059 from T, return the appropriate type. Used to determine how we found
8060 something via unqualified lookup. */
8062 tree
8063 currently_open_derived_class (tree t)
8065 int i;
8067 /* The bases of a dependent type are unknown. */
8068 if (dependent_type_p (t))
8069 return NULL_TREE;
8071 if (!current_class_type)
8072 return NULL_TREE;
8074 if (DERIVED_FROM_P (t, current_class_type))
8075 return current_class_type;
8077 for (i = current_class_depth - 1; i > 0; --i)
8079 if (current_class_stack[i].hidden)
8080 break;
8081 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8082 return current_class_stack[i].type;
8085 return NULL_TREE;
8088 /* Return the outermost enclosing class type that is still open, or
8089 NULL_TREE. */
8091 tree
8092 outermost_open_class (void)
8094 if (!current_class_type)
8095 return NULL_TREE;
8096 tree r = NULL_TREE;
8097 if (TYPE_BEING_DEFINED (current_class_type))
8098 r = current_class_type;
8099 for (int i = current_class_depth - 1; i > 0; --i)
8101 if (current_class_stack[i].hidden)
8102 break;
8103 tree t = current_class_stack[i].type;
8104 if (!TYPE_BEING_DEFINED (t))
8105 break;
8106 r = t;
8108 return r;
8111 /* Returns the innermost class type which is not a lambda closure type. */
8113 tree
8114 current_nonlambda_class_type (void)
8116 tree type = current_class_type;
8117 while (type && LAMBDA_TYPE_P (type))
8118 type = decl_type_context (TYPE_NAME (type));
8119 return type;
8122 /* When entering a class scope, all enclosing class scopes' names with
8123 static meaning (static variables, static functions, types and
8124 enumerators) have to be visible. This recursive function calls
8125 pushclass for all enclosing class contexts until global or a local
8126 scope is reached. TYPE is the enclosed class. */
8128 void
8129 push_nested_class (tree type)
8131 /* A namespace might be passed in error cases, like A::B:C. */
8132 if (type == NULL_TREE
8133 || !CLASS_TYPE_P (type))
8134 return;
8136 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8138 pushclass (type);
8141 /* Undoes a push_nested_class call. */
8143 void
8144 pop_nested_class (void)
8146 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8148 popclass ();
8149 if (context && CLASS_TYPE_P (context))
8150 pop_nested_class ();
8153 /* Returns the number of extern "LANG" blocks we are nested within. */
8156 current_lang_depth (void)
8158 return vec_safe_length (current_lang_base);
8161 /* Set global variables CURRENT_LANG_NAME to appropriate value
8162 so that behavior of name-mangling machinery is correct. */
8164 void
8165 push_lang_context (tree name)
8167 vec_safe_push (current_lang_base, current_lang_name);
8169 if (name == lang_name_cplusplus)
8170 current_lang_name = name;
8171 else if (name == lang_name_c)
8172 current_lang_name = name;
8173 else
8174 error ("language string %<\"%E\"%> not recognized", name);
8177 /* Get out of the current language scope. */
8179 void
8180 pop_lang_context (void)
8182 current_lang_name = current_lang_base->pop ();
8185 /* Type instantiation routines. */
8187 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8188 matches the TARGET_TYPE. If there is no satisfactory match, return
8189 error_mark_node, and issue an error & warning messages under
8190 control of FLAGS. Permit pointers to member function if FLAGS
8191 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8192 a template-id, and EXPLICIT_TARGS are the explicitly provided
8193 template arguments.
8195 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8196 is the base path used to reference those member functions. If
8197 the address is resolved to a member function, access checks will be
8198 performed and errors issued if appropriate. */
8200 static tree
8201 resolve_address_of_overloaded_function (tree target_type,
8202 tree overload,
8203 tsubst_flags_t complain,
8204 bool template_only,
8205 tree explicit_targs,
8206 tree access_path)
8208 /* Here's what the standard says:
8210 [over.over]
8212 If the name is a function template, template argument deduction
8213 is done, and if the argument deduction succeeds, the deduced
8214 arguments are used to generate a single template function, which
8215 is added to the set of overloaded functions considered.
8217 Non-member functions and static member functions match targets of
8218 type "pointer-to-function" or "reference-to-function." Nonstatic
8219 member functions match targets of type "pointer-to-member
8220 function;" the function type of the pointer to member is used to
8221 select the member function from the set of overloaded member
8222 functions. If a non-static member function is selected, the
8223 reference to the overloaded function name is required to have the
8224 form of a pointer to member as described in 5.3.1.
8226 If more than one function is selected, any template functions in
8227 the set are eliminated if the set also contains a non-template
8228 function, and any given template function is eliminated if the
8229 set contains a second template function that is more specialized
8230 than the first according to the partial ordering rules 14.5.5.2.
8231 After such eliminations, if any, there shall remain exactly one
8232 selected function. */
8234 int is_ptrmem = 0;
8235 /* We store the matches in a TREE_LIST rooted here. The functions
8236 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8237 interoperability with most_specialized_instantiation. */
8238 tree matches = NULL_TREE;
8239 tree fn;
8240 tree target_fn_type;
8242 /* By the time we get here, we should be seeing only real
8243 pointer-to-member types, not the internal POINTER_TYPE to
8244 METHOD_TYPE representation. */
8245 gcc_assert (!TYPE_PTR_P (target_type)
8246 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8248 gcc_assert (is_overloaded_fn (overload));
8250 /* Check that the TARGET_TYPE is reasonable. */
8251 if (TYPE_PTRFN_P (target_type)
8252 || TYPE_REFFN_P (target_type))
8253 /* This is OK. */;
8254 else if (TYPE_PTRMEMFUNC_P (target_type))
8255 /* This is OK, too. */
8256 is_ptrmem = 1;
8257 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8258 /* This is OK, too. This comes from a conversion to reference
8259 type. */
8260 target_type = build_reference_type (target_type);
8261 else
8263 if (complain & tf_error)
8264 error ("cannot resolve overloaded function %qD based on"
8265 " conversion to type %qT",
8266 OVL_NAME (overload), target_type);
8267 return error_mark_node;
8270 /* Non-member functions and static member functions match targets of type
8271 "pointer-to-function" or "reference-to-function." Nonstatic member
8272 functions match targets of type "pointer-to-member-function;" the
8273 function type of the pointer to member is used to select the member
8274 function from the set of overloaded member functions.
8276 So figure out the FUNCTION_TYPE that we want to match against. */
8277 target_fn_type = static_fn_type (target_type);
8279 /* If we can find a non-template function that matches, we can just
8280 use it. There's no point in generating template instantiations
8281 if we're just going to throw them out anyhow. But, of course, we
8282 can only do this when we don't *need* a template function. */
8283 if (!template_only)
8284 for (lkp_iterator iter (overload); iter; ++iter)
8286 tree fn = *iter;
8288 if (TREE_CODE (fn) == TEMPLATE_DECL)
8289 /* We're not looking for templates just yet. */
8290 continue;
8292 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8293 /* We're looking for a non-static member, and this isn't
8294 one, or vice versa. */
8295 continue;
8297 /* Constraints must be satisfied. This is done before
8298 return type deduction since that instantiates the
8299 function. */
8300 if (!constraints_satisfied_p (fn))
8301 continue;
8303 if (undeduced_auto_decl (fn))
8305 /* Force instantiation to do return type deduction. */
8306 maybe_instantiate_decl (fn);
8307 require_deduced_type (fn);
8310 /* In C++17 we need the noexcept-qualifier to compare types. */
8311 if (flag_noexcept_type
8312 && !maybe_instantiate_noexcept (fn, complain))
8313 continue;
8315 /* See if there's a match. */
8316 tree fntype = static_fn_type (fn);
8317 if (same_type_p (target_fn_type, fntype)
8318 || fnptr_conv_p (target_fn_type, fntype))
8319 matches = tree_cons (fn, NULL_TREE, matches);
8322 /* Now, if we've already got a match (or matches), there's no need
8323 to proceed to the template functions. But, if we don't have a
8324 match we need to look at them, too. */
8325 if (!matches)
8327 tree target_arg_types;
8328 tree target_ret_type;
8329 tree *args;
8330 unsigned int nargs, ia;
8331 tree arg;
8333 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8334 target_ret_type = TREE_TYPE (target_fn_type);
8336 nargs = list_length (target_arg_types);
8337 args = XALLOCAVEC (tree, nargs);
8338 for (arg = target_arg_types, ia = 0;
8339 arg != NULL_TREE && arg != void_list_node;
8340 arg = TREE_CHAIN (arg), ++ia)
8341 args[ia] = TREE_VALUE (arg);
8342 nargs = ia;
8344 for (lkp_iterator iter (overload); iter; ++iter)
8346 tree fn = *iter;
8347 tree instantiation;
8348 tree targs;
8350 if (TREE_CODE (fn) != TEMPLATE_DECL)
8351 /* We're only looking for templates. */
8352 continue;
8354 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8355 != is_ptrmem)
8356 /* We're not looking for a non-static member, and this is
8357 one, or vice versa. */
8358 continue;
8360 tree ret = target_ret_type;
8362 /* If the template has a deduced return type, don't expose it to
8363 template argument deduction. */
8364 if (undeduced_auto_decl (fn))
8365 ret = NULL_TREE;
8367 /* Try to do argument deduction. */
8368 targs = make_tree_vec (DECL_NTPARMS (fn));
8369 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8370 nargs, ret,
8371 DEDUCE_EXACT, LOOKUP_NORMAL,
8372 NULL, false, false);
8373 if (instantiation == error_mark_node)
8374 /* Instantiation failed. */
8375 continue;
8377 /* Constraints must be satisfied. This is done before
8378 return type deduction since that instantiates the
8379 function. */
8380 if (flag_concepts && !constraints_satisfied_p (instantiation))
8381 continue;
8383 /* And now force instantiation to do return type deduction. */
8384 if (undeduced_auto_decl (instantiation))
8386 ++function_depth;
8387 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8388 --function_depth;
8390 require_deduced_type (instantiation);
8393 /* In C++17 we need the noexcept-qualifier to compare types. */
8394 if (flag_noexcept_type)
8395 maybe_instantiate_noexcept (instantiation, complain);
8397 /* See if there's a match. */
8398 tree fntype = static_fn_type (instantiation);
8399 if (same_type_p (target_fn_type, fntype)
8400 || fnptr_conv_p (target_fn_type, fntype))
8401 matches = tree_cons (instantiation, fn, matches);
8404 /* Now, remove all but the most specialized of the matches. */
8405 if (matches)
8407 tree match = most_specialized_instantiation (matches);
8409 if (match != error_mark_node)
8410 matches = tree_cons (TREE_PURPOSE (match),
8411 NULL_TREE,
8412 NULL_TREE);
8416 /* Now we should have exactly one function in MATCHES. */
8417 if (matches == NULL_TREE)
8419 /* There were *no* matches. */
8420 if (complain & tf_error)
8422 error ("no matches converting function %qD to type %q#T",
8423 OVL_NAME (overload), target_type);
8425 print_candidates (overload);
8427 return error_mark_node;
8429 else if (TREE_CHAIN (matches))
8431 /* There were too many matches. First check if they're all
8432 the same function. */
8433 tree match = NULL_TREE;
8435 fn = TREE_PURPOSE (matches);
8437 /* For multi-versioned functions, more than one match is just fine and
8438 decls_match will return false as they are different. */
8439 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8440 if (!decls_match (fn, TREE_PURPOSE (match))
8441 && !targetm.target_option.function_versions
8442 (fn, TREE_PURPOSE (match)))
8443 break;
8445 if (match)
8447 if (complain & tf_error)
8449 error ("converting overloaded function %qD to type %q#T is ambiguous",
8450 OVL_NAME (overload), target_type);
8452 /* Since print_candidates expects the functions in the
8453 TREE_VALUE slot, we flip them here. */
8454 for (match = matches; match; match = TREE_CHAIN (match))
8455 TREE_VALUE (match) = TREE_PURPOSE (match);
8457 print_candidates (matches);
8460 return error_mark_node;
8464 /* Good, exactly one match. Now, convert it to the correct type. */
8465 fn = TREE_PURPOSE (matches);
8467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8468 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8470 static int explained;
8472 if (!(complain & tf_error))
8473 return error_mark_node;
8475 auto_diagnostic_group d;
8476 if (permerror (input_location, "assuming pointer to member %qD", fn)
8477 && !explained)
8479 inform (input_location, "(a pointer to member can only be "
8480 "formed with %<&%E%>)", fn);
8481 explained = 1;
8485 /* If a pointer to a function that is multi-versioned is requested, the
8486 pointer to the dispatcher function is returned instead. This works
8487 well because indirectly calling the function will dispatch the right
8488 function version at run-time. */
8489 if (DECL_FUNCTION_VERSIONED (fn))
8491 fn = get_function_version_dispatcher (fn);
8492 if (fn == NULL)
8493 return error_mark_node;
8494 /* Mark all the versions corresponding to the dispatcher as used. */
8495 if (!(complain & tf_conv))
8496 mark_versions_used (fn);
8499 /* If we're doing overload resolution purely for the purpose of
8500 determining conversion sequences, we should not consider the
8501 function used. If this conversion sequence is selected, the
8502 function will be marked as used at this point. */
8503 if (!(complain & tf_conv))
8505 /* Make =delete work with SFINAE. */
8506 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8507 return error_mark_node;
8508 if (!mark_used (fn, complain) && !(complain & tf_error))
8509 return error_mark_node;
8512 /* We could not check access to member functions when this
8513 expression was originally created since we did not know at that
8514 time to which function the expression referred. */
8515 if (DECL_FUNCTION_MEMBER_P (fn))
8517 gcc_assert (access_path);
8518 perform_or_defer_access_check (access_path, fn, fn, complain);
8521 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8522 return cp_build_addr_expr (fn, complain);
8523 else
8525 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8526 will mark the function as addressed, but here we must do it
8527 explicitly. */
8528 cxx_mark_addressable (fn);
8530 return fn;
8534 /* This function will instantiate the type of the expression given in
8535 RHS to match the type of LHSTYPE. If errors exist, then return
8536 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8537 we complain on errors. If we are not complaining, never modify rhs,
8538 as overload resolution wants to try many possible instantiations, in
8539 the hope that at least one will work.
8541 For non-recursive calls, LHSTYPE should be a function, pointer to
8542 function, or a pointer to member function. */
8544 tree
8545 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8547 tsubst_flags_t complain_in = complain;
8548 tree access_path = NULL_TREE;
8550 complain &= ~tf_ptrmem_ok;
8552 if (lhstype == unknown_type_node)
8554 if (complain & tf_error)
8555 error ("not enough type information");
8556 return error_mark_node;
8559 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8561 tree fntype = non_reference (lhstype);
8562 if (same_type_p (fntype, TREE_TYPE (rhs)))
8563 return rhs;
8564 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8565 return rhs;
8566 if (flag_ms_extensions
8567 && TYPE_PTRMEMFUNC_P (fntype)
8568 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8569 /* Microsoft allows `A::f' to be resolved to a
8570 pointer-to-member. */
8572 else
8574 if (complain & tf_error)
8575 error ("cannot convert %qE from type %qT to type %qT",
8576 rhs, TREE_TYPE (rhs), fntype);
8577 return error_mark_node;
8581 /* If we instantiate a template, and it is a A ?: C expression
8582 with omitted B, look through the SAVE_EXPR. */
8583 if (TREE_CODE (rhs) == SAVE_EXPR)
8584 rhs = TREE_OPERAND (rhs, 0);
8586 if (BASELINK_P (rhs))
8588 access_path = BASELINK_ACCESS_BINFO (rhs);
8589 rhs = BASELINK_FUNCTIONS (rhs);
8592 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8593 deduce any type information. */
8594 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8596 if (complain & tf_error)
8597 error ("not enough type information");
8598 return error_mark_node;
8601 /* There are only a few kinds of expressions that may have a type
8602 dependent on overload resolution. */
8603 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8604 || TREE_CODE (rhs) == COMPONENT_REF
8605 || is_overloaded_fn (rhs)
8606 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8608 /* This should really only be used when attempting to distinguish
8609 what sort of a pointer to function we have. For now, any
8610 arithmetic operation which is not supported on pointers
8611 is rejected as an error. */
8613 switch (TREE_CODE (rhs))
8615 case COMPONENT_REF:
8617 tree member = TREE_OPERAND (rhs, 1);
8619 member = instantiate_type (lhstype, member, complain);
8620 if (member != error_mark_node
8621 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8622 /* Do not lose object's side effects. */
8623 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8624 TREE_OPERAND (rhs, 0), member);
8625 return member;
8628 case OFFSET_REF:
8629 rhs = TREE_OPERAND (rhs, 1);
8630 if (BASELINK_P (rhs))
8631 return instantiate_type (lhstype, rhs, complain_in);
8633 /* This can happen if we are forming a pointer-to-member for a
8634 member template. */
8635 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8637 /* Fall through. */
8639 case TEMPLATE_ID_EXPR:
8641 tree fns = TREE_OPERAND (rhs, 0);
8642 tree args = TREE_OPERAND (rhs, 1);
8644 return
8645 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8646 /*template_only=*/true,
8647 args, access_path);
8650 case OVERLOAD:
8651 case FUNCTION_DECL:
8652 return
8653 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8654 /*template_only=*/false,
8655 /*explicit_targs=*/NULL_TREE,
8656 access_path);
8658 case ADDR_EXPR:
8660 if (PTRMEM_OK_P (rhs))
8661 complain |= tf_ptrmem_ok;
8663 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8666 case ERROR_MARK:
8667 return error_mark_node;
8669 default:
8670 gcc_unreachable ();
8672 return error_mark_node;
8675 /* Return the name of the virtual function pointer field
8676 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8677 this may have to look back through base types to find the
8678 ultimate field name. (For single inheritance, these could
8679 all be the same name. Who knows for multiple inheritance). */
8681 static tree
8682 get_vfield_name (tree type)
8684 tree binfo, base_binfo;
8686 for (binfo = TYPE_BINFO (type);
8687 BINFO_N_BASE_BINFOS (binfo);
8688 binfo = base_binfo)
8690 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8692 if (BINFO_VIRTUAL_P (base_binfo)
8693 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8694 break;
8697 type = BINFO_TYPE (binfo);
8698 tree ctor_name = constructor_name (type);
8699 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8700 + IDENTIFIER_LENGTH (ctor_name) + 2);
8701 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8702 return get_identifier (buf);
8705 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8706 according to [class]:
8707 The class-name is also inserted
8708 into the scope of the class itself. For purposes of access checking,
8709 the inserted class name is treated as if it were a public member name. */
8711 void
8712 build_self_reference (void)
8714 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8715 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
8717 DECL_NONLOCAL (decl) = 1;
8718 DECL_CONTEXT (decl) = current_class_type;
8719 DECL_ARTIFICIAL (decl) = 1;
8720 SET_DECL_SELF_REFERENCE_P (decl);
8721 set_underlying_type (decl);
8723 if (processing_template_decl)
8724 decl = push_template_decl (decl);
8726 tree saved_cas = current_access_specifier;
8727 current_access_specifier = access_public_node;
8728 finish_member_declaration (decl);
8729 current_access_specifier = saved_cas;
8732 /* Returns 1 if TYPE contains only padding bytes. */
8735 is_empty_class (tree type)
8737 if (type == error_mark_node)
8738 return 0;
8740 if (! CLASS_TYPE_P (type))
8741 return 0;
8743 return CLASSTYPE_EMPTY_P (type);
8746 /* Returns true if TYPE contains no actual data, just various
8747 possible combinations of empty classes. If IGNORE_VPTR is true,
8748 a vptr doesn't prevent the class from being considered empty. Typically
8749 we want to ignore the vptr on assignment, and not on initialization. */
8751 bool
8752 is_really_empty_class (tree type, bool ignore_vptr)
8754 if (CLASS_TYPE_P (type))
8756 tree field;
8757 tree binfo;
8758 tree base_binfo;
8759 int i;
8761 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8762 out, but we'd like to be able to check this before then. */
8763 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8764 return true;
8766 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
8767 return false;
8769 for (binfo = TYPE_BINFO (type), i = 0;
8770 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8771 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
8772 return false;
8773 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8774 if (TREE_CODE (field) == FIELD_DECL
8775 && !DECL_ARTIFICIAL (field)
8776 /* An unnamed bit-field is not a data member. */
8777 && !DECL_UNNAMED_BIT_FIELD (field)
8778 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
8779 return false;
8780 return true;
8782 else if (TREE_CODE (type) == ARRAY_TYPE)
8783 return (integer_zerop (array_type_nelts_top (type))
8784 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
8785 return false;
8788 /* Note that NAME was looked up while the current class was being
8789 defined and that the result of that lookup was DECL. */
8791 void
8792 maybe_note_name_used_in_class (tree name, tree decl)
8794 splay_tree names_used;
8796 /* If we're not defining a class, there's nothing to do. */
8797 if (!(innermost_scope_kind() == sk_class
8798 && TYPE_BEING_DEFINED (current_class_type)
8799 && !LAMBDA_TYPE_P (current_class_type)))
8800 return;
8802 /* If there's already a binding for this NAME, then we don't have
8803 anything to worry about. */
8804 if (lookup_member (current_class_type, name,
8805 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8806 return;
8808 if (!current_class_stack[current_class_depth - 1].names_used)
8809 current_class_stack[current_class_depth - 1].names_used
8810 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8811 names_used = current_class_stack[current_class_depth - 1].names_used;
8813 splay_tree_insert (names_used,
8814 (splay_tree_key) name,
8815 (splay_tree_value) decl);
8818 /* Note that NAME was declared (as DECL) in the current class. Check
8819 to see that the declaration is valid. */
8821 void
8822 note_name_declared_in_class (tree name, tree decl)
8824 splay_tree names_used;
8825 splay_tree_node n;
8827 /* Look to see if we ever used this name. */
8828 names_used
8829 = current_class_stack[current_class_depth - 1].names_used;
8830 if (!names_used)
8831 return;
8832 /* The C language allows members to be declared with a type of the same
8833 name, and the C++ standard says this diagnostic is not required. So
8834 allow it in extern "C" blocks unless predantic is specified.
8835 Allow it in all cases if -ms-extensions is specified. */
8836 if ((!pedantic && current_lang_name == lang_name_c)
8837 || flag_ms_extensions)
8838 return;
8839 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8840 if (n)
8842 /* [basic.scope.class]
8844 A name N used in a class S shall refer to the same declaration
8845 in its context and when re-evaluated in the completed scope of
8846 S. */
8847 if (permerror (location_of (decl),
8848 "declaration of %q#D changes meaning of %qD",
8849 decl, OVL_NAME (decl)))
8850 inform (location_of ((tree) n->value),
8851 "%qD declared here as %q#D",
8852 OVL_NAME (decl), (tree) n->value);
8856 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8857 Secondary vtables are merged with primary vtables; this function
8858 will return the VAR_DECL for the primary vtable. */
8860 tree
8861 get_vtbl_decl_for_binfo (tree binfo)
8863 tree decl;
8865 decl = BINFO_VTABLE (binfo);
8866 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8868 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8869 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8871 if (decl)
8872 gcc_assert (VAR_P (decl));
8873 return decl;
8877 /* Returns the binfo for the primary base of BINFO. If the resulting
8878 BINFO is a virtual base, and it is inherited elsewhere in the
8879 hierarchy, then the returned binfo might not be the primary base of
8880 BINFO in the complete object. Check BINFO_PRIMARY_P or
8881 BINFO_LOST_PRIMARY_P to be sure. */
8883 static tree
8884 get_primary_binfo (tree binfo)
8886 tree primary_base;
8888 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8889 if (!primary_base)
8890 return NULL_TREE;
8892 return copied_binfo (primary_base, binfo);
8895 /* As above, but iterate until we reach the binfo that actually provides the
8896 vptr for BINFO. */
8898 static tree
8899 most_primary_binfo (tree binfo)
8901 tree b = binfo;
8902 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8903 && !BINFO_LOST_PRIMARY_P (b))
8905 tree primary_base = get_primary_binfo (b);
8906 gcc_assert (BINFO_PRIMARY_P (primary_base)
8907 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8908 b = primary_base;
8910 return b;
8913 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8914 type. Note that the virtual inheritance might be above or below BINFO in
8915 the hierarchy. */
8917 bool
8918 vptr_via_virtual_p (tree binfo)
8920 if (TYPE_P (binfo))
8921 binfo = TYPE_BINFO (binfo);
8922 tree primary = most_primary_binfo (binfo);
8923 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8924 a morally virtual base. */
8925 tree virt = binfo_via_virtual (primary, NULL_TREE);
8926 return virt != NULL_TREE;
8929 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8931 static int
8932 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8934 if (!indented_p)
8935 fprintf (stream, "%*s", indent, "");
8936 return 1;
8939 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8940 INDENT should be zero when called from the top level; it is
8941 incremented recursively. IGO indicates the next expected BINFO in
8942 inheritance graph ordering. */
8944 static tree
8945 dump_class_hierarchy_r (FILE *stream,
8946 dump_flags_t flags,
8947 tree binfo,
8948 tree igo,
8949 int indent)
8951 int indented = 0;
8952 tree base_binfo;
8953 int i;
8955 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8956 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8957 (HOST_WIDE_INT) (uintptr_t) binfo);
8958 if (binfo != igo)
8960 fprintf (stream, "alternative-path\n");
8961 return igo;
8963 igo = TREE_CHAIN (binfo);
8965 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8966 tree_to_shwi (BINFO_OFFSET (binfo)));
8967 if (is_empty_class (BINFO_TYPE (binfo)))
8968 fprintf (stream, " empty");
8969 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8970 fprintf (stream, " nearly-empty");
8971 if (BINFO_VIRTUAL_P (binfo))
8972 fprintf (stream, " virtual");
8973 fprintf (stream, "\n");
8975 if (BINFO_PRIMARY_P (binfo))
8977 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8978 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8979 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8980 TFF_PLAIN_IDENTIFIER),
8981 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8983 if (BINFO_LOST_PRIMARY_P (binfo))
8985 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8986 fprintf (stream, " lost-primary");
8988 if (indented)
8989 fprintf (stream, "\n");
8991 if (!(flags & TDF_SLIM))
8993 int indented = 0;
8995 if (BINFO_SUBVTT_INDEX (binfo))
8997 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8998 fprintf (stream, " subvttidx=%s",
8999 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9000 TFF_PLAIN_IDENTIFIER));
9002 if (BINFO_VPTR_INDEX (binfo))
9004 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9005 fprintf (stream, " vptridx=%s",
9006 expr_as_string (BINFO_VPTR_INDEX (binfo),
9007 TFF_PLAIN_IDENTIFIER));
9009 if (BINFO_VPTR_FIELD (binfo))
9011 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9012 fprintf (stream, " vbaseoffset=%s",
9013 expr_as_string (BINFO_VPTR_FIELD (binfo),
9014 TFF_PLAIN_IDENTIFIER));
9016 if (BINFO_VTABLE (binfo))
9018 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9019 fprintf (stream, " vptr=%s",
9020 expr_as_string (BINFO_VTABLE (binfo),
9021 TFF_PLAIN_IDENTIFIER));
9024 if (indented)
9025 fprintf (stream, "\n");
9028 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9029 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9031 return igo;
9034 /* Dump the BINFO hierarchy for T. */
9036 static void
9037 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9039 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9040 fprintf (stream, " size=%lu align=%lu\n",
9041 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
9042 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
9043 if (tree as_base = CLASSTYPE_AS_BASE (t))
9044 fprintf (stream, " base size=%lu base align=%lu\n",
9045 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base))
9046 / BITS_PER_UNIT),
9047 (unsigned long)(TYPE_ALIGN (as_base) / BITS_PER_UNIT));
9048 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9049 fprintf (stream, "\n");
9052 /* Debug interface to hierarchy dumping. */
9054 void
9055 debug_class (tree t)
9057 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9060 static void
9061 dump_class_hierarchy (tree t)
9063 dump_flags_t flags;
9064 if (FILE *stream = dump_begin (class_dump_id, &flags))
9066 dump_class_hierarchy_1 (stream, flags, t);
9067 dump_end (class_dump_id, stream);
9071 static void
9072 dump_array (FILE * stream, tree decl)
9074 tree value;
9075 unsigned HOST_WIDE_INT ix;
9076 HOST_WIDE_INT elt;
9077 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9079 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9080 / BITS_PER_UNIT);
9081 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9082 fprintf (stream, " %s entries",
9083 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9084 TFF_PLAIN_IDENTIFIER));
9085 fprintf (stream, "\n");
9087 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9088 ix, value)
9089 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9090 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9093 static void
9094 dump_vtable (tree t, tree binfo, tree vtable)
9096 dump_flags_t flags;
9097 FILE *stream = dump_begin (class_dump_id, &flags);
9099 if (!stream)
9100 return;
9102 if (!(flags & TDF_SLIM))
9104 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9106 fprintf (stream, "%s for %s",
9107 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9108 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9109 if (ctor_vtbl_p)
9111 if (!BINFO_VIRTUAL_P (binfo))
9112 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9113 (HOST_WIDE_INT) (uintptr_t) binfo);
9114 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9116 fprintf (stream, "\n");
9117 dump_array (stream, vtable);
9118 fprintf (stream, "\n");
9121 dump_end (class_dump_id, stream);
9124 static void
9125 dump_vtt (tree t, tree vtt)
9127 dump_flags_t flags;
9128 FILE *stream = dump_begin (class_dump_id, &flags);
9130 if (!stream)
9131 return;
9133 if (!(flags & TDF_SLIM))
9135 fprintf (stream, "VTT for %s\n",
9136 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9137 dump_array (stream, vtt);
9138 fprintf (stream, "\n");
9141 dump_end (class_dump_id, stream);
9144 /* Dump a function or thunk and its thunkees. */
9146 static void
9147 dump_thunk (FILE *stream, int indent, tree thunk)
9149 static const char spaces[] = " ";
9150 tree name = DECL_NAME (thunk);
9151 tree thunks;
9153 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9154 (void *)thunk,
9155 !DECL_THUNK_P (thunk) ? "function"
9156 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9157 name ? IDENTIFIER_POINTER (name) : "<unset>");
9158 if (DECL_THUNK_P (thunk))
9160 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9161 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9163 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9164 if (!virtual_adjust)
9165 /*NOP*/;
9166 else if (DECL_THIS_THUNK_P (thunk))
9167 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9168 tree_to_shwi (virtual_adjust));
9169 else
9170 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9171 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9172 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9173 if (THUNK_ALIAS (thunk))
9174 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9176 fprintf (stream, "\n");
9177 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9178 dump_thunk (stream, indent + 2, thunks);
9181 /* Dump the thunks for FN. */
9183 void
9184 debug_thunks (tree fn)
9186 dump_thunk (stderr, 0, fn);
9189 /* Virtual function table initialization. */
9191 /* Create all the necessary vtables for T and its base classes. */
9193 static void
9194 finish_vtbls (tree t)
9196 tree vbase;
9197 vec<constructor_elt, va_gc> *v = NULL;
9198 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9200 /* We lay out the primary and secondary vtables in one contiguous
9201 vtable. The primary vtable is first, followed by the non-virtual
9202 secondary vtables in inheritance graph order. */
9203 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9204 vtable, t, &v);
9206 /* Then come the virtual bases, also in inheritance graph order. */
9207 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9209 if (!BINFO_VIRTUAL_P (vbase))
9210 continue;
9211 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9214 if (BINFO_VTABLE (TYPE_BINFO (t)))
9215 initialize_vtable (TYPE_BINFO (t), v);
9218 /* Initialize the vtable for BINFO with the INITS. */
9220 static void
9221 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9223 tree decl;
9225 layout_vtable_decl (binfo, vec_safe_length (inits));
9226 decl = get_vtbl_decl_for_binfo (binfo);
9227 initialize_artificial_var (decl, inits);
9228 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9231 /* Build the VTT (virtual table table) for T.
9232 A class requires a VTT if it has virtual bases.
9234 This holds
9235 1 - primary virtual pointer for complete object T
9236 2 - secondary VTTs for each direct non-virtual base of T which requires a
9238 3 - secondary virtual pointers for each direct or indirect base of T which
9239 has virtual bases or is reachable via a virtual path from T.
9240 4 - secondary VTTs for each direct or indirect virtual base of T.
9242 Secondary VTTs look like complete object VTTs without part 4. */
9244 static void
9245 build_vtt (tree t)
9247 tree type;
9248 tree vtt;
9249 tree index;
9250 vec<constructor_elt, va_gc> *inits;
9252 /* Build up the initializers for the VTT. */
9253 inits = NULL;
9254 index = size_zero_node;
9255 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9257 /* If we didn't need a VTT, we're done. */
9258 if (!inits)
9259 return;
9261 /* Figure out the type of the VTT. */
9262 type = build_array_of_n_type (const_ptr_type_node,
9263 inits->length ());
9265 /* Now, build the VTT object itself. */
9266 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9267 initialize_artificial_var (vtt, inits);
9268 /* Add the VTT to the vtables list. */
9269 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9270 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9272 dump_vtt (t, vtt);
9275 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9276 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9277 and CHAIN the vtable pointer for this binfo after construction is
9278 complete. VALUE can also be another BINFO, in which case we recurse. */
9280 static tree
9281 binfo_ctor_vtable (tree binfo)
9283 tree vt;
9285 while (1)
9287 vt = BINFO_VTABLE (binfo);
9288 if (TREE_CODE (vt) == TREE_LIST)
9289 vt = TREE_VALUE (vt);
9290 if (TREE_CODE (vt) == TREE_BINFO)
9291 binfo = vt;
9292 else
9293 break;
9296 return vt;
9299 /* Data for secondary VTT initialization. */
9300 struct secondary_vptr_vtt_init_data
9302 /* Is this the primary VTT? */
9303 bool top_level_p;
9305 /* Current index into the VTT. */
9306 tree index;
9308 /* Vector of initializers built up. */
9309 vec<constructor_elt, va_gc> *inits;
9311 /* The type being constructed by this secondary VTT. */
9312 tree type_being_constructed;
9315 /* Recursively build the VTT-initializer for BINFO (which is in the
9316 hierarchy dominated by T). INITS points to the end of the initializer
9317 list to date. INDEX is the VTT index where the next element will be
9318 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9319 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9320 for virtual bases of T. When it is not so, we build the constructor
9321 vtables for the BINFO-in-T variant. */
9323 static void
9324 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9325 tree *index)
9327 int i;
9328 tree b;
9329 tree init;
9330 secondary_vptr_vtt_init_data data;
9331 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9333 /* We only need VTTs for subobjects with virtual bases. */
9334 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9335 return;
9337 /* We need to use a construction vtable if this is not the primary
9338 VTT. */
9339 if (!top_level_p)
9341 build_ctor_vtbl_group (binfo, t);
9343 /* Record the offset in the VTT where this sub-VTT can be found. */
9344 BINFO_SUBVTT_INDEX (binfo) = *index;
9347 /* Add the address of the primary vtable for the complete object. */
9348 init = binfo_ctor_vtable (binfo);
9349 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9350 if (top_level_p)
9352 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9353 BINFO_VPTR_INDEX (binfo) = *index;
9355 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9357 /* Recursively add the secondary VTTs for non-virtual bases. */
9358 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9359 if (!BINFO_VIRTUAL_P (b))
9360 build_vtt_inits (b, t, inits, index);
9362 /* Add secondary virtual pointers for all subobjects of BINFO with
9363 either virtual bases or reachable along a virtual path, except
9364 subobjects that are non-virtual primary bases. */
9365 data.top_level_p = top_level_p;
9366 data.index = *index;
9367 data.inits = *inits;
9368 data.type_being_constructed = BINFO_TYPE (binfo);
9370 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9372 *index = data.index;
9374 /* data.inits might have grown as we added secondary virtual pointers.
9375 Make sure our caller knows about the new vector. */
9376 *inits = data.inits;
9378 if (top_level_p)
9379 /* Add the secondary VTTs for virtual bases in inheritance graph
9380 order. */
9381 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9383 if (!BINFO_VIRTUAL_P (b))
9384 continue;
9386 build_vtt_inits (b, t, inits, index);
9388 else
9389 /* Remove the ctor vtables we created. */
9390 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9393 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9394 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9396 static tree
9397 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9399 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9401 /* We don't care about bases that don't have vtables. */
9402 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9403 return dfs_skip_bases;
9405 /* We're only interested in proper subobjects of the type being
9406 constructed. */
9407 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9408 return NULL_TREE;
9410 /* We're only interested in bases with virtual bases or reachable
9411 via a virtual path from the type being constructed. */
9412 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9413 || binfo_via_virtual (binfo, data->type_being_constructed)))
9414 return dfs_skip_bases;
9416 /* We're not interested in non-virtual primary bases. */
9417 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9418 return NULL_TREE;
9420 /* Record the index where this secondary vptr can be found. */
9421 if (data->top_level_p)
9423 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9424 BINFO_VPTR_INDEX (binfo) = data->index;
9426 if (BINFO_VIRTUAL_P (binfo))
9428 /* It's a primary virtual base, and this is not a
9429 construction vtable. Find the base this is primary of in
9430 the inheritance graph, and use that base's vtable
9431 now. */
9432 while (BINFO_PRIMARY_P (binfo))
9433 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9437 /* Add the initializer for the secondary vptr itself. */
9438 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9440 /* Advance the vtt index. */
9441 data->index = size_binop (PLUS_EXPR, data->index,
9442 TYPE_SIZE_UNIT (ptr_type_node));
9444 return NULL_TREE;
9447 /* Called from build_vtt_inits via dfs_walk. After building
9448 constructor vtables and generating the sub-vtt from them, we need
9449 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9450 binfo of the base whose sub vtt was generated. */
9452 static tree
9453 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9455 tree vtable = BINFO_VTABLE (binfo);
9457 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9458 /* If this class has no vtable, none of its bases do. */
9459 return dfs_skip_bases;
9461 if (!vtable)
9462 /* This might be a primary base, so have no vtable in this
9463 hierarchy. */
9464 return NULL_TREE;
9466 /* If we scribbled the construction vtable vptr into BINFO, clear it
9467 out now. */
9468 if (TREE_CODE (vtable) == TREE_LIST
9469 && (TREE_PURPOSE (vtable) == (tree) data))
9470 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9472 return NULL_TREE;
9475 /* Build the construction vtable group for BINFO which is in the
9476 hierarchy dominated by T. */
9478 static void
9479 build_ctor_vtbl_group (tree binfo, tree t)
9481 tree type;
9482 tree vtbl;
9483 tree id;
9484 tree vbase;
9485 vec<constructor_elt, va_gc> *v;
9487 /* See if we've already created this construction vtable group. */
9488 id = mangle_ctor_vtbl_for_type (t, binfo);
9489 if (get_global_binding (id))
9490 return;
9492 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9493 /* Build a version of VTBL (with the wrong type) for use in
9494 constructing the addresses of secondary vtables in the
9495 construction vtable group. */
9496 vtbl = build_vtable (t, id, ptr_type_node);
9498 /* Don't export construction vtables from shared libraries. Even on
9499 targets that don't support hidden visibility, this tells
9500 can_refer_decl_in_current_unit_p not to assume that it's safe to
9501 access from a different compilation unit (bz 54314). */
9502 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9503 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9505 v = NULL;
9506 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9507 binfo, vtbl, t, &v);
9509 /* Add the vtables for each of our virtual bases using the vbase in T
9510 binfo. */
9511 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9512 vbase;
9513 vbase = TREE_CHAIN (vbase))
9515 tree b;
9517 if (!BINFO_VIRTUAL_P (vbase))
9518 continue;
9519 b = copied_binfo (vbase, binfo);
9521 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9524 /* Figure out the type of the construction vtable. */
9525 type = build_array_of_n_type (vtable_entry_type, v->length ());
9526 layout_type (type);
9527 TREE_TYPE (vtbl) = type;
9528 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9529 layout_decl (vtbl, 0);
9531 /* Initialize the construction vtable. */
9532 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9533 initialize_artificial_var (vtbl, v);
9534 dump_vtable (t, binfo, vtbl);
9537 /* Add the vtbl initializers for BINFO (and its bases other than
9538 non-virtual primaries) to the list of INITS. BINFO is in the
9539 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9540 the constructor the vtbl inits should be accumulated for. (If this
9541 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9542 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9543 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9544 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9545 but are not necessarily the same in terms of layout. */
9547 static void
9548 accumulate_vtbl_inits (tree binfo,
9549 tree orig_binfo,
9550 tree rtti_binfo,
9551 tree vtbl,
9552 tree t,
9553 vec<constructor_elt, va_gc> **inits)
9555 int i;
9556 tree base_binfo;
9557 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9559 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9561 /* If it doesn't have a vptr, we don't do anything. */
9562 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9563 return;
9565 /* If we're building a construction vtable, we're not interested in
9566 subobjects that don't require construction vtables. */
9567 if (ctor_vtbl_p
9568 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9569 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9570 return;
9572 /* Build the initializers for the BINFO-in-T vtable. */
9573 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9575 /* Walk the BINFO and its bases. We walk in preorder so that as we
9576 initialize each vtable we can figure out at what offset the
9577 secondary vtable lies from the primary vtable. We can't use
9578 dfs_walk here because we need to iterate through bases of BINFO
9579 and RTTI_BINFO simultaneously. */
9580 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9582 /* Skip virtual bases. */
9583 if (BINFO_VIRTUAL_P (base_binfo))
9584 continue;
9585 accumulate_vtbl_inits (base_binfo,
9586 BINFO_BASE_BINFO (orig_binfo, i),
9587 rtti_binfo, vtbl, t,
9588 inits);
9592 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9593 BINFO vtable to L. */
9595 static void
9596 dfs_accumulate_vtbl_inits (tree binfo,
9597 tree orig_binfo,
9598 tree rtti_binfo,
9599 tree orig_vtbl,
9600 tree t,
9601 vec<constructor_elt, va_gc> **l)
9603 tree vtbl = NULL_TREE;
9604 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9605 int n_inits;
9607 if (ctor_vtbl_p
9608 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9610 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9611 primary virtual base. If it is not the same primary in
9612 the hierarchy of T, we'll need to generate a ctor vtable
9613 for it, to place at its location in T. If it is the same
9614 primary, we still need a VTT entry for the vtable, but it
9615 should point to the ctor vtable for the base it is a
9616 primary for within the sub-hierarchy of RTTI_BINFO.
9618 There are three possible cases:
9620 1) We are in the same place.
9621 2) We are a primary base within a lost primary virtual base of
9622 RTTI_BINFO.
9623 3) We are primary to something not a base of RTTI_BINFO. */
9625 tree b;
9626 tree last = NULL_TREE;
9628 /* First, look through the bases we are primary to for RTTI_BINFO
9629 or a virtual base. */
9630 b = binfo;
9631 while (BINFO_PRIMARY_P (b))
9633 b = BINFO_INHERITANCE_CHAIN (b);
9634 last = b;
9635 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9636 goto found;
9638 /* If we run out of primary links, keep looking down our
9639 inheritance chain; we might be an indirect primary. */
9640 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9641 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9642 break;
9643 found:
9645 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9646 base B and it is a base of RTTI_BINFO, this is case 2. In
9647 either case, we share our vtable with LAST, i.e. the
9648 derived-most base within B of which we are a primary. */
9649 if (b == rtti_binfo
9650 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9651 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9652 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9653 binfo_ctor_vtable after everything's been set up. */
9654 vtbl = last;
9656 /* Otherwise, this is case 3 and we get our own. */
9658 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9659 return;
9661 n_inits = vec_safe_length (*l);
9663 if (!vtbl)
9665 tree index;
9666 int non_fn_entries;
9668 /* Add the initializer for this vtable. */
9669 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9670 &non_fn_entries, l);
9672 /* Figure out the position to which the VPTR should point. */
9673 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9674 index = size_binop (MULT_EXPR,
9675 TYPE_SIZE_UNIT (vtable_entry_type),
9676 size_int (non_fn_entries + n_inits));
9677 vtbl = fold_build_pointer_plus (vtbl, index);
9680 if (ctor_vtbl_p)
9681 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9682 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9683 straighten this out. */
9684 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9685 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9686 /* Throw away any unneeded intializers. */
9687 (*l)->truncate (n_inits);
9688 else
9689 /* For an ordinary vtable, set BINFO_VTABLE. */
9690 BINFO_VTABLE (binfo) = vtbl;
9693 static GTY(()) tree abort_fndecl_addr;
9694 static GTY(()) tree dvirt_fn;
9696 /* Construct the initializer for BINFO's virtual function table. BINFO
9697 is part of the hierarchy dominated by T. If we're building a
9698 construction vtable, the ORIG_BINFO is the binfo we should use to
9699 find the actual function pointers to put in the vtable - but they
9700 can be overridden on the path to most-derived in the graph that
9701 ORIG_BINFO belongs. Otherwise,
9702 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9703 BINFO that should be indicated by the RTTI information in the
9704 vtable; it will be a base class of T, rather than T itself, if we
9705 are building a construction vtable.
9707 The value returned is a TREE_LIST suitable for wrapping in a
9708 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9709 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9710 number of non-function entries in the vtable.
9712 It might seem that this function should never be called with a
9713 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9714 base is always subsumed by a derived class vtable. However, when
9715 we are building construction vtables, we do build vtables for
9716 primary bases; we need these while the primary base is being
9717 constructed. */
9719 static void
9720 build_vtbl_initializer (tree binfo,
9721 tree orig_binfo,
9722 tree t,
9723 tree rtti_binfo,
9724 int* non_fn_entries_p,
9725 vec<constructor_elt, va_gc> **inits)
9727 tree v;
9728 vtbl_init_data vid;
9729 unsigned ix, jx;
9730 tree vbinfo;
9731 vec<tree, va_gc> *vbases;
9732 constructor_elt *e;
9734 /* Initialize VID. */
9735 memset (&vid, 0, sizeof (vid));
9736 vid.binfo = binfo;
9737 vid.derived = t;
9738 vid.rtti_binfo = rtti_binfo;
9739 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9740 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9741 vid.generate_vcall_entries = true;
9742 /* The first vbase or vcall offset is at index -3 in the vtable. */
9743 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9745 /* Add entries to the vtable for RTTI. */
9746 build_rtti_vtbl_entries (binfo, &vid);
9748 /* Create an array for keeping track of the functions we've
9749 processed. When we see multiple functions with the same
9750 signature, we share the vcall offsets. */
9751 vec_alloc (vid.fns, 32);
9752 /* Add the vcall and vbase offset entries. */
9753 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9755 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9756 build_vbase_offset_vtbl_entries. */
9757 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9758 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9759 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9761 /* If the target requires padding between data entries, add that now. */
9762 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9764 int n_entries = vec_safe_length (vid.inits);
9766 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
9767 true);
9769 /* Move data entries into their new positions and add padding
9770 after the new positions. Iterate backwards so we don't
9771 overwrite entries that we would need to process later. */
9772 for (ix = n_entries - 1;
9773 vid.inits->iterate (ix, &e);
9774 ix--)
9776 int j;
9777 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9778 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9780 (*vid.inits)[new_position] = *e;
9782 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9784 constructor_elt *f = &(*vid.inits)[new_position - j];
9785 f->index = NULL_TREE;
9786 f->value = build1 (NOP_EXPR, vtable_entry_type,
9787 null_pointer_node);
9792 if (non_fn_entries_p)
9793 *non_fn_entries_p = vec_safe_length (vid.inits);
9795 /* The initializers for virtual functions were built up in reverse
9796 order. Straighten them out and add them to the running list in one
9797 step. */
9798 jx = vec_safe_length (*inits);
9799 vec_safe_grow (*inits, jx + vid.inits->length (), true);
9801 for (ix = vid.inits->length () - 1;
9802 vid.inits->iterate (ix, &e);
9803 ix--, jx++)
9804 (**inits)[jx] = *e;
9806 /* Go through all the ordinary virtual functions, building up
9807 initializers. */
9808 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9810 tree delta;
9811 tree vcall_index;
9812 tree fn, fn_original;
9813 tree init = NULL_TREE;
9815 fn = BV_FN (v);
9816 fn_original = fn;
9817 if (DECL_THUNK_P (fn))
9819 if (!DECL_NAME (fn))
9820 finish_thunk (fn);
9821 if (THUNK_ALIAS (fn))
9823 fn = THUNK_ALIAS (fn);
9824 BV_FN (v) = fn;
9826 fn_original = THUNK_TARGET (fn);
9829 /* If the only definition of this function signature along our
9830 primary base chain is from a lost primary, this vtable slot will
9831 never be used, so just zero it out. This is important to avoid
9832 requiring extra thunks which cannot be generated with the function.
9834 We first check this in update_vtable_entry_for_fn, so we handle
9835 restored primary bases properly; we also need to do it here so we
9836 zero out unused slots in ctor vtables, rather than filling them
9837 with erroneous values (though harmless, apart from relocation
9838 costs). */
9839 if (BV_LOST_PRIMARY (v))
9840 init = size_zero_node;
9842 if (! init)
9844 /* Pull the offset for `this', and the function to call, out of
9845 the list. */
9846 delta = BV_DELTA (v);
9847 vcall_index = BV_VCALL_INDEX (v);
9849 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9850 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9852 /* You can't call an abstract virtual function; it's abstract.
9853 So, we replace these functions with __pure_virtual. */
9854 if (DECL_PURE_VIRTUAL_P (fn_original))
9856 fn = abort_fndecl;
9857 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9859 if (abort_fndecl_addr == NULL)
9860 abort_fndecl_addr
9861 = fold_convert (vfunc_ptr_type_node,
9862 build_fold_addr_expr (fn));
9863 init = abort_fndecl_addr;
9866 /* Likewise for deleted virtuals. */
9867 else if (DECL_DELETED_FN (fn_original))
9869 if (!dvirt_fn)
9871 tree name = get_identifier ("__cxa_deleted_virtual");
9872 dvirt_fn = get_global_binding (name);
9873 if (!dvirt_fn)
9874 dvirt_fn = push_library_fn
9875 (name,
9876 build_function_type_list (void_type_node, NULL_TREE),
9877 NULL_TREE, ECF_NORETURN | ECF_COLD);
9879 fn = dvirt_fn;
9880 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9881 init = fold_convert (vfunc_ptr_type_node,
9882 build_fold_addr_expr (fn));
9884 else
9886 if (!integer_zerop (delta) || vcall_index)
9888 fn = make_thunk (fn, /*this_adjusting=*/1,
9889 delta, vcall_index);
9890 if (!DECL_NAME (fn))
9891 finish_thunk (fn);
9893 /* Take the address of the function, considering it to be of an
9894 appropriate generic type. */
9895 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9896 init = fold_convert (vfunc_ptr_type_node,
9897 build_fold_addr_expr (fn));
9898 /* Don't refer to a virtual destructor from a constructor
9899 vtable or a vtable for an abstract class, since destroying
9900 an object under construction is undefined behavior and we
9901 don't want it to be considered a candidate for speculative
9902 devirtualization. But do create the thunk for ABI
9903 compliance. */
9904 if (DECL_DESTRUCTOR_P (fn_original)
9905 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9906 || orig_binfo != binfo))
9907 init = size_zero_node;
9911 /* And add it to the chain of initializers. */
9912 if (TARGET_VTABLE_USES_DESCRIPTORS)
9914 int i;
9915 if (init == size_zero_node)
9916 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9917 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
9918 else
9919 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9921 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9922 fn, build_int_cst (NULL_TREE, i));
9923 TREE_CONSTANT (fdesc) = 1;
9925 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
9928 else
9929 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
9933 /* Adds to vid->inits the initializers for the vbase and vcall
9934 offsets in BINFO, which is in the hierarchy dominated by T. */
9936 static void
9937 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9939 tree b;
9941 /* If this is a derived class, we must first create entries
9942 corresponding to the primary base class. */
9943 b = get_primary_binfo (binfo);
9944 if (b)
9945 build_vcall_and_vbase_vtbl_entries (b, vid);
9947 /* Add the vbase entries for this base. */
9948 build_vbase_offset_vtbl_entries (binfo, vid);
9949 /* Add the vcall entries for this base. */
9950 build_vcall_offset_vtbl_entries (binfo, vid);
9953 /* Returns the initializers for the vbase offset entries in the vtable
9954 for BINFO (which is part of the class hierarchy dominated by T), in
9955 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9956 where the next vbase offset will go. */
9958 static void
9959 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9961 tree vbase;
9962 tree t;
9963 tree non_primary_binfo;
9965 /* If there are no virtual baseclasses, then there is nothing to
9966 do. */
9967 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9968 return;
9970 t = vid->derived;
9972 /* We might be a primary base class. Go up the inheritance hierarchy
9973 until we find the most derived class of which we are a primary base:
9974 it is the offset of that which we need to use. */
9975 non_primary_binfo = binfo;
9976 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9978 tree b;
9980 /* If we have reached a virtual base, then it must be a primary
9981 base (possibly multi-level) of vid->binfo, or we wouldn't
9982 have called build_vcall_and_vbase_vtbl_entries for it. But it
9983 might be a lost primary, so just skip down to vid->binfo. */
9984 if (BINFO_VIRTUAL_P (non_primary_binfo))
9986 non_primary_binfo = vid->binfo;
9987 break;
9990 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9991 if (get_primary_binfo (b) != non_primary_binfo)
9992 break;
9993 non_primary_binfo = b;
9996 /* Go through the virtual bases, adding the offsets. */
9997 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9998 vbase;
9999 vbase = TREE_CHAIN (vbase))
10001 tree b;
10002 tree delta;
10004 if (!BINFO_VIRTUAL_P (vbase))
10005 continue;
10007 /* Find the instance of this virtual base in the complete
10008 object. */
10009 b = copied_binfo (vbase, binfo);
10011 /* If we've already got an offset for this virtual base, we
10012 don't need another one. */
10013 if (BINFO_VTABLE_PATH_MARKED (b))
10014 continue;
10015 BINFO_VTABLE_PATH_MARKED (b) = 1;
10017 /* Figure out where we can find this vbase offset. */
10018 delta = size_binop (MULT_EXPR,
10019 vid->index,
10020 fold_convert (ssizetype,
10021 TYPE_SIZE_UNIT (vtable_entry_type)));
10022 if (vid->primary_vtbl_p)
10023 BINFO_VPTR_FIELD (b) = delta;
10025 if (binfo != TYPE_BINFO (t))
10026 /* The vbase offset had better be the same. */
10027 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10029 /* The next vbase will come at a more negative offset. */
10030 vid->index = size_binop (MINUS_EXPR, vid->index,
10031 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10033 /* The initializer is the delta from BINFO to this virtual base.
10034 The vbase offsets go in reverse inheritance-graph order, and
10035 we are walking in inheritance graph order so these end up in
10036 the right order. */
10037 delta = size_diffop_loc (input_location,
10038 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10040 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10041 fold_build1_loc (input_location, NOP_EXPR,
10042 vtable_entry_type, delta));
10046 /* Adds the initializers for the vcall offset entries in the vtable
10047 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10048 to VID->INITS. */
10050 static void
10051 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10053 /* We only need these entries if this base is a virtual base. We
10054 compute the indices -- but do not add to the vtable -- when
10055 building the main vtable for a class. */
10056 if (binfo == TYPE_BINFO (vid->derived)
10057 || (BINFO_VIRTUAL_P (binfo)
10058 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10059 correspond to VID->DERIVED), we are building a primary
10060 construction virtual table. Since this is a primary
10061 virtual table, we do not need the vcall offsets for
10062 BINFO. */
10063 && binfo != vid->rtti_binfo))
10065 /* We need a vcall offset for each of the virtual functions in this
10066 vtable. For example:
10068 class A { virtual void f (); };
10069 class B1 : virtual public A { virtual void f (); };
10070 class B2 : virtual public A { virtual void f (); };
10071 class C: public B1, public B2 { virtual void f (); };
10073 A C object has a primary base of B1, which has a primary base of A. A
10074 C also has a secondary base of B2, which no longer has a primary base
10075 of A. So the B2-in-C construction vtable needs a secondary vtable for
10076 A, which will adjust the A* to a B2* to call f. We have no way of
10077 knowing what (or even whether) this offset will be when we define B2,
10078 so we store this "vcall offset" in the A sub-vtable and look it up in
10079 a "virtual thunk" for B2::f.
10081 We need entries for all the functions in our primary vtable and
10082 in our non-virtual bases' secondary vtables. */
10083 vid->vbase = binfo;
10084 /* If we are just computing the vcall indices -- but do not need
10085 the actual entries -- not that. */
10086 if (!BINFO_VIRTUAL_P (binfo))
10087 vid->generate_vcall_entries = false;
10088 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10089 add_vcall_offset_vtbl_entries_r (binfo, vid);
10093 /* Build vcall offsets, starting with those for BINFO. */
10095 static void
10096 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10098 int i;
10099 tree primary_binfo;
10100 tree base_binfo;
10102 /* Don't walk into virtual bases -- except, of course, for the
10103 virtual base for which we are building vcall offsets. Any
10104 primary virtual base will have already had its offsets generated
10105 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10106 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10107 return;
10109 /* If BINFO has a primary base, process it first. */
10110 primary_binfo = get_primary_binfo (binfo);
10111 if (primary_binfo)
10112 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10114 /* Add BINFO itself to the list. */
10115 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10117 /* Scan the non-primary bases of BINFO. */
10118 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10119 if (base_binfo != primary_binfo)
10120 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10123 /* Called from build_vcall_offset_vtbl_entries_r. */
10125 static void
10126 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10128 /* Make entries for the rest of the virtuals. */
10129 tree orig_fn;
10131 /* The ABI requires that the methods be processed in declaration
10132 order. */
10133 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10134 orig_fn;
10135 orig_fn = DECL_CHAIN (orig_fn))
10136 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10137 add_vcall_offset (orig_fn, binfo, vid);
10140 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10142 static void
10143 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10145 size_t i;
10146 tree vcall_offset;
10147 tree derived_entry;
10149 /* If there is already an entry for a function with the same
10150 signature as FN, then we do not need a second vcall offset.
10151 Check the list of functions already present in the derived
10152 class vtable. */
10153 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10155 if (same_signature_p (derived_entry, orig_fn)
10156 /* We only use one vcall offset for virtual destructors,
10157 even though there are two virtual table entries. */
10158 || (DECL_DESTRUCTOR_P (derived_entry)
10159 && DECL_DESTRUCTOR_P (orig_fn)))
10160 return;
10163 /* If we are building these vcall offsets as part of building
10164 the vtable for the most derived class, remember the vcall
10165 offset. */
10166 if (vid->binfo == TYPE_BINFO (vid->derived))
10168 tree_pair_s elt = {orig_fn, vid->index};
10169 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10172 /* The next vcall offset will be found at a more negative
10173 offset. */
10174 vid->index = size_binop (MINUS_EXPR, vid->index,
10175 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10177 /* Keep track of this function. */
10178 vec_safe_push (vid->fns, orig_fn);
10180 if (vid->generate_vcall_entries)
10182 tree base;
10183 tree fn;
10185 /* Find the overriding function. */
10186 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10187 if (fn == error_mark_node)
10188 vcall_offset = build_zero_cst (vtable_entry_type);
10189 else
10191 base = TREE_VALUE (fn);
10193 /* The vbase we're working on is a primary base of
10194 vid->binfo. But it might be a lost primary, so its
10195 BINFO_OFFSET might be wrong, so we just use the
10196 BINFO_OFFSET from vid->binfo. */
10197 vcall_offset = size_diffop_loc (input_location,
10198 BINFO_OFFSET (base),
10199 BINFO_OFFSET (vid->binfo));
10200 vcall_offset = fold_build1_loc (input_location,
10201 NOP_EXPR, vtable_entry_type,
10202 vcall_offset);
10204 /* Add the initializer to the vtable. */
10205 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10209 /* Return vtbl initializers for the RTTI entries corresponding to the
10210 BINFO's vtable. The RTTI entries should indicate the object given
10211 by VID->rtti_binfo. */
10213 static void
10214 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10216 tree b;
10217 tree t;
10218 tree offset;
10219 tree decl;
10220 tree init;
10222 t = BINFO_TYPE (vid->rtti_binfo);
10224 /* To find the complete object, we will first convert to our most
10225 primary base, and then add the offset in the vtbl to that value. */
10226 b = most_primary_binfo (binfo);
10227 offset = size_diffop_loc (input_location,
10228 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10230 /* The second entry is the address of the typeinfo object. */
10231 if (flag_rtti)
10232 decl = build_address (get_tinfo_decl (t));
10233 else
10234 decl = integer_zero_node;
10236 /* Convert the declaration to a type that can be stored in the
10237 vtable. */
10238 init = build_nop (vfunc_ptr_type_node, decl);
10239 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10241 /* Add the offset-to-top entry. It comes earlier in the vtable than
10242 the typeinfo entry. Convert the offset to look like a
10243 function pointer, so that we can put it in the vtable. */
10244 init = build_nop (vfunc_ptr_type_node, offset);
10245 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10248 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10249 accessibility. */
10251 bool
10252 uniquely_derived_from_p (tree parent, tree type)
10254 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10255 return base && base != error_mark_node;
10258 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10260 bool
10261 publicly_uniquely_derived_p (tree parent, tree type)
10263 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10264 NULL, tf_none);
10265 return base && base != error_mark_node;
10268 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10269 class between them, if any. */
10271 tree
10272 common_enclosing_class (tree ctx1, tree ctx2)
10274 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10275 return NULL_TREE;
10276 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10277 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10278 if (ctx1 == ctx2)
10279 return ctx1;
10280 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10281 TYPE_MARKED_P (t) = true;
10282 tree found = NULL_TREE;
10283 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10284 if (TYPE_MARKED_P (t))
10286 found = t;
10287 break;
10289 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10290 TYPE_MARKED_P (t) = false;
10291 return found;
10294 #include "gt-cp-class.h"