c++: P0847R7 (deducing this) - xobj lambdas. [PR102609]
[official-gcc.git] / gcc / cp / class.cc
blobf3cfa9f0f23acb875654039e038d795ae55685ba
1 /* Functions related to building -*- C++ -*- classes and their related objects.
2 Copyright (C) 1987-2024 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 bool accessible_nvdtor_p (tree);
141 /* Used by find_flexarrays and related functions. */
142 struct flexmems_t;
143 static void diagnose_flexarrays (tree, const flexmems_t *);
144 static void find_flexarrays (tree, flexmems_t *, bool = false,
145 tree = NULL_TREE, tree = NULL_TREE);
146 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
147 static void check_bases (tree, int *, int *);
148 static void check_bases_and_members (tree);
149 static tree create_vtable_ptr (tree, tree *);
150 static void include_empty_classes (record_layout_info);
151 static void layout_class_type (tree, tree *);
152 static void propagate_binfo_offsets (tree, tree);
153 static void layout_virtual_bases (record_layout_info, splay_tree);
154 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
155 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
156 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
157 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset (tree, tree, vtbl_init_data *);
159 static void layout_vtable_decl (tree, int);
160 static tree dfs_find_final_overrider_pre (tree, void *);
161 static tree dfs_find_final_overrider_post (tree, void *);
162 static tree find_final_overrider (tree, tree, tree);
163 static int make_new_vtable (tree, tree);
164 static tree get_primary_binfo (tree);
165 static int maybe_indent_hierarchy (FILE *, int, int);
166 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
167 static void dump_class_hierarchy (tree);
168 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
169 static void dump_array (FILE *, tree);
170 static void dump_vtable (tree, tree, tree);
171 static void dump_vtt (tree, tree);
172 static void dump_thunk (FILE *, int, tree);
173 static tree build_vtable (tree, tree, tree);
174 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
175 static void layout_nonempty_base_or_field (record_layout_info,
176 tree, tree, splay_tree);
177 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
178 vec<constructor_elt, va_gc> **);
179 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
180 vec<constructor_elt, va_gc> **);
181 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
182 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
183 static void clone_constructors_and_destructors (tree);
184 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
185 static void build_ctor_vtbl_group (tree, tree);
186 static void build_vtt (tree);
187 static tree binfo_ctor_vtable (tree);
188 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
189 tree *);
190 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
191 static tree dfs_fixup_binfo_vtbls (tree, void *);
192 static int record_subobject_offset (tree, tree, splay_tree);
193 static int check_subobject_offset (tree, tree, splay_tree);
194 static int walk_subobject_offsets (tree, subobject_offset_fn,
195 tree, splay_tree, tree, int);
196 static int layout_conflict_p (tree, tree, splay_tree, int);
197 static int splay_tree_compare_integer_csts (splay_tree_key k1,
198 splay_tree_key k2);
199 static void maybe_warn_about_inaccessible_bases (tree);
200 static bool type_requires_array_cookie (tree);
201 static bool base_derived_from (tree, tree);
202 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
203 static tree end_of_base (tree);
204 static tree get_vcall_index (tree, tree);
205 static bool type_maybe_constexpr_default_constructor (tree);
206 static bool type_maybe_constexpr_destructor (tree);
207 static bool field_poverlapping_p (tree);
208 static void propagate_class_warmth_attribute (tree);
210 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
212 void
213 set_current_access_from_decl (tree decl)
215 if (TREE_PRIVATE (decl))
216 current_access_specifier = access_private_node;
217 else if (TREE_PROTECTED (decl))
218 current_access_specifier = access_protected_node;
219 else
220 current_access_specifier = access_public_node;
223 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
224 'structor is in charge of 'structing virtual bases, or FALSE_STMT
225 otherwise. */
227 tree
228 build_if_in_charge (tree true_stmt, tree false_stmt)
230 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
231 tree cmp = build2 (NE_EXPR, boolean_type_node,
232 current_in_charge_parm, integer_zero_node);
233 tree type = unlowered_expr_type (true_stmt);
234 if (VOID_TYPE_P (type))
235 type = unlowered_expr_type (false_stmt);
236 tree cond = build3 (COND_EXPR, type,
237 cmp, true_stmt, false_stmt);
238 return cond;
241 /* Convert to or from a base subobject. EXPR is an expression of type
242 `A' or `A*', an expression of type `B' or `B*' is returned. To
243 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
244 the B base instance within A. To convert base A to derived B, CODE
245 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
246 In this latter case, A must not be a morally virtual base of B.
247 NONNULL is true if EXPR is known to be non-NULL (this is only
248 needed when EXPR is of pointer type). CV qualifiers are preserved
249 from EXPR. */
251 tree
252 build_base_path (enum tree_code code,
253 tree expr,
254 tree binfo,
255 int nonnull,
256 tsubst_flags_t complain)
258 tree v_binfo = NULL_TREE;
259 tree d_binfo = NULL_TREE;
260 tree probe;
261 tree offset;
262 tree target_type;
263 tree null_test = NULL;
264 tree ptr_target_type;
265 int fixed_type_p;
266 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
267 bool has_empty = false;
268 bool virtual_access;
269 bool rvalue = false;
271 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
272 return error_mark_node;
274 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
276 d_binfo = probe;
277 if (is_empty_class (BINFO_TYPE (probe)))
278 has_empty = true;
279 if (!v_binfo && BINFO_VIRTUAL_P (probe))
280 v_binfo = probe;
283 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
284 if (want_pointer)
285 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
286 if (dependent_type_p (probe))
287 if (tree open = currently_open_class (probe))
288 probe = open;
290 if (code == PLUS_EXPR
291 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
293 /* This can happen when adjust_result_of_qualified_name_lookup can't
294 find a unique base binfo in a call to a member function. We
295 couldn't give the diagnostic then since we might have been calling
296 a static member function, so we do it now. In other cases, eg.
297 during error recovery (c++/71979), we may not have a base at all. */
298 if (complain & tf_error)
300 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
301 ba_unique, NULL, complain);
302 gcc_assert (base == error_mark_node || !base);
304 return error_mark_node;
307 gcc_assert ((code == MINUS_EXPR
308 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
309 || code == PLUS_EXPR);
311 if (binfo == d_binfo)
312 /* Nothing to do. */
313 return expr;
315 if (code == MINUS_EXPR && v_binfo)
317 if (complain & tf_error)
319 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
321 if (want_pointer)
322 error ("cannot convert from pointer to base class %qT to "
323 "pointer to derived class %qT because the base is "
324 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
325 else
326 error ("cannot convert from base class %qT to derived "
327 "class %qT because the base is virtual",
328 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
330 else
332 if (want_pointer)
333 error ("cannot convert from pointer to base class %qT to "
334 "pointer to derived class %qT via virtual base %qT",
335 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
336 BINFO_TYPE (v_binfo));
337 else
338 error ("cannot convert from base class %qT to derived "
339 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
340 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
343 return error_mark_node;
346 bool uneval = (cp_unevaluated_operand != 0
347 || processing_template_decl
348 || in_template_context);
350 /* For a non-pointer simple base reference, express it as a COMPONENT_REF
351 without taking its address (and so causing lambda capture, 91933). */
352 if (code == PLUS_EXPR && !v_binfo && !want_pointer && !has_empty && !uneval)
353 return build_simple_base_path (expr, binfo);
355 if (!want_pointer)
357 rvalue = !lvalue_p (expr);
358 /* This must happen before the call to save_expr. */
359 expr = cp_build_addr_expr (expr, complain);
361 else
362 expr = mark_rvalue_use (expr);
364 offset = BINFO_OFFSET (binfo);
365 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
366 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
367 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
368 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
369 expression returned matches the input. */
370 target_type = cp_build_qualified_type
371 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
372 ptr_target_type = build_pointer_type (target_type);
374 /* Do we need to look in the vtable for the real offset? */
375 virtual_access = (v_binfo && fixed_type_p <= 0);
377 /* Don't bother with the calculations inside sizeof; they'll ICE if the
378 source type is incomplete and the pointer value doesn't matter. In a
379 template (even in instantiate_non_dependent_expr), we don't have vtables
380 set up properly yet, and the value doesn't matter there either; we're
381 just interested in the result of overload resolution. */
382 if (uneval)
384 expr = build_nop (ptr_target_type, expr);
385 goto indout;
388 if (!COMPLETE_TYPE_P (probe))
390 if (complain & tf_error)
391 error ("cannot convert from %qT to base class %qT because %qT is "
392 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
393 BINFO_TYPE (d_binfo));
394 return error_mark_node;
397 /* If we're in an NSDMI, we don't have the full constructor context yet
398 that we need for converting to a virtual base, so just build a stub
399 CONVERT_EXPR and expand it later in bot_replace. */
400 if (virtual_access && fixed_type_p < 0
401 && current_scope () != current_function_decl)
403 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
404 CONVERT_EXPR_VBASE_PATH (expr) = true;
405 goto indout;
408 /* Do we need to check for a null pointer? */
409 if (want_pointer && !nonnull)
411 /* If we know the conversion will not actually change the value
412 of EXPR, then we can avoid testing the expression for NULL.
413 We have to avoid generating a COMPONENT_REF for a base class
414 field, because other parts of the compiler know that such
415 expressions are always non-NULL. */
416 if (!virtual_access && integer_zerop (offset))
417 return build_nop (ptr_target_type, expr);
418 null_test = error_mark_node;
421 /* Protect against multiple evaluation if necessary. */
422 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
423 expr = save_expr (expr);
425 /* Store EXPR and build the real null test just before returning. */
426 if (null_test)
427 null_test = expr;
429 /* If this is a simple base reference, express it as a COMPONENT_REF. */
430 if (code == PLUS_EXPR && !virtual_access
431 /* We don't build base fields for empty bases, and they aren't very
432 interesting to the optimizers anyway. */
433 && !has_empty)
435 expr = cp_build_fold_indirect_ref (expr);
436 expr = build_simple_base_path (expr, binfo);
437 if (rvalue && lvalue_p (expr))
438 expr = move (expr);
439 if (want_pointer)
440 expr = build_address (expr);
441 target_type = TREE_TYPE (expr);
442 goto out;
445 if (virtual_access)
447 /* Going via virtual base V_BINFO. We need the static offset
448 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
449 V_BINFO. That offset is an entry in D_BINFO's vtable. */
450 tree v_offset;
452 if (fixed_type_p < 0 && in_base_initializer)
454 /* In a base member initializer, we cannot rely on the
455 vtable being set up. We have to indirect via the
456 vtt_parm. */
457 tree t;
459 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
460 t = build_pointer_type (t);
461 v_offset = fold_convert (t, current_vtt_parm);
462 v_offset = cp_build_fold_indirect_ref (v_offset);
464 else
466 tree t = expr;
467 if (sanitize_flags_p (SANITIZE_VPTR)
468 && fixed_type_p == 0)
470 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
471 probe, expr);
472 if (t == NULL_TREE)
473 t = expr;
475 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
476 TREE_TYPE (TREE_TYPE (expr)));
479 if (v_offset == error_mark_node)
480 return error_mark_node;
482 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
483 v_offset = build1 (NOP_EXPR,
484 build_pointer_type (ptrdiff_type_node),
485 v_offset);
486 v_offset = cp_build_fold_indirect_ref (v_offset);
487 TREE_CONSTANT (v_offset) = 1;
489 offset = convert_to_integer (ptrdiff_type_node,
490 size_diffop_loc (input_location, offset,
491 BINFO_OFFSET (v_binfo)));
493 if (!integer_zerop (offset))
494 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
496 if (fixed_type_p < 0)
497 /* Negative fixed_type_p means this is a constructor or destructor;
498 virtual base layout is fixed in in-charge [cd]tors, but not in
499 base [cd]tors. */
500 offset = build_if_in_charge
501 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
502 v_offset);
503 else
504 offset = v_offset;
507 if (want_pointer)
508 target_type = ptr_target_type;
510 if (!integer_zerop (offset))
512 offset = fold_convert (sizetype, offset);
513 if (code == MINUS_EXPR)
514 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
515 expr = fold_build_pointer_plus (expr, offset);
517 else
518 null_test = NULL;
520 expr = build1 (NOP_EXPR, ptr_target_type, expr);
522 indout:
523 if (!want_pointer)
525 expr = cp_build_fold_indirect_ref (expr);
526 if (rvalue)
527 expr = move (expr);
530 out:
531 if (null_test)
532 /* Wrap EXPR in a null test. */
533 expr = build_if_nonnull (null_test, expr, complain);
535 return expr;
538 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
539 Perform a derived-to-base conversion by recursively building up a
540 sequence of COMPONENT_REFs to the appropriate base fields. */
542 static tree
543 build_simple_base_path (tree expr, tree binfo)
545 tree type = BINFO_TYPE (binfo);
546 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
547 tree field;
549 if (d_binfo == NULL_TREE)
551 tree temp;
553 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
555 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
556 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
557 an lvalue in the front end; only _DECLs and _REFs are lvalues
558 in the back end. */
559 temp = unary_complex_lvalue (ADDR_EXPR, expr);
560 if (temp)
561 expr = cp_build_fold_indirect_ref (temp);
563 return expr;
566 /* Recurse. */
567 expr = build_simple_base_path (expr, d_binfo);
569 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
570 field; field = DECL_CHAIN (field))
571 /* Is this the base field created by build_base_field? */
572 if (TREE_CODE (field) == FIELD_DECL
573 && DECL_FIELD_IS_BASE (field)
574 && TREE_TYPE (field) == type
575 /* If we're looking for a field in the most-derived class,
576 also check the field offset; we can have two base fields
577 of the same type if one is an indirect virtual base and one
578 is a direct non-virtual base. */
579 && (BINFO_INHERITANCE_CHAIN (d_binfo)
580 || tree_int_cst_equal (byte_position (field),
581 BINFO_OFFSET (binfo))))
583 /* We don't use build_class_member_access_expr here, as that
584 has unnecessary checks, and more importantly results in
585 recursive calls to dfs_walk_once. */
586 int type_quals = cp_type_quals (TREE_TYPE (expr));
588 expr = build3 (COMPONENT_REF,
589 cp_build_qualified_type (type, type_quals),
590 expr, field, NULL_TREE);
591 /* Mark the expression const or volatile, as appropriate.
592 Even though we've dealt with the type above, we still have
593 to mark the expression itself. */
594 if (type_quals & TYPE_QUAL_CONST)
595 TREE_READONLY (expr) = 1;
596 if (type_quals & TYPE_QUAL_VOLATILE)
597 TREE_THIS_VOLATILE (expr) = 1;
599 return expr;
602 /* Didn't find the base field?!? */
603 gcc_unreachable ();
606 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
607 type is a class type or a pointer to a class type. In the former
608 case, TYPE is also a class type; in the latter it is another
609 pointer type. If CHECK_ACCESS is true, an error message is emitted
610 if TYPE is inaccessible. If OBJECT has pointer type, the value is
611 assumed to be non-NULL. */
613 tree
614 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
615 tsubst_flags_t complain)
617 tree binfo;
618 tree object_type;
620 if (TYPE_PTR_P (TREE_TYPE (object)))
622 object_type = TREE_TYPE (TREE_TYPE (object));
623 type = TREE_TYPE (type);
625 else
626 object_type = TREE_TYPE (object);
628 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
629 NULL, complain);
630 if (!binfo || binfo == error_mark_node)
631 return error_mark_node;
633 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
636 /* EXPR is an expression with unqualified class type. BASE is a base
637 binfo of that class type. Returns EXPR, converted to the BASE
638 type. This function assumes that EXPR is the most derived class;
639 therefore virtual bases can be found at their static offsets. */
641 tree
642 convert_to_base_statically (tree expr, tree base)
644 tree expr_type;
646 expr_type = TREE_TYPE (expr);
647 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
649 /* If this is a non-empty base, use a COMPONENT_REF. */
650 if (!is_empty_class (BINFO_TYPE (base)))
651 return build_simple_base_path (expr, base);
653 /* We use fold_build2 and fold_convert below to simplify the trees
654 provided to the optimizers. It is not safe to call these functions
655 when processing a template because they do not handle C++-specific
656 trees. */
657 gcc_assert (!processing_template_decl);
658 expr = cp_build_addr_expr (expr, tf_warning_or_error);
659 if (!integer_zerop (BINFO_OFFSET (base)))
660 expr = fold_build_pointer_plus_loc (input_location,
661 expr, BINFO_OFFSET (base));
662 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
663 expr = build_fold_indirect_ref_loc (input_location, expr);
666 return expr;
669 /* True IFF EXPR is a reference to an empty base class "subobject", as built in
670 convert_to_base_statically. We look for the result of the fold_convert
671 call, a NOP_EXPR from one pointer type to another, where the target is an
672 empty base of the original type. */
674 bool
675 is_empty_base_ref (tree expr)
677 if (INDIRECT_REF_P (expr))
678 expr = TREE_OPERAND (expr, 0);
679 if (TREE_CODE (expr) != NOP_EXPR)
680 return false;
681 tree type = TREE_TYPE (expr);
682 if (!POINTER_TYPE_P (type))
683 return false;
684 type = TREE_TYPE (type);
685 if (!is_empty_class (type))
686 return false;
687 STRIP_NOPS (expr);
688 tree fromtype = TREE_TYPE (expr);
689 if (!POINTER_TYPE_P (fromtype))
690 return false;
691 fromtype = TREE_TYPE (fromtype);
692 return (CLASS_TYPE_P (fromtype)
693 && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
694 && DERIVED_FROM_P (type, fromtype));
697 tree
698 build_vfield_ref (tree datum, tree type)
700 tree vfield, vcontext;
702 if (datum == error_mark_node
703 /* Can happen in case of duplicate base types (c++/59082). */
704 || !TYPE_VFIELD (type))
705 return error_mark_node;
707 /* First, convert to the requested type. */
708 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
709 datum = convert_to_base (datum, type, /*check_access=*/false,
710 /*nonnull=*/true, tf_warning_or_error);
712 /* Second, the requested type may not be the owner of its own vptr.
713 If not, convert to the base class that owns it. We cannot use
714 convert_to_base here, because VCONTEXT may appear more than once
715 in the inheritance hierarchy of TYPE, and thus direct conversion
716 between the types may be ambiguous. Following the path back up
717 one step at a time via primary bases avoids the problem. */
718 vfield = TYPE_VFIELD (type);
719 vcontext = DECL_CONTEXT (vfield);
720 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
722 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
723 type = TREE_TYPE (datum);
726 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
729 /* Given an object INSTANCE, return an expression which yields the
730 vtable element corresponding to INDEX. There are many special
731 cases for INSTANCE which we take care of here, mainly to avoid
732 creating extra tree nodes when we don't have to. */
734 tree
735 build_vtbl_ref (tree instance, tree idx)
737 tree aref;
738 tree vtbl = NULL_TREE;
740 /* Try to figure out what a reference refers to, and
741 access its virtual function table directly. */
743 int cdtorp = 0;
744 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
746 tree basetype = non_reference (TREE_TYPE (instance));
748 if (fixed_type && !cdtorp)
750 tree binfo = lookup_base (fixed_type, basetype,
751 ba_unique, NULL, tf_none);
752 if (binfo && binfo != error_mark_node)
753 vtbl = unshare_expr (BINFO_VTABLE (binfo));
756 if (!vtbl)
757 vtbl = build_vfield_ref (instance, basetype);
759 aref = build_array_ref (input_location, vtbl, idx);
760 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
762 return aref;
765 /* Given a stable object pointer INSTANCE_PTR, return an expression which
766 yields a function pointer corresponding to vtable element INDEX. */
768 tree
769 build_vfn_ref (tree instance_ptr, tree idx)
771 tree aref;
773 aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
775 /* When using function descriptors, the address of the
776 vtable entry is treated as a function pointer. */
777 if (TARGET_VTABLE_USES_DESCRIPTORS)
778 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
779 cp_build_addr_expr (aref, tf_warning_or_error));
781 /* Remember this as a method reference, for later devirtualization. */
782 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr,
783 fold_convert (TREE_TYPE (instance_ptr), idx));
785 return aref;
788 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
789 for the given TYPE. */
791 static tree
792 get_vtable_name (tree type)
794 return mangle_vtbl_for_type (type);
797 /* DECL is an entity associated with TYPE, like a virtual table or an
798 implicitly generated constructor. Determine whether or not DECL
799 should have external or internal linkage at the object file
800 level. This routine does not deal with COMDAT linkage and other
801 similar complexities; it simply sets TREE_PUBLIC if it possible for
802 entities in other translation units to contain copies of DECL, in
803 the abstract. */
805 void
806 set_linkage_according_to_type (tree /*type*/, tree decl)
808 TREE_PUBLIC (decl) = 1;
809 determine_visibility (decl);
812 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
813 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
814 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
816 static tree
817 build_vtable (tree class_type, tree name, tree vtable_type)
819 tree decl;
821 decl = build_lang_decl (VAR_DECL, name, vtable_type);
822 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
823 now to avoid confusion in mangle_decl. */
824 SET_DECL_ASSEMBLER_NAME (decl, name);
825 DECL_CONTEXT (decl) = class_type;
826 DECL_ARTIFICIAL (decl) = 1;
827 TREE_STATIC (decl) = 1;
828 TREE_READONLY (decl) = 1;
829 DECL_VIRTUAL_P (decl) = 1;
830 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
831 DECL_USER_ALIGN (decl) = true;
832 DECL_VTABLE_OR_VTT_P (decl) = 1;
833 set_linkage_according_to_type (class_type, decl);
834 /* The vtable has not been defined -- yet. */
835 DECL_EXTERNAL (decl) = 1;
836 DECL_NOT_REALLY_EXTERN (decl) = 1;
838 /* Mark the VAR_DECL node representing the vtable itself as a
839 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
840 is rather important that such things be ignored because any
841 effort to actually generate DWARF for them will run into
842 trouble when/if we encounter code like:
844 #pragma interface
845 struct S { virtual void member (); };
847 because the artificial declaration of the vtable itself (as
848 manufactured by the g++ front end) will say that the vtable is
849 a static member of `S' but only *after* the debug output for
850 the definition of `S' has already been output. This causes
851 grief because the DWARF entry for the definition of the vtable
852 will try to refer back to an earlier *declaration* of the
853 vtable as a static member of `S' and there won't be one. We
854 might be able to arrange to have the "vtable static member"
855 attached to the member list for `S' before the debug info for
856 `S' get written (which would solve the problem) but that would
857 require more intrusive changes to the g++ front end. */
858 DECL_IGNORED_P (decl) = 1;
860 return decl;
863 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
864 or even complete. If this does not exist, create it. If COMPLETE is
865 nonzero, then complete the definition of it -- that will render it
866 impossible to actually build the vtable, but is useful to get at those
867 which are known to exist in the runtime. */
869 tree
870 get_vtable_decl (tree type, int complete)
872 tree decl;
874 if (CLASSTYPE_VTABLES (type))
875 return CLASSTYPE_VTABLES (type);
877 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
878 CLASSTYPE_VTABLES (type) = decl;
880 if (complete)
882 DECL_EXTERNAL (decl) = 1;
883 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
886 return decl;
889 /* Build the primary virtual function table for TYPE. If BINFO is
890 non-NULL, build the vtable starting with the initial approximation
891 that it is the same as the one which is the head of the association
892 list. Returns a nonzero value if a new vtable is actually
893 created. */
895 static int
896 build_primary_vtable (tree binfo, tree type)
898 tree decl;
899 tree virtuals;
901 decl = get_vtable_decl (type, /*complete=*/0);
903 if (binfo)
905 if (BINFO_NEW_VTABLE_MARKED (binfo))
906 /* We have already created a vtable for this base, so there's
907 no need to do it again. */
908 return 0;
910 virtuals = copy_list (BINFO_VIRTUALS (binfo));
911 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
912 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
913 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
915 else
917 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
918 virtuals = NULL_TREE;
921 /* Initialize the association list for this type, based
922 on our first approximation. */
923 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
924 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
925 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
926 return 1;
929 /* Give BINFO a new virtual function table which is initialized
930 with a skeleton-copy of its original initialization. The only
931 entry that changes is the `delta' entry, so we can really
932 share a lot of structure.
934 FOR_TYPE is the most derived type which caused this table to
935 be needed.
937 Returns nonzero if we haven't met BINFO before.
939 The order in which vtables are built (by calling this function) for
940 an object must remain the same, otherwise a binary incompatibility
941 can result. */
943 static int
944 build_secondary_vtable (tree binfo)
946 if (BINFO_NEW_VTABLE_MARKED (binfo))
947 /* We already created a vtable for this base. There's no need to
948 do it again. */
949 return 0;
951 /* Remember that we've created a vtable for this BINFO, so that we
952 don't try to do so again. */
953 SET_BINFO_NEW_VTABLE_MARKED (binfo);
955 /* Make fresh virtual list, so we can smash it later. */
956 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
958 /* Secondary vtables are laid out as part of the same structure as
959 the primary vtable. */
960 BINFO_VTABLE (binfo) = NULL_TREE;
961 return 1;
964 /* Create a new vtable for BINFO which is the hierarchy dominated by
965 T. Return nonzero if we actually created a new vtable. */
967 static int
968 make_new_vtable (tree t, tree binfo)
970 if (binfo == TYPE_BINFO (t))
971 /* In this case, it is *type*'s vtable we are modifying. We start
972 with the approximation that its vtable is that of the
973 immediate base class. */
974 return build_primary_vtable (binfo, t);
975 else
976 /* This is our very own copy of `basetype' to play with. Later,
977 we will fill in all the virtual functions that override the
978 virtual functions in these base classes which are not defined
979 by the current type. */
980 return build_secondary_vtable (binfo);
983 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
984 (which is in the hierarchy dominated by T) list FNDECL as its
985 BV_FN. DELTA is the required constant adjustment from the `this'
986 pointer where the vtable entry appears to the `this' required when
987 the function is actually called. */
989 static void
990 modify_vtable_entry (tree t,
991 tree binfo,
992 tree fndecl,
993 tree delta,
994 tree *virtuals)
996 tree v;
998 v = *virtuals;
1000 if (fndecl != BV_FN (v)
1001 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1003 /* We need a new vtable for BINFO. */
1004 if (make_new_vtable (t, binfo))
1006 /* If we really did make a new vtable, we also made a copy
1007 of the BINFO_VIRTUALS list. Now, we have to find the
1008 corresponding entry in that list. */
1009 *virtuals = BINFO_VIRTUALS (binfo);
1010 while (BV_FN (*virtuals) != BV_FN (v))
1011 *virtuals = TREE_CHAIN (*virtuals);
1012 v = *virtuals;
1015 BV_DELTA (v) = delta;
1016 BV_VCALL_INDEX (v) = NULL_TREE;
1017 BV_FN (v) = fndecl;
1022 /* Check if the object parameters of an xobj and iobj member function
1023 correspond. This function assumes that the iobj parameter has been correctly
1024 adjusted when the function is introduced by a using declaration per
1025 [over.match.funcs.general.4]. */
1027 bool
1028 xobj_iobj_parameters_correspond (tree fn1, tree fn2)
1030 gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn1)
1031 || DECL_IOBJ_MEMBER_FUNCTION_P (fn2));
1032 gcc_assert (DECL_XOBJ_MEMBER_FUNCTION_P (fn1)
1033 || DECL_XOBJ_MEMBER_FUNCTION_P (fn2));
1034 gcc_assert (fn1 != fn2);
1036 tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn1) ? fn1 : fn2;
1037 /* A reference, pointer, or something else. */
1038 tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn)));
1040 tree iobj_fn = DECL_IOBJ_MEMBER_FUNCTION_P (fn1) ? fn1 : fn2;
1041 tree iobj_fn_type = TREE_TYPE (iobj_fn);
1042 /* Will work for a pointer or reference param type. So this will continue
1043 to work even if we change how the object parameter of an iobj member
1044 function is represented. */
1045 tree iobj_param_type
1046 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (iobj_fn_type)));
1048 /* If the iobj member function was introduced with a using declaration, the
1049 type of its object parameter is considered to be that of the class it was
1050 introduced into.
1052 [over.match.funcs.general.4]
1053 For non-conversion functions that are implicit object member
1054 functions nominated by a using-declaration in a derived class, the
1055 function is considered to be a member of the derived class for the purpose
1056 of defining the type of the implicit object parameter.
1058 Unfortunately, because of this rule, we can't just compare the xobj member
1059 function's DECL_CONTEXT to its object parameter.
1061 struct S;
1063 struct B {
1064 int f(this S&) { return 5; }
1067 struct S : B {
1068 using B::f;
1069 int f() { return 10; }
1072 The using declaration does not change the object parameter of B::f as it
1073 is an xobj member function. However, its object parameter still
1074 corresponds to S::f as it was declared with an object parameter of type
1075 S const&. The DECL_CONTEXT of B::f is B, so if we compare the type of the
1076 object parameter to that, it will not match. If we naively assume a
1077 different type from the DECL_CONTEXT for an xobj parameter means that the
1078 object parameters do not correspond, then the object parameters in the
1079 above example will be considered non-corresponding.
1081 As a result of this, B::f would incorrectly not be discarded, causing an
1082 ambiguity when f is called on an object of type S.
1084 This also impacts member functions with constraints as in the following
1085 example.
1087 template<typename = void>
1088 struct S;
1090 template<typename = void>
1091 struct B {
1092 int f(this S<>&) requires true { return 5; }
1095 template<typename>
1096 struct S : B<> {
1097 using B<>::f;
1098 int f() { return 10; }
1101 Once again, if we compare the DECL_CONTEXT of B<>::f to it's xobj
1102 parameter, it would not match. If the object parameters do not
1103 correspond, constraints are not taken into account, so in this example we
1104 would (probably) get an ambiguous lookup instead of correctly picking
1105 B<>::f.
1107 Because of this caveat, we must actually compare the type of the iobj
1108 parameter to the type of the xobj parameter, shortcuts will have these
1109 edge cases.
1111 Aside from the more complex reasons above, this logic also implicitly
1112 handles xobj parameters of pointer type, we don't have to explicitly
1113 check for that case. */
1115 /* FIXME:
1117 template<typename>
1118 struct S;
1120 template<typename>
1121 struct B {
1122 int f(this S<void>&) requires true { return 5; }
1125 template<typename>
1126 struct S : B<void> {
1127 using B<void>::f;
1128 int f() { return 10; }
1131 This case is broken, the incomplete type seems to screw with things.
1132 I'm not sure how to fix that so I'm just noting the issue here, I have a
1133 feeling it's trivial to do if you know how. */
1135 if (TYPE_MAIN_VARIANT (iobj_param_type)
1136 != TYPE_MAIN_VARIANT (non_reference (xobj_param)))
1137 return false;
1138 /* We don't get to bail yet even if we have a by-value xobj parameter,
1139 a by-value xobj parameter can correspond to an iobj parameter provided the
1140 iobj member function is not declared with a reference qualifier.
1142 From this point on, we know we are dealing with an xobj parameter that has
1143 an object parameter of the same type as the class it was declared in.
1144 We still don't know if we have a reference or by-value parameter yet
1145 though. */
1147 cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type);
1148 /* We only care about cv qualifiers when determining correspondence. */
1149 static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE
1150 | TYPE_QUAL_CONST;
1151 cp_cv_quals const iobj_cv_quals = type_memfn_quals (iobj_fn_type) & cv_bits;
1152 /* We need to ignore the ref qualifier of the xobj parameter if the iobj
1153 member function lacks a ref qualifier.
1155 [basic.scope.scope.3]
1156 Two non-static member functions have corresponding object parameters if:
1157 -- exactly one is an implicit object member function with no ref-qualifier
1158 and the types of their object parameters ([dcl.fct]), after removing
1159 top-level references, are the same, or
1160 -- their object parameters have the same type.
1162 The cv qualifiers of a by-value parameter are supposed to be discarded, so
1163 we ignore them.
1165 [dcl.fct.5]
1166 After producing the list of parameter types, any top-level cv-qualifiers
1167 modifying a parameter type are deleted when forming the function type.
1169 However, they still need to be taken into account when our xobj parameter
1170 is a reference that is being ignored (according to [basic.scope.scope.3]
1171 quoted above), but when we are actually dealing with a by-value xobj
1172 parameter we can proceed following this table.
1173 | iobj | xobj | equal |
1174 | none | none | X |
1175 | none | c | X |
1176 | none | v | X |
1177 | none | cv | X |
1178 | c | none | O |
1179 | c | c | O |
1180 | c | v | O |
1181 | c | cv | O |
1182 | v | none | O |
1183 | v | c | O |
1184 | v | v | O |
1185 | v | cv | O |
1186 | cv | none | O |
1187 | cv | c | O |
1188 | cv | v | O |
1189 | cv | cv | O |
1191 Additionally, if the iobj member function is ref qualified, we aren't
1192 ignoring the ref qualifier of the iobj parameter, so we can't be dealing
1193 with correspondence in that case either.
1195 So to recap, if we have a by-value xobj parameter, we know for sure that
1196 we aren't dealing with corresponding object parameters if the iobj member
1197 function has any cv-ref qualifiers. The only case where we might still be
1198 dealing with corresponding object parameters is when the iobj member
1199 function lacks any cv-ref qualification. */
1200 if (!TYPE_REF_P (xobj_param))
1202 if (iobj_ref_qual || iobj_cv_quals)
1203 return false;
1205 else
1207 /* We are dealing with an xobj parameter that is a reference now, but due
1208 to [basic.scope.scope.3] we need to ignore its ref qual. */
1209 cp_ref_qualifier const xobj_ref_qual = [&](){
1210 if (!TYPE_REF_P (xobj_param) || !iobj_ref_qual)
1211 return REF_QUAL_NONE;
1212 return TYPE_REF_IS_RVALUE (xobj_param) ? REF_QUAL_RVALUE
1213 : REF_QUAL_LVALUE;
1214 }(); /* IILE. */
1216 /* Even if we are ignoring the reference qualifier, the xobj parameter
1217 was still a reference so we still take the cv qualifiers into
1218 account. */
1219 cp_cv_quals const xobj_cv_quals
1220 = cp_type_quals (TREE_TYPE (xobj_param)) & cv_bits;
1222 /* Finally, if the qualifications don't match exactly, the object
1223 parameters don't correspond. */
1224 if (iobj_ref_qual != xobj_ref_qual
1225 || iobj_cv_quals != xobj_cv_quals)
1226 return false;
1228 /* If we got past everything else, the object parameters of fn1 and fn2
1229 definitely correspond. */
1230 return true;
1233 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1234 METHOD is being injected via a using_decl. Returns true if the
1235 method could be added to the method vec. */
1237 bool
1238 add_method (tree type, tree method, bool via_using)
1240 if (method == error_mark_node)
1241 return false;
1243 gcc_assert (!DECL_EXTERN_C_P (method));
1245 tree *slot = find_member_slot (type, DECL_NAME (method));
1246 tree current_fns = slot ? *slot : NULL_TREE;
1248 /* See below. */
1249 int losem = -1;
1251 /* Check to see if we've already got this method. */
1252 for (ovl_iterator iter (current_fns); iter; ++iter)
1254 tree fn = *iter;
1256 if (TREE_CODE (fn) != TREE_CODE (method))
1257 continue;
1259 /* Two using-declarations can coexist, we'll complain about ambiguity in
1260 overload resolution. */
1261 if (via_using && iter.using_p ()
1262 /* Except handle inherited constructors specially. */
1263 && ! DECL_CONSTRUCTOR_P (fn))
1265 if (fn == method)
1266 /* Don't add the same one twice. */
1267 return false;
1268 continue;
1271 /* [over.load] Member function declarations with the
1272 same name and the same parameter types cannot be
1273 overloaded if any of them is a static member
1274 function declaration.
1276 [over.load] Member function declarations with the same name and
1277 the same parameter-type-list as well as member function template
1278 declarations with the same name, the same parameter-type-list, and
1279 the same template parameter lists cannot be overloaded if any of
1280 them, but not all, have a ref-qualifier.
1282 [namespace.udecl] When a using-declaration brings names
1283 from a base class into a derived class scope, member
1284 functions in the derived class override and/or hide member
1285 functions with the same name and parameter types in a base
1286 class (rather than conflicting). */
1287 tree fn_type = TREE_TYPE (fn);
1288 tree method_type = TREE_TYPE (method);
1290 /* Compare the quals on the 'this' parm. Don't compare
1291 the whole types, as used functions are treated as
1292 coming from the using class in overload resolution. */
1293 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
1294 && DECL_IOBJ_MEMBER_FUNCTION_P (method)
1295 /* Either both or neither need to be ref-qualified for
1296 differing quals to allow overloading. */
1297 && (FUNCTION_REF_QUALIFIED (fn_type)
1298 == FUNCTION_REF_QUALIFIED (method_type))
1299 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1300 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1301 continue;
1303 /* Handle special correspondence rules for xobj vs xobj and xobj vs iobj
1304 member function declarations.
1305 We don't worry about static member functions here. */
1306 if ((!DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1307 && !DECL_XOBJ_MEMBER_FUNCTION_P (method))
1308 || DECL_STATIC_FUNCTION_P (fn) || DECL_STATIC_FUNCTION_P (method))
1309 /* Early escape. */;
1310 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1311 && DECL_XOBJ_MEMBER_FUNCTION_P (method))
1313 auto get_object_param = [](tree fn){
1314 return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1316 /* We skip the object parameter below, check it here instead of
1317 making changes to that code. */
1318 tree fn_param = get_object_param (fn);
1319 tree method_param = get_object_param (method);
1320 if (!same_type_p (fn_param, method_param))
1321 continue;
1323 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1324 || DECL_XOBJ_MEMBER_FUNCTION_P (method))
1326 if (!xobj_iobj_parameters_correspond (fn, method))
1327 continue;
1329 else
1330 gcc_unreachable ();
1332 tree real_fn = fn;
1333 tree real_method = method;
1335 /* Templates and conversion ops must match return types. */
1336 if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1337 && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1338 continue;
1340 /* For templates, the template parameters must be identical. */
1341 if (TREE_CODE (fn) == TEMPLATE_DECL)
1343 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1344 DECL_TEMPLATE_PARMS (method)))
1345 continue;
1347 real_fn = DECL_TEMPLATE_RESULT (fn);
1348 real_method = DECL_TEMPLATE_RESULT (method);
1351 tree parms1 = TYPE_ARG_TYPES (fn_type);
1352 tree parms2 = TYPE_ARG_TYPES (method_type);
1353 if (! DECL_STATIC_FUNCTION_P (real_fn))
1354 parms1 = TREE_CHAIN (parms1);
1355 if (! DECL_STATIC_FUNCTION_P (real_method))
1356 parms2 = TREE_CHAIN (parms2);
1358 /* Bring back parameters omitted from an inherited ctor. The
1359 method and the function can have different omittedness. */
1360 if (ctor_omit_inherited_parms (real_fn))
1361 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1362 if (ctor_omit_inherited_parms (real_method))
1363 parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1364 (DECL_CLONED_FUNCTION (real_method)));
1366 if (!compparms (parms1, parms2))
1367 continue;
1369 if (!equivalently_constrained (fn, method))
1371 if (processing_template_decl)
1372 /* We can't check satisfaction in dependent context, wait until
1373 the class is instantiated. */
1374 continue;
1376 special_function_kind sfk = special_memfn_p (method);
1378 if (sfk == sfk_none
1379 || DECL_INHERITED_CTOR (fn)
1380 || TREE_CODE (fn) == TEMPLATE_DECL)
1381 /* Member function templates and non-special member functions
1382 coexist if they are not equivalently constrained. A member
1383 function is not hidden by an inherited constructor. */
1384 continue;
1386 /* P0848: For special member functions, deleted, unsatisfied, or
1387 less constrained overloads are ineligible. We implement this
1388 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1389 use the notion of eligibility, and the selected destructor can
1390 be deleted, but removing unsatisfied or less constrained
1391 overloads has the same effect as overload resolution. */
1392 bool dtor = (sfk == sfk_destructor);
1393 if (losem == -1)
1394 losem = ((!dtor && DECL_DELETED_FN (method))
1395 || !constraints_satisfied_p (method));
1396 bool losef = ((!dtor && DECL_DELETED_FN (fn))
1397 || !constraints_satisfied_p (fn));
1398 int win;
1399 if (losem || losef)
1400 win = losem - losef;
1401 else
1402 win = more_constrained (fn, method);
1403 if (win > 0)
1404 /* Leave FN in the method vec, discard METHOD. */
1405 return false;
1406 else if (win < 0)
1408 /* Remove FN, add METHOD. */
1409 current_fns = iter.remove_node (current_fns);
1410 continue;
1412 else
1413 /* Let them coexist for now. */
1414 continue;
1417 /* If these are versions of the same function, process and
1418 move on. */
1419 if (TREE_CODE (fn) == FUNCTION_DECL
1420 && maybe_version_functions (method, fn, true))
1421 continue;
1423 if (DECL_INHERITED_CTOR (method))
1425 if (!DECL_INHERITED_CTOR (fn))
1426 /* Defer to the other function. */
1427 return false;
1429 tree basem = DECL_INHERITED_CTOR_BASE (method);
1430 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1431 if (flag_new_inheriting_ctors)
1433 if (basem == basef)
1435 /* Inheriting the same constructor along different
1436 paths, combine them. */
1437 SET_DECL_INHERITED_CTOR
1438 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1439 DECL_INHERITED_CTOR (fn)));
1440 /* And discard the new one. */
1441 return false;
1443 else
1444 /* Inherited ctors can coexist until overload
1445 resolution. */
1446 continue;
1449 error_at (DECL_SOURCE_LOCATION (method),
1450 "%q#D conflicts with version inherited from %qT",
1451 method, basef);
1452 inform (DECL_SOURCE_LOCATION (fn),
1453 "version inherited from %qT declared here",
1454 basef);
1455 return false;
1458 if (via_using)
1459 /* Defer to the local function. */
1460 return false;
1461 else if (iter.using_p ()
1462 || (flag_new_inheriting_ctors
1463 && DECL_INHERITED_CTOR (fn)))
1465 /* Remove the inherited function. */
1466 current_fns = iter.remove_node (current_fns);
1467 continue;
1469 else
1471 error_at (DECL_SOURCE_LOCATION (method),
1472 "%q#D cannot be overloaded with %q#D", method, fn);
1473 inform (DECL_SOURCE_LOCATION (fn),
1474 "previous declaration %q#D", fn);
1475 return false;
1479 current_fns = ovl_insert (method, current_fns, via_using);
1481 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1482 && !push_class_level_binding (DECL_NAME (method), current_fns))
1483 return false;
1485 if (!slot)
1486 slot = add_member_slot (type, DECL_NAME (method));
1488 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1489 grok_special_member_properties (method);
1491 *slot = current_fns;
1493 return true;
1496 /* Subroutines of finish_struct. */
1498 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1499 legit, otherwise return 0. */
1501 static int
1502 alter_access (tree t, tree fdecl, tree access)
1504 tree elem;
1506 retrofit_lang_decl (fdecl);
1508 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1510 elem = purpose_member (t, DECL_ACCESS (fdecl));
1511 if (elem)
1513 if (TREE_VALUE (elem) != access)
1515 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1516 error ("conflicting access specifications for method"
1517 " %q+D, ignored", TREE_TYPE (fdecl));
1518 else
1519 error ("conflicting access specifications for field %qE, ignored",
1520 DECL_NAME (fdecl));
1522 else
1524 /* They're changing the access to the same thing they changed
1525 it to before. That's OK. */
1529 else
1531 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1532 tf_warning_or_error);
1533 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1534 return 1;
1536 return 0;
1539 /* Return the access node for DECL's access in its enclosing class. */
1541 tree
1542 declared_access (tree decl)
1544 return (TREE_PRIVATE (decl) ? access_private_node
1545 : TREE_PROTECTED (decl) ? access_protected_node
1546 : access_public_node);
1549 /* If DECL is a non-dependent using of non-ctor function members, push them
1550 and return true, otherwise return false. Called from
1551 finish_member_declaration. */
1553 bool
1554 maybe_push_used_methods (tree decl)
1556 if (TREE_CODE (decl) != USING_DECL)
1557 return false;
1558 tree used = strip_using_decl (decl);
1559 if (!used || !is_overloaded_fn (used))
1560 return false;
1562 /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1563 works within the class body. */
1564 for (tree f : ovl_range (used))
1566 if (DECL_CONSTRUCTOR_P (f))
1567 /* Inheriting constructors are handled separately. */
1568 return false;
1570 bool added = add_method (current_class_type, f, true);
1572 if (added)
1573 alter_access (current_class_type, f, current_access_specifier);
1575 /* If add_method returns false because f was already declared, look
1576 for a duplicate using-declaration. */
1577 else
1578 for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d))
1579 if (TREE_CODE (d) == USING_DECL
1580 && DECL_NAME (d) == DECL_NAME (decl)
1581 && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl)))
1583 diagnose_name_conflict (decl, d);
1584 break;
1587 return true;
1590 /* Process the USING_DECL, which is a member of T. */
1592 static void
1593 handle_using_decl (tree using_decl, tree t)
1595 tree decl = USING_DECL_DECLS (using_decl);
1597 gcc_assert (!processing_template_decl && decl);
1599 cp_emit_debug_info_for_using (decl, t);
1601 if (is_overloaded_fn (decl))
1602 /* Handled in maybe_push_used_methods. */
1603 return;
1605 tree name = DECL_NAME (using_decl);
1606 tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1607 tf_warning_or_error);
1608 if (old_value)
1610 old_value = OVL_FIRST (old_value);
1612 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1613 /* OK */;
1614 else
1615 old_value = NULL_TREE;
1618 if (! old_value)
1620 else if (is_overloaded_fn (old_value))
1622 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1623 "because of local method %q#D with same name",
1624 using_decl, t, old_value);
1625 inform (DECL_SOURCE_LOCATION (old_value),
1626 "local method %q#D declared here", old_value);
1627 return;
1629 else if (!DECL_ARTIFICIAL (old_value))
1631 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1632 "because of local member %q#D with same name",
1633 using_decl, t, old_value);
1634 inform (DECL_SOURCE_LOCATION (old_value),
1635 "local member %q#D declared here", old_value);
1636 return;
1639 iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1640 tree access = declared_access (using_decl);
1642 /* Make type T see field decl FDECL with access ACCESS. */
1643 if (USING_DECL_UNRELATED_P (using_decl))
1645 /* C++20 using enum can import non-inherited enumerators into class
1646 scope. We implement that by making a copy of the CONST_DECL for which
1647 CONST_DECL_USING_P is true. */
1648 gcc_assert (TREE_CODE (decl) == CONST_DECL);
1650 auto cas = make_temp_override (current_access_specifier, access);
1651 tree copy = copy_decl (decl);
1652 DECL_CONTEXT (copy) = t;
1653 DECL_ARTIFICIAL (copy) = true;
1654 /* We emitted debug info for the USING_DECL above; make sure we don't
1655 also emit anything for this clone. */
1656 DECL_IGNORED_P (copy) = true;
1657 DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1658 finish_member_declaration (copy);
1659 DECL_ABSTRACT_ORIGIN (copy) = decl;
1661 else
1662 alter_access (t, decl, access);
1665 /* Data structure for find_abi_tags_r, below. */
1667 struct abi_tag_data
1669 tree t; // The type that we're checking for missing tags.
1670 tree subob; // The subobject of T that we're getting tags from.
1671 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1674 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1675 in the context of P. TAG can be either an identifier (the DECL_NAME of
1676 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1678 static void
1679 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1681 if (!IDENTIFIER_MARKED (id))
1683 if (p->tags != error_mark_node)
1685 /* We're collecting tags from template arguments or from
1686 the type of a variable or function return type. */
1687 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1689 /* Don't inherit this tag multiple times. */
1690 IDENTIFIER_MARKED (id) = true;
1692 if (TYPE_P (p->t))
1694 /* Tags inherited from type template arguments are only used
1695 to avoid warnings. */
1696 ABI_TAG_IMPLICIT (p->tags) = true;
1697 return;
1699 /* For functions and variables we want to warn, too. */
1702 /* Otherwise we're diagnosing missing tags. */
1703 if (TREE_CODE (p->t) == FUNCTION_DECL)
1705 auto_diagnostic_group d;
1706 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1707 "that %qT (used in its return type) has",
1708 p->t, tag, *tp))
1709 inform (location_of (*tp), "%qT declared here", *tp);
1711 else if (VAR_P (p->t))
1713 auto_diagnostic_group d;
1714 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1715 "that %qT (used in its type) has", p->t, tag, *tp))
1716 inform (location_of (*tp), "%qT declared here", *tp);
1718 else if (TYPE_P (p->subob))
1720 auto_diagnostic_group d;
1721 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1722 "that base %qT has", p->t, tag, p->subob))
1723 inform (location_of (p->subob), "%qT declared here",
1724 p->subob);
1726 else
1728 auto_diagnostic_group d;
1729 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1730 "that %qT (used in the type of %qD) has",
1731 p->t, tag, *tp, p->subob))
1733 inform (location_of (p->subob), "%qD declared here",
1734 p->subob);
1735 inform (location_of (*tp), "%qT declared here", *tp);
1741 /* Find all the ABI tags in the attribute list ATTR and either call
1742 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1744 static void
1745 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1747 if (!attr)
1748 return;
1749 for (; (attr = lookup_attribute ("abi_tag", attr));
1750 attr = TREE_CHAIN (attr))
1751 for (tree list = TREE_VALUE (attr); list;
1752 list = TREE_CHAIN (list))
1754 tree tag = TREE_VALUE (list);
1755 tree id = get_identifier (TREE_STRING_POINTER (tag));
1756 if (tp)
1757 check_tag (tag, id, tp, p);
1758 else
1759 IDENTIFIER_MARKED (id) = val;
1763 /* Find all the ABI tags on T and its enclosing scopes and either call
1764 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1766 static void
1767 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1769 while (t != global_namespace)
1771 tree attr;
1772 if (TYPE_P (t))
1774 attr = TYPE_ATTRIBUTES (t);
1775 t = CP_TYPE_CONTEXT (t);
1777 else
1779 attr = DECL_ATTRIBUTES (t);
1780 t = CP_DECL_CONTEXT (t);
1782 mark_or_check_attr_tags (attr, tp, p, val);
1786 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1787 types with ABI tags, add the corresponding identifiers to the VEC in
1788 *DATA and set IDENTIFIER_MARKED. */
1790 static tree
1791 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1793 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1794 /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1795 *walk_subtrees = 2;
1797 if (!OVERLOAD_TYPE_P (*tp))
1798 return NULL_TREE;
1800 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1801 anyway, but let's make sure of it. */
1802 *walk_subtrees = false;
1804 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1806 mark_or_check_tags (*tp, tp, p, false);
1808 return NULL_TREE;
1811 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1812 IDENTIFIER_MARKED on its ABI tags. */
1814 static tree
1815 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1817 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1818 /* Tell cp_walk_subtrees to look though typedefs. */
1819 *walk_subtrees = 2;
1821 if (!OVERLOAD_TYPE_P (*tp))
1822 return NULL_TREE;
1824 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1825 anyway, but let's make sure of it. */
1826 *walk_subtrees = false;
1828 bool *valp = static_cast<bool*>(data);
1830 mark_or_check_tags (*tp, NULL, NULL, *valp);
1832 return NULL_TREE;
1835 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1836 scopes. */
1838 static void
1839 mark_abi_tags (tree t, bool val)
1841 mark_or_check_tags (t, NULL, NULL, val);
1842 if (DECL_P (t))
1844 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1845 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1847 /* Template arguments are part of the signature. */
1848 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1849 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1851 tree arg = TREE_VEC_ELT (level, j);
1852 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1855 if (TREE_CODE (t) == FUNCTION_DECL)
1856 /* A function's parameter types are part of the signature, so
1857 we don't need to inherit any tags that are also in them. */
1858 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1859 arg = TREE_CHAIN (arg))
1860 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1861 mark_abi_tags_r, &val);
1865 /* Check that T has all the ABI tags that subobject SUBOB has, or
1866 warn if not. If T is a (variable or function) declaration, also
1867 return any missing tags, and add them to T if JUST_CHECKING is false. */
1869 static tree
1870 check_abi_tags (tree t, tree subob, bool just_checking = false)
1872 bool inherit = DECL_P (t);
1874 if (!inherit && !warn_abi_tag)
1875 return NULL_TREE;
1877 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1878 if (!TREE_PUBLIC (decl))
1879 /* No need to worry about things local to this TU. */
1880 return NULL_TREE;
1882 mark_abi_tags (t, true);
1884 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1885 struct abi_tag_data data = { t, subob, error_mark_node };
1886 if (inherit)
1887 data.tags = NULL_TREE;
1889 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1891 if (!(inherit && data.tags))
1892 /* We don't need to do anything with data.tags. */;
1893 else if (just_checking)
1894 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1896 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1897 IDENTIFIER_MARKED (id) = false;
1899 else
1901 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1902 if (attr)
1903 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1904 else
1905 DECL_ATTRIBUTES (t)
1906 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1909 mark_abi_tags (t, false);
1911 return data.tags;
1914 /* Check that DECL has all the ABI tags that are used in parts of its type
1915 that are not reflected in its mangled name. */
1917 void
1918 check_abi_tags (tree decl)
1920 if (VAR_P (decl))
1921 check_abi_tags (decl, TREE_TYPE (decl));
1922 else if (TREE_CODE (decl) == FUNCTION_DECL
1923 && !DECL_CONV_FN_P (decl)
1924 && !mangle_return_type_p (decl))
1925 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1928 /* Return any ABI tags that are used in parts of the type of DECL
1929 that are not reflected in its mangled name. This function is only
1930 used in backward-compatible mangling for ABI <11. */
1932 tree
1933 missing_abi_tags (tree decl)
1935 if (VAR_P (decl))
1936 return check_abi_tags (decl, TREE_TYPE (decl), true);
1937 else if (TREE_CODE (decl) == FUNCTION_DECL
1938 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1939 that we can use this function for setting need_abi_warning
1940 regardless of the current flag_abi_version. */
1941 && !mangle_return_type_p (decl))
1942 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1943 else
1944 return NULL_TREE;
1947 void
1948 inherit_targ_abi_tags (tree t)
1950 if (!CLASS_TYPE_P (t)
1951 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1952 return;
1954 mark_abi_tags (t, true);
1956 tree args = CLASSTYPE_TI_ARGS (t);
1957 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1958 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1960 tree level = TMPL_ARGS_LEVEL (args, i+1);
1961 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1963 tree arg = TREE_VEC_ELT (level, j);
1964 data.subob = arg;
1965 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1969 // If we found some tags on our template arguments, add them to our
1970 // abi_tag attribute.
1971 if (data.tags)
1973 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1974 if (attr)
1975 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1976 else
1977 TYPE_ATTRIBUTES (t)
1978 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1981 mark_abi_tags (t, false);
1984 /* Return true, iff class T has a non-virtual destructor that is
1985 accessible from outside the class heirarchy (i.e. is public, or
1986 there's a suitable friend. */
1988 static bool
1989 accessible_nvdtor_p (tree t)
1991 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1993 /* An implicitly declared destructor is always public. And,
1994 if it were virtual, we would have created it by now. */
1995 if (!dtor)
1996 return true;
1998 if (DECL_VINDEX (dtor))
1999 return false; /* Virtual */
2001 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
2002 return true; /* Public */
2004 if (CLASSTYPE_FRIEND_CLASSES (t)
2005 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2006 return true; /* Has friends */
2008 return false;
2011 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
2012 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
2013 properties of the bases. */
2015 static void
2016 check_bases (tree t,
2017 int* cant_have_const_ctor_p,
2018 int* no_const_asn_ref_p)
2020 int i;
2021 bool seen_non_virtual_nearly_empty_base_p = 0;
2022 int seen_tm_mask = 0;
2023 tree base_binfo;
2024 tree binfo;
2025 tree field = NULL_TREE;
2027 if (!CLASSTYPE_NON_STD_LAYOUT (t))
2028 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2029 if (TREE_CODE (field) == FIELD_DECL)
2030 break;
2032 for (binfo = TYPE_BINFO (t), i = 0;
2033 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2035 tree basetype = TREE_TYPE (base_binfo);
2037 gcc_assert (COMPLETE_TYPE_P (basetype));
2039 if (CLASSTYPE_FINAL (basetype))
2040 error ("cannot derive from %<final%> base %qT in derived type %qT",
2041 basetype, t);
2043 /* If any base class is non-literal, so is the derived class. */
2044 if (!CLASSTYPE_LITERAL_P (basetype))
2045 CLASSTYPE_LITERAL_P (t) = false;
2047 /* If the base class doesn't have copy constructors or
2048 assignment operators that take const references, then the
2049 derived class cannot have such a member automatically
2050 generated. */
2051 if (TYPE_HAS_COPY_CTOR (basetype)
2052 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
2053 *cant_have_const_ctor_p = 1;
2054 if (TYPE_HAS_COPY_ASSIGN (basetype)
2055 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
2056 *no_const_asn_ref_p = 1;
2058 if (BINFO_VIRTUAL_P (base_binfo))
2059 /* A virtual base does not effect nearly emptiness. */
2061 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
2063 if (seen_non_virtual_nearly_empty_base_p)
2064 /* And if there is more than one nearly empty base, then the
2065 derived class is not nearly empty either. */
2066 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2067 else
2068 /* Remember we've seen one. */
2069 seen_non_virtual_nearly_empty_base_p = 1;
2071 else if (!is_empty_class (basetype))
2072 /* If the base class is not empty or nearly empty, then this
2073 class cannot be nearly empty. */
2074 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2076 /* A lot of properties from the bases also apply to the derived
2077 class. */
2078 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2079 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2080 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
2081 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2082 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
2083 || !TYPE_HAS_COPY_ASSIGN (basetype));
2084 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
2085 || !TYPE_HAS_COPY_CTOR (basetype));
2086 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2087 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
2088 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
2089 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
2090 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
2091 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
2092 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
2093 || TYPE_HAS_COMPLEX_DFLT (basetype));
2094 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
2095 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2096 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
2097 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
2098 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
2099 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
2100 if (TYPE_HAS_MUTABLE_P (basetype))
2101 CLASSTYPE_HAS_MUTABLE (t) = 1;
2103 /* A standard-layout class is a class that:
2105 * has no non-standard-layout base classes, */
2106 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
2107 if (!CLASSTYPE_NON_STD_LAYOUT (t))
2109 tree basefield;
2110 /* ...has no base classes of the same type as the first non-static
2111 data member... */
2112 if (field && DECL_CONTEXT (field) == t
2113 && (same_type_ignoring_top_level_qualifiers_p
2114 (TREE_TYPE (field), basetype)))
2115 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2116 /* DR 1813:
2117 ...has at most one base class subobject of any given type... */
2118 else if (CLASSTYPE_REPEATED_BASE_P (t))
2119 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2120 else
2121 /* ...has all non-static data members and bit-fields in the class
2122 and its base classes first declared in the same class. */
2123 for (basefield = TYPE_FIELDS (basetype); basefield;
2124 basefield = DECL_CHAIN (basefield))
2125 if (TREE_CODE (basefield) == FIELD_DECL
2126 && !(DECL_FIELD_IS_BASE (basefield)
2127 && is_empty_field (basefield)))
2129 if (field)
2130 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2131 else
2132 field = basefield;
2133 break;
2137 /* Don't bother collecting tm attributes if transactional memory
2138 support is not enabled. */
2139 if (flag_tm)
2141 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
2142 if (tm_attr)
2143 seen_tm_mask |= tm_attr_to_mask (tm_attr);
2146 check_abi_tags (t, basetype);
2149 /* If one of the base classes had TM attributes, and the current class
2150 doesn't define its own, then the current class inherits one. */
2151 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
2153 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
2154 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
2158 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
2159 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
2160 that have had a nearly-empty virtual primary base stolen by some
2161 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
2162 T. */
2164 static void
2165 determine_primary_bases (tree t)
2167 unsigned i;
2168 tree primary = NULL_TREE;
2169 tree type_binfo = TYPE_BINFO (t);
2170 tree base_binfo;
2172 /* Determine the primary bases of our bases. */
2173 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2174 base_binfo = TREE_CHAIN (base_binfo))
2176 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
2178 /* See if we're the non-virtual primary of our inheritance
2179 chain. */
2180 if (!BINFO_VIRTUAL_P (base_binfo))
2182 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
2183 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
2185 if (parent_primary
2186 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2187 BINFO_TYPE (parent_primary)))
2188 /* We are the primary binfo. */
2189 BINFO_PRIMARY_P (base_binfo) = 1;
2191 /* Determine if we have a virtual primary base, and mark it so.
2193 if (primary && BINFO_VIRTUAL_P (primary))
2195 tree this_primary = copied_binfo (primary, base_binfo);
2197 if (BINFO_PRIMARY_P (this_primary))
2198 /* Someone already claimed this base. */
2199 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
2200 else
2202 tree delta;
2204 BINFO_PRIMARY_P (this_primary) = 1;
2205 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
2207 /* A virtual binfo might have been copied from within
2208 another hierarchy. As we're about to use it as a
2209 primary base, make sure the offsets match. */
2210 delta = size_diffop_loc (input_location,
2211 fold_convert (ssizetype,
2212 BINFO_OFFSET (base_binfo)),
2213 fold_convert (ssizetype,
2214 BINFO_OFFSET (this_primary)));
2216 propagate_binfo_offsets (this_primary, delta);
2221 /* First look for a dynamic direct non-virtual base. */
2222 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
2224 tree basetype = BINFO_TYPE (base_binfo);
2226 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
2228 primary = base_binfo;
2229 goto found;
2233 /* A "nearly-empty" virtual base class can be the primary base
2234 class, if no non-virtual polymorphic base can be found. Look for
2235 a nearly-empty virtual dynamic base that is not already a primary
2236 base of something in the hierarchy. If there is no such base,
2237 just pick the first nearly-empty virtual base. */
2239 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2240 base_binfo = TREE_CHAIN (base_binfo))
2241 if (BINFO_VIRTUAL_P (base_binfo)
2242 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2244 if (!BINFO_PRIMARY_P (base_binfo))
2246 /* Found one that is not primary. */
2247 primary = base_binfo;
2248 goto found;
2250 else if (!primary)
2251 /* Remember the first candidate. */
2252 primary = base_binfo;
2255 found:
2256 /* If we've got a primary base, use it. */
2257 if (primary)
2259 tree basetype = BINFO_TYPE (primary);
2261 CLASSTYPE_PRIMARY_BINFO (t) = primary;
2262 if (BINFO_PRIMARY_P (primary))
2263 /* We are stealing a primary base. */
2264 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2265 BINFO_PRIMARY_P (primary) = 1;
2266 if (BINFO_VIRTUAL_P (primary))
2268 tree delta;
2270 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2271 /* A virtual binfo might have been copied from within
2272 another hierarchy. As we're about to use it as a primary
2273 base, make sure the offsets match. */
2274 delta = size_diffop_loc (input_location, ssize_int (0),
2275 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2277 propagate_binfo_offsets (primary, delta);
2280 primary = TYPE_BINFO (basetype);
2282 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2283 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2284 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2288 /* Update the variant types of T. */
2290 void
2291 fixup_type_variants (tree type)
2293 if (!type)
2294 return;
2296 for (tree variant = TYPE_NEXT_VARIANT (type);
2297 variant;
2298 variant = TYPE_NEXT_VARIANT (variant))
2300 /* These fields are in the _TYPE part of the node, not in
2301 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2302 TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2303 TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2304 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2305 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2307 TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2308 CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2310 TYPE_BINFO (variant) = TYPE_BINFO (type);
2312 /* Copy whatever these are holding today. */
2313 TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2314 TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2316 TYPE_SIZE (variant) = TYPE_SIZE (type);
2317 TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2319 if (!TYPE_USER_ALIGN (variant)
2320 || TYPE_NAME (variant) == TYPE_NAME (type)
2321 || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2323 TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2324 TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2327 TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2328 TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2329 TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2333 /* KLASS is a class that we're applying may_alias to after the body is
2334 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2335 canonical type(s) will be implicitly updated. */
2337 static void
2338 fixup_may_alias (tree klass)
2340 tree t, v;
2342 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2343 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2344 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2345 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2346 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2347 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2350 /* Early variant fixups: we apply attributes at the beginning of the class
2351 definition, and we need to fix up any variants that have already been
2352 made via elaborated-type-specifier so that check_qualified_type works. */
2354 void
2355 fixup_attribute_variants (tree t)
2357 tree variants;
2359 if (!t)
2360 return;
2362 tree attrs = TYPE_ATTRIBUTES (t);
2363 unsigned align = TYPE_ALIGN (t);
2364 bool user_align = TYPE_USER_ALIGN (t);
2365 bool may_alias = lookup_attribute ("may_alias", attrs);
2366 bool packed = TYPE_PACKED (t);
2368 if (may_alias)
2369 fixup_may_alias (t);
2371 for (variants = TYPE_NEXT_VARIANT (t);
2372 variants;
2373 variants = TYPE_NEXT_VARIANT (variants))
2375 /* These are the two fields that check_qualified_type looks at and
2376 are affected by attributes. */
2377 TYPE_ATTRIBUTES (variants) = attrs;
2378 unsigned valign = align;
2379 if (TYPE_USER_ALIGN (variants))
2380 valign = MAX (valign, TYPE_ALIGN (variants));
2381 else
2382 TYPE_USER_ALIGN (variants) = user_align;
2383 SET_TYPE_ALIGN (variants, valign);
2384 TYPE_PACKED (variants) = packed;
2385 if (may_alias)
2386 fixup_may_alias (variants);
2390 /* Set memoizing fields and bits of T (and its variants) for later
2391 use. */
2393 static void
2394 finish_struct_bits (tree t)
2396 /* Fix up variants (if any). */
2397 fixup_type_variants (t);
2399 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2400 /* For a class w/o baseclasses, 'finish_struct' has set
2401 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2402 Similarly for a class whose base classes do not have vtables.
2403 When neither of these is true, we might have removed abstract
2404 virtuals (by providing a definition), added some (by declaring
2405 new ones), or redeclared ones from a base class. We need to
2406 recalculate what's really an abstract virtual at this point (by
2407 looking in the vtables). */
2408 get_pure_virtuals (t);
2410 /* If this type has a copy constructor or a destructor, force its
2411 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2412 nonzero. This will cause it to be passed by invisible reference
2413 and prevent it from being returned in a register. */
2414 if (type_has_nontrivial_copy_init (t)
2415 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2417 tree variants;
2418 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2419 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2421 SET_TYPE_MODE (variants, BLKmode);
2422 TREE_ADDRESSABLE (variants) = 1;
2427 /* Issue warnings about T having private constructors, but no friends,
2428 and so forth.
2430 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2431 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2432 non-private static member functions. */
2434 static void
2435 maybe_warn_about_overly_private_class (tree t)
2437 int has_member_fn = 0;
2438 int has_nonprivate_method = 0;
2439 bool nonprivate_ctor = false;
2441 if (!warn_ctor_dtor_privacy
2442 /* If the class has friends, those entities might create and
2443 access instances, so we should not warn. */
2444 || (CLASSTYPE_FRIEND_CLASSES (t)
2445 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2446 /* We will have warned when the template was declared; there's
2447 no need to warn on every instantiation. */
2448 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2449 /* There's no reason to even consider warning about this
2450 class. */
2451 return;
2453 /* We only issue one warning, if more than one applies, because
2454 otherwise, on code like:
2456 class A {
2457 // Oops - forgot `public:'
2458 A();
2459 A(const A&);
2460 ~A();
2463 we warn several times about essentially the same problem. */
2465 /* Check to see if all (non-constructor, non-destructor) member
2466 functions are private. (Since there are no friends or
2467 non-private statics, we can't ever call any of the private member
2468 functions.) */
2469 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2470 if (TREE_CODE (fn) == USING_DECL
2471 && DECL_NAME (fn) == ctor_identifier
2472 && !TREE_PRIVATE (fn))
2473 nonprivate_ctor = true;
2474 else if (!DECL_DECLARES_FUNCTION_P (fn))
2475 /* Not a function. */;
2476 else if (DECL_ARTIFICIAL (fn))
2477 /* We're not interested in compiler-generated methods; they don't
2478 provide any way to call private members. */;
2479 else if (!TREE_PRIVATE (fn))
2481 if (DECL_STATIC_FUNCTION_P (fn))
2482 /* A non-private static member function is just like a
2483 friend; it can create and invoke private member
2484 functions, and be accessed without a class
2485 instance. */
2486 return;
2488 has_nonprivate_method = 1;
2489 /* Keep searching for a static member function. */
2491 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2492 has_member_fn = 1;
2494 if (!has_nonprivate_method && has_member_fn)
2496 /* There are no non-private methods, and there's at least one
2497 private member function that isn't a constructor or
2498 destructor. (If all the private members are
2499 constructors/destructors we want to use the code below that
2500 issues error messages specifically referring to
2501 constructors/destructors.) */
2502 unsigned i;
2503 tree binfo = TYPE_BINFO (t);
2505 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2506 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2508 has_nonprivate_method = 1;
2509 break;
2511 if (!has_nonprivate_method)
2513 warning (OPT_Wctor_dtor_privacy,
2514 "all member functions in class %qT are private", t);
2515 return;
2519 /* Even if some of the member functions are non-private, the class
2520 won't be useful for much if all the constructors or destructors
2521 are private: such an object can never be created or destroyed. */
2522 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2523 if (TREE_PRIVATE (dtor))
2525 warning (OPT_Wctor_dtor_privacy,
2526 "%q#T only defines a private destructor and has no friends",
2528 return;
2531 /* Warn about classes that have private constructors and no friends. */
2532 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2533 /* Implicitly generated constructors are always public. */
2534 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2536 tree copy_or_move = NULL_TREE;
2538 /* If a non-template class does not define a copy
2539 constructor, one is defined for it, enabling it to avoid
2540 this warning. For a template class, this does not
2541 happen, and so we would normally get a warning on:
2543 template <class T> class C { private: C(); };
2545 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2546 complete non-template or fully instantiated classes have this
2547 flag set. */
2548 if (!TYPE_HAS_COPY_CTOR (t))
2549 nonprivate_ctor = true;
2550 else
2551 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2552 if (TREE_PRIVATE (fn))
2553 continue;
2554 else if (copy_fn_p (fn) || move_fn_p (fn))
2555 /* Ideally, we wouldn't count any constructor that takes
2556 an argument of the class type as a parameter, because
2557 such things cannot be used to construct an instance of
2558 the class unless you already have one. */
2559 copy_or_move = fn;
2560 else
2562 nonprivate_ctor = true;
2563 break;
2566 if (!nonprivate_ctor)
2568 bool w = warning (OPT_Wctor_dtor_privacy,
2569 "%q#T only defines private constructors and has "
2570 "no friends", t);
2571 if (w && copy_or_move)
2572 inform (DECL_SOURCE_LOCATION (copy_or_move),
2573 "%q#D is public, but requires an existing %q#T object",
2574 copy_or_move, t);
2575 return;
2580 /* Make BINFO's vtable have N entries, including RTTI entries,
2581 vbase and vcall offsets, etc. Set its type and call the back end
2582 to lay it out. */
2584 static void
2585 layout_vtable_decl (tree binfo, int n)
2587 tree atype;
2588 tree vtable;
2590 atype = build_array_of_n_type (vtable_entry_type, n);
2591 layout_type (atype);
2593 /* We may have to grow the vtable. */
2594 vtable = get_vtbl_decl_for_binfo (binfo);
2595 if (!same_type_p (TREE_TYPE (vtable), atype))
2597 TREE_TYPE (vtable) = atype;
2598 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2599 layout_decl (vtable, 0);
2603 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2604 have the same signature. */
2607 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2609 /* One destructor overrides another if they are the same kind of
2610 destructor. */
2611 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2612 && special_function_p (base_fndecl) == special_function_p (fndecl))
2613 return 1;
2614 /* But a non-destructor never overrides a destructor, nor vice
2615 versa, nor do different kinds of destructors override
2616 one-another. For example, a complete object destructor does not
2617 override a deleting destructor. */
2618 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2619 return 0;
2621 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2622 || (DECL_CONV_FN_P (fndecl)
2623 && DECL_CONV_FN_P (base_fndecl)
2624 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2625 DECL_CONV_FN_TYPE (base_fndecl))))
2627 tree fntype = TREE_TYPE (fndecl);
2628 tree base_fntype = TREE_TYPE (base_fndecl);
2629 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2630 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2631 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2632 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2633 return 1;
2635 return 0;
2638 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2639 subobject. */
2641 static bool
2642 base_derived_from (tree derived, tree base)
2644 tree probe;
2646 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2648 if (probe == derived)
2649 return true;
2650 else if (BINFO_VIRTUAL_P (probe))
2651 /* If we meet a virtual base, we can't follow the inheritance
2652 any more. See if the complete type of DERIVED contains
2653 such a virtual base. */
2654 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2655 != NULL_TREE);
2657 return false;
2660 struct find_final_overrider_data {
2661 /* The function for which we are trying to find a final overrider. */
2662 tree fn;
2663 /* The base class in which the function was declared. */
2664 tree declaring_base;
2665 /* The candidate overriders. */
2666 tree candidates;
2667 /* Path to most derived. */
2668 auto_vec<tree> path;
2671 /* Add the overrider along the current path to FFOD->CANDIDATES.
2672 Returns true if an overrider was found; false otherwise. */
2674 static bool
2675 dfs_find_final_overrider_1 (tree binfo,
2676 find_final_overrider_data *ffod,
2677 unsigned depth)
2679 tree method;
2681 /* If BINFO is not the most derived type, try a more derived class.
2682 A definition there will overrider a definition here. */
2683 if (depth)
2685 depth--;
2686 if (dfs_find_final_overrider_1
2687 (ffod->path[depth], ffod, depth))
2688 return true;
2691 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2692 if (method)
2694 tree *candidate = &ffod->candidates;
2696 /* Remove any candidates overridden by this new function. */
2697 while (*candidate)
2699 /* If *CANDIDATE overrides METHOD, then METHOD
2700 cannot override anything else on the list. */
2701 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2702 return true;
2703 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2704 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2705 *candidate = TREE_CHAIN (*candidate);
2706 else
2707 candidate = &TREE_CHAIN (*candidate);
2710 /* Add the new function. */
2711 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2712 return true;
2715 return false;
2718 /* Called from find_final_overrider via dfs_walk. */
2720 static tree
2721 dfs_find_final_overrider_pre (tree binfo, void *data)
2723 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2725 if (binfo == ffod->declaring_base)
2726 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2727 ffod->path.safe_push (binfo);
2729 return NULL_TREE;
2732 static tree
2733 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2735 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2736 ffod->path.pop ();
2738 return NULL_TREE;
2741 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2742 FN and whose TREE_VALUE is the binfo for the base where the
2743 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2744 DERIVED) is the base object in which FN is declared. */
2746 static tree
2747 find_final_overrider (tree derived, tree binfo, tree fn)
2749 find_final_overrider_data ffod;
2751 /* Getting this right is a little tricky. This is valid:
2753 struct S { virtual void f (); };
2754 struct T { virtual void f (); };
2755 struct U : public S, public T { };
2757 even though calling `f' in `U' is ambiguous. But,
2759 struct R { virtual void f(); };
2760 struct S : virtual public R { virtual void f (); };
2761 struct T : virtual public R { virtual void f (); };
2762 struct U : public S, public T { };
2764 is not -- there's no way to decide whether to put `S::f' or
2765 `T::f' in the vtable for `R'.
2767 The solution is to look at all paths to BINFO. If we find
2768 different overriders along any two, then there is a problem. */
2769 if (DECL_THUNK_P (fn))
2770 fn = THUNK_TARGET (fn);
2772 /* Determine the depth of the hierarchy. */
2773 ffod.fn = fn;
2774 ffod.declaring_base = binfo;
2775 ffod.candidates = NULL_TREE;
2776 ffod.path.create (30);
2778 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2779 dfs_find_final_overrider_post, &ffod);
2781 /* If there was no winner, issue an error message. */
2782 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2783 return error_mark_node;
2785 return ffod.candidates;
2788 /* Return the index of the vcall offset for FN when TYPE is used as a
2789 virtual base. */
2791 static tree
2792 get_vcall_index (tree fn, tree type)
2794 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2795 tree_pair_p p;
2796 unsigned ix;
2798 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2799 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2800 || same_signature_p (fn, p->purpose))
2801 return p->value;
2803 /* There should always be an appropriate index. */
2804 gcc_unreachable ();
2807 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2808 overrider at that index in the vtable. This should only be used when we
2809 know that BINFO is correct for the dynamic type of the object. */
2811 tree
2812 lookup_vfn_in_binfo (tree idx, tree binfo)
2814 int ix = tree_to_shwi (idx);
2815 if (TARGET_VTABLE_USES_DESCRIPTORS)
2816 ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2817 while (BINFO_PRIMARY_P (binfo))
2818 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2819 class that actually owns the vtable. */
2820 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2821 tree virtuals = BINFO_VIRTUALS (binfo);
2822 return TREE_VALUE (chain_index (ix, virtuals));
2825 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2826 dominated by T. FN is the old function; VIRTUALS points to the
2827 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2828 of that entry in the list. */
2830 static void
2831 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2832 unsigned ix)
2834 tree b;
2835 tree overrider;
2836 tree delta;
2837 tree virtual_base;
2838 tree first_defn;
2839 tree overrider_fn, overrider_target;
2840 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2841 tree over_return, base_return;
2842 bool lost = false;
2844 /* Find the nearest primary base (possibly binfo itself) which defines
2845 this function; this is the class the caller will convert to when
2846 calling FN through BINFO. */
2847 for (b = binfo; ; b = get_primary_binfo (b))
2849 gcc_assert (b);
2850 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2851 break;
2853 /* The nearest definition is from a lost primary. */
2854 if (BINFO_LOST_PRIMARY_P (b))
2855 lost = true;
2857 first_defn = b;
2859 /* Find the final overrider. */
2860 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2861 if (overrider == error_mark_node)
2863 error ("no unique final overrider for %qD in %qT", target_fn, t);
2864 return;
2866 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2868 /* Check for adjusting covariant return types. */
2869 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2870 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2872 if (INDIRECT_TYPE_P (over_return)
2873 && TREE_CODE (over_return) == TREE_CODE (base_return)
2874 && CLASS_TYPE_P (TREE_TYPE (over_return))
2875 && CLASS_TYPE_P (TREE_TYPE (base_return))
2876 /* If the overrider is invalid, don't even try. */
2877 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2879 /* If FN is a covariant thunk, we must figure out the adjustment
2880 to the final base FN was converting to. As OVERRIDER_TARGET might
2881 also be converting to the return type of FN, we have to
2882 combine the two conversions here. */
2883 tree fixed_offset, virtual_offset;
2885 over_return = TREE_TYPE (over_return);
2886 base_return = TREE_TYPE (base_return);
2888 if (DECL_THUNK_P (fn))
2890 gcc_assert (DECL_RESULT_THUNK_P (fn));
2891 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2892 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2894 else
2895 fixed_offset = virtual_offset = NULL_TREE;
2897 if (virtual_offset)
2898 /* Find the equivalent binfo within the return type of the
2899 overriding function. We will want the vbase offset from
2900 there. */
2901 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2902 over_return);
2903 else if (!same_type_ignoring_top_level_qualifiers_p
2904 (over_return, base_return))
2906 /* There was no existing virtual thunk (which takes
2907 precedence). So find the binfo of the base function's
2908 return type within the overriding function's return type.
2909 Fortunately we know the covariancy is valid (it
2910 has already been checked), so we can just iterate along
2911 the binfos, which have been chained in inheritance graph
2912 order. Of course it is lame that we have to repeat the
2913 search here anyway -- we should really be caching pieces
2914 of the vtable and avoiding this repeated work. */
2915 tree thunk_binfo = NULL_TREE;
2916 tree base_binfo = TYPE_BINFO (base_return);
2918 /* Find the base binfo within the overriding function's
2919 return type. We will always find a thunk_binfo, except
2920 when the covariancy is invalid (which we will have
2921 already diagnosed). */
2922 if (base_binfo)
2923 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2924 thunk_binfo = TREE_CHAIN (thunk_binfo))
2925 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2926 BINFO_TYPE (base_binfo)))
2927 break;
2928 gcc_assert (thunk_binfo || errorcount);
2930 /* See if virtual inheritance is involved. */
2931 for (virtual_offset = thunk_binfo;
2932 virtual_offset;
2933 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2934 if (BINFO_VIRTUAL_P (virtual_offset))
2935 break;
2937 if (virtual_offset
2938 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2940 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2942 if (virtual_offset)
2944 /* We convert via virtual base. Adjust the fixed
2945 offset to be from there. */
2946 offset =
2947 size_diffop (offset,
2948 fold_convert (ssizetype,
2949 BINFO_OFFSET (virtual_offset)));
2951 if (fixed_offset)
2952 /* There was an existing fixed offset, this must be
2953 from the base just converted to, and the base the
2954 FN was thunking to. */
2955 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2956 else
2957 fixed_offset = offset;
2961 if (fixed_offset || virtual_offset)
2962 /* Replace the overriding function with a covariant thunk. We
2963 will emit the overriding function in its own slot as
2964 well. */
2965 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2966 fixed_offset, virtual_offset);
2968 else
2969 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2970 !DECL_THUNK_P (fn));
2972 /* If we need a covariant thunk, then we may need to adjust first_defn.
2973 The ABI specifies that the thunks emitted with a function are
2974 determined by which bases the function overrides, so we need to be
2975 sure that we're using a thunk for some overridden base; even if we
2976 know that the necessary this adjustment is zero, there may not be an
2977 appropriate zero-this-adjustment thunk for us to use since thunks for
2978 overriding virtual bases always use the vcall offset.
2980 Furthermore, just choosing any base that overrides this function isn't
2981 quite right, as this slot won't be used for calls through a type that
2982 puts a covariant thunk here. Calling the function through such a type
2983 will use a different slot, and that slot is the one that determines
2984 the thunk emitted for that base.
2986 So, keep looking until we find the base that we're really overriding
2987 in this slot: the nearest primary base that doesn't use a covariant
2988 thunk in this slot. */
2989 if (overrider_target != overrider_fn)
2991 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2992 /* We already know that the overrider needs a covariant thunk. */
2993 b = get_primary_binfo (b);
2994 for (; ; b = get_primary_binfo (b))
2996 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2997 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2998 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2999 break;
3000 if (BINFO_LOST_PRIMARY_P (b))
3001 lost = true;
3003 first_defn = b;
3006 /* Assume that we will produce a thunk that convert all the way to
3007 the final overrider, and not to an intermediate virtual base. */
3008 virtual_base = NULL_TREE;
3010 /* See if we can convert to an intermediate virtual base first, and then
3011 use the vcall offset located there to finish the conversion. */
3012 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
3014 /* If we find the final overrider, then we can stop
3015 walking. */
3016 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
3017 BINFO_TYPE (TREE_VALUE (overrider))))
3018 break;
3020 /* If we find a virtual base, and we haven't yet found the
3021 overrider, then there is a virtual base between the
3022 declaring base (first_defn) and the final overrider. */
3023 if (BINFO_VIRTUAL_P (b))
3025 virtual_base = b;
3026 break;
3030 /* Compute the constant adjustment to the `this' pointer. The
3031 `this' pointer, when this function is called, will point at BINFO
3032 (or one of its primary bases, which are at the same offset). */
3033 if (virtual_base)
3034 /* The `this' pointer needs to be adjusted from the declaration to
3035 the nearest virtual base. */
3036 delta = size_diffop_loc (input_location,
3037 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
3038 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
3039 else if (lost)
3040 /* If the nearest definition is in a lost primary, we don't need an
3041 entry in our vtable. Except possibly in a constructor vtable,
3042 if we happen to get our primary back. In that case, the offset
3043 will be zero, as it will be a primary base. */
3044 delta = size_zero_node;
3045 else
3046 /* The `this' pointer needs to be adjusted from pointing to
3047 BINFO to pointing at the base where the final overrider
3048 appears. */
3049 delta = size_diffop_loc (input_location,
3050 fold_convert (ssizetype,
3051 BINFO_OFFSET (TREE_VALUE (overrider))),
3052 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
3054 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
3056 if (virtual_base)
3057 BV_VCALL_INDEX (*virtuals)
3058 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
3059 else
3060 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
3062 BV_LOST_PRIMARY (*virtuals) = lost;
3065 /* Called from modify_all_vtables via dfs_walk. */
3067 static tree
3068 dfs_modify_vtables (tree binfo, void* data)
3070 tree t = (tree) data;
3071 tree virtuals;
3072 tree old_virtuals;
3073 unsigned ix;
3075 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3076 /* A base without a vtable needs no modification, and its bases
3077 are uninteresting. */
3078 return dfs_skip_bases;
3080 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
3081 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3082 /* Don't do the primary vtable, if it's new. */
3083 return NULL_TREE;
3085 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
3086 /* There's no need to modify the vtable for a non-virtual primary
3087 base; we're not going to use that vtable anyhow. We do still
3088 need to do this for virtual primary bases, as they could become
3089 non-primary in a construction vtable. */
3090 return NULL_TREE;
3092 make_new_vtable (t, binfo);
3094 /* Now, go through each of the virtual functions in the virtual
3095 function table for BINFO. Find the final overrider, and update
3096 the BINFO_VIRTUALS list appropriately. */
3097 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
3098 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3099 virtuals;
3100 ix++, virtuals = TREE_CHAIN (virtuals),
3101 old_virtuals = TREE_CHAIN (old_virtuals))
3102 update_vtable_entry_for_fn (t,
3103 binfo,
3104 BV_FN (old_virtuals),
3105 &virtuals, ix);
3107 return NULL_TREE;
3110 /* Update all of the primary and secondary vtables for T. Create new
3111 vtables as required, and initialize their RTTI information. Each
3112 of the functions in VIRTUALS is declared in T and may override a
3113 virtual function from a base class; find and modify the appropriate
3114 entries to point to the overriding functions. Returns a list, in
3115 declaration order, of the virtual functions that are declared in T,
3116 but do not appear in the primary base class vtable, and which
3117 should therefore be appended to the end of the vtable for T. */
3119 static tree
3120 modify_all_vtables (tree t, tree virtuals)
3122 tree binfo = TYPE_BINFO (t);
3123 tree *fnsp;
3125 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
3126 if (TYPE_CONTAINS_VPTR_P (t))
3127 get_vtable_decl (t, false);
3129 /* Update all of the vtables. */
3130 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
3132 /* Add virtual functions not already in our primary vtable. These
3133 will be both those introduced by this class, and those overridden
3134 from secondary bases. It does not include virtuals merely
3135 inherited from secondary bases. */
3136 for (fnsp = &virtuals; *fnsp; )
3138 tree fn = TREE_VALUE (*fnsp);
3140 if (!value_member (fn, BINFO_VIRTUALS (binfo))
3141 || DECL_VINDEX (fn) == error_mark_node)
3143 /* We don't need to adjust the `this' pointer when
3144 calling this function. */
3145 BV_DELTA (*fnsp) = integer_zero_node;
3146 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
3148 /* This is a function not already in our vtable. Keep it. */
3149 fnsp = &TREE_CHAIN (*fnsp);
3151 else
3152 /* We've already got an entry for this function. Skip it. */
3153 *fnsp = TREE_CHAIN (*fnsp);
3156 return virtuals;
3159 /* Get the base virtual function declarations in T that have the
3160 indicated NAME. */
3162 static void
3163 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
3165 bool found_decls = false;
3167 /* Find virtual functions in T with the indicated NAME. */
3168 for (tree method : ovl_range (get_class_binding (t, name)))
3170 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
3172 base_fndecls->safe_push (method);
3173 found_decls = true;
3177 if (found_decls)
3178 return;
3180 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3181 for (int i = 0; i < n_baseclasses; i++)
3183 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3184 get_basefndecls (name, basetype, base_fndecls);
3188 /* If this method overrides a virtual method from a base, then mark
3189 this member function as being virtual as well. Do 'final' and
3190 'override' checks too. */
3192 void
3193 check_for_override (tree decl, tree ctype)
3195 if (TREE_CODE (decl) == TEMPLATE_DECL)
3196 /* In [temp.mem] we have:
3198 A specialization of a member function template does not
3199 override a virtual function from a base class. */
3200 return;
3202 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
3203 used for a vfunc. That avoids the expensive look_for_overrides
3204 call that when we know there's nothing to find. As conversion
3205 operators for the same type can have distinct identifiers, we
3206 cannot optimize those in that way. */
3207 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3208 || DECL_CONV_FN_P (decl))
3209 && look_for_overrides (ctype, decl)
3210 /* Check staticness after we've checked if we 'override'. */
3211 && !DECL_STATIC_FUNCTION_P (decl))
3213 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3214 the error_mark_node so that we know it is an overriding
3215 function. */
3216 DECL_VINDEX (decl) = decl;
3218 if (warn_override
3219 && !DECL_OVERRIDE_P (decl)
3220 && !DECL_FINAL_P (decl)
3221 && !DECL_DESTRUCTOR_P (decl))
3222 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3223 "%qD can be marked override", decl);
3225 else if (DECL_OVERRIDE_P (decl))
3226 error ("%q+#D marked %<override%>, but does not override", decl);
3228 if (DECL_VIRTUAL_P (decl))
3230 /* Remember this identifier is virtual name. */
3231 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
3233 if (!DECL_VINDEX (decl))
3234 /* It's a new vfunc. */
3235 DECL_VINDEX (decl) = error_mark_node;
3237 if (DECL_DESTRUCTOR_P (decl))
3238 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3240 else if (DECL_FINAL_P (decl))
3241 error ("%q+#D marked %<final%>, but is not virtual", decl);
3244 /* Warn about hidden virtual functions that are not overridden in t.
3245 We know that constructors and destructors don't apply. */
3247 static void
3248 warn_hidden (tree t)
3250 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3251 for (unsigned ix = member_vec->length (); ix--;)
3253 tree fns = (*member_vec)[ix];
3255 if (!OVL_P (fns))
3256 continue;
3258 tree name = OVL_NAME (fns);
3259 auto_vec<tree, 20> base_fndecls;
3260 tree base_binfo;
3261 tree binfo;
3262 unsigned j;
3264 if (IDENTIFIER_CDTOR_P (name))
3265 continue;
3267 /* Iterate through all of the base classes looking for possibly
3268 hidden functions. */
3269 for (binfo = TYPE_BINFO (t), j = 0;
3270 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3272 tree basetype = BINFO_TYPE (base_binfo);
3273 get_basefndecls (name, basetype, &base_fndecls);
3276 /* If there are no functions to hide, continue. */
3277 if (base_fndecls.is_empty ())
3278 continue;
3280 /* Remove any overridden functions. */
3281 bool seen_non_override = false;
3282 for (tree fndecl : ovl_range (fns))
3284 bool any_override = false;
3285 if (TREE_CODE (fndecl) == FUNCTION_DECL
3286 && DECL_VINDEX (fndecl))
3288 /* If the method from the base class has the same
3289 signature as the method from the derived class, it
3290 has been overridden. Note that we can't move on
3291 after finding one match: fndecl might override
3292 multiple base fns. */
3293 for (size_t k = 0; k < base_fndecls.length (); k++)
3294 if (base_fndecls[k]
3295 && same_signature_p (fndecl, base_fndecls[k]))
3297 base_fndecls[k] = NULL_TREE;
3298 any_override = true;
3301 if (!any_override)
3302 seen_non_override = true;
3305 if (!seen_non_override && warn_overloaded_virtual == 1)
3306 /* All the derived fns override base virtuals. */
3307 return;
3309 /* Now give a warning for all base functions without overriders,
3310 as they are hidden. */
3311 for (tree base_fndecl : base_fndecls)
3312 if (base_fndecl)
3314 auto_diagnostic_group d;
3315 /* Here we know it is a hider, and no overrider exists. */
3316 if (warning_at (location_of (base_fndecl),
3317 OPT_Woverloaded_virtual_,
3318 "%qD was hidden", base_fndecl))
3319 inform (location_of (fns), " by %qD", fns);
3324 /* Recursive helper for finish_struct_anon. */
3326 static void
3327 finish_struct_anon_r (tree field)
3329 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3331 /* We're generally only interested in entities the user
3332 declared, but we also find nested classes by noticing
3333 the TYPE_DECL that we create implicitly. You're
3334 allowed to put one anonymous union inside another,
3335 though, so we explicitly tolerate that. We use
3336 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3337 we also allow unnamed types used for defining fields. */
3338 if (DECL_ARTIFICIAL (elt)
3339 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3340 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3341 continue;
3343 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3344 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3346 /* Recurse into the anonymous aggregates to correctly handle
3347 access control (c++/24926):
3349 class A {
3350 union {
3351 union {
3352 int i;
3357 int j=A().i; */
3358 if (DECL_NAME (elt) == NULL_TREE
3359 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3360 finish_struct_anon_r (elt);
3364 /* Fix up any anonymous union/struct members of T. */
3366 static void
3367 finish_struct_anon (tree t)
3369 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3371 if (TREE_STATIC (field))
3372 continue;
3373 if (TREE_CODE (field) != FIELD_DECL)
3374 continue;
3376 if (DECL_NAME (field) == NULL_TREE
3377 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3378 finish_struct_anon_r (field);
3382 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3383 will be used later during class template instantiation.
3384 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3385 a non-static member data (FIELD_DECL), a member function
3386 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3387 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3388 When FRIEND_P is nonzero, T is either a friend class
3389 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3390 (FUNCTION_DECL, TEMPLATE_DECL). */
3392 void
3393 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3395 if (CLASSTYPE_TEMPLATE_INFO (type)
3396 && TREE_CODE (t) != CONST_DECL)
3398 tree purpose = friend_p ? NULL_TREE : type;
3400 CLASSTYPE_DECL_LIST (type)
3401 = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3405 /* This function is called from declare_virt_assop_and_dtor via
3406 dfs_walk_all.
3408 DATA is a type that direcly or indirectly inherits the base
3409 represented by BINFO. If BINFO contains a virtual assignment [copy
3410 assignment or move assigment] operator or a virtual constructor,
3411 declare that function in DATA if it hasn't been already declared. */
3413 static tree
3414 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3416 tree bv, fn, t = (tree)data;
3417 tree opname = assign_op_identifier;
3419 gcc_assert (t && CLASS_TYPE_P (t));
3420 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3422 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3423 /* A base without a vtable needs no modification, and its bases
3424 are uninteresting. */
3425 return dfs_skip_bases;
3427 if (BINFO_PRIMARY_P (binfo))
3428 /* If this is a primary base, then we have already looked at the
3429 virtual functions of its vtable. */
3430 return NULL_TREE;
3432 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3434 fn = BV_FN (bv);
3436 if (DECL_NAME (fn) == opname)
3438 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3439 lazily_declare_fn (sfk_copy_assignment, t);
3440 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3441 lazily_declare_fn (sfk_move_assignment, t);
3443 else if (DECL_DESTRUCTOR_P (fn)
3444 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3445 lazily_declare_fn (sfk_destructor, t);
3448 return NULL_TREE;
3451 /* If the class type T has a direct or indirect base that contains a
3452 virtual assignment operator or a virtual destructor, declare that
3453 function in T if it hasn't been already declared. */
3455 static void
3456 declare_virt_assop_and_dtor (tree t)
3458 if (!(TYPE_POLYMORPHIC_P (t)
3459 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3460 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3461 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3462 return;
3464 dfs_walk_all (TYPE_BINFO (t),
3465 dfs_declare_virt_assop_and_dtor,
3466 NULL, t);
3469 /* Declare the inheriting constructor for class T inherited from base
3470 constructor CTOR with the parameter array PARMS of size NPARMS. */
3472 static void
3473 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3475 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3477 /* We don't declare an inheriting ctor that would be a default,
3478 copy or move ctor for derived or base. */
3479 if (nparms == 0)
3480 return;
3481 if (nparms == 1
3482 && TYPE_REF_P (parms[0]))
3484 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3485 if (parm == t || parm == DECL_CONTEXT (ctor))
3486 return;
3489 tree parmlist = void_list_node;
3490 for (int i = nparms - 1; i >= 0; i--)
3491 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3492 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3493 t, false, ctor, parmlist);
3495 if (add_method (t, fn, false))
3497 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3498 TYPE_FIELDS (t) = fn;
3502 /* Declare all the inheriting constructors for class T inherited from base
3503 constructor CTOR. */
3505 static void
3506 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3508 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3510 if (flag_new_inheriting_ctors)
3512 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3513 t, /*const*/false, ctor, parms);
3514 add_method (t, ctor, using_decl != NULL_TREE);
3515 return;
3518 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3519 int i = 0;
3520 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3522 if (TREE_PURPOSE (parms))
3523 one_inheriting_sig (t, ctor, new_parms, i);
3524 new_parms[i++] = TREE_VALUE (parms);
3526 one_inheriting_sig (t, ctor, new_parms, i);
3527 if (parms == NULL_TREE)
3529 auto_diagnostic_group d;
3530 if (warning (OPT_Winherited_variadic_ctor,
3531 "the ellipsis in %qD is not inherited", ctor))
3532 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3536 /* Implicitly declare T(). */
3538 static void
3539 add_implicit_default_ctor (tree t)
3541 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3542 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3543 if (cxx_dialect >= cxx11)
3544 TYPE_HAS_CONSTEXPR_CTOR (t)
3545 /* Don't force the declaration to get a hard answer; if the
3546 definition would have made the class non-literal, it will still be
3547 non-literal because of the base or member in question, and that
3548 gives a better diagnostic. */
3549 = type_maybe_constexpr_default_constructor (t);
3552 /* Create default constructors, assignment operators, and so forth for
3553 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3554 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3555 the class cannot have a default constructor, copy constructor
3556 taking a const reference argument, or an assignment operator taking
3557 a const reference, respectively. */
3559 static void
3560 add_implicitly_declared_members (tree t, tree* access_decls,
3561 int cant_have_const_cctor,
3562 int cant_have_const_assignment)
3564 /* Destructor. */
3565 if (!CLASSTYPE_DESTRUCTOR (t))
3566 /* In general, we create destructors lazily. */
3567 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3569 bool move_ok = false;
3570 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3571 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3572 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3573 move_ok = true;
3575 /* [class.ctor]
3577 If there is no user-declared constructor for a class, a default
3578 constructor is implicitly declared. */
3579 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3580 add_implicit_default_ctor (t);
3582 /* [class.ctor]
3584 If a class definition does not explicitly declare a copy
3585 constructor, one is declared implicitly. */
3586 if (! TYPE_HAS_COPY_CTOR (t))
3588 TYPE_HAS_COPY_CTOR (t) = 1;
3589 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3590 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3591 if (move_ok)
3592 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3595 /* If there is no assignment operator, one will be created if and
3596 when it is needed. For now, just record whether or not the type
3597 of the parameter to the assignment operator will be a const or
3598 non-const reference. */
3599 if (!TYPE_HAS_COPY_ASSIGN (t))
3601 TYPE_HAS_COPY_ASSIGN (t) = 1;
3602 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3603 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3604 if (move_ok && !LAMBDA_TYPE_P (t))
3605 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3608 /* We can't be lazy about declaring functions that might override
3609 a virtual function from a base class. */
3610 declare_virt_assop_and_dtor (t);
3612 /* If the class definition does not explicitly declare an == operator
3613 function, but declares a defaulted three-way comparison operator function,
3614 an == operator function is declared implicitly. */
3615 if (!classtype_has_op (t, EQ_EXPR))
3616 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3618 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3619 NULL_TREE);
3620 bool is_friend = DECL_CONTEXT (space) != t;
3621 if (is_friend)
3622 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3623 NO_SPECIAL, true);
3624 else
3626 add_method (t, eq, false);
3627 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3628 TYPE_FIELDS (t) = eq;
3630 maybe_add_class_template_decl_list (t, eq, is_friend);
3633 while (*access_decls)
3635 tree using_decl = TREE_VALUE (*access_decls);
3636 tree decl = USING_DECL_DECLS (using_decl);
3637 if (DECL_NAME (using_decl) == ctor_identifier)
3639 /* declare, then remove the decl */
3640 tree ctor_list = decl;
3641 location_t loc = input_location;
3642 input_location = DECL_SOURCE_LOCATION (using_decl);
3643 for (tree fn : ovl_range (ctor_list))
3645 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3646 /* CWG2799: Inheriting a default constructor gives us a default
3647 constructor, not just an inherited constructor. */
3648 add_implicit_default_ctor (t);
3649 one_inherited_ctor (fn, t, using_decl);
3651 *access_decls = TREE_CHAIN (*access_decls);
3652 input_location = loc;
3654 else
3655 access_decls = &TREE_CHAIN (*access_decls);
3659 /* Cache of enum_min_precision values. */
3660 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3662 /* Return the minimum precision of a bit-field needed to store all
3663 enumerators of ENUMERAL_TYPE TYPE. */
3665 static int
3666 enum_min_precision (tree type)
3668 type = TYPE_MAIN_VARIANT (type);
3669 /* For unscoped enums without fixed underlying type and without mode
3670 attribute we can just use precision of the underlying type. */
3671 if (UNSCOPED_ENUM_P (type)
3672 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3673 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3674 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3676 if (enum_to_min_precision == NULL)
3677 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3679 bool existed;
3680 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3681 if (existed)
3682 return prec;
3684 tree minnode, maxnode;
3685 if (TYPE_VALUES (type))
3687 minnode = maxnode = NULL_TREE;
3688 for (tree values = TYPE_VALUES (type);
3689 values; values = TREE_CHAIN (values))
3691 tree decl = TREE_VALUE (values);
3692 tree value = DECL_INITIAL (decl);
3693 if (value == error_mark_node)
3694 value = integer_zero_node;
3695 if (!minnode)
3696 minnode = maxnode = value;
3697 else if (tree_int_cst_lt (maxnode, value))
3698 maxnode = value;
3699 else if (tree_int_cst_lt (value, minnode))
3700 minnode = value;
3703 else
3704 minnode = maxnode = integer_zero_node;
3706 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3707 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3708 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3709 prec = MAX (lowprec, highprec);
3710 return prec;
3713 /* FIELD is a bit-field. We are finishing the processing for its
3714 enclosing type. Issue any appropriate messages and set appropriate
3715 flags. Returns false if an error has been diagnosed. */
3717 static bool
3718 check_bitfield_decl (tree field)
3720 tree type = TREE_TYPE (field);
3721 tree w;
3723 /* Extract the declared width of the bitfield, which has been
3724 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3725 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3726 gcc_assert (w != NULL_TREE);
3727 /* Remove the bit-field width indicator so that the rest of the
3728 compiler does not treat that value as a qualifier. */
3729 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3731 /* Detect invalid bit-field type. */
3732 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3734 error_at (DECL_SOURCE_LOCATION (field),
3735 "bit-field %q#D with non-integral type %qT", field, type);
3736 w = error_mark_node;
3738 else
3740 location_t loc = input_location;
3741 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3742 STRIP_NOPS (w);
3744 /* detect invalid field size. */
3745 input_location = DECL_SOURCE_LOCATION (field);
3746 w = cxx_constant_value (w);
3747 input_location = loc;
3749 if (TREE_CODE (w) != INTEGER_CST)
3751 error ("bit-field %q+D width not an integer constant", field);
3752 w = error_mark_node;
3754 else if (tree_int_cst_sgn (w) < 0)
3756 error ("negative width in bit-field %q+D", field);
3757 w = error_mark_node;
3759 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3761 error ("zero width for bit-field %q+D", field);
3762 w = error_mark_node;
3764 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3765 && TREE_CODE (type) != BOOLEAN_TYPE
3766 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3767 || ((TREE_CODE (type) == ENUMERAL_TYPE
3768 || TREE_CODE (type) == BOOLEAN_TYPE)
3769 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3770 warning_at (DECL_SOURCE_LOCATION (field), 0,
3771 "width of %qD exceeds its type", field);
3772 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3774 int prec = enum_min_precision (type);
3775 if (compare_tree_int (w, prec) < 0)
3776 warning_at (DECL_SOURCE_LOCATION (field), 0,
3777 "%qD is too small to hold all values of %q#T",
3778 field, type);
3782 if (w != error_mark_node)
3784 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3785 DECL_BIT_FIELD (field) = 1;
3786 return true;
3788 else
3790 /* Non-bit-fields are aligned for their type. */
3791 DECL_BIT_FIELD (field) = 0;
3792 CLEAR_DECL_C_BIT_FIELD (field);
3793 return false;
3797 /* FIELD is a non bit-field. We are finishing the processing for its
3798 enclosing type T. Issue any appropriate messages and set appropriate
3799 flags. */
3801 static bool
3802 check_field_decl (tree field,
3803 tree t,
3804 int* cant_have_const_ctor,
3805 int* no_const_asn_ref)
3807 tree type = strip_array_types (TREE_TYPE (field));
3808 bool any_default_members = false;
3810 /* In C++98 an anonymous union cannot contain any fields which would change
3811 the settings of CANT_HAVE_CONST_CTOR and friends. */
3812 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3814 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3815 structs. So, we recurse through their fields here. */
3816 else if (ANON_AGGR_TYPE_P (type))
3818 for (tree fields = TYPE_FIELDS (type); fields;
3819 fields = DECL_CHAIN (fields))
3820 if (TREE_CODE (fields) == FIELD_DECL)
3821 any_default_members |= check_field_decl (fields, t,
3822 cant_have_const_ctor,
3823 no_const_asn_ref);
3825 /* Check members with class type for constructors, destructors,
3826 etc. */
3827 else if (CLASS_TYPE_P (type))
3829 /* Never let anything with uninheritable virtuals
3830 make it through without complaint. */
3831 abstract_virtuals_error (field, type);
3833 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3835 static bool warned;
3836 int oldcount = errorcount;
3837 if (TYPE_NEEDS_CONSTRUCTING (type))
3838 error ("member %q+#D with constructor not allowed in union",
3839 field);
3840 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3841 error ("member %q+#D with destructor not allowed in union", field);
3842 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3843 error ("member %q+#D with copy assignment operator not allowed in union",
3844 field);
3845 if (!warned && errorcount > oldcount)
3847 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3848 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3849 warned = true;
3852 else
3854 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3855 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3856 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3857 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3858 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3859 || !TYPE_HAS_COPY_ASSIGN (type));
3860 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3861 || !TYPE_HAS_COPY_CTOR (type));
3862 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3863 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3864 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3865 || TYPE_HAS_COMPLEX_DFLT (type));
3868 if (TYPE_HAS_COPY_CTOR (type)
3869 && !TYPE_HAS_CONST_COPY_CTOR (type))
3870 *cant_have_const_ctor = 1;
3872 if (TYPE_HAS_COPY_ASSIGN (type)
3873 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3874 *no_const_asn_ref = 1;
3877 check_abi_tags (t, field);
3879 if (DECL_INITIAL (field) != NULL_TREE)
3880 /* `build_class_init_list' does not recognize
3881 non-FIELD_DECLs. */
3882 any_default_members = true;
3884 return any_default_members;
3887 /* Check the data members (both static and non-static), class-scoped
3888 typedefs, etc., appearing in the declaration of T. Issue
3889 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3890 declaration order) of access declarations; each TREE_VALUE in this
3891 list is a USING_DECL.
3893 In addition, set the following flags:
3895 EMPTY_P
3896 The class is empty, i.e., contains no non-static data members.
3898 CANT_HAVE_CONST_CTOR_P
3899 This class cannot have an implicitly generated copy constructor
3900 taking a const reference.
3902 CANT_HAVE_CONST_ASN_REF
3903 This class cannot have an implicitly generated assignment
3904 operator taking a const reference.
3906 All of these flags should be initialized before calling this
3907 function. */
3909 static void
3910 check_field_decls (tree t, tree *access_decls,
3911 int *cant_have_const_ctor_p,
3912 int *no_const_asn_ref_p)
3914 int cant_pack = 0;
3916 /* Assume there are no access declarations. */
3917 *access_decls = NULL_TREE;
3918 /* Effective C has things to say about classes with pointer members. */
3919 tree pointer_member = NULL_TREE;
3920 /* Default initialized members affect the whole class. */
3921 tree default_init_member = NULL_TREE;
3922 /* Lack of any non-static data member of non-volatile literal
3923 type affects a union. */
3924 bool found_nv_literal_p = false;
3925 /* Standard layout requires all FIELDS have same access. */
3926 int field_access = -1;
3928 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3930 tree type = TREE_TYPE (field);
3932 switch (TREE_CODE (field))
3934 default:
3935 gcc_unreachable ();
3937 case USING_DECL:
3938 /* Save the access declarations for our caller. */
3939 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3940 break;
3942 case TYPE_DECL:
3943 case TEMPLATE_DECL:
3944 break;
3946 case FUNCTION_DECL:
3947 /* FIXME: We should fold in the checking from check_methods. */
3948 break;
3950 case CONST_DECL:
3951 DECL_NONLOCAL (field) = 1;
3952 break;
3954 case VAR_DECL:
3955 if (TREE_CODE (t) == UNION_TYPE
3956 && cxx_dialect < cxx11)
3958 /* [class.union]
3960 (C++98) If a union contains a static data member,
3961 ... the program is ill-formed. */
3962 if (cxx_dialect < cxx11)
3963 error ("in C++98 %q+D may not be static because it is "
3964 "a member of a union", field);
3966 goto data_member;
3968 case FIELD_DECL:
3969 if (TREE_CODE (t) == UNION_TYPE)
3971 /* [class.union]
3973 If a union contains ... or a [non-static data] member
3974 of reference type, the program is ill-formed. */
3975 if (TYPE_REF_P (type))
3976 error ("non-static data member %q+D in a union may not "
3977 "have reference type %qT", field, type);
3980 data_member:
3981 /* Common VAR_DECL & FIELD_DECL processing. */
3982 DECL_CONTEXT (field) = t;
3983 DECL_NONLOCAL (field) = 1;
3985 /* Template instantiation can cause this. Perhaps this
3986 should be a specific instantiation check? */
3987 if (TREE_CODE (type) == FUNCTION_TYPE)
3989 error ("data member %q+D invalidly declared function type", field);
3990 type = build_pointer_type (type);
3991 TREE_TYPE (field) = type;
3993 else if (TREE_CODE (type) == METHOD_TYPE)
3995 error ("data member %q+D invalidly declared method type", field);
3996 type = build_pointer_type (type);
3997 TREE_TYPE (field) = type;
4000 break;
4003 if (TREE_CODE (field) != FIELD_DECL)
4004 continue;
4006 if (type == error_mark_node)
4007 continue;
4009 /* If it is not a union and at least one non-static data member is
4010 non-literal, the whole class becomes non-literal. Per Core/1453,
4011 volatile non-static data members and base classes are also not allowed.
4012 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
4013 members.
4014 Note: if the type is incomplete we will complain later on. */
4015 if (COMPLETE_TYPE_P (type))
4017 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
4018 CLASSTYPE_LITERAL_P (t) = false;
4019 else
4020 found_nv_literal_p = true;
4023 int this_field_access = (TREE_PROTECTED (field) ? 1
4024 : TREE_PRIVATE (field) ? 2 : 0);
4025 if (field_access != this_field_access)
4027 /* A standard-layout class is a class that:
4029 ... has the same access control (Clause 11) for all
4030 non-static data members, */
4031 if (field_access < 0)
4032 field_access = this_field_access;
4033 else
4034 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4036 /* Aggregates must be public. */
4037 if (this_field_access)
4038 CLASSTYPE_NON_AGGREGATE (t) = 1;
4041 /* If this is of reference type, check if it needs an init. */
4042 if (TYPE_REF_P (type))
4044 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4045 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4046 if (DECL_INITIAL (field) == NULL_TREE)
4047 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4048 if (cxx_dialect < cxx11)
4050 /* ARM $12.6.2: [A member initializer list] (or, for an
4051 aggregate, initialization by a brace-enclosed list) is the
4052 only way to initialize non-static const and reference
4053 members. */
4054 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4055 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4059 type = strip_array_types (type);
4061 if (TYPE_PACKED (t))
4063 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4065 warning_at (DECL_SOURCE_LOCATION (field), 0,
4066 "ignoring packed attribute because of"
4067 " unpacked non-POD field %q#D", field);
4068 cant_pack = 1;
4070 else if (DECL_C_BIT_FIELD (field)
4071 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
4072 DECL_PACKED (field) = 1;
4075 if (DECL_C_BIT_FIELD (field)
4076 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
4077 /* We don't treat zero-width bitfields as making a class
4078 non-empty. */
4080 else if (field_poverlapping_p (field)
4081 && is_empty_class (TREE_TYPE (field)))
4082 /* Empty data members also don't make a class non-empty. */
4083 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4084 else
4086 /* The class is non-empty. */
4087 CLASSTYPE_EMPTY_P (t) = 0;
4088 /* The class is not even nearly empty. */
4089 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4090 /* If one of the data members contains an empty class, so
4091 does T. */
4092 if (CLASS_TYPE_P (type)
4093 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4094 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4097 /* This is used by -Weffc++ (see below). Warn only for pointers
4098 to members which might hold dynamic memory. So do not warn
4099 for pointers to functions or pointers to members. */
4100 if (TYPE_PTR_P (type)
4101 && !TYPE_PTRFN_P (type))
4102 pointer_member = field;
4104 if (CLASS_TYPE_P (type))
4106 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
4107 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4108 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
4109 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4112 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
4113 CLASSTYPE_HAS_MUTABLE (t) = 1;
4115 if (DECL_MUTABLE_P (field))
4117 if (TYPE_REF_P (type))
4118 error ("member %q+D cannot be declared as a %<mutable%> "
4119 "reference", field);
4120 else if (CP_TYPE_CONST_P (type))
4121 error ("member %q+D cannot be declared both %<const%> "
4122 "and %<mutable%>", field);
4125 if (! layout_pod_type_p (type))
4126 /* DR 148 now allows pointers to members (which are POD themselves),
4127 to be allowed in POD structs. */
4128 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4130 if (field_poverlapping_p (field))
4131 /* A potentially-overlapping non-static data member makes the class
4132 non-layout-POD. */
4133 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4135 if (!std_layout_type_p (type))
4136 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4138 if (! zero_init_p (type))
4139 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
4141 /* We set DECL_C_BIT_FIELD in grokbitfield.
4142 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4143 if (DECL_C_BIT_FIELD (field))
4144 check_bitfield_decl (field);
4146 if (check_field_decl (field, t,
4147 cant_have_const_ctor_p, no_const_asn_ref_p))
4149 if (default_init_member
4150 && TREE_CODE (t) == UNION_TYPE)
4152 error ("multiple fields in union %qT initialized", t);
4153 inform (DECL_SOURCE_LOCATION (default_init_member),
4154 "initialized member %q+D declared here",
4155 default_init_member);
4157 default_init_member = field;
4160 /* Now that we've removed bit-field widths from DECL_INITIAL,
4161 anything left in DECL_INITIAL is an NSDMI that makes the class
4162 non-aggregate in C++11, and non-layout-POD always. */
4163 if (DECL_INITIAL (field))
4165 if (cxx_dialect < cxx14)
4166 CLASSTYPE_NON_AGGREGATE (t) = true;
4167 else
4168 CLASSTYPE_NON_POD_AGGREGATE (t) = true;
4171 if (CP_TYPE_CONST_P (type))
4173 /* If any field is const, the structure type is pseudo-const. */
4174 C_TYPE_FIELDS_READONLY (t) = 1;
4175 if (DECL_INITIAL (field) == NULL_TREE)
4176 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4177 if (cxx_dialect < cxx11)
4179 /* ARM $12.6.2: [A member initializer list] (or, for an
4180 aggregate, initialization by a brace-enclosed list) is the
4181 only way to initialize non-static const and reference
4182 members. */
4183 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4184 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4187 /* A field that is pseudo-const makes the structure likewise. */
4188 else if (CLASS_TYPE_P (type))
4190 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4191 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
4192 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4193 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
4196 /* Core issue 80: A non-static data member is required to have a
4197 different name from the class iff the class has a
4198 user-declared constructor. */
4199 if (constructor_name_p (DECL_NAME (field), t)
4200 && TYPE_HAS_USER_CONSTRUCTOR (t))
4201 permerror (DECL_SOURCE_LOCATION (field),
4202 "field %q#D with same name as class", field);
4205 /* Per CWG 2096, a type is a literal type if it is a union, and at least
4206 one of its non-static data members is of non-volatile literal type. */
4207 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
4208 CLASSTYPE_LITERAL_P (t) = true;
4210 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
4211 it should also define a copy constructor and an assignment operator to
4212 implement the correct copy semantic (deep vs shallow, etc.). As it is
4213 not feasible to check whether the constructors do allocate dynamic memory
4214 and store it within members, we approximate the warning like this:
4216 -- Warn only if there are members which are pointers
4217 -- Warn only if there is a non-trivial constructor (otherwise,
4218 there cannot be memory allocated).
4219 -- Warn only if there is a non-trivial destructor. We assume that the
4220 user at least implemented the cleanup correctly, and a destructor
4221 is needed to free dynamic memory.
4223 This seems enough for practical purposes. */
4224 if (warn_ecpp
4225 && pointer_member
4226 && TYPE_HAS_USER_CONSTRUCTOR (t)
4227 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4228 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
4230 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
4232 if (! TYPE_HAS_COPY_CTOR (t))
4234 warning (OPT_Weffc__,
4235 " but does not declare %<%T(const %T&)%>", t, t);
4236 if (!TYPE_HAS_COPY_ASSIGN (t))
4237 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4239 else if (! TYPE_HAS_COPY_ASSIGN (t))
4240 warning (OPT_Weffc__,
4241 " but does not declare %<operator=(const %T&)%>", t);
4242 inform (DECL_SOURCE_LOCATION (pointer_member),
4243 "pointer member %q+D declared here", pointer_member);
4247 /* Non-static data member initializers make the default constructor
4248 non-trivial. */
4249 if (default_init_member)
4251 TYPE_NEEDS_CONSTRUCTING (t) = true;
4252 TYPE_HAS_COMPLEX_DFLT (t) = true;
4255 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4256 if (cant_pack)
4257 TYPE_PACKED (t) = 0;
4259 /* Check anonymous struct/anonymous union fields. */
4260 finish_struct_anon (t);
4262 /* We've built up the list of access declarations in reverse order.
4263 Fix that now. */
4264 *access_decls = nreverse (*access_decls);
4267 /* If TYPE is an empty class type, records its OFFSET in the table of
4268 OFFSETS. */
4270 static int
4271 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4273 splay_tree_node n;
4275 if (!is_empty_class (type))
4276 return 0;
4278 /* Record the location of this empty object in OFFSETS. */
4279 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4280 if (!n)
4281 n = splay_tree_insert (offsets,
4282 (splay_tree_key) offset,
4283 (splay_tree_value) NULL_TREE);
4284 n->value = ((splay_tree_value)
4285 tree_cons (NULL_TREE,
4286 type,
4287 (tree) n->value));
4289 return 0;
4292 /* Returns nonzero if TYPE is an empty class type and there is
4293 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4295 static int
4296 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4298 splay_tree_node n;
4299 tree t;
4301 if (!is_empty_class (type))
4302 return 0;
4304 /* Record the location of this empty object in OFFSETS. */
4305 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4306 if (!n)
4307 return 0;
4309 enum { ignore, fast, slow, warn }
4310 cv_check = (abi_version_crosses (19) ? slow
4311 : abi_version_at_least (19) ? fast
4312 : ignore);
4313 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4315 tree elt = TREE_VALUE (t);
4317 if (same_type_p (elt, type))
4318 return 1;
4320 if (cv_check != ignore
4321 && similar_type_p (elt, type))
4323 if (cv_check == fast)
4324 return 1;
4325 cv_check = warn;
4329 if (cv_check == warn)
4331 warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4332 "[[no_unique_address]]", type, "-fabi-version=19");
4333 if (abi_version_at_least (19))
4334 return 1;
4337 return 0;
4340 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4341 F for every subobject, passing it the type, offset, and table of
4342 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4343 be traversed.
4345 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4346 than MAX_OFFSET will not be walked.
4348 If F returns a nonzero value, the traversal ceases, and that value
4349 is returned. Otherwise, returns zero. */
4351 static int
4352 walk_subobject_offsets (tree type,
4353 subobject_offset_fn f,
4354 tree offset,
4355 splay_tree offsets,
4356 tree max_offset,
4357 int vbases_p)
4359 int r = 0;
4360 tree type_binfo = NULL_TREE;
4362 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4363 stop. */
4364 if (max_offset && tree_int_cst_lt (max_offset, offset))
4365 return 0;
4367 if (type == error_mark_node)
4368 return 0;
4370 if (!TYPE_P (type))
4372 type_binfo = type;
4373 type = BINFO_TYPE (type);
4376 if (CLASS_TYPE_P (type))
4378 tree field;
4379 tree binfo;
4380 int i;
4382 /* Avoid recursing into objects that are not interesting. */
4383 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4384 return 0;
4386 /* Record the location of TYPE. */
4387 r = (*f) (type, offset, offsets);
4388 if (r)
4389 return r;
4391 /* Iterate through the direct base classes of TYPE. */
4392 if (!type_binfo)
4393 type_binfo = TYPE_BINFO (type);
4394 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4396 tree binfo_offset;
4398 if (BINFO_VIRTUAL_P (binfo))
4399 continue;
4401 tree orig_binfo;
4402 /* We cannot rely on BINFO_OFFSET being set for the base
4403 class yet, but the offsets for direct non-virtual
4404 bases can be calculated by going back to the TYPE. */
4405 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4406 binfo_offset = size_binop (PLUS_EXPR,
4407 offset,
4408 BINFO_OFFSET (orig_binfo));
4410 r = walk_subobject_offsets (binfo,
4412 binfo_offset,
4413 offsets,
4414 max_offset,
4415 /*vbases_p=*/0);
4416 if (r)
4417 return r;
4420 if (CLASSTYPE_VBASECLASSES (type))
4422 unsigned ix;
4423 vec<tree, va_gc> *vbases;
4425 /* Iterate through the virtual base classes of TYPE. In G++
4426 3.2, we included virtual bases in the direct base class
4427 loop above, which results in incorrect results; the
4428 correct offsets for virtual bases are only known when
4429 working with the most derived type. */
4430 if (vbases_p)
4431 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4432 vec_safe_iterate (vbases, ix, &binfo); ix++)
4434 r = walk_subobject_offsets (binfo,
4436 size_binop (PLUS_EXPR,
4437 offset,
4438 BINFO_OFFSET (binfo)),
4439 offsets,
4440 max_offset,
4441 /*vbases_p=*/0);
4442 if (r)
4443 return r;
4445 else
4447 /* We still have to walk the primary base, if it is
4448 virtual. (If it is non-virtual, then it was walked
4449 above.) */
4450 tree vbase = get_primary_binfo (type_binfo);
4452 if (vbase && BINFO_VIRTUAL_P (vbase)
4453 && BINFO_PRIMARY_P (vbase)
4454 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4456 r = (walk_subobject_offsets
4457 (vbase, f, offset,
4458 offsets, max_offset, /*vbases_p=*/0));
4459 if (r)
4460 return r;
4465 /* Iterate through the fields of TYPE. */
4466 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4467 if (TREE_CODE (field) == FIELD_DECL
4468 && TREE_TYPE (field) != error_mark_node
4469 && !DECL_ARTIFICIAL (field))
4471 tree field_offset;
4473 field_offset = byte_position (field);
4475 r = walk_subobject_offsets (TREE_TYPE (field),
4477 size_binop (PLUS_EXPR,
4478 offset,
4479 field_offset),
4480 offsets,
4481 max_offset,
4482 /*vbases_p=*/1);
4483 if (r)
4484 return r;
4487 else if (TREE_CODE (type) == ARRAY_TYPE)
4489 tree element_type = strip_array_types (type);
4490 tree domain = TYPE_DOMAIN (type);
4491 tree index;
4493 /* Avoid recursing into objects that are not interesting. */
4494 if (!CLASS_TYPE_P (element_type)
4495 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4496 || !domain
4497 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4498 return 0;
4500 /* Step through each of the elements in the array. */
4501 for (index = size_zero_node;
4502 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4503 index = size_binop (PLUS_EXPR, index, size_one_node))
4505 r = walk_subobject_offsets (TREE_TYPE (type),
4507 offset,
4508 offsets,
4509 max_offset,
4510 /*vbases_p=*/1);
4511 if (r)
4512 return r;
4513 offset = size_binop (PLUS_EXPR, offset,
4514 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4515 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4516 there's no point in iterating through the remaining
4517 elements of the array. */
4518 if (max_offset && tree_int_cst_lt (max_offset, offset))
4519 break;
4523 return 0;
4526 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4528 static bool
4529 field_poverlapping_p (tree decl)
4531 /* Base fields are actually potentially overlapping, but C++ bases go through
4532 a different code path based on binfos, and ObjC++ base fields are laid out
4533 in objc-act, so we don't want layout_class_type to mess with them. */
4534 if (DECL_FIELD_IS_BASE (decl))
4536 gcc_checking_assert (c_dialect_objc ());
4537 return false;
4540 return lookup_attribute ("no_unique_address",
4541 DECL_ATTRIBUTES (decl));
4544 /* Return true iff DECL is an empty field, either for an empty base or a
4545 [[no_unique_address]] data member. */
4547 bool
4548 is_empty_field (tree decl)
4550 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4551 return false;
4553 bool r = (is_empty_class (TREE_TYPE (decl))
4554 && (DECL_FIELD_IS_BASE (decl)
4555 || field_poverlapping_p (decl)));
4557 /* Empty fields should have size zero. */
4558 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4560 return r;
4563 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4565 static void
4566 record_subobject_offsets (tree decl_or_binfo,
4567 splay_tree offsets)
4569 tree type, offset;
4570 bool overlapping, vbases_p;
4572 if (DECL_P (decl_or_binfo))
4574 tree decl = decl_or_binfo;
4575 type = TREE_TYPE (decl);
4576 offset = byte_position (decl);
4577 overlapping = field_poverlapping_p (decl);
4578 vbases_p = true;
4580 else
4582 type = BINFO_TYPE (decl_or_binfo);
4583 offset = BINFO_OFFSET (decl_or_binfo);
4584 overlapping = true;
4585 vbases_p = false;
4588 tree max_offset;
4589 /* If recording subobjects for a non-static data member or a
4590 non-empty base class, we do not need to record offsets beyond
4591 the size of the biggest empty class. Additional data members
4592 will go at the end of the class. Additional base classes will go
4593 either at offset zero (if empty, in which case they cannot
4594 overlap with offsets past the size of the biggest empty class) or
4595 at the end of the class.
4597 However, if we are placing an empty base class, then we must record
4598 all offsets, as either the empty class is at offset zero (where
4599 other empty classes might later be placed) or at the end of the
4600 class (where other objects might then be placed, so other empty
4601 subobjects might later overlap). */
4602 if (!overlapping
4603 || !is_empty_class (type))
4604 max_offset = sizeof_biggest_empty_class;
4605 else
4606 max_offset = NULL_TREE;
4607 walk_subobject_offsets (type, record_subobject_offset, offset,
4608 offsets, max_offset, vbases_p);
4611 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4612 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4613 virtual bases of TYPE are examined. */
4615 static int
4616 layout_conflict_p (tree type,
4617 tree offset,
4618 splay_tree offsets,
4619 int vbases_p)
4621 splay_tree_node max_node;
4623 /* Get the node in OFFSETS that indicates the maximum offset where
4624 an empty subobject is located. */
4625 max_node = splay_tree_max (offsets);
4626 /* If there aren't any empty subobjects, then there's no point in
4627 performing this check. */
4628 if (!max_node)
4629 return 0;
4631 return walk_subobject_offsets (type, check_subobject_offset, offset,
4632 offsets, (tree) (max_node->key),
4633 vbases_p);
4636 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4637 non-static data member of the type indicated by RLI. BINFO is the
4638 binfo corresponding to the base subobject, OFFSETS maps offsets to
4639 types already located at those offsets. This function determines
4640 the position of the DECL. */
4642 static void
4643 layout_nonempty_base_or_field (record_layout_info rli,
4644 tree decl,
4645 tree binfo,
4646 splay_tree offsets)
4648 tree offset = NULL_TREE;
4649 bool field_p;
4650 tree type;
4652 if (binfo)
4654 /* For the purposes of determining layout conflicts, we want to
4655 use the class type of BINFO; TREE_TYPE (DECL) will be the
4656 CLASSTYPE_AS_BASE version, which does not contain entries for
4657 zero-sized bases. */
4658 type = TREE_TYPE (binfo);
4659 field_p = false;
4661 else
4663 type = TREE_TYPE (decl);
4664 field_p = true;
4667 /* Try to place the field. It may take more than one try if we have
4668 a hard time placing the field without putting two objects of the
4669 same type at the same address. */
4670 while (1)
4672 struct record_layout_info_s old_rli = *rli;
4674 /* Place this field. */
4675 place_field (rli, decl);
4676 offset = byte_position (decl);
4678 /* We have to check to see whether or not there is already
4679 something of the same type at the offset we're about to use.
4680 For example, consider:
4682 struct S {};
4683 struct T : public S { int i; };
4684 struct U : public S, public T {};
4686 Here, we put S at offset zero in U. Then, we can't put T at
4687 offset zero -- its S component would be at the same address
4688 as the S we already allocated. So, we have to skip ahead.
4689 Since all data members, including those whose type is an
4690 empty class, have nonzero size, any overlap can happen only
4691 with a direct or indirect base-class -- it can't happen with
4692 a data member. */
4693 /* In a union, overlap is permitted; all members are placed at
4694 offset zero. */
4695 if (TREE_CODE (rli->t) == UNION_TYPE)
4696 break;
4697 if (layout_conflict_p (field_p ? type : binfo, offset,
4698 offsets, field_p))
4700 /* Strip off the size allocated to this field. That puts us
4701 at the first place we could have put the field with
4702 proper alignment. */
4703 *rli = old_rli;
4705 /* Bump up by the alignment required for the type. */
4706 rli->bitpos
4707 = size_binop (PLUS_EXPR, rli->bitpos,
4708 bitsize_int (binfo
4709 ? CLASSTYPE_ALIGN (type)
4710 : TYPE_ALIGN (type)));
4711 normalize_rli (rli);
4713 else if (TREE_CODE (type) == NULLPTR_TYPE
4714 && warn_abi && abi_version_crosses (9))
4716 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4717 the offset wasn't aligned like a pointer when we started to
4718 layout this field, that affects its position. */
4719 tree pos = rli_size_unit_so_far (&old_rli);
4720 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4722 if (abi_version_at_least (9))
4723 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4724 "alignment of %qD increased in %<-fabi-version=9%> "
4725 "(GCC 5.2)", decl);
4726 else
4727 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4728 "of %qD will increase in %<-fabi-version=9%>",
4729 decl);
4731 break;
4733 else
4734 /* There was no conflict. We're done laying out this field. */
4735 break;
4738 /* Now that we know where it will be placed, update its
4739 BINFO_OFFSET. */
4740 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4741 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4742 this point because their BINFO_OFFSET is copied from another
4743 hierarchy. Therefore, we may not need to add the entire
4744 OFFSET. */
4745 propagate_binfo_offsets (binfo,
4746 size_diffop_loc (input_location,
4747 fold_convert (ssizetype, offset),
4748 fold_convert (ssizetype,
4749 BINFO_OFFSET (binfo))));
4752 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4754 static int
4755 empty_base_at_nonzero_offset_p (tree type,
4756 tree offset,
4757 splay_tree /*offsets*/)
4759 return is_empty_class (type) && !integer_zerop (offset);
4762 /* Layout the empty base BINFO. EOC indicates the byte currently just
4763 past the end of the class, and should be correctly aligned for a
4764 class of the type indicated by BINFO; OFFSETS gives the offsets of
4765 the empty bases allocated so far. T is the most derived
4766 type. Return nonzero iff we added it at the end. */
4768 static bool
4769 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4770 splay_tree offsets)
4772 tree alignment;
4773 bool atend = false;
4774 tree binfo = NULL_TREE;
4775 tree decl = NULL_TREE;
4776 tree type;
4777 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4779 binfo = binfo_or_decl;
4780 type = BINFO_TYPE (binfo);
4782 else
4784 decl = binfo_or_decl;
4785 type = TREE_TYPE (decl);
4788 /* On some platforms (ARM), even empty classes will not be
4789 byte-aligned. */
4790 tree eoc = round_up_loc (input_location,
4791 rli_size_unit_so_far (rli),
4792 CLASSTYPE_ALIGN_UNIT (type));
4794 /* This routine should only be used for empty classes. */
4795 gcc_assert (is_empty_class (type));
4797 if (decl && DECL_USER_ALIGN (decl))
4798 alignment = size_int (DECL_ALIGN_UNIT (decl));
4799 else
4800 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4802 /* This is an empty base class. We first try to put it at offset
4803 zero. */
4804 tree offset = size_zero_node;
4805 if (TREE_CODE (rli->t) != UNION_TYPE
4806 && layout_conflict_p (type,
4807 offset,
4808 offsets,
4809 /*vbases_p=*/0))
4811 /* That didn't work. Now, we move forward from the next
4812 available spot in the class. */
4813 atend = true;
4814 offset = eoc;
4815 while (1)
4817 if (!layout_conflict_p (type,
4818 offset,
4819 offsets,
4820 /*vbases_p=*/0))
4821 /* We finally found a spot where there's no overlap. */
4822 break;
4824 /* There's overlap here, too. Bump along to the next spot. */
4825 offset = size_binop (PLUS_EXPR, offset, alignment);
4829 if (decl && DECL_USER_ALIGN (decl))
4831 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4832 if (warn_packed)
4833 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4834 TYPE_USER_ALIGN (rli->t) = 1;
4836 else if (CLASSTYPE_USER_ALIGN (type))
4838 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4839 if (warn_packed)
4840 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4841 TYPE_USER_ALIGN (rli->t) = 1;
4844 if (binfo)
4845 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4846 propagate_binfo_offsets (binfo,
4847 size_diffop (offset, BINFO_OFFSET (binfo)));
4848 else
4850 DECL_FIELD_OFFSET (decl) = offset;
4851 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4852 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4855 return atend;
4858 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4859 fields at NEXT_FIELD, and return it. */
4861 static tree
4862 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4864 /* Create the FIELD_DECL. */
4865 tree basetype = BINFO_TYPE (binfo);
4866 tree as_base = CLASSTYPE_AS_BASE (basetype);
4867 gcc_assert (as_base);
4868 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4870 DECL_ARTIFICIAL (decl) = 1;
4871 DECL_IGNORED_P (decl) = 1;
4872 DECL_FIELD_CONTEXT (decl) = t;
4873 if (is_empty_class (basetype))
4874 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4875 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4876 else
4878 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4879 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4881 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4882 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4883 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4884 DECL_FIELD_IS_BASE (decl) = 1;
4886 if (access == access_private_node)
4887 TREE_PRIVATE (decl) = true;
4888 else if (access == access_protected_node)
4889 TREE_PROTECTED (decl) = true;
4891 /* Add the new FIELD_DECL to the list of fields for T. */
4892 DECL_CHAIN (decl) = *next_field;
4893 *next_field = decl;
4894 next_field = &DECL_CHAIN (decl);
4896 return decl;
4899 /* Layout the base given by BINFO in the class indicated by RLI.
4900 *BASE_ALIGN is a running maximum of the alignments of
4901 any base class. OFFSETS gives the location of empty base
4902 subobjects. T is the most derived type. Return nonzero if the new
4903 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4904 *NEXT_FIELD, unless BINFO is for an empty base class.
4906 Returns the location at which the next field should be inserted. */
4908 static tree *
4909 build_base_field (record_layout_info rli, tree binfo, tree access,
4910 splay_tree offsets, tree *next_field)
4912 tree t = rli->t;
4913 tree basetype = BINFO_TYPE (binfo);
4915 if (!COMPLETE_TYPE_P (basetype))
4916 /* This error is now reported in xref_tag, thus giving better
4917 location information. */
4918 return next_field;
4920 /* Place the base class. */
4921 if (!is_empty_class (basetype))
4923 tree decl;
4925 /* The containing class is non-empty because it has a non-empty
4926 base class. */
4927 CLASSTYPE_EMPTY_P (t) = 0;
4929 /* Create the FIELD_DECL. */
4930 decl = build_base_field_1 (t, binfo, access, next_field);
4932 /* Try to place the field. It may take more than one try if we
4933 have a hard time placing the field without putting two
4934 objects of the same type at the same address. */
4935 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4937 else
4939 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4940 /* A nearly-empty class "has no proper base class that is empty,
4941 not morally virtual, and at an offset other than zero." */
4942 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4944 if (atend)
4945 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4946 /* The check above (used in G++ 3.2) is insufficient because
4947 an empty class placed at offset zero might itself have an
4948 empty base at a nonzero offset. */
4949 else if (walk_subobject_offsets (basetype,
4950 empty_base_at_nonzero_offset_p,
4951 size_zero_node,
4952 /*offsets=*/NULL,
4953 /*max_offset=*/NULL_TREE,
4954 /*vbases_p=*/true))
4955 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4958 /* We used to not create a FIELD_DECL for empty base classes because of
4959 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4960 be a problem anymore. We need them to handle initialization of C++17
4961 aggregate bases. */
4962 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4964 tree decl = build_base_field_1 (t, binfo, access, next_field);
4965 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4966 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4967 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4968 SET_DECL_FIELD_ABI_IGNORED (decl, 1);
4971 /* An empty virtual base causes a class to be non-empty
4972 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4973 here because that was already done when the virtual table
4974 pointer was created. */
4977 /* Record the offsets of BINFO and its base subobjects. */
4978 record_subobject_offsets (binfo, offsets);
4980 return next_field;
4983 /* Layout all of the non-virtual base classes. Record empty
4984 subobjects in OFFSETS. T is the most derived type. Return nonzero
4985 if the type cannot be nearly empty. The fields created
4986 corresponding to the base classes will be inserted at
4987 *NEXT_FIELD. */
4989 static void
4990 build_base_fields (record_layout_info rli,
4991 splay_tree offsets, tree *next_field)
4993 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4994 subobjects. */
4995 tree t = rli->t;
4996 tree binfo = TYPE_BINFO (t);
4997 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4999 /* The primary base class is always allocated first. */
5000 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
5001 if (primary_binfo)
5003 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
5004 base, if it is direct. Indirect base fields are private. */
5005 tree primary_access = access_private_node;
5006 for (int i = 0; i < n_baseclasses; ++i)
5008 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5009 if (base_binfo == primary_binfo)
5011 primary_access = BINFO_BASE_ACCESS (binfo, i);
5012 break;
5015 next_field = build_base_field (rli, primary_binfo,
5016 primary_access,
5017 offsets, next_field);
5020 /* Now allocate the rest of the bases. */
5021 for (int i = 0; i < n_baseclasses; ++i)
5023 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5025 /* The primary base was already allocated above, so we don't
5026 need to allocate it again here. */
5027 if (base_binfo == primary_binfo)
5028 continue;
5030 /* Virtual bases are added at the end (a primary virtual base
5031 will have already been added). */
5032 if (BINFO_VIRTUAL_P (base_binfo))
5033 continue;
5035 next_field = build_base_field (rli, base_binfo,
5036 BINFO_BASE_ACCESS (binfo, i),
5037 offsets, next_field);
5041 /* Go through the TYPE_FIELDS of T issuing any appropriate
5042 diagnostics, figuring out which methods override which other
5043 methods, and so forth. */
5045 static void
5046 check_methods (tree t)
5048 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5049 if (DECL_DECLARES_FUNCTION_P (x))
5051 check_for_override (x, t);
5053 if (DECL_PURE_VIRTUAL_P (x)
5054 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
5055 error ("initializer specified for non-virtual method %q+D", x);
5056 /* The name of the field is the original field name
5057 Save this in auxiliary field for later overloading. */
5058 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
5060 TYPE_POLYMORPHIC_P (t) = 1;
5061 if (DECL_PURE_VIRTUAL_P (x))
5062 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
5065 if (!DECL_VIRTUAL_P (x)
5066 && lookup_attribute ("transaction_safe_dynamic",
5067 DECL_ATTRIBUTES (x)))
5068 error_at (DECL_SOURCE_LOCATION (x),
5069 "%<transaction_safe_dynamic%> may only be specified for "
5070 "a virtual function");
5073 /* Check whether the eligible special member functions (P0848) are
5074 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
5075 has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
5076 ineligible overloads, which is why this needs to be separate from the loop
5077 above. */
5079 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5081 if (TREE_CODE (dtor) == OVERLOAD)
5083 /* P0848: At the end of the definition of a class, overload
5084 resolution is performed among the prospective destructors declared
5085 in that class with an empty argument list to select the destructor
5086 for the class, also known as the selected destructor. The program
5087 is ill-formed if overload resolution fails. */
5088 int viable = 0;
5089 for (tree fn : ovl_range (dtor))
5090 if (constraints_satisfied_p (fn))
5091 ++viable;
5092 gcc_checking_assert (viable != 1);
5094 auto_diagnostic_group d;
5095 if (viable == 0)
5096 error_at (location_of (t), "no viable destructor for %qT", t);
5097 else
5098 error_at (location_of (t), "destructor for %qT is ambiguous", t);
5099 print_candidates (dtor);
5101 /* Arbitrarily prune the overload set to a single function for
5102 sake of error recovery. */
5103 tree *slot = find_member_slot (t, dtor_identifier);
5104 *slot = get_first_fn (dtor);
5106 else if (user_provided_p (dtor))
5107 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
5110 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5112 if (!user_provided_p (fn))
5113 /* Might be trivial. */;
5114 else if (TREE_CODE (fn) == TEMPLATE_DECL)
5115 /* Templates are never special members. */;
5116 else if (copy_fn_p (fn)
5117 && constraints_satisfied_p (fn))
5118 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
5119 else if (move_fn_p (fn)
5120 && constraints_satisfied_p (fn))
5121 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
5124 for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
5126 if (!user_provided_p (fn))
5127 /* Might be trivial. */;
5128 else if (TREE_CODE (fn) == TEMPLATE_DECL)
5129 /* Templates are never special members. */;
5130 else if (copy_fn_p (fn)
5131 && constraints_satisfied_p (fn))
5132 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
5133 else if (move_fn_p (fn)
5134 && constraints_satisfied_p (fn))
5135 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
5139 /* FN is constructor, destructor or operator function. Clone the
5140 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
5141 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
5143 static tree
5144 copy_fndecl_with_name (tree fn, tree name, tree_code code,
5145 bool need_vtt_parm_p, bool omit_inherited_parms_p)
5147 /* Copy the function. */
5148 tree clone = copy_decl (fn);
5149 /* Reset the function name. */
5150 DECL_NAME (clone) = name;
5152 if (flag_concepts)
5153 /* Clone constraints. */
5154 if (tree ci = get_constraints (fn))
5155 set_constraints (clone, copy_node (ci));
5157 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
5158 /* There's no pending inline data for this function. */
5159 DECL_PENDING_INLINE_INFO (clone) = NULL;
5160 DECL_PENDING_INLINE_P (clone) = 0;
5162 if (name == base_dtor_identifier)
5164 /* The base-class destructor is not virtual. */
5165 DECL_VIRTUAL_P (clone) = 0;
5166 DECL_VINDEX (clone) = NULL_TREE;
5168 else if (code != ERROR_MARK)
5170 /* Set the operator code. */
5171 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
5172 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
5174 /* The operator could be virtual. */
5175 if (DECL_VIRTUAL_P (clone))
5176 IDENTIFIER_VIRTUAL_P (name) = true;
5179 if (omit_inherited_parms_p)
5180 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
5182 /* If there was an in-charge parameter, drop it from the function
5183 type. */
5184 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5186 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5187 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
5188 /* Skip the `this' parameter. */
5189 parmtypes = TREE_CHAIN (parmtypes);
5190 /* Skip the in-charge parameter. */
5191 parmtypes = TREE_CHAIN (parmtypes);
5192 /* And the VTT parm, in a complete [cd]tor. */
5193 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
5194 parmtypes = TREE_CHAIN (parmtypes);
5195 if (omit_inherited_parms_p)
5197 /* If we're omitting inherited parms, that just leaves the VTT. */
5198 gcc_assert (need_vtt_parm_p);
5199 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
5201 TREE_TYPE (clone)
5202 = build_method_type_directly (basetype,
5203 TREE_TYPE (TREE_TYPE (clone)),
5204 parmtypes);
5205 TREE_TYPE (clone)
5206 = cp_build_type_attribute_variant (TREE_TYPE (clone),
5207 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
5208 TREE_TYPE (clone)
5209 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
5212 /* Copy the function parameters. */
5213 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
5215 /* Remove the in-charge parameter. */
5216 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5218 DECL_CHAIN (DECL_ARGUMENTS (clone))
5219 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5220 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
5223 /* And the VTT parm, in a complete [cd]tor. */
5224 if (DECL_HAS_VTT_PARM_P (fn))
5226 if (need_vtt_parm_p)
5227 DECL_HAS_VTT_PARM_P (clone) = 1;
5228 else
5230 DECL_CHAIN (DECL_ARGUMENTS (clone))
5231 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5232 DECL_HAS_VTT_PARM_P (clone) = 0;
5236 /* A base constructor inheriting from a virtual base doesn't get the
5237 arguments. */
5238 if (omit_inherited_parms_p)
5239 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5241 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5243 DECL_CONTEXT (parms) = clone;
5244 cxx_dup_lang_specific_decl (parms);
5247 /* Create the RTL for this function. */
5248 SET_DECL_RTL (clone, NULL);
5250 /* Regardless of the current scope, this is a member function, so
5251 not at namespace scope. */
5252 rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5254 return clone;
5257 /* FN is an operator function, create a variant for CODE. */
5259 tree
5260 copy_operator_fn (tree fn, tree_code code)
5262 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5263 code, false, false);
5266 /* FN is a constructor or destructor. Clone the declaration to create
5267 a specialized in-charge or not-in-charge version, as indicated by
5268 NAME. */
5270 static tree
5271 build_clone (tree fn, tree name, bool need_vtt_parm_p,
5272 bool omit_inherited_parms_p)
5274 tree clone;
5276 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5277 if (TREE_CODE (fn) == TEMPLATE_DECL)
5279 clone = copy_decl (fn);
5280 DECL_NAME (clone) = name;
5282 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5283 need_vtt_parm_p, omit_inherited_parms_p);
5284 DECL_TEMPLATE_RESULT (clone) = result;
5286 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5287 DECL_TI_TEMPLATE (result) = clone;
5289 TREE_TYPE (clone) = TREE_TYPE (result);
5291 else
5293 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5294 need_vtt_parm_p, omit_inherited_parms_p);
5295 DECL_CLONED_FUNCTION (clone) = fn;
5297 maybe_prepare_return_this (clone);
5300 /* Remember where this function came from. */
5301 DECL_ABSTRACT_ORIGIN (clone) = fn;
5303 /* Make it easy to find the CLONE given the FN. Note the
5304 template_result of a template will be chained this way too. */
5305 DECL_CHAIN (clone) = DECL_CHAIN (fn);
5306 DECL_CHAIN (fn) = clone;
5308 return clone;
5311 /* Build the clones of FN, return the number of clones built. These
5312 will be inserted onto DECL_CHAIN of FN. */
5314 void
5315 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5316 bool update_methods)
5318 unsigned count = 0;
5320 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5322 /* For each constructor, we need two variants: an in-charge version
5323 and a not-in-charge version. */
5324 build_clone (fn, complete_ctor_identifier, false, false);
5325 build_clone (fn, base_ctor_identifier, needs_vtt_p,
5326 base_omits_inherited_p);
5327 count += 2;
5329 else
5331 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5333 /* For each destructor, we need three variants: an in-charge
5334 version, a not-in-charge version, and an in-charge deleting
5335 version. We clone the deleting version first because that
5336 means it will go second on the TYPE_FIELDS list -- and that
5337 corresponds to the correct layout order in the virtual
5338 function table.
5340 For a non-virtual destructor, we do not build a deleting
5341 destructor. */
5342 if (DECL_VIRTUAL_P (fn))
5344 build_clone (fn, deleting_dtor_identifier, false, false);
5345 count++;
5347 build_clone (fn, complete_dtor_identifier, false, false);
5348 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5349 count += 2;
5352 /* The original is now an abstract function that is never
5353 emitted. */
5354 DECL_ABSTRACT_P (fn) = true;
5356 if (update_methods)
5357 for (tree clone = fn; count--;)
5359 clone = DECL_CHAIN (clone);
5360 add_method (DECL_CONTEXT (clone), clone, false);
5364 /* Produce declarations for all appropriate clones of FN. If
5365 UPDATE_METHODS is true, the clones are added to the
5366 CLASSTYPE_MEMBER_VEC. */
5368 void
5369 clone_cdtor (tree fn, bool update_methods)
5371 /* Avoid inappropriate cloning. */
5372 if (DECL_CHAIN (fn)
5373 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5374 return;
5376 /* Base cdtors need a vtt parm if there are virtual bases. */
5377 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5379 /* Base ctor omits inherited parms it needs a vttparm and inherited
5380 from a virtual nase ctor. */
5381 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5382 && base_ctor_omit_inherited_parms (fn));
5384 build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5387 /* DECL is an in charge constructor, which is being defined. This will
5388 have had an in class declaration, from whence clones were
5389 declared. An out-of-class definition can specify additional default
5390 arguments. As it is the clones that are involved in overload
5391 resolution, we must propagate the information from the DECL to its
5392 clones. */
5394 void
5395 adjust_clone_args (tree decl)
5397 tree clone;
5399 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5400 clone = DECL_CHAIN (clone))
5402 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5403 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5404 tree decl_parms, clone_parms;
5406 /* Skip the 'this' parameter. */
5407 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5408 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5410 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5411 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5412 if (DECL_HAS_VTT_PARM_P (decl))
5413 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5415 clone_parms = orig_clone_parms;
5416 if (DECL_HAS_VTT_PARM_P (clone))
5417 clone_parms = TREE_CHAIN (clone_parms);
5419 for (decl_parms = orig_decl_parms; decl_parms;
5420 decl_parms = TREE_CHAIN (decl_parms),
5421 clone_parms = TREE_CHAIN (clone_parms))
5423 if (clone_parms == void_list_node)
5425 gcc_assert (decl_parms == clone_parms
5426 || ctor_omit_inherited_parms (clone));
5427 break;
5430 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5431 TREE_VALUE (clone_parms)));
5433 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5435 /* A default parameter has been added. Adjust the
5436 clone's parameters. */
5437 clone_parms = orig_decl_parms;
5439 if (DECL_HAS_VTT_PARM_P (clone))
5441 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5442 TREE_VALUE (orig_clone_parms),
5443 clone_parms);
5444 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5447 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5448 tree type
5449 = build_method_type_directly (basetype,
5450 TREE_TYPE (TREE_TYPE (clone)),
5451 clone_parms);
5452 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5453 type = cp_build_type_attribute_variant (type, attrs);
5454 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5455 TREE_TYPE (clone) = type;
5457 clone_parms = NULL_TREE;
5458 break;
5461 gcc_assert (!clone_parms || clone_parms == void_list_node);
5465 /* For each of the constructors and destructors in T, create an
5466 in-charge and not-in-charge variant. */
5468 static void
5469 clone_constructors_and_destructors (tree t)
5471 /* We do not need to propagate the usingness to the clone, at this
5472 point that is not needed. */
5473 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5474 clone_cdtor (fn, /*update_methods=*/true);
5476 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5477 clone_cdtor (dtor, /*update_methods=*/true);
5480 /* Deduce noexcept for a destructor DTOR. */
5482 void
5483 deduce_noexcept_on_destructor (tree dtor)
5485 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5486 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5487 noexcept_deferred_spec);
5490 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5491 of TYPE for virtual functions which FNDECL overrides. Return a
5492 mask of the tm attributes found therein. */
5494 static int
5495 look_for_tm_attr_overrides (tree type, tree fndecl)
5497 tree binfo = TYPE_BINFO (type);
5498 tree base_binfo;
5499 int ix, found = 0;
5501 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5503 tree o, basetype = BINFO_TYPE (base_binfo);
5505 if (!TYPE_POLYMORPHIC_P (basetype))
5506 continue;
5508 o = look_for_overrides_here (basetype, fndecl);
5509 if (o)
5511 if (lookup_attribute ("transaction_safe_dynamic",
5512 DECL_ATTRIBUTES (o)))
5513 /* transaction_safe_dynamic is not inherited. */;
5514 else
5515 found |= tm_attr_to_mask (find_tm_attribute
5516 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5518 else
5519 found |= look_for_tm_attr_overrides (basetype, fndecl);
5522 return found;
5525 /* Subroutine of set_method_tm_attributes. Handle the checks and
5526 inheritance for one virtual method FNDECL. */
5528 static void
5529 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5531 tree tm_attr;
5532 int found, have;
5534 found = look_for_tm_attr_overrides (type, fndecl);
5536 /* If FNDECL doesn't actually override anything (i.e. T is the
5537 class that first declares FNDECL virtual), then we're done. */
5538 if (found == 0)
5539 return;
5541 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5542 have = tm_attr_to_mask (tm_attr);
5544 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5545 tm_pure must match exactly, otherwise no weakening of
5546 tm_safe > tm_callable > nothing. */
5547 /* ??? The tm_pure attribute didn't make the transition to the
5548 multivendor language spec. */
5549 if (have == TM_ATTR_PURE)
5551 if (found != TM_ATTR_PURE)
5553 found &= -found;
5554 goto err_override;
5557 /* If the overridden function is tm_pure, then FNDECL must be. */
5558 else if (found == TM_ATTR_PURE && tm_attr)
5559 goto err_override;
5560 /* Look for base class combinations that cannot be satisfied. */
5561 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5563 found &= ~TM_ATTR_PURE;
5564 found &= -found;
5565 error_at (DECL_SOURCE_LOCATION (fndecl),
5566 "method overrides both %<transaction_pure%> and %qE methods",
5567 tm_mask_to_attr (found));
5569 /* If FNDECL did not declare an attribute, then inherit the most
5570 restrictive one. */
5571 else if (tm_attr == NULL)
5573 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5575 /* Otherwise validate that we're not weaker than a function
5576 that is being overridden. */
5577 else
5579 found &= -found;
5580 if (found <= TM_ATTR_CALLABLE && have > found)
5581 goto err_override;
5583 return;
5585 err_override:
5586 error_at (DECL_SOURCE_LOCATION (fndecl),
5587 "method declared %qE overriding %qE method",
5588 tm_attr, tm_mask_to_attr (found));
5591 /* For each of the methods in T, propagate a class-level tm attribute. */
5593 static void
5594 set_method_tm_attributes (tree t)
5596 tree class_tm_attr, fndecl;
5598 /* Don't bother collecting tm attributes if transactional memory
5599 support is not enabled. */
5600 if (!flag_tm)
5601 return;
5603 /* Process virtual methods first, as they inherit directly from the
5604 base virtual function and also require validation of new attributes. */
5605 if (TYPE_CONTAINS_VPTR_P (t))
5607 tree vchain;
5608 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5609 vchain = TREE_CHAIN (vchain))
5611 fndecl = BV_FN (vchain);
5612 if (DECL_THUNK_P (fndecl))
5613 fndecl = THUNK_TARGET (fndecl);
5614 set_one_vmethod_tm_attributes (t, fndecl);
5618 /* If the class doesn't have an attribute, nothing more to do. */
5619 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5620 if (class_tm_attr == NULL)
5621 return;
5623 /* Any method that does not yet have a tm attribute inherits
5624 the one from the class. */
5625 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5626 if (DECL_DECLARES_FUNCTION_P (fndecl)
5627 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5628 apply_tm_attr (fndecl, class_tm_attr);
5631 /* Returns true if FN is a default constructor. */
5633 bool
5634 default_ctor_p (const_tree fn)
5636 return (DECL_CONSTRUCTOR_P (fn)
5637 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5640 /* Returns true iff class T has a user-provided constructor that can be called
5641 with more than zero arguments. */
5643 bool
5644 type_has_user_nondefault_constructor (tree t)
5646 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5647 return false;
5649 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5651 if (user_provided_p (fn)
5652 && (TREE_CODE (fn) == TEMPLATE_DECL
5653 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5654 != NULL_TREE)))
5655 return true;
5658 return false;
5661 /* Returns the defaulted constructor if T has one. Otherwise, returns
5662 NULL_TREE. */
5664 tree
5665 in_class_defaulted_default_constructor (tree t)
5667 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5668 return NULL_TREE;
5670 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5672 tree fn = *iter;
5674 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5675 && default_ctor_p (fn))
5676 return fn;
5679 return NULL_TREE;
5682 /* Returns true iff FN is a user-provided function, i.e. user-declared
5683 and not defaulted at its first declaration. */
5685 bool
5686 user_provided_p (tree fn)
5688 fn = STRIP_TEMPLATE (fn);
5689 return (!DECL_ARTIFICIAL (fn)
5690 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5691 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5694 /* Returns true iff class T has a user-provided constructor. */
5696 bool
5697 type_has_user_provided_constructor (tree t)
5699 if (!CLASS_TYPE_P (t))
5700 return false;
5702 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5703 return false;
5705 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5706 if (user_provided_p (*iter))
5707 return true;
5709 return false;
5712 /* Returns true iff class T has a user-provided or explicit constructor. */
5714 bool
5715 type_has_user_provided_or_explicit_constructor (tree t)
5717 if (!CLASS_TYPE_P (t))
5718 return false;
5720 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5721 return false;
5723 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5725 tree fn = *iter;
5726 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5727 return true;
5730 return false;
5733 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5734 declared or explicitly defaulted in the class body) default
5735 constructor. */
5737 bool
5738 type_has_non_user_provided_default_constructor (tree t)
5740 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5741 return false;
5742 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5743 return true;
5745 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5747 tree fn = *iter;
5748 if (TREE_CODE (fn) == FUNCTION_DECL
5749 && default_ctor_p (fn)
5750 && !user_provided_p (fn))
5751 return true;
5754 return false;
5757 /* TYPE is being used as a virtual base, and has a non-trivial move
5758 assignment. Return true if this is due to there being a user-provided
5759 move assignment in TYPE or one of its subobjects; if there isn't, then
5760 multiple move assignment can't cause any harm. */
5762 bool
5763 vbase_has_user_provided_move_assign (tree type)
5765 /* Does the type itself have a user-provided move assignment operator? */
5766 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5767 for (ovl_iterator iter (get_class_binding_direct
5768 (type, assign_op_identifier));
5769 iter; ++iter)
5770 if (user_provided_p (*iter) && move_fn_p (*iter))
5771 return true;
5773 /* Do any of its bases? */
5774 tree binfo = TYPE_BINFO (type);
5775 tree base_binfo;
5776 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5777 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5778 return true;
5780 /* Or non-static data members? */
5781 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5783 if (TREE_CODE (field) == FIELD_DECL
5784 && CLASS_TYPE_P (TREE_TYPE (field))
5785 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5786 return true;
5789 /* Seems not. */
5790 return false;
5793 /* If default-initialization leaves part of TYPE uninitialized, returns
5794 a DECL for the field or TYPE itself (DR 253). */
5796 tree
5797 default_init_uninitialized_part (tree type)
5799 tree t, r, binfo;
5800 int i;
5802 type = strip_array_types (type);
5803 if (!CLASS_TYPE_P (type))
5804 return type;
5805 if (!type_has_non_user_provided_default_constructor (type))
5806 return NULL_TREE;
5807 for (binfo = TYPE_BINFO (type), i = 0;
5808 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5810 r = default_init_uninitialized_part (BINFO_TYPE (t));
5811 if (r)
5812 return r;
5814 for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5815 t = next_aggregate_field (DECL_CHAIN (t)))
5816 if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5818 r = default_init_uninitialized_part (TREE_TYPE (t));
5819 if (r)
5820 return DECL_P (r) ? r : t;
5823 return NULL_TREE;
5826 /* Returns true iff for class T, a trivial synthesized default constructor
5827 would be constexpr. */
5829 bool
5830 trivial_default_constructor_is_constexpr (tree t)
5832 /* A defaulted trivial default constructor is constexpr
5833 if there is nothing to initialize. */
5834 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5835 /* A class with a vptr doesn't have a trivial default ctor.
5836 In C++20, a class can have transient uninitialized members, e.g.:
5838 struct S { int i; constexpr S() = default; };
5840 should work. */
5841 return (cxx_dialect >= cxx20
5842 || is_really_empty_class (t, /*ignore_vptr*/true));
5845 /* Returns true iff class T has a constexpr default constructor. */
5847 bool
5848 type_has_constexpr_default_constructor (tree t)
5850 tree fns;
5852 if (!CLASS_TYPE_P (t))
5854 /* The caller should have stripped an enclosing array. */
5855 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5856 return false;
5858 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5860 if (!TYPE_HAS_COMPLEX_DFLT (t))
5861 return trivial_default_constructor_is_constexpr (t);
5862 /* Non-trivial, we need to check subobject constructors. */
5863 lazily_declare_fn (sfk_constructor, t);
5865 fns = locate_ctor (t);
5866 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5869 /* Returns true iff class T has a constexpr default constructor or has an
5870 implicitly declared default constructor that we can't tell if it's constexpr
5871 without forcing a lazy declaration (which might cause undesired
5872 instantiations). */
5874 static bool
5875 type_maybe_constexpr_default_constructor (tree t)
5877 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5878 && TYPE_HAS_COMPLEX_DFLT (t))
5879 /* Assume it's constexpr. */
5880 return true;
5881 return type_has_constexpr_default_constructor (t);
5884 /* Returns true iff class T has a constexpr destructor. */
5886 bool
5887 type_has_constexpr_destructor (tree t)
5889 tree fns;
5891 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5892 /* Non-trivial, we need to check subobject destructors. */
5893 lazily_declare_fn (sfk_destructor, t);
5894 fns = CLASSTYPE_DESTRUCTOR (t);
5895 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5898 /* Returns true iff class T has a constexpr destructor or has an
5899 implicitly declared destructor that we can't tell if it's constexpr
5900 without forcing a lazy declaration (which might cause undesired
5901 instantiations). */
5903 static bool
5904 type_maybe_constexpr_destructor (tree t)
5906 /* Until C++20, only trivial destruction is constexpr. */
5907 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5908 return true;
5909 if (cxx_dialect < cxx20)
5910 return false;
5911 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5912 /* Assume it's constexpr. */
5913 return true;
5914 tree fn = CLASSTYPE_DESTRUCTOR (t);
5915 return (fn && maybe_constexpr_fn (fn));
5918 /* Returns true iff class TYPE has a virtual destructor. */
5920 bool
5921 type_has_virtual_destructor (tree type)
5923 tree dtor;
5925 if (!NON_UNION_CLASS_TYPE_P (type))
5926 return false;
5928 gcc_assert (COMPLETE_TYPE_P (type));
5929 dtor = CLASSTYPE_DESTRUCTOR (type);
5930 return (dtor && DECL_VIRTUAL_P (dtor));
5933 /* True iff class TYPE has a non-deleted trivial default
5934 constructor. */
5936 bool type_has_non_deleted_trivial_default_ctor (tree type)
5938 return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
5941 /* Returns true iff T, a class, has a move-assignment or
5942 move-constructor. Does not lazily declare either.
5943 If USER_P is false, any move function will do. If it is true, the
5944 move function must be user-declared.
5946 Note that user-declared here is different from "user-provided",
5947 which doesn't include functions that are defaulted in the
5948 class. */
5950 bool
5951 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5953 gcc_assert (user_p
5954 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5955 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5957 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5958 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5959 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5960 return true;
5962 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5963 for (ovl_iterator iter (get_class_binding_direct
5964 (t, assign_op_identifier));
5965 iter; ++iter)
5966 if ((!user_p || !DECL_ARTIFICIAL (*iter))
5967 && DECL_CONTEXT (*iter) == t
5968 && move_fn_p (*iter))
5969 return true;
5971 return false;
5974 /* True iff T has a move constructor that is not deleted. */
5976 bool
5977 classtype_has_non_deleted_move_ctor (tree t)
5979 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5980 lazily_declare_fn (sfk_move_constructor, t);
5981 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5982 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5983 return true;
5984 return false;
5987 /* If T, a class, has a user-provided copy constructor, copy assignment
5988 operator, or destructor, returns that function. Otherwise, null. */
5990 tree
5991 classtype_has_depr_implicit_copy (tree t)
5993 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5994 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5996 tree fn = *iter;
5997 if (user_provided_p (fn) && copy_fn_p (fn))
5998 return fn;
6001 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
6002 for (ovl_iterator iter (get_class_binding_direct
6003 (t, assign_op_identifier));
6004 iter; ++iter)
6006 tree fn = *iter;
6007 if (DECL_CONTEXT (fn) == t
6008 && user_provided_p (fn) && copy_fn_p (fn))
6009 return fn;
6012 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
6014 tree fn = CLASSTYPE_DESTRUCTOR (t);
6015 if (user_provided_p (fn))
6016 return fn;
6019 return NULL_TREE;
6022 /* True iff T has a member or friend declaration of operator OP. */
6024 bool
6025 classtype_has_op (tree t, tree_code op)
6027 tree name = ovl_op_identifier (op);
6028 if (get_class_binding (t, name))
6029 return true;
6030 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6031 if (FRIEND_NAME (f) == name)
6032 return true;
6033 return false;
6037 /* If T has a defaulted member or friend declaration of OP, return it. */
6039 tree
6040 classtype_has_defaulted_op (tree t, tree_code op)
6042 tree name = ovl_op_identifier (op);
6043 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
6045 tree fn = *oi;
6046 if (DECL_DEFAULTED_FN (fn))
6047 return fn;
6049 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6050 if (FRIEND_NAME (f) == name)
6051 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
6053 tree fn = TREE_VALUE (l);
6054 if (DECL_DEFAULTED_FN (fn))
6055 return fn;
6057 return NULL_TREE;
6060 /* Nonzero if we need to build up a constructor call when initializing an
6061 object of this class, either because it has a user-declared constructor
6062 or because it doesn't have a default constructor (so we need to give an
6063 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
6064 what you care about is whether or not an object can be produced by a
6065 constructor (e.g. so we don't set TREE_READONLY on const variables of
6066 such type); use this function when what you care about is whether or not
6067 to try to call a constructor to create an object. The latter case is
6068 the former plus some cases of constructors that cannot be called. */
6070 bool
6071 type_build_ctor_call (tree t)
6073 tree inner;
6074 if (TYPE_NEEDS_CONSTRUCTING (t))
6075 return true;
6076 inner = strip_array_types (t);
6077 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
6078 return false;
6079 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
6080 return true;
6081 if (cxx_dialect < cxx11)
6082 return false;
6083 /* A user-declared constructor might be private, and a constructor might
6084 be trivial but deleted. */
6085 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
6086 iter; ++iter)
6088 tree fn = *iter;
6089 if (!DECL_ARTIFICIAL (fn)
6090 || TREE_DEPRECATED (fn)
6091 || TREE_UNAVAILABLE (fn)
6092 || DECL_DELETED_FN (fn))
6093 return true;
6095 return false;
6098 /* Like type_build_ctor_call, but for destructors. */
6100 bool
6101 type_build_dtor_call (tree t)
6103 tree inner;
6104 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6105 return true;
6106 inner = strip_array_types (t);
6107 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
6108 || !COMPLETE_TYPE_P (inner))
6109 return false;
6110 if (cxx_dialect < cxx11)
6111 return false;
6112 /* A user-declared destructor might be private, and a destructor might
6113 be trivial but deleted. */
6114 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
6115 iter; ++iter)
6117 tree fn = *iter;
6118 if (!DECL_ARTIFICIAL (fn)
6119 || TREE_DEPRECATED (fn)
6120 || TREE_UNAVAILABLE (fn)
6121 || DECL_DELETED_FN (fn))
6122 return true;
6124 return false;
6127 /* Returns TRUE iff we need a cookie when dynamically allocating an
6128 array whose elements have the indicated class TYPE. */
6130 static bool
6131 type_requires_array_cookie (tree type)
6133 tree fns;
6134 bool has_two_argument_delete_p = false;
6136 gcc_assert (CLASS_TYPE_P (type));
6138 /* If there's a non-trivial destructor, we need a cookie. In order
6139 to iterate through the array calling the destructor for each
6140 element, we'll have to know how many elements there are. */
6141 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6142 return true;
6144 /* If the usual deallocation function is a two-argument whose second
6145 argument is of type `size_t', then we have to pass the size of
6146 the array to the deallocation function, so we will need to store
6147 a cookie. */
6148 fns = lookup_fnfields (TYPE_BINFO (type),
6149 ovl_op_identifier (false, VEC_DELETE_EXPR),
6150 /*protect=*/0, tf_warning_or_error);
6151 /* If there are no `operator []' members, or the lookup is
6152 ambiguous, then we don't need a cookie. */
6153 if (!fns || fns == error_mark_node)
6154 return false;
6155 /* Loop through all of the functions. */
6156 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
6158 tree fn = *iter;
6160 /* See if this function is a one-argument delete function. If
6161 it is, then it will be the usual deallocation function. */
6162 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
6163 if (second_parm == void_list_node)
6164 return false;
6165 /* Do not consider this function if its second argument is an
6166 ellipsis. */
6167 if (!second_parm)
6168 continue;
6169 /* Otherwise, if we have a two-argument function and the second
6170 argument is `size_t', it will be the usual deallocation
6171 function -- unless there is one-argument function, too. */
6172 if (TREE_CHAIN (second_parm) == void_list_node
6173 && same_type_p (TREE_VALUE (second_parm), size_type_node))
6174 has_two_argument_delete_p = true;
6177 return has_two_argument_delete_p;
6180 /* Finish computing the `literal type' property of class type T.
6182 At this point, we have already processed base classes and
6183 non-static data members. We need to check whether the copy
6184 constructor is trivial, the destructor is trivial, and there
6185 is a trivial default constructor or at least one constexpr
6186 constructor other than the copy constructor. */
6188 static void
6189 finalize_literal_type_property (tree t)
6191 tree fn;
6193 if (cxx_dialect < cxx11)
6194 CLASSTYPE_LITERAL_P (t) = false;
6195 else if (CLASSTYPE_LITERAL_P (t)
6196 && !type_maybe_constexpr_destructor (t))
6197 CLASSTYPE_LITERAL_P (t) = false;
6198 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
6199 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
6200 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6201 && CLASSTYPE_NON_AGGREGATE (t)
6202 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6203 CLASSTYPE_LITERAL_P (t) = false;
6205 /* C++14 DR 1684 removed this restriction. */
6206 if (cxx_dialect < cxx14
6207 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
6208 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6209 if (TREE_CODE (fn) == FUNCTION_DECL
6210 && DECL_DECLARED_CONSTEXPR_P (fn)
6211 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
6212 && !DECL_CONSTRUCTOR_P (fn))
6214 DECL_DECLARED_CONSTEXPR_P (fn) = false;
6215 if (!DECL_GENERATED_P (fn))
6217 auto_diagnostic_group d;
6218 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
6219 "enclosing class of %<constexpr%> non-static "
6220 "member function %q+#D is not a literal type", fn))
6221 explain_non_literal_class (t);
6226 /* T is a non-literal type used in a context which requires a constant
6227 expression. Explain why it isn't literal. */
6229 void
6230 explain_non_literal_class (tree t)
6232 static hash_set<tree> *diagnosed;
6234 if (!CLASS_TYPE_P (t))
6235 return;
6236 t = TYPE_MAIN_VARIANT (t);
6238 if (diagnosed == NULL)
6239 diagnosed = new hash_set<tree>;
6240 if (diagnosed->add (t))
6241 /* Already explained. */
6242 return;
6244 auto_diagnostic_group d;
6245 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6246 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
6247 inform (UNKNOWN_LOCATION,
6248 " %qT is a closure type, which is only literal in "
6249 "C++17 and later", t);
6250 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6251 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6252 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6253 && !type_maybe_constexpr_destructor (t))
6254 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6256 else if (CLASSTYPE_NON_AGGREGATE (t)
6257 && !TYPE_HAS_TRIVIAL_DFLT (t)
6258 && !LAMBDA_TYPE_P (t)
6259 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6261 inform (UNKNOWN_LOCATION,
6262 " %q+T is not an aggregate, does not have a trivial "
6263 "default constructor, and has no %<constexpr%> constructor that "
6264 "is not a copy or move constructor", t);
6265 if (type_has_non_user_provided_default_constructor (t))
6266 /* Note that we can't simply call locate_ctor because when the
6267 constructor is deleted it just returns NULL_TREE. */
6268 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6270 tree fn = *iter;
6271 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6273 parms = skip_artificial_parms_for (fn, parms);
6275 if (sufficient_parms_p (parms))
6277 if (DECL_DELETED_FN (fn))
6278 maybe_explain_implicit_delete (fn);
6279 else
6280 explain_invalid_constexpr_fn (fn);
6281 break;
6285 else
6287 tree binfo, base_binfo, field; int i;
6288 for (binfo = TYPE_BINFO (t), i = 0;
6289 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6291 tree basetype = TREE_TYPE (base_binfo);
6292 if (!CLASSTYPE_LITERAL_P (basetype))
6294 inform (UNKNOWN_LOCATION,
6295 " base class %qT of %q+T is non-literal",
6296 basetype, t);
6297 explain_non_literal_class (basetype);
6298 return;
6301 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6303 tree ftype;
6304 if (TREE_CODE (field) != FIELD_DECL)
6305 continue;
6306 ftype = TREE_TYPE (field);
6307 if (!literal_type_p (ftype))
6309 inform (DECL_SOURCE_LOCATION (field),
6310 " non-static data member %qD has non-literal type",
6311 field);
6312 if (CLASS_TYPE_P (ftype))
6313 explain_non_literal_class (ftype);
6315 if (CP_TYPE_VOLATILE_P (ftype))
6316 inform (DECL_SOURCE_LOCATION (field),
6317 " non-static data member %qD has volatile type", field);
6322 /* Check the validity of the bases and members declared in T. Add any
6323 implicitly-generated functions (like copy-constructors and
6324 assignment operators). Compute various flag bits (like
6325 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6326 level: i.e., independently of the ABI in use. */
6328 static void
6329 check_bases_and_members (tree t)
6331 /* Nonzero if the implicitly generated copy constructor should take
6332 a non-const reference argument. */
6333 int cant_have_const_ctor;
6334 /* Nonzero if the implicitly generated assignment operator
6335 should take a non-const reference argument. */
6336 int no_const_asn_ref;
6337 tree access_decls;
6338 bool saved_complex_asn_ref;
6339 bool saved_nontrivial_dtor;
6340 tree fn;
6342 /* By default, we use const reference arguments and generate default
6343 constructors. */
6344 cant_have_const_ctor = 0;
6345 no_const_asn_ref = 0;
6347 /* Check all the base-classes and set FMEM members to point to arrays
6348 of potential interest. */
6349 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6351 /* Deduce noexcept on destructor. This needs to happen after we've set
6352 triviality flags appropriately for our bases, and before checking
6353 overriden virtual functions via check_methods. */
6354 if (cxx_dialect >= cxx11)
6355 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6356 for (tree fn : ovl_range (dtor))
6357 deduce_noexcept_on_destructor (fn);
6359 /* Check all the method declarations. */
6360 check_methods (t);
6362 /* Save the initial values of these flags which only indicate whether
6363 or not the class has user-provided functions. As we analyze the
6364 bases and members we can set these flags for other reasons. */
6365 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6366 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6368 /* Check all the data member declarations. We cannot call
6369 check_field_decls until we have called check_bases check_methods,
6370 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6371 being set appropriately. */
6372 check_field_decls (t, &access_decls,
6373 &cant_have_const_ctor,
6374 &no_const_asn_ref);
6376 /* A nearly-empty class has to be vptr-containing; a nearly empty
6377 class contains just a vptr. */
6378 if (!TYPE_CONTAINS_VPTR_P (t))
6379 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6381 /* Do some bookkeeping that will guide the generation of implicitly
6382 declared member functions. */
6383 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6384 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6385 /* We need to call a constructor for this class if it has a
6386 user-provided constructor, or if the default constructor is going
6387 to initialize the vptr. (This is not an if-and-only-if;
6388 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6389 themselves need constructing.) */
6390 TYPE_NEEDS_CONSTRUCTING (t)
6391 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6392 /* [dcl.init.aggr]
6394 An aggregate is an array or a class with no user-provided
6395 constructors ... and no virtual functions.
6397 Again, other conditions for being an aggregate are checked
6398 elsewhere. */
6399 CLASSTYPE_NON_AGGREGATE (t)
6400 |= ((cxx_dialect < cxx20
6401 ? type_has_user_provided_or_explicit_constructor (t)
6402 : TYPE_HAS_USER_CONSTRUCTOR (t))
6403 || TYPE_POLYMORPHIC_P (t));
6404 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6405 retain the old definition internally for ABI reasons. */
6406 CLASSTYPE_NON_LAYOUT_POD_P (t)
6407 |= (CLASSTYPE_NON_AGGREGATE (t)
6408 || saved_nontrivial_dtor || saved_complex_asn_ref);
6409 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6410 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6411 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6412 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6414 /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6415 if (CLASSTYPE_NON_POD_AGGREGATE (t))
6417 if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6418 /* It's non-POD for another reason. */
6419 CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6420 else if (abi_version_at_least (17))
6421 CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6424 /* If the only explicitly declared default constructor is user-provided,
6425 set TYPE_HAS_COMPLEX_DFLT. */
6426 if (!TYPE_HAS_COMPLEX_DFLT (t)
6427 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6428 && !type_has_non_user_provided_default_constructor (t))
6429 TYPE_HAS_COMPLEX_DFLT (t) = true;
6431 /* Warn if a public base of a polymorphic type has an accessible
6432 non-virtual destructor. It is only now that we know the class is
6433 polymorphic. Although a polymorphic base will have a already
6434 been diagnosed during its definition, we warn on use too. */
6435 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6437 tree binfo = TYPE_BINFO (t);
6438 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6439 tree base_binfo;
6440 unsigned i;
6442 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6444 tree basetype = TREE_TYPE (base_binfo);
6446 if ((*accesses)[i] == access_public_node
6447 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6448 && accessible_nvdtor_p (basetype))
6449 warning (OPT_Wnon_virtual_dtor,
6450 "base class %q#T has accessible non-virtual destructor",
6451 basetype);
6455 /* If the class has no user-declared constructor, but does have
6456 non-static const or reference data members that can never be
6457 initialized, issue a warning. */
6458 if (warn_uninitialized
6459 /* Classes with user-declared constructors are presumed to
6460 initialize these members. */
6461 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6462 /* Aggregates can be initialized with brace-enclosed
6463 initializers. */
6464 && CLASSTYPE_NON_AGGREGATE (t))
6466 tree field;
6468 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6470 tree type;
6472 if (TREE_CODE (field) != FIELD_DECL
6473 || DECL_INITIAL (field) != NULL_TREE)
6474 continue;
6476 type = TREE_TYPE (field);
6477 if (TYPE_REF_P (type))
6478 warning_at (DECL_SOURCE_LOCATION (field),
6479 OPT_Wuninitialized, "non-static reference %q#D "
6480 "in class without a constructor", field);
6481 else if (CP_TYPE_CONST_P (type)
6482 && (!CLASS_TYPE_P (type)
6483 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6484 warning_at (DECL_SOURCE_LOCATION (field),
6485 OPT_Wuninitialized, "non-static const member %q#D "
6486 "in class without a constructor", field);
6490 /* Synthesize any needed methods. */
6491 add_implicitly_declared_members (t, &access_decls,
6492 cant_have_const_ctor,
6493 no_const_asn_ref);
6495 /* Check defaulted declarations here so we have cant_have_const_ctor
6496 and don't need to worry about clones. */
6497 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6498 if (DECL_DECLARES_FUNCTION_P (fn)
6499 && !DECL_ARTIFICIAL (fn)
6500 && DECL_DEFAULTED_IN_CLASS_P (fn))
6502 /* ...except handle comparisons later, in finish_struct_1. */
6503 if (special_function_p (fn) == sfk_comparison)
6504 continue;
6506 int copy = copy_fn_p (fn);
6507 if (copy > 0)
6509 bool imp_const_p
6510 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6511 : !no_const_asn_ref);
6512 bool fn_const_p = (copy == 2);
6514 if (fn_const_p && !imp_const_p)
6515 /* If the function is defaulted outside the class, we just
6516 give the synthesis error. Core Issue #1331 says this is
6517 no longer ill-formed, it is defined as deleted instead. */
6518 DECL_DELETED_FN (fn) = true;
6520 defaulted_late_check (fn);
6523 if (LAMBDA_TYPE_P (t))
6524 /* "This class type is not an aggregate." */
6525 CLASSTYPE_NON_AGGREGATE (t) = 1;
6527 /* Compute the 'literal type' property before we
6528 do anything with non-static member functions. */
6529 finalize_literal_type_property (t);
6531 /* Create the in-charge and not-in-charge variants of constructors
6532 and destructors. */
6533 clone_constructors_and_destructors (t);
6535 /* Process the using-declarations. */
6536 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6537 handle_using_decl (TREE_VALUE (access_decls), t);
6539 /* Figure out whether or not we will need a cookie when dynamically
6540 allocating an array of this type. */
6541 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6542 = type_requires_array_cookie (t);
6544 /* Classes marked hot or cold propagate the attribute to all members. We
6545 may do this now that methods are declared. This does miss some lazily
6546 declared special member functions (CLASSTYPE_LAZY_*), which are handled
6547 in lazily_declare_fn later on. */
6548 propagate_class_warmth_attribute (t);
6551 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6552 accordingly. If a new vfield was created (because T doesn't have a
6553 primary base class), then the newly created field is returned. It
6554 is not added to the TYPE_FIELDS list; it is the caller's
6555 responsibility to do that. Accumulate declared virtual functions
6556 on VIRTUALS_P. */
6558 static tree
6559 create_vtable_ptr (tree t, tree* virtuals_p)
6561 tree fn;
6563 /* Collect the virtual functions declared in T. */
6564 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6565 if (TREE_CODE (fn) == FUNCTION_DECL
6566 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6567 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6569 tree new_virtual = make_node (TREE_LIST);
6571 BV_FN (new_virtual) = fn;
6572 BV_DELTA (new_virtual) = integer_zero_node;
6573 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6575 TREE_CHAIN (new_virtual) = *virtuals_p;
6576 *virtuals_p = new_virtual;
6579 /* If we couldn't find an appropriate base class, create a new field
6580 here. Even if there weren't any new virtual functions, we might need a
6581 new virtual function table if we're supposed to include vptrs in
6582 all classes that need them. */
6583 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6585 /* We build this decl with vtbl_ptr_type_node, which is a
6586 `vtable_entry_type*'. It might seem more precise to use
6587 `vtable_entry_type (*)[N]' where N is the number of virtual
6588 functions. However, that would require the vtable pointer in
6589 base classes to have a different type than the vtable pointer
6590 in derived classes. We could make that happen, but that
6591 still wouldn't solve all the problems. In particular, the
6592 type-based alias analysis code would decide that assignments
6593 to the base class vtable pointer can't alias assignments to
6594 the derived class vtable pointer, since they have different
6595 types. Thus, in a derived class destructor, where the base
6596 class constructor was inlined, we could generate bad code for
6597 setting up the vtable pointer.
6599 Therefore, we use one type for all vtable pointers. We still
6600 use a type-correct type; it's just doesn't indicate the array
6601 bounds. That's better than using `void*' or some such; it's
6602 cleaner, and it let's the alias analysis code know that these
6603 stores cannot alias stores to void*! */
6604 tree field;
6606 field = build_decl (input_location,
6607 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6608 DECL_VIRTUAL_P (field) = 1;
6609 DECL_ARTIFICIAL (field) = 1;
6610 DECL_FIELD_CONTEXT (field) = t;
6611 DECL_FCONTEXT (field) = t;
6612 if (TYPE_PACKED (t))
6613 DECL_PACKED (field) = 1;
6615 TYPE_VFIELD (t) = field;
6617 /* This class is non-empty. */
6618 CLASSTYPE_EMPTY_P (t) = 0;
6620 return field;
6623 return NULL_TREE;
6626 /* Add OFFSET to all base types of BINFO which is a base in the
6627 hierarchy dominated by T.
6629 OFFSET, which is a type offset, is number of bytes. */
6631 static void
6632 propagate_binfo_offsets (tree binfo, tree offset)
6634 int i;
6635 tree primary_binfo;
6636 tree base_binfo;
6638 /* Update BINFO's offset. */
6639 BINFO_OFFSET (binfo)
6640 = fold_convert (sizetype,
6641 size_binop (PLUS_EXPR,
6642 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6643 offset));
6645 /* Find the primary base class. */
6646 primary_binfo = get_primary_binfo (binfo);
6648 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6649 propagate_binfo_offsets (primary_binfo, offset);
6651 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6652 downwards. */
6653 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6655 /* Don't do the primary base twice. */
6656 if (base_binfo == primary_binfo)
6657 continue;
6659 if (BINFO_VIRTUAL_P (base_binfo))
6660 continue;
6662 propagate_binfo_offsets (base_binfo, offset);
6666 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6667 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6668 empty subobjects of T. */
6670 static void
6671 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6673 tree vbase;
6674 tree t = rli->t;
6675 tree *next_field;
6677 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6678 return;
6680 /* Find the last field. The artificial fields created for virtual
6681 bases will go after the last extant field to date. */
6682 next_field = &TYPE_FIELDS (t);
6683 while (*next_field)
6684 next_field = &DECL_CHAIN (*next_field);
6686 /* Go through the virtual bases, allocating space for each virtual
6687 base that is not already a primary base class. These are
6688 allocated in inheritance graph order. */
6689 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6691 if (!BINFO_VIRTUAL_P (vbase))
6692 continue;
6694 if (!BINFO_PRIMARY_P (vbase))
6696 /* This virtual base is not a primary base of any class in the
6697 hierarchy, so we have to add space for it. */
6698 next_field = build_base_field (rli, vbase,
6699 access_private_node,
6700 offsets, next_field);
6705 /* Returns the offset of the byte just past the end of the base class
6706 BINFO. */
6708 static tree
6709 end_of_base (tree binfo)
6711 tree size;
6713 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6714 size = TYPE_SIZE_UNIT (char_type_node);
6715 else if (is_empty_class (BINFO_TYPE (binfo)))
6716 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6717 allocate some space for it. It cannot have virtual bases, so
6718 TYPE_SIZE_UNIT is fine. */
6719 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6720 else
6721 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6723 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6726 /* Returns one of three variations of the ending offset of T. If MODE is
6727 eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6728 vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6729 vbases but before rounding, which is not named in the ABI. If MODE is
6730 eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6731 of the actual data in the class, kinda), as used for allocation of
6732 potentially-overlapping fields. */
6734 enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6735 static tree
6736 end_of_class (tree t, eoc_mode mode)
6738 tree result = size_zero_node;
6739 vec<tree, va_gc> *vbases;
6740 tree binfo;
6741 tree base_binfo;
6742 tree offset;
6743 int i;
6745 for (binfo = TYPE_BINFO (t), i = 0;
6746 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6748 if (BINFO_VIRTUAL_P (base_binfo)
6749 && (!BINFO_PRIMARY_P (base_binfo)
6750 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6751 continue;
6753 offset = end_of_base (base_binfo);
6754 if (tree_int_cst_lt (result, offset))
6755 result = offset;
6758 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6759 if (TREE_CODE (field) == FIELD_DECL
6760 && !DECL_FIELD_IS_BASE (field))
6762 tree size = DECL_SIZE_UNIT (field);
6763 if (!size)
6764 /* DECL_SIZE_UNIT can be null for a flexible array. */
6765 continue;
6767 if (is_empty_field (field))
6768 /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6769 size of the type (usually 1) for computing nvsize. */
6770 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6772 if (DECL_BIT_FIELD_TYPE (field))
6774 offset = size_binop (PLUS_EXPR, bit_position (field),
6775 DECL_SIZE (field));
6776 offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6777 offset = fold_convert (sizetype, offset);
6779 else
6780 offset = size_binop (PLUS_EXPR, byte_position (field), size);
6781 if (tree_int_cst_lt (result, offset))
6782 result = offset;
6785 if (mode != eoc_nvsize)
6786 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6787 vec_safe_iterate (vbases, i, &base_binfo); i++)
6789 if (mode == eoc_nv_or_dsize)
6790 /* For dsize, don't count trailing empty bases. */
6791 offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6792 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6793 else
6794 offset = end_of_base (base_binfo);
6795 if (tree_int_cst_lt (result, offset))
6796 result = offset;
6799 return result;
6802 /* Warn as appropriate about the change in whether we pack into the tail
6803 padding of FIELD, a base field which has a C++14 aggregate type with default
6804 member initializers. */
6806 static void
6807 check_non_pod_aggregate (tree field)
6809 if (!abi_version_crosses (17) || cxx_dialect < cxx14)
6810 return;
6811 if (TREE_CODE (field) != FIELD_DECL
6812 || (!DECL_FIELD_IS_BASE (field)
6813 && !field_poverlapping_p (field)))
6814 return;
6815 tree next = DECL_CHAIN (field);
6816 while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6817 if (!next)
6818 return;
6819 tree type = TREE_TYPE (field);
6820 if (TYPE_IDENTIFIER (type) == as_base_identifier)
6821 type = TYPE_CONTEXT (type);
6822 if (!CLASS_TYPE_P (type) || !CLASSTYPE_NON_POD_AGGREGATE (type))
6823 return;
6824 tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6825 ? eoc_nvsize : eoc_nv_or_dsize));
6826 tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6827 if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6829 location_t loc = DECL_SOURCE_LOCATION (next);
6830 if (DECL_FIELD_IS_BASE (next))
6831 warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6832 "%<-std=c++14%> and up changes in "
6833 "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6834 else
6835 warning_at (loc, OPT_Wabi, "offset of %qD for "
6836 "%<-std=c++14%> and up changes in "
6837 "%<-fabi-version=17%> (GCC 12)", next);
6841 /* Warn about bases of T that are inaccessible because they are
6842 ambiguous. For example:
6844 struct S {};
6845 struct T : public S {};
6846 struct U : public S, public T {};
6848 Here, `(S*) new U' is not allowed because there are two `S'
6849 subobjects of U. */
6851 static void
6852 maybe_warn_about_inaccessible_bases (tree t)
6854 int i;
6855 vec<tree, va_gc> *vbases;
6856 tree basetype;
6857 tree binfo;
6858 tree base_binfo;
6860 /* If not checking for warning then return early. */
6861 if (!warn_inaccessible_base)
6862 return;
6864 /* If there are no repeated bases, nothing can be ambiguous. */
6865 if (!CLASSTYPE_REPEATED_BASE_P (t))
6866 return;
6868 /* Check direct bases. */
6869 for (binfo = TYPE_BINFO (t), i = 0;
6870 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6872 basetype = BINFO_TYPE (base_binfo);
6874 if (!uniquely_derived_from_p (basetype, t))
6875 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6876 "in %qT due to ambiguity", basetype, t);
6879 /* Check for ambiguous virtual bases. */
6880 if (extra_warnings)
6881 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6882 vec_safe_iterate (vbases, i, &binfo); i++)
6884 basetype = BINFO_TYPE (binfo);
6886 if (!uniquely_derived_from_p (basetype, t))
6887 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6888 "%qT due to ambiguity", basetype, t);
6892 /* Compare two INTEGER_CSTs K1 and K2. */
6894 static int
6895 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6897 return tree_int_cst_compare ((tree) k1, (tree) k2);
6900 /* Increase the size indicated in RLI to account for empty classes
6901 that are "off the end" of the class. */
6903 static void
6904 include_empty_classes (record_layout_info rli)
6906 tree eoc;
6907 tree rli_size;
6909 /* It might be the case that we grew the class to allocate a
6910 zero-sized base class. That won't be reflected in RLI, yet,
6911 because we are willing to overlay multiple bases at the same
6912 offset. However, now we need to make sure that RLI is big enough
6913 to reflect the entire class. */
6914 eoc = end_of_class (rli->t, eoc_vsize);
6915 rli_size = rli_size_unit_so_far (rli);
6916 if (TREE_CODE (rli_size) == INTEGER_CST
6917 && tree_int_cst_lt (rli_size, eoc))
6919 /* The size should have been rounded to a whole byte. */
6920 gcc_assert (tree_int_cst_equal
6921 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6922 rli->bitpos
6923 = size_binop (PLUS_EXPR,
6924 rli->bitpos,
6925 size_binop (MULT_EXPR,
6926 fold_convert (bitsizetype,
6927 size_binop (MINUS_EXPR,
6928 eoc, rli_size)),
6929 bitsize_int (BITS_PER_UNIT)));
6930 normalize_rli (rli);
6934 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6935 BINFO_OFFSETs for all of the base-classes. Position the vtable
6936 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6938 static void
6939 layout_class_type (tree t, tree *virtuals_p)
6941 tree non_static_data_members;
6942 tree field;
6943 tree vptr;
6944 record_layout_info rli;
6945 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6946 types that appear at that offset. */
6947 splay_tree empty_base_offsets;
6948 /* True if the last field laid out was a bit-field. */
6949 bool last_field_was_bitfield = false;
6950 /* The location at which the next field should be inserted. */
6951 tree *next_field;
6953 /* Keep track of the first non-static data member. */
6954 non_static_data_members = TYPE_FIELDS (t);
6956 /* Start laying out the record. */
6957 rli = start_record_layout (t);
6959 /* Mark all the primary bases in the hierarchy. */
6960 determine_primary_bases (t);
6962 /* Create a pointer to our virtual function table. */
6963 vptr = create_vtable_ptr (t, virtuals_p);
6965 /* The vptr is always the first thing in the class. */
6966 if (vptr)
6968 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6969 TYPE_FIELDS (t) = vptr;
6970 next_field = &DECL_CHAIN (vptr);
6971 place_field (rli, vptr);
6973 else
6974 next_field = &TYPE_FIELDS (t);
6976 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6977 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6978 NULL, NULL);
6979 build_base_fields (rli, empty_base_offsets, next_field);
6981 /* Layout the non-static data members. */
6982 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6984 tree type;
6985 tree padding;
6987 /* We still pass things that aren't non-static data members to
6988 the back end, in case it wants to do something with them. */
6989 if (TREE_CODE (field) != FIELD_DECL)
6991 place_field (rli, field);
6992 /* If the static data member has incomplete type, keep track
6993 of it so that it can be completed later. (The handling
6994 of pending statics in finish_record_layout is
6995 insufficient; consider:
6997 struct S1;
6998 struct S2 { static S1 s1; };
7000 At this point, finish_record_layout will be called, but
7001 S1 is still incomplete.) */
7002 if (VAR_P (field))
7004 maybe_register_incomplete_var (field);
7005 /* The visibility of static data members is determined
7006 at their point of declaration, not their point of
7007 definition. */
7008 determine_visibility (field);
7010 continue;
7013 type = TREE_TYPE (field);
7014 if (type == error_mark_node)
7015 continue;
7017 padding = NULL_TREE;
7019 bool might_overlap = field_poverlapping_p (field);
7021 if (might_overlap && CLASS_TYPE_P (type)
7022 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
7024 /* if D is a potentially-overlapping data member, update sizeof(C) to
7025 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
7026 if (CLASSTYPE_EMPTY_P (type))
7027 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
7028 else
7030 tree size = end_of_class (type, eoc_nv_or_dsize);
7031 DECL_SIZE_UNIT (field) = size;
7032 DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
7036 /* If this field is a bit-field whose width is greater than its
7037 type, then there are some special rules for allocating
7038 it. */
7039 if (DECL_C_BIT_FIELD (field)
7040 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
7042 bool was_unnamed_p = false;
7043 /* We must allocate the bits as if suitably aligned for the
7044 longest integer type that fits in this many bits. Then,
7045 we are supposed to use the left over bits as additional
7046 padding. */
7048 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
7049 tree limit = size_int (MAX_FIXED_MODE_SIZE);
7050 if (tree_int_cst_lt (DECL_SIZE (field), limit))
7051 limit = DECL_SIZE (field);
7053 tree integer_type = integer_types[itk_char];
7054 for (unsigned itk = itk_char; itk != itk_none; itk++)
7055 if (tree next = integer_types[itk])
7057 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
7058 /* Too big, so our current guess is what we want. */
7059 break;
7060 /* Not bigger than limit, ok */
7061 integer_type = next;
7064 /* Figure out how much additional padding is required. */
7065 if (TREE_CODE (t) == UNION_TYPE)
7066 /* In a union, the padding field must have the full width
7067 of the bit-field; all fields start at offset zero. */
7068 padding = DECL_SIZE (field);
7069 else
7070 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
7071 TYPE_SIZE (integer_type));
7073 if (integer_zerop (padding))
7074 padding = NULL_TREE;
7076 /* An unnamed bitfield does not normally affect the
7077 alignment of the containing class on a target where
7078 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
7079 make any exceptions for unnamed bitfields when the
7080 bitfields are longer than their types. Therefore, we
7081 temporarily give the field a name. */
7082 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
7084 was_unnamed_p = true;
7085 DECL_NAME (field) = make_anon_name ();
7088 DECL_SIZE (field) = TYPE_SIZE (integer_type);
7089 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
7090 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
7091 layout_nonempty_base_or_field (rli, field, NULL_TREE,
7092 empty_base_offsets);
7093 if (was_unnamed_p)
7094 DECL_NAME (field) = NULL_TREE;
7095 /* Now that layout has been performed, set the size of the
7096 field to the size of its declared type; the rest of the
7097 field is effectively invisible. */
7098 DECL_SIZE (field) = TYPE_SIZE (type);
7099 /* We must also reset the DECL_MODE of the field. */
7100 SET_DECL_MODE (field, TYPE_MODE (type));
7102 else if (might_overlap && is_empty_class (type))
7104 SET_DECL_FIELD_ABI_IGNORED (field, 1);
7105 layout_empty_base_or_field (rli, field, empty_base_offsets);
7107 else
7108 layout_nonempty_base_or_field (rli, field, NULL_TREE,
7109 empty_base_offsets);
7111 /* Remember the location of any empty classes in FIELD. */
7112 record_subobject_offsets (field, empty_base_offsets);
7114 /* If a bit-field does not immediately follow another bit-field,
7115 and yet it starts in the middle of a byte, we have failed to
7116 comply with the ABI. */
7117 if (warn_abi
7118 && DECL_C_BIT_FIELD (field)
7119 /* The TREE_NO_WARNING flag gets set by Objective-C when
7120 laying out an Objective-C class. The ObjC ABI differs
7121 from the C++ ABI, and so we do not want a warning
7122 here. */
7123 && !warning_suppressed_p (field, OPT_Wabi)
7124 && !last_field_was_bitfield
7125 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
7126 DECL_FIELD_BIT_OFFSET (field),
7127 bitsize_unit_node)))
7128 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
7129 "offset of %qD is not ABI-compliant and may "
7130 "change in a future version of GCC", field);
7132 /* The middle end uses the type of expressions to determine the
7133 possible range of expression values. In order to optimize
7134 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
7135 must be made aware of the width of "i", via its type.
7137 Because C++ does not have integer types of arbitrary width,
7138 we must (for the purposes of the front end) convert from the
7139 type assigned here to the declared type of the bitfield
7140 whenever a bitfield expression is used as an rvalue.
7141 Similarly, when assigning a value to a bitfield, the value
7142 must be converted to the type given the bitfield here. */
7143 if (DECL_C_BIT_FIELD (field))
7145 unsigned HOST_WIDE_INT width;
7146 tree ftype = TREE_TYPE (field);
7147 width = tree_to_uhwi (DECL_SIZE (field));
7148 if (width != TYPE_PRECISION (ftype))
7150 TREE_TYPE (field)
7151 = c_build_bitfield_integer_type (width,
7152 TYPE_UNSIGNED (ftype));
7153 TREE_TYPE (field)
7154 = cp_build_qualified_type (TREE_TYPE (field),
7155 cp_type_quals (ftype));
7159 /* If we needed additional padding after this field, add it
7160 now. */
7161 if (padding)
7163 tree padding_field;
7165 padding_field = build_decl (input_location,
7166 FIELD_DECL,
7167 NULL_TREE,
7168 char_type_node);
7169 DECL_BIT_FIELD (padding_field) = 1;
7170 DECL_SIZE (padding_field) = padding;
7171 DECL_CONTEXT (padding_field) = t;
7172 DECL_ARTIFICIAL (padding_field) = 1;
7173 DECL_IGNORED_P (padding_field) = 1;
7174 DECL_PADDING_P (padding_field) = 1;
7175 layout_nonempty_base_or_field (rli, padding_field,
7176 NULL_TREE,
7177 empty_base_offsets);
7180 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
7183 if (!integer_zerop (rli->bitpos))
7185 /* Make sure that we are on a byte boundary so that the size of
7186 the class without virtual bases will always be a round number
7187 of bytes. */
7188 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
7189 normalize_rli (rli);
7192 /* We used to remove zero width bitfields at this point since PR42217,
7193 while the C FE never did that. That caused ABI differences on various
7194 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
7195 instead, so that the backends can emit -Wpsabi warnings in the cases
7196 where the ABI changed. */
7197 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7199 if (TREE_CODE (field) == FIELD_DECL
7200 && DECL_C_BIT_FIELD (field)
7201 /* We should not be confused by the fact that grokbitfield
7202 temporarily sets the width of the bit field into
7203 DECL_BIT_FIELD_REPRESENTATIVE (field).
7204 check_bitfield_decl eventually sets DECL_SIZE (field)
7205 to that width. */
7206 && (DECL_SIZE (field) == NULL_TREE
7207 || integer_zerop (DECL_SIZE (field)))
7208 && TREE_TYPE (field) != error_mark_node)
7209 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
7210 check_non_pod_aggregate (field);
7213 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
7215 /* T needs a different layout as a base (eliding virtual bases
7216 or whatever). Create that version. */
7217 tree base_t = make_node (TREE_CODE (t));
7218 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
7220 TYPE_CONTEXT (base_t) = t;
7221 DECL_CONTEXT (base_d) = t;
7223 set_instantiating_module (base_d);
7225 /* If the ABI version is not at least two, and the last
7226 field was a bit-field, RLI may not be on a byte
7227 boundary. In particular, rli_size_unit_so_far might
7228 indicate the last complete byte, while rli_size_so_far
7229 indicates the total number of bits used. Therefore,
7230 rli_size_so_far, rather than rli_size_unit_so_far, is
7231 used to compute TYPE_SIZE_UNIT. */
7233 /* Set the size and alignment for the new type. */
7234 tree eoc = end_of_class (t, eoc_nvsize);
7235 TYPE_SIZE_UNIT (base_t)
7236 = size_binop (MAX_EXPR,
7237 fold_convert (sizetype,
7238 size_binop (CEIL_DIV_EXPR,
7239 rli_size_so_far (rli),
7240 bitsize_int (BITS_PER_UNIT))),
7241 eoc);
7242 TYPE_SIZE (base_t)
7243 = size_binop (MAX_EXPR,
7244 rli_size_so_far (rli),
7245 size_binop (MULT_EXPR,
7246 fold_convert (bitsizetype, eoc),
7247 bitsize_int (BITS_PER_UNIT)));
7248 SET_TYPE_ALIGN (base_t, rli->record_align);
7249 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
7250 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
7251 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
7253 /* Copy the non-static data members of T. This will include its
7254 direct non-virtual bases & vtable. */
7255 next_field = &TYPE_FIELDS (base_t);
7256 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7257 if (TREE_CODE (field) == FIELD_DECL)
7259 *next_field = copy_node (field);
7260 /* Zap any NSDMI, it's not needed and might be a deferred
7261 parse. */
7262 DECL_INITIAL (*next_field) = NULL_TREE;
7263 DECL_CONTEXT (*next_field) = base_t;
7264 next_field = &DECL_CHAIN (*next_field);
7266 *next_field = NULL_TREE;
7268 /* We use the base type for trivial assignments, and hence it
7269 needs a mode. */
7270 compute_record_mode (base_t);
7272 /* Record the base version of the type. */
7273 CLASSTYPE_AS_BASE (t) = base_t;
7275 else
7276 CLASSTYPE_AS_BASE (t) = t;
7278 /* Every empty class contains an empty class. */
7279 if (CLASSTYPE_EMPTY_P (t))
7280 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7282 /* Set the TYPE_DECL for this type to contain the right
7283 value for DECL_OFFSET, so that we can use it as part
7284 of a COMPONENT_REF for multiple inheritance. */
7285 layout_decl (TYPE_MAIN_DECL (t), 0);
7287 /* Now fix up any virtual base class types that we left lying
7288 around. We must get these done before we try to lay out the
7289 virtual function table. As a side-effect, this will remove the
7290 base subobject fields. */
7291 layout_virtual_bases (rli, empty_base_offsets);
7293 /* Make sure that empty classes are reflected in RLI at this
7294 point. */
7295 include_empty_classes (rli);
7297 /* Make sure not to create any structures with zero size. */
7298 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7299 place_field (rli,
7300 build_decl (input_location,
7301 FIELD_DECL, NULL_TREE, char_type_node));
7303 /* If this is a non-POD, declaring it packed makes a difference to how it
7304 can be used as a field; don't let finalize_record_size undo it. */
7305 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7306 rli->packed_maybe_necessary = true;
7308 /* Let the back end lay out the type. */
7309 finish_record_layout (rli, /*free_p=*/true);
7311 /* If we didn't end up needing an as-base type, don't use it. */
7312 if (CLASSTYPE_AS_BASE (t) != t
7313 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7314 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7315 causing us to lose the user-specified alignment as in PR94050. */
7316 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7317 && tree_int_cst_equal (TYPE_SIZE (t),
7318 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7319 CLASSTYPE_AS_BASE (t) = t;
7321 if (TYPE_SIZE_UNIT (t)
7322 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7323 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7324 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7325 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7327 /* Warn about bases that can't be talked about due to ambiguity. */
7328 maybe_warn_about_inaccessible_bases (t);
7330 /* Now that we're done with layout, give the base fields the real types. */
7331 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7332 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7333 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7335 /* Clean up. */
7336 splay_tree_delete (empty_base_offsets);
7338 if (CLASSTYPE_EMPTY_P (t)
7339 && tree_int_cst_lt (sizeof_biggest_empty_class,
7340 TYPE_SIZE_UNIT (t)))
7341 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7344 /* Determine the "key method" for the class type indicated by TYPE,
7345 and set CLASSTYPE_KEY_METHOD accordingly. */
7347 void
7348 determine_key_method (tree type)
7350 tree method;
7352 if (processing_template_decl
7353 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7354 || CLASSTYPE_INTERFACE_KNOWN (type))
7355 return;
7357 /* The key method is the first non-pure virtual function that is not
7358 inline at the point of class definition. On some targets the
7359 key function may not be inline; those targets should not call
7360 this function until the end of the translation unit. */
7361 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7362 if (TREE_CODE (method) == FUNCTION_DECL
7363 && DECL_VINDEX (method) != NULL_TREE
7364 && ! DECL_DECLARED_INLINE_P (method)
7365 && ! DECL_PURE_VIRTUAL_P (method))
7367 CLASSTYPE_KEY_METHOD (type) = method;
7368 break;
7371 return;
7374 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7375 class data member of non-zero size, otherwise false. */
7377 static inline bool
7378 field_nonempty_p (const_tree fld)
7380 if (TREE_CODE (fld) == ERROR_MARK)
7381 return false;
7383 tree type = TREE_TYPE (fld);
7384 if (TREE_CODE (fld) == FIELD_DECL
7385 && TREE_CODE (type) != ERROR_MARK
7386 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7388 return TYPE_SIZE (type)
7389 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
7390 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
7393 return false;
7396 /* Used by find_flexarrays and related functions. */
7398 struct flexmems_t
7400 /* The first flexible array member or non-zero array member found
7401 in the order of layout. */
7402 tree array;
7403 /* First non-static non-empty data member in the class or its bases. */
7404 tree first;
7405 /* The first non-static non-empty data member following either
7406 the flexible array member, if found, or the zero-length array member
7407 otherwise. AFTER[1] refers to the first such data member of a union
7408 of which the struct containing the flexible array member or zero-length
7409 array is a member, or NULL when no such union exists. This element is
7410 only used during searching, not for diagnosing problems. AFTER[0]
7411 refers to the first such data member that is not a member of such
7412 a union. */
7413 tree after[2];
7415 /* Refers to a struct (not union) in which the struct of which the flexible
7416 array is member is defined. Used to diagnose strictly (according to C)
7417 invalid uses of the latter structs. */
7418 tree enclosing;
7421 /* Find either the first flexible array member or the first zero-length
7422 array, in that order of preference, among members of class T (but not
7423 its base classes), and set members of FMEM accordingly.
7424 BASE_P is true if T is a base class of another class.
7425 PUN is set to the outermost union in which the flexible array member
7426 (or zero-length array) is defined if one such union exists, otherwise
7427 to NULL.
7428 Similarly, PSTR is set to a data member of the outermost struct of
7429 which the flexible array is a member if one such struct exists,
7430 otherwise to NULL. */
7432 static void
7433 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7434 tree pun /* = NULL_TREE */,
7435 tree pstr /* = NULL_TREE */)
7437 /* Set the "pointer" to the outermost enclosing union if not set
7438 yet and maintain it for the remainder of the recursion. */
7439 if (!pun && TREE_CODE (t) == UNION_TYPE)
7440 pun = t;
7442 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
7444 if (fld == error_mark_node)
7445 return;
7447 /* Is FLD a typedef for an anonymous struct? */
7449 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7450 handled elsewhere so that errors like the following are detected
7451 as well:
7452 typedef struct { int i, a[], j; } S; // bug c++/72753
7453 S s [2]; // bug c++/68489
7455 if (TREE_CODE (fld) == TYPE_DECL
7456 && DECL_IMPLICIT_TYPEDEF_P (fld)
7457 && CLASS_TYPE_P (TREE_TYPE (fld))
7458 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7460 /* Check the nested unnamed type referenced via a typedef
7461 independently of FMEM (since it's not a data member of
7462 the enclosing class). */
7463 check_flexarrays (TREE_TYPE (fld));
7464 continue;
7467 /* Skip anything that's GCC-generated or not a (non-static) data
7468 member. */
7469 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7470 continue;
7472 /* Type of the member. */
7473 tree fldtype = TREE_TYPE (fld);
7474 if (fldtype == error_mark_node)
7475 return;
7477 /* Determine the type of the array element or object referenced
7478 by the member so that it can be checked for flexible array
7479 members if it hasn't been yet. */
7480 tree eltype = fldtype;
7481 while (TREE_CODE (eltype) == ARRAY_TYPE
7482 || INDIRECT_TYPE_P (eltype))
7483 eltype = TREE_TYPE (eltype);
7485 if (RECORD_OR_UNION_TYPE_P (eltype))
7487 if (fmem->array && !fmem->after[bool (pun)])
7489 /* Once the member after the flexible array has been found
7490 we're done. */
7491 fmem->after[bool (pun)] = fld;
7492 break;
7495 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7497 /* Descend into the non-static member struct or union and try
7498 to find a flexible array member or zero-length array among
7499 its members. This is only necessary for anonymous types
7500 and types in whose context the current type T has not been
7501 defined (the latter must not be checked again because they
7502 are already in the process of being checked by one of the
7503 recursive calls). */
7505 tree first = fmem->first;
7506 tree array = fmem->array;
7508 /* If this member isn't anonymous and a prior non-flexible array
7509 member has been seen in one of the enclosing structs, clear
7510 the FIRST member since it doesn't contribute to the flexible
7511 array struct's members. */
7512 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7513 fmem->first = NULL_TREE;
7515 find_flexarrays (eltype, fmem, false, pun,
7516 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7518 if (fmem->array != array)
7519 continue;
7521 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7523 /* Restore the FIRST member reset above if no flexible
7524 array member has been found in this member's struct. */
7525 fmem->first = first;
7528 /* If the member struct contains the first flexible array
7529 member, or if this member is a base class, continue to
7530 the next member and avoid setting the FMEM->NEXT pointer
7531 to point to it. */
7532 if (base_p)
7533 continue;
7537 if (field_nonempty_p (fld))
7539 /* Remember the first non-static data member. */
7540 if (!fmem->first)
7541 fmem->first = fld;
7543 /* Remember the first non-static data member after the flexible
7544 array member, if one has been found, or the zero-length array
7545 if it has been found. */
7546 if (fmem->array && !fmem->after[bool (pun)])
7547 fmem->after[bool (pun)] = fld;
7550 /* Skip non-arrays. */
7551 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7552 continue;
7554 /* Determine the upper bound of the array if it has one. */
7555 if (TYPE_DOMAIN (fldtype))
7557 if (fmem->array)
7559 /* Make a record of the zero-length array if either one
7560 such field or a flexible array member has been seen to
7561 handle the pathological and unlikely case of multiple
7562 such members. */
7563 if (!fmem->after[bool (pun)])
7564 fmem->after[bool (pun)] = fld;
7566 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7568 /* Remember the first zero-length array unless a flexible array
7569 member has already been seen. */
7570 fmem->array = fld;
7571 fmem->enclosing = pstr;
7574 else
7576 /* Flexible array members have no upper bound. */
7577 if (fmem->array)
7579 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7581 /* Replace the zero-length array if it's been stored and
7582 reset the after pointer. */
7583 fmem->after[bool (pun)] = NULL_TREE;
7584 fmem->array = fld;
7585 fmem->enclosing = pstr;
7587 else if (!fmem->after[bool (pun)])
7588 /* Make a record of another flexible array member. */
7589 fmem->after[bool (pun)] = fld;
7591 else
7593 fmem->array = fld;
7594 fmem->enclosing = pstr;
7600 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7601 a flexible array member (or the zero-length array extension). */
7603 static void
7604 diagnose_invalid_flexarray (const flexmems_t *fmem)
7606 if (fmem->array && fmem->enclosing)
7608 auto_diagnostic_group d;
7609 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7610 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7611 ? G_("invalid use of %q#T with a zero-size array "
7612 "in %q#D")
7613 : G_("invalid use of %q#T with a flexible array member "
7614 "in %q#T"),
7615 DECL_CONTEXT (fmem->array),
7616 DECL_CONTEXT (fmem->enclosing)))
7617 inform (DECL_SOURCE_LOCATION (fmem->array),
7618 "array member %q#D declared here", fmem->array);
7622 /* Issue diagnostics for invalid flexible array members or zero-length
7623 arrays that are not the last elements of the containing class or its
7624 base classes or that are its sole members. */
7626 static void
7627 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7629 if (!fmem->array)
7630 return;
7632 if (fmem->first && !fmem->after[0])
7634 diagnose_invalid_flexarray (fmem);
7635 return;
7638 /* Has a diagnostic been issued? */
7639 bool diagd = false;
7641 const char *msg = 0;
7643 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7645 if (fmem->after[0])
7646 msg = G_("zero-size array member %qD not at end of %q#T");
7647 else if (!fmem->first)
7648 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7650 if (msg)
7652 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7654 auto_diagnostic_group d;
7655 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7657 inform (location_of (t), "in the definition of %q#T", t);
7658 diagd = true;
7662 else
7664 if (fmem->after[0])
7665 msg = G_("flexible array member %qD not at end of %q#T");
7666 else if (!fmem->first)
7667 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7669 if (msg)
7671 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7672 diagd = true;
7674 auto_diagnostic_group d;
7675 error_at (loc, msg, fmem->array, t);
7677 /* In the unlikely event that the member following the flexible
7678 array member is declared in a different class, or the member
7679 overlaps another member of a common union, point to it.
7680 Otherwise it should be obvious. */
7681 if (fmem->after[0]
7682 && ((DECL_CONTEXT (fmem->after[0])
7683 != DECL_CONTEXT (fmem->array))))
7685 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7686 "next member %q#D declared here",
7687 fmem->after[0]);
7688 inform (location_of (t), "in the definition of %q#T", t);
7693 if (!diagd && fmem->array && fmem->enclosing)
7694 diagnose_invalid_flexarray (fmem);
7698 /* Recursively check to make sure that any flexible array or zero-length
7699 array members of class T or its bases are valid (i.e., not the sole
7700 non-static data member of T and, if one exists, that it is the last
7701 non-static data member of T and its base classes. FMEM is expected
7702 to be initially null and is used internally by recursive calls to
7703 the function. Issue the appropriate diagnostics for the array member
7704 that fails the checks. */
7706 static void
7707 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7708 bool base_p /* = false */)
7710 /* Initialize the result of a search for flexible array and zero-length
7711 array members. Avoid doing any work if the most interesting FMEM data
7712 have already been populated. */
7713 flexmems_t flexmems = flexmems_t ();
7714 if (!fmem)
7715 fmem = &flexmems;
7716 else if (fmem->array && fmem->first && fmem->after[0])
7717 return;
7719 tree fam = fmem->array;
7721 /* Recursively check the primary base class first. */
7722 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7724 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7725 check_flexarrays (basetype, fmem, true);
7728 /* Recursively check the base classes. */
7729 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7730 for (int i = 0; i < nbases; ++i)
7732 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7734 /* The primary base class was already checked above. */
7735 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7736 continue;
7738 /* Virtual base classes are at the end. */
7739 if (BINFO_VIRTUAL_P (base_binfo))
7740 continue;
7742 /* Check the base class. */
7743 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7746 if (fmem == &flexmems)
7748 /* Check virtual base classes only once per derived class.
7749 I.e., this check is not performed recursively for base
7750 classes. */
7751 int i;
7752 tree base_binfo;
7753 vec<tree, va_gc> *vbases;
7754 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7755 vec_safe_iterate (vbases, i, &base_binfo); i++)
7757 /* Check the virtual base class. */
7758 tree basetype = TREE_TYPE (base_binfo);
7760 check_flexarrays (basetype, fmem, /*base_p=*/true);
7764 /* Is the type unnamed (and therefore a member of it potentially
7765 an anonymous struct or union)? */
7766 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7767 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7768 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7770 /* Search the members of the current (possibly derived) class, skipping
7771 unnamed structs and unions since those could be anonymous. */
7772 if (fmem != &flexmems || !maybe_anon_p)
7773 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7775 if (fmem == &flexmems && !maybe_anon_p)
7777 /* Issue diagnostics for invalid flexible and zero-length array
7778 members found in base classes or among the members of the current
7779 class. Ignore anonymous structs and unions whose members are
7780 considered to be members of the enclosing class and thus will
7781 be diagnosed when checking it. */
7782 diagnose_flexarrays (t, fmem);
7786 /* Perform processing required when the definition of T (a class type)
7787 is complete. Diagnose invalid definitions of flexible array members
7788 and zero-size arrays. */
7790 void
7791 finish_struct_1 (tree t)
7793 tree x;
7794 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7795 tree virtuals = NULL_TREE;
7797 if (COMPLETE_TYPE_P (t))
7799 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7800 error ("redefinition of %q#T", t);
7801 popclass ();
7802 return;
7805 /* If this type was previously laid out as a forward reference,
7806 make sure we lay it out again. */
7807 TYPE_SIZE (t) = NULL_TREE;
7808 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7810 /* Make assumptions about the class; we'll reset the flags if
7811 necessary. */
7812 CLASSTYPE_EMPTY_P (t) = 1;
7813 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7814 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7815 CLASSTYPE_LITERAL_P (t) = true;
7817 /* Do end-of-class semantic processing: checking the validity of the
7818 bases and members and add implicitly generated methods. */
7819 check_bases_and_members (t);
7821 /* Find the key method. */
7822 if (TYPE_CONTAINS_VPTR_P (t))
7824 /* The Itanium C++ ABI permits the key method to be chosen when
7825 the class is defined -- even though the key method so
7826 selected may later turn out to be an inline function. On
7827 some systems (such as ARM Symbian OS) the key method cannot
7828 be determined until the end of the translation unit. On such
7829 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7830 will cause the class to be added to KEYED_CLASSES. Then, in
7831 finish_file we will determine the key method. */
7832 if (targetm.cxx.key_method_may_be_inline ())
7833 determine_key_method (t);
7835 /* If a polymorphic class has no key method, we may emit the vtable
7836 in every translation unit where the class definition appears. If
7837 we're devirtualizing, we can look into the vtable even if we
7838 aren't emitting it. */
7839 if (!CLASSTYPE_KEY_METHOD (t))
7840 vec_safe_push (keyed_classes, t);
7843 /* Layout the class itself. */
7844 layout_class_type (t, &virtuals);
7845 /* COMPLETE_TYPE_P is now true. */
7847 set_class_bindings (t);
7849 /* With the layout complete, check for flexible array members and
7850 zero-length arrays that might overlap other members in the final
7851 layout. */
7852 check_flexarrays (t);
7854 virtuals = modify_all_vtables (t, nreverse (virtuals));
7856 /* If necessary, create the primary vtable for this class. */
7857 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7859 /* We must enter these virtuals into the table. */
7860 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7861 build_primary_vtable (NULL_TREE, t);
7862 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7863 /* Here we know enough to change the type of our virtual
7864 function table, but we will wait until later this function. */
7865 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7867 /* If we're warning about ABI tags, check the types of the new
7868 virtual functions. */
7869 if (warn_abi_tag)
7870 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7871 check_abi_tags (t, TREE_VALUE (v));
7874 if (TYPE_CONTAINS_VPTR_P (t))
7876 int vindex;
7877 tree fn;
7879 if (BINFO_VTABLE (TYPE_BINFO (t)))
7880 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7881 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7882 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7884 /* Add entries for virtual functions introduced by this class. */
7885 BINFO_VIRTUALS (TYPE_BINFO (t))
7886 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7888 /* Set DECL_VINDEX for all functions declared in this class. */
7889 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7891 fn = TREE_CHAIN (fn),
7892 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7893 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7895 tree fndecl = BV_FN (fn);
7897 if (DECL_THUNK_P (fndecl))
7898 /* A thunk. We should never be calling this entry directly
7899 from this vtable -- we'd use the entry for the non
7900 thunk base function. */
7901 DECL_VINDEX (fndecl) = NULL_TREE;
7902 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7903 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7907 finish_struct_bits (t);
7909 set_method_tm_attributes (t);
7910 if (flag_openmp || flag_openmp_simd)
7911 finish_omp_declare_simd_methods (t);
7913 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7914 for any static member objects of the type we're working on. */
7915 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7916 if (DECL_DECLARES_FUNCTION_P (x))
7918 /* Synthesize constexpr defaulted comparisons. */
7919 if (!DECL_ARTIFICIAL (x)
7920 && DECL_DEFAULTED_IN_CLASS_P (x)
7921 && special_function_p (x) == sfk_comparison)
7922 defaulted_late_check (x);
7923 DECL_IN_AGGR_P (x) = false;
7925 else if (VAR_P (x) && TREE_STATIC (x)
7926 && TREE_TYPE (x) != error_mark_node
7927 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7928 SET_DECL_MODE (x, TYPE_MODE (t));
7930 /* Complain if one of the field types requires lower visibility. */
7931 constrain_class_visibility (t);
7933 /* Make the rtl for any new vtables we have created, and unmark
7934 the base types we marked. */
7935 finish_vtbls (t);
7937 /* Build the VTT for T. */
7938 build_vtt (t);
7940 if (warn_nonvdtor
7941 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7942 && !CLASSTYPE_FINAL (t))
7943 warning (OPT_Wnon_virtual_dtor,
7944 "%q#T has virtual functions and accessible"
7945 " non-virtual destructor", t);
7947 complete_vars (t);
7949 if (warn_overloaded_virtual)
7950 warn_hidden (t);
7952 /* Class layout, assignment of virtual table slots, etc., is now
7953 complete. Give the back end a chance to tweak the visibility of
7954 the class or perform any other required target modifications. */
7955 targetm.cxx.adjust_class_at_definition (t);
7957 maybe_suppress_debug_info (t);
7959 if (flag_vtable_verify)
7960 vtv_save_class_info (t);
7962 dump_class_hierarchy (t);
7964 /* Finish debugging output for this type. */
7965 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7967 if (TYPE_TRANSPARENT_AGGR (t))
7969 tree field = first_field (t);
7970 if (field == NULL_TREE || error_operand_p (field))
7972 error ("type transparent %q#T does not have any fields", t);
7973 TYPE_TRANSPARENT_AGGR (t) = 0;
7975 else if (DECL_ARTIFICIAL (field))
7977 if (DECL_FIELD_IS_BASE (field))
7978 error ("type transparent class %qT has base classes", t);
7979 else
7981 gcc_checking_assert (DECL_VIRTUAL_P (field));
7982 error ("type transparent class %qT has virtual functions", t);
7984 TYPE_TRANSPARENT_AGGR (t) = 0;
7986 else if (TYPE_MODE (t) != DECL_MODE (field))
7988 error ("type transparent %q#T cannot be made transparent because "
7989 "the type of the first field has a different ABI from the "
7990 "class overall", t);
7991 TYPE_TRANSPARENT_AGGR (t) = 0;
7996 /* When T was built up, the member declarations were added in reverse
7997 order. Rearrange them to declaration order. */
7999 void
8000 unreverse_member_declarations (tree t)
8002 tree next;
8003 tree prev;
8004 tree x;
8006 /* The following lists are all in reverse order. Put them in
8007 declaration order now. */
8008 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
8010 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
8011 order, so we can't just use nreverse. Due to stat_hack
8012 chicanery in finish_member_declaration. */
8013 prev = NULL_TREE;
8014 for (x = TYPE_FIELDS (t);
8015 x && TREE_CODE (x) != TYPE_DECL;
8016 x = next)
8018 next = DECL_CHAIN (x);
8019 DECL_CHAIN (x) = prev;
8020 prev = x;
8023 if (prev)
8025 DECL_CHAIN (TYPE_FIELDS (t)) = x;
8026 TYPE_FIELDS (t) = prev;
8030 /* Classes, structs or unions T marked with hotness attributes propagate
8031 the attribute to all methods. */
8033 void
8034 propagate_class_warmth_attribute (tree t)
8036 if (t == NULL_TREE
8037 || !(TREE_CODE (t) == RECORD_TYPE
8038 || TREE_CODE (t) == UNION_TYPE))
8039 return;
8041 tree class_has_cold_attr
8042 = lookup_attribute ("cold", TYPE_ATTRIBUTES (t));
8043 tree class_has_hot_attr
8044 = lookup_attribute ("hot", TYPE_ATTRIBUTES (t));
8046 if (class_has_cold_attr || class_has_hot_attr)
8047 for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f))
8048 if (DECL_DECLARES_FUNCTION_P (f))
8049 maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t);
8052 tree
8053 finish_struct (tree t, tree attributes)
8055 location_t saved_loc = input_location;
8057 /* Now that we've got all the field declarations, reverse everything
8058 as necessary. */
8059 unreverse_member_declarations (t);
8061 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8062 fixup_attribute_variants (t);
8064 /* Nadger the current location so that diagnostics point to the start of
8065 the struct, not the end. */
8066 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
8068 if (processing_template_decl)
8070 tree x;
8072 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8073 if (DECL_DECLARES_FUNCTION_P (x))
8075 DECL_IN_AGGR_P (x) = false;
8076 if (DECL_VIRTUAL_P (x))
8077 CLASSTYPE_NON_AGGREGATE (t) = true;
8079 else if (TREE_CODE (x) == FIELD_DECL)
8081 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
8082 CLASSTYPE_NON_AGGREGATE (t) = true;
8085 /* Also add a USING_DECL for operator=. We know there'll be (at
8086 least) one, but we don't know the signature(s). We want name
8087 lookup not to fail or recurse into bases. This isn't added
8088 to the template decl list so we drop this at instantiation
8089 time. */
8090 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
8091 NULL_TREE);
8092 DECL_CONTEXT (ass_op) = t;
8093 USING_DECL_SCOPE (ass_op) = t;
8094 DECL_DEPENDENT_P (ass_op) = true;
8095 DECL_ARTIFICIAL (ass_op) = true;
8096 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
8097 TYPE_FIELDS (t) = ass_op;
8099 TYPE_SIZE (t) = bitsize_zero_node;
8100 TYPE_SIZE_UNIT (t) = size_zero_node;
8101 /* COMPLETE_TYPE_P is now true. */
8103 set_class_bindings (t);
8105 /* We need to emit an error message if this type was used as a parameter
8106 and it is an abstract type, even if it is a template. We construct
8107 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
8108 account and we call complete_vars with this type, which will check
8109 the PARM_DECLS. Note that while the type is being defined,
8110 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
8111 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
8112 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
8113 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8114 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
8115 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
8116 complete_vars (t);
8118 /* Remember current #pragma pack value. */
8119 TYPE_PRECISION (t) = maximum_field_alignment;
8121 if (cxx_dialect < cxx20)
8123 if (!CLASSTYPE_NON_AGGREGATE (t)
8124 && type_has_user_provided_or_explicit_constructor (t))
8125 CLASSTYPE_NON_AGGREGATE (t) = 1;
8127 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
8128 CLASSTYPE_NON_AGGREGATE (t) = 1;
8130 /* Fix up any variants we've already built. */
8131 fixup_type_variants (t);
8133 else
8134 finish_struct_1 (t);
8135 /* COMPLETE_TYPE_P is now true. */
8137 maybe_warn_about_overly_private_class (t);
8139 if (is_std_init_list (t))
8141 /* People keep complaining that the compiler crashes on an invalid
8142 definition of initializer_list, so I guess we should explicitly
8143 reject it. What the compiler internals care about is that it's a
8144 template and has a pointer field followed by size_type field. */
8145 bool ok = false;
8146 if (processing_template_decl)
8148 tree f = next_aggregate_field (TYPE_FIELDS (t));
8149 if (f && TYPE_PTR_P (TREE_TYPE (f)))
8151 f = next_aggregate_field (DECL_CHAIN (f));
8152 if (f && same_type_p (TREE_TYPE (f), size_type_node))
8153 ok = true;
8156 /* It also cannot be a union. */
8157 ok &= NON_UNION_CLASS_TYPE_P (t);
8158 if (!ok)
8159 fatal_error (input_location, "definition of %qD does not match "
8160 "%<#include <initializer_list>%>", TYPE_NAME (t));
8163 input_location = saved_loc;
8165 TYPE_BEING_DEFINED (t) = 0;
8167 if (current_class_type)
8168 popclass ();
8169 else
8170 error ("trying to finish struct, but kicked out due to previous parse errors");
8172 if (flag_openmp)
8173 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
8174 if (TREE_CODE (decl) == FUNCTION_DECL
8175 && DECL_OBJECT_MEMBER_FUNCTION_P (decl))
8176 if (tree attr = lookup_attribute ("omp declare variant base",
8177 DECL_ATTRIBUTES (decl)))
8178 omp_declare_variant_finalize (decl, attr);
8180 if (processing_template_decl && at_function_scope_p ()
8181 /* Lambdas are defined by the LAMBDA_EXPR. */
8182 && !LAMBDA_TYPE_P (t))
8183 add_stmt (build_min (TAG_DEFN, t));
8185 return t;
8188 /* Hash table to avoid endless recursion when handling references. */
8189 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
8191 /* Return the dynamic type of INSTANCE, if known.
8192 Used to determine whether the virtual function table is needed
8193 or not.
8195 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8196 of our knowledge of its type. *NONNULL should be initialized
8197 before this function is called. */
8199 static tree
8200 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8202 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
8204 switch (TREE_CODE (instance))
8206 case INDIRECT_REF:
8207 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8208 return NULL_TREE;
8209 else
8210 return RECUR (TREE_OPERAND (instance, 0));
8212 case CALL_EXPR:
8213 /* This is a call to a constructor, hence it's never zero. */
8214 if (CALL_EXPR_FN (instance)
8215 && TREE_HAS_CONSTRUCTOR (instance))
8217 if (nonnull)
8218 *nonnull = 1;
8219 return TREE_TYPE (instance);
8221 return NULL_TREE;
8223 case SAVE_EXPR:
8224 /* This is a call to a constructor, hence it's never zero. */
8225 if (TREE_HAS_CONSTRUCTOR (instance))
8227 if (nonnull)
8228 *nonnull = 1;
8229 return TREE_TYPE (instance);
8231 return RECUR (TREE_OPERAND (instance, 0));
8233 case POINTER_PLUS_EXPR:
8234 case PLUS_EXPR:
8235 case MINUS_EXPR:
8236 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
8237 return RECUR (TREE_OPERAND (instance, 0));
8238 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
8239 /* Propagate nonnull. */
8240 return RECUR (TREE_OPERAND (instance, 0));
8242 return NULL_TREE;
8244 CASE_CONVERT:
8245 return RECUR (TREE_OPERAND (instance, 0));
8247 case ADDR_EXPR:
8248 instance = TREE_OPERAND (instance, 0);
8249 if (nonnull)
8251 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8252 with a real object -- given &p->f, p can still be null. */
8253 tree t = get_base_address (instance);
8254 /* ??? Probably should check DECL_WEAK here. */
8255 if (t && DECL_P (t))
8256 *nonnull = 1;
8258 return RECUR (instance);
8260 case COMPONENT_REF:
8261 /* If this component is really a base class reference, then the field
8262 itself isn't definitive. */
8263 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
8264 return RECUR (TREE_OPERAND (instance, 0));
8265 return RECUR (TREE_OPERAND (instance, 1));
8267 case VAR_DECL:
8268 case FIELD_DECL:
8269 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
8270 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8272 if (nonnull)
8273 *nonnull = 1;
8274 return TREE_TYPE (TREE_TYPE (instance));
8276 /* fall through. */
8277 case TARGET_EXPR:
8278 case PARM_DECL:
8279 case RESULT_DECL:
8280 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8282 if (nonnull)
8283 *nonnull = 1;
8284 return TREE_TYPE (instance);
8286 else if (instance == current_class_ptr)
8288 if (nonnull)
8289 *nonnull = 1;
8291 /* if we're in a ctor or dtor, we know our type. If
8292 current_class_ptr is set but we aren't in a function, we're in
8293 an NSDMI (and therefore a constructor). */
8294 if (current_scope () != current_function_decl
8295 || (DECL_LANG_SPECIFIC (current_function_decl)
8296 && (DECL_CONSTRUCTOR_P (current_function_decl)
8297 || DECL_DESTRUCTOR_P (current_function_decl))))
8299 if (cdtorp)
8300 *cdtorp = 1;
8301 return TREE_TYPE (TREE_TYPE (instance));
8304 else if (TYPE_REF_P (TREE_TYPE (instance)))
8306 /* We only need one hash table because it is always left empty. */
8307 if (!fixed_type_or_null_ref_ht)
8308 fixed_type_or_null_ref_ht
8309 = new hash_table<nofree_ptr_hash<tree_node> > (37);
8311 /* Reference variables should be references to objects. */
8312 if (nonnull)
8313 *nonnull = 1;
8315 /* Enter the INSTANCE in a table to prevent recursion; a
8316 variable's initializer may refer to the variable
8317 itself. */
8318 if (VAR_P (instance)
8319 && DECL_INITIAL (instance)
8320 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8321 && !fixed_type_or_null_ref_ht->find (instance))
8323 tree type;
8324 tree_node **slot;
8326 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8327 *slot = instance;
8328 type = RECUR (DECL_INITIAL (instance));
8329 fixed_type_or_null_ref_ht->remove_elt (instance);
8331 return type;
8334 return NULL_TREE;
8336 case VIEW_CONVERT_EXPR:
8337 if (location_wrapper_p (instance))
8338 return RECUR (TREE_OPERAND (instance, 0));
8339 else
8340 /* TODO: Recursion may be correct for some non-location-wrapper
8341 uses of VIEW_CONVERT_EXPR. */
8342 return NULL_TREE;
8344 default:
8345 return NULL_TREE;
8347 #undef RECUR
8350 /* Return nonzero if the dynamic type of INSTANCE is known, and
8351 equivalent to the static type. We also handle the case where
8352 INSTANCE is really a pointer. Return negative if this is a
8353 ctor/dtor. There the dynamic type is known, but this might not be
8354 the most derived base of the original object, and hence virtual
8355 bases may not be laid out according to this type.
8357 Used to determine whether the virtual function table is needed
8358 or not.
8360 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8361 of our knowledge of its type. *NONNULL should be initialized
8362 before this function is called. */
8365 resolves_to_fixed_type_p (tree instance, int* nonnull)
8367 tree t = TREE_TYPE (instance);
8368 int cdtorp = 0;
8369 tree fixed;
8371 /* processing_template_decl can be false in a template if we're in
8372 instantiate_non_dependent_expr, but we still want to suppress
8373 this check. */
8374 if (in_template_context)
8376 /* In a template we only care about the type of the result. */
8377 if (nonnull)
8378 *nonnull = true;
8379 return true;
8382 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8383 if (INDIRECT_TYPE_P (t))
8384 t = TREE_TYPE (t);
8385 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8386 return 1;
8387 if (fixed == NULL_TREE)
8388 return 0;
8389 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8390 return 0;
8391 return cdtorp ? -1 : 1;
8395 void
8396 init_class_processing (void)
8398 current_class_depth = 0;
8399 current_class_stack_size = 10;
8400 current_class_stack
8401 = XNEWVEC (struct class_stack_node, current_class_stack_size);
8402 sizeof_biggest_empty_class = size_zero_node;
8404 ridpointers[(int) RID_PUBLIC] = access_public_node;
8405 ridpointers[(int) RID_PRIVATE] = access_private_node;
8406 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8409 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8411 static void
8412 restore_class_cache (void)
8414 tree type;
8416 /* We are re-entering the same class we just left, so we don't
8417 have to search the whole inheritance matrix to find all the
8418 decls to bind again. Instead, we install the cached
8419 class_shadowed list and walk through it binding names. */
8420 push_binding_level (previous_class_level);
8421 class_binding_level = previous_class_level;
8422 /* Restore IDENTIFIER_TYPE_VALUE. */
8423 for (type = class_binding_level->type_shadowed;
8424 type;
8425 type = TREE_CHAIN (type))
8426 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8429 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8430 appropriate for TYPE.
8432 So that we may avoid calls to lookup_name, we cache the _TYPE
8433 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8435 For multiple inheritance, we perform a two-pass depth-first search
8436 of the type lattice. */
8438 void
8439 pushclass (tree type)
8441 class_stack_node_t csn;
8443 type = TYPE_MAIN_VARIANT (type);
8445 /* Make sure there is enough room for the new entry on the stack. */
8446 if (current_class_depth + 1 >= current_class_stack_size)
8448 current_class_stack_size *= 2;
8449 current_class_stack
8450 = XRESIZEVEC (struct class_stack_node, current_class_stack,
8451 current_class_stack_size);
8454 /* Insert a new entry on the class stack. */
8455 csn = current_class_stack + current_class_depth;
8456 csn->name = current_class_name;
8457 csn->type = current_class_type;
8458 csn->access = current_access_specifier;
8459 csn->names_used = 0;
8460 csn->hidden = 0;
8461 current_class_depth++;
8463 /* Now set up the new type. */
8464 current_class_name = TYPE_NAME (type);
8465 if (TREE_CODE (current_class_name) == TYPE_DECL)
8466 current_class_name = DECL_NAME (current_class_name);
8467 current_class_type = type;
8469 /* By default, things in classes are private, while things in
8470 structures or unions are public. */
8471 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8472 ? access_private_node
8473 : access_public_node);
8475 if (previous_class_level
8476 && type != previous_class_level->this_entity
8477 && current_class_depth == 1)
8479 /* Forcibly remove any old class remnants. */
8480 invalidate_class_lookup_cache ();
8483 if (!previous_class_level
8484 || type != previous_class_level->this_entity
8485 || current_class_depth > 1)
8486 pushlevel_class ();
8487 else
8488 restore_class_cache ();
8491 /* Get out of the current class scope. If we were in a class scope
8492 previously, that is the one popped to. */
8494 void
8495 popclass (void)
8497 poplevel_class ();
8499 current_class_depth--;
8500 current_class_name = current_class_stack[current_class_depth].name;
8501 current_class_type = current_class_stack[current_class_depth].type;
8502 current_access_specifier = current_class_stack[current_class_depth].access;
8503 if (current_class_stack[current_class_depth].names_used)
8504 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8507 /* Mark the top of the class stack as hidden. */
8509 void
8510 push_class_stack (void)
8512 if (current_class_depth)
8513 ++current_class_stack[current_class_depth - 1].hidden;
8516 /* Mark the top of the class stack as un-hidden. */
8518 void
8519 pop_class_stack (void)
8521 if (current_class_depth)
8522 --current_class_stack[current_class_depth - 1].hidden;
8525 /* If the class type currently being defined is either T or
8526 a nested type of T, returns the type from the current_class_stack,
8527 which might be equivalent to but not equal to T in case of
8528 constrained partial specializations. */
8530 tree
8531 currently_open_class (tree t)
8533 int i;
8535 if (!CLASS_TYPE_P (t))
8536 return NULL_TREE;
8538 t = TYPE_MAIN_VARIANT (t);
8540 /* We start looking from 1 because entry 0 is from global scope,
8541 and has no type. */
8542 for (i = current_class_depth; i > 0; --i)
8544 tree c;
8545 if (i == current_class_depth)
8546 c = current_class_type;
8547 else
8549 if (current_class_stack[i].hidden)
8550 break;
8551 c = current_class_stack[i].type;
8553 if (!c)
8554 continue;
8555 if (same_type_p (c, t))
8556 return c;
8558 return NULL_TREE;
8561 /* If either current_class_type or one of its enclosing classes are derived
8562 from T, return the appropriate type. Used to determine how we found
8563 something via unqualified lookup. */
8565 tree
8566 currently_open_derived_class (tree t)
8568 int i;
8570 /* The bases of a dependent type are unknown. */
8571 if (dependent_type_p (t))
8572 return NULL_TREE;
8574 if (!current_class_type)
8575 return NULL_TREE;
8577 if (DERIVED_FROM_P (t, current_class_type))
8578 return current_class_type;
8580 for (i = current_class_depth - 1; i > 0; --i)
8582 if (current_class_stack[i].hidden)
8583 break;
8584 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8585 return current_class_stack[i].type;
8588 return NULL_TREE;
8591 /* Return the outermost enclosing class type that is still open, or
8592 NULL_TREE. */
8594 tree
8595 outermost_open_class (void)
8597 if (!current_class_type)
8598 return NULL_TREE;
8599 tree r = NULL_TREE;
8600 if (TYPE_BEING_DEFINED (current_class_type))
8601 r = current_class_type;
8602 for (int i = current_class_depth - 1; i > 0; --i)
8604 if (current_class_stack[i].hidden)
8605 break;
8606 tree t = current_class_stack[i].type;
8607 if (!TYPE_BEING_DEFINED (t))
8608 break;
8609 r = t;
8611 return r;
8614 /* Returns the innermost class type which is not a lambda closure type. */
8616 tree
8617 current_nonlambda_class_type (void)
8619 tree type = current_class_type;
8620 while (type && LAMBDA_TYPE_P (type))
8621 type = decl_type_context (TYPE_NAME (type));
8622 return type;
8625 /* When entering a class scope, all enclosing class scopes' names with
8626 static meaning (static variables, static functions, types and
8627 enumerators) have to be visible. This recursive function calls
8628 pushclass for all enclosing class contexts until global or a local
8629 scope is reached. TYPE is the enclosed class. */
8631 void
8632 push_nested_class (tree type)
8634 /* A namespace might be passed in error cases, like A::B:C. */
8635 if (type == NULL_TREE
8636 || !CLASS_TYPE_P (type))
8637 return;
8639 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8641 pushclass (type);
8644 /* Undoes a push_nested_class call. */
8646 void
8647 pop_nested_class (void)
8649 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8651 popclass ();
8652 if (context && CLASS_TYPE_P (context))
8653 pop_nested_class ();
8656 /* Returns the number of extern "LANG" blocks we are nested within. */
8659 current_lang_depth (void)
8661 return vec_safe_length (current_lang_base);
8664 /* Set global variables CURRENT_LANG_NAME to appropriate value
8665 so that behavior of name-mangling machinery is correct. */
8667 void
8668 push_lang_context (tree name)
8670 vec_safe_push (current_lang_base, current_lang_name);
8672 if (name == lang_name_cplusplus)
8673 current_lang_name = name;
8674 else if (name == lang_name_c)
8675 current_lang_name = name;
8676 else
8677 error ("language string %<\"%E\"%> not recognized", name);
8680 /* Get out of the current language scope. */
8682 void
8683 pop_lang_context (void)
8685 current_lang_name = current_lang_base->pop ();
8688 /* Type instantiation routines. */
8690 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8691 matches the TARGET_TYPE. If there is no satisfactory match, return
8692 error_mark_node, and issue an error & warning messages under
8693 control of FLAGS. Permit pointers to member function if FLAGS
8694 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8695 a template-id, and EXPLICIT_TARGS are the explicitly provided
8696 template arguments.
8698 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8699 is the base path used to reference those member functions. If
8700 the address is resolved to a member function, access checks will be
8701 performed and errors issued if appropriate. */
8703 static tree
8704 resolve_address_of_overloaded_function (tree target_type,
8705 tree overload,
8706 tsubst_flags_t complain,
8707 bool template_only,
8708 tree explicit_targs,
8709 tree access_path)
8711 /* Here's what the standard says:
8713 [over.over]
8715 If the name is a function template, template argument deduction
8716 is done, and if the argument deduction succeeds, the deduced
8717 arguments are used to generate a single template function, which
8718 is added to the set of overloaded functions considered.
8720 Non-member functions and static member functions match targets of
8721 type "pointer-to-function" or "reference-to-function." Nonstatic
8722 member functions match targets of type "pointer-to-member
8723 function;" the function type of the pointer to member is used to
8724 select the member function from the set of overloaded member
8725 functions. If a non-static member function is selected, the
8726 reference to the overloaded function name is required to have the
8727 form of a pointer to member as described in 5.3.1.
8729 If more than one function is selected, any template functions in
8730 the set are eliminated if the set also contains a non-template
8731 function, and any given template function is eliminated if the
8732 set contains a second template function that is more specialized
8733 than the first according to the partial ordering rules 14.5.5.2.
8734 After such eliminations, if any, there shall remain exactly one
8735 selected function. */
8737 int is_ptrmem = 0;
8738 /* We store the matches in a TREE_LIST rooted here. The functions
8739 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8740 interoperability with most_specialized_instantiation. */
8741 tree matches = NULL_TREE;
8742 tree fn;
8743 tree target_fn_type;
8745 /* By the time we get here, we should be seeing only real
8746 pointer-to-member types, not the internal POINTER_TYPE to
8747 METHOD_TYPE representation. */
8748 gcc_assert (!TYPE_PTR_P (target_type)
8749 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8751 gcc_assert (is_overloaded_fn (overload));
8753 /* Check that the TARGET_TYPE is reasonable. */
8754 if (TYPE_PTRFN_P (target_type)
8755 || TYPE_REFFN_P (target_type))
8756 /* This is OK. */;
8757 else if (TYPE_PTRMEMFUNC_P (target_type))
8758 /* This is OK, too. */
8759 is_ptrmem = 1;
8760 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8761 /* This is OK, too. This comes from a conversion to reference
8762 type. */
8763 target_type = build_reference_type (target_type);
8764 else
8766 if (complain & tf_error)
8767 error ("cannot resolve overloaded function %qD based on"
8768 " conversion to type %qT",
8769 OVL_NAME (overload), target_type);
8770 return error_mark_node;
8773 /* Non-member functions and static member functions match targets of type
8774 "pointer-to-function" or "reference-to-function." Nonstatic member
8775 functions match targets of type "pointer-to-member-function;" the
8776 function type of the pointer to member is used to select the member
8777 function from the set of overloaded member functions.
8779 So figure out the FUNCTION_TYPE that we want to match against. */
8780 target_fn_type = static_fn_type (target_type);
8782 /* If we can find a non-template function that matches, we can just
8783 use it. There's no point in generating template instantiations
8784 if we're just going to throw them out anyhow. But, of course, we
8785 can only do this when we don't *need* a template function. */
8786 if (!template_only)
8787 for (lkp_iterator iter (overload); iter; ++iter)
8789 tree fn = *iter;
8791 if (TREE_CODE (fn) == TEMPLATE_DECL)
8792 /* We're not looking for templates just yet. */
8793 continue;
8795 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8796 /* We're looking for a non-static member, and this isn't
8797 one, or vice versa. */
8798 continue;
8800 /* Constraints must be satisfied. This is done before
8801 return type deduction since that instantiates the
8802 function. */
8803 if (!constraints_satisfied_p (fn))
8804 continue;
8806 if (undeduced_auto_decl (fn))
8808 /* Force instantiation to do return type deduction. */
8809 maybe_instantiate_decl (fn);
8810 require_deduced_type (fn);
8813 /* In C++17 we need the noexcept-qualifier to compare types. */
8814 if (flag_noexcept_type
8815 && !maybe_instantiate_noexcept (fn, complain))
8816 continue;
8818 /* See if there's a match. */
8819 tree fntype = static_fn_type (fn);
8820 if (same_type_p (target_fn_type, fntype)
8821 || fnptr_conv_p (target_fn_type, fntype))
8822 matches = tree_cons (fn, NULL_TREE, matches);
8825 /* Now, if we've already got a match (or matches), there's no need
8826 to proceed to the template functions. But, if we don't have a
8827 match we need to look at them, too. */
8828 if (!matches)
8830 tree target_arg_types;
8831 tree target_ret_type;
8832 tree *args;
8833 unsigned int nargs, ia;
8834 tree arg;
8836 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8837 target_ret_type = TREE_TYPE (target_fn_type);
8839 nargs = list_length (target_arg_types);
8840 args = XALLOCAVEC (tree, nargs);
8841 for (arg = target_arg_types, ia = 0;
8842 arg != NULL_TREE;
8843 arg = TREE_CHAIN (arg), ++ia)
8844 args[ia] = TREE_VALUE (arg);
8845 nargs = ia;
8847 for (lkp_iterator iter (overload); iter; ++iter)
8849 tree fn = *iter;
8850 tree instantiation;
8851 tree targs;
8853 if (TREE_CODE (fn) != TEMPLATE_DECL)
8854 /* We're only looking for templates. */
8855 continue;
8857 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8858 != is_ptrmem)
8859 /* We're not looking for a non-static member, and this is
8860 one, or vice versa. */
8861 continue;
8863 tree ret = target_ret_type;
8865 /* If the template has a deduced return type, don't expose it to
8866 template argument deduction. */
8867 if (undeduced_auto_decl (fn))
8868 ret = NULL_TREE;
8870 /* Try to do argument deduction. */
8871 targs = make_tree_vec (DECL_NTPARMS (fn));
8872 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8873 nargs, ret,
8874 DEDUCE_EXACT, LOOKUP_NORMAL,
8875 NULL, false, false);
8876 if (instantiation == error_mark_node)
8877 /* Instantiation failed. */
8878 continue;
8880 /* Constraints must be satisfied. This is done before
8881 return type deduction since that instantiates the
8882 function. */
8883 if (flag_concepts && !constraints_satisfied_p (instantiation))
8884 continue;
8886 /* And now force instantiation to do return type deduction. */
8887 if (undeduced_auto_decl (instantiation))
8889 ++function_depth;
8890 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8891 --function_depth;
8893 require_deduced_type (instantiation);
8896 /* In C++17 we need the noexcept-qualifier to compare types. */
8897 if (flag_noexcept_type)
8898 maybe_instantiate_noexcept (instantiation, complain);
8900 /* See if there's a match. */
8901 tree fntype = static_fn_type (instantiation);
8902 if (same_type_p (target_fn_type, fntype)
8903 || fnptr_conv_p (target_fn_type, fntype))
8904 matches = tree_cons (instantiation, fn, matches);
8907 /* Now, remove all but the most specialized of the matches. */
8908 if (matches)
8910 tree match = most_specialized_instantiation (matches);
8912 if (match != error_mark_node)
8913 matches = tree_cons (TREE_PURPOSE (match),
8914 NULL_TREE,
8915 NULL_TREE);
8919 /* Now we should have exactly one function in MATCHES. */
8920 if (matches == NULL_TREE)
8922 /* There were *no* matches. */
8923 if (complain & tf_error)
8925 error ("no matches converting function %qD to type %q#T",
8926 OVL_NAME (overload), target_type);
8928 print_candidates (overload);
8930 return error_mark_node;
8932 else if (TREE_CHAIN (matches))
8934 /* There were too many matches. First check if they're all
8935 the same function. */
8936 tree match = NULL_TREE;
8938 fn = TREE_PURPOSE (matches);
8940 /* For multi-versioned functions, more than one match is just fine and
8941 decls_match will return false as they are different. */
8942 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8943 if (!decls_match (fn, TREE_PURPOSE (match))
8944 && !targetm.target_option.function_versions
8945 (fn, TREE_PURPOSE (match)))
8946 break;
8948 if (match)
8950 if (complain & tf_error)
8952 error ("converting overloaded function %qD to type %q#T is ambiguous",
8953 OVL_NAME (overload), target_type);
8955 /* Since print_candidates expects the functions in the
8956 TREE_VALUE slot, we flip them here. */
8957 for (match = matches; match; match = TREE_CHAIN (match))
8958 TREE_VALUE (match) = TREE_PURPOSE (match);
8960 print_candidates (matches);
8963 return error_mark_node;
8967 /* Good, exactly one match. Now, convert it to the correct type. */
8968 fn = TREE_PURPOSE (matches);
8970 if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
8971 && !(complain & tf_ptrmem_ok))
8973 /* Previously we allowed this behavior for iobj member functions when the
8974 -fms-extensions flag is passed as MSVC allows this as a language
8975 extension. MSVC also allows this for xobj member functions, but the
8976 documentation for -fms-extensions states it's purpose is to support
8977 the use of microsoft headers. Until otherwise demonstrated, we should
8978 assume xobj member functions are not used in this manner in microsoft
8979 headers and indiscriminately forbid the incorrect syntax instead of
8980 supporting it for non-legacy uses. This should hopefully encourage
8981 conformance going forward.
8982 This comment is referred to in typeck.cc:cp_build_addr_expr_1. */
8983 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions)
8984 /* Early escape. */;
8985 else if (!(complain & tf_error))
8986 return error_mark_node;
8987 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
8989 auto_diagnostic_group d;
8990 /* Should match the error in typeck.cc:cp_build_addr_expr_1.
8991 We seem to lack the details here to match that diagnostic exactly,
8992 perhaps this could be fixed in the future? See PR113075 bug 2. */
8993 error_at (input_location,
8994 "ISO C++ forbids taking the address of an unqualified"
8995 " or parenthesized non-static member function to form"
8996 " a pointer to explicit object member function.");
8997 /* This is incorrect, see PR113075 bug 3. */
8998 inform (input_location,
8999 "a pointer to explicit object member function can only be "
9000 "formed with %<&%E%>", fn);
9002 else
9004 static int explained;
9005 gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions);
9006 /* Is there a reason this error message doesn't match the one in
9007 typeck.cc:cp_build_addr_expr_1? */
9008 auto_diagnostic_group d;
9009 if (permerror (input_location, "assuming pointer to member %qD", fn)
9010 && !explained)
9012 inform (input_location, "(a pointer to member can only be "
9013 "formed with %<&%E%>)", fn);
9014 explained = 1;
9019 /* If a pointer to a function that is multi-versioned is requested, the
9020 pointer to the dispatcher function is returned instead. This works
9021 well because indirectly calling the function will dispatch the right
9022 function version at run-time. */
9023 if (DECL_FUNCTION_VERSIONED (fn))
9025 fn = get_function_version_dispatcher (fn);
9026 if (fn == NULL)
9027 return error_mark_node;
9028 /* Mark all the versions corresponding to the dispatcher as used. */
9029 if (!(complain & tf_conv))
9030 mark_versions_used (fn);
9033 /* If we're doing overload resolution purely for the purpose of
9034 determining conversion sequences, we should not consider the
9035 function used. If this conversion sequence is selected, the
9036 function will be marked as used at this point. */
9037 if (!(complain & tf_conv))
9039 /* Make =delete work with SFINAE. */
9040 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
9041 return error_mark_node;
9042 if (!mark_used (fn, complain) && !(complain & tf_error))
9043 return error_mark_node;
9046 /* We could not check access to member functions when this
9047 expression was originally created since we did not know at that
9048 time to which function the expression referred. */
9049 if (DECL_FUNCTION_MEMBER_P (fn))
9051 gcc_assert (access_path);
9052 perform_or_defer_access_check (access_path, fn, fn, complain);
9055 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
9056 return cp_build_addr_expr (fn, complain);
9057 else
9059 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
9060 will mark the function as addressed, but here we must do it
9061 explicitly. */
9062 cxx_mark_addressable (fn);
9064 return fn;
9068 /* This function will instantiate the type of the expression given in
9069 RHS to match the type of LHSTYPE. If errors exist, then return
9070 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
9071 we complain on errors. If we are not complaining, never modify rhs,
9072 as overload resolution wants to try many possible instantiations, in
9073 the hope that at least one will work.
9075 For non-recursive calls, LHSTYPE should be a function, pointer to
9076 function, or a pointer to member function. */
9078 tree
9079 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
9081 tsubst_flags_t complain_in = complain;
9082 tree access_path = NULL_TREE;
9084 complain &= ~tf_ptrmem_ok;
9086 STRIP_ANY_LOCATION_WRAPPER (rhs);
9088 if (lhstype == unknown_type_node)
9090 if (complain & tf_error)
9091 error ("not enough type information");
9092 return error_mark_node;
9095 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
9097 tree fntype = non_reference (lhstype);
9098 if (same_type_p (fntype, TREE_TYPE (rhs)))
9099 return rhs;
9100 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
9101 return rhs;
9102 if (flag_ms_extensions
9103 && TYPE_PTRMEMFUNC_P (fntype)
9104 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
9105 /* Microsoft allows `A::f' to be resolved to a
9106 pointer-to-member. */
9108 else
9110 if (complain & tf_error)
9111 error ("cannot convert %qE from type %qT to type %qT",
9112 rhs, TREE_TYPE (rhs), fntype);
9113 return error_mark_node;
9117 /* If we instantiate a template, and it is a A ?: C expression
9118 with omitted B, look through the SAVE_EXPR. */
9119 if (TREE_CODE (rhs) == SAVE_EXPR)
9120 rhs = TREE_OPERAND (rhs, 0);
9122 if (BASELINK_P (rhs))
9124 access_path = BASELINK_ACCESS_BINFO (rhs);
9125 rhs = BASELINK_FUNCTIONS (rhs);
9128 /* There are only a few kinds of expressions that may have a type
9129 dependent on overload resolution. */
9130 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
9131 || TREE_CODE (rhs) == COMPONENT_REF
9132 || is_overloaded_fn (rhs)
9133 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
9135 /* This should really only be used when attempting to distinguish
9136 what sort of a pointer to function we have. For now, any
9137 arithmetic operation which is not supported on pointers
9138 is rejected as an error. */
9140 switch (TREE_CODE (rhs))
9142 case COMPONENT_REF:
9144 tree member = TREE_OPERAND (rhs, 1);
9146 member = instantiate_type (lhstype, member, complain);
9147 if (member != error_mark_node
9148 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
9149 /* Do not lose object's side effects. */
9150 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
9151 TREE_OPERAND (rhs, 0), member);
9152 return member;
9155 case OFFSET_REF:
9156 rhs = TREE_OPERAND (rhs, 1);
9157 if (BASELINK_P (rhs))
9158 return instantiate_type (lhstype, rhs, complain_in);
9160 /* This can happen if we are forming a pointer-to-member for a
9161 member template. */
9162 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
9164 /* Fall through. */
9166 case TEMPLATE_ID_EXPR:
9168 tree fns = TREE_OPERAND (rhs, 0);
9169 tree args = TREE_OPERAND (rhs, 1);
9171 return
9172 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
9173 /*template_only=*/true,
9174 args, access_path);
9177 case OVERLOAD:
9178 case FUNCTION_DECL:
9179 return
9180 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
9181 /*template_only=*/false,
9182 /*explicit_targs=*/NULL_TREE,
9183 access_path);
9185 case ADDR_EXPR:
9187 if (PTRMEM_OK_P (rhs))
9188 complain |= tf_ptrmem_ok;
9190 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
9193 case ERROR_MARK:
9194 return error_mark_node;
9196 default:
9197 gcc_unreachable ();
9199 return error_mark_node;
9202 /* Return the name of the virtual function pointer field
9203 (as an IDENTIFIER_NODE) for the given TYPE. Note that
9204 this may have to look back through base types to find the
9205 ultimate field name. (For single inheritance, these could
9206 all be the same name. Who knows for multiple inheritance). */
9208 static tree
9209 get_vfield_name (tree type)
9211 tree binfo, base_binfo;
9213 for (binfo = TYPE_BINFO (type);
9214 BINFO_N_BASE_BINFOS (binfo);
9215 binfo = base_binfo)
9217 base_binfo = BINFO_BASE_BINFO (binfo, 0);
9219 if (BINFO_VIRTUAL_P (base_binfo)
9220 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
9221 break;
9224 type = BINFO_TYPE (binfo);
9225 tree ctor_name = constructor_name (type);
9226 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
9227 + IDENTIFIER_LENGTH (ctor_name) + 2);
9228 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
9229 return get_identifier (buf);
9232 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
9233 according to [class]:
9234 The class-name is also inserted
9235 into the scope of the class itself. For purposes of access checking,
9236 the inserted class name is treated as if it were a public member name. */
9238 void
9239 build_self_reference (void)
9241 tree name = DECL_NAME (TYPE_NAME (current_class_type));
9242 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
9244 DECL_NONLOCAL (decl) = 1;
9245 DECL_CONTEXT (decl) = current_class_type;
9246 DECL_ARTIFICIAL (decl) = 1;
9247 SET_DECL_SELF_REFERENCE_P (decl);
9248 set_underlying_type (decl);
9249 set_instantiating_module (decl);
9251 if (processing_template_decl)
9252 decl = push_template_decl (decl);
9254 tree saved_cas = current_access_specifier;
9255 current_access_specifier = access_public_node;
9256 finish_member_declaration (decl);
9257 current_access_specifier = saved_cas;
9260 /* Returns 1 if TYPE contains only padding bytes. */
9263 is_empty_class (tree type)
9265 if (type == error_mark_node)
9266 return 0;
9268 if (! CLASS_TYPE_P (type))
9269 return 0;
9271 return CLASSTYPE_EMPTY_P (type);
9274 /* Returns true if TYPE contains no actual data, just various
9275 possible combinations of empty classes. If IGNORE_VPTR is true,
9276 a vptr doesn't prevent the class from being considered empty. Typically
9277 we want to ignore the vptr on assignment, and not on initialization. */
9279 bool
9280 is_really_empty_class (tree type, bool ignore_vptr)
9282 if (CLASS_TYPE_P (type))
9284 tree field;
9285 tree binfo;
9286 tree base_binfo;
9287 int i;
9289 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9290 out, but we'd like to be able to check this before then. */
9291 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
9292 return true;
9294 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
9295 return false;
9297 for (binfo = TYPE_BINFO (type), i = 0;
9298 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9299 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
9300 return false;
9301 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9302 if (TREE_CODE (field) == FIELD_DECL
9303 && !DECL_ARTIFICIAL (field)
9304 /* An unnamed bit-field is not a data member. */
9305 && !DECL_UNNAMED_BIT_FIELD (field)
9306 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9307 return false;
9308 return true;
9310 else if (TREE_CODE (type) == ARRAY_TYPE)
9311 return (integer_zerop (array_type_nelts_top (type))
9312 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9313 return false;
9316 /* Note that NAME was looked up while the current class was being
9317 defined and that the result of that lookup was DECL. */
9319 void
9320 maybe_note_name_used_in_class (tree name, tree decl)
9322 /* If we're not defining a class, there's nothing to do. */
9323 if (!(innermost_scope_kind() == sk_class
9324 && TYPE_BEING_DEFINED (current_class_type)
9325 && !LAMBDA_TYPE_P (current_class_type)))
9326 return;
9328 const cp_binding_level *blev = nullptr;
9329 if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9330 blev = binding->scope;
9331 const cp_binding_level *lev = current_binding_level;
9333 /* Record the binding in the names_used tables for classes inside blev. */
9334 for (int i = current_class_depth; i > 0; --i)
9336 tree type = (i == current_class_depth
9337 ? current_class_type
9338 : current_class_stack[i].type);
9340 for (; lev; lev = lev->level_chain)
9342 if (lev == blev)
9343 /* We found the declaration. */
9344 return;
9345 if (lev->kind == sk_class && lev->this_entity == type)
9346 /* This class is inside the declaration scope. */
9347 break;
9350 auto &names_used = current_class_stack[i-1].names_used;
9351 if (!names_used)
9352 names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9354 tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9355 TREE_TYPE (decl), decl);
9356 EXPR_LOCATION_WRAPPER_P (use) = 1;
9357 splay_tree_insert (names_used,
9358 (splay_tree_key) name,
9359 (splay_tree_value) use);
9363 /* Note that NAME was declared (as DECL) in the current class. Check
9364 to see that the declaration is valid under [class.member.lookup]:
9366 If [the result of a search in T for N at point P] differs from the result of
9367 a search in T for N from immediately after the class-specifier of T, the
9368 program is ill-formed, no diagnostic required. */
9370 void
9371 note_name_declared_in_class (tree name, tree decl)
9373 splay_tree names_used;
9374 splay_tree_node n;
9376 /* Look to see if we ever used this name. */
9377 names_used
9378 = current_class_stack[current_class_depth - 1].names_used;
9379 if (!names_used)
9380 return;
9381 /* The C language allows members to be declared with a type of the same
9382 name, and the C++ standard says this diagnostic is not required. So
9383 allow it in extern "C" blocks unless pedantic is specified.
9384 Allow it in all cases if -ms-extensions is specified. */
9385 if ((!pedantic && current_lang_name == lang_name_c)
9386 || flag_ms_extensions)
9387 return;
9388 n = splay_tree_lookup (names_used, (splay_tree_key) name);
9389 if (n)
9391 tree use = (tree) n->value;
9392 location_t loc = EXPR_LOCATION (use);
9393 tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9394 /* [basic.scope.class]
9396 A name N used in a class S shall refer to the same declaration
9397 in its context and when re-evaluated in the completed scope of
9398 S. */
9399 auto ov = make_temp_override (global_dc->m_pedantic_errors);
9400 if (TREE_CODE (decl) == TYPE_DECL
9401 && TREE_CODE (olddecl) == TYPE_DECL
9402 && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9403 /* Different declaration, but same meaning; just warn. */;
9404 else if (flag_permissive)
9405 /* Let -fpermissive make it a warning like past versions. */;
9406 else
9407 /* Make it an error. */
9408 global_dc->m_pedantic_errors = 1;
9409 if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9410 "declaration of %q#D changes meaning of %qD",
9411 decl, OVL_NAME (decl)))
9413 inform (loc, "used here to mean %q#D", olddecl);
9414 inform (location_of (olddecl), "declared here" );
9419 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9420 Secondary vtables are merged with primary vtables; this function
9421 will return the VAR_DECL for the primary vtable. */
9423 tree
9424 get_vtbl_decl_for_binfo (tree binfo)
9426 tree decl;
9428 decl = BINFO_VTABLE (binfo);
9429 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9431 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9432 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9434 if (decl)
9435 gcc_assert (VAR_P (decl));
9436 return decl;
9440 /* Returns the binfo for the primary base of BINFO. If the resulting
9441 BINFO is a virtual base, and it is inherited elsewhere in the
9442 hierarchy, then the returned binfo might not be the primary base of
9443 BINFO in the complete object. Check BINFO_PRIMARY_P or
9444 BINFO_LOST_PRIMARY_P to be sure. */
9446 static tree
9447 get_primary_binfo (tree binfo)
9449 tree primary_base;
9451 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9452 if (!primary_base)
9453 return NULL_TREE;
9455 return copied_binfo (primary_base, binfo);
9458 /* As above, but iterate until we reach the binfo that actually provides the
9459 vptr for BINFO. */
9461 static tree
9462 most_primary_binfo (tree binfo)
9464 tree b = binfo;
9465 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9466 && !BINFO_LOST_PRIMARY_P (b))
9468 tree primary_base = get_primary_binfo (b);
9469 gcc_assert (BINFO_PRIMARY_P (primary_base)
9470 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9471 b = primary_base;
9473 return b;
9476 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9477 type. Note that the virtual inheritance might be above or below BINFO in
9478 the hierarchy. */
9480 bool
9481 vptr_via_virtual_p (tree binfo)
9483 if (TYPE_P (binfo))
9484 binfo = TYPE_BINFO (binfo);
9485 tree primary = most_primary_binfo (binfo);
9486 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9487 a morally virtual base. */
9488 tree virt = binfo_via_virtual (primary, NULL_TREE);
9489 return virt != NULL_TREE;
9492 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9494 static int
9495 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9497 if (!indented_p)
9498 fprintf (stream, "%*s", indent, "");
9499 return 1;
9502 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9503 INDENT should be zero when called from the top level; it is
9504 incremented recursively. IGO indicates the next expected BINFO in
9505 inheritance graph ordering. */
9507 static tree
9508 dump_class_hierarchy_r (FILE *stream,
9509 dump_flags_t flags,
9510 tree binfo,
9511 tree igo,
9512 int indent)
9514 int indented = 0;
9515 tree base_binfo;
9516 int i;
9518 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9519 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9520 (HOST_WIDE_INT) (uintptr_t) binfo);
9521 if (binfo != igo)
9523 fprintf (stream, "alternative-path\n");
9524 return igo;
9526 igo = TREE_CHAIN (binfo);
9528 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9529 tree_to_shwi (BINFO_OFFSET (binfo)));
9530 if (is_empty_class (BINFO_TYPE (binfo)))
9531 fprintf (stream, " empty");
9532 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9533 fprintf (stream, " nearly-empty");
9534 if (BINFO_VIRTUAL_P (binfo))
9535 fprintf (stream, " virtual");
9536 fprintf (stream, "\n");
9538 if (BINFO_PRIMARY_P (binfo))
9540 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9541 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9542 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9543 TFF_PLAIN_IDENTIFIER),
9544 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9546 if (BINFO_LOST_PRIMARY_P (binfo))
9548 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9549 fprintf (stream, " lost-primary");
9551 if (indented)
9552 fprintf (stream, "\n");
9554 if (!(flags & TDF_SLIM))
9556 int indented = 0;
9558 if (BINFO_SUBVTT_INDEX (binfo))
9560 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9561 fprintf (stream, " subvttidx=%s",
9562 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9563 TFF_PLAIN_IDENTIFIER));
9565 if (BINFO_VPTR_INDEX (binfo))
9567 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9568 fprintf (stream, " vptridx=%s",
9569 expr_as_string (BINFO_VPTR_INDEX (binfo),
9570 TFF_PLAIN_IDENTIFIER));
9572 if (BINFO_VPTR_FIELD (binfo))
9574 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9575 fprintf (stream, " vbaseoffset=%s",
9576 expr_as_string (BINFO_VPTR_FIELD (binfo),
9577 TFF_PLAIN_IDENTIFIER));
9579 if (BINFO_VTABLE (binfo))
9581 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9582 fprintf (stream, " vptr=%s",
9583 expr_as_string (BINFO_VTABLE (binfo),
9584 TFF_PLAIN_IDENTIFIER));
9587 if (indented)
9588 fprintf (stream, "\n");
9591 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9592 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9594 return igo;
9597 /* Dump the BINFO hierarchy for T. */
9599 static void
9600 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9602 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9603 fprintf (stream, " size=%lu align=%lu\n",
9604 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
9605 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
9606 if (tree as_base = CLASSTYPE_AS_BASE (t))
9607 fprintf (stream, " base size=%lu base align=%lu\n",
9608 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base))
9609 / BITS_PER_UNIT),
9610 (unsigned long)(TYPE_ALIGN (as_base) / BITS_PER_UNIT));
9611 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9612 fprintf (stream, "\n");
9615 /* Debug interface to hierarchy dumping. */
9617 void
9618 debug_class (tree t)
9620 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9623 static void
9624 dump_class_hierarchy (tree t)
9626 dump_flags_t flags;
9627 if (FILE *stream = dump_begin (class_dump_id, &flags))
9629 dump_class_hierarchy_1 (stream, flags, t);
9630 dump_end (class_dump_id, stream);
9634 static void
9635 dump_array (FILE * stream, tree decl)
9637 tree value;
9638 unsigned HOST_WIDE_INT ix;
9639 HOST_WIDE_INT elt;
9640 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9642 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9643 / BITS_PER_UNIT);
9644 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9645 fprintf (stream, " %s entries",
9646 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9647 TFF_PLAIN_IDENTIFIER));
9648 fprintf (stream, "\n");
9650 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9651 ix, value)
9652 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9653 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9656 static void
9657 dump_vtable (tree t, tree binfo, tree vtable)
9659 dump_flags_t flags;
9660 FILE *stream = dump_begin (class_dump_id, &flags);
9662 if (!stream)
9663 return;
9665 if (!(flags & TDF_SLIM))
9667 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9669 fprintf (stream, "%s for %s",
9670 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9671 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9672 if (ctor_vtbl_p)
9674 if (!BINFO_VIRTUAL_P (binfo))
9675 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9676 (HOST_WIDE_INT) (uintptr_t) binfo);
9677 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9679 fprintf (stream, "\n");
9680 dump_array (stream, vtable);
9681 fprintf (stream, "\n");
9684 dump_end (class_dump_id, stream);
9687 static void
9688 dump_vtt (tree t, tree vtt)
9690 dump_flags_t flags;
9691 FILE *stream = dump_begin (class_dump_id, &flags);
9693 if (!stream)
9694 return;
9696 if (!(flags & TDF_SLIM))
9698 fprintf (stream, "VTT for %s\n",
9699 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9700 dump_array (stream, vtt);
9701 fprintf (stream, "\n");
9704 dump_end (class_dump_id, stream);
9707 /* Dump a function or thunk and its thunkees. */
9709 static void
9710 dump_thunk (FILE *stream, int indent, tree thunk)
9712 static const char spaces[] = " ";
9713 tree name = DECL_NAME (thunk);
9714 tree thunks;
9716 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9717 (void *)thunk,
9718 !DECL_THUNK_P (thunk) ? "function"
9719 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9720 name ? IDENTIFIER_POINTER (name) : "<unset>");
9721 if (DECL_THUNK_P (thunk))
9723 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9724 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9726 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9727 if (!virtual_adjust)
9728 /*NOP*/;
9729 else if (DECL_THIS_THUNK_P (thunk))
9730 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9731 tree_to_shwi (virtual_adjust));
9732 else
9733 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9734 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9735 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9736 if (THUNK_ALIAS (thunk))
9737 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9739 fprintf (stream, "\n");
9740 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9741 dump_thunk (stream, indent + 2, thunks);
9744 /* Dump the thunks for FN. */
9746 void
9747 debug_thunks (tree fn)
9749 dump_thunk (stderr, 0, fn);
9752 /* Virtual function table initialization. */
9754 /* Create all the necessary vtables for T and its base classes. */
9756 static void
9757 finish_vtbls (tree t)
9759 tree vbase;
9760 vec<constructor_elt, va_gc> *v = NULL;
9761 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9763 /* We lay out the primary and secondary vtables in one contiguous
9764 vtable. The primary vtable is first, followed by the non-virtual
9765 secondary vtables in inheritance graph order. */
9766 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9767 vtable, t, &v);
9769 /* Then come the virtual bases, also in inheritance graph order. */
9770 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9772 if (!BINFO_VIRTUAL_P (vbase))
9773 continue;
9774 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9777 if (BINFO_VTABLE (TYPE_BINFO (t)))
9778 initialize_vtable (TYPE_BINFO (t), v);
9781 /* Initialize the vtable for BINFO with the INITS. */
9783 static void
9784 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9786 tree decl;
9788 layout_vtable_decl (binfo, vec_safe_length (inits));
9789 decl = get_vtbl_decl_for_binfo (binfo);
9790 initialize_artificial_var (decl, inits);
9791 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9794 /* Build the VTT (virtual table table) for T.
9795 A class requires a VTT if it has virtual bases.
9797 This holds
9798 1 - primary virtual pointer for complete object T
9799 2 - secondary VTTs for each direct non-virtual base of T which requires a
9801 3 - secondary virtual pointers for each direct or indirect base of T which
9802 has virtual bases or is reachable via a virtual path from T.
9803 4 - secondary VTTs for each direct or indirect virtual base of T.
9805 Secondary VTTs look like complete object VTTs without part 4. */
9807 static void
9808 build_vtt (tree t)
9810 tree type;
9811 tree vtt;
9812 tree index;
9813 vec<constructor_elt, va_gc> *inits;
9815 /* Build up the initializers for the VTT. */
9816 inits = NULL;
9817 index = size_zero_node;
9818 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9820 /* If we didn't need a VTT, we're done. */
9821 if (!inits)
9822 return;
9824 /* Figure out the type of the VTT. */
9825 type = build_array_of_n_type (const_ptr_type_node,
9826 inits->length ());
9828 /* Now, build the VTT object itself. */
9829 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9830 initialize_artificial_var (vtt, inits);
9831 /* Add the VTT to the vtables list. */
9832 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9833 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9835 dump_vtt (t, vtt);
9838 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9839 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9840 and CHAIN the vtable pointer for this binfo after construction is
9841 complete. VALUE can also be another BINFO, in which case we recurse. */
9843 static tree
9844 binfo_ctor_vtable (tree binfo)
9846 tree vt;
9848 while (1)
9850 vt = BINFO_VTABLE (binfo);
9851 if (TREE_CODE (vt) == TREE_LIST)
9852 vt = TREE_VALUE (vt);
9853 if (TREE_CODE (vt) == TREE_BINFO)
9854 binfo = vt;
9855 else
9856 break;
9859 return vt;
9862 /* Data for secondary VTT initialization. */
9863 struct secondary_vptr_vtt_init_data
9865 /* Is this the primary VTT? */
9866 bool top_level_p;
9868 /* Current index into the VTT. */
9869 tree index;
9871 /* Vector of initializers built up. */
9872 vec<constructor_elt, va_gc> *inits;
9874 /* The type being constructed by this secondary VTT. */
9875 tree type_being_constructed;
9878 /* Recursively build the VTT-initializer for BINFO (which is in the
9879 hierarchy dominated by T). INITS points to the end of the initializer
9880 list to date. INDEX is the VTT index where the next element will be
9881 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9882 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9883 for virtual bases of T. When it is not so, we build the constructor
9884 vtables for the BINFO-in-T variant. */
9886 static void
9887 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9888 tree *index)
9890 int i;
9891 tree b;
9892 tree init;
9893 secondary_vptr_vtt_init_data data;
9894 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9896 /* We only need VTTs for subobjects with virtual bases. */
9897 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9898 return;
9900 /* We need to use a construction vtable if this is not the primary
9901 VTT. */
9902 if (!top_level_p)
9904 build_ctor_vtbl_group (binfo, t);
9906 /* Record the offset in the VTT where this sub-VTT can be found. */
9907 BINFO_SUBVTT_INDEX (binfo) = *index;
9910 /* Add the address of the primary vtable for the complete object. */
9911 init = binfo_ctor_vtable (binfo);
9912 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9913 if (top_level_p)
9915 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9916 BINFO_VPTR_INDEX (binfo) = *index;
9918 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9920 /* Recursively add the secondary VTTs for non-virtual bases. */
9921 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9922 if (!BINFO_VIRTUAL_P (b))
9923 build_vtt_inits (b, t, inits, index);
9925 /* Add secondary virtual pointers for all subobjects of BINFO with
9926 either virtual bases or reachable along a virtual path, except
9927 subobjects that are non-virtual primary bases. */
9928 data.top_level_p = top_level_p;
9929 data.index = *index;
9930 data.inits = *inits;
9931 data.type_being_constructed = BINFO_TYPE (binfo);
9933 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9935 *index = data.index;
9937 /* data.inits might have grown as we added secondary virtual pointers.
9938 Make sure our caller knows about the new vector. */
9939 *inits = data.inits;
9941 if (top_level_p)
9942 /* Add the secondary VTTs for virtual bases in inheritance graph
9943 order. */
9944 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9946 if (!BINFO_VIRTUAL_P (b))
9947 continue;
9949 build_vtt_inits (b, t, inits, index);
9951 else
9952 /* Remove the ctor vtables we created. */
9953 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9956 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9957 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9959 static tree
9960 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9962 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9964 /* We don't care about bases that don't have vtables. */
9965 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9966 return dfs_skip_bases;
9968 /* We're only interested in proper subobjects of the type being
9969 constructed. */
9970 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9971 return NULL_TREE;
9973 /* We're only interested in bases with virtual bases or reachable
9974 via a virtual path from the type being constructed. */
9975 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9976 || binfo_via_virtual (binfo, data->type_being_constructed)))
9977 return dfs_skip_bases;
9979 /* We're not interested in non-virtual primary bases. */
9980 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9981 return NULL_TREE;
9983 /* Record the index where this secondary vptr can be found. */
9984 if (data->top_level_p)
9986 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9987 BINFO_VPTR_INDEX (binfo) = data->index;
9989 if (BINFO_VIRTUAL_P (binfo))
9991 /* It's a primary virtual base, and this is not a
9992 construction vtable. Find the base this is primary of in
9993 the inheritance graph, and use that base's vtable
9994 now. */
9995 while (BINFO_PRIMARY_P (binfo))
9996 binfo = BINFO_INHERITANCE_CHAIN (binfo);
10000 /* Add the initializer for the secondary vptr itself. */
10001 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
10003 /* Advance the vtt index. */
10004 data->index = size_binop (PLUS_EXPR, data->index,
10005 TYPE_SIZE_UNIT (ptr_type_node));
10007 return NULL_TREE;
10010 /* Called from build_vtt_inits via dfs_walk. After building
10011 constructor vtables and generating the sub-vtt from them, we need
10012 to restore the BINFO_VTABLES that were scribbled on. DATA is the
10013 binfo of the base whose sub vtt was generated. */
10015 static tree
10016 dfs_fixup_binfo_vtbls (tree binfo, void* data)
10018 tree vtable = BINFO_VTABLE (binfo);
10020 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10021 /* If this class has no vtable, none of its bases do. */
10022 return dfs_skip_bases;
10024 if (!vtable)
10025 /* This might be a primary base, so have no vtable in this
10026 hierarchy. */
10027 return NULL_TREE;
10029 /* If we scribbled the construction vtable vptr into BINFO, clear it
10030 out now. */
10031 if (TREE_CODE (vtable) == TREE_LIST
10032 && (TREE_PURPOSE (vtable) == (tree) data))
10033 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
10035 return NULL_TREE;
10038 /* Build the construction vtable group for BINFO which is in the
10039 hierarchy dominated by T. */
10041 static void
10042 build_ctor_vtbl_group (tree binfo, tree t)
10044 tree type;
10045 tree vtbl;
10046 tree id;
10047 tree vbase;
10048 vec<constructor_elt, va_gc> *v;
10050 /* See if we've already created this construction vtable group. */
10051 id = mangle_ctor_vtbl_for_type (t, binfo);
10052 if (get_global_binding (id))
10053 return;
10055 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
10056 /* Build a version of VTBL (with the wrong type) for use in
10057 constructing the addresses of secondary vtables in the
10058 construction vtable group. */
10059 vtbl = build_vtable (t, id, ptr_type_node);
10061 /* Don't export construction vtables from shared libraries. Even on
10062 targets that don't support hidden visibility, this tells
10063 can_refer_decl_in_current_unit_p not to assume that it's safe to
10064 access from a different compilation unit (bz 54314). */
10065 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
10066 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
10068 v = NULL;
10069 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
10070 binfo, vtbl, t, &v);
10072 /* Add the vtables for each of our virtual bases using the vbase in T
10073 binfo. */
10074 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10075 vbase;
10076 vbase = TREE_CHAIN (vbase))
10078 tree b;
10080 if (!BINFO_VIRTUAL_P (vbase))
10081 continue;
10082 b = copied_binfo (vbase, binfo);
10084 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
10087 /* Figure out the type of the construction vtable. */
10088 type = build_array_of_n_type (vtable_entry_type, v->length ());
10089 layout_type (type);
10090 TREE_TYPE (vtbl) = type;
10091 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
10092 layout_decl (vtbl, 0);
10094 /* Initialize the construction vtable. */
10095 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
10096 initialize_artificial_var (vtbl, v);
10097 dump_vtable (t, binfo, vtbl);
10100 /* Add the vtbl initializers for BINFO (and its bases other than
10101 non-virtual primaries) to the list of INITS. BINFO is in the
10102 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
10103 the constructor the vtbl inits should be accumulated for. (If this
10104 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
10105 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
10106 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
10107 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
10108 but are not necessarily the same in terms of layout. */
10110 static void
10111 accumulate_vtbl_inits (tree binfo,
10112 tree orig_binfo,
10113 tree rtti_binfo,
10114 tree vtbl,
10115 tree t,
10116 vec<constructor_elt, va_gc> **inits)
10118 int i;
10119 tree base_binfo;
10120 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10122 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
10124 /* If it doesn't have a vptr, we don't do anything. */
10125 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10126 return;
10128 /* If we're building a construction vtable, we're not interested in
10129 subobjects that don't require construction vtables. */
10130 if (ctor_vtbl_p
10131 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10132 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
10133 return;
10135 /* Build the initializers for the BINFO-in-T vtable. */
10136 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
10138 /* Walk the BINFO and its bases. We walk in preorder so that as we
10139 initialize each vtable we can figure out at what offset the
10140 secondary vtable lies from the primary vtable. We can't use
10141 dfs_walk here because we need to iterate through bases of BINFO
10142 and RTTI_BINFO simultaneously. */
10143 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10145 /* Skip virtual bases. */
10146 if (BINFO_VIRTUAL_P (base_binfo))
10147 continue;
10148 accumulate_vtbl_inits (base_binfo,
10149 BINFO_BASE_BINFO (orig_binfo, i),
10150 rtti_binfo, vtbl, t,
10151 inits);
10155 /* Called from accumulate_vtbl_inits. Adds the initializers for the
10156 BINFO vtable to L. */
10158 static void
10159 dfs_accumulate_vtbl_inits (tree binfo,
10160 tree orig_binfo,
10161 tree rtti_binfo,
10162 tree orig_vtbl,
10163 tree t,
10164 vec<constructor_elt, va_gc> **l)
10166 tree vtbl = NULL_TREE;
10167 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10168 int n_inits;
10170 if (ctor_vtbl_p
10171 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
10173 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
10174 primary virtual base. If it is not the same primary in
10175 the hierarchy of T, we'll need to generate a ctor vtable
10176 for it, to place at its location in T. If it is the same
10177 primary, we still need a VTT entry for the vtable, but it
10178 should point to the ctor vtable for the base it is a
10179 primary for within the sub-hierarchy of RTTI_BINFO.
10181 There are three possible cases:
10183 1) We are in the same place.
10184 2) We are a primary base within a lost primary virtual base of
10185 RTTI_BINFO.
10186 3) We are primary to something not a base of RTTI_BINFO. */
10188 tree b;
10189 tree last = NULL_TREE;
10191 /* First, look through the bases we are primary to for RTTI_BINFO
10192 or a virtual base. */
10193 b = binfo;
10194 while (BINFO_PRIMARY_P (b))
10196 b = BINFO_INHERITANCE_CHAIN (b);
10197 last = b;
10198 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10199 goto found;
10201 /* If we run out of primary links, keep looking down our
10202 inheritance chain; we might be an indirect primary. */
10203 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
10204 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10205 break;
10206 found:
10208 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
10209 base B and it is a base of RTTI_BINFO, this is case 2. In
10210 either case, we share our vtable with LAST, i.e. the
10211 derived-most base within B of which we are a primary. */
10212 if (b == rtti_binfo
10213 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
10214 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
10215 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
10216 binfo_ctor_vtable after everything's been set up. */
10217 vtbl = last;
10219 /* Otherwise, this is case 3 and we get our own. */
10221 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
10222 return;
10224 n_inits = vec_safe_length (*l);
10226 if (!vtbl)
10228 tree index;
10229 int non_fn_entries;
10231 /* Add the initializer for this vtable. */
10232 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
10233 &non_fn_entries, l);
10235 /* Figure out the position to which the VPTR should point. */
10236 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
10237 index = size_binop (MULT_EXPR,
10238 TYPE_SIZE_UNIT (vtable_entry_type),
10239 size_int (non_fn_entries + n_inits));
10240 vtbl = fold_build_pointer_plus (vtbl, index);
10243 if (ctor_vtbl_p)
10244 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
10245 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
10246 straighten this out. */
10247 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
10248 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
10249 /* Throw away any unneeded intializers. */
10250 (*l)->truncate (n_inits);
10251 else
10252 /* For an ordinary vtable, set BINFO_VTABLE. */
10253 BINFO_VTABLE (binfo) = vtbl;
10256 static GTY(()) tree abort_fndecl_addr;
10257 static GTY(()) tree dvirt_fn;
10259 /* Construct the initializer for BINFO's virtual function table. BINFO
10260 is part of the hierarchy dominated by T. If we're building a
10261 construction vtable, the ORIG_BINFO is the binfo we should use to
10262 find the actual function pointers to put in the vtable - but they
10263 can be overridden on the path to most-derived in the graph that
10264 ORIG_BINFO belongs. Otherwise,
10265 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
10266 BINFO that should be indicated by the RTTI information in the
10267 vtable; it will be a base class of T, rather than T itself, if we
10268 are building a construction vtable.
10270 The value returned is a TREE_LIST suitable for wrapping in a
10271 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10272 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10273 number of non-function entries in the vtable.
10275 It might seem that this function should never be called with a
10276 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10277 base is always subsumed by a derived class vtable. However, when
10278 we are building construction vtables, we do build vtables for
10279 primary bases; we need these while the primary base is being
10280 constructed. */
10282 static void
10283 build_vtbl_initializer (tree binfo,
10284 tree orig_binfo,
10285 tree t,
10286 tree rtti_binfo,
10287 int* non_fn_entries_p,
10288 vec<constructor_elt, va_gc> **inits)
10290 tree v;
10291 vtbl_init_data vid;
10292 unsigned ix, jx;
10293 tree vbinfo;
10294 vec<tree, va_gc> *vbases;
10295 constructor_elt *e;
10297 /* Initialize VID. */
10298 memset (&vid, 0, sizeof (vid));
10299 vid.binfo = binfo;
10300 vid.derived = t;
10301 vid.rtti_binfo = rtti_binfo;
10302 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10303 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10304 vid.generate_vcall_entries = true;
10305 /* The first vbase or vcall offset is at index -3 in the vtable. */
10306 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10308 /* Add entries to the vtable for RTTI. */
10309 build_rtti_vtbl_entries (binfo, &vid);
10311 /* Create an array for keeping track of the functions we've
10312 processed. When we see multiple functions with the same
10313 signature, we share the vcall offsets. */
10314 vec_alloc (vid.fns, 32);
10315 /* Add the vcall and vbase offset entries. */
10316 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10318 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10319 build_vbase_offset_vtbl_entries. */
10320 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10321 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10322 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10324 /* If the target requires padding between data entries, add that now. */
10325 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10327 int n_entries = vec_safe_length (vid.inits);
10329 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10330 true);
10332 /* Move data entries into their new positions and add padding
10333 after the new positions. Iterate backwards so we don't
10334 overwrite entries that we would need to process later. */
10335 for (ix = n_entries - 1;
10336 vid.inits->iterate (ix, &e);
10337 ix--)
10339 int j;
10340 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10341 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10343 (*vid.inits)[new_position] = *e;
10345 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10347 constructor_elt *f = &(*vid.inits)[new_position - j];
10348 f->index = NULL_TREE;
10349 f->value = build1 (NOP_EXPR, vtable_entry_type,
10350 null_pointer_node);
10355 if (non_fn_entries_p)
10356 *non_fn_entries_p = vec_safe_length (vid.inits);
10358 /* The initializers for virtual functions were built up in reverse
10359 order. Straighten them out and add them to the running list in one
10360 step. */
10361 jx = vec_safe_length (*inits);
10362 vec_safe_grow (*inits, jx + vid.inits->length (), true);
10364 for (ix = vid.inits->length () - 1;
10365 vid.inits->iterate (ix, &e);
10366 ix--, jx++)
10367 (**inits)[jx] = *e;
10369 /* Go through all the ordinary virtual functions, building up
10370 initializers. */
10371 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10373 tree delta;
10374 tree vcall_index;
10375 tree fn, fn_original;
10376 tree init = NULL_TREE;
10378 fn = BV_FN (v);
10379 fn_original = fn;
10380 if (DECL_THUNK_P (fn))
10382 if (!DECL_NAME (fn))
10383 finish_thunk (fn);
10384 if (THUNK_ALIAS (fn))
10386 fn = THUNK_ALIAS (fn);
10387 BV_FN (v) = fn;
10389 fn_original = THUNK_TARGET (fn);
10392 /* If the only definition of this function signature along our
10393 primary base chain is from a lost primary, this vtable slot will
10394 never be used, so just zero it out. This is important to avoid
10395 requiring extra thunks which cannot be generated with the function.
10397 We first check this in update_vtable_entry_for_fn, so we handle
10398 restored primary bases properly; we also need to do it here so we
10399 zero out unused slots in ctor vtables, rather than filling them
10400 with erroneous values (though harmless, apart from relocation
10401 costs). */
10402 if (BV_LOST_PRIMARY (v))
10403 init = size_zero_node;
10405 if (! init)
10407 /* Pull the offset for `this', and the function to call, out of
10408 the list. */
10409 delta = BV_DELTA (v);
10410 vcall_index = BV_VCALL_INDEX (v);
10412 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10413 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10415 /* You can't call an abstract virtual function; it's abstract.
10416 So, we replace these functions with __pure_virtual. */
10417 if (DECL_PURE_VIRTUAL_P (fn_original))
10419 fn = abort_fndecl;
10420 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10422 if (abort_fndecl_addr == NULL)
10423 abort_fndecl_addr
10424 = fold_convert (vfunc_ptr_type_node,
10425 build_fold_addr_expr (fn));
10426 init = abort_fndecl_addr;
10429 /* Likewise for deleted virtuals. */
10430 else if (DECL_DELETED_FN (fn_original))
10432 if (!dvirt_fn)
10434 tree name = get_identifier ("__cxa_deleted_virtual");
10435 dvirt_fn = get_global_binding (name);
10436 if (!dvirt_fn)
10437 dvirt_fn = push_library_fn
10438 (name,
10439 build_function_type_list (void_type_node, NULL_TREE),
10440 NULL_TREE, ECF_NORETURN | ECF_COLD);
10442 fn = dvirt_fn;
10443 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10444 init = fold_convert (vfunc_ptr_type_node,
10445 build_fold_addr_expr (fn));
10447 else
10449 if (!integer_zerop (delta) || vcall_index)
10451 fn = make_thunk (fn, /*this_adjusting=*/1,
10452 delta, vcall_index);
10453 if (!DECL_NAME (fn))
10454 finish_thunk (fn);
10456 /* Take the address of the function, considering it to be of an
10457 appropriate generic type. */
10458 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10459 init = fold_convert (vfunc_ptr_type_node,
10460 build_fold_addr_expr (fn));
10461 /* Don't refer to a virtual destructor from a constructor
10462 vtable or a vtable for an abstract class, since destroying
10463 an object under construction is undefined behavior and we
10464 don't want it to be considered a candidate for speculative
10465 devirtualization. But do create the thunk for ABI
10466 compliance. */
10467 if (DECL_DESTRUCTOR_P (fn_original)
10468 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10469 || orig_binfo != binfo))
10470 init = size_zero_node;
10474 /* And add it to the chain of initializers. */
10475 if (TARGET_VTABLE_USES_DESCRIPTORS)
10477 int i;
10478 if (init == size_zero_node)
10479 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10480 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10481 else
10482 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10484 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10485 fn, build_int_cst (NULL_TREE, i));
10486 TREE_CONSTANT (fdesc) = 1;
10488 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
10491 else
10492 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10496 /* Adds to vid->inits the initializers for the vbase and vcall
10497 offsets in BINFO, which is in the hierarchy dominated by T. */
10499 static void
10500 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10502 tree b;
10504 /* If this is a derived class, we must first create entries
10505 corresponding to the primary base class. */
10506 b = get_primary_binfo (binfo);
10507 if (b)
10508 build_vcall_and_vbase_vtbl_entries (b, vid);
10510 /* Add the vbase entries for this base. */
10511 build_vbase_offset_vtbl_entries (binfo, vid);
10512 /* Add the vcall entries for this base. */
10513 build_vcall_offset_vtbl_entries (binfo, vid);
10516 /* Returns the initializers for the vbase offset entries in the vtable
10517 for BINFO (which is part of the class hierarchy dominated by T), in
10518 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10519 where the next vbase offset will go. */
10521 static void
10522 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10524 tree vbase;
10525 tree t;
10526 tree non_primary_binfo;
10528 /* If there are no virtual baseclasses, then there is nothing to
10529 do. */
10530 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10531 return;
10533 t = vid->derived;
10535 /* We might be a primary base class. Go up the inheritance hierarchy
10536 until we find the most derived class of which we are a primary base:
10537 it is the offset of that which we need to use. */
10538 non_primary_binfo = binfo;
10539 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10541 tree b;
10543 /* If we have reached a virtual base, then it must be a primary
10544 base (possibly multi-level) of vid->binfo, or we wouldn't
10545 have called build_vcall_and_vbase_vtbl_entries for it. But it
10546 might be a lost primary, so just skip down to vid->binfo. */
10547 if (BINFO_VIRTUAL_P (non_primary_binfo))
10549 non_primary_binfo = vid->binfo;
10550 break;
10553 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10554 if (get_primary_binfo (b) != non_primary_binfo)
10555 break;
10556 non_primary_binfo = b;
10559 /* Go through the virtual bases, adding the offsets. */
10560 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10561 vbase;
10562 vbase = TREE_CHAIN (vbase))
10564 tree b;
10565 tree delta;
10567 if (!BINFO_VIRTUAL_P (vbase))
10568 continue;
10570 /* Find the instance of this virtual base in the complete
10571 object. */
10572 b = copied_binfo (vbase, binfo);
10574 /* If we've already got an offset for this virtual base, we
10575 don't need another one. */
10576 if (BINFO_VTABLE_PATH_MARKED (b))
10577 continue;
10578 BINFO_VTABLE_PATH_MARKED (b) = 1;
10580 /* Figure out where we can find this vbase offset. */
10581 delta = size_binop (MULT_EXPR,
10582 vid->index,
10583 fold_convert (ssizetype,
10584 TYPE_SIZE_UNIT (vtable_entry_type)));
10585 if (vid->primary_vtbl_p)
10586 BINFO_VPTR_FIELD (b) = delta;
10588 if (binfo != TYPE_BINFO (t))
10589 /* The vbase offset had better be the same. */
10590 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10592 /* The next vbase will come at a more negative offset. */
10593 vid->index = size_binop (MINUS_EXPR, vid->index,
10594 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10596 /* The initializer is the delta from BINFO to this virtual base.
10597 The vbase offsets go in reverse inheritance-graph order, and
10598 we are walking in inheritance graph order so these end up in
10599 the right order. */
10600 delta = size_diffop_loc (input_location,
10601 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10603 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10604 fold_build1_loc (input_location, NOP_EXPR,
10605 vtable_entry_type, delta));
10609 /* Adds the initializers for the vcall offset entries in the vtable
10610 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10611 to VID->INITS. */
10613 static void
10614 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10616 /* We only need these entries if this base is a virtual base. We
10617 compute the indices -- but do not add to the vtable -- when
10618 building the main vtable for a class. */
10619 if (binfo == TYPE_BINFO (vid->derived)
10620 || (BINFO_VIRTUAL_P (binfo)
10621 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10622 correspond to VID->DERIVED), we are building a primary
10623 construction virtual table. Since this is a primary
10624 virtual table, we do not need the vcall offsets for
10625 BINFO. */
10626 && binfo != vid->rtti_binfo))
10628 /* We need a vcall offset for each of the virtual functions in this
10629 vtable. For example:
10631 class A { virtual void f (); };
10632 class B1 : virtual public A { virtual void f (); };
10633 class B2 : virtual public A { virtual void f (); };
10634 class C: public B1, public B2 { virtual void f (); };
10636 A C object has a primary base of B1, which has a primary base of A. A
10637 C also has a secondary base of B2, which no longer has a primary base
10638 of A. So the B2-in-C construction vtable needs a secondary vtable for
10639 A, which will adjust the A* to a B2* to call f. We have no way of
10640 knowing what (or even whether) this offset will be when we define B2,
10641 so we store this "vcall offset" in the A sub-vtable and look it up in
10642 a "virtual thunk" for B2::f.
10644 We need entries for all the functions in our primary vtable and
10645 in our non-virtual bases' secondary vtables. */
10646 vid->vbase = binfo;
10647 /* If we are just computing the vcall indices -- but do not need
10648 the actual entries -- not that. */
10649 if (!BINFO_VIRTUAL_P (binfo))
10650 vid->generate_vcall_entries = false;
10651 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10652 add_vcall_offset_vtbl_entries_r (binfo, vid);
10656 /* Build vcall offsets, starting with those for BINFO. */
10658 static void
10659 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10661 int i;
10662 tree primary_binfo;
10663 tree base_binfo;
10665 /* Don't walk into virtual bases -- except, of course, for the
10666 virtual base for which we are building vcall offsets. Any
10667 primary virtual base will have already had its offsets generated
10668 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10669 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10670 return;
10672 /* If BINFO has a primary base, process it first. */
10673 primary_binfo = get_primary_binfo (binfo);
10674 if (primary_binfo)
10675 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10677 /* Add BINFO itself to the list. */
10678 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10680 /* Scan the non-primary bases of BINFO. */
10681 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10682 if (base_binfo != primary_binfo)
10683 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10686 /* Called from build_vcall_offset_vtbl_entries_r. */
10688 static void
10689 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10691 /* Make entries for the rest of the virtuals. */
10692 tree orig_fn;
10694 /* The ABI requires that the methods be processed in declaration
10695 order. */
10696 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10697 orig_fn;
10698 orig_fn = DECL_CHAIN (orig_fn))
10699 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10700 add_vcall_offset (orig_fn, binfo, vid);
10703 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10705 static void
10706 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10708 size_t i;
10709 tree vcall_offset;
10710 tree derived_entry;
10712 /* If there is already an entry for a function with the same
10713 signature as FN, then we do not need a second vcall offset.
10714 Check the list of functions already present in the derived
10715 class vtable. */
10716 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10718 if (same_signature_p (derived_entry, orig_fn)
10719 /* We only use one vcall offset for virtual destructors,
10720 even though there are two virtual table entries. */
10721 || (DECL_DESTRUCTOR_P (derived_entry)
10722 && DECL_DESTRUCTOR_P (orig_fn)))
10723 return;
10726 /* If we are building these vcall offsets as part of building
10727 the vtable for the most derived class, remember the vcall
10728 offset. */
10729 if (vid->binfo == TYPE_BINFO (vid->derived))
10731 tree_pair_s elt = {orig_fn, vid->index};
10732 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10735 /* The next vcall offset will be found at a more negative
10736 offset. */
10737 vid->index = size_binop (MINUS_EXPR, vid->index,
10738 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10740 /* Keep track of this function. */
10741 vec_safe_push (vid->fns, orig_fn);
10743 if (vid->generate_vcall_entries)
10745 tree base;
10746 tree fn;
10748 /* Find the overriding function. */
10749 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10750 if (fn == error_mark_node)
10751 vcall_offset = build_zero_cst (vtable_entry_type);
10752 else
10754 base = TREE_VALUE (fn);
10756 /* The vbase we're working on is a primary base of
10757 vid->binfo. But it might be a lost primary, so its
10758 BINFO_OFFSET might be wrong, so we just use the
10759 BINFO_OFFSET from vid->binfo. */
10760 vcall_offset = size_diffop_loc (input_location,
10761 BINFO_OFFSET (base),
10762 BINFO_OFFSET (vid->binfo));
10763 vcall_offset = fold_build1_loc (input_location,
10764 NOP_EXPR, vtable_entry_type,
10765 vcall_offset);
10767 /* Add the initializer to the vtable. */
10768 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10772 /* Return vtbl initializers for the RTTI entries corresponding to the
10773 BINFO's vtable. The RTTI entries should indicate the object given
10774 by VID->rtti_binfo. */
10776 static void
10777 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10779 tree b;
10780 tree t;
10781 tree offset;
10782 tree decl;
10783 tree init;
10785 t = BINFO_TYPE (vid->rtti_binfo);
10787 /* To find the complete object, we will first convert to our most
10788 primary base, and then add the offset in the vtbl to that value. */
10789 b = most_primary_binfo (binfo);
10790 offset = size_diffop_loc (input_location,
10791 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10793 /* The second entry is the address of the typeinfo object. */
10794 if (flag_rtti)
10795 decl = build_address (get_tinfo_decl (t));
10796 else
10797 decl = integer_zero_node;
10799 /* Convert the declaration to a type that can be stored in the
10800 vtable. */
10801 init = build_nop (vfunc_ptr_type_node, decl);
10802 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10804 /* Add the offset-to-top entry. It comes earlier in the vtable than
10805 the typeinfo entry. Convert the offset to look like a
10806 function pointer, so that we can put it in the vtable. */
10807 init = build_nop (vfunc_ptr_type_node, offset);
10808 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10811 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10812 accessibility. */
10814 bool
10815 uniquely_derived_from_p (tree parent, tree type)
10817 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10818 return base && base != error_mark_node;
10821 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10823 bool
10824 publicly_uniquely_derived_p (tree parent, tree type)
10826 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10827 NULL, tf_none);
10828 return base && base != error_mark_node;
10831 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10832 class between them, if any. */
10834 tree
10835 common_enclosing_class (tree ctx1, tree ctx2)
10837 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10838 return NULL_TREE;
10839 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10840 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10841 if (ctx1 == ctx2)
10842 return ctx1;
10843 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10844 TYPE_MARKED_P (t) = true;
10845 tree found = NULL_TREE;
10846 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10847 if (TYPE_MARKED_P (t))
10849 found = t;
10850 break;
10852 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10853 TYPE_MARKED_P (t) = false;
10854 return found;
10857 #include "gt-cp-class.h"