libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / cp / class.cc
blob8c39bb4a76b1a3edfc01963a89a6e2b8d0e73c8e
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 parameter of an iobj member function corresponds to
1023 another parameter type. CONTEXT is the class that the implicit object
1024 parameter is considered to refer to. */
1026 bool
1027 iobj_parm_corresponds_to (tree iobj_fn, tree xobj_param, tree context)
1029 tree iobj_fn_type = TREE_TYPE (iobj_fn);
1031 /* If the iobj member function was introduced with a using declaration, the
1032 type of its object parameter is considered to be that of the class it was
1033 introduced into.
1035 [over.match.funcs.general.4]
1036 For non-conversion functions that are implicit object member
1037 functions nominated by a using-declaration in a derived class, the
1038 function is considered to be a member of the derived class for the purpose
1039 of defining the type of the implicit object parameter.
1041 Unfortunately, because of this rule, we can't just compare the xobj member
1042 function's DECL_CONTEXT to its object parameter.
1044 struct S;
1046 struct B {
1047 int f(this S&) { return 5; }
1050 struct S : B {
1051 using B::f;
1052 int f() { return 10; }
1055 The using declaration does not change the object parameter of B::f as it
1056 is an xobj member function. However, its object parameter still
1057 corresponds to S::f as it was declared with an object parameter of type
1058 S const&. The DECL_CONTEXT of B::f is B, so if we compare the type of the
1059 object parameter to that, it will not match. If we naively assume a
1060 different type from the DECL_CONTEXT for an xobj parameter means that the
1061 object parameters do not correspond, then the object parameters in the
1062 above example will be considered non-corresponding.
1064 As a result of this, B::f would incorrectly not be discarded, causing an
1065 ambiguity when f is called on an object of type S.
1067 This also impacts member functions with constraints as in the following
1068 example.
1070 template<typename = void>
1071 struct S;
1073 template<typename = void>
1074 struct B {
1075 int f(this S<>&) requires true { return 5; }
1078 template<typename>
1079 struct S : B<> {
1080 using B<>::f;
1081 int f() { return 10; }
1084 Once again, if we compare the DECL_CONTEXT of B<>::f to it's xobj
1085 parameter, it would not match. If the object parameters do not
1086 correspond, constraints are not taken into account, so in this example we
1087 would (probably) get an ambiguous lookup instead of correctly picking
1088 B<>::f.
1090 Because of this caveat, we must actually compare the type of the iobj
1091 parameter to the type of the xobj parameter, shortcuts will have these
1092 edge cases.
1094 Aside from the more complex reasons above, this logic also implicitly
1095 handles xobj parameters of pointer type, we don't have to explicitly
1096 check for that case. */
1098 if (!same_type_ignoring_top_level_qualifiers_p
1099 (context, non_reference (xobj_param)))
1100 return false;
1102 /* We don't get to bail yet even if we have a by-value xobj parameter,
1103 a by-value xobj parameter can correspond to an iobj parameter provided the
1104 iobj member function is not declared with a reference qualifier.
1106 From this point on, we know we are dealing with an xobj parameter that has
1107 an object parameter of the same type as the class it was declared in.
1108 We still don't know if we have a reference or by-value parameter yet
1109 though. */
1111 cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type);
1112 /* We only care about cv qualifiers when determining correspondence. */
1113 static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE
1114 | TYPE_QUAL_CONST;
1115 cp_cv_quals const iobj_cv_quals = type_memfn_quals (iobj_fn_type) & cv_bits;
1116 /* We need to ignore the ref qualifier of the xobj parameter if the iobj
1117 member function lacks a ref qualifier.
1119 [basic.scope.scope.3]
1120 Two non-static member functions have corresponding object parameters if:
1121 -- exactly one is an implicit object member function with no ref-qualifier
1122 and the types of their object parameters ([dcl.fct]), after removing
1123 top-level references, are the same, or
1124 -- their object parameters have the same type.
1126 The cv qualifiers of a by-value parameter are supposed to be discarded, so
1127 we ignore them.
1129 [dcl.fct.5]
1130 After producing the list of parameter types, any top-level cv-qualifiers
1131 modifying a parameter type are deleted when forming the function type.
1133 However, they still need to be taken into account when our xobj parameter
1134 is a reference that is being ignored (according to [basic.scope.scope.3]
1135 quoted above), but when we are actually dealing with a by-value xobj
1136 parameter we can proceed following this table.
1137 | iobj | xobj | equal |
1138 | none | none | X |
1139 | none | c | X |
1140 | none | v | X |
1141 | none | cv | X |
1142 | c | none | O |
1143 | c | c | O |
1144 | c | v | O |
1145 | c | cv | O |
1146 | v | none | O |
1147 | v | c | O |
1148 | v | v | O |
1149 | v | cv | O |
1150 | cv | none | O |
1151 | cv | c | O |
1152 | cv | v | O |
1153 | cv | cv | O |
1155 Additionally, if the iobj member function is ref qualified, we aren't
1156 ignoring the ref qualifier of the iobj parameter, so we can't be dealing
1157 with correspondence in that case either.
1159 So to recap, if we have a by-value xobj parameter, we know for sure that
1160 we aren't dealing with corresponding object parameters if the iobj member
1161 function has any cv-ref qualifiers. The only case where we might still be
1162 dealing with corresponding object parameters is when the iobj member
1163 function lacks any cv-ref qualification. */
1164 if (!TYPE_REF_P (xobj_param))
1166 if (iobj_ref_qual || iobj_cv_quals)
1167 return false;
1169 else
1171 /* We are dealing with an xobj parameter that is a reference now, but due
1172 to [basic.scope.scope.3] we need to ignore its ref qual. */
1173 cp_ref_qualifier const xobj_ref_qual = [&](){
1174 if (!TYPE_REF_P (xobj_param) || !iobj_ref_qual)
1175 return REF_QUAL_NONE;
1176 return TYPE_REF_IS_RVALUE (xobj_param) ? REF_QUAL_RVALUE
1177 : REF_QUAL_LVALUE;
1178 }(); /* IILE. */
1180 /* Even if we are ignoring the reference qualifier, the xobj parameter
1181 was still a reference so we still take the cv qualifiers into
1182 account. */
1183 cp_cv_quals const xobj_cv_quals
1184 = cp_type_quals (TREE_TYPE (xobj_param)) & cv_bits;
1186 /* Finally, if the qualifications don't match exactly, the object
1187 parameters don't correspond. */
1188 if (iobj_ref_qual != xobj_ref_qual
1189 || iobj_cv_quals != xobj_cv_quals)
1190 return false;
1192 /* If we got past everything else, the object parameters of fn1 and fn2
1193 definitely correspond. */
1194 return true;
1197 /* True if FN and METHOD have corresponding object parms per
1198 [basic.scope.scope], or if one of them is a static member function (which
1199 are considered to have an object parm that corresponds to any other).
1200 CONTEXT is the class that an implicit object member function is considered
1201 to be a member of for the purpose of this comparison, per
1202 [over.match.funcs]. */
1204 bool
1205 object_parms_correspond (tree fn, tree method, tree context)
1207 tree fn_type = TREE_TYPE (fn);
1208 tree method_type = TREE_TYPE (method);
1210 /* Compare the quals on the 'this' parm. Don't compare
1211 the whole types, as used functions are treated as
1212 coming from the using class in overload resolution. */
1213 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
1214 && DECL_IOBJ_MEMBER_FUNCTION_P (method))
1216 /* Either both or neither need to be ref-qualified for
1217 differing quals to allow overloading. */
1218 if ((FUNCTION_REF_QUALIFIED (fn_type)
1219 == FUNCTION_REF_QUALIFIED (method_type))
1220 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1221 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1222 return false;
1223 return true;
1225 /* Treat a static member function as corresponding to any object parm. */
1226 else if (DECL_STATIC_FUNCTION_P (fn) || DECL_STATIC_FUNCTION_P (method))
1227 return true;
1228 /* Handle special correspondence rules for xobj vs xobj and xobj vs iobj
1229 member function declarations.
1230 We don't worry about static member functions here. */
1231 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1232 && DECL_XOBJ_MEMBER_FUNCTION_P (method))
1234 auto get_object_param = [] (tree fn)
1236 return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1238 /* We skip the object parameter below, check it here instead of
1239 making changes to that code. */
1240 tree fn_param = get_object_param (fn);
1241 tree method_param = get_object_param (method);
1242 if (!same_type_p (fn_param, method_param))
1243 return false;
1245 else
1247 tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? fn : method;
1248 tree iobj_fn = xobj_fn != fn ? fn : method;
1249 tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn)));
1251 return iobj_parm_corresponds_to (iobj_fn, xobj_param, context);
1254 return true;
1257 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1258 METHOD is being injected via a using_decl. Returns true if the
1259 method could be added to the method vec. */
1261 bool
1262 add_method (tree type, tree method, bool via_using)
1264 if (method == error_mark_node)
1265 return false;
1267 gcc_assert (!DECL_EXTERN_C_P (method));
1269 tree *slot = find_member_slot (type, DECL_NAME (method));
1270 tree current_fns = slot ? *slot : NULL_TREE;
1272 /* See below. */
1273 int losem = -1;
1275 /* Check to see if we've already got this method. */
1276 for (ovl_iterator iter (current_fns); iter; ++iter)
1278 tree fn = *iter;
1280 if (TREE_CODE (fn) != TREE_CODE (method))
1281 continue;
1283 /* Two using-declarations can coexist, we'll complain about ambiguity in
1284 overload resolution. */
1285 if (via_using && iter.using_p ()
1286 /* Except handle inherited constructors specially. */
1287 && ! DECL_CONSTRUCTOR_P (fn))
1289 if (fn == method)
1290 /* Don't add the same one twice. */
1291 return false;
1292 continue;
1295 /* [over.load] Member function declarations with the
1296 same name and the same parameter types cannot be
1297 overloaded if any of them is a static member
1298 function declaration.
1300 [over.load] Member function declarations with the same name and
1301 the same parameter-type-list as well as member function template
1302 declarations with the same name, the same parameter-type-list, and
1303 the same template parameter lists cannot be overloaded if any of
1304 them, but not all, have a ref-qualifier.
1306 [namespace.udecl] When a using-declaration brings names
1307 from a base class into a derived class scope, member
1308 functions in the derived class override and/or hide member
1309 functions with the same name and parameter types in a base
1310 class (rather than conflicting). */
1311 if (!object_parms_correspond (fn, method, type))
1312 continue;
1314 tree fn_type = TREE_TYPE (fn);
1315 tree method_type = TREE_TYPE (method);
1317 tree real_fn = fn;
1318 tree real_method = method;
1320 /* Templates and conversion ops must match return types. */
1321 if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1322 && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1323 continue;
1325 /* For templates, the template parameters must be identical. */
1326 if (TREE_CODE (fn) == TEMPLATE_DECL)
1328 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1329 DECL_TEMPLATE_PARMS (method)))
1330 continue;
1332 real_fn = DECL_TEMPLATE_RESULT (fn);
1333 real_method = DECL_TEMPLATE_RESULT (method);
1336 tree parms1 = TYPE_ARG_TYPES (fn_type);
1337 tree parms2 = TYPE_ARG_TYPES (method_type);
1338 if (! DECL_STATIC_FUNCTION_P (real_fn))
1339 parms1 = TREE_CHAIN (parms1);
1340 if (! DECL_STATIC_FUNCTION_P (real_method))
1341 parms2 = TREE_CHAIN (parms2);
1343 /* Bring back parameters omitted from an inherited ctor. The
1344 method and the function can have different omittedness. */
1345 if (ctor_omit_inherited_parms (real_fn))
1346 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1347 if (ctor_omit_inherited_parms (real_method))
1348 parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1349 (DECL_CLONED_FUNCTION (real_method)));
1351 if (!compparms (parms1, parms2))
1352 continue;
1354 if (!equivalently_constrained (fn, method))
1356 if (processing_template_decl)
1357 /* We can't check satisfaction in dependent context, wait until
1358 the class is instantiated. */
1359 continue;
1361 special_function_kind sfk = special_memfn_p (method);
1363 if (sfk == sfk_none
1364 || DECL_INHERITED_CTOR (fn)
1365 || TREE_CODE (fn) == TEMPLATE_DECL)
1366 /* Member function templates and non-special member functions
1367 coexist if they are not equivalently constrained. A member
1368 function is not hidden by an inherited constructor. */
1369 continue;
1371 /* P0848: For special member functions, deleted, unsatisfied, or
1372 less constrained overloads are ineligible. We implement this
1373 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1374 use the notion of eligibility, and the selected destructor can
1375 be deleted, but removing unsatisfied or less constrained
1376 overloads has the same effect as overload resolution. */
1377 bool dtor = (sfk == sfk_destructor);
1378 if (losem == -1)
1379 losem = ((!dtor && DECL_DELETED_FN (method))
1380 || !constraints_satisfied_p (method));
1381 bool losef = ((!dtor && DECL_DELETED_FN (fn))
1382 || !constraints_satisfied_p (fn));
1383 int win;
1384 if (losem || losef)
1385 win = losem - losef;
1386 else
1387 win = more_constrained (fn, method);
1388 if (win > 0)
1389 /* Leave FN in the method vec, discard METHOD. */
1390 return false;
1391 else if (win < 0)
1393 /* Remove FN, add METHOD. */
1394 current_fns = iter.remove_node (current_fns);
1395 continue;
1397 else
1398 /* Let them coexist for now. */
1399 continue;
1402 /* If these are versions of the same function, process and
1403 move on. */
1404 if (TREE_CODE (fn) == FUNCTION_DECL
1405 && maybe_version_functions (method, fn, true))
1406 continue;
1408 if (DECL_INHERITED_CTOR (method))
1410 if (!DECL_INHERITED_CTOR (fn))
1411 /* Defer to the other function. */
1412 return false;
1414 tree basem = DECL_INHERITED_CTOR_BASE (method);
1415 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1416 if (flag_new_inheriting_ctors)
1418 if (basem == basef)
1420 /* Inheriting the same constructor along different
1421 paths, combine them. */
1422 SET_DECL_INHERITED_CTOR
1423 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1424 DECL_INHERITED_CTOR (fn)));
1425 /* And discard the new one. */
1426 return false;
1428 else
1429 /* Inherited ctors can coexist until overload
1430 resolution. */
1431 continue;
1434 auto_diagnostic_group d;
1435 error_at (DECL_SOURCE_LOCATION (method),
1436 "%q#D conflicts with version inherited from %qT",
1437 method, basef);
1438 inform (DECL_SOURCE_LOCATION (fn),
1439 "version inherited from %qT declared here",
1440 basef);
1441 return false;
1444 if (via_using)
1445 /* Defer to the local function. */
1446 return false;
1447 else if (iter.using_p ()
1448 || (flag_new_inheriting_ctors
1449 && DECL_INHERITED_CTOR (fn)))
1451 /* Remove the inherited function. */
1452 current_fns = iter.remove_node (current_fns);
1453 continue;
1455 else
1457 auto_diagnostic_group d;
1458 error_at (DECL_SOURCE_LOCATION (method),
1459 "%q#D cannot be overloaded with %q#D", method, fn);
1460 inform (DECL_SOURCE_LOCATION (fn),
1461 "previous declaration %q#D", fn);
1462 return false;
1466 current_fns = ovl_insert (method, current_fns, via_using);
1468 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1469 && !push_class_level_binding (DECL_NAME (method), current_fns))
1470 return false;
1472 if (!slot)
1473 slot = add_member_slot (type, DECL_NAME (method));
1475 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1476 grok_special_member_properties (method);
1478 *slot = current_fns;
1480 return true;
1483 /* Subroutines of finish_struct. */
1485 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1486 legit, otherwise return 0. */
1488 static int
1489 alter_access (tree t, tree fdecl, tree access)
1491 tree elem;
1493 retrofit_lang_decl (fdecl);
1495 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1497 elem = purpose_member (t, DECL_ACCESS (fdecl));
1498 if (elem)
1500 if (TREE_VALUE (elem) != access)
1502 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1503 error ("conflicting access specifications for method"
1504 " %q+D, ignored", TREE_TYPE (fdecl));
1505 else
1506 error ("conflicting access specifications for field %qE, ignored",
1507 DECL_NAME (fdecl));
1509 else
1511 /* They're changing the access to the same thing they changed
1512 it to before. That's OK. */
1516 else
1518 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1519 tf_warning_or_error);
1520 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1521 return 1;
1523 return 0;
1526 /* Return the access node for DECL's access in its enclosing class. */
1528 tree
1529 declared_access (tree decl)
1531 return (TREE_PRIVATE (decl) ? access_private_node
1532 : TREE_PROTECTED (decl) ? access_protected_node
1533 : access_public_node);
1536 /* If DECL is a non-dependent using of non-ctor function members, push them
1537 and return true, otherwise return false. Called from
1538 finish_member_declaration. */
1540 bool
1541 maybe_push_used_methods (tree decl)
1543 if (TREE_CODE (decl) != USING_DECL)
1544 return false;
1545 tree used = strip_using_decl (decl);
1546 if (!used || !is_overloaded_fn (used))
1547 return false;
1549 /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1550 works within the class body. */
1551 for (tree f : ovl_range (used))
1553 if (DECL_CONSTRUCTOR_P (f))
1554 /* Inheriting constructors are handled separately. */
1555 return false;
1557 bool added = add_method (current_class_type, f, true);
1559 if (added)
1560 alter_access (current_class_type, f, current_access_specifier);
1562 /* If add_method returns false because f was already declared, look
1563 for a duplicate using-declaration. */
1564 else
1565 for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d))
1566 if (TREE_CODE (d) == USING_DECL
1567 && DECL_NAME (d) == DECL_NAME (decl)
1568 && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl)))
1570 diagnose_name_conflict (decl, d);
1571 break;
1574 return true;
1577 /* Process the USING_DECL, which is a member of T. */
1579 static void
1580 handle_using_decl (tree using_decl, tree t)
1582 tree decl = USING_DECL_DECLS (using_decl);
1584 gcc_assert (!processing_template_decl && decl);
1586 cp_emit_debug_info_for_using (decl, t);
1588 if (is_overloaded_fn (decl))
1589 /* Handled in maybe_push_used_methods. */
1590 return;
1592 tree name = DECL_NAME (using_decl);
1593 tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1594 tf_warning_or_error);
1595 if (old_value)
1597 old_value = OVL_FIRST (old_value);
1599 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1600 /* OK */;
1601 else
1602 old_value = NULL_TREE;
1605 if (! old_value)
1607 else if (is_overloaded_fn (old_value))
1609 auto_diagnostic_group d;
1610 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1611 "because of local method %q#D with same name",
1612 using_decl, t, old_value);
1613 inform (DECL_SOURCE_LOCATION (old_value),
1614 "local method %q#D declared here", old_value);
1615 return;
1617 else if (!DECL_ARTIFICIAL (old_value))
1619 auto_diagnostic_group d;
1620 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1621 "because of local member %q#D with same name",
1622 using_decl, t, old_value);
1623 inform (DECL_SOURCE_LOCATION (old_value),
1624 "local member %q#D declared here", old_value);
1625 return;
1628 iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1629 tree access = declared_access (using_decl);
1631 /* Make type T see field decl FDECL with access ACCESS. */
1632 if (USING_DECL_UNRELATED_P (using_decl))
1634 /* C++20 using enum can import non-inherited enumerators into class
1635 scope. We implement that by making a copy of the CONST_DECL for which
1636 CONST_DECL_USING_P is true. */
1637 gcc_assert (TREE_CODE (decl) == CONST_DECL);
1639 auto cas = make_temp_override (current_access_specifier, access);
1640 tree copy = copy_decl (decl);
1641 DECL_CONTEXT (copy) = t;
1642 DECL_ARTIFICIAL (copy) = true;
1643 /* We emitted debug info for the USING_DECL above; make sure we don't
1644 also emit anything for this clone. */
1645 DECL_IGNORED_P (copy) = true;
1646 DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1647 finish_member_declaration (copy);
1648 DECL_ABSTRACT_ORIGIN (copy) = decl;
1650 else
1651 alter_access (t, decl, access);
1654 /* Data structure for find_abi_tags_r, below. */
1656 struct abi_tag_data
1658 tree t; // The type that we're checking for missing tags.
1659 tree subob; // The subobject of T that we're getting tags from.
1660 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1663 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1664 in the context of P. TAG can be either an identifier (the DECL_NAME of
1665 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1667 static void
1668 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1670 if (!IDENTIFIER_MARKED (id))
1672 if (p->tags != error_mark_node)
1674 /* We're collecting tags from template arguments or from
1675 the type of a variable or function return type. */
1676 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1678 /* Don't inherit this tag multiple times. */
1679 IDENTIFIER_MARKED (id) = true;
1681 if (TYPE_P (p->t))
1683 /* Tags inherited from type template arguments are only used
1684 to avoid warnings. */
1685 ABI_TAG_IMPLICIT (p->tags) = true;
1686 return;
1688 /* For functions and variables we want to warn, too. */
1691 /* Otherwise we're diagnosing missing tags. */
1692 if (TREE_CODE (p->t) == FUNCTION_DECL)
1694 auto_diagnostic_group d;
1695 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1696 "that %qT (used in its return type) has",
1697 p->t, tag, *tp))
1698 inform (location_of (*tp), "%qT declared here", *tp);
1700 else if (VAR_P (p->t))
1702 auto_diagnostic_group d;
1703 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1704 "that %qT (used in its type) has", p->t, tag, *tp))
1705 inform (location_of (*tp), "%qT declared here", *tp);
1707 else if (TYPE_P (p->subob))
1709 auto_diagnostic_group d;
1710 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1711 "that base %qT has", p->t, tag, p->subob))
1712 inform (location_of (p->subob), "%qT declared here",
1713 p->subob);
1715 else
1717 auto_diagnostic_group d;
1718 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1719 "that %qT (used in the type of %qD) has",
1720 p->t, tag, *tp, p->subob))
1722 inform (location_of (p->subob), "%qD declared here",
1723 p->subob);
1724 inform (location_of (*tp), "%qT declared here", *tp);
1730 /* Find all the ABI tags in the attribute list ATTR and either call
1731 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1733 static void
1734 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1736 if (!attr)
1737 return;
1738 for (; (attr = lookup_attribute ("abi_tag", attr));
1739 attr = TREE_CHAIN (attr))
1740 for (tree list = TREE_VALUE (attr); list;
1741 list = TREE_CHAIN (list))
1743 tree tag = TREE_VALUE (list);
1744 tree id = get_identifier (TREE_STRING_POINTER (tag));
1745 if (tp)
1746 check_tag (tag, id, tp, p);
1747 else
1748 IDENTIFIER_MARKED (id) = val;
1752 /* Find all the ABI tags on T and its enclosing scopes and either call
1753 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1755 static void
1756 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1758 while (t != global_namespace)
1760 tree attr;
1761 if (TYPE_P (t))
1763 attr = TYPE_ATTRIBUTES (t);
1764 t = CP_TYPE_CONTEXT (t);
1766 else
1768 attr = DECL_ATTRIBUTES (t);
1769 t = CP_DECL_CONTEXT (t);
1771 mark_or_check_attr_tags (attr, tp, p, val);
1775 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1776 types with ABI tags, add the corresponding identifiers to the VEC in
1777 *DATA and set IDENTIFIER_MARKED. */
1779 static tree
1780 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1782 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1783 /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1784 *walk_subtrees = 2;
1786 if (!OVERLOAD_TYPE_P (*tp))
1787 return NULL_TREE;
1789 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1790 anyway, but let's make sure of it. */
1791 *walk_subtrees = false;
1793 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1795 mark_or_check_tags (*tp, tp, p, false);
1797 return NULL_TREE;
1800 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1801 IDENTIFIER_MARKED on its ABI tags. */
1803 static tree
1804 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1806 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1807 /* Tell cp_walk_subtrees to look though typedefs. */
1808 *walk_subtrees = 2;
1810 if (!OVERLOAD_TYPE_P (*tp))
1811 return NULL_TREE;
1813 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1814 anyway, but let's make sure of it. */
1815 *walk_subtrees = false;
1817 bool *valp = static_cast<bool*>(data);
1819 mark_or_check_tags (*tp, NULL, NULL, *valp);
1821 return NULL_TREE;
1824 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1825 scopes. */
1827 static void
1828 mark_abi_tags (tree t, bool val)
1830 mark_or_check_tags (t, NULL, NULL, val);
1831 if (DECL_P (t))
1833 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1834 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1836 /* Template arguments are part of the signature. */
1837 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1838 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1840 tree arg = TREE_VEC_ELT (level, j);
1841 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1844 if (TREE_CODE (t) == FUNCTION_DECL)
1845 /* A function's parameter types are part of the signature, so
1846 we don't need to inherit any tags that are also in them. */
1847 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1848 arg = TREE_CHAIN (arg))
1849 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1850 mark_abi_tags_r, &val);
1854 /* Check that T has all the ABI tags that subobject SUBOB has, or
1855 warn if not. If T is a (variable or function) declaration, also
1856 return any missing tags, and add them to T if JUST_CHECKING is false. */
1858 static tree
1859 check_abi_tags (tree t, tree subob, bool just_checking = false)
1861 bool inherit = DECL_P (t);
1863 if (!inherit && !warn_abi_tag)
1864 return NULL_TREE;
1866 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1867 if (!TREE_PUBLIC (decl))
1868 /* No need to worry about things local to this TU. */
1869 return NULL_TREE;
1871 mark_abi_tags (t, true);
1873 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1874 struct abi_tag_data data = { t, subob, error_mark_node };
1875 if (inherit)
1876 data.tags = NULL_TREE;
1878 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1880 if (!(inherit && data.tags))
1881 /* We don't need to do anything with data.tags. */;
1882 else if (just_checking)
1883 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1885 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1886 IDENTIFIER_MARKED (id) = false;
1888 else
1890 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1891 if (attr)
1892 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1893 else
1894 DECL_ATTRIBUTES (t)
1895 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1898 mark_abi_tags (t, false);
1900 return data.tags;
1903 /* Check that DECL has all the ABI tags that are used in parts of its type
1904 that are not reflected in its mangled name. */
1906 void
1907 check_abi_tags (tree decl)
1909 if (VAR_P (decl))
1910 check_abi_tags (decl, TREE_TYPE (decl));
1911 else if (TREE_CODE (decl) == FUNCTION_DECL
1912 && !DECL_CONV_FN_P (decl)
1913 && !mangle_return_type_p (decl))
1914 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1917 /* Return any ABI tags that are used in parts of the type of DECL
1918 that are not reflected in its mangled name. This function is only
1919 used in backward-compatible mangling for ABI <11. */
1921 tree
1922 missing_abi_tags (tree decl)
1924 if (VAR_P (decl))
1925 return check_abi_tags (decl, TREE_TYPE (decl), true);
1926 else if (TREE_CODE (decl) == FUNCTION_DECL
1927 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1928 that we can use this function for setting need_abi_warning
1929 regardless of the current flag_abi_version. */
1930 && !mangle_return_type_p (decl))
1931 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1932 else
1933 return NULL_TREE;
1936 void
1937 inherit_targ_abi_tags (tree t)
1939 if (!CLASS_TYPE_P (t)
1940 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1941 return;
1943 mark_abi_tags (t, true);
1945 tree args = CLASSTYPE_TI_ARGS (t);
1946 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1947 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1949 tree level = TMPL_ARGS_LEVEL (args, i+1);
1950 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1952 tree arg = TREE_VEC_ELT (level, j);
1953 data.subob = arg;
1954 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1958 // If we found some tags on our template arguments, add them to our
1959 // abi_tag attribute.
1960 if (data.tags)
1962 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1963 if (attr)
1964 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1965 else
1966 TYPE_ATTRIBUTES (t)
1967 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1970 mark_abi_tags (t, false);
1973 /* Return true, iff class T has a non-virtual destructor that is
1974 accessible from outside the class heirarchy (i.e. is public, or
1975 there's a suitable friend. */
1977 static bool
1978 accessible_nvdtor_p (tree t)
1980 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1982 /* An implicitly declared destructor is always public. And,
1983 if it were virtual, we would have created it by now. */
1984 if (!dtor)
1985 return true;
1987 if (DECL_VINDEX (dtor))
1988 return false; /* Virtual */
1990 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1991 return true; /* Public */
1993 if (CLASSTYPE_FRIEND_CLASSES (t)
1994 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1995 return true; /* Has friends */
1997 return false;
2000 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
2001 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
2002 properties of the bases. */
2004 static void
2005 check_bases (tree t,
2006 int* cant_have_const_ctor_p,
2007 int* no_const_asn_ref_p)
2009 int i;
2010 bool seen_non_virtual_nearly_empty_base_p = 0;
2011 int seen_tm_mask = 0;
2012 tree base_binfo;
2013 tree binfo;
2014 tree field = NULL_TREE;
2016 if (!CLASSTYPE_NON_STD_LAYOUT (t))
2017 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2018 if (TREE_CODE (field) == FIELD_DECL)
2019 break;
2021 for (binfo = TYPE_BINFO (t), i = 0;
2022 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2024 tree basetype = TREE_TYPE (base_binfo);
2026 gcc_assert (COMPLETE_TYPE_P (basetype));
2028 if (CLASSTYPE_FINAL (basetype))
2029 error ("cannot derive from %<final%> base %qT in derived type %qT",
2030 basetype, t);
2032 /* If any base class is non-literal, so is the derived class. */
2033 if (!CLASSTYPE_LITERAL_P (basetype))
2034 CLASSTYPE_LITERAL_P (t) = false;
2036 /* If the base class doesn't have copy constructors or
2037 assignment operators that take const references, then the
2038 derived class cannot have such a member automatically
2039 generated. */
2040 if (TYPE_HAS_COPY_CTOR (basetype)
2041 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
2042 *cant_have_const_ctor_p = 1;
2043 if (TYPE_HAS_COPY_ASSIGN (basetype)
2044 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
2045 *no_const_asn_ref_p = 1;
2047 if (BINFO_VIRTUAL_P (base_binfo))
2048 /* A virtual base does not effect nearly emptiness. */
2050 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
2052 if (seen_non_virtual_nearly_empty_base_p)
2053 /* And if there is more than one nearly empty base, then the
2054 derived class is not nearly empty either. */
2055 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2056 else
2057 /* Remember we've seen one. */
2058 seen_non_virtual_nearly_empty_base_p = 1;
2060 else if (!is_empty_class (basetype))
2061 /* If the base class is not empty or nearly empty, then this
2062 class cannot be nearly empty. */
2063 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2065 /* A lot of properties from the bases also apply to the derived
2066 class. */
2067 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2068 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2069 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
2070 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2071 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
2072 || !TYPE_HAS_COPY_ASSIGN (basetype));
2073 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
2074 || !TYPE_HAS_COPY_CTOR (basetype));
2075 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2076 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
2077 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
2078 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
2079 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
2080 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
2081 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
2082 || TYPE_HAS_COMPLEX_DFLT (basetype));
2083 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
2084 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2085 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
2086 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
2087 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
2088 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
2089 if (TYPE_HAS_MUTABLE_P (basetype))
2090 CLASSTYPE_HAS_MUTABLE (t) = 1;
2092 /* A standard-layout class is a class that:
2094 * has no non-standard-layout base classes, */
2095 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
2096 if (!CLASSTYPE_NON_STD_LAYOUT (t))
2098 tree basefield;
2099 /* ...has no base classes of the same type as the first non-static
2100 data member... */
2101 if (field && DECL_CONTEXT (field) == t
2102 && (same_type_ignoring_top_level_qualifiers_p
2103 (TREE_TYPE (field), basetype)))
2104 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2105 /* DR 1813:
2106 ...has at most one base class subobject of any given type... */
2107 else if (CLASSTYPE_REPEATED_BASE_P (t))
2108 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2109 else
2110 /* ...has all non-static data members and bit-fields in the class
2111 and its base classes first declared in the same class. */
2112 for (basefield = TYPE_FIELDS (basetype); basefield;
2113 basefield = DECL_CHAIN (basefield))
2114 if (TREE_CODE (basefield) == FIELD_DECL
2115 && !(DECL_FIELD_IS_BASE (basefield)
2116 && is_empty_field (basefield)))
2118 if (field)
2119 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2120 else
2121 field = basefield;
2122 break;
2126 /* Don't bother collecting tm attributes if transactional memory
2127 support is not enabled. */
2128 if (flag_tm)
2130 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
2131 if (tm_attr)
2132 seen_tm_mask |= tm_attr_to_mask (tm_attr);
2135 check_abi_tags (t, basetype);
2138 /* If one of the base classes had TM attributes, and the current class
2139 doesn't define its own, then the current class inherits one. */
2140 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
2142 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
2143 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
2147 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
2148 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
2149 that have had a nearly-empty virtual primary base stolen by some
2150 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
2151 T. */
2153 static void
2154 determine_primary_bases (tree t)
2156 unsigned i;
2157 tree primary = NULL_TREE;
2158 tree type_binfo = TYPE_BINFO (t);
2159 tree base_binfo;
2161 /* Determine the primary bases of our bases. */
2162 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2163 base_binfo = TREE_CHAIN (base_binfo))
2165 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
2167 /* See if we're the non-virtual primary of our inheritance
2168 chain. */
2169 if (!BINFO_VIRTUAL_P (base_binfo))
2171 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
2172 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
2174 if (parent_primary
2175 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2176 BINFO_TYPE (parent_primary)))
2177 /* We are the primary binfo. */
2178 BINFO_PRIMARY_P (base_binfo) = 1;
2180 /* Determine if we have a virtual primary base, and mark it so.
2182 if (primary && BINFO_VIRTUAL_P (primary))
2184 tree this_primary = copied_binfo (primary, base_binfo);
2186 if (BINFO_PRIMARY_P (this_primary))
2187 /* Someone already claimed this base. */
2188 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
2189 else
2191 tree delta;
2193 BINFO_PRIMARY_P (this_primary) = 1;
2194 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
2196 /* A virtual binfo might have been copied from within
2197 another hierarchy. As we're about to use it as a
2198 primary base, make sure the offsets match. */
2199 delta = size_diffop_loc (input_location,
2200 fold_convert (ssizetype,
2201 BINFO_OFFSET (base_binfo)),
2202 fold_convert (ssizetype,
2203 BINFO_OFFSET (this_primary)));
2205 propagate_binfo_offsets (this_primary, delta);
2210 /* First look for a dynamic direct non-virtual base. */
2211 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
2213 tree basetype = BINFO_TYPE (base_binfo);
2215 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
2217 primary = base_binfo;
2218 goto found;
2222 /* A "nearly-empty" virtual base class can be the primary base
2223 class, if no non-virtual polymorphic base can be found. Look for
2224 a nearly-empty virtual dynamic base that is not already a primary
2225 base of something in the hierarchy. If there is no such base,
2226 just pick the first nearly-empty virtual base. */
2228 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2229 base_binfo = TREE_CHAIN (base_binfo))
2230 if (BINFO_VIRTUAL_P (base_binfo)
2231 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2233 if (!BINFO_PRIMARY_P (base_binfo))
2235 /* Found one that is not primary. */
2236 primary = base_binfo;
2237 goto found;
2239 else if (!primary)
2240 /* Remember the first candidate. */
2241 primary = base_binfo;
2244 found:
2245 /* If we've got a primary base, use it. */
2246 if (primary)
2248 tree basetype = BINFO_TYPE (primary);
2250 CLASSTYPE_PRIMARY_BINFO (t) = primary;
2251 if (BINFO_PRIMARY_P (primary))
2252 /* We are stealing a primary base. */
2253 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2254 BINFO_PRIMARY_P (primary) = 1;
2255 if (BINFO_VIRTUAL_P (primary))
2257 tree delta;
2259 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2260 /* A virtual binfo might have been copied from within
2261 another hierarchy. As we're about to use it as a primary
2262 base, make sure the offsets match. */
2263 delta = size_diffop_loc (input_location, ssize_int (0),
2264 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2266 propagate_binfo_offsets (primary, delta);
2269 primary = TYPE_BINFO (basetype);
2271 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2272 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2273 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2277 /* Update the variant types of T. */
2279 void
2280 fixup_type_variants (tree type)
2282 if (!type)
2283 return;
2285 for (tree variant = TYPE_NEXT_VARIANT (type);
2286 variant;
2287 variant = TYPE_NEXT_VARIANT (variant))
2289 /* These fields are in the _TYPE part of the node, not in
2290 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2291 TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2292 TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2293 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2294 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2296 TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2297 CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2299 TYPE_BINFO (variant) = TYPE_BINFO (type);
2301 /* Copy whatever these are holding today. */
2302 TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2303 TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2305 TYPE_SIZE (variant) = TYPE_SIZE (type);
2306 TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2308 if (!TYPE_USER_ALIGN (variant)
2309 || TYPE_NAME (variant) == TYPE_NAME (type)
2310 || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2312 TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2313 TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2316 TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2317 TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2318 TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2319 TREE_ADDRESSABLE (variant) = TREE_ADDRESSABLE (type);
2323 /* KLASS is a class that we're applying may_alias to after the body is
2324 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2325 canonical type(s) will be implicitly updated. */
2327 static void
2328 fixup_may_alias (tree klass)
2330 tree t, v;
2332 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2333 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2334 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2335 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2336 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2337 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2340 /* Early variant fixups: we apply attributes at the beginning of the class
2341 definition, and we need to fix up any variants that have already been
2342 made via elaborated-type-specifier so that check_qualified_type works. */
2344 void
2345 fixup_attribute_variants (tree t)
2347 tree variants;
2349 if (!t)
2350 return;
2352 tree attrs = TYPE_ATTRIBUTES (t);
2353 unsigned align = TYPE_ALIGN (t);
2354 bool user_align = TYPE_USER_ALIGN (t);
2355 bool may_alias = lookup_attribute ("may_alias", attrs);
2356 bool packed = TYPE_PACKED (t);
2358 if (may_alias)
2359 fixup_may_alias (t);
2361 for (variants = TYPE_NEXT_VARIANT (t);
2362 variants;
2363 variants = TYPE_NEXT_VARIANT (variants))
2365 /* These are the two fields that check_qualified_type looks at and
2366 are affected by attributes. */
2367 TYPE_ATTRIBUTES (variants) = attrs;
2368 unsigned valign = align;
2369 if (TYPE_USER_ALIGN (variants))
2370 valign = MAX (valign, TYPE_ALIGN (variants));
2371 else
2372 TYPE_USER_ALIGN (variants) = user_align;
2373 SET_TYPE_ALIGN (variants, valign);
2374 TYPE_PACKED (variants) = packed;
2375 if (may_alias)
2376 fixup_may_alias (variants);
2380 /* Set memoizing fields and bits of T (and its variants) for later
2381 use. */
2383 static void
2384 finish_struct_bits (tree t)
2386 /* If this type has a copy constructor or a destructor, force its
2387 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2388 nonzero. This will cause it to be passed by invisible reference
2389 and prevent it from being returned in a register. */
2390 if (type_has_nontrivial_copy_init (t)
2391 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2393 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2394 SET_TYPE_MODE (t, BLKmode);
2395 TREE_ADDRESSABLE (t) = 1;
2398 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2399 /* For a class w/o baseclasses, 'finish_struct' has set
2400 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2401 Similarly for a class whose base classes do not have vtables.
2402 When neither of these is true, we might have removed abstract
2403 virtuals (by providing a definition), added some (by declaring
2404 new ones), or redeclared ones from a base class. We need to
2405 recalculate what's really an abstract virtual at this point (by
2406 looking in the vtables). */
2407 get_pure_virtuals (t);
2409 /* Fix up variants (if any). */
2410 fixup_type_variants (t);
2413 /* Issue warnings about T having private constructors, but no friends,
2414 and so forth.
2416 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2417 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2418 non-private static member functions. */
2420 static void
2421 maybe_warn_about_overly_private_class (tree t)
2423 int has_member_fn = 0;
2424 int has_nonprivate_method = 0;
2425 bool nonprivate_ctor = false;
2427 if (!warn_ctor_dtor_privacy
2428 /* If the class has friends, those entities might create and
2429 access instances, so we should not warn. */
2430 || (CLASSTYPE_FRIEND_CLASSES (t)
2431 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2432 /* We will have warned when the template was declared; there's
2433 no need to warn on every instantiation. */
2434 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2435 /* There's no reason to even consider warning about this
2436 class. */
2437 return;
2439 /* We only issue one warning, if more than one applies, because
2440 otherwise, on code like:
2442 class A {
2443 // Oops - forgot `public:'
2444 A();
2445 A(const A&);
2446 ~A();
2449 we warn several times about essentially the same problem. */
2451 /* Check to see if all (non-constructor, non-destructor) member
2452 functions are private. (Since there are no friends or
2453 non-private statics, we can't ever call any of the private member
2454 functions.) */
2455 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2456 if (TREE_CODE (fn) == USING_DECL
2457 && DECL_NAME (fn) == ctor_identifier
2458 && !TREE_PRIVATE (fn))
2459 nonprivate_ctor = true;
2460 else if (!DECL_DECLARES_FUNCTION_P (fn))
2461 /* Not a function. */;
2462 else if (DECL_ARTIFICIAL (fn))
2463 /* We're not interested in compiler-generated methods; they don't
2464 provide any way to call private members. */;
2465 else if (!TREE_PRIVATE (fn))
2467 if (DECL_STATIC_FUNCTION_P (fn))
2468 /* A non-private static member function is just like a
2469 friend; it can create and invoke private member
2470 functions, and be accessed without a class
2471 instance. */
2472 return;
2474 has_nonprivate_method = 1;
2475 /* Keep searching for a static member function. */
2477 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2478 has_member_fn = 1;
2480 if (!has_nonprivate_method && has_member_fn)
2482 /* There are no non-private methods, and there's at least one
2483 private member function that isn't a constructor or
2484 destructor. (If all the private members are
2485 constructors/destructors we want to use the code below that
2486 issues error messages specifically referring to
2487 constructors/destructors.) */
2488 unsigned i;
2489 tree binfo = TYPE_BINFO (t);
2491 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2492 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2494 has_nonprivate_method = 1;
2495 break;
2497 if (!has_nonprivate_method)
2499 warning (OPT_Wctor_dtor_privacy,
2500 "all member functions in class %qT are private", t);
2501 return;
2505 /* Even if some of the member functions are non-private, the class
2506 won't be useful for much if all the constructors or destructors
2507 are private: such an object can never be created or destroyed. */
2508 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2509 if (TREE_PRIVATE (dtor))
2511 warning (OPT_Wctor_dtor_privacy,
2512 "%q#T only defines a private destructor and has no friends",
2514 return;
2517 /* Warn about classes that have private constructors and no friends. */
2518 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2519 /* Implicitly generated constructors are always public. */
2520 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2522 tree copy_or_move = NULL_TREE;
2524 /* If a non-template class does not define a copy
2525 constructor, one is defined for it, enabling it to avoid
2526 this warning. For a template class, this does not
2527 happen, and so we would normally get a warning on:
2529 template <class T> class C { private: C(); };
2531 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2532 complete non-template or fully instantiated classes have this
2533 flag set. */
2534 if (!TYPE_HAS_COPY_CTOR (t))
2535 nonprivate_ctor = true;
2536 else
2537 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2538 if (TREE_PRIVATE (fn))
2539 continue;
2540 else if (copy_fn_p (fn) || move_fn_p (fn))
2541 /* Ideally, we wouldn't count any constructor that takes
2542 an argument of the class type as a parameter, because
2543 such things cannot be used to construct an instance of
2544 the class unless you already have one. */
2545 copy_or_move = fn;
2546 else
2548 nonprivate_ctor = true;
2549 break;
2552 if (!nonprivate_ctor)
2554 auto_diagnostic_group d;
2555 bool w = warning (OPT_Wctor_dtor_privacy,
2556 "%q#T only defines private constructors and has "
2557 "no friends", t);
2558 if (w && copy_or_move)
2559 inform (DECL_SOURCE_LOCATION (copy_or_move),
2560 "%q#D is public, but requires an existing %q#T object",
2561 copy_or_move, t);
2562 return;
2567 /* Make BINFO's vtable have N entries, including RTTI entries,
2568 vbase and vcall offsets, etc. Set its type and call the back end
2569 to lay it out. */
2571 static void
2572 layout_vtable_decl (tree binfo, int n)
2574 tree atype;
2575 tree vtable;
2577 atype = build_array_of_n_type (vtable_entry_type, n);
2578 layout_type (atype);
2580 /* We may have to grow the vtable. */
2581 vtable = get_vtbl_decl_for_binfo (binfo);
2582 if (!same_type_p (TREE_TYPE (vtable), atype))
2584 TREE_TYPE (vtable) = atype;
2585 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2586 layout_decl (vtable, 0);
2590 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2591 have the same signature. */
2594 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2596 /* One destructor overrides another if they are the same kind of
2597 destructor. */
2598 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2599 && special_function_p (base_fndecl) == special_function_p (fndecl))
2600 return 1;
2601 /* But a non-destructor never overrides a destructor, nor vice
2602 versa, nor do different kinds of destructors override
2603 one-another. For example, a complete object destructor does not
2604 override a deleting destructor. */
2605 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2606 return 0;
2608 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2609 || (DECL_CONV_FN_P (fndecl)
2610 && DECL_CONV_FN_P (base_fndecl)
2611 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2612 DECL_CONV_FN_TYPE (base_fndecl))))
2614 tree fntype = TREE_TYPE (fndecl);
2615 tree base_fntype = TREE_TYPE (base_fndecl);
2616 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2617 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2618 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2619 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2620 return 1;
2622 return 0;
2625 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2626 subobject. */
2628 static bool
2629 base_derived_from (tree derived, tree base)
2631 tree probe;
2633 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2635 if (probe == derived)
2636 return true;
2637 else if (BINFO_VIRTUAL_P (probe))
2638 /* If we meet a virtual base, we can't follow the inheritance
2639 any more. See if the complete type of DERIVED contains
2640 such a virtual base. */
2641 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2642 != NULL_TREE);
2644 return false;
2647 struct find_final_overrider_data {
2648 /* The function for which we are trying to find a final overrider. */
2649 tree fn;
2650 /* The base class in which the function was declared. */
2651 tree declaring_base;
2652 /* The candidate overriders. */
2653 tree candidates;
2654 /* Path to most derived. */
2655 auto_vec<tree> path;
2658 /* Add the overrider along the current path to FFOD->CANDIDATES.
2659 Returns true if an overrider was found; false otherwise. */
2661 static bool
2662 dfs_find_final_overrider_1 (tree binfo,
2663 find_final_overrider_data *ffod,
2664 unsigned depth)
2666 tree method;
2668 /* If BINFO is not the most derived type, try a more derived class.
2669 A definition there will overrider a definition here. */
2670 if (depth)
2672 depth--;
2673 if (dfs_find_final_overrider_1
2674 (ffod->path[depth], ffod, depth))
2675 return true;
2678 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2679 if (method)
2681 tree *candidate = &ffod->candidates;
2683 /* Remove any candidates overridden by this new function. */
2684 while (*candidate)
2686 /* If *CANDIDATE overrides METHOD, then METHOD
2687 cannot override anything else on the list. */
2688 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2689 return true;
2690 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2691 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2692 *candidate = TREE_CHAIN (*candidate);
2693 else
2694 candidate = &TREE_CHAIN (*candidate);
2697 /* Add the new function. */
2698 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2699 return true;
2702 return false;
2705 /* Called from find_final_overrider via dfs_walk. */
2707 static tree
2708 dfs_find_final_overrider_pre (tree binfo, void *data)
2710 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2712 if (binfo == ffod->declaring_base)
2713 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2714 ffod->path.safe_push (binfo);
2716 return NULL_TREE;
2719 static tree
2720 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2722 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2723 ffod->path.pop ();
2725 return NULL_TREE;
2728 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2729 FN and whose TREE_VALUE is the binfo for the base where the
2730 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2731 DERIVED) is the base object in which FN is declared. */
2733 static tree
2734 find_final_overrider (tree derived, tree binfo, tree fn)
2736 find_final_overrider_data ffod;
2738 /* Getting this right is a little tricky. This is valid:
2740 struct S { virtual void f (); };
2741 struct T { virtual void f (); };
2742 struct U : public S, public T { };
2744 even though calling `f' in `U' is ambiguous. But,
2746 struct R { virtual void f(); };
2747 struct S : virtual public R { virtual void f (); };
2748 struct T : virtual public R { virtual void f (); };
2749 struct U : public S, public T { };
2751 is not -- there's no way to decide whether to put `S::f' or
2752 `T::f' in the vtable for `R'.
2754 The solution is to look at all paths to BINFO. If we find
2755 different overriders along any two, then there is a problem. */
2756 if (DECL_THUNK_P (fn))
2757 fn = THUNK_TARGET (fn);
2759 /* Determine the depth of the hierarchy. */
2760 ffod.fn = fn;
2761 ffod.declaring_base = binfo;
2762 ffod.candidates = NULL_TREE;
2763 ffod.path.create (30);
2765 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2766 dfs_find_final_overrider_post, &ffod);
2768 /* If there was no winner, issue an error message. */
2769 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2770 return error_mark_node;
2772 return ffod.candidates;
2775 /* Return the index of the vcall offset for FN when TYPE is used as a
2776 virtual base. */
2778 static tree
2779 get_vcall_index (tree fn, tree type)
2781 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2782 tree_pair_p p;
2783 unsigned ix;
2785 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2786 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2787 || same_signature_p (fn, p->purpose))
2788 return p->value;
2790 /* There should always be an appropriate index. */
2791 gcc_unreachable ();
2794 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2795 overrider at that index in the vtable. This should only be used when we
2796 know that BINFO is correct for the dynamic type of the object. */
2798 tree
2799 lookup_vfn_in_binfo (tree idx, tree binfo)
2801 int ix = tree_to_shwi (idx);
2802 if (TARGET_VTABLE_USES_DESCRIPTORS)
2803 ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2804 while (BINFO_PRIMARY_P (binfo))
2805 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2806 class that actually owns the vtable. */
2807 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2808 tree virtuals = BINFO_VIRTUALS (binfo);
2809 return TREE_VALUE (chain_index (ix, virtuals));
2812 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2813 dominated by T. FN is the old function; VIRTUALS points to the
2814 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2815 of that entry in the list. */
2817 static void
2818 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2819 unsigned ix)
2821 tree b;
2822 tree overrider;
2823 tree delta;
2824 tree virtual_base;
2825 tree first_defn;
2826 tree overrider_fn, overrider_target;
2827 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2828 tree over_return, base_return;
2829 bool lost = false;
2831 /* Find the nearest primary base (possibly binfo itself) which defines
2832 this function; this is the class the caller will convert to when
2833 calling FN through BINFO. */
2834 for (b = binfo; ; b = get_primary_binfo (b))
2836 gcc_assert (b);
2837 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2838 break;
2840 /* The nearest definition is from a lost primary. */
2841 if (BINFO_LOST_PRIMARY_P (b))
2842 lost = true;
2844 first_defn = b;
2846 /* Find the final overrider. */
2847 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2848 if (overrider == error_mark_node)
2850 error ("no unique final overrider for %qD in %qT", target_fn, t);
2851 return;
2853 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2855 /* Check for adjusting covariant return types. */
2856 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2857 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2859 if (INDIRECT_TYPE_P (over_return)
2860 && TREE_CODE (over_return) == TREE_CODE (base_return)
2861 && CLASS_TYPE_P (TREE_TYPE (over_return))
2862 && CLASS_TYPE_P (TREE_TYPE (base_return))
2863 /* If the overrider is invalid, don't even try. */
2864 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2866 /* If FN is a covariant thunk, we must figure out the adjustment
2867 to the final base FN was converting to. As OVERRIDER_TARGET might
2868 also be converting to the return type of FN, we have to
2869 combine the two conversions here. */
2870 tree fixed_offset, virtual_offset;
2872 over_return = TREE_TYPE (over_return);
2873 base_return = TREE_TYPE (base_return);
2875 if (DECL_THUNK_P (fn))
2877 gcc_assert (DECL_RESULT_THUNK_P (fn));
2878 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2879 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2881 else
2882 fixed_offset = virtual_offset = NULL_TREE;
2884 if (virtual_offset)
2885 /* Find the equivalent binfo within the return type of the
2886 overriding function. We will want the vbase offset from
2887 there. */
2888 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2889 over_return);
2890 else if (!same_type_ignoring_top_level_qualifiers_p
2891 (over_return, base_return))
2893 /* There was no existing virtual thunk (which takes
2894 precedence). So find the binfo of the base function's
2895 return type within the overriding function's return type.
2896 Fortunately we know the covariancy is valid (it
2897 has already been checked), so we can just iterate along
2898 the binfos, which have been chained in inheritance graph
2899 order. Of course it is lame that we have to repeat the
2900 search here anyway -- we should really be caching pieces
2901 of the vtable and avoiding this repeated work. */
2902 tree thunk_binfo = NULL_TREE;
2903 tree base_binfo = TYPE_BINFO (base_return);
2905 /* Find the base binfo within the overriding function's
2906 return type. We will always find a thunk_binfo, except
2907 when the covariancy is invalid (which we will have
2908 already diagnosed). */
2909 if (base_binfo)
2910 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2911 thunk_binfo = TREE_CHAIN (thunk_binfo))
2912 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2913 BINFO_TYPE (base_binfo)))
2914 break;
2915 gcc_assert (thunk_binfo || errorcount);
2917 /* See if virtual inheritance is involved. */
2918 for (virtual_offset = thunk_binfo;
2919 virtual_offset;
2920 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2921 if (BINFO_VIRTUAL_P (virtual_offset))
2922 break;
2924 if (virtual_offset
2925 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2927 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2929 if (virtual_offset)
2931 /* We convert via virtual base. Adjust the fixed
2932 offset to be from there. */
2933 offset =
2934 size_diffop (offset,
2935 fold_convert (ssizetype,
2936 BINFO_OFFSET (virtual_offset)));
2938 if (fixed_offset)
2939 /* There was an existing fixed offset, this must be
2940 from the base just converted to, and the base the
2941 FN was thunking to. */
2942 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2943 else
2944 fixed_offset = offset;
2948 if (fixed_offset || virtual_offset)
2949 /* Replace the overriding function with a covariant thunk. We
2950 will emit the overriding function in its own slot as
2951 well. */
2952 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2953 fixed_offset, virtual_offset);
2955 else
2956 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2957 !DECL_THUNK_P (fn));
2959 /* If we need a covariant thunk, then we may need to adjust first_defn.
2960 The ABI specifies that the thunks emitted with a function are
2961 determined by which bases the function overrides, so we need to be
2962 sure that we're using a thunk for some overridden base; even if we
2963 know that the necessary this adjustment is zero, there may not be an
2964 appropriate zero-this-adjustment thunk for us to use since thunks for
2965 overriding virtual bases always use the vcall offset.
2967 Furthermore, just choosing any base that overrides this function isn't
2968 quite right, as this slot won't be used for calls through a type that
2969 puts a covariant thunk here. Calling the function through such a type
2970 will use a different slot, and that slot is the one that determines
2971 the thunk emitted for that base.
2973 So, keep looking until we find the base that we're really overriding
2974 in this slot: the nearest primary base that doesn't use a covariant
2975 thunk in this slot. */
2976 if (overrider_target != overrider_fn)
2978 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2979 /* We already know that the overrider needs a covariant thunk. */
2980 b = get_primary_binfo (b);
2981 for (; ; b = get_primary_binfo (b))
2983 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2984 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2985 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2986 break;
2987 if (BINFO_LOST_PRIMARY_P (b))
2988 lost = true;
2990 first_defn = b;
2993 /* Assume that we will produce a thunk that convert all the way to
2994 the final overrider, and not to an intermediate virtual base. */
2995 virtual_base = NULL_TREE;
2997 /* See if we can convert to an intermediate virtual base first, and then
2998 use the vcall offset located there to finish the conversion. */
2999 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
3001 /* If we find the final overrider, then we can stop
3002 walking. */
3003 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
3004 BINFO_TYPE (TREE_VALUE (overrider))))
3005 break;
3007 /* If we find a virtual base, and we haven't yet found the
3008 overrider, then there is a virtual base between the
3009 declaring base (first_defn) and the final overrider. */
3010 if (BINFO_VIRTUAL_P (b))
3012 virtual_base = b;
3013 break;
3017 /* Compute the constant adjustment to the `this' pointer. The
3018 `this' pointer, when this function is called, will point at BINFO
3019 (or one of its primary bases, which are at the same offset). */
3020 if (virtual_base)
3021 /* The `this' pointer needs to be adjusted from the declaration to
3022 the nearest virtual base. */
3023 delta = size_diffop_loc (input_location,
3024 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
3025 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
3026 else if (lost)
3027 /* If the nearest definition is in a lost primary, we don't need an
3028 entry in our vtable. Except possibly in a constructor vtable,
3029 if we happen to get our primary back. In that case, the offset
3030 will be zero, as it will be a primary base. */
3031 delta = size_zero_node;
3032 else
3033 /* The `this' pointer needs to be adjusted from pointing to
3034 BINFO to pointing at the base where the final overrider
3035 appears. */
3036 delta = size_diffop_loc (input_location,
3037 fold_convert (ssizetype,
3038 BINFO_OFFSET (TREE_VALUE (overrider))),
3039 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
3041 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
3043 if (virtual_base)
3044 BV_VCALL_INDEX (*virtuals)
3045 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
3046 else
3047 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
3049 BV_LOST_PRIMARY (*virtuals) = lost;
3052 /* Called from modify_all_vtables via dfs_walk. */
3054 static tree
3055 dfs_modify_vtables (tree binfo, void* data)
3057 tree t = (tree) data;
3058 tree virtuals;
3059 tree old_virtuals;
3060 unsigned ix;
3062 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3063 /* A base without a vtable needs no modification, and its bases
3064 are uninteresting. */
3065 return dfs_skip_bases;
3067 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
3068 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3069 /* Don't do the primary vtable, if it's new. */
3070 return NULL_TREE;
3072 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
3073 /* There's no need to modify the vtable for a non-virtual primary
3074 base; we're not going to use that vtable anyhow. We do still
3075 need to do this for virtual primary bases, as they could become
3076 non-primary in a construction vtable. */
3077 return NULL_TREE;
3079 make_new_vtable (t, binfo);
3081 /* Now, go through each of the virtual functions in the virtual
3082 function table for BINFO. Find the final overrider, and update
3083 the BINFO_VIRTUALS list appropriately. */
3084 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
3085 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3086 virtuals;
3087 ix++, virtuals = TREE_CHAIN (virtuals),
3088 old_virtuals = TREE_CHAIN (old_virtuals))
3089 update_vtable_entry_for_fn (t,
3090 binfo,
3091 BV_FN (old_virtuals),
3092 &virtuals, ix);
3094 return NULL_TREE;
3097 /* Update all of the primary and secondary vtables for T. Create new
3098 vtables as required, and initialize their RTTI information. Each
3099 of the functions in VIRTUALS is declared in T and may override a
3100 virtual function from a base class; find and modify the appropriate
3101 entries to point to the overriding functions. Returns a list, in
3102 declaration order, of the virtual functions that are declared in T,
3103 but do not appear in the primary base class vtable, and which
3104 should therefore be appended to the end of the vtable for T. */
3106 static tree
3107 modify_all_vtables (tree t, tree virtuals)
3109 tree binfo = TYPE_BINFO (t);
3110 tree *fnsp;
3112 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
3113 if (TYPE_CONTAINS_VPTR_P (t))
3114 get_vtable_decl (t, false);
3116 /* Update all of the vtables. */
3117 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
3119 /* Add virtual functions not already in our primary vtable. These
3120 will be both those introduced by this class, and those overridden
3121 from secondary bases. It does not include virtuals merely
3122 inherited from secondary bases. */
3123 for (fnsp = &virtuals; *fnsp; )
3125 tree fn = TREE_VALUE (*fnsp);
3127 if (!value_member (fn, BINFO_VIRTUALS (binfo))
3128 || DECL_VINDEX (fn) == error_mark_node)
3130 /* We don't need to adjust the `this' pointer when
3131 calling this function. */
3132 BV_DELTA (*fnsp) = integer_zero_node;
3133 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
3135 /* This is a function not already in our vtable. Keep it. */
3136 fnsp = &TREE_CHAIN (*fnsp);
3138 else
3139 /* We've already got an entry for this function. Skip it. */
3140 *fnsp = TREE_CHAIN (*fnsp);
3143 return virtuals;
3146 /* Get the base virtual function declarations in T that have the
3147 indicated NAME. */
3149 static void
3150 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
3152 bool found_decls = false;
3154 /* Find virtual functions in T with the indicated NAME. */
3155 for (tree method : ovl_range (get_class_binding (t, name)))
3157 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
3159 base_fndecls->safe_push (method);
3160 found_decls = true;
3164 if (found_decls)
3165 return;
3167 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3168 for (int i = 0; i < n_baseclasses; i++)
3170 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3171 get_basefndecls (name, basetype, base_fndecls);
3175 /* If this method overrides a virtual method from a base, then mark
3176 this member function as being virtual as well. Do 'final' and
3177 'override' checks too. */
3179 void
3180 check_for_override (tree decl, tree ctype)
3182 if (TREE_CODE (decl) == TEMPLATE_DECL)
3183 /* In [temp.mem] we have:
3185 A specialization of a member function template does not
3186 override a virtual function from a base class. */
3187 return;
3189 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
3190 used for a vfunc. That avoids the expensive look_for_overrides
3191 call that when we know there's nothing to find. As conversion
3192 operators for the same type can have distinct identifiers, we
3193 cannot optimize those in that way. */
3194 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3195 || DECL_CONV_FN_P (decl))
3196 && look_for_overrides (ctype, decl)
3197 /* Check staticness after we've checked if we 'override'. */
3198 && !DECL_STATIC_FUNCTION_P (decl))
3200 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3201 the error_mark_node so that we know it is an overriding
3202 function. */
3203 DECL_VINDEX (decl) = decl;
3205 if (warn_override
3206 && !DECL_OVERRIDE_P (decl)
3207 && !DECL_FINAL_P (decl)
3208 && !DECL_DESTRUCTOR_P (decl))
3209 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3210 "%qD can be marked override", decl);
3212 else if (DECL_OVERRIDE_P (decl))
3213 error ("%q+#D marked %<override%>, but does not override", decl);
3215 if (DECL_VIRTUAL_P (decl))
3217 /* Remember this identifier is virtual name. */
3218 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
3220 if (!DECL_VINDEX (decl))
3221 /* It's a new vfunc. */
3222 DECL_VINDEX (decl) = error_mark_node;
3224 if (DECL_DESTRUCTOR_P (decl))
3225 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3227 else if (DECL_FINAL_P (decl))
3228 error ("%q+#D marked %<final%>, but is not virtual", decl);
3231 /* Warn about hidden virtual functions that are not overridden in t.
3232 We know that constructors and destructors don't apply. */
3234 static void
3235 warn_hidden (tree t)
3237 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3238 for (unsigned ix = member_vec->length (); ix--;)
3240 tree fns = (*member_vec)[ix];
3242 if (!OVL_P (fns))
3243 continue;
3245 tree name = OVL_NAME (fns);
3246 auto_vec<tree, 20> base_fndecls;
3247 tree base_binfo;
3248 tree binfo;
3249 unsigned j;
3251 if (IDENTIFIER_CDTOR_P (name))
3252 continue;
3254 /* Iterate through all of the base classes looking for possibly
3255 hidden functions. */
3256 for (binfo = TYPE_BINFO (t), j = 0;
3257 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3259 tree basetype = BINFO_TYPE (base_binfo);
3260 get_basefndecls (name, basetype, &base_fndecls);
3263 /* If there are no functions to hide, continue. */
3264 if (base_fndecls.is_empty ())
3265 continue;
3267 /* Remove any overridden functions. */
3268 bool seen_non_override = false;
3269 for (tree fndecl : ovl_range (fns))
3271 bool any_override = false;
3272 if (TREE_CODE (fndecl) == FUNCTION_DECL
3273 && DECL_VINDEX (fndecl))
3275 /* If the method from the base class has the same
3276 signature as the method from the derived class, it
3277 has been overridden. Note that we can't move on
3278 after finding one match: fndecl might override
3279 multiple base fns. */
3280 for (size_t k = 0; k < base_fndecls.length (); k++)
3281 if (base_fndecls[k]
3282 && same_signature_p (fndecl, base_fndecls[k]))
3284 base_fndecls[k] = NULL_TREE;
3285 any_override = true;
3288 if (!any_override)
3289 seen_non_override = true;
3292 if (!seen_non_override && warn_overloaded_virtual == 1)
3293 /* All the derived fns override base virtuals. */
3294 return;
3296 /* Now give a warning for all base functions without overriders,
3297 as they are hidden. */
3298 for (tree base_fndecl : base_fndecls)
3299 if (base_fndecl)
3301 auto_diagnostic_group d;
3302 /* Here we know it is a hider, and no overrider exists. */
3303 if (warning_at (location_of (base_fndecl),
3304 OPT_Woverloaded_virtual_,
3305 "%qD was hidden", base_fndecl))
3306 inform (location_of (fns), " by %qD", fns);
3311 /* Recursive helper for finish_struct_anon. */
3313 static void
3314 finish_struct_anon_r (tree field)
3316 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3318 /* We're generally only interested in entities the user
3319 declared, but we also find nested classes by noticing
3320 the TYPE_DECL that we create implicitly. You're
3321 allowed to put one anonymous union inside another,
3322 though, so we explicitly tolerate that. We use
3323 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3324 we also allow unnamed types used for defining fields. */
3325 if (DECL_ARTIFICIAL (elt)
3326 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3327 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3328 continue;
3330 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3331 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3333 /* Recurse into the anonymous aggregates to correctly handle
3334 access control (c++/24926):
3336 class A {
3337 union {
3338 union {
3339 int i;
3344 int j=A().i; */
3345 if (DECL_NAME (elt) == NULL_TREE
3346 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3347 finish_struct_anon_r (elt);
3351 /* Fix up any anonymous union/struct members of T. */
3353 static void
3354 finish_struct_anon (tree t)
3356 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3358 if (TREE_STATIC (field))
3359 continue;
3360 if (TREE_CODE (field) != FIELD_DECL)
3361 continue;
3363 if (DECL_NAME (field) == NULL_TREE
3364 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3365 finish_struct_anon_r (field);
3369 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3370 will be used later during class template instantiation.
3371 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3372 a non-static member data (FIELD_DECL), a member function
3373 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3374 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3375 When FRIEND_P is nonzero, T is either a friend class
3376 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3377 (FUNCTION_DECL, TEMPLATE_DECL). */
3379 void
3380 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3382 if (CLASSTYPE_TEMPLATE_INFO (type)
3383 && TREE_CODE (t) != CONST_DECL)
3385 tree purpose = friend_p ? NULL_TREE : type;
3387 CLASSTYPE_DECL_LIST (type)
3388 = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3392 /* This function is called from declare_virt_assop_and_dtor via
3393 dfs_walk_all.
3395 DATA is a type that direcly or indirectly inherits the base
3396 represented by BINFO. If BINFO contains a virtual assignment [copy
3397 assignment or move assigment] operator or a virtual constructor,
3398 declare that function in DATA if it hasn't been already declared. */
3400 static tree
3401 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3403 tree bv, fn, t = (tree)data;
3404 tree opname = assign_op_identifier;
3406 gcc_assert (t && CLASS_TYPE_P (t));
3407 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3409 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3410 /* A base without a vtable needs no modification, and its bases
3411 are uninteresting. */
3412 return dfs_skip_bases;
3414 if (BINFO_PRIMARY_P (binfo))
3415 /* If this is a primary base, then we have already looked at the
3416 virtual functions of its vtable. */
3417 return NULL_TREE;
3419 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3421 fn = BV_FN (bv);
3423 if (DECL_NAME (fn) == opname)
3425 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3426 lazily_declare_fn (sfk_copy_assignment, t);
3427 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3428 lazily_declare_fn (sfk_move_assignment, t);
3430 else if (DECL_DESTRUCTOR_P (fn)
3431 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3432 lazily_declare_fn (sfk_destructor, t);
3435 return NULL_TREE;
3438 /* If the class type T has a direct or indirect base that contains a
3439 virtual assignment operator or a virtual destructor, declare that
3440 function in T if it hasn't been already declared. */
3442 static void
3443 declare_virt_assop_and_dtor (tree t)
3445 if (!(TYPE_POLYMORPHIC_P (t)
3446 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3447 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3448 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3449 return;
3451 dfs_walk_all (TYPE_BINFO (t),
3452 dfs_declare_virt_assop_and_dtor,
3453 NULL, t);
3456 /* Declare the inheriting constructor for class T inherited from base
3457 constructor CTOR with the parameter array PARMS of size NPARMS. */
3459 static void
3460 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3462 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3464 /* We don't declare an inheriting ctor that would be a default,
3465 copy or move ctor for derived or base. */
3466 if (nparms == 0)
3467 return;
3468 if (nparms == 1
3469 && TYPE_REF_P (parms[0]))
3471 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3472 if (parm == t || parm == DECL_CONTEXT (ctor))
3473 return;
3476 tree parmlist = void_list_node;
3477 for (int i = nparms - 1; i >= 0; i--)
3478 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3479 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3480 t, false, ctor, parmlist);
3482 if (add_method (t, fn, false))
3484 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3485 TYPE_FIELDS (t) = fn;
3489 /* Declare all the inheriting constructors for class T inherited from base
3490 constructor CTOR. */
3492 static void
3493 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3495 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3497 if (flag_new_inheriting_ctors)
3499 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3500 t, /*const*/false, ctor, parms);
3501 add_method (t, ctor, using_decl != NULL_TREE);
3502 return;
3505 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3506 int i = 0;
3507 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3509 if (TREE_PURPOSE (parms))
3510 one_inheriting_sig (t, ctor, new_parms, i);
3511 new_parms[i++] = TREE_VALUE (parms);
3513 one_inheriting_sig (t, ctor, new_parms, i);
3514 if (parms == NULL_TREE)
3516 auto_diagnostic_group d;
3517 if (warning (OPT_Winherited_variadic_ctor,
3518 "the ellipsis in %qD is not inherited", ctor))
3519 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3523 /* Implicitly declare T(). */
3525 static void
3526 add_implicit_default_ctor (tree t)
3528 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3529 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3530 if (cxx_dialect >= cxx11)
3531 TYPE_HAS_CONSTEXPR_CTOR (t)
3532 /* Don't force the declaration to get a hard answer; if the
3533 definition would have made the class non-literal, it will still be
3534 non-literal because of the base or member in question, and that
3535 gives a better diagnostic. */
3536 = type_maybe_constexpr_default_constructor (t);
3539 /* Create default constructors, assignment operators, and so forth for
3540 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3541 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3542 the class cannot have a default constructor, copy constructor
3543 taking a const reference argument, or an assignment operator taking
3544 a const reference, respectively. */
3546 static void
3547 add_implicitly_declared_members (tree t, tree* access_decls,
3548 int cant_have_const_cctor,
3549 int cant_have_const_assignment)
3551 /* Destructor. */
3552 if (!CLASSTYPE_DESTRUCTOR (t))
3553 /* In general, we create destructors lazily. */
3554 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3556 bool move_ok = false;
3557 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3558 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3559 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3560 move_ok = true;
3562 /* [class.ctor]
3564 If there is no user-declared constructor for a class, a default
3565 constructor is implicitly declared. */
3566 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3567 add_implicit_default_ctor (t);
3569 /* [class.ctor]
3571 If a class definition does not explicitly declare a copy
3572 constructor, one is declared implicitly. */
3573 if (! TYPE_HAS_COPY_CTOR (t))
3575 TYPE_HAS_COPY_CTOR (t) = 1;
3576 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3577 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3578 if (move_ok)
3579 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3582 /* If there is no assignment operator, one will be created if and
3583 when it is needed. For now, just record whether or not the type
3584 of the parameter to the assignment operator will be a const or
3585 non-const reference. */
3586 if (!TYPE_HAS_COPY_ASSIGN (t))
3588 TYPE_HAS_COPY_ASSIGN (t) = 1;
3589 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3590 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3591 if (move_ok && !LAMBDA_TYPE_P (t))
3592 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3595 /* We can't be lazy about declaring functions that might override
3596 a virtual function from a base class. */
3597 declare_virt_assop_and_dtor (t);
3599 /* If the class definition does not explicitly declare an == operator
3600 function, but declares a defaulted three-way comparison operator function,
3601 an == operator function is declared implicitly. */
3602 if (!classtype_has_op (t, EQ_EXPR))
3603 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3605 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3606 NULL_TREE);
3607 bool is_friend = DECL_CONTEXT (space) != t;
3608 if (is_friend)
3609 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3610 NO_SPECIAL, true);
3611 else
3613 add_method (t, eq, false);
3614 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3615 TYPE_FIELDS (t) = eq;
3617 maybe_add_class_template_decl_list (t, eq, is_friend);
3620 while (*access_decls)
3622 tree using_decl = TREE_VALUE (*access_decls);
3623 tree decl = USING_DECL_DECLS (using_decl);
3624 if (DECL_NAME (using_decl) == ctor_identifier)
3626 /* declare, then remove the decl */
3627 tree ctor_list = decl;
3628 location_t loc = input_location;
3629 input_location = DECL_SOURCE_LOCATION (using_decl);
3630 for (tree fn : ovl_range (ctor_list))
3632 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3633 /* CWG2799: Inheriting a default constructor gives us a default
3634 constructor, not just an inherited constructor. */
3635 add_implicit_default_ctor (t);
3636 one_inherited_ctor (fn, t, using_decl);
3638 *access_decls = TREE_CHAIN (*access_decls);
3639 input_location = loc;
3641 else
3642 access_decls = &TREE_CHAIN (*access_decls);
3646 /* Cache of enum_min_precision values. */
3647 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3649 /* Return the minimum precision of a bit-field needed to store all
3650 enumerators of ENUMERAL_TYPE TYPE. */
3652 static int
3653 enum_min_precision (tree type)
3655 type = TYPE_MAIN_VARIANT (type);
3656 /* For unscoped enums without fixed underlying type and without mode
3657 attribute we can just use precision of the underlying type. */
3658 if (UNSCOPED_ENUM_P (type)
3659 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3660 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3661 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3663 if (enum_to_min_precision == NULL)
3664 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3666 bool existed;
3667 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3668 if (existed)
3669 return prec;
3671 tree minnode, maxnode;
3672 if (TYPE_VALUES (type))
3674 minnode = maxnode = NULL_TREE;
3675 for (tree values = TYPE_VALUES (type);
3676 values; values = TREE_CHAIN (values))
3678 tree decl = TREE_VALUE (values);
3679 tree value = DECL_INITIAL (decl);
3680 if (value == error_mark_node)
3681 value = integer_zero_node;
3682 if (!minnode)
3683 minnode = maxnode = value;
3684 else if (tree_int_cst_lt (maxnode, value))
3685 maxnode = value;
3686 else if (tree_int_cst_lt (value, minnode))
3687 minnode = value;
3690 else
3691 minnode = maxnode = integer_zero_node;
3693 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3694 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3695 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3696 prec = MAX (lowprec, highprec);
3697 return prec;
3700 /* FIELD is a bit-field. We are finishing the processing for its
3701 enclosing type. Issue any appropriate messages and set appropriate
3702 flags. Returns false if an error has been diagnosed. */
3704 static bool
3705 check_bitfield_decl (tree field)
3707 tree type = TREE_TYPE (field);
3708 tree w;
3710 /* Extract the declared width of the bitfield, which has been
3711 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3712 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3713 gcc_assert (w != NULL_TREE);
3714 /* Remove the bit-field width indicator so that the rest of the
3715 compiler does not treat that value as a qualifier. */
3716 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3718 /* Detect invalid bit-field type. */
3719 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3721 error_at (DECL_SOURCE_LOCATION (field),
3722 "bit-field %q#D with non-integral type %qT", field, type);
3723 w = error_mark_node;
3725 else
3727 location_t loc = input_location;
3728 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3729 STRIP_NOPS (w);
3731 /* detect invalid field size. */
3732 input_location = DECL_SOURCE_LOCATION (field);
3733 w = cxx_constant_value (w);
3734 input_location = loc;
3736 if (TREE_CODE (w) != INTEGER_CST)
3738 error ("bit-field %q+D width not an integer constant", field);
3739 w = error_mark_node;
3741 else if (tree_int_cst_sgn (w) < 0)
3743 error ("negative width in bit-field %q+D", field);
3744 w = error_mark_node;
3746 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3748 error ("zero width for bit-field %q+D", field);
3749 w = error_mark_node;
3751 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3752 && TREE_CODE (type) != BOOLEAN_TYPE
3753 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3754 || ((TREE_CODE (type) == ENUMERAL_TYPE
3755 || TREE_CODE (type) == BOOLEAN_TYPE)
3756 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3757 warning_at (DECL_SOURCE_LOCATION (field), 0,
3758 "width of %qD exceeds its type", field);
3759 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3761 int prec = enum_min_precision (type);
3762 if (compare_tree_int (w, prec) < 0)
3763 warning_at (DECL_SOURCE_LOCATION (field), 0,
3764 "%qD is too small to hold all values of %q#T",
3765 field, type);
3769 if (w != error_mark_node)
3771 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3772 DECL_BIT_FIELD (field) = 1;
3773 return true;
3775 else
3777 /* Non-bit-fields are aligned for their type. */
3778 DECL_BIT_FIELD (field) = 0;
3779 CLEAR_DECL_C_BIT_FIELD (field);
3780 return false;
3784 /* FIELD is a non bit-field. We are finishing the processing for its
3785 enclosing type T. Issue any appropriate messages and set appropriate
3786 flags. */
3788 static bool
3789 check_field_decl (tree field,
3790 tree t,
3791 int* cant_have_const_ctor,
3792 int* no_const_asn_ref)
3794 tree type = strip_array_types (TREE_TYPE (field));
3795 bool any_default_members = false;
3797 /* In C++98 an anonymous union cannot contain any fields which would change
3798 the settings of CANT_HAVE_CONST_CTOR and friends. */
3799 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3801 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3802 structs. So, we recurse through their fields here. */
3803 else if (ANON_AGGR_TYPE_P (type))
3805 for (tree fields = TYPE_FIELDS (type); fields;
3806 fields = DECL_CHAIN (fields))
3807 if (TREE_CODE (fields) == FIELD_DECL)
3808 any_default_members |= check_field_decl (fields, t,
3809 cant_have_const_ctor,
3810 no_const_asn_ref);
3812 /* Check members with class type for constructors, destructors,
3813 etc. */
3814 else if (CLASS_TYPE_P (type))
3816 /* Never let anything with uninheritable virtuals
3817 make it through without complaint. */
3818 abstract_virtuals_error (field, type);
3820 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3822 static bool warned;
3823 auto_diagnostic_group d;
3824 int oldcount = errorcount;
3825 if (TYPE_NEEDS_CONSTRUCTING (type))
3826 error ("member %q+#D with constructor not allowed in union",
3827 field);
3828 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3829 error ("member %q+#D with destructor not allowed in union", field);
3830 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3831 error ("member %q+#D with copy assignment operator not allowed in union",
3832 field);
3833 if (!warned && errorcount > oldcount)
3835 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3836 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3837 warned = true;
3840 else
3842 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3843 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3844 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3845 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3846 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3847 || !TYPE_HAS_COPY_ASSIGN (type));
3848 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3849 || !TYPE_HAS_COPY_CTOR (type));
3850 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3851 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3852 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3853 || TYPE_HAS_COMPLEX_DFLT (type));
3856 if (TYPE_HAS_COPY_CTOR (type)
3857 && !TYPE_HAS_CONST_COPY_CTOR (type))
3858 *cant_have_const_ctor = 1;
3860 if (TYPE_HAS_COPY_ASSIGN (type)
3861 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3862 *no_const_asn_ref = 1;
3865 check_abi_tags (t, field);
3867 if (DECL_INITIAL (field) != NULL_TREE)
3868 /* `build_class_init_list' does not recognize
3869 non-FIELD_DECLs. */
3870 any_default_members = true;
3872 return any_default_members;
3875 /* Check the data members (both static and non-static), class-scoped
3876 typedefs, etc., appearing in the declaration of T. Issue
3877 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3878 declaration order) of access declarations; each TREE_VALUE in this
3879 list is a USING_DECL.
3881 In addition, set the following flags:
3883 EMPTY_P
3884 The class is empty, i.e., contains no non-static data members.
3886 CANT_HAVE_CONST_CTOR_P
3887 This class cannot have an implicitly generated copy constructor
3888 taking a const reference.
3890 CANT_HAVE_CONST_ASN_REF
3891 This class cannot have an implicitly generated assignment
3892 operator taking a const reference.
3894 All of these flags should be initialized before calling this
3895 function. */
3897 static void
3898 check_field_decls (tree t, tree *access_decls,
3899 int *cant_have_const_ctor_p,
3900 int *no_const_asn_ref_p)
3902 int cant_pack = 0;
3904 /* Assume there are no access declarations. */
3905 *access_decls = NULL_TREE;
3906 /* Effective C has things to say about classes with pointer members. */
3907 tree pointer_member = NULL_TREE;
3908 /* Default initialized members affect the whole class. */
3909 tree default_init_member = NULL_TREE;
3910 /* Lack of any non-static data member of non-volatile literal
3911 type affects a union. */
3912 bool found_nv_literal_p = false;
3913 /* Standard layout requires all FIELDS have same access. */
3914 int field_access = -1;
3916 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3918 tree type = TREE_TYPE (field);
3920 switch (TREE_CODE (field))
3922 default:
3923 gcc_unreachable ();
3925 case USING_DECL:
3926 /* Save the access declarations for our caller. */
3927 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3928 break;
3930 case TYPE_DECL:
3931 case TEMPLATE_DECL:
3932 break;
3934 case FUNCTION_DECL:
3935 /* FIXME: We should fold in the checking from check_methods. */
3936 break;
3938 case CONST_DECL:
3939 DECL_NONLOCAL (field) = 1;
3940 break;
3942 case VAR_DECL:
3943 if (TREE_CODE (t) == UNION_TYPE
3944 && cxx_dialect < cxx11)
3946 /* [class.union]
3948 (C++98) If a union contains a static data member,
3949 ... the program is ill-formed. */
3950 if (cxx_dialect < cxx11)
3951 error ("in C++98 %q+D may not be static because it is "
3952 "a member of a union", field);
3954 goto data_member;
3956 case FIELD_DECL:
3957 if (TREE_CODE (t) == UNION_TYPE)
3959 /* [class.union]
3961 If a union contains ... or a [non-static data] member
3962 of reference type, the program is ill-formed. */
3963 if (TYPE_REF_P (type))
3964 error ("non-static data member %q+D in a union may not "
3965 "have reference type %qT", field, type);
3968 data_member:
3969 /* Common VAR_DECL & FIELD_DECL processing. */
3970 DECL_CONTEXT (field) = t;
3971 DECL_NONLOCAL (field) = 1;
3973 /* Template instantiation can cause this. Perhaps this
3974 should be a specific instantiation check? */
3975 if (TREE_CODE (type) == FUNCTION_TYPE)
3977 error ("data member %q+D invalidly declared function type", field);
3978 type = build_pointer_type (type);
3979 TREE_TYPE (field) = type;
3981 else if (TREE_CODE (type) == METHOD_TYPE)
3983 error ("data member %q+D invalidly declared method type", field);
3984 type = build_pointer_type (type);
3985 TREE_TYPE (field) = type;
3988 break;
3991 if (TREE_CODE (field) != FIELD_DECL)
3992 continue;
3994 if (type == error_mark_node)
3995 continue;
3997 /* If it is not a union and at least one non-static data member is
3998 non-literal, the whole class becomes non-literal. Per Core/1453,
3999 volatile non-static data members and base classes are also not allowed.
4000 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
4001 members.
4002 Note: if the type is incomplete we will complain later on. */
4003 if (COMPLETE_TYPE_P (type))
4005 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
4006 CLASSTYPE_LITERAL_P (t) = false;
4007 else
4008 found_nv_literal_p = true;
4011 int this_field_access = (TREE_PROTECTED (field) ? 1
4012 : TREE_PRIVATE (field) ? 2 : 0);
4013 if (field_access != this_field_access)
4015 /* A standard-layout class is a class that:
4017 ... has the same access control (Clause 11) for all
4018 non-static data members, */
4019 if (field_access < 0)
4020 field_access = this_field_access;
4021 else
4022 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4024 /* Aggregates must be public. */
4025 if (this_field_access)
4026 CLASSTYPE_NON_AGGREGATE (t) = 1;
4029 /* If this is of reference type, check if it needs an init. */
4030 if (TYPE_REF_P (type))
4032 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4033 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4034 if (DECL_INITIAL (field) == NULL_TREE)
4035 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4036 if (cxx_dialect < cxx11)
4038 /* ARM $12.6.2: [A member initializer list] (or, for an
4039 aggregate, initialization by a brace-enclosed list) is the
4040 only way to initialize non-static const and reference
4041 members. */
4042 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4043 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4047 type = strip_array_types (type);
4049 if (TYPE_PACKED (t))
4051 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4053 warning_at (DECL_SOURCE_LOCATION (field), 0,
4054 "ignoring packed attribute because of"
4055 " unpacked non-POD field %q#D", field);
4056 cant_pack = 1;
4058 else if (DECL_C_BIT_FIELD (field)
4059 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
4060 DECL_PACKED (field) = 1;
4063 if (DECL_C_BIT_FIELD (field)
4064 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
4065 /* We don't treat zero-width bitfields as making a class
4066 non-empty. */
4068 else if (field_poverlapping_p (field)
4069 && is_empty_class (TREE_TYPE (field)))
4070 /* Empty data members also don't make a class non-empty. */
4071 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4072 else
4074 /* The class is non-empty. */
4075 CLASSTYPE_EMPTY_P (t) = 0;
4076 /* The class is not even nearly empty. */
4077 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4078 /* If one of the data members contains an empty class, so
4079 does T. */
4080 if (CLASS_TYPE_P (type)
4081 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4082 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4085 /* This is used by -Weffc++ (see below). Warn only for pointers
4086 to members which might hold dynamic memory. So do not warn
4087 for pointers to functions or pointers to members. */
4088 if (TYPE_PTR_P (type)
4089 && !TYPE_PTRFN_P (type))
4090 pointer_member = field;
4092 if (CLASS_TYPE_P (type))
4094 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
4095 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4096 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
4097 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4100 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
4101 CLASSTYPE_HAS_MUTABLE (t) = 1;
4103 if (DECL_MUTABLE_P (field))
4105 if (TYPE_REF_P (type))
4106 error ("member %q+D cannot be declared as a %<mutable%> "
4107 "reference", field);
4108 else if (CP_TYPE_CONST_P (type))
4109 error ("member %q+D cannot be declared both %<const%> "
4110 "and %<mutable%>", field);
4113 if (! layout_pod_type_p (type))
4114 /* DR 148 now allows pointers to members (which are POD themselves),
4115 to be allowed in POD structs. */
4116 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4118 if (field_poverlapping_p (field))
4119 /* A potentially-overlapping non-static data member makes the class
4120 non-layout-POD. */
4121 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4123 if (!std_layout_type_p (type))
4124 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4126 if (! zero_init_p (type))
4127 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
4129 /* We set DECL_C_BIT_FIELD in grokbitfield.
4130 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4131 if (DECL_C_BIT_FIELD (field))
4132 check_bitfield_decl (field);
4134 if (check_field_decl (field, t,
4135 cant_have_const_ctor_p, no_const_asn_ref_p))
4137 if (default_init_member
4138 && TREE_CODE (t) == UNION_TYPE)
4140 auto_diagnostic_group d;
4141 error ("multiple fields in union %qT initialized", t);
4142 inform (DECL_SOURCE_LOCATION (default_init_member),
4143 "initialized member %q+D declared here",
4144 default_init_member);
4146 default_init_member = field;
4149 /* Now that we've removed bit-field widths from DECL_INITIAL,
4150 anything left in DECL_INITIAL is an NSDMI that makes the class
4151 non-aggregate in C++11, and non-layout-POD always. */
4152 if (DECL_INITIAL (field))
4154 if (cxx_dialect < cxx14)
4155 CLASSTYPE_NON_AGGREGATE (t) = true;
4156 else
4157 CLASSTYPE_NON_POD_AGGREGATE (t) = true;
4160 if (CP_TYPE_CONST_P (type))
4162 /* If any field is const, the structure type is pseudo-const. */
4163 C_TYPE_FIELDS_READONLY (t) = 1;
4164 if (DECL_INITIAL (field) == NULL_TREE)
4165 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4166 if (cxx_dialect < cxx11)
4168 /* ARM $12.6.2: [A member initializer list] (or, for an
4169 aggregate, initialization by a brace-enclosed list) is the
4170 only way to initialize non-static const and reference
4171 members. */
4172 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4173 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4176 /* A field that is pseudo-const makes the structure likewise. */
4177 else if (CLASS_TYPE_P (type))
4179 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4180 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
4181 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4182 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
4185 /* Core issue 80: A non-static data member is required to have a
4186 different name from the class iff the class has a
4187 user-declared constructor. */
4188 if (constructor_name_p (DECL_NAME (field), t)
4189 && TYPE_HAS_USER_CONSTRUCTOR (t))
4190 permerror (DECL_SOURCE_LOCATION (field),
4191 "field %q#D with same name as class", field);
4194 /* Per CWG 2096, a type is a literal type if it is a union, and at least
4195 one of its non-static data members is of non-volatile literal type. */
4196 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
4197 CLASSTYPE_LITERAL_P (t) = true;
4199 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
4200 it should also define a copy constructor and an assignment operator to
4201 implement the correct copy semantic (deep vs shallow, etc.). As it is
4202 not feasible to check whether the constructors do allocate dynamic memory
4203 and store it within members, we approximate the warning like this:
4205 -- Warn only if there are members which are pointers
4206 -- Warn only if there is a non-trivial constructor (otherwise,
4207 there cannot be memory allocated).
4208 -- Warn only if there is a non-trivial destructor. We assume that the
4209 user at least implemented the cleanup correctly, and a destructor
4210 is needed to free dynamic memory.
4212 This seems enough for practical purposes. */
4213 if (warn_ecpp
4214 && pointer_member
4215 && TYPE_HAS_USER_CONSTRUCTOR (t)
4216 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4217 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
4219 auto_diagnostic_group d;
4220 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
4222 if (! TYPE_HAS_COPY_CTOR (t))
4224 warning (OPT_Weffc__,
4225 " but does not declare %<%T(const %T&)%>", t, t);
4226 if (!TYPE_HAS_COPY_ASSIGN (t))
4227 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4229 else if (! TYPE_HAS_COPY_ASSIGN (t))
4230 warning (OPT_Weffc__,
4231 " but does not declare %<operator=(const %T&)%>", t);
4232 inform (DECL_SOURCE_LOCATION (pointer_member),
4233 "pointer member %q+D declared here", pointer_member);
4237 /* Non-static data member initializers make the default constructor
4238 non-trivial. */
4239 if (default_init_member)
4241 TYPE_NEEDS_CONSTRUCTING (t) = true;
4242 TYPE_HAS_COMPLEX_DFLT (t) = true;
4245 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4246 if (cant_pack)
4247 TYPE_PACKED (t) = 0;
4249 /* Check anonymous struct/anonymous union fields. */
4250 finish_struct_anon (t);
4252 /* We've built up the list of access declarations in reverse order.
4253 Fix that now. */
4254 *access_decls = nreverse (*access_decls);
4257 /* If TYPE is an empty class type, records its OFFSET in the table of
4258 OFFSETS. */
4260 static int
4261 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4263 splay_tree_node n;
4265 if (!is_empty_class (type))
4266 return 0;
4268 /* Record the location of this empty object in OFFSETS. */
4269 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4270 if (!n)
4271 n = splay_tree_insert (offsets,
4272 (splay_tree_key) offset,
4273 (splay_tree_value) NULL_TREE);
4274 n->value = ((splay_tree_value)
4275 tree_cons (NULL_TREE,
4276 type,
4277 (tree) n->value));
4279 return 0;
4282 /* Returns nonzero if TYPE is an empty class type and there is
4283 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4285 static int
4286 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4288 splay_tree_node n;
4289 tree t;
4291 if (!is_empty_class (type))
4292 return 0;
4294 /* Record the location of this empty object in OFFSETS. */
4295 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4296 if (!n)
4297 return 0;
4299 enum { ignore, fast, slow, warn }
4300 cv_check = (abi_version_crosses (19) ? slow
4301 : abi_version_at_least (19) ? fast
4302 : ignore);
4303 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4305 tree elt = TREE_VALUE (t);
4307 if (same_type_p (elt, type))
4308 return 1;
4310 if (cv_check != ignore
4311 && similar_type_p (elt, type))
4313 if (cv_check == fast)
4314 return 1;
4315 cv_check = warn;
4319 if (cv_check == warn)
4321 warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4322 "[[no_unique_address]]", type, "-fabi-version=19");
4323 if (abi_version_at_least (19))
4324 return 1;
4327 return 0;
4330 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4331 F for every subobject, passing it the type, offset, and table of
4332 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4333 be traversed.
4335 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4336 than MAX_OFFSET will not be walked.
4338 If F returns a nonzero value, the traversal ceases, and that value
4339 is returned. Otherwise, returns zero. */
4341 static int
4342 walk_subobject_offsets (tree type,
4343 subobject_offset_fn f,
4344 tree offset,
4345 splay_tree offsets,
4346 tree max_offset,
4347 int vbases_p)
4349 int r = 0;
4350 tree type_binfo = NULL_TREE;
4352 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4353 stop. */
4354 if (max_offset && tree_int_cst_lt (max_offset, offset))
4355 return 0;
4357 if (type == error_mark_node)
4358 return 0;
4360 if (!TYPE_P (type))
4362 type_binfo = type;
4363 type = BINFO_TYPE (type);
4366 if (CLASS_TYPE_P (type))
4368 tree field;
4369 tree binfo;
4370 int i;
4372 /* Avoid recursing into objects that are not interesting. */
4373 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4374 return 0;
4376 /* Record the location of TYPE. */
4377 r = (*f) (type, offset, offsets);
4378 if (r)
4379 return r;
4381 /* Iterate through the direct base classes of TYPE. */
4382 if (!type_binfo)
4383 type_binfo = TYPE_BINFO (type);
4384 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4386 tree binfo_offset;
4388 if (BINFO_VIRTUAL_P (binfo))
4389 continue;
4391 tree orig_binfo;
4392 /* We cannot rely on BINFO_OFFSET being set for the base
4393 class yet, but the offsets for direct non-virtual
4394 bases can be calculated by going back to the TYPE. */
4395 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4396 binfo_offset = size_binop (PLUS_EXPR,
4397 offset,
4398 BINFO_OFFSET (orig_binfo));
4400 r = walk_subobject_offsets (binfo,
4402 binfo_offset,
4403 offsets,
4404 max_offset,
4405 /*vbases_p=*/0);
4406 if (r)
4407 return r;
4410 if (CLASSTYPE_VBASECLASSES (type))
4412 unsigned ix;
4413 vec<tree, va_gc> *vbases;
4415 /* Iterate through the virtual base classes of TYPE. In G++
4416 3.2, we included virtual bases in the direct base class
4417 loop above, which results in incorrect results; the
4418 correct offsets for virtual bases are only known when
4419 working with the most derived type. */
4420 if (vbases_p)
4421 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4422 vec_safe_iterate (vbases, ix, &binfo); ix++)
4424 r = walk_subobject_offsets (binfo,
4426 size_binop (PLUS_EXPR,
4427 offset,
4428 BINFO_OFFSET (binfo)),
4429 offsets,
4430 max_offset,
4431 /*vbases_p=*/0);
4432 if (r)
4433 return r;
4435 else
4437 /* We still have to walk the primary base, if it is
4438 virtual. (If it is non-virtual, then it was walked
4439 above.) */
4440 tree vbase = get_primary_binfo (type_binfo);
4442 if (vbase && BINFO_VIRTUAL_P (vbase)
4443 && BINFO_PRIMARY_P (vbase)
4444 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4446 r = (walk_subobject_offsets
4447 (vbase, f, offset,
4448 offsets, max_offset, /*vbases_p=*/0));
4449 if (r)
4450 return r;
4455 /* Iterate through the fields of TYPE. */
4456 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4457 if (TREE_CODE (field) == FIELD_DECL
4458 && TREE_TYPE (field) != error_mark_node
4459 && !DECL_ARTIFICIAL (field))
4461 tree field_offset;
4463 field_offset = byte_position (field);
4465 r = walk_subobject_offsets (TREE_TYPE (field),
4467 size_binop (PLUS_EXPR,
4468 offset,
4469 field_offset),
4470 offsets,
4471 max_offset,
4472 /*vbases_p=*/1);
4473 if (r)
4474 return r;
4477 else if (TREE_CODE (type) == ARRAY_TYPE)
4479 tree element_type = strip_array_types (type);
4480 tree domain = TYPE_DOMAIN (type);
4481 tree index;
4483 /* Avoid recursing into objects that are not interesting. */
4484 if (!CLASS_TYPE_P (element_type)
4485 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4486 || !domain
4487 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4488 return 0;
4490 /* Step through each of the elements in the array. */
4491 for (index = size_zero_node;
4492 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4493 index = size_binop (PLUS_EXPR, index, size_one_node))
4495 r = walk_subobject_offsets (TREE_TYPE (type),
4497 offset,
4498 offsets,
4499 max_offset,
4500 /*vbases_p=*/1);
4501 if (r)
4502 return r;
4503 offset = size_binop (PLUS_EXPR, offset,
4504 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4505 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4506 there's no point in iterating through the remaining
4507 elements of the array. */
4508 if (max_offset && tree_int_cst_lt (max_offset, offset))
4509 break;
4513 return 0;
4516 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4518 static bool
4519 field_poverlapping_p (tree decl)
4521 /* Base fields are actually potentially overlapping, but C++ bases go through
4522 a different code path based on binfos, and ObjC++ base fields are laid out
4523 in objc-act, so we don't want layout_class_type to mess with them. */
4524 if (DECL_FIELD_IS_BASE (decl))
4526 gcc_checking_assert (c_dialect_objc ());
4527 return false;
4530 return lookup_attribute ("no_unique_address",
4531 DECL_ATTRIBUTES (decl));
4534 /* Return true iff DECL is an empty field, either for an empty base or a
4535 [[no_unique_address]] data member. */
4537 bool
4538 is_empty_field (tree decl)
4540 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4541 return false;
4543 bool r = (is_empty_class (TREE_TYPE (decl))
4544 && (DECL_FIELD_IS_BASE (decl)
4545 || field_poverlapping_p (decl)));
4547 /* Empty fields should have size zero. */
4548 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4550 return r;
4553 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4555 static void
4556 record_subobject_offsets (tree decl_or_binfo,
4557 splay_tree offsets)
4559 tree type, offset;
4560 bool overlapping, vbases_p;
4562 if (DECL_P (decl_or_binfo))
4564 tree decl = decl_or_binfo;
4565 type = TREE_TYPE (decl);
4566 offset = byte_position (decl);
4567 overlapping = field_poverlapping_p (decl);
4568 vbases_p = true;
4570 else
4572 type = BINFO_TYPE (decl_or_binfo);
4573 offset = BINFO_OFFSET (decl_or_binfo);
4574 overlapping = true;
4575 vbases_p = false;
4578 tree max_offset;
4579 /* If recording subobjects for a non-static data member or a
4580 non-empty base class, we do not need to record offsets beyond
4581 the size of the biggest empty class. Additional data members
4582 will go at the end of the class. Additional base classes will go
4583 either at offset zero (if empty, in which case they cannot
4584 overlap with offsets past the size of the biggest empty class) or
4585 at the end of the class.
4587 However, if we are placing an empty base class, then we must record
4588 all offsets, as either the empty class is at offset zero (where
4589 other empty classes might later be placed) or at the end of the
4590 class (where other objects might then be placed, so other empty
4591 subobjects might later overlap). */
4592 if (!overlapping
4593 || !is_empty_class (type))
4594 max_offset = sizeof_biggest_empty_class;
4595 else
4596 max_offset = NULL_TREE;
4597 walk_subobject_offsets (type, record_subobject_offset, offset,
4598 offsets, max_offset, vbases_p);
4601 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4602 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4603 virtual bases of TYPE are examined. */
4605 static int
4606 layout_conflict_p (tree type,
4607 tree offset,
4608 splay_tree offsets,
4609 int vbases_p)
4611 splay_tree_node max_node;
4613 /* Get the node in OFFSETS that indicates the maximum offset where
4614 an empty subobject is located. */
4615 max_node = splay_tree_max (offsets);
4616 /* If there aren't any empty subobjects, then there's no point in
4617 performing this check. */
4618 if (!max_node)
4619 return 0;
4621 return walk_subobject_offsets (type, check_subobject_offset, offset,
4622 offsets, (tree) (max_node->key),
4623 vbases_p);
4626 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4627 non-static data member of the type indicated by RLI. BINFO is the
4628 binfo corresponding to the base subobject, OFFSETS maps offsets to
4629 types already located at those offsets. This function determines
4630 the position of the DECL. */
4632 static void
4633 layout_nonempty_base_or_field (record_layout_info rli,
4634 tree decl,
4635 tree binfo,
4636 splay_tree offsets)
4638 tree offset = NULL_TREE;
4639 bool field_p;
4640 tree type;
4642 if (binfo)
4644 /* For the purposes of determining layout conflicts, we want to
4645 use the class type of BINFO; TREE_TYPE (DECL) will be the
4646 CLASSTYPE_AS_BASE version, which does not contain entries for
4647 zero-sized bases. */
4648 type = TREE_TYPE (binfo);
4649 field_p = false;
4651 else
4653 type = TREE_TYPE (decl);
4654 field_p = true;
4657 /* Try to place the field. It may take more than one try if we have
4658 a hard time placing the field without putting two objects of the
4659 same type at the same address. */
4660 while (1)
4662 struct record_layout_info_s old_rli = *rli;
4664 /* Place this field. */
4665 place_field (rli, decl);
4666 offset = byte_position (decl);
4668 /* We have to check to see whether or not there is already
4669 something of the same type at the offset we're about to use.
4670 For example, consider:
4672 struct S {};
4673 struct T : public S { int i; };
4674 struct U : public S, public T {};
4676 Here, we put S at offset zero in U. Then, we can't put T at
4677 offset zero -- its S component would be at the same address
4678 as the S we already allocated. So, we have to skip ahead.
4679 Since all data members, including those whose type is an
4680 empty class, have nonzero size, any overlap can happen only
4681 with a direct or indirect base-class -- it can't happen with
4682 a data member. */
4683 /* In a union, overlap is permitted; all members are placed at
4684 offset zero. */
4685 if (TREE_CODE (rli->t) == UNION_TYPE)
4686 break;
4687 if (layout_conflict_p (field_p ? type : binfo, offset,
4688 offsets, field_p))
4690 /* Strip off the size allocated to this field. That puts us
4691 at the first place we could have put the field with
4692 proper alignment. */
4693 *rli = old_rli;
4695 /* Bump up by the alignment required for the type. */
4696 rli->bitpos
4697 = size_binop (PLUS_EXPR, rli->bitpos,
4698 bitsize_int (binfo
4699 ? CLASSTYPE_ALIGN (type)
4700 : TYPE_ALIGN (type)));
4701 normalize_rli (rli);
4703 else if (TREE_CODE (type) == NULLPTR_TYPE
4704 && warn_abi && abi_version_crosses (9))
4706 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4707 the offset wasn't aligned like a pointer when we started to
4708 layout this field, that affects its position. */
4709 tree pos = rli_size_unit_so_far (&old_rli);
4710 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4712 if (abi_version_at_least (9))
4713 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4714 "alignment of %qD increased in %<-fabi-version=9%> "
4715 "(GCC 5.2)", decl);
4716 else
4717 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4718 "of %qD will increase in %<-fabi-version=9%>",
4719 decl);
4721 break;
4723 else
4724 /* There was no conflict. We're done laying out this field. */
4725 break;
4728 /* Now that we know where it will be placed, update its
4729 BINFO_OFFSET. */
4730 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4731 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4732 this point because their BINFO_OFFSET is copied from another
4733 hierarchy. Therefore, we may not need to add the entire
4734 OFFSET. */
4735 propagate_binfo_offsets (binfo,
4736 size_diffop_loc (input_location,
4737 fold_convert (ssizetype, offset),
4738 fold_convert (ssizetype,
4739 BINFO_OFFSET (binfo))));
4742 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4744 static int
4745 empty_base_at_nonzero_offset_p (tree type,
4746 tree offset,
4747 splay_tree /*offsets*/)
4749 return is_empty_class (type) && !integer_zerop (offset);
4752 /* Layout the empty base BINFO. EOC indicates the byte currently just
4753 past the end of the class, and should be correctly aligned for a
4754 class of the type indicated by BINFO; OFFSETS gives the offsets of
4755 the empty bases allocated so far. T is the most derived
4756 type. Return nonzero iff we added it at the end. */
4758 static bool
4759 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4760 splay_tree offsets)
4762 tree alignment;
4763 bool atend = false;
4764 tree binfo = NULL_TREE;
4765 tree decl = NULL_TREE;
4766 tree type;
4767 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4769 binfo = binfo_or_decl;
4770 type = BINFO_TYPE (binfo);
4772 else
4774 decl = binfo_or_decl;
4775 type = TREE_TYPE (decl);
4778 /* On some platforms (ARM), even empty classes will not be
4779 byte-aligned. */
4780 tree eoc = round_up_loc (input_location,
4781 rli_size_unit_so_far (rli),
4782 CLASSTYPE_ALIGN_UNIT (type));
4784 /* This routine should only be used for empty classes. */
4785 gcc_assert (is_empty_class (type));
4787 if (decl && DECL_USER_ALIGN (decl))
4788 alignment = size_int (DECL_ALIGN_UNIT (decl));
4789 else
4790 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4792 /* This is an empty base class. We first try to put it at offset
4793 zero. */
4794 tree offset = size_zero_node;
4795 if (TREE_CODE (rli->t) != UNION_TYPE
4796 && layout_conflict_p (type,
4797 offset,
4798 offsets,
4799 /*vbases_p=*/0))
4801 /* That didn't work. Now, we move forward from the next
4802 available spot in the class. */
4803 atend = true;
4804 offset = eoc;
4805 while (1)
4807 if (!layout_conflict_p (type,
4808 offset,
4809 offsets,
4810 /*vbases_p=*/0))
4811 /* We finally found a spot where there's no overlap. */
4812 break;
4814 /* There's overlap here, too. Bump along to the next spot. */
4815 offset = size_binop (PLUS_EXPR, offset, alignment);
4819 if (decl && DECL_USER_ALIGN (decl))
4821 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4822 if (warn_packed)
4823 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4824 TYPE_USER_ALIGN (rli->t) = 1;
4826 else if (CLASSTYPE_USER_ALIGN (type))
4828 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4829 if (warn_packed)
4830 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4831 TYPE_USER_ALIGN (rli->t) = 1;
4834 if (binfo)
4835 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4836 propagate_binfo_offsets (binfo,
4837 size_diffop (offset, BINFO_OFFSET (binfo)));
4838 else
4840 DECL_FIELD_OFFSET (decl) = offset;
4841 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4842 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4845 return atend;
4848 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4849 fields at NEXT_FIELD, and return it. */
4851 static tree
4852 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4854 /* Create the FIELD_DECL. */
4855 tree basetype = BINFO_TYPE (binfo);
4856 tree as_base = CLASSTYPE_AS_BASE (basetype);
4857 gcc_assert (as_base);
4858 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4860 DECL_ARTIFICIAL (decl) = 1;
4861 DECL_IGNORED_P (decl) = 1;
4862 DECL_FIELD_CONTEXT (decl) = t;
4863 if (is_empty_class (basetype))
4864 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4865 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4866 else
4868 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4869 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4871 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4872 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4873 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4874 DECL_FIELD_IS_BASE (decl) = 1;
4876 if (access == access_private_node)
4877 TREE_PRIVATE (decl) = true;
4878 else if (access == access_protected_node)
4879 TREE_PROTECTED (decl) = true;
4881 /* Add the new FIELD_DECL to the list of fields for T. */
4882 DECL_CHAIN (decl) = *next_field;
4883 *next_field = decl;
4884 next_field = &DECL_CHAIN (decl);
4886 return decl;
4889 /* Layout the base given by BINFO in the class indicated by RLI.
4890 *BASE_ALIGN is a running maximum of the alignments of
4891 any base class. OFFSETS gives the location of empty base
4892 subobjects. T is the most derived type. Return nonzero if the new
4893 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4894 *NEXT_FIELD, unless BINFO is for an empty base class.
4896 Returns the location at which the next field should be inserted. */
4898 static tree *
4899 build_base_field (record_layout_info rli, tree binfo, tree access,
4900 splay_tree offsets, tree *next_field)
4902 tree t = rli->t;
4903 tree basetype = BINFO_TYPE (binfo);
4905 if (!COMPLETE_TYPE_P (basetype))
4906 /* This error is now reported in xref_tag, thus giving better
4907 location information. */
4908 return next_field;
4910 /* Place the base class. */
4911 if (!is_empty_class (basetype))
4913 tree decl;
4915 /* The containing class is non-empty because it has a non-empty
4916 base class. */
4917 CLASSTYPE_EMPTY_P (t) = 0;
4919 /* Create the FIELD_DECL. */
4920 decl = build_base_field_1 (t, binfo, access, next_field);
4922 /* Try to place the field. It may take more than one try if we
4923 have a hard time placing the field without putting two
4924 objects of the same type at the same address. */
4925 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4927 else
4929 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4930 /* A nearly-empty class "has no proper base class that is empty,
4931 not morally virtual, and at an offset other than zero." */
4932 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4934 if (atend)
4935 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4936 /* The check above (used in G++ 3.2) is insufficient because
4937 an empty class placed at offset zero might itself have an
4938 empty base at a nonzero offset. */
4939 else if (walk_subobject_offsets (basetype,
4940 empty_base_at_nonzero_offset_p,
4941 size_zero_node,
4942 /*offsets=*/NULL,
4943 /*max_offset=*/NULL_TREE,
4944 /*vbases_p=*/true))
4945 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4948 /* We used to not create a FIELD_DECL for empty base classes because of
4949 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4950 be a problem anymore. We need them to handle initialization of C++17
4951 aggregate bases. */
4952 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4954 tree decl = build_base_field_1 (t, binfo, access, next_field);
4955 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4956 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4957 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4958 SET_DECL_FIELD_ABI_IGNORED (decl, 1);
4961 /* An empty virtual base causes a class to be non-empty
4962 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4963 here because that was already done when the virtual table
4964 pointer was created. */
4967 /* Record the offsets of BINFO and its base subobjects. */
4968 record_subobject_offsets (binfo, offsets);
4970 return next_field;
4973 /* Layout all of the non-virtual base classes. Record empty
4974 subobjects in OFFSETS. T is the most derived type. Return nonzero
4975 if the type cannot be nearly empty. The fields created
4976 corresponding to the base classes will be inserted at
4977 *NEXT_FIELD. */
4979 static void
4980 build_base_fields (record_layout_info rli,
4981 splay_tree offsets, tree *next_field)
4983 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4984 subobjects. */
4985 tree t = rli->t;
4986 tree binfo = TYPE_BINFO (t);
4987 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4989 /* The primary base class is always allocated first. */
4990 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
4991 if (primary_binfo)
4993 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4994 base, if it is direct. Indirect base fields are private. */
4995 tree primary_access = access_private_node;
4996 for (int i = 0; i < n_baseclasses; ++i)
4998 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4999 if (base_binfo == primary_binfo)
5001 primary_access = BINFO_BASE_ACCESS (binfo, i);
5002 break;
5005 next_field = build_base_field (rli, primary_binfo,
5006 primary_access,
5007 offsets, next_field);
5010 /* Now allocate the rest of the bases. */
5011 for (int i = 0; i < n_baseclasses; ++i)
5013 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5015 /* The primary base was already allocated above, so we don't
5016 need to allocate it again here. */
5017 if (base_binfo == primary_binfo)
5018 continue;
5020 /* Virtual bases are added at the end (a primary virtual base
5021 will have already been added). */
5022 if (BINFO_VIRTUAL_P (base_binfo))
5023 continue;
5025 next_field = build_base_field (rli, base_binfo,
5026 BINFO_BASE_ACCESS (binfo, i),
5027 offsets, next_field);
5031 /* Go through the TYPE_FIELDS of T issuing any appropriate
5032 diagnostics, figuring out which methods override which other
5033 methods, and so forth. */
5035 static void
5036 check_methods (tree t)
5038 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5039 if (DECL_DECLARES_FUNCTION_P (x))
5041 check_for_override (x, t);
5043 if (DECL_PURE_VIRTUAL_P (x)
5044 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
5045 error ("initializer specified for non-virtual method %q+D", x);
5046 /* The name of the field is the original field name
5047 Save this in auxiliary field for later overloading. */
5048 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
5050 TYPE_POLYMORPHIC_P (t) = 1;
5051 if (DECL_PURE_VIRTUAL_P (x))
5052 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
5055 if (!DECL_VIRTUAL_P (x)
5056 && lookup_attribute ("transaction_safe_dynamic",
5057 DECL_ATTRIBUTES (x)))
5058 error_at (DECL_SOURCE_LOCATION (x),
5059 "%<transaction_safe_dynamic%> may only be specified for "
5060 "a virtual function");
5063 /* Check whether the eligible special member functions (P0848) are
5064 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
5065 has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
5066 ineligible overloads, which is why this needs to be separate from the loop
5067 above. */
5069 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5071 if (TREE_CODE (dtor) == OVERLOAD)
5073 /* P0848: At the end of the definition of a class, overload
5074 resolution is performed among the prospective destructors declared
5075 in that class with an empty argument list to select the destructor
5076 for the class, also known as the selected destructor. The program
5077 is ill-formed if overload resolution fails. */
5078 int viable = 0;
5079 for (tree fn : ovl_range (dtor))
5080 if (constraints_satisfied_p (fn))
5081 ++viable;
5082 gcc_checking_assert (viable != 1);
5084 auto_diagnostic_group d;
5085 if (viable == 0)
5086 error_at (location_of (t), "no viable destructor for %qT", t);
5087 else
5088 error_at (location_of (t), "destructor for %qT is ambiguous", t);
5089 print_candidates (dtor);
5091 /* Arbitrarily prune the overload set to a single function for
5092 sake of error recovery. */
5093 tree *slot = find_member_slot (t, dtor_identifier);
5094 *slot = get_first_fn (dtor);
5096 else if (user_provided_p (dtor))
5097 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
5100 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5102 if (!user_provided_p (fn))
5103 /* Might be trivial. */;
5104 else if (TREE_CODE (fn) == TEMPLATE_DECL)
5105 /* Templates are never special members. */;
5106 else if (copy_fn_p (fn)
5107 && constraints_satisfied_p (fn))
5108 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
5109 else if (move_fn_p (fn)
5110 && constraints_satisfied_p (fn))
5111 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
5114 for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
5116 if (!user_provided_p (fn))
5117 /* Might be trivial. */;
5118 else if (TREE_CODE (fn) == TEMPLATE_DECL)
5119 /* Templates are never special members. */;
5120 else if (copy_fn_p (fn)
5121 && constraints_satisfied_p (fn))
5122 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
5123 else if (move_fn_p (fn)
5124 && constraints_satisfied_p (fn))
5125 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
5129 /* FN is constructor, destructor or operator function. Clone the
5130 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
5131 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
5133 static tree
5134 copy_fndecl_with_name (tree fn, tree name, tree_code code,
5135 bool need_vtt_parm_p, bool omit_inherited_parms_p)
5137 /* Copy the function. */
5138 tree clone = copy_decl (fn);
5139 /* Reset the function name. */
5140 DECL_NAME (clone) = name;
5142 if (flag_concepts)
5143 /* Clone constraints. */
5144 if (tree ci = get_constraints (fn))
5145 set_constraints (clone, copy_node (ci));
5147 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
5148 /* There's no pending inline data for this function. */
5149 DECL_PENDING_INLINE_INFO (clone) = NULL;
5150 DECL_PENDING_INLINE_P (clone) = 0;
5152 if (name == base_dtor_identifier)
5154 /* The base-class destructor is not virtual. */
5155 DECL_VIRTUAL_P (clone) = 0;
5156 DECL_VINDEX (clone) = NULL_TREE;
5158 else if (code != ERROR_MARK)
5160 /* Set the operator code. */
5161 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
5162 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
5164 /* The operator could be virtual. */
5165 if (DECL_VIRTUAL_P (clone))
5166 IDENTIFIER_VIRTUAL_P (name) = true;
5169 if (omit_inherited_parms_p)
5170 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
5172 /* If there was an in-charge parameter, drop it from the function
5173 type. */
5174 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5176 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5177 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
5178 /* Skip the `this' parameter. */
5179 parmtypes = TREE_CHAIN (parmtypes);
5180 /* Skip the in-charge parameter. */
5181 parmtypes = TREE_CHAIN (parmtypes);
5182 /* And the VTT parm, in a complete [cd]tor. */
5183 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
5184 parmtypes = TREE_CHAIN (parmtypes);
5185 if (omit_inherited_parms_p)
5187 /* If we're omitting inherited parms, that just leaves the VTT. */
5188 gcc_assert (need_vtt_parm_p);
5189 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
5191 TREE_TYPE (clone)
5192 = build_method_type_directly (basetype,
5193 TREE_TYPE (TREE_TYPE (clone)),
5194 parmtypes);
5195 TREE_TYPE (clone)
5196 = cp_build_type_attribute_variant (TREE_TYPE (clone),
5197 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
5198 TREE_TYPE (clone)
5199 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
5202 /* Copy the function parameters. */
5203 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
5205 /* Remove the in-charge parameter. */
5206 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5208 DECL_CHAIN (DECL_ARGUMENTS (clone))
5209 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5210 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
5213 /* And the VTT parm, in a complete [cd]tor. */
5214 if (DECL_HAS_VTT_PARM_P (fn))
5216 if (need_vtt_parm_p)
5217 DECL_HAS_VTT_PARM_P (clone) = 1;
5218 else
5220 DECL_CHAIN (DECL_ARGUMENTS (clone))
5221 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5222 DECL_HAS_VTT_PARM_P (clone) = 0;
5226 /* A base constructor inheriting from a virtual base doesn't get the
5227 arguments. */
5228 if (omit_inherited_parms_p)
5229 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5231 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5233 DECL_CONTEXT (parms) = clone;
5234 cxx_dup_lang_specific_decl (parms);
5237 /* Create the RTL for this function. */
5238 SET_DECL_RTL (clone, NULL);
5240 /* Regardless of the current scope, this is a member function, so
5241 not at namespace scope. */
5242 rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5244 return clone;
5247 /* FN is an operator function, create a variant for CODE. */
5249 tree
5250 copy_operator_fn (tree fn, tree_code code)
5252 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5253 code, false, false);
5256 /* FN is a constructor or destructor. Clone the declaration to create
5257 a specialized in-charge or not-in-charge version, as indicated by
5258 NAME. */
5260 static tree
5261 build_clone (tree fn, tree name, bool need_vtt_parm_p,
5262 bool omit_inherited_parms_p)
5264 tree clone;
5266 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5267 if (TREE_CODE (fn) == TEMPLATE_DECL)
5269 clone = copy_decl (fn);
5270 DECL_NAME (clone) = name;
5272 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5273 need_vtt_parm_p, omit_inherited_parms_p);
5274 DECL_TEMPLATE_RESULT (clone) = result;
5276 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5277 DECL_TI_TEMPLATE (result) = clone;
5279 TREE_TYPE (clone) = TREE_TYPE (result);
5281 else
5283 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5284 need_vtt_parm_p, omit_inherited_parms_p);
5285 DECL_CLONED_FUNCTION (clone) = fn;
5287 maybe_prepare_return_this (clone);
5290 /* Remember where this function came from. */
5291 DECL_ABSTRACT_ORIGIN (clone) = fn;
5293 /* Make it easy to find the CLONE given the FN. Note the
5294 template_result of a template will be chained this way too. */
5295 DECL_CHAIN (clone) = DECL_CHAIN (fn);
5296 DECL_CHAIN (fn) = clone;
5298 return clone;
5301 /* Build the clones of FN, return the number of clones built. These
5302 will be inserted onto DECL_CHAIN of FN. */
5304 void
5305 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5306 bool update_methods)
5308 unsigned count = 0;
5310 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5312 /* For each constructor, we need two variants: an in-charge version
5313 and a not-in-charge version. */
5314 build_clone (fn, complete_ctor_identifier, false, false);
5315 build_clone (fn, base_ctor_identifier, needs_vtt_p,
5316 base_omits_inherited_p);
5317 count += 2;
5319 else
5321 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5323 /* For each destructor, we need three variants: an in-charge
5324 version, a not-in-charge version, and an in-charge deleting
5325 version. We clone the deleting version first because that
5326 means it will go second on the TYPE_FIELDS list -- and that
5327 corresponds to the correct layout order in the virtual
5328 function table.
5330 For a non-virtual destructor, we do not build a deleting
5331 destructor. */
5332 if (DECL_VIRTUAL_P (fn))
5334 build_clone (fn, deleting_dtor_identifier, false, false);
5335 count++;
5337 build_clone (fn, complete_dtor_identifier, false, false);
5338 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5339 count += 2;
5342 /* The original is now an abstract function that is never
5343 emitted. */
5344 DECL_ABSTRACT_P (fn) = true;
5346 if (update_methods)
5347 for (tree clone = fn; count--;)
5349 clone = DECL_CHAIN (clone);
5350 add_method (DECL_CONTEXT (clone), clone, false);
5354 /* Produce declarations for all appropriate clones of FN. If
5355 UPDATE_METHODS is true, the clones are added to the
5356 CLASSTYPE_MEMBER_VEC. */
5358 void
5359 clone_cdtor (tree fn, bool update_methods)
5361 /* Avoid inappropriate cloning. */
5362 if (DECL_CHAIN (fn)
5363 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5364 return;
5366 /* Base cdtors need a vtt parm if there are virtual bases. */
5367 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5369 /* Base ctor omits inherited parms it needs a vttparm and inherited
5370 from a virtual nase ctor. */
5371 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5372 && base_ctor_omit_inherited_parms (fn));
5374 build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5377 /* DECL is an in charge constructor, which is being defined. This will
5378 have had an in class declaration, from whence clones were
5379 declared. An out-of-class definition can specify additional default
5380 arguments. As it is the clones that are involved in overload
5381 resolution, we must propagate the information from the DECL to its
5382 clones. */
5384 void
5385 adjust_clone_args (tree decl)
5387 tree clone;
5389 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5390 clone = DECL_CHAIN (clone))
5392 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5393 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5394 tree decl_parms, clone_parms;
5396 /* Skip the 'this' parameter. */
5397 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5398 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5400 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5401 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5402 if (DECL_HAS_VTT_PARM_P (decl))
5403 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5405 clone_parms = orig_clone_parms;
5406 if (DECL_HAS_VTT_PARM_P (clone))
5407 clone_parms = TREE_CHAIN (clone_parms);
5409 for (decl_parms = orig_decl_parms; decl_parms;
5410 decl_parms = TREE_CHAIN (decl_parms),
5411 clone_parms = TREE_CHAIN (clone_parms))
5413 if (clone_parms == void_list_node)
5415 gcc_assert (decl_parms == clone_parms
5416 || ctor_omit_inherited_parms (clone));
5417 break;
5420 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5421 TREE_VALUE (clone_parms)));
5423 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5425 /* A default parameter has been added. Adjust the
5426 clone's parameters. */
5427 clone_parms = orig_decl_parms;
5429 if (DECL_HAS_VTT_PARM_P (clone))
5431 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5432 TREE_VALUE (orig_clone_parms),
5433 clone_parms);
5434 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5437 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5438 tree type
5439 = build_method_type_directly (basetype,
5440 TREE_TYPE (TREE_TYPE (clone)),
5441 clone_parms);
5442 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5443 type = cp_build_type_attribute_variant (type, attrs);
5444 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5445 TREE_TYPE (clone) = type;
5447 clone_parms = NULL_TREE;
5448 break;
5451 gcc_assert (!clone_parms || clone_parms == void_list_node);
5455 /* For each of the constructors and destructors in T, create an
5456 in-charge and not-in-charge variant. */
5458 static void
5459 clone_constructors_and_destructors (tree t)
5461 /* We do not need to propagate the usingness to the clone, at this
5462 point that is not needed. */
5463 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5464 clone_cdtor (fn, /*update_methods=*/true);
5466 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5467 clone_cdtor (dtor, /*update_methods=*/true);
5470 /* Deduce noexcept for a destructor DTOR. */
5472 void
5473 deduce_noexcept_on_destructor (tree dtor)
5475 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5476 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5477 noexcept_deferred_spec);
5480 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5481 of TYPE for virtual functions which FNDECL overrides. Return a
5482 mask of the tm attributes found therein. */
5484 static int
5485 look_for_tm_attr_overrides (tree type, tree fndecl)
5487 tree binfo = TYPE_BINFO (type);
5488 tree base_binfo;
5489 int ix, found = 0;
5491 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5493 tree o, basetype = BINFO_TYPE (base_binfo);
5495 if (!TYPE_POLYMORPHIC_P (basetype))
5496 continue;
5498 o = look_for_overrides_here (basetype, fndecl);
5499 if (o)
5501 if (lookup_attribute ("transaction_safe_dynamic",
5502 DECL_ATTRIBUTES (o)))
5503 /* transaction_safe_dynamic is not inherited. */;
5504 else
5505 found |= tm_attr_to_mask (find_tm_attribute
5506 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5508 else
5509 found |= look_for_tm_attr_overrides (basetype, fndecl);
5512 return found;
5515 /* Subroutine of set_method_tm_attributes. Handle the checks and
5516 inheritance for one virtual method FNDECL. */
5518 static void
5519 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5521 tree tm_attr;
5522 int found, have;
5524 found = look_for_tm_attr_overrides (type, fndecl);
5526 /* If FNDECL doesn't actually override anything (i.e. T is the
5527 class that first declares FNDECL virtual), then we're done. */
5528 if (found == 0)
5529 return;
5531 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5532 have = tm_attr_to_mask (tm_attr);
5534 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5535 tm_pure must match exactly, otherwise no weakening of
5536 tm_safe > tm_callable > nothing. */
5537 /* ??? The tm_pure attribute didn't make the transition to the
5538 multivendor language spec. */
5539 if (have == TM_ATTR_PURE)
5541 if (found != TM_ATTR_PURE)
5543 found &= -found;
5544 goto err_override;
5547 /* If the overridden function is tm_pure, then FNDECL must be. */
5548 else if (found == TM_ATTR_PURE && tm_attr)
5549 goto err_override;
5550 /* Look for base class combinations that cannot be satisfied. */
5551 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5553 found &= ~TM_ATTR_PURE;
5554 found &= -found;
5555 error_at (DECL_SOURCE_LOCATION (fndecl),
5556 "method overrides both %<transaction_pure%> and %qE methods",
5557 tm_mask_to_attr (found));
5559 /* If FNDECL did not declare an attribute, then inherit the most
5560 restrictive one. */
5561 else if (tm_attr == NULL)
5563 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5565 /* Otherwise validate that we're not weaker than a function
5566 that is being overridden. */
5567 else
5569 found &= -found;
5570 if (found <= TM_ATTR_CALLABLE && have > found)
5571 goto err_override;
5573 return;
5575 err_override:
5576 error_at (DECL_SOURCE_LOCATION (fndecl),
5577 "method declared %qE overriding %qE method",
5578 tm_attr, tm_mask_to_attr (found));
5581 /* For each of the methods in T, propagate a class-level tm attribute. */
5583 static void
5584 set_method_tm_attributes (tree t)
5586 tree class_tm_attr, fndecl;
5588 /* Don't bother collecting tm attributes if transactional memory
5589 support is not enabled. */
5590 if (!flag_tm)
5591 return;
5593 /* Process virtual methods first, as they inherit directly from the
5594 base virtual function and also require validation of new attributes. */
5595 if (TYPE_CONTAINS_VPTR_P (t))
5597 tree vchain;
5598 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5599 vchain = TREE_CHAIN (vchain))
5601 fndecl = BV_FN (vchain);
5602 if (DECL_THUNK_P (fndecl))
5603 fndecl = THUNK_TARGET (fndecl);
5604 set_one_vmethod_tm_attributes (t, fndecl);
5608 /* If the class doesn't have an attribute, nothing more to do. */
5609 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5610 if (class_tm_attr == NULL)
5611 return;
5613 /* Any method that does not yet have a tm attribute inherits
5614 the one from the class. */
5615 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5616 if (DECL_DECLARES_FUNCTION_P (fndecl)
5617 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5618 apply_tm_attr (fndecl, class_tm_attr);
5621 /* Returns true if FN is a default constructor. */
5623 bool
5624 default_ctor_p (const_tree fn)
5626 return (DECL_CONSTRUCTOR_P (fn)
5627 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5630 /* Returns true iff class T has a user-provided constructor that can be called
5631 with more than zero arguments. */
5633 bool
5634 type_has_user_nondefault_constructor (tree t)
5636 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5637 return false;
5639 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5641 if (user_provided_p (fn)
5642 && (TREE_CODE (fn) == TEMPLATE_DECL
5643 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5644 != NULL_TREE)))
5645 return true;
5648 return false;
5651 /* Returns the defaulted constructor if T has one. Otherwise, returns
5652 NULL_TREE. */
5654 tree
5655 in_class_defaulted_default_constructor (tree t)
5657 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5658 return NULL_TREE;
5660 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5662 tree fn = *iter;
5664 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5665 && default_ctor_p (fn))
5666 return fn;
5669 return NULL_TREE;
5672 /* Returns true iff FN is a user-provided function, i.e. user-declared
5673 and not defaulted at its first declaration. */
5675 bool
5676 user_provided_p (tree fn)
5678 fn = STRIP_TEMPLATE (fn);
5679 return (!DECL_ARTIFICIAL (fn)
5680 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5681 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5684 /* Returns true iff class T has a user-provided constructor. */
5686 bool
5687 type_has_user_provided_constructor (tree t)
5689 if (!CLASS_TYPE_P (t))
5690 return false;
5692 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5693 return false;
5695 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5696 if (user_provided_p (*iter))
5697 return true;
5699 return false;
5702 /* Returns true iff class T has a user-provided or explicit constructor. */
5704 bool
5705 type_has_user_provided_or_explicit_constructor (tree t)
5707 if (!CLASS_TYPE_P (t))
5708 return false;
5710 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5711 return false;
5713 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5715 tree fn = *iter;
5716 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5717 return true;
5720 return false;
5723 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5724 declared or explicitly defaulted in the class body) default
5725 constructor. */
5727 bool
5728 type_has_non_user_provided_default_constructor (tree t)
5730 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5731 return false;
5732 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5733 return true;
5735 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5737 tree fn = *iter;
5738 if (TREE_CODE (fn) == FUNCTION_DECL
5739 && default_ctor_p (fn)
5740 && !user_provided_p (fn))
5741 return true;
5744 return false;
5747 /* TYPE is being used as a virtual base, and has a non-trivial move
5748 assignment. Return true if this is due to there being a user-provided
5749 move assignment in TYPE or one of its subobjects; if there isn't, then
5750 multiple move assignment can't cause any harm. */
5752 bool
5753 vbase_has_user_provided_move_assign (tree type)
5755 /* Does the type itself have a user-provided move assignment operator? */
5756 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5757 for (ovl_iterator iter (get_class_binding_direct
5758 (type, assign_op_identifier));
5759 iter; ++iter)
5760 if (user_provided_p (*iter) && move_fn_p (*iter))
5761 return true;
5763 /* Do any of its bases? */
5764 tree binfo = TYPE_BINFO (type);
5765 tree base_binfo;
5766 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5767 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5768 return true;
5770 /* Or non-static data members? */
5771 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5773 if (TREE_CODE (field) == FIELD_DECL
5774 && CLASS_TYPE_P (TREE_TYPE (field))
5775 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5776 return true;
5779 /* Seems not. */
5780 return false;
5783 /* If default-initialization leaves part of TYPE uninitialized, returns
5784 a DECL for the field or TYPE itself (DR 253). */
5786 tree
5787 default_init_uninitialized_part (tree type)
5789 tree t, r, binfo;
5790 int i;
5792 type = strip_array_types (type);
5793 if (!CLASS_TYPE_P (type))
5794 return type;
5795 if (!type_has_non_user_provided_default_constructor (type))
5796 return NULL_TREE;
5797 for (binfo = TYPE_BINFO (type), i = 0;
5798 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5800 r = default_init_uninitialized_part (BINFO_TYPE (t));
5801 if (r)
5802 return r;
5804 for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5805 t = next_aggregate_field (DECL_CHAIN (t)))
5806 if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5808 r = default_init_uninitialized_part (TREE_TYPE (t));
5809 if (r)
5810 return DECL_P (r) ? r : t;
5813 return NULL_TREE;
5816 /* Returns true iff for class T, a trivial synthesized default constructor
5817 would be constexpr. */
5819 bool
5820 trivial_default_constructor_is_constexpr (tree t)
5822 /* A defaulted trivial default constructor is constexpr
5823 if there is nothing to initialize. */
5824 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5825 /* A class with a vptr doesn't have a trivial default ctor.
5826 In C++20, a class can have transient uninitialized members, e.g.:
5828 struct S { int i; constexpr S() = default; };
5830 should work. */
5831 return (cxx_dialect >= cxx20
5832 || is_really_empty_class (t, /*ignore_vptr*/true));
5835 /* Returns true iff class T has a constexpr default constructor. */
5837 bool
5838 type_has_constexpr_default_constructor (tree t)
5840 tree fns;
5842 if (!CLASS_TYPE_P (t))
5844 /* The caller should have stripped an enclosing array. */
5845 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5846 return false;
5848 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5850 if (!TYPE_HAS_COMPLEX_DFLT (t))
5851 return trivial_default_constructor_is_constexpr (t);
5852 /* Non-trivial, we need to check subobject constructors. */
5853 lazily_declare_fn (sfk_constructor, t);
5855 fns = locate_ctor (t);
5856 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5859 /* Returns true iff class T has a constexpr default constructor or has an
5860 implicitly declared default constructor that we can't tell if it's constexpr
5861 without forcing a lazy declaration (which might cause undesired
5862 instantiations). */
5864 static bool
5865 type_maybe_constexpr_default_constructor (tree t)
5867 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5868 && TYPE_HAS_COMPLEX_DFLT (t))
5869 /* Assume it's constexpr. */
5870 return true;
5871 return type_has_constexpr_default_constructor (t);
5874 /* Returns true iff class T has a constexpr destructor. */
5876 bool
5877 type_has_constexpr_destructor (tree t)
5879 tree fns;
5881 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5882 /* Non-trivial, we need to check subobject destructors. */
5883 lazily_declare_fn (sfk_destructor, t);
5884 fns = CLASSTYPE_DESTRUCTOR (t);
5885 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5888 /* Returns true iff class T has a constexpr destructor or has an
5889 implicitly declared destructor that we can't tell if it's constexpr
5890 without forcing a lazy declaration (which might cause undesired
5891 instantiations). */
5893 static bool
5894 type_maybe_constexpr_destructor (tree t)
5896 /* Until C++20, only trivial destruction is constexpr. */
5897 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5898 return true;
5899 if (cxx_dialect < cxx20)
5900 return false;
5901 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5902 /* Assume it's constexpr. */
5903 return true;
5904 tree fn = CLASSTYPE_DESTRUCTOR (t);
5905 return (fn && maybe_constexpr_fn (fn));
5908 /* Returns true iff class TYPE has a virtual destructor. */
5910 bool
5911 type_has_virtual_destructor (tree type)
5913 tree dtor;
5915 if (!NON_UNION_CLASS_TYPE_P (type))
5916 return false;
5918 gcc_assert (COMPLETE_TYPE_P (type));
5919 dtor = CLASSTYPE_DESTRUCTOR (type);
5920 return (dtor && DECL_VIRTUAL_P (dtor));
5923 /* True iff class TYPE has a non-deleted trivial default
5924 constructor. */
5926 bool
5927 type_has_non_deleted_trivial_default_ctor (tree type)
5929 return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
5932 /* Returns true iff T, a class, has a move-assignment or
5933 move-constructor. Does not lazily declare either.
5934 If USER_P is false, any move function will do. If it is true, the
5935 move function must be user-declared.
5937 Note that user-declared here is different from "user-provided",
5938 which doesn't include functions that are defaulted in the
5939 class. */
5941 bool
5942 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5944 gcc_assert (user_p
5945 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5946 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5948 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5949 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5950 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5951 return true;
5953 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5954 for (ovl_iterator iter (get_class_binding_direct
5955 (t, assign_op_identifier));
5956 iter; ++iter)
5957 if ((!user_p || !DECL_ARTIFICIAL (*iter))
5958 && DECL_CONTEXT (*iter) == t
5959 && move_fn_p (*iter))
5960 return true;
5962 return false;
5965 /* True iff T has a move constructor that is not deleted. */
5967 bool
5968 classtype_has_non_deleted_move_ctor (tree t)
5970 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5971 lazily_declare_fn (sfk_move_constructor, t);
5972 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5973 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5974 return true;
5975 return false;
5978 /* If T, a class, has a user-provided copy constructor, copy assignment
5979 operator, or destructor, returns that function. Otherwise, null. */
5981 tree
5982 classtype_has_depr_implicit_copy (tree t)
5984 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5985 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5987 tree fn = *iter;
5988 if (user_provided_p (fn) && copy_fn_p (fn))
5989 return fn;
5992 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5993 for (ovl_iterator iter (get_class_binding_direct
5994 (t, assign_op_identifier));
5995 iter; ++iter)
5997 tree fn = *iter;
5998 if (DECL_CONTEXT (fn) == t
5999 && user_provided_p (fn) && copy_fn_p (fn))
6000 return fn;
6003 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
6005 tree fn = CLASSTYPE_DESTRUCTOR (t);
6006 if (user_provided_p (fn))
6007 return fn;
6010 return NULL_TREE;
6013 /* True iff T has a member or friend declaration of operator OP. */
6015 bool
6016 classtype_has_op (tree t, tree_code op)
6018 tree name = ovl_op_identifier (op);
6019 if (get_class_binding (t, name))
6020 return true;
6021 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6022 if (FRIEND_NAME (f) == name)
6023 return true;
6024 return false;
6028 /* If T has a defaulted member or friend declaration of OP, return it. */
6030 tree
6031 classtype_has_defaulted_op (tree t, tree_code op)
6033 tree name = ovl_op_identifier (op);
6034 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
6036 tree fn = *oi;
6037 if (DECL_DEFAULTED_FN (fn))
6038 return fn;
6040 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6041 if (FRIEND_NAME (f) == name)
6042 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
6044 tree fn = TREE_VALUE (l);
6045 if (DECL_DEFAULTED_FN (fn))
6046 return fn;
6048 return NULL_TREE;
6051 /* Nonzero if we need to build up a constructor call when initializing an
6052 object of this class, either because it has a user-declared constructor
6053 or because it doesn't have a default constructor (so we need to give an
6054 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
6055 what you care about is whether or not an object can be produced by a
6056 constructor (e.g. so we don't set TREE_READONLY on const variables of
6057 such type); use this function when what you care about is whether or not
6058 to try to call a constructor to create an object. The latter case is
6059 the former plus some cases of constructors that cannot be called. */
6061 bool
6062 type_build_ctor_call (tree t)
6064 tree inner;
6065 if (TYPE_NEEDS_CONSTRUCTING (t))
6066 return true;
6067 inner = strip_array_types (t);
6068 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
6069 return false;
6070 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
6071 return true;
6072 if (cxx_dialect < cxx11)
6073 return false;
6074 /* A user-declared constructor might be private, and a constructor might
6075 be trivial but deleted. */
6076 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
6077 iter; ++iter)
6079 tree fn = *iter;
6080 if (!DECL_ARTIFICIAL (fn)
6081 || TREE_DEPRECATED (fn)
6082 || TREE_UNAVAILABLE (fn)
6083 || DECL_DELETED_FN (fn))
6084 return true;
6086 return false;
6089 /* Like type_build_ctor_call, but for destructors. */
6091 bool
6092 type_build_dtor_call (tree t)
6094 tree inner;
6095 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6096 return true;
6097 inner = strip_array_types (t);
6098 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
6099 || !COMPLETE_TYPE_P (inner))
6100 return false;
6101 if (cxx_dialect < cxx11)
6102 return false;
6103 /* A user-declared destructor might be private, and a destructor might
6104 be trivial but deleted. */
6105 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
6106 iter; ++iter)
6108 tree fn = *iter;
6109 if (!DECL_ARTIFICIAL (fn)
6110 || TREE_DEPRECATED (fn)
6111 || TREE_UNAVAILABLE (fn)
6112 || DECL_DELETED_FN (fn))
6113 return true;
6115 return false;
6118 /* Returns TRUE iff we need a cookie when dynamically allocating an
6119 array whose elements have the indicated class TYPE. */
6121 static bool
6122 type_requires_array_cookie (tree type)
6124 tree fns;
6125 bool has_two_argument_delete_p = false;
6127 gcc_assert (CLASS_TYPE_P (type));
6129 /* If there's a non-trivial destructor, we need a cookie. In order
6130 to iterate through the array calling the destructor for each
6131 element, we'll have to know how many elements there are. */
6132 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6133 return true;
6135 /* If the usual deallocation function is a two-argument whose second
6136 argument is of type `size_t', then we have to pass the size of
6137 the array to the deallocation function, so we will need to store
6138 a cookie. */
6139 fns = lookup_fnfields (TYPE_BINFO (type),
6140 ovl_op_identifier (false, VEC_DELETE_EXPR),
6141 /*protect=*/0, tf_warning_or_error);
6142 /* If there are no `operator []' members, or the lookup is
6143 ambiguous, then we don't need a cookie. */
6144 if (!fns || fns == error_mark_node)
6145 return false;
6146 /* Loop through all of the functions. */
6147 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
6149 tree fn = *iter;
6151 /* See if this function is a one-argument delete function. If
6152 it is, then it will be the usual deallocation function. */
6153 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
6154 if (second_parm == void_list_node)
6155 return false;
6156 /* Do not consider this function if its second argument is an
6157 ellipsis. */
6158 if (!second_parm)
6159 continue;
6160 /* Otherwise, if we have a two-argument function and the second
6161 argument is `size_t', it will be the usual deallocation
6162 function -- unless there is one-argument function, too. */
6163 if (TREE_CHAIN (second_parm) == void_list_node
6164 && same_type_p (TREE_VALUE (second_parm), size_type_node))
6165 has_two_argument_delete_p = true;
6168 return has_two_argument_delete_p;
6171 /* Finish computing the `literal type' property of class type T.
6173 At this point, we have already processed base classes and
6174 non-static data members. We need to check whether the copy
6175 constructor is trivial, the destructor is trivial, and there
6176 is a trivial default constructor or at least one constexpr
6177 constructor other than the copy constructor. */
6179 static void
6180 finalize_literal_type_property (tree t)
6182 tree fn;
6184 if (cxx_dialect < cxx11)
6185 CLASSTYPE_LITERAL_P (t) = false;
6186 else if (CLASSTYPE_LITERAL_P (t)
6187 && !type_maybe_constexpr_destructor (t))
6188 CLASSTYPE_LITERAL_P (t) = false;
6189 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
6190 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
6191 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6192 && CLASSTYPE_NON_AGGREGATE (t)
6193 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6194 CLASSTYPE_LITERAL_P (t) = false;
6196 /* C++14 DR 1684 removed this restriction. */
6197 if (cxx_dialect < cxx14
6198 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
6199 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6200 if (TREE_CODE (fn) == FUNCTION_DECL
6201 && DECL_DECLARED_CONSTEXPR_P (fn)
6202 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
6203 && !DECL_CONSTRUCTOR_P (fn))
6205 DECL_DECLARED_CONSTEXPR_P (fn) = false;
6206 if (!DECL_GENERATED_P (fn))
6208 auto_diagnostic_group d;
6209 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
6210 "enclosing class of %<constexpr%> non-static "
6211 "member function %q+#D is not a literal type", fn))
6212 explain_non_literal_class (t);
6217 /* T is a non-literal type used in a context which requires a constant
6218 expression. Explain why it isn't literal. */
6220 void
6221 explain_non_literal_class (tree t)
6223 static hash_set<tree> *diagnosed;
6225 if (!CLASS_TYPE_P (t))
6226 return;
6227 t = TYPE_MAIN_VARIANT (t);
6229 if (diagnosed == NULL)
6230 diagnosed = new hash_set<tree>;
6231 if (diagnosed->add (t))
6232 /* Already explained. */
6233 return;
6235 auto_diagnostic_group d;
6236 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6237 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
6238 inform (UNKNOWN_LOCATION,
6239 " %qT is a closure type, which is only literal in "
6240 "C++17 and later", t);
6241 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6242 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6243 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6244 && !type_maybe_constexpr_destructor (t))
6245 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6247 else if (CLASSTYPE_NON_AGGREGATE (t)
6248 && !TYPE_HAS_TRIVIAL_DFLT (t)
6249 && !LAMBDA_TYPE_P (t)
6250 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6252 inform (UNKNOWN_LOCATION,
6253 " %q+T is not an aggregate, does not have a trivial "
6254 "default constructor, and has no %<constexpr%> constructor that "
6255 "is not a copy or move constructor", t);
6256 if (type_has_non_user_provided_default_constructor (t))
6257 /* Note that we can't simply call locate_ctor because when the
6258 constructor is deleted it just returns NULL_TREE. */
6259 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6261 tree fn = *iter;
6262 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6264 parms = skip_artificial_parms_for (fn, parms);
6266 if (sufficient_parms_p (parms))
6268 if (DECL_DELETED_FN (fn))
6269 maybe_explain_implicit_delete (fn);
6270 else
6271 explain_invalid_constexpr_fn (fn);
6272 break;
6276 else
6278 tree binfo, base_binfo, field; int i;
6279 for (binfo = TYPE_BINFO (t), i = 0;
6280 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6282 tree basetype = TREE_TYPE (base_binfo);
6283 if (!CLASSTYPE_LITERAL_P (basetype))
6285 inform (UNKNOWN_LOCATION,
6286 " base class %qT of %q+T is non-literal",
6287 basetype, t);
6288 explain_non_literal_class (basetype);
6289 return;
6292 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6294 tree ftype;
6295 if (TREE_CODE (field) != FIELD_DECL)
6296 continue;
6297 ftype = TREE_TYPE (field);
6298 if (!literal_type_p (ftype))
6300 inform (DECL_SOURCE_LOCATION (field),
6301 " non-static data member %qD has non-literal type",
6302 field);
6303 if (CLASS_TYPE_P (ftype))
6304 explain_non_literal_class (ftype);
6306 if (CP_TYPE_VOLATILE_P (ftype))
6307 inform (DECL_SOURCE_LOCATION (field),
6308 " non-static data member %qD has volatile type", field);
6313 /* Check the validity of the bases and members declared in T. Add any
6314 implicitly-generated functions (like copy-constructors and
6315 assignment operators). Compute various flag bits (like
6316 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6317 level: i.e., independently of the ABI in use. */
6319 static void
6320 check_bases_and_members (tree t)
6322 /* Nonzero if the implicitly generated copy constructor should take
6323 a non-const reference argument. */
6324 int cant_have_const_ctor;
6325 /* Nonzero if the implicitly generated assignment operator
6326 should take a non-const reference argument. */
6327 int no_const_asn_ref;
6328 tree access_decls;
6329 bool saved_complex_asn_ref;
6330 bool saved_nontrivial_dtor;
6331 tree fn;
6333 /* By default, we use const reference arguments and generate default
6334 constructors. */
6335 cant_have_const_ctor = 0;
6336 no_const_asn_ref = 0;
6338 /* Check all the base-classes and set FMEM members to point to arrays
6339 of potential interest. */
6340 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6342 /* Deduce noexcept on destructor. This needs to happen after we've set
6343 triviality flags appropriately for our bases, and before checking
6344 overriden virtual functions via check_methods. */
6345 if (cxx_dialect >= cxx11)
6346 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6347 for (tree fn : ovl_range (dtor))
6348 deduce_noexcept_on_destructor (fn);
6350 /* Check all the method declarations. */
6351 check_methods (t);
6353 /* Save the initial values of these flags which only indicate whether
6354 or not the class has user-provided functions. As we analyze the
6355 bases and members we can set these flags for other reasons. */
6356 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6357 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6359 /* Check all the data member declarations. We cannot call
6360 check_field_decls until we have called check_bases check_methods,
6361 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6362 being set appropriately. */
6363 check_field_decls (t, &access_decls,
6364 &cant_have_const_ctor,
6365 &no_const_asn_ref);
6367 /* A nearly-empty class has to be vptr-containing; a nearly empty
6368 class contains just a vptr. */
6369 if (!TYPE_CONTAINS_VPTR_P (t))
6370 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6372 /* Do some bookkeeping that will guide the generation of implicitly
6373 declared member functions. */
6374 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6375 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6376 /* We need to call a constructor for this class if it has a
6377 user-provided constructor, or if the default constructor is going
6378 to initialize the vptr. (This is not an if-and-only-if;
6379 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6380 themselves need constructing.) */
6381 TYPE_NEEDS_CONSTRUCTING (t)
6382 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6383 /* [dcl.init.aggr]
6385 An aggregate is an array or a class with no user-provided
6386 constructors ... and no virtual functions.
6388 Again, other conditions for being an aggregate are checked
6389 elsewhere. */
6390 CLASSTYPE_NON_AGGREGATE (t)
6391 |= ((cxx_dialect < cxx20
6392 ? type_has_user_provided_or_explicit_constructor (t)
6393 : TYPE_HAS_USER_CONSTRUCTOR (t))
6394 || TYPE_POLYMORPHIC_P (t));
6395 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6396 retain the old definition internally for ABI reasons. */
6397 CLASSTYPE_NON_LAYOUT_POD_P (t)
6398 |= (CLASSTYPE_NON_AGGREGATE (t)
6399 || saved_nontrivial_dtor || saved_complex_asn_ref);
6400 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6401 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6402 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6403 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6405 /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6406 if (CLASSTYPE_NON_POD_AGGREGATE (t))
6408 if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6409 /* It's non-POD for another reason. */
6410 CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6411 else if (abi_version_at_least (17))
6412 CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6415 /* If the only explicitly declared default constructor is user-provided,
6416 set TYPE_HAS_COMPLEX_DFLT. */
6417 if (!TYPE_HAS_COMPLEX_DFLT (t)
6418 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6419 && !type_has_non_user_provided_default_constructor (t))
6420 TYPE_HAS_COMPLEX_DFLT (t) = true;
6422 /* Warn if a public base of a polymorphic type has an accessible
6423 non-virtual destructor. It is only now that we know the class is
6424 polymorphic. Although a polymorphic base will have a already
6425 been diagnosed during its definition, we warn on use too. */
6426 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6428 tree binfo = TYPE_BINFO (t);
6429 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6430 tree base_binfo;
6431 unsigned i;
6433 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6435 tree basetype = TREE_TYPE (base_binfo);
6437 if ((*accesses)[i] == access_public_node
6438 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6439 && accessible_nvdtor_p (basetype))
6440 warning (OPT_Wnon_virtual_dtor,
6441 "base class %q#T has accessible non-virtual destructor",
6442 basetype);
6446 /* If the class has no user-declared constructor, but does have
6447 non-static const or reference data members that can never be
6448 initialized, issue a warning. */
6449 if (warn_uninitialized
6450 /* Classes with user-declared constructors are presumed to
6451 initialize these members. */
6452 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6453 /* Aggregates can be initialized with brace-enclosed
6454 initializers. */
6455 && CLASSTYPE_NON_AGGREGATE (t))
6457 tree field;
6459 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6461 tree type;
6463 if (TREE_CODE (field) != FIELD_DECL
6464 || DECL_INITIAL (field) != NULL_TREE)
6465 continue;
6467 type = TREE_TYPE (field);
6468 if (TYPE_REF_P (type))
6469 warning_at (DECL_SOURCE_LOCATION (field),
6470 OPT_Wuninitialized, "non-static reference %q#D "
6471 "in class without a constructor", field);
6472 else if (CP_TYPE_CONST_P (type)
6473 && (!CLASS_TYPE_P (type)
6474 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6475 warning_at (DECL_SOURCE_LOCATION (field),
6476 OPT_Wuninitialized, "non-static const member %q#D "
6477 "in class without a constructor", field);
6481 /* Synthesize any needed methods. */
6482 add_implicitly_declared_members (t, &access_decls,
6483 cant_have_const_ctor,
6484 no_const_asn_ref);
6486 /* Check defaulted declarations here so we have cant_have_const_ctor
6487 and don't need to worry about clones. */
6488 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6489 if (DECL_DECLARES_FUNCTION_P (fn)
6490 && !DECL_ARTIFICIAL (fn)
6491 && DECL_DEFAULTED_IN_CLASS_P (fn)
6492 /* ...except handle comparisons later, in finish_struct_1. */
6493 && special_function_p (fn) != sfk_comparison)
6495 bool imp_const_p
6496 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6497 : !no_const_asn_ref);
6498 defaulted_late_check (fn, imp_const_p);
6501 if (LAMBDA_TYPE_P (t))
6502 /* "This class type is not an aggregate." */
6503 CLASSTYPE_NON_AGGREGATE (t) = 1;
6505 /* Compute the 'literal type' property before we
6506 do anything with non-static member functions. */
6507 finalize_literal_type_property (t);
6509 /* Create the in-charge and not-in-charge variants of constructors
6510 and destructors. */
6511 clone_constructors_and_destructors (t);
6513 /* Process the using-declarations. */
6514 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6515 handle_using_decl (TREE_VALUE (access_decls), t);
6517 /* Figure out whether or not we will need a cookie when dynamically
6518 allocating an array of this type. */
6519 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6520 = type_requires_array_cookie (t);
6522 /* Classes marked hot or cold propagate the attribute to all members. We
6523 may do this now that methods are declared. This does miss some lazily
6524 declared special member functions (CLASSTYPE_LAZY_*), which are handled
6525 in lazily_declare_fn later on. */
6526 propagate_class_warmth_attribute (t);
6529 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6530 accordingly. If a new vfield was created (because T doesn't have a
6531 primary base class), then the newly created field is returned. It
6532 is not added to the TYPE_FIELDS list; it is the caller's
6533 responsibility to do that. Accumulate declared virtual functions
6534 on VIRTUALS_P. */
6536 static tree
6537 create_vtable_ptr (tree t, tree* virtuals_p)
6539 tree fn;
6541 /* Collect the virtual functions declared in T. */
6542 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6543 if (TREE_CODE (fn) == FUNCTION_DECL
6544 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6545 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6547 tree new_virtual = make_node (TREE_LIST);
6549 BV_FN (new_virtual) = fn;
6550 BV_DELTA (new_virtual) = integer_zero_node;
6551 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6553 TREE_CHAIN (new_virtual) = *virtuals_p;
6554 *virtuals_p = new_virtual;
6557 /* If we couldn't find an appropriate base class, create a new field
6558 here. Even if there weren't any new virtual functions, we might need a
6559 new virtual function table if we're supposed to include vptrs in
6560 all classes that need them. */
6561 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6563 /* We build this decl with vtbl_ptr_type_node, which is a
6564 `vtable_entry_type*'. It might seem more precise to use
6565 `vtable_entry_type (*)[N]' where N is the number of virtual
6566 functions. However, that would require the vtable pointer in
6567 base classes to have a different type than the vtable pointer
6568 in derived classes. We could make that happen, but that
6569 still wouldn't solve all the problems. In particular, the
6570 type-based alias analysis code would decide that assignments
6571 to the base class vtable pointer can't alias assignments to
6572 the derived class vtable pointer, since they have different
6573 types. Thus, in a derived class destructor, where the base
6574 class constructor was inlined, we could generate bad code for
6575 setting up the vtable pointer.
6577 Therefore, we use one type for all vtable pointers. We still
6578 use a type-correct type; it's just doesn't indicate the array
6579 bounds. That's better than using `void*' or some such; it's
6580 cleaner, and it let's the alias analysis code know that these
6581 stores cannot alias stores to void*! */
6582 tree field;
6584 field = build_decl (input_location,
6585 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6586 DECL_VIRTUAL_P (field) = 1;
6587 DECL_ARTIFICIAL (field) = 1;
6588 DECL_FIELD_CONTEXT (field) = t;
6589 DECL_FCONTEXT (field) = t;
6590 if (TYPE_PACKED (t))
6591 DECL_PACKED (field) = 1;
6593 TYPE_VFIELD (t) = field;
6595 /* This class is non-empty. */
6596 CLASSTYPE_EMPTY_P (t) = 0;
6598 return field;
6601 return NULL_TREE;
6604 /* Add OFFSET to all base types of BINFO which is a base in the
6605 hierarchy dominated by T.
6607 OFFSET, which is a type offset, is number of bytes. */
6609 static void
6610 propagate_binfo_offsets (tree binfo, tree offset)
6612 int i;
6613 tree primary_binfo;
6614 tree base_binfo;
6616 /* Update BINFO's offset. */
6617 BINFO_OFFSET (binfo)
6618 = fold_convert (sizetype,
6619 size_binop (PLUS_EXPR,
6620 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6621 offset));
6623 /* Find the primary base class. */
6624 primary_binfo = get_primary_binfo (binfo);
6626 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6627 propagate_binfo_offsets (primary_binfo, offset);
6629 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6630 downwards. */
6631 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6633 /* Don't do the primary base twice. */
6634 if (base_binfo == primary_binfo)
6635 continue;
6637 if (BINFO_VIRTUAL_P (base_binfo))
6638 continue;
6640 propagate_binfo_offsets (base_binfo, offset);
6644 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6645 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6646 empty subobjects of T. */
6648 static void
6649 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6651 tree vbase;
6652 tree t = rli->t;
6653 tree *next_field;
6655 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6656 return;
6658 /* Find the last field. The artificial fields created for virtual
6659 bases will go after the last extant field to date. */
6660 next_field = &TYPE_FIELDS (t);
6661 while (*next_field)
6662 next_field = &DECL_CHAIN (*next_field);
6664 /* Go through the virtual bases, allocating space for each virtual
6665 base that is not already a primary base class. These are
6666 allocated in inheritance graph order. */
6667 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6669 if (!BINFO_VIRTUAL_P (vbase))
6670 continue;
6672 if (!BINFO_PRIMARY_P (vbase))
6674 /* This virtual base is not a primary base of any class in the
6675 hierarchy, so we have to add space for it. */
6676 next_field = build_base_field (rli, vbase,
6677 access_private_node,
6678 offsets, next_field);
6683 /* Returns the offset of the byte just past the end of the base class
6684 BINFO. */
6686 static tree
6687 end_of_base (tree binfo)
6689 tree size;
6691 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6692 size = TYPE_SIZE_UNIT (char_type_node);
6693 else if (is_empty_class (BINFO_TYPE (binfo)))
6694 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6695 allocate some space for it. It cannot have virtual bases, so
6696 TYPE_SIZE_UNIT is fine. */
6697 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6698 else
6699 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6701 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6704 /* Returns one of three variations of the ending offset of T. If MODE is
6705 eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6706 vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6707 vbases but before rounding, which is not named in the ABI. If MODE is
6708 eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6709 of the actual data in the class, kinda), as used for allocation of
6710 potentially-overlapping fields. */
6712 enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6713 static tree
6714 end_of_class (tree t, eoc_mode mode)
6716 tree result = size_zero_node;
6717 vec<tree, va_gc> *vbases;
6718 tree binfo;
6719 tree base_binfo;
6720 tree offset;
6721 int i;
6723 for (binfo = TYPE_BINFO (t), i = 0;
6724 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6726 if (BINFO_VIRTUAL_P (base_binfo)
6727 && (!BINFO_PRIMARY_P (base_binfo)
6728 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6729 continue;
6731 offset = end_of_base (base_binfo);
6732 if (tree_int_cst_lt (result, offset))
6733 result = offset;
6736 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6737 if (TREE_CODE (field) == FIELD_DECL
6738 && !DECL_FIELD_IS_BASE (field))
6740 tree size = DECL_SIZE_UNIT (field);
6741 if (!size)
6742 /* DECL_SIZE_UNIT can be null for a flexible array. */
6743 continue;
6745 if (is_empty_field (field))
6746 /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6747 size of the type (usually 1) for computing nvsize. */
6748 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6750 if (DECL_BIT_FIELD_TYPE (field))
6752 offset = size_binop (PLUS_EXPR, bit_position (field),
6753 DECL_SIZE (field));
6754 offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6755 offset = fold_convert (sizetype, offset);
6757 else
6758 offset = size_binop (PLUS_EXPR, byte_position (field), size);
6759 if (tree_int_cst_lt (result, offset))
6760 result = offset;
6763 if (mode != eoc_nvsize)
6764 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6765 vec_safe_iterate (vbases, i, &base_binfo); i++)
6767 if (mode == eoc_nv_or_dsize)
6768 /* For dsize, don't count trailing empty bases. */
6769 offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6770 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6771 else
6772 offset = end_of_base (base_binfo);
6773 if (tree_int_cst_lt (result, offset))
6774 result = offset;
6777 return result;
6780 /* Warn as appropriate about the change in whether we pack into the tail
6781 padding of FIELD, a base field which has a C++14 aggregate type with default
6782 member initializers. */
6784 static void
6785 check_non_pod_aggregate (tree field)
6787 if (!abi_version_crosses (17) || cxx_dialect < cxx14)
6788 return;
6789 if (TREE_CODE (field) != FIELD_DECL
6790 || (!DECL_FIELD_IS_BASE (field)
6791 && !field_poverlapping_p (field)))
6792 return;
6793 tree next = DECL_CHAIN (field);
6794 while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6795 if (!next)
6796 return;
6797 tree type = TREE_TYPE (field);
6798 if (TYPE_IDENTIFIER (type) == as_base_identifier)
6799 type = TYPE_CONTEXT (type);
6800 if (!CLASS_TYPE_P (type) || !CLASSTYPE_NON_POD_AGGREGATE (type))
6801 return;
6802 tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6803 ? eoc_nvsize : eoc_nv_or_dsize));
6804 tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6805 if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6807 location_t loc = DECL_SOURCE_LOCATION (next);
6808 if (DECL_FIELD_IS_BASE (next))
6809 warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6810 "%<-std=c++14%> and up changes in "
6811 "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6812 else
6813 warning_at (loc, OPT_Wabi, "offset of %qD for "
6814 "%<-std=c++14%> and up changes in "
6815 "%<-fabi-version=17%> (GCC 12)", next);
6819 /* Warn about bases of T that are inaccessible because they are
6820 ambiguous. For example:
6822 struct S {};
6823 struct T : public S {};
6824 struct U : public S, public T {};
6826 Here, `(S*) new U' is not allowed because there are two `S'
6827 subobjects of U. */
6829 static void
6830 maybe_warn_about_inaccessible_bases (tree t)
6832 int i;
6833 vec<tree, va_gc> *vbases;
6834 tree basetype;
6835 tree binfo;
6836 tree base_binfo;
6838 /* If not checking for warning then return early. */
6839 if (!warn_inaccessible_base)
6840 return;
6842 /* If there are no repeated bases, nothing can be ambiguous. */
6843 if (!CLASSTYPE_REPEATED_BASE_P (t))
6844 return;
6846 /* Check direct bases. */
6847 for (binfo = TYPE_BINFO (t), i = 0;
6848 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6850 basetype = BINFO_TYPE (base_binfo);
6852 if (!uniquely_derived_from_p (basetype, t))
6853 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6854 "in %qT due to ambiguity", basetype, t);
6857 /* Check for ambiguous virtual bases. */
6858 if (extra_warnings)
6859 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6860 vec_safe_iterate (vbases, i, &binfo); i++)
6862 basetype = BINFO_TYPE (binfo);
6864 if (!uniquely_derived_from_p (basetype, t))
6865 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6866 "%qT due to ambiguity", basetype, t);
6870 /* Compare two INTEGER_CSTs K1 and K2. */
6872 static int
6873 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6875 return tree_int_cst_compare ((tree) k1, (tree) k2);
6878 /* Increase the size indicated in RLI to account for empty classes
6879 that are "off the end" of the class. */
6881 static void
6882 include_empty_classes (record_layout_info rli)
6884 tree eoc;
6885 tree rli_size;
6887 /* It might be the case that we grew the class to allocate a
6888 zero-sized base class. That won't be reflected in RLI, yet,
6889 because we are willing to overlay multiple bases at the same
6890 offset. However, now we need to make sure that RLI is big enough
6891 to reflect the entire class. */
6892 eoc = end_of_class (rli->t, eoc_vsize);
6893 rli_size = rli_size_unit_so_far (rli);
6894 if (TREE_CODE (rli_size) == INTEGER_CST
6895 && tree_int_cst_lt (rli_size, eoc))
6897 /* The size should have been rounded to a whole byte. */
6898 gcc_assert (tree_int_cst_equal
6899 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6900 rli->bitpos
6901 = size_binop (PLUS_EXPR,
6902 rli->bitpos,
6903 size_binop (MULT_EXPR,
6904 fold_convert (bitsizetype,
6905 size_binop (MINUS_EXPR,
6906 eoc, rli_size)),
6907 bitsize_int (BITS_PER_UNIT)));
6908 normalize_rli (rli);
6912 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6913 BINFO_OFFSETs for all of the base-classes. Position the vtable
6914 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6916 static void
6917 layout_class_type (tree t, tree *virtuals_p)
6919 tree non_static_data_members;
6920 tree field;
6921 tree vptr;
6922 record_layout_info rli;
6923 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6924 types that appear at that offset. */
6925 splay_tree empty_base_offsets;
6926 /* True if the last field laid out was a bit-field. */
6927 bool last_field_was_bitfield = false;
6928 /* The location at which the next field should be inserted. */
6929 tree *next_field;
6931 /* Keep track of the first non-static data member. */
6932 non_static_data_members = TYPE_FIELDS (t);
6934 /* Start laying out the record. */
6935 rli = start_record_layout (t);
6937 /* Mark all the primary bases in the hierarchy. */
6938 determine_primary_bases (t);
6940 /* Create a pointer to our virtual function table. */
6941 vptr = create_vtable_ptr (t, virtuals_p);
6943 /* The vptr is always the first thing in the class. */
6944 if (vptr)
6946 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6947 TYPE_FIELDS (t) = vptr;
6948 next_field = &DECL_CHAIN (vptr);
6949 place_field (rli, vptr);
6951 else
6952 next_field = &TYPE_FIELDS (t);
6954 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6955 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6956 NULL, NULL);
6957 build_base_fields (rli, empty_base_offsets, next_field);
6959 /* Layout the non-static data members. */
6960 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6962 tree type;
6963 tree padding;
6965 /* We still pass things that aren't non-static data members to
6966 the back end, in case it wants to do something with them. */
6967 if (TREE_CODE (field) != FIELD_DECL)
6969 place_field (rli, field);
6970 /* If the static data member has incomplete type, keep track
6971 of it so that it can be completed later. (The handling
6972 of pending statics in finish_record_layout is
6973 insufficient; consider:
6975 struct S1;
6976 struct S2 { static S1 s1; };
6978 At this point, finish_record_layout will be called, but
6979 S1 is still incomplete.) */
6980 if (VAR_P (field))
6982 maybe_register_incomplete_var (field);
6983 /* The visibility of static data members is determined
6984 at their point of declaration, not their point of
6985 definition. */
6986 determine_visibility (field);
6988 continue;
6991 type = TREE_TYPE (field);
6992 if (type == error_mark_node)
6993 continue;
6995 padding = NULL_TREE;
6997 bool might_overlap = field_poverlapping_p (field);
6999 if (might_overlap && CLASS_TYPE_P (type)
7000 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
7002 /* if D is a potentially-overlapping data member, update sizeof(C) to
7003 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
7004 if (CLASSTYPE_EMPTY_P (type))
7005 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
7006 else
7008 tree size = end_of_class (type, eoc_nv_or_dsize);
7009 DECL_SIZE_UNIT (field) = size;
7010 DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
7014 /* If this field is a bit-field whose width is greater than its
7015 type, then there are some special rules for allocating
7016 it. */
7017 if (DECL_C_BIT_FIELD (field)
7018 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
7020 bool was_unnamed_p = false;
7021 /* We must allocate the bits as if suitably aligned for the
7022 longest integer type that fits in this many bits. Then,
7023 we are supposed to use the left over bits as additional
7024 padding. */
7026 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
7027 tree limit = size_int (MAX_FIXED_MODE_SIZE);
7028 if (tree_int_cst_lt (DECL_SIZE (field), limit))
7029 limit = DECL_SIZE (field);
7031 tree integer_type = integer_types[itk_char];
7032 for (unsigned itk = itk_char; itk != itk_none; itk++)
7033 if (tree next = integer_types[itk])
7035 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
7036 /* Too big, so our current guess is what we want. */
7037 break;
7038 /* Not bigger than limit, ok */
7039 integer_type = next;
7042 /* Figure out how much additional padding is required. */
7043 if (TREE_CODE (t) == UNION_TYPE)
7044 /* In a union, the padding field must have the full width
7045 of the bit-field; all fields start at offset zero. */
7046 padding = DECL_SIZE (field);
7047 else
7048 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
7049 TYPE_SIZE (integer_type));
7051 if (integer_zerop (padding))
7052 padding = NULL_TREE;
7054 /* An unnamed bitfield does not normally affect the
7055 alignment of the containing class on a target where
7056 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
7057 make any exceptions for unnamed bitfields when the
7058 bitfields are longer than their types. Therefore, we
7059 temporarily give the field a name. */
7060 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
7062 was_unnamed_p = true;
7063 DECL_NAME (field) = make_anon_name ();
7066 DECL_SIZE (field) = TYPE_SIZE (integer_type);
7067 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
7068 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
7069 layout_nonempty_base_or_field (rli, field, NULL_TREE,
7070 empty_base_offsets);
7071 if (was_unnamed_p)
7072 DECL_NAME (field) = NULL_TREE;
7073 /* Now that layout has been performed, set the size of the
7074 field to the size of its declared type; the rest of the
7075 field is effectively invisible. */
7076 DECL_SIZE (field) = TYPE_SIZE (type);
7077 /* We must also reset the DECL_MODE of the field. */
7078 SET_DECL_MODE (field, TYPE_MODE (type));
7080 else if (might_overlap && is_empty_class (type))
7082 SET_DECL_FIELD_ABI_IGNORED (field, 1);
7083 layout_empty_base_or_field (rli, field, empty_base_offsets);
7085 else
7086 layout_nonempty_base_or_field (rli, field, NULL_TREE,
7087 empty_base_offsets);
7089 /* Remember the location of any empty classes in FIELD. */
7090 record_subobject_offsets (field, empty_base_offsets);
7092 /* If a bit-field does not immediately follow another bit-field,
7093 and yet it starts in the middle of a byte, we have failed to
7094 comply with the ABI. */
7095 if (warn_abi
7096 && DECL_C_BIT_FIELD (field)
7097 /* The TREE_NO_WARNING flag gets set by Objective-C when
7098 laying out an Objective-C class. The ObjC ABI differs
7099 from the C++ ABI, and so we do not want a warning
7100 here. */
7101 && !warning_suppressed_p (field, OPT_Wabi)
7102 && !last_field_was_bitfield
7103 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
7104 DECL_FIELD_BIT_OFFSET (field),
7105 bitsize_unit_node)))
7106 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
7107 "offset of %qD is not ABI-compliant and may "
7108 "change in a future version of GCC", field);
7110 /* The middle end uses the type of expressions to determine the
7111 possible range of expression values. In order to optimize
7112 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
7113 must be made aware of the width of "i", via its type.
7115 Because C++ does not have integer types of arbitrary width,
7116 we must (for the purposes of the front end) convert from the
7117 type assigned here to the declared type of the bitfield
7118 whenever a bitfield expression is used as an rvalue.
7119 Similarly, when assigning a value to a bitfield, the value
7120 must be converted to the type given the bitfield here. */
7121 if (DECL_C_BIT_FIELD (field))
7123 unsigned HOST_WIDE_INT width;
7124 tree ftype = TREE_TYPE (field);
7125 width = tree_to_uhwi (DECL_SIZE (field));
7126 if (width != TYPE_PRECISION (ftype))
7128 TREE_TYPE (field)
7129 = c_build_bitfield_integer_type (width,
7130 TYPE_UNSIGNED (ftype));
7131 TREE_TYPE (field)
7132 = cp_build_qualified_type (TREE_TYPE (field),
7133 cp_type_quals (ftype));
7137 /* If we needed additional padding after this field, add it
7138 now. */
7139 if (padding)
7141 tree padding_field;
7143 padding_field = build_decl (input_location,
7144 FIELD_DECL,
7145 NULL_TREE,
7146 char_type_node);
7147 DECL_BIT_FIELD (padding_field) = 1;
7148 DECL_SIZE (padding_field) = padding;
7149 DECL_CONTEXT (padding_field) = t;
7150 DECL_ARTIFICIAL (padding_field) = 1;
7151 DECL_IGNORED_P (padding_field) = 1;
7152 DECL_PADDING_P (padding_field) = 1;
7153 layout_nonempty_base_or_field (rli, padding_field,
7154 NULL_TREE,
7155 empty_base_offsets);
7158 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
7161 if (!integer_zerop (rli->bitpos))
7163 /* Make sure that we are on a byte boundary so that the size of
7164 the class without virtual bases will always be a round number
7165 of bytes. */
7166 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
7167 normalize_rli (rli);
7170 /* We used to remove zero width bitfields at this point since PR42217,
7171 while the C FE never did that. That caused ABI differences on various
7172 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
7173 instead, so that the backends can emit -Wpsabi warnings in the cases
7174 where the ABI changed. */
7175 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7177 if (TREE_CODE (field) == FIELD_DECL
7178 && DECL_C_BIT_FIELD (field)
7179 /* We should not be confused by the fact that grokbitfield
7180 temporarily sets the width of the bit field into
7181 DECL_BIT_FIELD_REPRESENTATIVE (field).
7182 check_bitfield_decl eventually sets DECL_SIZE (field)
7183 to that width. */
7184 && (DECL_SIZE (field) == NULL_TREE
7185 || integer_zerop (DECL_SIZE (field)))
7186 && TREE_TYPE (field) != error_mark_node)
7187 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
7188 check_non_pod_aggregate (field);
7191 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
7193 /* T needs a different layout as a base (eliding virtual bases
7194 or whatever). Create that version. */
7195 tree base_t = make_node (TREE_CODE (t));
7196 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
7198 TYPE_CONTEXT (base_t) = t;
7199 DECL_CONTEXT (base_d) = t;
7201 set_instantiating_module (base_d);
7203 /* If the ABI version is not at least two, and the last
7204 field was a bit-field, RLI may not be on a byte
7205 boundary. In particular, rli_size_unit_so_far might
7206 indicate the last complete byte, while rli_size_so_far
7207 indicates the total number of bits used. Therefore,
7208 rli_size_so_far, rather than rli_size_unit_so_far, is
7209 used to compute TYPE_SIZE_UNIT. */
7211 /* Set the size and alignment for the new type. */
7212 tree eoc = end_of_class (t, eoc_nvsize);
7213 TYPE_SIZE_UNIT (base_t)
7214 = size_binop (MAX_EXPR,
7215 fold_convert (sizetype,
7216 size_binop (CEIL_DIV_EXPR,
7217 rli_size_so_far (rli),
7218 bitsize_int (BITS_PER_UNIT))),
7219 eoc);
7220 TYPE_SIZE (base_t)
7221 = size_binop (MAX_EXPR,
7222 rli_size_so_far (rli),
7223 size_binop (MULT_EXPR,
7224 fold_convert (bitsizetype, eoc),
7225 bitsize_int (BITS_PER_UNIT)));
7226 SET_TYPE_ALIGN (base_t, rli->record_align);
7227 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
7228 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
7229 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
7231 /* Copy the non-static data members of T. This will include its
7232 direct non-virtual bases & vtable. */
7233 next_field = &TYPE_FIELDS (base_t);
7234 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7235 if (TREE_CODE (field) == FIELD_DECL)
7237 *next_field = copy_node (field);
7238 /* Zap any NSDMI, it's not needed and might be a deferred
7239 parse. */
7240 DECL_INITIAL (*next_field) = NULL_TREE;
7241 DECL_CONTEXT (*next_field) = base_t;
7242 next_field = &DECL_CHAIN (*next_field);
7244 *next_field = NULL_TREE;
7246 /* We use the base type for trivial assignments, and hence it
7247 needs a mode. */
7248 compute_record_mode (base_t);
7250 /* Record the base version of the type. */
7251 CLASSTYPE_AS_BASE (t) = base_t;
7253 else
7254 CLASSTYPE_AS_BASE (t) = t;
7256 /* Every empty class contains an empty class. */
7257 if (CLASSTYPE_EMPTY_P (t))
7258 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7260 /* Set the TYPE_DECL for this type to contain the right
7261 value for DECL_OFFSET, so that we can use it as part
7262 of a COMPONENT_REF for multiple inheritance. */
7263 layout_decl (TYPE_MAIN_DECL (t), 0);
7265 /* Now fix up any virtual base class types that we left lying
7266 around. We must get these done before we try to lay out the
7267 virtual function table. As a side-effect, this will remove the
7268 base subobject fields. */
7269 layout_virtual_bases (rli, empty_base_offsets);
7271 /* Make sure that empty classes are reflected in RLI at this
7272 point. */
7273 include_empty_classes (rli);
7275 /* Make sure not to create any structures with zero size. */
7276 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7277 place_field (rli,
7278 build_decl (input_location,
7279 FIELD_DECL, NULL_TREE, char_type_node));
7281 /* If this is a non-POD, declaring it packed makes a difference to how it
7282 can be used as a field; don't let finalize_record_size undo it. */
7283 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7284 rli->packed_maybe_necessary = true;
7286 /* Let the back end lay out the type. */
7287 finish_record_layout (rli, /*free_p=*/true);
7289 /* If we didn't end up needing an as-base type, don't use it. */
7290 if (CLASSTYPE_AS_BASE (t) != t
7291 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7292 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7293 causing us to lose the user-specified alignment as in PR94050. */
7294 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7295 && tree_int_cst_equal (TYPE_SIZE (t),
7296 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7297 CLASSTYPE_AS_BASE (t) = t;
7299 if (TYPE_SIZE_UNIT (t)
7300 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7301 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7302 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7303 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7305 /* Warn about bases that can't be talked about due to ambiguity. */
7306 maybe_warn_about_inaccessible_bases (t);
7308 /* Now that we're done with layout, give the base fields the real types. */
7309 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7310 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7311 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7313 /* Clean up. */
7314 splay_tree_delete (empty_base_offsets);
7316 if (CLASSTYPE_EMPTY_P (t)
7317 && tree_int_cst_lt (sizeof_biggest_empty_class,
7318 TYPE_SIZE_UNIT (t)))
7319 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7322 /* Determine the "key method" for the class type indicated by TYPE,
7323 and set CLASSTYPE_KEY_METHOD accordingly. */
7325 void
7326 determine_key_method (tree type)
7328 tree method;
7330 if (processing_template_decl
7331 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7332 || CLASSTYPE_INTERFACE_KNOWN (type))
7333 return;
7335 /* The key method is the first non-pure virtual function that is not
7336 inline at the point of class definition. On some targets the
7337 key function may not be inline; those targets should not call
7338 this function until the end of the translation unit. */
7339 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7340 if (TREE_CODE (method) == FUNCTION_DECL
7341 && DECL_VINDEX (method) != NULL_TREE
7342 && ! DECL_DECLARED_INLINE_P (method)
7343 && ! DECL_PURE_VIRTUAL_P (method))
7345 CLASSTYPE_KEY_METHOD (type) = method;
7346 break;
7349 return;
7352 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7353 class data member of non-zero size, otherwise false. */
7355 static inline bool
7356 field_nonempty_p (const_tree fld)
7358 if (TREE_CODE (fld) == ERROR_MARK)
7359 return false;
7361 tree type = TREE_TYPE (fld);
7362 if (TREE_CODE (fld) == FIELD_DECL
7363 && TREE_CODE (type) != ERROR_MARK
7364 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7366 return TYPE_SIZE (type)
7367 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
7368 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
7371 return false;
7374 /* Used by find_flexarrays and related functions. */
7376 struct flexmems_t
7378 /* The first flexible array member or non-zero array member found
7379 in the order of layout. */
7380 tree array;
7381 /* First non-static non-empty data member in the class or its bases. */
7382 tree first;
7383 /* The first non-static non-empty data member following either
7384 the flexible array member, if found, or the zero-length array member
7385 otherwise. AFTER[1] refers to the first such data member of a union
7386 of which the struct containing the flexible array member or zero-length
7387 array is a member, or NULL when no such union exists. This element is
7388 only used during searching, not for diagnosing problems. AFTER[0]
7389 refers to the first such data member that is not a member of such
7390 a union. */
7391 tree after[2];
7393 /* Refers to a struct (not union) in which the struct of which the flexible
7394 array is member is defined. Used to diagnose strictly (according to C)
7395 invalid uses of the latter structs. */
7396 tree enclosing;
7399 /* Find either the first flexible array member or the first zero-length
7400 array, in that order of preference, among members of class T (but not
7401 its base classes), and set members of FMEM accordingly.
7402 BASE_P is true if T is a base class of another class.
7403 PUN is set to the outermost union in which the flexible array member
7404 (or zero-length array) is defined if one such union exists, otherwise
7405 to NULL.
7406 Similarly, PSTR is set to a data member of the outermost struct of
7407 which the flexible array is a member if one such struct exists,
7408 otherwise to NULL. */
7410 static void
7411 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7412 tree pun /* = NULL_TREE */,
7413 tree pstr /* = NULL_TREE */)
7415 /* Set the "pointer" to the outermost enclosing union if not set
7416 yet and maintain it for the remainder of the recursion. */
7417 if (!pun && TREE_CODE (t) == UNION_TYPE)
7418 pun = t;
7420 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
7422 if (fld == error_mark_node)
7423 return;
7425 /* Is FLD a typedef for an anonymous struct? */
7427 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7428 handled elsewhere so that errors like the following are detected
7429 as well:
7430 typedef struct { int i, a[], j; } S; // bug c++/72753
7431 S s [2]; // bug c++/68489
7433 if (TREE_CODE (fld) == TYPE_DECL
7434 && DECL_IMPLICIT_TYPEDEF_P (fld)
7435 && CLASS_TYPE_P (TREE_TYPE (fld))
7436 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7438 /* Check the nested unnamed type referenced via a typedef
7439 independently of FMEM (since it's not a data member of
7440 the enclosing class). */
7441 check_flexarrays (TREE_TYPE (fld));
7442 continue;
7445 /* Skip anything that's GCC-generated or not a (non-static) data
7446 member. */
7447 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7448 continue;
7450 /* Type of the member. */
7451 tree fldtype = TREE_TYPE (fld);
7452 if (fldtype == error_mark_node)
7453 return;
7455 /* Determine the type of the array element or object referenced
7456 by the member so that it can be checked for flexible array
7457 members if it hasn't been yet. */
7458 tree eltype = fldtype;
7459 while (TREE_CODE (eltype) == ARRAY_TYPE
7460 || INDIRECT_TYPE_P (eltype))
7461 eltype = TREE_TYPE (eltype);
7463 if (RECORD_OR_UNION_TYPE_P (eltype))
7465 if (fmem->array && !fmem->after[bool (pun)])
7467 /* Once the member after the flexible array has been found
7468 we're done. */
7469 fmem->after[bool (pun)] = fld;
7470 break;
7473 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7475 /* Descend into the non-static member struct or union and try
7476 to find a flexible array member or zero-length array among
7477 its members. This is only necessary for anonymous types
7478 and types in whose context the current type T has not been
7479 defined (the latter must not be checked again because they
7480 are already in the process of being checked by one of the
7481 recursive calls). */
7483 tree first = fmem->first;
7484 tree array = fmem->array;
7486 /* If this member isn't anonymous and a prior non-flexible array
7487 member has been seen in one of the enclosing structs, clear
7488 the FIRST member since it doesn't contribute to the flexible
7489 array struct's members. */
7490 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7491 fmem->first = NULL_TREE;
7493 find_flexarrays (eltype, fmem, false, pun,
7494 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7496 if (fmem->array != array)
7497 continue;
7499 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7501 /* Restore the FIRST member reset above if no flexible
7502 array member has been found in this member's struct. */
7503 fmem->first = first;
7506 /* If the member struct contains the first flexible array
7507 member, or if this member is a base class, continue to
7508 the next member and avoid setting the FMEM->NEXT pointer
7509 to point to it. */
7510 if (base_p)
7511 continue;
7515 if (field_nonempty_p (fld))
7517 /* Remember the first non-static data member. */
7518 if (!fmem->first)
7519 fmem->first = fld;
7521 /* Remember the first non-static data member after the flexible
7522 array member, if one has been found, or the zero-length array
7523 if it has been found. */
7524 if (fmem->array && !fmem->after[bool (pun)])
7525 fmem->after[bool (pun)] = fld;
7528 /* Skip non-arrays. */
7529 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7530 continue;
7532 /* Determine the upper bound of the array if it has one. */
7533 if (TYPE_DOMAIN (fldtype))
7535 if (fmem->array)
7537 /* Make a record of the zero-length array if either one
7538 such field or a flexible array member has been seen to
7539 handle the pathological and unlikely case of multiple
7540 such members. */
7541 if (!fmem->after[bool (pun)])
7542 fmem->after[bool (pun)] = fld;
7544 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7546 /* Remember the first zero-length array unless a flexible array
7547 member has already been seen. */
7548 fmem->array = fld;
7549 fmem->enclosing = pstr;
7552 else
7554 /* Flexible array members have no upper bound. */
7555 if (fmem->array)
7557 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7559 /* Replace the zero-length array if it's been stored and
7560 reset the after pointer. */
7561 fmem->after[bool (pun)] = NULL_TREE;
7562 fmem->array = fld;
7563 fmem->enclosing = pstr;
7565 else if (!fmem->after[bool (pun)])
7566 /* Make a record of another flexible array member. */
7567 fmem->after[bool (pun)] = fld;
7569 else
7571 fmem->array = fld;
7572 fmem->enclosing = pstr;
7578 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7579 a flexible array member (or the zero-length array extension). */
7581 static void
7582 diagnose_invalid_flexarray (const flexmems_t *fmem)
7584 if (fmem->array && fmem->enclosing)
7586 auto_diagnostic_group d;
7587 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7588 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7589 ? G_("invalid use of %q#T with a zero-size array "
7590 "in %q#D")
7591 : G_("invalid use of %q#T with a flexible array member "
7592 "in %q#T"),
7593 DECL_CONTEXT (fmem->array),
7594 DECL_CONTEXT (fmem->enclosing)))
7595 inform (DECL_SOURCE_LOCATION (fmem->array),
7596 "array member %q#D declared here", fmem->array);
7600 /* Issue diagnostics for invalid flexible array members or zero-length
7601 arrays that are not the last elements of the containing class or its
7602 base classes or that are its sole members. */
7604 static void
7605 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7607 if (!fmem->array)
7608 return;
7610 if (fmem->first && !fmem->after[0])
7612 diagnose_invalid_flexarray (fmem);
7613 return;
7616 /* Has a diagnostic been issued? */
7617 bool diagd = false;
7619 const char *msg = 0;
7620 const char *msg_fam = 0;
7622 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7624 if (fmem->after[0])
7625 msg = G_("zero-size array member %qD not at end of %q#T");
7626 else if (!fmem->first)
7627 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7629 if (msg)
7631 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7633 auto_diagnostic_group d;
7634 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7636 inform (location_of (t), "in the definition of %q#T", t);
7637 diagd = true;
7641 else
7643 if (fmem->after[0])
7644 msg = G_("flexible array member %qD not at end of %q#T");
7645 else if (!fmem->first)
7646 msg_fam = G_("flexible array member %qD in an otherwise"
7647 " empty %q#T is a GCC extension");
7649 if (msg || msg_fam)
7651 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7652 diagd = true;
7654 auto_diagnostic_group d;
7655 if (msg)
7656 error_at (loc, msg, fmem->array, t);
7657 else
7658 pedwarn (loc, OPT_Wpedantic, msg_fam, fmem->array, t);
7660 /* In the unlikely event that the member following the flexible
7661 array member is declared in a different class, or the member
7662 overlaps another member of a common union, point to it.
7663 Otherwise it should be obvious. */
7664 if (fmem->after[0]
7665 && ((DECL_CONTEXT (fmem->after[0])
7666 != DECL_CONTEXT (fmem->array))))
7668 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7669 "next member %q#D declared here",
7670 fmem->after[0]);
7671 inform (location_of (t), "in the definition of %q#T", t);
7676 if (!diagd && fmem->array && fmem->enclosing)
7677 diagnose_invalid_flexarray (fmem);
7681 /* Recursively check to make sure that any flexible array or zero-length
7682 array members of class T or its bases are valid (i.e., not the sole
7683 non-static data member of T and, if one exists, that it is the last
7684 non-static data member of T and its base classes. FMEM is expected
7685 to be initially null and is used internally by recursive calls to
7686 the function. Issue the appropriate diagnostics for the array member
7687 that fails the checks. */
7689 static void
7690 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7691 bool base_p /* = false */)
7693 /* Initialize the result of a search for flexible array and zero-length
7694 array members. Avoid doing any work if the most interesting FMEM data
7695 have already been populated. */
7696 flexmems_t flexmems = flexmems_t ();
7697 if (!fmem)
7698 fmem = &flexmems;
7699 else if (fmem->array && fmem->first && fmem->after[0])
7700 return;
7702 tree fam = fmem->array;
7704 /* Recursively check the primary base class first. */
7705 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7707 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7708 check_flexarrays (basetype, fmem, true);
7711 /* Recursively check the base classes. */
7712 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7713 for (int i = 0; i < nbases; ++i)
7715 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7717 /* The primary base class was already checked above. */
7718 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7719 continue;
7721 /* Virtual base classes are at the end. */
7722 if (BINFO_VIRTUAL_P (base_binfo))
7723 continue;
7725 /* Check the base class. */
7726 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7729 if (fmem == &flexmems)
7731 /* Check virtual base classes only once per derived class.
7732 I.e., this check is not performed recursively for base
7733 classes. */
7734 int i;
7735 tree base_binfo;
7736 vec<tree, va_gc> *vbases;
7737 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7738 vec_safe_iterate (vbases, i, &base_binfo); i++)
7740 /* Check the virtual base class. */
7741 tree basetype = TREE_TYPE (base_binfo);
7743 check_flexarrays (basetype, fmem, /*base_p=*/true);
7747 /* Is the type unnamed (and therefore a member of it potentially
7748 an anonymous struct or union)? */
7749 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7750 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7751 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7753 /* Search the members of the current (possibly derived) class, skipping
7754 unnamed structs and unions since those could be anonymous. */
7755 if (fmem != &flexmems || !maybe_anon_p)
7756 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7758 if (fmem == &flexmems && !maybe_anon_p)
7760 /* Issue diagnostics for invalid flexible and zero-length array
7761 members found in base classes or among the members of the current
7762 class. Ignore anonymous structs and unions whose members are
7763 considered to be members of the enclosing class and thus will
7764 be diagnosed when checking it. */
7765 diagnose_flexarrays (t, fmem);
7769 /* Perform processing required when the definition of T (a class type)
7770 is complete. Diagnose invalid definitions of flexible array members
7771 and zero-size arrays. */
7773 void
7774 finish_struct_1 (tree t)
7776 tree x;
7777 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7778 tree virtuals = NULL_TREE;
7780 if (COMPLETE_TYPE_P (t))
7782 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7783 error ("redefinition of %q#T", t);
7784 popclass ();
7785 return;
7788 /* If this type was previously laid out as a forward reference,
7789 make sure we lay it out again. */
7790 TYPE_SIZE (t) = NULL_TREE;
7791 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7793 /* Make assumptions about the class; we'll reset the flags if
7794 necessary. */
7795 CLASSTYPE_EMPTY_P (t) = 1;
7796 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7797 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7798 CLASSTYPE_LITERAL_P (t) = true;
7800 /* Do end-of-class semantic processing: checking the validity of the
7801 bases and members and add implicitly generated methods. */
7802 check_bases_and_members (t);
7804 /* Find the key method. */
7805 if (TYPE_CONTAINS_VPTR_P (t))
7807 /* The Itanium C++ ABI permits the key method to be chosen when
7808 the class is defined -- even though the key method so
7809 selected may later turn out to be an inline function. On
7810 some systems (such as ARM Symbian OS) the key method cannot
7811 be determined until the end of the translation unit. On such
7812 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7813 will cause the class to be added to KEYED_CLASSES. Then, in
7814 finish_file we will determine the key method. */
7815 if (targetm.cxx.key_method_may_be_inline ())
7816 determine_key_method (t);
7818 /* If a polymorphic class has no key method, we may emit the vtable
7819 in every translation unit where the class definition appears. If
7820 we're devirtualizing, we can look into the vtable even if we
7821 aren't emitting it.
7823 Additionally, if the class is attached to a named module, make sure
7824 to always emit the vtable in this TU. */
7825 if (!CLASSTYPE_KEY_METHOD (t) || module_attach_p ())
7826 vec_safe_push (keyed_classes, t);
7829 /* Layout the class itself. */
7830 layout_class_type (t, &virtuals);
7831 /* COMPLETE_TYPE_P is now true. */
7833 set_class_bindings (t);
7835 /* With the layout complete, check for flexible array members and
7836 zero-length arrays that might overlap other members in the final
7837 layout. */
7838 check_flexarrays (t);
7840 virtuals = modify_all_vtables (t, nreverse (virtuals));
7842 /* If necessary, create the primary vtable for this class. */
7843 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7845 /* We must enter these virtuals into the table. */
7846 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7847 build_primary_vtable (NULL_TREE, t);
7848 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7849 /* Here we know enough to change the type of our virtual
7850 function table, but we will wait until later this function. */
7851 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7853 /* If we're warning about ABI tags, check the types of the new
7854 virtual functions. */
7855 if (warn_abi_tag)
7856 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7857 check_abi_tags (t, TREE_VALUE (v));
7860 if (TYPE_CONTAINS_VPTR_P (t))
7862 int vindex;
7863 tree fn;
7865 if (BINFO_VTABLE (TYPE_BINFO (t)))
7866 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7867 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7868 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7870 /* Add entries for virtual functions introduced by this class. */
7871 BINFO_VIRTUALS (TYPE_BINFO (t))
7872 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7874 /* Set DECL_VINDEX for all functions declared in this class. */
7875 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7877 fn = TREE_CHAIN (fn),
7878 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7879 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7881 tree fndecl = BV_FN (fn);
7883 if (DECL_THUNK_P (fndecl))
7884 /* A thunk. We should never be calling this entry directly
7885 from this vtable -- we'd use the entry for the non
7886 thunk base function. */
7887 DECL_VINDEX (fndecl) = NULL_TREE;
7888 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7889 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7893 finish_struct_bits (t);
7895 set_method_tm_attributes (t);
7896 if (flag_openmp || flag_openmp_simd)
7897 finish_omp_declare_simd_methods (t);
7899 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7900 for any static member objects of the type we're working on. */
7901 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7902 if (DECL_DECLARES_FUNCTION_P (x))
7904 /* Synthesize constexpr defaulted comparisons. */
7905 if (!DECL_ARTIFICIAL (x)
7906 && DECL_DEFAULTED_IN_CLASS_P (x)
7907 && special_function_p (x) == sfk_comparison)
7908 defaulted_late_check (x);
7909 DECL_IN_AGGR_P (x) = false;
7911 else if (VAR_P (x) && TREE_STATIC (x)
7912 && TREE_TYPE (x) != error_mark_node
7913 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7914 SET_DECL_MODE (x, TYPE_MODE (t));
7916 /* Complain if one of the field types requires lower visibility. */
7917 constrain_class_visibility (t);
7919 /* Make the rtl for any new vtables we have created, and unmark
7920 the base types we marked. */
7921 finish_vtbls (t);
7923 /* Build the VTT for T. */
7924 build_vtt (t);
7926 if (warn_nonvdtor
7927 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7928 && !CLASSTYPE_FINAL (t))
7929 warning (OPT_Wnon_virtual_dtor,
7930 "%q#T has virtual functions and accessible"
7931 " non-virtual destructor", t);
7933 complete_vars (t);
7935 if (warn_overloaded_virtual)
7936 warn_hidden (t);
7938 /* Class layout, assignment of virtual table slots, etc., is now
7939 complete. Give the back end a chance to tweak the visibility of
7940 the class or perform any other required target modifications. */
7941 targetm.cxx.adjust_class_at_definition (t);
7943 maybe_suppress_debug_info (t);
7945 if (flag_vtable_verify)
7946 vtv_save_class_info (t);
7948 dump_class_hierarchy (t);
7950 /* Finish debugging output for this type. */
7951 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7953 if (TYPE_TRANSPARENT_AGGR (t))
7955 tree field = first_field (t);
7956 if (field == NULL_TREE || error_operand_p (field))
7958 error ("type transparent %q#T does not have any fields", t);
7959 TYPE_TRANSPARENT_AGGR (t) = 0;
7961 else if (DECL_ARTIFICIAL (field))
7963 if (DECL_FIELD_IS_BASE (field))
7964 error ("type transparent class %qT has base classes", t);
7965 else
7967 gcc_checking_assert (DECL_VIRTUAL_P (field));
7968 error ("type transparent class %qT has virtual functions", t);
7970 TYPE_TRANSPARENT_AGGR (t) = 0;
7972 else if (TYPE_MODE (t) != DECL_MODE (field))
7974 error ("type transparent %q#T cannot be made transparent because "
7975 "the type of the first field has a different ABI from the "
7976 "class overall", t);
7977 TYPE_TRANSPARENT_AGGR (t) = 0;
7982 /* When T was built up, the member declarations were added in reverse
7983 order. Rearrange them to declaration order. */
7985 void
7986 unreverse_member_declarations (tree t)
7988 tree next;
7989 tree prev;
7990 tree x;
7992 /* The following lists are all in reverse order. Put them in
7993 declaration order now. */
7994 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7996 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7997 order, so we can't just use nreverse. Due to stat_hack
7998 chicanery in finish_member_declaration. */
7999 prev = NULL_TREE;
8000 for (x = TYPE_FIELDS (t);
8001 x && TREE_CODE (x) != TYPE_DECL;
8002 x = next)
8004 next = DECL_CHAIN (x);
8005 DECL_CHAIN (x) = prev;
8006 prev = x;
8009 if (prev)
8011 DECL_CHAIN (TYPE_FIELDS (t)) = x;
8012 TYPE_FIELDS (t) = prev;
8016 /* Classes, structs or unions T marked with hotness attributes propagate
8017 the attribute to all methods. */
8019 void
8020 propagate_class_warmth_attribute (tree t)
8022 if (t == NULL_TREE
8023 || !(TREE_CODE (t) == RECORD_TYPE
8024 || TREE_CODE (t) == UNION_TYPE))
8025 return;
8027 tree class_has_cold_attr
8028 = lookup_attribute ("cold", TYPE_ATTRIBUTES (t));
8029 tree class_has_hot_attr
8030 = lookup_attribute ("hot", TYPE_ATTRIBUTES (t));
8032 if (class_has_cold_attr || class_has_hot_attr)
8033 for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f))
8034 if (DECL_DECLARES_FUNCTION_P (f))
8035 maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t);
8038 tree
8039 finish_struct (tree t, tree attributes)
8041 location_t saved_loc = input_location;
8043 /* Now that we've got all the field declarations, reverse everything
8044 as necessary. */
8045 unreverse_member_declarations (t);
8047 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8048 fixup_attribute_variants (t);
8050 /* Nadger the current location so that diagnostics point to the start of
8051 the struct, not the end. */
8052 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
8054 if (processing_template_decl)
8056 tree x;
8058 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8059 if (DECL_DECLARES_FUNCTION_P (x))
8061 DECL_IN_AGGR_P (x) = false;
8062 if (DECL_VIRTUAL_P (x))
8063 CLASSTYPE_NON_AGGREGATE (t) = true;
8065 else if (TREE_CODE (x) == FIELD_DECL)
8067 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
8068 CLASSTYPE_NON_AGGREGATE (t) = true;
8071 /* Also add a USING_DECL for operator=. We know there'll be (at
8072 least) one, but we don't know the signature(s). We want name
8073 lookup not to fail or recurse into bases. This isn't added
8074 to the template decl list so we drop this at instantiation
8075 time. */
8076 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
8077 NULL_TREE);
8078 DECL_CONTEXT (ass_op) = t;
8079 USING_DECL_SCOPE (ass_op) = t;
8080 DECL_DEPENDENT_P (ass_op) = true;
8081 DECL_ARTIFICIAL (ass_op) = true;
8082 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
8083 TYPE_FIELDS (t) = ass_op;
8085 TYPE_SIZE (t) = bitsize_zero_node;
8086 TYPE_SIZE_UNIT (t) = size_zero_node;
8087 /* COMPLETE_TYPE_P is now true. */
8089 set_class_bindings (t);
8091 /* We need to emit an error message if this type was used as a parameter
8092 and it is an abstract type, even if it is a template. We construct
8093 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
8094 account and we call complete_vars with this type, which will check
8095 the PARM_DECLS. Note that while the type is being defined,
8096 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
8097 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
8098 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
8099 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8100 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
8101 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
8102 complete_vars (t);
8104 /* Remember current #pragma pack value. */
8105 TYPE_PRECISION (t) = maximum_field_alignment;
8107 if (cxx_dialect < cxx20)
8109 if (!CLASSTYPE_NON_AGGREGATE (t)
8110 && type_has_user_provided_or_explicit_constructor (t))
8111 CLASSTYPE_NON_AGGREGATE (t) = 1;
8113 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
8114 CLASSTYPE_NON_AGGREGATE (t) = 1;
8116 /* Fix up any variants we've already built. */
8117 fixup_type_variants (t);
8119 else
8120 finish_struct_1 (t);
8121 /* COMPLETE_TYPE_P is now true. */
8123 maybe_warn_about_overly_private_class (t);
8125 if (is_std_init_list (t))
8127 /* People keep complaining that the compiler crashes on an invalid
8128 definition of initializer_list, so I guess we should explicitly
8129 reject it. What the compiler internals care about is that it's a
8130 template and has a pointer field followed by size_type field. */
8131 bool ok = false;
8132 if (processing_template_decl)
8134 tree f = next_aggregate_field (TYPE_FIELDS (t));
8135 if (f && TYPE_PTR_P (TREE_TYPE (f)))
8137 f = next_aggregate_field (DECL_CHAIN (f));
8138 if (f && same_type_p (TREE_TYPE (f), size_type_node))
8139 ok = true;
8142 /* It also cannot be a union. */
8143 ok &= NON_UNION_CLASS_TYPE_P (t);
8144 if (!ok)
8145 fatal_error (input_location, "definition of %qD does not match "
8146 "%<#include <initializer_list>%>", TYPE_NAME (t));
8149 input_location = saved_loc;
8151 TYPE_BEING_DEFINED (t) = 0;
8153 if (current_class_type)
8154 popclass ();
8155 else
8156 error ("trying to finish struct, but kicked out due to previous parse errors");
8158 if (flag_openmp)
8159 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
8160 if (TREE_CODE (decl) == FUNCTION_DECL
8161 && DECL_OBJECT_MEMBER_FUNCTION_P (decl))
8162 if (tree attr = lookup_attribute ("omp declare variant base",
8163 DECL_ATTRIBUTES (decl)))
8164 omp_declare_variant_finalize (decl, attr);
8166 if (processing_template_decl && at_function_scope_p ()
8167 /* Lambdas are defined by the LAMBDA_EXPR. */
8168 && !LAMBDA_TYPE_P (t))
8169 add_stmt (build_min (TAG_DEFN, t));
8171 return t;
8174 /* Hash table to avoid endless recursion when handling references. */
8175 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
8177 /* Return the dynamic type of INSTANCE, if known.
8178 Used to determine whether the virtual function table is needed
8179 or not.
8181 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8182 of our knowledge of its type. *NONNULL should be initialized
8183 before this function is called. */
8185 static tree
8186 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8188 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
8190 switch (TREE_CODE (instance))
8192 case INDIRECT_REF:
8193 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8194 return NULL_TREE;
8195 else
8196 return RECUR (TREE_OPERAND (instance, 0));
8198 case CALL_EXPR:
8199 /* This is a call to a constructor, hence it's never zero. */
8200 if (CALL_EXPR_FN (instance)
8201 && TREE_HAS_CONSTRUCTOR (instance))
8203 if (nonnull)
8204 *nonnull = 1;
8205 return TREE_TYPE (instance);
8207 return NULL_TREE;
8209 case SAVE_EXPR:
8210 /* This is a call to a constructor, hence it's never zero. */
8211 if (TREE_HAS_CONSTRUCTOR (instance))
8213 if (nonnull)
8214 *nonnull = 1;
8215 return TREE_TYPE (instance);
8217 return RECUR (TREE_OPERAND (instance, 0));
8219 case POINTER_PLUS_EXPR:
8220 case PLUS_EXPR:
8221 case MINUS_EXPR:
8222 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
8223 return RECUR (TREE_OPERAND (instance, 0));
8224 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
8225 /* Propagate nonnull. */
8226 return RECUR (TREE_OPERAND (instance, 0));
8228 return NULL_TREE;
8230 CASE_CONVERT:
8231 return RECUR (TREE_OPERAND (instance, 0));
8233 case ADDR_EXPR:
8234 instance = TREE_OPERAND (instance, 0);
8235 if (nonnull)
8237 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8238 with a real object -- given &p->f, p can still be null. */
8239 tree t = get_base_address (instance);
8240 /* ??? Probably should check DECL_WEAK here. */
8241 if (t && DECL_P (t))
8242 *nonnull = 1;
8244 return RECUR (instance);
8246 case COMPONENT_REF:
8247 /* If this component is really a base class reference, then the field
8248 itself isn't definitive. */
8249 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
8250 return RECUR (TREE_OPERAND (instance, 0));
8251 return RECUR (TREE_OPERAND (instance, 1));
8253 case VAR_DECL:
8254 case FIELD_DECL:
8255 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
8256 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8258 if (nonnull)
8259 *nonnull = 1;
8260 return TREE_TYPE (TREE_TYPE (instance));
8262 /* fall through. */
8263 case TARGET_EXPR:
8264 case PARM_DECL:
8265 case RESULT_DECL:
8266 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8268 if (nonnull)
8269 *nonnull = 1;
8270 return TREE_TYPE (instance);
8272 else if (instance == current_class_ptr)
8274 if (nonnull)
8275 *nonnull = 1;
8277 /* if we're in a ctor or dtor, we know our type. If
8278 current_class_ptr is set but we aren't in a function, we're in
8279 an NSDMI (and therefore a constructor). */
8280 if (current_scope () != current_function_decl
8281 || (DECL_LANG_SPECIFIC (current_function_decl)
8282 && (DECL_CONSTRUCTOR_P (current_function_decl)
8283 || DECL_DESTRUCTOR_P (current_function_decl))))
8285 if (cdtorp)
8286 *cdtorp = 1;
8287 return TREE_TYPE (TREE_TYPE (instance));
8290 else if (TYPE_REF_P (TREE_TYPE (instance)))
8292 /* We only need one hash table because it is always left empty. */
8293 if (!fixed_type_or_null_ref_ht)
8294 fixed_type_or_null_ref_ht
8295 = new hash_table<nofree_ptr_hash<tree_node> > (37);
8297 /* Reference variables should be references to objects. */
8298 if (nonnull)
8299 *nonnull = 1;
8301 /* Enter the INSTANCE in a table to prevent recursion; a
8302 variable's initializer may refer to the variable
8303 itself. */
8304 if (VAR_P (instance)
8305 && DECL_INITIAL (instance)
8306 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8307 && !fixed_type_or_null_ref_ht->find (instance))
8309 tree type;
8310 tree_node **slot;
8312 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8313 *slot = instance;
8314 type = RECUR (DECL_INITIAL (instance));
8315 fixed_type_or_null_ref_ht->remove_elt (instance);
8317 return type;
8320 return NULL_TREE;
8322 case VIEW_CONVERT_EXPR:
8323 if (location_wrapper_p (instance))
8324 return RECUR (TREE_OPERAND (instance, 0));
8325 else
8326 /* TODO: Recursion may be correct for some non-location-wrapper
8327 uses of VIEW_CONVERT_EXPR. */
8328 return NULL_TREE;
8330 default:
8331 return NULL_TREE;
8333 #undef RECUR
8336 /* Return nonzero if the dynamic type of INSTANCE is known, and
8337 equivalent to the static type. We also handle the case where
8338 INSTANCE is really a pointer. Return negative if this is a
8339 ctor/dtor. There the dynamic type is known, but this might not be
8340 the most derived base of the original object, and hence virtual
8341 bases may not be laid out according to this type.
8343 Used to determine whether the virtual function table is needed
8344 or not.
8346 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8347 of our knowledge of its type. *NONNULL should be initialized
8348 before this function is called. */
8351 resolves_to_fixed_type_p (tree instance, int* nonnull)
8353 tree t = TREE_TYPE (instance);
8354 int cdtorp = 0;
8355 tree fixed;
8357 /* processing_template_decl can be false in a template if we're in
8358 instantiate_non_dependent_expr, but we still want to suppress
8359 this check. */
8360 if (in_template_context)
8362 /* In a template we only care about the type of the result. */
8363 if (nonnull)
8364 *nonnull = true;
8365 return true;
8368 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8369 if (INDIRECT_TYPE_P (t))
8370 t = TREE_TYPE (t);
8371 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8372 return 1;
8373 if (fixed == NULL_TREE)
8374 return 0;
8375 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8376 return 0;
8377 return cdtorp ? -1 : 1;
8381 void
8382 init_class_processing (void)
8384 current_class_depth = 0;
8385 current_class_stack_size = 10;
8386 current_class_stack
8387 = XNEWVEC (struct class_stack_node, current_class_stack_size);
8388 sizeof_biggest_empty_class = size_zero_node;
8390 ridpointers[(int) RID_PUBLIC] = access_public_node;
8391 ridpointers[(int) RID_PRIVATE] = access_private_node;
8392 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8395 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8397 static void
8398 restore_class_cache (void)
8400 tree type;
8402 /* We are re-entering the same class we just left, so we don't
8403 have to search the whole inheritance matrix to find all the
8404 decls to bind again. Instead, we install the cached
8405 class_shadowed list and walk through it binding names. */
8406 push_binding_level (previous_class_level);
8407 class_binding_level = previous_class_level;
8408 /* Restore IDENTIFIER_TYPE_VALUE. */
8409 for (type = class_binding_level->type_shadowed;
8410 type;
8411 type = TREE_CHAIN (type))
8412 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8415 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8416 appropriate for TYPE.
8418 So that we may avoid calls to lookup_name, we cache the _TYPE
8419 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8421 For multiple inheritance, we perform a two-pass depth-first search
8422 of the type lattice. */
8424 void
8425 pushclass (tree type)
8427 class_stack_node_t csn;
8429 type = TYPE_MAIN_VARIANT (type);
8431 /* Make sure there is enough room for the new entry on the stack. */
8432 if (current_class_depth + 1 >= current_class_stack_size)
8434 current_class_stack_size *= 2;
8435 current_class_stack
8436 = XRESIZEVEC (struct class_stack_node, current_class_stack,
8437 current_class_stack_size);
8440 /* Insert a new entry on the class stack. */
8441 csn = current_class_stack + current_class_depth;
8442 csn->name = current_class_name;
8443 csn->type = current_class_type;
8444 csn->access = current_access_specifier;
8445 csn->names_used = 0;
8446 csn->hidden = 0;
8447 current_class_depth++;
8449 /* Now set up the new type. */
8450 current_class_name = TYPE_NAME (type);
8451 if (TREE_CODE (current_class_name) == TYPE_DECL)
8452 current_class_name = DECL_NAME (current_class_name);
8453 current_class_type = type;
8455 /* By default, things in classes are private, while things in
8456 structures or unions are public. */
8457 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8458 ? access_private_node
8459 : access_public_node);
8461 if (previous_class_level
8462 && type != previous_class_level->this_entity
8463 && current_class_depth == 1)
8465 /* Forcibly remove any old class remnants. */
8466 invalidate_class_lookup_cache ();
8469 if (!previous_class_level
8470 || type != previous_class_level->this_entity
8471 || current_class_depth > 1)
8472 pushlevel_class ();
8473 else
8474 restore_class_cache ();
8477 /* Get out of the current class scope. If we were in a class scope
8478 previously, that is the one popped to. */
8480 void
8481 popclass (void)
8483 poplevel_class ();
8485 current_class_depth--;
8486 current_class_name = current_class_stack[current_class_depth].name;
8487 current_class_type = current_class_stack[current_class_depth].type;
8488 current_access_specifier = current_class_stack[current_class_depth].access;
8489 if (current_class_stack[current_class_depth].names_used)
8490 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8493 /* Mark the top of the class stack as hidden. */
8495 void
8496 push_class_stack (void)
8498 if (current_class_depth)
8499 ++current_class_stack[current_class_depth - 1].hidden;
8502 /* Mark the top of the class stack as un-hidden. */
8504 void
8505 pop_class_stack (void)
8507 if (current_class_depth)
8508 --current_class_stack[current_class_depth - 1].hidden;
8511 /* If the class type currently being defined is either T or
8512 a nested type of T, returns the type from the current_class_stack,
8513 which might be equivalent to but not equal to T in case of
8514 constrained partial specializations. */
8516 tree
8517 currently_open_class (tree t)
8519 int i;
8521 if (!CLASS_TYPE_P (t))
8522 return NULL_TREE;
8524 t = TYPE_MAIN_VARIANT (t);
8526 /* We start looking from 1 because entry 0 is from global scope,
8527 and has no type. */
8528 for (i = current_class_depth; i > 0; --i)
8530 tree c;
8531 if (i == current_class_depth)
8532 c = current_class_type;
8533 else
8535 if (current_class_stack[i].hidden)
8536 break;
8537 c = current_class_stack[i].type;
8539 if (!c)
8540 continue;
8541 if (same_type_p (c, t))
8542 return c;
8544 return NULL_TREE;
8547 /* If either current_class_type or one of its enclosing classes are derived
8548 from T, return the appropriate type. Used to determine how we found
8549 something via unqualified lookup. */
8551 tree
8552 currently_open_derived_class (tree t)
8554 int i;
8556 /* The bases of a dependent type are unknown. */
8557 if (dependent_type_p (t))
8558 return NULL_TREE;
8560 if (!current_class_type)
8561 return NULL_TREE;
8563 if (DERIVED_FROM_P (t, current_class_type))
8564 return current_class_type;
8566 for (i = current_class_depth - 1; i > 0; --i)
8568 if (current_class_stack[i].hidden)
8569 break;
8570 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8571 return current_class_stack[i].type;
8574 return NULL_TREE;
8577 /* Return the outermost enclosing class type that is still open, or
8578 NULL_TREE. */
8580 tree
8581 outermost_open_class (void)
8583 if (!current_class_type)
8584 return NULL_TREE;
8585 tree r = NULL_TREE;
8586 if (TYPE_BEING_DEFINED (current_class_type))
8587 r = current_class_type;
8588 for (int i = current_class_depth - 1; i > 0; --i)
8590 if (current_class_stack[i].hidden)
8591 break;
8592 tree t = current_class_stack[i].type;
8593 if (!TYPE_BEING_DEFINED (t))
8594 break;
8595 r = t;
8597 return r;
8600 /* Returns the innermost class type which is not a lambda closure type. */
8602 tree
8603 current_nonlambda_class_type (void)
8605 tree type = current_class_type;
8606 while (type && LAMBDA_TYPE_P (type))
8607 type = decl_type_context (TYPE_NAME (type));
8608 return type;
8611 /* When entering a class scope, all enclosing class scopes' names with
8612 static meaning (static variables, static functions, types and
8613 enumerators) have to be visible. This recursive function calls
8614 pushclass for all enclosing class contexts until global or a local
8615 scope is reached. TYPE is the enclosed class. */
8617 void
8618 push_nested_class (tree type)
8620 /* A namespace might be passed in error cases, like A::B:C. */
8621 if (type == NULL_TREE
8622 || !CLASS_TYPE_P (type))
8623 return;
8625 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8627 pushclass (type);
8630 /* Undoes a push_nested_class call. */
8632 void
8633 pop_nested_class (void)
8635 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8637 popclass ();
8638 if (context && CLASS_TYPE_P (context))
8639 pop_nested_class ();
8642 /* Returns the number of extern "LANG" blocks we are nested within. */
8645 current_lang_depth (void)
8647 return vec_safe_length (current_lang_base);
8650 /* Set global variables CURRENT_LANG_NAME to appropriate value
8651 so that behavior of name-mangling machinery is correct. */
8653 void
8654 push_lang_context (tree name)
8656 vec_safe_push (current_lang_base, current_lang_name);
8658 if (name == lang_name_cplusplus)
8659 current_lang_name = name;
8660 else if (name == lang_name_c)
8661 current_lang_name = name;
8662 else
8663 error ("language string %<\"%E\"%> not recognized", name);
8666 /* Get out of the current language scope. */
8668 void
8669 pop_lang_context (void)
8671 current_lang_name = current_lang_base->pop ();
8674 /* Type instantiation routines. */
8676 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8677 matches the TARGET_TYPE. If there is no satisfactory match, return
8678 error_mark_node, and issue an error & warning messages under
8679 control of FLAGS. Permit pointers to member function if FLAGS
8680 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8681 a template-id, and EXPLICIT_TARGS are the explicitly provided
8682 template arguments.
8684 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8685 is the base path used to reference those member functions. If
8686 the address is resolved to a member function, access checks will be
8687 performed and errors issued if appropriate. */
8689 static tree
8690 resolve_address_of_overloaded_function (tree target_type,
8691 tree overload,
8692 tsubst_flags_t complain,
8693 bool template_only,
8694 tree explicit_targs,
8695 tree access_path)
8697 /* Here's what the standard says:
8699 [over.over]
8701 If the name is a function template, template argument deduction
8702 is done, and if the argument deduction succeeds, the deduced
8703 arguments are used to generate a single template function, which
8704 is added to the set of overloaded functions considered.
8706 Non-member functions and static member functions match targets of
8707 type "pointer-to-function" or "reference-to-function." Nonstatic
8708 member functions match targets of type "pointer-to-member
8709 function;" the function type of the pointer to member is used to
8710 select the member function from the set of overloaded member
8711 functions. If a non-static member function is selected, the
8712 reference to the overloaded function name is required to have the
8713 form of a pointer to member as described in 5.3.1.
8715 If more than one function is selected, any template functions in
8716 the set are eliminated if the set also contains a non-template
8717 function, and any given template function is eliminated if the
8718 set contains a second template function that is more specialized
8719 than the first according to the partial ordering rules 14.5.5.2.
8720 After such eliminations, if any, there shall remain exactly one
8721 selected function. */
8723 int is_ptrmem = 0;
8724 /* We store the matches in a TREE_LIST rooted here. The functions
8725 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8726 interoperability with most_specialized_instantiation. */
8727 tree matches = NULL_TREE;
8728 tree fn;
8729 tree target_fn_type;
8731 /* By the time we get here, we should be seeing only real
8732 pointer-to-member types, not the internal POINTER_TYPE to
8733 METHOD_TYPE representation. */
8734 gcc_assert (!TYPE_PTR_P (target_type)
8735 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8737 gcc_assert (is_overloaded_fn (overload));
8739 /* Check that the TARGET_TYPE is reasonable. */
8740 if (TYPE_PTRFN_P (target_type)
8741 || TYPE_REFFN_P (target_type))
8742 /* This is OK. */;
8743 else if (TYPE_PTRMEMFUNC_P (target_type))
8744 /* This is OK, too. */
8745 is_ptrmem = 1;
8746 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8747 /* This is OK, too. This comes from a conversion to reference
8748 type. */
8749 target_type = build_reference_type (target_type);
8750 else
8752 if (complain & tf_error)
8753 error ("cannot resolve overloaded function %qD based on"
8754 " conversion to type %qT",
8755 OVL_NAME (overload), target_type);
8756 return error_mark_node;
8759 /* Non-member functions and static member functions match targets of type
8760 "pointer-to-function" or "reference-to-function." Nonstatic member
8761 functions match targets of type "pointer-to-member-function;" the
8762 function type of the pointer to member is used to select the member
8763 function from the set of overloaded member functions.
8765 So figure out the FUNCTION_TYPE that we want to match against. */
8766 target_fn_type = static_fn_type (target_type);
8768 /* If we can find a non-template function that matches, we can just
8769 use it. There's no point in generating template instantiations
8770 if we're just going to throw them out anyhow. But, of course, we
8771 can only do this when we don't *need* a template function. */
8772 if (!template_only)
8773 for (lkp_iterator iter (overload); iter; ++iter)
8775 tree fn = *iter;
8777 if (TREE_CODE (fn) == TEMPLATE_DECL)
8778 /* We're not looking for templates just yet. */
8779 continue;
8781 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8782 /* We're looking for a non-static member, and this isn't
8783 one, or vice versa. */
8784 continue;
8786 /* Constraints must be satisfied. This is done before
8787 return type deduction since that instantiates the
8788 function. */
8789 if (!constraints_satisfied_p (fn))
8790 continue;
8792 if (undeduced_auto_decl (fn))
8794 /* Force instantiation to do return type deduction. */
8795 maybe_instantiate_decl (fn);
8796 require_deduced_type (fn);
8799 /* In C++17 we need the noexcept-qualifier to compare types. */
8800 if (flag_noexcept_type
8801 && !maybe_instantiate_noexcept (fn, complain))
8802 continue;
8804 /* See if there's a match. */
8805 tree fntype = static_fn_type (fn);
8806 if (same_type_p (target_fn_type, fntype)
8807 || fnptr_conv_p (target_fn_type, fntype))
8808 matches = tree_cons (fn, NULL_TREE, matches);
8811 /* Now, if we've already got a match (or matches), there's no need
8812 to proceed to the template functions. But, if we don't have a
8813 match we need to look at them, too. */
8814 if (!matches)
8816 tree target_arg_types;
8817 tree target_ret_type;
8818 tree *args;
8819 unsigned int nargs, ia;
8820 tree arg;
8822 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8823 target_ret_type = TREE_TYPE (target_fn_type);
8825 nargs = list_length (target_arg_types);
8826 args = XALLOCAVEC (tree, nargs);
8827 for (arg = target_arg_types, ia = 0;
8828 arg != NULL_TREE;
8829 arg = TREE_CHAIN (arg), ++ia)
8830 args[ia] = TREE_VALUE (arg);
8831 nargs = ia;
8833 for (lkp_iterator iter (overload); iter; ++iter)
8835 tree fn = *iter;
8836 tree instantiation;
8837 tree targs;
8839 if (TREE_CODE (fn) != TEMPLATE_DECL)
8840 /* We're only looking for templates. */
8841 continue;
8843 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8844 != is_ptrmem)
8845 /* We're not looking for a non-static member, and this is
8846 one, or vice versa. */
8847 continue;
8849 tree ret = target_ret_type;
8851 /* If the template has a deduced return type, don't expose it to
8852 template argument deduction. */
8853 if (undeduced_auto_decl (fn))
8854 ret = NULL_TREE;
8856 /* Try to do argument deduction. */
8857 targs = make_tree_vec (DECL_NTPARMS (fn));
8858 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8859 nargs, ret,
8860 DEDUCE_EXACT, LOOKUP_NORMAL,
8861 NULL, false, false);
8862 if (instantiation == error_mark_node)
8863 /* Instantiation failed. */
8864 continue;
8866 /* Constraints must be satisfied. This is done before
8867 return type deduction since that instantiates the
8868 function. */
8869 if (flag_concepts && !constraints_satisfied_p (instantiation))
8870 continue;
8872 /* And now force instantiation to do return type deduction. */
8873 if (undeduced_auto_decl (instantiation))
8875 ++function_depth;
8876 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8877 --function_depth;
8879 require_deduced_type (instantiation);
8882 /* In C++17 we need the noexcept-qualifier to compare types. */
8883 if (flag_noexcept_type)
8884 maybe_instantiate_noexcept (instantiation, complain);
8886 /* See if there's a match. */
8887 tree fntype = static_fn_type (instantiation);
8888 if (same_type_p (target_fn_type, fntype)
8889 || fnptr_conv_p (target_fn_type, fntype))
8890 matches = tree_cons (instantiation, fn, matches);
8893 /* Now, remove all but the most specialized of the matches. */
8894 if (matches)
8896 tree match = most_specialized_instantiation (matches);
8898 if (match != error_mark_node)
8900 matches = match;
8901 TREE_CHAIN (match) = NULL_TREE;
8905 else if (flag_concepts && TREE_CHAIN (matches))
8906 if (tree match = most_constrained_function (matches))
8908 matches = match;
8909 TREE_CHAIN (match) = NULL_TREE;
8912 /* Now we should have exactly one function in MATCHES. */
8913 if (matches == NULL_TREE)
8915 /* There were *no* matches. */
8916 if (complain & tf_error)
8918 auto_diagnostic_group d;
8919 error ("no matches converting function %qD to type %q#T",
8920 OVL_NAME (overload), target_type);
8922 print_candidates (overload);
8924 return error_mark_node;
8926 else if (TREE_CHAIN (matches))
8928 /* There were too many matches. First check if they're all
8929 the same function. */
8930 tree match = NULL_TREE;
8932 fn = TREE_PURPOSE (matches);
8934 /* For multi-versioned functions, more than one match is just fine and
8935 decls_match will return false as they are different. */
8936 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8937 if (!decls_match (fn, TREE_PURPOSE (match))
8938 && !targetm.target_option.function_versions
8939 (fn, TREE_PURPOSE (match)))
8940 break;
8942 if (match)
8944 if (complain & tf_error)
8946 auto_diagnostic_group d;
8947 error ("converting overloaded function %qD to type %q#T is ambiguous",
8948 OVL_NAME (overload), target_type);
8950 /* Since print_candidates expects the functions in the
8951 TREE_VALUE slot, we flip them here. */
8952 for (match = matches; match; match = TREE_CHAIN (match))
8953 TREE_VALUE (match) = TREE_PURPOSE (match);
8955 print_candidates (matches);
8958 return error_mark_node;
8962 /* Good, exactly one match. Now, convert it to the correct type. */
8963 fn = TREE_PURPOSE (matches);
8965 if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
8966 && !(complain & tf_ptrmem_ok))
8968 /* Previously we allowed this behavior for iobj member functions when the
8969 -fms-extensions flag is passed as MSVC allows this as a language
8970 extension. MSVC also allows this for xobj member functions, but the
8971 documentation for -fms-extensions states it's purpose is to support
8972 the use of microsoft headers. Until otherwise demonstrated, we should
8973 assume xobj member functions are not used in this manner in microsoft
8974 headers and forbid the incorrect syntax instead of supporting it for
8975 non-legacy uses. This should hopefully encourage conformance going
8976 forward.
8977 This comment is referred to in typeck.cc:cp_build_addr_expr_1. */
8978 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions)
8979 /* Early escape. */;
8980 else if (!(complain & tf_error))
8981 return error_mark_node;
8982 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
8984 auto_diagnostic_group d;
8985 /* Should match the error in typeck.cc:cp_build_addr_expr_1.
8986 We seem to lack the details here to match that diagnostic exactly,
8987 perhaps this could be fixed in the future? See PR113075 bug 2. */
8988 error_at (input_location,
8989 "ISO C++ forbids taking the address of an unqualified"
8990 " or parenthesized non-static member function to form"
8991 " a pointer to explicit object member function.");
8992 /* This is incorrect, see PR113075 bug 3. */
8993 inform (input_location,
8994 "a pointer to explicit object member function can only be "
8995 "formed with %<&%E%>", fn);
8997 else
8999 static int explained;
9000 gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions);
9001 /* Is there a reason this error message doesn't match the one in
9002 typeck.cc:cp_build_addr_expr_1? */
9003 auto_diagnostic_group d;
9004 if (permerror (input_location, "assuming pointer to member %qD", fn)
9005 && !explained)
9007 inform (input_location, "(a pointer to member can only be "
9008 "formed with %<&%E%>)", fn);
9009 explained = 1;
9014 /* If a pointer to a function that is multi-versioned is requested, the
9015 pointer to the dispatcher function is returned instead. This works
9016 well because indirectly calling the function will dispatch the right
9017 function version at run-time. */
9018 if (DECL_FUNCTION_VERSIONED (fn))
9020 fn = get_function_version_dispatcher (fn);
9021 if (fn == NULL)
9022 return error_mark_node;
9023 /* Mark all the versions corresponding to the dispatcher as used. */
9024 if (!(complain & tf_conv))
9025 mark_versions_used (fn);
9028 /* If we're doing overload resolution purely for the purpose of
9029 determining conversion sequences, we should not consider the
9030 function used. If this conversion sequence is selected, the
9031 function will be marked as used at this point. */
9032 if (!(complain & tf_conv))
9034 /* Make =delete work with SFINAE. */
9035 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
9036 return error_mark_node;
9037 if (!mark_used (fn, complain) && !(complain & tf_error))
9038 return error_mark_node;
9041 /* We could not check access to member functions when this
9042 expression was originally created since we did not know at that
9043 time to which function the expression referred. */
9044 if (DECL_FUNCTION_MEMBER_P (fn))
9046 gcc_assert (access_path);
9047 perform_or_defer_access_check (access_path, fn, fn, complain);
9050 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
9051 return cp_build_addr_expr (fn, complain);
9052 else
9054 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
9055 will mark the function as addressed, but here we must do it
9056 explicitly. */
9057 cxx_mark_addressable (fn);
9059 return fn;
9063 /* This function will instantiate the type of the expression given in
9064 RHS to match the type of LHSTYPE. If errors exist, then return
9065 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
9066 we complain on errors. If we are not complaining, never modify rhs,
9067 as overload resolution wants to try many possible instantiations, in
9068 the hope that at least one will work.
9070 For non-recursive calls, LHSTYPE should be a function, pointer to
9071 function, or a pointer to member function. */
9073 tree
9074 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
9076 tsubst_flags_t complain_in = complain;
9077 tree access_path = NULL_TREE;
9079 complain &= ~tf_ptrmem_ok;
9081 STRIP_ANY_LOCATION_WRAPPER (rhs);
9083 if (lhstype == unknown_type_node)
9085 if (complain & tf_error)
9086 error ("not enough type information");
9087 return error_mark_node;
9090 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
9092 tree fntype = non_reference (lhstype);
9093 if (same_type_p (fntype, TREE_TYPE (rhs)))
9094 return rhs;
9095 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
9096 return rhs;
9097 if (flag_ms_extensions
9098 && TYPE_PTRMEMFUNC_P (fntype)
9099 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
9100 /* Microsoft allows `A::f' to be resolved to a
9101 pointer-to-member. */
9103 else
9105 if (complain & tf_error)
9106 error ("cannot convert %qE from type %qT to type %qT",
9107 rhs, TREE_TYPE (rhs), fntype);
9108 return error_mark_node;
9112 /* If we instantiate a template, and it is a A ?: C expression
9113 with omitted B, look through the SAVE_EXPR. */
9114 if (TREE_CODE (rhs) == SAVE_EXPR)
9115 rhs = TREE_OPERAND (rhs, 0);
9117 if (BASELINK_P (rhs))
9119 access_path = BASELINK_ACCESS_BINFO (rhs);
9120 rhs = BASELINK_FUNCTIONS (rhs);
9123 /* There are only a few kinds of expressions that may have a type
9124 dependent on overload resolution. */
9125 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
9126 || TREE_CODE (rhs) == COMPONENT_REF
9127 || is_overloaded_fn (rhs)
9128 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
9130 /* This should really only be used when attempting to distinguish
9131 what sort of a pointer to function we have. For now, any
9132 arithmetic operation which is not supported on pointers
9133 is rejected as an error. */
9135 switch (TREE_CODE (rhs))
9137 case COMPONENT_REF:
9139 tree member = TREE_OPERAND (rhs, 1);
9141 member = instantiate_type (lhstype, member, complain);
9142 if (member != error_mark_node
9143 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
9144 /* Do not lose object's side effects. */
9145 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
9146 TREE_OPERAND (rhs, 0), member);
9147 return member;
9150 case OFFSET_REF:
9151 rhs = TREE_OPERAND (rhs, 1);
9152 if (BASELINK_P (rhs))
9153 return instantiate_type (lhstype, rhs, complain_in);
9155 /* This can happen if we are forming a pointer-to-member for a
9156 member template. */
9157 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
9159 /* Fall through. */
9161 case TEMPLATE_ID_EXPR:
9163 tree fns = TREE_OPERAND (rhs, 0);
9164 tree args = TREE_OPERAND (rhs, 1);
9166 return
9167 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
9168 /*template_only=*/true,
9169 args, access_path);
9172 case OVERLOAD:
9173 case FUNCTION_DECL:
9174 return
9175 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
9176 /*template_only=*/false,
9177 /*explicit_targs=*/NULL_TREE,
9178 access_path);
9180 case ADDR_EXPR:
9182 if (PTRMEM_OK_P (rhs))
9183 complain |= tf_ptrmem_ok;
9185 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
9188 case ERROR_MARK:
9189 return error_mark_node;
9191 default:
9192 gcc_unreachable ();
9194 return error_mark_node;
9197 /* Return the name of the virtual function pointer field
9198 (as an IDENTIFIER_NODE) for the given TYPE. Note that
9199 this may have to look back through base types to find the
9200 ultimate field name. (For single inheritance, these could
9201 all be the same name. Who knows for multiple inheritance). */
9203 static tree
9204 get_vfield_name (tree type)
9206 tree binfo, base_binfo;
9208 for (binfo = TYPE_BINFO (type);
9209 BINFO_N_BASE_BINFOS (binfo);
9210 binfo = base_binfo)
9212 base_binfo = BINFO_BASE_BINFO (binfo, 0);
9214 if (BINFO_VIRTUAL_P (base_binfo)
9215 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
9216 break;
9219 type = BINFO_TYPE (binfo);
9220 tree ctor_name = constructor_name (type);
9221 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
9222 + IDENTIFIER_LENGTH (ctor_name) + 2);
9223 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
9224 return get_identifier (buf);
9227 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
9228 according to [class]:
9229 The class-name is also inserted
9230 into the scope of the class itself. For purposes of access checking,
9231 the inserted class name is treated as if it were a public member name. */
9233 void
9234 build_self_reference (void)
9236 tree name = DECL_NAME (TYPE_NAME (current_class_type));
9237 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
9239 DECL_NONLOCAL (decl) = 1;
9240 DECL_CONTEXT (decl) = current_class_type;
9241 DECL_ARTIFICIAL (decl) = 1;
9242 SET_DECL_SELF_REFERENCE_P (decl);
9243 set_underlying_type (decl);
9244 set_instantiating_module (decl);
9246 if (processing_template_decl)
9247 decl = push_template_decl (decl);
9249 tree saved_cas = current_access_specifier;
9250 current_access_specifier = access_public_node;
9251 finish_member_declaration (decl);
9252 current_access_specifier = saved_cas;
9255 /* Returns 1 if TYPE contains only padding bytes. */
9258 is_empty_class (tree type)
9260 if (type == error_mark_node)
9261 return 0;
9263 if (! CLASS_TYPE_P (type))
9264 return 0;
9266 return CLASSTYPE_EMPTY_P (type);
9269 /* Returns true if TYPE contains no actual data, just various
9270 possible combinations of empty classes. If IGNORE_VPTR is true,
9271 a vptr doesn't prevent the class from being considered empty. Typically
9272 we want to ignore the vptr on assignment, and not on initialization. */
9274 bool
9275 is_really_empty_class (tree type, bool ignore_vptr)
9277 if (CLASS_TYPE_P (type))
9279 tree field;
9280 tree binfo;
9281 tree base_binfo;
9282 int i;
9284 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9285 out, but we'd like to be able to check this before then. */
9286 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
9287 return true;
9289 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
9290 return false;
9292 for (binfo = TYPE_BINFO (type), i = 0;
9293 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9294 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
9295 return false;
9296 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9297 if (TREE_CODE (field) == FIELD_DECL
9298 && !DECL_ARTIFICIAL (field)
9299 /* An unnamed bit-field is not a data member. */
9300 && !DECL_UNNAMED_BIT_FIELD (field)
9301 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9302 return false;
9303 return true;
9305 else if (TREE_CODE (type) == ARRAY_TYPE)
9306 return (integer_zerop (array_type_nelts_top (type))
9307 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9308 return false;
9311 /* Note that NAME was looked up while the current class was being
9312 defined and that the result of that lookup was DECL. */
9314 void
9315 maybe_note_name_used_in_class (tree name, tree decl)
9317 /* If we're not defining a class, there's nothing to do. */
9318 if (!(innermost_scope_kind() == sk_class
9319 && TYPE_BEING_DEFINED (current_class_type)
9320 && !LAMBDA_TYPE_P (current_class_type)))
9321 return;
9323 const cp_binding_level *blev = nullptr;
9324 if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9325 blev = binding->scope;
9326 const cp_binding_level *lev = current_binding_level;
9328 /* Record the binding in the names_used tables for classes inside blev. */
9329 for (int i = current_class_depth; i > 0; --i)
9331 tree type = (i == current_class_depth
9332 ? current_class_type
9333 : current_class_stack[i].type);
9335 for (; lev; lev = lev->level_chain)
9337 if (lev == blev)
9338 /* We found the declaration. */
9339 return;
9340 if (lev->kind == sk_class && lev->this_entity == type)
9341 /* This class is inside the declaration scope. */
9342 break;
9345 auto &names_used = current_class_stack[i-1].names_used;
9346 if (!names_used)
9347 names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9349 tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9350 TREE_TYPE (decl), decl);
9351 EXPR_LOCATION_WRAPPER_P (use) = 1;
9352 splay_tree_insert (names_used,
9353 (splay_tree_key) name,
9354 (splay_tree_value) use);
9358 /* Note that NAME was declared (as DECL) in the current class. Check
9359 to see that the declaration is valid under [class.member.lookup]:
9361 If [the result of a search in T for N at point P] differs from the result of
9362 a search in T for N from immediately after the class-specifier of T, the
9363 program is ill-formed, no diagnostic required. */
9365 void
9366 note_name_declared_in_class (tree name, tree decl)
9368 splay_tree names_used;
9369 splay_tree_node n;
9371 /* Look to see if we ever used this name. */
9372 names_used
9373 = current_class_stack[current_class_depth - 1].names_used;
9374 if (!names_used)
9375 return;
9376 /* The C language allows members to be declared with a type of the same
9377 name, and the C++ standard says this diagnostic is not required. So
9378 allow it in extern "C" blocks unless pedantic is specified.
9379 Allow it in all cases if -ms-extensions is specified. */
9380 if ((!pedantic && current_lang_name == lang_name_c)
9381 || flag_ms_extensions)
9382 return;
9383 n = splay_tree_lookup (names_used, (splay_tree_key) name);
9384 if (n)
9386 tree use = (tree) n->value;
9387 location_t loc = EXPR_LOCATION (use);
9388 tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9389 /* [basic.scope.class]
9391 A name N used in a class S shall refer to the same declaration
9392 in its context and when re-evaluated in the completed scope of
9393 S. */
9394 auto ov = make_temp_override (global_dc->m_pedantic_errors);
9395 if (TREE_CODE (decl) == TYPE_DECL
9396 && TREE_CODE (olddecl) == TYPE_DECL
9397 && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9398 /* Different declaration, but same meaning; just warn. */;
9399 else if (flag_permissive)
9400 /* Let -fpermissive make it a warning like past versions. */;
9401 else
9402 /* Make it an error. */
9403 global_dc->m_pedantic_errors = 1;
9405 auto_diagnostic_group d;
9406 if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9407 "declaration of %q#D changes meaning of %qD",
9408 decl, OVL_NAME (decl)))
9410 inform (loc, "used here to mean %q#D", olddecl);
9411 inform (location_of (olddecl), "declared here" );
9416 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9417 Secondary vtables are merged with primary vtables; this function
9418 will return the VAR_DECL for the primary vtable. */
9420 tree
9421 get_vtbl_decl_for_binfo (tree binfo)
9423 tree decl;
9425 decl = BINFO_VTABLE (binfo);
9426 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9428 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9429 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9431 if (decl)
9432 gcc_assert (VAR_P (decl));
9433 return decl;
9437 /* Returns the binfo for the primary base of BINFO. If the resulting
9438 BINFO is a virtual base, and it is inherited elsewhere in the
9439 hierarchy, then the returned binfo might not be the primary base of
9440 BINFO in the complete object. Check BINFO_PRIMARY_P or
9441 BINFO_LOST_PRIMARY_P to be sure. */
9443 static tree
9444 get_primary_binfo (tree binfo)
9446 tree primary_base;
9448 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9449 if (!primary_base)
9450 return NULL_TREE;
9452 return copied_binfo (primary_base, binfo);
9455 /* As above, but iterate until we reach the binfo that actually provides the
9456 vptr for BINFO. */
9458 static tree
9459 most_primary_binfo (tree binfo)
9461 tree b = binfo;
9462 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9463 && !BINFO_LOST_PRIMARY_P (b))
9465 tree primary_base = get_primary_binfo (b);
9466 gcc_assert (BINFO_PRIMARY_P (primary_base)
9467 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9468 b = primary_base;
9470 return b;
9473 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9474 type. Note that the virtual inheritance might be above or below BINFO in
9475 the hierarchy. */
9477 bool
9478 vptr_via_virtual_p (tree binfo)
9480 if (TYPE_P (binfo))
9481 binfo = TYPE_BINFO (binfo);
9482 tree primary = most_primary_binfo (binfo);
9483 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9484 a morally virtual base. */
9485 tree virt = binfo_via_virtual (primary, NULL_TREE);
9486 return virt != NULL_TREE;
9489 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9491 static int
9492 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9494 if (!indented_p)
9495 fprintf (stream, "%*s", indent, "");
9496 return 1;
9499 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9500 INDENT should be zero when called from the top level; it is
9501 incremented recursively. IGO indicates the next expected BINFO in
9502 inheritance graph ordering. */
9504 static tree
9505 dump_class_hierarchy_r (FILE *stream,
9506 dump_flags_t flags,
9507 tree binfo,
9508 tree igo,
9509 int indent)
9511 int indented = 0;
9512 tree base_binfo;
9513 int i;
9515 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9516 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9517 (HOST_WIDE_INT) (uintptr_t) binfo);
9518 if (binfo != igo)
9520 fprintf (stream, "alternative-path\n");
9521 return igo;
9523 igo = TREE_CHAIN (binfo);
9525 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9526 tree_to_shwi (BINFO_OFFSET (binfo)));
9527 if (is_empty_class (BINFO_TYPE (binfo)))
9528 fprintf (stream, " empty");
9529 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9530 fprintf (stream, " nearly-empty");
9531 if (BINFO_VIRTUAL_P (binfo))
9532 fprintf (stream, " virtual");
9533 fprintf (stream, "\n");
9535 if (BINFO_PRIMARY_P (binfo))
9537 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9538 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9539 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9540 TFF_PLAIN_IDENTIFIER),
9541 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9543 if (BINFO_LOST_PRIMARY_P (binfo))
9545 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9546 fprintf (stream, " lost-primary");
9548 if (indented)
9549 fprintf (stream, "\n");
9551 if (!(flags & TDF_SLIM))
9553 int indented = 0;
9555 if (BINFO_SUBVTT_INDEX (binfo))
9557 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9558 fprintf (stream, " subvttidx=%s",
9559 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9560 TFF_PLAIN_IDENTIFIER));
9562 if (BINFO_VPTR_INDEX (binfo))
9564 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9565 fprintf (stream, " vptridx=%s",
9566 expr_as_string (BINFO_VPTR_INDEX (binfo),
9567 TFF_PLAIN_IDENTIFIER));
9569 if (BINFO_VPTR_FIELD (binfo))
9571 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9572 fprintf (stream, " vbaseoffset=%s",
9573 expr_as_string (BINFO_VPTR_FIELD (binfo),
9574 TFF_PLAIN_IDENTIFIER));
9576 if (BINFO_VTABLE (binfo))
9578 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9579 fprintf (stream, " vptr=%s",
9580 expr_as_string (BINFO_VTABLE (binfo),
9581 TFF_PLAIN_IDENTIFIER));
9584 if (indented)
9585 fprintf (stream, "\n");
9588 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9589 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9591 return igo;
9594 /* Dump the BINFO hierarchy for T. */
9596 static void
9597 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9599 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9600 fprintf (stream, " size=" HOST_WIDE_INT_PRINT_UNSIGNED " align=%u\n",
9601 tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT,
9602 TYPE_ALIGN (t) / BITS_PER_UNIT);
9603 if (tree as_base = CLASSTYPE_AS_BASE (t))
9604 fprintf (stream, " base size=" HOST_WIDE_INT_PRINT_UNSIGNED
9605 " base align=%u\n",
9606 tree_to_shwi (TYPE_SIZE (as_base)) / BITS_PER_UNIT,
9607 TYPE_ALIGN (as_base) / BITS_PER_UNIT);
9608 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9609 fprintf (stream, "\n");
9612 /* Debug interface to hierarchy dumping. */
9614 void
9615 debug_class (tree t)
9617 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9620 static void
9621 dump_class_hierarchy (tree t)
9623 dump_flags_t flags;
9624 if (FILE *stream = dump_begin (class_dump_id, &flags))
9626 dump_class_hierarchy_1 (stream, flags, t);
9627 dump_end (class_dump_id, stream);
9631 static void
9632 dump_array (FILE * stream, tree decl)
9634 tree value;
9635 unsigned HOST_WIDE_INT ix;
9636 HOST_WIDE_INT elt;
9637 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9639 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9640 / BITS_PER_UNIT);
9641 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9642 fprintf (stream, " %s entries",
9643 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9644 TFF_PLAIN_IDENTIFIER));
9645 fprintf (stream, "\n");
9647 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9648 ix, value)
9649 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9650 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9653 static void
9654 dump_vtable (tree t, tree binfo, tree vtable)
9656 dump_flags_t flags;
9657 FILE *stream = dump_begin (class_dump_id, &flags);
9659 if (!stream)
9660 return;
9662 if (!(flags & TDF_SLIM))
9664 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9666 fprintf (stream, "%s for %s",
9667 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9668 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9669 if (ctor_vtbl_p)
9671 if (!BINFO_VIRTUAL_P (binfo))
9672 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9673 (HOST_WIDE_INT) (uintptr_t) binfo);
9674 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9676 fprintf (stream, "\n");
9677 dump_array (stream, vtable);
9678 fprintf (stream, "\n");
9681 dump_end (class_dump_id, stream);
9684 static void
9685 dump_vtt (tree t, tree vtt)
9687 dump_flags_t flags;
9688 FILE *stream = dump_begin (class_dump_id, &flags);
9690 if (!stream)
9691 return;
9693 if (!(flags & TDF_SLIM))
9695 fprintf (stream, "VTT for %s\n",
9696 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9697 dump_array (stream, vtt);
9698 fprintf (stream, "\n");
9701 dump_end (class_dump_id, stream);
9704 /* Dump a function or thunk and its thunkees. */
9706 static void
9707 dump_thunk (FILE *stream, int indent, tree thunk)
9709 static const char spaces[] = " ";
9710 tree name = DECL_NAME (thunk);
9711 tree thunks;
9713 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9714 (void *)thunk,
9715 !DECL_THUNK_P (thunk) ? "function"
9716 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9717 name ? IDENTIFIER_POINTER (name) : "<unset>");
9718 if (DECL_THUNK_P (thunk))
9720 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9721 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9723 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9724 if (!virtual_adjust)
9725 /*NOP*/;
9726 else if (DECL_THIS_THUNK_P (thunk))
9727 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9728 tree_to_shwi (virtual_adjust));
9729 else
9730 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9731 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9732 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9733 if (THUNK_ALIAS (thunk))
9734 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9736 fprintf (stream, "\n");
9737 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9738 dump_thunk (stream, indent + 2, thunks);
9741 /* Dump the thunks for FN. */
9743 void
9744 debug_thunks (tree fn)
9746 dump_thunk (stderr, 0, fn);
9749 /* Virtual function table initialization. */
9751 /* Create all the necessary vtables for T and its base classes. */
9753 static void
9754 finish_vtbls (tree t)
9756 tree vbase;
9757 vec<constructor_elt, va_gc> *v = NULL;
9758 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9760 /* We lay out the primary and secondary vtables in one contiguous
9761 vtable. The primary vtable is first, followed by the non-virtual
9762 secondary vtables in inheritance graph order. */
9763 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9764 vtable, t, &v);
9766 /* Then come the virtual bases, also in inheritance graph order. */
9767 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9769 if (!BINFO_VIRTUAL_P (vbase))
9770 continue;
9771 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9774 if (BINFO_VTABLE (TYPE_BINFO (t)))
9775 initialize_vtable (TYPE_BINFO (t), v);
9778 /* Initialize the vtable for BINFO with the INITS. */
9780 static void
9781 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9783 tree decl;
9785 layout_vtable_decl (binfo, vec_safe_length (inits));
9786 decl = get_vtbl_decl_for_binfo (binfo);
9787 initialize_artificial_var (decl, inits);
9788 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9791 /* Build the VTT (virtual table table) for T.
9792 A class requires a VTT if it has virtual bases.
9794 This holds
9795 1 - primary virtual pointer for complete object T
9796 2 - secondary VTTs for each direct non-virtual base of T which requires a
9798 3 - secondary virtual pointers for each direct or indirect base of T which
9799 has virtual bases or is reachable via a virtual path from T.
9800 4 - secondary VTTs for each direct or indirect virtual base of T.
9802 Secondary VTTs look like complete object VTTs without part 4. */
9804 static void
9805 build_vtt (tree t)
9807 tree type;
9808 tree vtt;
9809 tree index;
9810 vec<constructor_elt, va_gc> *inits;
9812 /* Build up the initializers for the VTT. */
9813 inits = NULL;
9814 index = size_zero_node;
9815 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9817 /* If we didn't need a VTT, we're done. */
9818 if (!inits)
9819 return;
9821 /* Figure out the type of the VTT. */
9822 type = build_array_of_n_type (const_ptr_type_node,
9823 inits->length ());
9825 /* Now, build the VTT object itself. */
9826 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9827 initialize_artificial_var (vtt, inits);
9828 /* Add the VTT to the vtables list. */
9829 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9830 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9832 dump_vtt (t, vtt);
9835 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9836 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9837 and CHAIN the vtable pointer for this binfo after construction is
9838 complete. VALUE can also be another BINFO, in which case we recurse. */
9840 static tree
9841 binfo_ctor_vtable (tree binfo)
9843 tree vt;
9845 while (1)
9847 vt = BINFO_VTABLE (binfo);
9848 if (TREE_CODE (vt) == TREE_LIST)
9849 vt = TREE_VALUE (vt);
9850 if (TREE_CODE (vt) == TREE_BINFO)
9851 binfo = vt;
9852 else
9853 break;
9856 return vt;
9859 /* Data for secondary VTT initialization. */
9860 struct secondary_vptr_vtt_init_data
9862 /* Is this the primary VTT? */
9863 bool top_level_p;
9865 /* Current index into the VTT. */
9866 tree index;
9868 /* Vector of initializers built up. */
9869 vec<constructor_elt, va_gc> *inits;
9871 /* The type being constructed by this secondary VTT. */
9872 tree type_being_constructed;
9875 /* Recursively build the VTT-initializer for BINFO (which is in the
9876 hierarchy dominated by T). INITS points to the end of the initializer
9877 list to date. INDEX is the VTT index where the next element will be
9878 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9879 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9880 for virtual bases of T. When it is not so, we build the constructor
9881 vtables for the BINFO-in-T variant. */
9883 static void
9884 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9885 tree *index)
9887 int i;
9888 tree b;
9889 tree init;
9890 secondary_vptr_vtt_init_data data;
9891 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9893 /* We only need VTTs for subobjects with virtual bases. */
9894 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9895 return;
9897 /* We need to use a construction vtable if this is not the primary
9898 VTT. */
9899 if (!top_level_p)
9901 build_ctor_vtbl_group (binfo, t);
9903 /* Record the offset in the VTT where this sub-VTT can be found. */
9904 BINFO_SUBVTT_INDEX (binfo) = *index;
9907 /* Add the address of the primary vtable for the complete object. */
9908 init = binfo_ctor_vtable (binfo);
9909 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9910 if (top_level_p)
9912 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9913 BINFO_VPTR_INDEX (binfo) = *index;
9915 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9917 /* Recursively add the secondary VTTs for non-virtual bases. */
9918 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9919 if (!BINFO_VIRTUAL_P (b))
9920 build_vtt_inits (b, t, inits, index);
9922 /* Add secondary virtual pointers for all subobjects of BINFO with
9923 either virtual bases or reachable along a virtual path, except
9924 subobjects that are non-virtual primary bases. */
9925 data.top_level_p = top_level_p;
9926 data.index = *index;
9927 data.inits = *inits;
9928 data.type_being_constructed = BINFO_TYPE (binfo);
9930 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9932 *index = data.index;
9934 /* data.inits might have grown as we added secondary virtual pointers.
9935 Make sure our caller knows about the new vector. */
9936 *inits = data.inits;
9938 if (top_level_p)
9939 /* Add the secondary VTTs for virtual bases in inheritance graph
9940 order. */
9941 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9943 if (!BINFO_VIRTUAL_P (b))
9944 continue;
9946 build_vtt_inits (b, t, inits, index);
9948 else
9949 /* Remove the ctor vtables we created. */
9950 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9953 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9954 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9956 static tree
9957 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9959 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9961 /* We don't care about bases that don't have vtables. */
9962 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9963 return dfs_skip_bases;
9965 /* We're only interested in proper subobjects of the type being
9966 constructed. */
9967 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9968 return NULL_TREE;
9970 /* We're only interested in bases with virtual bases or reachable
9971 via a virtual path from the type being constructed. */
9972 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9973 || binfo_via_virtual (binfo, data->type_being_constructed)))
9974 return dfs_skip_bases;
9976 /* We're not interested in non-virtual primary bases. */
9977 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9978 return NULL_TREE;
9980 /* Record the index where this secondary vptr can be found. */
9981 if (data->top_level_p)
9983 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9984 BINFO_VPTR_INDEX (binfo) = data->index;
9986 if (BINFO_VIRTUAL_P (binfo))
9988 /* It's a primary virtual base, and this is not a
9989 construction vtable. Find the base this is primary of in
9990 the inheritance graph, and use that base's vtable
9991 now. */
9992 while (BINFO_PRIMARY_P (binfo))
9993 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9997 /* Add the initializer for the secondary vptr itself. */
9998 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
10000 /* Advance the vtt index. */
10001 data->index = size_binop (PLUS_EXPR, data->index,
10002 TYPE_SIZE_UNIT (ptr_type_node));
10004 return NULL_TREE;
10007 /* Called from build_vtt_inits via dfs_walk. After building
10008 constructor vtables and generating the sub-vtt from them, we need
10009 to restore the BINFO_VTABLES that were scribbled on. DATA is the
10010 binfo of the base whose sub vtt was generated. */
10012 static tree
10013 dfs_fixup_binfo_vtbls (tree binfo, void* data)
10015 tree vtable = BINFO_VTABLE (binfo);
10017 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10018 /* If this class has no vtable, none of its bases do. */
10019 return dfs_skip_bases;
10021 if (!vtable)
10022 /* This might be a primary base, so have no vtable in this
10023 hierarchy. */
10024 return NULL_TREE;
10026 /* If we scribbled the construction vtable vptr into BINFO, clear it
10027 out now. */
10028 if (TREE_CODE (vtable) == TREE_LIST
10029 && (TREE_PURPOSE (vtable) == (tree) data))
10030 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
10032 return NULL_TREE;
10035 /* Build the construction vtable group for BINFO which is in the
10036 hierarchy dominated by T. */
10038 static void
10039 build_ctor_vtbl_group (tree binfo, tree t)
10041 tree type;
10042 tree vtbl;
10043 tree id;
10044 tree vbase;
10045 vec<constructor_elt, va_gc> *v;
10047 /* See if we've already created this construction vtable group. */
10048 id = mangle_ctor_vtbl_for_type (t, binfo);
10049 if (get_global_binding (id))
10050 return;
10052 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
10053 /* Build a version of VTBL (with the wrong type) for use in
10054 constructing the addresses of secondary vtables in the
10055 construction vtable group. */
10056 vtbl = build_vtable (t, id, ptr_type_node);
10058 /* Don't export construction vtables from shared libraries. Even on
10059 targets that don't support hidden visibility, this tells
10060 can_refer_decl_in_current_unit_p not to assume that it's safe to
10061 access from a different compilation unit (bz 54314). */
10062 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
10063 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
10065 v = NULL;
10066 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
10067 binfo, vtbl, t, &v);
10069 /* Add the vtables for each of our virtual bases using the vbase in T
10070 binfo. */
10071 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10072 vbase;
10073 vbase = TREE_CHAIN (vbase))
10075 tree b;
10077 if (!BINFO_VIRTUAL_P (vbase))
10078 continue;
10079 b = copied_binfo (vbase, binfo);
10081 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
10084 /* Figure out the type of the construction vtable. */
10085 type = build_array_of_n_type (vtable_entry_type, v->length ());
10086 layout_type (type);
10087 TREE_TYPE (vtbl) = type;
10088 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
10089 layout_decl (vtbl, 0);
10091 /* Initialize the construction vtable. */
10092 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
10093 initialize_artificial_var (vtbl, v);
10094 dump_vtable (t, binfo, vtbl);
10097 /* Add the vtbl initializers for BINFO (and its bases other than
10098 non-virtual primaries) to the list of INITS. BINFO is in the
10099 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
10100 the constructor the vtbl inits should be accumulated for. (If this
10101 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
10102 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
10103 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
10104 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
10105 but are not necessarily the same in terms of layout. */
10107 static void
10108 accumulate_vtbl_inits (tree binfo,
10109 tree orig_binfo,
10110 tree rtti_binfo,
10111 tree vtbl,
10112 tree t,
10113 vec<constructor_elt, va_gc> **inits)
10115 int i;
10116 tree base_binfo;
10117 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10119 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
10121 /* If it doesn't have a vptr, we don't do anything. */
10122 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10123 return;
10125 /* If we're building a construction vtable, we're not interested in
10126 subobjects that don't require construction vtables. */
10127 if (ctor_vtbl_p
10128 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10129 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
10130 return;
10132 /* Build the initializers for the BINFO-in-T vtable. */
10133 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
10135 /* Walk the BINFO and its bases. We walk in preorder so that as we
10136 initialize each vtable we can figure out at what offset the
10137 secondary vtable lies from the primary vtable. We can't use
10138 dfs_walk here because we need to iterate through bases of BINFO
10139 and RTTI_BINFO simultaneously. */
10140 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10142 /* Skip virtual bases. */
10143 if (BINFO_VIRTUAL_P (base_binfo))
10144 continue;
10145 accumulate_vtbl_inits (base_binfo,
10146 BINFO_BASE_BINFO (orig_binfo, i),
10147 rtti_binfo, vtbl, t,
10148 inits);
10152 /* Called from accumulate_vtbl_inits. Adds the initializers for the
10153 BINFO vtable to L. */
10155 static void
10156 dfs_accumulate_vtbl_inits (tree binfo,
10157 tree orig_binfo,
10158 tree rtti_binfo,
10159 tree orig_vtbl,
10160 tree t,
10161 vec<constructor_elt, va_gc> **l)
10163 tree vtbl = NULL_TREE;
10164 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10165 int n_inits;
10167 if (ctor_vtbl_p
10168 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
10170 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
10171 primary virtual base. If it is not the same primary in
10172 the hierarchy of T, we'll need to generate a ctor vtable
10173 for it, to place at its location in T. If it is the same
10174 primary, we still need a VTT entry for the vtable, but it
10175 should point to the ctor vtable for the base it is a
10176 primary for within the sub-hierarchy of RTTI_BINFO.
10178 There are three possible cases:
10180 1) We are in the same place.
10181 2) We are a primary base within a lost primary virtual base of
10182 RTTI_BINFO.
10183 3) We are primary to something not a base of RTTI_BINFO. */
10185 tree b;
10186 tree last = NULL_TREE;
10188 /* First, look through the bases we are primary to for RTTI_BINFO
10189 or a virtual base. */
10190 b = binfo;
10191 while (BINFO_PRIMARY_P (b))
10193 b = BINFO_INHERITANCE_CHAIN (b);
10194 last = b;
10195 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10196 goto found;
10198 /* If we run out of primary links, keep looking down our
10199 inheritance chain; we might be an indirect primary. */
10200 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
10201 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10202 break;
10203 found:
10205 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
10206 base B and it is a base of RTTI_BINFO, this is case 2. In
10207 either case, we share our vtable with LAST, i.e. the
10208 derived-most base within B of which we are a primary. */
10209 if (b == rtti_binfo
10210 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
10211 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
10212 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
10213 binfo_ctor_vtable after everything's been set up. */
10214 vtbl = last;
10216 /* Otherwise, this is case 3 and we get our own. */
10218 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
10219 return;
10221 n_inits = vec_safe_length (*l);
10223 if (!vtbl)
10225 tree index;
10226 int non_fn_entries;
10228 /* Add the initializer for this vtable. */
10229 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
10230 &non_fn_entries, l);
10232 /* Figure out the position to which the VPTR should point. */
10233 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
10234 index = size_binop (MULT_EXPR,
10235 TYPE_SIZE_UNIT (vtable_entry_type),
10236 size_int (non_fn_entries + n_inits));
10237 vtbl = fold_build_pointer_plus (vtbl, index);
10240 if (ctor_vtbl_p)
10241 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
10242 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
10243 straighten this out. */
10244 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
10245 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
10246 /* Throw away any unneeded intializers. */
10247 (*l)->truncate (n_inits);
10248 else
10249 /* For an ordinary vtable, set BINFO_VTABLE. */
10250 BINFO_VTABLE (binfo) = vtbl;
10253 static GTY(()) tree abort_fndecl_addr;
10254 static GTY(()) tree dvirt_fn;
10256 /* Construct the initializer for BINFO's virtual function table. BINFO
10257 is part of the hierarchy dominated by T. If we're building a
10258 construction vtable, the ORIG_BINFO is the binfo we should use to
10259 find the actual function pointers to put in the vtable - but they
10260 can be overridden on the path to most-derived in the graph that
10261 ORIG_BINFO belongs. Otherwise,
10262 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
10263 BINFO that should be indicated by the RTTI information in the
10264 vtable; it will be a base class of T, rather than T itself, if we
10265 are building a construction vtable.
10267 The value returned is a TREE_LIST suitable for wrapping in a
10268 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10269 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10270 number of non-function entries in the vtable.
10272 It might seem that this function should never be called with a
10273 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10274 base is always subsumed by a derived class vtable. However, when
10275 we are building construction vtables, we do build vtables for
10276 primary bases; we need these while the primary base is being
10277 constructed. */
10279 static void
10280 build_vtbl_initializer (tree binfo,
10281 tree orig_binfo,
10282 tree t,
10283 tree rtti_binfo,
10284 int* non_fn_entries_p,
10285 vec<constructor_elt, va_gc> **inits)
10287 tree v;
10288 vtbl_init_data vid;
10289 unsigned ix, jx;
10290 tree vbinfo;
10291 vec<tree, va_gc> *vbases;
10292 constructor_elt *e;
10294 /* Initialize VID. */
10295 memset (&vid, 0, sizeof (vid));
10296 vid.binfo = binfo;
10297 vid.derived = t;
10298 vid.rtti_binfo = rtti_binfo;
10299 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10300 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10301 vid.generate_vcall_entries = true;
10302 /* The first vbase or vcall offset is at index -3 in the vtable. */
10303 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10305 /* Add entries to the vtable for RTTI. */
10306 build_rtti_vtbl_entries (binfo, &vid);
10308 /* Create an array for keeping track of the functions we've
10309 processed. When we see multiple functions with the same
10310 signature, we share the vcall offsets. */
10311 vec_alloc (vid.fns, 32);
10312 /* Add the vcall and vbase offset entries. */
10313 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10315 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10316 build_vbase_offset_vtbl_entries. */
10317 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10318 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10319 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10321 /* If the target requires padding between data entries, add that now. */
10322 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10324 int n_entries = vec_safe_length (vid.inits);
10326 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10327 true);
10329 /* Move data entries into their new positions and add padding
10330 after the new positions. Iterate backwards so we don't
10331 overwrite entries that we would need to process later. */
10332 for (ix = n_entries - 1;
10333 vid.inits->iterate (ix, &e);
10334 ix--)
10336 int j;
10337 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10338 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10340 (*vid.inits)[new_position] = *e;
10342 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10344 constructor_elt *f = &(*vid.inits)[new_position - j];
10345 f->index = NULL_TREE;
10346 f->value = build1 (NOP_EXPR, vtable_entry_type,
10347 null_pointer_node);
10352 if (non_fn_entries_p)
10353 *non_fn_entries_p = vec_safe_length (vid.inits);
10355 /* The initializers for virtual functions were built up in reverse
10356 order. Straighten them out and add them to the running list in one
10357 step. */
10358 jx = vec_safe_length (*inits);
10359 vec_safe_grow (*inits, jx + vid.inits->length (), true);
10361 for (ix = vid.inits->length () - 1;
10362 vid.inits->iterate (ix, &e);
10363 ix--, jx++)
10364 (**inits)[jx] = *e;
10366 /* Go through all the ordinary virtual functions, building up
10367 initializers. */
10368 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10370 tree delta;
10371 tree vcall_index;
10372 tree fn, fn_original;
10373 tree init = NULL_TREE;
10375 fn = BV_FN (v);
10376 fn_original = fn;
10377 if (DECL_THUNK_P (fn))
10379 if (!DECL_NAME (fn))
10380 finish_thunk (fn);
10381 if (THUNK_ALIAS (fn))
10383 fn = THUNK_ALIAS (fn);
10384 BV_FN (v) = fn;
10386 fn_original = THUNK_TARGET (fn);
10389 /* If the only definition of this function signature along our
10390 primary base chain is from a lost primary, this vtable slot will
10391 never be used, so just zero it out. This is important to avoid
10392 requiring extra thunks which cannot be generated with the function.
10394 We first check this in update_vtable_entry_for_fn, so we handle
10395 restored primary bases properly; we also need to do it here so we
10396 zero out unused slots in ctor vtables, rather than filling them
10397 with erroneous values (though harmless, apart from relocation
10398 costs). */
10399 if (BV_LOST_PRIMARY (v))
10400 init = size_zero_node;
10402 if (! init)
10404 /* Pull the offset for `this', and the function to call, out of
10405 the list. */
10406 delta = BV_DELTA (v);
10407 vcall_index = BV_VCALL_INDEX (v);
10409 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10410 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10412 /* You can't call an abstract virtual function; it's abstract.
10413 So, we replace these functions with __pure_virtual. */
10414 if (DECL_PURE_VIRTUAL_P (fn_original))
10416 fn = abort_fndecl;
10417 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10419 if (abort_fndecl_addr == NULL)
10420 abort_fndecl_addr
10421 = fold_convert (vfunc_ptr_type_node,
10422 build_fold_addr_expr (fn));
10423 init = abort_fndecl_addr;
10426 /* Likewise for deleted virtuals. */
10427 else if (DECL_DELETED_FN (fn_original))
10429 if (!dvirt_fn)
10431 tree name = get_identifier ("__cxa_deleted_virtual");
10432 dvirt_fn = get_global_binding (name);
10433 if (!dvirt_fn)
10434 dvirt_fn = push_library_fn
10435 (name,
10436 build_function_type_list (void_type_node, NULL_TREE),
10437 NULL_TREE, ECF_NORETURN | ECF_COLD);
10439 fn = dvirt_fn;
10440 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10441 init = fold_convert (vfunc_ptr_type_node,
10442 build_fold_addr_expr (fn));
10444 else
10446 if (!integer_zerop (delta) || vcall_index)
10448 fn = make_thunk (fn, /*this_adjusting=*/1,
10449 delta, vcall_index);
10450 if (!DECL_NAME (fn))
10451 finish_thunk (fn);
10453 /* Take the address of the function, considering it to be of an
10454 appropriate generic type. */
10455 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10456 init = fold_convert (vfunc_ptr_type_node,
10457 build_fold_addr_expr (fn));
10458 /* Don't refer to a virtual destructor from a constructor
10459 vtable or a vtable for an abstract class, since destroying
10460 an object under construction is undefined behavior and we
10461 don't want it to be considered a candidate for speculative
10462 devirtualization. But do create the thunk for ABI
10463 compliance. */
10464 if (DECL_DESTRUCTOR_P (fn_original)
10465 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10466 || orig_binfo != binfo))
10467 init = size_zero_node;
10471 /* And add it to the chain of initializers. */
10472 if (TARGET_VTABLE_USES_DESCRIPTORS)
10474 int i;
10475 if (init == size_zero_node)
10476 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10477 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10478 else
10479 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10481 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10482 fn, build_int_cst (NULL_TREE, i));
10483 TREE_CONSTANT (fdesc) = 1;
10485 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
10488 else
10489 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10493 /* Adds to vid->inits the initializers for the vbase and vcall
10494 offsets in BINFO, which is in the hierarchy dominated by T. */
10496 static void
10497 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10499 tree b;
10501 /* If this is a derived class, we must first create entries
10502 corresponding to the primary base class. */
10503 b = get_primary_binfo (binfo);
10504 if (b)
10505 build_vcall_and_vbase_vtbl_entries (b, vid);
10507 /* Add the vbase entries for this base. */
10508 build_vbase_offset_vtbl_entries (binfo, vid);
10509 /* Add the vcall entries for this base. */
10510 build_vcall_offset_vtbl_entries (binfo, vid);
10513 /* Returns the initializers for the vbase offset entries in the vtable
10514 for BINFO (which is part of the class hierarchy dominated by T), in
10515 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10516 where the next vbase offset will go. */
10518 static void
10519 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10521 tree vbase;
10522 tree t;
10523 tree non_primary_binfo;
10525 /* If there are no virtual baseclasses, then there is nothing to
10526 do. */
10527 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10528 return;
10530 t = vid->derived;
10532 /* We might be a primary base class. Go up the inheritance hierarchy
10533 until we find the most derived class of which we are a primary base:
10534 it is the offset of that which we need to use. */
10535 non_primary_binfo = binfo;
10536 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10538 tree b;
10540 /* If we have reached a virtual base, then it must be a primary
10541 base (possibly multi-level) of vid->binfo, or we wouldn't
10542 have called build_vcall_and_vbase_vtbl_entries for it. But it
10543 might be a lost primary, so just skip down to vid->binfo. */
10544 if (BINFO_VIRTUAL_P (non_primary_binfo))
10546 non_primary_binfo = vid->binfo;
10547 break;
10550 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10551 if (get_primary_binfo (b) != non_primary_binfo)
10552 break;
10553 non_primary_binfo = b;
10556 /* Go through the virtual bases, adding the offsets. */
10557 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10558 vbase;
10559 vbase = TREE_CHAIN (vbase))
10561 tree b;
10562 tree delta;
10564 if (!BINFO_VIRTUAL_P (vbase))
10565 continue;
10567 /* Find the instance of this virtual base in the complete
10568 object. */
10569 b = copied_binfo (vbase, binfo);
10571 /* If we've already got an offset for this virtual base, we
10572 don't need another one. */
10573 if (BINFO_VTABLE_PATH_MARKED (b))
10574 continue;
10575 BINFO_VTABLE_PATH_MARKED (b) = 1;
10577 /* Figure out where we can find this vbase offset. */
10578 delta = size_binop (MULT_EXPR,
10579 vid->index,
10580 fold_convert (ssizetype,
10581 TYPE_SIZE_UNIT (vtable_entry_type)));
10582 if (vid->primary_vtbl_p)
10583 BINFO_VPTR_FIELD (b) = delta;
10585 if (binfo != TYPE_BINFO (t))
10586 /* The vbase offset had better be the same. */
10587 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10589 /* The next vbase will come at a more negative offset. */
10590 vid->index = size_binop (MINUS_EXPR, vid->index,
10591 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10593 /* The initializer is the delta from BINFO to this virtual base.
10594 The vbase offsets go in reverse inheritance-graph order, and
10595 we are walking in inheritance graph order so these end up in
10596 the right order. */
10597 delta = size_diffop_loc (input_location,
10598 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10600 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10601 fold_build1_loc (input_location, NOP_EXPR,
10602 vtable_entry_type, delta));
10606 /* Adds the initializers for the vcall offset entries in the vtable
10607 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10608 to VID->INITS. */
10610 static void
10611 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10613 /* We only need these entries if this base is a virtual base. We
10614 compute the indices -- but do not add to the vtable -- when
10615 building the main vtable for a class. */
10616 if (binfo == TYPE_BINFO (vid->derived)
10617 || (BINFO_VIRTUAL_P (binfo)
10618 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10619 correspond to VID->DERIVED), we are building a primary
10620 construction virtual table. Since this is a primary
10621 virtual table, we do not need the vcall offsets for
10622 BINFO. */
10623 && binfo != vid->rtti_binfo))
10625 /* We need a vcall offset for each of the virtual functions in this
10626 vtable. For example:
10628 class A { virtual void f (); };
10629 class B1 : virtual public A { virtual void f (); };
10630 class B2 : virtual public A { virtual void f (); };
10631 class C: public B1, public B2 { virtual void f (); };
10633 A C object has a primary base of B1, which has a primary base of A. A
10634 C also has a secondary base of B2, which no longer has a primary base
10635 of A. So the B2-in-C construction vtable needs a secondary vtable for
10636 A, which will adjust the A* to a B2* to call f. We have no way of
10637 knowing what (or even whether) this offset will be when we define B2,
10638 so we store this "vcall offset" in the A sub-vtable and look it up in
10639 a "virtual thunk" for B2::f.
10641 We need entries for all the functions in our primary vtable and
10642 in our non-virtual bases' secondary vtables. */
10643 vid->vbase = binfo;
10644 /* If we are just computing the vcall indices -- but do not need
10645 the actual entries -- not that. */
10646 if (!BINFO_VIRTUAL_P (binfo))
10647 vid->generate_vcall_entries = false;
10648 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10649 add_vcall_offset_vtbl_entries_r (binfo, vid);
10653 /* Build vcall offsets, starting with those for BINFO. */
10655 static void
10656 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10658 int i;
10659 tree primary_binfo;
10660 tree base_binfo;
10662 /* Don't walk into virtual bases -- except, of course, for the
10663 virtual base for which we are building vcall offsets. Any
10664 primary virtual base will have already had its offsets generated
10665 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10666 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10667 return;
10669 /* If BINFO has a primary base, process it first. */
10670 primary_binfo = get_primary_binfo (binfo);
10671 if (primary_binfo)
10672 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10674 /* Add BINFO itself to the list. */
10675 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10677 /* Scan the non-primary bases of BINFO. */
10678 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10679 if (base_binfo != primary_binfo)
10680 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10683 /* Called from build_vcall_offset_vtbl_entries_r. */
10685 static void
10686 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10688 /* Make entries for the rest of the virtuals. */
10689 tree orig_fn;
10691 /* The ABI requires that the methods be processed in declaration
10692 order. */
10693 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10694 orig_fn;
10695 orig_fn = DECL_CHAIN (orig_fn))
10696 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10697 add_vcall_offset (orig_fn, binfo, vid);
10700 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10702 static void
10703 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10705 size_t i;
10706 tree vcall_offset;
10707 tree derived_entry;
10709 /* If there is already an entry for a function with the same
10710 signature as FN, then we do not need a second vcall offset.
10711 Check the list of functions already present in the derived
10712 class vtable. */
10713 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10715 if (same_signature_p (derived_entry, orig_fn)
10716 /* We only use one vcall offset for virtual destructors,
10717 even though there are two virtual table entries. */
10718 || (DECL_DESTRUCTOR_P (derived_entry)
10719 && DECL_DESTRUCTOR_P (orig_fn)))
10720 return;
10723 /* If we are building these vcall offsets as part of building
10724 the vtable for the most derived class, remember the vcall
10725 offset. */
10726 if (vid->binfo == TYPE_BINFO (vid->derived))
10728 tree_pair_s elt = {orig_fn, vid->index};
10729 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10732 /* The next vcall offset will be found at a more negative
10733 offset. */
10734 vid->index = size_binop (MINUS_EXPR, vid->index,
10735 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10737 /* Keep track of this function. */
10738 vec_safe_push (vid->fns, orig_fn);
10740 if (vid->generate_vcall_entries)
10742 tree base;
10743 tree fn;
10745 /* Find the overriding function. */
10746 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10747 if (fn == error_mark_node)
10748 vcall_offset = build_zero_cst (vtable_entry_type);
10749 else
10751 base = TREE_VALUE (fn);
10753 /* The vbase we're working on is a primary base of
10754 vid->binfo. But it might be a lost primary, so its
10755 BINFO_OFFSET might be wrong, so we just use the
10756 BINFO_OFFSET from vid->binfo. */
10757 vcall_offset = size_diffop_loc (input_location,
10758 BINFO_OFFSET (base),
10759 BINFO_OFFSET (vid->binfo));
10760 vcall_offset = fold_build1_loc (input_location,
10761 NOP_EXPR, vtable_entry_type,
10762 vcall_offset);
10764 /* Add the initializer to the vtable. */
10765 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10769 /* Return vtbl initializers for the RTTI entries corresponding to the
10770 BINFO's vtable. The RTTI entries should indicate the object given
10771 by VID->rtti_binfo. */
10773 static void
10774 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10776 tree b;
10777 tree t;
10778 tree offset;
10779 tree decl;
10780 tree init;
10782 t = BINFO_TYPE (vid->rtti_binfo);
10784 /* To find the complete object, we will first convert to our most
10785 primary base, and then add the offset in the vtbl to that value. */
10786 b = most_primary_binfo (binfo);
10787 offset = size_diffop_loc (input_location,
10788 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10790 /* The second entry is the address of the typeinfo object. */
10791 if (flag_rtti)
10792 decl = build_address (get_tinfo_decl (t));
10793 else
10794 decl = integer_zero_node;
10796 /* Convert the declaration to a type that can be stored in the
10797 vtable. */
10798 init = build_nop (vfunc_ptr_type_node, decl);
10799 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10801 /* Add the offset-to-top entry. It comes earlier in the vtable than
10802 the typeinfo entry. Convert the offset to look like a
10803 function pointer, so that we can put it in the vtable. */
10804 init = build_nop (vfunc_ptr_type_node, offset);
10805 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10808 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10809 accessibility. */
10811 bool
10812 uniquely_derived_from_p (tree parent, tree type)
10814 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10815 return base && base != error_mark_node;
10818 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10820 bool
10821 publicly_uniquely_derived_p (tree parent, tree type)
10823 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10824 NULL, tf_none);
10825 return base && base != error_mark_node;
10828 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10829 class between them, if any. */
10831 tree
10832 common_enclosing_class (tree ctx1, tree ctx2)
10834 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10835 return NULL_TREE;
10836 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10837 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10838 if (ctx1 == ctx2)
10839 return ctx1;
10840 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10841 TYPE_MARKED_P (t) = true;
10842 tree found = NULL_TREE;
10843 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10844 if (TYPE_MARKED_P (t))
10846 found = t;
10847 break;
10849 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10850 TYPE_MARKED_P (t) = false;
10851 return found;
10854 #include "gt-cp-class.h"