1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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 void remove_zero_width_bit_fields (tree
);
140 static bool accessible_nvdtor_p (tree
);
142 /* Used by find_flexarrays and related functions. */
144 static void diagnose_flexarrays (tree
, const flexmems_t
*);
145 static void find_flexarrays (tree
, flexmems_t
*, bool = false,
146 tree
= NULL_TREE
, tree
= NULL_TREE
);
147 static void check_flexarrays (tree
, flexmems_t
* = NULL
, bool = false);
148 static void check_bases (tree
, int *, int *);
149 static void check_bases_and_members (tree
);
150 static tree
create_vtable_ptr (tree
, tree
*);
151 static void include_empty_classes (record_layout_info
);
152 static void layout_class_type (tree
, tree
*);
153 static void propagate_binfo_offsets (tree
, tree
);
154 static void layout_virtual_bases (record_layout_info
, splay_tree
);
155 static void build_vbase_offset_vtbl_entries (tree
, vtbl_init_data
*);
156 static void add_vcall_offset_vtbl_entries_r (tree
, vtbl_init_data
*);
157 static void add_vcall_offset_vtbl_entries_1 (tree
, vtbl_init_data
*);
158 static void build_vcall_offset_vtbl_entries (tree
, vtbl_init_data
*);
159 static void add_vcall_offset (tree
, tree
, vtbl_init_data
*);
160 static void layout_vtable_decl (tree
, int);
161 static tree
dfs_find_final_overrider_pre (tree
, void *);
162 static tree
dfs_find_final_overrider_post (tree
, void *);
163 static tree
find_final_overrider (tree
, tree
, tree
);
164 static int make_new_vtable (tree
, tree
);
165 static tree
get_primary_binfo (tree
);
166 static int maybe_indent_hierarchy (FILE *, int, int);
167 static tree
dump_class_hierarchy_r (FILE *, dump_flags_t
, tree
, tree
, int);
168 static void dump_class_hierarchy (tree
);
169 static void dump_class_hierarchy_1 (FILE *, dump_flags_t
, tree
);
170 static void dump_array (FILE *, tree
);
171 static void dump_vtable (tree
, tree
, tree
);
172 static void dump_vtt (tree
, tree
);
173 static void dump_thunk (FILE *, int, tree
);
174 static tree
build_vtable (tree
, tree
, tree
);
175 static void initialize_vtable (tree
, vec
<constructor_elt
, va_gc
> *);
176 static void layout_nonempty_base_or_field (record_layout_info
,
177 tree
, tree
, splay_tree
);
178 static void accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
179 vec
<constructor_elt
, va_gc
> **);
180 static void dfs_accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
181 vec
<constructor_elt
, va_gc
> **);
182 static void build_rtti_vtbl_entries (tree
, vtbl_init_data
*);
183 static void build_vcall_and_vbase_vtbl_entries (tree
, vtbl_init_data
*);
184 static void clone_constructors_and_destructors (tree
);
185 static void update_vtable_entry_for_fn (tree
, tree
, tree
, tree
*, unsigned);
186 static void build_ctor_vtbl_group (tree
, tree
);
187 static void build_vtt (tree
);
188 static tree
binfo_ctor_vtable (tree
);
189 static void build_vtt_inits (tree
, tree
, vec
<constructor_elt
, va_gc
> **,
191 static tree
dfs_build_secondary_vptr_vtt_inits (tree
, void *);
192 static tree
dfs_fixup_binfo_vtbls (tree
, void *);
193 static int record_subobject_offset (tree
, tree
, splay_tree
);
194 static int check_subobject_offset (tree
, tree
, splay_tree
);
195 static int walk_subobject_offsets (tree
, subobject_offset_fn
,
196 tree
, splay_tree
, tree
, int);
197 static int layout_conflict_p (tree
, tree
, splay_tree
, int);
198 static int splay_tree_compare_integer_csts (splay_tree_key k1
,
200 static void maybe_warn_about_inaccessible_bases (tree
);
201 static bool type_requires_array_cookie (tree
);
202 static bool base_derived_from (tree
, tree
);
203 static int empty_base_at_nonzero_offset_p (tree
, tree
, splay_tree
);
204 static tree
end_of_base (tree
);
205 static tree
get_vcall_index (tree
, tree
);
206 static bool type_maybe_constexpr_default_constructor (tree
);
207 static bool type_maybe_constexpr_destructor (tree
);
208 static bool field_poverlapping_p (tree
);
210 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
211 'structor is in charge of 'structing virtual bases, or FALSE_STMT
215 build_if_in_charge (tree true_stmt
, tree false_stmt
)
217 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl
));
218 tree cmp
= build2 (NE_EXPR
, boolean_type_node
,
219 current_in_charge_parm
, integer_zero_node
);
220 tree type
= unlowered_expr_type (true_stmt
);
221 if (VOID_TYPE_P (type
))
222 type
= unlowered_expr_type (false_stmt
);
223 tree cond
= build3 (COND_EXPR
, type
,
224 cmp
, true_stmt
, false_stmt
);
228 /* Convert to or from a base subobject. EXPR is an expression of type
229 `A' or `A*', an expression of type `B' or `B*' is returned. To
230 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
231 the B base instance within A. To convert base A to derived B, CODE
232 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
233 In this latter case, A must not be a morally virtual base of B.
234 NONNULL is true if EXPR is known to be non-NULL (this is only
235 needed when EXPR is of pointer type). CV qualifiers are preserved
239 build_base_path (enum tree_code code
,
243 tsubst_flags_t complain
)
245 tree v_binfo
= NULL_TREE
;
246 tree d_binfo
= NULL_TREE
;
250 tree null_test
= NULL
;
251 tree ptr_target_type
;
253 int want_pointer
= TYPE_PTR_P (TREE_TYPE (expr
));
254 bool has_empty
= false;
258 if (expr
== error_mark_node
|| binfo
== error_mark_node
|| !binfo
)
259 return error_mark_node
;
261 for (probe
= binfo
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
264 if (is_empty_class (BINFO_TYPE (probe
)))
266 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
270 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
272 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
273 if (dependent_type_p (probe
))
274 if (tree open
= currently_open_class (probe
))
277 if (code
== PLUS_EXPR
278 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo
), probe
))
280 /* This can happen when adjust_result_of_qualified_name_lookup can't
281 find a unique base binfo in a call to a member function. We
282 couldn't give the diagnostic then since we might have been calling
283 a static member function, so we do it now. In other cases, eg.
284 during error recovery (c++/71979), we may not have a base at all. */
285 if (complain
& tf_error
)
287 tree base
= lookup_base (probe
, BINFO_TYPE (d_binfo
),
288 ba_unique
, NULL
, complain
);
289 gcc_assert (base
== error_mark_node
|| !base
);
291 return error_mark_node
;
294 gcc_assert ((code
== MINUS_EXPR
295 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), probe
))
296 || code
== PLUS_EXPR
);
298 if (binfo
== d_binfo
)
302 if (code
== MINUS_EXPR
&& v_binfo
)
304 if (complain
& tf_error
)
306 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (v_binfo
)))
309 error ("cannot convert from pointer to base class %qT to "
310 "pointer to derived class %qT because the base is "
311 "virtual", BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
313 error ("cannot convert from base class %qT to derived "
314 "class %qT because the base is virtual",
315 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
320 error ("cannot convert from pointer to base class %qT to "
321 "pointer to derived class %qT via virtual base %qT",
322 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
),
323 BINFO_TYPE (v_binfo
));
325 error ("cannot convert from base class %qT to derived "
326 "class %qT via virtual base %qT", BINFO_TYPE (binfo
),
327 BINFO_TYPE (d_binfo
), BINFO_TYPE (v_binfo
));
330 return error_mark_node
;
335 rvalue
= !lvalue_p (expr
);
336 /* This must happen before the call to save_expr. */
337 expr
= cp_build_addr_expr (expr
, complain
);
340 expr
= mark_rvalue_use (expr
);
342 offset
= BINFO_OFFSET (binfo
);
343 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
344 target_type
= code
== PLUS_EXPR
? BINFO_TYPE (binfo
) : BINFO_TYPE (d_binfo
);
345 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
346 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
347 expression returned matches the input. */
348 target_type
= cp_build_qualified_type
349 (target_type
, cp_type_quals (TREE_TYPE (TREE_TYPE (expr
))));
350 ptr_target_type
= build_pointer_type (target_type
);
352 /* Do we need to look in the vtable for the real offset? */
353 virtual_access
= (v_binfo
&& fixed_type_p
<= 0);
355 /* Don't bother with the calculations inside sizeof; they'll ICE if the
356 source type is incomplete and the pointer value doesn't matter. In a
357 template (even in instantiate_non_dependent_expr), we don't have vtables
358 set up properly yet, and the value doesn't matter there either; we're
359 just interested in the result of overload resolution. */
360 if (cp_unevaluated_operand
!= 0
361 || processing_template_decl
362 || in_template_function ())
364 expr
= build_nop (ptr_target_type
, expr
);
368 if (!COMPLETE_TYPE_P (probe
))
370 if (complain
& tf_error
)
371 error ("cannot convert from %qT to base class %qT because %qT is "
372 "incomplete", BINFO_TYPE (d_binfo
), BINFO_TYPE (binfo
),
373 BINFO_TYPE (d_binfo
));
374 return error_mark_node
;
377 /* If we're in an NSDMI, we don't have the full constructor context yet
378 that we need for converting to a virtual base, so just build a stub
379 CONVERT_EXPR and expand it later in bot_replace. */
380 if (virtual_access
&& fixed_type_p
< 0
381 && current_scope () != current_function_decl
)
383 expr
= build1 (CONVERT_EXPR
, ptr_target_type
, expr
);
384 CONVERT_EXPR_VBASE_PATH (expr
) = true;
388 /* Do we need to check for a null pointer? */
389 if (want_pointer
&& !nonnull
)
391 /* If we know the conversion will not actually change the value
392 of EXPR, then we can avoid testing the expression for NULL.
393 We have to avoid generating a COMPONENT_REF for a base class
394 field, because other parts of the compiler know that such
395 expressions are always non-NULL. */
396 if (!virtual_access
&& integer_zerop (offset
))
397 return build_nop (ptr_target_type
, expr
);
398 null_test
= error_mark_node
;
401 /* Protect against multiple evaluation if necessary. */
402 if (TREE_SIDE_EFFECTS (expr
) && (null_test
|| virtual_access
))
403 expr
= save_expr (expr
);
405 /* Now that we've saved expr, build the real null test. */
408 tree zero
= cp_convert (TREE_TYPE (expr
), nullptr_node
, complain
);
409 null_test
= build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
411 /* This is a compiler generated comparison, don't emit
412 e.g. -Wnonnull-compare warning for it. */
413 TREE_NO_WARNING (null_test
) = 1;
416 /* If this is a simple base reference, express it as a COMPONENT_REF. */
417 if (code
== PLUS_EXPR
&& !virtual_access
418 /* We don't build base fields for empty bases, and they aren't very
419 interesting to the optimizers anyway. */
422 expr
= cp_build_fold_indirect_ref (expr
);
423 expr
= build_simple_base_path (expr
, binfo
);
424 if (rvalue
&& lvalue_p (expr
))
427 expr
= build_address (expr
);
428 target_type
= TREE_TYPE (expr
);
434 /* Going via virtual base V_BINFO. We need the static offset
435 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
436 V_BINFO. That offset is an entry in D_BINFO's vtable. */
439 if (fixed_type_p
< 0 && in_base_initializer
)
441 /* In a base member initializer, we cannot rely on the
442 vtable being set up. We have to indirect via the
446 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
447 t
= build_pointer_type (t
);
448 v_offset
= fold_convert (t
, current_vtt_parm
);
449 v_offset
= cp_build_fold_indirect_ref (v_offset
);
454 if (sanitize_flags_p (SANITIZE_VPTR
)
455 && fixed_type_p
== 0)
457 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
462 v_offset
= build_vfield_ref (cp_build_fold_indirect_ref (t
),
463 TREE_TYPE (TREE_TYPE (expr
)));
466 if (v_offset
== error_mark_node
)
467 return error_mark_node
;
469 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
470 v_offset
= build1 (NOP_EXPR
,
471 build_pointer_type (ptrdiff_type_node
),
473 v_offset
= cp_build_fold_indirect_ref (v_offset
);
474 TREE_CONSTANT (v_offset
) = 1;
476 offset
= convert_to_integer (ptrdiff_type_node
,
477 size_diffop_loc (input_location
, offset
,
478 BINFO_OFFSET (v_binfo
)));
480 if (!integer_zerop (offset
))
481 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
483 if (fixed_type_p
< 0)
484 /* Negative fixed_type_p means this is a constructor or destructor;
485 virtual base layout is fixed in in-charge [cd]tors, but not in
487 offset
= build_if_in_charge
488 (convert_to_integer (ptrdiff_type_node
, BINFO_OFFSET (binfo
)),
495 target_type
= ptr_target_type
;
497 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
499 if (!integer_zerop (offset
))
501 offset
= fold_convert (sizetype
, offset
);
502 if (code
== MINUS_EXPR
)
503 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
504 expr
= fold_build_pointer_plus (expr
, offset
);
512 expr
= cp_build_fold_indirect_ref (expr
);
520 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
,
521 expr
, build_zero_cst (target_type
));
522 /* Avoid warning for the whole conditional expression (in addition
523 to NULL_TEST itself -- see above) in case the result is used in
524 a nonnull context that the front end -Wnonnull checks. */
525 TREE_NO_WARNING (expr
) = 1;
531 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
532 Perform a derived-to-base conversion by recursively building up a
533 sequence of COMPONENT_REFs to the appropriate base fields. */
536 build_simple_base_path (tree expr
, tree binfo
)
538 tree type
= BINFO_TYPE (binfo
);
539 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
542 if (d_binfo
== NULL_TREE
)
546 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
548 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
549 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
550 an lvalue in the front end; only _DECLs and _REFs are lvalues
552 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
554 expr
= cp_build_fold_indirect_ref (temp
);
560 expr
= build_simple_base_path (expr
, d_binfo
);
562 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
563 field
; field
= DECL_CHAIN (field
))
564 /* Is this the base field created by build_base_field? */
565 if (TREE_CODE (field
) == FIELD_DECL
566 && DECL_FIELD_IS_BASE (field
)
567 && TREE_TYPE (field
) == type
568 /* If we're looking for a field in the most-derived class,
569 also check the field offset; we can have two base fields
570 of the same type if one is an indirect virtual base and one
571 is a direct non-virtual base. */
572 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
573 || tree_int_cst_equal (byte_position (field
),
574 BINFO_OFFSET (binfo
))))
576 /* We don't use build_class_member_access_expr here, as that
577 has unnecessary checks, and more importantly results in
578 recursive calls to dfs_walk_once. */
579 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
581 expr
= build3 (COMPONENT_REF
,
582 cp_build_qualified_type (type
, type_quals
),
583 expr
, field
, NULL_TREE
);
584 /* Mark the expression const or volatile, as appropriate.
585 Even though we've dealt with the type above, we still have
586 to mark the expression itself. */
587 if (type_quals
& TYPE_QUAL_CONST
)
588 TREE_READONLY (expr
) = 1;
589 if (type_quals
& TYPE_QUAL_VOLATILE
)
590 TREE_THIS_VOLATILE (expr
) = 1;
595 /* Didn't find the base field?!? */
599 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
600 type is a class type or a pointer to a class type. In the former
601 case, TYPE is also a class type; in the latter it is another
602 pointer type. If CHECK_ACCESS is true, an error message is emitted
603 if TYPE is inaccessible. If OBJECT has pointer type, the value is
604 assumed to be non-NULL. */
607 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
608 tsubst_flags_t complain
)
613 if (TYPE_PTR_P (TREE_TYPE (object
)))
615 object_type
= TREE_TYPE (TREE_TYPE (object
));
616 type
= TREE_TYPE (type
);
619 object_type
= TREE_TYPE (object
);
621 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
623 if (!binfo
|| binfo
== error_mark_node
)
624 return error_mark_node
;
626 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
629 /* EXPR is an expression with unqualified class type. BASE is a base
630 binfo of that class type. Returns EXPR, converted to the BASE
631 type. This function assumes that EXPR is the most derived class;
632 therefore virtual bases can be found at their static offsets. */
635 convert_to_base_statically (tree expr
, tree base
)
639 expr_type
= TREE_TYPE (expr
);
640 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
642 /* If this is a non-empty base, use a COMPONENT_REF. */
643 if (!is_empty_class (BINFO_TYPE (base
)))
644 return build_simple_base_path (expr
, base
);
646 /* We use fold_build2 and fold_convert below to simplify the trees
647 provided to the optimizers. It is not safe to call these functions
648 when processing a template because they do not handle C++-specific
650 gcc_assert (!processing_template_decl
);
651 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
652 if (!integer_zerop (BINFO_OFFSET (base
)))
653 expr
= fold_build_pointer_plus_loc (input_location
,
654 expr
, BINFO_OFFSET (base
));
655 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
656 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
664 build_vfield_ref (tree datum
, tree type
)
666 tree vfield
, vcontext
;
668 if (datum
== error_mark_node
669 /* Can happen in case of duplicate base types (c++/59082). */
670 || !TYPE_VFIELD (type
))
671 return error_mark_node
;
673 /* First, convert to the requested type. */
674 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
675 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
676 /*nonnull=*/true, tf_warning_or_error
);
678 /* Second, the requested type may not be the owner of its own vptr.
679 If not, convert to the base class that owns it. We cannot use
680 convert_to_base here, because VCONTEXT may appear more than once
681 in the inheritance hierarchy of TYPE, and thus direct conversion
682 between the types may be ambiguous. Following the path back up
683 one step at a time via primary bases avoids the problem. */
684 vfield
= TYPE_VFIELD (type
);
685 vcontext
= DECL_CONTEXT (vfield
);
686 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
688 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
689 type
= TREE_TYPE (datum
);
692 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
695 /* Given an object INSTANCE, return an expression which yields the
696 vtable element corresponding to INDEX. There are many special
697 cases for INSTANCE which we take care of here, mainly to avoid
698 creating extra tree nodes when we don't have to. */
701 build_vtbl_ref (tree instance
, tree idx
)
704 tree vtbl
= NULL_TREE
;
706 /* Try to figure out what a reference refers to, and
707 access its virtual function table directly. */
710 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
712 tree basetype
= non_reference (TREE_TYPE (instance
));
714 if (fixed_type
&& !cdtorp
)
716 tree binfo
= lookup_base (fixed_type
, basetype
,
717 ba_unique
, NULL
, tf_none
);
718 if (binfo
&& binfo
!= error_mark_node
)
719 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
723 vtbl
= build_vfield_ref (instance
, basetype
);
725 aref
= build_array_ref (input_location
, vtbl
, idx
);
726 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
731 /* Given a stable object pointer INSTANCE_PTR, return an expression which
732 yields a function pointer corresponding to vtable element INDEX. */
735 build_vfn_ref (tree instance_ptr
, tree idx
)
739 aref
= build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr
), idx
);
741 /* When using function descriptors, the address of the
742 vtable entry is treated as a function pointer. */
743 if (TARGET_VTABLE_USES_DESCRIPTORS
)
744 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
745 cp_build_addr_expr (aref
, tf_warning_or_error
));
747 /* Remember this as a method reference, for later devirtualization. */
748 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
753 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
754 for the given TYPE. */
757 get_vtable_name (tree type
)
759 return mangle_vtbl_for_type (type
);
762 /* DECL is an entity associated with TYPE, like a virtual table or an
763 implicitly generated constructor. Determine whether or not DECL
764 should have external or internal linkage at the object file
765 level. This routine does not deal with COMDAT linkage and other
766 similar complexities; it simply sets TREE_PUBLIC if it possible for
767 entities in other translation units to contain copies of DECL, in
771 set_linkage_according_to_type (tree
/*type*/, tree decl
)
773 TREE_PUBLIC (decl
) = 1;
774 determine_visibility (decl
);
777 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
778 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
779 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
782 build_vtable (tree class_type
, tree name
, tree vtable_type
)
786 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
787 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
788 now to avoid confusion in mangle_decl. */
789 SET_DECL_ASSEMBLER_NAME (decl
, name
);
790 DECL_CONTEXT (decl
) = class_type
;
791 DECL_ARTIFICIAL (decl
) = 1;
792 TREE_STATIC (decl
) = 1;
793 TREE_READONLY (decl
) = 1;
794 DECL_VIRTUAL_P (decl
) = 1;
795 SET_DECL_ALIGN (decl
, TARGET_VTABLE_ENTRY_ALIGN
);
796 DECL_USER_ALIGN (decl
) = true;
797 DECL_VTABLE_OR_VTT_P (decl
) = 1;
798 set_linkage_according_to_type (class_type
, decl
);
799 /* The vtable has not been defined -- yet. */
800 DECL_EXTERNAL (decl
) = 1;
801 DECL_NOT_REALLY_EXTERN (decl
) = 1;
803 /* Mark the VAR_DECL node representing the vtable itself as a
804 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
805 is rather important that such things be ignored because any
806 effort to actually generate DWARF for them will run into
807 trouble when/if we encounter code like:
810 struct S { virtual void member (); };
812 because the artificial declaration of the vtable itself (as
813 manufactured by the g++ front end) will say that the vtable is
814 a static member of `S' but only *after* the debug output for
815 the definition of `S' has already been output. This causes
816 grief because the DWARF entry for the definition of the vtable
817 will try to refer back to an earlier *declaration* of the
818 vtable as a static member of `S' and there won't be one. We
819 might be able to arrange to have the "vtable static member"
820 attached to the member list for `S' before the debug info for
821 `S' get written (which would solve the problem) but that would
822 require more intrusive changes to the g++ front end. */
823 DECL_IGNORED_P (decl
) = 1;
828 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
829 or even complete. If this does not exist, create it. If COMPLETE is
830 nonzero, then complete the definition of it -- that will render it
831 impossible to actually build the vtable, but is useful to get at those
832 which are known to exist in the runtime. */
835 get_vtable_decl (tree type
, int complete
)
839 if (CLASSTYPE_VTABLES (type
))
840 return CLASSTYPE_VTABLES (type
);
842 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
843 CLASSTYPE_VTABLES (type
) = decl
;
847 DECL_EXTERNAL (decl
) = 1;
848 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
854 /* Build the primary virtual function table for TYPE. If BINFO is
855 non-NULL, build the vtable starting with the initial approximation
856 that it is the same as the one which is the head of the association
857 list. Returns a nonzero value if a new vtable is actually
861 build_primary_vtable (tree binfo
, tree type
)
866 decl
= get_vtable_decl (type
, /*complete=*/0);
870 if (BINFO_NEW_VTABLE_MARKED (binfo
))
871 /* We have already created a vtable for this base, so there's
872 no need to do it again. */
875 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
876 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
877 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
878 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
882 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
883 virtuals
= NULL_TREE
;
886 /* Initialize the association list for this type, based
887 on our first approximation. */
888 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
889 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
890 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
894 /* Give BINFO a new virtual function table which is initialized
895 with a skeleton-copy of its original initialization. The only
896 entry that changes is the `delta' entry, so we can really
897 share a lot of structure.
899 FOR_TYPE is the most derived type which caused this table to
902 Returns nonzero if we haven't met BINFO before.
904 The order in which vtables are built (by calling this function) for
905 an object must remain the same, otherwise a binary incompatibility
909 build_secondary_vtable (tree binfo
)
911 if (BINFO_NEW_VTABLE_MARKED (binfo
))
912 /* We already created a vtable for this base. There's no need to
916 /* Remember that we've created a vtable for this BINFO, so that we
917 don't try to do so again. */
918 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
920 /* Make fresh virtual list, so we can smash it later. */
921 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
923 /* Secondary vtables are laid out as part of the same structure as
924 the primary vtable. */
925 BINFO_VTABLE (binfo
) = NULL_TREE
;
929 /* Create a new vtable for BINFO which is the hierarchy dominated by
930 T. Return nonzero if we actually created a new vtable. */
933 make_new_vtable (tree t
, tree binfo
)
935 if (binfo
== TYPE_BINFO (t
))
936 /* In this case, it is *type*'s vtable we are modifying. We start
937 with the approximation that its vtable is that of the
938 immediate base class. */
939 return build_primary_vtable (binfo
, t
);
941 /* This is our very own copy of `basetype' to play with. Later,
942 we will fill in all the virtual functions that override the
943 virtual functions in these base classes which are not defined
944 by the current type. */
945 return build_secondary_vtable (binfo
);
948 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
949 (which is in the hierarchy dominated by T) list FNDECL as its
950 BV_FN. DELTA is the required constant adjustment from the `this'
951 pointer where the vtable entry appears to the `this' required when
952 the function is actually called. */
955 modify_vtable_entry (tree t
,
965 if (fndecl
!= BV_FN (v
)
966 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
968 /* We need a new vtable for BINFO. */
969 if (make_new_vtable (t
, binfo
))
971 /* If we really did make a new vtable, we also made a copy
972 of the BINFO_VIRTUALS list. Now, we have to find the
973 corresponding entry in that list. */
974 *virtuals
= BINFO_VIRTUALS (binfo
);
975 while (BV_FN (*virtuals
) != BV_FN (v
))
976 *virtuals
= TREE_CHAIN (*virtuals
);
980 BV_DELTA (v
) = delta
;
981 BV_VCALL_INDEX (v
) = NULL_TREE
;
987 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
988 METHOD is being injected via a using_decl. Returns true if the
989 method could be added to the method vec. */
992 add_method (tree type
, tree method
, bool via_using
)
994 if (method
== error_mark_node
)
997 gcc_assert (!DECL_EXTERN_C_P (method
));
999 tree
*slot
= find_member_slot (type
, DECL_NAME (method
));
1000 tree current_fns
= slot
? *slot
: NULL_TREE
;
1005 /* Check to see if we've already got this method. */
1006 for (ovl_iterator
iter (current_fns
); iter
; ++iter
)
1014 if (TREE_CODE (fn
) != TREE_CODE (method
))
1017 /* Two using-declarations can coexist, we'll complain about ambiguity in
1018 overload resolution. */
1019 if (via_using
&& iter
.using_p ()
1020 /* Except handle inherited constructors specially. */
1021 && ! DECL_CONSTRUCTOR_P (fn
))
1024 /* [over.load] Member function declarations with the
1025 same name and the same parameter types cannot be
1026 overloaded if any of them is a static member
1027 function declaration.
1029 [over.load] Member function declarations with the same name and
1030 the same parameter-type-list as well as member function template
1031 declarations with the same name, the same parameter-type-list, and
1032 the same template parameter lists cannot be overloaded if any of
1033 them, but not all, have a ref-qualifier.
1035 [namespace.udecl] When a using-declaration brings names
1036 from a base class into a derived class scope, member
1037 functions in the derived class override and/or hide member
1038 functions with the same name and parameter types in a base
1039 class (rather than conflicting). */
1040 fn_type
= TREE_TYPE (fn
);
1041 method_type
= TREE_TYPE (method
);
1042 parms1
= TYPE_ARG_TYPES (fn_type
);
1043 parms2
= TYPE_ARG_TYPES (method_type
);
1045 /* Compare the quals on the 'this' parm. Don't compare
1046 the whole types, as used functions are treated as
1047 coming from the using class in overload resolution. */
1048 if (! DECL_STATIC_FUNCTION_P (fn
)
1049 && ! DECL_STATIC_FUNCTION_P (method
)
1050 /* Either both or neither need to be ref-qualified for
1051 differing quals to allow overloading. */
1052 && (FUNCTION_REF_QUALIFIED (fn_type
)
1053 == FUNCTION_REF_QUALIFIED (method_type
))
1054 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1055 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1058 /* For templates, the return type and template parameters
1059 must be identical. */
1060 if (TREE_CODE (fn
) == TEMPLATE_DECL
1061 && (!same_type_p (TREE_TYPE (fn_type
),
1062 TREE_TYPE (method_type
))
1063 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1064 DECL_TEMPLATE_PARMS (method
))))
1067 if (! DECL_STATIC_FUNCTION_P (fn
))
1068 parms1
= TREE_CHAIN (parms1
);
1069 if (! DECL_STATIC_FUNCTION_P (method
))
1070 parms2
= TREE_CHAIN (parms2
);
1072 /* Bring back parameters omitted from an inherited ctor. */
1073 if (ctor_omit_inherited_parms (fn
))
1074 parms1
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
1075 if (ctor_omit_inherited_parms (method
))
1076 parms2
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method
));
1078 if (compparms (parms1
, parms2
)
1079 && (!DECL_CONV_FN_P (fn
)
1080 || same_type_p (TREE_TYPE (fn_type
),
1081 TREE_TYPE (method_type
))))
1083 if (!equivalently_constrained (fn
, method
))
1085 if (processing_template_decl
)
1086 /* We can't check satisfaction in dependent context, wait until
1087 the class is instantiated. */
1090 special_function_kind sfk
= special_memfn_p (method
);
1093 || DECL_INHERITED_CTOR (fn
)
1094 || TREE_CODE (fn
) == TEMPLATE_DECL
)
1095 /* Member function templates and non-special member functions
1096 coexist if they are not equivalently constrained. A member
1097 function is not hidden by an inherited constructor. */
1100 /* P0848: For special member functions, deleted, unsatisfied, or
1101 less constrained overloads are ineligible. We implement this
1102 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1103 use the notion of eligibility, and the selected destructor can
1104 be deleted, but removing unsatisfied or less constrained
1105 overloads has the same effect as overload resolution. */
1106 bool dtor
= (sfk
== sfk_destructor
);
1108 losem
= ((!dtor
&& DECL_DELETED_FN (method
))
1109 || !constraints_satisfied_p (method
));
1110 bool losef
= ((!dtor
&& DECL_DELETED_FN (fn
))
1111 || !constraints_satisfied_p (fn
));
1114 win
= losem
- losef
;
1116 win
= more_constrained (fn
, method
);
1118 /* Leave FN in the method vec, discard METHOD. */
1122 /* Remove FN, add METHOD. */
1123 current_fns
= iter
.remove_node (current_fns
);
1127 /* Let them coexist for now. */
1131 /* If these are versions of the same function, process and
1133 if (TREE_CODE (fn
) == FUNCTION_DECL
1134 && maybe_version_functions (method
, fn
, true))
1137 if (DECL_INHERITED_CTOR (method
))
1139 if (DECL_INHERITED_CTOR (fn
))
1141 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1142 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1143 if (flag_new_inheriting_ctors
)
1147 /* Inheriting the same constructor along different
1148 paths, combine them. */
1149 SET_DECL_INHERITED_CTOR
1150 (fn
, ovl_make (DECL_INHERITED_CTOR (method
),
1151 DECL_INHERITED_CTOR (fn
)));
1152 /* And discard the new one. */
1156 /* Inherited ctors can coexist until overload
1160 error_at (DECL_SOURCE_LOCATION (method
),
1161 "%q#D conflicts with version inherited from %qT",
1163 inform (DECL_SOURCE_LOCATION (fn
),
1164 "version inherited from %qT declared here",
1167 /* Otherwise defer to the other function. */
1172 /* Defer to the local function. */
1174 else if (flag_new_inheriting_ctors
1175 && DECL_INHERITED_CTOR (fn
))
1177 /* Remove the inherited constructor. */
1178 current_fns
= iter
.remove_node (current_fns
);
1183 error_at (DECL_SOURCE_LOCATION (method
),
1184 "%q#D cannot be overloaded with %q#D", method
, fn
);
1185 inform (DECL_SOURCE_LOCATION (fn
),
1186 "previous declaration %q#D", fn
);
1192 current_fns
= ovl_insert (method
, current_fns
, via_using
);
1194 if (!COMPLETE_TYPE_P (type
) && !DECL_CONV_FN_P (method
)
1195 && !push_class_level_binding (DECL_NAME (method
), current_fns
))
1199 slot
= add_member_slot (type
, DECL_NAME (method
));
1201 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1202 grok_special_member_properties (method
);
1204 *slot
= current_fns
;
1209 /* Subroutines of finish_struct. */
1211 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1212 legit, otherwise return 0. */
1215 alter_access (tree t
, tree fdecl
, tree access
)
1219 retrofit_lang_decl (fdecl
);
1221 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1223 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1226 if (TREE_VALUE (elem
) != access
)
1228 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1229 error ("conflicting access specifications for method"
1230 " %q+D, ignored", TREE_TYPE (fdecl
));
1232 error ("conflicting access specifications for field %qE, ignored",
1237 /* They're changing the access to the same thing they changed
1238 it to before. That's OK. */
1244 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1245 tf_warning_or_error
);
1246 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1252 /* Return the access node for DECL's access in its enclosing class. */
1255 declared_access (tree decl
)
1257 return (TREE_PRIVATE (decl
) ? access_private_node
1258 : TREE_PROTECTED (decl
) ? access_protected_node
1259 : access_public_node
);
1262 /* Process the USING_DECL, which is a member of T. */
1265 handle_using_decl (tree using_decl
, tree t
)
1267 tree decl
= USING_DECL_DECLS (using_decl
);
1268 tree name
= DECL_NAME (using_decl
);
1269 tree access
= declared_access (using_decl
);
1270 tree flist
= NULL_TREE
;
1273 gcc_assert (!processing_template_decl
&& decl
);
1275 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1276 tf_warning_or_error
);
1279 old_value
= OVL_FIRST (old_value
);
1281 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1284 old_value
= NULL_TREE
;
1287 cp_emit_debug_info_for_using (decl
, t
);
1289 if (is_overloaded_fn (decl
))
1294 else if (is_overloaded_fn (old_value
))
1297 /* It's OK to use functions from a base when there are functions with
1298 the same name already present in the current class. */;
1301 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1302 "because of local method %q#D with same name",
1303 using_decl
, t
, old_value
);
1304 inform (DECL_SOURCE_LOCATION (old_value
),
1305 "local method %q#D declared here", old_value
);
1309 else if (!DECL_ARTIFICIAL (old_value
))
1311 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1312 "because of local member %q#D with same name",
1313 using_decl
, t
, old_value
);
1314 inform (DECL_SOURCE_LOCATION (old_value
),
1315 "local member %q#D declared here", old_value
);
1319 /* Make type T see field decl FDECL with access ACCESS. */
1321 for (ovl_iterator
iter (flist
); iter
; ++iter
)
1323 add_method (t
, *iter
, true);
1324 alter_access (t
, *iter
, access
);
1327 alter_access (t
, decl
, access
);
1330 /* Data structure for find_abi_tags_r, below. */
1334 tree t
; // The type that we're checking for missing tags.
1335 tree subob
; // The subobject of T that we're getting tags from.
1336 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1339 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1340 in the context of P. TAG can be either an identifier (the DECL_NAME of
1341 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1344 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1346 if (!IDENTIFIER_MARKED (id
))
1348 if (p
->tags
!= error_mark_node
)
1350 /* We're collecting tags from template arguments or from
1351 the type of a variable or function return type. */
1352 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1354 /* Don't inherit this tag multiple times. */
1355 IDENTIFIER_MARKED (id
) = true;
1359 /* Tags inherited from type template arguments are only used
1360 to avoid warnings. */
1361 ABI_TAG_IMPLICIT (p
->tags
) = true;
1364 /* For functions and variables we want to warn, too. */
1367 /* Otherwise we're diagnosing missing tags. */
1368 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1370 auto_diagnostic_group d
;
1371 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1372 "that %qT (used in its return type) has",
1374 inform (location_of (*tp
), "%qT declared here", *tp
);
1376 else if (VAR_P (p
->t
))
1378 auto_diagnostic_group d
;
1379 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1380 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1381 inform (location_of (*tp
), "%qT declared here", *tp
);
1383 else if (TYPE_P (p
->subob
))
1385 auto_diagnostic_group d
;
1386 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1387 "that base %qT has", p
->t
, tag
, p
->subob
))
1388 inform (location_of (p
->subob
), "%qT declared here",
1393 auto_diagnostic_group d
;
1394 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1395 "that %qT (used in the type of %qD) has",
1396 p
->t
, tag
, *tp
, p
->subob
))
1398 inform (location_of (p
->subob
), "%qD declared here",
1400 inform (location_of (*tp
), "%qT declared here", *tp
);
1406 /* Find all the ABI tags in the attribute list ATTR and either call
1407 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1410 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1414 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1415 attr
= TREE_CHAIN (attr
))
1416 for (tree list
= TREE_VALUE (attr
); list
;
1417 list
= TREE_CHAIN (list
))
1419 tree tag
= TREE_VALUE (list
);
1420 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1422 check_tag (tag
, id
, tp
, p
);
1424 IDENTIFIER_MARKED (id
) = val
;
1428 /* Find all the ABI tags on T and its enclosing scopes and either call
1429 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1432 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1434 while (t
!= global_namespace
)
1439 attr
= TYPE_ATTRIBUTES (t
);
1440 t
= CP_TYPE_CONTEXT (t
);
1444 attr
= DECL_ATTRIBUTES (t
);
1445 t
= CP_DECL_CONTEXT (t
);
1447 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1451 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1452 types with ABI tags, add the corresponding identifiers to the VEC in
1453 *DATA and set IDENTIFIER_MARKED. */
1456 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1458 if (!OVERLOAD_TYPE_P (*tp
))
1461 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1462 anyway, but let's make sure of it. */
1463 *walk_subtrees
= false;
1465 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1467 mark_or_check_tags (*tp
, tp
, p
, false);
1472 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1473 IDENTIFIER_MARKED on its ABI tags. */
1476 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1478 if (!OVERLOAD_TYPE_P (*tp
))
1481 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1482 anyway, but let's make sure of it. */
1483 *walk_subtrees
= false;
1485 bool *valp
= static_cast<bool*>(data
);
1487 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1492 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1496 mark_abi_tags (tree t
, bool val
)
1498 mark_or_check_tags (t
, NULL
, NULL
, val
);
1501 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1502 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1504 /* Template arguments are part of the signature. */
1505 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1506 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1508 tree arg
= TREE_VEC_ELT (level
, j
);
1509 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1512 if (TREE_CODE (t
) == FUNCTION_DECL
)
1513 /* A function's parameter types are part of the signature, so
1514 we don't need to inherit any tags that are also in them. */
1515 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1516 arg
= TREE_CHAIN (arg
))
1517 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1518 mark_abi_tags_r
, &val
);
1522 /* Check that T has all the ABI tags that subobject SUBOB has, or
1523 warn if not. If T is a (variable or function) declaration, also
1524 return any missing tags, and add them to T if JUST_CHECKING is false. */
1527 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1529 bool inherit
= DECL_P (t
);
1531 if (!inherit
&& !warn_abi_tag
)
1534 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1535 if (!TREE_PUBLIC (decl
))
1536 /* No need to worry about things local to this TU. */
1539 mark_abi_tags (t
, true);
1541 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1542 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1544 data
.tags
= NULL_TREE
;
1546 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1548 if (!(inherit
&& data
.tags
))
1549 /* We don't need to do anything with data.tags. */;
1550 else if (just_checking
)
1551 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1553 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1554 IDENTIFIER_MARKED (id
) = false;
1558 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1560 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1563 = tree_cons (abi_tag_identifier
, data
.tags
, DECL_ATTRIBUTES (t
));
1566 mark_abi_tags (t
, false);
1571 /* Check that DECL has all the ABI tags that are used in parts of its type
1572 that are not reflected in its mangled name. */
1575 check_abi_tags (tree decl
)
1578 check_abi_tags (decl
, TREE_TYPE (decl
));
1579 else if (TREE_CODE (decl
) == FUNCTION_DECL
1580 && !DECL_CONV_FN_P (decl
)
1581 && !mangle_return_type_p (decl
))
1582 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1585 /* Return any ABI tags that are used in parts of the type of DECL
1586 that are not reflected in its mangled name. This function is only
1587 used in backward-compatible mangling for ABI <11. */
1590 missing_abi_tags (tree decl
)
1593 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1594 else if (TREE_CODE (decl
) == FUNCTION_DECL
1595 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1596 that we can use this function for setting need_abi_warning
1597 regardless of the current flag_abi_version. */
1598 && !mangle_return_type_p (decl
))
1599 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1605 inherit_targ_abi_tags (tree t
)
1607 if (!CLASS_TYPE_P (t
)
1608 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1611 mark_abi_tags (t
, true);
1613 tree args
= CLASSTYPE_TI_ARGS (t
);
1614 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1615 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1617 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1618 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1620 tree arg
= TREE_VEC_ELT (level
, j
);
1622 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1626 // If we found some tags on our template arguments, add them to our
1627 // abi_tag attribute.
1630 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1632 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1635 = tree_cons (abi_tag_identifier
, data
.tags
, TYPE_ATTRIBUTES (t
));
1638 mark_abi_tags (t
, false);
1641 /* Return true, iff class T has a non-virtual destructor that is
1642 accessible from outside the class heirarchy (i.e. is public, or
1643 there's a suitable friend. */
1646 accessible_nvdtor_p (tree t
)
1648 tree dtor
= CLASSTYPE_DESTRUCTOR (t
);
1650 /* An implicitly declared destructor is always public. And,
1651 if it were virtual, we would have created it by now. */
1655 if (DECL_VINDEX (dtor
))
1656 return false; /* Virtual */
1658 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1659 return true; /* Public */
1661 if (CLASSTYPE_FRIEND_CLASSES (t
)
1662 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1663 return true; /* Has friends */
1668 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1669 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1670 properties of the bases. */
1673 check_bases (tree t
,
1674 int* cant_have_const_ctor_p
,
1675 int* no_const_asn_ref_p
)
1678 bool seen_non_virtual_nearly_empty_base_p
= 0;
1679 int seen_tm_mask
= 0;
1682 tree field
= NULL_TREE
;
1684 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1685 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1686 if (TREE_CODE (field
) == FIELD_DECL
)
1689 for (binfo
= TYPE_BINFO (t
), i
= 0;
1690 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1692 tree basetype
= TREE_TYPE (base_binfo
);
1694 gcc_assert (COMPLETE_TYPE_P (basetype
));
1696 if (CLASSTYPE_FINAL (basetype
))
1697 error ("cannot derive from %<final%> base %qT in derived type %qT",
1700 /* If any base class is non-literal, so is the derived class. */
1701 if (!CLASSTYPE_LITERAL_P (basetype
))
1702 CLASSTYPE_LITERAL_P (t
) = false;
1704 /* If the base class doesn't have copy constructors or
1705 assignment operators that take const references, then the
1706 derived class cannot have such a member automatically
1708 if (TYPE_HAS_COPY_CTOR (basetype
)
1709 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1710 *cant_have_const_ctor_p
= 1;
1711 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1712 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1713 *no_const_asn_ref_p
= 1;
1715 if (BINFO_VIRTUAL_P (base_binfo
))
1716 /* A virtual base does not effect nearly emptiness. */
1718 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1720 if (seen_non_virtual_nearly_empty_base_p
)
1721 /* And if there is more than one nearly empty base, then the
1722 derived class is not nearly empty either. */
1723 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1725 /* Remember we've seen one. */
1726 seen_non_virtual_nearly_empty_base_p
= 1;
1728 else if (!is_empty_class (basetype
))
1729 /* If the base class is not empty or nearly empty, then this
1730 class cannot be nearly empty. */
1731 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1733 /* A lot of properties from the bases also apply to the derived
1735 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1736 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1737 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1738 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1739 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1740 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1741 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1742 || !TYPE_HAS_COPY_CTOR (basetype
));
1743 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1744 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1745 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1746 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1747 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1748 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1749 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1750 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1751 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1752 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1753 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1754 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1755 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1756 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1757 if (TYPE_HAS_MUTABLE_P (basetype
))
1758 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1760 /* A standard-layout class is a class that:
1762 * has no non-standard-layout base classes, */
1763 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1764 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1767 /* ...has no base classes of the same type as the first non-static
1769 if (field
&& DECL_CONTEXT (field
) == t
1770 && (same_type_ignoring_top_level_qualifiers_p
1771 (TREE_TYPE (field
), basetype
)))
1772 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1774 ...has at most one base class subobject of any given type... */
1775 else if (CLASSTYPE_REPEATED_BASE_P (t
))
1776 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1778 /* ...either has no non-static data members in the most-derived
1779 class and at most one base class with non-static data
1780 members, or has no base classes with non-static data
1781 members. FIXME This was reworded in DR 1813. */
1782 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1783 basefield
= DECL_CHAIN (basefield
))
1784 if (TREE_CODE (basefield
) == FIELD_DECL
1785 && !(DECL_FIELD_IS_BASE (basefield
)
1786 && integer_zerop (DECL_SIZE (basefield
))))
1789 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1796 /* Don't bother collecting tm attributes if transactional memory
1797 support is not enabled. */
1800 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1802 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1805 check_abi_tags (t
, basetype
);
1808 /* If one of the base classes had TM attributes, and the current class
1809 doesn't define its own, then the current class inherits one. */
1810 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1812 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1813 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1817 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1818 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1819 that have had a nearly-empty virtual primary base stolen by some
1820 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1824 determine_primary_bases (tree t
)
1827 tree primary
= NULL_TREE
;
1828 tree type_binfo
= TYPE_BINFO (t
);
1831 /* Determine the primary bases of our bases. */
1832 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1833 base_binfo
= TREE_CHAIN (base_binfo
))
1835 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1837 /* See if we're the non-virtual primary of our inheritance
1839 if (!BINFO_VIRTUAL_P (base_binfo
))
1841 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1842 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1845 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1846 BINFO_TYPE (parent_primary
)))
1847 /* We are the primary binfo. */
1848 BINFO_PRIMARY_P (base_binfo
) = 1;
1850 /* Determine if we have a virtual primary base, and mark it so.
1852 if (primary
&& BINFO_VIRTUAL_P (primary
))
1854 tree this_primary
= copied_binfo (primary
, base_binfo
);
1856 if (BINFO_PRIMARY_P (this_primary
))
1857 /* Someone already claimed this base. */
1858 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1863 BINFO_PRIMARY_P (this_primary
) = 1;
1864 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1866 /* A virtual binfo might have been copied from within
1867 another hierarchy. As we're about to use it as a
1868 primary base, make sure the offsets match. */
1869 delta
= size_diffop_loc (input_location
,
1870 fold_convert (ssizetype
,
1871 BINFO_OFFSET (base_binfo
)),
1872 fold_convert (ssizetype
,
1873 BINFO_OFFSET (this_primary
)));
1875 propagate_binfo_offsets (this_primary
, delta
);
1880 /* First look for a dynamic direct non-virtual base. */
1881 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1883 tree basetype
= BINFO_TYPE (base_binfo
);
1885 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1887 primary
= base_binfo
;
1892 /* A "nearly-empty" virtual base class can be the primary base
1893 class, if no non-virtual polymorphic base can be found. Look for
1894 a nearly-empty virtual dynamic base that is not already a primary
1895 base of something in the hierarchy. If there is no such base,
1896 just pick the first nearly-empty virtual base. */
1898 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1899 base_binfo
= TREE_CHAIN (base_binfo
))
1900 if (BINFO_VIRTUAL_P (base_binfo
)
1901 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1903 if (!BINFO_PRIMARY_P (base_binfo
))
1905 /* Found one that is not primary. */
1906 primary
= base_binfo
;
1910 /* Remember the first candidate. */
1911 primary
= base_binfo
;
1915 /* If we've got a primary base, use it. */
1918 tree basetype
= BINFO_TYPE (primary
);
1920 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1921 if (BINFO_PRIMARY_P (primary
))
1922 /* We are stealing a primary base. */
1923 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1924 BINFO_PRIMARY_P (primary
) = 1;
1925 if (BINFO_VIRTUAL_P (primary
))
1929 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1930 /* A virtual binfo might have been copied from within
1931 another hierarchy. As we're about to use it as a primary
1932 base, make sure the offsets match. */
1933 delta
= size_diffop_loc (input_location
, ssize_int (0),
1934 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
1936 propagate_binfo_offsets (primary
, delta
);
1939 primary
= TYPE_BINFO (basetype
);
1941 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1942 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1943 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1947 /* Update the variant types of T. */
1950 fixup_type_variants (tree t
)
1957 for (variants
= TYPE_NEXT_VARIANT (t
);
1959 variants
= TYPE_NEXT_VARIANT (variants
))
1961 /* These fields are in the _TYPE part of the node, not in
1962 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1963 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1964 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1965 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1966 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1968 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1969 CLASSTYPE_FINAL (variants
) = CLASSTYPE_FINAL (t
);
1971 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1973 /* Copy whatever these are holding today. */
1974 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1975 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1977 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1978 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1982 /* KLASS is a class that we're applying may_alias to after the body is
1983 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1984 canonical type(s) will be implicitly updated. */
1987 fixup_may_alias (tree klass
)
1991 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
1992 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1993 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1994 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
1995 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1996 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1999 /* Early variant fixups: we apply attributes at the beginning of the class
2000 definition, and we need to fix up any variants that have already been
2001 made via elaborated-type-specifier so that check_qualified_type works. */
2004 fixup_attribute_variants (tree t
)
2011 tree attrs
= TYPE_ATTRIBUTES (t
);
2012 unsigned align
= TYPE_ALIGN (t
);
2013 bool user_align
= TYPE_USER_ALIGN (t
);
2014 bool may_alias
= lookup_attribute ("may_alias", attrs
);
2015 bool packed
= TYPE_PACKED (t
);
2018 fixup_may_alias (t
);
2020 for (variants
= TYPE_NEXT_VARIANT (t
);
2022 variants
= TYPE_NEXT_VARIANT (variants
))
2024 /* These are the two fields that check_qualified_type looks at and
2025 are affected by attributes. */
2026 TYPE_ATTRIBUTES (variants
) = attrs
;
2027 unsigned valign
= align
;
2028 if (TYPE_USER_ALIGN (variants
))
2029 valign
= MAX (valign
, TYPE_ALIGN (variants
));
2031 TYPE_USER_ALIGN (variants
) = user_align
;
2032 SET_TYPE_ALIGN (variants
, valign
);
2033 TYPE_PACKED (variants
) = packed
;
2035 fixup_may_alias (variants
);
2039 /* Set memoizing fields and bits of T (and its variants) for later
2043 finish_struct_bits (tree t
)
2045 /* Fix up variants (if any). */
2046 fixup_type_variants (t
);
2048 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
2049 /* For a class w/o baseclasses, 'finish_struct' has set
2050 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2051 Similarly for a class whose base classes do not have vtables.
2052 When neither of these is true, we might have removed abstract
2053 virtuals (by providing a definition), added some (by declaring
2054 new ones), or redeclared ones from a base class. We need to
2055 recalculate what's really an abstract virtual at this point (by
2056 looking in the vtables). */
2057 get_pure_virtuals (t
);
2059 /* If this type has a copy constructor or a destructor, force its
2060 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2061 nonzero. This will cause it to be passed by invisible reference
2062 and prevent it from being returned in a register. */
2063 if (type_has_nontrivial_copy_init (t
)
2064 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2067 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2068 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2070 SET_TYPE_MODE (variants
, BLKmode
);
2071 TREE_ADDRESSABLE (variants
) = 1;
2076 /* Issue warnings about T having private constructors, but no friends,
2079 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2080 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2081 non-private static member functions. */
2084 maybe_warn_about_overly_private_class (tree t
)
2086 int has_member_fn
= 0;
2087 int has_nonprivate_method
= 0;
2088 bool nonprivate_ctor
= false;
2090 if (!warn_ctor_dtor_privacy
2091 /* If the class has friends, those entities might create and
2092 access instances, so we should not warn. */
2093 || (CLASSTYPE_FRIEND_CLASSES (t
)
2094 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2095 /* We will have warned when the template was declared; there's
2096 no need to warn on every instantiation. */
2097 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2098 /* There's no reason to even consider warning about this
2102 /* We only issue one warning, if more than one applies, because
2103 otherwise, on code like:
2106 // Oops - forgot `public:'
2112 we warn several times about essentially the same problem. */
2114 /* Check to see if all (non-constructor, non-destructor) member
2115 functions are private. (Since there are no friends or
2116 non-private statics, we can't ever call any of the private member
2118 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2119 if (TREE_CODE (fn
) == USING_DECL
2120 && DECL_NAME (fn
) == ctor_identifier
2121 && !TREE_PRIVATE (fn
))
2122 nonprivate_ctor
= true;
2123 else if (!DECL_DECLARES_FUNCTION_P (fn
))
2124 /* Not a function. */;
2125 else if (DECL_ARTIFICIAL (fn
))
2126 /* We're not interested in compiler-generated methods; they don't
2127 provide any way to call private members. */;
2128 else if (!TREE_PRIVATE (fn
))
2130 if (DECL_STATIC_FUNCTION_P (fn
))
2131 /* A non-private static member function is just like a
2132 friend; it can create and invoke private member
2133 functions, and be accessed without a class
2137 has_nonprivate_method
= 1;
2138 /* Keep searching for a static member function. */
2140 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2143 if (!has_nonprivate_method
&& has_member_fn
)
2145 /* There are no non-private methods, and there's at least one
2146 private member function that isn't a constructor or
2147 destructor. (If all the private members are
2148 constructors/destructors we want to use the code below that
2149 issues error messages specifically referring to
2150 constructors/destructors.) */
2152 tree binfo
= TYPE_BINFO (t
);
2154 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2155 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2157 has_nonprivate_method
= 1;
2160 if (!has_nonprivate_method
)
2162 warning (OPT_Wctor_dtor_privacy
,
2163 "all member functions in class %qT are private", t
);
2168 /* Even if some of the member functions are non-private, the class
2169 won't be useful for much if all the constructors or destructors
2170 are private: such an object can never be created or destroyed. */
2171 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
2172 if (TREE_PRIVATE (dtor
))
2174 warning (OPT_Wctor_dtor_privacy
,
2175 "%q#T only defines a private destructor and has no friends",
2180 /* Warn about classes that have private constructors and no friends. */
2181 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2182 /* Implicitly generated constructors are always public. */
2183 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
2185 tree copy_or_move
= NULL_TREE
;
2187 /* If a non-template class does not define a copy
2188 constructor, one is defined for it, enabling it to avoid
2189 this warning. For a template class, this does not
2190 happen, and so we would normally get a warning on:
2192 template <class T> class C { private: C(); };
2194 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2195 complete non-template or fully instantiated classes have this
2197 if (!TYPE_HAS_COPY_CTOR (t
))
2198 nonprivate_ctor
= true;
2200 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
));
2201 !nonprivate_ctor
&& iter
; ++iter
)
2202 if (TREE_PRIVATE (*iter
))
2204 else if (copy_fn_p (*iter
) || move_fn_p (*iter
))
2205 /* Ideally, we wouldn't count any constructor that takes
2206 an argument of the class type as a parameter, because
2207 such things cannot be used to construct an instance of
2208 the class unless you already have one. */
2209 copy_or_move
= *iter
;
2211 nonprivate_ctor
= true;
2213 if (!nonprivate_ctor
)
2215 bool w
= warning (OPT_Wctor_dtor_privacy
,
2216 "%q#T only defines private constructors and has "
2218 if (w
&& copy_or_move
)
2219 inform (DECL_SOURCE_LOCATION (copy_or_move
),
2220 "%q#D is public, but requires an existing %q#T object",
2227 /* Make BINFO's vtable have N entries, including RTTI entries,
2228 vbase and vcall offsets, etc. Set its type and call the back end
2232 layout_vtable_decl (tree binfo
, int n
)
2237 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2238 layout_type (atype
);
2240 /* We may have to grow the vtable. */
2241 vtable
= get_vtbl_decl_for_binfo (binfo
);
2242 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2244 TREE_TYPE (vtable
) = atype
;
2245 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2246 layout_decl (vtable
, 0);
2250 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2251 have the same signature. */
2254 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2256 /* One destructor overrides another if they are the same kind of
2258 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2259 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2261 /* But a non-destructor never overrides a destructor, nor vice
2262 versa, nor do different kinds of destructors override
2263 one-another. For example, a complete object destructor does not
2264 override a deleting destructor. */
2265 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2268 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2269 || (DECL_CONV_FN_P (fndecl
)
2270 && DECL_CONV_FN_P (base_fndecl
)
2271 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2272 DECL_CONV_FN_TYPE (base_fndecl
))))
2274 tree fntype
= TREE_TYPE (fndecl
);
2275 tree base_fntype
= TREE_TYPE (base_fndecl
);
2276 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2277 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2278 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2279 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2285 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2289 base_derived_from (tree derived
, tree base
)
2293 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2295 if (probe
== derived
)
2297 else if (BINFO_VIRTUAL_P (probe
))
2298 /* If we meet a virtual base, we can't follow the inheritance
2299 any more. See if the complete type of DERIVED contains
2300 such a virtual base. */
2301 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2307 struct find_final_overrider_data
{
2308 /* The function for which we are trying to find a final overrider. */
2310 /* The base class in which the function was declared. */
2311 tree declaring_base
;
2312 /* The candidate overriders. */
2314 /* Path to most derived. */
2318 /* Add the overrider along the current path to FFOD->CANDIDATES.
2319 Returns true if an overrider was found; false otherwise. */
2322 dfs_find_final_overrider_1 (tree binfo
,
2323 find_final_overrider_data
*ffod
,
2328 /* If BINFO is not the most derived type, try a more derived class.
2329 A definition there will overrider a definition here. */
2333 if (dfs_find_final_overrider_1
2334 (ffod
->path
[depth
], ffod
, depth
))
2338 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2341 tree
*candidate
= &ffod
->candidates
;
2343 /* Remove any candidates overridden by this new function. */
2346 /* If *CANDIDATE overrides METHOD, then METHOD
2347 cannot override anything else on the list. */
2348 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2350 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2351 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2352 *candidate
= TREE_CHAIN (*candidate
);
2354 candidate
= &TREE_CHAIN (*candidate
);
2357 /* Add the new function. */
2358 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2365 /* Called from find_final_overrider via dfs_walk. */
2368 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2370 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2372 if (binfo
== ffod
->declaring_base
)
2373 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2374 ffod
->path
.safe_push (binfo
);
2380 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2382 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2388 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2389 FN and whose TREE_VALUE is the binfo for the base where the
2390 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2391 DERIVED) is the base object in which FN is declared. */
2394 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2396 find_final_overrider_data ffod
;
2398 /* Getting this right is a little tricky. This is valid:
2400 struct S { virtual void f (); };
2401 struct T { virtual void f (); };
2402 struct U : public S, public T { };
2404 even though calling `f' in `U' is ambiguous. But,
2406 struct R { virtual void f(); };
2407 struct S : virtual public R { virtual void f (); };
2408 struct T : virtual public R { virtual void f (); };
2409 struct U : public S, public T { };
2411 is not -- there's no way to decide whether to put `S::f' or
2412 `T::f' in the vtable for `R'.
2414 The solution is to look at all paths to BINFO. If we find
2415 different overriders along any two, then there is a problem. */
2416 if (DECL_THUNK_P (fn
))
2417 fn
= THUNK_TARGET (fn
);
2419 /* Determine the depth of the hierarchy. */
2421 ffod
.declaring_base
= binfo
;
2422 ffod
.candidates
= NULL_TREE
;
2423 ffod
.path
.create (30);
2425 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2426 dfs_find_final_overrider_post
, &ffod
);
2428 ffod
.path
.release ();
2430 /* If there was no winner, issue an error message. */
2431 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2432 return error_mark_node
;
2434 return ffod
.candidates
;
2437 /* Return the index of the vcall offset for FN when TYPE is used as a
2441 get_vcall_index (tree fn
, tree type
)
2443 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2447 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2448 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2449 || same_signature_p (fn
, p
->purpose
))
2452 /* There should always be an appropriate index. */
2456 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2457 overrider at that index in the vtable. This should only be used when we
2458 know that BINFO is correct for the dynamic type of the object. */
2461 lookup_vfn_in_binfo (tree idx
, tree binfo
)
2463 int ix
= tree_to_shwi (idx
);
2464 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2465 ix
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
2466 while (BINFO_PRIMARY_P (binfo
))
2467 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2468 class that actually owns the vtable. */
2469 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
2470 tree virtuals
= BINFO_VIRTUALS (binfo
);
2471 return TREE_VALUE (chain_index (ix
, virtuals
));
2474 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2475 dominated by T. FN is the old function; VIRTUALS points to the
2476 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2477 of that entry in the list. */
2480 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2488 tree overrider_fn
, overrider_target
;
2489 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2490 tree over_return
, base_return
;
2493 /* Find the nearest primary base (possibly binfo itself) which defines
2494 this function; this is the class the caller will convert to when
2495 calling FN through BINFO. */
2496 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2499 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2502 /* The nearest definition is from a lost primary. */
2503 if (BINFO_LOST_PRIMARY_P (b
))
2508 /* Find the final overrider. */
2509 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2510 if (overrider
== error_mark_node
)
2512 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2515 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2517 /* Check for adjusting covariant return types. */
2518 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2519 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2521 if (INDIRECT_TYPE_P (over_return
)
2522 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2523 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2524 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2525 /* If the overrider is invalid, don't even try. */
2526 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2528 /* If FN is a covariant thunk, we must figure out the adjustment
2529 to the final base FN was converting to. As OVERRIDER_TARGET might
2530 also be converting to the return type of FN, we have to
2531 combine the two conversions here. */
2532 tree fixed_offset
, virtual_offset
;
2534 over_return
= TREE_TYPE (over_return
);
2535 base_return
= TREE_TYPE (base_return
);
2537 if (DECL_THUNK_P (fn
))
2539 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2540 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2541 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2544 fixed_offset
= virtual_offset
= NULL_TREE
;
2547 /* Find the equivalent binfo within the return type of the
2548 overriding function. We will want the vbase offset from
2550 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2552 else if (!same_type_ignoring_top_level_qualifiers_p
2553 (over_return
, base_return
))
2555 /* There was no existing virtual thunk (which takes
2556 precedence). So find the binfo of the base function's
2557 return type within the overriding function's return type.
2558 Fortunately we know the covariancy is valid (it
2559 has already been checked), so we can just iterate along
2560 the binfos, which have been chained in inheritance graph
2561 order. Of course it is lame that we have to repeat the
2562 search here anyway -- we should really be caching pieces
2563 of the vtable and avoiding this repeated work. */
2564 tree thunk_binfo
= NULL_TREE
;
2565 tree base_binfo
= TYPE_BINFO (base_return
);
2567 /* Find the base binfo within the overriding function's
2568 return type. We will always find a thunk_binfo, except
2569 when the covariancy is invalid (which we will have
2570 already diagnosed). */
2572 for (thunk_binfo
= TYPE_BINFO (over_return
); thunk_binfo
;
2573 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2574 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2575 BINFO_TYPE (base_binfo
)))
2577 gcc_assert (thunk_binfo
|| errorcount
);
2579 /* See if virtual inheritance is involved. */
2580 for (virtual_offset
= thunk_binfo
;
2582 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2583 if (BINFO_VIRTUAL_P (virtual_offset
))
2587 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2589 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2593 /* We convert via virtual base. Adjust the fixed
2594 offset to be from there. */
2596 size_diffop (offset
,
2597 fold_convert (ssizetype
,
2598 BINFO_OFFSET (virtual_offset
)));
2601 /* There was an existing fixed offset, this must be
2602 from the base just converted to, and the base the
2603 FN was thunking to. */
2604 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2606 fixed_offset
= offset
;
2610 if (fixed_offset
|| virtual_offset
)
2611 /* Replace the overriding function with a covariant thunk. We
2612 will emit the overriding function in its own slot as
2614 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2615 fixed_offset
, virtual_offset
);
2618 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2619 !DECL_THUNK_P (fn
));
2621 /* If we need a covariant thunk, then we may need to adjust first_defn.
2622 The ABI specifies that the thunks emitted with a function are
2623 determined by which bases the function overrides, so we need to be
2624 sure that we're using a thunk for some overridden base; even if we
2625 know that the necessary this adjustment is zero, there may not be an
2626 appropriate zero-this-adjustment thunk for us to use since thunks for
2627 overriding virtual bases always use the vcall offset.
2629 Furthermore, just choosing any base that overrides this function isn't
2630 quite right, as this slot won't be used for calls through a type that
2631 puts a covariant thunk here. Calling the function through such a type
2632 will use a different slot, and that slot is the one that determines
2633 the thunk emitted for that base.
2635 So, keep looking until we find the base that we're really overriding
2636 in this slot: the nearest primary base that doesn't use a covariant
2637 thunk in this slot. */
2638 if (overrider_target
!= overrider_fn
)
2640 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2641 /* We already know that the overrider needs a covariant thunk. */
2642 b
= get_primary_binfo (b
);
2643 for (; ; b
= get_primary_binfo (b
))
2645 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2646 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2647 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2649 if (BINFO_LOST_PRIMARY_P (b
))
2655 /* Assume that we will produce a thunk that convert all the way to
2656 the final overrider, and not to an intermediate virtual base. */
2657 virtual_base
= NULL_TREE
;
2659 /* See if we can convert to an intermediate virtual base first, and then
2660 use the vcall offset located there to finish the conversion. */
2661 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2663 /* If we find the final overrider, then we can stop
2665 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2666 BINFO_TYPE (TREE_VALUE (overrider
))))
2669 /* If we find a virtual base, and we haven't yet found the
2670 overrider, then there is a virtual base between the
2671 declaring base (first_defn) and the final overrider. */
2672 if (BINFO_VIRTUAL_P (b
))
2679 /* Compute the constant adjustment to the `this' pointer. The
2680 `this' pointer, when this function is called, will point at BINFO
2681 (or one of its primary bases, which are at the same offset). */
2683 /* The `this' pointer needs to be adjusted from the declaration to
2684 the nearest virtual base. */
2685 delta
= size_diffop_loc (input_location
,
2686 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2687 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2689 /* If the nearest definition is in a lost primary, we don't need an
2690 entry in our vtable. Except possibly in a constructor vtable,
2691 if we happen to get our primary back. In that case, the offset
2692 will be zero, as it will be a primary base. */
2693 delta
= size_zero_node
;
2695 /* The `this' pointer needs to be adjusted from pointing to
2696 BINFO to pointing at the base where the final overrider
2698 delta
= size_diffop_loc (input_location
,
2699 fold_convert (ssizetype
,
2700 BINFO_OFFSET (TREE_VALUE (overrider
))),
2701 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2703 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2706 BV_VCALL_INDEX (*virtuals
)
2707 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2709 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2711 BV_LOST_PRIMARY (*virtuals
) = lost
;
2714 /* Called from modify_all_vtables via dfs_walk. */
2717 dfs_modify_vtables (tree binfo
, void* data
)
2719 tree t
= (tree
) data
;
2724 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2725 /* A base without a vtable needs no modification, and its bases
2726 are uninteresting. */
2727 return dfs_skip_bases
;
2729 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2730 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2731 /* Don't do the primary vtable, if it's new. */
2734 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2735 /* There's no need to modify the vtable for a non-virtual primary
2736 base; we're not going to use that vtable anyhow. We do still
2737 need to do this for virtual primary bases, as they could become
2738 non-primary in a construction vtable. */
2741 make_new_vtable (t
, binfo
);
2743 /* Now, go through each of the virtual functions in the virtual
2744 function table for BINFO. Find the final overrider, and update
2745 the BINFO_VIRTUALS list appropriately. */
2746 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2747 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2749 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2750 old_virtuals
= TREE_CHAIN (old_virtuals
))
2751 update_vtable_entry_for_fn (t
,
2753 BV_FN (old_virtuals
),
2759 /* Update all of the primary and secondary vtables for T. Create new
2760 vtables as required, and initialize their RTTI information. Each
2761 of the functions in VIRTUALS is declared in T and may override a
2762 virtual function from a base class; find and modify the appropriate
2763 entries to point to the overriding functions. Returns a list, in
2764 declaration order, of the virtual functions that are declared in T,
2765 but do not appear in the primary base class vtable, and which
2766 should therefore be appended to the end of the vtable for T. */
2769 modify_all_vtables (tree t
, tree virtuals
)
2771 tree binfo
= TYPE_BINFO (t
);
2774 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2775 if (TYPE_CONTAINS_VPTR_P (t
))
2776 get_vtable_decl (t
, false);
2778 /* Update all of the vtables. */
2779 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2781 /* Add virtual functions not already in our primary vtable. These
2782 will be both those introduced by this class, and those overridden
2783 from secondary bases. It does not include virtuals merely
2784 inherited from secondary bases. */
2785 for (fnsp
= &virtuals
; *fnsp
; )
2787 tree fn
= TREE_VALUE (*fnsp
);
2789 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2790 || DECL_VINDEX (fn
) == error_mark_node
)
2792 /* We don't need to adjust the `this' pointer when
2793 calling this function. */
2794 BV_DELTA (*fnsp
) = integer_zero_node
;
2795 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2797 /* This is a function not already in our vtable. Keep it. */
2798 fnsp
= &TREE_CHAIN (*fnsp
);
2801 /* We've already got an entry for this function. Skip it. */
2802 *fnsp
= TREE_CHAIN (*fnsp
);
2808 /* Get the base virtual function declarations in T that have the
2812 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2814 bool found_decls
= false;
2816 /* Find virtual functions in T with the indicated NAME. */
2817 for (ovl_iterator
iter (get_class_binding (t
, name
)); iter
; ++iter
)
2819 tree method
= *iter
;
2821 if (TREE_CODE (method
) == FUNCTION_DECL
&& DECL_VINDEX (method
))
2823 base_fndecls
->safe_push (method
);
2831 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2832 for (int i
= 0; i
< n_baseclasses
; i
++)
2834 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2835 get_basefndecls (name
, basetype
, base_fndecls
);
2839 /* If this method overrides a virtual method from a base, then mark
2840 this member function as being virtual as well. Do 'final' and
2841 'override' checks too. */
2844 check_for_override (tree decl
, tree ctype
)
2846 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2847 /* In [temp.mem] we have:
2849 A specialization of a member function template does not
2850 override a virtual function from a base class. */
2853 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2854 used for a vfunc. That avoids the expensive look_for_overrides
2855 call that when we know there's nothing to find. As conversion
2856 operators for the same type can have distinct identifiers, we
2857 cannot optimize those in that way. */
2858 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2859 || DECL_CONV_FN_P (decl
))
2860 && look_for_overrides (ctype
, decl
)
2861 /* Check staticness after we've checked if we 'override'. */
2862 && !DECL_STATIC_FUNCTION_P (decl
))
2864 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2865 the error_mark_node so that we know it is an overriding
2867 DECL_VINDEX (decl
) = decl
;
2870 && !DECL_OVERRIDE_P (decl
)
2871 && !DECL_FINAL_P (decl
)
2872 && !DECL_DESTRUCTOR_P (decl
))
2873 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2874 "%qD can be marked override", decl
);
2876 else if (DECL_OVERRIDE_P (decl
))
2877 error ("%q+#D marked %<override%>, but does not override", decl
);
2879 if (DECL_VIRTUAL_P (decl
))
2881 /* Remember this identifier is virtual name. */
2882 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = true;
2884 if (!DECL_VINDEX (decl
))
2885 /* It's a new vfunc. */
2886 DECL_VINDEX (decl
) = error_mark_node
;
2888 if (DECL_DESTRUCTOR_P (decl
))
2889 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2891 else if (DECL_FINAL_P (decl
))
2892 error ("%q+#D marked %<final%>, but is not virtual", decl
);
2895 /* Warn about hidden virtual functions that are not overridden in t.
2896 We know that constructors and destructors don't apply. */
2899 warn_hidden (tree t
)
2901 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (t
))
2902 for (unsigned ix
= member_vec
->length (); ix
--;)
2904 tree fns
= (*member_vec
)[ix
];
2909 tree name
= OVL_NAME (fns
);
2910 auto_vec
<tree
, 20> base_fndecls
;
2915 /* Iterate through all of the base classes looking for possibly
2916 hidden functions. */
2917 for (binfo
= TYPE_BINFO (t
), j
= 0;
2918 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2920 tree basetype
= BINFO_TYPE (base_binfo
);
2921 get_basefndecls (name
, basetype
, &base_fndecls
);
2924 /* If there are no functions to hide, continue. */
2925 if (base_fndecls
.is_empty ())
2928 /* Remove any overridden functions. */
2929 for (ovl_iterator
iter (fns
); iter
; ++iter
)
2931 tree fndecl
= *iter
;
2932 if (TREE_CODE (fndecl
) == FUNCTION_DECL
2933 && DECL_VINDEX (fndecl
))
2935 /* If the method from the base class has the same
2936 signature as the method from the derived class, it
2937 has been overridden. */
2938 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
2940 && same_signature_p (fndecl
, base_fndecls
[k
]))
2941 base_fndecls
[k
] = NULL_TREE
;
2945 /* Now give a warning for all base functions without overriders,
2946 as they are hidden. */
2948 FOR_EACH_VEC_ELT (base_fndecls
, j
, base_fndecl
)
2951 auto_diagnostic_group d
;
2952 /* Here we know it is a hider, and no overrider exists. */
2953 if (warning_at (location_of (base_fndecl
),
2954 OPT_Woverloaded_virtual
,
2955 "%qD was hidden", base_fndecl
))
2956 inform (location_of (fns
), " by %qD", fns
);
2961 /* Recursive helper for finish_struct_anon. */
2964 finish_struct_anon_r (tree field
, bool complain
)
2966 for (tree elt
= TYPE_FIELDS (TREE_TYPE (field
)); elt
; elt
= DECL_CHAIN (elt
))
2968 /* We're generally only interested in entities the user
2969 declared, but we also find nested classes by noticing
2970 the TYPE_DECL that we create implicitly. You're
2971 allowed to put one anonymous union inside another,
2972 though, so we explicitly tolerate that. We use
2973 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2974 we also allow unnamed types used for defining fields. */
2975 if (DECL_ARTIFICIAL (elt
)
2976 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2977 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
2981 && (TREE_CODE (elt
) != FIELD_DECL
2982 || (TREE_PRIVATE (elt
) || TREE_PROTECTED (elt
))))
2984 /* We already complained about static data members in
2985 finish_static_data_member_decl. */
2988 auto_diagnostic_group d
;
2989 if (permerror (DECL_SOURCE_LOCATION (elt
),
2990 TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
2991 ? "%q#D invalid; an anonymous union may "
2992 "only have public non-static data members"
2993 : "%q#D invalid; an anonymous struct may "
2994 "only have public non-static data members", elt
))
2997 if (flag_permissive
&& !hint
)
3000 inform (DECL_SOURCE_LOCATION (elt
),
3001 "this flexibility is deprecated and will be "
3008 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3009 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3011 /* Recurse into the anonymous aggregates to correctly handle
3012 access control (c++/24926):
3023 if (DECL_NAME (elt
) == NULL_TREE
3024 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
3025 finish_struct_anon_r (elt
, /*complain=*/false);
3029 /* Check for things that are invalid. There are probably plenty of other
3030 things we should check for also. */
3033 finish_struct_anon (tree t
)
3035 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3037 if (TREE_STATIC (field
))
3039 if (TREE_CODE (field
) != FIELD_DECL
)
3042 if (DECL_NAME (field
) == NULL_TREE
3043 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3044 finish_struct_anon_r (field
, /*complain=*/true);
3048 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3049 will be used later during class template instantiation.
3050 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3051 a non-static member data (FIELD_DECL), a member function
3052 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3053 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3054 When FRIEND_P is nonzero, T is either a friend class
3055 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3056 (FUNCTION_DECL, TEMPLATE_DECL). */
3059 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
3061 if (CLASSTYPE_TEMPLATE_INFO (type
)
3062 && TREE_CODE (t
) != CONST_DECL
)
3064 tree purpose
= friend_p
? NULL_TREE
: type
;
3066 CLASSTYPE_DECL_LIST (type
)
3067 = tree_cons (purpose
, t
, CLASSTYPE_DECL_LIST (type
));
3071 /* This function is called from declare_virt_assop_and_dtor via
3074 DATA is a type that direcly or indirectly inherits the base
3075 represented by BINFO. If BINFO contains a virtual assignment [copy
3076 assignment or move assigment] operator or a virtual constructor,
3077 declare that function in DATA if it hasn't been already declared. */
3080 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
3082 tree bv
, fn
, t
= (tree
)data
;
3083 tree opname
= assign_op_identifier
;
3085 gcc_assert (t
&& CLASS_TYPE_P (t
));
3086 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
3088 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
3089 /* A base without a vtable needs no modification, and its bases
3090 are uninteresting. */
3091 return dfs_skip_bases
;
3093 if (BINFO_PRIMARY_P (binfo
))
3094 /* If this is a primary base, then we have already looked at the
3095 virtual functions of its vtable. */
3098 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3102 if (DECL_NAME (fn
) == opname
)
3104 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3105 lazily_declare_fn (sfk_copy_assignment
, t
);
3106 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3107 lazily_declare_fn (sfk_move_assignment
, t
);
3109 else if (DECL_DESTRUCTOR_P (fn
)
3110 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3111 lazily_declare_fn (sfk_destructor
, t
);
3117 /* If the class type T has a direct or indirect base that contains a
3118 virtual assignment operator or a virtual destructor, declare that
3119 function in T if it hasn't been already declared. */
3122 declare_virt_assop_and_dtor (tree t
)
3124 if (!(TYPE_POLYMORPHIC_P (t
)
3125 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3126 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3127 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3130 dfs_walk_all (TYPE_BINFO (t
),
3131 dfs_declare_virt_assop_and_dtor
,
3135 /* Declare the inheriting constructor for class T inherited from base
3136 constructor CTOR with the parameter array PARMS of size NPARMS. */
3139 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3141 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3143 /* We don't declare an inheriting ctor that would be a default,
3144 copy or move ctor for derived or base. */
3148 && TYPE_REF_P (parms
[0]))
3150 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3151 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3155 tree parmlist
= void_list_node
;
3156 for (int i
= nparms
- 1; i
>= 0; i
--)
3157 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3158 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3159 t
, false, ctor
, parmlist
);
3161 if (add_method (t
, fn
, false))
3163 DECL_CHAIN (fn
) = TYPE_FIELDS (t
);
3164 TYPE_FIELDS (t
) = fn
;
3168 /* Declare all the inheriting constructors for class T inherited from base
3169 constructor CTOR. */
3172 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3174 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3176 if (flag_new_inheriting_ctors
)
3178 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3179 t
, /*const*/false, ctor
, parms
);
3180 add_method (t
, ctor
, using_decl
!= NULL_TREE
);
3184 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3186 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3188 if (TREE_PURPOSE (parms
))
3189 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3190 new_parms
[i
++] = TREE_VALUE (parms
);
3192 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3193 if (parms
== NULL_TREE
)
3195 auto_diagnostic_group d
;
3196 if (warning (OPT_Winherited_variadic_ctor
,
3197 "the ellipsis in %qD is not inherited", ctor
))
3198 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3202 /* Create default constructors, assignment operators, and so forth for
3203 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3204 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3205 the class cannot have a default constructor, copy constructor
3206 taking a const reference argument, or an assignment operator taking
3207 a const reference, respectively. */
3210 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3211 int cant_have_const_cctor
,
3212 int cant_have_const_assignment
)
3215 if (!CLASSTYPE_DESTRUCTOR (t
))
3216 /* In general, we create destructors lazily. */
3217 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3219 bool move_ok
= false;
3220 if (cxx_dialect
>= cxx11
&& CLASSTYPE_LAZY_DESTRUCTOR (t
)
3221 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3222 && !classtype_has_move_assign_or_move_ctor_p (t
, false))
3227 If there is no user-declared constructor for a class, a default
3228 constructor is implicitly declared. */
3229 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3231 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3232 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3233 if (cxx_dialect
>= cxx11
)
3234 TYPE_HAS_CONSTEXPR_CTOR (t
)
3235 /* Don't force the declaration to get a hard answer; if the
3236 definition would have made the class non-literal, it will still be
3237 non-literal because of the base or member in question, and that
3238 gives a better diagnostic. */
3239 = type_maybe_constexpr_default_constructor (t
);
3244 If a class definition does not explicitly declare a copy
3245 constructor, one is declared implicitly. */
3246 if (! TYPE_HAS_COPY_CTOR (t
))
3248 TYPE_HAS_COPY_CTOR (t
) = 1;
3249 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3250 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3252 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3255 /* If there is no assignment operator, one will be created if and
3256 when it is needed. For now, just record whether or not the type
3257 of the parameter to the assignment operator will be a const or
3258 non-const reference. */
3259 if (!TYPE_HAS_COPY_ASSIGN (t
))
3261 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3262 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3263 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3264 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3265 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3268 /* We can't be lazy about declaring functions that might override
3269 a virtual function from a base class. */
3270 declare_virt_assop_and_dtor (t
);
3272 /* If the class definition does not explicitly declare an == operator
3273 function, but declares a defaulted three-way comparison operator function,
3274 an == operator function is declared implicitly. */
3275 if (!classtype_has_op (t
, EQ_EXPR
))
3276 if (tree space
= classtype_has_defaulted_op (t
, SPACESHIP_EXPR
))
3278 tree eq
= implicitly_declare_fn (sfk_comparison
, t
, false, space
,
3280 if (DECL_FRIEND_P (space
))
3281 do_friend (NULL_TREE
, DECL_NAME (eq
), eq
,
3282 NULL_TREE
, NO_SPECIAL
, true);
3285 add_method (t
, eq
, false);
3286 DECL_CHAIN (eq
) = TYPE_FIELDS (t
);
3287 TYPE_FIELDS (t
) = eq
;
3289 maybe_add_class_template_decl_list (t
, eq
, DECL_FRIEND_P (space
));
3292 while (*access_decls
)
3294 tree using_decl
= TREE_VALUE (*access_decls
);
3295 tree decl
= USING_DECL_DECLS (using_decl
);
3296 if (DECL_NAME (using_decl
) == ctor_identifier
)
3298 /* declare, then remove the decl */
3299 tree ctor_list
= decl
;
3300 location_t loc
= input_location
;
3301 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3302 for (ovl_iterator
iter (ctor_list
); iter
; ++iter
)
3303 one_inherited_ctor (*iter
, t
, using_decl
);
3304 *access_decls
= TREE_CHAIN (*access_decls
);
3305 input_location
= loc
;
3308 access_decls
= &TREE_CHAIN (*access_decls
);
3312 /* Cache of enum_min_precision values. */
3313 static GTY((deletable
)) hash_map
<tree
, int> *enum_to_min_precision
;
3315 /* Return the minimum precision of a bit-field needed to store all
3316 enumerators of ENUMERAL_TYPE TYPE. */
3319 enum_min_precision (tree type
)
3321 type
= TYPE_MAIN_VARIANT (type
);
3322 /* For unscoped enums without fixed underlying type and without mode
3323 attribute we can just use precision of the underlying type. */
3324 if (UNSCOPED_ENUM_P (type
)
3325 && !ENUM_FIXED_UNDERLYING_TYPE_P (type
)
3326 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type
)))
3327 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
));
3329 if (enum_to_min_precision
== NULL
)
3330 enum_to_min_precision
= hash_map
<tree
, int>::create_ggc (37);
3333 int &prec
= enum_to_min_precision
->get_or_insert (type
, &existed
);
3337 tree minnode
, maxnode
;
3338 if (TYPE_VALUES (type
))
3340 minnode
= maxnode
= NULL_TREE
;
3341 for (tree values
= TYPE_VALUES (type
);
3342 values
; values
= TREE_CHAIN (values
))
3344 tree decl
= TREE_VALUE (values
);
3345 tree value
= DECL_INITIAL (decl
);
3346 if (value
== error_mark_node
)
3347 value
= integer_zero_node
;
3349 minnode
= maxnode
= value
;
3350 else if (tree_int_cst_lt (maxnode
, value
))
3352 else if (tree_int_cst_lt (value
, minnode
))
3357 minnode
= maxnode
= integer_zero_node
;
3359 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
3360 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
3361 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
3362 prec
= MAX (lowprec
, highprec
);
3366 /* FIELD is a bit-field. We are finishing the processing for its
3367 enclosing type. Issue any appropriate messages and set appropriate
3368 flags. Returns false if an error has been diagnosed. */
3371 check_bitfield_decl (tree field
)
3373 tree type
= TREE_TYPE (field
);
3376 /* Extract the declared width of the bitfield, which has been
3377 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3378 w
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
3379 gcc_assert (w
!= NULL_TREE
);
3380 /* Remove the bit-field width indicator so that the rest of the
3381 compiler does not treat that value as a qualifier. */
3382 DECL_BIT_FIELD_REPRESENTATIVE (field
) = NULL_TREE
;
3384 /* Detect invalid bit-field type. */
3385 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3387 error_at (DECL_SOURCE_LOCATION (field
),
3388 "bit-field %q#D with non-integral type %qT", field
, type
);
3389 w
= error_mark_node
;
3393 location_t loc
= input_location
;
3394 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3397 /* detect invalid field size. */
3398 input_location
= DECL_SOURCE_LOCATION (field
);
3399 w
= cxx_constant_value (w
);
3400 input_location
= loc
;
3402 if (TREE_CODE (w
) != INTEGER_CST
)
3404 error ("bit-field %q+D width not an integer constant", field
);
3405 w
= error_mark_node
;
3407 else if (tree_int_cst_sgn (w
) < 0)
3409 error ("negative width in bit-field %q+D", field
);
3410 w
= error_mark_node
;
3412 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3414 error ("zero width for bit-field %q+D", field
);
3415 w
= error_mark_node
;
3417 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3418 && TREE_CODE (type
) != BOOLEAN_TYPE
3419 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3420 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3421 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3422 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3423 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3424 "width of %qD exceeds its type", field
);
3425 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3427 int prec
= enum_min_precision (type
);
3428 if (compare_tree_int (w
, prec
) < 0)
3429 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3430 "%qD is too small to hold all values of %q#T",
3435 if (w
!= error_mark_node
)
3437 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3438 DECL_BIT_FIELD (field
) = 1;
3443 /* Non-bit-fields are aligned for their type. */
3444 DECL_BIT_FIELD (field
) = 0;
3445 CLEAR_DECL_C_BIT_FIELD (field
);
3450 /* FIELD is a non bit-field. We are finishing the processing for its
3451 enclosing type T. Issue any appropriate messages and set appropriate
3455 check_field_decl (tree field
,
3457 int* cant_have_const_ctor
,
3458 int* no_const_asn_ref
)
3460 tree type
= strip_array_types (TREE_TYPE (field
));
3461 bool any_default_members
= false;
3463 /* In C++98 an anonymous union cannot contain any fields which would change
3464 the settings of CANT_HAVE_CONST_CTOR and friends. */
3465 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3467 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3468 structs. So, we recurse through their fields here. */
3469 else if (ANON_AGGR_TYPE_P (type
))
3471 for (tree fields
= TYPE_FIELDS (type
); fields
;
3472 fields
= DECL_CHAIN (fields
))
3473 if (TREE_CODE (fields
) == FIELD_DECL
)
3474 any_default_members
|= check_field_decl (fields
, t
,
3475 cant_have_const_ctor
,
3478 /* Check members with class type for constructors, destructors,
3480 else if (CLASS_TYPE_P (type
))
3482 /* Never let anything with uninheritable virtuals
3483 make it through without complaint. */
3484 abstract_virtuals_error (field
, type
);
3486 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3489 int oldcount
= errorcount
;
3490 if (TYPE_NEEDS_CONSTRUCTING (type
))
3491 error ("member %q+#D with constructor not allowed in union",
3493 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3494 error ("member %q+#D with destructor not allowed in union", field
);
3495 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3496 error ("member %q+#D with copy assignment operator not allowed in union",
3498 if (!warned
&& errorcount
> oldcount
)
3500 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3501 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3507 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3508 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3509 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3510 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3511 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3512 || !TYPE_HAS_COPY_ASSIGN (type
));
3513 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3514 || !TYPE_HAS_COPY_CTOR (type
));
3515 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3516 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3517 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3518 || TYPE_HAS_COMPLEX_DFLT (type
));
3521 if (TYPE_HAS_COPY_CTOR (type
)
3522 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3523 *cant_have_const_ctor
= 1;
3525 if (TYPE_HAS_COPY_ASSIGN (type
)
3526 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3527 *no_const_asn_ref
= 1;
3530 check_abi_tags (t
, field
);
3532 if (DECL_INITIAL (field
) != NULL_TREE
)
3533 /* `build_class_init_list' does not recognize
3535 any_default_members
= true;
3537 return any_default_members
;
3540 /* Check the data members (both static and non-static), class-scoped
3541 typedefs, etc., appearing in the declaration of T. Issue
3542 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3543 declaration order) of access declarations; each TREE_VALUE in this
3544 list is a USING_DECL.
3546 In addition, set the following flags:
3549 The class is empty, i.e., contains no non-static data members.
3551 CANT_HAVE_CONST_CTOR_P
3552 This class cannot have an implicitly generated copy constructor
3553 taking a const reference.
3555 CANT_HAVE_CONST_ASN_REF
3556 This class cannot have an implicitly generated assignment
3557 operator taking a const reference.
3559 All of these flags should be initialized before calling this
3563 check_field_decls (tree t
, tree
*access_decls
,
3564 int *cant_have_const_ctor_p
,
3565 int *no_const_asn_ref_p
)
3569 /* Assume there are no access declarations. */
3570 *access_decls
= NULL_TREE
;
3571 /* Effective C has things to say about classes with pointer members. */
3572 tree pointer_member
= NULL_TREE
;
3573 /* Default initialized members affect the whole class. */
3574 tree default_init_member
= NULL_TREE
;
3575 /* Lack of any non-static data member of non-volatile literal
3576 type affects a union. */
3577 bool found_nv_literal_p
= false;
3578 /* Standard layout requires all FIELDS have same access. */
3579 int field_access
= -1;
3581 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3583 tree type
= TREE_TYPE (field
);
3585 switch (TREE_CODE (field
))
3591 /* Save the access declarations for our caller. */
3592 *access_decls
= tree_cons (NULL_TREE
, field
, *access_decls
);
3600 /* FIXME: We should fold in the checking from check_methods. */
3604 DECL_NONLOCAL (field
) = 1;
3608 if (TREE_CODE (t
) == UNION_TYPE
3609 && cxx_dialect
< cxx11
)
3613 (C++98) If a union contains a static data member,
3614 ... the program is ill-formed. */
3615 if (cxx_dialect
< cxx11
)
3616 error ("in C++98 %q+D may not be static because it is "
3617 "a member of a union", field
);
3622 if (TREE_CODE (t
) == UNION_TYPE
)
3626 If a union contains ... or a [non-static data] member
3627 of reference type, the program is ill-formed. */
3628 if (TYPE_REF_P (type
))
3629 error ("non-static data member %q+D in a union may not "
3630 "have reference type %qT", field
, type
);
3634 /* Common VAR_DECL & FIELD_DECL processing. */
3635 DECL_CONTEXT (field
) = t
;
3636 DECL_NONLOCAL (field
) = 1;
3638 /* Template instantiation can cause this. Perhaps this
3639 should be a specific instantiation check? */
3640 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3642 error ("data member %q+D invalidly declared function type", field
);
3643 type
= build_pointer_type (type
);
3644 TREE_TYPE (field
) = type
;
3646 else if (TREE_CODE (type
) == METHOD_TYPE
)
3648 error ("data member %q+D invalidly declared method type", field
);
3649 type
= build_pointer_type (type
);
3650 TREE_TYPE (field
) = type
;
3656 if (TREE_CODE (field
) != FIELD_DECL
)
3659 if (type
== error_mark_node
)
3662 /* If it is not a union and at least one non-static data member is
3663 non-literal, the whole class becomes non-literal. Per Core/1453,
3664 volatile non-static data members and base classes are also not allowed.
3665 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3667 Note: if the type is incomplete we will complain later on. */
3668 if (COMPLETE_TYPE_P (type
))
3670 if (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
))
3671 CLASSTYPE_LITERAL_P (t
) = false;
3673 found_nv_literal_p
= true;
3676 int this_field_access
= (TREE_PROTECTED (field
) ? 1
3677 : TREE_PRIVATE (field
) ? 2 : 0);
3678 if (field_access
!= this_field_access
)
3680 /* A standard-layout class is a class that:
3682 ... has the same access control (Clause 11) for all
3683 non-static data members, */
3684 if (field_access
< 0)
3685 field_access
= this_field_access
;
3687 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3689 /* Aggregates must be public. */
3690 if (this_field_access
)
3691 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3694 /* If this is of reference type, check if it needs an init. */
3695 if (TYPE_REF_P (type
))
3697 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3698 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3699 if (DECL_INITIAL (field
) == NULL_TREE
)
3700 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3701 if (cxx_dialect
< cxx11
)
3703 /* ARM $12.6.2: [A member initializer list] (or, for an
3704 aggregate, initialization by a brace-enclosed list) is the
3705 only way to initialize non-static const and reference
3707 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3708 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3712 type
= strip_array_types (type
);
3714 if (TYPE_PACKED (t
))
3716 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3718 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3719 "ignoring packed attribute because of"
3720 " unpacked non-POD field %q#D", field
);
3723 else if (DECL_C_BIT_FIELD (field
)
3724 || TYPE_ALIGN (TREE_TYPE (field
)) > BITS_PER_UNIT
)
3725 DECL_PACKED (field
) = 1;
3728 if (DECL_C_BIT_FIELD (field
)
3729 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field
)))
3730 /* We don't treat zero-width bitfields as making a class
3733 else if (field_poverlapping_p (field
)
3734 && is_empty_class (TREE_TYPE (field
)))
3735 /* Empty data members also don't make a class non-empty. */
3736 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3739 /* The class is non-empty. */
3740 CLASSTYPE_EMPTY_P (t
) = 0;
3741 /* The class is not even nearly empty. */
3742 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3743 /* If one of the data members contains an empty class, so
3745 if (CLASS_TYPE_P (type
)
3746 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3747 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3750 /* This is used by -Weffc++ (see below). Warn only for pointers
3751 to members which might hold dynamic memory. So do not warn
3752 for pointers to functions or pointers to members. */
3753 if (TYPE_PTR_P (type
)
3754 && !TYPE_PTRFN_P (type
))
3755 pointer_member
= field
;
3757 if (CLASS_TYPE_P (type
))
3759 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3760 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3761 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3762 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3765 if (DECL_MUTABLE_P (field
) || TYPE_HAS_MUTABLE_P (type
))
3766 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3768 if (DECL_MUTABLE_P (field
))
3770 if (TYPE_REF_P (type
))
3771 error ("member %q+D cannot be declared as a %<mutable%> "
3772 "reference", field
);
3773 else if (CP_TYPE_CONST_P (type
))
3774 error ("member %q+D cannot be declared both %<const%> "
3775 "and %<mutable%>", field
);
3778 if (! layout_pod_type_p (type
))
3779 /* DR 148 now allows pointers to members (which are POD themselves),
3780 to be allowed in POD structs. */
3781 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3783 if (field_poverlapping_p (field
))
3784 /* A potentially-overlapping non-static data member makes the class
3786 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3788 if (!std_layout_type_p (type
))
3789 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3791 if (! zero_init_p (type
))
3792 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3794 /* We set DECL_C_BIT_FIELD in grokbitfield.
3795 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3796 if (DECL_C_BIT_FIELD (field
))
3797 check_bitfield_decl (field
);
3799 if (check_field_decl (field
, t
,
3800 cant_have_const_ctor_p
, no_const_asn_ref_p
))
3802 if (default_init_member
3803 && TREE_CODE (t
) == UNION_TYPE
)
3805 error ("multiple fields in union %qT initialized", t
);
3806 inform (DECL_SOURCE_LOCATION (default_init_member
),
3807 "initialized member %q+D declared here",
3808 default_init_member
);
3810 default_init_member
= field
;
3813 /* Now that we've removed bit-field widths from DECL_INITIAL,
3814 anything left in DECL_INITIAL is an NSDMI that makes the class
3815 non-aggregate in C++11. */
3816 if (DECL_INITIAL (field
) && cxx_dialect
< cxx14
)
3817 CLASSTYPE_NON_AGGREGATE (t
) = true;
3819 if (CP_TYPE_CONST_P (type
))
3821 /* If any field is const, the structure type is pseudo-const. */
3822 C_TYPE_FIELDS_READONLY (t
) = 1;
3823 if (DECL_INITIAL (field
) == NULL_TREE
)
3824 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3825 if (cxx_dialect
< cxx11
)
3827 /* ARM $12.6.2: [A member initializer list] (or, for an
3828 aggregate, initialization by a brace-enclosed list) is the
3829 only way to initialize non-static const and reference
3831 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3832 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3835 /* A field that is pseudo-const makes the structure likewise. */
3836 else if (CLASS_TYPE_P (type
))
3838 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3839 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3840 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3841 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3844 /* Core issue 80: A non-static data member is required to have a
3845 different name from the class iff the class has a
3846 user-declared constructor. */
3847 if (constructor_name_p (DECL_NAME (field
), t
)
3848 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3849 permerror (DECL_SOURCE_LOCATION (field
),
3850 "field %q#D with same name as class", field
);
3853 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3854 one of its non-static data members is of non-volatile literal type. */
3855 if (TREE_CODE (t
) == UNION_TYPE
&& found_nv_literal_p
)
3856 CLASSTYPE_LITERAL_P (t
) = true;
3858 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3859 it should also define a copy constructor and an assignment operator to
3860 implement the correct copy semantic (deep vs shallow, etc.). As it is
3861 not feasible to check whether the constructors do allocate dynamic memory
3862 and store it within members, we approximate the warning like this:
3864 -- Warn only if there are members which are pointers
3865 -- Warn only if there is a non-trivial constructor (otherwise,
3866 there cannot be memory allocated).
3867 -- Warn only if there is a non-trivial destructor. We assume that the
3868 user at least implemented the cleanup correctly, and a destructor
3869 is needed to free dynamic memory.
3871 This seems enough for practical purposes. */
3874 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3875 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3876 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3878 if (warning (OPT_Weffc__
, "%q#T has pointer data members", t
))
3880 if (! TYPE_HAS_COPY_CTOR (t
))
3882 warning (OPT_Weffc__
,
3883 " but does not declare %<%T(const %T&)%>", t
, t
);
3884 if (!TYPE_HAS_COPY_ASSIGN (t
))
3885 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3887 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3888 warning (OPT_Weffc__
,
3889 " but does not declare %<operator=(const %T&)%>", t
);
3890 inform (DECL_SOURCE_LOCATION (pointer_member
),
3891 "pointer member %q+D declared here", pointer_member
);
3895 /* Non-static data member initializers make the default constructor
3897 if (default_init_member
)
3899 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3900 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3903 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3905 TYPE_PACKED (t
) = 0;
3907 /* Check anonymous struct/anonymous union fields. */
3908 finish_struct_anon (t
);
3910 /* We've built up the list of access declarations in reverse order.
3912 *access_decls
= nreverse (*access_decls
);
3915 /* If TYPE is an empty class type, records its OFFSET in the table of
3919 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3923 if (!is_empty_class (type
))
3926 /* Record the location of this empty object in OFFSETS. */
3927 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3929 n
= splay_tree_insert (offsets
,
3930 (splay_tree_key
) offset
,
3931 (splay_tree_value
) NULL_TREE
);
3932 n
->value
= ((splay_tree_value
)
3933 tree_cons (NULL_TREE
,
3940 /* Returns nonzero if TYPE is an empty class type and there is
3941 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3944 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3949 if (!is_empty_class (type
))
3952 /* Record the location of this empty object in OFFSETS. */
3953 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3957 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3958 if (same_type_p (TREE_VALUE (t
), type
))
3964 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3965 F for every subobject, passing it the type, offset, and table of
3966 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3969 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3970 than MAX_OFFSET will not be walked.
3972 If F returns a nonzero value, the traversal ceases, and that value
3973 is returned. Otherwise, returns zero. */
3976 walk_subobject_offsets (tree type
,
3977 subobject_offset_fn f
,
3984 tree type_binfo
= NULL_TREE
;
3986 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3988 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3991 if (type
== error_mark_node
)
3997 type
= BINFO_TYPE (type
);
4000 if (CLASS_TYPE_P (type
))
4006 /* Avoid recursing into objects that are not interesting. */
4007 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
4010 /* Record the location of TYPE. */
4011 r
= (*f
) (type
, offset
, offsets
);
4015 /* Iterate through the direct base classes of TYPE. */
4017 type_binfo
= TYPE_BINFO (type
);
4018 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
4022 if (BINFO_VIRTUAL_P (binfo
))
4026 /* We cannot rely on BINFO_OFFSET being set for the base
4027 class yet, but the offsets for direct non-virtual
4028 bases can be calculated by going back to the TYPE. */
4029 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
4030 binfo_offset
= size_binop (PLUS_EXPR
,
4032 BINFO_OFFSET (orig_binfo
));
4034 r
= walk_subobject_offsets (binfo
,
4044 if (CLASSTYPE_VBASECLASSES (type
))
4047 vec
<tree
, va_gc
> *vbases
;
4049 /* Iterate through the virtual base classes of TYPE. In G++
4050 3.2, we included virtual bases in the direct base class
4051 loop above, which results in incorrect results; the
4052 correct offsets for virtual bases are only known when
4053 working with the most derived type. */
4055 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
4056 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
4058 r
= walk_subobject_offsets (binfo
,
4060 size_binop (PLUS_EXPR
,
4062 BINFO_OFFSET (binfo
)),
4071 /* We still have to walk the primary base, if it is
4072 virtual. (If it is non-virtual, then it was walked
4074 tree vbase
= get_primary_binfo (type_binfo
);
4076 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
4077 && BINFO_PRIMARY_P (vbase
)
4078 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
4080 r
= (walk_subobject_offsets
4082 offsets
, max_offset
, /*vbases_p=*/0));
4089 /* Iterate through the fields of TYPE. */
4090 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4091 if (TREE_CODE (field
) == FIELD_DECL
4092 && TREE_TYPE (field
) != error_mark_node
4093 && !DECL_ARTIFICIAL (field
))
4097 field_offset
= byte_position (field
);
4099 r
= walk_subobject_offsets (TREE_TYPE (field
),
4101 size_binop (PLUS_EXPR
,
4111 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4113 tree element_type
= strip_array_types (type
);
4114 tree domain
= TYPE_DOMAIN (type
);
4117 /* Avoid recursing into objects that are not interesting. */
4118 if (!CLASS_TYPE_P (element_type
)
4119 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
4121 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
4124 /* Step through each of the elements in the array. */
4125 for (index
= size_zero_node
;
4126 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
4127 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
4129 r
= walk_subobject_offsets (TREE_TYPE (type
),
4137 offset
= size_binop (PLUS_EXPR
, offset
,
4138 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
4139 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4140 there's no point in iterating through the remaining
4141 elements of the array. */
4142 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4150 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4153 field_poverlapping_p (tree decl
)
4155 /* Base fields are actually potentially overlapping, but C++ bases go through
4156 a different code path based on binfos, and ObjC++ base fields are laid out
4157 in objc-act, so we don't want layout_class_type to mess with them. */
4158 if (DECL_FIELD_IS_BASE (decl
))
4160 gcc_checking_assert (c_dialect_objc ());
4164 return lookup_attribute ("no_unique_address",
4165 DECL_ATTRIBUTES (decl
));
4168 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4171 record_subobject_offsets (tree decl_or_binfo
,
4175 bool overlapping
, vbases_p
;
4177 if (DECL_P (decl_or_binfo
))
4179 tree decl
= decl_or_binfo
;
4180 type
= TREE_TYPE (decl
);
4181 offset
= byte_position (decl
);
4182 overlapping
= field_poverlapping_p (decl
);
4187 type
= BINFO_TYPE (decl_or_binfo
);
4188 offset
= BINFO_OFFSET (decl_or_binfo
);
4194 /* If recording subobjects for a non-static data member or a
4195 non-empty base class, we do not need to record offsets beyond
4196 the size of the biggest empty class. Additional data members
4197 will go at the end of the class. Additional base classes will go
4198 either at offset zero (if empty, in which case they cannot
4199 overlap with offsets past the size of the biggest empty class) or
4200 at the end of the class.
4202 However, if we are placing an empty base class, then we must record
4203 all offsets, as either the empty class is at offset zero (where
4204 other empty classes might later be placed) or at the end of the
4205 class (where other objects might then be placed, so other empty
4206 subobjects might later overlap). */
4208 || !is_empty_class (type
))
4209 max_offset
= sizeof_biggest_empty_class
;
4211 max_offset
= NULL_TREE
;
4212 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4213 offsets
, max_offset
, vbases_p
);
4216 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4217 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4218 virtual bases of TYPE are examined. */
4221 layout_conflict_p (tree type
,
4226 splay_tree_node max_node
;
4228 /* Get the node in OFFSETS that indicates the maximum offset where
4229 an empty subobject is located. */
4230 max_node
= splay_tree_max (offsets
);
4231 /* If there aren't any empty subobjects, then there's no point in
4232 performing this check. */
4236 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4237 offsets
, (tree
) (max_node
->key
),
4241 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4242 non-static data member of the type indicated by RLI. BINFO is the
4243 binfo corresponding to the base subobject, OFFSETS maps offsets to
4244 types already located at those offsets. This function determines
4245 the position of the DECL. */
4248 layout_nonempty_base_or_field (record_layout_info rli
,
4253 tree offset
= NULL_TREE
;
4259 /* For the purposes of determining layout conflicts, we want to
4260 use the class type of BINFO; TREE_TYPE (DECL) will be the
4261 CLASSTYPE_AS_BASE version, which does not contain entries for
4262 zero-sized bases. */
4263 type
= TREE_TYPE (binfo
);
4268 type
= TREE_TYPE (decl
);
4272 /* Try to place the field. It may take more than one try if we have
4273 a hard time placing the field without putting two objects of the
4274 same type at the same address. */
4277 struct record_layout_info_s old_rli
= *rli
;
4279 /* Place this field. */
4280 place_field (rli
, decl
);
4281 offset
= byte_position (decl
);
4283 /* We have to check to see whether or not there is already
4284 something of the same type at the offset we're about to use.
4285 For example, consider:
4288 struct T : public S { int i; };
4289 struct U : public S, public T {};
4291 Here, we put S at offset zero in U. Then, we can't put T at
4292 offset zero -- its S component would be at the same address
4293 as the S we already allocated. So, we have to skip ahead.
4294 Since all data members, including those whose type is an
4295 empty class, have nonzero size, any overlap can happen only
4296 with a direct or indirect base-class -- it can't happen with
4298 /* In a union, overlap is permitted; all members are placed at
4300 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4302 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4305 /* Strip off the size allocated to this field. That puts us
4306 at the first place we could have put the field with
4307 proper alignment. */
4310 /* Bump up by the alignment required for the type. */
4312 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4314 ? CLASSTYPE_ALIGN (type
)
4315 : TYPE_ALIGN (type
)));
4316 normalize_rli (rli
);
4318 else if (TREE_CODE (type
) == NULLPTR_TYPE
4319 && warn_abi
&& abi_version_crosses (9))
4321 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4322 the offset wasn't aligned like a pointer when we started to
4323 layout this field, that affects its position. */
4324 tree pos
= rli_size_unit_so_far (&old_rli
);
4325 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4327 if (abi_version_at_least (9))
4328 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4329 "alignment of %qD increased in %<-fabi-version=9%> "
4332 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4333 "of %qD will increase in %<-fabi-version=9%>",
4339 /* There was no conflict. We're done laying out this field. */
4343 /* Now that we know where it will be placed, update its
4345 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4346 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4347 this point because their BINFO_OFFSET is copied from another
4348 hierarchy. Therefore, we may not need to add the entire
4350 propagate_binfo_offsets (binfo
,
4351 size_diffop_loc (input_location
,
4352 fold_convert (ssizetype
, offset
),
4353 fold_convert (ssizetype
,
4354 BINFO_OFFSET (binfo
))));
4357 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4360 empty_base_at_nonzero_offset_p (tree type
,
4362 splay_tree
/*offsets*/)
4364 return is_empty_class (type
) && !integer_zerop (offset
);
4367 /* Layout the empty base BINFO. EOC indicates the byte currently just
4368 past the end of the class, and should be correctly aligned for a
4369 class of the type indicated by BINFO; OFFSETS gives the offsets of
4370 the empty bases allocated so far. T is the most derived
4371 type. Return nonzero iff we added it at the end. */
4374 layout_empty_base_or_field (record_layout_info rli
, tree binfo_or_decl
,
4379 tree binfo
= NULL_TREE
;
4380 tree decl
= NULL_TREE
;
4382 if (TREE_CODE (binfo_or_decl
) == TREE_BINFO
)
4384 binfo
= binfo_or_decl
;
4385 type
= BINFO_TYPE (binfo
);
4389 decl
= binfo_or_decl
;
4390 type
= TREE_TYPE (decl
);
4393 /* On some platforms (ARM), even empty classes will not be
4395 tree eoc
= round_up_loc (input_location
,
4396 rli_size_unit_so_far (rli
),
4397 CLASSTYPE_ALIGN_UNIT (type
));
4399 /* This routine should only be used for empty classes. */
4400 gcc_assert (is_empty_class (type
));
4402 if (decl
&& DECL_USER_ALIGN (decl
))
4403 alignment
= size_int (DECL_ALIGN_UNIT (decl
));
4405 alignment
= size_int (CLASSTYPE_ALIGN_UNIT (type
));
4407 /* This is an empty base class. We first try to put it at offset
4409 tree offset
= size_zero_node
;
4410 if (TREE_CODE (rli
->t
) != UNION_TYPE
4411 && layout_conflict_p (type
,
4416 /* That didn't work. Now, we move forward from the next
4417 available spot in the class. */
4422 if (!layout_conflict_p (type
,
4426 /* We finally found a spot where there's no overlap. */
4429 /* There's overlap here, too. Bump along to the next spot. */
4430 offset
= size_binop (PLUS_EXPR
, offset
, alignment
);
4434 if (decl
&& DECL_USER_ALIGN (decl
))
4436 rli
->record_align
= MAX (rli
->record_align
, DECL_ALIGN (decl
));
4438 rli
->unpacked_align
= MAX (rli
->unpacked_align
, DECL_ALIGN (decl
));
4439 TYPE_USER_ALIGN (rli
->t
) = 1;
4441 else if (CLASSTYPE_USER_ALIGN (type
))
4443 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (type
));
4445 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (type
));
4446 TYPE_USER_ALIGN (rli
->t
) = 1;
4450 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4451 propagate_binfo_offsets (binfo
,
4452 size_diffop (offset
, BINFO_OFFSET (binfo
)));
4455 DECL_FIELD_OFFSET (decl
) = offset
;
4456 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4457 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4463 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4464 fields at NEXT_FIELD, and return it. */
4467 build_base_field_1 (tree t
, tree binfo
, tree access
, tree
*&next_field
)
4469 /* Create the FIELD_DECL. */
4470 tree basetype
= BINFO_TYPE (binfo
);
4471 tree as_base
= CLASSTYPE_AS_BASE (basetype
);
4472 gcc_assert (as_base
);
4473 tree decl
= build_decl (input_location
, FIELD_DECL
, NULL_TREE
, as_base
);
4475 DECL_ARTIFICIAL (decl
) = 1;
4476 DECL_IGNORED_P (decl
) = 1;
4477 DECL_FIELD_CONTEXT (decl
) = t
;
4478 if (is_empty_class (basetype
))
4479 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4480 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = size_zero_node
;
4483 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4484 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4486 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4487 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4488 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4489 DECL_FIELD_IS_BASE (decl
) = 1;
4491 if (access
== access_private_node
)
4492 TREE_PRIVATE (decl
) = true;
4493 else if (access
== access_protected_node
)
4494 TREE_PROTECTED (decl
) = true;
4496 /* Add the new FIELD_DECL to the list of fields for T. */
4497 DECL_CHAIN (decl
) = *next_field
;
4499 next_field
= &DECL_CHAIN (decl
);
4504 /* Layout the base given by BINFO in the class indicated by RLI.
4505 *BASE_ALIGN is a running maximum of the alignments of
4506 any base class. OFFSETS gives the location of empty base
4507 subobjects. T is the most derived type. Return nonzero if the new
4508 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4509 *NEXT_FIELD, unless BINFO is for an empty base class.
4511 Returns the location at which the next field should be inserted. */
4514 build_base_field (record_layout_info rli
, tree binfo
, tree access
,
4515 splay_tree offsets
, tree
*next_field
)
4518 tree basetype
= BINFO_TYPE (binfo
);
4520 if (!COMPLETE_TYPE_P (basetype
))
4521 /* This error is now reported in xref_tag, thus giving better
4522 location information. */
4525 /* Place the base class. */
4526 if (!is_empty_class (basetype
))
4530 /* The containing class is non-empty because it has a non-empty
4532 CLASSTYPE_EMPTY_P (t
) = 0;
4534 /* Create the FIELD_DECL. */
4535 decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4537 /* Try to place the field. It may take more than one try if we
4538 have a hard time placing the field without putting two
4539 objects of the same type at the same address. */
4540 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4544 bool atend
= layout_empty_base_or_field (rli
, binfo
, offsets
);
4545 /* A nearly-empty class "has no proper base class that is empty,
4546 not morally virtual, and at an offset other than zero." */
4547 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4550 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4551 /* The check above (used in G++ 3.2) is insufficient because
4552 an empty class placed at offset zero might itself have an
4553 empty base at a nonzero offset. */
4554 else if (walk_subobject_offsets (basetype
,
4555 empty_base_at_nonzero_offset_p
,
4558 /*max_offset=*/NULL_TREE
,
4560 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4563 /* We used to not create a FIELD_DECL for empty base classes because of
4564 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4565 be a problem anymore. We need them to handle initialization of C++17
4567 if (cxx_dialect
>= cxx17
&& !BINFO_VIRTUAL_P (binfo
))
4569 tree decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4570 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4571 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4572 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4573 DECL_FIELD_ABI_IGNORED (decl
) = 1;
4576 /* An empty virtual base causes a class to be non-empty
4577 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4578 here because that was already done when the virtual table
4579 pointer was created. */
4582 /* Record the offsets of BINFO and its base subobjects. */
4583 record_subobject_offsets (binfo
, offsets
);
4588 /* Layout all of the non-virtual base classes. Record empty
4589 subobjects in OFFSETS. T is the most derived type. Return nonzero
4590 if the type cannot be nearly empty. The fields created
4591 corresponding to the base classes will be inserted at
4595 build_base_fields (record_layout_info rli
,
4596 splay_tree offsets
, tree
*next_field
)
4598 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4601 tree binfo
= TYPE_BINFO (t
);
4602 int n_baseclasses
= BINFO_N_BASE_BINFOS (binfo
);
4604 /* The primary base class is always allocated first. */
4605 const tree primary_binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
4608 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4609 base, if it is direct. Indirect base fields are private. */
4610 tree primary_access
= access_private_node
;
4611 for (int i
= 0; i
< n_baseclasses
; ++i
)
4613 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4614 if (base_binfo
== primary_binfo
)
4616 primary_access
= BINFO_BASE_ACCESS (binfo
, i
);
4620 next_field
= build_base_field (rli
, primary_binfo
,
4622 offsets
, next_field
);
4625 /* Now allocate the rest of the bases. */
4626 for (int i
= 0; i
< n_baseclasses
; ++i
)
4628 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4630 /* The primary base was already allocated above, so we don't
4631 need to allocate it again here. */
4632 if (base_binfo
== primary_binfo
)
4635 /* Virtual bases are added at the end (a primary virtual base
4636 will have already been added). */
4637 if (BINFO_VIRTUAL_P (base_binfo
))
4640 next_field
= build_base_field (rli
, base_binfo
,
4641 BINFO_BASE_ACCESS (binfo
, i
),
4642 offsets
, next_field
);
4646 /* Go through the TYPE_FIELDS of T issuing any appropriate
4647 diagnostics, figuring out which methods override which other
4648 methods, and so forth. */
4651 check_methods (tree t
)
4653 for (tree x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
4654 if (DECL_DECLARES_FUNCTION_P (x
))
4656 check_for_override (x
, t
);
4658 if (DECL_PURE_VIRTUAL_P (x
)
4659 && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4660 error ("initializer specified for non-virtual method %q+D", x
);
4661 /* The name of the field is the original field name
4662 Save this in auxiliary field for later overloading. */
4663 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4665 TYPE_POLYMORPHIC_P (t
) = 1;
4666 if (DECL_PURE_VIRTUAL_P (x
))
4667 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4670 if (!DECL_VIRTUAL_P (x
)
4671 && lookup_attribute ("transaction_safe_dynamic",
4672 DECL_ATTRIBUTES (x
)))
4673 error_at (DECL_SOURCE_LOCATION (x
),
4674 "%<transaction_safe_dynamic%> may only be specified for "
4675 "a virtual function");
4678 /* Check whether the eligible special member functions (P0848) are
4679 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4680 has the eligible ones; TYPE_FIELDS also contains ineligible overloads,
4681 which is why this needs to be separate from the loop above. */
4683 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
4685 if (TREE_CODE (dtor
) == OVERLOAD
)
4687 /* P0848: At the end of the definition of a class, overload
4688 resolution is performed among the prospective destructors declared
4689 in that class with an empty argument list to select the destructor
4690 for the class, also known as the selected destructor. The program
4691 is ill-formed if overload resolution fails. */
4692 auto_diagnostic_group d
;
4693 error_at (location_of (t
), "destructor for %qT is ambiguous", t
);
4694 print_candidates (dtor
);
4696 else if (user_provided_p (dtor
))
4697 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = true;
4700 for (ovl_iterator
i (CLASSTYPE_CONSTRUCTORS (t
)); i
; ++i
)
4703 if (!user_provided_p (fn
))
4704 /* Might be trivial. */;
4705 else if (copy_fn_p (fn
))
4706 TYPE_HAS_COMPLEX_COPY_CTOR (t
) = true;
4707 else if (move_fn_p (fn
))
4708 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) = true;
4711 for (ovl_iterator
i (get_class_binding_direct (t
, assign_op_identifier
));
4715 if (!user_provided_p (fn
))
4716 /* Might be trivial. */;
4717 else if (copy_fn_p (fn
))
4718 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = true;
4719 else if (move_fn_p (fn
))
4720 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = true;
4724 /* FN is constructor, destructor or operator function. Clone the
4725 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4726 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4729 copy_fndecl_with_name (tree fn
, tree name
, tree_code code
,
4730 bool need_vtt_parm_p
, bool omit_inherited_parms_p
)
4732 /* Copy the function. */
4733 tree clone
= copy_decl (fn
);
4734 /* Reset the function name. */
4735 DECL_NAME (clone
) = name
;
4738 /* Clone constraints. */
4739 if (tree ci
= get_constraints (fn
))
4740 set_constraints (clone
, copy_node (ci
));
4742 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4743 /* There's no pending inline data for this function. */
4744 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4745 DECL_PENDING_INLINE_P (clone
) = 0;
4747 if (name
== base_dtor_identifier
)
4749 /* The base-class destructor is not virtual. */
4750 DECL_VIRTUAL_P (clone
) = 0;
4751 DECL_VINDEX (clone
) = NULL_TREE
;
4753 else if (code
!= ERROR_MARK
)
4755 /* Set the operator code. */
4756 const ovl_op_info_t
*ovl_op
= OVL_OP_INFO (false, code
);
4757 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone
) = ovl_op
->ovl_op_code
;
4759 /* The operator could be virtual. */
4760 if (DECL_VIRTUAL_P (clone
))
4761 IDENTIFIER_VIRTUAL_P (name
) = true;
4764 if (omit_inherited_parms_p
)
4765 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone
));
4767 /* If there was an in-charge parameter, drop it from the function
4769 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4771 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4772 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4773 /* Skip the `this' parameter. */
4774 parmtypes
= TREE_CHAIN (parmtypes
);
4775 /* Skip the in-charge parameter. */
4776 parmtypes
= TREE_CHAIN (parmtypes
);
4777 /* And the VTT parm, in a complete [cd]tor. */
4778 if (DECL_HAS_VTT_PARM_P (fn
) && !need_vtt_parm_p
)
4779 parmtypes
= TREE_CHAIN (parmtypes
);
4780 if (omit_inherited_parms_p
)
4782 /* If we're omitting inherited parms, that just leaves the VTT. */
4783 gcc_assert (need_vtt_parm_p
);
4784 parmtypes
= tree_cons (NULL_TREE
, vtt_parm_type
, void_list_node
);
4787 = build_method_type_directly (basetype
,
4788 TREE_TYPE (TREE_TYPE (clone
)),
4791 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4792 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4794 = cxx_copy_lang_qualifiers (TREE_TYPE (clone
), TREE_TYPE (fn
));
4797 /* Copy the function parameters. */
4798 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4800 /* Remove the in-charge parameter. */
4801 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4803 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4804 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4805 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4808 /* And the VTT parm, in a complete [cd]tor. */
4809 if (DECL_HAS_VTT_PARM_P (fn
))
4811 if (need_vtt_parm_p
)
4812 DECL_HAS_VTT_PARM_P (clone
) = 1;
4815 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4816 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4817 DECL_HAS_VTT_PARM_P (clone
) = 0;
4821 /* A base constructor inheriting from a virtual base doesn't get the
4823 if (omit_inherited_parms_p
)
4824 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
4826 for (tree parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4828 DECL_CONTEXT (parms
) = clone
;
4829 cxx_dup_lang_specific_decl (parms
);
4832 /* Create the RTL for this function. */
4833 SET_DECL_RTL (clone
, NULL
);
4834 rest_of_decl_compilation (clone
, namespace_bindings_p (), at_eof
);
4839 /* FN is an operator function, create a variant for CODE. */
4842 copy_operator_fn (tree fn
, tree_code code
)
4844 return copy_fndecl_with_name (fn
, ovl_op_identifier (code
),
4845 code
, false, false);
4848 /* FN is a constructor or destructor. Clone the declaration to create
4849 a specialized in-charge or not-in-charge version, as indicated by
4853 build_clone (tree fn
, tree name
, bool need_vtt_parm_p
,
4854 bool omit_inherited_parms_p
)
4858 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4859 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4861 clone
= copy_decl (fn
);
4862 DECL_NAME (clone
) = name
;
4864 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
,
4865 need_vtt_parm_p
, omit_inherited_parms_p
);
4866 DECL_TEMPLATE_RESULT (clone
) = result
;
4868 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4869 DECL_TI_TEMPLATE (result
) = clone
;
4871 TREE_TYPE (clone
) = TREE_TYPE (result
);
4875 clone
= copy_fndecl_with_name (fn
, name
, ERROR_MARK
,
4876 need_vtt_parm_p
, omit_inherited_parms_p
);
4877 DECL_CLONED_FUNCTION (clone
) = fn
;
4880 /* Remember where this function came from. */
4881 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4883 /* Make it easy to find the CLONE given the FN. Note the
4884 template_result of a template will be chained this way too. */
4885 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4886 DECL_CHAIN (fn
) = clone
;
4891 /* Build the clones of FN, return the number of clones built. These
4892 will be inserted onto DECL_CHAIN of FN. */
4895 build_cdtor_clones (tree fn
, bool needs_vtt_parm_p
, bool omit_inherited_parms_p
)
4899 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4901 /* For each constructor, we need two variants: an in-charge version
4902 and a not-in-charge version. */
4903 build_clone (fn
, complete_ctor_identifier
, false, false);
4904 build_clone (fn
, base_ctor_identifier
, needs_vtt_parm_p
,
4905 omit_inherited_parms_p
);
4910 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4912 /* For each destructor, we need three variants: an in-charge
4913 version, a not-in-charge version, and an in-charge deleting
4914 version. We clone the deleting version first because that
4915 means it will go second on the TYPE_FIELDS list -- and that
4916 corresponds to the correct layout order in the virtual
4919 For a non-virtual destructor, we do not build a deleting
4921 if (DECL_VIRTUAL_P (fn
))
4923 build_clone (fn
, deleting_dtor_identifier
, false, false);
4926 build_clone (fn
, complete_dtor_identifier
, false, false);
4927 build_clone (fn
, base_dtor_identifier
, needs_vtt_parm_p
, false);
4934 /* Produce declarations for all appropriate clones of FN. If
4935 UPDATE_METHODS is true, the clones are added to the
4936 CLASSTYPE_MEMBER_VEC. */
4939 clone_cdtor (tree fn
, bool update_methods
)
4941 /* Avoid inappropriate cloning. */
4943 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4946 /* Base cdtors need a vtt parm if there are virtual bases. */
4947 bool vtt
= CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
));
4949 /* Base ctor omits inherited parms it needs a vttparm and inherited
4950 from a virtual nase ctor. */
4951 bool omit_inherited
= ctor_omit_inherited_parms (fn
);
4953 unsigned count
= build_cdtor_clones (fn
, vtt
, omit_inherited
);
4955 /* Note that this is an abstract function that is never emitted. */
4956 DECL_ABSTRACT_P (fn
) = true;
4959 for (tree clone
= fn
; count
--;)
4961 clone
= DECL_CHAIN (clone
);
4962 add_method (DECL_CONTEXT (clone
), clone
, false);
4966 /* DECL is an in charge constructor, which is being defined. This will
4967 have had an in class declaration, from whence clones were
4968 declared. An out-of-class definition can specify additional default
4969 arguments. As it is the clones that are involved in overload
4970 resolution, we must propagate the information from the DECL to its
4974 adjust_clone_args (tree decl
)
4978 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4979 clone
= DECL_CHAIN (clone
))
4981 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4982 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4983 tree decl_parms
, clone_parms
;
4985 /* Skip the 'this' parameter. */
4986 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4987 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4989 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4990 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4991 if (DECL_HAS_VTT_PARM_P (decl
))
4992 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4994 clone_parms
= orig_clone_parms
;
4995 if (DECL_HAS_VTT_PARM_P (clone
))
4996 clone_parms
= TREE_CHAIN (clone_parms
);
4998 for (decl_parms
= orig_decl_parms
; decl_parms
;
4999 decl_parms
= TREE_CHAIN (decl_parms
),
5000 clone_parms
= TREE_CHAIN (clone_parms
))
5002 if (clone_parms
== void_list_node
)
5004 gcc_assert (decl_parms
== clone_parms
5005 || ctor_omit_inherited_parms (clone
));
5009 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms
),
5010 TREE_VALUE (clone_parms
)));
5012 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
5014 /* A default parameter has been added. Adjust the
5015 clone's parameters. */
5016 clone_parms
= orig_decl_parms
;
5018 if (DECL_HAS_VTT_PARM_P (clone
))
5020 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
5021 TREE_VALUE (orig_clone_parms
),
5023 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
5026 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
5028 = build_method_type_directly (basetype
,
5029 TREE_TYPE (TREE_TYPE (clone
)),
5031 if (tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
)))
5032 type
= cp_build_type_attribute_variant (type
, attrs
);
5033 type
= cxx_copy_lang_qualifiers (type
, TREE_TYPE (clone
));
5034 TREE_TYPE (clone
) = type
;
5036 clone_parms
= NULL_TREE
;
5040 gcc_assert (!clone_parms
|| clone_parms
== void_list_node
);
5044 /* For each of the constructors and destructors in T, create an
5045 in-charge and not-in-charge variant. */
5048 clone_constructors_and_destructors (tree t
)
5050 /* While constructors can be via a using declaration, at this point
5051 we no longer need to know that. */
5052 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5053 clone_cdtor (*iter
, /*update_methods=*/true);
5055 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5056 clone_cdtor (dtor
, /*update_methods=*/true);
5059 /* Deduce noexcept for a destructor DTOR. */
5062 deduce_noexcept_on_destructor (tree dtor
)
5064 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
5065 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
),
5066 noexcept_deferred_spec
);
5069 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5070 of TYPE for virtual functions which FNDECL overrides. Return a
5071 mask of the tm attributes found therein. */
5074 look_for_tm_attr_overrides (tree type
, tree fndecl
)
5076 tree binfo
= TYPE_BINFO (type
);
5080 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
5082 tree o
, basetype
= BINFO_TYPE (base_binfo
);
5084 if (!TYPE_POLYMORPHIC_P (basetype
))
5087 o
= look_for_overrides_here (basetype
, fndecl
);
5090 if (lookup_attribute ("transaction_safe_dynamic",
5091 DECL_ATTRIBUTES (o
)))
5092 /* transaction_safe_dynamic is not inherited. */;
5094 found
|= tm_attr_to_mask (find_tm_attribute
5095 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
5098 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
5104 /* Subroutine of set_method_tm_attributes. Handle the checks and
5105 inheritance for one virtual method FNDECL. */
5108 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
5113 found
= look_for_tm_attr_overrides (type
, fndecl
);
5115 /* If FNDECL doesn't actually override anything (i.e. T is the
5116 class that first declares FNDECL virtual), then we're done. */
5120 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
5121 have
= tm_attr_to_mask (tm_attr
);
5123 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5124 tm_pure must match exactly, otherwise no weakening of
5125 tm_safe > tm_callable > nothing. */
5126 /* ??? The tm_pure attribute didn't make the transition to the
5127 multivendor language spec. */
5128 if (have
== TM_ATTR_PURE
)
5130 if (found
!= TM_ATTR_PURE
)
5136 /* If the overridden function is tm_pure, then FNDECL must be. */
5137 else if (found
== TM_ATTR_PURE
&& tm_attr
)
5139 /* Look for base class combinations that cannot be satisfied. */
5140 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
5142 found
&= ~TM_ATTR_PURE
;
5144 error_at (DECL_SOURCE_LOCATION (fndecl
),
5145 "method overrides both %<transaction_pure%> and %qE methods",
5146 tm_mask_to_attr (found
));
5148 /* If FNDECL did not declare an attribute, then inherit the most
5150 else if (tm_attr
== NULL
)
5152 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
5154 /* Otherwise validate that we're not weaker than a function
5155 that is being overridden. */
5159 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
5165 error_at (DECL_SOURCE_LOCATION (fndecl
),
5166 "method declared %qE overriding %qE method",
5167 tm_attr
, tm_mask_to_attr (found
));
5170 /* For each of the methods in T, propagate a class-level tm attribute. */
5173 set_method_tm_attributes (tree t
)
5175 tree class_tm_attr
, fndecl
;
5177 /* Don't bother collecting tm attributes if transactional memory
5178 support is not enabled. */
5182 /* Process virtual methods first, as they inherit directly from the
5183 base virtual function and also require validation of new attributes. */
5184 if (TYPE_CONTAINS_VPTR_P (t
))
5187 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
5188 vchain
= TREE_CHAIN (vchain
))
5190 fndecl
= BV_FN (vchain
);
5191 if (DECL_THUNK_P (fndecl
))
5192 fndecl
= THUNK_TARGET (fndecl
);
5193 set_one_vmethod_tm_attributes (t
, fndecl
);
5197 /* If the class doesn't have an attribute, nothing more to do. */
5198 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
5199 if (class_tm_attr
== NULL
)
5202 /* Any method that does not yet have a tm attribute inherits
5203 the one from the class. */
5204 for (fndecl
= TYPE_FIELDS (t
); fndecl
; fndecl
= DECL_CHAIN (fndecl
))
5205 if (DECL_DECLARES_FUNCTION_P (fndecl
)
5206 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
5207 apply_tm_attr (fndecl
, class_tm_attr
);
5210 /* Returns true if FN is a default constructor. */
5213 default_ctor_p (const_tree fn
)
5215 return (DECL_CONSTRUCTOR_P (fn
)
5216 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
5219 /* Returns true iff class T has a user-provided constructor that can be called
5220 with more than zero arguments. */
5223 type_has_user_nondefault_constructor (tree t
)
5225 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5228 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5231 if (user_provided_p (fn
)
5232 && (TREE_CODE (fn
) == TEMPLATE_DECL
5233 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
5241 /* Returns the defaulted constructor if T has one. Otherwise, returns
5245 in_class_defaulted_default_constructor (tree t
)
5247 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5250 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5254 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
5255 && default_ctor_p (fn
))
5262 /* Returns true iff FN is a user-provided function, i.e. user-declared
5263 and not defaulted at its first declaration. */
5266 user_provided_p (tree fn
)
5268 fn
= STRIP_TEMPLATE (fn
);
5269 return (!DECL_ARTIFICIAL (fn
)
5270 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
5271 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
5274 /* Returns true iff class T has a user-provided constructor. */
5277 type_has_user_provided_constructor (tree t
)
5279 if (!CLASS_TYPE_P (t
))
5282 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5285 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5286 if (user_provided_p (*iter
))
5292 /* Returns true iff class T has a user-provided or explicit constructor. */
5295 type_has_user_provided_or_explicit_constructor (tree t
)
5297 if (!CLASS_TYPE_P (t
))
5300 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5303 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5306 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5313 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5314 declared or explicitly defaulted in the class body) default
5318 type_has_non_user_provided_default_constructor (tree t
)
5320 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5322 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5325 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5328 if (TREE_CODE (fn
) == FUNCTION_DECL
5329 && default_ctor_p (fn
)
5330 && !user_provided_p (fn
))
5337 /* TYPE is being used as a virtual base, and has a non-trivial move
5338 assignment. Return true if this is due to there being a user-provided
5339 move assignment in TYPE or one of its subobjects; if there isn't, then
5340 multiple move assignment can't cause any harm. */
5343 vbase_has_user_provided_move_assign (tree type
)
5345 /* Does the type itself have a user-provided move assignment operator? */
5346 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type
))
5347 for (ovl_iterator
iter (get_class_binding_direct
5348 (type
, assign_op_identifier
));
5350 if (user_provided_p (*iter
) && move_fn_p (*iter
))
5353 /* Do any of its bases? */
5354 tree binfo
= TYPE_BINFO (type
);
5356 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5357 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5360 /* Or non-static data members? */
5361 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5363 if (TREE_CODE (field
) == FIELD_DECL
5364 && CLASS_TYPE_P (TREE_TYPE (field
))
5365 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5373 /* If default-initialization leaves part of TYPE uninitialized, returns
5374 a DECL for the field or TYPE itself (DR 253). */
5377 default_init_uninitialized_part (tree type
)
5382 type
= strip_array_types (type
);
5383 if (!CLASS_TYPE_P (type
))
5385 if (!type_has_non_user_provided_default_constructor (type
))
5387 for (binfo
= TYPE_BINFO (type
), i
= 0;
5388 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5390 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5394 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5395 if (TREE_CODE (t
) == FIELD_DECL
5396 && !DECL_ARTIFICIAL (t
)
5397 && !DECL_INITIAL (t
))
5399 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5401 return DECL_P (r
) ? r
: t
;
5407 /* Returns true iff for class T, a trivial synthesized default constructor
5408 would be constexpr. */
5411 trivial_default_constructor_is_constexpr (tree t
)
5413 /* A defaulted trivial default constructor is constexpr
5414 if there is nothing to initialize. */
5415 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5416 /* A class with a vptr doesn't have a trivial default ctor.
5417 In C++20, a class can have transient uninitialized members, e.g.:
5419 struct S { int i; constexpr S() = default; };
5422 return (cxx_dialect
>= cxx20
5423 || is_really_empty_class (t
, /*ignore_vptr*/true));
5426 /* Returns true iff class T has a constexpr default constructor. */
5429 type_has_constexpr_default_constructor (tree t
)
5433 if (!CLASS_TYPE_P (t
))
5435 /* The caller should have stripped an enclosing array. */
5436 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5439 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5441 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5442 return trivial_default_constructor_is_constexpr (t
);
5443 /* Non-trivial, we need to check subobject constructors. */
5444 lazily_declare_fn (sfk_constructor
, t
);
5446 fns
= locate_ctor (t
);
5447 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5450 /* Returns true iff class T has a constexpr default constructor or has an
5451 implicitly declared default constructor that we can't tell if it's constexpr
5452 without forcing a lazy declaration (which might cause undesired
5456 type_maybe_constexpr_default_constructor (tree t
)
5458 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5459 && TYPE_HAS_COMPLEX_DFLT (t
))
5460 /* Assume it's constexpr. */
5462 return type_has_constexpr_default_constructor (t
);
5465 /* Returns true iff class T has a constexpr destructor. */
5468 type_has_constexpr_destructor (tree t
)
5472 if (CLASSTYPE_LAZY_DESTRUCTOR (t
))
5473 /* Non-trivial, we need to check subobject destructors. */
5474 lazily_declare_fn (sfk_destructor
, t
);
5475 fns
= CLASSTYPE_DESTRUCTOR (t
);
5476 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5479 /* Returns true iff class T has a constexpr destructor or has an
5480 implicitly declared destructor that we can't tell if it's constexpr
5481 without forcing a lazy declaration (which might cause undesired
5485 type_maybe_constexpr_destructor (tree t
)
5487 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DESTRUCTOR (t
))
5488 /* Assume it's constexpr. */
5490 return type_has_constexpr_destructor (t
);
5493 /* Returns true iff class TYPE has a virtual destructor. */
5496 type_has_virtual_destructor (tree type
)
5500 if (!CLASS_TYPE_P (type
))
5503 gcc_assert (COMPLETE_TYPE_P (type
));
5504 dtor
= CLASSTYPE_DESTRUCTOR (type
);
5505 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5508 /* Returns true iff T, a class, has a move-assignment or
5509 move-constructor. Does not lazily declare either.
5510 If USER_P is false, any move function will do. If it is true, the
5511 move function must be user-declared.
5513 Note that user-declared here is different from "user-provided",
5514 which doesn't include functions that are defaulted in the
5518 classtype_has_move_assign_or_move_ctor_p (tree t
, bool user_p
)
5521 || (!CLASSTYPE_LAZY_MOVE_CTOR (t
)
5522 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t
)));
5524 if (!CLASSTYPE_LAZY_MOVE_CTOR (t
))
5525 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5526 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5529 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5530 for (ovl_iterator
iter (get_class_binding_direct
5531 (t
, assign_op_identifier
));
5533 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
))
5534 && DECL_CONTEXT (*iter
) == t
5535 && move_fn_p (*iter
))
5541 /* True iff T has a move constructor that is not deleted. */
5544 classtype_has_non_deleted_move_ctor (tree t
)
5546 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5547 lazily_declare_fn (sfk_move_constructor
, t
);
5548 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5549 if (move_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5554 /* True iff T has a copy constructor that is not deleted. */
5557 classtype_has_non_deleted_copy_ctor (tree t
)
5559 if (CLASSTYPE_LAZY_COPY_CTOR (t
))
5560 lazily_declare_fn (sfk_copy_constructor
, t
);
5561 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5562 if (copy_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5567 /* If T, a class, has a user-provided copy constructor, copy assignment
5568 operator, or destructor, returns that function. Otherwise, null. */
5571 classtype_has_depr_implicit_copy (tree t
)
5573 if (!CLASSTYPE_LAZY_COPY_CTOR (t
))
5574 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5577 if (user_provided_p (fn
) && copy_fn_p (fn
))
5581 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t
))
5582 for (ovl_iterator
iter (get_class_binding_direct
5583 (t
, assign_op_identifier
));
5587 if (user_provided_p (fn
) && copy_fn_p (fn
))
5591 if (!CLASSTYPE_LAZY_DESTRUCTOR (t
))
5593 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5594 if (user_provided_p (fn
))
5601 /* True iff T has a member or friend declaration of operator OP. */
5604 classtype_has_op (tree t
, tree_code op
)
5606 tree name
= ovl_op_identifier (op
);
5607 if (get_class_binding (t
, name
))
5609 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5610 if (FRIEND_NAME (f
) == name
)
5616 /* If T has a defaulted member or friend declaration of OP, return it. */
5619 classtype_has_defaulted_op (tree t
, tree_code op
)
5621 tree name
= ovl_op_identifier (op
);
5622 for (ovl_iterator
oi (get_class_binding (t
, name
)); oi
; ++oi
)
5625 if (DECL_DEFAULTED_FN (fn
))
5628 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5629 if (FRIEND_NAME (f
) == name
)
5630 for (tree l
= FRIEND_DECLS (f
); l
; l
= TREE_CHAIN (l
))
5632 tree fn
= TREE_VALUE (l
);
5633 if (DECL_DEFAULTED_FN (fn
))
5639 /* Nonzero if we need to build up a constructor call when initializing an
5640 object of this class, either because it has a user-declared constructor
5641 or because it doesn't have a default constructor (so we need to give an
5642 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5643 what you care about is whether or not an object can be produced by a
5644 constructor (e.g. so we don't set TREE_READONLY on const variables of
5645 such type); use this function when what you care about is whether or not
5646 to try to call a constructor to create an object. The latter case is
5647 the former plus some cases of constructors that cannot be called. */
5650 type_build_ctor_call (tree t
)
5653 if (TYPE_NEEDS_CONSTRUCTING (t
))
5655 inner
= strip_array_types (t
);
5656 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5658 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5660 if (cxx_dialect
< cxx11
)
5662 /* A user-declared constructor might be private, and a constructor might
5663 be trivial but deleted. */
5664 for (ovl_iterator
iter (get_class_binding (inner
, complete_ctor_identifier
));
5668 if (!DECL_ARTIFICIAL (fn
)
5669 || TREE_DEPRECATED (fn
)
5670 || DECL_DELETED_FN (fn
))
5676 /* Like type_build_ctor_call, but for destructors. */
5679 type_build_dtor_call (tree t
)
5682 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5684 inner
= strip_array_types (t
);
5685 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5686 || !COMPLETE_TYPE_P (inner
))
5688 if (cxx_dialect
< cxx11
)
5690 /* A user-declared destructor might be private, and a destructor might
5691 be trivial but deleted. */
5692 for (ovl_iterator
iter (get_class_binding (inner
, complete_dtor_identifier
));
5696 if (!DECL_ARTIFICIAL (fn
)
5697 || TREE_DEPRECATED (fn
)
5698 || DECL_DELETED_FN (fn
))
5704 /* Remove all zero-width bit-fields from T. */
5707 remove_zero_width_bit_fields (tree t
)
5711 fieldsp
= &TYPE_FIELDS (t
);
5714 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5715 && DECL_C_BIT_FIELD (*fieldsp
)
5716 /* We should not be confused by the fact that grokbitfield
5717 temporarily sets the width of the bit field into
5718 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5719 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5721 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5722 || integer_zerop (DECL_SIZE (*fieldsp
))))
5723 *fieldsp
= DECL_CHAIN (*fieldsp
);
5725 fieldsp
= &DECL_CHAIN (*fieldsp
);
5729 /* Returns TRUE iff we need a cookie when dynamically allocating an
5730 array whose elements have the indicated class TYPE. */
5733 type_requires_array_cookie (tree type
)
5736 bool has_two_argument_delete_p
= false;
5738 gcc_assert (CLASS_TYPE_P (type
));
5740 /* If there's a non-trivial destructor, we need a cookie. In order
5741 to iterate through the array calling the destructor for each
5742 element, we'll have to know how many elements there are. */
5743 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5746 /* If the usual deallocation function is a two-argument whose second
5747 argument is of type `size_t', then we have to pass the size of
5748 the array to the deallocation function, so we will need to store
5750 fns
= lookup_fnfields (TYPE_BINFO (type
),
5751 ovl_op_identifier (false, VEC_DELETE_EXPR
),
5752 /*protect=*/0, tf_warning_or_error
);
5753 /* If there are no `operator []' members, or the lookup is
5754 ambiguous, then we don't need a cookie. */
5755 if (!fns
|| fns
== error_mark_node
)
5757 /* Loop through all of the functions. */
5758 for (lkp_iterator
iter (BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
5762 /* See if this function is a one-argument delete function. If
5763 it is, then it will be the usual deallocation function. */
5764 tree second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5765 if (second_parm
== void_list_node
)
5767 /* Do not consider this function if its second argument is an
5771 /* Otherwise, if we have a two-argument function and the second
5772 argument is `size_t', it will be the usual deallocation
5773 function -- unless there is one-argument function, too. */
5774 if (TREE_CHAIN (second_parm
) == void_list_node
5775 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5776 has_two_argument_delete_p
= true;
5779 return has_two_argument_delete_p
;
5782 /* Finish computing the `literal type' property of class type T.
5784 At this point, we have already processed base classes and
5785 non-static data members. We need to check whether the copy
5786 constructor is trivial, the destructor is trivial, and there
5787 is a trivial default constructor or at least one constexpr
5788 constructor other than the copy constructor. */
5791 finalize_literal_type_property (tree t
)
5795 if (cxx_dialect
< cxx11
)
5796 CLASSTYPE_LITERAL_P (t
) = false;
5797 else if (CLASSTYPE_LITERAL_P (t
)
5798 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
5799 && (cxx_dialect
< cxx20
|| !type_maybe_constexpr_destructor (t
)))
5800 CLASSTYPE_LITERAL_P (t
) = false;
5801 else if (CLASSTYPE_LITERAL_P (t
) && LAMBDA_TYPE_P (t
))
5802 CLASSTYPE_LITERAL_P (t
) = (cxx_dialect
>= cxx17
);
5803 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5804 && CLASSTYPE_NON_AGGREGATE (t
)
5805 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5806 CLASSTYPE_LITERAL_P (t
) = false;
5808 /* C++14 DR 1684 removed this restriction. */
5809 if (cxx_dialect
< cxx14
5810 && !CLASSTYPE_LITERAL_P (t
) && !LAMBDA_TYPE_P (t
))
5811 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5812 if (TREE_CODE (fn
) == FUNCTION_DECL
5813 && DECL_DECLARED_CONSTEXPR_P (fn
)
5814 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5815 && !DECL_CONSTRUCTOR_P (fn
))
5817 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5818 if (!DECL_GENERATED_P (fn
))
5820 auto_diagnostic_group d
;
5821 if (pedwarn (DECL_SOURCE_LOCATION (fn
), OPT_Wpedantic
,
5822 "enclosing class of %<constexpr%> non-static "
5823 "member function %q+#D is not a literal type", fn
))
5824 explain_non_literal_class (t
);
5829 /* T is a non-literal type used in a context which requires a constant
5830 expression. Explain why it isn't literal. */
5833 explain_non_literal_class (tree t
)
5835 static hash_set
<tree
> *diagnosed
;
5837 if (!CLASS_TYPE_P (t
))
5839 t
= TYPE_MAIN_VARIANT (t
);
5841 if (diagnosed
== NULL
)
5842 diagnosed
= new hash_set
<tree
>;
5843 if (diagnosed
->add (t
))
5844 /* Already explained. */
5847 auto_diagnostic_group d
;
5848 inform (UNKNOWN_LOCATION
, "%q+T is not literal because:", t
);
5849 if (cxx_dialect
< cxx17
&& LAMBDA_TYPE_P (t
))
5850 inform (UNKNOWN_LOCATION
,
5851 " %qT is a closure type, which is only literal in "
5852 "C++17 and later", t
);
5853 else if (cxx_dialect
< cxx20
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5854 inform (UNKNOWN_LOCATION
, " %q+T has a non-trivial destructor", t
);
5855 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
5856 && !type_maybe_constexpr_destructor (t
))
5857 inform (UNKNOWN_LOCATION
, " %q+T does not have %<constexpr%> destructor",
5859 else if (CLASSTYPE_NON_AGGREGATE (t
)
5860 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5861 && !LAMBDA_TYPE_P (t
)
5862 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5864 inform (UNKNOWN_LOCATION
,
5865 " %q+T is not an aggregate, does not have a trivial "
5866 "default constructor, and has no %<constexpr%> constructor that "
5867 "is not a copy or move constructor", t
);
5868 if (type_has_non_user_provided_default_constructor (t
))
5869 /* Note that we can't simply call locate_ctor because when the
5870 constructor is deleted it just returns NULL_TREE. */
5871 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5874 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5876 parms
= skip_artificial_parms_for (fn
, parms
);
5878 if (sufficient_parms_p (parms
))
5880 if (DECL_DELETED_FN (fn
))
5881 maybe_explain_implicit_delete (fn
);
5883 explain_invalid_constexpr_fn (fn
);
5890 tree binfo
, base_binfo
, field
; int i
;
5891 for (binfo
= TYPE_BINFO (t
), i
= 0;
5892 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5894 tree basetype
= TREE_TYPE (base_binfo
);
5895 if (!CLASSTYPE_LITERAL_P (basetype
))
5897 inform (UNKNOWN_LOCATION
,
5898 " base class %qT of %q+T is non-literal",
5900 explain_non_literal_class (basetype
);
5904 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5907 if (TREE_CODE (field
) != FIELD_DECL
)
5909 ftype
= TREE_TYPE (field
);
5910 if (!literal_type_p (ftype
))
5912 inform (DECL_SOURCE_LOCATION (field
),
5913 " non-static data member %qD has non-literal type",
5915 if (CLASS_TYPE_P (ftype
))
5916 explain_non_literal_class (ftype
);
5918 if (CP_TYPE_VOLATILE_P (ftype
))
5919 inform (DECL_SOURCE_LOCATION (field
),
5920 " non-static data member %qD has volatile type", field
);
5925 /* Check the validity of the bases and members declared in T. Add any
5926 implicitly-generated functions (like copy-constructors and
5927 assignment operators). Compute various flag bits (like
5928 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5929 level: i.e., independently of the ABI in use. */
5932 check_bases_and_members (tree t
)
5934 /* Nonzero if the implicitly generated copy constructor should take
5935 a non-const reference argument. */
5936 int cant_have_const_ctor
;
5937 /* Nonzero if the implicitly generated assignment operator
5938 should take a non-const reference argument. */
5939 int no_const_asn_ref
;
5941 bool saved_complex_asn_ref
;
5942 bool saved_nontrivial_dtor
;
5945 /* By default, we use const reference arguments and generate default
5947 cant_have_const_ctor
= 0;
5948 no_const_asn_ref
= 0;
5950 /* Check all the base-classes and set FMEM members to point to arrays
5951 of potential interest. */
5952 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
5954 /* Deduce noexcept on destructor. This needs to happen after we've set
5955 triviality flags appropriately for our bases. */
5956 if (cxx_dialect
>= cxx11
)
5957 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5958 deduce_noexcept_on_destructor (dtor
);
5960 /* Check all the method declarations. */
5963 /* Save the initial values of these flags which only indicate whether
5964 or not the class has user-provided functions. As we analyze the
5965 bases and members we can set these flags for other reasons. */
5966 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5967 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5969 /* Check all the data member declarations. We cannot call
5970 check_field_decls until we have called check_bases check_methods,
5971 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5972 being set appropriately. */
5973 check_field_decls (t
, &access_decls
,
5974 &cant_have_const_ctor
,
5977 /* A nearly-empty class has to be vptr-containing; a nearly empty
5978 class contains just a vptr. */
5979 if (!TYPE_CONTAINS_VPTR_P (t
))
5980 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5982 /* Do some bookkeeping that will guide the generation of implicitly
5983 declared member functions. */
5984 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5985 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5986 /* We need to call a constructor for this class if it has a
5987 user-provided constructor, or if the default constructor is going
5988 to initialize the vptr. (This is not an if-and-only-if;
5989 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5990 themselves need constructing.) */
5991 TYPE_NEEDS_CONSTRUCTING (t
)
5992 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5995 An aggregate is an array or a class with no user-provided
5996 constructors ... and no virtual functions.
5998 Again, other conditions for being an aggregate are checked
6000 CLASSTYPE_NON_AGGREGATE (t
)
6001 |= ((cxx_dialect
< cxx20
6002 ? type_has_user_provided_or_explicit_constructor (t
)
6003 : TYPE_HAS_USER_CONSTRUCTOR (t
))
6004 || TYPE_POLYMORPHIC_P (t
));
6005 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6006 retain the old definition internally for ABI reasons. */
6007 CLASSTYPE_NON_LAYOUT_POD_P (t
)
6008 |= (CLASSTYPE_NON_AGGREGATE (t
)
6009 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
6010 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6011 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6012 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6013 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6015 /* If the only explicitly declared default constructor is user-provided,
6016 set TYPE_HAS_COMPLEX_DFLT. */
6017 if (!TYPE_HAS_COMPLEX_DFLT (t
)
6018 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
6019 && !type_has_non_user_provided_default_constructor (t
))
6020 TYPE_HAS_COMPLEX_DFLT (t
) = true;
6022 /* Warn if a public base of a polymorphic type has an accessible
6023 non-virtual destructor. It is only now that we know the class is
6024 polymorphic. Although a polymorphic base will have a already
6025 been diagnosed during its definition, we warn on use too. */
6026 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
6028 tree binfo
= TYPE_BINFO (t
);
6029 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
6033 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
6035 tree basetype
= TREE_TYPE (base_binfo
);
6037 if ((*accesses
)[i
] == access_public_node
6038 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
6039 && accessible_nvdtor_p (basetype
))
6040 warning (OPT_Wnon_virtual_dtor
,
6041 "base class %q#T has accessible non-virtual destructor",
6046 /* If the class has no user-declared constructor, but does have
6047 non-static const or reference data members that can never be
6048 initialized, issue a warning. */
6049 if (warn_uninitialized
6050 /* Classes with user-declared constructors are presumed to
6051 initialize these members. */
6052 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
6053 /* Aggregates can be initialized with brace-enclosed
6055 && CLASSTYPE_NON_AGGREGATE (t
))
6059 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6063 if (TREE_CODE (field
) != FIELD_DECL
6064 || DECL_INITIAL (field
) != NULL_TREE
)
6067 type
= TREE_TYPE (field
);
6068 if (TYPE_REF_P (type
))
6069 warning_at (DECL_SOURCE_LOCATION (field
),
6070 OPT_Wuninitialized
, "non-static reference %q#D "
6071 "in class without a constructor", field
);
6072 else if (CP_TYPE_CONST_P (type
)
6073 && (!CLASS_TYPE_P (type
)
6074 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
6075 warning_at (DECL_SOURCE_LOCATION (field
),
6076 OPT_Wuninitialized
, "non-static const member %q#D "
6077 "in class without a constructor", field
);
6081 /* Synthesize any needed methods. */
6082 add_implicitly_declared_members (t
, &access_decls
,
6083 cant_have_const_ctor
,
6086 /* Check defaulted declarations here so we have cant_have_const_ctor
6087 and don't need to worry about clones. */
6088 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6089 if (DECL_DECLARES_FUNCTION_P (fn
)
6090 && !DECL_ARTIFICIAL (fn
)
6091 && DECL_DEFAULTED_IN_CLASS_P (fn
))
6093 int copy
= copy_fn_p (fn
);
6097 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
6098 : !no_const_asn_ref
);
6099 bool fn_const_p
= (copy
== 2);
6101 if (fn_const_p
&& !imp_const_p
)
6102 /* If the function is defaulted outside the class, we just
6103 give the synthesis error. Core Issue #1331 says this is
6104 no longer ill-formed, it is defined as deleted instead. */
6105 DECL_DELETED_FN (fn
) = true;
6107 defaulted_late_check (fn
);
6110 if (LAMBDA_TYPE_P (t
))
6111 /* "This class type is not an aggregate." */
6112 CLASSTYPE_NON_AGGREGATE (t
) = 1;
6114 /* Compute the 'literal type' property before we
6115 do anything with non-static member functions. */
6116 finalize_literal_type_property (t
);
6118 /* Create the in-charge and not-in-charge variants of constructors
6120 clone_constructors_and_destructors (t
);
6122 /* Process the using-declarations. */
6123 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
6124 handle_using_decl (TREE_VALUE (access_decls
), t
);
6126 /* Figure out whether or not we will need a cookie when dynamically
6127 allocating an array of this type. */
6128 LANG_TYPE_CLASS_CHECK (t
)->vec_new_uses_cookie
6129 = type_requires_array_cookie (t
);
6132 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6133 accordingly. If a new vfield was created (because T doesn't have a
6134 primary base class), then the newly created field is returned. It
6135 is not added to the TYPE_FIELDS list; it is the caller's
6136 responsibility to do that. Accumulate declared virtual functions
6140 create_vtable_ptr (tree t
, tree
* virtuals_p
)
6144 /* Collect the virtual functions declared in T. */
6145 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6146 if (TREE_CODE (fn
) == FUNCTION_DECL
6147 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
6148 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
6150 tree new_virtual
= make_node (TREE_LIST
);
6152 BV_FN (new_virtual
) = fn
;
6153 BV_DELTA (new_virtual
) = integer_zero_node
;
6154 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
6156 TREE_CHAIN (new_virtual
) = *virtuals_p
;
6157 *virtuals_p
= new_virtual
;
6160 /* If we couldn't find an appropriate base class, create a new field
6161 here. Even if there weren't any new virtual functions, we might need a
6162 new virtual function table if we're supposed to include vptrs in
6163 all classes that need them. */
6164 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
6166 /* We build this decl with vtbl_ptr_type_node, which is a
6167 `vtable_entry_type*'. It might seem more precise to use
6168 `vtable_entry_type (*)[N]' where N is the number of virtual
6169 functions. However, that would require the vtable pointer in
6170 base classes to have a different type than the vtable pointer
6171 in derived classes. We could make that happen, but that
6172 still wouldn't solve all the problems. In particular, the
6173 type-based alias analysis code would decide that assignments
6174 to the base class vtable pointer can't alias assignments to
6175 the derived class vtable pointer, since they have different
6176 types. Thus, in a derived class destructor, where the base
6177 class constructor was inlined, we could generate bad code for
6178 setting up the vtable pointer.
6180 Therefore, we use one type for all vtable pointers. We still
6181 use a type-correct type; it's just doesn't indicate the array
6182 bounds. That's better than using `void*' or some such; it's
6183 cleaner, and it let's the alias analysis code know that these
6184 stores cannot alias stores to void*! */
6187 field
= build_decl (input_location
,
6188 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
6189 DECL_VIRTUAL_P (field
) = 1;
6190 DECL_ARTIFICIAL (field
) = 1;
6191 DECL_FIELD_CONTEXT (field
) = t
;
6192 DECL_FCONTEXT (field
) = t
;
6193 if (TYPE_PACKED (t
))
6194 DECL_PACKED (field
) = 1;
6196 TYPE_VFIELD (t
) = field
;
6198 /* This class is non-empty. */
6199 CLASSTYPE_EMPTY_P (t
) = 0;
6207 /* Add OFFSET to all base types of BINFO which is a base in the
6208 hierarchy dominated by T.
6210 OFFSET, which is a type offset, is number of bytes. */
6213 propagate_binfo_offsets (tree binfo
, tree offset
)
6219 /* Update BINFO's offset. */
6220 BINFO_OFFSET (binfo
)
6221 = fold_convert (sizetype
,
6222 size_binop (PLUS_EXPR
,
6223 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
6226 /* Find the primary base class. */
6227 primary_binfo
= get_primary_binfo (binfo
);
6229 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
6230 propagate_binfo_offsets (primary_binfo
, offset
);
6232 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6234 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6236 /* Don't do the primary base twice. */
6237 if (base_binfo
== primary_binfo
)
6240 if (BINFO_VIRTUAL_P (base_binfo
))
6243 propagate_binfo_offsets (base_binfo
, offset
);
6247 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6248 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6249 empty subobjects of T. */
6252 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
6258 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
6261 /* Find the last field. The artificial fields created for virtual
6262 bases will go after the last extant field to date. */
6263 next_field
= &TYPE_FIELDS (t
);
6265 next_field
= &DECL_CHAIN (*next_field
);
6267 /* Go through the virtual bases, allocating space for each virtual
6268 base that is not already a primary base class. These are
6269 allocated in inheritance graph order. */
6270 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6272 if (!BINFO_VIRTUAL_P (vbase
))
6275 if (!BINFO_PRIMARY_P (vbase
))
6277 /* This virtual base is not a primary base of any class in the
6278 hierarchy, so we have to add space for it. */
6279 next_field
= build_base_field (rli
, vbase
,
6280 access_private_node
,
6281 offsets
, next_field
);
6286 /* Returns the offset of the byte just past the end of the base class
6290 end_of_base (tree binfo
)
6294 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
6295 size
= TYPE_SIZE_UNIT (char_type_node
);
6296 else if (is_empty_class (BINFO_TYPE (binfo
)))
6297 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6298 allocate some space for it. It cannot have virtual bases, so
6299 TYPE_SIZE_UNIT is fine. */
6300 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6302 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6304 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
6307 /* Returns the offset of the byte just past the end of the base class or empty
6308 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
6309 then only non-virtual bases are included. */
6312 end_of_class (tree t
, bool include_virtuals_p
)
6314 tree result
= size_zero_node
;
6315 vec
<tree
, va_gc
> *vbases
;
6321 for (binfo
= TYPE_BINFO (t
), i
= 0;
6322 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6324 if (!include_virtuals_p
6325 && BINFO_VIRTUAL_P (base_binfo
)
6326 && (!BINFO_PRIMARY_P (base_binfo
)
6327 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
6330 offset
= end_of_base (base_binfo
);
6331 if (tree_int_cst_lt (result
, offset
))
6335 /* Also consider empty data members. */
6336 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6337 if (TREE_CODE (field
) == FIELD_DECL
6338 && !DECL_ARTIFICIAL (field
)
6339 && field_poverlapping_p (field
)
6340 && is_empty_class (TREE_TYPE (field
)))
6342 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
6343 offset
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (field
),
6344 TYPE_SIZE_UNIT (TREE_TYPE (field
)));
6345 if (tree_int_cst_lt (result
, offset
))
6349 if (include_virtuals_p
)
6350 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6351 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6353 offset
= end_of_base (base_binfo
);
6354 if (tree_int_cst_lt (result
, offset
))
6361 /* Warn about bases of T that are inaccessible because they are
6362 ambiguous. For example:
6365 struct T : public S {};
6366 struct U : public S, public T {};
6368 Here, `(S*) new U' is not allowed because there are two `S'
6372 maybe_warn_about_inaccessible_bases (tree t
)
6375 vec
<tree
, va_gc
> *vbases
;
6380 /* If not checking for warning then return early. */
6381 if (!warn_inaccessible_base
)
6384 /* If there are no repeated bases, nothing can be ambiguous. */
6385 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6388 /* Check direct bases. */
6389 for (binfo
= TYPE_BINFO (t
), i
= 0;
6390 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6392 basetype
= BINFO_TYPE (base_binfo
);
6394 if (!uniquely_derived_from_p (basetype
, t
))
6395 warning (OPT_Winaccessible_base
, "direct base %qT inaccessible "
6396 "in %qT due to ambiguity", basetype
, t
);
6399 /* Check for ambiguous virtual bases. */
6401 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6402 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6404 basetype
= BINFO_TYPE (binfo
);
6406 if (!uniquely_derived_from_p (basetype
, t
))
6407 warning (OPT_Winaccessible_base
, "virtual base %qT inaccessible in "
6408 "%qT due to ambiguity", basetype
, t
);
6412 /* Compare two INTEGER_CSTs K1 and K2. */
6415 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6417 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6420 /* Increase the size indicated in RLI to account for empty classes
6421 that are "off the end" of the class. */
6424 include_empty_classes (record_layout_info rli
)
6429 /* It might be the case that we grew the class to allocate a
6430 zero-sized base class. That won't be reflected in RLI, yet,
6431 because we are willing to overlay multiple bases at the same
6432 offset. However, now we need to make sure that RLI is big enough
6433 to reflect the entire class. */
6434 eoc
= end_of_class (rli
->t
, CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6435 rli_size
= rli_size_unit_so_far (rli
);
6436 if (TREE_CODE (rli_size
) == INTEGER_CST
6437 && tree_int_cst_lt (rli_size
, eoc
))
6439 /* The size should have been rounded to a whole byte. */
6440 gcc_assert (tree_int_cst_equal
6441 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6443 = size_binop (PLUS_EXPR
,
6445 size_binop (MULT_EXPR
,
6446 fold_convert (bitsizetype
,
6447 size_binop (MINUS_EXPR
,
6449 bitsize_int (BITS_PER_UNIT
)));
6450 normalize_rli (rli
);
6454 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6455 BINFO_OFFSETs for all of the base-classes. Position the vtable
6456 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6459 layout_class_type (tree t
, tree
*virtuals_p
)
6461 tree non_static_data_members
;
6464 record_layout_info rli
;
6465 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6466 types that appear at that offset. */
6467 splay_tree empty_base_offsets
;
6468 /* True if the last field laid out was a bit-field. */
6469 bool last_field_was_bitfield
= false;
6470 /* The location at which the next field should be inserted. */
6473 /* Keep track of the first non-static data member. */
6474 non_static_data_members
= TYPE_FIELDS (t
);
6476 /* Start laying out the record. */
6477 rli
= start_record_layout (t
);
6479 /* Mark all the primary bases in the hierarchy. */
6480 determine_primary_bases (t
);
6482 /* Create a pointer to our virtual function table. */
6483 vptr
= create_vtable_ptr (t
, virtuals_p
);
6485 /* The vptr is always the first thing in the class. */
6488 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6489 TYPE_FIELDS (t
) = vptr
;
6490 next_field
= &DECL_CHAIN (vptr
);
6491 place_field (rli
, vptr
);
6494 next_field
= &TYPE_FIELDS (t
);
6496 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6497 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6499 build_base_fields (rli
, empty_base_offsets
, next_field
);
6501 /* Layout the non-static data members. */
6502 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6507 /* We still pass things that aren't non-static data members to
6508 the back end, in case it wants to do something with them. */
6509 if (TREE_CODE (field
) != FIELD_DECL
)
6511 place_field (rli
, field
);
6512 /* If the static data member has incomplete type, keep track
6513 of it so that it can be completed later. (The handling
6514 of pending statics in finish_record_layout is
6515 insufficient; consider:
6518 struct S2 { static S1 s1; };
6520 At this point, finish_record_layout will be called, but
6521 S1 is still incomplete.) */
6524 maybe_register_incomplete_var (field
);
6525 /* The visibility of static data members is determined
6526 at their point of declaration, not their point of
6528 determine_visibility (field
);
6533 type
= TREE_TYPE (field
);
6534 if (type
== error_mark_node
)
6537 padding
= NULL_TREE
;
6539 bool might_overlap
= field_poverlapping_p (field
);
6541 if (might_overlap
&& CLASS_TYPE_P (type
)
6542 && (CLASSTYPE_NON_LAYOUT_POD_P (type
) || CLASSTYPE_EMPTY_P (type
)))
6544 /* if D is a potentially-overlapping data member, update sizeof(C) to
6545 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6546 tree nvsize
= CLASSTYPE_SIZE_UNIT (type
);
6547 /* end_of_class doesn't always give dsize, but it does in the case of
6548 a class with virtual bases, which is when dsize > nvsize. */
6549 tree dsize
= end_of_class (type
, /*vbases*/true);
6550 if (tree_int_cst_le (dsize
, nvsize
))
6552 DECL_SIZE_UNIT (field
) = nvsize
;
6553 DECL_SIZE (field
) = CLASSTYPE_SIZE (type
);
6557 DECL_SIZE_UNIT (field
) = dsize
;
6558 DECL_SIZE (field
) = bit_from_pos (dsize
, bitsize_zero_node
);
6562 /* If this field is a bit-field whose width is greater than its
6563 type, then there are some special rules for allocating
6565 if (DECL_C_BIT_FIELD (field
)
6566 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6568 bool was_unnamed_p
= false;
6569 /* We must allocate the bits as if suitably aligned for the
6570 longest integer type that fits in this many bits. Then,
6571 we are supposed to use the left over bits as additional
6574 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6575 tree limit
= size_int (MAX_FIXED_MODE_SIZE
);
6576 if (tree_int_cst_lt (DECL_SIZE (field
), limit
))
6577 limit
= DECL_SIZE (field
);
6579 tree integer_type
= integer_types
[itk_char
];
6580 for (unsigned itk
= itk_char
; itk
!= itk_none
; itk
++)
6581 if (tree next
= integer_types
[itk
])
6583 if (tree_int_cst_lt (limit
, TYPE_SIZE (next
)))
6584 /* Too big, so our current guess is what we want. */
6586 /* Not bigger than limit, ok */
6587 integer_type
= next
;
6590 /* Figure out how much additional padding is required. */
6591 if (TREE_CODE (t
) == UNION_TYPE
)
6592 /* In a union, the padding field must have the full width
6593 of the bit-field; all fields start at offset zero. */
6594 padding
= DECL_SIZE (field
);
6596 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6597 TYPE_SIZE (integer_type
));
6599 if (integer_zerop (padding
))
6600 padding
= NULL_TREE
;
6602 /* An unnamed bitfield does not normally affect the
6603 alignment of the containing class on a target where
6604 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6605 make any exceptions for unnamed bitfields when the
6606 bitfields are longer than their types. Therefore, we
6607 temporarily give the field a name. */
6608 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6610 was_unnamed_p
= true;
6611 DECL_NAME (field
) = make_anon_name ();
6614 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6615 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6616 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6617 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6618 empty_base_offsets
);
6620 DECL_NAME (field
) = NULL_TREE
;
6621 /* Now that layout has been performed, set the size of the
6622 field to the size of its declared type; the rest of the
6623 field is effectively invisible. */
6624 DECL_SIZE (field
) = TYPE_SIZE (type
);
6625 /* We must also reset the DECL_MODE of the field. */
6626 SET_DECL_MODE (field
, TYPE_MODE (type
));
6628 else if (might_overlap
&& is_empty_class (type
))
6630 DECL_FIELD_ABI_IGNORED (field
) = 1;
6631 layout_empty_base_or_field (rli
, field
, empty_base_offsets
);
6634 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6635 empty_base_offsets
);
6637 /* Remember the location of any empty classes in FIELD. */
6638 record_subobject_offsets (field
, empty_base_offsets
);
6640 /* If a bit-field does not immediately follow another bit-field,
6641 and yet it starts in the middle of a byte, we have failed to
6642 comply with the ABI. */
6644 && DECL_C_BIT_FIELD (field
)
6645 /* The TREE_NO_WARNING flag gets set by Objective-C when
6646 laying out an Objective-C class. The ObjC ABI differs
6647 from the C++ ABI, and so we do not want a warning
6649 && !TREE_NO_WARNING (field
)
6650 && !last_field_was_bitfield
6651 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6652 DECL_FIELD_BIT_OFFSET (field
),
6653 bitsize_unit_node
)))
6654 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6655 "offset of %qD is not ABI-compliant and may "
6656 "change in a future version of GCC", field
);
6658 /* The middle end uses the type of expressions to determine the
6659 possible range of expression values. In order to optimize
6660 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6661 must be made aware of the width of "i", via its type.
6663 Because C++ does not have integer types of arbitrary width,
6664 we must (for the purposes of the front end) convert from the
6665 type assigned here to the declared type of the bitfield
6666 whenever a bitfield expression is used as an rvalue.
6667 Similarly, when assigning a value to a bitfield, the value
6668 must be converted to the type given the bitfield here. */
6669 if (DECL_C_BIT_FIELD (field
))
6671 unsigned HOST_WIDE_INT width
;
6672 tree ftype
= TREE_TYPE (field
);
6673 width
= tree_to_uhwi (DECL_SIZE (field
));
6674 if (width
!= TYPE_PRECISION (ftype
))
6677 = c_build_bitfield_integer_type (width
,
6678 TYPE_UNSIGNED (ftype
));
6680 = cp_build_qualified_type (TREE_TYPE (field
),
6681 cp_type_quals (ftype
));
6685 /* If we needed additional padding after this field, add it
6691 padding_field
= build_decl (input_location
,
6695 DECL_BIT_FIELD (padding_field
) = 1;
6696 DECL_SIZE (padding_field
) = padding
;
6697 DECL_CONTEXT (padding_field
) = t
;
6698 DECL_ARTIFICIAL (padding_field
) = 1;
6699 DECL_IGNORED_P (padding_field
) = 1;
6700 DECL_PADDING_P (padding_field
) = 1;
6701 layout_nonempty_base_or_field (rli
, padding_field
,
6703 empty_base_offsets
);
6706 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6709 if (!integer_zerop (rli
->bitpos
))
6711 /* Make sure that we are on a byte boundary so that the size of
6712 the class without virtual bases will always be a round number
6714 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6715 normalize_rli (rli
);
6718 /* Delete all zero-width bit-fields from the list of fields. Now
6719 that the type is laid out they are no longer important. */
6720 remove_zero_width_bit_fields (t
);
6722 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6724 /* T needs a different layout as a base (eliding virtual bases
6725 or whatever). Create that version. */
6726 tree base_t
= make_node (TREE_CODE (t
));
6727 tree base_d
= create_implicit_typedef (as_base_identifier
, base_t
);
6729 TYPE_CONTEXT (base_t
) = t
;
6730 DECL_CONTEXT (base_d
) = t
;
6732 /* If the ABI version is not at least two, and the last
6733 field was a bit-field, RLI may not be on a byte
6734 boundary. In particular, rli_size_unit_so_far might
6735 indicate the last complete byte, while rli_size_so_far
6736 indicates the total number of bits used. Therefore,
6737 rli_size_so_far, rather than rli_size_unit_so_far, is
6738 used to compute TYPE_SIZE_UNIT. */
6740 /* Set the size and alignment for the new type. */
6741 tree eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6742 TYPE_SIZE_UNIT (base_t
)
6743 = size_binop (MAX_EXPR
,
6744 fold_convert (sizetype
,
6745 size_binop (CEIL_DIV_EXPR
,
6746 rli_size_so_far (rli
),
6747 bitsize_int (BITS_PER_UNIT
))),
6750 = size_binop (MAX_EXPR
,
6751 rli_size_so_far (rli
),
6752 size_binop (MULT_EXPR
,
6753 fold_convert (bitsizetype
, eoc
),
6754 bitsize_int (BITS_PER_UNIT
)));
6755 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
6756 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6757 TYPE_TYPELESS_STORAGE (base_t
) = TYPE_TYPELESS_STORAGE (t
);
6758 TYPE_CXX_ODR_P (base_t
) = TYPE_CXX_ODR_P (t
);
6760 /* Copy the non-static data members of T. This will include its
6761 direct non-virtual bases & vtable. */
6762 next_field
= &TYPE_FIELDS (base_t
);
6763 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6764 if (TREE_CODE (field
) == FIELD_DECL
)
6766 *next_field
= copy_node (field
);
6767 /* Zap any NSDMI, it's not needed and might be a deferred
6769 DECL_INITIAL (*next_field
) = NULL_TREE
;
6770 DECL_CONTEXT (*next_field
) = base_t
;
6771 next_field
= &DECL_CHAIN (*next_field
);
6773 *next_field
= NULL_TREE
;
6775 /* We use the base type for trivial assignments, and hence it
6777 compute_record_mode (base_t
);
6779 /* Record the base version of the type. */
6780 CLASSTYPE_AS_BASE (t
) = base_t
;
6783 CLASSTYPE_AS_BASE (t
) = t
;
6785 /* Every empty class contains an empty class. */
6786 if (CLASSTYPE_EMPTY_P (t
))
6787 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6789 /* Set the TYPE_DECL for this type to contain the right
6790 value for DECL_OFFSET, so that we can use it as part
6791 of a COMPONENT_REF for multiple inheritance. */
6792 layout_decl (TYPE_MAIN_DECL (t
), 0);
6794 /* Now fix up any virtual base class types that we left lying
6795 around. We must get these done before we try to lay out the
6796 virtual function table. As a side-effect, this will remove the
6797 base subobject fields. */
6798 layout_virtual_bases (rli
, empty_base_offsets
);
6800 /* Make sure that empty classes are reflected in RLI at this
6802 include_empty_classes (rli
);
6804 /* Make sure not to create any structures with zero size. */
6805 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6807 build_decl (input_location
,
6808 FIELD_DECL
, NULL_TREE
, char_type_node
));
6810 /* If this is a non-POD, declaring it packed makes a difference to how it
6811 can be used as a field; don't let finalize_record_size undo it. */
6812 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6813 rli
->packed_maybe_necessary
= true;
6815 /* Let the back end lay out the type. */
6816 finish_record_layout (rli
, /*free_p=*/true);
6818 /* If we didn't end up needing an as-base type, don't use it. */
6819 if (CLASSTYPE_AS_BASE (t
) != t
6820 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
6821 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
6822 causing us to lose the user-specified alignment as in PR94050. */
6823 && TYPE_USER_ALIGN (t
) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t
))
6824 && tree_int_cst_equal (TYPE_SIZE (t
),
6825 TYPE_SIZE (CLASSTYPE_AS_BASE (t
))))
6826 CLASSTYPE_AS_BASE (t
) = t
;
6828 if (TYPE_SIZE_UNIT (t
)
6829 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6830 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6831 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6832 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
6834 /* Warn about bases that can't be talked about due to ambiguity. */
6835 maybe_warn_about_inaccessible_bases (t
);
6837 /* Now that we're done with layout, give the base fields the real types. */
6838 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6839 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6840 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6843 splay_tree_delete (empty_base_offsets
);
6845 if (CLASSTYPE_EMPTY_P (t
)
6846 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6847 TYPE_SIZE_UNIT (t
)))
6848 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6851 /* Determine the "key method" for the class type indicated by TYPE,
6852 and set CLASSTYPE_KEY_METHOD accordingly. */
6855 determine_key_method (tree type
)
6859 if (processing_template_decl
6860 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6861 || CLASSTYPE_INTERFACE_KNOWN (type
))
6864 /* The key method is the first non-pure virtual function that is not
6865 inline at the point of class definition. On some targets the
6866 key function may not be inline; those targets should not call
6867 this function until the end of the translation unit. */
6868 for (method
= TYPE_FIELDS (type
); method
; method
= DECL_CHAIN (method
))
6869 if (TREE_CODE (method
) == FUNCTION_DECL
6870 && DECL_VINDEX (method
) != NULL_TREE
6871 && ! DECL_DECLARED_INLINE_P (method
)
6872 && ! DECL_PURE_VIRTUAL_P (method
))
6874 CLASSTYPE_KEY_METHOD (type
) = method
;
6881 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6882 class data member of non-zero size, otherwise false. */
6885 field_nonempty_p (const_tree fld
)
6887 if (TREE_CODE (fld
) == ERROR_MARK
)
6890 tree type
= TREE_TYPE (fld
);
6891 if (TREE_CODE (fld
) == FIELD_DECL
6892 && TREE_CODE (type
) != ERROR_MARK
6893 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
6895 return TYPE_SIZE (type
)
6896 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
6897 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
6903 /* Used by find_flexarrays and related functions. */
6907 /* The first flexible array member or non-zero array member found
6908 in the order of layout. */
6910 /* First non-static non-empty data member in the class or its bases. */
6912 /* The first non-static non-empty data member following either
6913 the flexible array member, if found, or the zero-length array member
6914 otherwise. AFTER[1] refers to the first such data member of a union
6915 of which the struct containing the flexible array member or zero-length
6916 array is a member, or NULL when no such union exists. This element is
6917 only used during searching, not for diagnosing problems. AFTER[0]
6918 refers to the first such data member that is not a member of such
6922 /* Refers to a struct (not union) in which the struct of which the flexible
6923 array is member is defined. Used to diagnose strictly (according to C)
6924 invalid uses of the latter structs. */
6928 /* Find either the first flexible array member or the first zero-length
6929 array, in that order of preference, among members of class T (but not
6930 its base classes), and set members of FMEM accordingly.
6931 BASE_P is true if T is a base class of another class.
6932 PUN is set to the outermost union in which the flexible array member
6933 (or zero-length array) is defined if one such union exists, otherwise
6935 Similarly, PSTR is set to a data member of the outermost struct of
6936 which the flexible array is a member if one such struct exists,
6937 otherwise to NULL. */
6940 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
6941 tree pun
/* = NULL_TREE */,
6942 tree pstr
/* = NULL_TREE */)
6944 /* Set the "pointer" to the outermost enclosing union if not set
6945 yet and maintain it for the remainder of the recursion. */
6946 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
6949 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
6951 if (fld
== error_mark_node
)
6954 /* Is FLD a typedef for an anonymous struct? */
6956 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6957 handled elsewhere so that errors like the following are detected
6959 typedef struct { int i, a[], j; } S; // bug c++/72753
6960 S s [2]; // bug c++/68489
6962 if (TREE_CODE (fld
) == TYPE_DECL
6963 && DECL_IMPLICIT_TYPEDEF_P (fld
)
6964 && CLASS_TYPE_P (TREE_TYPE (fld
))
6965 && IDENTIFIER_ANON_P (DECL_NAME (fld
)))
6967 /* Check the nested unnamed type referenced via a typedef
6968 independently of FMEM (since it's not a data member of
6969 the enclosing class). */
6970 check_flexarrays (TREE_TYPE (fld
));
6974 /* Skip anything that's GCC-generated or not a (non-static) data
6976 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
6979 /* Type of the member. */
6980 tree fldtype
= TREE_TYPE (fld
);
6981 if (fldtype
== error_mark_node
)
6984 /* Determine the type of the array element or object referenced
6985 by the member so that it can be checked for flexible array
6986 members if it hasn't been yet. */
6987 tree eltype
= fldtype
;
6988 while (TREE_CODE (eltype
) == ARRAY_TYPE
6989 || INDIRECT_TYPE_P (eltype
))
6990 eltype
= TREE_TYPE (eltype
);
6992 if (RECORD_OR_UNION_TYPE_P (eltype
))
6994 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6996 /* Once the member after the flexible array has been found
6998 fmem
->after
[bool (pun
)] = fld
;
7002 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
7004 /* Descend into the non-static member struct or union and try
7005 to find a flexible array member or zero-length array among
7006 its members. This is only necessary for anonymous types
7007 and types in whose context the current type T has not been
7008 defined (the latter must not be checked again because they
7009 are already in the process of being checked by one of the
7010 recursive calls). */
7012 tree first
= fmem
->first
;
7013 tree array
= fmem
->array
;
7015 /* If this member isn't anonymous and a prior non-flexible array
7016 member has been seen in one of the enclosing structs, clear
7017 the FIRST member since it doesn't contribute to the flexible
7018 array struct's members. */
7019 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7020 fmem
->first
= NULL_TREE
;
7022 find_flexarrays (eltype
, fmem
, false, pun
,
7023 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
7025 if (fmem
->array
!= array
)
7028 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7030 /* Restore the FIRST member reset above if no flexible
7031 array member has been found in this member's struct. */
7032 fmem
->first
= first
;
7035 /* If the member struct contains the first flexible array
7036 member, or if this member is a base class, continue to
7037 the next member and avoid setting the FMEM->NEXT pointer
7044 if (field_nonempty_p (fld
))
7046 /* Remember the first non-static data member. */
7050 /* Remember the first non-static data member after the flexible
7051 array member, if one has been found, or the zero-length array
7052 if it has been found. */
7053 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
7054 fmem
->after
[bool (pun
)] = fld
;
7057 /* Skip non-arrays. */
7058 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
7061 /* Determine the upper bound of the array if it has one. */
7062 if (TYPE_DOMAIN (fldtype
))
7066 /* Make a record of the zero-length array if either one
7067 such field or a flexible array member has been seen to
7068 handle the pathological and unlikely case of multiple
7070 if (!fmem
->after
[bool (pun
)])
7071 fmem
->after
[bool (pun
)] = fld
;
7073 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
7075 /* Remember the first zero-length array unless a flexible array
7076 member has already been seen. */
7078 fmem
->enclosing
= pstr
;
7083 /* Flexible array members have no upper bound. */
7086 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7088 /* Replace the zero-length array if it's been stored and
7089 reset the after pointer. */
7090 fmem
->after
[bool (pun
)] = NULL_TREE
;
7092 fmem
->enclosing
= pstr
;
7094 else if (!fmem
->after
[bool (pun
)])
7095 /* Make a record of another flexible array member. */
7096 fmem
->after
[bool (pun
)] = fld
;
7101 fmem
->enclosing
= pstr
;
7107 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7108 a flexible array member (or the zero-length array extension). */
7111 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
7113 if (fmem
->array
&& fmem
->enclosing
)
7115 auto_diagnostic_group d
;
7116 if (pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
7117 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
7118 ? G_("invalid use of %q#T with a zero-size array "
7120 : G_("invalid use of %q#T with a flexible array member "
7122 DECL_CONTEXT (fmem
->array
),
7123 DECL_CONTEXT (fmem
->enclosing
)))
7124 inform (DECL_SOURCE_LOCATION (fmem
->array
),
7125 "array member %q#D declared here", fmem
->array
);
7129 /* Issue diagnostics for invalid flexible array members or zero-length
7130 arrays that are not the last elements of the containing class or its
7131 base classes or that are its sole members. */
7134 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
7139 if (fmem
->first
&& !fmem
->after
[0])
7141 diagnose_invalid_flexarray (fmem
);
7145 /* Has a diagnostic been issued? */
7148 const char *msg
= 0;
7150 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7153 msg
= G_("zero-size array member %qD not at end of %q#T");
7154 else if (!fmem
->first
)
7155 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
7159 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7161 auto_diagnostic_group d
;
7162 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
7164 inform (location_of (t
), "in the definition of %q#T", t
);
7172 msg
= G_("flexible array member %qD not at end of %q#T");
7173 else if (!fmem
->first
)
7174 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
7178 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7181 auto_diagnostic_group d
;
7182 error_at (loc
, msg
, fmem
->array
, t
);
7184 /* In the unlikely event that the member following the flexible
7185 array member is declared in a different class, or the member
7186 overlaps another member of a common union, point to it.
7187 Otherwise it should be obvious. */
7189 && ((DECL_CONTEXT (fmem
->after
[0])
7190 != DECL_CONTEXT (fmem
->array
))))
7192 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
7193 "next member %q#D declared here",
7195 inform (location_of (t
), "in the definition of %q#T", t
);
7200 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
7201 diagnose_invalid_flexarray (fmem
);
7205 /* Recursively check to make sure that any flexible array or zero-length
7206 array members of class T or its bases are valid (i.e., not the sole
7207 non-static data member of T and, if one exists, that it is the last
7208 non-static data member of T and its base classes. FMEM is expected
7209 to be initially null and is used internally by recursive calls to
7210 the function. Issue the appropriate diagnostics for the array member
7211 that fails the checks. */
7214 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
7215 bool base_p
/* = false */)
7217 /* Initialize the result of a search for flexible array and zero-length
7218 array members. Avoid doing any work if the most interesting FMEM data
7219 have already been populated. */
7220 flexmems_t flexmems
= flexmems_t ();
7223 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
7226 tree fam
= fmem
->array
;
7228 /* Recursively check the primary base class first. */
7229 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7231 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
7232 check_flexarrays (basetype
, fmem
, true);
7235 /* Recursively check the base classes. */
7236 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
7237 for (int i
= 0; i
< nbases
; ++i
)
7239 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
7241 /* The primary base class was already checked above. */
7242 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
7245 /* Virtual base classes are at the end. */
7246 if (BINFO_VIRTUAL_P (base_binfo
))
7249 /* Check the base class. */
7250 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
7253 if (fmem
== &flexmems
)
7255 /* Check virtual base classes only once per derived class.
7256 I.e., this check is not performed recursively for base
7260 vec
<tree
, va_gc
> *vbases
;
7261 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
7262 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
7264 /* Check the virtual base class. */
7265 tree basetype
= TREE_TYPE (base_binfo
);
7267 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
7271 /* Is the type unnamed (and therefore a member of it potentially
7272 an anonymous struct or union)? */
7273 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
7274 if (tree ctx
= maybe_anon_p
? TYPE_CONTEXT (t
) : NULL_TREE
)
7275 maybe_anon_p
= RECORD_OR_UNION_TYPE_P (ctx
);
7277 /* Search the members of the current (possibly derived) class, skipping
7278 unnamed structs and unions since those could be anonymous. */
7279 if (fmem
!= &flexmems
|| !maybe_anon_p
)
7280 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
7282 if (fmem
== &flexmems
&& !maybe_anon_p
)
7284 /* Issue diagnostics for invalid flexible and zero-length array
7285 members found in base classes or among the members of the current
7286 class. Ignore anonymous structs and unions whose members are
7287 considered to be members of the enclosing class and thus will
7288 be diagnosed when checking it. */
7289 diagnose_flexarrays (t
, fmem
);
7293 /* Perform processing required when the definition of T (a class type)
7294 is complete. Diagnose invalid definitions of flexible array members
7295 and zero-size arrays. */
7298 finish_struct_1 (tree t
)
7301 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7302 tree virtuals
= NULL_TREE
;
7304 if (COMPLETE_TYPE_P (t
))
7306 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
7307 error ("redefinition of %q#T", t
);
7312 /* If this type was previously laid out as a forward reference,
7313 make sure we lay it out again. */
7314 TYPE_SIZE (t
) = NULL_TREE
;
7315 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
7317 /* Make assumptions about the class; we'll reset the flags if
7319 CLASSTYPE_EMPTY_P (t
) = 1;
7320 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
7321 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
7322 CLASSTYPE_LITERAL_P (t
) = true;
7324 /* Do end-of-class semantic processing: checking the validity of the
7325 bases and members and add implicitly generated methods. */
7326 check_bases_and_members (t
);
7328 /* Find the key method. */
7329 if (TYPE_CONTAINS_VPTR_P (t
))
7331 /* The Itanium C++ ABI permits the key method to be chosen when
7332 the class is defined -- even though the key method so
7333 selected may later turn out to be an inline function. On
7334 some systems (such as ARM Symbian OS) the key method cannot
7335 be determined until the end of the translation unit. On such
7336 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7337 will cause the class to be added to KEYED_CLASSES. Then, in
7338 finish_file we will determine the key method. */
7339 if (targetm
.cxx
.key_method_may_be_inline ())
7340 determine_key_method (t
);
7342 /* If a polymorphic class has no key method, we may emit the vtable
7343 in every translation unit where the class definition appears. If
7344 we're devirtualizing, we can look into the vtable even if we
7345 aren't emitting it. */
7346 if (!CLASSTYPE_KEY_METHOD (t
))
7347 vec_safe_push (keyed_classes
, t
);
7350 /* Layout the class itself. */
7351 layout_class_type (t
, &virtuals
);
7352 /* COMPLETE_TYPE_P is now true. */
7354 set_class_bindings (t
);
7356 /* With the layout complete, check for flexible array members and
7357 zero-length arrays that might overlap other members in the final
7359 check_flexarrays (t
);
7361 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
7363 /* If necessary, create the primary vtable for this class. */
7364 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
7366 /* We must enter these virtuals into the table. */
7367 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7368 build_primary_vtable (NULL_TREE
, t
);
7369 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
7370 /* Here we know enough to change the type of our virtual
7371 function table, but we will wait until later this function. */
7372 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
7374 /* If we're warning about ABI tags, check the types of the new
7375 virtual functions. */
7377 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
7378 check_abi_tags (t
, TREE_VALUE (v
));
7381 if (TYPE_CONTAINS_VPTR_P (t
))
7386 if (BINFO_VTABLE (TYPE_BINFO (t
)))
7387 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
7388 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7389 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
7391 /* Add entries for virtual functions introduced by this class. */
7392 BINFO_VIRTUALS (TYPE_BINFO (t
))
7393 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
7395 /* Set DECL_VINDEX for all functions declared in this class. */
7396 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
7398 fn
= TREE_CHAIN (fn
),
7399 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
7400 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
7402 tree fndecl
= BV_FN (fn
);
7404 if (DECL_THUNK_P (fndecl
))
7405 /* A thunk. We should never be calling this entry directly
7406 from this vtable -- we'd use the entry for the non
7407 thunk base function. */
7408 DECL_VINDEX (fndecl
) = NULL_TREE
;
7409 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7410 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7414 finish_struct_bits (t
);
7416 set_method_tm_attributes (t
);
7417 if (flag_openmp
|| flag_openmp_simd
)
7418 finish_omp_declare_simd_methods (t
);
7420 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7421 for any static member objects of the type we're working on. */
7422 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7423 if (DECL_DECLARES_FUNCTION_P (x
))
7424 DECL_IN_AGGR_P (x
) = false;
7425 else if (VAR_P (x
) && TREE_STATIC (x
)
7426 && TREE_TYPE (x
) != error_mark_node
7427 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7428 SET_DECL_MODE (x
, TYPE_MODE (t
));
7430 /* Complain if one of the field types requires lower visibility. */
7431 constrain_class_visibility (t
);
7433 /* Make the rtl for any new vtables we have created, and unmark
7434 the base types we marked. */
7437 /* Build the VTT for T. */
7441 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7442 && !CLASSTYPE_FINAL (t
))
7443 warning (OPT_Wnon_virtual_dtor
,
7444 "%q#T has virtual functions and accessible"
7445 " non-virtual destructor", t
);
7449 if (warn_overloaded_virtual
)
7452 /* Class layout, assignment of virtual table slots, etc., is now
7453 complete. Give the back end a chance to tweak the visibility of
7454 the class or perform any other required target modifications. */
7455 targetm
.cxx
.adjust_class_at_definition (t
);
7457 maybe_suppress_debug_info (t
);
7459 if (flag_vtable_verify
)
7460 vtv_save_class_info (t
);
7462 dump_class_hierarchy (t
);
7464 /* Finish debugging output for this type. */
7465 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7467 /* Recalculate satisfaction that might depend on completeness. */
7468 clear_satisfaction_cache ();
7470 if (TYPE_TRANSPARENT_AGGR (t
))
7472 tree field
= first_field (t
);
7473 if (field
== NULL_TREE
|| error_operand_p (field
))
7475 error ("type transparent %q#T does not have any fields", t
);
7476 TYPE_TRANSPARENT_AGGR (t
) = 0;
7478 else if (DECL_ARTIFICIAL (field
))
7480 if (DECL_FIELD_IS_BASE (field
))
7481 error ("type transparent class %qT has base classes", t
);
7484 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7485 error ("type transparent class %qT has virtual functions", t
);
7487 TYPE_TRANSPARENT_AGGR (t
) = 0;
7489 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7491 error ("type transparent %q#T cannot be made transparent because "
7492 "the type of the first field has a different ABI from the "
7493 "class overall", t
);
7494 TYPE_TRANSPARENT_AGGR (t
) = 0;
7499 /* When T was built up, the member declarations were added in reverse
7500 order. Rearrange them to declaration order. */
7503 unreverse_member_declarations (tree t
)
7509 /* The following lists are all in reverse order. Put them in
7510 declaration order now. */
7511 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7513 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7514 order, so we can't just use nreverse. Due to stat_hack
7515 chicanery in finish_member_declaration. */
7517 for (x
= TYPE_FIELDS (t
);
7518 x
&& TREE_CODE (x
) != TYPE_DECL
;
7521 next
= DECL_CHAIN (x
);
7522 DECL_CHAIN (x
) = prev
;
7528 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7529 TYPE_FIELDS (t
) = prev
;
7534 finish_struct (tree t
, tree attributes
)
7536 location_t saved_loc
= input_location
;
7538 /* Now that we've got all the field declarations, reverse everything
7540 unreverse_member_declarations (t
);
7542 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7543 fixup_attribute_variants (t
);
7545 /* Nadger the current location so that diagnostics point to the start of
7546 the struct, not the end. */
7547 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7549 if (processing_template_decl
)
7553 /* We need to add the target functions of USING_DECLS, so that
7554 they can be found when the using declaration is not
7555 instantiated yet. */
7556 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7557 if (TREE_CODE (x
) == USING_DECL
)
7559 tree fn
= strip_using_decl (x
);
7561 for (lkp_iterator
iter (fn
); iter
; ++iter
)
7562 add_method (t
, *iter
, true);
7564 else if (DECL_DECLARES_FUNCTION_P (x
))
7566 DECL_IN_AGGR_P (x
) = false;
7567 if (DECL_VIRTUAL_P (x
))
7568 CLASSTYPE_NON_AGGREGATE (t
) = true;
7570 else if (TREE_CODE (x
) == FIELD_DECL
)
7572 if (TREE_PROTECTED (x
) || TREE_PRIVATE (x
))
7573 CLASSTYPE_NON_AGGREGATE (t
) = true;
7576 /* Also add a USING_DECL for operator=. We know there'll be (at
7577 least) one, but we don't know the signature(s). We want name
7578 lookup not to fail or recurse into bases. This isn't added
7579 to the template decl list so we drop this at instantiation
7581 tree ass_op
= build_lang_decl (USING_DECL
, assign_op_identifier
,
7583 DECL_CONTEXT (ass_op
) = t
;
7584 USING_DECL_SCOPE (ass_op
) = t
;
7585 DECL_DEPENDENT_P (ass_op
) = true;
7586 DECL_ARTIFICIAL (ass_op
) = true;
7587 DECL_CHAIN (ass_op
) = TYPE_FIELDS (t
);
7588 TYPE_FIELDS (t
) = ass_op
;
7590 TYPE_SIZE (t
) = bitsize_zero_node
;
7591 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7592 /* COMPLETE_TYPE_P is now true. */
7594 set_class_bindings (t
);
7596 /* We need to emit an error message if this type was used as a parameter
7597 and it is an abstract type, even if it is a template. We construct
7598 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7599 account and we call complete_vars with this type, which will check
7600 the PARM_DECLS. Note that while the type is being defined,
7601 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7602 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7603 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7604 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7605 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_PURE_VIRTUAL_P (x
))
7606 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7609 /* Remember current #pragma pack value. */
7610 TYPE_PRECISION (t
) = maximum_field_alignment
;
7612 if (cxx_dialect
< cxx20
)
7614 if (!CLASSTYPE_NON_AGGREGATE (t
)
7615 && type_has_user_provided_or_explicit_constructor (t
))
7616 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7618 else if (TYPE_HAS_USER_CONSTRUCTOR (t
))
7619 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7621 /* Fix up any variants we've already built. */
7622 fixup_type_variants (t
);
7625 finish_struct_1 (t
);
7626 /* COMPLETE_TYPE_P is now true. */
7628 maybe_warn_about_overly_private_class (t
);
7630 if (is_std_init_list (t
))
7632 /* People keep complaining that the compiler crashes on an invalid
7633 definition of initializer_list, so I guess we should explicitly
7634 reject it. What the compiler internals care about is that it's a
7635 template and has a pointer field followed by size_type field. */
7637 if (processing_template_decl
)
7639 tree f
= next_initializable_field (TYPE_FIELDS (t
));
7640 if (f
&& TYPE_PTR_P (TREE_TYPE (f
)))
7642 f
= next_initializable_field (DECL_CHAIN (f
));
7643 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7648 fatal_error (input_location
, "definition of %qD does not match "
7649 "%<#include <initializer_list>%>", TYPE_NAME (t
));
7652 input_location
= saved_loc
;
7654 TYPE_BEING_DEFINED (t
) = 0;
7656 if (current_class_type
)
7659 error ("trying to finish struct, but kicked out due to previous parse errors");
7662 for (tree decl
= TYPE_FIELDS (t
); decl
; decl
= DECL_CHAIN (decl
))
7663 if (TREE_CODE (decl
) == FUNCTION_DECL
7664 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
7665 if (tree attr
= lookup_attribute ("omp declare variant base",
7666 DECL_ATTRIBUTES (decl
)))
7667 omp_declare_variant_finalize (decl
, attr
);
7669 if (processing_template_decl
&& at_function_scope_p ()
7670 /* Lambdas are defined by the LAMBDA_EXPR. */
7671 && !LAMBDA_TYPE_P (t
))
7672 add_stmt (build_min (TAG_DEFN
, t
));
7677 /* Hash table to avoid endless recursion when handling references. */
7678 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7680 /* Return the dynamic type of INSTANCE, if known.
7681 Used to determine whether the virtual function table is needed
7684 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7685 of our knowledge of its type. *NONNULL should be initialized
7686 before this function is called. */
7689 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7691 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7693 switch (TREE_CODE (instance
))
7696 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
7699 return RECUR (TREE_OPERAND (instance
, 0));
7702 /* This is a call to a constructor, hence it's never zero. */
7703 if (CALL_EXPR_FN (instance
)
7704 && TREE_HAS_CONSTRUCTOR (instance
))
7708 return TREE_TYPE (instance
);
7713 /* This is a call to a constructor, hence it's never zero. */
7714 if (TREE_HAS_CONSTRUCTOR (instance
))
7718 return TREE_TYPE (instance
);
7720 return RECUR (TREE_OPERAND (instance
, 0));
7722 case POINTER_PLUS_EXPR
:
7725 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
7726 return RECUR (TREE_OPERAND (instance
, 0));
7727 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7728 /* Propagate nonnull. */
7729 return RECUR (TREE_OPERAND (instance
, 0));
7734 return RECUR (TREE_OPERAND (instance
, 0));
7737 instance
= TREE_OPERAND (instance
, 0);
7740 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7741 with a real object -- given &p->f, p can still be null. */
7742 tree t
= get_base_address (instance
);
7743 /* ??? Probably should check DECL_WEAK here. */
7744 if (t
&& DECL_P (t
))
7747 return RECUR (instance
);
7750 /* If this component is really a base class reference, then the field
7751 itself isn't definitive. */
7752 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
7753 return RECUR (TREE_OPERAND (instance
, 0));
7754 return RECUR (TREE_OPERAND (instance
, 1));
7758 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
7759 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7763 return TREE_TYPE (TREE_TYPE (instance
));
7769 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7773 return TREE_TYPE (instance
);
7775 else if (instance
== current_class_ptr
)
7780 /* if we're in a ctor or dtor, we know our type. If
7781 current_class_ptr is set but we aren't in a function, we're in
7782 an NSDMI (and therefore a constructor). */
7783 if (current_scope () != current_function_decl
7784 || (DECL_LANG_SPECIFIC (current_function_decl
)
7785 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7786 || DECL_DESTRUCTOR_P (current_function_decl
))))
7790 return TREE_TYPE (TREE_TYPE (instance
));
7793 else if (TYPE_REF_P (TREE_TYPE (instance
)))
7795 /* We only need one hash table because it is always left empty. */
7796 if (!fixed_type_or_null_ref_ht
)
7797 fixed_type_or_null_ref_ht
7798 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7800 /* Reference variables should be references to objects. */
7804 /* Enter the INSTANCE in a table to prevent recursion; a
7805 variable's initializer may refer to the variable
7807 if (VAR_P (instance
)
7808 && DECL_INITIAL (instance
)
7809 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7810 && !fixed_type_or_null_ref_ht
->find (instance
))
7815 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7817 type
= RECUR (DECL_INITIAL (instance
));
7818 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7825 case VIEW_CONVERT_EXPR
:
7826 if (location_wrapper_p (instance
))
7827 return RECUR (TREE_OPERAND (instance
, 0));
7829 /* TODO: Recursion may be correct for some non-location-wrapper
7830 uses of VIEW_CONVERT_EXPR. */
7839 /* Return nonzero if the dynamic type of INSTANCE is known, and
7840 equivalent to the static type. We also handle the case where
7841 INSTANCE is really a pointer. Return negative if this is a
7842 ctor/dtor. There the dynamic type is known, but this might not be
7843 the most derived base of the original object, and hence virtual
7844 bases may not be laid out according to this type.
7846 Used to determine whether the virtual function table is needed
7849 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7850 of our knowledge of its type. *NONNULL should be initialized
7851 before this function is called. */
7854 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7856 tree t
= TREE_TYPE (instance
);
7860 /* processing_template_decl can be false in a template if we're in
7861 instantiate_non_dependent_expr, but we still want to suppress
7863 if (in_template_function ())
7865 /* In a template we only care about the type of the result. */
7871 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7872 if (INDIRECT_TYPE_P (t
))
7874 if (CLASS_TYPE_P (t
) && CLASSTYPE_FINAL (t
))
7876 if (fixed
== NULL_TREE
)
7878 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7880 return cdtorp
? -1 : 1;
7885 init_class_processing (void)
7887 current_class_depth
= 0;
7888 current_class_stack_size
= 10;
7890 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7891 sizeof_biggest_empty_class
= size_zero_node
;
7893 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7894 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7895 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7898 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7901 restore_class_cache (void)
7905 /* We are re-entering the same class we just left, so we don't
7906 have to search the whole inheritance matrix to find all the
7907 decls to bind again. Instead, we install the cached
7908 class_shadowed list and walk through it binding names. */
7909 push_binding_level (previous_class_level
);
7910 class_binding_level
= previous_class_level
;
7911 /* Restore IDENTIFIER_TYPE_VALUE. */
7912 for (type
= class_binding_level
->type_shadowed
;
7914 type
= TREE_CHAIN (type
))
7915 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7918 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7919 appropriate for TYPE.
7921 So that we may avoid calls to lookup_name, we cache the _TYPE
7922 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7924 For multiple inheritance, we perform a two-pass depth-first search
7925 of the type lattice. */
7928 pushclass (tree type
)
7930 class_stack_node_t csn
;
7932 type
= TYPE_MAIN_VARIANT (type
);
7934 /* Make sure there is enough room for the new entry on the stack. */
7935 if (current_class_depth
+ 1 >= current_class_stack_size
)
7937 current_class_stack_size
*= 2;
7939 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7940 current_class_stack_size
);
7943 /* Insert a new entry on the class stack. */
7944 csn
= current_class_stack
+ current_class_depth
;
7945 csn
->name
= current_class_name
;
7946 csn
->type
= current_class_type
;
7947 csn
->access
= current_access_specifier
;
7948 csn
->names_used
= 0;
7950 current_class_depth
++;
7952 /* Now set up the new type. */
7953 current_class_name
= TYPE_NAME (type
);
7954 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7955 current_class_name
= DECL_NAME (current_class_name
);
7956 current_class_type
= type
;
7958 /* By default, things in classes are private, while things in
7959 structures or unions are public. */
7960 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7961 ? access_private_node
7962 : access_public_node
);
7964 if (previous_class_level
7965 && type
!= previous_class_level
->this_entity
7966 && current_class_depth
== 1)
7968 /* Forcibly remove any old class remnants. */
7969 invalidate_class_lookup_cache ();
7972 if (!previous_class_level
7973 || type
!= previous_class_level
->this_entity
7974 || current_class_depth
> 1)
7977 restore_class_cache ();
7980 /* Get out of the current class scope. If we were in a class scope
7981 previously, that is the one popped to. */
7988 current_class_depth
--;
7989 current_class_name
= current_class_stack
[current_class_depth
].name
;
7990 current_class_type
= current_class_stack
[current_class_depth
].type
;
7991 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7992 if (current_class_stack
[current_class_depth
].names_used
)
7993 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
7996 /* Mark the top of the class stack as hidden. */
7999 push_class_stack (void)
8001 if (current_class_depth
)
8002 ++current_class_stack
[current_class_depth
- 1].hidden
;
8005 /* Mark the top of the class stack as un-hidden. */
8008 pop_class_stack (void)
8010 if (current_class_depth
)
8011 --current_class_stack
[current_class_depth
- 1].hidden
;
8014 /* If the class type currently being defined is either T or
8015 a nested type of T, returns the type from the current_class_stack,
8016 which might be equivalent to but not equal to T in case of
8017 constrained partial specializations. */
8020 currently_open_class (tree t
)
8024 if (!CLASS_TYPE_P (t
))
8027 t
= TYPE_MAIN_VARIANT (t
);
8029 /* We start looking from 1 because entry 0 is from global scope,
8031 for (i
= current_class_depth
; i
> 0; --i
)
8034 if (i
== current_class_depth
)
8035 c
= current_class_type
;
8038 if (current_class_stack
[i
].hidden
)
8040 c
= current_class_stack
[i
].type
;
8044 if (same_type_p (c
, t
))
8050 /* If either current_class_type or one of its enclosing classes are derived
8051 from T, return the appropriate type. Used to determine how we found
8052 something via unqualified lookup. */
8055 currently_open_derived_class (tree t
)
8059 /* The bases of a dependent type are unknown. */
8060 if (dependent_type_p (t
))
8063 if (!current_class_type
)
8066 if (DERIVED_FROM_P (t
, current_class_type
))
8067 return current_class_type
;
8069 for (i
= current_class_depth
- 1; i
> 0; --i
)
8071 if (current_class_stack
[i
].hidden
)
8073 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
8074 return current_class_stack
[i
].type
;
8080 /* Return the outermost enclosing class type that is still open, or
8084 outermost_open_class (void)
8086 if (!current_class_type
)
8089 if (TYPE_BEING_DEFINED (current_class_type
))
8090 r
= current_class_type
;
8091 for (int i
= current_class_depth
- 1; i
> 0; --i
)
8093 if (current_class_stack
[i
].hidden
)
8095 tree t
= current_class_stack
[i
].type
;
8096 if (!TYPE_BEING_DEFINED (t
))
8103 /* Returns the innermost class type which is not a lambda closure type. */
8106 current_nonlambda_class_type (void)
8108 tree type
= current_class_type
;
8109 while (type
&& LAMBDA_TYPE_P (type
))
8110 type
= decl_type_context (TYPE_NAME (type
));
8114 /* When entering a class scope, all enclosing class scopes' names with
8115 static meaning (static variables, static functions, types and
8116 enumerators) have to be visible. This recursive function calls
8117 pushclass for all enclosing class contexts until global or a local
8118 scope is reached. TYPE is the enclosed class. */
8121 push_nested_class (tree type
)
8123 /* A namespace might be passed in error cases, like A::B:C. */
8124 if (type
== NULL_TREE
8125 || !CLASS_TYPE_P (type
))
8128 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
8133 /* Undoes a push_nested_class call. */
8136 pop_nested_class (void)
8138 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
8141 if (context
&& CLASS_TYPE_P (context
))
8142 pop_nested_class ();
8145 /* Returns the number of extern "LANG" blocks we are nested within. */
8148 current_lang_depth (void)
8150 return vec_safe_length (current_lang_base
);
8153 /* Set global variables CURRENT_LANG_NAME to appropriate value
8154 so that behavior of name-mangling machinery is correct. */
8157 push_lang_context (tree name
)
8159 vec_safe_push (current_lang_base
, current_lang_name
);
8161 if (name
== lang_name_cplusplus
)
8162 current_lang_name
= name
;
8163 else if (name
== lang_name_c
)
8164 current_lang_name
= name
;
8166 error ("language string %<\"%E\"%> not recognized", name
);
8169 /* Get out of the current language scope. */
8172 pop_lang_context (void)
8174 current_lang_name
= current_lang_base
->pop ();
8177 /* Type instantiation routines. */
8179 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8180 matches the TARGET_TYPE. If there is no satisfactory match, return
8181 error_mark_node, and issue an error & warning messages under
8182 control of FLAGS. Permit pointers to member function if FLAGS
8183 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8184 a template-id, and EXPLICIT_TARGS are the explicitly provided
8187 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8188 is the base path used to reference those member functions. If
8189 the address is resolved to a member function, access checks will be
8190 performed and errors issued if appropriate. */
8193 resolve_address_of_overloaded_function (tree target_type
,
8195 tsubst_flags_t complain
,
8197 tree explicit_targs
,
8200 /* Here's what the standard says:
8204 If the name is a function template, template argument deduction
8205 is done, and if the argument deduction succeeds, the deduced
8206 arguments are used to generate a single template function, which
8207 is added to the set of overloaded functions considered.
8209 Non-member functions and static member functions match targets of
8210 type "pointer-to-function" or "reference-to-function." Nonstatic
8211 member functions match targets of type "pointer-to-member
8212 function;" the function type of the pointer to member is used to
8213 select the member function from the set of overloaded member
8214 functions. If a non-static member function is selected, the
8215 reference to the overloaded function name is required to have the
8216 form of a pointer to member as described in 5.3.1.
8218 If more than one function is selected, any template functions in
8219 the set are eliminated if the set also contains a non-template
8220 function, and any given template function is eliminated if the
8221 set contains a second template function that is more specialized
8222 than the first according to the partial ordering rules 14.5.5.2.
8223 After such eliminations, if any, there shall remain exactly one
8224 selected function. */
8227 /* We store the matches in a TREE_LIST rooted here. The functions
8228 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8229 interoperability with most_specialized_instantiation. */
8230 tree matches
= NULL_TREE
;
8232 tree target_fn_type
;
8234 /* By the time we get here, we should be seeing only real
8235 pointer-to-member types, not the internal POINTER_TYPE to
8236 METHOD_TYPE representation. */
8237 gcc_assert (!TYPE_PTR_P (target_type
)
8238 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
8240 gcc_assert (is_overloaded_fn (overload
));
8242 /* Check that the TARGET_TYPE is reasonable. */
8243 if (TYPE_PTRFN_P (target_type
)
8244 || TYPE_REFFN_P (target_type
))
8246 else if (TYPE_PTRMEMFUNC_P (target_type
))
8247 /* This is OK, too. */
8249 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
8250 /* This is OK, too. This comes from a conversion to reference
8252 target_type
= build_reference_type (target_type
);
8255 if (complain
& tf_error
)
8256 error ("cannot resolve overloaded function %qD based on"
8257 " conversion to type %qT",
8258 OVL_NAME (overload
), target_type
);
8259 return error_mark_node
;
8262 /* Non-member functions and static member functions match targets of type
8263 "pointer-to-function" or "reference-to-function." Nonstatic member
8264 functions match targets of type "pointer-to-member-function;" the
8265 function type of the pointer to member is used to select the member
8266 function from the set of overloaded member functions.
8268 So figure out the FUNCTION_TYPE that we want to match against. */
8269 target_fn_type
= static_fn_type (target_type
);
8271 /* If we can find a non-template function that matches, we can just
8272 use it. There's no point in generating template instantiations
8273 if we're just going to throw them out anyhow. But, of course, we
8274 can only do this when we don't *need* a template function. */
8276 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8280 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8281 /* We're not looking for templates just yet. */
8284 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
) != is_ptrmem
)
8285 /* We're looking for a non-static member, and this isn't
8286 one, or vice versa. */
8289 /* In C++17 we need the noexcept-qualifier to compare types. */
8290 if (flag_noexcept_type
8291 && !maybe_instantiate_noexcept (fn
, complain
))
8294 /* See if there's a match. */
8295 tree fntype
= static_fn_type (fn
);
8296 if (same_type_p (target_fn_type
, fntype
)
8297 || fnptr_conv_p (target_fn_type
, fntype
))
8298 matches
= tree_cons (fn
, NULL_TREE
, matches
);
8301 /* Now, if we've already got a match (or matches), there's no need
8302 to proceed to the template functions. But, if we don't have a
8303 match we need to look at them, too. */
8306 tree target_arg_types
;
8307 tree target_ret_type
;
8309 unsigned int nargs
, ia
;
8312 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
8313 target_ret_type
= TREE_TYPE (target_fn_type
);
8315 nargs
= list_length (target_arg_types
);
8316 args
= XALLOCAVEC (tree
, nargs
);
8317 for (arg
= target_arg_types
, ia
= 0;
8318 arg
!= NULL_TREE
&& arg
!= void_list_node
;
8319 arg
= TREE_CHAIN (arg
), ++ia
)
8320 args
[ia
] = TREE_VALUE (arg
);
8323 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8329 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8330 /* We're only looking for templates. */
8333 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8335 /* We're not looking for a non-static member, and this is
8336 one, or vice versa. */
8339 tree ret
= target_ret_type
;
8341 /* If the template has a deduced return type, don't expose it to
8342 template argument deduction. */
8343 if (undeduced_auto_decl (fn
))
8346 /* Try to do argument deduction. */
8347 targs
= make_tree_vec (DECL_NTPARMS (fn
));
8348 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
8350 DEDUCE_EXACT
, LOOKUP_NORMAL
,
8351 NULL
, false, false);
8352 if (instantiation
== error_mark_node
)
8353 /* Instantiation failed. */
8356 /* Constraints must be satisfied. This is done before
8357 return type deduction since that instantiates the
8359 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
8362 /* And now force instantiation to do return type deduction. */
8363 if (undeduced_auto_decl (instantiation
))
8366 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
8369 require_deduced_type (instantiation
);
8372 /* In C++17 we need the noexcept-qualifier to compare types. */
8373 if (flag_noexcept_type
)
8374 maybe_instantiate_noexcept (instantiation
, complain
);
8376 /* See if there's a match. */
8377 tree fntype
= static_fn_type (instantiation
);
8378 if (same_type_p (target_fn_type
, fntype
)
8379 || fnptr_conv_p (target_fn_type
, fntype
))
8380 matches
= tree_cons (instantiation
, fn
, matches
);
8383 /* Now, remove all but the most specialized of the matches. */
8386 tree match
= most_specialized_instantiation (matches
);
8388 if (match
!= error_mark_node
)
8389 matches
= tree_cons (TREE_PURPOSE (match
),
8395 /* Now we should have exactly one function in MATCHES. */
8396 if (matches
== NULL_TREE
)
8398 /* There were *no* matches. */
8399 if (complain
& tf_error
)
8401 error ("no matches converting function %qD to type %q#T",
8402 OVL_NAME (overload
), target_type
);
8404 print_candidates (overload
);
8406 return error_mark_node
;
8408 else if (TREE_CHAIN (matches
))
8410 /* There were too many matches. First check if they're all
8411 the same function. */
8412 tree match
= NULL_TREE
;
8414 fn
= TREE_PURPOSE (matches
);
8416 /* For multi-versioned functions, more than one match is just fine and
8417 decls_match will return false as they are different. */
8418 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
8419 if (!decls_match (fn
, TREE_PURPOSE (match
))
8420 && !targetm
.target_option
.function_versions
8421 (fn
, TREE_PURPOSE (match
)))
8426 if (complain
& tf_error
)
8428 error ("converting overloaded function %qD to type %q#T is ambiguous",
8429 OVL_NAME (overload
), target_type
);
8431 /* Since print_candidates expects the functions in the
8432 TREE_VALUE slot, we flip them here. */
8433 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8434 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8436 print_candidates (matches
);
8439 return error_mark_node
;
8443 /* Good, exactly one match. Now, convert it to the correct type. */
8444 fn
= TREE_PURPOSE (matches
);
8446 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8447 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8449 static int explained
;
8451 if (!(complain
& tf_error
))
8452 return error_mark_node
;
8454 auto_diagnostic_group d
;
8455 if (permerror (input_location
, "assuming pointer to member %qD", fn
)
8458 inform (input_location
, "(a pointer to member can only be "
8459 "formed with %<&%E%>)", fn
);
8464 /* If a pointer to a function that is multi-versioned is requested, the
8465 pointer to the dispatcher function is returned instead. This works
8466 well because indirectly calling the function will dispatch the right
8467 function version at run-time. */
8468 if (DECL_FUNCTION_VERSIONED (fn
))
8470 fn
= get_function_version_dispatcher (fn
);
8472 return error_mark_node
;
8473 /* Mark all the versions corresponding to the dispatcher as used. */
8474 if (!(complain
& tf_conv
))
8475 mark_versions_used (fn
);
8478 /* If we're doing overload resolution purely for the purpose of
8479 determining conversion sequences, we should not consider the
8480 function used. If this conversion sequence is selected, the
8481 function will be marked as used at this point. */
8482 if (!(complain
& tf_conv
))
8484 /* Make =delete work with SFINAE. */
8485 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8486 return error_mark_node
;
8487 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8488 return error_mark_node
;
8491 /* We could not check access to member functions when this
8492 expression was originally created since we did not know at that
8493 time to which function the expression referred. */
8494 if (DECL_FUNCTION_MEMBER_P (fn
))
8496 gcc_assert (access_path
);
8497 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8500 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8501 return cp_build_addr_expr (fn
, complain
);
8504 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8505 will mark the function as addressed, but here we must do it
8507 cxx_mark_addressable (fn
);
8513 /* This function will instantiate the type of the expression given in
8514 RHS to match the type of LHSTYPE. If errors exist, then return
8515 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8516 we complain on errors. If we are not complaining, never modify rhs,
8517 as overload resolution wants to try many possible instantiations, in
8518 the hope that at least one will work.
8520 For non-recursive calls, LHSTYPE should be a function, pointer to
8521 function, or a pointer to member function. */
8524 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8526 tsubst_flags_t complain_in
= complain
;
8527 tree access_path
= NULL_TREE
;
8529 complain
&= ~tf_ptrmem_ok
;
8531 if (lhstype
== unknown_type_node
)
8533 if (complain
& tf_error
)
8534 error ("not enough type information");
8535 return error_mark_node
;
8538 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8540 tree fntype
= non_reference (lhstype
);
8541 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8543 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8545 if (flag_ms_extensions
8546 && TYPE_PTRMEMFUNC_P (fntype
)
8547 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8548 /* Microsoft allows `A::f' to be resolved to a
8549 pointer-to-member. */
8553 if (complain
& tf_error
)
8554 error ("cannot convert %qE from type %qT to type %qT",
8555 rhs
, TREE_TYPE (rhs
), fntype
);
8556 return error_mark_node
;
8560 /* If we instantiate a template, and it is a A ?: C expression
8561 with omitted B, look through the SAVE_EXPR. */
8562 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8563 rhs
= TREE_OPERAND (rhs
, 0);
8565 if (BASELINK_P (rhs
))
8567 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8568 rhs
= BASELINK_FUNCTIONS (rhs
);
8571 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8572 deduce any type information. */
8573 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
8575 if (complain
& tf_error
)
8576 error ("not enough type information");
8577 return error_mark_node
;
8580 /* There are only a few kinds of expressions that may have a type
8581 dependent on overload resolution. */
8582 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
8583 || TREE_CODE (rhs
) == COMPONENT_REF
8584 || is_overloaded_fn (rhs
)
8585 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8587 /* This should really only be used when attempting to distinguish
8588 what sort of a pointer to function we have. For now, any
8589 arithmetic operation which is not supported on pointers
8590 is rejected as an error. */
8592 switch (TREE_CODE (rhs
))
8596 tree member
= TREE_OPERAND (rhs
, 1);
8598 member
= instantiate_type (lhstype
, member
, complain
);
8599 if (member
!= error_mark_node
8600 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8601 /* Do not lose object's side effects. */
8602 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8603 TREE_OPERAND (rhs
, 0), member
);
8608 rhs
= TREE_OPERAND (rhs
, 1);
8609 if (BASELINK_P (rhs
))
8610 return instantiate_type (lhstype
, rhs
, complain_in
);
8612 /* This can happen if we are forming a pointer-to-member for a
8614 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8618 case TEMPLATE_ID_EXPR
:
8620 tree fns
= TREE_OPERAND (rhs
, 0);
8621 tree args
= TREE_OPERAND (rhs
, 1);
8624 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8625 /*template_only=*/true,
8632 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8633 /*template_only=*/false,
8634 /*explicit_targs=*/NULL_TREE
,
8639 if (PTRMEM_OK_P (rhs
))
8640 complain
|= tf_ptrmem_ok
;
8642 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8646 return error_mark_node
;
8651 return error_mark_node
;
8654 /* Return the name of the virtual function pointer field
8655 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8656 this may have to look back through base types to find the
8657 ultimate field name. (For single inheritance, these could
8658 all be the same name. Who knows for multiple inheritance). */
8661 get_vfield_name (tree type
)
8663 tree binfo
, base_binfo
;
8665 for (binfo
= TYPE_BINFO (type
);
8666 BINFO_N_BASE_BINFOS (binfo
);
8669 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8671 if (BINFO_VIRTUAL_P (base_binfo
)
8672 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8676 type
= BINFO_TYPE (binfo
);
8677 tree ctor_name
= constructor_name (type
);
8678 char *buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8679 + IDENTIFIER_LENGTH (ctor_name
) + 2);
8680 sprintf (buf
, VFIELD_NAME_FORMAT
, IDENTIFIER_POINTER (ctor_name
));
8681 return get_identifier (buf
);
8684 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8685 according to [class]:
8686 The class-name is also inserted
8687 into the scope of the class itself. For purposes of access checking,
8688 the inserted class name is treated as if it were a public member name. */
8691 build_self_reference (void)
8693 tree name
= DECL_NAME (TYPE_NAME (current_class_type
));
8694 tree decl
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8696 DECL_NONLOCAL (decl
) = 1;
8697 DECL_CONTEXT (decl
) = current_class_type
;
8698 DECL_ARTIFICIAL (decl
) = 1;
8699 SET_DECL_SELF_REFERENCE_P (decl
);
8700 set_underlying_type (decl
);
8702 if (processing_template_decl
)
8703 decl
= push_template_decl (decl
);
8705 tree saved_cas
= current_access_specifier
;
8706 current_access_specifier
= access_public_node
;
8707 finish_member_declaration (decl
);
8708 current_access_specifier
= saved_cas
;
8711 /* Returns 1 if TYPE contains only padding bytes. */
8714 is_empty_class (tree type
)
8716 if (type
== error_mark_node
)
8719 if (! CLASS_TYPE_P (type
))
8722 return CLASSTYPE_EMPTY_P (type
);
8725 /* Returns true if TYPE contains no actual data, just various
8726 possible combinations of empty classes. If IGNORE_VPTR is true,
8727 a vptr doesn't prevent the class from being considered empty. Typically
8728 we want to ignore the vptr on assignment, and not on initialization. */
8731 is_really_empty_class (tree type
, bool ignore_vptr
)
8733 if (CLASS_TYPE_P (type
))
8740 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8741 out, but we'd like to be able to check this before then. */
8742 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8745 if (!ignore_vptr
&& TYPE_CONTAINS_VPTR_P (type
))
8748 for (binfo
= TYPE_BINFO (type
), i
= 0;
8749 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8750 if (!is_really_empty_class (BINFO_TYPE (base_binfo
), ignore_vptr
))
8752 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8753 if (TREE_CODE (field
) == FIELD_DECL
8754 && !DECL_ARTIFICIAL (field
)
8755 /* An unnamed bit-field is not a data member. */
8756 && !DECL_UNNAMED_BIT_FIELD (field
)
8757 && !is_really_empty_class (TREE_TYPE (field
), ignore_vptr
))
8761 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8762 return (integer_zerop (array_type_nelts_top (type
))
8763 || is_really_empty_class (TREE_TYPE (type
), ignore_vptr
));
8767 /* Note that NAME was looked up while the current class was being
8768 defined and that the result of that lookup was DECL. */
8771 maybe_note_name_used_in_class (tree name
, tree decl
)
8773 splay_tree names_used
;
8775 /* If we're not defining a class, there's nothing to do. */
8776 if (!(innermost_scope_kind() == sk_class
8777 && TYPE_BEING_DEFINED (current_class_type
)
8778 && !LAMBDA_TYPE_P (current_class_type
)))
8781 /* If there's already a binding for this NAME, then we don't have
8782 anything to worry about. */
8783 if (lookup_member (current_class_type
, name
,
8784 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8787 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8788 current_class_stack
[current_class_depth
- 1].names_used
8789 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8790 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8792 splay_tree_insert (names_used
,
8793 (splay_tree_key
) name
,
8794 (splay_tree_value
) decl
);
8797 /* Note that NAME was declared (as DECL) in the current class. Check
8798 to see that the declaration is valid. */
8801 note_name_declared_in_class (tree name
, tree decl
)
8803 splay_tree names_used
;
8806 /* Look to see if we ever used this name. */
8808 = current_class_stack
[current_class_depth
- 1].names_used
;
8811 /* The C language allows members to be declared with a type of the same
8812 name, and the C++ standard says this diagnostic is not required. So
8813 allow it in extern "C" blocks unless predantic is specified.
8814 Allow it in all cases if -ms-extensions is specified. */
8815 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8816 || flag_ms_extensions
)
8818 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8821 /* [basic.scope.class]
8823 A name N used in a class S shall refer to the same declaration
8824 in its context and when re-evaluated in the completed scope of
8826 if (permerror (location_of (decl
),
8827 "declaration of %q#D changes meaning of %qD",
8828 decl
, OVL_NAME (decl
)))
8829 inform (location_of ((tree
) n
->value
),
8830 "%qD declared here as %q#D",
8831 OVL_NAME (decl
), (tree
) n
->value
);
8835 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8836 Secondary vtables are merged with primary vtables; this function
8837 will return the VAR_DECL for the primary vtable. */
8840 get_vtbl_decl_for_binfo (tree binfo
)
8844 decl
= BINFO_VTABLE (binfo
);
8845 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8847 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
8848 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8851 gcc_assert (VAR_P (decl
));
8856 /* Returns the binfo for the primary base of BINFO. If the resulting
8857 BINFO is a virtual base, and it is inherited elsewhere in the
8858 hierarchy, then the returned binfo might not be the primary base of
8859 BINFO in the complete object. Check BINFO_PRIMARY_P or
8860 BINFO_LOST_PRIMARY_P to be sure. */
8863 get_primary_binfo (tree binfo
)
8867 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8871 return copied_binfo (primary_base
, binfo
);
8874 /* As above, but iterate until we reach the binfo that actually provides the
8878 most_primary_binfo (tree binfo
)
8881 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
8882 && !BINFO_LOST_PRIMARY_P (b
))
8884 tree primary_base
= get_primary_binfo (b
);
8885 gcc_assert (BINFO_PRIMARY_P (primary_base
)
8886 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
8892 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8893 type. Note that the virtual inheritance might be above or below BINFO in
8897 vptr_via_virtual_p (tree binfo
)
8900 binfo
= TYPE_BINFO (binfo
);
8901 tree primary
= most_primary_binfo (binfo
);
8902 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8903 a morally virtual base. */
8904 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
8905 return virt
!= NULL_TREE
;
8908 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8911 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8914 fprintf (stream
, "%*s", indent
, "");
8918 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8919 INDENT should be zero when called from the top level; it is
8920 incremented recursively. IGO indicates the next expected BINFO in
8921 inheritance graph ordering. */
8924 dump_class_hierarchy_r (FILE *stream
,
8934 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8935 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8936 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8939 fprintf (stream
, "alternative-path\n");
8942 igo
= TREE_CHAIN (binfo
);
8944 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8945 tree_to_shwi (BINFO_OFFSET (binfo
)));
8946 if (is_empty_class (BINFO_TYPE (binfo
)))
8947 fprintf (stream
, " empty");
8948 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8949 fprintf (stream
, " nearly-empty");
8950 if (BINFO_VIRTUAL_P (binfo
))
8951 fprintf (stream
, " virtual");
8952 fprintf (stream
, "\n");
8954 if (BINFO_PRIMARY_P (binfo
))
8956 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8957 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8958 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8959 TFF_PLAIN_IDENTIFIER
),
8960 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8962 if (BINFO_LOST_PRIMARY_P (binfo
))
8964 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8965 fprintf (stream
, " lost-primary");
8968 fprintf (stream
, "\n");
8970 if (!(flags
& TDF_SLIM
))
8974 if (BINFO_SUBVTT_INDEX (binfo
))
8976 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8977 fprintf (stream
, " subvttidx=%s",
8978 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8979 TFF_PLAIN_IDENTIFIER
));
8981 if (BINFO_VPTR_INDEX (binfo
))
8983 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8984 fprintf (stream
, " vptridx=%s",
8985 expr_as_string (BINFO_VPTR_INDEX (binfo
),
8986 TFF_PLAIN_IDENTIFIER
));
8988 if (BINFO_VPTR_FIELD (binfo
))
8990 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8991 fprintf (stream
, " vbaseoffset=%s",
8992 expr_as_string (BINFO_VPTR_FIELD (binfo
),
8993 TFF_PLAIN_IDENTIFIER
));
8995 if (BINFO_VTABLE (binfo
))
8997 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8998 fprintf (stream
, " vptr=%s",
8999 expr_as_string (BINFO_VTABLE (binfo
),
9000 TFF_PLAIN_IDENTIFIER
));
9004 fprintf (stream
, "\n");
9007 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9008 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
9013 /* Dump the BINFO hierarchy for T. */
9016 dump_class_hierarchy_1 (FILE *stream
, dump_flags_t flags
, tree t
)
9018 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9019 fprintf (stream
, " size=%lu align=%lu\n",
9020 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
9021 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
9022 if (tree as_base
= CLASSTYPE_AS_BASE (t
))
9023 fprintf (stream
, " base size=%lu base align=%lu\n",
9024 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base
))
9026 (unsigned long)(TYPE_ALIGN (as_base
) / BITS_PER_UNIT
));
9027 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
9028 fprintf (stream
, "\n");
9031 /* Debug interface to hierarchy dumping. */
9034 debug_class (tree t
)
9036 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
9040 dump_class_hierarchy (tree t
)
9043 if (FILE *stream
= dump_begin (class_dump_id
, &flags
))
9045 dump_class_hierarchy_1 (stream
, flags
, t
);
9046 dump_end (class_dump_id
, stream
);
9051 dump_array (FILE * stream
, tree decl
)
9054 unsigned HOST_WIDE_INT ix
;
9056 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
9058 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
9060 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
9061 fprintf (stream
, " %s entries",
9062 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
9063 TFF_PLAIN_IDENTIFIER
));
9064 fprintf (stream
, "\n");
9066 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
9068 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
9069 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
9073 dump_vtable (tree t
, tree binfo
, tree vtable
)
9076 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9081 if (!(flags
& TDF_SLIM
))
9083 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
9085 fprintf (stream
, "%s for %s",
9086 ctor_vtbl_p
? "Construction vtable" : "Vtable",
9087 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
9090 if (!BINFO_VIRTUAL_P (binfo
))
9091 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
9092 (HOST_WIDE_INT
) (uintptr_t) binfo
);
9093 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9095 fprintf (stream
, "\n");
9096 dump_array (stream
, vtable
);
9097 fprintf (stream
, "\n");
9100 dump_end (class_dump_id
, stream
);
9104 dump_vtt (tree t
, tree vtt
)
9107 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9112 if (!(flags
& TDF_SLIM
))
9114 fprintf (stream
, "VTT for %s\n",
9115 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9116 dump_array (stream
, vtt
);
9117 fprintf (stream
, "\n");
9120 dump_end (class_dump_id
, stream
);
9123 /* Dump a function or thunk and its thunkees. */
9126 dump_thunk (FILE *stream
, int indent
, tree thunk
)
9128 static const char spaces
[] = " ";
9129 tree name
= DECL_NAME (thunk
);
9132 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
9134 !DECL_THUNK_P (thunk
) ? "function"
9135 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
9136 name
? IDENTIFIER_POINTER (name
) : "<unset>");
9137 if (DECL_THUNK_P (thunk
))
9139 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
9140 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
9142 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
9143 if (!virtual_adjust
)
9145 else if (DECL_THIS_THUNK_P (thunk
))
9146 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
9147 tree_to_shwi (virtual_adjust
));
9149 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
9150 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
9151 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
9152 if (THUNK_ALIAS (thunk
))
9153 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
9155 fprintf (stream
, "\n");
9156 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
9157 dump_thunk (stream
, indent
+ 2, thunks
);
9160 /* Dump the thunks for FN. */
9163 debug_thunks (tree fn
)
9165 dump_thunk (stderr
, 0, fn
);
9168 /* Virtual function table initialization. */
9170 /* Create all the necessary vtables for T and its base classes. */
9173 finish_vtbls (tree t
)
9176 vec
<constructor_elt
, va_gc
> *v
= NULL
;
9177 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
9179 /* We lay out the primary and secondary vtables in one contiguous
9180 vtable. The primary vtable is first, followed by the non-virtual
9181 secondary vtables in inheritance graph order. */
9182 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
9185 /* Then come the virtual bases, also in inheritance graph order. */
9186 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
9188 if (!BINFO_VIRTUAL_P (vbase
))
9190 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
9193 if (BINFO_VTABLE (TYPE_BINFO (t
)))
9194 initialize_vtable (TYPE_BINFO (t
), v
);
9197 /* Initialize the vtable for BINFO with the INITS. */
9200 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
9204 layout_vtable_decl (binfo
, vec_safe_length (inits
));
9205 decl
= get_vtbl_decl_for_binfo (binfo
);
9206 initialize_artificial_var (decl
, inits
);
9207 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
9210 /* Build the VTT (virtual table table) for T.
9211 A class requires a VTT if it has virtual bases.
9214 1 - primary virtual pointer for complete object T
9215 2 - secondary VTTs for each direct non-virtual base of T which requires a
9217 3 - secondary virtual pointers for each direct or indirect base of T which
9218 has virtual bases or is reachable via a virtual path from T.
9219 4 - secondary VTTs for each direct or indirect virtual base of T.
9221 Secondary VTTs look like complete object VTTs without part 4. */
9229 vec
<constructor_elt
, va_gc
> *inits
;
9231 /* Build up the initializers for the VTT. */
9233 index
= size_zero_node
;
9234 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
9236 /* If we didn't need a VTT, we're done. */
9240 /* Figure out the type of the VTT. */
9241 type
= build_array_of_n_type (const_ptr_type_node
,
9244 /* Now, build the VTT object itself. */
9245 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
9246 initialize_artificial_var (vtt
, inits
);
9247 /* Add the VTT to the vtables list. */
9248 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
9249 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
9254 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9255 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9256 and CHAIN the vtable pointer for this binfo after construction is
9257 complete. VALUE can also be another BINFO, in which case we recurse. */
9260 binfo_ctor_vtable (tree binfo
)
9266 vt
= BINFO_VTABLE (binfo
);
9267 if (TREE_CODE (vt
) == TREE_LIST
)
9268 vt
= TREE_VALUE (vt
);
9269 if (TREE_CODE (vt
) == TREE_BINFO
)
9278 /* Data for secondary VTT initialization. */
9279 struct secondary_vptr_vtt_init_data
9281 /* Is this the primary VTT? */
9284 /* Current index into the VTT. */
9287 /* Vector of initializers built up. */
9288 vec
<constructor_elt
, va_gc
> *inits
;
9290 /* The type being constructed by this secondary VTT. */
9291 tree type_being_constructed
;
9294 /* Recursively build the VTT-initializer for BINFO (which is in the
9295 hierarchy dominated by T). INITS points to the end of the initializer
9296 list to date. INDEX is the VTT index where the next element will be
9297 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9298 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9299 for virtual bases of T. When it is not so, we build the constructor
9300 vtables for the BINFO-in-T variant. */
9303 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
9309 secondary_vptr_vtt_init_data data
;
9310 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9312 /* We only need VTTs for subobjects with virtual bases. */
9313 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9316 /* We need to use a construction vtable if this is not the primary
9320 build_ctor_vtbl_group (binfo
, t
);
9322 /* Record the offset in the VTT where this sub-VTT can be found. */
9323 BINFO_SUBVTT_INDEX (binfo
) = *index
;
9326 /* Add the address of the primary vtable for the complete object. */
9327 init
= binfo_ctor_vtable (binfo
);
9328 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9331 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9332 BINFO_VPTR_INDEX (binfo
) = *index
;
9334 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
9336 /* Recursively add the secondary VTTs for non-virtual bases. */
9337 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
9338 if (!BINFO_VIRTUAL_P (b
))
9339 build_vtt_inits (b
, t
, inits
, index
);
9341 /* Add secondary virtual pointers for all subobjects of BINFO with
9342 either virtual bases or reachable along a virtual path, except
9343 subobjects that are non-virtual primary bases. */
9344 data
.top_level_p
= top_level_p
;
9345 data
.index
= *index
;
9346 data
.inits
= *inits
;
9347 data
.type_being_constructed
= BINFO_TYPE (binfo
);
9349 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
9351 *index
= data
.index
;
9353 /* data.inits might have grown as we added secondary virtual pointers.
9354 Make sure our caller knows about the new vector. */
9355 *inits
= data
.inits
;
9358 /* Add the secondary VTTs for virtual bases in inheritance graph
9360 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
9362 if (!BINFO_VIRTUAL_P (b
))
9365 build_vtt_inits (b
, t
, inits
, index
);
9368 /* Remove the ctor vtables we created. */
9369 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
9372 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9373 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9376 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
9378 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
9380 /* We don't care about bases that don't have vtables. */
9381 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
9382 return dfs_skip_bases
;
9384 /* We're only interested in proper subobjects of the type being
9386 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
9389 /* We're only interested in bases with virtual bases or reachable
9390 via a virtual path from the type being constructed. */
9391 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9392 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
9393 return dfs_skip_bases
;
9395 /* We're not interested in non-virtual primary bases. */
9396 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
9399 /* Record the index where this secondary vptr can be found. */
9400 if (data
->top_level_p
)
9402 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9403 BINFO_VPTR_INDEX (binfo
) = data
->index
;
9405 if (BINFO_VIRTUAL_P (binfo
))
9407 /* It's a primary virtual base, and this is not a
9408 construction vtable. Find the base this is primary of in
9409 the inheritance graph, and use that base's vtable
9411 while (BINFO_PRIMARY_P (binfo
))
9412 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
9416 /* Add the initializer for the secondary vptr itself. */
9417 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
9419 /* Advance the vtt index. */
9420 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
9421 TYPE_SIZE_UNIT (ptr_type_node
));
9426 /* Called from build_vtt_inits via dfs_walk. After building
9427 constructor vtables and generating the sub-vtt from them, we need
9428 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9429 binfo of the base whose sub vtt was generated. */
9432 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9434 tree vtable
= BINFO_VTABLE (binfo
);
9436 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9437 /* If this class has no vtable, none of its bases do. */
9438 return dfs_skip_bases
;
9441 /* This might be a primary base, so have no vtable in this
9445 /* If we scribbled the construction vtable vptr into BINFO, clear it
9447 if (TREE_CODE (vtable
) == TREE_LIST
9448 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9449 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9454 /* Build the construction vtable group for BINFO which is in the
9455 hierarchy dominated by T. */
9458 build_ctor_vtbl_group (tree binfo
, tree t
)
9464 vec
<constructor_elt
, va_gc
> *v
;
9466 /* See if we've already created this construction vtable group. */
9467 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9468 if (get_global_binding (id
))
9471 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9472 /* Build a version of VTBL (with the wrong type) for use in
9473 constructing the addresses of secondary vtables in the
9474 construction vtable group. */
9475 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9477 /* Don't export construction vtables from shared libraries. Even on
9478 targets that don't support hidden visibility, this tells
9479 can_refer_decl_in_current_unit_p not to assume that it's safe to
9480 access from a different compilation unit (bz 54314). */
9481 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9482 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9485 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9486 binfo
, vtbl
, t
, &v
);
9488 /* Add the vtables for each of our virtual bases using the vbase in T
9490 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9492 vbase
= TREE_CHAIN (vbase
))
9496 if (!BINFO_VIRTUAL_P (vbase
))
9498 b
= copied_binfo (vbase
, binfo
);
9500 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9503 /* Figure out the type of the construction vtable. */
9504 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9506 TREE_TYPE (vtbl
) = type
;
9507 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9508 layout_decl (vtbl
, 0);
9510 /* Initialize the construction vtable. */
9511 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9512 initialize_artificial_var (vtbl
, v
);
9513 dump_vtable (t
, binfo
, vtbl
);
9516 /* Add the vtbl initializers for BINFO (and its bases other than
9517 non-virtual primaries) to the list of INITS. BINFO is in the
9518 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9519 the constructor the vtbl inits should be accumulated for. (If this
9520 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9521 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9522 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9523 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9524 but are not necessarily the same in terms of layout. */
9527 accumulate_vtbl_inits (tree binfo
,
9532 vec
<constructor_elt
, va_gc
> **inits
)
9536 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9538 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9540 /* If it doesn't have a vptr, we don't do anything. */
9541 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9544 /* If we're building a construction vtable, we're not interested in
9545 subobjects that don't require construction vtables. */
9547 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9548 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9551 /* Build the initializers for the BINFO-in-T vtable. */
9552 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9554 /* Walk the BINFO and its bases. We walk in preorder so that as we
9555 initialize each vtable we can figure out at what offset the
9556 secondary vtable lies from the primary vtable. We can't use
9557 dfs_walk here because we need to iterate through bases of BINFO
9558 and RTTI_BINFO simultaneously. */
9559 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9561 /* Skip virtual bases. */
9562 if (BINFO_VIRTUAL_P (base_binfo
))
9564 accumulate_vtbl_inits (base_binfo
,
9565 BINFO_BASE_BINFO (orig_binfo
, i
),
9566 rtti_binfo
, vtbl
, t
,
9571 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9572 BINFO vtable to L. */
9575 dfs_accumulate_vtbl_inits (tree binfo
,
9580 vec
<constructor_elt
, va_gc
> **l
)
9582 tree vtbl
= NULL_TREE
;
9583 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9587 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9589 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9590 primary virtual base. If it is not the same primary in
9591 the hierarchy of T, we'll need to generate a ctor vtable
9592 for it, to place at its location in T. If it is the same
9593 primary, we still need a VTT entry for the vtable, but it
9594 should point to the ctor vtable for the base it is a
9595 primary for within the sub-hierarchy of RTTI_BINFO.
9597 There are three possible cases:
9599 1) We are in the same place.
9600 2) We are a primary base within a lost primary virtual base of
9602 3) We are primary to something not a base of RTTI_BINFO. */
9605 tree last
= NULL_TREE
;
9607 /* First, look through the bases we are primary to for RTTI_BINFO
9608 or a virtual base. */
9610 while (BINFO_PRIMARY_P (b
))
9612 b
= BINFO_INHERITANCE_CHAIN (b
);
9614 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9617 /* If we run out of primary links, keep looking down our
9618 inheritance chain; we might be an indirect primary. */
9619 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9620 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9624 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9625 base B and it is a base of RTTI_BINFO, this is case 2. In
9626 either case, we share our vtable with LAST, i.e. the
9627 derived-most base within B of which we are a primary. */
9629 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9630 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9631 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9632 binfo_ctor_vtable after everything's been set up. */
9635 /* Otherwise, this is case 3 and we get our own. */
9637 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9640 n_inits
= vec_safe_length (*l
);
9647 /* Add the initializer for this vtable. */
9648 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9649 &non_fn_entries
, l
);
9651 /* Figure out the position to which the VPTR should point. */
9652 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9653 index
= size_binop (MULT_EXPR
,
9654 TYPE_SIZE_UNIT (vtable_entry_type
),
9655 size_int (non_fn_entries
+ n_inits
));
9656 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9660 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9661 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9662 straighten this out. */
9663 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9664 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9665 /* Throw away any unneeded intializers. */
9666 (*l
)->truncate (n_inits
);
9668 /* For an ordinary vtable, set BINFO_VTABLE. */
9669 BINFO_VTABLE (binfo
) = vtbl
;
9672 static GTY(()) tree abort_fndecl_addr
;
9673 static GTY(()) tree dvirt_fn
;
9675 /* Construct the initializer for BINFO's virtual function table. BINFO
9676 is part of the hierarchy dominated by T. If we're building a
9677 construction vtable, the ORIG_BINFO is the binfo we should use to
9678 find the actual function pointers to put in the vtable - but they
9679 can be overridden on the path to most-derived in the graph that
9680 ORIG_BINFO belongs. Otherwise,
9681 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9682 BINFO that should be indicated by the RTTI information in the
9683 vtable; it will be a base class of T, rather than T itself, if we
9684 are building a construction vtable.
9686 The value returned is a TREE_LIST suitable for wrapping in a
9687 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9688 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9689 number of non-function entries in the vtable.
9691 It might seem that this function should never be called with a
9692 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9693 base is always subsumed by a derived class vtable. However, when
9694 we are building construction vtables, we do build vtables for
9695 primary bases; we need these while the primary base is being
9699 build_vtbl_initializer (tree binfo
,
9703 int* non_fn_entries_p
,
9704 vec
<constructor_elt
, va_gc
> **inits
)
9710 vec
<tree
, va_gc
> *vbases
;
9713 /* Initialize VID. */
9714 memset (&vid
, 0, sizeof (vid
));
9717 vid
.rtti_binfo
= rtti_binfo
;
9718 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9719 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9720 vid
.generate_vcall_entries
= true;
9721 /* The first vbase or vcall offset is at index -3 in the vtable. */
9722 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
9724 /* Add entries to the vtable for RTTI. */
9725 build_rtti_vtbl_entries (binfo
, &vid
);
9727 /* Create an array for keeping track of the functions we've
9728 processed. When we see multiple functions with the same
9729 signature, we share the vcall offsets. */
9730 vec_alloc (vid
.fns
, 32);
9731 /* Add the vcall and vbase offset entries. */
9732 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
9734 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9735 build_vbase_offset_vtbl_entries. */
9736 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
9737 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
9738 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
9740 /* If the target requires padding between data entries, add that now. */
9741 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
9743 int n_entries
= vec_safe_length (vid
.inits
);
9745 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
,
9748 /* Move data entries into their new positions and add padding
9749 after the new positions. Iterate backwards so we don't
9750 overwrite entries that we would need to process later. */
9751 for (ix
= n_entries
- 1;
9752 vid
.inits
->iterate (ix
, &e
);
9756 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
9757 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
9759 (*vid
.inits
)[new_position
] = *e
;
9761 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
9763 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
9764 f
->index
= NULL_TREE
;
9765 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9771 if (non_fn_entries_p
)
9772 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9774 /* The initializers for virtual functions were built up in reverse
9775 order. Straighten them out and add them to the running list in one
9777 jx
= vec_safe_length (*inits
);
9778 vec_safe_grow (*inits
, jx
+ vid
.inits
->length (), true);
9780 for (ix
= vid
.inits
->length () - 1;
9781 vid
.inits
->iterate (ix
, &e
);
9785 /* Go through all the ordinary virtual functions, building up
9787 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9791 tree fn
, fn_original
;
9792 tree init
= NULL_TREE
;
9796 if (DECL_THUNK_P (fn
))
9798 if (!DECL_NAME (fn
))
9800 if (THUNK_ALIAS (fn
))
9802 fn
= THUNK_ALIAS (fn
);
9805 fn_original
= THUNK_TARGET (fn
);
9808 /* If the only definition of this function signature along our
9809 primary base chain is from a lost primary, this vtable slot will
9810 never be used, so just zero it out. This is important to avoid
9811 requiring extra thunks which cannot be generated with the function.
9813 We first check this in update_vtable_entry_for_fn, so we handle
9814 restored primary bases properly; we also need to do it here so we
9815 zero out unused slots in ctor vtables, rather than filling them
9816 with erroneous values (though harmless, apart from relocation
9818 if (BV_LOST_PRIMARY (v
))
9819 init
= size_zero_node
;
9823 /* Pull the offset for `this', and the function to call, out of
9825 delta
= BV_DELTA (v
);
9826 vcall_index
= BV_VCALL_INDEX (v
);
9828 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9829 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9831 /* You can't call an abstract virtual function; it's abstract.
9832 So, we replace these functions with __pure_virtual. */
9833 if (DECL_PURE_VIRTUAL_P (fn_original
))
9836 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9838 if (abort_fndecl_addr
== NULL
)
9840 = fold_convert (vfunc_ptr_type_node
,
9841 build_fold_addr_expr (fn
));
9842 init
= abort_fndecl_addr
;
9845 /* Likewise for deleted virtuals. */
9846 else if (DECL_DELETED_FN (fn_original
))
9850 tree name
= get_identifier ("__cxa_deleted_virtual");
9851 dvirt_fn
= get_global_binding (name
);
9853 dvirt_fn
= push_library_fn
9855 build_function_type_list (void_type_node
, NULL_TREE
),
9856 NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
9859 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9860 init
= fold_convert (vfunc_ptr_type_node
,
9861 build_fold_addr_expr (fn
));
9865 if (!integer_zerop (delta
) || vcall_index
)
9867 fn
= make_thunk (fn
, /*this_adjusting=*/1,
9868 delta
, vcall_index
);
9869 if (!DECL_NAME (fn
))
9872 /* Take the address of the function, considering it to be of an
9873 appropriate generic type. */
9874 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9875 init
= fold_convert (vfunc_ptr_type_node
,
9876 build_fold_addr_expr (fn
));
9877 /* Don't refer to a virtual destructor from a constructor
9878 vtable or a vtable for an abstract class, since destroying
9879 an object under construction is undefined behavior and we
9880 don't want it to be considered a candidate for speculative
9881 devirtualization. But do create the thunk for ABI
9883 if (DECL_DESTRUCTOR_P (fn_original
)
9884 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9885 || orig_binfo
!= binfo
))
9886 init
= size_zero_node
;
9890 /* And add it to the chain of initializers. */
9891 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9894 if (init
== size_zero_node
)
9895 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9896 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9898 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9900 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9901 fn
, build_int_cst (NULL_TREE
, i
));
9902 TREE_CONSTANT (fdesc
) = 1;
9904 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), fdesc
);
9908 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9912 /* Adds to vid->inits the initializers for the vbase and vcall
9913 offsets in BINFO, which is in the hierarchy dominated by T. */
9916 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9920 /* If this is a derived class, we must first create entries
9921 corresponding to the primary base class. */
9922 b
= get_primary_binfo (binfo
);
9924 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9926 /* Add the vbase entries for this base. */
9927 build_vbase_offset_vtbl_entries (binfo
, vid
);
9928 /* Add the vcall entries for this base. */
9929 build_vcall_offset_vtbl_entries (binfo
, vid
);
9932 /* Returns the initializers for the vbase offset entries in the vtable
9933 for BINFO (which is part of the class hierarchy dominated by T), in
9934 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9935 where the next vbase offset will go. */
9938 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9942 tree non_primary_binfo
;
9944 /* If there are no virtual baseclasses, then there is nothing to
9946 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9951 /* We might be a primary base class. Go up the inheritance hierarchy
9952 until we find the most derived class of which we are a primary base:
9953 it is the offset of that which we need to use. */
9954 non_primary_binfo
= binfo
;
9955 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9959 /* If we have reached a virtual base, then it must be a primary
9960 base (possibly multi-level) of vid->binfo, or we wouldn't
9961 have called build_vcall_and_vbase_vtbl_entries for it. But it
9962 might be a lost primary, so just skip down to vid->binfo. */
9963 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9965 non_primary_binfo
= vid
->binfo
;
9969 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9970 if (get_primary_binfo (b
) != non_primary_binfo
)
9972 non_primary_binfo
= b
;
9975 /* Go through the virtual bases, adding the offsets. */
9976 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9978 vbase
= TREE_CHAIN (vbase
))
9983 if (!BINFO_VIRTUAL_P (vbase
))
9986 /* Find the instance of this virtual base in the complete
9988 b
= copied_binfo (vbase
, binfo
);
9990 /* If we've already got an offset for this virtual base, we
9991 don't need another one. */
9992 if (BINFO_VTABLE_PATH_MARKED (b
))
9994 BINFO_VTABLE_PATH_MARKED (b
) = 1;
9996 /* Figure out where we can find this vbase offset. */
9997 delta
= size_binop (MULT_EXPR
,
9999 fold_convert (ssizetype
,
10000 TYPE_SIZE_UNIT (vtable_entry_type
)));
10001 if (vid
->primary_vtbl_p
)
10002 BINFO_VPTR_FIELD (b
) = delta
;
10004 if (binfo
!= TYPE_BINFO (t
))
10005 /* The vbase offset had better be the same. */
10006 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
10008 /* The next vbase will come at a more negative offset. */
10009 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10010 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10012 /* The initializer is the delta from BINFO to this virtual base.
10013 The vbase offsets go in reverse inheritance-graph order, and
10014 we are walking in inheritance graph order so these end up in
10015 the right order. */
10016 delta
= size_diffop_loc (input_location
,
10017 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
10019 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
10020 fold_build1_loc (input_location
, NOP_EXPR
,
10021 vtable_entry_type
, delta
));
10025 /* Adds the initializers for the vcall offset entries in the vtable
10026 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10030 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10032 /* We only need these entries if this base is a virtual base. We
10033 compute the indices -- but do not add to the vtable -- when
10034 building the main vtable for a class. */
10035 if (binfo
== TYPE_BINFO (vid
->derived
)
10036 || (BINFO_VIRTUAL_P (binfo
)
10037 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10038 correspond to VID->DERIVED), we are building a primary
10039 construction virtual table. Since this is a primary
10040 virtual table, we do not need the vcall offsets for
10042 && binfo
!= vid
->rtti_binfo
))
10044 /* We need a vcall offset for each of the virtual functions in this
10045 vtable. For example:
10047 class A { virtual void f (); };
10048 class B1 : virtual public A { virtual void f (); };
10049 class B2 : virtual public A { virtual void f (); };
10050 class C: public B1, public B2 { virtual void f (); };
10052 A C object has a primary base of B1, which has a primary base of A. A
10053 C also has a secondary base of B2, which no longer has a primary base
10054 of A. So the B2-in-C construction vtable needs a secondary vtable for
10055 A, which will adjust the A* to a B2* to call f. We have no way of
10056 knowing what (or even whether) this offset will be when we define B2,
10057 so we store this "vcall offset" in the A sub-vtable and look it up in
10058 a "virtual thunk" for B2::f.
10060 We need entries for all the functions in our primary vtable and
10061 in our non-virtual bases' secondary vtables. */
10062 vid
->vbase
= binfo
;
10063 /* If we are just computing the vcall indices -- but do not need
10064 the actual entries -- not that. */
10065 if (!BINFO_VIRTUAL_P (binfo
))
10066 vid
->generate_vcall_entries
= false;
10067 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10068 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
10072 /* Build vcall offsets, starting with those for BINFO. */
10075 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
10078 tree primary_binfo
;
10081 /* Don't walk into virtual bases -- except, of course, for the
10082 virtual base for which we are building vcall offsets. Any
10083 primary virtual base will have already had its offsets generated
10084 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10085 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
10088 /* If BINFO has a primary base, process it first. */
10089 primary_binfo
= get_primary_binfo (binfo
);
10091 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
10093 /* Add BINFO itself to the list. */
10094 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
10096 /* Scan the non-primary bases of BINFO. */
10097 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
10098 if (base_binfo
!= primary_binfo
)
10099 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
10102 /* Called from build_vcall_offset_vtbl_entries_r. */
10105 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
10107 /* Make entries for the rest of the virtuals. */
10110 /* The ABI requires that the methods be processed in declaration
10112 for (orig_fn
= TYPE_FIELDS (BINFO_TYPE (binfo
));
10114 orig_fn
= DECL_CHAIN (orig_fn
))
10115 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
10116 add_vcall_offset (orig_fn
, binfo
, vid
);
10119 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10122 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
10126 tree derived_entry
;
10128 /* If there is already an entry for a function with the same
10129 signature as FN, then we do not need a second vcall offset.
10130 Check the list of functions already present in the derived
10132 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
10134 if (same_signature_p (derived_entry
, orig_fn
)
10135 /* We only use one vcall offset for virtual destructors,
10136 even though there are two virtual table entries. */
10137 || (DECL_DESTRUCTOR_P (derived_entry
)
10138 && DECL_DESTRUCTOR_P (orig_fn
)))
10142 /* If we are building these vcall offsets as part of building
10143 the vtable for the most derived class, remember the vcall
10145 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
10147 tree_pair_s elt
= {orig_fn
, vid
->index
};
10148 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
10151 /* The next vcall offset will be found at a more negative
10153 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10154 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10156 /* Keep track of this function. */
10157 vec_safe_push (vid
->fns
, orig_fn
);
10159 if (vid
->generate_vcall_entries
)
10164 /* Find the overriding function. */
10165 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
10166 if (fn
== error_mark_node
)
10167 vcall_offset
= build_zero_cst (vtable_entry_type
);
10170 base
= TREE_VALUE (fn
);
10172 /* The vbase we're working on is a primary base of
10173 vid->binfo. But it might be a lost primary, so its
10174 BINFO_OFFSET might be wrong, so we just use the
10175 BINFO_OFFSET from vid->binfo. */
10176 vcall_offset
= size_diffop_loc (input_location
,
10177 BINFO_OFFSET (base
),
10178 BINFO_OFFSET (vid
->binfo
));
10179 vcall_offset
= fold_build1_loc (input_location
,
10180 NOP_EXPR
, vtable_entry_type
,
10183 /* Add the initializer to the vtable. */
10184 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
10188 /* Return vtbl initializers for the RTTI entries corresponding to the
10189 BINFO's vtable. The RTTI entries should indicate the object given
10190 by VID->rtti_binfo. */
10193 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10201 t
= BINFO_TYPE (vid
->rtti_binfo
);
10203 /* To find the complete object, we will first convert to our most
10204 primary base, and then add the offset in the vtbl to that value. */
10205 b
= most_primary_binfo (binfo
);
10206 offset
= size_diffop_loc (input_location
,
10207 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
10209 /* The second entry is the address of the typeinfo object. */
10211 decl
= build_address (get_tinfo_decl (t
));
10213 decl
= integer_zero_node
;
10215 /* Convert the declaration to a type that can be stored in the
10217 init
= build_nop (vfunc_ptr_type_node
, decl
);
10218 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10220 /* Add the offset-to-top entry. It comes earlier in the vtable than
10221 the typeinfo entry. Convert the offset to look like a
10222 function pointer, so that we can put it in the vtable. */
10223 init
= build_nop (vfunc_ptr_type_node
, offset
);
10224 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10227 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10231 uniquely_derived_from_p (tree parent
, tree type
)
10233 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
10234 return base
&& base
!= error_mark_node
;
10237 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10240 publicly_uniquely_derived_p (tree parent
, tree type
)
10242 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
10244 return base
&& base
!= error_mark_node
;
10247 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10248 class between them, if any. */
10251 common_enclosing_class (tree ctx1
, tree ctx2
)
10253 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
10255 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
10256 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
10259 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10260 TYPE_MARKED_P (t
) = true;
10261 tree found
= NULL_TREE
;
10262 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10263 if (TYPE_MARKED_P (t
))
10268 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10269 TYPE_MARKED_P (t
) = false;
10273 #include "gt-cp-class.h"