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)
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. */
26 #include "coretypes.h"
29 #include "stringpool.h"
31 #include "stor-layout.h"
41 /* Id for dumping the class hierarchy. */
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. */
57 /* The _TYPE node for the class. */
60 /* The access specifier pending for new declarations in the scope of
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
70 }* class_stack_node_t
;
74 /* The base for which we're building initializers. */
76 /* The type of the most-derived type. */
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
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. */
87 /* The functions in vbase for which we have already provided vcall
89 vec
<tree
, va_gc
> *fns
;
90 /* The vtable index of the next vcall or vbase offset. */
92 /* Nonzero if we are building the initializer for the primary
95 /* Nonzero if we are building the initializer for a construction
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. */
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
> **,
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
,
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. */
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
;
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
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
);
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
252 build_base_path (enum tree_code code
,
256 tsubst_flags_t complain
)
258 tree v_binfo
= NULL_TREE
;
259 tree d_binfo
= NULL_TREE
;
263 tree null_test
= NULL
;
264 tree ptr_target_type
;
266 int want_pointer
= TYPE_PTR_P (TREE_TYPE (expr
));
267 bool has_empty
= 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
))
277 if (is_empty_class (BINFO_TYPE (probe
)))
279 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
283 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
285 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
286 if (dependent_type_p (probe
))
287 if (tree open
= currently_open_class (probe
))
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
)
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
)))
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
));
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
));
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
));
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
);
357 rvalue
= !lvalue_p (expr
);
358 /* This must happen before the call to save_expr. */
359 expr
= cp_build_addr_expr (expr
, complain
);
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. */
384 expr
= build_nop (ptr_target_type
, expr
);
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;
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. */
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. */
435 expr
= cp_build_fold_indirect_ref (expr
);
436 expr
= build_simple_base_path (expr
, binfo
);
437 if (rvalue
&& lvalue_p (expr
))
440 expr
= build_address (expr
);
441 target_type
= TREE_TYPE (expr
);
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. */
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
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
);
467 if (sanitize_flags_p (SANITIZE_VPTR
)
468 && fixed_type_p
== 0)
470 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
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
),
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
500 offset
= build_if_in_charge
501 (convert_to_integer (ptrdiff_type_node
, BINFO_OFFSET (binfo
)),
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
);
520 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
525 expr
= cp_build_fold_indirect_ref (expr
);
532 /* Wrap EXPR in a null test. */
533 expr
= build_if_nonnull (null_test
, expr
, complain
);
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. */
543 build_simple_base_path (tree expr
, tree binfo
)
545 tree type
= BINFO_TYPE (binfo
);
546 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
549 if (d_binfo
== NULL_TREE
)
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
559 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
561 expr
= cp_build_fold_indirect_ref (temp
);
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;
602 /* Didn't find the base field?!? */
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. */
614 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
615 tsubst_flags_t complain
)
620 if (TYPE_PTR_P (TREE_TYPE (object
)))
622 object_type
= TREE_TYPE (TREE_TYPE (object
));
623 type
= TREE_TYPE (type
);
626 object_type
= TREE_TYPE (object
);
628 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
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. */
642 convert_to_base_statically (tree expr
, tree base
)
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
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
);
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. */
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
)
681 tree type
= TREE_TYPE (expr
);
682 if (!POINTER_TYPE_P (type
))
684 type
= TREE_TYPE (type
);
685 if (!is_empty_class (type
))
688 tree fromtype
= TREE_TYPE (expr
);
689 if (!POINTER_TYPE_P (fromtype
))
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
));
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. */
735 build_vtbl_ref (tree instance
, tree idx
)
738 tree vtbl
= NULL_TREE
;
740 /* Try to figure out what a reference refers to, and
741 access its virtual function table directly. */
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
));
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
);
765 /* Given a stable object pointer INSTANCE_PTR, return an expression which
766 yields a function pointer corresponding to vtable element INDEX. */
769 build_vfn_ref (tree instance_ptr
, tree idx
)
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
));
788 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
789 for the given TYPE. */
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
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. */
817 build_vtable (tree class_type
, tree name
, tree vtable_type
)
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:
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;
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. */
870 get_vtable_decl (tree type
, int complete
)
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
;
882 DECL_EXTERNAL (decl
) = 1;
883 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
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
896 build_primary_vtable (tree binfo
, tree type
)
901 decl
= get_vtable_decl (type
, /*complete=*/0);
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. */
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
));
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
));
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
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
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
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
;
964 /* Create a new vtable for BINFO which is the hierarchy dominated by
965 T. Return nonzero if we actually created a new vtable. */
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
);
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. */
990 modify_vtable_entry (tree t
,
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
);
1015 BV_DELTA (v
) = delta
;
1016 BV_VCALL_INDEX (v
) = NULL_TREE
;
1022 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1023 METHOD is being injected via a using_decl. Returns true if the
1024 method could be added to the method vec. */
1027 add_method (tree type
, tree method
, bool via_using
)
1029 if (method
== error_mark_node
)
1032 gcc_assert (!DECL_EXTERN_C_P (method
));
1034 tree
*slot
= find_member_slot (type
, DECL_NAME (method
));
1035 tree current_fns
= slot
? *slot
: NULL_TREE
;
1040 /* Check to see if we've already got this method. */
1041 for (ovl_iterator
iter (current_fns
); iter
; ++iter
)
1045 if (TREE_CODE (fn
) != TREE_CODE (method
))
1048 /* Two using-declarations can coexist, we'll complain about ambiguity in
1049 overload resolution. */
1050 if (via_using
&& iter
.using_p ()
1051 /* Except handle inherited constructors specially. */
1052 && ! DECL_CONSTRUCTOR_P (fn
))
1055 /* Don't add the same one twice. */
1060 /* [over.load] Member function declarations with the
1061 same name and the same parameter types cannot be
1062 overloaded if any of them is a static member
1063 function declaration.
1065 [over.load] Member function declarations with the same name and
1066 the same parameter-type-list as well as member function template
1067 declarations with the same name, the same parameter-type-list, and
1068 the same template parameter lists cannot be overloaded if any of
1069 them, but not all, have a ref-qualifier.
1071 [namespace.udecl] When a using-declaration brings names
1072 from a base class into a derived class scope, member
1073 functions in the derived class override and/or hide member
1074 functions with the same name and parameter types in a base
1075 class (rather than conflicting). */
1076 tree fn_type
= TREE_TYPE (fn
);
1077 tree method_type
= TREE_TYPE (method
);
1079 /* Compare the quals on the 'this' parm. Don't compare
1080 the whole types, as used functions are treated as
1081 coming from the using class in overload resolution. */
1082 if (! DECL_STATIC_FUNCTION_P (fn
)
1083 && ! DECL_STATIC_FUNCTION_P (method
)
1084 /* Either both or neither need to be ref-qualified for
1085 differing quals to allow overloading. */
1086 && (FUNCTION_REF_QUALIFIED (fn_type
)
1087 == FUNCTION_REF_QUALIFIED (method_type
))
1088 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1089 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1093 tree real_method
= method
;
1095 /* Templates and conversion ops must match return types. */
1096 if ((DECL_CONV_FN_P (fn
) || TREE_CODE (fn
) == TEMPLATE_DECL
)
1097 && !same_type_p (TREE_TYPE (fn_type
), TREE_TYPE (method_type
)))
1100 /* For templates, the template parameters must be identical. */
1101 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1103 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1104 DECL_TEMPLATE_PARMS (method
)))
1107 real_fn
= DECL_TEMPLATE_RESULT (fn
);
1108 real_method
= DECL_TEMPLATE_RESULT (method
);
1111 tree parms1
= TYPE_ARG_TYPES (fn_type
);
1112 tree parms2
= TYPE_ARG_TYPES (method_type
);
1113 if (! DECL_STATIC_FUNCTION_P (real_fn
))
1114 parms1
= TREE_CHAIN (parms1
);
1115 if (! DECL_STATIC_FUNCTION_P (real_method
))
1116 parms2
= TREE_CHAIN (parms2
);
1118 /* Bring back parameters omitted from an inherited ctor. The
1119 method and the function can have different omittedness. */
1120 if (ctor_omit_inherited_parms (real_fn
))
1121 parms1
= FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn
));
1122 if (ctor_omit_inherited_parms (real_method
))
1123 parms2
= (FUNCTION_FIRST_USER_PARMTYPE
1124 (DECL_CLONED_FUNCTION (real_method
)));
1126 if (!compparms (parms1
, parms2
))
1129 if (!equivalently_constrained (fn
, method
))
1131 if (processing_template_decl
)
1132 /* We can't check satisfaction in dependent context, wait until
1133 the class is instantiated. */
1136 special_function_kind sfk
= special_memfn_p (method
);
1139 || DECL_INHERITED_CTOR (fn
)
1140 || TREE_CODE (fn
) == TEMPLATE_DECL
)
1141 /* Member function templates and non-special member functions
1142 coexist if they are not equivalently constrained. A member
1143 function is not hidden by an inherited constructor. */
1146 /* P0848: For special member functions, deleted, unsatisfied, or
1147 less constrained overloads are ineligible. We implement this
1148 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1149 use the notion of eligibility, and the selected destructor can
1150 be deleted, but removing unsatisfied or less constrained
1151 overloads has the same effect as overload resolution. */
1152 bool dtor
= (sfk
== sfk_destructor
);
1154 losem
= ((!dtor
&& DECL_DELETED_FN (method
))
1155 || !constraints_satisfied_p (method
));
1156 bool losef
= ((!dtor
&& DECL_DELETED_FN (fn
))
1157 || !constraints_satisfied_p (fn
));
1160 win
= losem
- losef
;
1162 win
= more_constrained (fn
, method
);
1164 /* Leave FN in the method vec, discard METHOD. */
1168 /* Remove FN, add METHOD. */
1169 current_fns
= iter
.remove_node (current_fns
);
1173 /* Let them coexist for now. */
1177 /* If these are versions of the same function, process and
1179 if (TREE_CODE (fn
) == FUNCTION_DECL
1180 && maybe_version_functions (method
, fn
, true))
1183 if (DECL_INHERITED_CTOR (method
))
1185 if (!DECL_INHERITED_CTOR (fn
))
1186 /* Defer to the other function. */
1189 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1190 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1191 if (flag_new_inheriting_ctors
)
1195 /* Inheriting the same constructor along different
1196 paths, combine them. */
1197 SET_DECL_INHERITED_CTOR
1198 (fn
, ovl_make (DECL_INHERITED_CTOR (method
),
1199 DECL_INHERITED_CTOR (fn
)));
1200 /* And discard the new one. */
1204 /* Inherited ctors can coexist until overload
1209 error_at (DECL_SOURCE_LOCATION (method
),
1210 "%q#D conflicts with version inherited from %qT",
1212 inform (DECL_SOURCE_LOCATION (fn
),
1213 "version inherited from %qT declared here",
1219 /* Defer to the local function. */
1221 else if (iter
.using_p ()
1222 || (flag_new_inheriting_ctors
1223 && DECL_INHERITED_CTOR (fn
)))
1225 /* Remove the inherited function. */
1226 current_fns
= iter
.remove_node (current_fns
);
1231 error_at (DECL_SOURCE_LOCATION (method
),
1232 "%q#D cannot be overloaded with %q#D", method
, fn
);
1233 inform (DECL_SOURCE_LOCATION (fn
),
1234 "previous declaration %q#D", fn
);
1239 current_fns
= ovl_insert (method
, current_fns
, via_using
);
1241 if (!COMPLETE_TYPE_P (type
) && !DECL_CONV_FN_P (method
)
1242 && !push_class_level_binding (DECL_NAME (method
), current_fns
))
1246 slot
= add_member_slot (type
, DECL_NAME (method
));
1248 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1249 grok_special_member_properties (method
);
1251 *slot
= current_fns
;
1256 /* Subroutines of finish_struct. */
1258 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1259 legit, otherwise return 0. */
1262 alter_access (tree t
, tree fdecl
, tree access
)
1266 retrofit_lang_decl (fdecl
);
1268 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1270 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1273 if (TREE_VALUE (elem
) != access
)
1275 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1276 error ("conflicting access specifications for method"
1277 " %q+D, ignored", TREE_TYPE (fdecl
));
1279 error ("conflicting access specifications for field %qE, ignored",
1284 /* They're changing the access to the same thing they changed
1285 it to before. That's OK. */
1291 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1292 tf_warning_or_error
);
1293 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1299 /* Return the access node for DECL's access in its enclosing class. */
1302 declared_access (tree decl
)
1304 return (TREE_PRIVATE (decl
) ? access_private_node
1305 : TREE_PROTECTED (decl
) ? access_protected_node
1306 : access_public_node
);
1309 /* If DECL is a non-dependent using of non-ctor function members, push them
1310 and return true, otherwise return false. Called from
1311 finish_member_declaration. */
1314 maybe_push_used_methods (tree decl
)
1316 if (TREE_CODE (decl
) != USING_DECL
)
1318 tree used
= strip_using_decl (decl
);
1319 if (!used
|| !is_overloaded_fn (used
))
1322 /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1323 works within the class body. */
1324 for (tree f
: ovl_range (used
))
1326 if (DECL_CONSTRUCTOR_P (f
))
1327 /* Inheriting constructors are handled separately. */
1330 bool added
= add_method (current_class_type
, f
, true);
1333 alter_access (current_class_type
, f
, current_access_specifier
);
1335 /* If add_method returns false because f was already declared, look
1336 for a duplicate using-declaration. */
1338 for (tree d
= TYPE_FIELDS (current_class_type
); d
; d
= DECL_CHAIN (d
))
1339 if (TREE_CODE (d
) == USING_DECL
1340 && DECL_NAME (d
) == DECL_NAME (decl
)
1341 && same_type_p (USING_DECL_SCOPE (d
), USING_DECL_SCOPE (decl
)))
1343 diagnose_name_conflict (decl
, d
);
1350 /* Process the USING_DECL, which is a member of T. */
1353 handle_using_decl (tree using_decl
, tree t
)
1355 tree decl
= USING_DECL_DECLS (using_decl
);
1357 gcc_assert (!processing_template_decl
&& decl
);
1359 cp_emit_debug_info_for_using (decl
, t
);
1361 if (is_overloaded_fn (decl
))
1362 /* Handled in maybe_push_used_methods. */
1365 tree name
= DECL_NAME (using_decl
);
1366 tree old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1367 tf_warning_or_error
);
1370 old_value
= OVL_FIRST (old_value
);
1372 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1375 old_value
= NULL_TREE
;
1380 else if (is_overloaded_fn (old_value
))
1382 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1383 "because of local method %q#D with same name",
1384 using_decl
, t
, old_value
);
1385 inform (DECL_SOURCE_LOCATION (old_value
),
1386 "local method %q#D declared here", old_value
);
1389 else if (!DECL_ARTIFICIAL (old_value
))
1391 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1392 "because of local member %q#D with same name",
1393 using_decl
, t
, old_value
);
1394 inform (DECL_SOURCE_LOCATION (old_value
),
1395 "local member %q#D declared here", old_value
);
1399 iloc_sentinel
ils (DECL_SOURCE_LOCATION (using_decl
));
1400 tree access
= declared_access (using_decl
);
1402 /* Make type T see field decl FDECL with access ACCESS. */
1403 if (USING_DECL_UNRELATED_P (using_decl
))
1405 /* C++20 using enum can import non-inherited enumerators into class
1406 scope. We implement that by making a copy of the CONST_DECL for which
1407 CONST_DECL_USING_P is true. */
1408 gcc_assert (TREE_CODE (decl
) == CONST_DECL
);
1410 auto cas
= make_temp_override (current_access_specifier
, access
);
1411 tree copy
= copy_decl (decl
);
1412 DECL_CONTEXT (copy
) = t
;
1413 DECL_ARTIFICIAL (copy
) = true;
1414 /* We emitted debug info for the USING_DECL above; make sure we don't
1415 also emit anything for this clone. */
1416 DECL_IGNORED_P (copy
) = true;
1417 DECL_SOURCE_LOCATION (copy
) = DECL_SOURCE_LOCATION (using_decl
);
1418 finish_member_declaration (copy
);
1419 DECL_ABSTRACT_ORIGIN (copy
) = decl
;
1422 alter_access (t
, decl
, access
);
1425 /* Data structure for find_abi_tags_r, below. */
1429 tree t
; // The type that we're checking for missing tags.
1430 tree subob
; // The subobject of T that we're getting tags from.
1431 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1434 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1435 in the context of P. TAG can be either an identifier (the DECL_NAME of
1436 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1439 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1441 if (!IDENTIFIER_MARKED (id
))
1443 if (p
->tags
!= error_mark_node
)
1445 /* We're collecting tags from template arguments or from
1446 the type of a variable or function return type. */
1447 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1449 /* Don't inherit this tag multiple times. */
1450 IDENTIFIER_MARKED (id
) = true;
1454 /* Tags inherited from type template arguments are only used
1455 to avoid warnings. */
1456 ABI_TAG_IMPLICIT (p
->tags
) = true;
1459 /* For functions and variables we want to warn, too. */
1462 /* Otherwise we're diagnosing missing tags. */
1463 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1465 auto_diagnostic_group d
;
1466 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1467 "that %qT (used in its return type) has",
1469 inform (location_of (*tp
), "%qT declared here", *tp
);
1471 else if (VAR_P (p
->t
))
1473 auto_diagnostic_group d
;
1474 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1475 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1476 inform (location_of (*tp
), "%qT declared here", *tp
);
1478 else if (TYPE_P (p
->subob
))
1480 auto_diagnostic_group d
;
1481 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1482 "that base %qT has", p
->t
, tag
, p
->subob
))
1483 inform (location_of (p
->subob
), "%qT declared here",
1488 auto_diagnostic_group d
;
1489 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1490 "that %qT (used in the type of %qD) has",
1491 p
->t
, tag
, *tp
, p
->subob
))
1493 inform (location_of (p
->subob
), "%qD declared here",
1495 inform (location_of (*tp
), "%qT declared here", *tp
);
1501 /* Find all the ABI tags in the attribute list ATTR and either call
1502 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1505 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1509 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1510 attr
= TREE_CHAIN (attr
))
1511 for (tree list
= TREE_VALUE (attr
); list
;
1512 list
= TREE_CHAIN (list
))
1514 tree tag
= TREE_VALUE (list
);
1515 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1517 check_tag (tag
, id
, tp
, p
);
1519 IDENTIFIER_MARKED (id
) = val
;
1523 /* Find all the ABI tags on T and its enclosing scopes and either call
1524 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1527 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1529 while (t
!= global_namespace
)
1534 attr
= TYPE_ATTRIBUTES (t
);
1535 t
= CP_TYPE_CONTEXT (t
);
1539 attr
= DECL_ATTRIBUTES (t
);
1540 t
= CP_DECL_CONTEXT (t
);
1542 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1546 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1547 types with ABI tags, add the corresponding identifiers to the VEC in
1548 *DATA and set IDENTIFIER_MARKED. */
1551 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1553 if (TYPE_P (*tp
) && *walk_subtrees
== 1 && flag_abi_version
!= 14)
1554 /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1557 if (!OVERLOAD_TYPE_P (*tp
))
1560 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1561 anyway, but let's make sure of it. */
1562 *walk_subtrees
= false;
1564 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1566 mark_or_check_tags (*tp
, tp
, p
, false);
1571 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1572 IDENTIFIER_MARKED on its ABI tags. */
1575 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1577 if (TYPE_P (*tp
) && *walk_subtrees
== 1 && flag_abi_version
!= 14)
1578 /* Tell cp_walk_subtrees to look though typedefs. */
1581 if (!OVERLOAD_TYPE_P (*tp
))
1584 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1585 anyway, but let's make sure of it. */
1586 *walk_subtrees
= false;
1588 bool *valp
= static_cast<bool*>(data
);
1590 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1595 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1599 mark_abi_tags (tree t
, bool val
)
1601 mark_or_check_tags (t
, NULL
, NULL
, val
);
1604 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1605 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1607 /* Template arguments are part of the signature. */
1608 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1609 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1611 tree arg
= TREE_VEC_ELT (level
, j
);
1612 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1615 if (TREE_CODE (t
) == FUNCTION_DECL
)
1616 /* A function's parameter types are part of the signature, so
1617 we don't need to inherit any tags that are also in them. */
1618 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1619 arg
= TREE_CHAIN (arg
))
1620 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1621 mark_abi_tags_r
, &val
);
1625 /* Check that T has all the ABI tags that subobject SUBOB has, or
1626 warn if not. If T is a (variable or function) declaration, also
1627 return any missing tags, and add them to T if JUST_CHECKING is false. */
1630 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1632 bool inherit
= DECL_P (t
);
1634 if (!inherit
&& !warn_abi_tag
)
1637 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1638 if (!TREE_PUBLIC (decl
))
1639 /* No need to worry about things local to this TU. */
1642 mark_abi_tags (t
, true);
1644 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1645 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1647 data
.tags
= NULL_TREE
;
1649 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1651 if (!(inherit
&& data
.tags
))
1652 /* We don't need to do anything with data.tags. */;
1653 else if (just_checking
)
1654 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1656 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1657 IDENTIFIER_MARKED (id
) = false;
1661 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1663 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1666 = tree_cons (abi_tag_identifier
, data
.tags
, DECL_ATTRIBUTES (t
));
1669 mark_abi_tags (t
, false);
1674 /* Check that DECL has all the ABI tags that are used in parts of its type
1675 that are not reflected in its mangled name. */
1678 check_abi_tags (tree decl
)
1681 check_abi_tags (decl
, TREE_TYPE (decl
));
1682 else if (TREE_CODE (decl
) == FUNCTION_DECL
1683 && !DECL_CONV_FN_P (decl
)
1684 && !mangle_return_type_p (decl
))
1685 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1688 /* Return any ABI tags that are used in parts of the type of DECL
1689 that are not reflected in its mangled name. This function is only
1690 used in backward-compatible mangling for ABI <11. */
1693 missing_abi_tags (tree decl
)
1696 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1697 else if (TREE_CODE (decl
) == FUNCTION_DECL
1698 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1699 that we can use this function for setting need_abi_warning
1700 regardless of the current flag_abi_version. */
1701 && !mangle_return_type_p (decl
))
1702 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1708 inherit_targ_abi_tags (tree t
)
1710 if (!CLASS_TYPE_P (t
)
1711 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1714 mark_abi_tags (t
, true);
1716 tree args
= CLASSTYPE_TI_ARGS (t
);
1717 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1718 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1720 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1721 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1723 tree arg
= TREE_VEC_ELT (level
, j
);
1725 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1729 // If we found some tags on our template arguments, add them to our
1730 // abi_tag attribute.
1733 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1735 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1738 = tree_cons (abi_tag_identifier
, data
.tags
, TYPE_ATTRIBUTES (t
));
1741 mark_abi_tags (t
, false);
1744 /* Return true, iff class T has a non-virtual destructor that is
1745 accessible from outside the class heirarchy (i.e. is public, or
1746 there's a suitable friend. */
1749 accessible_nvdtor_p (tree t
)
1751 tree dtor
= CLASSTYPE_DESTRUCTOR (t
);
1753 /* An implicitly declared destructor is always public. And,
1754 if it were virtual, we would have created it by now. */
1758 if (DECL_VINDEX (dtor
))
1759 return false; /* Virtual */
1761 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1762 return true; /* Public */
1764 if (CLASSTYPE_FRIEND_CLASSES (t
)
1765 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1766 return true; /* Has friends */
1771 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1772 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1773 properties of the bases. */
1776 check_bases (tree t
,
1777 int* cant_have_const_ctor_p
,
1778 int* no_const_asn_ref_p
)
1781 bool seen_non_virtual_nearly_empty_base_p
= 0;
1782 int seen_tm_mask
= 0;
1785 tree field
= NULL_TREE
;
1787 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1788 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1789 if (TREE_CODE (field
) == FIELD_DECL
)
1792 for (binfo
= TYPE_BINFO (t
), i
= 0;
1793 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1795 tree basetype
= TREE_TYPE (base_binfo
);
1797 gcc_assert (COMPLETE_TYPE_P (basetype
));
1799 if (CLASSTYPE_FINAL (basetype
))
1800 error ("cannot derive from %<final%> base %qT in derived type %qT",
1803 /* If any base class is non-literal, so is the derived class. */
1804 if (!CLASSTYPE_LITERAL_P (basetype
))
1805 CLASSTYPE_LITERAL_P (t
) = false;
1807 /* If the base class doesn't have copy constructors or
1808 assignment operators that take const references, then the
1809 derived class cannot have such a member automatically
1811 if (TYPE_HAS_COPY_CTOR (basetype
)
1812 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1813 *cant_have_const_ctor_p
= 1;
1814 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1815 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1816 *no_const_asn_ref_p
= 1;
1818 if (BINFO_VIRTUAL_P (base_binfo
))
1819 /* A virtual base does not effect nearly emptiness. */
1821 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1823 if (seen_non_virtual_nearly_empty_base_p
)
1824 /* And if there is more than one nearly empty base, then the
1825 derived class is not nearly empty either. */
1826 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1828 /* Remember we've seen one. */
1829 seen_non_virtual_nearly_empty_base_p
= 1;
1831 else if (!is_empty_class (basetype
))
1832 /* If the base class is not empty or nearly empty, then this
1833 class cannot be nearly empty. */
1834 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1836 /* A lot of properties from the bases also apply to the derived
1838 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1839 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1840 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1841 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1842 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1843 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1844 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1845 || !TYPE_HAS_COPY_CTOR (basetype
));
1846 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1847 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1848 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1849 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1850 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1851 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1852 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1853 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1854 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1855 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1856 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1857 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1858 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1859 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1860 if (TYPE_HAS_MUTABLE_P (basetype
))
1861 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1863 /* A standard-layout class is a class that:
1865 * has no non-standard-layout base classes, */
1866 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1867 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1870 /* ...has no base classes of the same type as the first non-static
1872 if (field
&& DECL_CONTEXT (field
) == t
1873 && (same_type_ignoring_top_level_qualifiers_p
1874 (TREE_TYPE (field
), basetype
)))
1875 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1877 ...has at most one base class subobject of any given type... */
1878 else if (CLASSTYPE_REPEATED_BASE_P (t
))
1879 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1881 /* ...has all non-static data members and bit-fields in the class
1882 and its base classes first declared in the same class. */
1883 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1884 basefield
= DECL_CHAIN (basefield
))
1885 if (TREE_CODE (basefield
) == FIELD_DECL
1886 && !(DECL_FIELD_IS_BASE (basefield
)
1887 && is_empty_field (basefield
)))
1890 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1897 /* Don't bother collecting tm attributes if transactional memory
1898 support is not enabled. */
1901 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1903 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1906 check_abi_tags (t
, basetype
);
1909 /* If one of the base classes had TM attributes, and the current class
1910 doesn't define its own, then the current class inherits one. */
1911 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1913 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1914 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1918 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1919 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1920 that have had a nearly-empty virtual primary base stolen by some
1921 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1925 determine_primary_bases (tree t
)
1928 tree primary
= NULL_TREE
;
1929 tree type_binfo
= TYPE_BINFO (t
);
1932 /* Determine the primary bases of our bases. */
1933 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1934 base_binfo
= TREE_CHAIN (base_binfo
))
1936 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1938 /* See if we're the non-virtual primary of our inheritance
1940 if (!BINFO_VIRTUAL_P (base_binfo
))
1942 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1943 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1946 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1947 BINFO_TYPE (parent_primary
)))
1948 /* We are the primary binfo. */
1949 BINFO_PRIMARY_P (base_binfo
) = 1;
1951 /* Determine if we have a virtual primary base, and mark it so.
1953 if (primary
&& BINFO_VIRTUAL_P (primary
))
1955 tree this_primary
= copied_binfo (primary
, base_binfo
);
1957 if (BINFO_PRIMARY_P (this_primary
))
1958 /* Someone already claimed this base. */
1959 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1964 BINFO_PRIMARY_P (this_primary
) = 1;
1965 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1967 /* A virtual binfo might have been copied from within
1968 another hierarchy. As we're about to use it as a
1969 primary base, make sure the offsets match. */
1970 delta
= size_diffop_loc (input_location
,
1971 fold_convert (ssizetype
,
1972 BINFO_OFFSET (base_binfo
)),
1973 fold_convert (ssizetype
,
1974 BINFO_OFFSET (this_primary
)));
1976 propagate_binfo_offsets (this_primary
, delta
);
1981 /* First look for a dynamic direct non-virtual base. */
1982 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1984 tree basetype
= BINFO_TYPE (base_binfo
);
1986 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1988 primary
= base_binfo
;
1993 /* A "nearly-empty" virtual base class can be the primary base
1994 class, if no non-virtual polymorphic base can be found. Look for
1995 a nearly-empty virtual dynamic base that is not already a primary
1996 base of something in the hierarchy. If there is no such base,
1997 just pick the first nearly-empty virtual base. */
1999 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
2000 base_binfo
= TREE_CHAIN (base_binfo
))
2001 if (BINFO_VIRTUAL_P (base_binfo
)
2002 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
2004 if (!BINFO_PRIMARY_P (base_binfo
))
2006 /* Found one that is not primary. */
2007 primary
= base_binfo
;
2011 /* Remember the first candidate. */
2012 primary
= base_binfo
;
2016 /* If we've got a primary base, use it. */
2019 tree basetype
= BINFO_TYPE (primary
);
2021 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
2022 if (BINFO_PRIMARY_P (primary
))
2023 /* We are stealing a primary base. */
2024 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
2025 BINFO_PRIMARY_P (primary
) = 1;
2026 if (BINFO_VIRTUAL_P (primary
))
2030 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
2031 /* A virtual binfo might have been copied from within
2032 another hierarchy. As we're about to use it as a primary
2033 base, make sure the offsets match. */
2034 delta
= size_diffop_loc (input_location
, ssize_int (0),
2035 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
2037 propagate_binfo_offsets (primary
, delta
);
2040 primary
= TYPE_BINFO (basetype
);
2042 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
2043 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
2044 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
2048 /* Update the variant types of T. */
2051 fixup_type_variants (tree type
)
2056 for (tree variant
= TYPE_NEXT_VARIANT (type
);
2058 variant
= TYPE_NEXT_VARIANT (variant
))
2060 /* These fields are in the _TYPE part of the node, not in
2061 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2062 TYPE_HAS_USER_CONSTRUCTOR (variant
) = TYPE_HAS_USER_CONSTRUCTOR (type
);
2063 TYPE_NEEDS_CONSTRUCTING (variant
) = TYPE_NEEDS_CONSTRUCTING (type
);
2064 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant
)
2065 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
2067 TYPE_POLYMORPHIC_P (variant
) = TYPE_POLYMORPHIC_P (type
);
2068 CLASSTYPE_FINAL (variant
) = CLASSTYPE_FINAL (type
);
2070 TYPE_BINFO (variant
) = TYPE_BINFO (type
);
2072 /* Copy whatever these are holding today. */
2073 TYPE_VFIELD (variant
) = TYPE_VFIELD (type
);
2074 TYPE_FIELDS (variant
) = TYPE_FIELDS (type
);
2076 TYPE_SIZE (variant
) = TYPE_SIZE (type
);
2077 TYPE_SIZE_UNIT (variant
) = TYPE_SIZE_UNIT (type
);
2079 if (!TYPE_USER_ALIGN (variant
)
2080 || TYPE_NAME (variant
) == TYPE_NAME (type
)
2081 || TYPE_ALIGN_RAW (variant
) < TYPE_ALIGN_RAW (type
))
2083 TYPE_ALIGN_RAW (variant
) = TYPE_ALIGN_RAW (type
);
2084 TYPE_USER_ALIGN (variant
) = TYPE_USER_ALIGN (type
);
2087 TYPE_PRECISION (variant
) = TYPE_PRECISION (type
);
2088 TYPE_MODE_RAW (variant
) = TYPE_MODE_RAW (type
);
2089 TYPE_EMPTY_P (variant
) = TYPE_EMPTY_P (type
);
2093 /* KLASS is a class that we're applying may_alias to after the body is
2094 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2095 canonical type(s) will be implicitly updated. */
2098 fixup_may_alias (tree klass
)
2102 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
2103 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
2104 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
2105 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
2106 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
2107 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
2110 /* Early variant fixups: we apply attributes at the beginning of the class
2111 definition, and we need to fix up any variants that have already been
2112 made via elaborated-type-specifier so that check_qualified_type works. */
2115 fixup_attribute_variants (tree t
)
2122 tree attrs
= TYPE_ATTRIBUTES (t
);
2123 unsigned align
= TYPE_ALIGN (t
);
2124 bool user_align
= TYPE_USER_ALIGN (t
);
2125 bool may_alias
= lookup_attribute ("may_alias", attrs
);
2126 bool packed
= TYPE_PACKED (t
);
2129 fixup_may_alias (t
);
2131 for (variants
= TYPE_NEXT_VARIANT (t
);
2133 variants
= TYPE_NEXT_VARIANT (variants
))
2135 /* These are the two fields that check_qualified_type looks at and
2136 are affected by attributes. */
2137 TYPE_ATTRIBUTES (variants
) = attrs
;
2138 unsigned valign
= align
;
2139 if (TYPE_USER_ALIGN (variants
))
2140 valign
= MAX (valign
, TYPE_ALIGN (variants
));
2142 TYPE_USER_ALIGN (variants
) = user_align
;
2143 SET_TYPE_ALIGN (variants
, valign
);
2144 TYPE_PACKED (variants
) = packed
;
2146 fixup_may_alias (variants
);
2150 /* Set memoizing fields and bits of T (and its variants) for later
2154 finish_struct_bits (tree t
)
2156 /* Fix up variants (if any). */
2157 fixup_type_variants (t
);
2159 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
2160 /* For a class w/o baseclasses, 'finish_struct' has set
2161 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2162 Similarly for a class whose base classes do not have vtables.
2163 When neither of these is true, we might have removed abstract
2164 virtuals (by providing a definition), added some (by declaring
2165 new ones), or redeclared ones from a base class. We need to
2166 recalculate what's really an abstract virtual at this point (by
2167 looking in the vtables). */
2168 get_pure_virtuals (t
);
2170 /* If this type has a copy constructor or a destructor, force its
2171 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2172 nonzero. This will cause it to be passed by invisible reference
2173 and prevent it from being returned in a register. */
2174 if (type_has_nontrivial_copy_init (t
)
2175 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2178 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2179 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2181 SET_TYPE_MODE (variants
, BLKmode
);
2182 TREE_ADDRESSABLE (variants
) = 1;
2187 /* Issue warnings about T having private constructors, but no friends,
2190 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2191 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2192 non-private static member functions. */
2195 maybe_warn_about_overly_private_class (tree t
)
2197 int has_member_fn
= 0;
2198 int has_nonprivate_method
= 0;
2199 bool nonprivate_ctor
= false;
2201 if (!warn_ctor_dtor_privacy
2202 /* If the class has friends, those entities might create and
2203 access instances, so we should not warn. */
2204 || (CLASSTYPE_FRIEND_CLASSES (t
)
2205 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2206 /* We will have warned when the template was declared; there's
2207 no need to warn on every instantiation. */
2208 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2209 /* There's no reason to even consider warning about this
2213 /* We only issue one warning, if more than one applies, because
2214 otherwise, on code like:
2217 // Oops - forgot `public:'
2223 we warn several times about essentially the same problem. */
2225 /* Check to see if all (non-constructor, non-destructor) member
2226 functions are private. (Since there are no friends or
2227 non-private statics, we can't ever call any of the private member
2229 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2230 if (TREE_CODE (fn
) == USING_DECL
2231 && DECL_NAME (fn
) == ctor_identifier
2232 && !TREE_PRIVATE (fn
))
2233 nonprivate_ctor
= true;
2234 else if (!DECL_DECLARES_FUNCTION_P (fn
))
2235 /* Not a function. */;
2236 else if (DECL_ARTIFICIAL (fn
))
2237 /* We're not interested in compiler-generated methods; they don't
2238 provide any way to call private members. */;
2239 else if (!TREE_PRIVATE (fn
))
2241 if (DECL_STATIC_FUNCTION_P (fn
))
2242 /* A non-private static member function is just like a
2243 friend; it can create and invoke private member
2244 functions, and be accessed without a class
2248 has_nonprivate_method
= 1;
2249 /* Keep searching for a static member function. */
2251 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2254 if (!has_nonprivate_method
&& has_member_fn
)
2256 /* There are no non-private methods, and there's at least one
2257 private member function that isn't a constructor or
2258 destructor. (If all the private members are
2259 constructors/destructors we want to use the code below that
2260 issues error messages specifically referring to
2261 constructors/destructors.) */
2263 tree binfo
= TYPE_BINFO (t
);
2265 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2266 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2268 has_nonprivate_method
= 1;
2271 if (!has_nonprivate_method
)
2273 warning (OPT_Wctor_dtor_privacy
,
2274 "all member functions in class %qT are private", t
);
2279 /* Even if some of the member functions are non-private, the class
2280 won't be useful for much if all the constructors or destructors
2281 are private: such an object can never be created or destroyed. */
2282 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
2283 if (TREE_PRIVATE (dtor
))
2285 warning (OPT_Wctor_dtor_privacy
,
2286 "%q#T only defines a private destructor and has no friends",
2291 /* Warn about classes that have private constructors and no friends. */
2292 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2293 /* Implicitly generated constructors are always public. */
2294 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
2296 tree copy_or_move
= NULL_TREE
;
2298 /* If a non-template class does not define a copy
2299 constructor, one is defined for it, enabling it to avoid
2300 this warning. For a template class, this does not
2301 happen, and so we would normally get a warning on:
2303 template <class T> class C { private: C(); };
2305 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2306 complete non-template or fully instantiated classes have this
2308 if (!TYPE_HAS_COPY_CTOR (t
))
2309 nonprivate_ctor
= true;
2311 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (t
)))
2312 if (TREE_PRIVATE (fn
))
2314 else if (copy_fn_p (fn
) || move_fn_p (fn
))
2315 /* Ideally, we wouldn't count any constructor that takes
2316 an argument of the class type as a parameter, because
2317 such things cannot be used to construct an instance of
2318 the class unless you already have one. */
2322 nonprivate_ctor
= true;
2326 if (!nonprivate_ctor
)
2328 bool w
= warning (OPT_Wctor_dtor_privacy
,
2329 "%q#T only defines private constructors and has "
2331 if (w
&& copy_or_move
)
2332 inform (DECL_SOURCE_LOCATION (copy_or_move
),
2333 "%q#D is public, but requires an existing %q#T object",
2340 /* Make BINFO's vtable have N entries, including RTTI entries,
2341 vbase and vcall offsets, etc. Set its type and call the back end
2345 layout_vtable_decl (tree binfo
, int n
)
2350 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2351 layout_type (atype
);
2353 /* We may have to grow the vtable. */
2354 vtable
= get_vtbl_decl_for_binfo (binfo
);
2355 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2357 TREE_TYPE (vtable
) = atype
;
2358 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2359 layout_decl (vtable
, 0);
2363 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2364 have the same signature. */
2367 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2369 /* One destructor overrides another if they are the same kind of
2371 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2372 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2374 /* But a non-destructor never overrides a destructor, nor vice
2375 versa, nor do different kinds of destructors override
2376 one-another. For example, a complete object destructor does not
2377 override a deleting destructor. */
2378 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2381 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2382 || (DECL_CONV_FN_P (fndecl
)
2383 && DECL_CONV_FN_P (base_fndecl
)
2384 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2385 DECL_CONV_FN_TYPE (base_fndecl
))))
2387 tree fntype
= TREE_TYPE (fndecl
);
2388 tree base_fntype
= TREE_TYPE (base_fndecl
);
2389 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2390 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2391 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2392 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2398 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2402 base_derived_from (tree derived
, tree base
)
2406 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2408 if (probe
== derived
)
2410 else if (BINFO_VIRTUAL_P (probe
))
2411 /* If we meet a virtual base, we can't follow the inheritance
2412 any more. See if the complete type of DERIVED contains
2413 such a virtual base. */
2414 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2420 struct find_final_overrider_data
{
2421 /* The function for which we are trying to find a final overrider. */
2423 /* The base class in which the function was declared. */
2424 tree declaring_base
;
2425 /* The candidate overriders. */
2427 /* Path to most derived. */
2428 auto_vec
<tree
> path
;
2431 /* Add the overrider along the current path to FFOD->CANDIDATES.
2432 Returns true if an overrider was found; false otherwise. */
2435 dfs_find_final_overrider_1 (tree binfo
,
2436 find_final_overrider_data
*ffod
,
2441 /* If BINFO is not the most derived type, try a more derived class.
2442 A definition there will overrider a definition here. */
2446 if (dfs_find_final_overrider_1
2447 (ffod
->path
[depth
], ffod
, depth
))
2451 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2454 tree
*candidate
= &ffod
->candidates
;
2456 /* Remove any candidates overridden by this new function. */
2459 /* If *CANDIDATE overrides METHOD, then METHOD
2460 cannot override anything else on the list. */
2461 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2463 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2464 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2465 *candidate
= TREE_CHAIN (*candidate
);
2467 candidate
= &TREE_CHAIN (*candidate
);
2470 /* Add the new function. */
2471 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2478 /* Called from find_final_overrider via dfs_walk. */
2481 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2483 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2485 if (binfo
== ffod
->declaring_base
)
2486 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2487 ffod
->path
.safe_push (binfo
);
2493 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2495 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2501 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2502 FN and whose TREE_VALUE is the binfo for the base where the
2503 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2504 DERIVED) is the base object in which FN is declared. */
2507 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2509 find_final_overrider_data ffod
;
2511 /* Getting this right is a little tricky. This is valid:
2513 struct S { virtual void f (); };
2514 struct T { virtual void f (); };
2515 struct U : public S, public T { };
2517 even though calling `f' in `U' is ambiguous. But,
2519 struct R { virtual void f(); };
2520 struct S : virtual public R { virtual void f (); };
2521 struct T : virtual public R { virtual void f (); };
2522 struct U : public S, public T { };
2524 is not -- there's no way to decide whether to put `S::f' or
2525 `T::f' in the vtable for `R'.
2527 The solution is to look at all paths to BINFO. If we find
2528 different overriders along any two, then there is a problem. */
2529 if (DECL_THUNK_P (fn
))
2530 fn
= THUNK_TARGET (fn
);
2532 /* Determine the depth of the hierarchy. */
2534 ffod
.declaring_base
= binfo
;
2535 ffod
.candidates
= NULL_TREE
;
2536 ffod
.path
.create (30);
2538 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2539 dfs_find_final_overrider_post
, &ffod
);
2541 /* If there was no winner, issue an error message. */
2542 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2543 return error_mark_node
;
2545 return ffod
.candidates
;
2548 /* Return the index of the vcall offset for FN when TYPE is used as a
2552 get_vcall_index (tree fn
, tree type
)
2554 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2558 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2559 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2560 || same_signature_p (fn
, p
->purpose
))
2563 /* There should always be an appropriate index. */
2567 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2568 overrider at that index in the vtable. This should only be used when we
2569 know that BINFO is correct for the dynamic type of the object. */
2572 lookup_vfn_in_binfo (tree idx
, tree binfo
)
2574 int ix
= tree_to_shwi (idx
);
2575 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2576 ix
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
2577 while (BINFO_PRIMARY_P (binfo
))
2578 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2579 class that actually owns the vtable. */
2580 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
2581 tree virtuals
= BINFO_VIRTUALS (binfo
);
2582 return TREE_VALUE (chain_index (ix
, virtuals
));
2585 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2586 dominated by T. FN is the old function; VIRTUALS points to the
2587 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2588 of that entry in the list. */
2591 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2599 tree overrider_fn
, overrider_target
;
2600 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2601 tree over_return
, base_return
;
2604 /* Find the nearest primary base (possibly binfo itself) which defines
2605 this function; this is the class the caller will convert to when
2606 calling FN through BINFO. */
2607 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2610 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2613 /* The nearest definition is from a lost primary. */
2614 if (BINFO_LOST_PRIMARY_P (b
))
2619 /* Find the final overrider. */
2620 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2621 if (overrider
== error_mark_node
)
2623 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2626 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2628 /* Check for adjusting covariant return types. */
2629 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2630 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2632 if (INDIRECT_TYPE_P (over_return
)
2633 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2634 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2635 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2636 /* If the overrider is invalid, don't even try. */
2637 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2639 /* If FN is a covariant thunk, we must figure out the adjustment
2640 to the final base FN was converting to. As OVERRIDER_TARGET might
2641 also be converting to the return type of FN, we have to
2642 combine the two conversions here. */
2643 tree fixed_offset
, virtual_offset
;
2645 over_return
= TREE_TYPE (over_return
);
2646 base_return
= TREE_TYPE (base_return
);
2648 if (DECL_THUNK_P (fn
))
2650 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2651 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2652 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2655 fixed_offset
= virtual_offset
= NULL_TREE
;
2658 /* Find the equivalent binfo within the return type of the
2659 overriding function. We will want the vbase offset from
2661 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2663 else if (!same_type_ignoring_top_level_qualifiers_p
2664 (over_return
, base_return
))
2666 /* There was no existing virtual thunk (which takes
2667 precedence). So find the binfo of the base function's
2668 return type within the overriding function's return type.
2669 Fortunately we know the covariancy is valid (it
2670 has already been checked), so we can just iterate along
2671 the binfos, which have been chained in inheritance graph
2672 order. Of course it is lame that we have to repeat the
2673 search here anyway -- we should really be caching pieces
2674 of the vtable and avoiding this repeated work. */
2675 tree thunk_binfo
= NULL_TREE
;
2676 tree base_binfo
= TYPE_BINFO (base_return
);
2678 /* Find the base binfo within the overriding function's
2679 return type. We will always find a thunk_binfo, except
2680 when the covariancy is invalid (which we will have
2681 already diagnosed). */
2683 for (thunk_binfo
= TYPE_BINFO (over_return
); thunk_binfo
;
2684 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2685 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2686 BINFO_TYPE (base_binfo
)))
2688 gcc_assert (thunk_binfo
|| errorcount
);
2690 /* See if virtual inheritance is involved. */
2691 for (virtual_offset
= thunk_binfo
;
2693 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2694 if (BINFO_VIRTUAL_P (virtual_offset
))
2698 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2700 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2704 /* We convert via virtual base. Adjust the fixed
2705 offset to be from there. */
2707 size_diffop (offset
,
2708 fold_convert (ssizetype
,
2709 BINFO_OFFSET (virtual_offset
)));
2712 /* There was an existing fixed offset, this must be
2713 from the base just converted to, and the base the
2714 FN was thunking to. */
2715 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2717 fixed_offset
= offset
;
2721 if (fixed_offset
|| virtual_offset
)
2722 /* Replace the overriding function with a covariant thunk. We
2723 will emit the overriding function in its own slot as
2725 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2726 fixed_offset
, virtual_offset
);
2729 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2730 !DECL_THUNK_P (fn
));
2732 /* If we need a covariant thunk, then we may need to adjust first_defn.
2733 The ABI specifies that the thunks emitted with a function are
2734 determined by which bases the function overrides, so we need to be
2735 sure that we're using a thunk for some overridden base; even if we
2736 know that the necessary this adjustment is zero, there may not be an
2737 appropriate zero-this-adjustment thunk for us to use since thunks for
2738 overriding virtual bases always use the vcall offset.
2740 Furthermore, just choosing any base that overrides this function isn't
2741 quite right, as this slot won't be used for calls through a type that
2742 puts a covariant thunk here. Calling the function through such a type
2743 will use a different slot, and that slot is the one that determines
2744 the thunk emitted for that base.
2746 So, keep looking until we find the base that we're really overriding
2747 in this slot: the nearest primary base that doesn't use a covariant
2748 thunk in this slot. */
2749 if (overrider_target
!= overrider_fn
)
2751 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2752 /* We already know that the overrider needs a covariant thunk. */
2753 b
= get_primary_binfo (b
);
2754 for (; ; b
= get_primary_binfo (b
))
2756 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2757 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2758 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2760 if (BINFO_LOST_PRIMARY_P (b
))
2766 /* Assume that we will produce a thunk that convert all the way to
2767 the final overrider, and not to an intermediate virtual base. */
2768 virtual_base
= NULL_TREE
;
2770 /* See if we can convert to an intermediate virtual base first, and then
2771 use the vcall offset located there to finish the conversion. */
2772 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2774 /* If we find the final overrider, then we can stop
2776 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2777 BINFO_TYPE (TREE_VALUE (overrider
))))
2780 /* If we find a virtual base, and we haven't yet found the
2781 overrider, then there is a virtual base between the
2782 declaring base (first_defn) and the final overrider. */
2783 if (BINFO_VIRTUAL_P (b
))
2790 /* Compute the constant adjustment to the `this' pointer. The
2791 `this' pointer, when this function is called, will point at BINFO
2792 (or one of its primary bases, which are at the same offset). */
2794 /* The `this' pointer needs to be adjusted from the declaration to
2795 the nearest virtual base. */
2796 delta
= size_diffop_loc (input_location
,
2797 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2798 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2800 /* If the nearest definition is in a lost primary, we don't need an
2801 entry in our vtable. Except possibly in a constructor vtable,
2802 if we happen to get our primary back. In that case, the offset
2803 will be zero, as it will be a primary base. */
2804 delta
= size_zero_node
;
2806 /* The `this' pointer needs to be adjusted from pointing to
2807 BINFO to pointing at the base where the final overrider
2809 delta
= size_diffop_loc (input_location
,
2810 fold_convert (ssizetype
,
2811 BINFO_OFFSET (TREE_VALUE (overrider
))),
2812 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2814 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2817 BV_VCALL_INDEX (*virtuals
)
2818 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2820 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2822 BV_LOST_PRIMARY (*virtuals
) = lost
;
2825 /* Called from modify_all_vtables via dfs_walk. */
2828 dfs_modify_vtables (tree binfo
, void* data
)
2830 tree t
= (tree
) data
;
2835 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2836 /* A base without a vtable needs no modification, and its bases
2837 are uninteresting. */
2838 return dfs_skip_bases
;
2840 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2841 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2842 /* Don't do the primary vtable, if it's new. */
2845 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2846 /* There's no need to modify the vtable for a non-virtual primary
2847 base; we're not going to use that vtable anyhow. We do still
2848 need to do this for virtual primary bases, as they could become
2849 non-primary in a construction vtable. */
2852 make_new_vtable (t
, binfo
);
2854 /* Now, go through each of the virtual functions in the virtual
2855 function table for BINFO. Find the final overrider, and update
2856 the BINFO_VIRTUALS list appropriately. */
2857 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2858 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2860 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2861 old_virtuals
= TREE_CHAIN (old_virtuals
))
2862 update_vtable_entry_for_fn (t
,
2864 BV_FN (old_virtuals
),
2870 /* Update all of the primary and secondary vtables for T. Create new
2871 vtables as required, and initialize their RTTI information. Each
2872 of the functions in VIRTUALS is declared in T and may override a
2873 virtual function from a base class; find and modify the appropriate
2874 entries to point to the overriding functions. Returns a list, in
2875 declaration order, of the virtual functions that are declared in T,
2876 but do not appear in the primary base class vtable, and which
2877 should therefore be appended to the end of the vtable for T. */
2880 modify_all_vtables (tree t
, tree virtuals
)
2882 tree binfo
= TYPE_BINFO (t
);
2885 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2886 if (TYPE_CONTAINS_VPTR_P (t
))
2887 get_vtable_decl (t
, false);
2889 /* Update all of the vtables. */
2890 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2892 /* Add virtual functions not already in our primary vtable. These
2893 will be both those introduced by this class, and those overridden
2894 from secondary bases. It does not include virtuals merely
2895 inherited from secondary bases. */
2896 for (fnsp
= &virtuals
; *fnsp
; )
2898 tree fn
= TREE_VALUE (*fnsp
);
2900 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2901 || DECL_VINDEX (fn
) == error_mark_node
)
2903 /* We don't need to adjust the `this' pointer when
2904 calling this function. */
2905 BV_DELTA (*fnsp
) = integer_zero_node
;
2906 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2908 /* This is a function not already in our vtable. Keep it. */
2909 fnsp
= &TREE_CHAIN (*fnsp
);
2912 /* We've already got an entry for this function. Skip it. */
2913 *fnsp
= TREE_CHAIN (*fnsp
);
2919 /* Get the base virtual function declarations in T that have the
2923 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2925 bool found_decls
= false;
2927 /* Find virtual functions in T with the indicated NAME. */
2928 for (tree method
: ovl_range (get_class_binding (t
, name
)))
2930 if (TREE_CODE (method
) == FUNCTION_DECL
&& DECL_VINDEX (method
))
2932 base_fndecls
->safe_push (method
);
2940 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2941 for (int i
= 0; i
< n_baseclasses
; i
++)
2943 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2944 get_basefndecls (name
, basetype
, base_fndecls
);
2948 /* If this method overrides a virtual method from a base, then mark
2949 this member function as being virtual as well. Do 'final' and
2950 'override' checks too. */
2953 check_for_override (tree decl
, tree ctype
)
2955 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2956 /* In [temp.mem] we have:
2958 A specialization of a member function template does not
2959 override a virtual function from a base class. */
2962 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2963 used for a vfunc. That avoids the expensive look_for_overrides
2964 call that when we know there's nothing to find. As conversion
2965 operators for the same type can have distinct identifiers, we
2966 cannot optimize those in that way. */
2967 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2968 || DECL_CONV_FN_P (decl
))
2969 && look_for_overrides (ctype
, decl
)
2970 /* Check staticness after we've checked if we 'override'. */
2971 && !DECL_STATIC_FUNCTION_P (decl
))
2973 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2974 the error_mark_node so that we know it is an overriding
2976 DECL_VINDEX (decl
) = decl
;
2979 && !DECL_OVERRIDE_P (decl
)
2980 && !DECL_FINAL_P (decl
)
2981 && !DECL_DESTRUCTOR_P (decl
))
2982 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2983 "%qD can be marked override", decl
);
2985 else if (DECL_OVERRIDE_P (decl
))
2986 error ("%q+#D marked %<override%>, but does not override", decl
);
2988 if (DECL_VIRTUAL_P (decl
))
2990 /* Remember this identifier is virtual name. */
2991 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = true;
2993 if (!DECL_VINDEX (decl
))
2994 /* It's a new vfunc. */
2995 DECL_VINDEX (decl
) = error_mark_node
;
2997 if (DECL_DESTRUCTOR_P (decl
))
2998 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
3000 else if (DECL_FINAL_P (decl
))
3001 error ("%q+#D marked %<final%>, but is not virtual", decl
);
3004 /* Warn about hidden virtual functions that are not overridden in t.
3005 We know that constructors and destructors don't apply. */
3008 warn_hidden (tree t
)
3010 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (t
))
3011 for (unsigned ix
= member_vec
->length (); ix
--;)
3013 tree fns
= (*member_vec
)[ix
];
3018 tree name
= OVL_NAME (fns
);
3019 auto_vec
<tree
, 20> base_fndecls
;
3024 if (IDENTIFIER_CDTOR_P (name
))
3027 /* Iterate through all of the base classes looking for possibly
3028 hidden functions. */
3029 for (binfo
= TYPE_BINFO (t
), j
= 0;
3030 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
3032 tree basetype
= BINFO_TYPE (base_binfo
);
3033 get_basefndecls (name
, basetype
, &base_fndecls
);
3036 /* If there are no functions to hide, continue. */
3037 if (base_fndecls
.is_empty ())
3040 /* Remove any overridden functions. */
3041 bool seen_non_override
= false;
3042 for (tree fndecl
: ovl_range (fns
))
3044 bool any_override
= false;
3045 if (TREE_CODE (fndecl
) == FUNCTION_DECL
3046 && DECL_VINDEX (fndecl
))
3048 /* If the method from the base class has the same
3049 signature as the method from the derived class, it
3050 has been overridden. Note that we can't move on
3051 after finding one match: fndecl might override
3052 multiple base fns. */
3053 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
3055 && same_signature_p (fndecl
, base_fndecls
[k
]))
3057 base_fndecls
[k
] = NULL_TREE
;
3058 any_override
= true;
3062 seen_non_override
= true;
3065 if (!seen_non_override
&& warn_overloaded_virtual
== 1)
3066 /* All the derived fns override base virtuals. */
3069 /* Now give a warning for all base functions without overriders,
3070 as they are hidden. */
3071 for (tree base_fndecl
: base_fndecls
)
3074 auto_diagnostic_group d
;
3075 /* Here we know it is a hider, and no overrider exists. */
3076 if (warning_at (location_of (base_fndecl
),
3077 OPT_Woverloaded_virtual_
,
3078 "%qD was hidden", base_fndecl
))
3079 inform (location_of (fns
), " by %qD", fns
);
3084 /* Recursive helper for finish_struct_anon. */
3087 finish_struct_anon_r (tree field
)
3089 for (tree elt
= TYPE_FIELDS (TREE_TYPE (field
)); elt
; elt
= DECL_CHAIN (elt
))
3091 /* We're generally only interested in entities the user
3092 declared, but we also find nested classes by noticing
3093 the TYPE_DECL that we create implicitly. You're
3094 allowed to put one anonymous union inside another,
3095 though, so we explicitly tolerate that. We use
3096 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3097 we also allow unnamed types used for defining fields. */
3098 if (DECL_ARTIFICIAL (elt
)
3099 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
3100 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
3103 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3104 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3106 /* Recurse into the anonymous aggregates to correctly handle
3107 access control (c++/24926):
3118 if (DECL_NAME (elt
) == NULL_TREE
3119 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
3120 finish_struct_anon_r (elt
);
3124 /* Fix up any anonymous union/struct members of T. */
3127 finish_struct_anon (tree t
)
3129 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3131 if (TREE_STATIC (field
))
3133 if (TREE_CODE (field
) != FIELD_DECL
)
3136 if (DECL_NAME (field
) == NULL_TREE
3137 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3138 finish_struct_anon_r (field
);
3142 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3143 will be used later during class template instantiation.
3144 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3145 a non-static member data (FIELD_DECL), a member function
3146 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3147 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3148 When FRIEND_P is nonzero, T is either a friend class
3149 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3150 (FUNCTION_DECL, TEMPLATE_DECL). */
3153 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
3155 if (CLASSTYPE_TEMPLATE_INFO (type
)
3156 && TREE_CODE (t
) != CONST_DECL
)
3158 tree purpose
= friend_p
? NULL_TREE
: type
;
3160 CLASSTYPE_DECL_LIST (type
)
3161 = tree_cons (purpose
, t
, CLASSTYPE_DECL_LIST (type
));
3165 /* This function is called from declare_virt_assop_and_dtor via
3168 DATA is a type that direcly or indirectly inherits the base
3169 represented by BINFO. If BINFO contains a virtual assignment [copy
3170 assignment or move assigment] operator or a virtual constructor,
3171 declare that function in DATA if it hasn't been already declared. */
3174 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
3176 tree bv
, fn
, t
= (tree
)data
;
3177 tree opname
= assign_op_identifier
;
3179 gcc_assert (t
&& CLASS_TYPE_P (t
));
3180 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
3182 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
3183 /* A base without a vtable needs no modification, and its bases
3184 are uninteresting. */
3185 return dfs_skip_bases
;
3187 if (BINFO_PRIMARY_P (binfo
))
3188 /* If this is a primary base, then we have already looked at the
3189 virtual functions of its vtable. */
3192 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3196 if (DECL_NAME (fn
) == opname
)
3198 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3199 lazily_declare_fn (sfk_copy_assignment
, t
);
3200 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3201 lazily_declare_fn (sfk_move_assignment
, t
);
3203 else if (DECL_DESTRUCTOR_P (fn
)
3204 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3205 lazily_declare_fn (sfk_destructor
, t
);
3211 /* If the class type T has a direct or indirect base that contains a
3212 virtual assignment operator or a virtual destructor, declare that
3213 function in T if it hasn't been already declared. */
3216 declare_virt_assop_and_dtor (tree t
)
3218 if (!(TYPE_POLYMORPHIC_P (t
)
3219 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3220 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3221 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3224 dfs_walk_all (TYPE_BINFO (t
),
3225 dfs_declare_virt_assop_and_dtor
,
3229 /* Declare the inheriting constructor for class T inherited from base
3230 constructor CTOR with the parameter array PARMS of size NPARMS. */
3233 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3235 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3237 /* We don't declare an inheriting ctor that would be a default,
3238 copy or move ctor for derived or base. */
3242 && TYPE_REF_P (parms
[0]))
3244 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3245 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3249 tree parmlist
= void_list_node
;
3250 for (int i
= nparms
- 1; i
>= 0; i
--)
3251 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3252 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3253 t
, false, ctor
, parmlist
);
3255 if (add_method (t
, fn
, false))
3257 DECL_CHAIN (fn
) = TYPE_FIELDS (t
);
3258 TYPE_FIELDS (t
) = fn
;
3262 /* Declare all the inheriting constructors for class T inherited from base
3263 constructor CTOR. */
3266 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3268 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3270 if (flag_new_inheriting_ctors
)
3272 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3273 t
, /*const*/false, ctor
, parms
);
3274 add_method (t
, ctor
, using_decl
!= NULL_TREE
);
3278 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3280 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3282 if (TREE_PURPOSE (parms
))
3283 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3284 new_parms
[i
++] = TREE_VALUE (parms
);
3286 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3287 if (parms
== NULL_TREE
)
3289 auto_diagnostic_group d
;
3290 if (warning (OPT_Winherited_variadic_ctor
,
3291 "the ellipsis in %qD is not inherited", ctor
))
3292 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3296 /* Implicitly declare T(). */
3299 add_implicit_default_ctor (tree t
)
3301 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3302 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3303 if (cxx_dialect
>= cxx11
)
3304 TYPE_HAS_CONSTEXPR_CTOR (t
)
3305 /* Don't force the declaration to get a hard answer; if the
3306 definition would have made the class non-literal, it will still be
3307 non-literal because of the base or member in question, and that
3308 gives a better diagnostic. */
3309 = type_maybe_constexpr_default_constructor (t
);
3312 /* Create default constructors, assignment operators, and so forth for
3313 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3314 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3315 the class cannot have a default constructor, copy constructor
3316 taking a const reference argument, or an assignment operator taking
3317 a const reference, respectively. */
3320 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3321 int cant_have_const_cctor
,
3322 int cant_have_const_assignment
)
3325 if (!CLASSTYPE_DESTRUCTOR (t
))
3326 /* In general, we create destructors lazily. */
3327 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3329 bool move_ok
= false;
3330 if (cxx_dialect
>= cxx11
&& CLASSTYPE_LAZY_DESTRUCTOR (t
)
3331 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3332 && !classtype_has_move_assign_or_move_ctor_p (t
, false))
3337 If there is no user-declared constructor for a class, a default
3338 constructor is implicitly declared. */
3339 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3340 add_implicit_default_ctor (t
);
3344 If a class definition does not explicitly declare a copy
3345 constructor, one is declared implicitly. */
3346 if (! TYPE_HAS_COPY_CTOR (t
))
3348 TYPE_HAS_COPY_CTOR (t
) = 1;
3349 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3350 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3352 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3355 /* If there is no assignment operator, one will be created if and
3356 when it is needed. For now, just record whether or not the type
3357 of the parameter to the assignment operator will be a const or
3358 non-const reference. */
3359 if (!TYPE_HAS_COPY_ASSIGN (t
))
3361 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3362 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3363 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3364 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3365 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3368 /* We can't be lazy about declaring functions that might override
3369 a virtual function from a base class. */
3370 declare_virt_assop_and_dtor (t
);
3372 /* If the class definition does not explicitly declare an == operator
3373 function, but declares a defaulted three-way comparison operator function,
3374 an == operator function is declared implicitly. */
3375 if (!classtype_has_op (t
, EQ_EXPR
))
3376 if (tree space
= classtype_has_defaulted_op (t
, SPACESHIP_EXPR
))
3378 tree eq
= implicitly_declare_fn (sfk_comparison
, t
, false, space
,
3380 bool is_friend
= DECL_CONTEXT (space
) != t
;
3382 do_friend (NULL_TREE
, DECL_NAME (eq
), eq
,
3386 add_method (t
, eq
, false);
3387 DECL_CHAIN (eq
) = TYPE_FIELDS (t
);
3388 TYPE_FIELDS (t
) = eq
;
3390 maybe_add_class_template_decl_list (t
, eq
, is_friend
);
3393 while (*access_decls
)
3395 tree using_decl
= TREE_VALUE (*access_decls
);
3396 tree decl
= USING_DECL_DECLS (using_decl
);
3397 if (DECL_NAME (using_decl
) == ctor_identifier
)
3399 /* declare, then remove the decl */
3400 tree ctor_list
= decl
;
3401 location_t loc
= input_location
;
3402 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3403 for (tree fn
: ovl_range (ctor_list
))
3405 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) && default_ctor_p (fn
))
3406 /* CWG2799: Inheriting a default constructor gives us a default
3407 constructor, not just an inherited constructor. */
3408 add_implicit_default_ctor (t
);
3409 one_inherited_ctor (fn
, t
, using_decl
);
3411 *access_decls
= TREE_CHAIN (*access_decls
);
3412 input_location
= loc
;
3415 access_decls
= &TREE_CHAIN (*access_decls
);
3419 /* Cache of enum_min_precision values. */
3420 static GTY((deletable
)) hash_map
<tree
, int> *enum_to_min_precision
;
3422 /* Return the minimum precision of a bit-field needed to store all
3423 enumerators of ENUMERAL_TYPE TYPE. */
3426 enum_min_precision (tree type
)
3428 type
= TYPE_MAIN_VARIANT (type
);
3429 /* For unscoped enums without fixed underlying type and without mode
3430 attribute we can just use precision of the underlying type. */
3431 if (UNSCOPED_ENUM_P (type
)
3432 && !ENUM_FIXED_UNDERLYING_TYPE_P (type
)
3433 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type
)))
3434 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
));
3436 if (enum_to_min_precision
== NULL
)
3437 enum_to_min_precision
= hash_map
<tree
, int>::create_ggc (37);
3440 int &prec
= enum_to_min_precision
->get_or_insert (type
, &existed
);
3444 tree minnode
, maxnode
;
3445 if (TYPE_VALUES (type
))
3447 minnode
= maxnode
= NULL_TREE
;
3448 for (tree values
= TYPE_VALUES (type
);
3449 values
; values
= TREE_CHAIN (values
))
3451 tree decl
= TREE_VALUE (values
);
3452 tree value
= DECL_INITIAL (decl
);
3453 if (value
== error_mark_node
)
3454 value
= integer_zero_node
;
3456 minnode
= maxnode
= value
;
3457 else if (tree_int_cst_lt (maxnode
, value
))
3459 else if (tree_int_cst_lt (value
, minnode
))
3464 minnode
= maxnode
= integer_zero_node
;
3466 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
3467 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
3468 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
3469 prec
= MAX (lowprec
, highprec
);
3473 /* FIELD is a bit-field. We are finishing the processing for its
3474 enclosing type. Issue any appropriate messages and set appropriate
3475 flags. Returns false if an error has been diagnosed. */
3478 check_bitfield_decl (tree field
)
3480 tree type
= TREE_TYPE (field
);
3483 /* Extract the declared width of the bitfield, which has been
3484 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3485 w
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
3486 gcc_assert (w
!= NULL_TREE
);
3487 /* Remove the bit-field width indicator so that the rest of the
3488 compiler does not treat that value as a qualifier. */
3489 DECL_BIT_FIELD_REPRESENTATIVE (field
) = NULL_TREE
;
3491 /* Detect invalid bit-field type. */
3492 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3494 error_at (DECL_SOURCE_LOCATION (field
),
3495 "bit-field %q#D with non-integral type %qT", field
, type
);
3496 w
= error_mark_node
;
3500 location_t loc
= input_location
;
3501 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3504 /* detect invalid field size. */
3505 input_location
= DECL_SOURCE_LOCATION (field
);
3506 w
= cxx_constant_value (w
);
3507 input_location
= loc
;
3509 if (TREE_CODE (w
) != INTEGER_CST
)
3511 error ("bit-field %q+D width not an integer constant", field
);
3512 w
= error_mark_node
;
3514 else if (tree_int_cst_sgn (w
) < 0)
3516 error ("negative width in bit-field %q+D", field
);
3517 w
= error_mark_node
;
3519 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3521 error ("zero width for bit-field %q+D", field
);
3522 w
= error_mark_node
;
3524 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3525 && TREE_CODE (type
) != BOOLEAN_TYPE
3526 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3527 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3528 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3529 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3530 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3531 "width of %qD exceeds its type", field
);
3532 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3534 int prec
= enum_min_precision (type
);
3535 if (compare_tree_int (w
, prec
) < 0)
3536 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3537 "%qD is too small to hold all values of %q#T",
3542 if (w
!= error_mark_node
)
3544 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3545 DECL_BIT_FIELD (field
) = 1;
3550 /* Non-bit-fields are aligned for their type. */
3551 DECL_BIT_FIELD (field
) = 0;
3552 CLEAR_DECL_C_BIT_FIELD (field
);
3557 /* FIELD is a non bit-field. We are finishing the processing for its
3558 enclosing type T. Issue any appropriate messages and set appropriate
3562 check_field_decl (tree field
,
3564 int* cant_have_const_ctor
,
3565 int* no_const_asn_ref
)
3567 tree type
= strip_array_types (TREE_TYPE (field
));
3568 bool any_default_members
= false;
3570 /* In C++98 an anonymous union cannot contain any fields which would change
3571 the settings of CANT_HAVE_CONST_CTOR and friends. */
3572 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3574 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3575 structs. So, we recurse through their fields here. */
3576 else if (ANON_AGGR_TYPE_P (type
))
3578 for (tree fields
= TYPE_FIELDS (type
); fields
;
3579 fields
= DECL_CHAIN (fields
))
3580 if (TREE_CODE (fields
) == FIELD_DECL
)
3581 any_default_members
|= check_field_decl (fields
, t
,
3582 cant_have_const_ctor
,
3585 /* Check members with class type for constructors, destructors,
3587 else if (CLASS_TYPE_P (type
))
3589 /* Never let anything with uninheritable virtuals
3590 make it through without complaint. */
3591 abstract_virtuals_error (field
, type
);
3593 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3596 int oldcount
= errorcount
;
3597 if (TYPE_NEEDS_CONSTRUCTING (type
))
3598 error ("member %q+#D with constructor not allowed in union",
3600 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3601 error ("member %q+#D with destructor not allowed in union", field
);
3602 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3603 error ("member %q+#D with copy assignment operator not allowed in union",
3605 if (!warned
&& errorcount
> oldcount
)
3607 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3608 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3614 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3615 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3616 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3617 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3618 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3619 || !TYPE_HAS_COPY_ASSIGN (type
));
3620 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3621 || !TYPE_HAS_COPY_CTOR (type
));
3622 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3623 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3624 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3625 || TYPE_HAS_COMPLEX_DFLT (type
));
3628 if (TYPE_HAS_COPY_CTOR (type
)
3629 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3630 *cant_have_const_ctor
= 1;
3632 if (TYPE_HAS_COPY_ASSIGN (type
)
3633 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3634 *no_const_asn_ref
= 1;
3637 check_abi_tags (t
, field
);
3639 if (DECL_INITIAL (field
) != NULL_TREE
)
3640 /* `build_class_init_list' does not recognize
3642 any_default_members
= true;
3644 return any_default_members
;
3647 /* Check the data members (both static and non-static), class-scoped
3648 typedefs, etc., appearing in the declaration of T. Issue
3649 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3650 declaration order) of access declarations; each TREE_VALUE in this
3651 list is a USING_DECL.
3653 In addition, set the following flags:
3656 The class is empty, i.e., contains no non-static data members.
3658 CANT_HAVE_CONST_CTOR_P
3659 This class cannot have an implicitly generated copy constructor
3660 taking a const reference.
3662 CANT_HAVE_CONST_ASN_REF
3663 This class cannot have an implicitly generated assignment
3664 operator taking a const reference.
3666 All of these flags should be initialized before calling this
3670 check_field_decls (tree t
, tree
*access_decls
,
3671 int *cant_have_const_ctor_p
,
3672 int *no_const_asn_ref_p
)
3676 /* Assume there are no access declarations. */
3677 *access_decls
= NULL_TREE
;
3678 /* Effective C has things to say about classes with pointer members. */
3679 tree pointer_member
= NULL_TREE
;
3680 /* Default initialized members affect the whole class. */
3681 tree default_init_member
= NULL_TREE
;
3682 /* Lack of any non-static data member of non-volatile literal
3683 type affects a union. */
3684 bool found_nv_literal_p
= false;
3685 /* Standard layout requires all FIELDS have same access. */
3686 int field_access
= -1;
3688 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3690 tree type
= TREE_TYPE (field
);
3692 switch (TREE_CODE (field
))
3698 /* Save the access declarations for our caller. */
3699 *access_decls
= tree_cons (NULL_TREE
, field
, *access_decls
);
3707 /* FIXME: We should fold in the checking from check_methods. */
3711 DECL_NONLOCAL (field
) = 1;
3715 if (TREE_CODE (t
) == UNION_TYPE
3716 && cxx_dialect
< cxx11
)
3720 (C++98) If a union contains a static data member,
3721 ... the program is ill-formed. */
3722 if (cxx_dialect
< cxx11
)
3723 error ("in C++98 %q+D may not be static because it is "
3724 "a member of a union", field
);
3729 if (TREE_CODE (t
) == UNION_TYPE
)
3733 If a union contains ... or a [non-static data] member
3734 of reference type, the program is ill-formed. */
3735 if (TYPE_REF_P (type
))
3736 error ("non-static data member %q+D in a union may not "
3737 "have reference type %qT", field
, type
);
3741 /* Common VAR_DECL & FIELD_DECL processing. */
3742 DECL_CONTEXT (field
) = t
;
3743 DECL_NONLOCAL (field
) = 1;
3745 /* Template instantiation can cause this. Perhaps this
3746 should be a specific instantiation check? */
3747 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3749 error ("data member %q+D invalidly declared function type", field
);
3750 type
= build_pointer_type (type
);
3751 TREE_TYPE (field
) = type
;
3753 else if (TREE_CODE (type
) == METHOD_TYPE
)
3755 error ("data member %q+D invalidly declared method type", field
);
3756 type
= build_pointer_type (type
);
3757 TREE_TYPE (field
) = type
;
3763 if (TREE_CODE (field
) != FIELD_DECL
)
3766 if (type
== error_mark_node
)
3769 /* If it is not a union and at least one non-static data member is
3770 non-literal, the whole class becomes non-literal. Per Core/1453,
3771 volatile non-static data members and base classes are also not allowed.
3772 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3774 Note: if the type is incomplete we will complain later on. */
3775 if (COMPLETE_TYPE_P (type
))
3777 if (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
))
3778 CLASSTYPE_LITERAL_P (t
) = false;
3780 found_nv_literal_p
= true;
3783 int this_field_access
= (TREE_PROTECTED (field
) ? 1
3784 : TREE_PRIVATE (field
) ? 2 : 0);
3785 if (field_access
!= this_field_access
)
3787 /* A standard-layout class is a class that:
3789 ... has the same access control (Clause 11) for all
3790 non-static data members, */
3791 if (field_access
< 0)
3792 field_access
= this_field_access
;
3794 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3796 /* Aggregates must be public. */
3797 if (this_field_access
)
3798 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3801 /* If this is of reference type, check if it needs an init. */
3802 if (TYPE_REF_P (type
))
3804 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3805 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3806 if (DECL_INITIAL (field
) == NULL_TREE
)
3807 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3808 if (cxx_dialect
< cxx11
)
3810 /* ARM $12.6.2: [A member initializer list] (or, for an
3811 aggregate, initialization by a brace-enclosed list) is the
3812 only way to initialize non-static const and reference
3814 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3815 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3819 type
= strip_array_types (type
);
3821 if (TYPE_PACKED (t
))
3823 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3825 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3826 "ignoring packed attribute because of"
3827 " unpacked non-POD field %q#D", field
);
3830 else if (DECL_C_BIT_FIELD (field
)
3831 || TYPE_ALIGN (TREE_TYPE (field
)) > BITS_PER_UNIT
)
3832 DECL_PACKED (field
) = 1;
3835 if (DECL_C_BIT_FIELD (field
)
3836 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field
)))
3837 /* We don't treat zero-width bitfields as making a class
3840 else if (field_poverlapping_p (field
)
3841 && is_empty_class (TREE_TYPE (field
)))
3842 /* Empty data members also don't make a class non-empty. */
3843 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3846 /* The class is non-empty. */
3847 CLASSTYPE_EMPTY_P (t
) = 0;
3848 /* The class is not even nearly empty. */
3849 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3850 /* If one of the data members contains an empty class, so
3852 if (CLASS_TYPE_P (type
)
3853 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3854 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3857 /* This is used by -Weffc++ (see below). Warn only for pointers
3858 to members which might hold dynamic memory. So do not warn
3859 for pointers to functions or pointers to members. */
3860 if (TYPE_PTR_P (type
)
3861 && !TYPE_PTRFN_P (type
))
3862 pointer_member
= field
;
3864 if (CLASS_TYPE_P (type
))
3866 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3867 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3868 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3869 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3872 if (DECL_MUTABLE_P (field
) || TYPE_HAS_MUTABLE_P (type
))
3873 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3875 if (DECL_MUTABLE_P (field
))
3877 if (TYPE_REF_P (type
))
3878 error ("member %q+D cannot be declared as a %<mutable%> "
3879 "reference", field
);
3880 else if (CP_TYPE_CONST_P (type
))
3881 error ("member %q+D cannot be declared both %<const%> "
3882 "and %<mutable%>", field
);
3885 if (! layout_pod_type_p (type
))
3886 /* DR 148 now allows pointers to members (which are POD themselves),
3887 to be allowed in POD structs. */
3888 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3890 if (field_poverlapping_p (field
))
3891 /* A potentially-overlapping non-static data member makes the class
3893 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3895 if (!std_layout_type_p (type
))
3896 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3898 if (! zero_init_p (type
))
3899 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3901 /* We set DECL_C_BIT_FIELD in grokbitfield.
3902 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3903 if (DECL_C_BIT_FIELD (field
))
3904 check_bitfield_decl (field
);
3906 if (check_field_decl (field
, t
,
3907 cant_have_const_ctor_p
, no_const_asn_ref_p
))
3909 if (default_init_member
3910 && TREE_CODE (t
) == UNION_TYPE
)
3912 error ("multiple fields in union %qT initialized", t
);
3913 inform (DECL_SOURCE_LOCATION (default_init_member
),
3914 "initialized member %q+D declared here",
3915 default_init_member
);
3917 default_init_member
= field
;
3920 /* Now that we've removed bit-field widths from DECL_INITIAL,
3921 anything left in DECL_INITIAL is an NSDMI that makes the class
3922 non-aggregate in C++11, and non-layout-POD always. */
3923 if (DECL_INITIAL (field
))
3925 if (cxx_dialect
< cxx14
)
3926 CLASSTYPE_NON_AGGREGATE (t
) = true;
3928 CLASSTYPE_NON_POD_AGGREGATE (t
) = true;
3931 if (CP_TYPE_CONST_P (type
))
3933 /* If any field is const, the structure type is pseudo-const. */
3934 C_TYPE_FIELDS_READONLY (t
) = 1;
3935 if (DECL_INITIAL (field
) == NULL_TREE
)
3936 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3937 if (cxx_dialect
< cxx11
)
3939 /* ARM $12.6.2: [A member initializer list] (or, for an
3940 aggregate, initialization by a brace-enclosed list) is the
3941 only way to initialize non-static const and reference
3943 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3944 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3947 /* A field that is pseudo-const makes the structure likewise. */
3948 else if (CLASS_TYPE_P (type
))
3950 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3951 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3952 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3953 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3956 /* Core issue 80: A non-static data member is required to have a
3957 different name from the class iff the class has a
3958 user-declared constructor. */
3959 if (constructor_name_p (DECL_NAME (field
), t
)
3960 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3961 permerror (DECL_SOURCE_LOCATION (field
),
3962 "field %q#D with same name as class", field
);
3965 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3966 one of its non-static data members is of non-volatile literal type. */
3967 if (TREE_CODE (t
) == UNION_TYPE
&& found_nv_literal_p
)
3968 CLASSTYPE_LITERAL_P (t
) = true;
3970 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3971 it should also define a copy constructor and an assignment operator to
3972 implement the correct copy semantic (deep vs shallow, etc.). As it is
3973 not feasible to check whether the constructors do allocate dynamic memory
3974 and store it within members, we approximate the warning like this:
3976 -- Warn only if there are members which are pointers
3977 -- Warn only if there is a non-trivial constructor (otherwise,
3978 there cannot be memory allocated).
3979 -- Warn only if there is a non-trivial destructor. We assume that the
3980 user at least implemented the cleanup correctly, and a destructor
3981 is needed to free dynamic memory.
3983 This seems enough for practical purposes. */
3986 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3987 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3988 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3990 if (warning (OPT_Weffc__
, "%q#T has pointer data members", t
))
3992 if (! TYPE_HAS_COPY_CTOR (t
))
3994 warning (OPT_Weffc__
,
3995 " but does not declare %<%T(const %T&)%>", t
, t
);
3996 if (!TYPE_HAS_COPY_ASSIGN (t
))
3997 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3999 else if (! TYPE_HAS_COPY_ASSIGN (t
))
4000 warning (OPT_Weffc__
,
4001 " but does not declare %<operator=(const %T&)%>", t
);
4002 inform (DECL_SOURCE_LOCATION (pointer_member
),
4003 "pointer member %q+D declared here", pointer_member
);
4007 /* Non-static data member initializers make the default constructor
4009 if (default_init_member
)
4011 TYPE_NEEDS_CONSTRUCTING (t
) = true;
4012 TYPE_HAS_COMPLEX_DFLT (t
) = true;
4015 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4017 TYPE_PACKED (t
) = 0;
4019 /* Check anonymous struct/anonymous union fields. */
4020 finish_struct_anon (t
);
4022 /* We've built up the list of access declarations in reverse order.
4024 *access_decls
= nreverse (*access_decls
);
4027 /* If TYPE is an empty class type, records its OFFSET in the table of
4031 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
4035 if (!is_empty_class (type
))
4038 /* Record the location of this empty object in OFFSETS. */
4039 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
4041 n
= splay_tree_insert (offsets
,
4042 (splay_tree_key
) offset
,
4043 (splay_tree_value
) NULL_TREE
);
4044 n
->value
= ((splay_tree_value
)
4045 tree_cons (NULL_TREE
,
4052 /* Returns nonzero if TYPE is an empty class type and there is
4053 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4056 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
4061 if (!is_empty_class (type
))
4064 /* Record the location of this empty object in OFFSETS. */
4065 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
4069 enum { ignore
, fast
, slow
, warn
}
4070 cv_check
= (abi_version_crosses (19) ? slow
4071 : abi_version_at_least (19) ? fast
4073 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
4075 tree elt
= TREE_VALUE (t
);
4077 if (same_type_p (elt
, type
))
4080 if (cv_check
!= ignore
4081 && similar_type_p (elt
, type
))
4083 if (cv_check
== fast
)
4089 if (cv_check
== warn
)
4091 warning (OPT_Wabi
, "layout of %qs member of type %qT changes in %qs",
4092 "[[no_unique_address]]", type
, "-fabi-version=19");
4093 if (abi_version_at_least (19))
4100 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4101 F for every subobject, passing it the type, offset, and table of
4102 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4105 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4106 than MAX_OFFSET will not be walked.
4108 If F returns a nonzero value, the traversal ceases, and that value
4109 is returned. Otherwise, returns zero. */
4112 walk_subobject_offsets (tree type
,
4113 subobject_offset_fn f
,
4120 tree type_binfo
= NULL_TREE
;
4122 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4124 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4127 if (type
== error_mark_node
)
4133 type
= BINFO_TYPE (type
);
4136 if (CLASS_TYPE_P (type
))
4142 /* Avoid recursing into objects that are not interesting. */
4143 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
4146 /* Record the location of TYPE. */
4147 r
= (*f
) (type
, offset
, offsets
);
4151 /* Iterate through the direct base classes of TYPE. */
4153 type_binfo
= TYPE_BINFO (type
);
4154 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
4158 if (BINFO_VIRTUAL_P (binfo
))
4162 /* We cannot rely on BINFO_OFFSET being set for the base
4163 class yet, but the offsets for direct non-virtual
4164 bases can be calculated by going back to the TYPE. */
4165 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
4166 binfo_offset
= size_binop (PLUS_EXPR
,
4168 BINFO_OFFSET (orig_binfo
));
4170 r
= walk_subobject_offsets (binfo
,
4180 if (CLASSTYPE_VBASECLASSES (type
))
4183 vec
<tree
, va_gc
> *vbases
;
4185 /* Iterate through the virtual base classes of TYPE. In G++
4186 3.2, we included virtual bases in the direct base class
4187 loop above, which results in incorrect results; the
4188 correct offsets for virtual bases are only known when
4189 working with the most derived type. */
4191 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
4192 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
4194 r
= walk_subobject_offsets (binfo
,
4196 size_binop (PLUS_EXPR
,
4198 BINFO_OFFSET (binfo
)),
4207 /* We still have to walk the primary base, if it is
4208 virtual. (If it is non-virtual, then it was walked
4210 tree vbase
= get_primary_binfo (type_binfo
);
4212 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
4213 && BINFO_PRIMARY_P (vbase
)
4214 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
4216 r
= (walk_subobject_offsets
4218 offsets
, max_offset
, /*vbases_p=*/0));
4225 /* Iterate through the fields of TYPE. */
4226 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4227 if (TREE_CODE (field
) == FIELD_DECL
4228 && TREE_TYPE (field
) != error_mark_node
4229 && !DECL_ARTIFICIAL (field
))
4233 field_offset
= byte_position (field
);
4235 r
= walk_subobject_offsets (TREE_TYPE (field
),
4237 size_binop (PLUS_EXPR
,
4247 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4249 tree element_type
= strip_array_types (type
);
4250 tree domain
= TYPE_DOMAIN (type
);
4253 /* Avoid recursing into objects that are not interesting. */
4254 if (!CLASS_TYPE_P (element_type
)
4255 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
4257 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
4260 /* Step through each of the elements in the array. */
4261 for (index
= size_zero_node
;
4262 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
4263 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
4265 r
= walk_subobject_offsets (TREE_TYPE (type
),
4273 offset
= size_binop (PLUS_EXPR
, offset
,
4274 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
4275 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4276 there's no point in iterating through the remaining
4277 elements of the array. */
4278 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4286 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4289 field_poverlapping_p (tree decl
)
4291 /* Base fields are actually potentially overlapping, but C++ bases go through
4292 a different code path based on binfos, and ObjC++ base fields are laid out
4293 in objc-act, so we don't want layout_class_type to mess with them. */
4294 if (DECL_FIELD_IS_BASE (decl
))
4296 gcc_checking_assert (c_dialect_objc ());
4300 return lookup_attribute ("no_unique_address",
4301 DECL_ATTRIBUTES (decl
));
4304 /* Return true iff DECL is an empty field, either for an empty base or a
4305 [[no_unique_address]] data member. */
4308 is_empty_field (tree decl
)
4310 if (!decl
|| TREE_CODE (decl
) != FIELD_DECL
)
4313 bool r
= (is_empty_class (TREE_TYPE (decl
))
4314 && (DECL_FIELD_IS_BASE (decl
)
4315 || field_poverlapping_p (decl
)));
4317 /* Empty fields should have size zero. */
4318 gcc_checking_assert (!r
|| integer_zerop (DECL_SIZE (decl
)));
4323 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4326 record_subobject_offsets (tree decl_or_binfo
,
4330 bool overlapping
, vbases_p
;
4332 if (DECL_P (decl_or_binfo
))
4334 tree decl
= decl_or_binfo
;
4335 type
= TREE_TYPE (decl
);
4336 offset
= byte_position (decl
);
4337 overlapping
= field_poverlapping_p (decl
);
4342 type
= BINFO_TYPE (decl_or_binfo
);
4343 offset
= BINFO_OFFSET (decl_or_binfo
);
4349 /* If recording subobjects for a non-static data member or a
4350 non-empty base class, we do not need to record offsets beyond
4351 the size of the biggest empty class. Additional data members
4352 will go at the end of the class. Additional base classes will go
4353 either at offset zero (if empty, in which case they cannot
4354 overlap with offsets past the size of the biggest empty class) or
4355 at the end of the class.
4357 However, if we are placing an empty base class, then we must record
4358 all offsets, as either the empty class is at offset zero (where
4359 other empty classes might later be placed) or at the end of the
4360 class (where other objects might then be placed, so other empty
4361 subobjects might later overlap). */
4363 || !is_empty_class (type
))
4364 max_offset
= sizeof_biggest_empty_class
;
4366 max_offset
= NULL_TREE
;
4367 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4368 offsets
, max_offset
, vbases_p
);
4371 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4372 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4373 virtual bases of TYPE are examined. */
4376 layout_conflict_p (tree type
,
4381 splay_tree_node max_node
;
4383 /* Get the node in OFFSETS that indicates the maximum offset where
4384 an empty subobject is located. */
4385 max_node
= splay_tree_max (offsets
);
4386 /* If there aren't any empty subobjects, then there's no point in
4387 performing this check. */
4391 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4392 offsets
, (tree
) (max_node
->key
),
4396 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4397 non-static data member of the type indicated by RLI. BINFO is the
4398 binfo corresponding to the base subobject, OFFSETS maps offsets to
4399 types already located at those offsets. This function determines
4400 the position of the DECL. */
4403 layout_nonempty_base_or_field (record_layout_info rli
,
4408 tree offset
= NULL_TREE
;
4414 /* For the purposes of determining layout conflicts, we want to
4415 use the class type of BINFO; TREE_TYPE (DECL) will be the
4416 CLASSTYPE_AS_BASE version, which does not contain entries for
4417 zero-sized bases. */
4418 type
= TREE_TYPE (binfo
);
4423 type
= TREE_TYPE (decl
);
4427 /* Try to place the field. It may take more than one try if we have
4428 a hard time placing the field without putting two objects of the
4429 same type at the same address. */
4432 struct record_layout_info_s old_rli
= *rli
;
4434 /* Place this field. */
4435 place_field (rli
, decl
);
4436 offset
= byte_position (decl
);
4438 /* We have to check to see whether or not there is already
4439 something of the same type at the offset we're about to use.
4440 For example, consider:
4443 struct T : public S { int i; };
4444 struct U : public S, public T {};
4446 Here, we put S at offset zero in U. Then, we can't put T at
4447 offset zero -- its S component would be at the same address
4448 as the S we already allocated. So, we have to skip ahead.
4449 Since all data members, including those whose type is an
4450 empty class, have nonzero size, any overlap can happen only
4451 with a direct or indirect base-class -- it can't happen with
4453 /* In a union, overlap is permitted; all members are placed at
4455 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4457 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4460 /* Strip off the size allocated to this field. That puts us
4461 at the first place we could have put the field with
4462 proper alignment. */
4465 /* Bump up by the alignment required for the type. */
4467 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4469 ? CLASSTYPE_ALIGN (type
)
4470 : TYPE_ALIGN (type
)));
4471 normalize_rli (rli
);
4473 else if (TREE_CODE (type
) == NULLPTR_TYPE
4474 && warn_abi
&& abi_version_crosses (9))
4476 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4477 the offset wasn't aligned like a pointer when we started to
4478 layout this field, that affects its position. */
4479 tree pos
= rli_size_unit_so_far (&old_rli
);
4480 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4482 if (abi_version_at_least (9))
4483 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4484 "alignment of %qD increased in %<-fabi-version=9%> "
4487 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4488 "of %qD will increase in %<-fabi-version=9%>",
4494 /* There was no conflict. We're done laying out this field. */
4498 /* Now that we know where it will be placed, update its
4500 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4501 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4502 this point because their BINFO_OFFSET is copied from another
4503 hierarchy. Therefore, we may not need to add the entire
4505 propagate_binfo_offsets (binfo
,
4506 size_diffop_loc (input_location
,
4507 fold_convert (ssizetype
, offset
),
4508 fold_convert (ssizetype
,
4509 BINFO_OFFSET (binfo
))));
4512 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4515 empty_base_at_nonzero_offset_p (tree type
,
4517 splay_tree
/*offsets*/)
4519 return is_empty_class (type
) && !integer_zerop (offset
);
4522 /* Layout the empty base BINFO. EOC indicates the byte currently just
4523 past the end of the class, and should be correctly aligned for a
4524 class of the type indicated by BINFO; OFFSETS gives the offsets of
4525 the empty bases allocated so far. T is the most derived
4526 type. Return nonzero iff we added it at the end. */
4529 layout_empty_base_or_field (record_layout_info rli
, tree binfo_or_decl
,
4534 tree binfo
= NULL_TREE
;
4535 tree decl
= NULL_TREE
;
4537 if (TREE_CODE (binfo_or_decl
) == TREE_BINFO
)
4539 binfo
= binfo_or_decl
;
4540 type
= BINFO_TYPE (binfo
);
4544 decl
= binfo_or_decl
;
4545 type
= TREE_TYPE (decl
);
4548 /* On some platforms (ARM), even empty classes will not be
4550 tree eoc
= round_up_loc (input_location
,
4551 rli_size_unit_so_far (rli
),
4552 CLASSTYPE_ALIGN_UNIT (type
));
4554 /* This routine should only be used for empty classes. */
4555 gcc_assert (is_empty_class (type
));
4557 if (decl
&& DECL_USER_ALIGN (decl
))
4558 alignment
= size_int (DECL_ALIGN_UNIT (decl
));
4560 alignment
= size_int (CLASSTYPE_ALIGN_UNIT (type
));
4562 /* This is an empty base class. We first try to put it at offset
4564 tree offset
= size_zero_node
;
4565 if (TREE_CODE (rli
->t
) != UNION_TYPE
4566 && layout_conflict_p (type
,
4571 /* That didn't work. Now, we move forward from the next
4572 available spot in the class. */
4577 if (!layout_conflict_p (type
,
4581 /* We finally found a spot where there's no overlap. */
4584 /* There's overlap here, too. Bump along to the next spot. */
4585 offset
= size_binop (PLUS_EXPR
, offset
, alignment
);
4589 if (decl
&& DECL_USER_ALIGN (decl
))
4591 rli
->record_align
= MAX (rli
->record_align
, DECL_ALIGN (decl
));
4593 rli
->unpacked_align
= MAX (rli
->unpacked_align
, DECL_ALIGN (decl
));
4594 TYPE_USER_ALIGN (rli
->t
) = 1;
4596 else if (CLASSTYPE_USER_ALIGN (type
))
4598 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (type
));
4600 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (type
));
4601 TYPE_USER_ALIGN (rli
->t
) = 1;
4605 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4606 propagate_binfo_offsets (binfo
,
4607 size_diffop (offset
, BINFO_OFFSET (binfo
)));
4610 DECL_FIELD_OFFSET (decl
) = offset
;
4611 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4612 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4618 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4619 fields at NEXT_FIELD, and return it. */
4622 build_base_field_1 (tree t
, tree binfo
, tree access
, tree
*&next_field
)
4624 /* Create the FIELD_DECL. */
4625 tree basetype
= BINFO_TYPE (binfo
);
4626 tree as_base
= CLASSTYPE_AS_BASE (basetype
);
4627 gcc_assert (as_base
);
4628 tree decl
= build_decl (input_location
, FIELD_DECL
, NULL_TREE
, as_base
);
4630 DECL_ARTIFICIAL (decl
) = 1;
4631 DECL_IGNORED_P (decl
) = 1;
4632 DECL_FIELD_CONTEXT (decl
) = t
;
4633 if (is_empty_class (basetype
))
4634 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4635 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = size_zero_node
;
4638 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4639 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4641 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4642 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4643 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4644 DECL_FIELD_IS_BASE (decl
) = 1;
4646 if (access
== access_private_node
)
4647 TREE_PRIVATE (decl
) = true;
4648 else if (access
== access_protected_node
)
4649 TREE_PROTECTED (decl
) = true;
4651 /* Add the new FIELD_DECL to the list of fields for T. */
4652 DECL_CHAIN (decl
) = *next_field
;
4654 next_field
= &DECL_CHAIN (decl
);
4659 /* Layout the base given by BINFO in the class indicated by RLI.
4660 *BASE_ALIGN is a running maximum of the alignments of
4661 any base class. OFFSETS gives the location of empty base
4662 subobjects. T is the most derived type. Return nonzero if the new
4663 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4664 *NEXT_FIELD, unless BINFO is for an empty base class.
4666 Returns the location at which the next field should be inserted. */
4669 build_base_field (record_layout_info rli
, tree binfo
, tree access
,
4670 splay_tree offsets
, tree
*next_field
)
4673 tree basetype
= BINFO_TYPE (binfo
);
4675 if (!COMPLETE_TYPE_P (basetype
))
4676 /* This error is now reported in xref_tag, thus giving better
4677 location information. */
4680 /* Place the base class. */
4681 if (!is_empty_class (basetype
))
4685 /* The containing class is non-empty because it has a non-empty
4687 CLASSTYPE_EMPTY_P (t
) = 0;
4689 /* Create the FIELD_DECL. */
4690 decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4692 /* Try to place the field. It may take more than one try if we
4693 have a hard time placing the field without putting two
4694 objects of the same type at the same address. */
4695 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4699 bool atend
= layout_empty_base_or_field (rli
, binfo
, offsets
);
4700 /* A nearly-empty class "has no proper base class that is empty,
4701 not morally virtual, and at an offset other than zero." */
4702 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4705 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4706 /* The check above (used in G++ 3.2) is insufficient because
4707 an empty class placed at offset zero might itself have an
4708 empty base at a nonzero offset. */
4709 else if (walk_subobject_offsets (basetype
,
4710 empty_base_at_nonzero_offset_p
,
4713 /*max_offset=*/NULL_TREE
,
4715 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4718 /* We used to not create a FIELD_DECL for empty base classes because of
4719 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4720 be a problem anymore. We need them to handle initialization of C++17
4722 if (cxx_dialect
>= cxx17
&& !BINFO_VIRTUAL_P (binfo
))
4724 tree decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4725 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4726 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4727 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4728 SET_DECL_FIELD_ABI_IGNORED (decl
, 1);
4731 /* An empty virtual base causes a class to be non-empty
4732 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4733 here because that was already done when the virtual table
4734 pointer was created. */
4737 /* Record the offsets of BINFO and its base subobjects. */
4738 record_subobject_offsets (binfo
, offsets
);
4743 /* Layout all of the non-virtual base classes. Record empty
4744 subobjects in OFFSETS. T is the most derived type. Return nonzero
4745 if the type cannot be nearly empty. The fields created
4746 corresponding to the base classes will be inserted at
4750 build_base_fields (record_layout_info rli
,
4751 splay_tree offsets
, tree
*next_field
)
4753 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4756 tree binfo
= TYPE_BINFO (t
);
4757 int n_baseclasses
= BINFO_N_BASE_BINFOS (binfo
);
4759 /* The primary base class is always allocated first. */
4760 const tree primary_binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
4763 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4764 base, if it is direct. Indirect base fields are private. */
4765 tree primary_access
= access_private_node
;
4766 for (int i
= 0; i
< n_baseclasses
; ++i
)
4768 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4769 if (base_binfo
== primary_binfo
)
4771 primary_access
= BINFO_BASE_ACCESS (binfo
, i
);
4775 next_field
= build_base_field (rli
, primary_binfo
,
4777 offsets
, next_field
);
4780 /* Now allocate the rest of the bases. */
4781 for (int i
= 0; i
< n_baseclasses
; ++i
)
4783 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4785 /* The primary base was already allocated above, so we don't
4786 need to allocate it again here. */
4787 if (base_binfo
== primary_binfo
)
4790 /* Virtual bases are added at the end (a primary virtual base
4791 will have already been added). */
4792 if (BINFO_VIRTUAL_P (base_binfo
))
4795 next_field
= build_base_field (rli
, base_binfo
,
4796 BINFO_BASE_ACCESS (binfo
, i
),
4797 offsets
, next_field
);
4801 /* Go through the TYPE_FIELDS of T issuing any appropriate
4802 diagnostics, figuring out which methods override which other
4803 methods, and so forth. */
4806 check_methods (tree t
)
4808 for (tree x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
4809 if (DECL_DECLARES_FUNCTION_P (x
))
4811 check_for_override (x
, t
);
4813 if (DECL_PURE_VIRTUAL_P (x
)
4814 && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4815 error ("initializer specified for non-virtual method %q+D", x
);
4816 /* The name of the field is the original field name
4817 Save this in auxiliary field for later overloading. */
4818 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4820 TYPE_POLYMORPHIC_P (t
) = 1;
4821 if (DECL_PURE_VIRTUAL_P (x
))
4822 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4825 if (!DECL_VIRTUAL_P (x
)
4826 && lookup_attribute ("transaction_safe_dynamic",
4827 DECL_ATTRIBUTES (x
)))
4828 error_at (DECL_SOURCE_LOCATION (x
),
4829 "%<transaction_safe_dynamic%> may only be specified for "
4830 "a virtual function");
4833 /* Check whether the eligible special member functions (P0848) are
4834 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4835 has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
4836 ineligible overloads, which is why this needs to be separate from the loop
4839 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
4841 if (TREE_CODE (dtor
) == OVERLOAD
)
4843 /* P0848: At the end of the definition of a class, overload
4844 resolution is performed among the prospective destructors declared
4845 in that class with an empty argument list to select the destructor
4846 for the class, also known as the selected destructor. The program
4847 is ill-formed if overload resolution fails. */
4849 for (tree fn
: ovl_range (dtor
))
4850 if (constraints_satisfied_p (fn
))
4852 gcc_checking_assert (viable
!= 1);
4854 auto_diagnostic_group d
;
4856 error_at (location_of (t
), "no viable destructor for %qT", t
);
4858 error_at (location_of (t
), "destructor for %qT is ambiguous", t
);
4859 print_candidates (dtor
);
4861 /* Arbitrarily prune the overload set to a single function for
4862 sake of error recovery. */
4863 tree
*slot
= find_member_slot (t
, dtor_identifier
);
4864 *slot
= get_first_fn (dtor
);
4866 else if (user_provided_p (dtor
))
4867 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = true;
4870 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (t
)))
4872 if (!user_provided_p (fn
))
4873 /* Might be trivial. */;
4874 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4875 /* Templates are never special members. */;
4876 else if (copy_fn_p (fn
)
4877 && constraints_satisfied_p (fn
))
4878 TYPE_HAS_COMPLEX_COPY_CTOR (t
) = true;
4879 else if (move_fn_p (fn
)
4880 && constraints_satisfied_p (fn
))
4881 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) = true;
4884 for (tree fn
: ovl_range (get_class_binding_direct (t
, assign_op_identifier
)))
4886 if (!user_provided_p (fn
))
4887 /* Might be trivial. */;
4888 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4889 /* Templates are never special members. */;
4890 else if (copy_fn_p (fn
)
4891 && constraints_satisfied_p (fn
))
4892 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = true;
4893 else if (move_fn_p (fn
)
4894 && constraints_satisfied_p (fn
))
4895 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = true;
4899 /* FN is constructor, destructor or operator function. Clone the
4900 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4901 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4904 copy_fndecl_with_name (tree fn
, tree name
, tree_code code
,
4905 bool need_vtt_parm_p
, bool omit_inherited_parms_p
)
4907 /* Copy the function. */
4908 tree clone
= copy_decl (fn
);
4909 /* Reset the function name. */
4910 DECL_NAME (clone
) = name
;
4913 /* Clone constraints. */
4914 if (tree ci
= get_constraints (fn
))
4915 set_constraints (clone
, copy_node (ci
));
4917 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4918 /* There's no pending inline data for this function. */
4919 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4920 DECL_PENDING_INLINE_P (clone
) = 0;
4922 if (name
== base_dtor_identifier
)
4924 /* The base-class destructor is not virtual. */
4925 DECL_VIRTUAL_P (clone
) = 0;
4926 DECL_VINDEX (clone
) = NULL_TREE
;
4928 else if (code
!= ERROR_MARK
)
4930 /* Set the operator code. */
4931 const ovl_op_info_t
*ovl_op
= OVL_OP_INFO (false, code
);
4932 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone
) = ovl_op
->ovl_op_code
;
4934 /* The operator could be virtual. */
4935 if (DECL_VIRTUAL_P (clone
))
4936 IDENTIFIER_VIRTUAL_P (name
) = true;
4939 if (omit_inherited_parms_p
)
4940 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone
));
4942 /* If there was an in-charge parameter, drop it from the function
4944 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4946 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4947 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4948 /* Skip the `this' parameter. */
4949 parmtypes
= TREE_CHAIN (parmtypes
);
4950 /* Skip the in-charge parameter. */
4951 parmtypes
= TREE_CHAIN (parmtypes
);
4952 /* And the VTT parm, in a complete [cd]tor. */
4953 if (DECL_HAS_VTT_PARM_P (fn
) && !need_vtt_parm_p
)
4954 parmtypes
= TREE_CHAIN (parmtypes
);
4955 if (omit_inherited_parms_p
)
4957 /* If we're omitting inherited parms, that just leaves the VTT. */
4958 gcc_assert (need_vtt_parm_p
);
4959 parmtypes
= tree_cons (NULL_TREE
, vtt_parm_type
, void_list_node
);
4962 = build_method_type_directly (basetype
,
4963 TREE_TYPE (TREE_TYPE (clone
)),
4966 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4967 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4969 = cxx_copy_lang_qualifiers (TREE_TYPE (clone
), TREE_TYPE (fn
));
4972 /* Copy the function parameters. */
4973 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4975 /* Remove the in-charge parameter. */
4976 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4978 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4979 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4980 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4983 /* And the VTT parm, in a complete [cd]tor. */
4984 if (DECL_HAS_VTT_PARM_P (fn
))
4986 if (need_vtt_parm_p
)
4987 DECL_HAS_VTT_PARM_P (clone
) = 1;
4990 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4991 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4992 DECL_HAS_VTT_PARM_P (clone
) = 0;
4996 /* A base constructor inheriting from a virtual base doesn't get the
4998 if (omit_inherited_parms_p
)
4999 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
5001 for (tree parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
5003 DECL_CONTEXT (parms
) = clone
;
5004 cxx_dup_lang_specific_decl (parms
);
5007 /* Create the RTL for this function. */
5008 SET_DECL_RTL (clone
, NULL
);
5010 /* Regardless of the current scope, this is a member function, so
5011 not at namespace scope. */
5012 rest_of_decl_compilation (clone
, /*top_level=*/0, at_eof
);
5017 /* FN is an operator function, create a variant for CODE. */
5020 copy_operator_fn (tree fn
, tree_code code
)
5022 return copy_fndecl_with_name (fn
, ovl_op_identifier (code
),
5023 code
, false, false);
5026 /* FN is a constructor or destructor. Clone the declaration to create
5027 a specialized in-charge or not-in-charge version, as indicated by
5031 build_clone (tree fn
, tree name
, bool need_vtt_parm_p
,
5032 bool omit_inherited_parms_p
)
5036 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5037 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5039 clone
= copy_decl (fn
);
5040 DECL_NAME (clone
) = name
;
5042 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
,
5043 need_vtt_parm_p
, omit_inherited_parms_p
);
5044 DECL_TEMPLATE_RESULT (clone
) = result
;
5046 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
5047 DECL_TI_TEMPLATE (result
) = clone
;
5049 TREE_TYPE (clone
) = TREE_TYPE (result
);
5053 clone
= copy_fndecl_with_name (fn
, name
, ERROR_MARK
,
5054 need_vtt_parm_p
, omit_inherited_parms_p
);
5055 DECL_CLONED_FUNCTION (clone
) = fn
;
5057 maybe_prepare_return_this (clone
);
5060 /* Remember where this function came from. */
5061 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
5063 /* Make it easy to find the CLONE given the FN. Note the
5064 template_result of a template will be chained this way too. */
5065 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
5066 DECL_CHAIN (fn
) = clone
;
5071 /* Build the clones of FN, return the number of clones built. These
5072 will be inserted onto DECL_CHAIN of FN. */
5075 build_cdtor_clones (tree fn
, bool needs_vtt_p
, bool base_omits_inherited_p
,
5076 bool update_methods
)
5080 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
5082 /* For each constructor, we need two variants: an in-charge version
5083 and a not-in-charge version. */
5084 build_clone (fn
, complete_ctor_identifier
, false, false);
5085 build_clone (fn
, base_ctor_identifier
, needs_vtt_p
,
5086 base_omits_inherited_p
);
5091 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
5093 /* For each destructor, we need three variants: an in-charge
5094 version, a not-in-charge version, and an in-charge deleting
5095 version. We clone the deleting version first because that
5096 means it will go second on the TYPE_FIELDS list -- and that
5097 corresponds to the correct layout order in the virtual
5100 For a non-virtual destructor, we do not build a deleting
5102 if (DECL_VIRTUAL_P (fn
))
5104 build_clone (fn
, deleting_dtor_identifier
, false, false);
5107 build_clone (fn
, complete_dtor_identifier
, false, false);
5108 build_clone (fn
, base_dtor_identifier
, needs_vtt_p
, false);
5112 /* The original is now an abstract function that is never
5114 DECL_ABSTRACT_P (fn
) = true;
5117 for (tree clone
= fn
; count
--;)
5119 clone
= DECL_CHAIN (clone
);
5120 add_method (DECL_CONTEXT (clone
), clone
, false);
5124 /* Produce declarations for all appropriate clones of FN. If
5125 UPDATE_METHODS is true, the clones are added to the
5126 CLASSTYPE_MEMBER_VEC. */
5129 clone_cdtor (tree fn
, bool update_methods
)
5131 /* Avoid inappropriate cloning. */
5133 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
5136 /* Base cdtors need a vtt parm if there are virtual bases. */
5137 bool vtt
= CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
));
5139 /* Base ctor omits inherited parms it needs a vttparm and inherited
5140 from a virtual nase ctor. */
5141 bool base_omits_inherited
= (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
)
5142 && base_ctor_omit_inherited_parms (fn
));
5144 build_cdtor_clones (fn
, vtt
, base_omits_inherited
, update_methods
);
5147 /* DECL is an in charge constructor, which is being defined. This will
5148 have had an in class declaration, from whence clones were
5149 declared. An out-of-class definition can specify additional default
5150 arguments. As it is the clones that are involved in overload
5151 resolution, we must propagate the information from the DECL to its
5155 adjust_clone_args (tree decl
)
5159 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
5160 clone
= DECL_CHAIN (clone
))
5162 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
5163 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
5164 tree decl_parms
, clone_parms
;
5166 /* Skip the 'this' parameter. */
5167 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
5168 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
5170 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
5171 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
5172 if (DECL_HAS_VTT_PARM_P (decl
))
5173 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
5175 clone_parms
= orig_clone_parms
;
5176 if (DECL_HAS_VTT_PARM_P (clone
))
5177 clone_parms
= TREE_CHAIN (clone_parms
);
5179 for (decl_parms
= orig_decl_parms
; decl_parms
;
5180 decl_parms
= TREE_CHAIN (decl_parms
),
5181 clone_parms
= TREE_CHAIN (clone_parms
))
5183 if (clone_parms
== void_list_node
)
5185 gcc_assert (decl_parms
== clone_parms
5186 || ctor_omit_inherited_parms (clone
));
5190 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms
),
5191 TREE_VALUE (clone_parms
)));
5193 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
5195 /* A default parameter has been added. Adjust the
5196 clone's parameters. */
5197 clone_parms
= orig_decl_parms
;
5199 if (DECL_HAS_VTT_PARM_P (clone
))
5201 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
5202 TREE_VALUE (orig_clone_parms
),
5204 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
5207 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
5209 = build_method_type_directly (basetype
,
5210 TREE_TYPE (TREE_TYPE (clone
)),
5212 if (tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
)))
5213 type
= cp_build_type_attribute_variant (type
, attrs
);
5214 type
= cxx_copy_lang_qualifiers (type
, TREE_TYPE (clone
));
5215 TREE_TYPE (clone
) = type
;
5217 clone_parms
= NULL_TREE
;
5221 gcc_assert (!clone_parms
|| clone_parms
== void_list_node
);
5225 /* For each of the constructors and destructors in T, create an
5226 in-charge and not-in-charge variant. */
5229 clone_constructors_and_destructors (tree t
)
5231 /* We do not need to propagate the usingness to the clone, at this
5232 point that is not needed. */
5233 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (t
)))
5234 clone_cdtor (fn
, /*update_methods=*/true);
5236 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5237 clone_cdtor (dtor
, /*update_methods=*/true);
5240 /* Deduce noexcept for a destructor DTOR. */
5243 deduce_noexcept_on_destructor (tree dtor
)
5245 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
5246 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
),
5247 noexcept_deferred_spec
);
5250 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5251 of TYPE for virtual functions which FNDECL overrides. Return a
5252 mask of the tm attributes found therein. */
5255 look_for_tm_attr_overrides (tree type
, tree fndecl
)
5257 tree binfo
= TYPE_BINFO (type
);
5261 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
5263 tree o
, basetype
= BINFO_TYPE (base_binfo
);
5265 if (!TYPE_POLYMORPHIC_P (basetype
))
5268 o
= look_for_overrides_here (basetype
, fndecl
);
5271 if (lookup_attribute ("transaction_safe_dynamic",
5272 DECL_ATTRIBUTES (o
)))
5273 /* transaction_safe_dynamic is not inherited. */;
5275 found
|= tm_attr_to_mask (find_tm_attribute
5276 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
5279 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
5285 /* Subroutine of set_method_tm_attributes. Handle the checks and
5286 inheritance for one virtual method FNDECL. */
5289 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
5294 found
= look_for_tm_attr_overrides (type
, fndecl
);
5296 /* If FNDECL doesn't actually override anything (i.e. T is the
5297 class that first declares FNDECL virtual), then we're done. */
5301 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
5302 have
= tm_attr_to_mask (tm_attr
);
5304 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5305 tm_pure must match exactly, otherwise no weakening of
5306 tm_safe > tm_callable > nothing. */
5307 /* ??? The tm_pure attribute didn't make the transition to the
5308 multivendor language spec. */
5309 if (have
== TM_ATTR_PURE
)
5311 if (found
!= TM_ATTR_PURE
)
5317 /* If the overridden function is tm_pure, then FNDECL must be. */
5318 else if (found
== TM_ATTR_PURE
&& tm_attr
)
5320 /* Look for base class combinations that cannot be satisfied. */
5321 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
5323 found
&= ~TM_ATTR_PURE
;
5325 error_at (DECL_SOURCE_LOCATION (fndecl
),
5326 "method overrides both %<transaction_pure%> and %qE methods",
5327 tm_mask_to_attr (found
));
5329 /* If FNDECL did not declare an attribute, then inherit the most
5331 else if (tm_attr
== NULL
)
5333 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
5335 /* Otherwise validate that we're not weaker than a function
5336 that is being overridden. */
5340 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
5346 error_at (DECL_SOURCE_LOCATION (fndecl
),
5347 "method declared %qE overriding %qE method",
5348 tm_attr
, tm_mask_to_attr (found
));
5351 /* For each of the methods in T, propagate a class-level tm attribute. */
5354 set_method_tm_attributes (tree t
)
5356 tree class_tm_attr
, fndecl
;
5358 /* Don't bother collecting tm attributes if transactional memory
5359 support is not enabled. */
5363 /* Process virtual methods first, as they inherit directly from the
5364 base virtual function and also require validation of new attributes. */
5365 if (TYPE_CONTAINS_VPTR_P (t
))
5368 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
5369 vchain
= TREE_CHAIN (vchain
))
5371 fndecl
= BV_FN (vchain
);
5372 if (DECL_THUNK_P (fndecl
))
5373 fndecl
= THUNK_TARGET (fndecl
);
5374 set_one_vmethod_tm_attributes (t
, fndecl
);
5378 /* If the class doesn't have an attribute, nothing more to do. */
5379 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
5380 if (class_tm_attr
== NULL
)
5383 /* Any method that does not yet have a tm attribute inherits
5384 the one from the class. */
5385 for (fndecl
= TYPE_FIELDS (t
); fndecl
; fndecl
= DECL_CHAIN (fndecl
))
5386 if (DECL_DECLARES_FUNCTION_P (fndecl
)
5387 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
5388 apply_tm_attr (fndecl
, class_tm_attr
);
5391 /* Returns true if FN is a default constructor. */
5394 default_ctor_p (const_tree fn
)
5396 return (DECL_CONSTRUCTOR_P (fn
)
5397 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
5400 /* Returns true iff class T has a user-provided constructor that can be called
5401 with more than zero arguments. */
5404 type_has_user_nondefault_constructor (tree t
)
5406 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5409 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (t
)))
5411 if (user_provided_p (fn
)
5412 && (TREE_CODE (fn
) == TEMPLATE_DECL
5413 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
5421 /* Returns the defaulted constructor if T has one. Otherwise, returns
5425 in_class_defaulted_default_constructor (tree t
)
5427 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5430 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5434 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
5435 && default_ctor_p (fn
))
5442 /* Returns true iff FN is a user-provided function, i.e. user-declared
5443 and not defaulted at its first declaration. */
5446 user_provided_p (tree fn
)
5448 fn
= STRIP_TEMPLATE (fn
);
5449 return (!DECL_ARTIFICIAL (fn
)
5450 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
5451 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
5454 /* Returns true iff class T has a user-provided constructor. */
5457 type_has_user_provided_constructor (tree t
)
5459 if (!CLASS_TYPE_P (t
))
5462 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5465 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5466 if (user_provided_p (*iter
))
5472 /* Returns true iff class T has a user-provided or explicit constructor. */
5475 type_has_user_provided_or_explicit_constructor (tree t
)
5477 if (!CLASS_TYPE_P (t
))
5480 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5483 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5486 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5493 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5494 declared or explicitly defaulted in the class body) default
5498 type_has_non_user_provided_default_constructor (tree t
)
5500 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5502 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5505 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5508 if (TREE_CODE (fn
) == FUNCTION_DECL
5509 && default_ctor_p (fn
)
5510 && !user_provided_p (fn
))
5517 /* TYPE is being used as a virtual base, and has a non-trivial move
5518 assignment. Return true if this is due to there being a user-provided
5519 move assignment in TYPE or one of its subobjects; if there isn't, then
5520 multiple move assignment can't cause any harm. */
5523 vbase_has_user_provided_move_assign (tree type
)
5525 /* Does the type itself have a user-provided move assignment operator? */
5526 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type
))
5527 for (ovl_iterator
iter (get_class_binding_direct
5528 (type
, assign_op_identifier
));
5530 if (user_provided_p (*iter
) && move_fn_p (*iter
))
5533 /* Do any of its bases? */
5534 tree binfo
= TYPE_BINFO (type
);
5536 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5537 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5540 /* Or non-static data members? */
5541 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5543 if (TREE_CODE (field
) == FIELD_DECL
5544 && CLASS_TYPE_P (TREE_TYPE (field
))
5545 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5553 /* If default-initialization leaves part of TYPE uninitialized, returns
5554 a DECL for the field or TYPE itself (DR 253). */
5557 default_init_uninitialized_part (tree type
)
5562 type
= strip_array_types (type
);
5563 if (!CLASS_TYPE_P (type
))
5565 if (!type_has_non_user_provided_default_constructor (type
))
5567 for (binfo
= TYPE_BINFO (type
), i
= 0;
5568 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5570 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5574 for (t
= next_aggregate_field (TYPE_FIELDS (type
)); t
;
5575 t
= next_aggregate_field (DECL_CHAIN (t
)))
5576 if (!DECL_INITIAL (t
) && !DECL_ARTIFICIAL (t
))
5578 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5580 return DECL_P (r
) ? r
: t
;
5586 /* Returns true iff for class T, a trivial synthesized default constructor
5587 would be constexpr. */
5590 trivial_default_constructor_is_constexpr (tree t
)
5592 /* A defaulted trivial default constructor is constexpr
5593 if there is nothing to initialize. */
5594 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5595 /* A class with a vptr doesn't have a trivial default ctor.
5596 In C++20, a class can have transient uninitialized members, e.g.:
5598 struct S { int i; constexpr S() = default; };
5601 return (cxx_dialect
>= cxx20
5602 || is_really_empty_class (t
, /*ignore_vptr*/true));
5605 /* Returns true iff class T has a constexpr default constructor. */
5608 type_has_constexpr_default_constructor (tree t
)
5612 if (!CLASS_TYPE_P (t
))
5614 /* The caller should have stripped an enclosing array. */
5615 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5618 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5620 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5621 return trivial_default_constructor_is_constexpr (t
);
5622 /* Non-trivial, we need to check subobject constructors. */
5623 lazily_declare_fn (sfk_constructor
, t
);
5625 fns
= locate_ctor (t
);
5626 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5629 /* Returns true iff class T has a constexpr default constructor or has an
5630 implicitly declared default constructor that we can't tell if it's constexpr
5631 without forcing a lazy declaration (which might cause undesired
5635 type_maybe_constexpr_default_constructor (tree t
)
5637 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5638 && TYPE_HAS_COMPLEX_DFLT (t
))
5639 /* Assume it's constexpr. */
5641 return type_has_constexpr_default_constructor (t
);
5644 /* Returns true iff class T has a constexpr destructor. */
5647 type_has_constexpr_destructor (tree t
)
5651 if (CLASSTYPE_LAZY_DESTRUCTOR (t
))
5652 /* Non-trivial, we need to check subobject destructors. */
5653 lazily_declare_fn (sfk_destructor
, t
);
5654 fns
= CLASSTYPE_DESTRUCTOR (t
);
5655 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5658 /* Returns true iff class T has a constexpr destructor or has an
5659 implicitly declared destructor that we can't tell if it's constexpr
5660 without forcing a lazy declaration (which might cause undesired
5664 type_maybe_constexpr_destructor (tree t
)
5666 /* Until C++20, only trivial destruction is constexpr. */
5667 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t
))
5669 if (cxx_dialect
< cxx20
)
5671 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DESTRUCTOR (t
))
5672 /* Assume it's constexpr. */
5674 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5675 return (fn
&& maybe_constexpr_fn (fn
));
5678 /* Returns true iff class TYPE has a virtual destructor. */
5681 type_has_virtual_destructor (tree type
)
5685 if (!NON_UNION_CLASS_TYPE_P (type
))
5688 gcc_assert (COMPLETE_TYPE_P (type
));
5689 dtor
= CLASSTYPE_DESTRUCTOR (type
);
5690 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5693 /* True iff class TYPE has a non-deleted trivial default
5696 bool type_has_non_deleted_trivial_default_ctor (tree type
)
5698 return TYPE_HAS_TRIVIAL_DFLT (type
) && locate_ctor (type
);
5701 /* Returns true iff T, a class, has a move-assignment or
5702 move-constructor. Does not lazily declare either.
5703 If USER_P is false, any move function will do. If it is true, the
5704 move function must be user-declared.
5706 Note that user-declared here is different from "user-provided",
5707 which doesn't include functions that are defaulted in the
5711 classtype_has_move_assign_or_move_ctor_p (tree t
, bool user_p
)
5714 || (!CLASSTYPE_LAZY_MOVE_CTOR (t
)
5715 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t
)));
5717 if (!CLASSTYPE_LAZY_MOVE_CTOR (t
))
5718 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5719 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5722 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5723 for (ovl_iterator
iter (get_class_binding_direct
5724 (t
, assign_op_identifier
));
5726 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
))
5727 && DECL_CONTEXT (*iter
) == t
5728 && move_fn_p (*iter
))
5734 /* True iff T has a move constructor that is not deleted. */
5737 classtype_has_non_deleted_move_ctor (tree t
)
5739 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5740 lazily_declare_fn (sfk_move_constructor
, t
);
5741 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5742 if (move_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5747 /* If T, a class, has a user-provided copy constructor, copy assignment
5748 operator, or destructor, returns that function. Otherwise, null. */
5751 classtype_has_depr_implicit_copy (tree t
)
5753 if (!CLASSTYPE_LAZY_COPY_CTOR (t
))
5754 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5757 if (user_provided_p (fn
) && copy_fn_p (fn
))
5761 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t
))
5762 for (ovl_iterator
iter (get_class_binding_direct
5763 (t
, assign_op_identifier
));
5767 if (DECL_CONTEXT (fn
) == t
5768 && user_provided_p (fn
) && copy_fn_p (fn
))
5772 if (!CLASSTYPE_LAZY_DESTRUCTOR (t
))
5774 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5775 if (user_provided_p (fn
))
5782 /* True iff T has a member or friend declaration of operator OP. */
5785 classtype_has_op (tree t
, tree_code op
)
5787 tree name
= ovl_op_identifier (op
);
5788 if (get_class_binding (t
, name
))
5790 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5791 if (FRIEND_NAME (f
) == name
)
5797 /* If T has a defaulted member or friend declaration of OP, return it. */
5800 classtype_has_defaulted_op (tree t
, tree_code op
)
5802 tree name
= ovl_op_identifier (op
);
5803 for (ovl_iterator
oi (get_class_binding (t
, name
)); oi
; ++oi
)
5806 if (DECL_DEFAULTED_FN (fn
))
5809 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5810 if (FRIEND_NAME (f
) == name
)
5811 for (tree l
= FRIEND_DECLS (f
); l
; l
= TREE_CHAIN (l
))
5813 tree fn
= TREE_VALUE (l
);
5814 if (DECL_DEFAULTED_FN (fn
))
5820 /* Nonzero if we need to build up a constructor call when initializing an
5821 object of this class, either because it has a user-declared constructor
5822 or because it doesn't have a default constructor (so we need to give an
5823 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5824 what you care about is whether or not an object can be produced by a
5825 constructor (e.g. so we don't set TREE_READONLY on const variables of
5826 such type); use this function when what you care about is whether or not
5827 to try to call a constructor to create an object. The latter case is
5828 the former plus some cases of constructors that cannot be called. */
5831 type_build_ctor_call (tree t
)
5834 if (TYPE_NEEDS_CONSTRUCTING (t
))
5836 inner
= strip_array_types (t
);
5837 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5839 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5841 if (cxx_dialect
< cxx11
)
5843 /* A user-declared constructor might be private, and a constructor might
5844 be trivial but deleted. */
5845 for (ovl_iterator
iter (get_class_binding (inner
, complete_ctor_identifier
));
5849 if (!DECL_ARTIFICIAL (fn
)
5850 || TREE_DEPRECATED (fn
)
5851 || TREE_UNAVAILABLE (fn
)
5852 || DECL_DELETED_FN (fn
))
5858 /* Like type_build_ctor_call, but for destructors. */
5861 type_build_dtor_call (tree t
)
5864 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5866 inner
= strip_array_types (t
);
5867 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5868 || !COMPLETE_TYPE_P (inner
))
5870 if (cxx_dialect
< cxx11
)
5872 /* A user-declared destructor might be private, and a destructor might
5873 be trivial but deleted. */
5874 for (ovl_iterator
iter (get_class_binding (inner
, complete_dtor_identifier
));
5878 if (!DECL_ARTIFICIAL (fn
)
5879 || TREE_DEPRECATED (fn
)
5880 || TREE_UNAVAILABLE (fn
)
5881 || DECL_DELETED_FN (fn
))
5887 /* Returns TRUE iff we need a cookie when dynamically allocating an
5888 array whose elements have the indicated class TYPE. */
5891 type_requires_array_cookie (tree type
)
5894 bool has_two_argument_delete_p
= false;
5896 gcc_assert (CLASS_TYPE_P (type
));
5898 /* If there's a non-trivial destructor, we need a cookie. In order
5899 to iterate through the array calling the destructor for each
5900 element, we'll have to know how many elements there are. */
5901 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5904 /* If the usual deallocation function is a two-argument whose second
5905 argument is of type `size_t', then we have to pass the size of
5906 the array to the deallocation function, so we will need to store
5908 fns
= lookup_fnfields (TYPE_BINFO (type
),
5909 ovl_op_identifier (false, VEC_DELETE_EXPR
),
5910 /*protect=*/0, tf_warning_or_error
);
5911 /* If there are no `operator []' members, or the lookup is
5912 ambiguous, then we don't need a cookie. */
5913 if (!fns
|| fns
== error_mark_node
)
5915 /* Loop through all of the functions. */
5916 for (lkp_iterator
iter (BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
5920 /* See if this function is a one-argument delete function. If
5921 it is, then it will be the usual deallocation function. */
5922 tree second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5923 if (second_parm
== void_list_node
)
5925 /* Do not consider this function if its second argument is an
5929 /* Otherwise, if we have a two-argument function and the second
5930 argument is `size_t', it will be the usual deallocation
5931 function -- unless there is one-argument function, too. */
5932 if (TREE_CHAIN (second_parm
) == void_list_node
5933 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5934 has_two_argument_delete_p
= true;
5937 return has_two_argument_delete_p
;
5940 /* Finish computing the `literal type' property of class type T.
5942 At this point, we have already processed base classes and
5943 non-static data members. We need to check whether the copy
5944 constructor is trivial, the destructor is trivial, and there
5945 is a trivial default constructor or at least one constexpr
5946 constructor other than the copy constructor. */
5949 finalize_literal_type_property (tree t
)
5953 if (cxx_dialect
< cxx11
)
5954 CLASSTYPE_LITERAL_P (t
) = false;
5955 else if (CLASSTYPE_LITERAL_P (t
)
5956 && !type_maybe_constexpr_destructor (t
))
5957 CLASSTYPE_LITERAL_P (t
) = false;
5958 else if (CLASSTYPE_LITERAL_P (t
) && LAMBDA_TYPE_P (t
))
5959 CLASSTYPE_LITERAL_P (t
) = (cxx_dialect
>= cxx17
);
5960 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5961 && CLASSTYPE_NON_AGGREGATE (t
)
5962 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5963 CLASSTYPE_LITERAL_P (t
) = false;
5965 /* C++14 DR 1684 removed this restriction. */
5966 if (cxx_dialect
< cxx14
5967 && !CLASSTYPE_LITERAL_P (t
) && !LAMBDA_TYPE_P (t
))
5968 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5969 if (TREE_CODE (fn
) == FUNCTION_DECL
5970 && DECL_DECLARED_CONSTEXPR_P (fn
)
5971 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5972 && !DECL_CONSTRUCTOR_P (fn
))
5974 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5975 if (!DECL_GENERATED_P (fn
))
5977 auto_diagnostic_group d
;
5978 if (pedwarn (DECL_SOURCE_LOCATION (fn
), OPT_Wpedantic
,
5979 "enclosing class of %<constexpr%> non-static "
5980 "member function %q+#D is not a literal type", fn
))
5981 explain_non_literal_class (t
);
5986 /* T is a non-literal type used in a context which requires a constant
5987 expression. Explain why it isn't literal. */
5990 explain_non_literal_class (tree t
)
5992 static hash_set
<tree
> *diagnosed
;
5994 if (!CLASS_TYPE_P (t
))
5996 t
= TYPE_MAIN_VARIANT (t
);
5998 if (diagnosed
== NULL
)
5999 diagnosed
= new hash_set
<tree
>;
6000 if (diagnosed
->add (t
))
6001 /* Already explained. */
6004 auto_diagnostic_group d
;
6005 inform (UNKNOWN_LOCATION
, "%q+T is not literal because:", t
);
6006 if (cxx_dialect
< cxx17
&& LAMBDA_TYPE_P (t
))
6007 inform (UNKNOWN_LOCATION
,
6008 " %qT is a closure type, which is only literal in "
6009 "C++17 and later", t
);
6010 else if (cxx_dialect
< cxx20
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
6011 inform (UNKNOWN_LOCATION
, " %q+T has a non-trivial destructor", t
);
6012 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
6013 && !type_maybe_constexpr_destructor (t
))
6014 inform (UNKNOWN_LOCATION
, " %q+T does not have %<constexpr%> destructor",
6016 else if (CLASSTYPE_NON_AGGREGATE (t
)
6017 && !TYPE_HAS_TRIVIAL_DFLT (t
)
6018 && !LAMBDA_TYPE_P (t
)
6019 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
6021 inform (UNKNOWN_LOCATION
,
6022 " %q+T is not an aggregate, does not have a trivial "
6023 "default constructor, and has no %<constexpr%> constructor that "
6024 "is not a copy or move constructor", t
);
6025 if (type_has_non_user_provided_default_constructor (t
))
6026 /* Note that we can't simply call locate_ctor because when the
6027 constructor is deleted it just returns NULL_TREE. */
6028 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
6031 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6033 parms
= skip_artificial_parms_for (fn
, parms
);
6035 if (sufficient_parms_p (parms
))
6037 if (DECL_DELETED_FN (fn
))
6038 maybe_explain_implicit_delete (fn
);
6040 explain_invalid_constexpr_fn (fn
);
6047 tree binfo
, base_binfo
, field
; int i
;
6048 for (binfo
= TYPE_BINFO (t
), i
= 0;
6049 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
6051 tree basetype
= TREE_TYPE (base_binfo
);
6052 if (!CLASSTYPE_LITERAL_P (basetype
))
6054 inform (UNKNOWN_LOCATION
,
6055 " base class %qT of %q+T is non-literal",
6057 explain_non_literal_class (basetype
);
6061 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
6064 if (TREE_CODE (field
) != FIELD_DECL
)
6066 ftype
= TREE_TYPE (field
);
6067 if (!literal_type_p (ftype
))
6069 inform (DECL_SOURCE_LOCATION (field
),
6070 " non-static data member %qD has non-literal type",
6072 if (CLASS_TYPE_P (ftype
))
6073 explain_non_literal_class (ftype
);
6075 if (CP_TYPE_VOLATILE_P (ftype
))
6076 inform (DECL_SOURCE_LOCATION (field
),
6077 " non-static data member %qD has volatile type", field
);
6082 /* Check the validity of the bases and members declared in T. Add any
6083 implicitly-generated functions (like copy-constructors and
6084 assignment operators). Compute various flag bits (like
6085 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6086 level: i.e., independently of the ABI in use. */
6089 check_bases_and_members (tree t
)
6091 /* Nonzero if the implicitly generated copy constructor should take
6092 a non-const reference argument. */
6093 int cant_have_const_ctor
;
6094 /* Nonzero if the implicitly generated assignment operator
6095 should take a non-const reference argument. */
6096 int no_const_asn_ref
;
6098 bool saved_complex_asn_ref
;
6099 bool saved_nontrivial_dtor
;
6102 /* By default, we use const reference arguments and generate default
6104 cant_have_const_ctor
= 0;
6105 no_const_asn_ref
= 0;
6107 /* Check all the base-classes and set FMEM members to point to arrays
6108 of potential interest. */
6109 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
6111 /* Deduce noexcept on destructor. This needs to happen after we've set
6112 triviality flags appropriately for our bases, and before checking
6113 overriden virtual functions via check_methods. */
6114 if (cxx_dialect
>= cxx11
)
6115 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
6116 for (tree fn
: ovl_range (dtor
))
6117 deduce_noexcept_on_destructor (fn
);
6119 /* Check all the method declarations. */
6122 /* Save the initial values of these flags which only indicate whether
6123 or not the class has user-provided functions. As we analyze the
6124 bases and members we can set these flags for other reasons. */
6125 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
6126 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
6128 /* Check all the data member declarations. We cannot call
6129 check_field_decls until we have called check_bases check_methods,
6130 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6131 being set appropriately. */
6132 check_field_decls (t
, &access_decls
,
6133 &cant_have_const_ctor
,
6136 /* A nearly-empty class has to be vptr-containing; a nearly empty
6137 class contains just a vptr. */
6138 if (!TYPE_CONTAINS_VPTR_P (t
))
6139 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
6141 /* Do some bookkeeping that will guide the generation of implicitly
6142 declared member functions. */
6143 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6144 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6145 /* We need to call a constructor for this class if it has a
6146 user-provided constructor, or if the default constructor is going
6147 to initialize the vptr. (This is not an if-and-only-if;
6148 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6149 themselves need constructing.) */
6150 TYPE_NEEDS_CONSTRUCTING (t
)
6151 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
6154 An aggregate is an array or a class with no user-provided
6155 constructors ... and no virtual functions.
6157 Again, other conditions for being an aggregate are checked
6159 CLASSTYPE_NON_AGGREGATE (t
)
6160 |= ((cxx_dialect
< cxx20
6161 ? type_has_user_provided_or_explicit_constructor (t
)
6162 : TYPE_HAS_USER_CONSTRUCTOR (t
))
6163 || TYPE_POLYMORPHIC_P (t
));
6164 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6165 retain the old definition internally for ABI reasons. */
6166 CLASSTYPE_NON_LAYOUT_POD_P (t
)
6167 |= (CLASSTYPE_NON_AGGREGATE (t
)
6168 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
6169 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6170 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6171 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6172 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6174 /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6175 if (CLASSTYPE_NON_POD_AGGREGATE (t
))
6177 if (CLASSTYPE_NON_LAYOUT_POD_P (t
))
6178 /* It's non-POD for another reason. */
6179 CLASSTYPE_NON_POD_AGGREGATE (t
) = false;
6180 else if (abi_version_at_least (17))
6181 CLASSTYPE_NON_LAYOUT_POD_P (t
) = true;
6184 /* If the only explicitly declared default constructor is user-provided,
6185 set TYPE_HAS_COMPLEX_DFLT. */
6186 if (!TYPE_HAS_COMPLEX_DFLT (t
)
6187 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
6188 && !type_has_non_user_provided_default_constructor (t
))
6189 TYPE_HAS_COMPLEX_DFLT (t
) = true;
6191 /* Warn if a public base of a polymorphic type has an accessible
6192 non-virtual destructor. It is only now that we know the class is
6193 polymorphic. Although a polymorphic base will have a already
6194 been diagnosed during its definition, we warn on use too. */
6195 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
6197 tree binfo
= TYPE_BINFO (t
);
6198 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
6202 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
6204 tree basetype
= TREE_TYPE (base_binfo
);
6206 if ((*accesses
)[i
] == access_public_node
6207 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
6208 && accessible_nvdtor_p (basetype
))
6209 warning (OPT_Wnon_virtual_dtor
,
6210 "base class %q#T has accessible non-virtual destructor",
6215 /* If the class has no user-declared constructor, but does have
6216 non-static const or reference data members that can never be
6217 initialized, issue a warning. */
6218 if (warn_uninitialized
6219 /* Classes with user-declared constructors are presumed to
6220 initialize these members. */
6221 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
6222 /* Aggregates can be initialized with brace-enclosed
6224 && CLASSTYPE_NON_AGGREGATE (t
))
6228 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6232 if (TREE_CODE (field
) != FIELD_DECL
6233 || DECL_INITIAL (field
) != NULL_TREE
)
6236 type
= TREE_TYPE (field
);
6237 if (TYPE_REF_P (type
))
6238 warning_at (DECL_SOURCE_LOCATION (field
),
6239 OPT_Wuninitialized
, "non-static reference %q#D "
6240 "in class without a constructor", field
);
6241 else if (CP_TYPE_CONST_P (type
)
6242 && (!CLASS_TYPE_P (type
)
6243 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
6244 warning_at (DECL_SOURCE_LOCATION (field
),
6245 OPT_Wuninitialized
, "non-static const member %q#D "
6246 "in class without a constructor", field
);
6250 /* Synthesize any needed methods. */
6251 add_implicitly_declared_members (t
, &access_decls
,
6252 cant_have_const_ctor
,
6255 /* Check defaulted declarations here so we have cant_have_const_ctor
6256 and don't need to worry about clones. */
6257 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6258 if (DECL_DECLARES_FUNCTION_P (fn
)
6259 && !DECL_ARTIFICIAL (fn
)
6260 && DECL_DEFAULTED_IN_CLASS_P (fn
))
6262 /* ...except handle comparisons later, in finish_struct_1. */
6263 if (special_function_p (fn
) == sfk_comparison
)
6266 int copy
= copy_fn_p (fn
);
6270 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
6271 : !no_const_asn_ref
);
6272 bool fn_const_p
= (copy
== 2);
6274 if (fn_const_p
&& !imp_const_p
)
6275 /* If the function is defaulted outside the class, we just
6276 give the synthesis error. Core Issue #1331 says this is
6277 no longer ill-formed, it is defined as deleted instead. */
6278 DECL_DELETED_FN (fn
) = true;
6280 defaulted_late_check (fn
);
6283 if (LAMBDA_TYPE_P (t
))
6284 /* "This class type is not an aggregate." */
6285 CLASSTYPE_NON_AGGREGATE (t
) = 1;
6287 /* Compute the 'literal type' property before we
6288 do anything with non-static member functions. */
6289 finalize_literal_type_property (t
);
6291 /* Create the in-charge and not-in-charge variants of constructors
6293 clone_constructors_and_destructors (t
);
6295 /* Process the using-declarations. */
6296 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
6297 handle_using_decl (TREE_VALUE (access_decls
), t
);
6299 /* Figure out whether or not we will need a cookie when dynamically
6300 allocating an array of this type. */
6301 LANG_TYPE_CLASS_CHECK (t
)->vec_new_uses_cookie
6302 = type_requires_array_cookie (t
);
6304 /* Classes marked hot or cold propagate the attribute to all members. We
6305 may do this now that methods are declared. This does miss some lazily
6306 declared special member functions (CLASSTYPE_LAZY_*), which are handled
6307 in lazily_declare_fn later on. */
6308 propagate_class_warmth_attribute (t
);
6311 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6312 accordingly. If a new vfield was created (because T doesn't have a
6313 primary base class), then the newly created field is returned. It
6314 is not added to the TYPE_FIELDS list; it is the caller's
6315 responsibility to do that. Accumulate declared virtual functions
6319 create_vtable_ptr (tree t
, tree
* virtuals_p
)
6323 /* Collect the virtual functions declared in T. */
6324 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6325 if (TREE_CODE (fn
) == FUNCTION_DECL
6326 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
6327 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
6329 tree new_virtual
= make_node (TREE_LIST
);
6331 BV_FN (new_virtual
) = fn
;
6332 BV_DELTA (new_virtual
) = integer_zero_node
;
6333 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
6335 TREE_CHAIN (new_virtual
) = *virtuals_p
;
6336 *virtuals_p
= new_virtual
;
6339 /* If we couldn't find an appropriate base class, create a new field
6340 here. Even if there weren't any new virtual functions, we might need a
6341 new virtual function table if we're supposed to include vptrs in
6342 all classes that need them. */
6343 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
6345 /* We build this decl with vtbl_ptr_type_node, which is a
6346 `vtable_entry_type*'. It might seem more precise to use
6347 `vtable_entry_type (*)[N]' where N is the number of virtual
6348 functions. However, that would require the vtable pointer in
6349 base classes to have a different type than the vtable pointer
6350 in derived classes. We could make that happen, but that
6351 still wouldn't solve all the problems. In particular, the
6352 type-based alias analysis code would decide that assignments
6353 to the base class vtable pointer can't alias assignments to
6354 the derived class vtable pointer, since they have different
6355 types. Thus, in a derived class destructor, where the base
6356 class constructor was inlined, we could generate bad code for
6357 setting up the vtable pointer.
6359 Therefore, we use one type for all vtable pointers. We still
6360 use a type-correct type; it's just doesn't indicate the array
6361 bounds. That's better than using `void*' or some such; it's
6362 cleaner, and it let's the alias analysis code know that these
6363 stores cannot alias stores to void*! */
6366 field
= build_decl (input_location
,
6367 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
6368 DECL_VIRTUAL_P (field
) = 1;
6369 DECL_ARTIFICIAL (field
) = 1;
6370 DECL_FIELD_CONTEXT (field
) = t
;
6371 DECL_FCONTEXT (field
) = t
;
6372 if (TYPE_PACKED (t
))
6373 DECL_PACKED (field
) = 1;
6375 TYPE_VFIELD (t
) = field
;
6377 /* This class is non-empty. */
6378 CLASSTYPE_EMPTY_P (t
) = 0;
6386 /* Add OFFSET to all base types of BINFO which is a base in the
6387 hierarchy dominated by T.
6389 OFFSET, which is a type offset, is number of bytes. */
6392 propagate_binfo_offsets (tree binfo
, tree offset
)
6398 /* Update BINFO's offset. */
6399 BINFO_OFFSET (binfo
)
6400 = fold_convert (sizetype
,
6401 size_binop (PLUS_EXPR
,
6402 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
6405 /* Find the primary base class. */
6406 primary_binfo
= get_primary_binfo (binfo
);
6408 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
6409 propagate_binfo_offsets (primary_binfo
, offset
);
6411 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6413 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6415 /* Don't do the primary base twice. */
6416 if (base_binfo
== primary_binfo
)
6419 if (BINFO_VIRTUAL_P (base_binfo
))
6422 propagate_binfo_offsets (base_binfo
, offset
);
6426 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6427 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6428 empty subobjects of T. */
6431 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
6437 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
6440 /* Find the last field. The artificial fields created for virtual
6441 bases will go after the last extant field to date. */
6442 next_field
= &TYPE_FIELDS (t
);
6444 next_field
= &DECL_CHAIN (*next_field
);
6446 /* Go through the virtual bases, allocating space for each virtual
6447 base that is not already a primary base class. These are
6448 allocated in inheritance graph order. */
6449 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6451 if (!BINFO_VIRTUAL_P (vbase
))
6454 if (!BINFO_PRIMARY_P (vbase
))
6456 /* This virtual base is not a primary base of any class in the
6457 hierarchy, so we have to add space for it. */
6458 next_field
= build_base_field (rli
, vbase
,
6459 access_private_node
,
6460 offsets
, next_field
);
6465 /* Returns the offset of the byte just past the end of the base class
6469 end_of_base (tree binfo
)
6473 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
6474 size
= TYPE_SIZE_UNIT (char_type_node
);
6475 else if (is_empty_class (BINFO_TYPE (binfo
)))
6476 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6477 allocate some space for it. It cannot have virtual bases, so
6478 TYPE_SIZE_UNIT is fine. */
6479 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6481 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6483 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
6486 /* Returns one of three variations of the ending offset of T. If MODE is
6487 eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6488 vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6489 vbases but before rounding, which is not named in the ABI. If MODE is
6490 eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6491 of the actual data in the class, kinda), as used for allocation of
6492 potentially-overlapping fields. */
6494 enum eoc_mode
{ eoc_nvsize
, eoc_vsize
, eoc_nv_or_dsize
};
6496 end_of_class (tree t
, eoc_mode mode
)
6498 tree result
= size_zero_node
;
6499 vec
<tree
, va_gc
> *vbases
;
6505 for (binfo
= TYPE_BINFO (t
), i
= 0;
6506 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6508 if (BINFO_VIRTUAL_P (base_binfo
)
6509 && (!BINFO_PRIMARY_P (base_binfo
)
6510 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
6513 offset
= end_of_base (base_binfo
);
6514 if (tree_int_cst_lt (result
, offset
))
6518 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6519 if (TREE_CODE (field
) == FIELD_DECL
6520 && !DECL_FIELD_IS_BASE (field
))
6522 tree size
= DECL_SIZE_UNIT (field
);
6524 /* DECL_SIZE_UNIT can be null for a flexible array. */
6527 if (is_empty_field (field
))
6528 /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6529 size of the type (usually 1) for computing nvsize. */
6530 size
= TYPE_SIZE_UNIT (TREE_TYPE (field
));
6532 if (DECL_BIT_FIELD_TYPE (field
))
6534 offset
= size_binop (PLUS_EXPR
, bit_position (field
),
6536 offset
= size_binop (CEIL_DIV_EXPR
, offset
, bitsize_unit_node
);
6537 offset
= fold_convert (sizetype
, offset
);
6540 offset
= size_binop (PLUS_EXPR
, byte_position (field
), size
);
6541 if (tree_int_cst_lt (result
, offset
))
6545 if (mode
!= eoc_nvsize
)
6546 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6547 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6549 if (mode
== eoc_nv_or_dsize
)
6550 /* For dsize, don't count trailing empty bases. */
6551 offset
= size_binop (PLUS_EXPR
, BINFO_OFFSET (base_binfo
),
6552 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo
)));
6554 offset
= end_of_base (base_binfo
);
6555 if (tree_int_cst_lt (result
, offset
))
6562 /* Warn as appropriate about the change in whether we pack into the tail
6563 padding of FIELD, a base field which has a C++14 aggregate type with default
6564 member initializers. */
6567 check_non_pod_aggregate (tree field
)
6569 if (!abi_version_crosses (17) || cxx_dialect
< cxx14
)
6571 if (TREE_CODE (field
) != FIELD_DECL
6572 || (!DECL_FIELD_IS_BASE (field
)
6573 && !field_poverlapping_p (field
)))
6575 tree next
= DECL_CHAIN (field
);
6576 while (next
&& TREE_CODE (next
) != FIELD_DECL
) next
= DECL_CHAIN (next
);
6579 tree type
= TREE_TYPE (field
);
6580 if (TYPE_IDENTIFIER (type
) == as_base_identifier
)
6581 type
= TYPE_CONTEXT (type
);
6582 if (!CLASS_TYPE_P (type
) || !CLASSTYPE_NON_POD_AGGREGATE (type
))
6584 tree size
= end_of_class (type
, (DECL_FIELD_IS_BASE (field
)
6585 ? eoc_nvsize
: eoc_nv_or_dsize
));
6586 tree rounded
= round_up_loc (input_location
, size
, DECL_ALIGN_UNIT (next
));
6587 if (tree_int_cst_lt (rounded
, TYPE_SIZE_UNIT (type
)))
6589 location_t loc
= DECL_SOURCE_LOCATION (next
);
6590 if (DECL_FIELD_IS_BASE (next
))
6591 warning_at (loc
, OPT_Wabi
,"offset of %qT base class for "
6592 "%<-std=c++14%> and up changes in "
6593 "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next
));
6595 warning_at (loc
, OPT_Wabi
, "offset of %qD for "
6596 "%<-std=c++14%> and up changes in "
6597 "%<-fabi-version=17%> (GCC 12)", next
);
6601 /* Warn about bases of T that are inaccessible because they are
6602 ambiguous. For example:
6605 struct T : public S {};
6606 struct U : public S, public T {};
6608 Here, `(S*) new U' is not allowed because there are two `S'
6612 maybe_warn_about_inaccessible_bases (tree t
)
6615 vec
<tree
, va_gc
> *vbases
;
6620 /* If not checking for warning then return early. */
6621 if (!warn_inaccessible_base
)
6624 /* If there are no repeated bases, nothing can be ambiguous. */
6625 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6628 /* Check direct bases. */
6629 for (binfo
= TYPE_BINFO (t
), i
= 0;
6630 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6632 basetype
= BINFO_TYPE (base_binfo
);
6634 if (!uniquely_derived_from_p (basetype
, t
))
6635 warning (OPT_Winaccessible_base
, "direct base %qT inaccessible "
6636 "in %qT due to ambiguity", basetype
, t
);
6639 /* Check for ambiguous virtual bases. */
6641 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6642 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6644 basetype
= BINFO_TYPE (binfo
);
6646 if (!uniquely_derived_from_p (basetype
, t
))
6647 warning (OPT_Winaccessible_base
, "virtual base %qT inaccessible in "
6648 "%qT due to ambiguity", basetype
, t
);
6652 /* Compare two INTEGER_CSTs K1 and K2. */
6655 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6657 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6660 /* Increase the size indicated in RLI to account for empty classes
6661 that are "off the end" of the class. */
6664 include_empty_classes (record_layout_info rli
)
6669 /* It might be the case that we grew the class to allocate a
6670 zero-sized base class. That won't be reflected in RLI, yet,
6671 because we are willing to overlay multiple bases at the same
6672 offset. However, now we need to make sure that RLI is big enough
6673 to reflect the entire class. */
6674 eoc
= end_of_class (rli
->t
, eoc_vsize
);
6675 rli_size
= rli_size_unit_so_far (rli
);
6676 if (TREE_CODE (rli_size
) == INTEGER_CST
6677 && tree_int_cst_lt (rli_size
, eoc
))
6679 /* The size should have been rounded to a whole byte. */
6680 gcc_assert (tree_int_cst_equal
6681 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6683 = size_binop (PLUS_EXPR
,
6685 size_binop (MULT_EXPR
,
6686 fold_convert (bitsizetype
,
6687 size_binop (MINUS_EXPR
,
6689 bitsize_int (BITS_PER_UNIT
)));
6690 normalize_rli (rli
);
6694 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6695 BINFO_OFFSETs for all of the base-classes. Position the vtable
6696 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6699 layout_class_type (tree t
, tree
*virtuals_p
)
6701 tree non_static_data_members
;
6704 record_layout_info rli
;
6705 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6706 types that appear at that offset. */
6707 splay_tree empty_base_offsets
;
6708 /* True if the last field laid out was a bit-field. */
6709 bool last_field_was_bitfield
= false;
6710 /* The location at which the next field should be inserted. */
6713 /* Keep track of the first non-static data member. */
6714 non_static_data_members
= TYPE_FIELDS (t
);
6716 /* Start laying out the record. */
6717 rli
= start_record_layout (t
);
6719 /* Mark all the primary bases in the hierarchy. */
6720 determine_primary_bases (t
);
6722 /* Create a pointer to our virtual function table. */
6723 vptr
= create_vtable_ptr (t
, virtuals_p
);
6725 /* The vptr is always the first thing in the class. */
6728 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6729 TYPE_FIELDS (t
) = vptr
;
6730 next_field
= &DECL_CHAIN (vptr
);
6731 place_field (rli
, vptr
);
6734 next_field
= &TYPE_FIELDS (t
);
6736 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6737 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6739 build_base_fields (rli
, empty_base_offsets
, next_field
);
6741 /* Layout the non-static data members. */
6742 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6747 /* We still pass things that aren't non-static data members to
6748 the back end, in case it wants to do something with them. */
6749 if (TREE_CODE (field
) != FIELD_DECL
)
6751 place_field (rli
, field
);
6752 /* If the static data member has incomplete type, keep track
6753 of it so that it can be completed later. (The handling
6754 of pending statics in finish_record_layout is
6755 insufficient; consider:
6758 struct S2 { static S1 s1; };
6760 At this point, finish_record_layout will be called, but
6761 S1 is still incomplete.) */
6764 maybe_register_incomplete_var (field
);
6765 /* The visibility of static data members is determined
6766 at their point of declaration, not their point of
6768 determine_visibility (field
);
6773 type
= TREE_TYPE (field
);
6774 if (type
== error_mark_node
)
6777 padding
= NULL_TREE
;
6779 bool might_overlap
= field_poverlapping_p (field
);
6781 if (might_overlap
&& CLASS_TYPE_P (type
)
6782 && (CLASSTYPE_NON_LAYOUT_POD_P (type
) || CLASSTYPE_EMPTY_P (type
)))
6784 /* if D is a potentially-overlapping data member, update sizeof(C) to
6785 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6786 if (CLASSTYPE_EMPTY_P (type
))
6787 DECL_SIZE (field
) = DECL_SIZE_UNIT (field
) = size_zero_node
;
6790 tree size
= end_of_class (type
, eoc_nv_or_dsize
);
6791 DECL_SIZE_UNIT (field
) = size
;
6792 DECL_SIZE (field
) = bit_from_pos (size
, bitsize_zero_node
);
6796 /* If this field is a bit-field whose width is greater than its
6797 type, then there are some special rules for allocating
6799 if (DECL_C_BIT_FIELD (field
)
6800 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6802 bool was_unnamed_p
= false;
6803 /* We must allocate the bits as if suitably aligned for the
6804 longest integer type that fits in this many bits. Then,
6805 we are supposed to use the left over bits as additional
6808 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6809 tree limit
= size_int (MAX_FIXED_MODE_SIZE
);
6810 if (tree_int_cst_lt (DECL_SIZE (field
), limit
))
6811 limit
= DECL_SIZE (field
);
6813 tree integer_type
= integer_types
[itk_char
];
6814 for (unsigned itk
= itk_char
; itk
!= itk_none
; itk
++)
6815 if (tree next
= integer_types
[itk
])
6817 if (tree_int_cst_lt (limit
, TYPE_SIZE (next
)))
6818 /* Too big, so our current guess is what we want. */
6820 /* Not bigger than limit, ok */
6821 integer_type
= next
;
6824 /* Figure out how much additional padding is required. */
6825 if (TREE_CODE (t
) == UNION_TYPE
)
6826 /* In a union, the padding field must have the full width
6827 of the bit-field; all fields start at offset zero. */
6828 padding
= DECL_SIZE (field
);
6830 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6831 TYPE_SIZE (integer_type
));
6833 if (integer_zerop (padding
))
6834 padding
= NULL_TREE
;
6836 /* An unnamed bitfield does not normally affect the
6837 alignment of the containing class on a target where
6838 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6839 make any exceptions for unnamed bitfields when the
6840 bitfields are longer than their types. Therefore, we
6841 temporarily give the field a name. */
6842 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6844 was_unnamed_p
= true;
6845 DECL_NAME (field
) = make_anon_name ();
6848 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6849 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6850 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6851 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6852 empty_base_offsets
);
6854 DECL_NAME (field
) = NULL_TREE
;
6855 /* Now that layout has been performed, set the size of the
6856 field to the size of its declared type; the rest of the
6857 field is effectively invisible. */
6858 DECL_SIZE (field
) = TYPE_SIZE (type
);
6859 /* We must also reset the DECL_MODE of the field. */
6860 SET_DECL_MODE (field
, TYPE_MODE (type
));
6862 else if (might_overlap
&& is_empty_class (type
))
6864 SET_DECL_FIELD_ABI_IGNORED (field
, 1);
6865 layout_empty_base_or_field (rli
, field
, empty_base_offsets
);
6868 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6869 empty_base_offsets
);
6871 /* Remember the location of any empty classes in FIELD. */
6872 record_subobject_offsets (field
, empty_base_offsets
);
6874 /* If a bit-field does not immediately follow another bit-field,
6875 and yet it starts in the middle of a byte, we have failed to
6876 comply with the ABI. */
6878 && DECL_C_BIT_FIELD (field
)
6879 /* The TREE_NO_WARNING flag gets set by Objective-C when
6880 laying out an Objective-C class. The ObjC ABI differs
6881 from the C++ ABI, and so we do not want a warning
6883 && !warning_suppressed_p (field
, OPT_Wabi
)
6884 && !last_field_was_bitfield
6885 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6886 DECL_FIELD_BIT_OFFSET (field
),
6887 bitsize_unit_node
)))
6888 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6889 "offset of %qD is not ABI-compliant and may "
6890 "change in a future version of GCC", field
);
6892 /* The middle end uses the type of expressions to determine the
6893 possible range of expression values. In order to optimize
6894 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6895 must be made aware of the width of "i", via its type.
6897 Because C++ does not have integer types of arbitrary width,
6898 we must (for the purposes of the front end) convert from the
6899 type assigned here to the declared type of the bitfield
6900 whenever a bitfield expression is used as an rvalue.
6901 Similarly, when assigning a value to a bitfield, the value
6902 must be converted to the type given the bitfield here. */
6903 if (DECL_C_BIT_FIELD (field
))
6905 unsigned HOST_WIDE_INT width
;
6906 tree ftype
= TREE_TYPE (field
);
6907 width
= tree_to_uhwi (DECL_SIZE (field
));
6908 if (width
!= TYPE_PRECISION (ftype
))
6911 = c_build_bitfield_integer_type (width
,
6912 TYPE_UNSIGNED (ftype
));
6914 = cp_build_qualified_type (TREE_TYPE (field
),
6915 cp_type_quals (ftype
));
6919 /* If we needed additional padding after this field, add it
6925 padding_field
= build_decl (input_location
,
6929 DECL_BIT_FIELD (padding_field
) = 1;
6930 DECL_SIZE (padding_field
) = padding
;
6931 DECL_CONTEXT (padding_field
) = t
;
6932 DECL_ARTIFICIAL (padding_field
) = 1;
6933 DECL_IGNORED_P (padding_field
) = 1;
6934 DECL_PADDING_P (padding_field
) = 1;
6935 layout_nonempty_base_or_field (rli
, padding_field
,
6937 empty_base_offsets
);
6940 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6943 if (!integer_zerop (rli
->bitpos
))
6945 /* Make sure that we are on a byte boundary so that the size of
6946 the class without virtual bases will always be a round number
6948 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6949 normalize_rli (rli
);
6952 /* We used to remove zero width bitfields at this point since PR42217,
6953 while the C FE never did that. That caused ABI differences on various
6954 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
6955 instead, so that the backends can emit -Wpsabi warnings in the cases
6956 where the ABI changed. */
6957 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6959 if (TREE_CODE (field
) == FIELD_DECL
6960 && DECL_C_BIT_FIELD (field
)
6961 /* We should not be confused by the fact that grokbitfield
6962 temporarily sets the width of the bit field into
6963 DECL_BIT_FIELD_REPRESENTATIVE (field).
6964 check_bitfield_decl eventually sets DECL_SIZE (field)
6966 && (DECL_SIZE (field
) == NULL_TREE
6967 || integer_zerop (DECL_SIZE (field
)))
6968 && TREE_TYPE (field
) != error_mark_node
)
6969 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field
, 1);
6970 check_non_pod_aggregate (field
);
6973 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6975 /* T needs a different layout as a base (eliding virtual bases
6976 or whatever). Create that version. */
6977 tree base_t
= make_node (TREE_CODE (t
));
6978 tree base_d
= create_implicit_typedef (as_base_identifier
, base_t
);
6980 TYPE_CONTEXT (base_t
) = t
;
6981 DECL_CONTEXT (base_d
) = t
;
6983 set_instantiating_module (base_d
);
6985 /* If the ABI version is not at least two, and the last
6986 field was a bit-field, RLI may not be on a byte
6987 boundary. In particular, rli_size_unit_so_far might
6988 indicate the last complete byte, while rli_size_so_far
6989 indicates the total number of bits used. Therefore,
6990 rli_size_so_far, rather than rli_size_unit_so_far, is
6991 used to compute TYPE_SIZE_UNIT. */
6993 /* Set the size and alignment for the new type. */
6994 tree eoc
= end_of_class (t
, eoc_nvsize
);
6995 TYPE_SIZE_UNIT (base_t
)
6996 = size_binop (MAX_EXPR
,
6997 fold_convert (sizetype
,
6998 size_binop (CEIL_DIV_EXPR
,
6999 rli_size_so_far (rli
),
7000 bitsize_int (BITS_PER_UNIT
))),
7003 = size_binop (MAX_EXPR
,
7004 rli_size_so_far (rli
),
7005 size_binop (MULT_EXPR
,
7006 fold_convert (bitsizetype
, eoc
),
7007 bitsize_int (BITS_PER_UNIT
)));
7008 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
7009 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
7010 TYPE_TYPELESS_STORAGE (base_t
) = TYPE_TYPELESS_STORAGE (t
);
7011 TYPE_CXX_ODR_P (base_t
) = TYPE_CXX_ODR_P (t
);
7013 /* Copy the non-static data members of T. This will include its
7014 direct non-virtual bases & vtable. */
7015 next_field
= &TYPE_FIELDS (base_t
);
7016 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
7017 if (TREE_CODE (field
) == FIELD_DECL
)
7019 *next_field
= copy_node (field
);
7020 /* Zap any NSDMI, it's not needed and might be a deferred
7022 DECL_INITIAL (*next_field
) = NULL_TREE
;
7023 DECL_CONTEXT (*next_field
) = base_t
;
7024 next_field
= &DECL_CHAIN (*next_field
);
7026 *next_field
= NULL_TREE
;
7028 /* We use the base type for trivial assignments, and hence it
7030 compute_record_mode (base_t
);
7032 /* Record the base version of the type. */
7033 CLASSTYPE_AS_BASE (t
) = base_t
;
7036 CLASSTYPE_AS_BASE (t
) = t
;
7038 /* Every empty class contains an empty class. */
7039 if (CLASSTYPE_EMPTY_P (t
))
7040 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
7042 /* Set the TYPE_DECL for this type to contain the right
7043 value for DECL_OFFSET, so that we can use it as part
7044 of a COMPONENT_REF for multiple inheritance. */
7045 layout_decl (TYPE_MAIN_DECL (t
), 0);
7047 /* Now fix up any virtual base class types that we left lying
7048 around. We must get these done before we try to lay out the
7049 virtual function table. As a side-effect, this will remove the
7050 base subobject fields. */
7051 layout_virtual_bases (rli
, empty_base_offsets
);
7053 /* Make sure that empty classes are reflected in RLI at this
7055 include_empty_classes (rli
);
7057 /* Make sure not to create any structures with zero size. */
7058 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
7060 build_decl (input_location
,
7061 FIELD_DECL
, NULL_TREE
, char_type_node
));
7063 /* If this is a non-POD, declaring it packed makes a difference to how it
7064 can be used as a field; don't let finalize_record_size undo it. */
7065 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
7066 rli
->packed_maybe_necessary
= true;
7068 /* Let the back end lay out the type. */
7069 finish_record_layout (rli
, /*free_p=*/true);
7071 /* If we didn't end up needing an as-base type, don't use it. */
7072 if (CLASSTYPE_AS_BASE (t
) != t
7073 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7074 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7075 causing us to lose the user-specified alignment as in PR94050. */
7076 && TYPE_USER_ALIGN (t
) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t
))
7077 && tree_int_cst_equal (TYPE_SIZE (t
),
7078 TYPE_SIZE (CLASSTYPE_AS_BASE (t
))))
7079 CLASSTYPE_AS_BASE (t
) = t
;
7081 if (TYPE_SIZE_UNIT (t
)
7082 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
7083 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
7084 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
7085 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
7087 /* Warn about bases that can't be talked about due to ambiguity. */
7088 maybe_warn_about_inaccessible_bases (t
);
7090 /* Now that we're done with layout, give the base fields the real types. */
7091 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
7092 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
7093 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
7096 splay_tree_delete (empty_base_offsets
);
7098 if (CLASSTYPE_EMPTY_P (t
)
7099 && tree_int_cst_lt (sizeof_biggest_empty_class
,
7100 TYPE_SIZE_UNIT (t
)))
7101 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
7104 /* Determine the "key method" for the class type indicated by TYPE,
7105 and set CLASSTYPE_KEY_METHOD accordingly. */
7108 determine_key_method (tree type
)
7112 if (processing_template_decl
7113 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
7114 || CLASSTYPE_INTERFACE_KNOWN (type
))
7117 /* The key method is the first non-pure virtual function that is not
7118 inline at the point of class definition. On some targets the
7119 key function may not be inline; those targets should not call
7120 this function until the end of the translation unit. */
7121 for (method
= TYPE_FIELDS (type
); method
; method
= DECL_CHAIN (method
))
7122 if (TREE_CODE (method
) == FUNCTION_DECL
7123 && DECL_VINDEX (method
) != NULL_TREE
7124 && ! DECL_DECLARED_INLINE_P (method
)
7125 && ! DECL_PURE_VIRTUAL_P (method
))
7127 CLASSTYPE_KEY_METHOD (type
) = method
;
7134 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7135 class data member of non-zero size, otherwise false. */
7138 field_nonempty_p (const_tree fld
)
7140 if (TREE_CODE (fld
) == ERROR_MARK
)
7143 tree type
= TREE_TYPE (fld
);
7144 if (TREE_CODE (fld
) == FIELD_DECL
7145 && TREE_CODE (type
) != ERROR_MARK
7146 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
7148 return TYPE_SIZE (type
)
7149 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
7150 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
7156 /* Used by find_flexarrays and related functions. */
7160 /* The first flexible array member or non-zero array member found
7161 in the order of layout. */
7163 /* First non-static non-empty data member in the class or its bases. */
7165 /* The first non-static non-empty data member following either
7166 the flexible array member, if found, or the zero-length array member
7167 otherwise. AFTER[1] refers to the first such data member of a union
7168 of which the struct containing the flexible array member or zero-length
7169 array is a member, or NULL when no such union exists. This element is
7170 only used during searching, not for diagnosing problems. AFTER[0]
7171 refers to the first such data member that is not a member of such
7175 /* Refers to a struct (not union) in which the struct of which the flexible
7176 array is member is defined. Used to diagnose strictly (according to C)
7177 invalid uses of the latter structs. */
7181 /* Find either the first flexible array member or the first zero-length
7182 array, in that order of preference, among members of class T (but not
7183 its base classes), and set members of FMEM accordingly.
7184 BASE_P is true if T is a base class of another class.
7185 PUN is set to the outermost union in which the flexible array member
7186 (or zero-length array) is defined if one such union exists, otherwise
7188 Similarly, PSTR is set to a data member of the outermost struct of
7189 which the flexible array is a member if one such struct exists,
7190 otherwise to NULL. */
7193 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
7194 tree pun
/* = NULL_TREE */,
7195 tree pstr
/* = NULL_TREE */)
7197 /* Set the "pointer" to the outermost enclosing union if not set
7198 yet and maintain it for the remainder of the recursion. */
7199 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
7202 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
7204 if (fld
== error_mark_node
)
7207 /* Is FLD a typedef for an anonymous struct? */
7209 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7210 handled elsewhere so that errors like the following are detected
7212 typedef struct { int i, a[], j; } S; // bug c++/72753
7213 S s [2]; // bug c++/68489
7215 if (TREE_CODE (fld
) == TYPE_DECL
7216 && DECL_IMPLICIT_TYPEDEF_P (fld
)
7217 && CLASS_TYPE_P (TREE_TYPE (fld
))
7218 && IDENTIFIER_ANON_P (DECL_NAME (fld
)))
7220 /* Check the nested unnamed type referenced via a typedef
7221 independently of FMEM (since it's not a data member of
7222 the enclosing class). */
7223 check_flexarrays (TREE_TYPE (fld
));
7227 /* Skip anything that's GCC-generated or not a (non-static) data
7229 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
7232 /* Type of the member. */
7233 tree fldtype
= TREE_TYPE (fld
);
7234 if (fldtype
== error_mark_node
)
7237 /* Determine the type of the array element or object referenced
7238 by the member so that it can be checked for flexible array
7239 members if it hasn't been yet. */
7240 tree eltype
= fldtype
;
7241 while (TREE_CODE (eltype
) == ARRAY_TYPE
7242 || INDIRECT_TYPE_P (eltype
))
7243 eltype
= TREE_TYPE (eltype
);
7245 if (RECORD_OR_UNION_TYPE_P (eltype
))
7247 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
7249 /* Once the member after the flexible array has been found
7251 fmem
->after
[bool (pun
)] = fld
;
7255 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
7257 /* Descend into the non-static member struct or union and try
7258 to find a flexible array member or zero-length array among
7259 its members. This is only necessary for anonymous types
7260 and types in whose context the current type T has not been
7261 defined (the latter must not be checked again because they
7262 are already in the process of being checked by one of the
7263 recursive calls). */
7265 tree first
= fmem
->first
;
7266 tree array
= fmem
->array
;
7268 /* If this member isn't anonymous and a prior non-flexible array
7269 member has been seen in one of the enclosing structs, clear
7270 the FIRST member since it doesn't contribute to the flexible
7271 array struct's members. */
7272 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7273 fmem
->first
= NULL_TREE
;
7275 find_flexarrays (eltype
, fmem
, false, pun
,
7276 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
7278 if (fmem
->array
!= array
)
7281 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7283 /* Restore the FIRST member reset above if no flexible
7284 array member has been found in this member's struct. */
7285 fmem
->first
= first
;
7288 /* If the member struct contains the first flexible array
7289 member, or if this member is a base class, continue to
7290 the next member and avoid setting the FMEM->NEXT pointer
7297 if (field_nonempty_p (fld
))
7299 /* Remember the first non-static data member. */
7303 /* Remember the first non-static data member after the flexible
7304 array member, if one has been found, or the zero-length array
7305 if it has been found. */
7306 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
7307 fmem
->after
[bool (pun
)] = fld
;
7310 /* Skip non-arrays. */
7311 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
7314 /* Determine the upper bound of the array if it has one. */
7315 if (TYPE_DOMAIN (fldtype
))
7319 /* Make a record of the zero-length array if either one
7320 such field or a flexible array member has been seen to
7321 handle the pathological and unlikely case of multiple
7323 if (!fmem
->after
[bool (pun
)])
7324 fmem
->after
[bool (pun
)] = fld
;
7326 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
7328 /* Remember the first zero-length array unless a flexible array
7329 member has already been seen. */
7331 fmem
->enclosing
= pstr
;
7336 /* Flexible array members have no upper bound. */
7339 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7341 /* Replace the zero-length array if it's been stored and
7342 reset the after pointer. */
7343 fmem
->after
[bool (pun
)] = NULL_TREE
;
7345 fmem
->enclosing
= pstr
;
7347 else if (!fmem
->after
[bool (pun
)])
7348 /* Make a record of another flexible array member. */
7349 fmem
->after
[bool (pun
)] = fld
;
7354 fmem
->enclosing
= pstr
;
7360 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7361 a flexible array member (or the zero-length array extension). */
7364 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
7366 if (fmem
->array
&& fmem
->enclosing
)
7368 auto_diagnostic_group d
;
7369 if (pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
7370 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
7371 ? G_("invalid use of %q#T with a zero-size array "
7373 : G_("invalid use of %q#T with a flexible array member "
7375 DECL_CONTEXT (fmem
->array
),
7376 DECL_CONTEXT (fmem
->enclosing
)))
7377 inform (DECL_SOURCE_LOCATION (fmem
->array
),
7378 "array member %q#D declared here", fmem
->array
);
7382 /* Issue diagnostics for invalid flexible array members or zero-length
7383 arrays that are not the last elements of the containing class or its
7384 base classes or that are its sole members. */
7387 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
7392 if (fmem
->first
&& !fmem
->after
[0])
7394 diagnose_invalid_flexarray (fmem
);
7398 /* Has a diagnostic been issued? */
7401 const char *msg
= 0;
7403 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7406 msg
= G_("zero-size array member %qD not at end of %q#T");
7407 else if (!fmem
->first
)
7408 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
7412 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7414 auto_diagnostic_group d
;
7415 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
7417 inform (location_of (t
), "in the definition of %q#T", t
);
7425 msg
= G_("flexible array member %qD not at end of %q#T");
7426 else if (!fmem
->first
)
7427 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
7431 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7434 auto_diagnostic_group d
;
7435 error_at (loc
, msg
, fmem
->array
, t
);
7437 /* In the unlikely event that the member following the flexible
7438 array member is declared in a different class, or the member
7439 overlaps another member of a common union, point to it.
7440 Otherwise it should be obvious. */
7442 && ((DECL_CONTEXT (fmem
->after
[0])
7443 != DECL_CONTEXT (fmem
->array
))))
7445 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
7446 "next member %q#D declared here",
7448 inform (location_of (t
), "in the definition of %q#T", t
);
7453 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
7454 diagnose_invalid_flexarray (fmem
);
7458 /* Recursively check to make sure that any flexible array or zero-length
7459 array members of class T or its bases are valid (i.e., not the sole
7460 non-static data member of T and, if one exists, that it is the last
7461 non-static data member of T and its base classes. FMEM is expected
7462 to be initially null and is used internally by recursive calls to
7463 the function. Issue the appropriate diagnostics for the array member
7464 that fails the checks. */
7467 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
7468 bool base_p
/* = false */)
7470 /* Initialize the result of a search for flexible array and zero-length
7471 array members. Avoid doing any work if the most interesting FMEM data
7472 have already been populated. */
7473 flexmems_t flexmems
= flexmems_t ();
7476 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
7479 tree fam
= fmem
->array
;
7481 /* Recursively check the primary base class first. */
7482 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7484 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
7485 check_flexarrays (basetype
, fmem
, true);
7488 /* Recursively check the base classes. */
7489 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
7490 for (int i
= 0; i
< nbases
; ++i
)
7492 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
7494 /* The primary base class was already checked above. */
7495 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
7498 /* Virtual base classes are at the end. */
7499 if (BINFO_VIRTUAL_P (base_binfo
))
7502 /* Check the base class. */
7503 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
7506 if (fmem
== &flexmems
)
7508 /* Check virtual base classes only once per derived class.
7509 I.e., this check is not performed recursively for base
7513 vec
<tree
, va_gc
> *vbases
;
7514 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
7515 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
7517 /* Check the virtual base class. */
7518 tree basetype
= TREE_TYPE (base_binfo
);
7520 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
7524 /* Is the type unnamed (and therefore a member of it potentially
7525 an anonymous struct or union)? */
7526 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
7527 if (tree ctx
= maybe_anon_p
? TYPE_CONTEXT (t
) : NULL_TREE
)
7528 maybe_anon_p
= RECORD_OR_UNION_TYPE_P (ctx
);
7530 /* Search the members of the current (possibly derived) class, skipping
7531 unnamed structs and unions since those could be anonymous. */
7532 if (fmem
!= &flexmems
|| !maybe_anon_p
)
7533 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
7535 if (fmem
== &flexmems
&& !maybe_anon_p
)
7537 /* Issue diagnostics for invalid flexible and zero-length array
7538 members found in base classes or among the members of the current
7539 class. Ignore anonymous structs and unions whose members are
7540 considered to be members of the enclosing class and thus will
7541 be diagnosed when checking it. */
7542 diagnose_flexarrays (t
, fmem
);
7546 /* Perform processing required when the definition of T (a class type)
7547 is complete. Diagnose invalid definitions of flexible array members
7548 and zero-size arrays. */
7551 finish_struct_1 (tree t
)
7554 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7555 tree virtuals
= NULL_TREE
;
7557 if (COMPLETE_TYPE_P (t
))
7559 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
7560 error ("redefinition of %q#T", t
);
7565 /* If this type was previously laid out as a forward reference,
7566 make sure we lay it out again. */
7567 TYPE_SIZE (t
) = NULL_TREE
;
7568 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
7570 /* Make assumptions about the class; we'll reset the flags if
7572 CLASSTYPE_EMPTY_P (t
) = 1;
7573 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
7574 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
7575 CLASSTYPE_LITERAL_P (t
) = true;
7577 /* Do end-of-class semantic processing: checking the validity of the
7578 bases and members and add implicitly generated methods. */
7579 check_bases_and_members (t
);
7581 /* Find the key method. */
7582 if (TYPE_CONTAINS_VPTR_P (t
))
7584 /* The Itanium C++ ABI permits the key method to be chosen when
7585 the class is defined -- even though the key method so
7586 selected may later turn out to be an inline function. On
7587 some systems (such as ARM Symbian OS) the key method cannot
7588 be determined until the end of the translation unit. On such
7589 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7590 will cause the class to be added to KEYED_CLASSES. Then, in
7591 finish_file we will determine the key method. */
7592 if (targetm
.cxx
.key_method_may_be_inline ())
7593 determine_key_method (t
);
7595 /* If a polymorphic class has no key method, we may emit the vtable
7596 in every translation unit where the class definition appears. If
7597 we're devirtualizing, we can look into the vtable even if we
7598 aren't emitting it. */
7599 if (!CLASSTYPE_KEY_METHOD (t
))
7600 vec_safe_push (keyed_classes
, t
);
7603 /* Layout the class itself. */
7604 layout_class_type (t
, &virtuals
);
7605 /* COMPLETE_TYPE_P is now true. */
7607 set_class_bindings (t
);
7609 /* With the layout complete, check for flexible array members and
7610 zero-length arrays that might overlap other members in the final
7612 check_flexarrays (t
);
7614 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
7616 /* If necessary, create the primary vtable for this class. */
7617 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
7619 /* We must enter these virtuals into the table. */
7620 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7621 build_primary_vtable (NULL_TREE
, t
);
7622 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
7623 /* Here we know enough to change the type of our virtual
7624 function table, but we will wait until later this function. */
7625 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
7627 /* If we're warning about ABI tags, check the types of the new
7628 virtual functions. */
7630 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
7631 check_abi_tags (t
, TREE_VALUE (v
));
7634 if (TYPE_CONTAINS_VPTR_P (t
))
7639 if (BINFO_VTABLE (TYPE_BINFO (t
)))
7640 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
7641 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7642 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
7644 /* Add entries for virtual functions introduced by this class. */
7645 BINFO_VIRTUALS (TYPE_BINFO (t
))
7646 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
7648 /* Set DECL_VINDEX for all functions declared in this class. */
7649 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
7651 fn
= TREE_CHAIN (fn
),
7652 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
7653 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
7655 tree fndecl
= BV_FN (fn
);
7657 if (DECL_THUNK_P (fndecl
))
7658 /* A thunk. We should never be calling this entry directly
7659 from this vtable -- we'd use the entry for the non
7660 thunk base function. */
7661 DECL_VINDEX (fndecl
) = NULL_TREE
;
7662 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7663 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7667 finish_struct_bits (t
);
7669 set_method_tm_attributes (t
);
7670 if (flag_openmp
|| flag_openmp_simd
)
7671 finish_omp_declare_simd_methods (t
);
7673 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7674 for any static member objects of the type we're working on. */
7675 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7676 if (DECL_DECLARES_FUNCTION_P (x
))
7678 /* Synthesize constexpr defaulted comparisons. */
7679 if (!DECL_ARTIFICIAL (x
)
7680 && DECL_DEFAULTED_IN_CLASS_P (x
)
7681 && special_function_p (x
) == sfk_comparison
)
7682 defaulted_late_check (x
);
7683 DECL_IN_AGGR_P (x
) = false;
7685 else if (VAR_P (x
) && TREE_STATIC (x
)
7686 && TREE_TYPE (x
) != error_mark_node
7687 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7688 SET_DECL_MODE (x
, TYPE_MODE (t
));
7690 /* Complain if one of the field types requires lower visibility. */
7691 constrain_class_visibility (t
);
7693 /* Make the rtl for any new vtables we have created, and unmark
7694 the base types we marked. */
7697 /* Build the VTT for T. */
7701 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7702 && !CLASSTYPE_FINAL (t
))
7703 warning (OPT_Wnon_virtual_dtor
,
7704 "%q#T has virtual functions and accessible"
7705 " non-virtual destructor", t
);
7709 if (warn_overloaded_virtual
)
7712 /* Class layout, assignment of virtual table slots, etc., is now
7713 complete. Give the back end a chance to tweak the visibility of
7714 the class or perform any other required target modifications. */
7715 targetm
.cxx
.adjust_class_at_definition (t
);
7717 maybe_suppress_debug_info (t
);
7719 if (flag_vtable_verify
)
7720 vtv_save_class_info (t
);
7722 dump_class_hierarchy (t
);
7724 /* Finish debugging output for this type. */
7725 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7727 if (TYPE_TRANSPARENT_AGGR (t
))
7729 tree field
= first_field (t
);
7730 if (field
== NULL_TREE
|| error_operand_p (field
))
7732 error ("type transparent %q#T does not have any fields", t
);
7733 TYPE_TRANSPARENT_AGGR (t
) = 0;
7735 else if (DECL_ARTIFICIAL (field
))
7737 if (DECL_FIELD_IS_BASE (field
))
7738 error ("type transparent class %qT has base classes", t
);
7741 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7742 error ("type transparent class %qT has virtual functions", t
);
7744 TYPE_TRANSPARENT_AGGR (t
) = 0;
7746 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7748 error ("type transparent %q#T cannot be made transparent because "
7749 "the type of the first field has a different ABI from the "
7750 "class overall", t
);
7751 TYPE_TRANSPARENT_AGGR (t
) = 0;
7756 /* When T was built up, the member declarations were added in reverse
7757 order. Rearrange them to declaration order. */
7760 unreverse_member_declarations (tree t
)
7766 /* The following lists are all in reverse order. Put them in
7767 declaration order now. */
7768 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7770 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7771 order, so we can't just use nreverse. Due to stat_hack
7772 chicanery in finish_member_declaration. */
7774 for (x
= TYPE_FIELDS (t
);
7775 x
&& TREE_CODE (x
) != TYPE_DECL
;
7778 next
= DECL_CHAIN (x
);
7779 DECL_CHAIN (x
) = prev
;
7785 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7786 TYPE_FIELDS (t
) = prev
;
7790 /* Classes, structs or unions T marked with hotness attributes propagate
7791 the attribute to all methods. */
7794 propagate_class_warmth_attribute (tree t
)
7797 || !(TREE_CODE (t
) == RECORD_TYPE
7798 || TREE_CODE (t
) == UNION_TYPE
))
7801 tree class_has_cold_attr
7802 = lookup_attribute ("cold", TYPE_ATTRIBUTES (t
));
7803 tree class_has_hot_attr
7804 = lookup_attribute ("hot", TYPE_ATTRIBUTES (t
));
7806 if (class_has_cold_attr
|| class_has_hot_attr
)
7807 for (tree f
= TYPE_FIELDS (t
); f
; f
= DECL_CHAIN (f
))
7808 if (DECL_DECLARES_FUNCTION_P (f
))
7809 maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f
), t
);
7813 finish_struct (tree t
, tree attributes
)
7815 location_t saved_loc
= input_location
;
7817 /* Now that we've got all the field declarations, reverse everything
7819 unreverse_member_declarations (t
);
7821 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7822 fixup_attribute_variants (t
);
7824 /* Nadger the current location so that diagnostics point to the start of
7825 the struct, not the end. */
7826 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7828 if (processing_template_decl
)
7832 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7833 if (DECL_DECLARES_FUNCTION_P (x
))
7835 DECL_IN_AGGR_P (x
) = false;
7836 if (DECL_VIRTUAL_P (x
))
7837 CLASSTYPE_NON_AGGREGATE (t
) = true;
7839 else if (TREE_CODE (x
) == FIELD_DECL
)
7841 if (TREE_PROTECTED (x
) || TREE_PRIVATE (x
))
7842 CLASSTYPE_NON_AGGREGATE (t
) = true;
7845 /* Also add a USING_DECL for operator=. We know there'll be (at
7846 least) one, but we don't know the signature(s). We want name
7847 lookup not to fail or recurse into bases. This isn't added
7848 to the template decl list so we drop this at instantiation
7850 tree ass_op
= build_lang_decl (USING_DECL
, assign_op_identifier
,
7852 DECL_CONTEXT (ass_op
) = t
;
7853 USING_DECL_SCOPE (ass_op
) = t
;
7854 DECL_DEPENDENT_P (ass_op
) = true;
7855 DECL_ARTIFICIAL (ass_op
) = true;
7856 DECL_CHAIN (ass_op
) = TYPE_FIELDS (t
);
7857 TYPE_FIELDS (t
) = ass_op
;
7859 TYPE_SIZE (t
) = bitsize_zero_node
;
7860 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7861 /* COMPLETE_TYPE_P is now true. */
7863 set_class_bindings (t
);
7865 /* We need to emit an error message if this type was used as a parameter
7866 and it is an abstract type, even if it is a template. We construct
7867 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7868 account and we call complete_vars with this type, which will check
7869 the PARM_DECLS. Note that while the type is being defined,
7870 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7871 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7872 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7873 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7874 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_PURE_VIRTUAL_P (x
))
7875 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7878 /* Remember current #pragma pack value. */
7879 TYPE_PRECISION (t
) = maximum_field_alignment
;
7881 if (cxx_dialect
< cxx20
)
7883 if (!CLASSTYPE_NON_AGGREGATE (t
)
7884 && type_has_user_provided_or_explicit_constructor (t
))
7885 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7887 else if (TYPE_HAS_USER_CONSTRUCTOR (t
))
7888 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7890 /* Fix up any variants we've already built. */
7891 fixup_type_variants (t
);
7894 finish_struct_1 (t
);
7895 /* COMPLETE_TYPE_P is now true. */
7897 maybe_warn_about_overly_private_class (t
);
7899 if (is_std_init_list (t
))
7901 /* People keep complaining that the compiler crashes on an invalid
7902 definition of initializer_list, so I guess we should explicitly
7903 reject it. What the compiler internals care about is that it's a
7904 template and has a pointer field followed by size_type field. */
7906 if (processing_template_decl
)
7908 tree f
= next_aggregate_field (TYPE_FIELDS (t
));
7909 if (f
&& TYPE_PTR_P (TREE_TYPE (f
)))
7911 f
= next_aggregate_field (DECL_CHAIN (f
));
7912 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7916 /* It also cannot be a union. */
7917 ok
&= NON_UNION_CLASS_TYPE_P (t
);
7919 fatal_error (input_location
, "definition of %qD does not match "
7920 "%<#include <initializer_list>%>", TYPE_NAME (t
));
7923 input_location
= saved_loc
;
7925 TYPE_BEING_DEFINED (t
) = 0;
7927 if (current_class_type
)
7930 error ("trying to finish struct, but kicked out due to previous parse errors");
7933 for (tree decl
= TYPE_FIELDS (t
); decl
; decl
= DECL_CHAIN (decl
))
7934 if (TREE_CODE (decl
) == FUNCTION_DECL
7935 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
7936 if (tree attr
= lookup_attribute ("omp declare variant base",
7937 DECL_ATTRIBUTES (decl
)))
7938 omp_declare_variant_finalize (decl
, attr
);
7940 if (processing_template_decl
&& at_function_scope_p ()
7941 /* Lambdas are defined by the LAMBDA_EXPR. */
7942 && !LAMBDA_TYPE_P (t
))
7943 add_stmt (build_min (TAG_DEFN
, t
));
7948 /* Hash table to avoid endless recursion when handling references. */
7949 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7951 /* Return the dynamic type of INSTANCE, if known.
7952 Used to determine whether the virtual function table is needed
7955 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7956 of our knowledge of its type. *NONNULL should be initialized
7957 before this function is called. */
7960 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7962 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7964 switch (TREE_CODE (instance
))
7967 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
7970 return RECUR (TREE_OPERAND (instance
, 0));
7973 /* This is a call to a constructor, hence it's never zero. */
7974 if (CALL_EXPR_FN (instance
)
7975 && TREE_HAS_CONSTRUCTOR (instance
))
7979 return TREE_TYPE (instance
);
7984 /* This is a call to a constructor, hence it's never zero. */
7985 if (TREE_HAS_CONSTRUCTOR (instance
))
7989 return TREE_TYPE (instance
);
7991 return RECUR (TREE_OPERAND (instance
, 0));
7993 case POINTER_PLUS_EXPR
:
7996 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
7997 return RECUR (TREE_OPERAND (instance
, 0));
7998 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7999 /* Propagate nonnull. */
8000 return RECUR (TREE_OPERAND (instance
, 0));
8005 return RECUR (TREE_OPERAND (instance
, 0));
8008 instance
= TREE_OPERAND (instance
, 0);
8011 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8012 with a real object -- given &p->f, p can still be null. */
8013 tree t
= get_base_address (instance
);
8014 /* ??? Probably should check DECL_WEAK here. */
8015 if (t
&& DECL_P (t
))
8018 return RECUR (instance
);
8021 /* If this component is really a base class reference, then the field
8022 itself isn't definitive. */
8023 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
8024 return RECUR (TREE_OPERAND (instance
, 0));
8025 return RECUR (TREE_OPERAND (instance
, 1));
8029 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
8030 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
8034 return TREE_TYPE (TREE_TYPE (instance
));
8040 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
8044 return TREE_TYPE (instance
);
8046 else if (instance
== current_class_ptr
)
8051 /* if we're in a ctor or dtor, we know our type. If
8052 current_class_ptr is set but we aren't in a function, we're in
8053 an NSDMI (and therefore a constructor). */
8054 if (current_scope () != current_function_decl
8055 || (DECL_LANG_SPECIFIC (current_function_decl
)
8056 && (DECL_CONSTRUCTOR_P (current_function_decl
)
8057 || DECL_DESTRUCTOR_P (current_function_decl
))))
8061 return TREE_TYPE (TREE_TYPE (instance
));
8064 else if (TYPE_REF_P (TREE_TYPE (instance
)))
8066 /* We only need one hash table because it is always left empty. */
8067 if (!fixed_type_or_null_ref_ht
)
8068 fixed_type_or_null_ref_ht
8069 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
8071 /* Reference variables should be references to objects. */
8075 /* Enter the INSTANCE in a table to prevent recursion; a
8076 variable's initializer may refer to the variable
8078 if (VAR_P (instance
)
8079 && DECL_INITIAL (instance
)
8080 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
8081 && !fixed_type_or_null_ref_ht
->find (instance
))
8086 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
8088 type
= RECUR (DECL_INITIAL (instance
));
8089 fixed_type_or_null_ref_ht
->remove_elt (instance
);
8096 case VIEW_CONVERT_EXPR
:
8097 if (location_wrapper_p (instance
))
8098 return RECUR (TREE_OPERAND (instance
, 0));
8100 /* TODO: Recursion may be correct for some non-location-wrapper
8101 uses of VIEW_CONVERT_EXPR. */
8110 /* Return nonzero if the dynamic type of INSTANCE is known, and
8111 equivalent to the static type. We also handle the case where
8112 INSTANCE is really a pointer. Return negative if this is a
8113 ctor/dtor. There the dynamic type is known, but this might not be
8114 the most derived base of the original object, and hence virtual
8115 bases may not be laid out according to this type.
8117 Used to determine whether the virtual function table is needed
8120 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8121 of our knowledge of its type. *NONNULL should be initialized
8122 before this function is called. */
8125 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
8127 tree t
= TREE_TYPE (instance
);
8131 /* processing_template_decl can be false in a template if we're in
8132 instantiate_non_dependent_expr, but we still want to suppress
8134 if (in_template_context
)
8136 /* In a template we only care about the type of the result. */
8142 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
8143 if (INDIRECT_TYPE_P (t
))
8145 if (CLASS_TYPE_P (t
) && CLASSTYPE_FINAL (t
))
8147 if (fixed
== NULL_TREE
)
8149 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
8151 return cdtorp
? -1 : 1;
8156 init_class_processing (void)
8158 current_class_depth
= 0;
8159 current_class_stack_size
= 10;
8161 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
8162 sizeof_biggest_empty_class
= size_zero_node
;
8164 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
8165 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
8166 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
8169 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8172 restore_class_cache (void)
8176 /* We are re-entering the same class we just left, so we don't
8177 have to search the whole inheritance matrix to find all the
8178 decls to bind again. Instead, we install the cached
8179 class_shadowed list and walk through it binding names. */
8180 push_binding_level (previous_class_level
);
8181 class_binding_level
= previous_class_level
;
8182 /* Restore IDENTIFIER_TYPE_VALUE. */
8183 for (type
= class_binding_level
->type_shadowed
;
8185 type
= TREE_CHAIN (type
))
8186 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
8189 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8190 appropriate for TYPE.
8192 So that we may avoid calls to lookup_name, we cache the _TYPE
8193 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8195 For multiple inheritance, we perform a two-pass depth-first search
8196 of the type lattice. */
8199 pushclass (tree type
)
8201 class_stack_node_t csn
;
8203 type
= TYPE_MAIN_VARIANT (type
);
8205 /* Make sure there is enough room for the new entry on the stack. */
8206 if (current_class_depth
+ 1 >= current_class_stack_size
)
8208 current_class_stack_size
*= 2;
8210 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
8211 current_class_stack_size
);
8214 /* Insert a new entry on the class stack. */
8215 csn
= current_class_stack
+ current_class_depth
;
8216 csn
->name
= current_class_name
;
8217 csn
->type
= current_class_type
;
8218 csn
->access
= current_access_specifier
;
8219 csn
->names_used
= 0;
8221 current_class_depth
++;
8223 /* Now set up the new type. */
8224 current_class_name
= TYPE_NAME (type
);
8225 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
8226 current_class_name
= DECL_NAME (current_class_name
);
8227 current_class_type
= type
;
8229 /* By default, things in classes are private, while things in
8230 structures or unions are public. */
8231 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
8232 ? access_private_node
8233 : access_public_node
);
8235 if (previous_class_level
8236 && type
!= previous_class_level
->this_entity
8237 && current_class_depth
== 1)
8239 /* Forcibly remove any old class remnants. */
8240 invalidate_class_lookup_cache ();
8243 if (!previous_class_level
8244 || type
!= previous_class_level
->this_entity
8245 || current_class_depth
> 1)
8248 restore_class_cache ();
8251 /* Get out of the current class scope. If we were in a class scope
8252 previously, that is the one popped to. */
8259 current_class_depth
--;
8260 current_class_name
= current_class_stack
[current_class_depth
].name
;
8261 current_class_type
= current_class_stack
[current_class_depth
].type
;
8262 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
8263 if (current_class_stack
[current_class_depth
].names_used
)
8264 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
8267 /* Mark the top of the class stack as hidden. */
8270 push_class_stack (void)
8272 if (current_class_depth
)
8273 ++current_class_stack
[current_class_depth
- 1].hidden
;
8276 /* Mark the top of the class stack as un-hidden. */
8279 pop_class_stack (void)
8281 if (current_class_depth
)
8282 --current_class_stack
[current_class_depth
- 1].hidden
;
8285 /* If the class type currently being defined is either T or
8286 a nested type of T, returns the type from the current_class_stack,
8287 which might be equivalent to but not equal to T in case of
8288 constrained partial specializations. */
8291 currently_open_class (tree t
)
8295 if (!CLASS_TYPE_P (t
))
8298 t
= TYPE_MAIN_VARIANT (t
);
8300 /* We start looking from 1 because entry 0 is from global scope,
8302 for (i
= current_class_depth
; i
> 0; --i
)
8305 if (i
== current_class_depth
)
8306 c
= current_class_type
;
8309 if (current_class_stack
[i
].hidden
)
8311 c
= current_class_stack
[i
].type
;
8315 if (same_type_p (c
, t
))
8321 /* If either current_class_type or one of its enclosing classes are derived
8322 from T, return the appropriate type. Used to determine how we found
8323 something via unqualified lookup. */
8326 currently_open_derived_class (tree t
)
8330 /* The bases of a dependent type are unknown. */
8331 if (dependent_type_p (t
))
8334 if (!current_class_type
)
8337 if (DERIVED_FROM_P (t
, current_class_type
))
8338 return current_class_type
;
8340 for (i
= current_class_depth
- 1; i
> 0; --i
)
8342 if (current_class_stack
[i
].hidden
)
8344 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
8345 return current_class_stack
[i
].type
;
8351 /* Return the outermost enclosing class type that is still open, or
8355 outermost_open_class (void)
8357 if (!current_class_type
)
8360 if (TYPE_BEING_DEFINED (current_class_type
))
8361 r
= current_class_type
;
8362 for (int i
= current_class_depth
- 1; i
> 0; --i
)
8364 if (current_class_stack
[i
].hidden
)
8366 tree t
= current_class_stack
[i
].type
;
8367 if (!TYPE_BEING_DEFINED (t
))
8374 /* Returns the innermost class type which is not a lambda closure type. */
8377 current_nonlambda_class_type (void)
8379 tree type
= current_class_type
;
8380 while (type
&& LAMBDA_TYPE_P (type
))
8381 type
= decl_type_context (TYPE_NAME (type
));
8385 /* When entering a class scope, all enclosing class scopes' names with
8386 static meaning (static variables, static functions, types and
8387 enumerators) have to be visible. This recursive function calls
8388 pushclass for all enclosing class contexts until global or a local
8389 scope is reached. TYPE is the enclosed class. */
8392 push_nested_class (tree type
)
8394 /* A namespace might be passed in error cases, like A::B:C. */
8395 if (type
== NULL_TREE
8396 || !CLASS_TYPE_P (type
))
8399 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
8404 /* Undoes a push_nested_class call. */
8407 pop_nested_class (void)
8409 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
8412 if (context
&& CLASS_TYPE_P (context
))
8413 pop_nested_class ();
8416 /* Returns the number of extern "LANG" blocks we are nested within. */
8419 current_lang_depth (void)
8421 return vec_safe_length (current_lang_base
);
8424 /* Set global variables CURRENT_LANG_NAME to appropriate value
8425 so that behavior of name-mangling machinery is correct. */
8428 push_lang_context (tree name
)
8430 vec_safe_push (current_lang_base
, current_lang_name
);
8432 if (name
== lang_name_cplusplus
)
8433 current_lang_name
= name
;
8434 else if (name
== lang_name_c
)
8435 current_lang_name
= name
;
8437 error ("language string %<\"%E\"%> not recognized", name
);
8440 /* Get out of the current language scope. */
8443 pop_lang_context (void)
8445 current_lang_name
= current_lang_base
->pop ();
8448 /* Type instantiation routines. */
8450 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8451 matches the TARGET_TYPE. If there is no satisfactory match, return
8452 error_mark_node, and issue an error & warning messages under
8453 control of FLAGS. Permit pointers to member function if FLAGS
8454 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8455 a template-id, and EXPLICIT_TARGS are the explicitly provided
8458 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8459 is the base path used to reference those member functions. If
8460 the address is resolved to a member function, access checks will be
8461 performed and errors issued if appropriate. */
8464 resolve_address_of_overloaded_function (tree target_type
,
8466 tsubst_flags_t complain
,
8468 tree explicit_targs
,
8471 /* Here's what the standard says:
8475 If the name is a function template, template argument deduction
8476 is done, and if the argument deduction succeeds, the deduced
8477 arguments are used to generate a single template function, which
8478 is added to the set of overloaded functions considered.
8480 Non-member functions and static member functions match targets of
8481 type "pointer-to-function" or "reference-to-function." Nonstatic
8482 member functions match targets of type "pointer-to-member
8483 function;" the function type of the pointer to member is used to
8484 select the member function from the set of overloaded member
8485 functions. If a non-static member function is selected, the
8486 reference to the overloaded function name is required to have the
8487 form of a pointer to member as described in 5.3.1.
8489 If more than one function is selected, any template functions in
8490 the set are eliminated if the set also contains a non-template
8491 function, and any given template function is eliminated if the
8492 set contains a second template function that is more specialized
8493 than the first according to the partial ordering rules 14.5.5.2.
8494 After such eliminations, if any, there shall remain exactly one
8495 selected function. */
8498 /* We store the matches in a TREE_LIST rooted here. The functions
8499 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8500 interoperability with most_specialized_instantiation. */
8501 tree matches
= NULL_TREE
;
8503 tree target_fn_type
;
8505 /* By the time we get here, we should be seeing only real
8506 pointer-to-member types, not the internal POINTER_TYPE to
8507 METHOD_TYPE representation. */
8508 gcc_assert (!TYPE_PTR_P (target_type
)
8509 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
8511 gcc_assert (is_overloaded_fn (overload
));
8513 /* Check that the TARGET_TYPE is reasonable. */
8514 if (TYPE_PTRFN_P (target_type
)
8515 || TYPE_REFFN_P (target_type
))
8517 else if (TYPE_PTRMEMFUNC_P (target_type
))
8518 /* This is OK, too. */
8520 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
8521 /* This is OK, too. This comes from a conversion to reference
8523 target_type
= build_reference_type (target_type
);
8526 if (complain
& tf_error
)
8527 error ("cannot resolve overloaded function %qD based on"
8528 " conversion to type %qT",
8529 OVL_NAME (overload
), target_type
);
8530 return error_mark_node
;
8533 /* Non-member functions and static member functions match targets of type
8534 "pointer-to-function" or "reference-to-function." Nonstatic member
8535 functions match targets of type "pointer-to-member-function;" the
8536 function type of the pointer to member is used to select the member
8537 function from the set of overloaded member functions.
8539 So figure out the FUNCTION_TYPE that we want to match against. */
8540 target_fn_type
= static_fn_type (target_type
);
8542 /* If we can find a non-template function that matches, we can just
8543 use it. There's no point in generating template instantiations
8544 if we're just going to throw them out anyhow. But, of course, we
8545 can only do this when we don't *need* a template function. */
8547 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8551 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8552 /* We're not looking for templates just yet. */
8555 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
) != is_ptrmem
)
8556 /* We're looking for a non-static member, and this isn't
8557 one, or vice versa. */
8560 /* Constraints must be satisfied. This is done before
8561 return type deduction since that instantiates the
8563 if (!constraints_satisfied_p (fn
))
8566 if (undeduced_auto_decl (fn
))
8568 /* Force instantiation to do return type deduction. */
8569 maybe_instantiate_decl (fn
);
8570 require_deduced_type (fn
);
8573 /* In C++17 we need the noexcept-qualifier to compare types. */
8574 if (flag_noexcept_type
8575 && !maybe_instantiate_noexcept (fn
, complain
))
8578 /* See if there's a match. */
8579 tree fntype
= static_fn_type (fn
);
8580 if (same_type_p (target_fn_type
, fntype
)
8581 || fnptr_conv_p (target_fn_type
, fntype
))
8582 matches
= tree_cons (fn
, NULL_TREE
, matches
);
8585 /* Now, if we've already got a match (or matches), there's no need
8586 to proceed to the template functions. But, if we don't have a
8587 match we need to look at them, too. */
8590 tree target_arg_types
;
8591 tree target_ret_type
;
8593 unsigned int nargs
, ia
;
8596 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
8597 target_ret_type
= TREE_TYPE (target_fn_type
);
8599 nargs
= list_length (target_arg_types
);
8600 args
= XALLOCAVEC (tree
, nargs
);
8601 for (arg
= target_arg_types
, ia
= 0;
8603 arg
= TREE_CHAIN (arg
), ++ia
)
8604 args
[ia
] = TREE_VALUE (arg
);
8607 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8613 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8614 /* We're only looking for templates. */
8617 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8619 /* We're not looking for a non-static member, and this is
8620 one, or vice versa. */
8623 tree ret
= target_ret_type
;
8625 /* If the template has a deduced return type, don't expose it to
8626 template argument deduction. */
8627 if (undeduced_auto_decl (fn
))
8630 /* Try to do argument deduction. */
8631 targs
= make_tree_vec (DECL_NTPARMS (fn
));
8632 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
8634 DEDUCE_EXACT
, LOOKUP_NORMAL
,
8635 NULL
, false, false);
8636 if (instantiation
== error_mark_node
)
8637 /* Instantiation failed. */
8640 /* Constraints must be satisfied. This is done before
8641 return type deduction since that instantiates the
8643 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
8646 /* And now force instantiation to do return type deduction. */
8647 if (undeduced_auto_decl (instantiation
))
8650 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
8653 require_deduced_type (instantiation
);
8656 /* In C++17 we need the noexcept-qualifier to compare types. */
8657 if (flag_noexcept_type
)
8658 maybe_instantiate_noexcept (instantiation
, complain
);
8660 /* See if there's a match. */
8661 tree fntype
= static_fn_type (instantiation
);
8662 if (same_type_p (target_fn_type
, fntype
)
8663 || fnptr_conv_p (target_fn_type
, fntype
))
8664 matches
= tree_cons (instantiation
, fn
, matches
);
8667 /* Now, remove all but the most specialized of the matches. */
8670 tree match
= most_specialized_instantiation (matches
);
8672 if (match
!= error_mark_node
)
8673 matches
= tree_cons (TREE_PURPOSE (match
),
8679 /* Now we should have exactly one function in MATCHES. */
8680 if (matches
== NULL_TREE
)
8682 /* There were *no* matches. */
8683 if (complain
& tf_error
)
8685 error ("no matches converting function %qD to type %q#T",
8686 OVL_NAME (overload
), target_type
);
8688 print_candidates (overload
);
8690 return error_mark_node
;
8692 else if (TREE_CHAIN (matches
))
8694 /* There were too many matches. First check if they're all
8695 the same function. */
8696 tree match
= NULL_TREE
;
8698 fn
= TREE_PURPOSE (matches
);
8700 /* For multi-versioned functions, more than one match is just fine and
8701 decls_match will return false as they are different. */
8702 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
8703 if (!decls_match (fn
, TREE_PURPOSE (match
))
8704 && !targetm
.target_option
.function_versions
8705 (fn
, TREE_PURPOSE (match
)))
8710 if (complain
& tf_error
)
8712 error ("converting overloaded function %qD to type %q#T is ambiguous",
8713 OVL_NAME (overload
), target_type
);
8715 /* Since print_candidates expects the functions in the
8716 TREE_VALUE slot, we flip them here. */
8717 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8718 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8720 print_candidates (matches
);
8723 return error_mark_node
;
8727 /* Good, exactly one match. Now, convert it to the correct type. */
8728 fn
= TREE_PURPOSE (matches
);
8730 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8731 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8733 static int explained
;
8735 if (!(complain
& tf_error
))
8736 return error_mark_node
;
8738 auto_diagnostic_group d
;
8739 if (permerror (input_location
, "assuming pointer to member %qD", fn
)
8742 inform (input_location
, "(a pointer to member can only be "
8743 "formed with %<&%E%>)", fn
);
8748 /* If a pointer to a function that is multi-versioned is requested, the
8749 pointer to the dispatcher function is returned instead. This works
8750 well because indirectly calling the function will dispatch the right
8751 function version at run-time. */
8752 if (DECL_FUNCTION_VERSIONED (fn
))
8754 fn
= get_function_version_dispatcher (fn
);
8756 return error_mark_node
;
8757 /* Mark all the versions corresponding to the dispatcher as used. */
8758 if (!(complain
& tf_conv
))
8759 mark_versions_used (fn
);
8762 /* If we're doing overload resolution purely for the purpose of
8763 determining conversion sequences, we should not consider the
8764 function used. If this conversion sequence is selected, the
8765 function will be marked as used at this point. */
8766 if (!(complain
& tf_conv
))
8768 /* Make =delete work with SFINAE. */
8769 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8770 return error_mark_node
;
8771 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8772 return error_mark_node
;
8775 /* We could not check access to member functions when this
8776 expression was originally created since we did not know at that
8777 time to which function the expression referred. */
8778 if (DECL_FUNCTION_MEMBER_P (fn
))
8780 gcc_assert (access_path
);
8781 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8784 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8785 return cp_build_addr_expr (fn
, complain
);
8788 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8789 will mark the function as addressed, but here we must do it
8791 cxx_mark_addressable (fn
);
8797 /* This function will instantiate the type of the expression given in
8798 RHS to match the type of LHSTYPE. If errors exist, then return
8799 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8800 we complain on errors. If we are not complaining, never modify rhs,
8801 as overload resolution wants to try many possible instantiations, in
8802 the hope that at least one will work.
8804 For non-recursive calls, LHSTYPE should be a function, pointer to
8805 function, or a pointer to member function. */
8808 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8810 tsubst_flags_t complain_in
= complain
;
8811 tree access_path
= NULL_TREE
;
8813 complain
&= ~tf_ptrmem_ok
;
8815 STRIP_ANY_LOCATION_WRAPPER (rhs
);
8817 if (lhstype
== unknown_type_node
)
8819 if (complain
& tf_error
)
8820 error ("not enough type information");
8821 return error_mark_node
;
8824 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8826 tree fntype
= non_reference (lhstype
);
8827 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8829 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8831 if (flag_ms_extensions
8832 && TYPE_PTRMEMFUNC_P (fntype
)
8833 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8834 /* Microsoft allows `A::f' to be resolved to a
8835 pointer-to-member. */
8839 if (complain
& tf_error
)
8840 error ("cannot convert %qE from type %qT to type %qT",
8841 rhs
, TREE_TYPE (rhs
), fntype
);
8842 return error_mark_node
;
8846 /* If we instantiate a template, and it is a A ?: C expression
8847 with omitted B, look through the SAVE_EXPR. */
8848 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8849 rhs
= TREE_OPERAND (rhs
, 0);
8851 if (BASELINK_P (rhs
))
8853 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8854 rhs
= BASELINK_FUNCTIONS (rhs
);
8857 /* There are only a few kinds of expressions that may have a type
8858 dependent on overload resolution. */
8859 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
8860 || TREE_CODE (rhs
) == COMPONENT_REF
8861 || is_overloaded_fn (rhs
)
8862 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8864 /* This should really only be used when attempting to distinguish
8865 what sort of a pointer to function we have. For now, any
8866 arithmetic operation which is not supported on pointers
8867 is rejected as an error. */
8869 switch (TREE_CODE (rhs
))
8873 tree member
= TREE_OPERAND (rhs
, 1);
8875 member
= instantiate_type (lhstype
, member
, complain
);
8876 if (member
!= error_mark_node
8877 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8878 /* Do not lose object's side effects. */
8879 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8880 TREE_OPERAND (rhs
, 0), member
);
8885 rhs
= TREE_OPERAND (rhs
, 1);
8886 if (BASELINK_P (rhs
))
8887 return instantiate_type (lhstype
, rhs
, complain_in
);
8889 /* This can happen if we are forming a pointer-to-member for a
8891 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8895 case TEMPLATE_ID_EXPR
:
8897 tree fns
= TREE_OPERAND (rhs
, 0);
8898 tree args
= TREE_OPERAND (rhs
, 1);
8901 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8902 /*template_only=*/true,
8909 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8910 /*template_only=*/false,
8911 /*explicit_targs=*/NULL_TREE
,
8916 if (PTRMEM_OK_P (rhs
))
8917 complain
|= tf_ptrmem_ok
;
8919 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8923 return error_mark_node
;
8928 return error_mark_node
;
8931 /* Return the name of the virtual function pointer field
8932 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8933 this may have to look back through base types to find the
8934 ultimate field name. (For single inheritance, these could
8935 all be the same name. Who knows for multiple inheritance). */
8938 get_vfield_name (tree type
)
8940 tree binfo
, base_binfo
;
8942 for (binfo
= TYPE_BINFO (type
);
8943 BINFO_N_BASE_BINFOS (binfo
);
8946 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8948 if (BINFO_VIRTUAL_P (base_binfo
)
8949 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8953 type
= BINFO_TYPE (binfo
);
8954 tree ctor_name
= constructor_name (type
);
8955 char *buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8956 + IDENTIFIER_LENGTH (ctor_name
) + 2);
8957 sprintf (buf
, VFIELD_NAME_FORMAT
, IDENTIFIER_POINTER (ctor_name
));
8958 return get_identifier (buf
);
8961 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8962 according to [class]:
8963 The class-name is also inserted
8964 into the scope of the class itself. For purposes of access checking,
8965 the inserted class name is treated as if it were a public member name. */
8968 build_self_reference (void)
8970 tree name
= DECL_NAME (TYPE_NAME (current_class_type
));
8971 tree decl
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8973 DECL_NONLOCAL (decl
) = 1;
8974 DECL_CONTEXT (decl
) = current_class_type
;
8975 DECL_ARTIFICIAL (decl
) = 1;
8976 SET_DECL_SELF_REFERENCE_P (decl
);
8977 set_underlying_type (decl
);
8978 set_instantiating_module (decl
);
8980 if (processing_template_decl
)
8981 decl
= push_template_decl (decl
);
8983 tree saved_cas
= current_access_specifier
;
8984 current_access_specifier
= access_public_node
;
8985 finish_member_declaration (decl
);
8986 current_access_specifier
= saved_cas
;
8989 /* Returns 1 if TYPE contains only padding bytes. */
8992 is_empty_class (tree type
)
8994 if (type
== error_mark_node
)
8997 if (! CLASS_TYPE_P (type
))
9000 return CLASSTYPE_EMPTY_P (type
);
9003 /* Returns true if TYPE contains no actual data, just various
9004 possible combinations of empty classes. If IGNORE_VPTR is true,
9005 a vptr doesn't prevent the class from being considered empty. Typically
9006 we want to ignore the vptr on assignment, and not on initialization. */
9009 is_really_empty_class (tree type
, bool ignore_vptr
)
9011 if (CLASS_TYPE_P (type
))
9018 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9019 out, but we'd like to be able to check this before then. */
9020 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
9023 if (!ignore_vptr
&& TYPE_CONTAINS_VPTR_P (type
))
9026 for (binfo
= TYPE_BINFO (type
), i
= 0;
9027 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9028 if (!is_really_empty_class (BINFO_TYPE (base_binfo
), ignore_vptr
))
9030 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
9031 if (TREE_CODE (field
) == FIELD_DECL
9032 && !DECL_ARTIFICIAL (field
)
9033 /* An unnamed bit-field is not a data member. */
9034 && !DECL_UNNAMED_BIT_FIELD (field
)
9035 && !is_really_empty_class (TREE_TYPE (field
), ignore_vptr
))
9039 else if (TREE_CODE (type
) == ARRAY_TYPE
)
9040 return (integer_zerop (array_type_nelts_top (type
))
9041 || is_really_empty_class (TREE_TYPE (type
), ignore_vptr
));
9045 /* Note that NAME was looked up while the current class was being
9046 defined and that the result of that lookup was DECL. */
9049 maybe_note_name_used_in_class (tree name
, tree decl
)
9051 /* If we're not defining a class, there's nothing to do. */
9052 if (!(innermost_scope_kind() == sk_class
9053 && TYPE_BEING_DEFINED (current_class_type
)
9054 && !LAMBDA_TYPE_P (current_class_type
)))
9057 const cp_binding_level
*blev
= nullptr;
9058 if (const cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
9059 blev
= binding
->scope
;
9060 const cp_binding_level
*lev
= current_binding_level
;
9062 /* Record the binding in the names_used tables for classes inside blev. */
9063 for (int i
= current_class_depth
; i
> 0; --i
)
9065 tree type
= (i
== current_class_depth
9066 ? current_class_type
9067 : current_class_stack
[i
].type
);
9069 for (; lev
; lev
= lev
->level_chain
)
9072 /* We found the declaration. */
9074 if (lev
->kind
== sk_class
&& lev
->this_entity
== type
)
9075 /* This class is inside the declaration scope. */
9079 auto &names_used
= current_class_stack
[i
-1].names_used
;
9081 names_used
= splay_tree_new (splay_tree_compare_pointers
, 0, 0);
9083 tree use
= build1_loc (input_location
, VIEW_CONVERT_EXPR
,
9084 TREE_TYPE (decl
), decl
);
9085 EXPR_LOCATION_WRAPPER_P (use
) = 1;
9086 splay_tree_insert (names_used
,
9087 (splay_tree_key
) name
,
9088 (splay_tree_value
) use
);
9092 /* Note that NAME was declared (as DECL) in the current class. Check
9093 to see that the declaration is valid under [class.member.lookup]:
9095 If [the result of a search in T for N at point P] differs from the result of
9096 a search in T for N from immediately after the class-specifier of T, the
9097 program is ill-formed, no diagnostic required. */
9100 note_name_declared_in_class (tree name
, tree decl
)
9102 splay_tree names_used
;
9105 /* Look to see if we ever used this name. */
9107 = current_class_stack
[current_class_depth
- 1].names_used
;
9110 /* The C language allows members to be declared with a type of the same
9111 name, and the C++ standard says this diagnostic is not required. So
9112 allow it in extern "C" blocks unless pedantic is specified.
9113 Allow it in all cases if -ms-extensions is specified. */
9114 if ((!pedantic
&& current_lang_name
== lang_name_c
)
9115 || flag_ms_extensions
)
9117 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
9120 tree use
= (tree
) n
->value
;
9121 location_t loc
= EXPR_LOCATION (use
);
9122 tree olddecl
= OVL_FIRST (TREE_OPERAND (use
, 0));
9123 /* [basic.scope.class]
9125 A name N used in a class S shall refer to the same declaration
9126 in its context and when re-evaluated in the completed scope of
9128 auto ov
= make_temp_override (global_dc
->m_pedantic_errors
);
9129 if (TREE_CODE (decl
) == TYPE_DECL
9130 && TREE_CODE (olddecl
) == TYPE_DECL
9131 && same_type_p (TREE_TYPE (decl
), TREE_TYPE (olddecl
)))
9132 /* Different declaration, but same meaning; just warn. */;
9133 else if (flag_permissive
)
9134 /* Let -fpermissive make it a warning like past versions. */;
9136 /* Make it an error. */
9137 global_dc
->m_pedantic_errors
= 1;
9138 if (pedwarn (location_of (decl
), OPT_Wchanges_meaning
,
9139 "declaration of %q#D changes meaning of %qD",
9140 decl
, OVL_NAME (decl
)))
9142 inform (loc
, "used here to mean %q#D", olddecl
);
9143 inform (location_of (olddecl
), "declared here" );
9148 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9149 Secondary vtables are merged with primary vtables; this function
9150 will return the VAR_DECL for the primary vtable. */
9153 get_vtbl_decl_for_binfo (tree binfo
)
9157 decl
= BINFO_VTABLE (binfo
);
9158 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
9160 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
9161 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
9164 gcc_assert (VAR_P (decl
));
9169 /* Returns the binfo for the primary base of BINFO. If the resulting
9170 BINFO is a virtual base, and it is inherited elsewhere in the
9171 hierarchy, then the returned binfo might not be the primary base of
9172 BINFO in the complete object. Check BINFO_PRIMARY_P or
9173 BINFO_LOST_PRIMARY_P to be sure. */
9176 get_primary_binfo (tree binfo
)
9180 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
9184 return copied_binfo (primary_base
, binfo
);
9187 /* As above, but iterate until we reach the binfo that actually provides the
9191 most_primary_binfo (tree binfo
)
9194 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
9195 && !BINFO_LOST_PRIMARY_P (b
))
9197 tree primary_base
= get_primary_binfo (b
);
9198 gcc_assert (BINFO_PRIMARY_P (primary_base
)
9199 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
9205 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9206 type. Note that the virtual inheritance might be above or below BINFO in
9210 vptr_via_virtual_p (tree binfo
)
9213 binfo
= TYPE_BINFO (binfo
);
9214 tree primary
= most_primary_binfo (binfo
);
9215 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9216 a morally virtual base. */
9217 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
9218 return virt
!= NULL_TREE
;
9221 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9224 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
9227 fprintf (stream
, "%*s", indent
, "");
9231 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9232 INDENT should be zero when called from the top level; it is
9233 incremented recursively. IGO indicates the next expected BINFO in
9234 inheritance graph ordering. */
9237 dump_class_hierarchy_r (FILE *stream
,
9247 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
9248 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
9249 (HOST_WIDE_INT
) (uintptr_t) binfo
);
9252 fprintf (stream
, "alternative-path\n");
9255 igo
= TREE_CHAIN (binfo
);
9257 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
9258 tree_to_shwi (BINFO_OFFSET (binfo
)));
9259 if (is_empty_class (BINFO_TYPE (binfo
)))
9260 fprintf (stream
, " empty");
9261 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
9262 fprintf (stream
, " nearly-empty");
9263 if (BINFO_VIRTUAL_P (binfo
))
9264 fprintf (stream
, " virtual");
9265 fprintf (stream
, "\n");
9267 if (BINFO_PRIMARY_P (binfo
))
9269 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9270 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
9271 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
9272 TFF_PLAIN_IDENTIFIER
),
9273 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
9275 if (BINFO_LOST_PRIMARY_P (binfo
))
9277 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9278 fprintf (stream
, " lost-primary");
9281 fprintf (stream
, "\n");
9283 if (!(flags
& TDF_SLIM
))
9287 if (BINFO_SUBVTT_INDEX (binfo
))
9289 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9290 fprintf (stream
, " subvttidx=%s",
9291 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
9292 TFF_PLAIN_IDENTIFIER
));
9294 if (BINFO_VPTR_INDEX (binfo
))
9296 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9297 fprintf (stream
, " vptridx=%s",
9298 expr_as_string (BINFO_VPTR_INDEX (binfo
),
9299 TFF_PLAIN_IDENTIFIER
));
9301 if (BINFO_VPTR_FIELD (binfo
))
9303 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9304 fprintf (stream
, " vbaseoffset=%s",
9305 expr_as_string (BINFO_VPTR_FIELD (binfo
),
9306 TFF_PLAIN_IDENTIFIER
));
9308 if (BINFO_VTABLE (binfo
))
9310 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9311 fprintf (stream
, " vptr=%s",
9312 expr_as_string (BINFO_VTABLE (binfo
),
9313 TFF_PLAIN_IDENTIFIER
));
9317 fprintf (stream
, "\n");
9320 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9321 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
9326 /* Dump the BINFO hierarchy for T. */
9329 dump_class_hierarchy_1 (FILE *stream
, dump_flags_t flags
, tree t
)
9331 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9332 fprintf (stream
, " size=%lu align=%lu\n",
9333 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
9334 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
9335 if (tree as_base
= CLASSTYPE_AS_BASE (t
))
9336 fprintf (stream
, " base size=%lu base align=%lu\n",
9337 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base
))
9339 (unsigned long)(TYPE_ALIGN (as_base
) / BITS_PER_UNIT
));
9340 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
9341 fprintf (stream
, "\n");
9344 /* Debug interface to hierarchy dumping. */
9347 debug_class (tree t
)
9349 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
9353 dump_class_hierarchy (tree t
)
9356 if (FILE *stream
= dump_begin (class_dump_id
, &flags
))
9358 dump_class_hierarchy_1 (stream
, flags
, t
);
9359 dump_end (class_dump_id
, stream
);
9364 dump_array (FILE * stream
, tree decl
)
9367 unsigned HOST_WIDE_INT ix
;
9369 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
9371 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
9373 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
9374 fprintf (stream
, " %s entries",
9375 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
9376 TFF_PLAIN_IDENTIFIER
));
9377 fprintf (stream
, "\n");
9379 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
9381 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
9382 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
9386 dump_vtable (tree t
, tree binfo
, tree vtable
)
9389 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9394 if (!(flags
& TDF_SLIM
))
9396 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
9398 fprintf (stream
, "%s for %s",
9399 ctor_vtbl_p
? "Construction vtable" : "Vtable",
9400 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
9403 if (!BINFO_VIRTUAL_P (binfo
))
9404 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
9405 (HOST_WIDE_INT
) (uintptr_t) binfo
);
9406 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9408 fprintf (stream
, "\n");
9409 dump_array (stream
, vtable
);
9410 fprintf (stream
, "\n");
9413 dump_end (class_dump_id
, stream
);
9417 dump_vtt (tree t
, tree vtt
)
9420 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9425 if (!(flags
& TDF_SLIM
))
9427 fprintf (stream
, "VTT for %s\n",
9428 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9429 dump_array (stream
, vtt
);
9430 fprintf (stream
, "\n");
9433 dump_end (class_dump_id
, stream
);
9436 /* Dump a function or thunk and its thunkees. */
9439 dump_thunk (FILE *stream
, int indent
, tree thunk
)
9441 static const char spaces
[] = " ";
9442 tree name
= DECL_NAME (thunk
);
9445 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
9447 !DECL_THUNK_P (thunk
) ? "function"
9448 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
9449 name
? IDENTIFIER_POINTER (name
) : "<unset>");
9450 if (DECL_THUNK_P (thunk
))
9452 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
9453 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
9455 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
9456 if (!virtual_adjust
)
9458 else if (DECL_THIS_THUNK_P (thunk
))
9459 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
9460 tree_to_shwi (virtual_adjust
));
9462 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
9463 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
9464 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
9465 if (THUNK_ALIAS (thunk
))
9466 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
9468 fprintf (stream
, "\n");
9469 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
9470 dump_thunk (stream
, indent
+ 2, thunks
);
9473 /* Dump the thunks for FN. */
9476 debug_thunks (tree fn
)
9478 dump_thunk (stderr
, 0, fn
);
9481 /* Virtual function table initialization. */
9483 /* Create all the necessary vtables for T and its base classes. */
9486 finish_vtbls (tree t
)
9489 vec
<constructor_elt
, va_gc
> *v
= NULL
;
9490 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
9492 /* We lay out the primary and secondary vtables in one contiguous
9493 vtable. The primary vtable is first, followed by the non-virtual
9494 secondary vtables in inheritance graph order. */
9495 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
9498 /* Then come the virtual bases, also in inheritance graph order. */
9499 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
9501 if (!BINFO_VIRTUAL_P (vbase
))
9503 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
9506 if (BINFO_VTABLE (TYPE_BINFO (t
)))
9507 initialize_vtable (TYPE_BINFO (t
), v
);
9510 /* Initialize the vtable for BINFO with the INITS. */
9513 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
9517 layout_vtable_decl (binfo
, vec_safe_length (inits
));
9518 decl
= get_vtbl_decl_for_binfo (binfo
);
9519 initialize_artificial_var (decl
, inits
);
9520 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
9523 /* Build the VTT (virtual table table) for T.
9524 A class requires a VTT if it has virtual bases.
9527 1 - primary virtual pointer for complete object T
9528 2 - secondary VTTs for each direct non-virtual base of T which requires a
9530 3 - secondary virtual pointers for each direct or indirect base of T which
9531 has virtual bases or is reachable via a virtual path from T.
9532 4 - secondary VTTs for each direct or indirect virtual base of T.
9534 Secondary VTTs look like complete object VTTs without part 4. */
9542 vec
<constructor_elt
, va_gc
> *inits
;
9544 /* Build up the initializers for the VTT. */
9546 index
= size_zero_node
;
9547 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
9549 /* If we didn't need a VTT, we're done. */
9553 /* Figure out the type of the VTT. */
9554 type
= build_array_of_n_type (const_ptr_type_node
,
9557 /* Now, build the VTT object itself. */
9558 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
9559 initialize_artificial_var (vtt
, inits
);
9560 /* Add the VTT to the vtables list. */
9561 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
9562 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
9567 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9568 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9569 and CHAIN the vtable pointer for this binfo after construction is
9570 complete. VALUE can also be another BINFO, in which case we recurse. */
9573 binfo_ctor_vtable (tree binfo
)
9579 vt
= BINFO_VTABLE (binfo
);
9580 if (TREE_CODE (vt
) == TREE_LIST
)
9581 vt
= TREE_VALUE (vt
);
9582 if (TREE_CODE (vt
) == TREE_BINFO
)
9591 /* Data for secondary VTT initialization. */
9592 struct secondary_vptr_vtt_init_data
9594 /* Is this the primary VTT? */
9597 /* Current index into the VTT. */
9600 /* Vector of initializers built up. */
9601 vec
<constructor_elt
, va_gc
> *inits
;
9603 /* The type being constructed by this secondary VTT. */
9604 tree type_being_constructed
;
9607 /* Recursively build the VTT-initializer for BINFO (which is in the
9608 hierarchy dominated by T). INITS points to the end of the initializer
9609 list to date. INDEX is the VTT index where the next element will be
9610 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9611 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9612 for virtual bases of T. When it is not so, we build the constructor
9613 vtables for the BINFO-in-T variant. */
9616 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
9622 secondary_vptr_vtt_init_data data
;
9623 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9625 /* We only need VTTs for subobjects with virtual bases. */
9626 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9629 /* We need to use a construction vtable if this is not the primary
9633 build_ctor_vtbl_group (binfo
, t
);
9635 /* Record the offset in the VTT where this sub-VTT can be found. */
9636 BINFO_SUBVTT_INDEX (binfo
) = *index
;
9639 /* Add the address of the primary vtable for the complete object. */
9640 init
= binfo_ctor_vtable (binfo
);
9641 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9644 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9645 BINFO_VPTR_INDEX (binfo
) = *index
;
9647 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
9649 /* Recursively add the secondary VTTs for non-virtual bases. */
9650 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
9651 if (!BINFO_VIRTUAL_P (b
))
9652 build_vtt_inits (b
, t
, inits
, index
);
9654 /* Add secondary virtual pointers for all subobjects of BINFO with
9655 either virtual bases or reachable along a virtual path, except
9656 subobjects that are non-virtual primary bases. */
9657 data
.top_level_p
= top_level_p
;
9658 data
.index
= *index
;
9659 data
.inits
= *inits
;
9660 data
.type_being_constructed
= BINFO_TYPE (binfo
);
9662 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
9664 *index
= data
.index
;
9666 /* data.inits might have grown as we added secondary virtual pointers.
9667 Make sure our caller knows about the new vector. */
9668 *inits
= data
.inits
;
9671 /* Add the secondary VTTs for virtual bases in inheritance graph
9673 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
9675 if (!BINFO_VIRTUAL_P (b
))
9678 build_vtt_inits (b
, t
, inits
, index
);
9681 /* Remove the ctor vtables we created. */
9682 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
9685 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9686 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9689 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
9691 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
9693 /* We don't care about bases that don't have vtables. */
9694 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
9695 return dfs_skip_bases
;
9697 /* We're only interested in proper subobjects of the type being
9699 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
9702 /* We're only interested in bases with virtual bases or reachable
9703 via a virtual path from the type being constructed. */
9704 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9705 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
9706 return dfs_skip_bases
;
9708 /* We're not interested in non-virtual primary bases. */
9709 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
9712 /* Record the index where this secondary vptr can be found. */
9713 if (data
->top_level_p
)
9715 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9716 BINFO_VPTR_INDEX (binfo
) = data
->index
;
9718 if (BINFO_VIRTUAL_P (binfo
))
9720 /* It's a primary virtual base, and this is not a
9721 construction vtable. Find the base this is primary of in
9722 the inheritance graph, and use that base's vtable
9724 while (BINFO_PRIMARY_P (binfo
))
9725 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
9729 /* Add the initializer for the secondary vptr itself. */
9730 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
9732 /* Advance the vtt index. */
9733 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
9734 TYPE_SIZE_UNIT (ptr_type_node
));
9739 /* Called from build_vtt_inits via dfs_walk. After building
9740 constructor vtables and generating the sub-vtt from them, we need
9741 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9742 binfo of the base whose sub vtt was generated. */
9745 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9747 tree vtable
= BINFO_VTABLE (binfo
);
9749 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9750 /* If this class has no vtable, none of its bases do. */
9751 return dfs_skip_bases
;
9754 /* This might be a primary base, so have no vtable in this
9758 /* If we scribbled the construction vtable vptr into BINFO, clear it
9760 if (TREE_CODE (vtable
) == TREE_LIST
9761 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9762 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9767 /* Build the construction vtable group for BINFO which is in the
9768 hierarchy dominated by T. */
9771 build_ctor_vtbl_group (tree binfo
, tree t
)
9777 vec
<constructor_elt
, va_gc
> *v
;
9779 /* See if we've already created this construction vtable group. */
9780 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9781 if (get_global_binding (id
))
9784 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9785 /* Build a version of VTBL (with the wrong type) for use in
9786 constructing the addresses of secondary vtables in the
9787 construction vtable group. */
9788 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9790 /* Don't export construction vtables from shared libraries. Even on
9791 targets that don't support hidden visibility, this tells
9792 can_refer_decl_in_current_unit_p not to assume that it's safe to
9793 access from a different compilation unit (bz 54314). */
9794 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9795 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9798 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9799 binfo
, vtbl
, t
, &v
);
9801 /* Add the vtables for each of our virtual bases using the vbase in T
9803 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9805 vbase
= TREE_CHAIN (vbase
))
9809 if (!BINFO_VIRTUAL_P (vbase
))
9811 b
= copied_binfo (vbase
, binfo
);
9813 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9816 /* Figure out the type of the construction vtable. */
9817 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9819 TREE_TYPE (vtbl
) = type
;
9820 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9821 layout_decl (vtbl
, 0);
9823 /* Initialize the construction vtable. */
9824 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9825 initialize_artificial_var (vtbl
, v
);
9826 dump_vtable (t
, binfo
, vtbl
);
9829 /* Add the vtbl initializers for BINFO (and its bases other than
9830 non-virtual primaries) to the list of INITS. BINFO is in the
9831 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9832 the constructor the vtbl inits should be accumulated for. (If this
9833 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9834 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9835 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9836 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9837 but are not necessarily the same in terms of layout. */
9840 accumulate_vtbl_inits (tree binfo
,
9845 vec
<constructor_elt
, va_gc
> **inits
)
9849 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9851 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9853 /* If it doesn't have a vptr, we don't do anything. */
9854 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9857 /* If we're building a construction vtable, we're not interested in
9858 subobjects that don't require construction vtables. */
9860 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9861 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9864 /* Build the initializers for the BINFO-in-T vtable. */
9865 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9867 /* Walk the BINFO and its bases. We walk in preorder so that as we
9868 initialize each vtable we can figure out at what offset the
9869 secondary vtable lies from the primary vtable. We can't use
9870 dfs_walk here because we need to iterate through bases of BINFO
9871 and RTTI_BINFO simultaneously. */
9872 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9874 /* Skip virtual bases. */
9875 if (BINFO_VIRTUAL_P (base_binfo
))
9877 accumulate_vtbl_inits (base_binfo
,
9878 BINFO_BASE_BINFO (orig_binfo
, i
),
9879 rtti_binfo
, vtbl
, t
,
9884 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9885 BINFO vtable to L. */
9888 dfs_accumulate_vtbl_inits (tree binfo
,
9893 vec
<constructor_elt
, va_gc
> **l
)
9895 tree vtbl
= NULL_TREE
;
9896 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9900 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9902 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9903 primary virtual base. If it is not the same primary in
9904 the hierarchy of T, we'll need to generate a ctor vtable
9905 for it, to place at its location in T. If it is the same
9906 primary, we still need a VTT entry for the vtable, but it
9907 should point to the ctor vtable for the base it is a
9908 primary for within the sub-hierarchy of RTTI_BINFO.
9910 There are three possible cases:
9912 1) We are in the same place.
9913 2) We are a primary base within a lost primary virtual base of
9915 3) We are primary to something not a base of RTTI_BINFO. */
9918 tree last
= NULL_TREE
;
9920 /* First, look through the bases we are primary to for RTTI_BINFO
9921 or a virtual base. */
9923 while (BINFO_PRIMARY_P (b
))
9925 b
= BINFO_INHERITANCE_CHAIN (b
);
9927 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9930 /* If we run out of primary links, keep looking down our
9931 inheritance chain; we might be an indirect primary. */
9932 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9933 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9937 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9938 base B and it is a base of RTTI_BINFO, this is case 2. In
9939 either case, we share our vtable with LAST, i.e. the
9940 derived-most base within B of which we are a primary. */
9942 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9943 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9944 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9945 binfo_ctor_vtable after everything's been set up. */
9948 /* Otherwise, this is case 3 and we get our own. */
9950 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9953 n_inits
= vec_safe_length (*l
);
9960 /* Add the initializer for this vtable. */
9961 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9962 &non_fn_entries
, l
);
9964 /* Figure out the position to which the VPTR should point. */
9965 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9966 index
= size_binop (MULT_EXPR
,
9967 TYPE_SIZE_UNIT (vtable_entry_type
),
9968 size_int (non_fn_entries
+ n_inits
));
9969 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9973 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9974 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9975 straighten this out. */
9976 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9977 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9978 /* Throw away any unneeded intializers. */
9979 (*l
)->truncate (n_inits
);
9981 /* For an ordinary vtable, set BINFO_VTABLE. */
9982 BINFO_VTABLE (binfo
) = vtbl
;
9985 static GTY(()) tree abort_fndecl_addr
;
9986 static GTY(()) tree dvirt_fn
;
9988 /* Construct the initializer for BINFO's virtual function table. BINFO
9989 is part of the hierarchy dominated by T. If we're building a
9990 construction vtable, the ORIG_BINFO is the binfo we should use to
9991 find the actual function pointers to put in the vtable - but they
9992 can be overridden on the path to most-derived in the graph that
9993 ORIG_BINFO belongs. Otherwise,
9994 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9995 BINFO that should be indicated by the RTTI information in the
9996 vtable; it will be a base class of T, rather than T itself, if we
9997 are building a construction vtable.
9999 The value returned is a TREE_LIST suitable for wrapping in a
10000 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10001 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10002 number of non-function entries in the vtable.
10004 It might seem that this function should never be called with a
10005 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10006 base is always subsumed by a derived class vtable. However, when
10007 we are building construction vtables, we do build vtables for
10008 primary bases; we need these while the primary base is being
10012 build_vtbl_initializer (tree binfo
,
10016 int* non_fn_entries_p
,
10017 vec
<constructor_elt
, va_gc
> **inits
)
10020 vtbl_init_data vid
;
10023 vec
<tree
, va_gc
> *vbases
;
10024 constructor_elt
*e
;
10026 /* Initialize VID. */
10027 memset (&vid
, 0, sizeof (vid
));
10030 vid
.rtti_binfo
= rtti_binfo
;
10031 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
10032 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
10033 vid
.generate_vcall_entries
= true;
10034 /* The first vbase or vcall offset is at index -3 in the vtable. */
10035 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
10037 /* Add entries to the vtable for RTTI. */
10038 build_rtti_vtbl_entries (binfo
, &vid
);
10040 /* Create an array for keeping track of the functions we've
10041 processed. When we see multiple functions with the same
10042 signature, we share the vcall offsets. */
10043 vec_alloc (vid
.fns
, 32);
10044 /* Add the vcall and vbase offset entries. */
10045 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
10047 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10048 build_vbase_offset_vtbl_entries. */
10049 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
10050 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
10051 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
10053 /* If the target requires padding between data entries, add that now. */
10054 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
10056 int n_entries
= vec_safe_length (vid
.inits
);
10058 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
,
10061 /* Move data entries into their new positions and add padding
10062 after the new positions. Iterate backwards so we don't
10063 overwrite entries that we would need to process later. */
10064 for (ix
= n_entries
- 1;
10065 vid
.inits
->iterate (ix
, &e
);
10069 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
10070 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
10072 (*vid
.inits
)[new_position
] = *e
;
10074 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
10076 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
10077 f
->index
= NULL_TREE
;
10078 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
10079 null_pointer_node
);
10084 if (non_fn_entries_p
)
10085 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
10087 /* The initializers for virtual functions were built up in reverse
10088 order. Straighten them out and add them to the running list in one
10090 jx
= vec_safe_length (*inits
);
10091 vec_safe_grow (*inits
, jx
+ vid
.inits
->length (), true);
10093 for (ix
= vid
.inits
->length () - 1;
10094 vid
.inits
->iterate (ix
, &e
);
10096 (**inits
)[jx
] = *e
;
10098 /* Go through all the ordinary virtual functions, building up
10100 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
10104 tree fn
, fn_original
;
10105 tree init
= NULL_TREE
;
10109 if (DECL_THUNK_P (fn
))
10111 if (!DECL_NAME (fn
))
10113 if (THUNK_ALIAS (fn
))
10115 fn
= THUNK_ALIAS (fn
);
10118 fn_original
= THUNK_TARGET (fn
);
10121 /* If the only definition of this function signature along our
10122 primary base chain is from a lost primary, this vtable slot will
10123 never be used, so just zero it out. This is important to avoid
10124 requiring extra thunks which cannot be generated with the function.
10126 We first check this in update_vtable_entry_for_fn, so we handle
10127 restored primary bases properly; we also need to do it here so we
10128 zero out unused slots in ctor vtables, rather than filling them
10129 with erroneous values (though harmless, apart from relocation
10131 if (BV_LOST_PRIMARY (v
))
10132 init
= size_zero_node
;
10136 /* Pull the offset for `this', and the function to call, out of
10138 delta
= BV_DELTA (v
);
10139 vcall_index
= BV_VCALL_INDEX (v
);
10141 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
10142 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
10144 /* You can't call an abstract virtual function; it's abstract.
10145 So, we replace these functions with __pure_virtual. */
10146 if (DECL_PURE_VIRTUAL_P (fn_original
))
10149 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
10151 if (abort_fndecl_addr
== NULL
)
10153 = fold_convert (vfunc_ptr_type_node
,
10154 build_fold_addr_expr (fn
));
10155 init
= abort_fndecl_addr
;
10158 /* Likewise for deleted virtuals. */
10159 else if (DECL_DELETED_FN (fn_original
))
10163 tree name
= get_identifier ("__cxa_deleted_virtual");
10164 dvirt_fn
= get_global_binding (name
);
10166 dvirt_fn
= push_library_fn
10168 build_function_type_list (void_type_node
, NULL_TREE
),
10169 NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
10172 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
10173 init
= fold_convert (vfunc_ptr_type_node
,
10174 build_fold_addr_expr (fn
));
10178 if (!integer_zerop (delta
) || vcall_index
)
10180 fn
= make_thunk (fn
, /*this_adjusting=*/1,
10181 delta
, vcall_index
);
10182 if (!DECL_NAME (fn
))
10185 /* Take the address of the function, considering it to be of an
10186 appropriate generic type. */
10187 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
10188 init
= fold_convert (vfunc_ptr_type_node
,
10189 build_fold_addr_expr (fn
));
10190 /* Don't refer to a virtual destructor from a constructor
10191 vtable or a vtable for an abstract class, since destroying
10192 an object under construction is undefined behavior and we
10193 don't want it to be considered a candidate for speculative
10194 devirtualization. But do create the thunk for ABI
10196 if (DECL_DESTRUCTOR_P (fn_original
)
10197 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
10198 || orig_binfo
!= binfo
))
10199 init
= size_zero_node
;
10203 /* And add it to the chain of initializers. */
10204 if (TARGET_VTABLE_USES_DESCRIPTORS
)
10207 if (init
== size_zero_node
)
10208 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
10209 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
10211 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
10213 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
10214 fn
, build_int_cst (NULL_TREE
, i
));
10215 TREE_CONSTANT (fdesc
) = 1;
10217 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), fdesc
);
10221 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
10225 /* Adds to vid->inits the initializers for the vbase and vcall
10226 offsets in BINFO, which is in the hierarchy dominated by T. */
10229 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10233 /* If this is a derived class, we must first create entries
10234 corresponding to the primary base class. */
10235 b
= get_primary_binfo (binfo
);
10237 build_vcall_and_vbase_vtbl_entries (b
, vid
);
10239 /* Add the vbase entries for this base. */
10240 build_vbase_offset_vtbl_entries (binfo
, vid
);
10241 /* Add the vcall entries for this base. */
10242 build_vcall_offset_vtbl_entries (binfo
, vid
);
10245 /* Returns the initializers for the vbase offset entries in the vtable
10246 for BINFO (which is part of the class hierarchy dominated by T), in
10247 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10248 where the next vbase offset will go. */
10251 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10255 tree non_primary_binfo
;
10257 /* If there are no virtual baseclasses, then there is nothing to
10259 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
10264 /* We might be a primary base class. Go up the inheritance hierarchy
10265 until we find the most derived class of which we are a primary base:
10266 it is the offset of that which we need to use. */
10267 non_primary_binfo
= binfo
;
10268 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
10272 /* If we have reached a virtual base, then it must be a primary
10273 base (possibly multi-level) of vid->binfo, or we wouldn't
10274 have called build_vcall_and_vbase_vtbl_entries for it. But it
10275 might be a lost primary, so just skip down to vid->binfo. */
10276 if (BINFO_VIRTUAL_P (non_primary_binfo
))
10278 non_primary_binfo
= vid
->binfo
;
10282 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
10283 if (get_primary_binfo (b
) != non_primary_binfo
)
10285 non_primary_binfo
= b
;
10288 /* Go through the virtual bases, adding the offsets. */
10289 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
10291 vbase
= TREE_CHAIN (vbase
))
10296 if (!BINFO_VIRTUAL_P (vbase
))
10299 /* Find the instance of this virtual base in the complete
10301 b
= copied_binfo (vbase
, binfo
);
10303 /* If we've already got an offset for this virtual base, we
10304 don't need another one. */
10305 if (BINFO_VTABLE_PATH_MARKED (b
))
10307 BINFO_VTABLE_PATH_MARKED (b
) = 1;
10309 /* Figure out where we can find this vbase offset. */
10310 delta
= size_binop (MULT_EXPR
,
10312 fold_convert (ssizetype
,
10313 TYPE_SIZE_UNIT (vtable_entry_type
)));
10314 if (vid
->primary_vtbl_p
)
10315 BINFO_VPTR_FIELD (b
) = delta
;
10317 if (binfo
!= TYPE_BINFO (t
))
10318 /* The vbase offset had better be the same. */
10319 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
10321 /* The next vbase will come at a more negative offset. */
10322 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10323 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10325 /* The initializer is the delta from BINFO to this virtual base.
10326 The vbase offsets go in reverse inheritance-graph order, and
10327 we are walking in inheritance graph order so these end up in
10328 the right order. */
10329 delta
= size_diffop_loc (input_location
,
10330 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
10332 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
10333 fold_build1_loc (input_location
, NOP_EXPR
,
10334 vtable_entry_type
, delta
));
10338 /* Adds the initializers for the vcall offset entries in the vtable
10339 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10343 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10345 /* We only need these entries if this base is a virtual base. We
10346 compute the indices -- but do not add to the vtable -- when
10347 building the main vtable for a class. */
10348 if (binfo
== TYPE_BINFO (vid
->derived
)
10349 || (BINFO_VIRTUAL_P (binfo
)
10350 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10351 correspond to VID->DERIVED), we are building a primary
10352 construction virtual table. Since this is a primary
10353 virtual table, we do not need the vcall offsets for
10355 && binfo
!= vid
->rtti_binfo
))
10357 /* We need a vcall offset for each of the virtual functions in this
10358 vtable. For example:
10360 class A { virtual void f (); };
10361 class B1 : virtual public A { virtual void f (); };
10362 class B2 : virtual public A { virtual void f (); };
10363 class C: public B1, public B2 { virtual void f (); };
10365 A C object has a primary base of B1, which has a primary base of A. A
10366 C also has a secondary base of B2, which no longer has a primary base
10367 of A. So the B2-in-C construction vtable needs a secondary vtable for
10368 A, which will adjust the A* to a B2* to call f. We have no way of
10369 knowing what (or even whether) this offset will be when we define B2,
10370 so we store this "vcall offset" in the A sub-vtable and look it up in
10371 a "virtual thunk" for B2::f.
10373 We need entries for all the functions in our primary vtable and
10374 in our non-virtual bases' secondary vtables. */
10375 vid
->vbase
= binfo
;
10376 /* If we are just computing the vcall indices -- but do not need
10377 the actual entries -- not that. */
10378 if (!BINFO_VIRTUAL_P (binfo
))
10379 vid
->generate_vcall_entries
= false;
10380 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10381 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
10385 /* Build vcall offsets, starting with those for BINFO. */
10388 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
10391 tree primary_binfo
;
10394 /* Don't walk into virtual bases -- except, of course, for the
10395 virtual base for which we are building vcall offsets. Any
10396 primary virtual base will have already had its offsets generated
10397 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10398 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
10401 /* If BINFO has a primary base, process it first. */
10402 primary_binfo
= get_primary_binfo (binfo
);
10404 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
10406 /* Add BINFO itself to the list. */
10407 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
10409 /* Scan the non-primary bases of BINFO. */
10410 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
10411 if (base_binfo
!= primary_binfo
)
10412 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
10415 /* Called from build_vcall_offset_vtbl_entries_r. */
10418 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
10420 /* Make entries for the rest of the virtuals. */
10423 /* The ABI requires that the methods be processed in declaration
10425 for (orig_fn
= TYPE_FIELDS (BINFO_TYPE (binfo
));
10427 orig_fn
= DECL_CHAIN (orig_fn
))
10428 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
10429 add_vcall_offset (orig_fn
, binfo
, vid
);
10432 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10435 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
10439 tree derived_entry
;
10441 /* If there is already an entry for a function with the same
10442 signature as FN, then we do not need a second vcall offset.
10443 Check the list of functions already present in the derived
10445 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
10447 if (same_signature_p (derived_entry
, orig_fn
)
10448 /* We only use one vcall offset for virtual destructors,
10449 even though there are two virtual table entries. */
10450 || (DECL_DESTRUCTOR_P (derived_entry
)
10451 && DECL_DESTRUCTOR_P (orig_fn
)))
10455 /* If we are building these vcall offsets as part of building
10456 the vtable for the most derived class, remember the vcall
10458 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
10460 tree_pair_s elt
= {orig_fn
, vid
->index
};
10461 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
10464 /* The next vcall offset will be found at a more negative
10466 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10467 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10469 /* Keep track of this function. */
10470 vec_safe_push (vid
->fns
, orig_fn
);
10472 if (vid
->generate_vcall_entries
)
10477 /* Find the overriding function. */
10478 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
10479 if (fn
== error_mark_node
)
10480 vcall_offset
= build_zero_cst (vtable_entry_type
);
10483 base
= TREE_VALUE (fn
);
10485 /* The vbase we're working on is a primary base of
10486 vid->binfo. But it might be a lost primary, so its
10487 BINFO_OFFSET might be wrong, so we just use the
10488 BINFO_OFFSET from vid->binfo. */
10489 vcall_offset
= size_diffop_loc (input_location
,
10490 BINFO_OFFSET (base
),
10491 BINFO_OFFSET (vid
->binfo
));
10492 vcall_offset
= fold_build1_loc (input_location
,
10493 NOP_EXPR
, vtable_entry_type
,
10496 /* Add the initializer to the vtable. */
10497 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
10501 /* Return vtbl initializers for the RTTI entries corresponding to the
10502 BINFO's vtable. The RTTI entries should indicate the object given
10503 by VID->rtti_binfo. */
10506 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10514 t
= BINFO_TYPE (vid
->rtti_binfo
);
10516 /* To find the complete object, we will first convert to our most
10517 primary base, and then add the offset in the vtbl to that value. */
10518 b
= most_primary_binfo (binfo
);
10519 offset
= size_diffop_loc (input_location
,
10520 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
10522 /* The second entry is the address of the typeinfo object. */
10524 decl
= build_address (get_tinfo_decl (t
));
10526 decl
= integer_zero_node
;
10528 /* Convert the declaration to a type that can be stored in the
10530 init
= build_nop (vfunc_ptr_type_node
, decl
);
10531 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10533 /* Add the offset-to-top entry. It comes earlier in the vtable than
10534 the typeinfo entry. Convert the offset to look like a
10535 function pointer, so that we can put it in the vtable. */
10536 init
= build_nop (vfunc_ptr_type_node
, offset
);
10537 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10540 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10544 uniquely_derived_from_p (tree parent
, tree type
)
10546 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
10547 return base
&& base
!= error_mark_node
;
10550 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10553 publicly_uniquely_derived_p (tree parent
, tree type
)
10555 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
10557 return base
&& base
!= error_mark_node
;
10560 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10561 class between them, if any. */
10564 common_enclosing_class (tree ctx1
, tree ctx2
)
10566 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
10568 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
10569 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
10572 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10573 TYPE_MARKED_P (t
) = true;
10574 tree found
= NULL_TREE
;
10575 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10576 if (TYPE_MARKED_P (t
))
10581 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10582 TYPE_MARKED_P (t
) = false;
10586 #include "gt-cp-class.h"