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
)
1010 if (TREE_CODE (fn
) != TREE_CODE (method
))
1013 /* Two using-declarations can coexist, we'll complain about ambiguity in
1014 overload resolution. */
1015 if (via_using
&& iter
.using_p ()
1016 /* Except handle inherited constructors specially. */
1017 && ! DECL_CONSTRUCTOR_P (fn
))
1020 /* [over.load] Member function declarations with the
1021 same name and the same parameter types cannot be
1022 overloaded if any of them is a static member
1023 function declaration.
1025 [over.load] Member function declarations with the same name and
1026 the same parameter-type-list as well as member function template
1027 declarations with the same name, the same parameter-type-list, and
1028 the same template parameter lists cannot be overloaded if any of
1029 them, but not all, have a ref-qualifier.
1031 [namespace.udecl] When a using-declaration brings names
1032 from a base class into a derived class scope, member
1033 functions in the derived class override and/or hide member
1034 functions with the same name and parameter types in a base
1035 class (rather than conflicting). */
1036 tree fn_type
= TREE_TYPE (fn
);
1037 tree method_type
= TREE_TYPE (method
);
1039 /* Compare the quals on the 'this' parm. Don't compare
1040 the whole types, as used functions are treated as
1041 coming from the using class in overload resolution. */
1042 if (! DECL_STATIC_FUNCTION_P (fn
)
1043 && ! DECL_STATIC_FUNCTION_P (method
)
1044 /* Either both or neither need to be ref-qualified for
1045 differing quals to allow overloading. */
1046 && (FUNCTION_REF_QUALIFIED (fn_type
)
1047 == FUNCTION_REF_QUALIFIED (method_type
))
1048 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1049 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1053 tree real_method
= method
;
1055 /* Templates and conversion ops must match return types. */
1056 if ((DECL_CONV_FN_P (fn
) || TREE_CODE (fn
) == TEMPLATE_DECL
)
1057 && !same_type_p (TREE_TYPE (fn_type
), TREE_TYPE (method_type
)))
1060 /* For templates, the template parameters must be identical. */
1061 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1063 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1064 DECL_TEMPLATE_PARMS (method
)))
1067 real_fn
= DECL_TEMPLATE_RESULT (fn
);
1068 real_method
= DECL_TEMPLATE_RESULT (method
);
1071 tree parms1
= TYPE_ARG_TYPES (fn_type
);
1072 tree parms2
= TYPE_ARG_TYPES (method_type
);
1073 if (! DECL_STATIC_FUNCTION_P (real_fn
))
1074 parms1
= TREE_CHAIN (parms1
);
1075 if (! DECL_STATIC_FUNCTION_P (real_method
))
1076 parms2
= TREE_CHAIN (parms2
);
1078 /* Bring back parameters omitted from an inherited ctor. The
1079 method and the function can have different omittedness. */
1080 if (ctor_omit_inherited_parms (real_fn
))
1081 parms1
= FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn
));
1082 if (ctor_omit_inherited_parms (real_method
))
1083 parms2
= (FUNCTION_FIRST_USER_PARMTYPE
1084 (DECL_CLONED_FUNCTION (real_method
)));
1086 if (!compparms (parms1
, parms2
))
1089 if (!equivalently_constrained (fn
, method
))
1091 if (processing_template_decl
)
1092 /* We can't check satisfaction in dependent context, wait until
1093 the class is instantiated. */
1096 special_function_kind sfk
= special_memfn_p (method
);
1099 || DECL_INHERITED_CTOR (fn
)
1100 || TREE_CODE (fn
) == TEMPLATE_DECL
)
1101 /* Member function templates and non-special member functions
1102 coexist if they are not equivalently constrained. A member
1103 function is not hidden by an inherited constructor. */
1106 /* P0848: For special member functions, deleted, unsatisfied, or
1107 less constrained overloads are ineligible. We implement this
1108 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1109 use the notion of eligibility, and the selected destructor can
1110 be deleted, but removing unsatisfied or less constrained
1111 overloads has the same effect as overload resolution. */
1112 bool dtor
= (sfk
== sfk_destructor
);
1114 losem
= ((!dtor
&& DECL_DELETED_FN (method
))
1115 || !constraints_satisfied_p (method
));
1116 bool losef
= ((!dtor
&& DECL_DELETED_FN (fn
))
1117 || !constraints_satisfied_p (fn
));
1120 win
= losem
- losef
;
1122 win
= more_constrained (fn
, method
);
1124 /* Leave FN in the method vec, discard METHOD. */
1128 /* Remove FN, add METHOD. */
1129 current_fns
= iter
.remove_node (current_fns
);
1133 /* Let them coexist for now. */
1137 /* If these are versions of the same function, process and
1139 if (TREE_CODE (fn
) == FUNCTION_DECL
1140 && maybe_version_functions (method
, fn
, true))
1143 if (DECL_INHERITED_CTOR (method
))
1145 if (!DECL_INHERITED_CTOR (fn
))
1146 /* Defer to the other function. */
1149 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1150 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1151 if (flag_new_inheriting_ctors
)
1155 /* Inheriting the same constructor along different
1156 paths, combine them. */
1157 SET_DECL_INHERITED_CTOR
1158 (fn
, ovl_make (DECL_INHERITED_CTOR (method
),
1159 DECL_INHERITED_CTOR (fn
)));
1160 /* And discard the new one. */
1164 /* Inherited ctors can coexist until overload
1169 error_at (DECL_SOURCE_LOCATION (method
),
1170 "%q#D conflicts with version inherited from %qT",
1172 inform (DECL_SOURCE_LOCATION (fn
),
1173 "version inherited from %qT declared here",
1179 /* Defer to the local function. */
1181 else if (flag_new_inheriting_ctors
1182 && DECL_INHERITED_CTOR (fn
))
1184 /* Remove the inherited constructor. */
1185 current_fns
= iter
.remove_node (current_fns
);
1190 error_at (DECL_SOURCE_LOCATION (method
),
1191 "%q#D cannot be overloaded with %q#D", method
, fn
);
1192 inform (DECL_SOURCE_LOCATION (fn
),
1193 "previous declaration %q#D", fn
);
1198 current_fns
= ovl_insert (method
, current_fns
, via_using
);
1200 if (!COMPLETE_TYPE_P (type
) && !DECL_CONV_FN_P (method
)
1201 && !push_class_level_binding (DECL_NAME (method
), current_fns
))
1205 slot
= add_member_slot (type
, DECL_NAME (method
));
1207 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1208 grok_special_member_properties (method
);
1210 *slot
= current_fns
;
1215 /* Subroutines of finish_struct. */
1217 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1218 legit, otherwise return 0. */
1221 alter_access (tree t
, tree fdecl
, tree access
)
1225 retrofit_lang_decl (fdecl
);
1227 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1229 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1232 if (TREE_VALUE (elem
) != access
)
1234 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1235 error ("conflicting access specifications for method"
1236 " %q+D, ignored", TREE_TYPE (fdecl
));
1238 error ("conflicting access specifications for field %qE, ignored",
1243 /* They're changing the access to the same thing they changed
1244 it to before. That's OK. */
1250 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1251 tf_warning_or_error
);
1252 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1258 /* Return the access node for DECL's access in its enclosing class. */
1261 declared_access (tree decl
)
1263 return (TREE_PRIVATE (decl
) ? access_private_node
1264 : TREE_PROTECTED (decl
) ? access_protected_node
1265 : access_public_node
);
1268 /* Process the USING_DECL, which is a member of T. */
1271 handle_using_decl (tree using_decl
, tree t
)
1273 tree decl
= USING_DECL_DECLS (using_decl
);
1274 tree name
= DECL_NAME (using_decl
);
1275 tree access
= declared_access (using_decl
);
1276 tree flist
= NULL_TREE
;
1279 gcc_assert (!processing_template_decl
&& decl
);
1281 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1282 tf_warning_or_error
);
1285 old_value
= OVL_FIRST (old_value
);
1287 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1290 old_value
= NULL_TREE
;
1293 cp_emit_debug_info_for_using (decl
, t
);
1295 if (is_overloaded_fn (decl
))
1300 else if (is_overloaded_fn (old_value
))
1303 /* It's OK to use functions from a base when there are functions with
1304 the same name already present in the current class. */;
1307 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1308 "because of local method %q#D with same name",
1309 using_decl
, t
, old_value
);
1310 inform (DECL_SOURCE_LOCATION (old_value
),
1311 "local method %q#D declared here", old_value
);
1315 else if (!DECL_ARTIFICIAL (old_value
))
1317 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1318 "because of local member %q#D with same name",
1319 using_decl
, t
, old_value
);
1320 inform (DECL_SOURCE_LOCATION (old_value
),
1321 "local member %q#D declared here", old_value
);
1325 /* Make type T see field decl FDECL with access ACCESS. */
1327 for (ovl_iterator
iter (flist
); iter
; ++iter
)
1329 add_method (t
, *iter
, true);
1330 alter_access (t
, *iter
, access
);
1333 alter_access (t
, decl
, access
);
1336 /* Data structure for find_abi_tags_r, below. */
1340 tree t
; // The type that we're checking for missing tags.
1341 tree subob
; // The subobject of T that we're getting tags from.
1342 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1345 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1346 in the context of P. TAG can be either an identifier (the DECL_NAME of
1347 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1350 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1352 if (!IDENTIFIER_MARKED (id
))
1354 if (p
->tags
!= error_mark_node
)
1356 /* We're collecting tags from template arguments or from
1357 the type of a variable or function return type. */
1358 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1360 /* Don't inherit this tag multiple times. */
1361 IDENTIFIER_MARKED (id
) = true;
1365 /* Tags inherited from type template arguments are only used
1366 to avoid warnings. */
1367 ABI_TAG_IMPLICIT (p
->tags
) = true;
1370 /* For functions and variables we want to warn, too. */
1373 /* Otherwise we're diagnosing missing tags. */
1374 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1376 auto_diagnostic_group d
;
1377 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1378 "that %qT (used in its return type) has",
1380 inform (location_of (*tp
), "%qT declared here", *tp
);
1382 else if (VAR_P (p
->t
))
1384 auto_diagnostic_group d
;
1385 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1386 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1387 inform (location_of (*tp
), "%qT declared here", *tp
);
1389 else if (TYPE_P (p
->subob
))
1391 auto_diagnostic_group d
;
1392 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1393 "that base %qT has", p
->t
, tag
, p
->subob
))
1394 inform (location_of (p
->subob
), "%qT declared here",
1399 auto_diagnostic_group d
;
1400 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1401 "that %qT (used in the type of %qD) has",
1402 p
->t
, tag
, *tp
, p
->subob
))
1404 inform (location_of (p
->subob
), "%qD declared here",
1406 inform (location_of (*tp
), "%qT declared here", *tp
);
1412 /* Find all the ABI tags in the attribute list ATTR and either call
1413 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1416 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1420 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1421 attr
= TREE_CHAIN (attr
))
1422 for (tree list
= TREE_VALUE (attr
); list
;
1423 list
= TREE_CHAIN (list
))
1425 tree tag
= TREE_VALUE (list
);
1426 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1428 check_tag (tag
, id
, tp
, p
);
1430 IDENTIFIER_MARKED (id
) = val
;
1434 /* Find all the ABI tags on T and its enclosing scopes and either call
1435 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1438 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1440 while (t
!= global_namespace
)
1445 attr
= TYPE_ATTRIBUTES (t
);
1446 t
= CP_TYPE_CONTEXT (t
);
1450 attr
= DECL_ATTRIBUTES (t
);
1451 t
= CP_DECL_CONTEXT (t
);
1453 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1457 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1458 types with ABI tags, add the corresponding identifiers to the VEC in
1459 *DATA and set IDENTIFIER_MARKED. */
1462 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1464 if (!OVERLOAD_TYPE_P (*tp
))
1467 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1468 anyway, but let's make sure of it. */
1469 *walk_subtrees
= false;
1471 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1473 mark_or_check_tags (*tp
, tp
, p
, false);
1478 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1479 IDENTIFIER_MARKED on its ABI tags. */
1482 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1484 if (!OVERLOAD_TYPE_P (*tp
))
1487 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1488 anyway, but let's make sure of it. */
1489 *walk_subtrees
= false;
1491 bool *valp
= static_cast<bool*>(data
);
1493 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1498 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1502 mark_abi_tags (tree t
, bool val
)
1504 mark_or_check_tags (t
, NULL
, NULL
, val
);
1507 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1508 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1510 /* Template arguments are part of the signature. */
1511 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1512 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1514 tree arg
= TREE_VEC_ELT (level
, j
);
1515 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1518 if (TREE_CODE (t
) == FUNCTION_DECL
)
1519 /* A function's parameter types are part of the signature, so
1520 we don't need to inherit any tags that are also in them. */
1521 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1522 arg
= TREE_CHAIN (arg
))
1523 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1524 mark_abi_tags_r
, &val
);
1528 /* Check that T has all the ABI tags that subobject SUBOB has, or
1529 warn if not. If T is a (variable or function) declaration, also
1530 return any missing tags, and add them to T if JUST_CHECKING is false. */
1533 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1535 bool inherit
= DECL_P (t
);
1537 if (!inherit
&& !warn_abi_tag
)
1540 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1541 if (!TREE_PUBLIC (decl
))
1542 /* No need to worry about things local to this TU. */
1545 mark_abi_tags (t
, true);
1547 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1548 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1550 data
.tags
= NULL_TREE
;
1552 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1554 if (!(inherit
&& data
.tags
))
1555 /* We don't need to do anything with data.tags. */;
1556 else if (just_checking
)
1557 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1559 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1560 IDENTIFIER_MARKED (id
) = false;
1564 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1566 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1569 = tree_cons (abi_tag_identifier
, data
.tags
, DECL_ATTRIBUTES (t
));
1572 mark_abi_tags (t
, false);
1577 /* Check that DECL has all the ABI tags that are used in parts of its type
1578 that are not reflected in its mangled name. */
1581 check_abi_tags (tree decl
)
1584 check_abi_tags (decl
, TREE_TYPE (decl
));
1585 else if (TREE_CODE (decl
) == FUNCTION_DECL
1586 && !DECL_CONV_FN_P (decl
)
1587 && !mangle_return_type_p (decl
))
1588 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1591 /* Return any ABI tags that are used in parts of the type of DECL
1592 that are not reflected in its mangled name. This function is only
1593 used in backward-compatible mangling for ABI <11. */
1596 missing_abi_tags (tree decl
)
1599 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1600 else if (TREE_CODE (decl
) == FUNCTION_DECL
1601 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1602 that we can use this function for setting need_abi_warning
1603 regardless of the current flag_abi_version. */
1604 && !mangle_return_type_p (decl
))
1605 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1611 inherit_targ_abi_tags (tree t
)
1613 if (!CLASS_TYPE_P (t
)
1614 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1617 mark_abi_tags (t
, true);
1619 tree args
= CLASSTYPE_TI_ARGS (t
);
1620 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1621 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1623 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1624 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1626 tree arg
= TREE_VEC_ELT (level
, j
);
1628 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1632 // If we found some tags on our template arguments, add them to our
1633 // abi_tag attribute.
1636 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1638 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1641 = tree_cons (abi_tag_identifier
, data
.tags
, TYPE_ATTRIBUTES (t
));
1644 mark_abi_tags (t
, false);
1647 /* Return true, iff class T has a non-virtual destructor that is
1648 accessible from outside the class heirarchy (i.e. is public, or
1649 there's a suitable friend. */
1652 accessible_nvdtor_p (tree t
)
1654 tree dtor
= CLASSTYPE_DESTRUCTOR (t
);
1656 /* An implicitly declared destructor is always public. And,
1657 if it were virtual, we would have created it by now. */
1661 if (DECL_VINDEX (dtor
))
1662 return false; /* Virtual */
1664 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1665 return true; /* Public */
1667 if (CLASSTYPE_FRIEND_CLASSES (t
)
1668 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1669 return true; /* Has friends */
1674 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1675 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1676 properties of the bases. */
1679 check_bases (tree t
,
1680 int* cant_have_const_ctor_p
,
1681 int* no_const_asn_ref_p
)
1684 bool seen_non_virtual_nearly_empty_base_p
= 0;
1685 int seen_tm_mask
= 0;
1688 tree field
= NULL_TREE
;
1690 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1691 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1692 if (TREE_CODE (field
) == FIELD_DECL
)
1695 for (binfo
= TYPE_BINFO (t
), i
= 0;
1696 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1698 tree basetype
= TREE_TYPE (base_binfo
);
1700 gcc_assert (COMPLETE_TYPE_P (basetype
));
1702 if (CLASSTYPE_FINAL (basetype
))
1703 error ("cannot derive from %<final%> base %qT in derived type %qT",
1706 /* If any base class is non-literal, so is the derived class. */
1707 if (!CLASSTYPE_LITERAL_P (basetype
))
1708 CLASSTYPE_LITERAL_P (t
) = false;
1710 /* If the base class doesn't have copy constructors or
1711 assignment operators that take const references, then the
1712 derived class cannot have such a member automatically
1714 if (TYPE_HAS_COPY_CTOR (basetype
)
1715 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1716 *cant_have_const_ctor_p
= 1;
1717 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1718 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1719 *no_const_asn_ref_p
= 1;
1721 if (BINFO_VIRTUAL_P (base_binfo
))
1722 /* A virtual base does not effect nearly emptiness. */
1724 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1726 if (seen_non_virtual_nearly_empty_base_p
)
1727 /* And if there is more than one nearly empty base, then the
1728 derived class is not nearly empty either. */
1729 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1731 /* Remember we've seen one. */
1732 seen_non_virtual_nearly_empty_base_p
= 1;
1734 else if (!is_empty_class (basetype
))
1735 /* If the base class is not empty or nearly empty, then this
1736 class cannot be nearly empty. */
1737 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1739 /* A lot of properties from the bases also apply to the derived
1741 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1742 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1743 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1744 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1745 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1746 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1747 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1748 || !TYPE_HAS_COPY_CTOR (basetype
));
1749 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1750 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1751 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1752 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1753 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1754 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1755 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1756 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1757 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1758 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1759 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1760 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1761 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1762 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1763 if (TYPE_HAS_MUTABLE_P (basetype
))
1764 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1766 /* A standard-layout class is a class that:
1768 * has no non-standard-layout base classes, */
1769 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1770 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1773 /* ...has no base classes of the same type as the first non-static
1775 if (field
&& DECL_CONTEXT (field
) == t
1776 && (same_type_ignoring_top_level_qualifiers_p
1777 (TREE_TYPE (field
), basetype
)))
1778 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1780 ...has at most one base class subobject of any given type... */
1781 else if (CLASSTYPE_REPEATED_BASE_P (t
))
1782 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1784 /* ...either has no non-static data members in the most-derived
1785 class and at most one base class with non-static data
1786 members, or has no base classes with non-static data
1787 members. FIXME This was reworded in DR 1813. */
1788 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1789 basefield
= DECL_CHAIN (basefield
))
1790 if (TREE_CODE (basefield
) == FIELD_DECL
1791 && !(DECL_FIELD_IS_BASE (basefield
)
1792 && integer_zerop (DECL_SIZE (basefield
))))
1795 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1802 /* Don't bother collecting tm attributes if transactional memory
1803 support is not enabled. */
1806 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1808 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1811 check_abi_tags (t
, basetype
);
1814 /* If one of the base classes had TM attributes, and the current class
1815 doesn't define its own, then the current class inherits one. */
1816 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1818 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1819 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1823 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1824 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1825 that have had a nearly-empty virtual primary base stolen by some
1826 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1830 determine_primary_bases (tree t
)
1833 tree primary
= NULL_TREE
;
1834 tree type_binfo
= TYPE_BINFO (t
);
1837 /* Determine the primary bases of our bases. */
1838 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1839 base_binfo
= TREE_CHAIN (base_binfo
))
1841 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1843 /* See if we're the non-virtual primary of our inheritance
1845 if (!BINFO_VIRTUAL_P (base_binfo
))
1847 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1848 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1851 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1852 BINFO_TYPE (parent_primary
)))
1853 /* We are the primary binfo. */
1854 BINFO_PRIMARY_P (base_binfo
) = 1;
1856 /* Determine if we have a virtual primary base, and mark it so.
1858 if (primary
&& BINFO_VIRTUAL_P (primary
))
1860 tree this_primary
= copied_binfo (primary
, base_binfo
);
1862 if (BINFO_PRIMARY_P (this_primary
))
1863 /* Someone already claimed this base. */
1864 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1869 BINFO_PRIMARY_P (this_primary
) = 1;
1870 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1872 /* A virtual binfo might have been copied from within
1873 another hierarchy. As we're about to use it as a
1874 primary base, make sure the offsets match. */
1875 delta
= size_diffop_loc (input_location
,
1876 fold_convert (ssizetype
,
1877 BINFO_OFFSET (base_binfo
)),
1878 fold_convert (ssizetype
,
1879 BINFO_OFFSET (this_primary
)));
1881 propagate_binfo_offsets (this_primary
, delta
);
1886 /* First look for a dynamic direct non-virtual base. */
1887 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1889 tree basetype
= BINFO_TYPE (base_binfo
);
1891 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1893 primary
= base_binfo
;
1898 /* A "nearly-empty" virtual base class can be the primary base
1899 class, if no non-virtual polymorphic base can be found. Look for
1900 a nearly-empty virtual dynamic base that is not already a primary
1901 base of something in the hierarchy. If there is no such base,
1902 just pick the first nearly-empty virtual base. */
1904 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1905 base_binfo
= TREE_CHAIN (base_binfo
))
1906 if (BINFO_VIRTUAL_P (base_binfo
)
1907 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1909 if (!BINFO_PRIMARY_P (base_binfo
))
1911 /* Found one that is not primary. */
1912 primary
= base_binfo
;
1916 /* Remember the first candidate. */
1917 primary
= base_binfo
;
1921 /* If we've got a primary base, use it. */
1924 tree basetype
= BINFO_TYPE (primary
);
1926 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1927 if (BINFO_PRIMARY_P (primary
))
1928 /* We are stealing a primary base. */
1929 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1930 BINFO_PRIMARY_P (primary
) = 1;
1931 if (BINFO_VIRTUAL_P (primary
))
1935 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1936 /* A virtual binfo might have been copied from within
1937 another hierarchy. As we're about to use it as a primary
1938 base, make sure the offsets match. */
1939 delta
= size_diffop_loc (input_location
, ssize_int (0),
1940 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
1942 propagate_binfo_offsets (primary
, delta
);
1945 primary
= TYPE_BINFO (basetype
);
1947 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1948 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1949 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1953 /* Update the variant types of T. */
1956 fixup_type_variants (tree t
)
1963 for (variants
= TYPE_NEXT_VARIANT (t
);
1965 variants
= TYPE_NEXT_VARIANT (variants
))
1967 /* These fields are in the _TYPE part of the node, not in
1968 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1969 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1970 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1971 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1972 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1974 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1975 CLASSTYPE_FINAL (variants
) = CLASSTYPE_FINAL (t
);
1977 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1979 /* Copy whatever these are holding today. */
1980 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1981 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1983 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1984 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1988 /* KLASS is a class that we're applying may_alias to after the body is
1989 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1990 canonical type(s) will be implicitly updated. */
1993 fixup_may_alias (tree klass
)
1997 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
1998 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1999 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
2000 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
2001 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
2002 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
2005 /* Early variant fixups: we apply attributes at the beginning of the class
2006 definition, and we need to fix up any variants that have already been
2007 made via elaborated-type-specifier so that check_qualified_type works. */
2010 fixup_attribute_variants (tree t
)
2017 tree attrs
= TYPE_ATTRIBUTES (t
);
2018 unsigned align
= TYPE_ALIGN (t
);
2019 bool user_align
= TYPE_USER_ALIGN (t
);
2020 bool may_alias
= lookup_attribute ("may_alias", attrs
);
2021 bool packed
= TYPE_PACKED (t
);
2024 fixup_may_alias (t
);
2026 for (variants
= TYPE_NEXT_VARIANT (t
);
2028 variants
= TYPE_NEXT_VARIANT (variants
))
2030 /* These are the two fields that check_qualified_type looks at and
2031 are affected by attributes. */
2032 TYPE_ATTRIBUTES (variants
) = attrs
;
2033 unsigned valign
= align
;
2034 if (TYPE_USER_ALIGN (variants
))
2035 valign
= MAX (valign
, TYPE_ALIGN (variants
));
2037 TYPE_USER_ALIGN (variants
) = user_align
;
2038 SET_TYPE_ALIGN (variants
, valign
);
2039 TYPE_PACKED (variants
) = packed
;
2041 fixup_may_alias (variants
);
2045 /* Set memoizing fields and bits of T (and its variants) for later
2049 finish_struct_bits (tree t
)
2051 /* Fix up variants (if any). */
2052 fixup_type_variants (t
);
2054 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
2055 /* For a class w/o baseclasses, 'finish_struct' has set
2056 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2057 Similarly for a class whose base classes do not have vtables.
2058 When neither of these is true, we might have removed abstract
2059 virtuals (by providing a definition), added some (by declaring
2060 new ones), or redeclared ones from a base class. We need to
2061 recalculate what's really an abstract virtual at this point (by
2062 looking in the vtables). */
2063 get_pure_virtuals (t
);
2065 /* If this type has a copy constructor or a destructor, force its
2066 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2067 nonzero. This will cause it to be passed by invisible reference
2068 and prevent it from being returned in a register. */
2069 if (type_has_nontrivial_copy_init (t
)
2070 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2073 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2074 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2076 SET_TYPE_MODE (variants
, BLKmode
);
2077 TREE_ADDRESSABLE (variants
) = 1;
2082 /* Issue warnings about T having private constructors, but no friends,
2085 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2086 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2087 non-private static member functions. */
2090 maybe_warn_about_overly_private_class (tree t
)
2092 int has_member_fn
= 0;
2093 int has_nonprivate_method
= 0;
2094 bool nonprivate_ctor
= false;
2096 if (!warn_ctor_dtor_privacy
2097 /* If the class has friends, those entities might create and
2098 access instances, so we should not warn. */
2099 || (CLASSTYPE_FRIEND_CLASSES (t
)
2100 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2101 /* We will have warned when the template was declared; there's
2102 no need to warn on every instantiation. */
2103 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2104 /* There's no reason to even consider warning about this
2108 /* We only issue one warning, if more than one applies, because
2109 otherwise, on code like:
2112 // Oops - forgot `public:'
2118 we warn several times about essentially the same problem. */
2120 /* Check to see if all (non-constructor, non-destructor) member
2121 functions are private. (Since there are no friends or
2122 non-private statics, we can't ever call any of the private member
2124 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2125 if (TREE_CODE (fn
) == USING_DECL
2126 && DECL_NAME (fn
) == ctor_identifier
2127 && !TREE_PRIVATE (fn
))
2128 nonprivate_ctor
= true;
2129 else if (!DECL_DECLARES_FUNCTION_P (fn
))
2130 /* Not a function. */;
2131 else if (DECL_ARTIFICIAL (fn
))
2132 /* We're not interested in compiler-generated methods; they don't
2133 provide any way to call private members. */;
2134 else if (!TREE_PRIVATE (fn
))
2136 if (DECL_STATIC_FUNCTION_P (fn
))
2137 /* A non-private static member function is just like a
2138 friend; it can create and invoke private member
2139 functions, and be accessed without a class
2143 has_nonprivate_method
= 1;
2144 /* Keep searching for a static member function. */
2146 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2149 if (!has_nonprivate_method
&& has_member_fn
)
2151 /* There are no non-private methods, and there's at least one
2152 private member function that isn't a constructor or
2153 destructor. (If all the private members are
2154 constructors/destructors we want to use the code below that
2155 issues error messages specifically referring to
2156 constructors/destructors.) */
2158 tree binfo
= TYPE_BINFO (t
);
2160 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2161 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2163 has_nonprivate_method
= 1;
2166 if (!has_nonprivate_method
)
2168 warning (OPT_Wctor_dtor_privacy
,
2169 "all member functions in class %qT are private", t
);
2174 /* Even if some of the member functions are non-private, the class
2175 won't be useful for much if all the constructors or destructors
2176 are private: such an object can never be created or destroyed. */
2177 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
2178 if (TREE_PRIVATE (dtor
))
2180 warning (OPT_Wctor_dtor_privacy
,
2181 "%q#T only defines a private destructor and has no friends",
2186 /* Warn about classes that have private constructors and no friends. */
2187 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2188 /* Implicitly generated constructors are always public. */
2189 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
2191 tree copy_or_move
= NULL_TREE
;
2193 /* If a non-template class does not define a copy
2194 constructor, one is defined for it, enabling it to avoid
2195 this warning. For a template class, this does not
2196 happen, and so we would normally get a warning on:
2198 template <class T> class C { private: C(); };
2200 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2201 complete non-template or fully instantiated classes have this
2203 if (!TYPE_HAS_COPY_CTOR (t
))
2204 nonprivate_ctor
= true;
2206 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
));
2207 !nonprivate_ctor
&& iter
; ++iter
)
2208 if (TREE_PRIVATE (*iter
))
2210 else if (copy_fn_p (*iter
) || move_fn_p (*iter
))
2211 /* Ideally, we wouldn't count any constructor that takes
2212 an argument of the class type as a parameter, because
2213 such things cannot be used to construct an instance of
2214 the class unless you already have one. */
2215 copy_or_move
= *iter
;
2217 nonprivate_ctor
= true;
2219 if (!nonprivate_ctor
)
2221 bool w
= warning (OPT_Wctor_dtor_privacy
,
2222 "%q#T only defines private constructors and has "
2224 if (w
&& copy_or_move
)
2225 inform (DECL_SOURCE_LOCATION (copy_or_move
),
2226 "%q#D is public, but requires an existing %q#T object",
2233 /* Make BINFO's vtable have N entries, including RTTI entries,
2234 vbase and vcall offsets, etc. Set its type and call the back end
2238 layout_vtable_decl (tree binfo
, int n
)
2243 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2244 layout_type (atype
);
2246 /* We may have to grow the vtable. */
2247 vtable
= get_vtbl_decl_for_binfo (binfo
);
2248 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2250 TREE_TYPE (vtable
) = atype
;
2251 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2252 layout_decl (vtable
, 0);
2256 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2257 have the same signature. */
2260 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2262 /* One destructor overrides another if they are the same kind of
2264 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2265 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2267 /* But a non-destructor never overrides a destructor, nor vice
2268 versa, nor do different kinds of destructors override
2269 one-another. For example, a complete object destructor does not
2270 override a deleting destructor. */
2271 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2274 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2275 || (DECL_CONV_FN_P (fndecl
)
2276 && DECL_CONV_FN_P (base_fndecl
)
2277 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2278 DECL_CONV_FN_TYPE (base_fndecl
))))
2280 tree fntype
= TREE_TYPE (fndecl
);
2281 tree base_fntype
= TREE_TYPE (base_fndecl
);
2282 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2283 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2284 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2285 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2291 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2295 base_derived_from (tree derived
, tree base
)
2299 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2301 if (probe
== derived
)
2303 else if (BINFO_VIRTUAL_P (probe
))
2304 /* If we meet a virtual base, we can't follow the inheritance
2305 any more. See if the complete type of DERIVED contains
2306 such a virtual base. */
2307 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2313 struct find_final_overrider_data
{
2314 /* The function for which we are trying to find a final overrider. */
2316 /* The base class in which the function was declared. */
2317 tree declaring_base
;
2318 /* The candidate overriders. */
2320 /* Path to most derived. */
2324 /* Add the overrider along the current path to FFOD->CANDIDATES.
2325 Returns true if an overrider was found; false otherwise. */
2328 dfs_find_final_overrider_1 (tree binfo
,
2329 find_final_overrider_data
*ffod
,
2334 /* If BINFO is not the most derived type, try a more derived class.
2335 A definition there will overrider a definition here. */
2339 if (dfs_find_final_overrider_1
2340 (ffod
->path
[depth
], ffod
, depth
))
2344 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2347 tree
*candidate
= &ffod
->candidates
;
2349 /* Remove any candidates overridden by this new function. */
2352 /* If *CANDIDATE overrides METHOD, then METHOD
2353 cannot override anything else on the list. */
2354 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2356 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2357 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2358 *candidate
= TREE_CHAIN (*candidate
);
2360 candidate
= &TREE_CHAIN (*candidate
);
2363 /* Add the new function. */
2364 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2371 /* Called from find_final_overrider via dfs_walk. */
2374 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2376 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2378 if (binfo
== ffod
->declaring_base
)
2379 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2380 ffod
->path
.safe_push (binfo
);
2386 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2388 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2394 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2395 FN and whose TREE_VALUE is the binfo for the base where the
2396 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2397 DERIVED) is the base object in which FN is declared. */
2400 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2402 find_final_overrider_data ffod
;
2404 /* Getting this right is a little tricky. This is valid:
2406 struct S { virtual void f (); };
2407 struct T { virtual void f (); };
2408 struct U : public S, public T { };
2410 even though calling `f' in `U' is ambiguous. But,
2412 struct R { virtual void f(); };
2413 struct S : virtual public R { virtual void f (); };
2414 struct T : virtual public R { virtual void f (); };
2415 struct U : public S, public T { };
2417 is not -- there's no way to decide whether to put `S::f' or
2418 `T::f' in the vtable for `R'.
2420 The solution is to look at all paths to BINFO. If we find
2421 different overriders along any two, then there is a problem. */
2422 if (DECL_THUNK_P (fn
))
2423 fn
= THUNK_TARGET (fn
);
2425 /* Determine the depth of the hierarchy. */
2427 ffod
.declaring_base
= binfo
;
2428 ffod
.candidates
= NULL_TREE
;
2429 ffod
.path
.create (30);
2431 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2432 dfs_find_final_overrider_post
, &ffod
);
2434 ffod
.path
.release ();
2436 /* If there was no winner, issue an error message. */
2437 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2438 return error_mark_node
;
2440 return ffod
.candidates
;
2443 /* Return the index of the vcall offset for FN when TYPE is used as a
2447 get_vcall_index (tree fn
, tree type
)
2449 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2453 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2454 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2455 || same_signature_p (fn
, p
->purpose
))
2458 /* There should always be an appropriate index. */
2462 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2463 overrider at that index in the vtable. This should only be used when we
2464 know that BINFO is correct for the dynamic type of the object. */
2467 lookup_vfn_in_binfo (tree idx
, tree binfo
)
2469 int ix
= tree_to_shwi (idx
);
2470 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2471 ix
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
2472 while (BINFO_PRIMARY_P (binfo
))
2473 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2474 class that actually owns the vtable. */
2475 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
2476 tree virtuals
= BINFO_VIRTUALS (binfo
);
2477 return TREE_VALUE (chain_index (ix
, virtuals
));
2480 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2481 dominated by T. FN is the old function; VIRTUALS points to the
2482 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2483 of that entry in the list. */
2486 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2494 tree overrider_fn
, overrider_target
;
2495 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2496 tree over_return
, base_return
;
2499 /* Find the nearest primary base (possibly binfo itself) which defines
2500 this function; this is the class the caller will convert to when
2501 calling FN through BINFO. */
2502 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2505 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2508 /* The nearest definition is from a lost primary. */
2509 if (BINFO_LOST_PRIMARY_P (b
))
2514 /* Find the final overrider. */
2515 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2516 if (overrider
== error_mark_node
)
2518 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2521 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2523 /* Check for adjusting covariant return types. */
2524 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2525 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2527 if (INDIRECT_TYPE_P (over_return
)
2528 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2529 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2530 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2531 /* If the overrider is invalid, don't even try. */
2532 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2534 /* If FN is a covariant thunk, we must figure out the adjustment
2535 to the final base FN was converting to. As OVERRIDER_TARGET might
2536 also be converting to the return type of FN, we have to
2537 combine the two conversions here. */
2538 tree fixed_offset
, virtual_offset
;
2540 over_return
= TREE_TYPE (over_return
);
2541 base_return
= TREE_TYPE (base_return
);
2543 if (DECL_THUNK_P (fn
))
2545 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2546 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2547 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2550 fixed_offset
= virtual_offset
= NULL_TREE
;
2553 /* Find the equivalent binfo within the return type of the
2554 overriding function. We will want the vbase offset from
2556 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2558 else if (!same_type_ignoring_top_level_qualifiers_p
2559 (over_return
, base_return
))
2561 /* There was no existing virtual thunk (which takes
2562 precedence). So find the binfo of the base function's
2563 return type within the overriding function's return type.
2564 Fortunately we know the covariancy is valid (it
2565 has already been checked), so we can just iterate along
2566 the binfos, which have been chained in inheritance graph
2567 order. Of course it is lame that we have to repeat the
2568 search here anyway -- we should really be caching pieces
2569 of the vtable and avoiding this repeated work. */
2570 tree thunk_binfo
= NULL_TREE
;
2571 tree base_binfo
= TYPE_BINFO (base_return
);
2573 /* Find the base binfo within the overriding function's
2574 return type. We will always find a thunk_binfo, except
2575 when the covariancy is invalid (which we will have
2576 already diagnosed). */
2578 for (thunk_binfo
= TYPE_BINFO (over_return
); thunk_binfo
;
2579 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2580 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2581 BINFO_TYPE (base_binfo
)))
2583 gcc_assert (thunk_binfo
|| errorcount
);
2585 /* See if virtual inheritance is involved. */
2586 for (virtual_offset
= thunk_binfo
;
2588 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2589 if (BINFO_VIRTUAL_P (virtual_offset
))
2593 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2595 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2599 /* We convert via virtual base. Adjust the fixed
2600 offset to be from there. */
2602 size_diffop (offset
,
2603 fold_convert (ssizetype
,
2604 BINFO_OFFSET (virtual_offset
)));
2607 /* There was an existing fixed offset, this must be
2608 from the base just converted to, and the base the
2609 FN was thunking to. */
2610 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2612 fixed_offset
= offset
;
2616 if (fixed_offset
|| virtual_offset
)
2617 /* Replace the overriding function with a covariant thunk. We
2618 will emit the overriding function in its own slot as
2620 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2621 fixed_offset
, virtual_offset
);
2624 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2625 !DECL_THUNK_P (fn
));
2627 /* If we need a covariant thunk, then we may need to adjust first_defn.
2628 The ABI specifies that the thunks emitted with a function are
2629 determined by which bases the function overrides, so we need to be
2630 sure that we're using a thunk for some overridden base; even if we
2631 know that the necessary this adjustment is zero, there may not be an
2632 appropriate zero-this-adjustment thunk for us to use since thunks for
2633 overriding virtual bases always use the vcall offset.
2635 Furthermore, just choosing any base that overrides this function isn't
2636 quite right, as this slot won't be used for calls through a type that
2637 puts a covariant thunk here. Calling the function through such a type
2638 will use a different slot, and that slot is the one that determines
2639 the thunk emitted for that base.
2641 So, keep looking until we find the base that we're really overriding
2642 in this slot: the nearest primary base that doesn't use a covariant
2643 thunk in this slot. */
2644 if (overrider_target
!= overrider_fn
)
2646 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2647 /* We already know that the overrider needs a covariant thunk. */
2648 b
= get_primary_binfo (b
);
2649 for (; ; b
= get_primary_binfo (b
))
2651 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2652 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2653 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2655 if (BINFO_LOST_PRIMARY_P (b
))
2661 /* Assume that we will produce a thunk that convert all the way to
2662 the final overrider, and not to an intermediate virtual base. */
2663 virtual_base
= NULL_TREE
;
2665 /* See if we can convert to an intermediate virtual base first, and then
2666 use the vcall offset located there to finish the conversion. */
2667 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2669 /* If we find the final overrider, then we can stop
2671 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2672 BINFO_TYPE (TREE_VALUE (overrider
))))
2675 /* If we find a virtual base, and we haven't yet found the
2676 overrider, then there is a virtual base between the
2677 declaring base (first_defn) and the final overrider. */
2678 if (BINFO_VIRTUAL_P (b
))
2685 /* Compute the constant adjustment to the `this' pointer. The
2686 `this' pointer, when this function is called, will point at BINFO
2687 (or one of its primary bases, which are at the same offset). */
2689 /* The `this' pointer needs to be adjusted from the declaration to
2690 the nearest virtual base. */
2691 delta
= size_diffop_loc (input_location
,
2692 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2693 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2695 /* If the nearest definition is in a lost primary, we don't need an
2696 entry in our vtable. Except possibly in a constructor vtable,
2697 if we happen to get our primary back. In that case, the offset
2698 will be zero, as it will be a primary base. */
2699 delta
= size_zero_node
;
2701 /* The `this' pointer needs to be adjusted from pointing to
2702 BINFO to pointing at the base where the final overrider
2704 delta
= size_diffop_loc (input_location
,
2705 fold_convert (ssizetype
,
2706 BINFO_OFFSET (TREE_VALUE (overrider
))),
2707 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2709 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2712 BV_VCALL_INDEX (*virtuals
)
2713 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2715 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2717 BV_LOST_PRIMARY (*virtuals
) = lost
;
2720 /* Called from modify_all_vtables via dfs_walk. */
2723 dfs_modify_vtables (tree binfo
, void* data
)
2725 tree t
= (tree
) data
;
2730 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2731 /* A base without a vtable needs no modification, and its bases
2732 are uninteresting. */
2733 return dfs_skip_bases
;
2735 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2736 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2737 /* Don't do the primary vtable, if it's new. */
2740 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2741 /* There's no need to modify the vtable for a non-virtual primary
2742 base; we're not going to use that vtable anyhow. We do still
2743 need to do this for virtual primary bases, as they could become
2744 non-primary in a construction vtable. */
2747 make_new_vtable (t
, binfo
);
2749 /* Now, go through each of the virtual functions in the virtual
2750 function table for BINFO. Find the final overrider, and update
2751 the BINFO_VIRTUALS list appropriately. */
2752 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2753 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2755 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2756 old_virtuals
= TREE_CHAIN (old_virtuals
))
2757 update_vtable_entry_for_fn (t
,
2759 BV_FN (old_virtuals
),
2765 /* Update all of the primary and secondary vtables for T. Create new
2766 vtables as required, and initialize their RTTI information. Each
2767 of the functions in VIRTUALS is declared in T and may override a
2768 virtual function from a base class; find and modify the appropriate
2769 entries to point to the overriding functions. Returns a list, in
2770 declaration order, of the virtual functions that are declared in T,
2771 but do not appear in the primary base class vtable, and which
2772 should therefore be appended to the end of the vtable for T. */
2775 modify_all_vtables (tree t
, tree virtuals
)
2777 tree binfo
= TYPE_BINFO (t
);
2780 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2781 if (TYPE_CONTAINS_VPTR_P (t
))
2782 get_vtable_decl (t
, false);
2784 /* Update all of the vtables. */
2785 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2787 /* Add virtual functions not already in our primary vtable. These
2788 will be both those introduced by this class, and those overridden
2789 from secondary bases. It does not include virtuals merely
2790 inherited from secondary bases. */
2791 for (fnsp
= &virtuals
; *fnsp
; )
2793 tree fn
= TREE_VALUE (*fnsp
);
2795 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2796 || DECL_VINDEX (fn
) == error_mark_node
)
2798 /* We don't need to adjust the `this' pointer when
2799 calling this function. */
2800 BV_DELTA (*fnsp
) = integer_zero_node
;
2801 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2803 /* This is a function not already in our vtable. Keep it. */
2804 fnsp
= &TREE_CHAIN (*fnsp
);
2807 /* We've already got an entry for this function. Skip it. */
2808 *fnsp
= TREE_CHAIN (*fnsp
);
2814 /* Get the base virtual function declarations in T that have the
2818 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2820 bool found_decls
= false;
2822 /* Find virtual functions in T with the indicated NAME. */
2823 for (ovl_iterator
iter (get_class_binding (t
, name
)); iter
; ++iter
)
2825 tree method
= *iter
;
2827 if (TREE_CODE (method
) == FUNCTION_DECL
&& DECL_VINDEX (method
))
2829 base_fndecls
->safe_push (method
);
2837 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2838 for (int i
= 0; i
< n_baseclasses
; i
++)
2840 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2841 get_basefndecls (name
, basetype
, base_fndecls
);
2845 /* If this method overrides a virtual method from a base, then mark
2846 this member function as being virtual as well. Do 'final' and
2847 'override' checks too. */
2850 check_for_override (tree decl
, tree ctype
)
2852 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2853 /* In [temp.mem] we have:
2855 A specialization of a member function template does not
2856 override a virtual function from a base class. */
2859 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2860 used for a vfunc. That avoids the expensive look_for_overrides
2861 call that when we know there's nothing to find. As conversion
2862 operators for the same type can have distinct identifiers, we
2863 cannot optimize those in that way. */
2864 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2865 || DECL_CONV_FN_P (decl
))
2866 && look_for_overrides (ctype
, decl
)
2867 /* Check staticness after we've checked if we 'override'. */
2868 && !DECL_STATIC_FUNCTION_P (decl
))
2870 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2871 the error_mark_node so that we know it is an overriding
2873 DECL_VINDEX (decl
) = decl
;
2876 && !DECL_OVERRIDE_P (decl
)
2877 && !DECL_FINAL_P (decl
)
2878 && !DECL_DESTRUCTOR_P (decl
))
2879 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2880 "%qD can be marked override", decl
);
2882 else if (DECL_OVERRIDE_P (decl
))
2883 error ("%q+#D marked %<override%>, but does not override", decl
);
2885 if (DECL_VIRTUAL_P (decl
))
2887 /* Remember this identifier is virtual name. */
2888 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = true;
2890 if (!DECL_VINDEX (decl
))
2891 /* It's a new vfunc. */
2892 DECL_VINDEX (decl
) = error_mark_node
;
2894 if (DECL_DESTRUCTOR_P (decl
))
2895 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2897 else if (DECL_FINAL_P (decl
))
2898 error ("%q+#D marked %<final%>, but is not virtual", decl
);
2901 /* Warn about hidden virtual functions that are not overridden in t.
2902 We know that constructors and destructors don't apply. */
2905 warn_hidden (tree t
)
2907 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (t
))
2908 for (unsigned ix
= member_vec
->length (); ix
--;)
2910 tree fns
= (*member_vec
)[ix
];
2915 tree name
= OVL_NAME (fns
);
2916 auto_vec
<tree
, 20> base_fndecls
;
2921 /* Iterate through all of the base classes looking for possibly
2922 hidden functions. */
2923 for (binfo
= TYPE_BINFO (t
), j
= 0;
2924 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2926 tree basetype
= BINFO_TYPE (base_binfo
);
2927 get_basefndecls (name
, basetype
, &base_fndecls
);
2930 /* If there are no functions to hide, continue. */
2931 if (base_fndecls
.is_empty ())
2934 /* Remove any overridden functions. */
2935 for (ovl_iterator
iter (fns
); iter
; ++iter
)
2937 tree fndecl
= *iter
;
2938 if (TREE_CODE (fndecl
) == FUNCTION_DECL
2939 && DECL_VINDEX (fndecl
))
2941 /* If the method from the base class has the same
2942 signature as the method from the derived class, it
2943 has been overridden. */
2944 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
2946 && same_signature_p (fndecl
, base_fndecls
[k
]))
2947 base_fndecls
[k
] = NULL_TREE
;
2951 /* Now give a warning for all base functions without overriders,
2952 as they are hidden. */
2954 FOR_EACH_VEC_ELT (base_fndecls
, j
, base_fndecl
)
2957 auto_diagnostic_group d
;
2958 /* Here we know it is a hider, and no overrider exists. */
2959 if (warning_at (location_of (base_fndecl
),
2960 OPT_Woverloaded_virtual
,
2961 "%qD was hidden", base_fndecl
))
2962 inform (location_of (fns
), " by %qD", fns
);
2967 /* Recursive helper for finish_struct_anon. */
2970 finish_struct_anon_r (tree field
, bool complain
)
2972 for (tree elt
= TYPE_FIELDS (TREE_TYPE (field
)); elt
; elt
= DECL_CHAIN (elt
))
2974 /* We're generally only interested in entities the user
2975 declared, but we also find nested classes by noticing
2976 the TYPE_DECL that we create implicitly. You're
2977 allowed to put one anonymous union inside another,
2978 though, so we explicitly tolerate that. We use
2979 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2980 we also allow unnamed types used for defining fields. */
2981 if (DECL_ARTIFICIAL (elt
)
2982 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2983 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
2987 && (TREE_CODE (elt
) != FIELD_DECL
2988 || (TREE_PRIVATE (elt
) || TREE_PROTECTED (elt
))))
2990 /* We already complained about static data members in
2991 finish_static_data_member_decl. */
2994 auto_diagnostic_group d
;
2995 if (permerror (DECL_SOURCE_LOCATION (elt
),
2996 TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
2997 ? "%q#D invalid; an anonymous union may "
2998 "only have public non-static data members"
2999 : "%q#D invalid; an anonymous struct may "
3000 "only have public non-static data members", elt
))
3003 if (flag_permissive
&& !hint
)
3006 inform (DECL_SOURCE_LOCATION (elt
),
3007 "this flexibility is deprecated and will be "
3014 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3015 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3017 /* Recurse into the anonymous aggregates to correctly handle
3018 access control (c++/24926):
3029 if (DECL_NAME (elt
) == NULL_TREE
3030 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
3031 finish_struct_anon_r (elt
, /*complain=*/false);
3035 /* Check for things that are invalid. There are probably plenty of other
3036 things we should check for also. */
3039 finish_struct_anon (tree t
)
3041 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3043 if (TREE_STATIC (field
))
3045 if (TREE_CODE (field
) != FIELD_DECL
)
3048 if (DECL_NAME (field
) == NULL_TREE
3049 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3050 finish_struct_anon_r (field
, /*complain=*/true);
3054 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3055 will be used later during class template instantiation.
3056 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3057 a non-static member data (FIELD_DECL), a member function
3058 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3059 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3060 When FRIEND_P is nonzero, T is either a friend class
3061 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3062 (FUNCTION_DECL, TEMPLATE_DECL). */
3065 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
3067 if (CLASSTYPE_TEMPLATE_INFO (type
)
3068 && TREE_CODE (t
) != CONST_DECL
)
3070 tree purpose
= friend_p
? NULL_TREE
: type
;
3072 CLASSTYPE_DECL_LIST (type
)
3073 = tree_cons (purpose
, t
, CLASSTYPE_DECL_LIST (type
));
3077 /* This function is called from declare_virt_assop_and_dtor via
3080 DATA is a type that direcly or indirectly inherits the base
3081 represented by BINFO. If BINFO contains a virtual assignment [copy
3082 assignment or move assigment] operator or a virtual constructor,
3083 declare that function in DATA if it hasn't been already declared. */
3086 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
3088 tree bv
, fn
, t
= (tree
)data
;
3089 tree opname
= assign_op_identifier
;
3091 gcc_assert (t
&& CLASS_TYPE_P (t
));
3092 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
3094 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
3095 /* A base without a vtable needs no modification, and its bases
3096 are uninteresting. */
3097 return dfs_skip_bases
;
3099 if (BINFO_PRIMARY_P (binfo
))
3100 /* If this is a primary base, then we have already looked at the
3101 virtual functions of its vtable. */
3104 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3108 if (DECL_NAME (fn
) == opname
)
3110 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3111 lazily_declare_fn (sfk_copy_assignment
, t
);
3112 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3113 lazily_declare_fn (sfk_move_assignment
, t
);
3115 else if (DECL_DESTRUCTOR_P (fn
)
3116 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3117 lazily_declare_fn (sfk_destructor
, t
);
3123 /* If the class type T has a direct or indirect base that contains a
3124 virtual assignment operator or a virtual destructor, declare that
3125 function in T if it hasn't been already declared. */
3128 declare_virt_assop_and_dtor (tree t
)
3130 if (!(TYPE_POLYMORPHIC_P (t
)
3131 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3132 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3133 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3136 dfs_walk_all (TYPE_BINFO (t
),
3137 dfs_declare_virt_assop_and_dtor
,
3141 /* Declare the inheriting constructor for class T inherited from base
3142 constructor CTOR with the parameter array PARMS of size NPARMS. */
3145 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3147 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3149 /* We don't declare an inheriting ctor that would be a default,
3150 copy or move ctor for derived or base. */
3154 && TYPE_REF_P (parms
[0]))
3156 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3157 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3161 tree parmlist
= void_list_node
;
3162 for (int i
= nparms
- 1; i
>= 0; i
--)
3163 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3164 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3165 t
, false, ctor
, parmlist
);
3167 if (add_method (t
, fn
, false))
3169 DECL_CHAIN (fn
) = TYPE_FIELDS (t
);
3170 TYPE_FIELDS (t
) = fn
;
3174 /* Declare all the inheriting constructors for class T inherited from base
3175 constructor CTOR. */
3178 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3180 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3182 if (flag_new_inheriting_ctors
)
3184 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3185 t
, /*const*/false, ctor
, parms
);
3186 add_method (t
, ctor
, using_decl
!= NULL_TREE
);
3190 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3192 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3194 if (TREE_PURPOSE (parms
))
3195 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3196 new_parms
[i
++] = TREE_VALUE (parms
);
3198 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3199 if (parms
== NULL_TREE
)
3201 auto_diagnostic_group d
;
3202 if (warning (OPT_Winherited_variadic_ctor
,
3203 "the ellipsis in %qD is not inherited", ctor
))
3204 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3208 /* Create default constructors, assignment operators, and so forth for
3209 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3210 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3211 the class cannot have a default constructor, copy constructor
3212 taking a const reference argument, or an assignment operator taking
3213 a const reference, respectively. */
3216 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3217 int cant_have_const_cctor
,
3218 int cant_have_const_assignment
)
3221 if (!CLASSTYPE_DESTRUCTOR (t
))
3222 /* In general, we create destructors lazily. */
3223 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3225 bool move_ok
= false;
3226 if (cxx_dialect
>= cxx11
&& CLASSTYPE_LAZY_DESTRUCTOR (t
)
3227 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3228 && !classtype_has_move_assign_or_move_ctor_p (t
, false))
3233 If there is no user-declared constructor for a class, a default
3234 constructor is implicitly declared. */
3235 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3237 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3238 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3239 if (cxx_dialect
>= cxx11
)
3240 TYPE_HAS_CONSTEXPR_CTOR (t
)
3241 /* Don't force the declaration to get a hard answer; if the
3242 definition would have made the class non-literal, it will still be
3243 non-literal because of the base or member in question, and that
3244 gives a better diagnostic. */
3245 = type_maybe_constexpr_default_constructor (t
);
3250 If a class definition does not explicitly declare a copy
3251 constructor, one is declared implicitly. */
3252 if (! TYPE_HAS_COPY_CTOR (t
))
3254 TYPE_HAS_COPY_CTOR (t
) = 1;
3255 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3256 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3258 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3261 /* If there is no assignment operator, one will be created if and
3262 when it is needed. For now, just record whether or not the type
3263 of the parameter to the assignment operator will be a const or
3264 non-const reference. */
3265 if (!TYPE_HAS_COPY_ASSIGN (t
))
3267 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3268 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3269 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3270 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3271 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3274 /* We can't be lazy about declaring functions that might override
3275 a virtual function from a base class. */
3276 declare_virt_assop_and_dtor (t
);
3278 /* If the class definition does not explicitly declare an == operator
3279 function, but declares a defaulted three-way comparison operator function,
3280 an == operator function is declared implicitly. */
3281 if (!classtype_has_op (t
, EQ_EXPR
))
3282 if (tree space
= classtype_has_defaulted_op (t
, SPACESHIP_EXPR
))
3284 tree eq
= implicitly_declare_fn (sfk_comparison
, t
, false, space
,
3286 if (DECL_FRIEND_P (space
))
3287 do_friend (NULL_TREE
, DECL_NAME (eq
), eq
,
3288 NULL_TREE
, NO_SPECIAL
, true);
3291 add_method (t
, eq
, false);
3292 DECL_CHAIN (eq
) = TYPE_FIELDS (t
);
3293 TYPE_FIELDS (t
) = eq
;
3295 maybe_add_class_template_decl_list (t
, eq
, DECL_FRIEND_P (space
));
3298 while (*access_decls
)
3300 tree using_decl
= TREE_VALUE (*access_decls
);
3301 tree decl
= USING_DECL_DECLS (using_decl
);
3302 if (DECL_NAME (using_decl
) == ctor_identifier
)
3304 /* declare, then remove the decl */
3305 tree ctor_list
= decl
;
3306 location_t loc
= input_location
;
3307 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3308 for (ovl_iterator
iter (ctor_list
); iter
; ++iter
)
3309 one_inherited_ctor (*iter
, t
, using_decl
);
3310 *access_decls
= TREE_CHAIN (*access_decls
);
3311 input_location
= loc
;
3314 access_decls
= &TREE_CHAIN (*access_decls
);
3318 /* Cache of enum_min_precision values. */
3319 static GTY((deletable
)) hash_map
<tree
, int> *enum_to_min_precision
;
3321 /* Return the minimum precision of a bit-field needed to store all
3322 enumerators of ENUMERAL_TYPE TYPE. */
3325 enum_min_precision (tree type
)
3327 type
= TYPE_MAIN_VARIANT (type
);
3328 /* For unscoped enums without fixed underlying type and without mode
3329 attribute we can just use precision of the underlying type. */
3330 if (UNSCOPED_ENUM_P (type
)
3331 && !ENUM_FIXED_UNDERLYING_TYPE_P (type
)
3332 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type
)))
3333 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
));
3335 if (enum_to_min_precision
== NULL
)
3336 enum_to_min_precision
= hash_map
<tree
, int>::create_ggc (37);
3339 int &prec
= enum_to_min_precision
->get_or_insert (type
, &existed
);
3343 tree minnode
, maxnode
;
3344 if (TYPE_VALUES (type
))
3346 minnode
= maxnode
= NULL_TREE
;
3347 for (tree values
= TYPE_VALUES (type
);
3348 values
; values
= TREE_CHAIN (values
))
3350 tree decl
= TREE_VALUE (values
);
3351 tree value
= DECL_INITIAL (decl
);
3352 if (value
== error_mark_node
)
3353 value
= integer_zero_node
;
3355 minnode
= maxnode
= value
;
3356 else if (tree_int_cst_lt (maxnode
, value
))
3358 else if (tree_int_cst_lt (value
, minnode
))
3363 minnode
= maxnode
= integer_zero_node
;
3365 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
3366 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
3367 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
3368 prec
= MAX (lowprec
, highprec
);
3372 /* FIELD is a bit-field. We are finishing the processing for its
3373 enclosing type. Issue any appropriate messages and set appropriate
3374 flags. Returns false if an error has been diagnosed. */
3377 check_bitfield_decl (tree field
)
3379 tree type
= TREE_TYPE (field
);
3382 /* Extract the declared width of the bitfield, which has been
3383 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3384 w
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
3385 gcc_assert (w
!= NULL_TREE
);
3386 /* Remove the bit-field width indicator so that the rest of the
3387 compiler does not treat that value as a qualifier. */
3388 DECL_BIT_FIELD_REPRESENTATIVE (field
) = NULL_TREE
;
3390 /* Detect invalid bit-field type. */
3391 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3393 error_at (DECL_SOURCE_LOCATION (field
),
3394 "bit-field %q#D with non-integral type %qT", field
, type
);
3395 w
= error_mark_node
;
3399 location_t loc
= input_location
;
3400 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3403 /* detect invalid field size. */
3404 input_location
= DECL_SOURCE_LOCATION (field
);
3405 w
= cxx_constant_value (w
);
3406 input_location
= loc
;
3408 if (TREE_CODE (w
) != INTEGER_CST
)
3410 error ("bit-field %q+D width not an integer constant", field
);
3411 w
= error_mark_node
;
3413 else if (tree_int_cst_sgn (w
) < 0)
3415 error ("negative width in bit-field %q+D", field
);
3416 w
= error_mark_node
;
3418 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3420 error ("zero width for bit-field %q+D", field
);
3421 w
= error_mark_node
;
3423 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3424 && TREE_CODE (type
) != BOOLEAN_TYPE
3425 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3426 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3427 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3428 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3429 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3430 "width of %qD exceeds its type", field
);
3431 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3433 int prec
= enum_min_precision (type
);
3434 if (compare_tree_int (w
, prec
) < 0)
3435 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3436 "%qD is too small to hold all values of %q#T",
3441 if (w
!= error_mark_node
)
3443 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3444 DECL_BIT_FIELD (field
) = 1;
3449 /* Non-bit-fields are aligned for their type. */
3450 DECL_BIT_FIELD (field
) = 0;
3451 CLEAR_DECL_C_BIT_FIELD (field
);
3456 /* FIELD is a non bit-field. We are finishing the processing for its
3457 enclosing type T. Issue any appropriate messages and set appropriate
3461 check_field_decl (tree field
,
3463 int* cant_have_const_ctor
,
3464 int* no_const_asn_ref
)
3466 tree type
= strip_array_types (TREE_TYPE (field
));
3467 bool any_default_members
= false;
3469 /* In C++98 an anonymous union cannot contain any fields which would change
3470 the settings of CANT_HAVE_CONST_CTOR and friends. */
3471 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3473 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3474 structs. So, we recurse through their fields here. */
3475 else if (ANON_AGGR_TYPE_P (type
))
3477 for (tree fields
= TYPE_FIELDS (type
); fields
;
3478 fields
= DECL_CHAIN (fields
))
3479 if (TREE_CODE (fields
) == FIELD_DECL
)
3480 any_default_members
|= check_field_decl (fields
, t
,
3481 cant_have_const_ctor
,
3484 /* Check members with class type for constructors, destructors,
3486 else if (CLASS_TYPE_P (type
))
3488 /* Never let anything with uninheritable virtuals
3489 make it through without complaint. */
3490 abstract_virtuals_error (field
, type
);
3492 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3495 int oldcount
= errorcount
;
3496 if (TYPE_NEEDS_CONSTRUCTING (type
))
3497 error ("member %q+#D with constructor not allowed in union",
3499 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3500 error ("member %q+#D with destructor not allowed in union", field
);
3501 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3502 error ("member %q+#D with copy assignment operator not allowed in union",
3504 if (!warned
&& errorcount
> oldcount
)
3506 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3507 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3513 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3514 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3515 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3516 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3517 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3518 || !TYPE_HAS_COPY_ASSIGN (type
));
3519 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3520 || !TYPE_HAS_COPY_CTOR (type
));
3521 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3522 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3523 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3524 || TYPE_HAS_COMPLEX_DFLT (type
));
3527 if (TYPE_HAS_COPY_CTOR (type
)
3528 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3529 *cant_have_const_ctor
= 1;
3531 if (TYPE_HAS_COPY_ASSIGN (type
)
3532 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3533 *no_const_asn_ref
= 1;
3536 check_abi_tags (t
, field
);
3538 if (DECL_INITIAL (field
) != NULL_TREE
)
3539 /* `build_class_init_list' does not recognize
3541 any_default_members
= true;
3543 return any_default_members
;
3546 /* Check the data members (both static and non-static), class-scoped
3547 typedefs, etc., appearing in the declaration of T. Issue
3548 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3549 declaration order) of access declarations; each TREE_VALUE in this
3550 list is a USING_DECL.
3552 In addition, set the following flags:
3555 The class is empty, i.e., contains no non-static data members.
3557 CANT_HAVE_CONST_CTOR_P
3558 This class cannot have an implicitly generated copy constructor
3559 taking a const reference.
3561 CANT_HAVE_CONST_ASN_REF
3562 This class cannot have an implicitly generated assignment
3563 operator taking a const reference.
3565 All of these flags should be initialized before calling this
3569 check_field_decls (tree t
, tree
*access_decls
,
3570 int *cant_have_const_ctor_p
,
3571 int *no_const_asn_ref_p
)
3575 /* Assume there are no access declarations. */
3576 *access_decls
= NULL_TREE
;
3577 /* Effective C has things to say about classes with pointer members. */
3578 tree pointer_member
= NULL_TREE
;
3579 /* Default initialized members affect the whole class. */
3580 tree default_init_member
= NULL_TREE
;
3581 /* Lack of any non-static data member of non-volatile literal
3582 type affects a union. */
3583 bool found_nv_literal_p
= false;
3584 /* Standard layout requires all FIELDS have same access. */
3585 int field_access
= -1;
3587 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3589 tree type
= TREE_TYPE (field
);
3591 switch (TREE_CODE (field
))
3597 /* Save the access declarations for our caller. */
3598 *access_decls
= tree_cons (NULL_TREE
, field
, *access_decls
);
3606 /* FIXME: We should fold in the checking from check_methods. */
3610 DECL_NONLOCAL (field
) = 1;
3614 if (TREE_CODE (t
) == UNION_TYPE
3615 && cxx_dialect
< cxx11
)
3619 (C++98) If a union contains a static data member,
3620 ... the program is ill-formed. */
3621 if (cxx_dialect
< cxx11
)
3622 error ("in C++98 %q+D may not be static because it is "
3623 "a member of a union", field
);
3628 if (TREE_CODE (t
) == UNION_TYPE
)
3632 If a union contains ... or a [non-static data] member
3633 of reference type, the program is ill-formed. */
3634 if (TYPE_REF_P (type
))
3635 error ("non-static data member %q+D in a union may not "
3636 "have reference type %qT", field
, type
);
3640 /* Common VAR_DECL & FIELD_DECL processing. */
3641 DECL_CONTEXT (field
) = t
;
3642 DECL_NONLOCAL (field
) = 1;
3644 /* Template instantiation can cause this. Perhaps this
3645 should be a specific instantiation check? */
3646 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3648 error ("data member %q+D invalidly declared function type", field
);
3649 type
= build_pointer_type (type
);
3650 TREE_TYPE (field
) = type
;
3652 else if (TREE_CODE (type
) == METHOD_TYPE
)
3654 error ("data member %q+D invalidly declared method type", field
);
3655 type
= build_pointer_type (type
);
3656 TREE_TYPE (field
) = type
;
3662 if (TREE_CODE (field
) != FIELD_DECL
)
3665 if (type
== error_mark_node
)
3668 /* If it is not a union and at least one non-static data member is
3669 non-literal, the whole class becomes non-literal. Per Core/1453,
3670 volatile non-static data members and base classes are also not allowed.
3671 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3673 Note: if the type is incomplete we will complain later on. */
3674 if (COMPLETE_TYPE_P (type
))
3676 if (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
))
3677 CLASSTYPE_LITERAL_P (t
) = false;
3679 found_nv_literal_p
= true;
3682 int this_field_access
= (TREE_PROTECTED (field
) ? 1
3683 : TREE_PRIVATE (field
) ? 2 : 0);
3684 if (field_access
!= this_field_access
)
3686 /* A standard-layout class is a class that:
3688 ... has the same access control (Clause 11) for all
3689 non-static data members, */
3690 if (field_access
< 0)
3691 field_access
= this_field_access
;
3693 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3695 /* Aggregates must be public. */
3696 if (this_field_access
)
3697 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3700 /* If this is of reference type, check if it needs an init. */
3701 if (TYPE_REF_P (type
))
3703 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3704 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3705 if (DECL_INITIAL (field
) == NULL_TREE
)
3706 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3707 if (cxx_dialect
< cxx11
)
3709 /* ARM $12.6.2: [A member initializer list] (or, for an
3710 aggregate, initialization by a brace-enclosed list) is the
3711 only way to initialize non-static const and reference
3713 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3714 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3718 type
= strip_array_types (type
);
3720 if (TYPE_PACKED (t
))
3722 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3724 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3725 "ignoring packed attribute because of"
3726 " unpacked non-POD field %q#D", field
);
3729 else if (DECL_C_BIT_FIELD (field
)
3730 || TYPE_ALIGN (TREE_TYPE (field
)) > BITS_PER_UNIT
)
3731 DECL_PACKED (field
) = 1;
3734 if (DECL_C_BIT_FIELD (field
)
3735 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field
)))
3736 /* We don't treat zero-width bitfields as making a class
3739 else if (field_poverlapping_p (field
)
3740 && is_empty_class (TREE_TYPE (field
)))
3741 /* Empty data members also don't make a class non-empty. */
3742 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3745 /* The class is non-empty. */
3746 CLASSTYPE_EMPTY_P (t
) = 0;
3747 /* The class is not even nearly empty. */
3748 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3749 /* If one of the data members contains an empty class, so
3751 if (CLASS_TYPE_P (type
)
3752 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3753 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3756 /* This is used by -Weffc++ (see below). Warn only for pointers
3757 to members which might hold dynamic memory. So do not warn
3758 for pointers to functions or pointers to members. */
3759 if (TYPE_PTR_P (type
)
3760 && !TYPE_PTRFN_P (type
))
3761 pointer_member
= field
;
3763 if (CLASS_TYPE_P (type
))
3765 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3766 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3767 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3768 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3771 if (DECL_MUTABLE_P (field
) || TYPE_HAS_MUTABLE_P (type
))
3772 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3774 if (DECL_MUTABLE_P (field
))
3776 if (TYPE_REF_P (type
))
3777 error ("member %q+D cannot be declared as a %<mutable%> "
3778 "reference", field
);
3779 else if (CP_TYPE_CONST_P (type
))
3780 error ("member %q+D cannot be declared both %<const%> "
3781 "and %<mutable%>", field
);
3784 if (! layout_pod_type_p (type
))
3785 /* DR 148 now allows pointers to members (which are POD themselves),
3786 to be allowed in POD structs. */
3787 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3789 if (field_poverlapping_p (field
))
3790 /* A potentially-overlapping non-static data member makes the class
3792 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3794 if (!std_layout_type_p (type
))
3795 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3797 if (! zero_init_p (type
))
3798 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3800 /* We set DECL_C_BIT_FIELD in grokbitfield.
3801 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3802 if (DECL_C_BIT_FIELD (field
))
3803 check_bitfield_decl (field
);
3805 if (check_field_decl (field
, t
,
3806 cant_have_const_ctor_p
, no_const_asn_ref_p
))
3808 if (default_init_member
3809 && TREE_CODE (t
) == UNION_TYPE
)
3811 error ("multiple fields in union %qT initialized", t
);
3812 inform (DECL_SOURCE_LOCATION (default_init_member
),
3813 "initialized member %q+D declared here",
3814 default_init_member
);
3816 default_init_member
= field
;
3819 /* Now that we've removed bit-field widths from DECL_INITIAL,
3820 anything left in DECL_INITIAL is an NSDMI that makes the class
3821 non-aggregate in C++11. */
3822 if (DECL_INITIAL (field
) && cxx_dialect
< cxx14
)
3823 CLASSTYPE_NON_AGGREGATE (t
) = true;
3825 if (CP_TYPE_CONST_P (type
))
3827 /* If any field is const, the structure type is pseudo-const. */
3828 C_TYPE_FIELDS_READONLY (t
) = 1;
3829 if (DECL_INITIAL (field
) == NULL_TREE
)
3830 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3831 if (cxx_dialect
< cxx11
)
3833 /* ARM $12.6.2: [A member initializer list] (or, for an
3834 aggregate, initialization by a brace-enclosed list) is the
3835 only way to initialize non-static const and reference
3837 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3838 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3841 /* A field that is pseudo-const makes the structure likewise. */
3842 else if (CLASS_TYPE_P (type
))
3844 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3845 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3846 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3847 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3850 /* Core issue 80: A non-static data member is required to have a
3851 different name from the class iff the class has a
3852 user-declared constructor. */
3853 if (constructor_name_p (DECL_NAME (field
), t
)
3854 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3855 permerror (DECL_SOURCE_LOCATION (field
),
3856 "field %q#D with same name as class", field
);
3859 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3860 one of its non-static data members is of non-volatile literal type. */
3861 if (TREE_CODE (t
) == UNION_TYPE
&& found_nv_literal_p
)
3862 CLASSTYPE_LITERAL_P (t
) = true;
3864 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3865 it should also define a copy constructor and an assignment operator to
3866 implement the correct copy semantic (deep vs shallow, etc.). As it is
3867 not feasible to check whether the constructors do allocate dynamic memory
3868 and store it within members, we approximate the warning like this:
3870 -- Warn only if there are members which are pointers
3871 -- Warn only if there is a non-trivial constructor (otherwise,
3872 there cannot be memory allocated).
3873 -- Warn only if there is a non-trivial destructor. We assume that the
3874 user at least implemented the cleanup correctly, and a destructor
3875 is needed to free dynamic memory.
3877 This seems enough for practical purposes. */
3880 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3881 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3882 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3884 if (warning (OPT_Weffc__
, "%q#T has pointer data members", t
))
3886 if (! TYPE_HAS_COPY_CTOR (t
))
3888 warning (OPT_Weffc__
,
3889 " but does not declare %<%T(const %T&)%>", t
, t
);
3890 if (!TYPE_HAS_COPY_ASSIGN (t
))
3891 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3893 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3894 warning (OPT_Weffc__
,
3895 " but does not declare %<operator=(const %T&)%>", t
);
3896 inform (DECL_SOURCE_LOCATION (pointer_member
),
3897 "pointer member %q+D declared here", pointer_member
);
3901 /* Non-static data member initializers make the default constructor
3903 if (default_init_member
)
3905 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3906 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3909 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3911 TYPE_PACKED (t
) = 0;
3913 /* Check anonymous struct/anonymous union fields. */
3914 finish_struct_anon (t
);
3916 /* We've built up the list of access declarations in reverse order.
3918 *access_decls
= nreverse (*access_decls
);
3921 /* If TYPE is an empty class type, records its OFFSET in the table of
3925 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3929 if (!is_empty_class (type
))
3932 /* Record the location of this empty object in OFFSETS. */
3933 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3935 n
= splay_tree_insert (offsets
,
3936 (splay_tree_key
) offset
,
3937 (splay_tree_value
) NULL_TREE
);
3938 n
->value
= ((splay_tree_value
)
3939 tree_cons (NULL_TREE
,
3946 /* Returns nonzero if TYPE is an empty class type and there is
3947 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3950 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3955 if (!is_empty_class (type
))
3958 /* Record the location of this empty object in OFFSETS. */
3959 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3963 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3964 if (same_type_p (TREE_VALUE (t
), type
))
3970 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3971 F for every subobject, passing it the type, offset, and table of
3972 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3975 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3976 than MAX_OFFSET will not be walked.
3978 If F returns a nonzero value, the traversal ceases, and that value
3979 is returned. Otherwise, returns zero. */
3982 walk_subobject_offsets (tree type
,
3983 subobject_offset_fn f
,
3990 tree type_binfo
= NULL_TREE
;
3992 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3994 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3997 if (type
== error_mark_node
)
4003 type
= BINFO_TYPE (type
);
4006 if (CLASS_TYPE_P (type
))
4012 /* Avoid recursing into objects that are not interesting. */
4013 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
4016 /* Record the location of TYPE. */
4017 r
= (*f
) (type
, offset
, offsets
);
4021 /* Iterate through the direct base classes of TYPE. */
4023 type_binfo
= TYPE_BINFO (type
);
4024 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
4028 if (BINFO_VIRTUAL_P (binfo
))
4032 /* We cannot rely on BINFO_OFFSET being set for the base
4033 class yet, but the offsets for direct non-virtual
4034 bases can be calculated by going back to the TYPE. */
4035 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
4036 binfo_offset
= size_binop (PLUS_EXPR
,
4038 BINFO_OFFSET (orig_binfo
));
4040 r
= walk_subobject_offsets (binfo
,
4050 if (CLASSTYPE_VBASECLASSES (type
))
4053 vec
<tree
, va_gc
> *vbases
;
4055 /* Iterate through the virtual base classes of TYPE. In G++
4056 3.2, we included virtual bases in the direct base class
4057 loop above, which results in incorrect results; the
4058 correct offsets for virtual bases are only known when
4059 working with the most derived type. */
4061 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
4062 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
4064 r
= walk_subobject_offsets (binfo
,
4066 size_binop (PLUS_EXPR
,
4068 BINFO_OFFSET (binfo
)),
4077 /* We still have to walk the primary base, if it is
4078 virtual. (If it is non-virtual, then it was walked
4080 tree vbase
= get_primary_binfo (type_binfo
);
4082 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
4083 && BINFO_PRIMARY_P (vbase
)
4084 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
4086 r
= (walk_subobject_offsets
4088 offsets
, max_offset
, /*vbases_p=*/0));
4095 /* Iterate through the fields of TYPE. */
4096 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4097 if (TREE_CODE (field
) == FIELD_DECL
4098 && TREE_TYPE (field
) != error_mark_node
4099 && !DECL_ARTIFICIAL (field
))
4103 field_offset
= byte_position (field
);
4105 r
= walk_subobject_offsets (TREE_TYPE (field
),
4107 size_binop (PLUS_EXPR
,
4117 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4119 tree element_type
= strip_array_types (type
);
4120 tree domain
= TYPE_DOMAIN (type
);
4123 /* Avoid recursing into objects that are not interesting. */
4124 if (!CLASS_TYPE_P (element_type
)
4125 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
4127 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
4130 /* Step through each of the elements in the array. */
4131 for (index
= size_zero_node
;
4132 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
4133 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
4135 r
= walk_subobject_offsets (TREE_TYPE (type
),
4143 offset
= size_binop (PLUS_EXPR
, offset
,
4144 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
4145 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4146 there's no point in iterating through the remaining
4147 elements of the array. */
4148 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4156 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4159 field_poverlapping_p (tree decl
)
4161 /* Base fields are actually potentially overlapping, but C++ bases go through
4162 a different code path based on binfos, and ObjC++ base fields are laid out
4163 in objc-act, so we don't want layout_class_type to mess with them. */
4164 if (DECL_FIELD_IS_BASE (decl
))
4166 gcc_checking_assert (c_dialect_objc ());
4170 return lookup_attribute ("no_unique_address",
4171 DECL_ATTRIBUTES (decl
));
4174 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4177 record_subobject_offsets (tree decl_or_binfo
,
4181 bool overlapping
, vbases_p
;
4183 if (DECL_P (decl_or_binfo
))
4185 tree decl
= decl_or_binfo
;
4186 type
= TREE_TYPE (decl
);
4187 offset
= byte_position (decl
);
4188 overlapping
= field_poverlapping_p (decl
);
4193 type
= BINFO_TYPE (decl_or_binfo
);
4194 offset
= BINFO_OFFSET (decl_or_binfo
);
4200 /* If recording subobjects for a non-static data member or a
4201 non-empty base class, we do not need to record offsets beyond
4202 the size of the biggest empty class. Additional data members
4203 will go at the end of the class. Additional base classes will go
4204 either at offset zero (if empty, in which case they cannot
4205 overlap with offsets past the size of the biggest empty class) or
4206 at the end of the class.
4208 However, if we are placing an empty base class, then we must record
4209 all offsets, as either the empty class is at offset zero (where
4210 other empty classes might later be placed) or at the end of the
4211 class (where other objects might then be placed, so other empty
4212 subobjects might later overlap). */
4214 || !is_empty_class (type
))
4215 max_offset
= sizeof_biggest_empty_class
;
4217 max_offset
= NULL_TREE
;
4218 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4219 offsets
, max_offset
, vbases_p
);
4222 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4223 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4224 virtual bases of TYPE are examined. */
4227 layout_conflict_p (tree type
,
4232 splay_tree_node max_node
;
4234 /* Get the node in OFFSETS that indicates the maximum offset where
4235 an empty subobject is located. */
4236 max_node
= splay_tree_max (offsets
);
4237 /* If there aren't any empty subobjects, then there's no point in
4238 performing this check. */
4242 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4243 offsets
, (tree
) (max_node
->key
),
4247 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4248 non-static data member of the type indicated by RLI. BINFO is the
4249 binfo corresponding to the base subobject, OFFSETS maps offsets to
4250 types already located at those offsets. This function determines
4251 the position of the DECL. */
4254 layout_nonempty_base_or_field (record_layout_info rli
,
4259 tree offset
= NULL_TREE
;
4265 /* For the purposes of determining layout conflicts, we want to
4266 use the class type of BINFO; TREE_TYPE (DECL) will be the
4267 CLASSTYPE_AS_BASE version, which does not contain entries for
4268 zero-sized bases. */
4269 type
= TREE_TYPE (binfo
);
4274 type
= TREE_TYPE (decl
);
4278 /* Try to place the field. It may take more than one try if we have
4279 a hard time placing the field without putting two objects of the
4280 same type at the same address. */
4283 struct record_layout_info_s old_rli
= *rli
;
4285 /* Place this field. */
4286 place_field (rli
, decl
);
4287 offset
= byte_position (decl
);
4289 /* We have to check to see whether or not there is already
4290 something of the same type at the offset we're about to use.
4291 For example, consider:
4294 struct T : public S { int i; };
4295 struct U : public S, public T {};
4297 Here, we put S at offset zero in U. Then, we can't put T at
4298 offset zero -- its S component would be at the same address
4299 as the S we already allocated. So, we have to skip ahead.
4300 Since all data members, including those whose type is an
4301 empty class, have nonzero size, any overlap can happen only
4302 with a direct or indirect base-class -- it can't happen with
4304 /* In a union, overlap is permitted; all members are placed at
4306 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4308 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4311 /* Strip off the size allocated to this field. That puts us
4312 at the first place we could have put the field with
4313 proper alignment. */
4316 /* Bump up by the alignment required for the type. */
4318 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4320 ? CLASSTYPE_ALIGN (type
)
4321 : TYPE_ALIGN (type
)));
4322 normalize_rli (rli
);
4324 else if (TREE_CODE (type
) == NULLPTR_TYPE
4325 && warn_abi
&& abi_version_crosses (9))
4327 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4328 the offset wasn't aligned like a pointer when we started to
4329 layout this field, that affects its position. */
4330 tree pos
= rli_size_unit_so_far (&old_rli
);
4331 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4333 if (abi_version_at_least (9))
4334 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4335 "alignment of %qD increased in %<-fabi-version=9%> "
4338 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4339 "of %qD will increase in %<-fabi-version=9%>",
4345 /* There was no conflict. We're done laying out this field. */
4349 /* Now that we know where it will be placed, update its
4351 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4352 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4353 this point because their BINFO_OFFSET is copied from another
4354 hierarchy. Therefore, we may not need to add the entire
4356 propagate_binfo_offsets (binfo
,
4357 size_diffop_loc (input_location
,
4358 fold_convert (ssizetype
, offset
),
4359 fold_convert (ssizetype
,
4360 BINFO_OFFSET (binfo
))));
4363 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4366 empty_base_at_nonzero_offset_p (tree type
,
4368 splay_tree
/*offsets*/)
4370 return is_empty_class (type
) && !integer_zerop (offset
);
4373 /* Layout the empty base BINFO. EOC indicates the byte currently just
4374 past the end of the class, and should be correctly aligned for a
4375 class of the type indicated by BINFO; OFFSETS gives the offsets of
4376 the empty bases allocated so far. T is the most derived
4377 type. Return nonzero iff we added it at the end. */
4380 layout_empty_base_or_field (record_layout_info rli
, tree binfo_or_decl
,
4385 tree binfo
= NULL_TREE
;
4386 tree decl
= NULL_TREE
;
4388 if (TREE_CODE (binfo_or_decl
) == TREE_BINFO
)
4390 binfo
= binfo_or_decl
;
4391 type
= BINFO_TYPE (binfo
);
4395 decl
= binfo_or_decl
;
4396 type
= TREE_TYPE (decl
);
4399 /* On some platforms (ARM), even empty classes will not be
4401 tree eoc
= round_up_loc (input_location
,
4402 rli_size_unit_so_far (rli
),
4403 CLASSTYPE_ALIGN_UNIT (type
));
4405 /* This routine should only be used for empty classes. */
4406 gcc_assert (is_empty_class (type
));
4408 if (decl
&& DECL_USER_ALIGN (decl
))
4409 alignment
= size_int (DECL_ALIGN_UNIT (decl
));
4411 alignment
= size_int (CLASSTYPE_ALIGN_UNIT (type
));
4413 /* This is an empty base class. We first try to put it at offset
4415 tree offset
= size_zero_node
;
4416 if (TREE_CODE (rli
->t
) != UNION_TYPE
4417 && layout_conflict_p (type
,
4422 /* That didn't work. Now, we move forward from the next
4423 available spot in the class. */
4428 if (!layout_conflict_p (type
,
4432 /* We finally found a spot where there's no overlap. */
4435 /* There's overlap here, too. Bump along to the next spot. */
4436 offset
= size_binop (PLUS_EXPR
, offset
, alignment
);
4440 if (decl
&& DECL_USER_ALIGN (decl
))
4442 rli
->record_align
= MAX (rli
->record_align
, DECL_ALIGN (decl
));
4444 rli
->unpacked_align
= MAX (rli
->unpacked_align
, DECL_ALIGN (decl
));
4445 TYPE_USER_ALIGN (rli
->t
) = 1;
4447 else if (CLASSTYPE_USER_ALIGN (type
))
4449 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (type
));
4451 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (type
));
4452 TYPE_USER_ALIGN (rli
->t
) = 1;
4456 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4457 propagate_binfo_offsets (binfo
,
4458 size_diffop (offset
, BINFO_OFFSET (binfo
)));
4461 DECL_FIELD_OFFSET (decl
) = offset
;
4462 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4463 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4469 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4470 fields at NEXT_FIELD, and return it. */
4473 build_base_field_1 (tree t
, tree binfo
, tree access
, tree
*&next_field
)
4475 /* Create the FIELD_DECL. */
4476 tree basetype
= BINFO_TYPE (binfo
);
4477 tree as_base
= CLASSTYPE_AS_BASE (basetype
);
4478 gcc_assert (as_base
);
4479 tree decl
= build_decl (input_location
, FIELD_DECL
, NULL_TREE
, as_base
);
4481 DECL_ARTIFICIAL (decl
) = 1;
4482 DECL_IGNORED_P (decl
) = 1;
4483 DECL_FIELD_CONTEXT (decl
) = t
;
4484 if (is_empty_class (basetype
))
4485 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4486 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = size_zero_node
;
4489 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4490 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4492 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4493 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4494 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4495 DECL_FIELD_IS_BASE (decl
) = 1;
4497 if (access
== access_private_node
)
4498 TREE_PRIVATE (decl
) = true;
4499 else if (access
== access_protected_node
)
4500 TREE_PROTECTED (decl
) = true;
4502 /* Add the new FIELD_DECL to the list of fields for T. */
4503 DECL_CHAIN (decl
) = *next_field
;
4505 next_field
= &DECL_CHAIN (decl
);
4510 /* Layout the base given by BINFO in the class indicated by RLI.
4511 *BASE_ALIGN is a running maximum of the alignments of
4512 any base class. OFFSETS gives the location of empty base
4513 subobjects. T is the most derived type. Return nonzero if the new
4514 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4515 *NEXT_FIELD, unless BINFO is for an empty base class.
4517 Returns the location at which the next field should be inserted. */
4520 build_base_field (record_layout_info rli
, tree binfo
, tree access
,
4521 splay_tree offsets
, tree
*next_field
)
4524 tree basetype
= BINFO_TYPE (binfo
);
4526 if (!COMPLETE_TYPE_P (basetype
))
4527 /* This error is now reported in xref_tag, thus giving better
4528 location information. */
4531 /* Place the base class. */
4532 if (!is_empty_class (basetype
))
4536 /* The containing class is non-empty because it has a non-empty
4538 CLASSTYPE_EMPTY_P (t
) = 0;
4540 /* Create the FIELD_DECL. */
4541 decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4543 /* Try to place the field. It may take more than one try if we
4544 have a hard time placing the field without putting two
4545 objects of the same type at the same address. */
4546 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4550 bool atend
= layout_empty_base_or_field (rli
, binfo
, offsets
);
4551 /* A nearly-empty class "has no proper base class that is empty,
4552 not morally virtual, and at an offset other than zero." */
4553 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4556 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4557 /* The check above (used in G++ 3.2) is insufficient because
4558 an empty class placed at offset zero might itself have an
4559 empty base at a nonzero offset. */
4560 else if (walk_subobject_offsets (basetype
,
4561 empty_base_at_nonzero_offset_p
,
4564 /*max_offset=*/NULL_TREE
,
4566 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4569 /* We used to not create a FIELD_DECL for empty base classes because of
4570 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4571 be a problem anymore. We need them to handle initialization of C++17
4573 if (cxx_dialect
>= cxx17
&& !BINFO_VIRTUAL_P (binfo
))
4575 tree decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4576 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4577 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4578 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4579 DECL_FIELD_ABI_IGNORED (decl
) = 1;
4582 /* An empty virtual base causes a class to be non-empty
4583 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4584 here because that was already done when the virtual table
4585 pointer was created. */
4588 /* Record the offsets of BINFO and its base subobjects. */
4589 record_subobject_offsets (binfo
, offsets
);
4594 /* Layout all of the non-virtual base classes. Record empty
4595 subobjects in OFFSETS. T is the most derived type. Return nonzero
4596 if the type cannot be nearly empty. The fields created
4597 corresponding to the base classes will be inserted at
4601 build_base_fields (record_layout_info rli
,
4602 splay_tree offsets
, tree
*next_field
)
4604 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4607 tree binfo
= TYPE_BINFO (t
);
4608 int n_baseclasses
= BINFO_N_BASE_BINFOS (binfo
);
4610 /* The primary base class is always allocated first. */
4611 const tree primary_binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
4614 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4615 base, if it is direct. Indirect base fields are private. */
4616 tree primary_access
= access_private_node
;
4617 for (int i
= 0; i
< n_baseclasses
; ++i
)
4619 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4620 if (base_binfo
== primary_binfo
)
4622 primary_access
= BINFO_BASE_ACCESS (binfo
, i
);
4626 next_field
= build_base_field (rli
, primary_binfo
,
4628 offsets
, next_field
);
4631 /* Now allocate the rest of the bases. */
4632 for (int i
= 0; i
< n_baseclasses
; ++i
)
4634 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4636 /* The primary base was already allocated above, so we don't
4637 need to allocate it again here. */
4638 if (base_binfo
== primary_binfo
)
4641 /* Virtual bases are added at the end (a primary virtual base
4642 will have already been added). */
4643 if (BINFO_VIRTUAL_P (base_binfo
))
4646 next_field
= build_base_field (rli
, base_binfo
,
4647 BINFO_BASE_ACCESS (binfo
, i
),
4648 offsets
, next_field
);
4652 /* Go through the TYPE_FIELDS of T issuing any appropriate
4653 diagnostics, figuring out which methods override which other
4654 methods, and so forth. */
4657 check_methods (tree t
)
4659 for (tree x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
4660 if (DECL_DECLARES_FUNCTION_P (x
))
4662 check_for_override (x
, t
);
4664 if (DECL_PURE_VIRTUAL_P (x
)
4665 && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4666 error ("initializer specified for non-virtual method %q+D", x
);
4667 /* The name of the field is the original field name
4668 Save this in auxiliary field for later overloading. */
4669 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4671 TYPE_POLYMORPHIC_P (t
) = 1;
4672 if (DECL_PURE_VIRTUAL_P (x
))
4673 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4676 if (!DECL_VIRTUAL_P (x
)
4677 && lookup_attribute ("transaction_safe_dynamic",
4678 DECL_ATTRIBUTES (x
)))
4679 error_at (DECL_SOURCE_LOCATION (x
),
4680 "%<transaction_safe_dynamic%> may only be specified for "
4681 "a virtual function");
4684 /* Check whether the eligible special member functions (P0848) are
4685 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4686 has the eligible ones; TYPE_FIELDS also contains ineligible overloads,
4687 which is why this needs to be separate from the loop above. */
4689 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
4691 if (TREE_CODE (dtor
) == OVERLOAD
)
4693 /* P0848: At the end of the definition of a class, overload
4694 resolution is performed among the prospective destructors declared
4695 in that class with an empty argument list to select the destructor
4696 for the class, also known as the selected destructor. The program
4697 is ill-formed if overload resolution fails. */
4698 auto_diagnostic_group d
;
4699 error_at (location_of (t
), "destructor for %qT is ambiguous", t
);
4700 print_candidates (dtor
);
4702 else if (user_provided_p (dtor
))
4703 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = true;
4706 for (ovl_iterator
i (CLASSTYPE_CONSTRUCTORS (t
)); i
; ++i
)
4709 if (!user_provided_p (fn
))
4710 /* Might be trivial. */;
4711 else if (copy_fn_p (fn
))
4712 TYPE_HAS_COMPLEX_COPY_CTOR (t
) = true;
4713 else if (move_fn_p (fn
))
4714 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) = true;
4717 for (ovl_iterator
i (get_class_binding_direct (t
, assign_op_identifier
));
4721 if (!user_provided_p (fn
))
4722 /* Might be trivial. */;
4723 else if (copy_fn_p (fn
))
4724 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = true;
4725 else if (move_fn_p (fn
))
4726 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = true;
4730 /* FN is constructor, destructor or operator function. Clone the
4731 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4732 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4735 copy_fndecl_with_name (tree fn
, tree name
, tree_code code
,
4736 bool need_vtt_parm_p
, bool omit_inherited_parms_p
)
4738 /* Copy the function. */
4739 tree clone
= copy_decl (fn
);
4740 /* Reset the function name. */
4741 DECL_NAME (clone
) = name
;
4744 /* Clone constraints. */
4745 if (tree ci
= get_constraints (fn
))
4746 set_constraints (clone
, copy_node (ci
));
4748 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4749 /* There's no pending inline data for this function. */
4750 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4751 DECL_PENDING_INLINE_P (clone
) = 0;
4753 if (name
== base_dtor_identifier
)
4755 /* The base-class destructor is not virtual. */
4756 DECL_VIRTUAL_P (clone
) = 0;
4757 DECL_VINDEX (clone
) = NULL_TREE
;
4759 else if (code
!= ERROR_MARK
)
4761 /* Set the operator code. */
4762 const ovl_op_info_t
*ovl_op
= OVL_OP_INFO (false, code
);
4763 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone
) = ovl_op
->ovl_op_code
;
4765 /* The operator could be virtual. */
4766 if (DECL_VIRTUAL_P (clone
))
4767 IDENTIFIER_VIRTUAL_P (name
) = true;
4770 if (omit_inherited_parms_p
)
4771 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone
));
4773 /* If there was an in-charge parameter, drop it from the function
4775 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4777 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4778 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4779 /* Skip the `this' parameter. */
4780 parmtypes
= TREE_CHAIN (parmtypes
);
4781 /* Skip the in-charge parameter. */
4782 parmtypes
= TREE_CHAIN (parmtypes
);
4783 /* And the VTT parm, in a complete [cd]tor. */
4784 if (DECL_HAS_VTT_PARM_P (fn
) && !need_vtt_parm_p
)
4785 parmtypes
= TREE_CHAIN (parmtypes
);
4786 if (omit_inherited_parms_p
)
4788 /* If we're omitting inherited parms, that just leaves the VTT. */
4789 gcc_assert (need_vtt_parm_p
);
4790 parmtypes
= tree_cons (NULL_TREE
, vtt_parm_type
, void_list_node
);
4793 = build_method_type_directly (basetype
,
4794 TREE_TYPE (TREE_TYPE (clone
)),
4797 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4798 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4800 = cxx_copy_lang_qualifiers (TREE_TYPE (clone
), TREE_TYPE (fn
));
4803 /* Copy the function parameters. */
4804 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4806 /* Remove the in-charge parameter. */
4807 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4809 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4810 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4811 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4814 /* And the VTT parm, in a complete [cd]tor. */
4815 if (DECL_HAS_VTT_PARM_P (fn
))
4817 if (need_vtt_parm_p
)
4818 DECL_HAS_VTT_PARM_P (clone
) = 1;
4821 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4822 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4823 DECL_HAS_VTT_PARM_P (clone
) = 0;
4827 /* A base constructor inheriting from a virtual base doesn't get the
4829 if (omit_inherited_parms_p
)
4830 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
4832 for (tree parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4834 DECL_CONTEXT (parms
) = clone
;
4835 cxx_dup_lang_specific_decl (parms
);
4838 /* Create the RTL for this function. */
4839 SET_DECL_RTL (clone
, NULL
);
4840 rest_of_decl_compilation (clone
, namespace_bindings_p (), at_eof
);
4845 /* FN is an operator function, create a variant for CODE. */
4848 copy_operator_fn (tree fn
, tree_code code
)
4850 return copy_fndecl_with_name (fn
, ovl_op_identifier (code
),
4851 code
, false, false);
4854 /* FN is a constructor or destructor. Clone the declaration to create
4855 a specialized in-charge or not-in-charge version, as indicated by
4859 build_clone (tree fn
, tree name
, bool need_vtt_parm_p
,
4860 bool omit_inherited_parms_p
)
4864 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4865 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4867 clone
= copy_decl (fn
);
4868 DECL_NAME (clone
) = name
;
4870 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
,
4871 need_vtt_parm_p
, omit_inherited_parms_p
);
4872 DECL_TEMPLATE_RESULT (clone
) = result
;
4874 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4875 DECL_TI_TEMPLATE (result
) = clone
;
4877 TREE_TYPE (clone
) = TREE_TYPE (result
);
4881 clone
= copy_fndecl_with_name (fn
, name
, ERROR_MARK
,
4882 need_vtt_parm_p
, omit_inherited_parms_p
);
4883 DECL_CLONED_FUNCTION (clone
) = fn
;
4886 /* Remember where this function came from. */
4887 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4889 /* Make it easy to find the CLONE given the FN. Note the
4890 template_result of a template will be chained this way too. */
4891 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4892 DECL_CHAIN (fn
) = clone
;
4897 /* Build the clones of FN, return the number of clones built. These
4898 will be inserted onto DECL_CHAIN of FN. */
4901 build_cdtor_clones (tree fn
, bool needs_vtt_p
, bool base_omits_inherited_p
)
4905 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4907 /* For each constructor, we need two variants: an in-charge version
4908 and a not-in-charge version. */
4909 build_clone (fn
, complete_ctor_identifier
, false, false);
4910 build_clone (fn
, base_ctor_identifier
, needs_vtt_p
,
4911 base_omits_inherited_p
);
4916 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4918 /* For each destructor, we need three variants: an in-charge
4919 version, a not-in-charge version, and an in-charge deleting
4920 version. We clone the deleting version first because that
4921 means it will go second on the TYPE_FIELDS list -- and that
4922 corresponds to the correct layout order in the virtual
4925 For a non-virtual destructor, we do not build a deleting
4927 if (DECL_VIRTUAL_P (fn
))
4929 build_clone (fn
, deleting_dtor_identifier
, false, false);
4932 build_clone (fn
, complete_dtor_identifier
, false, false);
4933 build_clone (fn
, base_dtor_identifier
, needs_vtt_p
, false);
4940 /* Produce declarations for all appropriate clones of FN. If
4941 UPDATE_METHODS is true, the clones are added to the
4942 CLASSTYPE_MEMBER_VEC. */
4945 clone_cdtor (tree fn
, bool update_methods
)
4947 /* Avoid inappropriate cloning. */
4949 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4952 /* Base cdtors need a vtt parm if there are virtual bases. */
4953 bool vtt
= CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
));
4955 /* Base ctor omits inherited parms it needs a vttparm and inherited
4956 from a virtual nase ctor. */
4957 bool base_omits_inherited
= (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
)
4958 && base_ctor_omit_inherited_parms (fn
));
4960 unsigned count
= build_cdtor_clones (fn
, vtt
, base_omits_inherited
);
4962 /* Note that this is an abstract function that is never emitted. */
4963 DECL_ABSTRACT_P (fn
) = true;
4966 for (tree clone
= fn
; count
--;)
4968 clone
= DECL_CHAIN (clone
);
4969 add_method (DECL_CONTEXT (clone
), clone
, false);
4973 /* DECL is an in charge constructor, which is being defined. This will
4974 have had an in class declaration, from whence clones were
4975 declared. An out-of-class definition can specify additional default
4976 arguments. As it is the clones that are involved in overload
4977 resolution, we must propagate the information from the DECL to its
4981 adjust_clone_args (tree decl
)
4985 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4986 clone
= DECL_CHAIN (clone
))
4988 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4989 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4990 tree decl_parms
, clone_parms
;
4992 /* Skip the 'this' parameter. */
4993 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4994 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4996 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4997 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4998 if (DECL_HAS_VTT_PARM_P (decl
))
4999 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
5001 clone_parms
= orig_clone_parms
;
5002 if (DECL_HAS_VTT_PARM_P (clone
))
5003 clone_parms
= TREE_CHAIN (clone_parms
);
5005 for (decl_parms
= orig_decl_parms
; decl_parms
;
5006 decl_parms
= TREE_CHAIN (decl_parms
),
5007 clone_parms
= TREE_CHAIN (clone_parms
))
5009 if (clone_parms
== void_list_node
)
5011 gcc_assert (decl_parms
== clone_parms
5012 || ctor_omit_inherited_parms (clone
));
5016 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms
),
5017 TREE_VALUE (clone_parms
)));
5019 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
5021 /* A default parameter has been added. Adjust the
5022 clone's parameters. */
5023 clone_parms
= orig_decl_parms
;
5025 if (DECL_HAS_VTT_PARM_P (clone
))
5027 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
5028 TREE_VALUE (orig_clone_parms
),
5030 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
5033 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
5035 = build_method_type_directly (basetype
,
5036 TREE_TYPE (TREE_TYPE (clone
)),
5038 if (tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
)))
5039 type
= cp_build_type_attribute_variant (type
, attrs
);
5040 type
= cxx_copy_lang_qualifiers (type
, TREE_TYPE (clone
));
5041 TREE_TYPE (clone
) = type
;
5043 clone_parms
= NULL_TREE
;
5047 gcc_assert (!clone_parms
|| clone_parms
== void_list_node
);
5051 /* For each of the constructors and destructors in T, create an
5052 in-charge and not-in-charge variant. */
5055 clone_constructors_and_destructors (tree t
)
5057 /* While constructors can be via a using declaration, at this point
5058 we no longer need to know that. */
5059 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5060 clone_cdtor (*iter
, /*update_methods=*/true);
5062 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5063 clone_cdtor (dtor
, /*update_methods=*/true);
5066 /* Deduce noexcept for a destructor DTOR. */
5069 deduce_noexcept_on_destructor (tree dtor
)
5071 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
5072 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
),
5073 noexcept_deferred_spec
);
5076 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5077 of TYPE for virtual functions which FNDECL overrides. Return a
5078 mask of the tm attributes found therein. */
5081 look_for_tm_attr_overrides (tree type
, tree fndecl
)
5083 tree binfo
= TYPE_BINFO (type
);
5087 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
5089 tree o
, basetype
= BINFO_TYPE (base_binfo
);
5091 if (!TYPE_POLYMORPHIC_P (basetype
))
5094 o
= look_for_overrides_here (basetype
, fndecl
);
5097 if (lookup_attribute ("transaction_safe_dynamic",
5098 DECL_ATTRIBUTES (o
)))
5099 /* transaction_safe_dynamic is not inherited. */;
5101 found
|= tm_attr_to_mask (find_tm_attribute
5102 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
5105 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
5111 /* Subroutine of set_method_tm_attributes. Handle the checks and
5112 inheritance for one virtual method FNDECL. */
5115 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
5120 found
= look_for_tm_attr_overrides (type
, fndecl
);
5122 /* If FNDECL doesn't actually override anything (i.e. T is the
5123 class that first declares FNDECL virtual), then we're done. */
5127 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
5128 have
= tm_attr_to_mask (tm_attr
);
5130 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5131 tm_pure must match exactly, otherwise no weakening of
5132 tm_safe > tm_callable > nothing. */
5133 /* ??? The tm_pure attribute didn't make the transition to the
5134 multivendor language spec. */
5135 if (have
== TM_ATTR_PURE
)
5137 if (found
!= TM_ATTR_PURE
)
5143 /* If the overridden function is tm_pure, then FNDECL must be. */
5144 else if (found
== TM_ATTR_PURE
&& tm_attr
)
5146 /* Look for base class combinations that cannot be satisfied. */
5147 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
5149 found
&= ~TM_ATTR_PURE
;
5151 error_at (DECL_SOURCE_LOCATION (fndecl
),
5152 "method overrides both %<transaction_pure%> and %qE methods",
5153 tm_mask_to_attr (found
));
5155 /* If FNDECL did not declare an attribute, then inherit the most
5157 else if (tm_attr
== NULL
)
5159 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
5161 /* Otherwise validate that we're not weaker than a function
5162 that is being overridden. */
5166 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
5172 error_at (DECL_SOURCE_LOCATION (fndecl
),
5173 "method declared %qE overriding %qE method",
5174 tm_attr
, tm_mask_to_attr (found
));
5177 /* For each of the methods in T, propagate a class-level tm attribute. */
5180 set_method_tm_attributes (tree t
)
5182 tree class_tm_attr
, fndecl
;
5184 /* Don't bother collecting tm attributes if transactional memory
5185 support is not enabled. */
5189 /* Process virtual methods first, as they inherit directly from the
5190 base virtual function and also require validation of new attributes. */
5191 if (TYPE_CONTAINS_VPTR_P (t
))
5194 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
5195 vchain
= TREE_CHAIN (vchain
))
5197 fndecl
= BV_FN (vchain
);
5198 if (DECL_THUNK_P (fndecl
))
5199 fndecl
= THUNK_TARGET (fndecl
);
5200 set_one_vmethod_tm_attributes (t
, fndecl
);
5204 /* If the class doesn't have an attribute, nothing more to do. */
5205 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
5206 if (class_tm_attr
== NULL
)
5209 /* Any method that does not yet have a tm attribute inherits
5210 the one from the class. */
5211 for (fndecl
= TYPE_FIELDS (t
); fndecl
; fndecl
= DECL_CHAIN (fndecl
))
5212 if (DECL_DECLARES_FUNCTION_P (fndecl
)
5213 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
5214 apply_tm_attr (fndecl
, class_tm_attr
);
5217 /* Returns true if FN is a default constructor. */
5220 default_ctor_p (const_tree fn
)
5222 return (DECL_CONSTRUCTOR_P (fn
)
5223 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
5226 /* Returns true iff class T has a user-provided constructor that can be called
5227 with more than zero arguments. */
5230 type_has_user_nondefault_constructor (tree t
)
5232 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5235 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5238 if (user_provided_p (fn
)
5239 && (TREE_CODE (fn
) == TEMPLATE_DECL
5240 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
5248 /* Returns the defaulted constructor if T has one. Otherwise, returns
5252 in_class_defaulted_default_constructor (tree t
)
5254 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5257 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5261 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
5262 && default_ctor_p (fn
))
5269 /* Returns true iff FN is a user-provided function, i.e. user-declared
5270 and not defaulted at its first declaration. */
5273 user_provided_p (tree fn
)
5275 fn
= STRIP_TEMPLATE (fn
);
5276 return (!DECL_ARTIFICIAL (fn
)
5277 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
5278 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
5281 /* Returns true iff class T has a user-provided constructor. */
5284 type_has_user_provided_constructor (tree t
)
5286 if (!CLASS_TYPE_P (t
))
5289 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5292 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5293 if (user_provided_p (*iter
))
5299 /* Returns true iff class T has a user-provided or explicit constructor. */
5302 type_has_user_provided_or_explicit_constructor (tree t
)
5304 if (!CLASS_TYPE_P (t
))
5307 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5310 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5313 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5320 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5321 declared or explicitly defaulted in the class body) default
5325 type_has_non_user_provided_default_constructor (tree t
)
5327 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5329 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5332 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5335 if (TREE_CODE (fn
) == FUNCTION_DECL
5336 && default_ctor_p (fn
)
5337 && !user_provided_p (fn
))
5344 /* TYPE is being used as a virtual base, and has a non-trivial move
5345 assignment. Return true if this is due to there being a user-provided
5346 move assignment in TYPE or one of its subobjects; if there isn't, then
5347 multiple move assignment can't cause any harm. */
5350 vbase_has_user_provided_move_assign (tree type
)
5352 /* Does the type itself have a user-provided move assignment operator? */
5353 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type
))
5354 for (ovl_iterator
iter (get_class_binding_direct
5355 (type
, assign_op_identifier
));
5357 if (user_provided_p (*iter
) && move_fn_p (*iter
))
5360 /* Do any of its bases? */
5361 tree binfo
= TYPE_BINFO (type
);
5363 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5364 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5367 /* Or non-static data members? */
5368 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5370 if (TREE_CODE (field
) == FIELD_DECL
5371 && CLASS_TYPE_P (TREE_TYPE (field
))
5372 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5380 /* If default-initialization leaves part of TYPE uninitialized, returns
5381 a DECL for the field or TYPE itself (DR 253). */
5384 default_init_uninitialized_part (tree type
)
5389 type
= strip_array_types (type
);
5390 if (!CLASS_TYPE_P (type
))
5392 if (!type_has_non_user_provided_default_constructor (type
))
5394 for (binfo
= TYPE_BINFO (type
), i
= 0;
5395 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5397 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5401 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5402 if (TREE_CODE (t
) == FIELD_DECL
5403 && !DECL_ARTIFICIAL (t
)
5404 && !DECL_INITIAL (t
))
5406 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5408 return DECL_P (r
) ? r
: t
;
5414 /* Returns true iff for class T, a trivial synthesized default constructor
5415 would be constexpr. */
5418 trivial_default_constructor_is_constexpr (tree t
)
5420 /* A defaulted trivial default constructor is constexpr
5421 if there is nothing to initialize. */
5422 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5423 /* A class with a vptr doesn't have a trivial default ctor.
5424 In C++20, a class can have transient uninitialized members, e.g.:
5426 struct S { int i; constexpr S() = default; };
5429 return (cxx_dialect
>= cxx20
5430 || is_really_empty_class (t
, /*ignore_vptr*/true));
5433 /* Returns true iff class T has a constexpr default constructor. */
5436 type_has_constexpr_default_constructor (tree t
)
5440 if (!CLASS_TYPE_P (t
))
5442 /* The caller should have stripped an enclosing array. */
5443 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5446 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5448 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5449 return trivial_default_constructor_is_constexpr (t
);
5450 /* Non-trivial, we need to check subobject constructors. */
5451 lazily_declare_fn (sfk_constructor
, t
);
5453 fns
= locate_ctor (t
);
5454 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5457 /* Returns true iff class T has a constexpr default constructor or has an
5458 implicitly declared default constructor that we can't tell if it's constexpr
5459 without forcing a lazy declaration (which might cause undesired
5463 type_maybe_constexpr_default_constructor (tree t
)
5465 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5466 && TYPE_HAS_COMPLEX_DFLT (t
))
5467 /* Assume it's constexpr. */
5469 return type_has_constexpr_default_constructor (t
);
5472 /* Returns true iff class T has a constexpr destructor. */
5475 type_has_constexpr_destructor (tree t
)
5479 if (CLASSTYPE_LAZY_DESTRUCTOR (t
))
5480 /* Non-trivial, we need to check subobject destructors. */
5481 lazily_declare_fn (sfk_destructor
, t
);
5482 fns
= CLASSTYPE_DESTRUCTOR (t
);
5483 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5486 /* Returns true iff class T has a constexpr destructor or has an
5487 implicitly declared destructor that we can't tell if it's constexpr
5488 without forcing a lazy declaration (which might cause undesired
5492 type_maybe_constexpr_destructor (tree t
)
5494 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DESTRUCTOR (t
))
5495 /* Assume it's constexpr. */
5497 return type_has_constexpr_destructor (t
);
5500 /* Returns true iff class TYPE has a virtual destructor. */
5503 type_has_virtual_destructor (tree type
)
5507 if (!CLASS_TYPE_P (type
))
5510 gcc_assert (COMPLETE_TYPE_P (type
));
5511 dtor
= CLASSTYPE_DESTRUCTOR (type
);
5512 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5515 /* Returns true iff T, a class, has a move-assignment or
5516 move-constructor. Does not lazily declare either.
5517 If USER_P is false, any move function will do. If it is true, the
5518 move function must be user-declared.
5520 Note that user-declared here is different from "user-provided",
5521 which doesn't include functions that are defaulted in the
5525 classtype_has_move_assign_or_move_ctor_p (tree t
, bool user_p
)
5528 || (!CLASSTYPE_LAZY_MOVE_CTOR (t
)
5529 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t
)));
5531 if (!CLASSTYPE_LAZY_MOVE_CTOR (t
))
5532 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5533 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5536 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5537 for (ovl_iterator
iter (get_class_binding_direct
5538 (t
, assign_op_identifier
));
5540 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
))
5541 && DECL_CONTEXT (*iter
) == t
5542 && move_fn_p (*iter
))
5548 /* True iff T has a move constructor that is not deleted. */
5551 classtype_has_non_deleted_move_ctor (tree t
)
5553 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5554 lazily_declare_fn (sfk_move_constructor
, t
);
5555 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5556 if (move_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5561 /* True iff T has a copy constructor that is not deleted. */
5564 classtype_has_non_deleted_copy_ctor (tree t
)
5566 if (CLASSTYPE_LAZY_COPY_CTOR (t
))
5567 lazily_declare_fn (sfk_copy_constructor
, t
);
5568 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5569 if (copy_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5574 /* If T, a class, has a user-provided copy constructor, copy assignment
5575 operator, or destructor, returns that function. Otherwise, null. */
5578 classtype_has_depr_implicit_copy (tree t
)
5580 if (!CLASSTYPE_LAZY_COPY_CTOR (t
))
5581 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5584 if (user_provided_p (fn
) && copy_fn_p (fn
))
5588 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t
))
5589 for (ovl_iterator
iter (get_class_binding_direct
5590 (t
, assign_op_identifier
));
5594 if (user_provided_p (fn
) && copy_fn_p (fn
))
5598 if (!CLASSTYPE_LAZY_DESTRUCTOR (t
))
5600 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5601 if (user_provided_p (fn
))
5608 /* True iff T has a member or friend declaration of operator OP. */
5611 classtype_has_op (tree t
, tree_code op
)
5613 tree name
= ovl_op_identifier (op
);
5614 if (get_class_binding (t
, name
))
5616 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5617 if (FRIEND_NAME (f
) == name
)
5623 /* If T has a defaulted member or friend declaration of OP, return it. */
5626 classtype_has_defaulted_op (tree t
, tree_code op
)
5628 tree name
= ovl_op_identifier (op
);
5629 for (ovl_iterator
oi (get_class_binding (t
, name
)); oi
; ++oi
)
5632 if (DECL_DEFAULTED_FN (fn
))
5635 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5636 if (FRIEND_NAME (f
) == name
)
5637 for (tree l
= FRIEND_DECLS (f
); l
; l
= TREE_CHAIN (l
))
5639 tree fn
= TREE_VALUE (l
);
5640 if (DECL_DEFAULTED_FN (fn
))
5646 /* Nonzero if we need to build up a constructor call when initializing an
5647 object of this class, either because it has a user-declared constructor
5648 or because it doesn't have a default constructor (so we need to give an
5649 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5650 what you care about is whether or not an object can be produced by a
5651 constructor (e.g. so we don't set TREE_READONLY on const variables of
5652 such type); use this function when what you care about is whether or not
5653 to try to call a constructor to create an object. The latter case is
5654 the former plus some cases of constructors that cannot be called. */
5657 type_build_ctor_call (tree t
)
5660 if (TYPE_NEEDS_CONSTRUCTING (t
))
5662 inner
= strip_array_types (t
);
5663 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5665 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5667 if (cxx_dialect
< cxx11
)
5669 /* A user-declared constructor might be private, and a constructor might
5670 be trivial but deleted. */
5671 for (ovl_iterator
iter (get_class_binding (inner
, complete_ctor_identifier
));
5675 if (!DECL_ARTIFICIAL (fn
)
5676 || TREE_DEPRECATED (fn
)
5677 || DECL_DELETED_FN (fn
))
5683 /* Like type_build_ctor_call, but for destructors. */
5686 type_build_dtor_call (tree t
)
5689 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5691 inner
= strip_array_types (t
);
5692 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5693 || !COMPLETE_TYPE_P (inner
))
5695 if (cxx_dialect
< cxx11
)
5697 /* A user-declared destructor might be private, and a destructor might
5698 be trivial but deleted. */
5699 for (ovl_iterator
iter (get_class_binding (inner
, complete_dtor_identifier
));
5703 if (!DECL_ARTIFICIAL (fn
)
5704 || TREE_DEPRECATED (fn
)
5705 || DECL_DELETED_FN (fn
))
5711 /* Remove all zero-width bit-fields from T. */
5714 remove_zero_width_bit_fields (tree t
)
5718 fieldsp
= &TYPE_FIELDS (t
);
5721 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5722 && DECL_C_BIT_FIELD (*fieldsp
)
5723 /* We should not be confused by the fact that grokbitfield
5724 temporarily sets the width of the bit field into
5725 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5726 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5728 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5729 || integer_zerop (DECL_SIZE (*fieldsp
))))
5730 *fieldsp
= DECL_CHAIN (*fieldsp
);
5732 fieldsp
= &DECL_CHAIN (*fieldsp
);
5736 /* Returns TRUE iff we need a cookie when dynamically allocating an
5737 array whose elements have the indicated class TYPE. */
5740 type_requires_array_cookie (tree type
)
5743 bool has_two_argument_delete_p
= false;
5745 gcc_assert (CLASS_TYPE_P (type
));
5747 /* If there's a non-trivial destructor, we need a cookie. In order
5748 to iterate through the array calling the destructor for each
5749 element, we'll have to know how many elements there are. */
5750 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5753 /* If the usual deallocation function is a two-argument whose second
5754 argument is of type `size_t', then we have to pass the size of
5755 the array to the deallocation function, so we will need to store
5757 fns
= lookup_fnfields (TYPE_BINFO (type
),
5758 ovl_op_identifier (false, VEC_DELETE_EXPR
),
5759 /*protect=*/0, tf_warning_or_error
);
5760 /* If there are no `operator []' members, or the lookup is
5761 ambiguous, then we don't need a cookie. */
5762 if (!fns
|| fns
== error_mark_node
)
5764 /* Loop through all of the functions. */
5765 for (lkp_iterator
iter (BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
5769 /* See if this function is a one-argument delete function. If
5770 it is, then it will be the usual deallocation function. */
5771 tree second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5772 if (second_parm
== void_list_node
)
5774 /* Do not consider this function if its second argument is an
5778 /* Otherwise, if we have a two-argument function and the second
5779 argument is `size_t', it will be the usual deallocation
5780 function -- unless there is one-argument function, too. */
5781 if (TREE_CHAIN (second_parm
) == void_list_node
5782 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5783 has_two_argument_delete_p
= true;
5786 return has_two_argument_delete_p
;
5789 /* Finish computing the `literal type' property of class type T.
5791 At this point, we have already processed base classes and
5792 non-static data members. We need to check whether the copy
5793 constructor is trivial, the destructor is trivial, and there
5794 is a trivial default constructor or at least one constexpr
5795 constructor other than the copy constructor. */
5798 finalize_literal_type_property (tree t
)
5802 if (cxx_dialect
< cxx11
)
5803 CLASSTYPE_LITERAL_P (t
) = false;
5804 else if (CLASSTYPE_LITERAL_P (t
)
5805 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
5806 && (cxx_dialect
< cxx20
|| !type_maybe_constexpr_destructor (t
)))
5807 CLASSTYPE_LITERAL_P (t
) = false;
5808 else if (CLASSTYPE_LITERAL_P (t
) && LAMBDA_TYPE_P (t
))
5809 CLASSTYPE_LITERAL_P (t
) = (cxx_dialect
>= cxx17
);
5810 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5811 && CLASSTYPE_NON_AGGREGATE (t
)
5812 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5813 CLASSTYPE_LITERAL_P (t
) = false;
5815 /* C++14 DR 1684 removed this restriction. */
5816 if (cxx_dialect
< cxx14
5817 && !CLASSTYPE_LITERAL_P (t
) && !LAMBDA_TYPE_P (t
))
5818 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5819 if (TREE_CODE (fn
) == FUNCTION_DECL
5820 && DECL_DECLARED_CONSTEXPR_P (fn
)
5821 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5822 && !DECL_CONSTRUCTOR_P (fn
))
5824 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5825 if (!DECL_GENERATED_P (fn
))
5827 auto_diagnostic_group d
;
5828 if (pedwarn (DECL_SOURCE_LOCATION (fn
), OPT_Wpedantic
,
5829 "enclosing class of %<constexpr%> non-static "
5830 "member function %q+#D is not a literal type", fn
))
5831 explain_non_literal_class (t
);
5836 /* T is a non-literal type used in a context which requires a constant
5837 expression. Explain why it isn't literal. */
5840 explain_non_literal_class (tree t
)
5842 static hash_set
<tree
> *diagnosed
;
5844 if (!CLASS_TYPE_P (t
))
5846 t
= TYPE_MAIN_VARIANT (t
);
5848 if (diagnosed
== NULL
)
5849 diagnosed
= new hash_set
<tree
>;
5850 if (diagnosed
->add (t
))
5851 /* Already explained. */
5854 auto_diagnostic_group d
;
5855 inform (UNKNOWN_LOCATION
, "%q+T is not literal because:", t
);
5856 if (cxx_dialect
< cxx17
&& LAMBDA_TYPE_P (t
))
5857 inform (UNKNOWN_LOCATION
,
5858 " %qT is a closure type, which is only literal in "
5859 "C++17 and later", t
);
5860 else if (cxx_dialect
< cxx20
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5861 inform (UNKNOWN_LOCATION
, " %q+T has a non-trivial destructor", t
);
5862 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
5863 && !type_maybe_constexpr_destructor (t
))
5864 inform (UNKNOWN_LOCATION
, " %q+T does not have %<constexpr%> destructor",
5866 else if (CLASSTYPE_NON_AGGREGATE (t
)
5867 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5868 && !LAMBDA_TYPE_P (t
)
5869 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5871 inform (UNKNOWN_LOCATION
,
5872 " %q+T is not an aggregate, does not have a trivial "
5873 "default constructor, and has no %<constexpr%> constructor that "
5874 "is not a copy or move constructor", t
);
5875 if (type_has_non_user_provided_default_constructor (t
))
5876 /* Note that we can't simply call locate_ctor because when the
5877 constructor is deleted it just returns NULL_TREE. */
5878 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5881 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5883 parms
= skip_artificial_parms_for (fn
, parms
);
5885 if (sufficient_parms_p (parms
))
5887 if (DECL_DELETED_FN (fn
))
5888 maybe_explain_implicit_delete (fn
);
5890 explain_invalid_constexpr_fn (fn
);
5897 tree binfo
, base_binfo
, field
; int i
;
5898 for (binfo
= TYPE_BINFO (t
), i
= 0;
5899 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5901 tree basetype
= TREE_TYPE (base_binfo
);
5902 if (!CLASSTYPE_LITERAL_P (basetype
))
5904 inform (UNKNOWN_LOCATION
,
5905 " base class %qT of %q+T is non-literal",
5907 explain_non_literal_class (basetype
);
5911 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5914 if (TREE_CODE (field
) != FIELD_DECL
)
5916 ftype
= TREE_TYPE (field
);
5917 if (!literal_type_p (ftype
))
5919 inform (DECL_SOURCE_LOCATION (field
),
5920 " non-static data member %qD has non-literal type",
5922 if (CLASS_TYPE_P (ftype
))
5923 explain_non_literal_class (ftype
);
5925 if (CP_TYPE_VOLATILE_P (ftype
))
5926 inform (DECL_SOURCE_LOCATION (field
),
5927 " non-static data member %qD has volatile type", field
);
5932 /* Check the validity of the bases and members declared in T. Add any
5933 implicitly-generated functions (like copy-constructors and
5934 assignment operators). Compute various flag bits (like
5935 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5936 level: i.e., independently of the ABI in use. */
5939 check_bases_and_members (tree t
)
5941 /* Nonzero if the implicitly generated copy constructor should take
5942 a non-const reference argument. */
5943 int cant_have_const_ctor
;
5944 /* Nonzero if the implicitly generated assignment operator
5945 should take a non-const reference argument. */
5946 int no_const_asn_ref
;
5948 bool saved_complex_asn_ref
;
5949 bool saved_nontrivial_dtor
;
5952 /* By default, we use const reference arguments and generate default
5954 cant_have_const_ctor
= 0;
5955 no_const_asn_ref
= 0;
5957 /* Check all the base-classes and set FMEM members to point to arrays
5958 of potential interest. */
5959 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
5961 /* Deduce noexcept on destructor. This needs to happen after we've set
5962 triviality flags appropriately for our bases. */
5963 if (cxx_dialect
>= cxx11
)
5964 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5965 deduce_noexcept_on_destructor (dtor
);
5967 /* Check all the method declarations. */
5970 /* Save the initial values of these flags which only indicate whether
5971 or not the class has user-provided functions. As we analyze the
5972 bases and members we can set these flags for other reasons. */
5973 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5974 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5976 /* Check all the data member declarations. We cannot call
5977 check_field_decls until we have called check_bases check_methods,
5978 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5979 being set appropriately. */
5980 check_field_decls (t
, &access_decls
,
5981 &cant_have_const_ctor
,
5984 /* A nearly-empty class has to be vptr-containing; a nearly empty
5985 class contains just a vptr. */
5986 if (!TYPE_CONTAINS_VPTR_P (t
))
5987 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5989 /* Do some bookkeeping that will guide the generation of implicitly
5990 declared member functions. */
5991 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5992 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5993 /* We need to call a constructor for this class if it has a
5994 user-provided constructor, or if the default constructor is going
5995 to initialize the vptr. (This is not an if-and-only-if;
5996 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5997 themselves need constructing.) */
5998 TYPE_NEEDS_CONSTRUCTING (t
)
5999 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
6002 An aggregate is an array or a class with no user-provided
6003 constructors ... and no virtual functions.
6005 Again, other conditions for being an aggregate are checked
6007 CLASSTYPE_NON_AGGREGATE (t
)
6008 |= ((cxx_dialect
< cxx20
6009 ? type_has_user_provided_or_explicit_constructor (t
)
6010 : TYPE_HAS_USER_CONSTRUCTOR (t
))
6011 || TYPE_POLYMORPHIC_P (t
));
6012 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6013 retain the old definition internally for ABI reasons. */
6014 CLASSTYPE_NON_LAYOUT_POD_P (t
)
6015 |= (CLASSTYPE_NON_AGGREGATE (t
)
6016 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
6017 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6018 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6019 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6020 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6022 /* If the only explicitly declared default constructor is user-provided,
6023 set TYPE_HAS_COMPLEX_DFLT. */
6024 if (!TYPE_HAS_COMPLEX_DFLT (t
)
6025 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
6026 && !type_has_non_user_provided_default_constructor (t
))
6027 TYPE_HAS_COMPLEX_DFLT (t
) = true;
6029 /* Warn if a public base of a polymorphic type has an accessible
6030 non-virtual destructor. It is only now that we know the class is
6031 polymorphic. Although a polymorphic base will have a already
6032 been diagnosed during its definition, we warn on use too. */
6033 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
6035 tree binfo
= TYPE_BINFO (t
);
6036 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
6040 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
6042 tree basetype
= TREE_TYPE (base_binfo
);
6044 if ((*accesses
)[i
] == access_public_node
6045 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
6046 && accessible_nvdtor_p (basetype
))
6047 warning (OPT_Wnon_virtual_dtor
,
6048 "base class %q#T has accessible non-virtual destructor",
6053 /* If the class has no user-declared constructor, but does have
6054 non-static const or reference data members that can never be
6055 initialized, issue a warning. */
6056 if (warn_uninitialized
6057 /* Classes with user-declared constructors are presumed to
6058 initialize these members. */
6059 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
6060 /* Aggregates can be initialized with brace-enclosed
6062 && CLASSTYPE_NON_AGGREGATE (t
))
6066 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6070 if (TREE_CODE (field
) != FIELD_DECL
6071 || DECL_INITIAL (field
) != NULL_TREE
)
6074 type
= TREE_TYPE (field
);
6075 if (TYPE_REF_P (type
))
6076 warning_at (DECL_SOURCE_LOCATION (field
),
6077 OPT_Wuninitialized
, "non-static reference %q#D "
6078 "in class without a constructor", field
);
6079 else if (CP_TYPE_CONST_P (type
)
6080 && (!CLASS_TYPE_P (type
)
6081 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
6082 warning_at (DECL_SOURCE_LOCATION (field
),
6083 OPT_Wuninitialized
, "non-static const member %q#D "
6084 "in class without a constructor", field
);
6088 /* Synthesize any needed methods. */
6089 add_implicitly_declared_members (t
, &access_decls
,
6090 cant_have_const_ctor
,
6093 /* Check defaulted declarations here so we have cant_have_const_ctor
6094 and don't need to worry about clones. */
6095 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6096 if (DECL_DECLARES_FUNCTION_P (fn
)
6097 && !DECL_ARTIFICIAL (fn
)
6098 && DECL_DEFAULTED_IN_CLASS_P (fn
))
6100 int copy
= copy_fn_p (fn
);
6104 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
6105 : !no_const_asn_ref
);
6106 bool fn_const_p
= (copy
== 2);
6108 if (fn_const_p
&& !imp_const_p
)
6109 /* If the function is defaulted outside the class, we just
6110 give the synthesis error. Core Issue #1331 says this is
6111 no longer ill-formed, it is defined as deleted instead. */
6112 DECL_DELETED_FN (fn
) = true;
6114 defaulted_late_check (fn
);
6117 if (LAMBDA_TYPE_P (t
))
6118 /* "This class type is not an aggregate." */
6119 CLASSTYPE_NON_AGGREGATE (t
) = 1;
6121 /* Compute the 'literal type' property before we
6122 do anything with non-static member functions. */
6123 finalize_literal_type_property (t
);
6125 /* Create the in-charge and not-in-charge variants of constructors
6127 clone_constructors_and_destructors (t
);
6129 /* Process the using-declarations. */
6130 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
6131 handle_using_decl (TREE_VALUE (access_decls
), t
);
6133 /* Figure out whether or not we will need a cookie when dynamically
6134 allocating an array of this type. */
6135 LANG_TYPE_CLASS_CHECK (t
)->vec_new_uses_cookie
6136 = type_requires_array_cookie (t
);
6139 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6140 accordingly. If a new vfield was created (because T doesn't have a
6141 primary base class), then the newly created field is returned. It
6142 is not added to the TYPE_FIELDS list; it is the caller's
6143 responsibility to do that. Accumulate declared virtual functions
6147 create_vtable_ptr (tree t
, tree
* virtuals_p
)
6151 /* Collect the virtual functions declared in T. */
6152 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6153 if (TREE_CODE (fn
) == FUNCTION_DECL
6154 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
6155 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
6157 tree new_virtual
= make_node (TREE_LIST
);
6159 BV_FN (new_virtual
) = fn
;
6160 BV_DELTA (new_virtual
) = integer_zero_node
;
6161 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
6163 TREE_CHAIN (new_virtual
) = *virtuals_p
;
6164 *virtuals_p
= new_virtual
;
6167 /* If we couldn't find an appropriate base class, create a new field
6168 here. Even if there weren't any new virtual functions, we might need a
6169 new virtual function table if we're supposed to include vptrs in
6170 all classes that need them. */
6171 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
6173 /* We build this decl with vtbl_ptr_type_node, which is a
6174 `vtable_entry_type*'. It might seem more precise to use
6175 `vtable_entry_type (*)[N]' where N is the number of virtual
6176 functions. However, that would require the vtable pointer in
6177 base classes to have a different type than the vtable pointer
6178 in derived classes. We could make that happen, but that
6179 still wouldn't solve all the problems. In particular, the
6180 type-based alias analysis code would decide that assignments
6181 to the base class vtable pointer can't alias assignments to
6182 the derived class vtable pointer, since they have different
6183 types. Thus, in a derived class destructor, where the base
6184 class constructor was inlined, we could generate bad code for
6185 setting up the vtable pointer.
6187 Therefore, we use one type for all vtable pointers. We still
6188 use a type-correct type; it's just doesn't indicate the array
6189 bounds. That's better than using `void*' or some such; it's
6190 cleaner, and it let's the alias analysis code know that these
6191 stores cannot alias stores to void*! */
6194 field
= build_decl (input_location
,
6195 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
6196 DECL_VIRTUAL_P (field
) = 1;
6197 DECL_ARTIFICIAL (field
) = 1;
6198 DECL_FIELD_CONTEXT (field
) = t
;
6199 DECL_FCONTEXT (field
) = t
;
6200 if (TYPE_PACKED (t
))
6201 DECL_PACKED (field
) = 1;
6203 TYPE_VFIELD (t
) = field
;
6205 /* This class is non-empty. */
6206 CLASSTYPE_EMPTY_P (t
) = 0;
6214 /* Add OFFSET to all base types of BINFO which is a base in the
6215 hierarchy dominated by T.
6217 OFFSET, which is a type offset, is number of bytes. */
6220 propagate_binfo_offsets (tree binfo
, tree offset
)
6226 /* Update BINFO's offset. */
6227 BINFO_OFFSET (binfo
)
6228 = fold_convert (sizetype
,
6229 size_binop (PLUS_EXPR
,
6230 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
6233 /* Find the primary base class. */
6234 primary_binfo
= get_primary_binfo (binfo
);
6236 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
6237 propagate_binfo_offsets (primary_binfo
, offset
);
6239 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6241 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6243 /* Don't do the primary base twice. */
6244 if (base_binfo
== primary_binfo
)
6247 if (BINFO_VIRTUAL_P (base_binfo
))
6250 propagate_binfo_offsets (base_binfo
, offset
);
6254 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6255 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6256 empty subobjects of T. */
6259 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
6265 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
6268 /* Find the last field. The artificial fields created for virtual
6269 bases will go after the last extant field to date. */
6270 next_field
= &TYPE_FIELDS (t
);
6272 next_field
= &DECL_CHAIN (*next_field
);
6274 /* Go through the virtual bases, allocating space for each virtual
6275 base that is not already a primary base class. These are
6276 allocated in inheritance graph order. */
6277 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6279 if (!BINFO_VIRTUAL_P (vbase
))
6282 if (!BINFO_PRIMARY_P (vbase
))
6284 /* This virtual base is not a primary base of any class in the
6285 hierarchy, so we have to add space for it. */
6286 next_field
= build_base_field (rli
, vbase
,
6287 access_private_node
,
6288 offsets
, next_field
);
6293 /* Returns the offset of the byte just past the end of the base class
6297 end_of_base (tree binfo
)
6301 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
6302 size
= TYPE_SIZE_UNIT (char_type_node
);
6303 else if (is_empty_class (BINFO_TYPE (binfo
)))
6304 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6305 allocate some space for it. It cannot have virtual bases, so
6306 TYPE_SIZE_UNIT is fine. */
6307 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6309 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6311 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
6314 /* Returns the offset of the byte just past the end of the base class or empty
6315 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
6316 then only non-virtual bases are included. */
6319 end_of_class (tree t
, bool include_virtuals_p
)
6321 tree result
= size_zero_node
;
6322 vec
<tree
, va_gc
> *vbases
;
6328 for (binfo
= TYPE_BINFO (t
), i
= 0;
6329 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6331 if (!include_virtuals_p
6332 && BINFO_VIRTUAL_P (base_binfo
)
6333 && (!BINFO_PRIMARY_P (base_binfo
)
6334 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
6337 offset
= end_of_base (base_binfo
);
6338 if (tree_int_cst_lt (result
, offset
))
6342 /* Also consider empty data members. */
6343 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6344 if (TREE_CODE (field
) == FIELD_DECL
6345 && !DECL_ARTIFICIAL (field
)
6346 && field_poverlapping_p (field
)
6347 && is_empty_class (TREE_TYPE (field
)))
6349 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
6350 offset
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (field
),
6351 TYPE_SIZE_UNIT (TREE_TYPE (field
)));
6352 if (tree_int_cst_lt (result
, offset
))
6356 if (include_virtuals_p
)
6357 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6358 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6360 offset
= end_of_base (base_binfo
);
6361 if (tree_int_cst_lt (result
, offset
))
6368 /* Warn about bases of T that are inaccessible because they are
6369 ambiguous. For example:
6372 struct T : public S {};
6373 struct U : public S, public T {};
6375 Here, `(S*) new U' is not allowed because there are two `S'
6379 maybe_warn_about_inaccessible_bases (tree t
)
6382 vec
<tree
, va_gc
> *vbases
;
6387 /* If not checking for warning then return early. */
6388 if (!warn_inaccessible_base
)
6391 /* If there are no repeated bases, nothing can be ambiguous. */
6392 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6395 /* Check direct bases. */
6396 for (binfo
= TYPE_BINFO (t
), i
= 0;
6397 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6399 basetype
= BINFO_TYPE (base_binfo
);
6401 if (!uniquely_derived_from_p (basetype
, t
))
6402 warning (OPT_Winaccessible_base
, "direct base %qT inaccessible "
6403 "in %qT due to ambiguity", basetype
, t
);
6406 /* Check for ambiguous virtual bases. */
6408 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6409 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6411 basetype
= BINFO_TYPE (binfo
);
6413 if (!uniquely_derived_from_p (basetype
, t
))
6414 warning (OPT_Winaccessible_base
, "virtual base %qT inaccessible in "
6415 "%qT due to ambiguity", basetype
, t
);
6419 /* Compare two INTEGER_CSTs K1 and K2. */
6422 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6424 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6427 /* Increase the size indicated in RLI to account for empty classes
6428 that are "off the end" of the class. */
6431 include_empty_classes (record_layout_info rli
)
6436 /* It might be the case that we grew the class to allocate a
6437 zero-sized base class. That won't be reflected in RLI, yet,
6438 because we are willing to overlay multiple bases at the same
6439 offset. However, now we need to make sure that RLI is big enough
6440 to reflect the entire class. */
6441 eoc
= end_of_class (rli
->t
, CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6442 rli_size
= rli_size_unit_so_far (rli
);
6443 if (TREE_CODE (rli_size
) == INTEGER_CST
6444 && tree_int_cst_lt (rli_size
, eoc
))
6446 /* The size should have been rounded to a whole byte. */
6447 gcc_assert (tree_int_cst_equal
6448 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6450 = size_binop (PLUS_EXPR
,
6452 size_binop (MULT_EXPR
,
6453 fold_convert (bitsizetype
,
6454 size_binop (MINUS_EXPR
,
6456 bitsize_int (BITS_PER_UNIT
)));
6457 normalize_rli (rli
);
6461 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6462 BINFO_OFFSETs for all of the base-classes. Position the vtable
6463 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6466 layout_class_type (tree t
, tree
*virtuals_p
)
6468 tree non_static_data_members
;
6471 record_layout_info rli
;
6472 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6473 types that appear at that offset. */
6474 splay_tree empty_base_offsets
;
6475 /* True if the last field laid out was a bit-field. */
6476 bool last_field_was_bitfield
= false;
6477 /* The location at which the next field should be inserted. */
6480 /* Keep track of the first non-static data member. */
6481 non_static_data_members
= TYPE_FIELDS (t
);
6483 /* Start laying out the record. */
6484 rli
= start_record_layout (t
);
6486 /* Mark all the primary bases in the hierarchy. */
6487 determine_primary_bases (t
);
6489 /* Create a pointer to our virtual function table. */
6490 vptr
= create_vtable_ptr (t
, virtuals_p
);
6492 /* The vptr is always the first thing in the class. */
6495 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6496 TYPE_FIELDS (t
) = vptr
;
6497 next_field
= &DECL_CHAIN (vptr
);
6498 place_field (rli
, vptr
);
6501 next_field
= &TYPE_FIELDS (t
);
6503 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6504 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6506 build_base_fields (rli
, empty_base_offsets
, next_field
);
6508 /* Layout the non-static data members. */
6509 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6514 /* We still pass things that aren't non-static data members to
6515 the back end, in case it wants to do something with them. */
6516 if (TREE_CODE (field
) != FIELD_DECL
)
6518 place_field (rli
, field
);
6519 /* If the static data member has incomplete type, keep track
6520 of it so that it can be completed later. (The handling
6521 of pending statics in finish_record_layout is
6522 insufficient; consider:
6525 struct S2 { static S1 s1; };
6527 At this point, finish_record_layout will be called, but
6528 S1 is still incomplete.) */
6531 maybe_register_incomplete_var (field
);
6532 /* The visibility of static data members is determined
6533 at their point of declaration, not their point of
6535 determine_visibility (field
);
6540 type
= TREE_TYPE (field
);
6541 if (type
== error_mark_node
)
6544 padding
= NULL_TREE
;
6546 bool might_overlap
= field_poverlapping_p (field
);
6548 if (might_overlap
&& CLASS_TYPE_P (type
)
6549 && (CLASSTYPE_NON_LAYOUT_POD_P (type
) || CLASSTYPE_EMPTY_P (type
)))
6551 /* if D is a potentially-overlapping data member, update sizeof(C) to
6552 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6553 tree nvsize
= CLASSTYPE_SIZE_UNIT (type
);
6554 /* end_of_class doesn't always give dsize, but it does in the case of
6555 a class with virtual bases, which is when dsize > nvsize. */
6556 tree dsize
= end_of_class (type
, /*vbases*/true);
6557 if (tree_int_cst_le (dsize
, nvsize
))
6559 DECL_SIZE_UNIT (field
) = nvsize
;
6560 DECL_SIZE (field
) = CLASSTYPE_SIZE (type
);
6564 DECL_SIZE_UNIT (field
) = dsize
;
6565 DECL_SIZE (field
) = bit_from_pos (dsize
, bitsize_zero_node
);
6569 /* If this field is a bit-field whose width is greater than its
6570 type, then there are some special rules for allocating
6572 if (DECL_C_BIT_FIELD (field
)
6573 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6575 bool was_unnamed_p
= false;
6576 /* We must allocate the bits as if suitably aligned for the
6577 longest integer type that fits in this many bits. Then,
6578 we are supposed to use the left over bits as additional
6581 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6582 tree limit
= size_int (MAX_FIXED_MODE_SIZE
);
6583 if (tree_int_cst_lt (DECL_SIZE (field
), limit
))
6584 limit
= DECL_SIZE (field
);
6586 tree integer_type
= integer_types
[itk_char
];
6587 for (unsigned itk
= itk_char
; itk
!= itk_none
; itk
++)
6588 if (tree next
= integer_types
[itk
])
6590 if (tree_int_cst_lt (limit
, TYPE_SIZE (next
)))
6591 /* Too big, so our current guess is what we want. */
6593 /* Not bigger than limit, ok */
6594 integer_type
= next
;
6597 /* Figure out how much additional padding is required. */
6598 if (TREE_CODE (t
) == UNION_TYPE
)
6599 /* In a union, the padding field must have the full width
6600 of the bit-field; all fields start at offset zero. */
6601 padding
= DECL_SIZE (field
);
6603 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6604 TYPE_SIZE (integer_type
));
6606 if (integer_zerop (padding
))
6607 padding
= NULL_TREE
;
6609 /* An unnamed bitfield does not normally affect the
6610 alignment of the containing class on a target where
6611 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6612 make any exceptions for unnamed bitfields when the
6613 bitfields are longer than their types. Therefore, we
6614 temporarily give the field a name. */
6615 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6617 was_unnamed_p
= true;
6618 DECL_NAME (field
) = make_anon_name ();
6621 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6622 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6623 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6624 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6625 empty_base_offsets
);
6627 DECL_NAME (field
) = NULL_TREE
;
6628 /* Now that layout has been performed, set the size of the
6629 field to the size of its declared type; the rest of the
6630 field is effectively invisible. */
6631 DECL_SIZE (field
) = TYPE_SIZE (type
);
6632 /* We must also reset the DECL_MODE of the field. */
6633 SET_DECL_MODE (field
, TYPE_MODE (type
));
6635 else if (might_overlap
&& is_empty_class (type
))
6637 DECL_FIELD_ABI_IGNORED (field
) = 1;
6638 layout_empty_base_or_field (rli
, field
, empty_base_offsets
);
6641 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6642 empty_base_offsets
);
6644 /* Remember the location of any empty classes in FIELD. */
6645 record_subobject_offsets (field
, empty_base_offsets
);
6647 /* If a bit-field does not immediately follow another bit-field,
6648 and yet it starts in the middle of a byte, we have failed to
6649 comply with the ABI. */
6651 && DECL_C_BIT_FIELD (field
)
6652 /* The TREE_NO_WARNING flag gets set by Objective-C when
6653 laying out an Objective-C class. The ObjC ABI differs
6654 from the C++ ABI, and so we do not want a warning
6656 && !TREE_NO_WARNING (field
)
6657 && !last_field_was_bitfield
6658 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6659 DECL_FIELD_BIT_OFFSET (field
),
6660 bitsize_unit_node
)))
6661 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6662 "offset of %qD is not ABI-compliant and may "
6663 "change in a future version of GCC", field
);
6665 /* The middle end uses the type of expressions to determine the
6666 possible range of expression values. In order to optimize
6667 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6668 must be made aware of the width of "i", via its type.
6670 Because C++ does not have integer types of arbitrary width,
6671 we must (for the purposes of the front end) convert from the
6672 type assigned here to the declared type of the bitfield
6673 whenever a bitfield expression is used as an rvalue.
6674 Similarly, when assigning a value to a bitfield, the value
6675 must be converted to the type given the bitfield here. */
6676 if (DECL_C_BIT_FIELD (field
))
6678 unsigned HOST_WIDE_INT width
;
6679 tree ftype
= TREE_TYPE (field
);
6680 width
= tree_to_uhwi (DECL_SIZE (field
));
6681 if (width
!= TYPE_PRECISION (ftype
))
6684 = c_build_bitfield_integer_type (width
,
6685 TYPE_UNSIGNED (ftype
));
6687 = cp_build_qualified_type (TREE_TYPE (field
),
6688 cp_type_quals (ftype
));
6692 /* If we needed additional padding after this field, add it
6698 padding_field
= build_decl (input_location
,
6702 DECL_BIT_FIELD (padding_field
) = 1;
6703 DECL_SIZE (padding_field
) = padding
;
6704 DECL_CONTEXT (padding_field
) = t
;
6705 DECL_ARTIFICIAL (padding_field
) = 1;
6706 DECL_IGNORED_P (padding_field
) = 1;
6707 DECL_PADDING_P (padding_field
) = 1;
6708 layout_nonempty_base_or_field (rli
, padding_field
,
6710 empty_base_offsets
);
6713 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6716 if (!integer_zerop (rli
->bitpos
))
6718 /* Make sure that we are on a byte boundary so that the size of
6719 the class without virtual bases will always be a round number
6721 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6722 normalize_rli (rli
);
6725 /* Delete all zero-width bit-fields from the list of fields. Now
6726 that the type is laid out they are no longer important. */
6727 remove_zero_width_bit_fields (t
);
6729 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6731 /* T needs a different layout as a base (eliding virtual bases
6732 or whatever). Create that version. */
6733 tree base_t
= make_node (TREE_CODE (t
));
6734 tree base_d
= create_implicit_typedef (as_base_identifier
, base_t
);
6736 TYPE_CONTEXT (base_t
) = t
;
6737 DECL_CONTEXT (base_d
) = t
;
6739 /* If the ABI version is not at least two, and the last
6740 field was a bit-field, RLI may not be on a byte
6741 boundary. In particular, rli_size_unit_so_far might
6742 indicate the last complete byte, while rli_size_so_far
6743 indicates the total number of bits used. Therefore,
6744 rli_size_so_far, rather than rli_size_unit_so_far, is
6745 used to compute TYPE_SIZE_UNIT. */
6747 /* Set the size and alignment for the new type. */
6748 tree eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6749 TYPE_SIZE_UNIT (base_t
)
6750 = size_binop (MAX_EXPR
,
6751 fold_convert (sizetype
,
6752 size_binop (CEIL_DIV_EXPR
,
6753 rli_size_so_far (rli
),
6754 bitsize_int (BITS_PER_UNIT
))),
6757 = size_binop (MAX_EXPR
,
6758 rli_size_so_far (rli
),
6759 size_binop (MULT_EXPR
,
6760 fold_convert (bitsizetype
, eoc
),
6761 bitsize_int (BITS_PER_UNIT
)));
6762 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
6763 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6764 TYPE_TYPELESS_STORAGE (base_t
) = TYPE_TYPELESS_STORAGE (t
);
6765 TYPE_CXX_ODR_P (base_t
) = TYPE_CXX_ODR_P (t
);
6767 /* Copy the non-static data members of T. This will include its
6768 direct non-virtual bases & vtable. */
6769 next_field
= &TYPE_FIELDS (base_t
);
6770 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6771 if (TREE_CODE (field
) == FIELD_DECL
)
6773 *next_field
= copy_node (field
);
6774 /* Zap any NSDMI, it's not needed and might be a deferred
6776 DECL_INITIAL (*next_field
) = NULL_TREE
;
6777 DECL_CONTEXT (*next_field
) = base_t
;
6778 next_field
= &DECL_CHAIN (*next_field
);
6780 *next_field
= NULL_TREE
;
6782 /* We use the base type for trivial assignments, and hence it
6784 compute_record_mode (base_t
);
6786 /* Record the base version of the type. */
6787 CLASSTYPE_AS_BASE (t
) = base_t
;
6790 CLASSTYPE_AS_BASE (t
) = t
;
6792 /* Every empty class contains an empty class. */
6793 if (CLASSTYPE_EMPTY_P (t
))
6794 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6796 /* Set the TYPE_DECL for this type to contain the right
6797 value for DECL_OFFSET, so that we can use it as part
6798 of a COMPONENT_REF for multiple inheritance. */
6799 layout_decl (TYPE_MAIN_DECL (t
), 0);
6801 /* Now fix up any virtual base class types that we left lying
6802 around. We must get these done before we try to lay out the
6803 virtual function table. As a side-effect, this will remove the
6804 base subobject fields. */
6805 layout_virtual_bases (rli
, empty_base_offsets
);
6807 /* Make sure that empty classes are reflected in RLI at this
6809 include_empty_classes (rli
);
6811 /* Make sure not to create any structures with zero size. */
6812 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6814 build_decl (input_location
,
6815 FIELD_DECL
, NULL_TREE
, char_type_node
));
6817 /* If this is a non-POD, declaring it packed makes a difference to how it
6818 can be used as a field; don't let finalize_record_size undo it. */
6819 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6820 rli
->packed_maybe_necessary
= true;
6822 /* Let the back end lay out the type. */
6823 finish_record_layout (rli
, /*free_p=*/true);
6825 /* If we didn't end up needing an as-base type, don't use it. */
6826 if (CLASSTYPE_AS_BASE (t
) != t
6827 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
6828 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
6829 causing us to lose the user-specified alignment as in PR94050. */
6830 && TYPE_USER_ALIGN (t
) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t
))
6831 && tree_int_cst_equal (TYPE_SIZE (t
),
6832 TYPE_SIZE (CLASSTYPE_AS_BASE (t
))))
6833 CLASSTYPE_AS_BASE (t
) = t
;
6835 if (TYPE_SIZE_UNIT (t
)
6836 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6837 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6838 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6839 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
6841 /* Warn about bases that can't be talked about due to ambiguity. */
6842 maybe_warn_about_inaccessible_bases (t
);
6844 /* Now that we're done with layout, give the base fields the real types. */
6845 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6846 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6847 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6850 splay_tree_delete (empty_base_offsets
);
6852 if (CLASSTYPE_EMPTY_P (t
)
6853 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6854 TYPE_SIZE_UNIT (t
)))
6855 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6858 /* Determine the "key method" for the class type indicated by TYPE,
6859 and set CLASSTYPE_KEY_METHOD accordingly. */
6862 determine_key_method (tree type
)
6866 if (processing_template_decl
6867 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6868 || CLASSTYPE_INTERFACE_KNOWN (type
))
6871 /* The key method is the first non-pure virtual function that is not
6872 inline at the point of class definition. On some targets the
6873 key function may not be inline; those targets should not call
6874 this function until the end of the translation unit. */
6875 for (method
= TYPE_FIELDS (type
); method
; method
= DECL_CHAIN (method
))
6876 if (TREE_CODE (method
) == FUNCTION_DECL
6877 && DECL_VINDEX (method
) != NULL_TREE
6878 && ! DECL_DECLARED_INLINE_P (method
)
6879 && ! DECL_PURE_VIRTUAL_P (method
))
6881 CLASSTYPE_KEY_METHOD (type
) = method
;
6888 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6889 class data member of non-zero size, otherwise false. */
6892 field_nonempty_p (const_tree fld
)
6894 if (TREE_CODE (fld
) == ERROR_MARK
)
6897 tree type
= TREE_TYPE (fld
);
6898 if (TREE_CODE (fld
) == FIELD_DECL
6899 && TREE_CODE (type
) != ERROR_MARK
6900 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
6902 return TYPE_SIZE (type
)
6903 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
6904 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
6910 /* Used by find_flexarrays and related functions. */
6914 /* The first flexible array member or non-zero array member found
6915 in the order of layout. */
6917 /* First non-static non-empty data member in the class or its bases. */
6919 /* The first non-static non-empty data member following either
6920 the flexible array member, if found, or the zero-length array member
6921 otherwise. AFTER[1] refers to the first such data member of a union
6922 of which the struct containing the flexible array member or zero-length
6923 array is a member, or NULL when no such union exists. This element is
6924 only used during searching, not for diagnosing problems. AFTER[0]
6925 refers to the first such data member that is not a member of such
6929 /* Refers to a struct (not union) in which the struct of which the flexible
6930 array is member is defined. Used to diagnose strictly (according to C)
6931 invalid uses of the latter structs. */
6935 /* Find either the first flexible array member or the first zero-length
6936 array, in that order of preference, among members of class T (but not
6937 its base classes), and set members of FMEM accordingly.
6938 BASE_P is true if T is a base class of another class.
6939 PUN is set to the outermost union in which the flexible array member
6940 (or zero-length array) is defined if one such union exists, otherwise
6942 Similarly, PSTR is set to a data member of the outermost struct of
6943 which the flexible array is a member if one such struct exists,
6944 otherwise to NULL. */
6947 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
6948 tree pun
/* = NULL_TREE */,
6949 tree pstr
/* = NULL_TREE */)
6951 /* Set the "pointer" to the outermost enclosing union if not set
6952 yet and maintain it for the remainder of the recursion. */
6953 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
6956 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
6958 if (fld
== error_mark_node
)
6961 /* Is FLD a typedef for an anonymous struct? */
6963 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6964 handled elsewhere so that errors like the following are detected
6966 typedef struct { int i, a[], j; } S; // bug c++/72753
6967 S s [2]; // bug c++/68489
6969 if (TREE_CODE (fld
) == TYPE_DECL
6970 && DECL_IMPLICIT_TYPEDEF_P (fld
)
6971 && CLASS_TYPE_P (TREE_TYPE (fld
))
6972 && IDENTIFIER_ANON_P (DECL_NAME (fld
)))
6974 /* Check the nested unnamed type referenced via a typedef
6975 independently of FMEM (since it's not a data member of
6976 the enclosing class). */
6977 check_flexarrays (TREE_TYPE (fld
));
6981 /* Skip anything that's GCC-generated or not a (non-static) data
6983 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
6986 /* Type of the member. */
6987 tree fldtype
= TREE_TYPE (fld
);
6988 if (fldtype
== error_mark_node
)
6991 /* Determine the type of the array element or object referenced
6992 by the member so that it can be checked for flexible array
6993 members if it hasn't been yet. */
6994 tree eltype
= fldtype
;
6995 while (TREE_CODE (eltype
) == ARRAY_TYPE
6996 || INDIRECT_TYPE_P (eltype
))
6997 eltype
= TREE_TYPE (eltype
);
6999 if (RECORD_OR_UNION_TYPE_P (eltype
))
7001 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
7003 /* Once the member after the flexible array has been found
7005 fmem
->after
[bool (pun
)] = fld
;
7009 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
7011 /* Descend into the non-static member struct or union and try
7012 to find a flexible array member or zero-length array among
7013 its members. This is only necessary for anonymous types
7014 and types in whose context the current type T has not been
7015 defined (the latter must not be checked again because they
7016 are already in the process of being checked by one of the
7017 recursive calls). */
7019 tree first
= fmem
->first
;
7020 tree array
= fmem
->array
;
7022 /* If this member isn't anonymous and a prior non-flexible array
7023 member has been seen in one of the enclosing structs, clear
7024 the FIRST member since it doesn't contribute to the flexible
7025 array struct's members. */
7026 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7027 fmem
->first
= NULL_TREE
;
7029 find_flexarrays (eltype
, fmem
, false, pun
,
7030 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
7032 if (fmem
->array
!= array
)
7035 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7037 /* Restore the FIRST member reset above if no flexible
7038 array member has been found in this member's struct. */
7039 fmem
->first
= first
;
7042 /* If the member struct contains the first flexible array
7043 member, or if this member is a base class, continue to
7044 the next member and avoid setting the FMEM->NEXT pointer
7051 if (field_nonempty_p (fld
))
7053 /* Remember the first non-static data member. */
7057 /* Remember the first non-static data member after the flexible
7058 array member, if one has been found, or the zero-length array
7059 if it has been found. */
7060 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
7061 fmem
->after
[bool (pun
)] = fld
;
7064 /* Skip non-arrays. */
7065 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
7068 /* Determine the upper bound of the array if it has one. */
7069 if (TYPE_DOMAIN (fldtype
))
7073 /* Make a record of the zero-length array if either one
7074 such field or a flexible array member has been seen to
7075 handle the pathological and unlikely case of multiple
7077 if (!fmem
->after
[bool (pun
)])
7078 fmem
->after
[bool (pun
)] = fld
;
7080 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
7082 /* Remember the first zero-length array unless a flexible array
7083 member has already been seen. */
7085 fmem
->enclosing
= pstr
;
7090 /* Flexible array members have no upper bound. */
7093 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7095 /* Replace the zero-length array if it's been stored and
7096 reset the after pointer. */
7097 fmem
->after
[bool (pun
)] = NULL_TREE
;
7099 fmem
->enclosing
= pstr
;
7101 else if (!fmem
->after
[bool (pun
)])
7102 /* Make a record of another flexible array member. */
7103 fmem
->after
[bool (pun
)] = fld
;
7108 fmem
->enclosing
= pstr
;
7114 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7115 a flexible array member (or the zero-length array extension). */
7118 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
7120 if (fmem
->array
&& fmem
->enclosing
)
7122 auto_diagnostic_group d
;
7123 if (pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
7124 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
7125 ? G_("invalid use of %q#T with a zero-size array "
7127 : G_("invalid use of %q#T with a flexible array member "
7129 DECL_CONTEXT (fmem
->array
),
7130 DECL_CONTEXT (fmem
->enclosing
)))
7131 inform (DECL_SOURCE_LOCATION (fmem
->array
),
7132 "array member %q#D declared here", fmem
->array
);
7136 /* Issue diagnostics for invalid flexible array members or zero-length
7137 arrays that are not the last elements of the containing class or its
7138 base classes or that are its sole members. */
7141 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
7146 if (fmem
->first
&& !fmem
->after
[0])
7148 diagnose_invalid_flexarray (fmem
);
7152 /* Has a diagnostic been issued? */
7155 const char *msg
= 0;
7157 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7160 msg
= G_("zero-size array member %qD not at end of %q#T");
7161 else if (!fmem
->first
)
7162 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
7166 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7168 auto_diagnostic_group d
;
7169 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
7171 inform (location_of (t
), "in the definition of %q#T", t
);
7179 msg
= G_("flexible array member %qD not at end of %q#T");
7180 else if (!fmem
->first
)
7181 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
7185 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7188 auto_diagnostic_group d
;
7189 error_at (loc
, msg
, fmem
->array
, t
);
7191 /* In the unlikely event that the member following the flexible
7192 array member is declared in a different class, or the member
7193 overlaps another member of a common union, point to it.
7194 Otherwise it should be obvious. */
7196 && ((DECL_CONTEXT (fmem
->after
[0])
7197 != DECL_CONTEXT (fmem
->array
))))
7199 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
7200 "next member %q#D declared here",
7202 inform (location_of (t
), "in the definition of %q#T", t
);
7207 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
7208 diagnose_invalid_flexarray (fmem
);
7212 /* Recursively check to make sure that any flexible array or zero-length
7213 array members of class T or its bases are valid (i.e., not the sole
7214 non-static data member of T and, if one exists, that it is the last
7215 non-static data member of T and its base classes. FMEM is expected
7216 to be initially null and is used internally by recursive calls to
7217 the function. Issue the appropriate diagnostics for the array member
7218 that fails the checks. */
7221 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
7222 bool base_p
/* = false */)
7224 /* Initialize the result of a search for flexible array and zero-length
7225 array members. Avoid doing any work if the most interesting FMEM data
7226 have already been populated. */
7227 flexmems_t flexmems
= flexmems_t ();
7230 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
7233 tree fam
= fmem
->array
;
7235 /* Recursively check the primary base class first. */
7236 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7238 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
7239 check_flexarrays (basetype
, fmem
, true);
7242 /* Recursively check the base classes. */
7243 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
7244 for (int i
= 0; i
< nbases
; ++i
)
7246 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
7248 /* The primary base class was already checked above. */
7249 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
7252 /* Virtual base classes are at the end. */
7253 if (BINFO_VIRTUAL_P (base_binfo
))
7256 /* Check the base class. */
7257 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
7260 if (fmem
== &flexmems
)
7262 /* Check virtual base classes only once per derived class.
7263 I.e., this check is not performed recursively for base
7267 vec
<tree
, va_gc
> *vbases
;
7268 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
7269 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
7271 /* Check the virtual base class. */
7272 tree basetype
= TREE_TYPE (base_binfo
);
7274 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
7278 /* Is the type unnamed (and therefore a member of it potentially
7279 an anonymous struct or union)? */
7280 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
7281 if (tree ctx
= maybe_anon_p
? TYPE_CONTEXT (t
) : NULL_TREE
)
7282 maybe_anon_p
= RECORD_OR_UNION_TYPE_P (ctx
);
7284 /* Search the members of the current (possibly derived) class, skipping
7285 unnamed structs and unions since those could be anonymous. */
7286 if (fmem
!= &flexmems
|| !maybe_anon_p
)
7287 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
7289 if (fmem
== &flexmems
&& !maybe_anon_p
)
7291 /* Issue diagnostics for invalid flexible and zero-length array
7292 members found in base classes or among the members of the current
7293 class. Ignore anonymous structs and unions whose members are
7294 considered to be members of the enclosing class and thus will
7295 be diagnosed when checking it. */
7296 diagnose_flexarrays (t
, fmem
);
7300 /* Perform processing required when the definition of T (a class type)
7301 is complete. Diagnose invalid definitions of flexible array members
7302 and zero-size arrays. */
7305 finish_struct_1 (tree t
)
7308 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7309 tree virtuals
= NULL_TREE
;
7311 if (COMPLETE_TYPE_P (t
))
7313 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
7314 error ("redefinition of %q#T", t
);
7319 /* If this type was previously laid out as a forward reference,
7320 make sure we lay it out again. */
7321 TYPE_SIZE (t
) = NULL_TREE
;
7322 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
7324 /* Make assumptions about the class; we'll reset the flags if
7326 CLASSTYPE_EMPTY_P (t
) = 1;
7327 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
7328 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
7329 CLASSTYPE_LITERAL_P (t
) = true;
7331 /* Do end-of-class semantic processing: checking the validity of the
7332 bases and members and add implicitly generated methods. */
7333 check_bases_and_members (t
);
7335 /* Find the key method. */
7336 if (TYPE_CONTAINS_VPTR_P (t
))
7338 /* The Itanium C++ ABI permits the key method to be chosen when
7339 the class is defined -- even though the key method so
7340 selected may later turn out to be an inline function. On
7341 some systems (such as ARM Symbian OS) the key method cannot
7342 be determined until the end of the translation unit. On such
7343 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7344 will cause the class to be added to KEYED_CLASSES. Then, in
7345 finish_file we will determine the key method. */
7346 if (targetm
.cxx
.key_method_may_be_inline ())
7347 determine_key_method (t
);
7349 /* If a polymorphic class has no key method, we may emit the vtable
7350 in every translation unit where the class definition appears. If
7351 we're devirtualizing, we can look into the vtable even if we
7352 aren't emitting it. */
7353 if (!CLASSTYPE_KEY_METHOD (t
))
7354 vec_safe_push (keyed_classes
, t
);
7357 /* Layout the class itself. */
7358 layout_class_type (t
, &virtuals
);
7359 /* COMPLETE_TYPE_P is now true. */
7361 set_class_bindings (t
);
7363 /* With the layout complete, check for flexible array members and
7364 zero-length arrays that might overlap other members in the final
7366 check_flexarrays (t
);
7368 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
7370 /* If necessary, create the primary vtable for this class. */
7371 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
7373 /* We must enter these virtuals into the table. */
7374 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7375 build_primary_vtable (NULL_TREE
, t
);
7376 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
7377 /* Here we know enough to change the type of our virtual
7378 function table, but we will wait until later this function. */
7379 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
7381 /* If we're warning about ABI tags, check the types of the new
7382 virtual functions. */
7384 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
7385 check_abi_tags (t
, TREE_VALUE (v
));
7388 if (TYPE_CONTAINS_VPTR_P (t
))
7393 if (BINFO_VTABLE (TYPE_BINFO (t
)))
7394 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
7395 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7396 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
7398 /* Add entries for virtual functions introduced by this class. */
7399 BINFO_VIRTUALS (TYPE_BINFO (t
))
7400 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
7402 /* Set DECL_VINDEX for all functions declared in this class. */
7403 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
7405 fn
= TREE_CHAIN (fn
),
7406 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
7407 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
7409 tree fndecl
= BV_FN (fn
);
7411 if (DECL_THUNK_P (fndecl
))
7412 /* A thunk. We should never be calling this entry directly
7413 from this vtable -- we'd use the entry for the non
7414 thunk base function. */
7415 DECL_VINDEX (fndecl
) = NULL_TREE
;
7416 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7417 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7421 finish_struct_bits (t
);
7423 set_method_tm_attributes (t
);
7424 if (flag_openmp
|| flag_openmp_simd
)
7425 finish_omp_declare_simd_methods (t
);
7427 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7428 for any static member objects of the type we're working on. */
7429 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7430 if (DECL_DECLARES_FUNCTION_P (x
))
7431 DECL_IN_AGGR_P (x
) = false;
7432 else if (VAR_P (x
) && TREE_STATIC (x
)
7433 && TREE_TYPE (x
) != error_mark_node
7434 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7435 SET_DECL_MODE (x
, TYPE_MODE (t
));
7437 /* Complain if one of the field types requires lower visibility. */
7438 constrain_class_visibility (t
);
7440 /* Make the rtl for any new vtables we have created, and unmark
7441 the base types we marked. */
7444 /* Build the VTT for T. */
7448 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7449 && !CLASSTYPE_FINAL (t
))
7450 warning (OPT_Wnon_virtual_dtor
,
7451 "%q#T has virtual functions and accessible"
7452 " non-virtual destructor", t
);
7456 if (warn_overloaded_virtual
)
7459 /* Class layout, assignment of virtual table slots, etc., is now
7460 complete. Give the back end a chance to tweak the visibility of
7461 the class or perform any other required target modifications. */
7462 targetm
.cxx
.adjust_class_at_definition (t
);
7464 maybe_suppress_debug_info (t
);
7466 if (flag_vtable_verify
)
7467 vtv_save_class_info (t
);
7469 dump_class_hierarchy (t
);
7471 /* Finish debugging output for this type. */
7472 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7474 /* Recalculate satisfaction that might depend on completeness. */
7475 clear_satisfaction_cache ();
7477 if (TYPE_TRANSPARENT_AGGR (t
))
7479 tree field
= first_field (t
);
7480 if (field
== NULL_TREE
|| error_operand_p (field
))
7482 error ("type transparent %q#T does not have any fields", t
);
7483 TYPE_TRANSPARENT_AGGR (t
) = 0;
7485 else if (DECL_ARTIFICIAL (field
))
7487 if (DECL_FIELD_IS_BASE (field
))
7488 error ("type transparent class %qT has base classes", t
);
7491 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7492 error ("type transparent class %qT has virtual functions", t
);
7494 TYPE_TRANSPARENT_AGGR (t
) = 0;
7496 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7498 error ("type transparent %q#T cannot be made transparent because "
7499 "the type of the first field has a different ABI from the "
7500 "class overall", t
);
7501 TYPE_TRANSPARENT_AGGR (t
) = 0;
7506 /* When T was built up, the member declarations were added in reverse
7507 order. Rearrange them to declaration order. */
7510 unreverse_member_declarations (tree t
)
7516 /* The following lists are all in reverse order. Put them in
7517 declaration order now. */
7518 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7520 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7521 order, so we can't just use nreverse. Due to stat_hack
7522 chicanery in finish_member_declaration. */
7524 for (x
= TYPE_FIELDS (t
);
7525 x
&& TREE_CODE (x
) != TYPE_DECL
;
7528 next
= DECL_CHAIN (x
);
7529 DECL_CHAIN (x
) = prev
;
7535 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7536 TYPE_FIELDS (t
) = prev
;
7541 finish_struct (tree t
, tree attributes
)
7543 location_t saved_loc
= input_location
;
7545 /* Now that we've got all the field declarations, reverse everything
7547 unreverse_member_declarations (t
);
7549 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7550 fixup_attribute_variants (t
);
7552 /* Nadger the current location so that diagnostics point to the start of
7553 the struct, not the end. */
7554 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7556 if (processing_template_decl
)
7560 /* We need to add the target functions of USING_DECLS, so that
7561 they can be found when the using declaration is not
7562 instantiated yet. */
7563 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7564 if (TREE_CODE (x
) == USING_DECL
)
7566 tree fn
= strip_using_decl (x
);
7568 for (lkp_iterator
iter (fn
); iter
; ++iter
)
7569 add_method (t
, *iter
, true);
7571 else if (DECL_DECLARES_FUNCTION_P (x
))
7573 DECL_IN_AGGR_P (x
) = false;
7574 if (DECL_VIRTUAL_P (x
))
7575 CLASSTYPE_NON_AGGREGATE (t
) = true;
7577 else if (TREE_CODE (x
) == FIELD_DECL
)
7579 if (TREE_PROTECTED (x
) || TREE_PRIVATE (x
))
7580 CLASSTYPE_NON_AGGREGATE (t
) = true;
7583 /* Also add a USING_DECL for operator=. We know there'll be (at
7584 least) one, but we don't know the signature(s). We want name
7585 lookup not to fail or recurse into bases. This isn't added
7586 to the template decl list so we drop this at instantiation
7588 tree ass_op
= build_lang_decl (USING_DECL
, assign_op_identifier
,
7590 DECL_CONTEXT (ass_op
) = t
;
7591 USING_DECL_SCOPE (ass_op
) = t
;
7592 DECL_DEPENDENT_P (ass_op
) = true;
7593 DECL_ARTIFICIAL (ass_op
) = true;
7594 DECL_CHAIN (ass_op
) = TYPE_FIELDS (t
);
7595 TYPE_FIELDS (t
) = ass_op
;
7597 TYPE_SIZE (t
) = bitsize_zero_node
;
7598 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7599 /* COMPLETE_TYPE_P is now true. */
7601 set_class_bindings (t
);
7603 /* We need to emit an error message if this type was used as a parameter
7604 and it is an abstract type, even if it is a template. We construct
7605 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7606 account and we call complete_vars with this type, which will check
7607 the PARM_DECLS. Note that while the type is being defined,
7608 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7609 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7610 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7611 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7612 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_PURE_VIRTUAL_P (x
))
7613 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7616 /* Remember current #pragma pack value. */
7617 TYPE_PRECISION (t
) = maximum_field_alignment
;
7619 if (cxx_dialect
< cxx20
)
7621 if (!CLASSTYPE_NON_AGGREGATE (t
)
7622 && type_has_user_provided_or_explicit_constructor (t
))
7623 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7625 else if (TYPE_HAS_USER_CONSTRUCTOR (t
))
7626 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7628 /* Fix up any variants we've already built. */
7629 fixup_type_variants (t
);
7632 finish_struct_1 (t
);
7633 /* COMPLETE_TYPE_P is now true. */
7635 maybe_warn_about_overly_private_class (t
);
7637 if (is_std_init_list (t
))
7639 /* People keep complaining that the compiler crashes on an invalid
7640 definition of initializer_list, so I guess we should explicitly
7641 reject it. What the compiler internals care about is that it's a
7642 template and has a pointer field followed by size_type field. */
7644 if (processing_template_decl
)
7646 tree f
= next_initializable_field (TYPE_FIELDS (t
));
7647 if (f
&& TYPE_PTR_P (TREE_TYPE (f
)))
7649 f
= next_initializable_field (DECL_CHAIN (f
));
7650 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7655 fatal_error (input_location
, "definition of %qD does not match "
7656 "%<#include <initializer_list>%>", TYPE_NAME (t
));
7659 input_location
= saved_loc
;
7661 TYPE_BEING_DEFINED (t
) = 0;
7663 if (current_class_type
)
7666 error ("trying to finish struct, but kicked out due to previous parse errors");
7669 for (tree decl
= TYPE_FIELDS (t
); decl
; decl
= DECL_CHAIN (decl
))
7670 if (TREE_CODE (decl
) == FUNCTION_DECL
7671 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
7672 if (tree attr
= lookup_attribute ("omp declare variant base",
7673 DECL_ATTRIBUTES (decl
)))
7674 omp_declare_variant_finalize (decl
, attr
);
7676 if (processing_template_decl
&& at_function_scope_p ()
7677 /* Lambdas are defined by the LAMBDA_EXPR. */
7678 && !LAMBDA_TYPE_P (t
))
7679 add_stmt (build_min (TAG_DEFN
, t
));
7684 /* Hash table to avoid endless recursion when handling references. */
7685 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7687 /* Return the dynamic type of INSTANCE, if known.
7688 Used to determine whether the virtual function table is needed
7691 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7692 of our knowledge of its type. *NONNULL should be initialized
7693 before this function is called. */
7696 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7698 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7700 switch (TREE_CODE (instance
))
7703 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
7706 return RECUR (TREE_OPERAND (instance
, 0));
7709 /* This is a call to a constructor, hence it's never zero. */
7710 if (CALL_EXPR_FN (instance
)
7711 && TREE_HAS_CONSTRUCTOR (instance
))
7715 return TREE_TYPE (instance
);
7720 /* This is a call to a constructor, hence it's never zero. */
7721 if (TREE_HAS_CONSTRUCTOR (instance
))
7725 return TREE_TYPE (instance
);
7727 return RECUR (TREE_OPERAND (instance
, 0));
7729 case POINTER_PLUS_EXPR
:
7732 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
7733 return RECUR (TREE_OPERAND (instance
, 0));
7734 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7735 /* Propagate nonnull. */
7736 return RECUR (TREE_OPERAND (instance
, 0));
7741 return RECUR (TREE_OPERAND (instance
, 0));
7744 instance
= TREE_OPERAND (instance
, 0);
7747 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7748 with a real object -- given &p->f, p can still be null. */
7749 tree t
= get_base_address (instance
);
7750 /* ??? Probably should check DECL_WEAK here. */
7751 if (t
&& DECL_P (t
))
7754 return RECUR (instance
);
7757 /* If this component is really a base class reference, then the field
7758 itself isn't definitive. */
7759 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
7760 return RECUR (TREE_OPERAND (instance
, 0));
7761 return RECUR (TREE_OPERAND (instance
, 1));
7765 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
7766 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7770 return TREE_TYPE (TREE_TYPE (instance
));
7776 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7780 return TREE_TYPE (instance
);
7782 else if (instance
== current_class_ptr
)
7787 /* if we're in a ctor or dtor, we know our type. If
7788 current_class_ptr is set but we aren't in a function, we're in
7789 an NSDMI (and therefore a constructor). */
7790 if (current_scope () != current_function_decl
7791 || (DECL_LANG_SPECIFIC (current_function_decl
)
7792 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7793 || DECL_DESTRUCTOR_P (current_function_decl
))))
7797 return TREE_TYPE (TREE_TYPE (instance
));
7800 else if (TYPE_REF_P (TREE_TYPE (instance
)))
7802 /* We only need one hash table because it is always left empty. */
7803 if (!fixed_type_or_null_ref_ht
)
7804 fixed_type_or_null_ref_ht
7805 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7807 /* Reference variables should be references to objects. */
7811 /* Enter the INSTANCE in a table to prevent recursion; a
7812 variable's initializer may refer to the variable
7814 if (VAR_P (instance
)
7815 && DECL_INITIAL (instance
)
7816 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7817 && !fixed_type_or_null_ref_ht
->find (instance
))
7822 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7824 type
= RECUR (DECL_INITIAL (instance
));
7825 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7832 case VIEW_CONVERT_EXPR
:
7833 if (location_wrapper_p (instance
))
7834 return RECUR (TREE_OPERAND (instance
, 0));
7836 /* TODO: Recursion may be correct for some non-location-wrapper
7837 uses of VIEW_CONVERT_EXPR. */
7846 /* Return nonzero if the dynamic type of INSTANCE is known, and
7847 equivalent to the static type. We also handle the case where
7848 INSTANCE is really a pointer. Return negative if this is a
7849 ctor/dtor. There the dynamic type is known, but this might not be
7850 the most derived base of the original object, and hence virtual
7851 bases may not be laid out according to this type.
7853 Used to determine whether the virtual function table is needed
7856 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7857 of our knowledge of its type. *NONNULL should be initialized
7858 before this function is called. */
7861 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7863 tree t
= TREE_TYPE (instance
);
7867 /* processing_template_decl can be false in a template if we're in
7868 instantiate_non_dependent_expr, but we still want to suppress
7870 if (in_template_function ())
7872 /* In a template we only care about the type of the result. */
7878 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7879 if (INDIRECT_TYPE_P (t
))
7881 if (CLASS_TYPE_P (t
) && CLASSTYPE_FINAL (t
))
7883 if (fixed
== NULL_TREE
)
7885 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7887 return cdtorp
? -1 : 1;
7892 init_class_processing (void)
7894 current_class_depth
= 0;
7895 current_class_stack_size
= 10;
7897 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7898 sizeof_biggest_empty_class
= size_zero_node
;
7900 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7901 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7902 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7905 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7908 restore_class_cache (void)
7912 /* We are re-entering the same class we just left, so we don't
7913 have to search the whole inheritance matrix to find all the
7914 decls to bind again. Instead, we install the cached
7915 class_shadowed list and walk through it binding names. */
7916 push_binding_level (previous_class_level
);
7917 class_binding_level
= previous_class_level
;
7918 /* Restore IDENTIFIER_TYPE_VALUE. */
7919 for (type
= class_binding_level
->type_shadowed
;
7921 type
= TREE_CHAIN (type
))
7922 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7925 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7926 appropriate for TYPE.
7928 So that we may avoid calls to lookup_name, we cache the _TYPE
7929 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7931 For multiple inheritance, we perform a two-pass depth-first search
7932 of the type lattice. */
7935 pushclass (tree type
)
7937 class_stack_node_t csn
;
7939 type
= TYPE_MAIN_VARIANT (type
);
7941 /* Make sure there is enough room for the new entry on the stack. */
7942 if (current_class_depth
+ 1 >= current_class_stack_size
)
7944 current_class_stack_size
*= 2;
7946 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7947 current_class_stack_size
);
7950 /* Insert a new entry on the class stack. */
7951 csn
= current_class_stack
+ current_class_depth
;
7952 csn
->name
= current_class_name
;
7953 csn
->type
= current_class_type
;
7954 csn
->access
= current_access_specifier
;
7955 csn
->names_used
= 0;
7957 current_class_depth
++;
7959 /* Now set up the new type. */
7960 current_class_name
= TYPE_NAME (type
);
7961 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7962 current_class_name
= DECL_NAME (current_class_name
);
7963 current_class_type
= type
;
7965 /* By default, things in classes are private, while things in
7966 structures or unions are public. */
7967 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7968 ? access_private_node
7969 : access_public_node
);
7971 if (previous_class_level
7972 && type
!= previous_class_level
->this_entity
7973 && current_class_depth
== 1)
7975 /* Forcibly remove any old class remnants. */
7976 invalidate_class_lookup_cache ();
7979 if (!previous_class_level
7980 || type
!= previous_class_level
->this_entity
7981 || current_class_depth
> 1)
7984 restore_class_cache ();
7987 /* Get out of the current class scope. If we were in a class scope
7988 previously, that is the one popped to. */
7995 current_class_depth
--;
7996 current_class_name
= current_class_stack
[current_class_depth
].name
;
7997 current_class_type
= current_class_stack
[current_class_depth
].type
;
7998 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7999 if (current_class_stack
[current_class_depth
].names_used
)
8000 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
8003 /* Mark the top of the class stack as hidden. */
8006 push_class_stack (void)
8008 if (current_class_depth
)
8009 ++current_class_stack
[current_class_depth
- 1].hidden
;
8012 /* Mark the top of the class stack as un-hidden. */
8015 pop_class_stack (void)
8017 if (current_class_depth
)
8018 --current_class_stack
[current_class_depth
- 1].hidden
;
8021 /* If the class type currently being defined is either T or
8022 a nested type of T, returns the type from the current_class_stack,
8023 which might be equivalent to but not equal to T in case of
8024 constrained partial specializations. */
8027 currently_open_class (tree t
)
8031 if (!CLASS_TYPE_P (t
))
8034 t
= TYPE_MAIN_VARIANT (t
);
8036 /* We start looking from 1 because entry 0 is from global scope,
8038 for (i
= current_class_depth
; i
> 0; --i
)
8041 if (i
== current_class_depth
)
8042 c
= current_class_type
;
8045 if (current_class_stack
[i
].hidden
)
8047 c
= current_class_stack
[i
].type
;
8051 if (same_type_p (c
, t
))
8057 /* If either current_class_type or one of its enclosing classes are derived
8058 from T, return the appropriate type. Used to determine how we found
8059 something via unqualified lookup. */
8062 currently_open_derived_class (tree t
)
8066 /* The bases of a dependent type are unknown. */
8067 if (dependent_type_p (t
))
8070 if (!current_class_type
)
8073 if (DERIVED_FROM_P (t
, current_class_type
))
8074 return current_class_type
;
8076 for (i
= current_class_depth
- 1; i
> 0; --i
)
8078 if (current_class_stack
[i
].hidden
)
8080 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
8081 return current_class_stack
[i
].type
;
8087 /* Return the outermost enclosing class type that is still open, or
8091 outermost_open_class (void)
8093 if (!current_class_type
)
8096 if (TYPE_BEING_DEFINED (current_class_type
))
8097 r
= current_class_type
;
8098 for (int i
= current_class_depth
- 1; i
> 0; --i
)
8100 if (current_class_stack
[i
].hidden
)
8102 tree t
= current_class_stack
[i
].type
;
8103 if (!TYPE_BEING_DEFINED (t
))
8110 /* Returns the innermost class type which is not a lambda closure type. */
8113 current_nonlambda_class_type (void)
8115 tree type
= current_class_type
;
8116 while (type
&& LAMBDA_TYPE_P (type
))
8117 type
= decl_type_context (TYPE_NAME (type
));
8121 /* When entering a class scope, all enclosing class scopes' names with
8122 static meaning (static variables, static functions, types and
8123 enumerators) have to be visible. This recursive function calls
8124 pushclass for all enclosing class contexts until global or a local
8125 scope is reached. TYPE is the enclosed class. */
8128 push_nested_class (tree type
)
8130 /* A namespace might be passed in error cases, like A::B:C. */
8131 if (type
== NULL_TREE
8132 || !CLASS_TYPE_P (type
))
8135 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
8140 /* Undoes a push_nested_class call. */
8143 pop_nested_class (void)
8145 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
8148 if (context
&& CLASS_TYPE_P (context
))
8149 pop_nested_class ();
8152 /* Returns the number of extern "LANG" blocks we are nested within. */
8155 current_lang_depth (void)
8157 return vec_safe_length (current_lang_base
);
8160 /* Set global variables CURRENT_LANG_NAME to appropriate value
8161 so that behavior of name-mangling machinery is correct. */
8164 push_lang_context (tree name
)
8166 vec_safe_push (current_lang_base
, current_lang_name
);
8168 if (name
== lang_name_cplusplus
)
8169 current_lang_name
= name
;
8170 else if (name
== lang_name_c
)
8171 current_lang_name
= name
;
8173 error ("language string %<\"%E\"%> not recognized", name
);
8176 /* Get out of the current language scope. */
8179 pop_lang_context (void)
8181 current_lang_name
= current_lang_base
->pop ();
8184 /* Type instantiation routines. */
8186 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8187 matches the TARGET_TYPE. If there is no satisfactory match, return
8188 error_mark_node, and issue an error & warning messages under
8189 control of FLAGS. Permit pointers to member function if FLAGS
8190 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8191 a template-id, and EXPLICIT_TARGS are the explicitly provided
8194 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8195 is the base path used to reference those member functions. If
8196 the address is resolved to a member function, access checks will be
8197 performed and errors issued if appropriate. */
8200 resolve_address_of_overloaded_function (tree target_type
,
8202 tsubst_flags_t complain
,
8204 tree explicit_targs
,
8207 /* Here's what the standard says:
8211 If the name is a function template, template argument deduction
8212 is done, and if the argument deduction succeeds, the deduced
8213 arguments are used to generate a single template function, which
8214 is added to the set of overloaded functions considered.
8216 Non-member functions and static member functions match targets of
8217 type "pointer-to-function" or "reference-to-function." Nonstatic
8218 member functions match targets of type "pointer-to-member
8219 function;" the function type of the pointer to member is used to
8220 select the member function from the set of overloaded member
8221 functions. If a non-static member function is selected, the
8222 reference to the overloaded function name is required to have the
8223 form of a pointer to member as described in 5.3.1.
8225 If more than one function is selected, any template functions in
8226 the set are eliminated if the set also contains a non-template
8227 function, and any given template function is eliminated if the
8228 set contains a second template function that is more specialized
8229 than the first according to the partial ordering rules 14.5.5.2.
8230 After such eliminations, if any, there shall remain exactly one
8231 selected function. */
8234 /* We store the matches in a TREE_LIST rooted here. The functions
8235 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8236 interoperability with most_specialized_instantiation. */
8237 tree matches
= NULL_TREE
;
8239 tree target_fn_type
;
8241 /* By the time we get here, we should be seeing only real
8242 pointer-to-member types, not the internal POINTER_TYPE to
8243 METHOD_TYPE representation. */
8244 gcc_assert (!TYPE_PTR_P (target_type
)
8245 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
8247 gcc_assert (is_overloaded_fn (overload
));
8249 /* Check that the TARGET_TYPE is reasonable. */
8250 if (TYPE_PTRFN_P (target_type
)
8251 || TYPE_REFFN_P (target_type
))
8253 else if (TYPE_PTRMEMFUNC_P (target_type
))
8254 /* This is OK, too. */
8256 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
8257 /* This is OK, too. This comes from a conversion to reference
8259 target_type
= build_reference_type (target_type
);
8262 if (complain
& tf_error
)
8263 error ("cannot resolve overloaded function %qD based on"
8264 " conversion to type %qT",
8265 OVL_NAME (overload
), target_type
);
8266 return error_mark_node
;
8269 /* Non-member functions and static member functions match targets of type
8270 "pointer-to-function" or "reference-to-function." Nonstatic member
8271 functions match targets of type "pointer-to-member-function;" the
8272 function type of the pointer to member is used to select the member
8273 function from the set of overloaded member functions.
8275 So figure out the FUNCTION_TYPE that we want to match against. */
8276 target_fn_type
= static_fn_type (target_type
);
8278 /* If we can find a non-template function that matches, we can just
8279 use it. There's no point in generating template instantiations
8280 if we're just going to throw them out anyhow. But, of course, we
8281 can only do this when we don't *need* a template function. */
8283 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8287 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8288 /* We're not looking for templates just yet. */
8291 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
) != is_ptrmem
)
8292 /* We're looking for a non-static member, and this isn't
8293 one, or vice versa. */
8296 /* Constraints must be satisfied. This is done before
8297 return type deduction since that instantiates the
8299 if (!constraints_satisfied_p (fn
))
8302 if (undeduced_auto_decl (fn
))
8304 /* Force instantiation to do return type deduction. */
8305 maybe_instantiate_decl (fn
);
8306 require_deduced_type (fn
);
8309 /* In C++17 we need the noexcept-qualifier to compare types. */
8310 if (flag_noexcept_type
8311 && !maybe_instantiate_noexcept (fn
, complain
))
8314 /* See if there's a match. */
8315 tree fntype
= static_fn_type (fn
);
8316 if (same_type_p (target_fn_type
, fntype
)
8317 || fnptr_conv_p (target_fn_type
, fntype
))
8318 matches
= tree_cons (fn
, NULL_TREE
, matches
);
8321 /* Now, if we've already got a match (or matches), there's no need
8322 to proceed to the template functions. But, if we don't have a
8323 match we need to look at them, too. */
8326 tree target_arg_types
;
8327 tree target_ret_type
;
8329 unsigned int nargs
, ia
;
8332 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
8333 target_ret_type
= TREE_TYPE (target_fn_type
);
8335 nargs
= list_length (target_arg_types
);
8336 args
= XALLOCAVEC (tree
, nargs
);
8337 for (arg
= target_arg_types
, ia
= 0;
8338 arg
!= NULL_TREE
&& arg
!= void_list_node
;
8339 arg
= TREE_CHAIN (arg
), ++ia
)
8340 args
[ia
] = TREE_VALUE (arg
);
8343 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8349 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8350 /* We're only looking for templates. */
8353 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8355 /* We're not looking for a non-static member, and this is
8356 one, or vice versa. */
8359 tree ret
= target_ret_type
;
8361 /* If the template has a deduced return type, don't expose it to
8362 template argument deduction. */
8363 if (undeduced_auto_decl (fn
))
8366 /* Try to do argument deduction. */
8367 targs
= make_tree_vec (DECL_NTPARMS (fn
));
8368 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
8370 DEDUCE_EXACT
, LOOKUP_NORMAL
,
8371 NULL
, false, false);
8372 if (instantiation
== error_mark_node
)
8373 /* Instantiation failed. */
8376 /* Constraints must be satisfied. This is done before
8377 return type deduction since that instantiates the
8379 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
8382 /* And now force instantiation to do return type deduction. */
8383 if (undeduced_auto_decl (instantiation
))
8386 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
8389 require_deduced_type (instantiation
);
8392 /* In C++17 we need the noexcept-qualifier to compare types. */
8393 if (flag_noexcept_type
)
8394 maybe_instantiate_noexcept (instantiation
, complain
);
8396 /* See if there's a match. */
8397 tree fntype
= static_fn_type (instantiation
);
8398 if (same_type_p (target_fn_type
, fntype
)
8399 || fnptr_conv_p (target_fn_type
, fntype
))
8400 matches
= tree_cons (instantiation
, fn
, matches
);
8403 /* Now, remove all but the most specialized of the matches. */
8406 tree match
= most_specialized_instantiation (matches
);
8408 if (match
!= error_mark_node
)
8409 matches
= tree_cons (TREE_PURPOSE (match
),
8415 /* Now we should have exactly one function in MATCHES. */
8416 if (matches
== NULL_TREE
)
8418 /* There were *no* matches. */
8419 if (complain
& tf_error
)
8421 error ("no matches converting function %qD to type %q#T",
8422 OVL_NAME (overload
), target_type
);
8424 print_candidates (overload
);
8426 return error_mark_node
;
8428 else if (TREE_CHAIN (matches
))
8430 /* There were too many matches. First check if they're all
8431 the same function. */
8432 tree match
= NULL_TREE
;
8434 fn
= TREE_PURPOSE (matches
);
8436 /* For multi-versioned functions, more than one match is just fine and
8437 decls_match will return false as they are different. */
8438 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
8439 if (!decls_match (fn
, TREE_PURPOSE (match
))
8440 && !targetm
.target_option
.function_versions
8441 (fn
, TREE_PURPOSE (match
)))
8446 if (complain
& tf_error
)
8448 error ("converting overloaded function %qD to type %q#T is ambiguous",
8449 OVL_NAME (overload
), target_type
);
8451 /* Since print_candidates expects the functions in the
8452 TREE_VALUE slot, we flip them here. */
8453 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8454 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8456 print_candidates (matches
);
8459 return error_mark_node
;
8463 /* Good, exactly one match. Now, convert it to the correct type. */
8464 fn
= TREE_PURPOSE (matches
);
8466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8467 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8469 static int explained
;
8471 if (!(complain
& tf_error
))
8472 return error_mark_node
;
8474 auto_diagnostic_group d
;
8475 if (permerror (input_location
, "assuming pointer to member %qD", fn
)
8478 inform (input_location
, "(a pointer to member can only be "
8479 "formed with %<&%E%>)", fn
);
8484 /* If a pointer to a function that is multi-versioned is requested, the
8485 pointer to the dispatcher function is returned instead. This works
8486 well because indirectly calling the function will dispatch the right
8487 function version at run-time. */
8488 if (DECL_FUNCTION_VERSIONED (fn
))
8490 fn
= get_function_version_dispatcher (fn
);
8492 return error_mark_node
;
8493 /* Mark all the versions corresponding to the dispatcher as used. */
8494 if (!(complain
& tf_conv
))
8495 mark_versions_used (fn
);
8498 /* If we're doing overload resolution purely for the purpose of
8499 determining conversion sequences, we should not consider the
8500 function used. If this conversion sequence is selected, the
8501 function will be marked as used at this point. */
8502 if (!(complain
& tf_conv
))
8504 /* Make =delete work with SFINAE. */
8505 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8506 return error_mark_node
;
8507 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8508 return error_mark_node
;
8511 /* We could not check access to member functions when this
8512 expression was originally created since we did not know at that
8513 time to which function the expression referred. */
8514 if (DECL_FUNCTION_MEMBER_P (fn
))
8516 gcc_assert (access_path
);
8517 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8520 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8521 return cp_build_addr_expr (fn
, complain
);
8524 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8525 will mark the function as addressed, but here we must do it
8527 cxx_mark_addressable (fn
);
8533 /* This function will instantiate the type of the expression given in
8534 RHS to match the type of LHSTYPE. If errors exist, then return
8535 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8536 we complain on errors. If we are not complaining, never modify rhs,
8537 as overload resolution wants to try many possible instantiations, in
8538 the hope that at least one will work.
8540 For non-recursive calls, LHSTYPE should be a function, pointer to
8541 function, or a pointer to member function. */
8544 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8546 tsubst_flags_t complain_in
= complain
;
8547 tree access_path
= NULL_TREE
;
8549 complain
&= ~tf_ptrmem_ok
;
8551 if (lhstype
== unknown_type_node
)
8553 if (complain
& tf_error
)
8554 error ("not enough type information");
8555 return error_mark_node
;
8558 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8560 tree fntype
= non_reference (lhstype
);
8561 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8563 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8565 if (flag_ms_extensions
8566 && TYPE_PTRMEMFUNC_P (fntype
)
8567 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8568 /* Microsoft allows `A::f' to be resolved to a
8569 pointer-to-member. */
8573 if (complain
& tf_error
)
8574 error ("cannot convert %qE from type %qT to type %qT",
8575 rhs
, TREE_TYPE (rhs
), fntype
);
8576 return error_mark_node
;
8580 /* If we instantiate a template, and it is a A ?: C expression
8581 with omitted B, look through the SAVE_EXPR. */
8582 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8583 rhs
= TREE_OPERAND (rhs
, 0);
8585 if (BASELINK_P (rhs
))
8587 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8588 rhs
= BASELINK_FUNCTIONS (rhs
);
8591 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8592 deduce any type information. */
8593 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
8595 if (complain
& tf_error
)
8596 error ("not enough type information");
8597 return error_mark_node
;
8600 /* There are only a few kinds of expressions that may have a type
8601 dependent on overload resolution. */
8602 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
8603 || TREE_CODE (rhs
) == COMPONENT_REF
8604 || is_overloaded_fn (rhs
)
8605 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8607 /* This should really only be used when attempting to distinguish
8608 what sort of a pointer to function we have. For now, any
8609 arithmetic operation which is not supported on pointers
8610 is rejected as an error. */
8612 switch (TREE_CODE (rhs
))
8616 tree member
= TREE_OPERAND (rhs
, 1);
8618 member
= instantiate_type (lhstype
, member
, complain
);
8619 if (member
!= error_mark_node
8620 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8621 /* Do not lose object's side effects. */
8622 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8623 TREE_OPERAND (rhs
, 0), member
);
8628 rhs
= TREE_OPERAND (rhs
, 1);
8629 if (BASELINK_P (rhs
))
8630 return instantiate_type (lhstype
, rhs
, complain_in
);
8632 /* This can happen if we are forming a pointer-to-member for a
8634 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8638 case TEMPLATE_ID_EXPR
:
8640 tree fns
= TREE_OPERAND (rhs
, 0);
8641 tree args
= TREE_OPERAND (rhs
, 1);
8644 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8645 /*template_only=*/true,
8652 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8653 /*template_only=*/false,
8654 /*explicit_targs=*/NULL_TREE
,
8659 if (PTRMEM_OK_P (rhs
))
8660 complain
|= tf_ptrmem_ok
;
8662 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8666 return error_mark_node
;
8671 return error_mark_node
;
8674 /* Return the name of the virtual function pointer field
8675 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8676 this may have to look back through base types to find the
8677 ultimate field name. (For single inheritance, these could
8678 all be the same name. Who knows for multiple inheritance). */
8681 get_vfield_name (tree type
)
8683 tree binfo
, base_binfo
;
8685 for (binfo
= TYPE_BINFO (type
);
8686 BINFO_N_BASE_BINFOS (binfo
);
8689 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8691 if (BINFO_VIRTUAL_P (base_binfo
)
8692 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8696 type
= BINFO_TYPE (binfo
);
8697 tree ctor_name
= constructor_name (type
);
8698 char *buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8699 + IDENTIFIER_LENGTH (ctor_name
) + 2);
8700 sprintf (buf
, VFIELD_NAME_FORMAT
, IDENTIFIER_POINTER (ctor_name
));
8701 return get_identifier (buf
);
8704 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8705 according to [class]:
8706 The class-name is also inserted
8707 into the scope of the class itself. For purposes of access checking,
8708 the inserted class name is treated as if it were a public member name. */
8711 build_self_reference (void)
8713 tree name
= DECL_NAME (TYPE_NAME (current_class_type
));
8714 tree decl
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8716 DECL_NONLOCAL (decl
) = 1;
8717 DECL_CONTEXT (decl
) = current_class_type
;
8718 DECL_ARTIFICIAL (decl
) = 1;
8719 SET_DECL_SELF_REFERENCE_P (decl
);
8720 set_underlying_type (decl
);
8722 if (processing_template_decl
)
8723 decl
= push_template_decl (decl
);
8725 tree saved_cas
= current_access_specifier
;
8726 current_access_specifier
= access_public_node
;
8727 finish_member_declaration (decl
);
8728 current_access_specifier
= saved_cas
;
8731 /* Returns 1 if TYPE contains only padding bytes. */
8734 is_empty_class (tree type
)
8736 if (type
== error_mark_node
)
8739 if (! CLASS_TYPE_P (type
))
8742 return CLASSTYPE_EMPTY_P (type
);
8745 /* Returns true if TYPE contains no actual data, just various
8746 possible combinations of empty classes. If IGNORE_VPTR is true,
8747 a vptr doesn't prevent the class from being considered empty. Typically
8748 we want to ignore the vptr on assignment, and not on initialization. */
8751 is_really_empty_class (tree type
, bool ignore_vptr
)
8753 if (CLASS_TYPE_P (type
))
8760 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8761 out, but we'd like to be able to check this before then. */
8762 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8765 if (!ignore_vptr
&& TYPE_CONTAINS_VPTR_P (type
))
8768 for (binfo
= TYPE_BINFO (type
), i
= 0;
8769 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8770 if (!is_really_empty_class (BINFO_TYPE (base_binfo
), ignore_vptr
))
8772 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8773 if (TREE_CODE (field
) == FIELD_DECL
8774 && !DECL_ARTIFICIAL (field
)
8775 /* An unnamed bit-field is not a data member. */
8776 && !DECL_UNNAMED_BIT_FIELD (field
)
8777 && !is_really_empty_class (TREE_TYPE (field
), ignore_vptr
))
8781 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8782 return (integer_zerop (array_type_nelts_top (type
))
8783 || is_really_empty_class (TREE_TYPE (type
), ignore_vptr
));
8787 /* Note that NAME was looked up while the current class was being
8788 defined and that the result of that lookup was DECL. */
8791 maybe_note_name_used_in_class (tree name
, tree decl
)
8793 splay_tree names_used
;
8795 /* If we're not defining a class, there's nothing to do. */
8796 if (!(innermost_scope_kind() == sk_class
8797 && TYPE_BEING_DEFINED (current_class_type
)
8798 && !LAMBDA_TYPE_P (current_class_type
)))
8801 /* If there's already a binding for this NAME, then we don't have
8802 anything to worry about. */
8803 if (lookup_member (current_class_type
, name
,
8804 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8807 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8808 current_class_stack
[current_class_depth
- 1].names_used
8809 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8810 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8812 splay_tree_insert (names_used
,
8813 (splay_tree_key
) name
,
8814 (splay_tree_value
) decl
);
8817 /* Note that NAME was declared (as DECL) in the current class. Check
8818 to see that the declaration is valid. */
8821 note_name_declared_in_class (tree name
, tree decl
)
8823 splay_tree names_used
;
8826 /* Look to see if we ever used this name. */
8828 = current_class_stack
[current_class_depth
- 1].names_used
;
8831 /* The C language allows members to be declared with a type of the same
8832 name, and the C++ standard says this diagnostic is not required. So
8833 allow it in extern "C" blocks unless predantic is specified.
8834 Allow it in all cases if -ms-extensions is specified. */
8835 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8836 || flag_ms_extensions
)
8838 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8841 /* [basic.scope.class]
8843 A name N used in a class S shall refer to the same declaration
8844 in its context and when re-evaluated in the completed scope of
8846 if (permerror (location_of (decl
),
8847 "declaration of %q#D changes meaning of %qD",
8848 decl
, OVL_NAME (decl
)))
8849 inform (location_of ((tree
) n
->value
),
8850 "%qD declared here as %q#D",
8851 OVL_NAME (decl
), (tree
) n
->value
);
8855 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8856 Secondary vtables are merged with primary vtables; this function
8857 will return the VAR_DECL for the primary vtable. */
8860 get_vtbl_decl_for_binfo (tree binfo
)
8864 decl
= BINFO_VTABLE (binfo
);
8865 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8867 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
8868 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8871 gcc_assert (VAR_P (decl
));
8876 /* Returns the binfo for the primary base of BINFO. If the resulting
8877 BINFO is a virtual base, and it is inherited elsewhere in the
8878 hierarchy, then the returned binfo might not be the primary base of
8879 BINFO in the complete object. Check BINFO_PRIMARY_P or
8880 BINFO_LOST_PRIMARY_P to be sure. */
8883 get_primary_binfo (tree binfo
)
8887 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8891 return copied_binfo (primary_base
, binfo
);
8894 /* As above, but iterate until we reach the binfo that actually provides the
8898 most_primary_binfo (tree binfo
)
8901 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
8902 && !BINFO_LOST_PRIMARY_P (b
))
8904 tree primary_base
= get_primary_binfo (b
);
8905 gcc_assert (BINFO_PRIMARY_P (primary_base
)
8906 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
8912 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8913 type. Note that the virtual inheritance might be above or below BINFO in
8917 vptr_via_virtual_p (tree binfo
)
8920 binfo
= TYPE_BINFO (binfo
);
8921 tree primary
= most_primary_binfo (binfo
);
8922 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8923 a morally virtual base. */
8924 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
8925 return virt
!= NULL_TREE
;
8928 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8931 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8934 fprintf (stream
, "%*s", indent
, "");
8938 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8939 INDENT should be zero when called from the top level; it is
8940 incremented recursively. IGO indicates the next expected BINFO in
8941 inheritance graph ordering. */
8944 dump_class_hierarchy_r (FILE *stream
,
8954 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8955 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8956 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8959 fprintf (stream
, "alternative-path\n");
8962 igo
= TREE_CHAIN (binfo
);
8964 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8965 tree_to_shwi (BINFO_OFFSET (binfo
)));
8966 if (is_empty_class (BINFO_TYPE (binfo
)))
8967 fprintf (stream
, " empty");
8968 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8969 fprintf (stream
, " nearly-empty");
8970 if (BINFO_VIRTUAL_P (binfo
))
8971 fprintf (stream
, " virtual");
8972 fprintf (stream
, "\n");
8974 if (BINFO_PRIMARY_P (binfo
))
8976 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8977 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8978 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8979 TFF_PLAIN_IDENTIFIER
),
8980 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8982 if (BINFO_LOST_PRIMARY_P (binfo
))
8984 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8985 fprintf (stream
, " lost-primary");
8988 fprintf (stream
, "\n");
8990 if (!(flags
& TDF_SLIM
))
8994 if (BINFO_SUBVTT_INDEX (binfo
))
8996 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8997 fprintf (stream
, " subvttidx=%s",
8998 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8999 TFF_PLAIN_IDENTIFIER
));
9001 if (BINFO_VPTR_INDEX (binfo
))
9003 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9004 fprintf (stream
, " vptridx=%s",
9005 expr_as_string (BINFO_VPTR_INDEX (binfo
),
9006 TFF_PLAIN_IDENTIFIER
));
9008 if (BINFO_VPTR_FIELD (binfo
))
9010 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9011 fprintf (stream
, " vbaseoffset=%s",
9012 expr_as_string (BINFO_VPTR_FIELD (binfo
),
9013 TFF_PLAIN_IDENTIFIER
));
9015 if (BINFO_VTABLE (binfo
))
9017 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
9018 fprintf (stream
, " vptr=%s",
9019 expr_as_string (BINFO_VTABLE (binfo
),
9020 TFF_PLAIN_IDENTIFIER
));
9024 fprintf (stream
, "\n");
9027 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9028 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
9033 /* Dump the BINFO hierarchy for T. */
9036 dump_class_hierarchy_1 (FILE *stream
, dump_flags_t flags
, tree t
)
9038 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9039 fprintf (stream
, " size=%lu align=%lu\n",
9040 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
9041 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
9042 if (tree as_base
= CLASSTYPE_AS_BASE (t
))
9043 fprintf (stream
, " base size=%lu base align=%lu\n",
9044 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base
))
9046 (unsigned long)(TYPE_ALIGN (as_base
) / BITS_PER_UNIT
));
9047 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
9048 fprintf (stream
, "\n");
9051 /* Debug interface to hierarchy dumping. */
9054 debug_class (tree t
)
9056 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
9060 dump_class_hierarchy (tree t
)
9063 if (FILE *stream
= dump_begin (class_dump_id
, &flags
))
9065 dump_class_hierarchy_1 (stream
, flags
, t
);
9066 dump_end (class_dump_id
, stream
);
9071 dump_array (FILE * stream
, tree decl
)
9074 unsigned HOST_WIDE_INT ix
;
9076 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
9078 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
9080 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
9081 fprintf (stream
, " %s entries",
9082 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
9083 TFF_PLAIN_IDENTIFIER
));
9084 fprintf (stream
, "\n");
9086 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
9088 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
9089 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
9093 dump_vtable (tree t
, tree binfo
, tree vtable
)
9096 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9101 if (!(flags
& TDF_SLIM
))
9103 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
9105 fprintf (stream
, "%s for %s",
9106 ctor_vtbl_p
? "Construction vtable" : "Vtable",
9107 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
9110 if (!BINFO_VIRTUAL_P (binfo
))
9111 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
9112 (HOST_WIDE_INT
) (uintptr_t) binfo
);
9113 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9115 fprintf (stream
, "\n");
9116 dump_array (stream
, vtable
);
9117 fprintf (stream
, "\n");
9120 dump_end (class_dump_id
, stream
);
9124 dump_vtt (tree t
, tree vtt
)
9127 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9132 if (!(flags
& TDF_SLIM
))
9134 fprintf (stream
, "VTT for %s\n",
9135 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9136 dump_array (stream
, vtt
);
9137 fprintf (stream
, "\n");
9140 dump_end (class_dump_id
, stream
);
9143 /* Dump a function or thunk and its thunkees. */
9146 dump_thunk (FILE *stream
, int indent
, tree thunk
)
9148 static const char spaces
[] = " ";
9149 tree name
= DECL_NAME (thunk
);
9152 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
9154 !DECL_THUNK_P (thunk
) ? "function"
9155 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
9156 name
? IDENTIFIER_POINTER (name
) : "<unset>");
9157 if (DECL_THUNK_P (thunk
))
9159 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
9160 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
9162 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
9163 if (!virtual_adjust
)
9165 else if (DECL_THIS_THUNK_P (thunk
))
9166 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
9167 tree_to_shwi (virtual_adjust
));
9169 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
9170 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
9171 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
9172 if (THUNK_ALIAS (thunk
))
9173 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
9175 fprintf (stream
, "\n");
9176 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
9177 dump_thunk (stream
, indent
+ 2, thunks
);
9180 /* Dump the thunks for FN. */
9183 debug_thunks (tree fn
)
9185 dump_thunk (stderr
, 0, fn
);
9188 /* Virtual function table initialization. */
9190 /* Create all the necessary vtables for T and its base classes. */
9193 finish_vtbls (tree t
)
9196 vec
<constructor_elt
, va_gc
> *v
= NULL
;
9197 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
9199 /* We lay out the primary and secondary vtables in one contiguous
9200 vtable. The primary vtable is first, followed by the non-virtual
9201 secondary vtables in inheritance graph order. */
9202 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
9205 /* Then come the virtual bases, also in inheritance graph order. */
9206 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
9208 if (!BINFO_VIRTUAL_P (vbase
))
9210 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
9213 if (BINFO_VTABLE (TYPE_BINFO (t
)))
9214 initialize_vtable (TYPE_BINFO (t
), v
);
9217 /* Initialize the vtable for BINFO with the INITS. */
9220 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
9224 layout_vtable_decl (binfo
, vec_safe_length (inits
));
9225 decl
= get_vtbl_decl_for_binfo (binfo
);
9226 initialize_artificial_var (decl
, inits
);
9227 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
9230 /* Build the VTT (virtual table table) for T.
9231 A class requires a VTT if it has virtual bases.
9234 1 - primary virtual pointer for complete object T
9235 2 - secondary VTTs for each direct non-virtual base of T which requires a
9237 3 - secondary virtual pointers for each direct or indirect base of T which
9238 has virtual bases or is reachable via a virtual path from T.
9239 4 - secondary VTTs for each direct or indirect virtual base of T.
9241 Secondary VTTs look like complete object VTTs without part 4. */
9249 vec
<constructor_elt
, va_gc
> *inits
;
9251 /* Build up the initializers for the VTT. */
9253 index
= size_zero_node
;
9254 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
9256 /* If we didn't need a VTT, we're done. */
9260 /* Figure out the type of the VTT. */
9261 type
= build_array_of_n_type (const_ptr_type_node
,
9264 /* Now, build the VTT object itself. */
9265 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
9266 initialize_artificial_var (vtt
, inits
);
9267 /* Add the VTT to the vtables list. */
9268 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
9269 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
9274 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9275 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9276 and CHAIN the vtable pointer for this binfo after construction is
9277 complete. VALUE can also be another BINFO, in which case we recurse. */
9280 binfo_ctor_vtable (tree binfo
)
9286 vt
= BINFO_VTABLE (binfo
);
9287 if (TREE_CODE (vt
) == TREE_LIST
)
9288 vt
= TREE_VALUE (vt
);
9289 if (TREE_CODE (vt
) == TREE_BINFO
)
9298 /* Data for secondary VTT initialization. */
9299 struct secondary_vptr_vtt_init_data
9301 /* Is this the primary VTT? */
9304 /* Current index into the VTT. */
9307 /* Vector of initializers built up. */
9308 vec
<constructor_elt
, va_gc
> *inits
;
9310 /* The type being constructed by this secondary VTT. */
9311 tree type_being_constructed
;
9314 /* Recursively build the VTT-initializer for BINFO (which is in the
9315 hierarchy dominated by T). INITS points to the end of the initializer
9316 list to date. INDEX is the VTT index where the next element will be
9317 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9318 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9319 for virtual bases of T. When it is not so, we build the constructor
9320 vtables for the BINFO-in-T variant. */
9323 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
9329 secondary_vptr_vtt_init_data data
;
9330 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9332 /* We only need VTTs for subobjects with virtual bases. */
9333 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9336 /* We need to use a construction vtable if this is not the primary
9340 build_ctor_vtbl_group (binfo
, t
);
9342 /* Record the offset in the VTT where this sub-VTT can be found. */
9343 BINFO_SUBVTT_INDEX (binfo
) = *index
;
9346 /* Add the address of the primary vtable for the complete object. */
9347 init
= binfo_ctor_vtable (binfo
);
9348 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9351 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9352 BINFO_VPTR_INDEX (binfo
) = *index
;
9354 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
9356 /* Recursively add the secondary VTTs for non-virtual bases. */
9357 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
9358 if (!BINFO_VIRTUAL_P (b
))
9359 build_vtt_inits (b
, t
, inits
, index
);
9361 /* Add secondary virtual pointers for all subobjects of BINFO with
9362 either virtual bases or reachable along a virtual path, except
9363 subobjects that are non-virtual primary bases. */
9364 data
.top_level_p
= top_level_p
;
9365 data
.index
= *index
;
9366 data
.inits
= *inits
;
9367 data
.type_being_constructed
= BINFO_TYPE (binfo
);
9369 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
9371 *index
= data
.index
;
9373 /* data.inits might have grown as we added secondary virtual pointers.
9374 Make sure our caller knows about the new vector. */
9375 *inits
= data
.inits
;
9378 /* Add the secondary VTTs for virtual bases in inheritance graph
9380 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
9382 if (!BINFO_VIRTUAL_P (b
))
9385 build_vtt_inits (b
, t
, inits
, index
);
9388 /* Remove the ctor vtables we created. */
9389 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
9392 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9393 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9396 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
9398 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
9400 /* We don't care about bases that don't have vtables. */
9401 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
9402 return dfs_skip_bases
;
9404 /* We're only interested in proper subobjects of the type being
9406 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
9409 /* We're only interested in bases with virtual bases or reachable
9410 via a virtual path from the type being constructed. */
9411 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9412 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
9413 return dfs_skip_bases
;
9415 /* We're not interested in non-virtual primary bases. */
9416 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
9419 /* Record the index where this secondary vptr can be found. */
9420 if (data
->top_level_p
)
9422 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9423 BINFO_VPTR_INDEX (binfo
) = data
->index
;
9425 if (BINFO_VIRTUAL_P (binfo
))
9427 /* It's a primary virtual base, and this is not a
9428 construction vtable. Find the base this is primary of in
9429 the inheritance graph, and use that base's vtable
9431 while (BINFO_PRIMARY_P (binfo
))
9432 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
9436 /* Add the initializer for the secondary vptr itself. */
9437 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
9439 /* Advance the vtt index. */
9440 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
9441 TYPE_SIZE_UNIT (ptr_type_node
));
9446 /* Called from build_vtt_inits via dfs_walk. After building
9447 constructor vtables and generating the sub-vtt from them, we need
9448 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9449 binfo of the base whose sub vtt was generated. */
9452 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9454 tree vtable
= BINFO_VTABLE (binfo
);
9456 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9457 /* If this class has no vtable, none of its bases do. */
9458 return dfs_skip_bases
;
9461 /* This might be a primary base, so have no vtable in this
9465 /* If we scribbled the construction vtable vptr into BINFO, clear it
9467 if (TREE_CODE (vtable
) == TREE_LIST
9468 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9469 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9474 /* Build the construction vtable group for BINFO which is in the
9475 hierarchy dominated by T. */
9478 build_ctor_vtbl_group (tree binfo
, tree t
)
9484 vec
<constructor_elt
, va_gc
> *v
;
9486 /* See if we've already created this construction vtable group. */
9487 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9488 if (get_global_binding (id
))
9491 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9492 /* Build a version of VTBL (with the wrong type) for use in
9493 constructing the addresses of secondary vtables in the
9494 construction vtable group. */
9495 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9497 /* Don't export construction vtables from shared libraries. Even on
9498 targets that don't support hidden visibility, this tells
9499 can_refer_decl_in_current_unit_p not to assume that it's safe to
9500 access from a different compilation unit (bz 54314). */
9501 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9502 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9505 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9506 binfo
, vtbl
, t
, &v
);
9508 /* Add the vtables for each of our virtual bases using the vbase in T
9510 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9512 vbase
= TREE_CHAIN (vbase
))
9516 if (!BINFO_VIRTUAL_P (vbase
))
9518 b
= copied_binfo (vbase
, binfo
);
9520 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9523 /* Figure out the type of the construction vtable. */
9524 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9526 TREE_TYPE (vtbl
) = type
;
9527 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9528 layout_decl (vtbl
, 0);
9530 /* Initialize the construction vtable. */
9531 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9532 initialize_artificial_var (vtbl
, v
);
9533 dump_vtable (t
, binfo
, vtbl
);
9536 /* Add the vtbl initializers for BINFO (and its bases other than
9537 non-virtual primaries) to the list of INITS. BINFO is in the
9538 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9539 the constructor the vtbl inits should be accumulated for. (If this
9540 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9541 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9542 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9543 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9544 but are not necessarily the same in terms of layout. */
9547 accumulate_vtbl_inits (tree binfo
,
9552 vec
<constructor_elt
, va_gc
> **inits
)
9556 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9558 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9560 /* If it doesn't have a vptr, we don't do anything. */
9561 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9564 /* If we're building a construction vtable, we're not interested in
9565 subobjects that don't require construction vtables. */
9567 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9568 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9571 /* Build the initializers for the BINFO-in-T vtable. */
9572 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9574 /* Walk the BINFO and its bases. We walk in preorder so that as we
9575 initialize each vtable we can figure out at what offset the
9576 secondary vtable lies from the primary vtable. We can't use
9577 dfs_walk here because we need to iterate through bases of BINFO
9578 and RTTI_BINFO simultaneously. */
9579 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9581 /* Skip virtual bases. */
9582 if (BINFO_VIRTUAL_P (base_binfo
))
9584 accumulate_vtbl_inits (base_binfo
,
9585 BINFO_BASE_BINFO (orig_binfo
, i
),
9586 rtti_binfo
, vtbl
, t
,
9591 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9592 BINFO vtable to L. */
9595 dfs_accumulate_vtbl_inits (tree binfo
,
9600 vec
<constructor_elt
, va_gc
> **l
)
9602 tree vtbl
= NULL_TREE
;
9603 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9607 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9609 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9610 primary virtual base. If it is not the same primary in
9611 the hierarchy of T, we'll need to generate a ctor vtable
9612 for it, to place at its location in T. If it is the same
9613 primary, we still need a VTT entry for the vtable, but it
9614 should point to the ctor vtable for the base it is a
9615 primary for within the sub-hierarchy of RTTI_BINFO.
9617 There are three possible cases:
9619 1) We are in the same place.
9620 2) We are a primary base within a lost primary virtual base of
9622 3) We are primary to something not a base of RTTI_BINFO. */
9625 tree last
= NULL_TREE
;
9627 /* First, look through the bases we are primary to for RTTI_BINFO
9628 or a virtual base. */
9630 while (BINFO_PRIMARY_P (b
))
9632 b
= BINFO_INHERITANCE_CHAIN (b
);
9634 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9637 /* If we run out of primary links, keep looking down our
9638 inheritance chain; we might be an indirect primary. */
9639 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9640 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9644 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9645 base B and it is a base of RTTI_BINFO, this is case 2. In
9646 either case, we share our vtable with LAST, i.e. the
9647 derived-most base within B of which we are a primary. */
9649 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9650 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9651 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9652 binfo_ctor_vtable after everything's been set up. */
9655 /* Otherwise, this is case 3 and we get our own. */
9657 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9660 n_inits
= vec_safe_length (*l
);
9667 /* Add the initializer for this vtable. */
9668 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9669 &non_fn_entries
, l
);
9671 /* Figure out the position to which the VPTR should point. */
9672 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9673 index
= size_binop (MULT_EXPR
,
9674 TYPE_SIZE_UNIT (vtable_entry_type
),
9675 size_int (non_fn_entries
+ n_inits
));
9676 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9680 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9681 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9682 straighten this out. */
9683 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9684 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9685 /* Throw away any unneeded intializers. */
9686 (*l
)->truncate (n_inits
);
9688 /* For an ordinary vtable, set BINFO_VTABLE. */
9689 BINFO_VTABLE (binfo
) = vtbl
;
9692 static GTY(()) tree abort_fndecl_addr
;
9693 static GTY(()) tree dvirt_fn
;
9695 /* Construct the initializer for BINFO's virtual function table. BINFO
9696 is part of the hierarchy dominated by T. If we're building a
9697 construction vtable, the ORIG_BINFO is the binfo we should use to
9698 find the actual function pointers to put in the vtable - but they
9699 can be overridden on the path to most-derived in the graph that
9700 ORIG_BINFO belongs. Otherwise,
9701 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9702 BINFO that should be indicated by the RTTI information in the
9703 vtable; it will be a base class of T, rather than T itself, if we
9704 are building a construction vtable.
9706 The value returned is a TREE_LIST suitable for wrapping in a
9707 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9708 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9709 number of non-function entries in the vtable.
9711 It might seem that this function should never be called with a
9712 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9713 base is always subsumed by a derived class vtable. However, when
9714 we are building construction vtables, we do build vtables for
9715 primary bases; we need these while the primary base is being
9719 build_vtbl_initializer (tree binfo
,
9723 int* non_fn_entries_p
,
9724 vec
<constructor_elt
, va_gc
> **inits
)
9730 vec
<tree
, va_gc
> *vbases
;
9733 /* Initialize VID. */
9734 memset (&vid
, 0, sizeof (vid
));
9737 vid
.rtti_binfo
= rtti_binfo
;
9738 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9739 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9740 vid
.generate_vcall_entries
= true;
9741 /* The first vbase or vcall offset is at index -3 in the vtable. */
9742 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
9744 /* Add entries to the vtable for RTTI. */
9745 build_rtti_vtbl_entries (binfo
, &vid
);
9747 /* Create an array for keeping track of the functions we've
9748 processed. When we see multiple functions with the same
9749 signature, we share the vcall offsets. */
9750 vec_alloc (vid
.fns
, 32);
9751 /* Add the vcall and vbase offset entries. */
9752 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
9754 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9755 build_vbase_offset_vtbl_entries. */
9756 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
9757 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
9758 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
9760 /* If the target requires padding between data entries, add that now. */
9761 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
9763 int n_entries
= vec_safe_length (vid
.inits
);
9765 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
,
9768 /* Move data entries into their new positions and add padding
9769 after the new positions. Iterate backwards so we don't
9770 overwrite entries that we would need to process later. */
9771 for (ix
= n_entries
- 1;
9772 vid
.inits
->iterate (ix
, &e
);
9776 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
9777 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
9779 (*vid
.inits
)[new_position
] = *e
;
9781 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
9783 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
9784 f
->index
= NULL_TREE
;
9785 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9791 if (non_fn_entries_p
)
9792 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9794 /* The initializers for virtual functions were built up in reverse
9795 order. Straighten them out and add them to the running list in one
9797 jx
= vec_safe_length (*inits
);
9798 vec_safe_grow (*inits
, jx
+ vid
.inits
->length (), true);
9800 for (ix
= vid
.inits
->length () - 1;
9801 vid
.inits
->iterate (ix
, &e
);
9805 /* Go through all the ordinary virtual functions, building up
9807 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9811 tree fn
, fn_original
;
9812 tree init
= NULL_TREE
;
9816 if (DECL_THUNK_P (fn
))
9818 if (!DECL_NAME (fn
))
9820 if (THUNK_ALIAS (fn
))
9822 fn
= THUNK_ALIAS (fn
);
9825 fn_original
= THUNK_TARGET (fn
);
9828 /* If the only definition of this function signature along our
9829 primary base chain is from a lost primary, this vtable slot will
9830 never be used, so just zero it out. This is important to avoid
9831 requiring extra thunks which cannot be generated with the function.
9833 We first check this in update_vtable_entry_for_fn, so we handle
9834 restored primary bases properly; we also need to do it here so we
9835 zero out unused slots in ctor vtables, rather than filling them
9836 with erroneous values (though harmless, apart from relocation
9838 if (BV_LOST_PRIMARY (v
))
9839 init
= size_zero_node
;
9843 /* Pull the offset for `this', and the function to call, out of
9845 delta
= BV_DELTA (v
);
9846 vcall_index
= BV_VCALL_INDEX (v
);
9848 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9849 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9851 /* You can't call an abstract virtual function; it's abstract.
9852 So, we replace these functions with __pure_virtual. */
9853 if (DECL_PURE_VIRTUAL_P (fn_original
))
9856 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9858 if (abort_fndecl_addr
== NULL
)
9860 = fold_convert (vfunc_ptr_type_node
,
9861 build_fold_addr_expr (fn
));
9862 init
= abort_fndecl_addr
;
9865 /* Likewise for deleted virtuals. */
9866 else if (DECL_DELETED_FN (fn_original
))
9870 tree name
= get_identifier ("__cxa_deleted_virtual");
9871 dvirt_fn
= get_global_binding (name
);
9873 dvirt_fn
= push_library_fn
9875 build_function_type_list (void_type_node
, NULL_TREE
),
9876 NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
9879 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9880 init
= fold_convert (vfunc_ptr_type_node
,
9881 build_fold_addr_expr (fn
));
9885 if (!integer_zerop (delta
) || vcall_index
)
9887 fn
= make_thunk (fn
, /*this_adjusting=*/1,
9888 delta
, vcall_index
);
9889 if (!DECL_NAME (fn
))
9892 /* Take the address of the function, considering it to be of an
9893 appropriate generic type. */
9894 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9895 init
= fold_convert (vfunc_ptr_type_node
,
9896 build_fold_addr_expr (fn
));
9897 /* Don't refer to a virtual destructor from a constructor
9898 vtable or a vtable for an abstract class, since destroying
9899 an object under construction is undefined behavior and we
9900 don't want it to be considered a candidate for speculative
9901 devirtualization. But do create the thunk for ABI
9903 if (DECL_DESTRUCTOR_P (fn_original
)
9904 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9905 || orig_binfo
!= binfo
))
9906 init
= size_zero_node
;
9910 /* And add it to the chain of initializers. */
9911 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9914 if (init
== size_zero_node
)
9915 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9916 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9918 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9920 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9921 fn
, build_int_cst (NULL_TREE
, i
));
9922 TREE_CONSTANT (fdesc
) = 1;
9924 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), fdesc
);
9928 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9932 /* Adds to vid->inits the initializers for the vbase and vcall
9933 offsets in BINFO, which is in the hierarchy dominated by T. */
9936 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9940 /* If this is a derived class, we must first create entries
9941 corresponding to the primary base class. */
9942 b
= get_primary_binfo (binfo
);
9944 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9946 /* Add the vbase entries for this base. */
9947 build_vbase_offset_vtbl_entries (binfo
, vid
);
9948 /* Add the vcall entries for this base. */
9949 build_vcall_offset_vtbl_entries (binfo
, vid
);
9952 /* Returns the initializers for the vbase offset entries in the vtable
9953 for BINFO (which is part of the class hierarchy dominated by T), in
9954 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9955 where the next vbase offset will go. */
9958 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9962 tree non_primary_binfo
;
9964 /* If there are no virtual baseclasses, then there is nothing to
9966 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9971 /* We might be a primary base class. Go up the inheritance hierarchy
9972 until we find the most derived class of which we are a primary base:
9973 it is the offset of that which we need to use. */
9974 non_primary_binfo
= binfo
;
9975 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9979 /* If we have reached a virtual base, then it must be a primary
9980 base (possibly multi-level) of vid->binfo, or we wouldn't
9981 have called build_vcall_and_vbase_vtbl_entries for it. But it
9982 might be a lost primary, so just skip down to vid->binfo. */
9983 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9985 non_primary_binfo
= vid
->binfo
;
9989 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9990 if (get_primary_binfo (b
) != non_primary_binfo
)
9992 non_primary_binfo
= b
;
9995 /* Go through the virtual bases, adding the offsets. */
9996 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9998 vbase
= TREE_CHAIN (vbase
))
10003 if (!BINFO_VIRTUAL_P (vbase
))
10006 /* Find the instance of this virtual base in the complete
10008 b
= copied_binfo (vbase
, binfo
);
10010 /* If we've already got an offset for this virtual base, we
10011 don't need another one. */
10012 if (BINFO_VTABLE_PATH_MARKED (b
))
10014 BINFO_VTABLE_PATH_MARKED (b
) = 1;
10016 /* Figure out where we can find this vbase offset. */
10017 delta
= size_binop (MULT_EXPR
,
10019 fold_convert (ssizetype
,
10020 TYPE_SIZE_UNIT (vtable_entry_type
)));
10021 if (vid
->primary_vtbl_p
)
10022 BINFO_VPTR_FIELD (b
) = delta
;
10024 if (binfo
!= TYPE_BINFO (t
))
10025 /* The vbase offset had better be the same. */
10026 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
10028 /* The next vbase will come at a more negative offset. */
10029 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10030 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10032 /* The initializer is the delta from BINFO to this virtual base.
10033 The vbase offsets go in reverse inheritance-graph order, and
10034 we are walking in inheritance graph order so these end up in
10035 the right order. */
10036 delta
= size_diffop_loc (input_location
,
10037 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
10039 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
10040 fold_build1_loc (input_location
, NOP_EXPR
,
10041 vtable_entry_type
, delta
));
10045 /* Adds the initializers for the vcall offset entries in the vtable
10046 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10050 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10052 /* We only need these entries if this base is a virtual base. We
10053 compute the indices -- but do not add to the vtable -- when
10054 building the main vtable for a class. */
10055 if (binfo
== TYPE_BINFO (vid
->derived
)
10056 || (BINFO_VIRTUAL_P (binfo
)
10057 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10058 correspond to VID->DERIVED), we are building a primary
10059 construction virtual table. Since this is a primary
10060 virtual table, we do not need the vcall offsets for
10062 && binfo
!= vid
->rtti_binfo
))
10064 /* We need a vcall offset for each of the virtual functions in this
10065 vtable. For example:
10067 class A { virtual void f (); };
10068 class B1 : virtual public A { virtual void f (); };
10069 class B2 : virtual public A { virtual void f (); };
10070 class C: public B1, public B2 { virtual void f (); };
10072 A C object has a primary base of B1, which has a primary base of A. A
10073 C also has a secondary base of B2, which no longer has a primary base
10074 of A. So the B2-in-C construction vtable needs a secondary vtable for
10075 A, which will adjust the A* to a B2* to call f. We have no way of
10076 knowing what (or even whether) this offset will be when we define B2,
10077 so we store this "vcall offset" in the A sub-vtable and look it up in
10078 a "virtual thunk" for B2::f.
10080 We need entries for all the functions in our primary vtable and
10081 in our non-virtual bases' secondary vtables. */
10082 vid
->vbase
= binfo
;
10083 /* If we are just computing the vcall indices -- but do not need
10084 the actual entries -- not that. */
10085 if (!BINFO_VIRTUAL_P (binfo
))
10086 vid
->generate_vcall_entries
= false;
10087 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10088 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
10092 /* Build vcall offsets, starting with those for BINFO. */
10095 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
10098 tree primary_binfo
;
10101 /* Don't walk into virtual bases -- except, of course, for the
10102 virtual base for which we are building vcall offsets. Any
10103 primary virtual base will have already had its offsets generated
10104 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10105 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
10108 /* If BINFO has a primary base, process it first. */
10109 primary_binfo
= get_primary_binfo (binfo
);
10111 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
10113 /* Add BINFO itself to the list. */
10114 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
10116 /* Scan the non-primary bases of BINFO. */
10117 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
10118 if (base_binfo
!= primary_binfo
)
10119 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
10122 /* Called from build_vcall_offset_vtbl_entries_r. */
10125 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
10127 /* Make entries for the rest of the virtuals. */
10130 /* The ABI requires that the methods be processed in declaration
10132 for (orig_fn
= TYPE_FIELDS (BINFO_TYPE (binfo
));
10134 orig_fn
= DECL_CHAIN (orig_fn
))
10135 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
10136 add_vcall_offset (orig_fn
, binfo
, vid
);
10139 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10142 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
10146 tree derived_entry
;
10148 /* If there is already an entry for a function with the same
10149 signature as FN, then we do not need a second vcall offset.
10150 Check the list of functions already present in the derived
10152 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
10154 if (same_signature_p (derived_entry
, orig_fn
)
10155 /* We only use one vcall offset for virtual destructors,
10156 even though there are two virtual table entries. */
10157 || (DECL_DESTRUCTOR_P (derived_entry
)
10158 && DECL_DESTRUCTOR_P (orig_fn
)))
10162 /* If we are building these vcall offsets as part of building
10163 the vtable for the most derived class, remember the vcall
10165 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
10167 tree_pair_s elt
= {orig_fn
, vid
->index
};
10168 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
10171 /* The next vcall offset will be found at a more negative
10173 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10174 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10176 /* Keep track of this function. */
10177 vec_safe_push (vid
->fns
, orig_fn
);
10179 if (vid
->generate_vcall_entries
)
10184 /* Find the overriding function. */
10185 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
10186 if (fn
== error_mark_node
)
10187 vcall_offset
= build_zero_cst (vtable_entry_type
);
10190 base
= TREE_VALUE (fn
);
10192 /* The vbase we're working on is a primary base of
10193 vid->binfo. But it might be a lost primary, so its
10194 BINFO_OFFSET might be wrong, so we just use the
10195 BINFO_OFFSET from vid->binfo. */
10196 vcall_offset
= size_diffop_loc (input_location
,
10197 BINFO_OFFSET (base
),
10198 BINFO_OFFSET (vid
->binfo
));
10199 vcall_offset
= fold_build1_loc (input_location
,
10200 NOP_EXPR
, vtable_entry_type
,
10203 /* Add the initializer to the vtable. */
10204 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
10208 /* Return vtbl initializers for the RTTI entries corresponding to the
10209 BINFO's vtable. The RTTI entries should indicate the object given
10210 by VID->rtti_binfo. */
10213 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10221 t
= BINFO_TYPE (vid
->rtti_binfo
);
10223 /* To find the complete object, we will first convert to our most
10224 primary base, and then add the offset in the vtbl to that value. */
10225 b
= most_primary_binfo (binfo
);
10226 offset
= size_diffop_loc (input_location
,
10227 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
10229 /* The second entry is the address of the typeinfo object. */
10231 decl
= build_address (get_tinfo_decl (t
));
10233 decl
= integer_zero_node
;
10235 /* Convert the declaration to a type that can be stored in the
10237 init
= build_nop (vfunc_ptr_type_node
, decl
);
10238 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10240 /* Add the offset-to-top entry. It comes earlier in the vtable than
10241 the typeinfo entry. Convert the offset to look like a
10242 function pointer, so that we can put it in the vtable. */
10243 init
= build_nop (vfunc_ptr_type_node
, offset
);
10244 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10247 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10251 uniquely_derived_from_p (tree parent
, tree type
)
10253 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
10254 return base
&& base
!= error_mark_node
;
10257 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10260 publicly_uniquely_derived_p (tree parent
, tree type
)
10262 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
10264 return base
&& base
!= error_mark_node
;
10267 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10268 class between them, if any. */
10271 common_enclosing_class (tree ctx1
, tree ctx2
)
10273 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
10275 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
10276 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
10279 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10280 TYPE_MARKED_P (t
) = true;
10281 tree found
= NULL_TREE
;
10282 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10283 if (TYPE_MARKED_P (t
))
10288 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10289 TYPE_MARKED_P (t
) = false;
10293 #include "gt-cp-class.h"