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 (noncapability_type (TREE_TYPE (expr
)),
409 nullptr_node
, complain
);
410 null_test
= build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
411 drop_capability (expr
), zero
);
412 /* This is a compiler generated comparison, don't emit
413 e.g. -Wnonnull-compare warning for it. */
414 TREE_NO_WARNING (null_test
) = 1;
417 /* If this is a simple base reference, express it as a COMPONENT_REF. */
418 if (code
== PLUS_EXPR
&& !virtual_access
419 /* We don't build base fields for empty bases, and they aren't very
420 interesting to the optimizers anyway. */
423 expr
= cp_build_fold_indirect_ref (expr
);
424 expr
= build_simple_base_path (expr
, binfo
);
425 if (rvalue
&& lvalue_p (expr
))
428 expr
= build_address (expr
);
429 target_type
= TREE_TYPE (expr
);
435 /* Going via virtual base V_BINFO. We need the static offset
436 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
437 V_BINFO. That offset is an entry in D_BINFO's vtable. */
440 if (fixed_type_p
< 0 && in_base_initializer
)
442 /* In a base member initializer, we cannot rely on the
443 vtable being set up. We have to indirect via the
447 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
448 t
= build_pointer_type (t
);
449 v_offset
= fold_convert (t
, current_vtt_parm
);
450 v_offset
= cp_build_fold_indirect_ref (v_offset
);
455 if (sanitize_flags_p (SANITIZE_VPTR
)
456 && fixed_type_p
== 0)
458 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
463 v_offset
= build_vfield_ref (cp_build_fold_indirect_ref (t
),
464 TREE_TYPE (TREE_TYPE (expr
)));
467 if (v_offset
== error_mark_node
)
468 return error_mark_node
;
470 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
471 v_offset
= build1 (NOP_EXPR
,
472 build_pointer_type (ptrdiff_type_node
),
474 v_offset
= cp_build_fold_indirect_ref (v_offset
);
475 TREE_CONSTANT (v_offset
) = 1;
477 offset
= convert_to_integer (ptrdiff_type_node
,
478 size_diffop_loc (input_location
, offset
,
479 BINFO_OFFSET (v_binfo
)));
481 if (!integer_zerop (offset
))
482 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
484 if (fixed_type_p
< 0)
485 /* Negative fixed_type_p means this is a constructor or destructor;
486 virtual base layout is fixed in in-charge [cd]tors, but not in
488 offset
= build_if_in_charge
489 (convert_to_integer (ptrdiff_type_node
, BINFO_OFFSET (binfo
)),
496 target_type
= ptr_target_type
;
498 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
500 if (!integer_zerop (offset
))
502 offset
= fold_convert (sizetype
, offset
);
503 if (code
== MINUS_EXPR
)
504 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
505 expr
= fold_build_pointer_plus (expr
, offset
);
513 expr
= cp_build_fold_indirect_ref (expr
);
521 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
,
522 expr
, build_zero_cst (target_type
));
523 /* Avoid warning for the whole conditional expression (in addition
524 to NULL_TEST itself -- see above) in case the result is used in
525 a nonnull context that the front end -Wnonnull checks. */
526 TREE_NO_WARNING (expr
) = 1;
532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
533 Perform a derived-to-base conversion by recursively building up a
534 sequence of COMPONENT_REFs to the appropriate base fields. */
537 build_simple_base_path (tree expr
, tree binfo
)
539 tree type
= BINFO_TYPE (binfo
);
540 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
543 if (d_binfo
== NULL_TREE
)
547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
551 an lvalue in the front end; only _DECLs and _REFs are lvalues
553 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
555 expr
= cp_build_fold_indirect_ref (temp
);
561 expr
= build_simple_base_path (expr
, d_binfo
);
563 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
564 field
; field
= DECL_CHAIN (field
))
565 /* Is this the base field created by build_base_field? */
566 if (TREE_CODE (field
) == FIELD_DECL
567 && DECL_FIELD_IS_BASE (field
)
568 && TREE_TYPE (field
) == type
569 /* If we're looking for a field in the most-derived class,
570 also check the field offset; we can have two base fields
571 of the same type if one is an indirect virtual base and one
572 is a direct non-virtual base. */
573 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
574 || tree_int_cst_equal (byte_position (field
),
575 BINFO_OFFSET (binfo
))))
577 /* We don't use build_class_member_access_expr here, as that
578 has unnecessary checks, and more importantly results in
579 recursive calls to dfs_walk_once. */
580 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
582 expr
= build3 (COMPONENT_REF
,
583 cp_build_qualified_type (type
, type_quals
),
584 expr
, field
, NULL_TREE
);
585 /* Mark the expression const or volatile, as appropriate.
586 Even though we've dealt with the type above, we still have
587 to mark the expression itself. */
588 if (type_quals
& TYPE_QUAL_CONST
)
589 TREE_READONLY (expr
) = 1;
590 if (type_quals
& TYPE_QUAL_VOLATILE
)
591 TREE_THIS_VOLATILE (expr
) = 1;
596 /* Didn't find the base field?!? */
600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
601 type is a class type or a pointer to a class type. In the former
602 case, TYPE is also a class type; in the latter it is another
603 pointer type. If CHECK_ACCESS is true, an error message is emitted
604 if TYPE is inaccessible. If OBJECT has pointer type, the value is
605 assumed to be non-NULL. */
608 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
609 tsubst_flags_t complain
)
614 if (TYPE_PTR_P (TREE_TYPE (object
)))
616 object_type
= TREE_TYPE (TREE_TYPE (object
));
617 type
= TREE_TYPE (type
);
620 object_type
= TREE_TYPE (object
);
622 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
624 if (!binfo
|| binfo
== error_mark_node
)
625 return error_mark_node
;
627 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
630 /* EXPR is an expression with unqualified class type. BASE is a base
631 binfo of that class type. Returns EXPR, converted to the BASE
632 type. This function assumes that EXPR is the most derived class;
633 therefore virtual bases can be found at their static offsets. */
636 convert_to_base_statically (tree expr
, tree base
)
640 expr_type
= TREE_TYPE (expr
);
641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
643 /* If this is a non-empty base, use a COMPONENT_REF. */
644 if (!is_empty_class (BINFO_TYPE (base
)))
645 return build_simple_base_path (expr
, base
);
647 /* We use fold_build2 and fold_convert below to simplify the trees
648 provided to the optimizers. It is not safe to call these functions
649 when processing a template because they do not handle C++-specific
651 gcc_assert (!processing_template_decl
);
652 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
653 if (!integer_zerop (BINFO_OFFSET (base
)))
654 expr
= fold_build_pointer_plus_loc (input_location
,
655 expr
, BINFO_OFFSET (base
));
656 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
657 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
665 build_vfield_ref (tree datum
, tree type
)
667 tree vfield
, vcontext
;
669 if (datum
== error_mark_node
670 /* Can happen in case of duplicate base types (c++/59082). */
671 || !TYPE_VFIELD (type
))
672 return error_mark_node
;
674 /* First, convert to the requested type. */
675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
676 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
677 /*nonnull=*/true, tf_warning_or_error
);
679 /* Second, the requested type may not be the owner of its own vptr.
680 If not, convert to the base class that owns it. We cannot use
681 convert_to_base here, because VCONTEXT may appear more than once
682 in the inheritance hierarchy of TYPE, and thus direct conversion
683 between the types may be ambiguous. Following the path back up
684 one step at a time via primary bases avoids the problem. */
685 vfield
= TYPE_VFIELD (type
);
686 vcontext
= DECL_CONTEXT (vfield
);
687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
689 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
690 type
= TREE_TYPE (datum
);
693 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
696 /* Given an object INSTANCE, return an expression which yields the
697 vtable element corresponding to INDEX. There are many special
698 cases for INSTANCE which we take care of here, mainly to avoid
699 creating extra tree nodes when we don't have to. */
702 build_vtbl_ref (tree instance
, tree idx
)
705 tree vtbl
= NULL_TREE
;
707 /* Try to figure out what a reference refers to, and
708 access its virtual function table directly. */
711 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
713 tree basetype
= non_reference (TREE_TYPE (instance
));
715 if (fixed_type
&& !cdtorp
)
717 tree binfo
= lookup_base (fixed_type
, basetype
,
718 ba_unique
, NULL
, tf_none
);
719 if (binfo
&& binfo
!= error_mark_node
)
720 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
724 vtbl
= build_vfield_ref (instance
, basetype
);
726 aref
= build_array_ref (input_location
, vtbl
, idx
);
727 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
732 /* Given a stable object pointer INSTANCE_PTR, return an expression which
733 yields a function pointer corresponding to vtable element INDEX. */
736 build_vfn_ref (tree instance_ptr
, tree idx
)
740 aref
= build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr
), idx
);
742 /* When using function descriptors, the address of the
743 vtable entry is treated as a function pointer. */
744 if (TARGET_VTABLE_USES_DESCRIPTORS
)
745 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
746 cp_build_addr_expr (aref
, tf_warning_or_error
));
748 /* Remember this as a method reference, for later devirtualization. */
749 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
754 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
755 for the given TYPE. */
758 get_vtable_name (tree type
)
760 return mangle_vtbl_for_type (type
);
763 /* DECL is an entity associated with TYPE, like a virtual table or an
764 implicitly generated constructor. Determine whether or not DECL
765 should have external or internal linkage at the object file
766 level. This routine does not deal with COMDAT linkage and other
767 similar complexities; it simply sets TREE_PUBLIC if it possible for
768 entities in other translation units to contain copies of DECL, in
772 set_linkage_according_to_type (tree
/*type*/, tree decl
)
774 TREE_PUBLIC (decl
) = 1;
775 determine_visibility (decl
);
778 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
779 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
780 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
783 build_vtable (tree class_type
, tree name
, tree vtable_type
)
787 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
788 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
789 now to avoid confusion in mangle_decl. */
790 SET_DECL_ASSEMBLER_NAME (decl
, name
);
791 DECL_CONTEXT (decl
) = class_type
;
792 DECL_ARTIFICIAL (decl
) = 1;
793 TREE_STATIC (decl
) = 1;
794 TREE_READONLY (decl
) = 1;
795 DECL_VIRTUAL_P (decl
) = 1;
796 SET_DECL_ALIGN (decl
, TARGET_VTABLE_ENTRY_ALIGN
);
797 DECL_USER_ALIGN (decl
) = true;
798 DECL_VTABLE_OR_VTT_P (decl
) = 1;
799 set_linkage_according_to_type (class_type
, decl
);
800 /* The vtable has not been defined -- yet. */
801 DECL_EXTERNAL (decl
) = 1;
802 DECL_NOT_REALLY_EXTERN (decl
) = 1;
804 /* Mark the VAR_DECL node representing the vtable itself as a
805 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
806 is rather important that such things be ignored because any
807 effort to actually generate DWARF for them will run into
808 trouble when/if we encounter code like:
811 struct S { virtual void member (); };
813 because the artificial declaration of the vtable itself (as
814 manufactured by the g++ front end) will say that the vtable is
815 a static member of `S' but only *after* the debug output for
816 the definition of `S' has already been output. This causes
817 grief because the DWARF entry for the definition of the vtable
818 will try to refer back to an earlier *declaration* of the
819 vtable as a static member of `S' and there won't be one. We
820 might be able to arrange to have the "vtable static member"
821 attached to the member list for `S' before the debug info for
822 `S' get written (which would solve the problem) but that would
823 require more intrusive changes to the g++ front end. */
824 DECL_IGNORED_P (decl
) = 1;
829 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
830 or even complete. If this does not exist, create it. If COMPLETE is
831 nonzero, then complete the definition of it -- that will render it
832 impossible to actually build the vtable, but is useful to get at those
833 which are known to exist in the runtime. */
836 get_vtable_decl (tree type
, int complete
)
840 if (CLASSTYPE_VTABLES (type
))
841 return CLASSTYPE_VTABLES (type
);
843 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
844 CLASSTYPE_VTABLES (type
) = decl
;
848 DECL_EXTERNAL (decl
) = 1;
849 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
855 /* Build the primary virtual function table for TYPE. If BINFO is
856 non-NULL, build the vtable starting with the initial approximation
857 that it is the same as the one which is the head of the association
858 list. Returns a nonzero value if a new vtable is actually
862 build_primary_vtable (tree binfo
, tree type
)
867 decl
= get_vtable_decl (type
, /*complete=*/0);
871 if (BINFO_NEW_VTABLE_MARKED (binfo
))
872 /* We have already created a vtable for this base, so there's
873 no need to do it again. */
876 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
877 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
878 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
879 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
883 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
884 virtuals
= NULL_TREE
;
887 /* Initialize the association list for this type, based
888 on our first approximation. */
889 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
890 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
891 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
895 /* Give BINFO a new virtual function table which is initialized
896 with a skeleton-copy of its original initialization. The only
897 entry that changes is the `delta' entry, so we can really
898 share a lot of structure.
900 FOR_TYPE is the most derived type which caused this table to
903 Returns nonzero if we haven't met BINFO before.
905 The order in which vtables are built (by calling this function) for
906 an object must remain the same, otherwise a binary incompatibility
910 build_secondary_vtable (tree binfo
)
912 if (BINFO_NEW_VTABLE_MARKED (binfo
))
913 /* We already created a vtable for this base. There's no need to
917 /* Remember that we've created a vtable for this BINFO, so that we
918 don't try to do so again. */
919 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
921 /* Make fresh virtual list, so we can smash it later. */
922 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
924 /* Secondary vtables are laid out as part of the same structure as
925 the primary vtable. */
926 BINFO_VTABLE (binfo
) = NULL_TREE
;
930 /* Create a new vtable for BINFO which is the hierarchy dominated by
931 T. Return nonzero if we actually created a new vtable. */
934 make_new_vtable (tree t
, tree binfo
)
936 if (binfo
== TYPE_BINFO (t
))
937 /* In this case, it is *type*'s vtable we are modifying. We start
938 with the approximation that its vtable is that of the
939 immediate base class. */
940 return build_primary_vtable (binfo
, t
);
942 /* This is our very own copy of `basetype' to play with. Later,
943 we will fill in all the virtual functions that override the
944 virtual functions in these base classes which are not defined
945 by the current type. */
946 return build_secondary_vtable (binfo
);
949 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
950 (which is in the hierarchy dominated by T) list FNDECL as its
951 BV_FN. DELTA is the required constant adjustment from the `this'
952 pointer where the vtable entry appears to the `this' required when
953 the function is actually called. */
956 modify_vtable_entry (tree t
,
966 if (fndecl
!= BV_FN (v
)
967 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
969 /* We need a new vtable for BINFO. */
970 if (make_new_vtable (t
, binfo
))
972 /* If we really did make a new vtable, we also made a copy
973 of the BINFO_VIRTUALS list. Now, we have to find the
974 corresponding entry in that list. */
975 *virtuals
= BINFO_VIRTUALS (binfo
);
976 while (BV_FN (*virtuals
) != BV_FN (v
))
977 *virtuals
= TREE_CHAIN (*virtuals
);
981 BV_DELTA (v
) = delta
;
982 BV_VCALL_INDEX (v
) = NULL_TREE
;
988 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
989 METHOD is being injected via a using_decl. Returns true if the
990 method could be added to the method vec. */
993 add_method (tree type
, tree method
, bool via_using
)
995 if (method
== error_mark_node
)
998 gcc_assert (!DECL_EXTERN_C_P (method
));
1000 tree
*slot
= find_member_slot (type
, DECL_NAME (method
));
1001 tree current_fns
= slot
? *slot
: NULL_TREE
;
1006 /* Check to see if we've already got this method. */
1007 for (ovl_iterator
iter (current_fns
); iter
; ++iter
)
1015 if (TREE_CODE (fn
) != TREE_CODE (method
))
1018 /* Two using-declarations can coexist, we'll complain about ambiguity in
1019 overload resolution. */
1020 if (via_using
&& iter
.using_p ()
1021 /* Except handle inherited constructors specially. */
1022 && ! DECL_CONSTRUCTOR_P (fn
))
1025 /* [over.load] Member function declarations with the
1026 same name and the same parameter types cannot be
1027 overloaded if any of them is a static member
1028 function declaration.
1030 [over.load] Member function declarations with the same name and
1031 the same parameter-type-list as well as member function template
1032 declarations with the same name, the same parameter-type-list, and
1033 the same template parameter lists cannot be overloaded if any of
1034 them, but not all, have a ref-qualifier.
1036 [namespace.udecl] When a using-declaration brings names
1037 from a base class into a derived class scope, member
1038 functions in the derived class override and/or hide member
1039 functions with the same name and parameter types in a base
1040 class (rather than conflicting). */
1041 fn_type
= TREE_TYPE (fn
);
1042 method_type
= TREE_TYPE (method
);
1043 parms1
= TYPE_ARG_TYPES (fn_type
);
1044 parms2
= TYPE_ARG_TYPES (method_type
);
1046 /* Compare the quals on the 'this' parm. Don't compare
1047 the whole types, as used functions are treated as
1048 coming from the using class in overload resolution. */
1049 if (! DECL_STATIC_FUNCTION_P (fn
)
1050 && ! DECL_STATIC_FUNCTION_P (method
)
1051 /* Either both or neither need to be ref-qualified for
1052 differing quals to allow overloading. */
1053 && (FUNCTION_REF_QUALIFIED (fn_type
)
1054 == FUNCTION_REF_QUALIFIED (method_type
))
1055 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1056 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1059 /* For templates, the return type and template parameters
1060 must be identical. */
1061 if (TREE_CODE (fn
) == TEMPLATE_DECL
1062 && (!same_type_p (TREE_TYPE (fn_type
),
1063 TREE_TYPE (method_type
))
1064 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1065 DECL_TEMPLATE_PARMS (method
))))
1068 if (! DECL_STATIC_FUNCTION_P (fn
))
1069 parms1
= TREE_CHAIN (parms1
);
1070 if (! DECL_STATIC_FUNCTION_P (method
))
1071 parms2
= TREE_CHAIN (parms2
);
1073 /* Bring back parameters omitted from an inherited ctor. */
1074 if (ctor_omit_inherited_parms (fn
))
1075 parms1
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
1076 if (ctor_omit_inherited_parms (method
))
1077 parms2
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method
));
1079 if (compparms (parms1
, parms2
)
1080 && (!DECL_CONV_FN_P (fn
)
1081 || same_type_p (TREE_TYPE (fn_type
),
1082 TREE_TYPE (method_type
))))
1084 if (!equivalently_constrained (fn
, method
))
1086 if (processing_template_decl
)
1087 /* We can't check satisfaction in dependent context, wait until
1088 the class is instantiated. */
1091 special_function_kind sfk
= special_memfn_p (method
);
1094 || DECL_INHERITED_CTOR (fn
)
1095 || TREE_CODE (fn
) == TEMPLATE_DECL
)
1096 /* Member function templates and non-special member functions
1097 coexist if they are not equivalently constrained. A member
1098 function is not hidden by an inherited constructor. */
1101 /* P0848: For special member functions, deleted, unsatisfied, or
1102 less constrained overloads are ineligible. We implement this
1103 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1104 use the notion of eligibility, and the selected destructor can
1105 be deleted, but removing unsatisfied or less constrained
1106 overloads has the same effect as overload resolution. */
1107 bool dtor
= (sfk
== sfk_destructor
);
1109 losem
= ((!dtor
&& DECL_DELETED_FN (method
))
1110 || !constraints_satisfied_p (method
));
1111 bool losef
= ((!dtor
&& DECL_DELETED_FN (fn
))
1112 || !constraints_satisfied_p (fn
));
1115 win
= losem
- losef
;
1117 win
= more_constrained (fn
, method
);
1119 /* Leave FN in the method vec, discard METHOD. */
1123 /* Remove FN, add METHOD. */
1124 current_fns
= iter
.remove_node (current_fns
);
1128 /* Let them coexist for now. */
1132 /* If these are versions of the same function, process and
1134 if (TREE_CODE (fn
) == FUNCTION_DECL
1135 && maybe_version_functions (method
, fn
, true))
1138 if (DECL_INHERITED_CTOR (method
))
1140 if (DECL_INHERITED_CTOR (fn
))
1142 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1143 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1144 if (flag_new_inheriting_ctors
)
1148 /* Inheriting the same constructor along different
1149 paths, combine them. */
1150 SET_DECL_INHERITED_CTOR
1151 (fn
, ovl_make (DECL_INHERITED_CTOR (method
),
1152 DECL_INHERITED_CTOR (fn
)));
1153 /* And discard the new one. */
1157 /* Inherited ctors can coexist until overload
1161 error_at (DECL_SOURCE_LOCATION (method
),
1162 "%q#D conflicts with version inherited from %qT",
1164 inform (DECL_SOURCE_LOCATION (fn
),
1165 "version inherited from %qT declared here",
1168 /* Otherwise defer to the other function. */
1173 /* Defer to the local function. */
1175 else if (flag_new_inheriting_ctors
1176 && DECL_INHERITED_CTOR (fn
))
1178 /* Remove the inherited constructor. */
1179 current_fns
= iter
.remove_node (current_fns
);
1184 error_at (DECL_SOURCE_LOCATION (method
),
1185 "%q#D cannot be overloaded with %q#D", method
, fn
);
1186 inform (DECL_SOURCE_LOCATION (fn
),
1187 "previous declaration %q#D", fn
);
1193 current_fns
= ovl_insert (method
, current_fns
, via_using
);
1195 if (!COMPLETE_TYPE_P (type
) && !DECL_CONV_FN_P (method
)
1196 && !push_class_level_binding (DECL_NAME (method
), current_fns
))
1200 slot
= add_member_slot (type
, DECL_NAME (method
));
1202 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1203 grok_special_member_properties (method
);
1205 *slot
= current_fns
;
1210 /* Subroutines of finish_struct. */
1212 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1213 legit, otherwise return 0. */
1216 alter_access (tree t
, tree fdecl
, tree access
)
1220 retrofit_lang_decl (fdecl
);
1222 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1224 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1227 if (TREE_VALUE (elem
) != access
)
1229 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1230 error ("conflicting access specifications for method"
1231 " %q+D, ignored", TREE_TYPE (fdecl
));
1233 error ("conflicting access specifications for field %qE, ignored",
1238 /* They're changing the access to the same thing they changed
1239 it to before. That's OK. */
1245 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1246 tf_warning_or_error
);
1247 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1253 /* Return the access node for DECL's access in its enclosing class. */
1256 declared_access (tree decl
)
1258 return (TREE_PRIVATE (decl
) ? access_private_node
1259 : TREE_PROTECTED (decl
) ? access_protected_node
1260 : access_public_node
);
1263 /* Process the USING_DECL, which is a member of T. */
1266 handle_using_decl (tree using_decl
, tree t
)
1268 tree decl
= USING_DECL_DECLS (using_decl
);
1269 tree name
= DECL_NAME (using_decl
);
1270 tree access
= declared_access (using_decl
);
1271 tree flist
= NULL_TREE
;
1274 gcc_assert (!processing_template_decl
&& decl
);
1276 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1277 tf_warning_or_error
);
1280 old_value
= OVL_FIRST (old_value
);
1282 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1285 old_value
= NULL_TREE
;
1288 cp_emit_debug_info_for_using (decl
, t
);
1290 if (is_overloaded_fn (decl
))
1295 else if (is_overloaded_fn (old_value
))
1298 /* It's OK to use functions from a base when there are functions with
1299 the same name already present in the current class. */;
1302 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1303 "because of local method %q#D with same name",
1304 using_decl
, t
, old_value
);
1305 inform (DECL_SOURCE_LOCATION (old_value
),
1306 "local method %q#D declared here", old_value
);
1310 else if (!DECL_ARTIFICIAL (old_value
))
1312 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1313 "because of local member %q#D with same name",
1314 using_decl
, t
, old_value
);
1315 inform (DECL_SOURCE_LOCATION (old_value
),
1316 "local member %q#D declared here", old_value
);
1320 /* Make type T see field decl FDECL with access ACCESS. */
1322 for (ovl_iterator
iter (flist
); iter
; ++iter
)
1324 add_method (t
, *iter
, true);
1325 alter_access (t
, *iter
, access
);
1328 alter_access (t
, decl
, access
);
1331 /* Data structure for find_abi_tags_r, below. */
1335 tree t
; // The type that we're checking for missing tags.
1336 tree subob
; // The subobject of T that we're getting tags from.
1337 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1340 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1341 in the context of P. TAG can be either an identifier (the DECL_NAME of
1342 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1345 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1347 if (!IDENTIFIER_MARKED (id
))
1349 if (p
->tags
!= error_mark_node
)
1351 /* We're collecting tags from template arguments or from
1352 the type of a variable or function return type. */
1353 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1355 /* Don't inherit this tag multiple times. */
1356 IDENTIFIER_MARKED (id
) = true;
1360 /* Tags inherited from type template arguments are only used
1361 to avoid warnings. */
1362 ABI_TAG_IMPLICIT (p
->tags
) = true;
1365 /* For functions and variables we want to warn, too. */
1368 /* Otherwise we're diagnosing missing tags. */
1369 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1371 auto_diagnostic_group d
;
1372 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1373 "that %qT (used in its return type) has",
1375 inform (location_of (*tp
), "%qT declared here", *tp
);
1377 else if (VAR_P (p
->t
))
1379 auto_diagnostic_group d
;
1380 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1381 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1382 inform (location_of (*tp
), "%qT declared here", *tp
);
1384 else if (TYPE_P (p
->subob
))
1386 auto_diagnostic_group d
;
1387 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1388 "that base %qT has", p
->t
, tag
, p
->subob
))
1389 inform (location_of (p
->subob
), "%qT declared here",
1394 auto_diagnostic_group d
;
1395 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1396 "that %qT (used in the type of %qD) has",
1397 p
->t
, tag
, *tp
, p
->subob
))
1399 inform (location_of (p
->subob
), "%qD declared here",
1401 inform (location_of (*tp
), "%qT declared here", *tp
);
1407 /* Find all the ABI tags in the attribute list ATTR and either call
1408 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1411 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1415 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1416 attr
= TREE_CHAIN (attr
))
1417 for (tree list
= TREE_VALUE (attr
); list
;
1418 list
= TREE_CHAIN (list
))
1420 tree tag
= TREE_VALUE (list
);
1421 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1423 check_tag (tag
, id
, tp
, p
);
1425 IDENTIFIER_MARKED (id
) = val
;
1429 /* Find all the ABI tags on T and its enclosing scopes and either call
1430 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1433 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1435 while (t
!= global_namespace
)
1440 attr
= TYPE_ATTRIBUTES (t
);
1441 t
= CP_TYPE_CONTEXT (t
);
1445 attr
= DECL_ATTRIBUTES (t
);
1446 t
= CP_DECL_CONTEXT (t
);
1448 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1452 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1453 types with ABI tags, add the corresponding identifiers to the VEC in
1454 *DATA and set IDENTIFIER_MARKED. */
1457 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1459 if (!OVERLOAD_TYPE_P (*tp
))
1462 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1463 anyway, but let's make sure of it. */
1464 *walk_subtrees
= false;
1466 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1468 mark_or_check_tags (*tp
, tp
, p
, false);
1473 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1474 IDENTIFIER_MARKED on its ABI tags. */
1477 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1479 if (!OVERLOAD_TYPE_P (*tp
))
1482 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1483 anyway, but let's make sure of it. */
1484 *walk_subtrees
= false;
1486 bool *valp
= static_cast<bool*>(data
);
1488 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1493 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1497 mark_abi_tags (tree t
, bool val
)
1499 mark_or_check_tags (t
, NULL
, NULL
, val
);
1502 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1503 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1505 /* Template arguments are part of the signature. */
1506 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1507 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1509 tree arg
= TREE_VEC_ELT (level
, j
);
1510 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1513 if (TREE_CODE (t
) == FUNCTION_DECL
)
1514 /* A function's parameter types are part of the signature, so
1515 we don't need to inherit any tags that are also in them. */
1516 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1517 arg
= TREE_CHAIN (arg
))
1518 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1519 mark_abi_tags_r
, &val
);
1523 /* Check that T has all the ABI tags that subobject SUBOB has, or
1524 warn if not. If T is a (variable or function) declaration, also
1525 return any missing tags, and add them to T if JUST_CHECKING is false. */
1528 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1530 bool inherit
= DECL_P (t
);
1532 if (!inherit
&& !warn_abi_tag
)
1535 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1536 if (!TREE_PUBLIC (decl
))
1537 /* No need to worry about things local to this TU. */
1540 mark_abi_tags (t
, true);
1542 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1543 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1545 data
.tags
= NULL_TREE
;
1547 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1549 if (!(inherit
&& data
.tags
))
1550 /* We don't need to do anything with data.tags. */;
1551 else if (just_checking
)
1552 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1554 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1555 IDENTIFIER_MARKED (id
) = false;
1559 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1561 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1564 = tree_cons (abi_tag_identifier
, data
.tags
, DECL_ATTRIBUTES (t
));
1567 mark_abi_tags (t
, false);
1572 /* Check that DECL has all the ABI tags that are used in parts of its type
1573 that are not reflected in its mangled name. */
1576 check_abi_tags (tree decl
)
1579 check_abi_tags (decl
, TREE_TYPE (decl
));
1580 else if (TREE_CODE (decl
) == FUNCTION_DECL
1581 && !DECL_CONV_FN_P (decl
)
1582 && !mangle_return_type_p (decl
))
1583 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1586 /* Return any ABI tags that are used in parts of the type of DECL
1587 that are not reflected in its mangled name. This function is only
1588 used in backward-compatible mangling for ABI <11. */
1591 missing_abi_tags (tree decl
)
1594 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1595 else if (TREE_CODE (decl
) == FUNCTION_DECL
1596 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1597 that we can use this function for setting need_abi_warning
1598 regardless of the current flag_abi_version. */
1599 && !mangle_return_type_p (decl
))
1600 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1606 inherit_targ_abi_tags (tree t
)
1608 if (!CLASS_TYPE_P (t
)
1609 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1612 mark_abi_tags (t
, true);
1614 tree args
= CLASSTYPE_TI_ARGS (t
);
1615 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1616 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1618 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1619 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1621 tree arg
= TREE_VEC_ELT (level
, j
);
1623 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1627 // If we found some tags on our template arguments, add them to our
1628 // abi_tag attribute.
1631 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1633 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1636 = tree_cons (abi_tag_identifier
, data
.tags
, TYPE_ATTRIBUTES (t
));
1639 mark_abi_tags (t
, false);
1642 /* Return true, iff class T has a non-virtual destructor that is
1643 accessible from outside the class heirarchy (i.e. is public, or
1644 there's a suitable friend. */
1647 accessible_nvdtor_p (tree t
)
1649 tree dtor
= CLASSTYPE_DESTRUCTOR (t
);
1651 /* An implicitly declared destructor is always public. And,
1652 if it were virtual, we would have created it by now. */
1656 if (DECL_VINDEX (dtor
))
1657 return false; /* Virtual */
1659 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1660 return true; /* Public */
1662 if (CLASSTYPE_FRIEND_CLASSES (t
)
1663 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1664 return true; /* Has friends */
1669 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1670 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1671 properties of the bases. */
1674 check_bases (tree t
,
1675 int* cant_have_const_ctor_p
,
1676 int* no_const_asn_ref_p
)
1679 bool seen_non_virtual_nearly_empty_base_p
= 0;
1680 int seen_tm_mask
= 0;
1683 tree field
= NULL_TREE
;
1685 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1686 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1687 if (TREE_CODE (field
) == FIELD_DECL
)
1690 for (binfo
= TYPE_BINFO (t
), i
= 0;
1691 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1693 tree basetype
= TREE_TYPE (base_binfo
);
1695 gcc_assert (COMPLETE_TYPE_P (basetype
));
1697 if (CLASSTYPE_FINAL (basetype
))
1698 error ("cannot derive from %<final%> base %qT in derived type %qT",
1701 /* If any base class is non-literal, so is the derived class. */
1702 if (!CLASSTYPE_LITERAL_P (basetype
))
1703 CLASSTYPE_LITERAL_P (t
) = false;
1705 /* If the base class doesn't have copy constructors or
1706 assignment operators that take const references, then the
1707 derived class cannot have such a member automatically
1709 if (TYPE_HAS_COPY_CTOR (basetype
)
1710 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1711 *cant_have_const_ctor_p
= 1;
1712 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1713 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1714 *no_const_asn_ref_p
= 1;
1716 if (BINFO_VIRTUAL_P (base_binfo
))
1717 /* A virtual base does not effect nearly emptiness. */
1719 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1721 if (seen_non_virtual_nearly_empty_base_p
)
1722 /* And if there is more than one nearly empty base, then the
1723 derived class is not nearly empty either. */
1724 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1726 /* Remember we've seen one. */
1727 seen_non_virtual_nearly_empty_base_p
= 1;
1729 else if (!is_empty_class (basetype
))
1730 /* If the base class is not empty or nearly empty, then this
1731 class cannot be nearly empty. */
1732 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1734 /* A lot of properties from the bases also apply to the derived
1736 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1737 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1738 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1739 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1740 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1741 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1742 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1743 || !TYPE_HAS_COPY_CTOR (basetype
));
1744 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1745 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1746 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1747 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1748 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1749 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1750 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1751 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1752 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1753 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1754 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1755 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1756 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1757 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1758 if (TYPE_HAS_MUTABLE_P (basetype
))
1759 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1761 /* A standard-layout class is a class that:
1763 * has no non-standard-layout base classes, */
1764 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1765 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1768 /* ...has no base classes of the same type as the first non-static
1770 if (field
&& DECL_CONTEXT (field
) == t
1771 && (same_type_ignoring_top_level_qualifiers_p
1772 (TREE_TYPE (field
), basetype
)))
1773 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1775 ...has at most one base class subobject of any given type... */
1776 else if (CLASSTYPE_REPEATED_BASE_P (t
))
1777 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1779 /* ...either has no non-static data members in the most-derived
1780 class and at most one base class with non-static data
1781 members, or has no base classes with non-static data
1782 members. FIXME This was reworded in DR 1813. */
1783 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1784 basefield
= DECL_CHAIN (basefield
))
1785 if (TREE_CODE (basefield
) == FIELD_DECL
1786 && !(DECL_FIELD_IS_BASE (basefield
)
1787 && integer_zerop (DECL_SIZE (basefield
))))
1790 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1797 /* Don't bother collecting tm attributes if transactional memory
1798 support is not enabled. */
1801 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1803 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1806 check_abi_tags (t
, basetype
);
1809 /* If one of the base classes had TM attributes, and the current class
1810 doesn't define its own, then the current class inherits one. */
1811 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1813 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1814 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1818 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1819 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1820 that have had a nearly-empty virtual primary base stolen by some
1821 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1825 determine_primary_bases (tree t
)
1828 tree primary
= NULL_TREE
;
1829 tree type_binfo
= TYPE_BINFO (t
);
1832 /* Determine the primary bases of our bases. */
1833 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1834 base_binfo
= TREE_CHAIN (base_binfo
))
1836 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1838 /* See if we're the non-virtual primary of our inheritance
1840 if (!BINFO_VIRTUAL_P (base_binfo
))
1842 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1843 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1846 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1847 BINFO_TYPE (parent_primary
)))
1848 /* We are the primary binfo. */
1849 BINFO_PRIMARY_P (base_binfo
) = 1;
1851 /* Determine if we have a virtual primary base, and mark it so.
1853 if (primary
&& BINFO_VIRTUAL_P (primary
))
1855 tree this_primary
= copied_binfo (primary
, base_binfo
);
1857 if (BINFO_PRIMARY_P (this_primary
))
1858 /* Someone already claimed this base. */
1859 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1864 BINFO_PRIMARY_P (this_primary
) = 1;
1865 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1867 /* A virtual binfo might have been copied from within
1868 another hierarchy. As we're about to use it as a
1869 primary base, make sure the offsets match. */
1870 delta
= size_diffop_loc (input_location
,
1871 fold_convert (ssizetype
,
1872 BINFO_OFFSET (base_binfo
)),
1873 fold_convert (ssizetype
,
1874 BINFO_OFFSET (this_primary
)));
1876 propagate_binfo_offsets (this_primary
, delta
);
1881 /* First look for a dynamic direct non-virtual base. */
1882 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1884 tree basetype
= BINFO_TYPE (base_binfo
);
1886 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1888 primary
= base_binfo
;
1893 /* A "nearly-empty" virtual base class can be the primary base
1894 class, if no non-virtual polymorphic base can be found. Look for
1895 a nearly-empty virtual dynamic base that is not already a primary
1896 base of something in the hierarchy. If there is no such base,
1897 just pick the first nearly-empty virtual base. */
1899 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1900 base_binfo
= TREE_CHAIN (base_binfo
))
1901 if (BINFO_VIRTUAL_P (base_binfo
)
1902 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1904 if (!BINFO_PRIMARY_P (base_binfo
))
1906 /* Found one that is not primary. */
1907 primary
= base_binfo
;
1911 /* Remember the first candidate. */
1912 primary
= base_binfo
;
1916 /* If we've got a primary base, use it. */
1919 tree basetype
= BINFO_TYPE (primary
);
1921 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1922 if (BINFO_PRIMARY_P (primary
))
1923 /* We are stealing a primary base. */
1924 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1925 BINFO_PRIMARY_P (primary
) = 1;
1926 if (BINFO_VIRTUAL_P (primary
))
1930 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1931 /* A virtual binfo might have been copied from within
1932 another hierarchy. As we're about to use it as a primary
1933 base, make sure the offsets match. */
1934 delta
= size_diffop_loc (input_location
, ssize_int (0),
1935 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
1937 propagate_binfo_offsets (primary
, delta
);
1940 primary
= TYPE_BINFO (basetype
);
1942 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1943 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1944 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1948 /* Update the variant types of T. */
1951 fixup_type_variants (tree t
)
1958 for (variants
= TYPE_NEXT_VARIANT (t
);
1960 variants
= TYPE_NEXT_VARIANT (variants
))
1962 /* These fields are in the _TYPE part of the node, not in
1963 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1964 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1965 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1966 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1967 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1969 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1970 CLASSTYPE_FINAL (variants
) = CLASSTYPE_FINAL (t
);
1972 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1974 /* Copy whatever these are holding today. */
1975 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1976 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1978 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1979 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1983 /* KLASS is a class that we're applying may_alias to after the body is
1984 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1985 canonical type(s) will be implicitly updated. */
1988 fixup_may_alias (tree klass
)
1992 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
1993 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1994 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1995 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
1996 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1997 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
2000 /* Early variant fixups: we apply attributes at the beginning of the class
2001 definition, and we need to fix up any variants that have already been
2002 made via elaborated-type-specifier so that check_qualified_type works. */
2005 fixup_attribute_variants (tree t
)
2012 tree attrs
= TYPE_ATTRIBUTES (t
);
2013 unsigned align
= TYPE_ALIGN (t
);
2014 bool user_align
= TYPE_USER_ALIGN (t
);
2015 bool may_alias
= lookup_attribute ("may_alias", attrs
);
2016 bool packed
= TYPE_PACKED (t
);
2019 fixup_may_alias (t
);
2021 for (variants
= TYPE_NEXT_VARIANT (t
);
2023 variants
= TYPE_NEXT_VARIANT (variants
))
2025 /* These are the two fields that check_qualified_type looks at and
2026 are affected by attributes. */
2027 TYPE_ATTRIBUTES (variants
) = attrs
;
2028 unsigned valign
= align
;
2029 if (TYPE_USER_ALIGN (variants
))
2030 valign
= MAX (valign
, TYPE_ALIGN (variants
));
2032 TYPE_USER_ALIGN (variants
) = user_align
;
2033 SET_TYPE_ALIGN (variants
, valign
);
2034 TYPE_PACKED (variants
) = packed
;
2036 fixup_may_alias (variants
);
2040 /* Set memoizing fields and bits of T (and its variants) for later
2044 finish_struct_bits (tree t
)
2046 /* Fix up variants (if any). */
2047 fixup_type_variants (t
);
2049 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
2050 /* For a class w/o baseclasses, 'finish_struct' has set
2051 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2052 Similarly for a class whose base classes do not have vtables.
2053 When neither of these is true, we might have removed abstract
2054 virtuals (by providing a definition), added some (by declaring
2055 new ones), or redeclared ones from a base class. We need to
2056 recalculate what's really an abstract virtual at this point (by
2057 looking in the vtables). */
2058 get_pure_virtuals (t
);
2060 /* If this type has a copy constructor or a destructor, force its
2061 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2062 nonzero. This will cause it to be passed by invisible reference
2063 and prevent it from being returned in a register. */
2064 if (type_has_nontrivial_copy_init (t
)
2065 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2068 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2069 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2071 SET_TYPE_MODE (variants
, BLKmode
);
2072 TREE_ADDRESSABLE (variants
) = 1;
2077 /* Issue warnings about T having private constructors, but no friends,
2080 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2081 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2082 non-private static member functions. */
2085 maybe_warn_about_overly_private_class (tree t
)
2087 int has_member_fn
= 0;
2088 int has_nonprivate_method
= 0;
2089 bool nonprivate_ctor
= false;
2091 if (!warn_ctor_dtor_privacy
2092 /* If the class has friends, those entities might create and
2093 access instances, so we should not warn. */
2094 || (CLASSTYPE_FRIEND_CLASSES (t
)
2095 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2096 /* We will have warned when the template was declared; there's
2097 no need to warn on every instantiation. */
2098 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2099 /* There's no reason to even consider warning about this
2103 /* We only issue one warning, if more than one applies, because
2104 otherwise, on code like:
2107 // Oops - forgot `public:'
2113 we warn several times about essentially the same problem. */
2115 /* Check to see if all (non-constructor, non-destructor) member
2116 functions are private. (Since there are no friends or
2117 non-private statics, we can't ever call any of the private member
2119 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2120 if (TREE_CODE (fn
) == USING_DECL
2121 && DECL_NAME (fn
) == ctor_identifier
2122 && !TREE_PRIVATE (fn
))
2123 nonprivate_ctor
= true;
2124 else if (!DECL_DECLARES_FUNCTION_P (fn
))
2125 /* Not a function. */;
2126 else if (DECL_ARTIFICIAL (fn
))
2127 /* We're not interested in compiler-generated methods; they don't
2128 provide any way to call private members. */;
2129 else if (!TREE_PRIVATE (fn
))
2131 if (DECL_STATIC_FUNCTION_P (fn
))
2132 /* A non-private static member function is just like a
2133 friend; it can create and invoke private member
2134 functions, and be accessed without a class
2138 has_nonprivate_method
= 1;
2139 /* Keep searching for a static member function. */
2141 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2144 if (!has_nonprivate_method
&& has_member_fn
)
2146 /* There are no non-private methods, and there's at least one
2147 private member function that isn't a constructor or
2148 destructor. (If all the private members are
2149 constructors/destructors we want to use the code below that
2150 issues error messages specifically referring to
2151 constructors/destructors.) */
2153 tree binfo
= TYPE_BINFO (t
);
2155 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2156 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2158 has_nonprivate_method
= 1;
2161 if (!has_nonprivate_method
)
2163 warning (OPT_Wctor_dtor_privacy
,
2164 "all member functions in class %qT are private", t
);
2169 /* Even if some of the member functions are non-private, the class
2170 won't be useful for much if all the constructors or destructors
2171 are private: such an object can never be created or destroyed. */
2172 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
2173 if (TREE_PRIVATE (dtor
))
2175 warning (OPT_Wctor_dtor_privacy
,
2176 "%q#T only defines a private destructor and has no friends",
2181 /* Warn about classes that have private constructors and no friends. */
2182 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2183 /* Implicitly generated constructors are always public. */
2184 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
2186 tree copy_or_move
= NULL_TREE
;
2188 /* If a non-template class does not define a copy
2189 constructor, one is defined for it, enabling it to avoid
2190 this warning. For a template class, this does not
2191 happen, and so we would normally get a warning on:
2193 template <class T> class C { private: C(); };
2195 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2196 complete non-template or fully instantiated classes have this
2198 if (!TYPE_HAS_COPY_CTOR (t
))
2199 nonprivate_ctor
= true;
2201 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
));
2202 !nonprivate_ctor
&& iter
; ++iter
)
2203 if (TREE_PRIVATE (*iter
))
2205 else if (copy_fn_p (*iter
) || move_fn_p (*iter
))
2206 /* Ideally, we wouldn't count any constructor that takes
2207 an argument of the class type as a parameter, because
2208 such things cannot be used to construct an instance of
2209 the class unless you already have one. */
2210 copy_or_move
= *iter
;
2212 nonprivate_ctor
= true;
2214 if (!nonprivate_ctor
)
2216 bool w
= warning (OPT_Wctor_dtor_privacy
,
2217 "%q#T only defines private constructors and has "
2219 if (w
&& copy_or_move
)
2220 inform (DECL_SOURCE_LOCATION (copy_or_move
),
2221 "%q#D is public, but requires an existing %q#T object",
2228 /* Make BINFO's vtable have N entries, including RTTI entries,
2229 vbase and vcall offsets, etc. Set its type and call the back end
2233 layout_vtable_decl (tree binfo
, int n
)
2238 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2239 layout_type (atype
);
2241 /* We may have to grow the vtable. */
2242 vtable
= get_vtbl_decl_for_binfo (binfo
);
2243 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2245 TREE_TYPE (vtable
) = atype
;
2246 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2247 layout_decl (vtable
, 0);
2251 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2252 have the same signature. */
2255 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2257 /* One destructor overrides another if they are the same kind of
2259 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2260 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2262 /* But a non-destructor never overrides a destructor, nor vice
2263 versa, nor do different kinds of destructors override
2264 one-another. For example, a complete object destructor does not
2265 override a deleting destructor. */
2266 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2269 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2270 || (DECL_CONV_FN_P (fndecl
)
2271 && DECL_CONV_FN_P (base_fndecl
)
2272 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2273 DECL_CONV_FN_TYPE (base_fndecl
))))
2275 tree fntype
= TREE_TYPE (fndecl
);
2276 tree base_fntype
= TREE_TYPE (base_fndecl
);
2277 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2278 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2279 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2280 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2286 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2290 base_derived_from (tree derived
, tree base
)
2294 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2296 if (probe
== derived
)
2298 else if (BINFO_VIRTUAL_P (probe
))
2299 /* If we meet a virtual base, we can't follow the inheritance
2300 any more. See if the complete type of DERIVED contains
2301 such a virtual base. */
2302 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2308 struct find_final_overrider_data
{
2309 /* The function for which we are trying to find a final overrider. */
2311 /* The base class in which the function was declared. */
2312 tree declaring_base
;
2313 /* The candidate overriders. */
2315 /* Path to most derived. */
2319 /* Add the overrider along the current path to FFOD->CANDIDATES.
2320 Returns true if an overrider was found; false otherwise. */
2323 dfs_find_final_overrider_1 (tree binfo
,
2324 find_final_overrider_data
*ffod
,
2329 /* If BINFO is not the most derived type, try a more derived class.
2330 A definition there will overrider a definition here. */
2334 if (dfs_find_final_overrider_1
2335 (ffod
->path
[depth
], ffod
, depth
))
2339 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2342 tree
*candidate
= &ffod
->candidates
;
2344 /* Remove any candidates overridden by this new function. */
2347 /* If *CANDIDATE overrides METHOD, then METHOD
2348 cannot override anything else on the list. */
2349 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2351 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2352 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2353 *candidate
= TREE_CHAIN (*candidate
);
2355 candidate
= &TREE_CHAIN (*candidate
);
2358 /* Add the new function. */
2359 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2366 /* Called from find_final_overrider via dfs_walk. */
2369 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2371 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2373 if (binfo
== ffod
->declaring_base
)
2374 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2375 ffod
->path
.safe_push (binfo
);
2381 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2383 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2389 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2390 FN and whose TREE_VALUE is the binfo for the base where the
2391 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2392 DERIVED) is the base object in which FN is declared. */
2395 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2397 find_final_overrider_data ffod
;
2399 /* Getting this right is a little tricky. This is valid:
2401 struct S { virtual void f (); };
2402 struct T { virtual void f (); };
2403 struct U : public S, public T { };
2405 even though calling `f' in `U' is ambiguous. But,
2407 struct R { virtual void f(); };
2408 struct S : virtual public R { virtual void f (); };
2409 struct T : virtual public R { virtual void f (); };
2410 struct U : public S, public T { };
2412 is not -- there's no way to decide whether to put `S::f' or
2413 `T::f' in the vtable for `R'.
2415 The solution is to look at all paths to BINFO. If we find
2416 different overriders along any two, then there is a problem. */
2417 if (DECL_THUNK_P (fn
))
2418 fn
= THUNK_TARGET (fn
);
2420 /* Determine the depth of the hierarchy. */
2422 ffod
.declaring_base
= binfo
;
2423 ffod
.candidates
= NULL_TREE
;
2424 ffod
.path
.create (30);
2426 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2427 dfs_find_final_overrider_post
, &ffod
);
2429 ffod
.path
.release ();
2431 /* If there was no winner, issue an error message. */
2432 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2433 return error_mark_node
;
2435 return ffod
.candidates
;
2438 /* Return the index of the vcall offset for FN when TYPE is used as a
2442 get_vcall_index (tree fn
, tree type
)
2444 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2448 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2449 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2450 || same_signature_p (fn
, p
->purpose
))
2453 /* There should always be an appropriate index. */
2457 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2458 overrider at that index in the vtable. This should only be used when we
2459 know that BINFO is correct for the dynamic type of the object. */
2462 lookup_vfn_in_binfo (tree idx
, tree binfo
)
2464 int ix
= tree_to_shwi (idx
);
2465 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2466 ix
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
2467 while (BINFO_PRIMARY_P (binfo
))
2468 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2469 class that actually owns the vtable. */
2470 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
2471 tree virtuals
= BINFO_VIRTUALS (binfo
);
2472 return TREE_VALUE (chain_index (ix
, virtuals
));
2475 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2476 dominated by T. FN is the old function; VIRTUALS points to the
2477 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2478 of that entry in the list. */
2481 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2489 tree overrider_fn
, overrider_target
;
2490 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2491 tree over_return
, base_return
;
2494 /* Find the nearest primary base (possibly binfo itself) which defines
2495 this function; this is the class the caller will convert to when
2496 calling FN through BINFO. */
2497 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2500 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2503 /* The nearest definition is from a lost primary. */
2504 if (BINFO_LOST_PRIMARY_P (b
))
2509 /* Find the final overrider. */
2510 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2511 if (overrider
== error_mark_node
)
2513 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2516 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2518 /* Check for adjusting covariant return types. */
2519 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2520 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2522 if (INDIRECT_TYPE_P (over_return
)
2523 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2524 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2525 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2526 /* If the overrider is invalid, don't even try. */
2527 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2529 /* If FN is a covariant thunk, we must figure out the adjustment
2530 to the final base FN was converting to. As OVERRIDER_TARGET might
2531 also be converting to the return type of FN, we have to
2532 combine the two conversions here. */
2533 tree fixed_offset
, virtual_offset
;
2535 over_return
= TREE_TYPE (over_return
);
2536 base_return
= TREE_TYPE (base_return
);
2538 if (DECL_THUNK_P (fn
))
2540 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2541 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2542 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2545 fixed_offset
= virtual_offset
= NULL_TREE
;
2548 /* Find the equivalent binfo within the return type of the
2549 overriding function. We will want the vbase offset from
2551 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2553 else if (!same_type_ignoring_top_level_qualifiers_p
2554 (over_return
, base_return
))
2556 /* There was no existing virtual thunk (which takes
2557 precedence). So find the binfo of the base function's
2558 return type within the overriding function's return type.
2559 Fortunately we know the covariancy is valid (it
2560 has already been checked), so we can just iterate along
2561 the binfos, which have been chained in inheritance graph
2562 order. Of course it is lame that we have to repeat the
2563 search here anyway -- we should really be caching pieces
2564 of the vtable and avoiding this repeated work. */
2565 tree thunk_binfo
= NULL_TREE
;
2566 tree base_binfo
= TYPE_BINFO (base_return
);
2568 /* Find the base binfo within the overriding function's
2569 return type. We will always find a thunk_binfo, except
2570 when the covariancy is invalid (which we will have
2571 already diagnosed). */
2573 for (thunk_binfo
= TYPE_BINFO (over_return
); thunk_binfo
;
2574 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2575 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2576 BINFO_TYPE (base_binfo
)))
2578 gcc_assert (thunk_binfo
|| errorcount
);
2580 /* See if virtual inheritance is involved. */
2581 for (virtual_offset
= thunk_binfo
;
2583 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2584 if (BINFO_VIRTUAL_P (virtual_offset
))
2588 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2590 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2594 /* We convert via virtual base. Adjust the fixed
2595 offset to be from there. */
2597 size_diffop (offset
,
2598 fold_convert (ssizetype
,
2599 BINFO_OFFSET (virtual_offset
)));
2602 /* There was an existing fixed offset, this must be
2603 from the base just converted to, and the base the
2604 FN was thunking to. */
2605 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2607 fixed_offset
= offset
;
2611 if (fixed_offset
|| virtual_offset
)
2612 /* Replace the overriding function with a covariant thunk. We
2613 will emit the overriding function in its own slot as
2615 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2616 fixed_offset
, virtual_offset
);
2619 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2620 !DECL_THUNK_P (fn
));
2622 /* If we need a covariant thunk, then we may need to adjust first_defn.
2623 The ABI specifies that the thunks emitted with a function are
2624 determined by which bases the function overrides, so we need to be
2625 sure that we're using a thunk for some overridden base; even if we
2626 know that the necessary this adjustment is zero, there may not be an
2627 appropriate zero-this-adjustment thunk for us to use since thunks for
2628 overriding virtual bases always use the vcall offset.
2630 Furthermore, just choosing any base that overrides this function isn't
2631 quite right, as this slot won't be used for calls through a type that
2632 puts a covariant thunk here. Calling the function through such a type
2633 will use a different slot, and that slot is the one that determines
2634 the thunk emitted for that base.
2636 So, keep looking until we find the base that we're really overriding
2637 in this slot: the nearest primary base that doesn't use a covariant
2638 thunk in this slot. */
2639 if (overrider_target
!= overrider_fn
)
2641 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2642 /* We already know that the overrider needs a covariant thunk. */
2643 b
= get_primary_binfo (b
);
2644 for (; ; b
= get_primary_binfo (b
))
2646 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2647 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2648 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2650 if (BINFO_LOST_PRIMARY_P (b
))
2656 /* Assume that we will produce a thunk that convert all the way to
2657 the final overrider, and not to an intermediate virtual base. */
2658 virtual_base
= NULL_TREE
;
2660 /* See if we can convert to an intermediate virtual base first, and then
2661 use the vcall offset located there to finish the conversion. */
2662 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2664 /* If we find the final overrider, then we can stop
2666 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2667 BINFO_TYPE (TREE_VALUE (overrider
))))
2670 /* If we find a virtual base, and we haven't yet found the
2671 overrider, then there is a virtual base between the
2672 declaring base (first_defn) and the final overrider. */
2673 if (BINFO_VIRTUAL_P (b
))
2680 /* Compute the constant adjustment to the `this' pointer. The
2681 `this' pointer, when this function is called, will point at BINFO
2682 (or one of its primary bases, which are at the same offset). */
2684 /* The `this' pointer needs to be adjusted from the declaration to
2685 the nearest virtual base. */
2686 delta
= size_diffop_loc (input_location
,
2687 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2688 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2690 /* If the nearest definition is in a lost primary, we don't need an
2691 entry in our vtable. Except possibly in a constructor vtable,
2692 if we happen to get our primary back. In that case, the offset
2693 will be zero, as it will be a primary base. */
2694 delta
= size_zero_node
;
2696 /* The `this' pointer needs to be adjusted from pointing to
2697 BINFO to pointing at the base where the final overrider
2699 delta
= size_diffop_loc (input_location
,
2700 fold_convert (ssizetype
,
2701 BINFO_OFFSET (TREE_VALUE (overrider
))),
2702 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2704 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2707 BV_VCALL_INDEX (*virtuals
)
2708 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2710 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2712 BV_LOST_PRIMARY (*virtuals
) = lost
;
2715 /* Called from modify_all_vtables via dfs_walk. */
2718 dfs_modify_vtables (tree binfo
, void* data
)
2720 tree t
= (tree
) data
;
2725 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2726 /* A base without a vtable needs no modification, and its bases
2727 are uninteresting. */
2728 return dfs_skip_bases
;
2730 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2731 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2732 /* Don't do the primary vtable, if it's new. */
2735 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2736 /* There's no need to modify the vtable for a non-virtual primary
2737 base; we're not going to use that vtable anyhow. We do still
2738 need to do this for virtual primary bases, as they could become
2739 non-primary in a construction vtable. */
2742 make_new_vtable (t
, binfo
);
2744 /* Now, go through each of the virtual functions in the virtual
2745 function table for BINFO. Find the final overrider, and update
2746 the BINFO_VIRTUALS list appropriately. */
2747 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2748 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2750 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2751 old_virtuals
= TREE_CHAIN (old_virtuals
))
2752 update_vtable_entry_for_fn (t
,
2754 BV_FN (old_virtuals
),
2760 /* Update all of the primary and secondary vtables for T. Create new
2761 vtables as required, and initialize their RTTI information. Each
2762 of the functions in VIRTUALS is declared in T and may override a
2763 virtual function from a base class; find and modify the appropriate
2764 entries to point to the overriding functions. Returns a list, in
2765 declaration order, of the virtual functions that are declared in T,
2766 but do not appear in the primary base class vtable, and which
2767 should therefore be appended to the end of the vtable for T. */
2770 modify_all_vtables (tree t
, tree virtuals
)
2772 tree binfo
= TYPE_BINFO (t
);
2775 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2776 if (TYPE_CONTAINS_VPTR_P (t
))
2777 get_vtable_decl (t
, false);
2779 /* Update all of the vtables. */
2780 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2782 /* Add virtual functions not already in our primary vtable. These
2783 will be both those introduced by this class, and those overridden
2784 from secondary bases. It does not include virtuals merely
2785 inherited from secondary bases. */
2786 for (fnsp
= &virtuals
; *fnsp
; )
2788 tree fn
= TREE_VALUE (*fnsp
);
2790 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2791 || DECL_VINDEX (fn
) == error_mark_node
)
2793 /* We don't need to adjust the `this' pointer when
2794 calling this function. */
2795 BV_DELTA (*fnsp
) = integer_zero_node
;
2796 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2798 /* This is a function not already in our vtable. Keep it. */
2799 fnsp
= &TREE_CHAIN (*fnsp
);
2802 /* We've already got an entry for this function. Skip it. */
2803 *fnsp
= TREE_CHAIN (*fnsp
);
2809 /* Get the base virtual function declarations in T that have the
2813 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2815 bool found_decls
= false;
2817 /* Find virtual functions in T with the indicated NAME. */
2818 for (ovl_iterator
iter (get_class_binding (t
, name
)); iter
; ++iter
)
2820 tree method
= *iter
;
2822 if (TREE_CODE (method
) == FUNCTION_DECL
&& DECL_VINDEX (method
))
2824 base_fndecls
->safe_push (method
);
2832 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2833 for (int i
= 0; i
< n_baseclasses
; i
++)
2835 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2836 get_basefndecls (name
, basetype
, base_fndecls
);
2840 /* If this method overrides a virtual method from a base, then mark
2841 this member function as being virtual as well. Do 'final' and
2842 'override' checks too. */
2845 check_for_override (tree decl
, tree ctype
)
2847 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2848 /* In [temp.mem] we have:
2850 A specialization of a member function template does not
2851 override a virtual function from a base class. */
2854 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2855 used for a vfunc. That avoids the expensive look_for_overrides
2856 call that when we know there's nothing to find. As conversion
2857 operators for the same type can have distinct identifiers, we
2858 cannot optimize those in that way. */
2859 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2860 || DECL_CONV_FN_P (decl
))
2861 && look_for_overrides (ctype
, decl
)
2862 /* Check staticness after we've checked if we 'override'. */
2863 && !DECL_STATIC_FUNCTION_P (decl
))
2865 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2866 the error_mark_node so that we know it is an overriding
2868 DECL_VINDEX (decl
) = decl
;
2871 && !DECL_OVERRIDE_P (decl
)
2872 && !DECL_FINAL_P (decl
)
2873 && !DECL_DESTRUCTOR_P (decl
))
2874 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2875 "%qD can be marked override", decl
);
2877 else if (DECL_OVERRIDE_P (decl
))
2878 error ("%q+#D marked %<override%>, but does not override", decl
);
2880 if (DECL_VIRTUAL_P (decl
))
2882 /* Remember this identifier is virtual name. */
2883 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = true;
2885 if (!DECL_VINDEX (decl
))
2886 /* It's a new vfunc. */
2887 DECL_VINDEX (decl
) = error_mark_node
;
2889 if (DECL_DESTRUCTOR_P (decl
))
2890 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2892 else if (DECL_FINAL_P (decl
))
2893 error ("%q+#D marked %<final%>, but is not virtual", decl
);
2896 /* Warn about hidden virtual functions that are not overridden in t.
2897 We know that constructors and destructors don't apply. */
2900 warn_hidden (tree t
)
2902 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (t
))
2903 for (unsigned ix
= member_vec
->length (); ix
--;)
2905 tree fns
= (*member_vec
)[ix
];
2910 tree name
= OVL_NAME (fns
);
2911 auto_vec
<tree
, 20> base_fndecls
;
2916 /* Iterate through all of the base classes looking for possibly
2917 hidden functions. */
2918 for (binfo
= TYPE_BINFO (t
), j
= 0;
2919 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2921 tree basetype
= BINFO_TYPE (base_binfo
);
2922 get_basefndecls (name
, basetype
, &base_fndecls
);
2925 /* If there are no functions to hide, continue. */
2926 if (base_fndecls
.is_empty ())
2929 /* Remove any overridden functions. */
2930 for (ovl_iterator
iter (fns
); iter
; ++iter
)
2932 tree fndecl
= *iter
;
2933 if (TREE_CODE (fndecl
) == FUNCTION_DECL
2934 && DECL_VINDEX (fndecl
))
2936 /* If the method from the base class has the same
2937 signature as the method from the derived class, it
2938 has been overridden. */
2939 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
2941 && same_signature_p (fndecl
, base_fndecls
[k
]))
2942 base_fndecls
[k
] = NULL_TREE
;
2946 /* Now give a warning for all base functions without overriders,
2947 as they are hidden. */
2949 FOR_EACH_VEC_ELT (base_fndecls
, j
, base_fndecl
)
2952 auto_diagnostic_group d
;
2953 /* Here we know it is a hider, and no overrider exists. */
2954 if (warning_at (location_of (base_fndecl
),
2955 OPT_Woverloaded_virtual
,
2956 "%qD was hidden", base_fndecl
))
2957 inform (location_of (fns
), " by %qD", fns
);
2962 /* Recursive helper for finish_struct_anon. */
2965 finish_struct_anon_r (tree field
, bool complain
)
2967 for (tree elt
= TYPE_FIELDS (TREE_TYPE (field
)); elt
; elt
= DECL_CHAIN (elt
))
2969 /* We're generally only interested in entities the user
2970 declared, but we also find nested classes by noticing
2971 the TYPE_DECL that we create implicitly. You're
2972 allowed to put one anonymous union inside another,
2973 though, so we explicitly tolerate that. We use
2974 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2975 we also allow unnamed types used for defining fields. */
2976 if (DECL_ARTIFICIAL (elt
)
2977 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2978 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
2982 && (TREE_CODE (elt
) != FIELD_DECL
2983 || (TREE_PRIVATE (elt
) || TREE_PROTECTED (elt
))))
2985 /* We already complained about static data members in
2986 finish_static_data_member_decl. */
2989 auto_diagnostic_group d
;
2990 if (permerror (DECL_SOURCE_LOCATION (elt
),
2991 TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
2992 ? "%q#D invalid; an anonymous union may "
2993 "only have public non-static data members"
2994 : "%q#D invalid; an anonymous struct may "
2995 "only have public non-static data members", elt
))
2998 if (flag_permissive
&& !hint
)
3001 inform (DECL_SOURCE_LOCATION (elt
),
3002 "this flexibility is deprecated and will be "
3009 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3010 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3012 /* Recurse into the anonymous aggregates to correctly handle
3013 access control (c++/24926):
3024 if (DECL_NAME (elt
) == NULL_TREE
3025 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
3026 finish_struct_anon_r (elt
, /*complain=*/false);
3030 /* Check for things that are invalid. There are probably plenty of other
3031 things we should check for also. */
3034 finish_struct_anon (tree t
)
3036 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3038 if (TREE_STATIC (field
))
3040 if (TREE_CODE (field
) != FIELD_DECL
)
3043 if (DECL_NAME (field
) == NULL_TREE
3044 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3045 finish_struct_anon_r (field
, /*complain=*/true);
3049 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3050 will be used later during class template instantiation.
3051 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3052 a non-static member data (FIELD_DECL), a member function
3053 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3054 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3055 When FRIEND_P is nonzero, T is either a friend class
3056 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3057 (FUNCTION_DECL, TEMPLATE_DECL). */
3060 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
3062 if (CLASSTYPE_TEMPLATE_INFO (type
)
3063 && TREE_CODE (t
) != CONST_DECL
)
3065 tree purpose
= friend_p
? NULL_TREE
: type
;
3067 CLASSTYPE_DECL_LIST (type
)
3068 = tree_cons (purpose
, t
, CLASSTYPE_DECL_LIST (type
));
3072 /* This function is called from declare_virt_assop_and_dtor via
3075 DATA is a type that direcly or indirectly inherits the base
3076 represented by BINFO. If BINFO contains a virtual assignment [copy
3077 assignment or move assigment] operator or a virtual constructor,
3078 declare that function in DATA if it hasn't been already declared. */
3081 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
3083 tree bv
, fn
, t
= (tree
)data
;
3084 tree opname
= assign_op_identifier
;
3086 gcc_assert (t
&& CLASS_TYPE_P (t
));
3087 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
3089 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
3090 /* A base without a vtable needs no modification, and its bases
3091 are uninteresting. */
3092 return dfs_skip_bases
;
3094 if (BINFO_PRIMARY_P (binfo
))
3095 /* If this is a primary base, then we have already looked at the
3096 virtual functions of its vtable. */
3099 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3103 if (DECL_NAME (fn
) == opname
)
3105 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3106 lazily_declare_fn (sfk_copy_assignment
, t
);
3107 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3108 lazily_declare_fn (sfk_move_assignment
, t
);
3110 else if (DECL_DESTRUCTOR_P (fn
)
3111 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3112 lazily_declare_fn (sfk_destructor
, t
);
3118 /* If the class type T has a direct or indirect base that contains a
3119 virtual assignment operator or a virtual destructor, declare that
3120 function in T if it hasn't been already declared. */
3123 declare_virt_assop_and_dtor (tree t
)
3125 if (!(TYPE_POLYMORPHIC_P (t
)
3126 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3127 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3128 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3131 dfs_walk_all (TYPE_BINFO (t
),
3132 dfs_declare_virt_assop_and_dtor
,
3136 /* Declare the inheriting constructor for class T inherited from base
3137 constructor CTOR with the parameter array PARMS of size NPARMS. */
3140 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3142 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3144 /* We don't declare an inheriting ctor that would be a default,
3145 copy or move ctor for derived or base. */
3149 && TYPE_REF_P (parms
[0]))
3151 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3152 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3156 tree parmlist
= void_list_node
;
3157 for (int i
= nparms
- 1; i
>= 0; i
--)
3158 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3159 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3160 t
, false, ctor
, parmlist
);
3162 if (add_method (t
, fn
, false))
3164 DECL_CHAIN (fn
) = TYPE_FIELDS (t
);
3165 TYPE_FIELDS (t
) = fn
;
3169 /* Declare all the inheriting constructors for class T inherited from base
3170 constructor CTOR. */
3173 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3175 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3177 if (flag_new_inheriting_ctors
)
3179 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3180 t
, /*const*/false, ctor
, parms
);
3181 add_method (t
, ctor
, using_decl
!= NULL_TREE
);
3185 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3187 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3189 if (TREE_PURPOSE (parms
))
3190 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3191 new_parms
[i
++] = TREE_VALUE (parms
);
3193 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3194 if (parms
== NULL_TREE
)
3196 auto_diagnostic_group d
;
3197 if (warning (OPT_Winherited_variadic_ctor
,
3198 "the ellipsis in %qD is not inherited", ctor
))
3199 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3203 /* Create default constructors, assignment operators, and so forth for
3204 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3205 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3206 the class cannot have a default constructor, copy constructor
3207 taking a const reference argument, or an assignment operator taking
3208 a const reference, respectively. */
3211 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3212 int cant_have_const_cctor
,
3213 int cant_have_const_assignment
)
3216 if (!CLASSTYPE_DESTRUCTOR (t
))
3217 /* In general, we create destructors lazily. */
3218 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3220 bool move_ok
= false;
3221 if (cxx_dialect
>= cxx11
&& CLASSTYPE_LAZY_DESTRUCTOR (t
)
3222 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3223 && !classtype_has_move_assign_or_move_ctor_p (t
, false))
3228 If there is no user-declared constructor for a class, a default
3229 constructor is implicitly declared. */
3230 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3232 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3233 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3234 if (cxx_dialect
>= cxx11
)
3235 TYPE_HAS_CONSTEXPR_CTOR (t
)
3236 /* Don't force the declaration to get a hard answer; if the
3237 definition would have made the class non-literal, it will still be
3238 non-literal because of the base or member in question, and that
3239 gives a better diagnostic. */
3240 = type_maybe_constexpr_default_constructor (t
);
3245 If a class definition does not explicitly declare a copy
3246 constructor, one is declared implicitly. */
3247 if (! TYPE_HAS_COPY_CTOR (t
))
3249 TYPE_HAS_COPY_CTOR (t
) = 1;
3250 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3251 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3253 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3256 /* If there is no assignment operator, one will be created if and
3257 when it is needed. For now, just record whether or not the type
3258 of the parameter to the assignment operator will be a const or
3259 non-const reference. */
3260 if (!TYPE_HAS_COPY_ASSIGN (t
))
3262 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3263 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3264 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3265 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3266 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3269 /* We can't be lazy about declaring functions that might override
3270 a virtual function from a base class. */
3271 declare_virt_assop_and_dtor (t
);
3273 /* If the class definition does not explicitly declare an == operator
3274 function, but declares a defaulted three-way comparison operator function,
3275 an == operator function is declared implicitly. */
3276 if (!classtype_has_op (t
, EQ_EXPR
))
3277 if (tree space
= classtype_has_defaulted_op (t
, SPACESHIP_EXPR
))
3279 tree eq
= implicitly_declare_fn (sfk_comparison
, t
, false, space
,
3281 if (DECL_FRIEND_P (space
))
3282 do_friend (NULL_TREE
, DECL_NAME (eq
), eq
,
3283 NULL_TREE
, NO_SPECIAL
, true);
3286 add_method (t
, eq
, false);
3287 DECL_CHAIN (eq
) = TYPE_FIELDS (t
);
3288 TYPE_FIELDS (t
) = eq
;
3290 maybe_add_class_template_decl_list (t
, eq
, DECL_FRIEND_P (space
));
3293 while (*access_decls
)
3295 tree using_decl
= TREE_VALUE (*access_decls
);
3296 tree decl
= USING_DECL_DECLS (using_decl
);
3297 if (DECL_NAME (using_decl
) == ctor_identifier
)
3299 /* declare, then remove the decl */
3300 tree ctor_list
= decl
;
3301 location_t loc
= input_location
;
3302 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3303 for (ovl_iterator
iter (ctor_list
); iter
; ++iter
)
3304 one_inherited_ctor (*iter
, t
, using_decl
);
3305 *access_decls
= TREE_CHAIN (*access_decls
);
3306 input_location
= loc
;
3309 access_decls
= &TREE_CHAIN (*access_decls
);
3313 /* Cache of enum_min_precision values. */
3314 static GTY((deletable
)) hash_map
<tree
, int> *enum_to_min_precision
;
3316 /* Return the minimum precision of a bit-field needed to store all
3317 enumerators of ENUMERAL_TYPE TYPE. */
3320 enum_min_precision (tree type
)
3322 type
= TYPE_MAIN_VARIANT (type
);
3323 /* For unscoped enums without fixed underlying type and without mode
3324 attribute we can just use precision of the underlying type. */
3325 if (UNSCOPED_ENUM_P (type
)
3326 && !ENUM_FIXED_UNDERLYING_TYPE_P (type
)
3327 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type
)))
3328 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
));
3330 if (enum_to_min_precision
== NULL
)
3331 enum_to_min_precision
= hash_map
<tree
, int>::create_ggc (37);
3334 int &prec
= enum_to_min_precision
->get_or_insert (type
, &existed
);
3338 tree minnode
, maxnode
;
3339 if (TYPE_VALUES (type
))
3341 minnode
= maxnode
= NULL_TREE
;
3342 for (tree values
= TYPE_VALUES (type
);
3343 values
; values
= TREE_CHAIN (values
))
3345 tree decl
= TREE_VALUE (values
);
3346 tree value
= DECL_INITIAL (decl
);
3347 if (value
== error_mark_node
)
3348 value
= integer_zero_node
;
3350 minnode
= maxnode
= value
;
3351 else if (tree_int_cst_lt (maxnode
, value
))
3353 else if (tree_int_cst_lt (value
, minnode
))
3358 minnode
= maxnode
= integer_zero_node
;
3360 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
3361 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
3362 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
3363 prec
= MAX (lowprec
, highprec
);
3367 /* FIELD is a bit-field. We are finishing the processing for its
3368 enclosing type. Issue any appropriate messages and set appropriate
3369 flags. Returns false if an error has been diagnosed. */
3372 check_bitfield_decl (tree field
)
3374 tree type
= TREE_TYPE (field
);
3377 /* Extract the declared width of the bitfield, which has been
3378 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3379 w
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
3380 gcc_assert (w
!= NULL_TREE
);
3381 /* Remove the bit-field width indicator so that the rest of the
3382 compiler does not treat that value as a qualifier. */
3383 DECL_BIT_FIELD_REPRESENTATIVE (field
) = NULL_TREE
;
3385 /* Detect invalid bit-field type. */
3386 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3388 error_at (DECL_SOURCE_LOCATION (field
),
3389 "bit-field %q#D with non-integral type %qT", field
, type
);
3390 w
= error_mark_node
;
3394 location_t loc
= input_location
;
3395 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3398 /* detect invalid field size. */
3399 input_location
= DECL_SOURCE_LOCATION (field
);
3400 w
= cxx_constant_value (w
);
3401 input_location
= loc
;
3403 if (TREE_CODE (w
) != INTEGER_CST
)
3405 error ("bit-field %q+D width not an integer constant", field
);
3406 w
= error_mark_node
;
3408 else if (tree_int_cst_sgn (w
) < 0)
3410 error ("negative width in bit-field %q+D", field
);
3411 w
= error_mark_node
;
3413 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3415 error ("zero width for bit-field %q+D", field
);
3416 w
= error_mark_node
;
3418 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3419 && TREE_CODE (type
) != BOOLEAN_TYPE
3420 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3421 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3422 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3423 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3424 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3425 "width of %qD exceeds its type", field
);
3426 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3428 int prec
= enum_min_precision (type
);
3429 if (compare_tree_int (w
, prec
) < 0)
3430 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3431 "%qD is too small to hold all values of %q#T",
3436 if (w
!= error_mark_node
)
3438 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3439 DECL_BIT_FIELD (field
) = 1;
3444 /* Non-bit-fields are aligned for their type. */
3445 DECL_BIT_FIELD (field
) = 0;
3446 CLEAR_DECL_C_BIT_FIELD (field
);
3451 /* FIELD is a non bit-field. We are finishing the processing for its
3452 enclosing type T. Issue any appropriate messages and set appropriate
3456 check_field_decl (tree field
,
3458 int* cant_have_const_ctor
,
3459 int* no_const_asn_ref
)
3461 tree type
= strip_array_types (TREE_TYPE (field
));
3462 bool any_default_members
= false;
3464 /* In C++98 an anonymous union cannot contain any fields which would change
3465 the settings of CANT_HAVE_CONST_CTOR and friends. */
3466 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3468 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3469 structs. So, we recurse through their fields here. */
3470 else if (ANON_AGGR_TYPE_P (type
))
3472 for (tree fields
= TYPE_FIELDS (type
); fields
;
3473 fields
= DECL_CHAIN (fields
))
3474 if (TREE_CODE (fields
) == FIELD_DECL
)
3475 any_default_members
|= check_field_decl (fields
, t
,
3476 cant_have_const_ctor
,
3479 /* Check members with class type for constructors, destructors,
3481 else if (CLASS_TYPE_P (type
))
3483 /* Never let anything with uninheritable virtuals
3484 make it through without complaint. */
3485 abstract_virtuals_error (field
, type
);
3487 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3490 int oldcount
= errorcount
;
3491 if (TYPE_NEEDS_CONSTRUCTING (type
))
3492 error ("member %q+#D with constructor not allowed in union",
3494 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3495 error ("member %q+#D with destructor not allowed in union", field
);
3496 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3497 error ("member %q+#D with copy assignment operator not allowed in union",
3499 if (!warned
&& errorcount
> oldcount
)
3501 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3502 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3508 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3509 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3510 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3511 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3512 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3513 || !TYPE_HAS_COPY_ASSIGN (type
));
3514 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3515 || !TYPE_HAS_COPY_CTOR (type
));
3516 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3517 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3518 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3519 || TYPE_HAS_COMPLEX_DFLT (type
));
3522 if (TYPE_HAS_COPY_CTOR (type
)
3523 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3524 *cant_have_const_ctor
= 1;
3526 if (TYPE_HAS_COPY_ASSIGN (type
)
3527 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3528 *no_const_asn_ref
= 1;
3531 check_abi_tags (t
, field
);
3533 if (DECL_INITIAL (field
) != NULL_TREE
)
3534 /* `build_class_init_list' does not recognize
3536 any_default_members
= true;
3538 return any_default_members
;
3541 /* Check the data members (both static and non-static), class-scoped
3542 typedefs, etc., appearing in the declaration of T. Issue
3543 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3544 declaration order) of access declarations; each TREE_VALUE in this
3545 list is a USING_DECL.
3547 In addition, set the following flags:
3550 The class is empty, i.e., contains no non-static data members.
3552 CANT_HAVE_CONST_CTOR_P
3553 This class cannot have an implicitly generated copy constructor
3554 taking a const reference.
3556 CANT_HAVE_CONST_ASN_REF
3557 This class cannot have an implicitly generated assignment
3558 operator taking a const reference.
3560 All of these flags should be initialized before calling this
3564 check_field_decls (tree t
, tree
*access_decls
,
3565 int *cant_have_const_ctor_p
,
3566 int *no_const_asn_ref_p
)
3570 /* Assume there are no access declarations. */
3571 *access_decls
= NULL_TREE
;
3572 /* Effective C has things to say about classes with pointer members. */
3573 tree pointer_member
= NULL_TREE
;
3574 /* Default initialized members affect the whole class. */
3575 tree default_init_member
= NULL_TREE
;
3576 /* Lack of any non-static data member of non-volatile literal
3577 type affects a union. */
3578 bool found_nv_literal_p
= false;
3579 /* Standard layout requires all FIELDS have same access. */
3580 int field_access
= -1;
3582 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3584 tree type
= TREE_TYPE (field
);
3586 switch (TREE_CODE (field
))
3592 /* Save the access declarations for our caller. */
3593 *access_decls
= tree_cons (NULL_TREE
, field
, *access_decls
);
3601 /* FIXME: We should fold in the checking from check_methods. */
3605 DECL_NONLOCAL (field
) = 1;
3609 if (TREE_CODE (t
) == UNION_TYPE
3610 && cxx_dialect
< cxx11
)
3614 (C++98) If a union contains a static data member,
3615 ... the program is ill-formed. */
3616 if (cxx_dialect
< cxx11
)
3617 error ("in C++98 %q+D may not be static because it is "
3618 "a member of a union", field
);
3623 if (TREE_CODE (t
) == UNION_TYPE
)
3627 If a union contains ... or a [non-static data] member
3628 of reference type, the program is ill-formed. */
3629 if (TYPE_REF_P (type
))
3630 error ("non-static data member %q+D in a union may not "
3631 "have reference type %qT", field
, type
);
3635 /* Common VAR_DECL & FIELD_DECL processing. */
3636 DECL_CONTEXT (field
) = t
;
3637 DECL_NONLOCAL (field
) = 1;
3639 /* Template instantiation can cause this. Perhaps this
3640 should be a specific instantiation check? */
3641 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3643 error ("data member %q+D invalidly declared function type", field
);
3644 type
= build_pointer_type (type
);
3645 TREE_TYPE (field
) = type
;
3647 else if (TREE_CODE (type
) == METHOD_TYPE
)
3649 error ("data member %q+D invalidly declared method type", field
);
3650 type
= build_pointer_type (type
);
3651 TREE_TYPE (field
) = type
;
3657 if (TREE_CODE (field
) != FIELD_DECL
)
3660 if (type
== error_mark_node
)
3663 /* If it is not a union and at least one non-static data member is
3664 non-literal, the whole class becomes non-literal. Per Core/1453,
3665 volatile non-static data members and base classes are also not allowed.
3666 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3668 Note: if the type is incomplete we will complain later on. */
3669 if (COMPLETE_TYPE_P (type
))
3671 if (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
))
3672 CLASSTYPE_LITERAL_P (t
) = false;
3674 found_nv_literal_p
= true;
3677 int this_field_access
= (TREE_PROTECTED (field
) ? 1
3678 : TREE_PRIVATE (field
) ? 2 : 0);
3679 if (field_access
!= this_field_access
)
3681 /* A standard-layout class is a class that:
3683 ... has the same access control (Clause 11) for all
3684 non-static data members, */
3685 if (field_access
< 0)
3686 field_access
= this_field_access
;
3688 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3690 /* Aggregates must be public. */
3691 if (this_field_access
)
3692 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3695 /* If this is of reference type, check if it needs an init. */
3696 if (TYPE_REF_P (type
))
3698 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3699 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3700 if (DECL_INITIAL (field
) == NULL_TREE
)
3701 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3702 if (cxx_dialect
< cxx11
)
3704 /* ARM $12.6.2: [A member initializer list] (or, for an
3705 aggregate, initialization by a brace-enclosed list) is the
3706 only way to initialize non-static const and reference
3708 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3709 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3713 type
= strip_array_types (type
);
3715 if (TYPE_PACKED (t
))
3717 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3719 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3720 "ignoring packed attribute because of"
3721 " unpacked non-POD field %q#D", field
);
3724 else if (DECL_C_BIT_FIELD (field
)
3725 || TYPE_ALIGN (TREE_TYPE (field
)) > BITS_PER_UNIT
)
3726 DECL_PACKED (field
) = 1;
3729 if (DECL_C_BIT_FIELD (field
)
3730 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field
)))
3731 /* We don't treat zero-width bitfields as making a class
3734 else if (field_poverlapping_p (field
)
3735 && is_empty_class (TREE_TYPE (field
)))
3736 /* Empty data members also don't make a class non-empty. */
3737 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3740 /* The class is non-empty. */
3741 CLASSTYPE_EMPTY_P (t
) = 0;
3742 /* The class is not even nearly empty. */
3743 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3744 /* If one of the data members contains an empty class, so
3746 if (CLASS_TYPE_P (type
)
3747 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3748 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3751 /* This is used by -Weffc++ (see below). Warn only for pointers
3752 to members which might hold dynamic memory. So do not warn
3753 for pointers to functions or pointers to members. */
3754 if (TYPE_PTR_P (type
)
3755 && !TYPE_PTRFN_P (type
))
3756 pointer_member
= field
;
3758 if (CLASS_TYPE_P (type
))
3760 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3761 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3762 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3763 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3766 if (DECL_MUTABLE_P (field
) || TYPE_HAS_MUTABLE_P (type
))
3767 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3769 if (DECL_MUTABLE_P (field
))
3771 if (TYPE_REF_P (type
))
3772 error ("member %q+D cannot be declared as a %<mutable%> "
3773 "reference", field
);
3774 else if (CP_TYPE_CONST_P (type
))
3775 error ("member %q+D cannot be declared both %<const%> "
3776 "and %<mutable%>", field
);
3779 if (! layout_pod_type_p (type
))
3780 /* DR 148 now allows pointers to members (which are POD themselves),
3781 to be allowed in POD structs. */
3782 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3784 if (field_poverlapping_p (field
))
3785 /* A potentially-overlapping non-static data member makes the class
3787 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3789 if (!std_layout_type_p (type
))
3790 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3792 if (! zero_init_p (type
))
3793 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3795 /* We set DECL_C_BIT_FIELD in grokbitfield.
3796 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3797 if (DECL_C_BIT_FIELD (field
))
3798 check_bitfield_decl (field
);
3800 if (check_field_decl (field
, t
,
3801 cant_have_const_ctor_p
, no_const_asn_ref_p
))
3803 if (default_init_member
3804 && TREE_CODE (t
) == UNION_TYPE
)
3806 error ("multiple fields in union %qT initialized", t
);
3807 inform (DECL_SOURCE_LOCATION (default_init_member
),
3808 "initialized member %q+D declared here",
3809 default_init_member
);
3811 default_init_member
= field
;
3814 /* Now that we've removed bit-field widths from DECL_INITIAL,
3815 anything left in DECL_INITIAL is an NSDMI that makes the class
3816 non-aggregate in C++11. */
3817 if (DECL_INITIAL (field
) && cxx_dialect
< cxx14
)
3818 CLASSTYPE_NON_AGGREGATE (t
) = true;
3820 if (CP_TYPE_CONST_P (type
))
3822 /* If any field is const, the structure type is pseudo-const. */
3823 C_TYPE_FIELDS_READONLY (t
) = 1;
3824 if (DECL_INITIAL (field
) == NULL_TREE
)
3825 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3826 if (cxx_dialect
< cxx11
)
3828 /* ARM $12.6.2: [A member initializer list] (or, for an
3829 aggregate, initialization by a brace-enclosed list) is the
3830 only way to initialize non-static const and reference
3832 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3833 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3836 /* A field that is pseudo-const makes the structure likewise. */
3837 else if (CLASS_TYPE_P (type
))
3839 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3840 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3841 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3842 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3845 /* Core issue 80: A non-static data member is required to have a
3846 different name from the class iff the class has a
3847 user-declared constructor. */
3848 if (constructor_name_p (DECL_NAME (field
), t
)
3849 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3850 permerror (DECL_SOURCE_LOCATION (field
),
3851 "field %q#D with same name as class", field
);
3854 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3855 one of its non-static data members is of non-volatile literal type. */
3856 if (TREE_CODE (t
) == UNION_TYPE
&& found_nv_literal_p
)
3857 CLASSTYPE_LITERAL_P (t
) = true;
3859 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3860 it should also define a copy constructor and an assignment operator to
3861 implement the correct copy semantic (deep vs shallow, etc.). As it is
3862 not feasible to check whether the constructors do allocate dynamic memory
3863 and store it within members, we approximate the warning like this:
3865 -- Warn only if there are members which are pointers
3866 -- Warn only if there is a non-trivial constructor (otherwise,
3867 there cannot be memory allocated).
3868 -- Warn only if there is a non-trivial destructor. We assume that the
3869 user at least implemented the cleanup correctly, and a destructor
3870 is needed to free dynamic memory.
3872 This seems enough for practical purposes. */
3875 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3876 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3877 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3879 if (warning (OPT_Weffc__
, "%q#T has pointer data members", t
))
3881 if (! TYPE_HAS_COPY_CTOR (t
))
3883 warning (OPT_Weffc__
,
3884 " but does not declare %<%T(const %T&)%>", t
, t
);
3885 if (!TYPE_HAS_COPY_ASSIGN (t
))
3886 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3888 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3889 warning (OPT_Weffc__
,
3890 " but does not declare %<operator=(const %T&)%>", t
);
3891 inform (DECL_SOURCE_LOCATION (pointer_member
),
3892 "pointer member %q+D declared here", pointer_member
);
3896 /* Non-static data member initializers make the default constructor
3898 if (default_init_member
)
3900 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3901 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3904 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3906 TYPE_PACKED (t
) = 0;
3908 /* Check anonymous struct/anonymous union fields. */
3909 finish_struct_anon (t
);
3911 /* We've built up the list of access declarations in reverse order.
3913 *access_decls
= nreverse (*access_decls
);
3916 /* If TYPE is an empty class type, records its OFFSET in the table of
3920 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3924 if (!is_empty_class (type
))
3927 /* Record the location of this empty object in OFFSETS. */
3928 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3930 n
= splay_tree_insert (offsets
,
3931 (splay_tree_key
) offset
,
3932 (splay_tree_value
) NULL_TREE
);
3933 n
->value
= ((splay_tree_value
)
3934 tree_cons (NULL_TREE
,
3941 /* Returns nonzero if TYPE is an empty class type and there is
3942 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3945 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3950 if (!is_empty_class (type
))
3953 /* Record the location of this empty object in OFFSETS. */
3954 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3958 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3959 if (same_type_p (TREE_VALUE (t
), type
))
3965 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3966 F for every subobject, passing it the type, offset, and table of
3967 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3970 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3971 than MAX_OFFSET will not be walked.
3973 If F returns a nonzero value, the traversal ceases, and that value
3974 is returned. Otherwise, returns zero. */
3977 walk_subobject_offsets (tree type
,
3978 subobject_offset_fn f
,
3985 tree type_binfo
= NULL_TREE
;
3987 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3989 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3992 if (type
== error_mark_node
)
3998 type
= BINFO_TYPE (type
);
4001 if (CLASS_TYPE_P (type
))
4007 /* Avoid recursing into objects that are not interesting. */
4008 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
4011 /* Record the location of TYPE. */
4012 r
= (*f
) (type
, offset
, offsets
);
4016 /* Iterate through the direct base classes of TYPE. */
4018 type_binfo
= TYPE_BINFO (type
);
4019 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
4023 if (BINFO_VIRTUAL_P (binfo
))
4027 /* We cannot rely on BINFO_OFFSET being set for the base
4028 class yet, but the offsets for direct non-virtual
4029 bases can be calculated by going back to the TYPE. */
4030 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
4031 binfo_offset
= size_binop (PLUS_EXPR
,
4033 BINFO_OFFSET (orig_binfo
));
4035 r
= walk_subobject_offsets (binfo
,
4045 if (CLASSTYPE_VBASECLASSES (type
))
4048 vec
<tree
, va_gc
> *vbases
;
4050 /* Iterate through the virtual base classes of TYPE. In G++
4051 3.2, we included virtual bases in the direct base class
4052 loop above, which results in incorrect results; the
4053 correct offsets for virtual bases are only known when
4054 working with the most derived type. */
4056 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
4057 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
4059 r
= walk_subobject_offsets (binfo
,
4061 size_binop (PLUS_EXPR
,
4063 BINFO_OFFSET (binfo
)),
4072 /* We still have to walk the primary base, if it is
4073 virtual. (If it is non-virtual, then it was walked
4075 tree vbase
= get_primary_binfo (type_binfo
);
4077 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
4078 && BINFO_PRIMARY_P (vbase
)
4079 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
4081 r
= (walk_subobject_offsets
4083 offsets
, max_offset
, /*vbases_p=*/0));
4090 /* Iterate through the fields of TYPE. */
4091 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4092 if (TREE_CODE (field
) == FIELD_DECL
4093 && TREE_TYPE (field
) != error_mark_node
4094 && !DECL_ARTIFICIAL (field
))
4098 field_offset
= byte_position (field
);
4100 r
= walk_subobject_offsets (TREE_TYPE (field
),
4102 size_binop (PLUS_EXPR
,
4112 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4114 tree element_type
= strip_array_types (type
);
4115 tree domain
= TYPE_DOMAIN (type
);
4118 /* Avoid recursing into objects that are not interesting. */
4119 if (!CLASS_TYPE_P (element_type
)
4120 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
4122 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
4125 /* Step through each of the elements in the array. */
4126 for (index
= size_zero_node
;
4127 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
4128 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
4130 r
= walk_subobject_offsets (TREE_TYPE (type
),
4138 offset
= size_binop (PLUS_EXPR
, offset
,
4139 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
4140 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4141 there's no point in iterating through the remaining
4142 elements of the array. */
4143 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4151 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4154 field_poverlapping_p (tree decl
)
4156 /* Base fields are actually potentially overlapping, but C++ bases go through
4157 a different code path based on binfos, and ObjC++ base fields are laid out
4158 in objc-act, so we don't want layout_class_type to mess with them. */
4159 if (DECL_FIELD_IS_BASE (decl
))
4161 gcc_checking_assert (c_dialect_objc ());
4165 return lookup_attribute ("no_unique_address",
4166 DECL_ATTRIBUTES (decl
));
4169 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4172 record_subobject_offsets (tree decl_or_binfo
,
4176 bool overlapping
, vbases_p
;
4178 if (DECL_P (decl_or_binfo
))
4180 tree decl
= decl_or_binfo
;
4181 type
= TREE_TYPE (decl
);
4182 offset
= byte_position (decl
);
4183 overlapping
= field_poverlapping_p (decl
);
4188 type
= BINFO_TYPE (decl_or_binfo
);
4189 offset
= BINFO_OFFSET (decl_or_binfo
);
4195 /* If recording subobjects for a non-static data member or a
4196 non-empty base class, we do not need to record offsets beyond
4197 the size of the biggest empty class. Additional data members
4198 will go at the end of the class. Additional base classes will go
4199 either at offset zero (if empty, in which case they cannot
4200 overlap with offsets past the size of the biggest empty class) or
4201 at the end of the class.
4203 However, if we are placing an empty base class, then we must record
4204 all offsets, as either the empty class is at offset zero (where
4205 other empty classes might later be placed) or at the end of the
4206 class (where other objects might then be placed, so other empty
4207 subobjects might later overlap). */
4209 || !is_empty_class (type
))
4210 max_offset
= sizeof_biggest_empty_class
;
4212 max_offset
= NULL_TREE
;
4213 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4214 offsets
, max_offset
, vbases_p
);
4217 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4218 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4219 virtual bases of TYPE are examined. */
4222 layout_conflict_p (tree type
,
4227 splay_tree_node max_node
;
4229 /* Get the node in OFFSETS that indicates the maximum offset where
4230 an empty subobject is located. */
4231 max_node
= splay_tree_max (offsets
);
4232 /* If there aren't any empty subobjects, then there's no point in
4233 performing this check. */
4237 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4238 offsets
, (tree
) (max_node
->key
),
4242 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4243 non-static data member of the type indicated by RLI. BINFO is the
4244 binfo corresponding to the base subobject, OFFSETS maps offsets to
4245 types already located at those offsets. This function determines
4246 the position of the DECL. */
4249 layout_nonempty_base_or_field (record_layout_info rli
,
4254 tree offset
= NULL_TREE
;
4260 /* For the purposes of determining layout conflicts, we want to
4261 use the class type of BINFO; TREE_TYPE (DECL) will be the
4262 CLASSTYPE_AS_BASE version, which does not contain entries for
4263 zero-sized bases. */
4264 type
= TREE_TYPE (binfo
);
4269 type
= TREE_TYPE (decl
);
4273 /* Try to place the field. It may take more than one try if we have
4274 a hard time placing the field without putting two objects of the
4275 same type at the same address. */
4278 struct record_layout_info_s old_rli
= *rli
;
4280 /* Place this field. */
4281 place_field (rli
, decl
);
4282 offset
= byte_position (decl
);
4284 /* We have to check to see whether or not there is already
4285 something of the same type at the offset we're about to use.
4286 For example, consider:
4289 struct T : public S { int i; };
4290 struct U : public S, public T {};
4292 Here, we put S at offset zero in U. Then, we can't put T at
4293 offset zero -- its S component would be at the same address
4294 as the S we already allocated. So, we have to skip ahead.
4295 Since all data members, including those whose type is an
4296 empty class, have nonzero size, any overlap can happen only
4297 with a direct or indirect base-class -- it can't happen with
4299 /* In a union, overlap is permitted; all members are placed at
4301 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4303 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4306 /* Strip off the size allocated to this field. That puts us
4307 at the first place we could have put the field with
4308 proper alignment. */
4311 /* Bump up by the alignment required for the type. */
4313 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4315 ? CLASSTYPE_ALIGN (type
)
4316 : TYPE_ALIGN (type
)));
4317 normalize_rli (rli
);
4319 else if (TREE_CODE (type
) == NULLPTR_TYPE
4320 && warn_abi
&& abi_version_crosses (9))
4322 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4323 the offset wasn't aligned like a pointer when we started to
4324 layout this field, that affects its position. */
4325 tree pos
= rli_size_unit_so_far (&old_rli
);
4326 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4328 if (abi_version_at_least (9))
4329 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4330 "alignment of %qD increased in %<-fabi-version=9%> "
4333 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4334 "of %qD will increase in %<-fabi-version=9%>",
4340 /* There was no conflict. We're done laying out this field. */
4344 /* Now that we know where it will be placed, update its
4346 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4347 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4348 this point because their BINFO_OFFSET is copied from another
4349 hierarchy. Therefore, we may not need to add the entire
4351 propagate_binfo_offsets (binfo
,
4352 size_diffop_loc (input_location
,
4353 fold_convert (ssizetype
, offset
),
4354 fold_convert (ssizetype
,
4355 BINFO_OFFSET (binfo
))));
4358 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4361 empty_base_at_nonzero_offset_p (tree type
,
4363 splay_tree
/*offsets*/)
4365 return is_empty_class (type
) && !integer_zerop (offset
);
4368 /* Layout the empty base BINFO. EOC indicates the byte currently just
4369 past the end of the class, and should be correctly aligned for a
4370 class of the type indicated by BINFO; OFFSETS gives the offsets of
4371 the empty bases allocated so far. T is the most derived
4372 type. Return nonzero iff we added it at the end. */
4375 layout_empty_base_or_field (record_layout_info rli
, tree binfo_or_decl
,
4380 tree binfo
= NULL_TREE
;
4381 tree decl
= NULL_TREE
;
4383 if (TREE_CODE (binfo_or_decl
) == TREE_BINFO
)
4385 binfo
= binfo_or_decl
;
4386 type
= BINFO_TYPE (binfo
);
4390 decl
= binfo_or_decl
;
4391 type
= TREE_TYPE (decl
);
4394 /* On some platforms (ARM), even empty classes will not be
4396 tree eoc
= round_up_loc (input_location
,
4397 rli_size_unit_so_far (rli
),
4398 CLASSTYPE_ALIGN_UNIT (type
));
4400 /* This routine should only be used for empty classes. */
4401 gcc_assert (is_empty_class (type
));
4403 if (decl
&& DECL_USER_ALIGN (decl
))
4404 alignment
= size_int (DECL_ALIGN_UNIT (decl
));
4406 alignment
= size_int (CLASSTYPE_ALIGN_UNIT (type
));
4408 /* This is an empty base class. We first try to put it at offset
4410 tree offset
= size_zero_node
;
4411 if (TREE_CODE (rli
->t
) != UNION_TYPE
4412 && layout_conflict_p (type
,
4417 /* That didn't work. Now, we move forward from the next
4418 available spot in the class. */
4423 if (!layout_conflict_p (type
,
4427 /* We finally found a spot where there's no overlap. */
4430 /* There's overlap here, too. Bump along to the next spot. */
4431 offset
= size_binop (PLUS_EXPR
, offset
, alignment
);
4435 if (decl
&& DECL_USER_ALIGN (decl
))
4437 rli
->record_align
= MAX (rli
->record_align
, DECL_ALIGN (decl
));
4439 rli
->unpacked_align
= MAX (rli
->unpacked_align
, DECL_ALIGN (decl
));
4440 TYPE_USER_ALIGN (rli
->t
) = 1;
4442 else if (CLASSTYPE_USER_ALIGN (type
))
4444 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (type
));
4446 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (type
));
4447 TYPE_USER_ALIGN (rli
->t
) = 1;
4451 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4452 propagate_binfo_offsets (binfo
,
4453 size_diffop (offset
, BINFO_OFFSET (binfo
)));
4456 DECL_FIELD_OFFSET (decl
) = offset
;
4457 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4458 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4464 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4465 fields at NEXT_FIELD, and return it. */
4468 build_base_field_1 (tree t
, tree binfo
, tree access
, tree
*&next_field
)
4470 /* Create the FIELD_DECL. */
4471 tree basetype
= BINFO_TYPE (binfo
);
4472 tree as_base
= CLASSTYPE_AS_BASE (basetype
);
4473 gcc_assert (as_base
);
4474 tree decl
= build_decl (input_location
, FIELD_DECL
, NULL_TREE
, as_base
);
4476 DECL_ARTIFICIAL (decl
) = 1;
4477 DECL_IGNORED_P (decl
) = 1;
4478 DECL_FIELD_CONTEXT (decl
) = t
;
4479 if (is_empty_class (basetype
))
4480 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4481 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = size_zero_node
;
4484 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4485 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4487 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4488 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4489 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4490 DECL_FIELD_IS_BASE (decl
) = 1;
4492 if (access
== access_private_node
)
4493 TREE_PRIVATE (decl
) = true;
4494 else if (access
== access_protected_node
)
4495 TREE_PROTECTED (decl
) = true;
4497 /* Add the new FIELD_DECL to the list of fields for T. */
4498 DECL_CHAIN (decl
) = *next_field
;
4500 next_field
= &DECL_CHAIN (decl
);
4505 /* Layout the base given by BINFO in the class indicated by RLI.
4506 *BASE_ALIGN is a running maximum of the alignments of
4507 any base class. OFFSETS gives the location of empty base
4508 subobjects. T is the most derived type. Return nonzero if the new
4509 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4510 *NEXT_FIELD, unless BINFO is for an empty base class.
4512 Returns the location at which the next field should be inserted. */
4515 build_base_field (record_layout_info rli
, tree binfo
, tree access
,
4516 splay_tree offsets
, tree
*next_field
)
4519 tree basetype
= BINFO_TYPE (binfo
);
4521 if (!COMPLETE_TYPE_P (basetype
))
4522 /* This error is now reported in xref_tag, thus giving better
4523 location information. */
4526 /* Place the base class. */
4527 if (!is_empty_class (basetype
))
4531 /* The containing class is non-empty because it has a non-empty
4533 CLASSTYPE_EMPTY_P (t
) = 0;
4535 /* Create the FIELD_DECL. */
4536 decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4538 /* Try to place the field. It may take more than one try if we
4539 have a hard time placing the field without putting two
4540 objects of the same type at the same address. */
4541 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4545 bool atend
= layout_empty_base_or_field (rli
, binfo
, offsets
);
4546 /* A nearly-empty class "has no proper base class that is empty,
4547 not morally virtual, and at an offset other than zero." */
4548 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4551 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4552 /* The check above (used in G++ 3.2) is insufficient because
4553 an empty class placed at offset zero might itself have an
4554 empty base at a nonzero offset. */
4555 else if (walk_subobject_offsets (basetype
,
4556 empty_base_at_nonzero_offset_p
,
4559 /*max_offset=*/NULL_TREE
,
4561 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4564 /* We used to not create a FIELD_DECL for empty base classes because of
4565 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4566 be a problem anymore. We need them to handle initialization of C++17
4568 if (cxx_dialect
>= cxx17
&& !BINFO_VIRTUAL_P (binfo
))
4570 tree decl
= build_base_field_1 (t
, binfo
, access
, next_field
);
4571 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4572 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4573 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4574 DECL_FIELD_ABI_IGNORED (decl
) = 1;
4577 /* An empty virtual base causes a class to be non-empty
4578 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4579 here because that was already done when the virtual table
4580 pointer was created. */
4583 /* Record the offsets of BINFO and its base subobjects. */
4584 record_subobject_offsets (binfo
, offsets
);
4589 /* Layout all of the non-virtual base classes. Record empty
4590 subobjects in OFFSETS. T is the most derived type. Return nonzero
4591 if the type cannot be nearly empty. The fields created
4592 corresponding to the base classes will be inserted at
4596 build_base_fields (record_layout_info rli
,
4597 splay_tree offsets
, tree
*next_field
)
4599 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4602 tree binfo
= TYPE_BINFO (t
);
4603 int n_baseclasses
= BINFO_N_BASE_BINFOS (binfo
);
4605 /* The primary base class is always allocated first. */
4606 const tree primary_binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
4609 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4610 base, if it is direct. Indirect base fields are private. */
4611 tree primary_access
= access_private_node
;
4612 for (int i
= 0; i
< n_baseclasses
; ++i
)
4614 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4615 if (base_binfo
== primary_binfo
)
4617 primary_access
= BINFO_BASE_ACCESS (binfo
, i
);
4621 next_field
= build_base_field (rli
, primary_binfo
,
4623 offsets
, next_field
);
4626 /* Now allocate the rest of the bases. */
4627 for (int i
= 0; i
< n_baseclasses
; ++i
)
4629 tree base_binfo
= BINFO_BASE_BINFO (binfo
, i
);
4631 /* The primary base was already allocated above, so we don't
4632 need to allocate it again here. */
4633 if (base_binfo
== primary_binfo
)
4636 /* Virtual bases are added at the end (a primary virtual base
4637 will have already been added). */
4638 if (BINFO_VIRTUAL_P (base_binfo
))
4641 next_field
= build_base_field (rli
, base_binfo
,
4642 BINFO_BASE_ACCESS (binfo
, i
),
4643 offsets
, next_field
);
4647 /* Go through the TYPE_FIELDS of T issuing any appropriate
4648 diagnostics, figuring out which methods override which other
4649 methods, and so forth. */
4652 check_methods (tree t
)
4654 for (tree x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
4655 if (DECL_DECLARES_FUNCTION_P (x
))
4657 check_for_override (x
, t
);
4659 if (DECL_PURE_VIRTUAL_P (x
)
4660 && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4661 error ("initializer specified for non-virtual method %q+D", x
);
4662 /* The name of the field is the original field name
4663 Save this in auxiliary field for later overloading. */
4664 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4666 TYPE_POLYMORPHIC_P (t
) = 1;
4667 if (DECL_PURE_VIRTUAL_P (x
))
4668 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4671 if (!DECL_VIRTUAL_P (x
)
4672 && lookup_attribute ("transaction_safe_dynamic",
4673 DECL_ATTRIBUTES (x
)))
4674 error_at (DECL_SOURCE_LOCATION (x
),
4675 "%<transaction_safe_dynamic%> may only be specified for "
4676 "a virtual function");
4679 /* Check whether the eligible special member functions (P0848) are
4680 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4681 has the eligible ones; TYPE_FIELDS also contains ineligible overloads,
4682 which is why this needs to be separate from the loop above. */
4684 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
4686 if (TREE_CODE (dtor
) == OVERLOAD
)
4688 /* P0848: At the end of the definition of a class, overload
4689 resolution is performed among the prospective destructors declared
4690 in that class with an empty argument list to select the destructor
4691 for the class, also known as the selected destructor. The program
4692 is ill-formed if overload resolution fails. */
4693 auto_diagnostic_group d
;
4694 error_at (location_of (t
), "destructor for %qT is ambiguous", t
);
4695 print_candidates (dtor
);
4697 else if (user_provided_p (dtor
))
4698 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = true;
4701 for (ovl_iterator
i (CLASSTYPE_CONSTRUCTORS (t
)); i
; ++i
)
4704 if (!user_provided_p (fn
))
4705 /* Might be trivial. */;
4706 else if (copy_fn_p (fn
))
4707 TYPE_HAS_COMPLEX_COPY_CTOR (t
) = true;
4708 else if (move_fn_p (fn
))
4709 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) = true;
4712 for (ovl_iterator
i (get_class_binding_direct (t
, assign_op_identifier
));
4716 if (!user_provided_p (fn
))
4717 /* Might be trivial. */;
4718 else if (copy_fn_p (fn
))
4719 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = true;
4720 else if (move_fn_p (fn
))
4721 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = true;
4725 /* FN is constructor, destructor or operator function. Clone the
4726 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4727 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4730 copy_fndecl_with_name (tree fn
, tree name
, tree_code code
,
4731 bool need_vtt_parm_p
, bool omit_inherited_parms_p
)
4733 /* Copy the function. */
4734 tree clone
= copy_decl (fn
);
4735 /* Reset the function name. */
4736 DECL_NAME (clone
) = name
;
4739 /* Clone constraints. */
4740 if (tree ci
= get_constraints (fn
))
4741 set_constraints (clone
, copy_node (ci
));
4743 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4744 /* There's no pending inline data for this function. */
4745 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4746 DECL_PENDING_INLINE_P (clone
) = 0;
4748 if (name
== base_dtor_identifier
)
4750 /* The base-class destructor is not virtual. */
4751 DECL_VIRTUAL_P (clone
) = 0;
4752 DECL_VINDEX (clone
) = NULL_TREE
;
4754 else if (code
!= ERROR_MARK
)
4756 /* Set the operator code. */
4757 const ovl_op_info_t
*ovl_op
= OVL_OP_INFO (false, code
);
4758 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone
) = ovl_op
->ovl_op_code
;
4760 /* The operator could be virtual. */
4761 if (DECL_VIRTUAL_P (clone
))
4762 IDENTIFIER_VIRTUAL_P (name
) = true;
4765 if (omit_inherited_parms_p
)
4766 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone
));
4768 /* If there was an in-charge parameter, drop it from the function
4770 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4772 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4773 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4774 /* Skip the `this' parameter. */
4775 parmtypes
= TREE_CHAIN (parmtypes
);
4776 /* Skip the in-charge parameter. */
4777 parmtypes
= TREE_CHAIN (parmtypes
);
4778 /* And the VTT parm, in a complete [cd]tor. */
4779 if (DECL_HAS_VTT_PARM_P (fn
) && !need_vtt_parm_p
)
4780 parmtypes
= TREE_CHAIN (parmtypes
);
4781 if (omit_inherited_parms_p
)
4783 /* If we're omitting inherited parms, that just leaves the VTT. */
4784 gcc_assert (need_vtt_parm_p
);
4785 parmtypes
= tree_cons (NULL_TREE
, vtt_parm_type
, void_list_node
);
4788 = build_method_type_directly (basetype
,
4789 TREE_TYPE (TREE_TYPE (clone
)),
4792 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4793 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4795 = cxx_copy_lang_qualifiers (TREE_TYPE (clone
), TREE_TYPE (fn
));
4798 /* Copy the function parameters. */
4799 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4801 /* Remove the in-charge parameter. */
4802 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4804 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4805 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4806 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4809 /* And the VTT parm, in a complete [cd]tor. */
4810 if (DECL_HAS_VTT_PARM_P (fn
))
4812 if (need_vtt_parm_p
)
4813 DECL_HAS_VTT_PARM_P (clone
) = 1;
4816 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4817 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4818 DECL_HAS_VTT_PARM_P (clone
) = 0;
4822 /* A base constructor inheriting from a virtual base doesn't get the
4824 if (omit_inherited_parms_p
)
4825 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
4827 for (tree parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4829 DECL_CONTEXT (parms
) = clone
;
4830 cxx_dup_lang_specific_decl (parms
);
4833 /* Create the RTL for this function. */
4834 SET_DECL_RTL (clone
, NULL
);
4835 rest_of_decl_compilation (clone
, namespace_bindings_p (), at_eof
);
4840 /* FN is an operator function, create a variant for CODE. */
4843 copy_operator_fn (tree fn
, tree_code code
)
4845 return copy_fndecl_with_name (fn
, ovl_op_identifier (code
),
4846 code
, false, false);
4849 /* FN is a constructor or destructor. Clone the declaration to create
4850 a specialized in-charge or not-in-charge version, as indicated by
4854 build_clone (tree fn
, tree name
, bool need_vtt_parm_p
,
4855 bool omit_inherited_parms_p
)
4859 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4860 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4862 clone
= copy_decl (fn
);
4863 DECL_NAME (clone
) = name
;
4865 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
,
4866 need_vtt_parm_p
, omit_inherited_parms_p
);
4867 DECL_TEMPLATE_RESULT (clone
) = result
;
4869 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4870 DECL_TI_TEMPLATE (result
) = clone
;
4872 TREE_TYPE (clone
) = TREE_TYPE (result
);
4876 clone
= copy_fndecl_with_name (fn
, name
, ERROR_MARK
,
4877 need_vtt_parm_p
, omit_inherited_parms_p
);
4878 DECL_CLONED_FUNCTION (clone
) = fn
;
4881 /* Remember where this function came from. */
4882 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4884 /* Make it easy to find the CLONE given the FN. Note the
4885 template_result of a template will be chained this way too. */
4886 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4887 DECL_CHAIN (fn
) = clone
;
4892 /* Build the clones of FN, return the number of clones built. These
4893 will be inserted onto DECL_CHAIN of FN. */
4896 build_cdtor_clones (tree fn
, bool needs_vtt_parm_p
, bool omit_inherited_parms_p
)
4900 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4902 /* For each constructor, we need two variants: an in-charge version
4903 and a not-in-charge version. */
4904 build_clone (fn
, complete_ctor_identifier
, false, false);
4905 build_clone (fn
, base_ctor_identifier
, needs_vtt_parm_p
,
4906 omit_inherited_parms_p
);
4911 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4913 /* For each destructor, we need three variants: an in-charge
4914 version, a not-in-charge version, and an in-charge deleting
4915 version. We clone the deleting version first because that
4916 means it will go second on the TYPE_FIELDS list -- and that
4917 corresponds to the correct layout order in the virtual
4920 For a non-virtual destructor, we do not build a deleting
4922 if (DECL_VIRTUAL_P (fn
))
4924 build_clone (fn
, deleting_dtor_identifier
, false, false);
4927 build_clone (fn
, complete_dtor_identifier
, false, false);
4928 build_clone (fn
, base_dtor_identifier
, needs_vtt_parm_p
, false);
4935 /* Produce declarations for all appropriate clones of FN. If
4936 UPDATE_METHODS is true, the clones are added to the
4937 CLASSTYPE_MEMBER_VEC. */
4940 clone_cdtor (tree fn
, bool update_methods
)
4942 /* Avoid inappropriate cloning. */
4944 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4947 /* Base cdtors need a vtt parm if there are virtual bases. */
4948 bool vtt
= CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
));
4950 /* Base ctor omits inherited parms it needs a vttparm and inherited
4951 from a virtual nase ctor. */
4952 bool omit_inherited
= ctor_omit_inherited_parms (fn
);
4954 unsigned count
= build_cdtor_clones (fn
, vtt
, omit_inherited
);
4956 /* Note that this is an abstract function that is never emitted. */
4957 DECL_ABSTRACT_P (fn
) = true;
4960 for (tree clone
= fn
; count
--;)
4962 clone
= DECL_CHAIN (clone
);
4963 add_method (DECL_CONTEXT (clone
), clone
, false);
4967 /* DECL is an in charge constructor, which is being defined. This will
4968 have had an in class declaration, from whence clones were
4969 declared. An out-of-class definition can specify additional default
4970 arguments. As it is the clones that are involved in overload
4971 resolution, we must propagate the information from the DECL to its
4975 adjust_clone_args (tree decl
)
4979 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4980 clone
= DECL_CHAIN (clone
))
4982 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4983 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4984 tree decl_parms
, clone_parms
;
4986 /* Skip the 'this' parameter. */
4987 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4988 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4990 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4991 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4992 if (DECL_HAS_VTT_PARM_P (decl
))
4993 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4995 clone_parms
= orig_clone_parms
;
4996 if (DECL_HAS_VTT_PARM_P (clone
))
4997 clone_parms
= TREE_CHAIN (clone_parms
);
4999 for (decl_parms
= orig_decl_parms
; decl_parms
;
5000 decl_parms
= TREE_CHAIN (decl_parms
),
5001 clone_parms
= TREE_CHAIN (clone_parms
))
5003 if (clone_parms
== void_list_node
)
5005 gcc_assert (decl_parms
== clone_parms
5006 || ctor_omit_inherited_parms (clone
));
5010 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms
),
5011 TREE_VALUE (clone_parms
)));
5013 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
5015 /* A default parameter has been added. Adjust the
5016 clone's parameters. */
5017 clone_parms
= orig_decl_parms
;
5019 if (DECL_HAS_VTT_PARM_P (clone
))
5021 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
5022 TREE_VALUE (orig_clone_parms
),
5024 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
5027 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
5029 = build_method_type_directly (basetype
,
5030 TREE_TYPE (TREE_TYPE (clone
)),
5032 if (tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
)))
5033 type
= cp_build_type_attribute_variant (type
, attrs
);
5034 type
= cxx_copy_lang_qualifiers (type
, TREE_TYPE (clone
));
5035 TREE_TYPE (clone
) = type
;
5037 clone_parms
= NULL_TREE
;
5041 gcc_assert (!clone_parms
|| clone_parms
== void_list_node
);
5045 /* For each of the constructors and destructors in T, create an
5046 in-charge and not-in-charge variant. */
5049 clone_constructors_and_destructors (tree t
)
5051 /* While constructors can be via a using declaration, at this point
5052 we no longer need to know that. */
5053 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5054 clone_cdtor (*iter
, /*update_methods=*/true);
5056 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5057 clone_cdtor (dtor
, /*update_methods=*/true);
5060 /* Deduce noexcept for a destructor DTOR. */
5063 deduce_noexcept_on_destructor (tree dtor
)
5065 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
5066 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
),
5067 noexcept_deferred_spec
);
5070 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5071 of TYPE for virtual functions which FNDECL overrides. Return a
5072 mask of the tm attributes found therein. */
5075 look_for_tm_attr_overrides (tree type
, tree fndecl
)
5077 tree binfo
= TYPE_BINFO (type
);
5081 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
5083 tree o
, basetype
= BINFO_TYPE (base_binfo
);
5085 if (!TYPE_POLYMORPHIC_P (basetype
))
5088 o
= look_for_overrides_here (basetype
, fndecl
);
5091 if (lookup_attribute ("transaction_safe_dynamic",
5092 DECL_ATTRIBUTES (o
)))
5093 /* transaction_safe_dynamic is not inherited. */;
5095 found
|= tm_attr_to_mask (find_tm_attribute
5096 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
5099 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
5105 /* Subroutine of set_method_tm_attributes. Handle the checks and
5106 inheritance for one virtual method FNDECL. */
5109 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
5114 found
= look_for_tm_attr_overrides (type
, fndecl
);
5116 /* If FNDECL doesn't actually override anything (i.e. T is the
5117 class that first declares FNDECL virtual), then we're done. */
5121 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
5122 have
= tm_attr_to_mask (tm_attr
);
5124 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5125 tm_pure must match exactly, otherwise no weakening of
5126 tm_safe > tm_callable > nothing. */
5127 /* ??? The tm_pure attribute didn't make the transition to the
5128 multivendor language spec. */
5129 if (have
== TM_ATTR_PURE
)
5131 if (found
!= TM_ATTR_PURE
)
5137 /* If the overridden function is tm_pure, then FNDECL must be. */
5138 else if (found
== TM_ATTR_PURE
&& tm_attr
)
5140 /* Look for base class combinations that cannot be satisfied. */
5141 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
5143 found
&= ~TM_ATTR_PURE
;
5145 error_at (DECL_SOURCE_LOCATION (fndecl
),
5146 "method overrides both %<transaction_pure%> and %qE methods",
5147 tm_mask_to_attr (found
));
5149 /* If FNDECL did not declare an attribute, then inherit the most
5151 else if (tm_attr
== NULL
)
5153 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
5155 /* Otherwise validate that we're not weaker than a function
5156 that is being overridden. */
5160 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
5166 error_at (DECL_SOURCE_LOCATION (fndecl
),
5167 "method declared %qE overriding %qE method",
5168 tm_attr
, tm_mask_to_attr (found
));
5171 /* For each of the methods in T, propagate a class-level tm attribute. */
5174 set_method_tm_attributes (tree t
)
5176 tree class_tm_attr
, fndecl
;
5178 /* Don't bother collecting tm attributes if transactional memory
5179 support is not enabled. */
5183 /* Process virtual methods first, as they inherit directly from the
5184 base virtual function and also require validation of new attributes. */
5185 if (TYPE_CONTAINS_VPTR_P (t
))
5188 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
5189 vchain
= TREE_CHAIN (vchain
))
5191 fndecl
= BV_FN (vchain
);
5192 if (DECL_THUNK_P (fndecl
))
5193 fndecl
= THUNK_TARGET (fndecl
);
5194 set_one_vmethod_tm_attributes (t
, fndecl
);
5198 /* If the class doesn't have an attribute, nothing more to do. */
5199 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
5200 if (class_tm_attr
== NULL
)
5203 /* Any method that does not yet have a tm attribute inherits
5204 the one from the class. */
5205 for (fndecl
= TYPE_FIELDS (t
); fndecl
; fndecl
= DECL_CHAIN (fndecl
))
5206 if (DECL_DECLARES_FUNCTION_P (fndecl
)
5207 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
5208 apply_tm_attr (fndecl
, class_tm_attr
);
5211 /* Returns true if FN is a default constructor. */
5214 default_ctor_p (const_tree fn
)
5216 return (DECL_CONSTRUCTOR_P (fn
)
5217 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
5220 /* Returns true iff class T has a user-provided constructor that can be called
5221 with more than zero arguments. */
5224 type_has_user_nondefault_constructor (tree t
)
5226 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5229 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5232 if (user_provided_p (fn
)
5233 && (TREE_CODE (fn
) == TEMPLATE_DECL
5234 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
5242 /* Returns the defaulted constructor if T has one. Otherwise, returns
5246 in_class_defaulted_default_constructor (tree t
)
5248 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5251 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5255 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
5256 && default_ctor_p (fn
))
5263 /* Returns true iff FN is a user-provided function, i.e. user-declared
5264 and not defaulted at its first declaration. */
5267 user_provided_p (tree fn
)
5269 fn
= STRIP_TEMPLATE (fn
);
5270 return (!DECL_ARTIFICIAL (fn
)
5271 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
5272 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
5275 /* Returns true iff class T has a user-provided constructor. */
5278 type_has_user_provided_constructor (tree t
)
5280 if (!CLASS_TYPE_P (t
))
5283 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5286 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5287 if (user_provided_p (*iter
))
5293 /* Returns true iff class T has a user-provided or explicit constructor. */
5296 type_has_user_provided_or_explicit_constructor (tree t
)
5298 if (!CLASS_TYPE_P (t
))
5301 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5304 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5307 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5314 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5315 declared or explicitly defaulted in the class body) default
5319 type_has_non_user_provided_default_constructor (tree t
)
5321 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5323 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5326 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5329 if (TREE_CODE (fn
) == FUNCTION_DECL
5330 && default_ctor_p (fn
)
5331 && !user_provided_p (fn
))
5338 /* TYPE is being used as a virtual base, and has a non-trivial move
5339 assignment. Return true if this is due to there being a user-provided
5340 move assignment in TYPE or one of its subobjects; if there isn't, then
5341 multiple move assignment can't cause any harm. */
5344 vbase_has_user_provided_move_assign (tree type
)
5346 /* Does the type itself have a user-provided move assignment operator? */
5347 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type
))
5348 for (ovl_iterator
iter (get_class_binding_direct
5349 (type
, assign_op_identifier
));
5351 if (user_provided_p (*iter
) && move_fn_p (*iter
))
5354 /* Do any of its bases? */
5355 tree binfo
= TYPE_BINFO (type
);
5357 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5358 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5361 /* Or non-static data members? */
5362 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5364 if (TREE_CODE (field
) == FIELD_DECL
5365 && CLASS_TYPE_P (TREE_TYPE (field
))
5366 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5374 /* If default-initialization leaves part of TYPE uninitialized, returns
5375 a DECL for the field or TYPE itself (DR 253). */
5378 default_init_uninitialized_part (tree type
)
5383 type
= strip_array_types (type
);
5384 if (!CLASS_TYPE_P (type
))
5386 if (!type_has_non_user_provided_default_constructor (type
))
5388 for (binfo
= TYPE_BINFO (type
), i
= 0;
5389 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5391 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5395 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5396 if (TREE_CODE (t
) == FIELD_DECL
5397 && !DECL_ARTIFICIAL (t
)
5398 && !DECL_INITIAL (t
))
5400 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5402 return DECL_P (r
) ? r
: t
;
5408 /* Returns true iff for class T, a trivial synthesized default constructor
5409 would be constexpr. */
5412 trivial_default_constructor_is_constexpr (tree t
)
5414 /* A defaulted trivial default constructor is constexpr
5415 if there is nothing to initialize. */
5416 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5417 /* A class with a vptr doesn't have a trivial default ctor.
5418 In C++20, a class can have transient uninitialized members, e.g.:
5420 struct S { int i; constexpr S() = default; };
5423 return (cxx_dialect
>= cxx20
5424 || is_really_empty_class (t
, /*ignore_vptr*/true));
5427 /* Returns true iff class T has a constexpr default constructor. */
5430 type_has_constexpr_default_constructor (tree t
)
5434 if (!CLASS_TYPE_P (t
))
5436 /* The caller should have stripped an enclosing array. */
5437 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5440 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5442 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5443 return trivial_default_constructor_is_constexpr (t
);
5444 /* Non-trivial, we need to check subobject constructors. */
5445 lazily_declare_fn (sfk_constructor
, t
);
5447 fns
= locate_ctor (t
);
5448 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5451 /* Returns true iff class T has a constexpr default constructor or has an
5452 implicitly declared default constructor that we can't tell if it's constexpr
5453 without forcing a lazy declaration (which might cause undesired
5457 type_maybe_constexpr_default_constructor (tree t
)
5459 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5460 && TYPE_HAS_COMPLEX_DFLT (t
))
5461 /* Assume it's constexpr. */
5463 return type_has_constexpr_default_constructor (t
);
5466 /* Returns true iff class T has a constexpr destructor. */
5469 type_has_constexpr_destructor (tree t
)
5473 if (CLASSTYPE_LAZY_DESTRUCTOR (t
))
5474 /* Non-trivial, we need to check subobject destructors. */
5475 lazily_declare_fn (sfk_destructor
, t
);
5476 fns
= CLASSTYPE_DESTRUCTOR (t
);
5477 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5480 /* Returns true iff class T has a constexpr destructor or has an
5481 implicitly declared destructor that we can't tell if it's constexpr
5482 without forcing a lazy declaration (which might cause undesired
5486 type_maybe_constexpr_destructor (tree t
)
5488 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DESTRUCTOR (t
))
5489 /* Assume it's constexpr. */
5491 return type_has_constexpr_destructor (t
);
5494 /* Returns true iff class TYPE has a virtual destructor. */
5497 type_has_virtual_destructor (tree type
)
5501 if (!CLASS_TYPE_P (type
))
5504 gcc_assert (COMPLETE_TYPE_P (type
));
5505 dtor
= CLASSTYPE_DESTRUCTOR (type
);
5506 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5509 /* Returns true iff T, a class, has a move-assignment or
5510 move-constructor. Does not lazily declare either.
5511 If USER_P is false, any move function will do. If it is true, the
5512 move function must be user-declared.
5514 Note that user-declared here is different from "user-provided",
5515 which doesn't include functions that are defaulted in the
5519 classtype_has_move_assign_or_move_ctor_p (tree t
, bool user_p
)
5522 || (!CLASSTYPE_LAZY_MOVE_CTOR (t
)
5523 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t
)));
5525 if (!CLASSTYPE_LAZY_MOVE_CTOR (t
))
5526 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5527 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5530 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5531 for (ovl_iterator
iter (get_class_binding_direct
5532 (t
, assign_op_identifier
));
5534 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
))
5535 && DECL_CONTEXT (*iter
) == t
5536 && move_fn_p (*iter
))
5542 /* True iff T has a move constructor that is not deleted. */
5545 classtype_has_non_deleted_move_ctor (tree t
)
5547 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5548 lazily_declare_fn (sfk_move_constructor
, t
);
5549 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5550 if (move_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5555 /* True iff T has a copy constructor that is not deleted. */
5558 classtype_has_non_deleted_copy_ctor (tree t
)
5560 if (CLASSTYPE_LAZY_COPY_CTOR (t
))
5561 lazily_declare_fn (sfk_copy_constructor
, t
);
5562 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5563 if (copy_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5568 /* If T, a class, has a user-provided copy constructor, copy assignment
5569 operator, or destructor, returns that function. Otherwise, null. */
5572 classtype_has_depr_implicit_copy (tree t
)
5574 if (!CLASSTYPE_LAZY_COPY_CTOR (t
))
5575 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5578 if (user_provided_p (fn
) && copy_fn_p (fn
))
5582 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t
))
5583 for (ovl_iterator
iter (get_class_binding_direct
5584 (t
, assign_op_identifier
));
5588 if (user_provided_p (fn
) && copy_fn_p (fn
))
5592 if (!CLASSTYPE_LAZY_DESTRUCTOR (t
))
5594 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5595 if (user_provided_p (fn
))
5602 /* True iff T has a member or friend declaration of operator OP. */
5605 classtype_has_op (tree t
, tree_code op
)
5607 tree name
= ovl_op_identifier (op
);
5608 if (get_class_binding (t
, name
))
5610 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5611 if (FRIEND_NAME (f
) == name
)
5617 /* If T has a defaulted member or friend declaration of OP, return it. */
5620 classtype_has_defaulted_op (tree t
, tree_code op
)
5622 tree name
= ovl_op_identifier (op
);
5623 for (ovl_iterator
oi (get_class_binding (t
, name
)); oi
; ++oi
)
5626 if (DECL_DEFAULTED_FN (fn
))
5629 for (tree f
= DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)); f
; f
= TREE_CHAIN (f
))
5630 if (FRIEND_NAME (f
) == name
)
5631 for (tree l
= FRIEND_DECLS (f
); l
; l
= TREE_CHAIN (l
))
5633 tree fn
= TREE_VALUE (l
);
5634 if (DECL_DEFAULTED_FN (fn
))
5640 /* Nonzero if we need to build up a constructor call when initializing an
5641 object of this class, either because it has a user-declared constructor
5642 or because it doesn't have a default constructor (so we need to give an
5643 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5644 what you care about is whether or not an object can be produced by a
5645 constructor (e.g. so we don't set TREE_READONLY on const variables of
5646 such type); use this function when what you care about is whether or not
5647 to try to call a constructor to create an object. The latter case is
5648 the former plus some cases of constructors that cannot be called. */
5651 type_build_ctor_call (tree t
)
5654 if (TYPE_NEEDS_CONSTRUCTING (t
))
5656 inner
= strip_array_types (t
);
5657 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5659 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5661 if (cxx_dialect
< cxx11
)
5663 /* A user-declared constructor might be private, and a constructor might
5664 be trivial but deleted. */
5665 for (ovl_iterator
iter (get_class_binding (inner
, complete_ctor_identifier
));
5669 if (!DECL_ARTIFICIAL (fn
)
5670 || TREE_DEPRECATED (fn
)
5671 || DECL_DELETED_FN (fn
))
5677 /* Like type_build_ctor_call, but for destructors. */
5680 type_build_dtor_call (tree t
)
5683 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5685 inner
= strip_array_types (t
);
5686 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5687 || !COMPLETE_TYPE_P (inner
))
5689 if (cxx_dialect
< cxx11
)
5691 /* A user-declared destructor might be private, and a destructor might
5692 be trivial but deleted. */
5693 for (ovl_iterator
iter (get_class_binding (inner
, complete_dtor_identifier
));
5697 if (!DECL_ARTIFICIAL (fn
)
5698 || TREE_DEPRECATED (fn
)
5699 || DECL_DELETED_FN (fn
))
5705 /* Remove all zero-width bit-fields from T. */
5708 remove_zero_width_bit_fields (tree t
)
5712 fieldsp
= &TYPE_FIELDS (t
);
5715 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5716 && DECL_C_BIT_FIELD (*fieldsp
)
5717 /* We should not be confused by the fact that grokbitfield
5718 temporarily sets the width of the bit field into
5719 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5720 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5722 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5723 || integer_zerop (DECL_SIZE (*fieldsp
))))
5724 *fieldsp
= DECL_CHAIN (*fieldsp
);
5726 fieldsp
= &DECL_CHAIN (*fieldsp
);
5730 /* Returns TRUE iff we need a cookie when dynamically allocating an
5731 array whose elements have the indicated class TYPE. */
5734 type_requires_array_cookie (tree type
)
5737 bool has_two_argument_delete_p
= false;
5739 gcc_assert (CLASS_TYPE_P (type
));
5741 /* If there's a non-trivial destructor, we need a cookie. In order
5742 to iterate through the array calling the destructor for each
5743 element, we'll have to know how many elements there are. */
5744 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5747 /* If the usual deallocation function is a two-argument whose second
5748 argument is of type `size_t', then we have to pass the size of
5749 the array to the deallocation function, so we will need to store
5751 fns
= lookup_fnfields (TYPE_BINFO (type
),
5752 ovl_op_identifier (false, VEC_DELETE_EXPR
),
5753 /*protect=*/0, tf_warning_or_error
);
5754 /* If there are no `operator []' members, or the lookup is
5755 ambiguous, then we don't need a cookie. */
5756 if (!fns
|| fns
== error_mark_node
)
5758 /* Loop through all of the functions. */
5759 for (lkp_iterator
iter (BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
5763 /* See if this function is a one-argument delete function. If
5764 it is, then it will be the usual deallocation function. */
5765 tree second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5766 if (second_parm
== void_list_node
)
5768 /* Do not consider this function if its second argument is an
5772 /* Otherwise, if we have a two-argument function and the second
5773 argument is `size_t', it will be the usual deallocation
5774 function -- unless there is one-argument function, too. */
5775 if (TREE_CHAIN (second_parm
) == void_list_node
5776 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5777 has_two_argument_delete_p
= true;
5780 return has_two_argument_delete_p
;
5783 /* Finish computing the `literal type' property of class type T.
5785 At this point, we have already processed base classes and
5786 non-static data members. We need to check whether the copy
5787 constructor is trivial, the destructor is trivial, and there
5788 is a trivial default constructor or at least one constexpr
5789 constructor other than the copy constructor. */
5792 finalize_literal_type_property (tree t
)
5796 if (cxx_dialect
< cxx11
)
5797 CLASSTYPE_LITERAL_P (t
) = false;
5798 else if (CLASSTYPE_LITERAL_P (t
)
5799 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
5800 && (cxx_dialect
< cxx20
|| !type_maybe_constexpr_destructor (t
)))
5801 CLASSTYPE_LITERAL_P (t
) = false;
5802 else if (CLASSTYPE_LITERAL_P (t
) && LAMBDA_TYPE_P (t
))
5803 CLASSTYPE_LITERAL_P (t
) = (cxx_dialect
>= cxx17
);
5804 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5805 && CLASSTYPE_NON_AGGREGATE (t
)
5806 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5807 CLASSTYPE_LITERAL_P (t
) = false;
5809 /* C++14 DR 1684 removed this restriction. */
5810 if (cxx_dialect
< cxx14
5811 && !CLASSTYPE_LITERAL_P (t
) && !LAMBDA_TYPE_P (t
))
5812 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5813 if (TREE_CODE (fn
) == FUNCTION_DECL
5814 && DECL_DECLARED_CONSTEXPR_P (fn
)
5815 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5816 && !DECL_CONSTRUCTOR_P (fn
))
5818 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5819 if (!DECL_GENERATED_P (fn
))
5821 auto_diagnostic_group d
;
5822 if (pedwarn (DECL_SOURCE_LOCATION (fn
), OPT_Wpedantic
,
5823 "enclosing class of %<constexpr%> non-static "
5824 "member function %q+#D is not a literal type", fn
))
5825 explain_non_literal_class (t
);
5830 /* T is a non-literal type used in a context which requires a constant
5831 expression. Explain why it isn't literal. */
5834 explain_non_literal_class (tree t
)
5836 static hash_set
<tree
> *diagnosed
;
5838 if (!CLASS_TYPE_P (t
))
5840 t
= TYPE_MAIN_VARIANT (t
);
5842 if (diagnosed
== NULL
)
5843 diagnosed
= new hash_set
<tree
>;
5844 if (diagnosed
->add (t
))
5845 /* Already explained. */
5848 auto_diagnostic_group d
;
5849 inform (UNKNOWN_LOCATION
, "%q+T is not literal because:", t
);
5850 if (cxx_dialect
< cxx17
&& LAMBDA_TYPE_P (t
))
5851 inform (UNKNOWN_LOCATION
,
5852 " %qT is a closure type, which is only literal in "
5853 "C++17 and later", t
);
5854 else if (cxx_dialect
< cxx20
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5855 inform (UNKNOWN_LOCATION
, " %q+T has a non-trivial destructor", t
);
5856 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
5857 && !type_maybe_constexpr_destructor (t
))
5858 inform (UNKNOWN_LOCATION
, " %q+T does not have %<constexpr%> destructor",
5860 else if (CLASSTYPE_NON_AGGREGATE (t
)
5861 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5862 && !LAMBDA_TYPE_P (t
)
5863 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5865 inform (UNKNOWN_LOCATION
,
5866 " %q+T is not an aggregate, does not have a trivial "
5867 "default constructor, and has no %<constexpr%> constructor that "
5868 "is not a copy or move constructor", t
);
5869 if (type_has_non_user_provided_default_constructor (t
))
5870 /* Note that we can't simply call locate_ctor because when the
5871 constructor is deleted it just returns NULL_TREE. */
5872 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5875 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5877 parms
= skip_artificial_parms_for (fn
, parms
);
5879 if (sufficient_parms_p (parms
))
5881 if (DECL_DELETED_FN (fn
))
5882 maybe_explain_implicit_delete (fn
);
5884 explain_invalid_constexpr_fn (fn
);
5891 tree binfo
, base_binfo
, field
; int i
;
5892 for (binfo
= TYPE_BINFO (t
), i
= 0;
5893 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5895 tree basetype
= TREE_TYPE (base_binfo
);
5896 if (!CLASSTYPE_LITERAL_P (basetype
))
5898 inform (UNKNOWN_LOCATION
,
5899 " base class %qT of %q+T is non-literal",
5901 explain_non_literal_class (basetype
);
5905 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5908 if (TREE_CODE (field
) != FIELD_DECL
)
5910 ftype
= TREE_TYPE (field
);
5911 if (!literal_type_p (ftype
))
5913 inform (DECL_SOURCE_LOCATION (field
),
5914 " non-static data member %qD has non-literal type",
5916 if (CLASS_TYPE_P (ftype
))
5917 explain_non_literal_class (ftype
);
5919 if (CP_TYPE_VOLATILE_P (ftype
))
5920 inform (DECL_SOURCE_LOCATION (field
),
5921 " non-static data member %qD has volatile type", field
);
5926 /* Check the validity of the bases and members declared in T. Add any
5927 implicitly-generated functions (like copy-constructors and
5928 assignment operators). Compute various flag bits (like
5929 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5930 level: i.e., independently of the ABI in use. */
5933 check_bases_and_members (tree t
)
5935 /* Nonzero if the implicitly generated copy constructor should take
5936 a non-const reference argument. */
5937 int cant_have_const_ctor
;
5938 /* Nonzero if the implicitly generated assignment operator
5939 should take a non-const reference argument. */
5940 int no_const_asn_ref
;
5942 bool saved_complex_asn_ref
;
5943 bool saved_nontrivial_dtor
;
5946 /* By default, we use const reference arguments and generate default
5948 cant_have_const_ctor
= 0;
5949 no_const_asn_ref
= 0;
5951 /* Check all the base-classes and set FMEM members to point to arrays
5952 of potential interest. */
5953 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
5955 /* Deduce noexcept on destructor. This needs to happen after we've set
5956 triviality flags appropriately for our bases. */
5957 if (cxx_dialect
>= cxx11
)
5958 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5959 deduce_noexcept_on_destructor (dtor
);
5961 /* Check all the method declarations. */
5964 /* Save the initial values of these flags which only indicate whether
5965 or not the class has user-provided functions. As we analyze the
5966 bases and members we can set these flags for other reasons. */
5967 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5968 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5970 /* Check all the data member declarations. We cannot call
5971 check_field_decls until we have called check_bases check_methods,
5972 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5973 being set appropriately. */
5974 check_field_decls (t
, &access_decls
,
5975 &cant_have_const_ctor
,
5978 /* A nearly-empty class has to be vptr-containing; a nearly empty
5979 class contains just a vptr. */
5980 if (!TYPE_CONTAINS_VPTR_P (t
))
5981 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5983 /* Do some bookkeeping that will guide the generation of implicitly
5984 declared member functions. */
5985 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5986 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5987 /* We need to call a constructor for this class if it has a
5988 user-provided constructor, or if the default constructor is going
5989 to initialize the vptr. (This is not an if-and-only-if;
5990 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5991 themselves need constructing.) */
5992 TYPE_NEEDS_CONSTRUCTING (t
)
5993 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5996 An aggregate is an array or a class with no user-provided
5997 constructors ... and no virtual functions.
5999 Again, other conditions for being an aggregate are checked
6001 CLASSTYPE_NON_AGGREGATE (t
)
6002 |= ((cxx_dialect
< cxx20
6003 ? type_has_user_provided_or_explicit_constructor (t
)
6004 : TYPE_HAS_USER_CONSTRUCTOR (t
))
6005 || TYPE_POLYMORPHIC_P (t
));
6006 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6007 retain the old definition internally for ABI reasons. */
6008 CLASSTYPE_NON_LAYOUT_POD_P (t
)
6009 |= (CLASSTYPE_NON_AGGREGATE (t
)
6010 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
6011 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6012 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6013 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6014 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
6016 /* If the only explicitly declared default constructor is user-provided,
6017 set TYPE_HAS_COMPLEX_DFLT. */
6018 if (!TYPE_HAS_COMPLEX_DFLT (t
)
6019 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
6020 && !type_has_non_user_provided_default_constructor (t
))
6021 TYPE_HAS_COMPLEX_DFLT (t
) = true;
6023 /* Warn if a public base of a polymorphic type has an accessible
6024 non-virtual destructor. It is only now that we know the class is
6025 polymorphic. Although a polymorphic base will have a already
6026 been diagnosed during its definition, we warn on use too. */
6027 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
6029 tree binfo
= TYPE_BINFO (t
);
6030 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
6034 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
6036 tree basetype
= TREE_TYPE (base_binfo
);
6038 if ((*accesses
)[i
] == access_public_node
6039 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
6040 && accessible_nvdtor_p (basetype
))
6041 warning (OPT_Wnon_virtual_dtor
,
6042 "base class %q#T has accessible non-virtual destructor",
6047 /* If the class has no user-declared constructor, but does have
6048 non-static const or reference data members that can never be
6049 initialized, issue a warning. */
6050 if (warn_uninitialized
6051 /* Classes with user-declared constructors are presumed to
6052 initialize these members. */
6053 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
6054 /* Aggregates can be initialized with brace-enclosed
6056 && CLASSTYPE_NON_AGGREGATE (t
))
6060 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6064 if (TREE_CODE (field
) != FIELD_DECL
6065 || DECL_INITIAL (field
) != NULL_TREE
)
6068 type
= TREE_TYPE (field
);
6069 if (TYPE_REF_P (type
))
6070 warning_at (DECL_SOURCE_LOCATION (field
),
6071 OPT_Wuninitialized
, "non-static reference %q#D "
6072 "in class without a constructor", field
);
6073 else if (CP_TYPE_CONST_P (type
)
6074 && (!CLASS_TYPE_P (type
)
6075 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
6076 warning_at (DECL_SOURCE_LOCATION (field
),
6077 OPT_Wuninitialized
, "non-static const member %q#D "
6078 "in class without a constructor", field
);
6082 /* Synthesize any needed methods. */
6083 add_implicitly_declared_members (t
, &access_decls
,
6084 cant_have_const_ctor
,
6087 /* Check defaulted declarations here so we have cant_have_const_ctor
6088 and don't need to worry about clones. */
6089 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6090 if (DECL_DECLARES_FUNCTION_P (fn
)
6091 && !DECL_ARTIFICIAL (fn
)
6092 && DECL_DEFAULTED_IN_CLASS_P (fn
))
6094 int copy
= copy_fn_p (fn
);
6098 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
6099 : !no_const_asn_ref
);
6100 bool fn_const_p
= (copy
== 2);
6102 if (fn_const_p
&& !imp_const_p
)
6103 /* If the function is defaulted outside the class, we just
6104 give the synthesis error. Core Issue #1331 says this is
6105 no longer ill-formed, it is defined as deleted instead. */
6106 DECL_DELETED_FN (fn
) = true;
6108 defaulted_late_check (fn
);
6111 if (LAMBDA_TYPE_P (t
))
6112 /* "This class type is not an aggregate." */
6113 CLASSTYPE_NON_AGGREGATE (t
) = 1;
6115 /* Compute the 'literal type' property before we
6116 do anything with non-static member functions. */
6117 finalize_literal_type_property (t
);
6119 /* Create the in-charge and not-in-charge variants of constructors
6121 clone_constructors_and_destructors (t
);
6123 /* Process the using-declarations. */
6124 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
6125 handle_using_decl (TREE_VALUE (access_decls
), t
);
6127 /* Figure out whether or not we will need a cookie when dynamically
6128 allocating an array of this type. */
6129 LANG_TYPE_CLASS_CHECK (t
)->vec_new_uses_cookie
6130 = type_requires_array_cookie (t
);
6133 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6134 accordingly. If a new vfield was created (because T doesn't have a
6135 primary base class), then the newly created field is returned. It
6136 is not added to the TYPE_FIELDS list; it is the caller's
6137 responsibility to do that. Accumulate declared virtual functions
6141 create_vtable_ptr (tree t
, tree
* virtuals_p
)
6145 /* Collect the virtual functions declared in T. */
6146 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
6147 if (TREE_CODE (fn
) == FUNCTION_DECL
6148 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
6149 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
6151 tree new_virtual
= make_node (TREE_LIST
);
6153 BV_FN (new_virtual
) = fn
;
6154 BV_DELTA (new_virtual
) = integer_zero_node
;
6155 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
6157 TREE_CHAIN (new_virtual
) = *virtuals_p
;
6158 *virtuals_p
= new_virtual
;
6161 /* If we couldn't find an appropriate base class, create a new field
6162 here. Even if there weren't any new virtual functions, we might need a
6163 new virtual function table if we're supposed to include vptrs in
6164 all classes that need them. */
6165 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
6167 /* We build this decl with vtbl_ptr_type_node, which is a
6168 `vtable_entry_type*'. It might seem more precise to use
6169 `vtable_entry_type (*)[N]' where N is the number of virtual
6170 functions. However, that would require the vtable pointer in
6171 base classes to have a different type than the vtable pointer
6172 in derived classes. We could make that happen, but that
6173 still wouldn't solve all the problems. In particular, the
6174 type-based alias analysis code would decide that assignments
6175 to the base class vtable pointer can't alias assignments to
6176 the derived class vtable pointer, since they have different
6177 types. Thus, in a derived class destructor, where the base
6178 class constructor was inlined, we could generate bad code for
6179 setting up the vtable pointer.
6181 Therefore, we use one type for all vtable pointers. We still
6182 use a type-correct type; it's just doesn't indicate the array
6183 bounds. That's better than using `void*' or some such; it's
6184 cleaner, and it let's the alias analysis code know that these
6185 stores cannot alias stores to void*! */
6188 field
= build_decl (input_location
,
6189 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
6190 DECL_VIRTUAL_P (field
) = 1;
6191 DECL_ARTIFICIAL (field
) = 1;
6192 DECL_FIELD_CONTEXT (field
) = t
;
6193 DECL_FCONTEXT (field
) = t
;
6194 if (TYPE_PACKED (t
))
6195 DECL_PACKED (field
) = 1;
6197 TYPE_VFIELD (t
) = field
;
6199 /* This class is non-empty. */
6200 CLASSTYPE_EMPTY_P (t
) = 0;
6208 /* Add OFFSET to all base types of BINFO which is a base in the
6209 hierarchy dominated by T.
6211 OFFSET, which is a type offset, is number of bytes. */
6214 propagate_binfo_offsets (tree binfo
, tree offset
)
6220 /* Update BINFO's offset. */
6221 BINFO_OFFSET (binfo
)
6222 = fold_convert (sizetype
,
6223 size_binop (PLUS_EXPR
,
6224 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
6227 /* Find the primary base class. */
6228 primary_binfo
= get_primary_binfo (binfo
);
6230 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
6231 propagate_binfo_offsets (primary_binfo
, offset
);
6233 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6235 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6237 /* Don't do the primary base twice. */
6238 if (base_binfo
== primary_binfo
)
6241 if (BINFO_VIRTUAL_P (base_binfo
))
6244 propagate_binfo_offsets (base_binfo
, offset
);
6248 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6249 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6250 empty subobjects of T. */
6253 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
6259 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
6262 /* Find the last field. The artificial fields created for virtual
6263 bases will go after the last extant field to date. */
6264 next_field
= &TYPE_FIELDS (t
);
6266 next_field
= &DECL_CHAIN (*next_field
);
6268 /* Go through the virtual bases, allocating space for each virtual
6269 base that is not already a primary base class. These are
6270 allocated in inheritance graph order. */
6271 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6273 if (!BINFO_VIRTUAL_P (vbase
))
6276 if (!BINFO_PRIMARY_P (vbase
))
6278 /* This virtual base is not a primary base of any class in the
6279 hierarchy, so we have to add space for it. */
6280 next_field
= build_base_field (rli
, vbase
,
6281 access_private_node
,
6282 offsets
, next_field
);
6287 /* Returns the offset of the byte just past the end of the base class
6291 end_of_base (tree binfo
)
6295 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
6296 size
= TYPE_SIZE_UNIT (char_type_node
);
6297 else if (is_empty_class (BINFO_TYPE (binfo
)))
6298 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6299 allocate some space for it. It cannot have virtual bases, so
6300 TYPE_SIZE_UNIT is fine. */
6301 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6303 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6305 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
6308 /* Returns the offset of the byte just past the end of the base class or empty
6309 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
6310 then only non-virtual bases are included. */
6313 end_of_class (tree t
, bool include_virtuals_p
)
6315 tree result
= size_zero_node
;
6316 vec
<tree
, va_gc
> *vbases
;
6322 for (binfo
= TYPE_BINFO (t
), i
= 0;
6323 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6325 if (!include_virtuals_p
6326 && BINFO_VIRTUAL_P (base_binfo
)
6327 && (!BINFO_PRIMARY_P (base_binfo
)
6328 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
6331 offset
= end_of_base (base_binfo
);
6332 if (tree_int_cst_lt (result
, offset
))
6336 /* Also consider empty data members. */
6337 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6338 if (TREE_CODE (field
) == FIELD_DECL
6339 && !DECL_ARTIFICIAL (field
)
6340 && field_poverlapping_p (field
)
6341 && is_empty_class (TREE_TYPE (field
)))
6343 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
6344 offset
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (field
),
6345 TYPE_SIZE_UNIT (TREE_TYPE (field
)));
6346 if (tree_int_cst_lt (result
, offset
))
6350 if (include_virtuals_p
)
6351 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6352 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6354 offset
= end_of_base (base_binfo
);
6355 if (tree_int_cst_lt (result
, offset
))
6362 /* Warn about bases of T that are inaccessible because they are
6363 ambiguous. For example:
6366 struct T : public S {};
6367 struct U : public S, public T {};
6369 Here, `(S*) new U' is not allowed because there are two `S'
6373 maybe_warn_about_inaccessible_bases (tree t
)
6376 vec
<tree
, va_gc
> *vbases
;
6381 /* If not checking for warning then return early. */
6382 if (!warn_inaccessible_base
)
6385 /* If there are no repeated bases, nothing can be ambiguous. */
6386 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6389 /* Check direct bases. */
6390 for (binfo
= TYPE_BINFO (t
), i
= 0;
6391 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6393 basetype
= BINFO_TYPE (base_binfo
);
6395 if (!uniquely_derived_from_p (basetype
, t
))
6396 warning (OPT_Winaccessible_base
, "direct base %qT inaccessible "
6397 "in %qT due to ambiguity", basetype
, t
);
6400 /* Check for ambiguous virtual bases. */
6402 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6403 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6405 basetype
= BINFO_TYPE (binfo
);
6407 if (!uniquely_derived_from_p (basetype
, t
))
6408 warning (OPT_Winaccessible_base
, "virtual base %qT inaccessible in "
6409 "%qT due to ambiguity", basetype
, t
);
6413 /* Compare two INTEGER_CSTs K1 and K2. */
6416 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6418 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6421 /* Increase the size indicated in RLI to account for empty classes
6422 that are "off the end" of the class. */
6425 include_empty_classes (record_layout_info rli
)
6430 /* It might be the case that we grew the class to allocate a
6431 zero-sized base class. That won't be reflected in RLI, yet,
6432 because we are willing to overlay multiple bases at the same
6433 offset. However, now we need to make sure that RLI is big enough
6434 to reflect the entire class. */
6435 eoc
= end_of_class (rli
->t
, CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6436 rli_size
= rli_size_unit_so_far (rli
);
6437 if (TREE_CODE (rli_size
) == INTEGER_CST
6438 && tree_int_cst_lt (rli_size
, eoc
))
6440 /* The size should have been rounded to a whole byte. */
6441 gcc_assert (tree_int_cst_equal
6442 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6444 = size_binop (PLUS_EXPR
,
6446 size_binop (MULT_EXPR
,
6447 fold_convert (bitsizetype
,
6448 size_binop (MINUS_EXPR
,
6450 bitsize_int (BITS_PER_UNIT
)));
6451 normalize_rli (rli
);
6455 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6456 BINFO_OFFSETs for all of the base-classes. Position the vtable
6457 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6460 layout_class_type (tree t
, tree
*virtuals_p
)
6462 tree non_static_data_members
;
6465 record_layout_info rli
;
6466 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6467 types that appear at that offset. */
6468 splay_tree empty_base_offsets
;
6469 /* True if the last field laid out was a bit-field. */
6470 bool last_field_was_bitfield
= false;
6471 /* The location at which the next field should be inserted. */
6474 /* Keep track of the first non-static data member. */
6475 non_static_data_members
= TYPE_FIELDS (t
);
6477 /* Start laying out the record. */
6478 rli
= start_record_layout (t
);
6480 /* Mark all the primary bases in the hierarchy. */
6481 determine_primary_bases (t
);
6483 /* Create a pointer to our virtual function table. */
6484 vptr
= create_vtable_ptr (t
, virtuals_p
);
6486 /* The vptr is always the first thing in the class. */
6489 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6490 TYPE_FIELDS (t
) = vptr
;
6491 next_field
= &DECL_CHAIN (vptr
);
6492 place_field (rli
, vptr
);
6495 next_field
= &TYPE_FIELDS (t
);
6497 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6498 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6500 build_base_fields (rli
, empty_base_offsets
, next_field
);
6502 /* Layout the non-static data members. */
6503 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6508 /* We still pass things that aren't non-static data members to
6509 the back end, in case it wants to do something with them. */
6510 if (TREE_CODE (field
) != FIELD_DECL
)
6512 place_field (rli
, field
);
6513 /* If the static data member has incomplete type, keep track
6514 of it so that it can be completed later. (The handling
6515 of pending statics in finish_record_layout is
6516 insufficient; consider:
6519 struct S2 { static S1 s1; };
6521 At this point, finish_record_layout will be called, but
6522 S1 is still incomplete.) */
6525 maybe_register_incomplete_var (field
);
6526 /* The visibility of static data members is determined
6527 at their point of declaration, not their point of
6529 determine_visibility (field
);
6534 type
= TREE_TYPE (field
);
6535 if (type
== error_mark_node
)
6538 padding
= NULL_TREE
;
6540 bool might_overlap
= field_poverlapping_p (field
);
6542 if (might_overlap
&& CLASS_TYPE_P (type
)
6543 && (CLASSTYPE_NON_LAYOUT_POD_P (type
) || CLASSTYPE_EMPTY_P (type
)))
6545 /* if D is a potentially-overlapping data member, update sizeof(C) to
6546 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6547 tree nvsize
= CLASSTYPE_SIZE_UNIT (type
);
6548 /* end_of_class doesn't always give dsize, but it does in the case of
6549 a class with virtual bases, which is when dsize > nvsize. */
6550 tree dsize
= end_of_class (type
, /*vbases*/true);
6551 if (tree_int_cst_le (dsize
, nvsize
))
6553 DECL_SIZE_UNIT (field
) = nvsize
;
6554 DECL_SIZE (field
) = CLASSTYPE_SIZE (type
);
6558 DECL_SIZE_UNIT (field
) = dsize
;
6559 DECL_SIZE (field
) = bit_from_pos (dsize
, bitsize_zero_node
);
6563 /* If this field is a bit-field whose width is greater than its
6564 type, then there are some special rules for allocating
6566 if (DECL_C_BIT_FIELD (field
)
6567 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6569 bool was_unnamed_p
= false;
6570 /* We must allocate the bits as if suitably aligned for the
6571 longest integer type that fits in this many bits. Then,
6572 we are supposed to use the left over bits as additional
6575 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6576 tree limit
= size_int (MAX_FIXED_MODE_SIZE
);
6577 if (tree_int_cst_lt (DECL_SIZE (field
), limit
))
6578 limit
= DECL_SIZE (field
);
6580 tree integer_type
= integer_types
[itk_char
];
6581 for (unsigned itk
= itk_char
; itk
!= itk_none
; itk
++)
6582 if (tree next
= integer_types
[itk
])
6584 if (tree_int_cst_lt (limit
, TYPE_SIZE (next
)))
6585 /* Too big, so our current guess is what we want. */
6587 /* Not bigger than limit, ok */
6588 integer_type
= next
;
6591 /* Figure out how much additional padding is required. */
6592 if (TREE_CODE (t
) == UNION_TYPE
)
6593 /* In a union, the padding field must have the full width
6594 of the bit-field; all fields start at offset zero. */
6595 padding
= DECL_SIZE (field
);
6597 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6598 TYPE_SIZE (integer_type
));
6600 if (integer_zerop (padding
))
6601 padding
= NULL_TREE
;
6603 /* An unnamed bitfield does not normally affect the
6604 alignment of the containing class on a target where
6605 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6606 make any exceptions for unnamed bitfields when the
6607 bitfields are longer than their types. Therefore, we
6608 temporarily give the field a name. */
6609 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6611 was_unnamed_p
= true;
6612 DECL_NAME (field
) = make_anon_name ();
6615 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6616 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6617 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6618 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6619 empty_base_offsets
);
6621 DECL_NAME (field
) = NULL_TREE
;
6622 /* Now that layout has been performed, set the size of the
6623 field to the size of its declared type; the rest of the
6624 field is effectively invisible. */
6625 DECL_SIZE (field
) = TYPE_SIZE (type
);
6626 /* We must also reset the DECL_MODE of the field. */
6627 SET_DECL_MODE (field
, TYPE_MODE (type
));
6629 else if (might_overlap
&& is_empty_class (type
))
6631 DECL_FIELD_ABI_IGNORED (field
) = 1;
6632 layout_empty_base_or_field (rli
, field
, empty_base_offsets
);
6635 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6636 empty_base_offsets
);
6638 /* Remember the location of any empty classes in FIELD. */
6639 record_subobject_offsets (field
, empty_base_offsets
);
6641 /* If a bit-field does not immediately follow another bit-field,
6642 and yet it starts in the middle of a byte, we have failed to
6643 comply with the ABI. */
6645 && DECL_C_BIT_FIELD (field
)
6646 /* The TREE_NO_WARNING flag gets set by Objective-C when
6647 laying out an Objective-C class. The ObjC ABI differs
6648 from the C++ ABI, and so we do not want a warning
6650 && !TREE_NO_WARNING (field
)
6651 && !last_field_was_bitfield
6652 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6653 DECL_FIELD_BIT_OFFSET (field
),
6654 bitsize_unit_node
)))
6655 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6656 "offset of %qD is not ABI-compliant and may "
6657 "change in a future version of GCC", field
);
6659 /* The middle end uses the type of expressions to determine the
6660 possible range of expression values. In order to optimize
6661 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6662 must be made aware of the width of "i", via its type.
6664 Because C++ does not have integer types of arbitrary width,
6665 we must (for the purposes of the front end) convert from the
6666 type assigned here to the declared type of the bitfield
6667 whenever a bitfield expression is used as an rvalue.
6668 Similarly, when assigning a value to a bitfield, the value
6669 must be converted to the type given the bitfield here. */
6670 if (DECL_C_BIT_FIELD (field
))
6672 unsigned HOST_WIDE_INT width
;
6673 tree ftype
= TREE_TYPE (field
);
6674 width
= tree_to_uhwi (DECL_SIZE (field
));
6675 if (width
!= TYPE_PRECISION (ftype
))
6678 = c_build_bitfield_integer_type (width
,
6679 TYPE_UNSIGNED (ftype
));
6681 = cp_build_qualified_type (TREE_TYPE (field
),
6682 cp_type_quals (ftype
));
6686 /* If we needed additional padding after this field, add it
6692 padding_field
= build_decl (input_location
,
6696 DECL_BIT_FIELD (padding_field
) = 1;
6697 DECL_SIZE (padding_field
) = padding
;
6698 DECL_CONTEXT (padding_field
) = t
;
6699 DECL_ARTIFICIAL (padding_field
) = 1;
6700 DECL_IGNORED_P (padding_field
) = 1;
6701 DECL_PADDING_P (padding_field
) = 1;
6702 layout_nonempty_base_or_field (rli
, padding_field
,
6704 empty_base_offsets
);
6707 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6710 if (!integer_zerop (rli
->bitpos
))
6712 /* Make sure that we are on a byte boundary so that the size of
6713 the class without virtual bases will always be a round number
6715 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6716 normalize_rli (rli
);
6719 /* Delete all zero-width bit-fields from the list of fields. Now
6720 that the type is laid out they are no longer important. */
6721 remove_zero_width_bit_fields (t
);
6723 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6725 /* T needs a different layout as a base (eliding virtual bases
6726 or whatever). Create that version. */
6727 tree base_t
= make_node (TREE_CODE (t
));
6728 tree base_d
= create_implicit_typedef (as_base_identifier
, base_t
);
6730 TYPE_CONTEXT (base_t
) = t
;
6731 DECL_CONTEXT (base_d
) = t
;
6733 /* If the ABI version is not at least two, and the last
6734 field was a bit-field, RLI may not be on a byte
6735 boundary. In particular, rli_size_unit_so_far might
6736 indicate the last complete byte, while rli_size_so_far
6737 indicates the total number of bits used. Therefore,
6738 rli_size_so_far, rather than rli_size_unit_so_far, is
6739 used to compute TYPE_SIZE_UNIT. */
6741 /* Set the size and alignment for the new type. */
6742 tree eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6743 TYPE_SIZE_UNIT (base_t
)
6744 = size_binop (MAX_EXPR
,
6745 fold_convert (sizetype
,
6746 size_binop (CEIL_DIV_EXPR
,
6747 rli_size_so_far (rli
),
6748 bitsize_int (BITS_PER_UNIT
))),
6751 = size_binop (MAX_EXPR
,
6752 rli_size_so_far (rli
),
6753 size_binop (MULT_EXPR
,
6754 fold_convert (bitsizetype
, eoc
),
6755 bitsize_int (BITS_PER_UNIT
)));
6756 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
6757 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6758 TYPE_TYPELESS_STORAGE (base_t
) = TYPE_TYPELESS_STORAGE (t
);
6759 TYPE_CXX_ODR_P (base_t
) = TYPE_CXX_ODR_P (t
);
6761 /* Copy the non-static data members of T. This will include its
6762 direct non-virtual bases & vtable. */
6763 next_field
= &TYPE_FIELDS (base_t
);
6764 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6765 if (TREE_CODE (field
) == FIELD_DECL
)
6767 *next_field
= copy_node (field
);
6768 /* Zap any NSDMI, it's not needed and might be a deferred
6770 DECL_INITIAL (*next_field
) = NULL_TREE
;
6771 DECL_CONTEXT (*next_field
) = base_t
;
6772 next_field
= &DECL_CHAIN (*next_field
);
6774 *next_field
= NULL_TREE
;
6776 /* We use the base type for trivial assignments, and hence it
6778 compute_record_mode (base_t
);
6780 /* Record the base version of the type. */
6781 CLASSTYPE_AS_BASE (t
) = base_t
;
6784 CLASSTYPE_AS_BASE (t
) = t
;
6786 /* Every empty class contains an empty class. */
6787 if (CLASSTYPE_EMPTY_P (t
))
6788 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6790 /* Set the TYPE_DECL for this type to contain the right
6791 value for DECL_OFFSET, so that we can use it as part
6792 of a COMPONENT_REF for multiple inheritance. */
6793 layout_decl (TYPE_MAIN_DECL (t
), 0);
6795 /* Now fix up any virtual base class types that we left lying
6796 around. We must get these done before we try to lay out the
6797 virtual function table. As a side-effect, this will remove the
6798 base subobject fields. */
6799 layout_virtual_bases (rli
, empty_base_offsets
);
6801 /* Make sure that empty classes are reflected in RLI at this
6803 include_empty_classes (rli
);
6805 /* Make sure not to create any structures with zero size. */
6806 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6808 build_decl (input_location
,
6809 FIELD_DECL
, NULL_TREE
, char_type_node
));
6811 /* If this is a non-POD, declaring it packed makes a difference to how it
6812 can be used as a field; don't let finalize_record_size undo it. */
6813 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6814 rli
->packed_maybe_necessary
= true;
6816 /* Let the back end lay out the type. */
6817 finish_record_layout (rli
, /*free_p=*/true);
6819 /* If we didn't end up needing an as-base type, don't use it. */
6820 if (CLASSTYPE_AS_BASE (t
) != t
6821 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
6822 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
6823 causing us to lose the user-specified alignment as in PR94050. */
6824 && TYPE_USER_ALIGN (t
) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t
))
6825 && tree_int_cst_equal (TYPE_SIZE (t
),
6826 TYPE_SIZE (CLASSTYPE_AS_BASE (t
))))
6827 CLASSTYPE_AS_BASE (t
) = t
;
6829 if (TYPE_SIZE_UNIT (t
)
6830 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6831 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6832 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6833 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
6835 /* Warn about bases that can't be talked about due to ambiguity. */
6836 maybe_warn_about_inaccessible_bases (t
);
6838 /* Now that we're done with layout, give the base fields the real types. */
6839 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6840 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6841 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6844 splay_tree_delete (empty_base_offsets
);
6846 if (CLASSTYPE_EMPTY_P (t
)
6847 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6848 TYPE_SIZE_UNIT (t
)))
6849 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6852 /* Determine the "key method" for the class type indicated by TYPE,
6853 and set CLASSTYPE_KEY_METHOD accordingly. */
6856 determine_key_method (tree type
)
6860 if (processing_template_decl
6861 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6862 || CLASSTYPE_INTERFACE_KNOWN (type
))
6865 /* The key method is the first non-pure virtual function that is not
6866 inline at the point of class definition. On some targets the
6867 key function may not be inline; those targets should not call
6868 this function until the end of the translation unit. */
6869 for (method
= TYPE_FIELDS (type
); method
; method
= DECL_CHAIN (method
))
6870 if (TREE_CODE (method
) == FUNCTION_DECL
6871 && DECL_VINDEX (method
) != NULL_TREE
6872 && ! DECL_DECLARED_INLINE_P (method
)
6873 && ! DECL_PURE_VIRTUAL_P (method
))
6875 CLASSTYPE_KEY_METHOD (type
) = method
;
6882 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6883 class data member of non-zero size, otherwise false. */
6886 field_nonempty_p (const_tree fld
)
6888 if (TREE_CODE (fld
) == ERROR_MARK
)
6891 tree type
= TREE_TYPE (fld
);
6892 if (TREE_CODE (fld
) == FIELD_DECL
6893 && TREE_CODE (type
) != ERROR_MARK
6894 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
6896 return TYPE_SIZE (type
)
6897 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
6898 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
6904 /* Used by find_flexarrays and related functions. */
6908 /* The first flexible array member or non-zero array member found
6909 in the order of layout. */
6911 /* First non-static non-empty data member in the class or its bases. */
6913 /* The first non-static non-empty data member following either
6914 the flexible array member, if found, or the zero-length array member
6915 otherwise. AFTER[1] refers to the first such data member of a union
6916 of which the struct containing the flexible array member or zero-length
6917 array is a member, or NULL when no such union exists. This element is
6918 only used during searching, not for diagnosing problems. AFTER[0]
6919 refers to the first such data member that is not a member of such
6923 /* Refers to a struct (not union) in which the struct of which the flexible
6924 array is member is defined. Used to diagnose strictly (according to C)
6925 invalid uses of the latter structs. */
6929 /* Find either the first flexible array member or the first zero-length
6930 array, in that order of preference, among members of class T (but not
6931 its base classes), and set members of FMEM accordingly.
6932 BASE_P is true if T is a base class of another class.
6933 PUN is set to the outermost union in which the flexible array member
6934 (or zero-length array) is defined if one such union exists, otherwise
6936 Similarly, PSTR is set to a data member of the outermost struct of
6937 which the flexible array is a member if one such struct exists,
6938 otherwise to NULL. */
6941 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
6942 tree pun
/* = NULL_TREE */,
6943 tree pstr
/* = NULL_TREE */)
6945 /* Set the "pointer" to the outermost enclosing union if not set
6946 yet and maintain it for the remainder of the recursion. */
6947 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
6950 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
6952 if (fld
== error_mark_node
)
6955 /* Is FLD a typedef for an anonymous struct? */
6957 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6958 handled elsewhere so that errors like the following are detected
6960 typedef struct { int i, a[], j; } S; // bug c++/72753
6961 S s [2]; // bug c++/68489
6963 if (TREE_CODE (fld
) == TYPE_DECL
6964 && DECL_IMPLICIT_TYPEDEF_P (fld
)
6965 && CLASS_TYPE_P (TREE_TYPE (fld
))
6966 && IDENTIFIER_ANON_P (DECL_NAME (fld
)))
6968 /* Check the nested unnamed type referenced via a typedef
6969 independently of FMEM (since it's not a data member of
6970 the enclosing class). */
6971 check_flexarrays (TREE_TYPE (fld
));
6975 /* Skip anything that's GCC-generated or not a (non-static) data
6977 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
6980 /* Type of the member. */
6981 tree fldtype
= TREE_TYPE (fld
);
6982 if (fldtype
== error_mark_node
)
6985 /* Determine the type of the array element or object referenced
6986 by the member so that it can be checked for flexible array
6987 members if it hasn't been yet. */
6988 tree eltype
= fldtype
;
6989 while (TREE_CODE (eltype
) == ARRAY_TYPE
6990 || INDIRECT_TYPE_P (eltype
))
6991 eltype
= TREE_TYPE (eltype
);
6993 if (RECORD_OR_UNION_TYPE_P (eltype
))
6995 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6997 /* Once the member after the flexible array has been found
6999 fmem
->after
[bool (pun
)] = fld
;
7003 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
7005 /* Descend into the non-static member struct or union and try
7006 to find a flexible array member or zero-length array among
7007 its members. This is only necessary for anonymous types
7008 and types in whose context the current type T has not been
7009 defined (the latter must not be checked again because they
7010 are already in the process of being checked by one of the
7011 recursive calls). */
7013 tree first
= fmem
->first
;
7014 tree array
= fmem
->array
;
7016 /* If this member isn't anonymous and a prior non-flexible array
7017 member has been seen in one of the enclosing structs, clear
7018 the FIRST member since it doesn't contribute to the flexible
7019 array struct's members. */
7020 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7021 fmem
->first
= NULL_TREE
;
7023 find_flexarrays (eltype
, fmem
, false, pun
,
7024 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
7026 if (fmem
->array
!= array
)
7029 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
7031 /* Restore the FIRST member reset above if no flexible
7032 array member has been found in this member's struct. */
7033 fmem
->first
= first
;
7036 /* If the member struct contains the first flexible array
7037 member, or if this member is a base class, continue to
7038 the next member and avoid setting the FMEM->NEXT pointer
7045 if (field_nonempty_p (fld
))
7047 /* Remember the first non-static data member. */
7051 /* Remember the first non-static data member after the flexible
7052 array member, if one has been found, or the zero-length array
7053 if it has been found. */
7054 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
7055 fmem
->after
[bool (pun
)] = fld
;
7058 /* Skip non-arrays. */
7059 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
7062 /* Determine the upper bound of the array if it has one. */
7063 if (TYPE_DOMAIN (fldtype
))
7067 /* Make a record of the zero-length array if either one
7068 such field or a flexible array member has been seen to
7069 handle the pathological and unlikely case of multiple
7071 if (!fmem
->after
[bool (pun
)])
7072 fmem
->after
[bool (pun
)] = fld
;
7074 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
7076 /* Remember the first zero-length array unless a flexible array
7077 member has already been seen. */
7079 fmem
->enclosing
= pstr
;
7084 /* Flexible array members have no upper bound. */
7087 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7089 /* Replace the zero-length array if it's been stored and
7090 reset the after pointer. */
7091 fmem
->after
[bool (pun
)] = NULL_TREE
;
7093 fmem
->enclosing
= pstr
;
7095 else if (!fmem
->after
[bool (pun
)])
7096 /* Make a record of another flexible array member. */
7097 fmem
->after
[bool (pun
)] = fld
;
7102 fmem
->enclosing
= pstr
;
7108 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7109 a flexible array member (or the zero-length array extension). */
7112 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
7114 if (fmem
->array
&& fmem
->enclosing
)
7116 auto_diagnostic_group d
;
7117 if (pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
7118 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
7119 ? G_("invalid use of %q#T with a zero-size array "
7121 : G_("invalid use of %q#T with a flexible array member "
7123 DECL_CONTEXT (fmem
->array
),
7124 DECL_CONTEXT (fmem
->enclosing
)))
7125 inform (DECL_SOURCE_LOCATION (fmem
->array
),
7126 "array member %q#D declared here", fmem
->array
);
7130 /* Issue diagnostics for invalid flexible array members or zero-length
7131 arrays that are not the last elements of the containing class or its
7132 base classes or that are its sole members. */
7135 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
7140 if (fmem
->first
&& !fmem
->after
[0])
7142 diagnose_invalid_flexarray (fmem
);
7146 /* Has a diagnostic been issued? */
7149 const char *msg
= 0;
7151 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7154 msg
= G_("zero-size array member %qD not at end of %q#T");
7155 else if (!fmem
->first
)
7156 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
7160 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7162 auto_diagnostic_group d
;
7163 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
7165 inform (location_of (t
), "in the definition of %q#T", t
);
7173 msg
= G_("flexible array member %qD not at end of %q#T");
7174 else if (!fmem
->first
)
7175 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
7179 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7182 auto_diagnostic_group d
;
7183 error_at (loc
, msg
, fmem
->array
, t
);
7185 /* In the unlikely event that the member following the flexible
7186 array member is declared in a different class, or the member
7187 overlaps another member of a common union, point to it.
7188 Otherwise it should be obvious. */
7190 && ((DECL_CONTEXT (fmem
->after
[0])
7191 != DECL_CONTEXT (fmem
->array
))))
7193 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
7194 "next member %q#D declared here",
7196 inform (location_of (t
), "in the definition of %q#T", t
);
7201 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
7202 diagnose_invalid_flexarray (fmem
);
7206 /* Recursively check to make sure that any flexible array or zero-length
7207 array members of class T or its bases are valid (i.e., not the sole
7208 non-static data member of T and, if one exists, that it is the last
7209 non-static data member of T and its base classes. FMEM is expected
7210 to be initially null and is used internally by recursive calls to
7211 the function. Issue the appropriate diagnostics for the array member
7212 that fails the checks. */
7215 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
7216 bool base_p
/* = false */)
7218 /* Initialize the result of a search for flexible array and zero-length
7219 array members. Avoid doing any work if the most interesting FMEM data
7220 have already been populated. */
7221 flexmems_t flexmems
= flexmems_t ();
7224 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
7227 tree fam
= fmem
->array
;
7229 /* Recursively check the primary base class first. */
7230 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7232 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
7233 check_flexarrays (basetype
, fmem
, true);
7236 /* Recursively check the base classes. */
7237 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
7238 for (int i
= 0; i
< nbases
; ++i
)
7240 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
7242 /* The primary base class was already checked above. */
7243 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
7246 /* Virtual base classes are at the end. */
7247 if (BINFO_VIRTUAL_P (base_binfo
))
7250 /* Check the base class. */
7251 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
7254 if (fmem
== &flexmems
)
7256 /* Check virtual base classes only once per derived class.
7257 I.e., this check is not performed recursively for base
7261 vec
<tree
, va_gc
> *vbases
;
7262 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
7263 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
7265 /* Check the virtual base class. */
7266 tree basetype
= TREE_TYPE (base_binfo
);
7268 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
7272 /* Is the type unnamed (and therefore a member of it potentially
7273 an anonymous struct or union)? */
7274 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
7275 if (tree ctx
= maybe_anon_p
? TYPE_CONTEXT (t
) : NULL_TREE
)
7276 maybe_anon_p
= RECORD_OR_UNION_TYPE_P (ctx
);
7278 /* Search the members of the current (possibly derived) class, skipping
7279 unnamed structs and unions since those could be anonymous. */
7280 if (fmem
!= &flexmems
|| !maybe_anon_p
)
7281 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
7283 if (fmem
== &flexmems
&& !maybe_anon_p
)
7285 /* Issue diagnostics for invalid flexible and zero-length array
7286 members found in base classes or among the members of the current
7287 class. Ignore anonymous structs and unions whose members are
7288 considered to be members of the enclosing class and thus will
7289 be diagnosed when checking it. */
7290 diagnose_flexarrays (t
, fmem
);
7294 /* Perform processing required when the definition of T (a class type)
7295 is complete. Diagnose invalid definitions of flexible array members
7296 and zero-size arrays. */
7299 finish_struct_1 (tree t
)
7302 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7303 tree virtuals
= NULL_TREE
;
7305 if (COMPLETE_TYPE_P (t
))
7307 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
7308 error ("redefinition of %q#T", t
);
7313 /* If this type was previously laid out as a forward reference,
7314 make sure we lay it out again. */
7315 TYPE_SIZE (t
) = NULL_TREE
;
7316 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
7318 /* Make assumptions about the class; we'll reset the flags if
7320 CLASSTYPE_EMPTY_P (t
) = 1;
7321 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
7322 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
7323 CLASSTYPE_LITERAL_P (t
) = true;
7325 /* Do end-of-class semantic processing: checking the validity of the
7326 bases and members and add implicitly generated methods. */
7327 check_bases_and_members (t
);
7329 /* Find the key method. */
7330 if (TYPE_CONTAINS_VPTR_P (t
))
7332 /* The Itanium C++ ABI permits the key method to be chosen when
7333 the class is defined -- even though the key method so
7334 selected may later turn out to be an inline function. On
7335 some systems (such as ARM Symbian OS) the key method cannot
7336 be determined until the end of the translation unit. On such
7337 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7338 will cause the class to be added to KEYED_CLASSES. Then, in
7339 finish_file we will determine the key method. */
7340 if (targetm
.cxx
.key_method_may_be_inline ())
7341 determine_key_method (t
);
7343 /* If a polymorphic class has no key method, we may emit the vtable
7344 in every translation unit where the class definition appears. If
7345 we're devirtualizing, we can look into the vtable even if we
7346 aren't emitting it. */
7347 if (!CLASSTYPE_KEY_METHOD (t
))
7348 vec_safe_push (keyed_classes
, t
);
7351 /* Layout the class itself. */
7352 layout_class_type (t
, &virtuals
);
7353 /* COMPLETE_TYPE_P is now true. */
7355 set_class_bindings (t
);
7357 /* With the layout complete, check for flexible array members and
7358 zero-length arrays that might overlap other members in the final
7360 check_flexarrays (t
);
7362 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
7364 /* If necessary, create the primary vtable for this class. */
7365 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
7367 /* We must enter these virtuals into the table. */
7368 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7369 build_primary_vtable (NULL_TREE
, t
);
7370 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
7371 /* Here we know enough to change the type of our virtual
7372 function table, but we will wait until later this function. */
7373 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
7375 /* If we're warning about ABI tags, check the types of the new
7376 virtual functions. */
7378 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
7379 check_abi_tags (t
, TREE_VALUE (v
));
7382 if (TYPE_CONTAINS_VPTR_P (t
))
7387 if (BINFO_VTABLE (TYPE_BINFO (t
)))
7388 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
7389 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7390 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
7392 /* Add entries for virtual functions introduced by this class. */
7393 BINFO_VIRTUALS (TYPE_BINFO (t
))
7394 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
7396 /* Set DECL_VINDEX for all functions declared in this class. */
7397 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
7399 fn
= TREE_CHAIN (fn
),
7400 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
7401 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
7403 tree fndecl
= BV_FN (fn
);
7405 if (DECL_THUNK_P (fndecl
))
7406 /* A thunk. We should never be calling this entry directly
7407 from this vtable -- we'd use the entry for the non
7408 thunk base function. */
7409 DECL_VINDEX (fndecl
) = NULL_TREE
;
7410 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7411 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7415 finish_struct_bits (t
);
7417 set_method_tm_attributes (t
);
7418 if (flag_openmp
|| flag_openmp_simd
)
7419 finish_omp_declare_simd_methods (t
);
7421 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7422 for any static member objects of the type we're working on. */
7423 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7424 if (DECL_DECLARES_FUNCTION_P (x
))
7425 DECL_IN_AGGR_P (x
) = false;
7426 else if (VAR_P (x
) && TREE_STATIC (x
)
7427 && TREE_TYPE (x
) != error_mark_node
7428 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7429 SET_DECL_MODE (x
, TYPE_MODE (t
));
7431 /* Complain if one of the field types requires lower visibility. */
7432 constrain_class_visibility (t
);
7434 /* Make the rtl for any new vtables we have created, and unmark
7435 the base types we marked. */
7438 /* Build the VTT for T. */
7442 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7443 && !CLASSTYPE_FINAL (t
))
7444 warning (OPT_Wnon_virtual_dtor
,
7445 "%q#T has virtual functions and accessible"
7446 " non-virtual destructor", t
);
7450 if (warn_overloaded_virtual
)
7453 /* Class layout, assignment of virtual table slots, etc., is now
7454 complete. Give the back end a chance to tweak the visibility of
7455 the class or perform any other required target modifications. */
7456 targetm
.cxx
.adjust_class_at_definition (t
);
7458 maybe_suppress_debug_info (t
);
7460 if (flag_vtable_verify
)
7461 vtv_save_class_info (t
);
7463 dump_class_hierarchy (t
);
7465 /* Finish debugging output for this type. */
7466 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7468 /* Recalculate satisfaction that might depend on completeness. */
7469 clear_satisfaction_cache ();
7471 if (TYPE_TRANSPARENT_AGGR (t
))
7473 tree field
= first_field (t
);
7474 if (field
== NULL_TREE
|| error_operand_p (field
))
7476 error ("type transparent %q#T does not have any fields", t
);
7477 TYPE_TRANSPARENT_AGGR (t
) = 0;
7479 else if (DECL_ARTIFICIAL (field
))
7481 if (DECL_FIELD_IS_BASE (field
))
7482 error ("type transparent class %qT has base classes", t
);
7485 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7486 error ("type transparent class %qT has virtual functions", t
);
7488 TYPE_TRANSPARENT_AGGR (t
) = 0;
7490 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7492 error ("type transparent %q#T cannot be made transparent because "
7493 "the type of the first field has a different ABI from the "
7494 "class overall", t
);
7495 TYPE_TRANSPARENT_AGGR (t
) = 0;
7500 /* When T was built up, the member declarations were added in reverse
7501 order. Rearrange them to declaration order. */
7504 unreverse_member_declarations (tree t
)
7510 /* The following lists are all in reverse order. Put them in
7511 declaration order now. */
7512 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7514 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7515 order, so we can't just use nreverse. Due to stat_hack
7516 chicanery in finish_member_declaration. */
7518 for (x
= TYPE_FIELDS (t
);
7519 x
&& TREE_CODE (x
) != TYPE_DECL
;
7522 next
= DECL_CHAIN (x
);
7523 DECL_CHAIN (x
) = prev
;
7529 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7530 TYPE_FIELDS (t
) = prev
;
7535 finish_struct (tree t
, tree attributes
)
7537 location_t saved_loc
= input_location
;
7539 /* Now that we've got all the field declarations, reverse everything
7541 unreverse_member_declarations (t
);
7543 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7544 fixup_attribute_variants (t
);
7546 /* Nadger the current location so that diagnostics point to the start of
7547 the struct, not the end. */
7548 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7550 if (processing_template_decl
)
7554 /* We need to add the target functions of USING_DECLS, so that
7555 they can be found when the using declaration is not
7556 instantiated yet. */
7557 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7558 if (TREE_CODE (x
) == USING_DECL
)
7560 tree fn
= strip_using_decl (x
);
7562 for (lkp_iterator
iter (fn
); iter
; ++iter
)
7563 add_method (t
, *iter
, true);
7565 else if (DECL_DECLARES_FUNCTION_P (x
))
7567 DECL_IN_AGGR_P (x
) = false;
7568 if (DECL_VIRTUAL_P (x
))
7569 CLASSTYPE_NON_AGGREGATE (t
) = true;
7571 else if (TREE_CODE (x
) == FIELD_DECL
)
7573 if (TREE_PROTECTED (x
) || TREE_PRIVATE (x
))
7574 CLASSTYPE_NON_AGGREGATE (t
) = true;
7577 /* Also add a USING_DECL for operator=. We know there'll be (at
7578 least) one, but we don't know the signature(s). We want name
7579 lookup not to fail or recurse into bases. This isn't added
7580 to the template decl list so we drop this at instantiation
7582 tree ass_op
= build_lang_decl (USING_DECL
, assign_op_identifier
,
7584 DECL_CONTEXT (ass_op
) = t
;
7585 USING_DECL_SCOPE (ass_op
) = t
;
7586 DECL_DEPENDENT_P (ass_op
) = true;
7587 DECL_ARTIFICIAL (ass_op
) = true;
7588 DECL_CHAIN (ass_op
) = TYPE_FIELDS (t
);
7589 TYPE_FIELDS (t
) = ass_op
;
7591 TYPE_SIZE (t
) = bitsize_zero_node
;
7592 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7593 /* COMPLETE_TYPE_P is now true. */
7595 set_class_bindings (t
);
7597 /* We need to emit an error message if this type was used as a parameter
7598 and it is an abstract type, even if it is a template. We construct
7599 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7600 account and we call complete_vars with this type, which will check
7601 the PARM_DECLS. Note that while the type is being defined,
7602 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7603 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7604 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7605 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7606 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_PURE_VIRTUAL_P (x
))
7607 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7610 /* Remember current #pragma pack value. */
7611 TYPE_PRECISION (t
) = maximum_field_alignment
;
7613 if (cxx_dialect
< cxx20
)
7615 if (!CLASSTYPE_NON_AGGREGATE (t
)
7616 && type_has_user_provided_or_explicit_constructor (t
))
7617 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7619 else if (TYPE_HAS_USER_CONSTRUCTOR (t
))
7620 CLASSTYPE_NON_AGGREGATE (t
) = 1;
7622 /* Fix up any variants we've already built. */
7623 fixup_type_variants (t
);
7626 finish_struct_1 (t
);
7627 /* COMPLETE_TYPE_P is now true. */
7629 maybe_warn_about_overly_private_class (t
);
7631 if (is_std_init_list (t
))
7633 /* People keep complaining that the compiler crashes on an invalid
7634 definition of initializer_list, so I guess we should explicitly
7635 reject it. What the compiler internals care about is that it's a
7636 template and has a pointer field followed by size_type field. */
7638 if (processing_template_decl
)
7640 tree f
= next_initializable_field (TYPE_FIELDS (t
));
7641 if (f
&& TYPE_PTR_P (TREE_TYPE (f
)))
7643 f
= next_initializable_field (DECL_CHAIN (f
));
7644 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7649 fatal_error (input_location
, "definition of %qD does not match "
7650 "%<#include <initializer_list>%>", TYPE_NAME (t
));
7653 input_location
= saved_loc
;
7655 TYPE_BEING_DEFINED (t
) = 0;
7657 if (current_class_type
)
7660 error ("trying to finish struct, but kicked out due to previous parse errors");
7663 for (tree decl
= TYPE_FIELDS (t
); decl
; decl
= DECL_CHAIN (decl
))
7664 if (TREE_CODE (decl
) == FUNCTION_DECL
7665 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
7666 if (tree attr
= lookup_attribute ("omp declare variant base",
7667 DECL_ATTRIBUTES (decl
)))
7668 omp_declare_variant_finalize (decl
, attr
);
7670 if (processing_template_decl
&& at_function_scope_p ()
7671 /* Lambdas are defined by the LAMBDA_EXPR. */
7672 && !LAMBDA_TYPE_P (t
))
7673 add_stmt (build_min (TAG_DEFN
, t
));
7678 /* Hash table to avoid endless recursion when handling references. */
7679 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7681 /* Return the dynamic type of INSTANCE, if known.
7682 Used to determine whether the virtual function table is needed
7685 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7686 of our knowledge of its type. *NONNULL should be initialized
7687 before this function is called. */
7690 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7692 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7694 switch (TREE_CODE (instance
))
7697 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
7700 return RECUR (TREE_OPERAND (instance
, 0));
7703 /* This is a call to a constructor, hence it's never zero. */
7704 if (CALL_EXPR_FN (instance
)
7705 && TREE_HAS_CONSTRUCTOR (instance
))
7709 return TREE_TYPE (instance
);
7714 /* This is a call to a constructor, hence it's never zero. */
7715 if (TREE_HAS_CONSTRUCTOR (instance
))
7719 return TREE_TYPE (instance
);
7721 return RECUR (TREE_OPERAND (instance
, 0));
7723 case POINTER_PLUS_EXPR
:
7726 if (ADDR_EXPR_P (TREE_OPERAND (instance
, 0)))
7727 return RECUR (TREE_OPERAND (instance
, 0));
7728 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7729 /* Propagate nonnull. */
7730 return RECUR (TREE_OPERAND (instance
, 0));
7735 return RECUR (TREE_OPERAND (instance
, 0));
7738 instance
= TREE_OPERAND (instance
, 0);
7741 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7742 with a real object -- given &p->f, p can still be null. */
7743 tree t
= get_base_address (instance
);
7744 /* ??? Probably should check DECL_WEAK here. */
7745 if (t
&& DECL_P (t
))
7748 return RECUR (instance
);
7751 /* If this component is really a base class reference, then the field
7752 itself isn't definitive. */
7753 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
7754 return RECUR (TREE_OPERAND (instance
, 0));
7755 return RECUR (TREE_OPERAND (instance
, 1));
7759 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
7760 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7764 return TREE_TYPE (TREE_TYPE (instance
));
7770 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7774 return TREE_TYPE (instance
);
7776 else if (instance
== current_class_ptr
)
7781 /* if we're in a ctor or dtor, we know our type. If
7782 current_class_ptr is set but we aren't in a function, we're in
7783 an NSDMI (and therefore a constructor). */
7784 if (current_scope () != current_function_decl
7785 || (DECL_LANG_SPECIFIC (current_function_decl
)
7786 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7787 || DECL_DESTRUCTOR_P (current_function_decl
))))
7791 return TREE_TYPE (TREE_TYPE (instance
));
7794 else if (TYPE_REF_P (TREE_TYPE (instance
)))
7796 /* We only need one hash table because it is always left empty. */
7797 if (!fixed_type_or_null_ref_ht
)
7798 fixed_type_or_null_ref_ht
7799 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7801 /* Reference variables should be references to objects. */
7805 /* Enter the INSTANCE in a table to prevent recursion; a
7806 variable's initializer may refer to the variable
7808 if (VAR_P (instance
)
7809 && DECL_INITIAL (instance
)
7810 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7811 && !fixed_type_or_null_ref_ht
->find (instance
))
7816 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7818 type
= RECUR (DECL_INITIAL (instance
));
7819 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7826 case VIEW_CONVERT_EXPR
:
7827 if (location_wrapper_p (instance
))
7828 return RECUR (TREE_OPERAND (instance
, 0));
7830 /* TODO: Recursion may be correct for some non-location-wrapper
7831 uses of VIEW_CONVERT_EXPR. */
7840 /* Return nonzero if the dynamic type of INSTANCE is known, and
7841 equivalent to the static type. We also handle the case where
7842 INSTANCE is really a pointer. Return negative if this is a
7843 ctor/dtor. There the dynamic type is known, but this might not be
7844 the most derived base of the original object, and hence virtual
7845 bases may not be laid out according to this type.
7847 Used to determine whether the virtual function table is needed
7850 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7851 of our knowledge of its type. *NONNULL should be initialized
7852 before this function is called. */
7855 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7857 tree t
= TREE_TYPE (instance
);
7861 /* processing_template_decl can be false in a template if we're in
7862 instantiate_non_dependent_expr, but we still want to suppress
7864 if (in_template_function ())
7866 /* In a template we only care about the type of the result. */
7872 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7873 if (INDIRECT_TYPE_P (t
))
7875 if (CLASS_TYPE_P (t
) && CLASSTYPE_FINAL (t
))
7877 if (fixed
== NULL_TREE
)
7879 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7881 return cdtorp
? -1 : 1;
7886 init_class_processing (void)
7888 current_class_depth
= 0;
7889 current_class_stack_size
= 10;
7891 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7892 sizeof_biggest_empty_class
= size_zero_node
;
7894 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7895 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7896 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7899 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7902 restore_class_cache (void)
7906 /* We are re-entering the same class we just left, so we don't
7907 have to search the whole inheritance matrix to find all the
7908 decls to bind again. Instead, we install the cached
7909 class_shadowed list and walk through it binding names. */
7910 push_binding_level (previous_class_level
);
7911 class_binding_level
= previous_class_level
;
7912 /* Restore IDENTIFIER_TYPE_VALUE. */
7913 for (type
= class_binding_level
->type_shadowed
;
7915 type
= TREE_CHAIN (type
))
7916 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7919 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7920 appropriate for TYPE.
7922 So that we may avoid calls to lookup_name, we cache the _TYPE
7923 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7925 For multiple inheritance, we perform a two-pass depth-first search
7926 of the type lattice. */
7929 pushclass (tree type
)
7931 class_stack_node_t csn
;
7933 type
= TYPE_MAIN_VARIANT (type
);
7935 /* Make sure there is enough room for the new entry on the stack. */
7936 if (current_class_depth
+ 1 >= current_class_stack_size
)
7938 current_class_stack_size
*= 2;
7940 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7941 current_class_stack_size
);
7944 /* Insert a new entry on the class stack. */
7945 csn
= current_class_stack
+ current_class_depth
;
7946 csn
->name
= current_class_name
;
7947 csn
->type
= current_class_type
;
7948 csn
->access
= current_access_specifier
;
7949 csn
->names_used
= 0;
7951 current_class_depth
++;
7953 /* Now set up the new type. */
7954 current_class_name
= TYPE_NAME (type
);
7955 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7956 current_class_name
= DECL_NAME (current_class_name
);
7957 current_class_type
= type
;
7959 /* By default, things in classes are private, while things in
7960 structures or unions are public. */
7961 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7962 ? access_private_node
7963 : access_public_node
);
7965 if (previous_class_level
7966 && type
!= previous_class_level
->this_entity
7967 && current_class_depth
== 1)
7969 /* Forcibly remove any old class remnants. */
7970 invalidate_class_lookup_cache ();
7973 if (!previous_class_level
7974 || type
!= previous_class_level
->this_entity
7975 || current_class_depth
> 1)
7978 restore_class_cache ();
7981 /* Get out of the current class scope. If we were in a class scope
7982 previously, that is the one popped to. */
7989 current_class_depth
--;
7990 current_class_name
= current_class_stack
[current_class_depth
].name
;
7991 current_class_type
= current_class_stack
[current_class_depth
].type
;
7992 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7993 if (current_class_stack
[current_class_depth
].names_used
)
7994 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
7997 /* Mark the top of the class stack as hidden. */
8000 push_class_stack (void)
8002 if (current_class_depth
)
8003 ++current_class_stack
[current_class_depth
- 1].hidden
;
8006 /* Mark the top of the class stack as un-hidden. */
8009 pop_class_stack (void)
8011 if (current_class_depth
)
8012 --current_class_stack
[current_class_depth
- 1].hidden
;
8015 /* If the class type currently being defined is either T or
8016 a nested type of T, returns the type from the current_class_stack,
8017 which might be equivalent to but not equal to T in case of
8018 constrained partial specializations. */
8021 currently_open_class (tree t
)
8025 if (!CLASS_TYPE_P (t
))
8028 t
= TYPE_MAIN_VARIANT (t
);
8030 /* We start looking from 1 because entry 0 is from global scope,
8032 for (i
= current_class_depth
; i
> 0; --i
)
8035 if (i
== current_class_depth
)
8036 c
= current_class_type
;
8039 if (current_class_stack
[i
].hidden
)
8041 c
= current_class_stack
[i
].type
;
8045 if (same_type_p (c
, t
))
8051 /* If either current_class_type or one of its enclosing classes are derived
8052 from T, return the appropriate type. Used to determine how we found
8053 something via unqualified lookup. */
8056 currently_open_derived_class (tree t
)
8060 /* The bases of a dependent type are unknown. */
8061 if (dependent_type_p (t
))
8064 if (!current_class_type
)
8067 if (DERIVED_FROM_P (t
, current_class_type
))
8068 return current_class_type
;
8070 for (i
= current_class_depth
- 1; i
> 0; --i
)
8072 if (current_class_stack
[i
].hidden
)
8074 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
8075 return current_class_stack
[i
].type
;
8081 /* Return the outermost enclosing class type that is still open, or
8085 outermost_open_class (void)
8087 if (!current_class_type
)
8090 if (TYPE_BEING_DEFINED (current_class_type
))
8091 r
= current_class_type
;
8092 for (int i
= current_class_depth
- 1; i
> 0; --i
)
8094 if (current_class_stack
[i
].hidden
)
8096 tree t
= current_class_stack
[i
].type
;
8097 if (!TYPE_BEING_DEFINED (t
))
8104 /* Returns the innermost class type which is not a lambda closure type. */
8107 current_nonlambda_class_type (void)
8109 tree type
= current_class_type
;
8110 while (type
&& LAMBDA_TYPE_P (type
))
8111 type
= decl_type_context (TYPE_NAME (type
));
8115 /* When entering a class scope, all enclosing class scopes' names with
8116 static meaning (static variables, static functions, types and
8117 enumerators) have to be visible. This recursive function calls
8118 pushclass for all enclosing class contexts until global or a local
8119 scope is reached. TYPE is the enclosed class. */
8122 push_nested_class (tree type
)
8124 /* A namespace might be passed in error cases, like A::B:C. */
8125 if (type
== NULL_TREE
8126 || !CLASS_TYPE_P (type
))
8129 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
8134 /* Undoes a push_nested_class call. */
8137 pop_nested_class (void)
8139 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
8142 if (context
&& CLASS_TYPE_P (context
))
8143 pop_nested_class ();
8146 /* Returns the number of extern "LANG" blocks we are nested within. */
8149 current_lang_depth (void)
8151 return vec_safe_length (current_lang_base
);
8154 /* Set global variables CURRENT_LANG_NAME to appropriate value
8155 so that behavior of name-mangling machinery is correct. */
8158 push_lang_context (tree name
)
8160 vec_safe_push (current_lang_base
, current_lang_name
);
8162 if (name
== lang_name_cplusplus
)
8163 current_lang_name
= name
;
8164 else if (name
== lang_name_c
)
8165 current_lang_name
= name
;
8167 error ("language string %<\"%E\"%> not recognized", name
);
8170 /* Get out of the current language scope. */
8173 pop_lang_context (void)
8175 current_lang_name
= current_lang_base
->pop ();
8178 /* Type instantiation routines. */
8180 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8181 matches the TARGET_TYPE. If there is no satisfactory match, return
8182 error_mark_node, and issue an error & warning messages under
8183 control of FLAGS. Permit pointers to member function if FLAGS
8184 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8185 a template-id, and EXPLICIT_TARGS are the explicitly provided
8188 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8189 is the base path used to reference those member functions. If
8190 the address is resolved to a member function, access checks will be
8191 performed and errors issued if appropriate. */
8194 resolve_address_of_overloaded_function (tree target_type
,
8196 tsubst_flags_t complain
,
8198 tree explicit_targs
,
8201 /* Here's what the standard says:
8205 If the name is a function template, template argument deduction
8206 is done, and if the argument deduction succeeds, the deduced
8207 arguments are used to generate a single template function, which
8208 is added to the set of overloaded functions considered.
8210 Non-member functions and static member functions match targets of
8211 type "pointer-to-function" or "reference-to-function." Nonstatic
8212 member functions match targets of type "pointer-to-member
8213 function;" the function type of the pointer to member is used to
8214 select the member function from the set of overloaded member
8215 functions. If a non-static member function is selected, the
8216 reference to the overloaded function name is required to have the
8217 form of a pointer to member as described in 5.3.1.
8219 If more than one function is selected, any template functions in
8220 the set are eliminated if the set also contains a non-template
8221 function, and any given template function is eliminated if the
8222 set contains a second template function that is more specialized
8223 than the first according to the partial ordering rules 14.5.5.2.
8224 After such eliminations, if any, there shall remain exactly one
8225 selected function. */
8228 /* We store the matches in a TREE_LIST rooted here. The functions
8229 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8230 interoperability with most_specialized_instantiation. */
8231 tree matches
= NULL_TREE
;
8233 tree target_fn_type
;
8235 /* By the time we get here, we should be seeing only real
8236 pointer-to-member types, not the internal POINTER_TYPE to
8237 METHOD_TYPE representation. */
8238 gcc_assert (!TYPE_PTR_P (target_type
)
8239 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
8241 gcc_assert (is_overloaded_fn (overload
));
8243 /* Check that the TARGET_TYPE is reasonable. */
8244 if (TYPE_PTRFN_P (target_type
)
8245 || TYPE_REFFN_P (target_type
))
8247 else if (TYPE_PTRMEMFUNC_P (target_type
))
8248 /* This is OK, too. */
8250 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
8251 /* This is OK, too. This comes from a conversion to reference
8253 target_type
= build_reference_type (target_type
);
8256 if (complain
& tf_error
)
8257 error ("cannot resolve overloaded function %qD based on"
8258 " conversion to type %qT",
8259 OVL_NAME (overload
), target_type
);
8260 return error_mark_node
;
8263 /* Non-member functions and static member functions match targets of type
8264 "pointer-to-function" or "reference-to-function." Nonstatic member
8265 functions match targets of type "pointer-to-member-function;" the
8266 function type of the pointer to member is used to select the member
8267 function from the set of overloaded member functions.
8269 So figure out the FUNCTION_TYPE that we want to match against. */
8270 target_fn_type
= static_fn_type (target_type
);
8272 /* If we can find a non-template function that matches, we can just
8273 use it. There's no point in generating template instantiations
8274 if we're just going to throw them out anyhow. But, of course, we
8275 can only do this when we don't *need* a template function. */
8277 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8281 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8282 /* We're not looking for templates just yet. */
8285 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
) != is_ptrmem
)
8286 /* We're looking for a non-static member, and this isn't
8287 one, or vice versa. */
8290 /* In C++17 we need the noexcept-qualifier to compare types. */
8291 if (flag_noexcept_type
8292 && !maybe_instantiate_noexcept (fn
, complain
))
8295 /* See if there's a match. */
8296 tree fntype
= static_fn_type (fn
);
8297 if (same_type_p (target_fn_type
, fntype
)
8298 || fnptr_conv_p (target_fn_type
, fntype
))
8299 matches
= tree_cons (fn
, NULL_TREE
, matches
);
8302 /* Now, if we've already got a match (or matches), there's no need
8303 to proceed to the template functions. But, if we don't have a
8304 match we need to look at them, too. */
8307 tree target_arg_types
;
8308 tree target_ret_type
;
8310 unsigned int nargs
, ia
;
8313 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
8314 target_ret_type
= TREE_TYPE (target_fn_type
);
8316 nargs
= list_length (target_arg_types
);
8317 args
= XALLOCAVEC (tree
, nargs
);
8318 for (arg
= target_arg_types
, ia
= 0;
8319 arg
!= NULL_TREE
&& arg
!= void_list_node
;
8320 arg
= TREE_CHAIN (arg
), ++ia
)
8321 args
[ia
] = TREE_VALUE (arg
);
8324 for (lkp_iterator
iter (overload
); iter
; ++iter
)
8330 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8331 /* We're only looking for templates. */
8334 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8336 /* We're not looking for a non-static member, and this is
8337 one, or vice versa. */
8340 tree ret
= target_ret_type
;
8342 /* If the template has a deduced return type, don't expose it to
8343 template argument deduction. */
8344 if (undeduced_auto_decl (fn
))
8347 /* Try to do argument deduction. */
8348 targs
= make_tree_vec (DECL_NTPARMS (fn
));
8349 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
8351 DEDUCE_EXACT
, LOOKUP_NORMAL
,
8352 NULL
, false, false);
8353 if (instantiation
== error_mark_node
)
8354 /* Instantiation failed. */
8357 /* Constraints must be satisfied. This is done before
8358 return type deduction since that instantiates the
8360 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
8363 /* And now force instantiation to do return type deduction. */
8364 if (undeduced_auto_decl (instantiation
))
8367 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
8370 require_deduced_type (instantiation
);
8373 /* In C++17 we need the noexcept-qualifier to compare types. */
8374 if (flag_noexcept_type
)
8375 maybe_instantiate_noexcept (instantiation
, complain
);
8377 /* See if there's a match. */
8378 tree fntype
= static_fn_type (instantiation
);
8379 if (same_type_p (target_fn_type
, fntype
)
8380 || fnptr_conv_p (target_fn_type
, fntype
))
8381 matches
= tree_cons (instantiation
, fn
, matches
);
8384 /* Now, remove all but the most specialized of the matches. */
8387 tree match
= most_specialized_instantiation (matches
);
8389 if (match
!= error_mark_node
)
8390 matches
= tree_cons (TREE_PURPOSE (match
),
8396 /* Now we should have exactly one function in MATCHES. */
8397 if (matches
== NULL_TREE
)
8399 /* There were *no* matches. */
8400 if (complain
& tf_error
)
8402 error ("no matches converting function %qD to type %q#T",
8403 OVL_NAME (overload
), target_type
);
8405 print_candidates (overload
);
8407 return error_mark_node
;
8409 else if (TREE_CHAIN (matches
))
8411 /* There were too many matches. First check if they're all
8412 the same function. */
8413 tree match
= NULL_TREE
;
8415 fn
= TREE_PURPOSE (matches
);
8417 /* For multi-versioned functions, more than one match is just fine and
8418 decls_match will return false as they are different. */
8419 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
8420 if (!decls_match (fn
, TREE_PURPOSE (match
))
8421 && !targetm
.target_option
.function_versions
8422 (fn
, TREE_PURPOSE (match
)))
8427 if (complain
& tf_error
)
8429 error ("converting overloaded function %qD to type %q#T is ambiguous",
8430 OVL_NAME (overload
), target_type
);
8432 /* Since print_candidates expects the functions in the
8433 TREE_VALUE slot, we flip them here. */
8434 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8435 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8437 print_candidates (matches
);
8440 return error_mark_node
;
8444 /* Good, exactly one match. Now, convert it to the correct type. */
8445 fn
= TREE_PURPOSE (matches
);
8447 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8448 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8450 static int explained
;
8452 if (!(complain
& tf_error
))
8453 return error_mark_node
;
8455 auto_diagnostic_group d
;
8456 if (permerror (input_location
, "assuming pointer to member %qD", fn
)
8459 inform (input_location
, "(a pointer to member can only be "
8460 "formed with %<&%E%>)", fn
);
8465 /* If a pointer to a function that is multi-versioned is requested, the
8466 pointer to the dispatcher function is returned instead. This works
8467 well because indirectly calling the function will dispatch the right
8468 function version at run-time. */
8469 if (DECL_FUNCTION_VERSIONED (fn
))
8471 fn
= get_function_version_dispatcher (fn
);
8473 return error_mark_node
;
8474 /* Mark all the versions corresponding to the dispatcher as used. */
8475 if (!(complain
& tf_conv
))
8476 mark_versions_used (fn
);
8479 /* If we're doing overload resolution purely for the purpose of
8480 determining conversion sequences, we should not consider the
8481 function used. If this conversion sequence is selected, the
8482 function will be marked as used at this point. */
8483 if (!(complain
& tf_conv
))
8485 /* Make =delete work with SFINAE. */
8486 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8487 return error_mark_node
;
8488 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8489 return error_mark_node
;
8492 /* We could not check access to member functions when this
8493 expression was originally created since we did not know at that
8494 time to which function the expression referred. */
8495 if (DECL_FUNCTION_MEMBER_P (fn
))
8497 gcc_assert (access_path
);
8498 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8501 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8502 return cp_build_addr_expr (fn
, complain
);
8505 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8506 will mark the function as addressed, but here we must do it
8508 cxx_mark_addressable (fn
);
8514 /* This function will instantiate the type of the expression given in
8515 RHS to match the type of LHSTYPE. If errors exist, then return
8516 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8517 we complain on errors. If we are not complaining, never modify rhs,
8518 as overload resolution wants to try many possible instantiations, in
8519 the hope that at least one will work.
8521 For non-recursive calls, LHSTYPE should be a function, pointer to
8522 function, or a pointer to member function. */
8525 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8527 tsubst_flags_t complain_in
= complain
;
8528 tree access_path
= NULL_TREE
;
8530 complain
&= ~tf_ptrmem_ok
;
8532 if (lhstype
== unknown_type_node
)
8534 if (complain
& tf_error
)
8535 error ("not enough type information");
8536 return error_mark_node
;
8539 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8541 tree fntype
= non_reference (lhstype
);
8542 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8544 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8546 if (flag_ms_extensions
8547 && TYPE_PTRMEMFUNC_P (fntype
)
8548 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8549 /* Microsoft allows `A::f' to be resolved to a
8550 pointer-to-member. */
8554 if (complain
& tf_error
)
8555 error ("cannot convert %qE from type %qT to type %qT",
8556 rhs
, TREE_TYPE (rhs
), fntype
);
8557 return error_mark_node
;
8561 /* If we instantiate a template, and it is a A ?: C expression
8562 with omitted B, look through the SAVE_EXPR. */
8563 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8564 rhs
= TREE_OPERAND (rhs
, 0);
8566 if (BASELINK_P (rhs
))
8568 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8569 rhs
= BASELINK_FUNCTIONS (rhs
);
8572 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8573 deduce any type information. */
8574 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
8576 if (complain
& tf_error
)
8577 error ("not enough type information");
8578 return error_mark_node
;
8581 /* There are only a few kinds of expressions that may have a type
8582 dependent on overload resolution. */
8583 gcc_assert (ADDR_EXPR_P (rhs
)
8584 || TREE_CODE (rhs
) == COMPONENT_REF
8585 || is_overloaded_fn (rhs
)
8586 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8588 /* This should really only be used when attempting to distinguish
8589 what sort of a pointer to function we have. For now, any
8590 arithmetic operation which is not supported on pointers
8591 is rejected as an error. */
8593 switch (TREE_CODE (rhs
))
8597 tree member
= TREE_OPERAND (rhs
, 1);
8599 member
= instantiate_type (lhstype
, member
, complain
);
8600 if (member
!= error_mark_node
8601 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8602 /* Do not lose object's side effects. */
8603 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8604 TREE_OPERAND (rhs
, 0), member
);
8609 rhs
= TREE_OPERAND (rhs
, 1);
8610 if (BASELINK_P (rhs
))
8611 return instantiate_type (lhstype
, rhs
, complain_in
);
8613 /* This can happen if we are forming a pointer-to-member for a
8615 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8619 case TEMPLATE_ID_EXPR
:
8621 tree fns
= TREE_OPERAND (rhs
, 0);
8622 tree args
= TREE_OPERAND (rhs
, 1);
8625 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8626 /*template_only=*/true,
8633 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8634 /*template_only=*/false,
8635 /*explicit_targs=*/NULL_TREE
,
8640 if (PTRMEM_OK_P (rhs
))
8641 complain
|= tf_ptrmem_ok
;
8643 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8647 return error_mark_node
;
8652 return error_mark_node
;
8655 /* Return the name of the virtual function pointer field
8656 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8657 this may have to look back through base types to find the
8658 ultimate field name. (For single inheritance, these could
8659 all be the same name. Who knows for multiple inheritance). */
8662 get_vfield_name (tree type
)
8664 tree binfo
, base_binfo
;
8666 for (binfo
= TYPE_BINFO (type
);
8667 BINFO_N_BASE_BINFOS (binfo
);
8670 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8672 if (BINFO_VIRTUAL_P (base_binfo
)
8673 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8677 type
= BINFO_TYPE (binfo
);
8678 tree ctor_name
= constructor_name (type
);
8679 char *buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8680 + IDENTIFIER_LENGTH (ctor_name
) + 2);
8681 sprintf (buf
, VFIELD_NAME_FORMAT
, IDENTIFIER_POINTER (ctor_name
));
8682 return get_identifier (buf
);
8685 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8686 according to [class]:
8687 The class-name is also inserted
8688 into the scope of the class itself. For purposes of access checking,
8689 the inserted class name is treated as if it were a public member name. */
8692 build_self_reference (void)
8694 tree name
= DECL_NAME (TYPE_NAME (current_class_type
));
8695 tree decl
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8697 DECL_NONLOCAL (decl
) = 1;
8698 DECL_CONTEXT (decl
) = current_class_type
;
8699 DECL_ARTIFICIAL (decl
) = 1;
8700 SET_DECL_SELF_REFERENCE_P (decl
);
8701 set_underlying_type (decl
);
8703 if (processing_template_decl
)
8704 decl
= push_template_decl (decl
);
8706 tree saved_cas
= current_access_specifier
;
8707 current_access_specifier
= access_public_node
;
8708 finish_member_declaration (decl
);
8709 current_access_specifier
= saved_cas
;
8712 /* Returns 1 if TYPE contains only padding bytes. */
8715 is_empty_class (tree type
)
8717 if (type
== error_mark_node
)
8720 if (! CLASS_TYPE_P (type
))
8723 return CLASSTYPE_EMPTY_P (type
);
8726 /* Returns true if TYPE contains no actual data, just various
8727 possible combinations of empty classes. If IGNORE_VPTR is true,
8728 a vptr doesn't prevent the class from being considered empty. Typically
8729 we want to ignore the vptr on assignment, and not on initialization. */
8732 is_really_empty_class (tree type
, bool ignore_vptr
)
8734 if (CLASS_TYPE_P (type
))
8741 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8742 out, but we'd like to be able to check this before then. */
8743 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8746 if (!ignore_vptr
&& TYPE_CONTAINS_VPTR_P (type
))
8749 for (binfo
= TYPE_BINFO (type
), i
= 0;
8750 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8751 if (!is_really_empty_class (BINFO_TYPE (base_binfo
), ignore_vptr
))
8753 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8754 if (TREE_CODE (field
) == FIELD_DECL
8755 && !DECL_ARTIFICIAL (field
)
8756 /* An unnamed bit-field is not a data member. */
8757 && !DECL_UNNAMED_BIT_FIELD (field
)
8758 && !is_really_empty_class (TREE_TYPE (field
), ignore_vptr
))
8762 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8763 return (integer_zerop (array_type_nelts_top (type
))
8764 || is_really_empty_class (TREE_TYPE (type
), ignore_vptr
));
8768 /* Note that NAME was looked up while the current class was being
8769 defined and that the result of that lookup was DECL. */
8772 maybe_note_name_used_in_class (tree name
, tree decl
)
8774 splay_tree names_used
;
8776 /* If we're not defining a class, there's nothing to do. */
8777 if (!(innermost_scope_kind() == sk_class
8778 && TYPE_BEING_DEFINED (current_class_type
)
8779 && !LAMBDA_TYPE_P (current_class_type
)))
8782 /* If there's already a binding for this NAME, then we don't have
8783 anything to worry about. */
8784 if (lookup_member (current_class_type
, name
,
8785 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8788 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8789 current_class_stack
[current_class_depth
- 1].names_used
8790 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8791 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8793 splay_tree_insert (names_used
,
8794 (splay_tree_key
) name
,
8795 (splay_tree_value
) decl
);
8798 /* Note that NAME was declared (as DECL) in the current class. Check
8799 to see that the declaration is valid. */
8802 note_name_declared_in_class (tree name
, tree decl
)
8804 splay_tree names_used
;
8807 /* Look to see if we ever used this name. */
8809 = current_class_stack
[current_class_depth
- 1].names_used
;
8812 /* The C language allows members to be declared with a type of the same
8813 name, and the C++ standard says this diagnostic is not required. So
8814 allow it in extern "C" blocks unless predantic is specified.
8815 Allow it in all cases if -ms-extensions is specified. */
8816 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8817 || flag_ms_extensions
)
8819 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8822 /* [basic.scope.class]
8824 A name N used in a class S shall refer to the same declaration
8825 in its context and when re-evaluated in the completed scope of
8827 if (permerror (location_of (decl
),
8828 "declaration of %q#D changes meaning of %qD",
8829 decl
, OVL_NAME (decl
)))
8830 inform (location_of ((tree
) n
->value
),
8831 "%qD declared here as %q#D",
8832 OVL_NAME (decl
), (tree
) n
->value
);
8836 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8837 Secondary vtables are merged with primary vtables; this function
8838 will return the VAR_DECL for the primary vtable. */
8841 get_vtbl_decl_for_binfo (tree binfo
)
8845 decl
= BINFO_VTABLE (binfo
);
8846 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8848 gcc_assert (ADDR_EXPR_P (TREE_OPERAND (decl
, 0)));
8849 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8852 gcc_assert (VAR_P (decl
));
8857 /* Returns the binfo for the primary base of BINFO. If the resulting
8858 BINFO is a virtual base, and it is inherited elsewhere in the
8859 hierarchy, then the returned binfo might not be the primary base of
8860 BINFO in the complete object. Check BINFO_PRIMARY_P or
8861 BINFO_LOST_PRIMARY_P to be sure. */
8864 get_primary_binfo (tree binfo
)
8868 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8872 return copied_binfo (primary_base
, binfo
);
8875 /* As above, but iterate until we reach the binfo that actually provides the
8879 most_primary_binfo (tree binfo
)
8882 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
8883 && !BINFO_LOST_PRIMARY_P (b
))
8885 tree primary_base
= get_primary_binfo (b
);
8886 gcc_assert (BINFO_PRIMARY_P (primary_base
)
8887 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
8893 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8894 type. Note that the virtual inheritance might be above or below BINFO in
8898 vptr_via_virtual_p (tree binfo
)
8901 binfo
= TYPE_BINFO (binfo
);
8902 tree primary
= most_primary_binfo (binfo
);
8903 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8904 a morally virtual base. */
8905 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
8906 return virt
!= NULL_TREE
;
8909 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8912 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8915 fprintf (stream
, "%*s", indent
, "");
8919 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8920 INDENT should be zero when called from the top level; it is
8921 incremented recursively. IGO indicates the next expected BINFO in
8922 inheritance graph ordering. */
8925 dump_class_hierarchy_r (FILE *stream
,
8935 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8936 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8937 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8940 fprintf (stream
, "alternative-path\n");
8943 igo
= TREE_CHAIN (binfo
);
8945 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8946 tree_to_shwi (BINFO_OFFSET (binfo
)));
8947 if (is_empty_class (BINFO_TYPE (binfo
)))
8948 fprintf (stream
, " empty");
8949 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8950 fprintf (stream
, " nearly-empty");
8951 if (BINFO_VIRTUAL_P (binfo
))
8952 fprintf (stream
, " virtual");
8953 fprintf (stream
, "\n");
8955 if (BINFO_PRIMARY_P (binfo
))
8957 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8958 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8959 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8960 TFF_PLAIN_IDENTIFIER
),
8961 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8963 if (BINFO_LOST_PRIMARY_P (binfo
))
8965 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8966 fprintf (stream
, " lost-primary");
8969 fprintf (stream
, "\n");
8971 if (!(flags
& TDF_SLIM
))
8975 if (BINFO_SUBVTT_INDEX (binfo
))
8977 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8978 fprintf (stream
, " subvttidx=%s",
8979 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8980 TFF_PLAIN_IDENTIFIER
));
8982 if (BINFO_VPTR_INDEX (binfo
))
8984 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8985 fprintf (stream
, " vptridx=%s",
8986 expr_as_string (BINFO_VPTR_INDEX (binfo
),
8987 TFF_PLAIN_IDENTIFIER
));
8989 if (BINFO_VPTR_FIELD (binfo
))
8991 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8992 fprintf (stream
, " vbaseoffset=%s",
8993 expr_as_string (BINFO_VPTR_FIELD (binfo
),
8994 TFF_PLAIN_IDENTIFIER
));
8996 if (BINFO_VTABLE (binfo
))
8998 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8999 fprintf (stream
, " vptr=%s",
9000 expr_as_string (BINFO_VTABLE (binfo
),
9001 TFF_PLAIN_IDENTIFIER
));
9005 fprintf (stream
, "\n");
9008 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9009 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
9014 /* Dump the BINFO hierarchy for T. */
9017 dump_class_hierarchy_1 (FILE *stream
, dump_flags_t flags
, tree t
)
9019 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9020 fprintf (stream
, " size=%lu align=%lu\n",
9021 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
9022 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
9023 if (tree as_base
= CLASSTYPE_AS_BASE (t
))
9024 fprintf (stream
, " base size=%lu base align=%lu\n",
9025 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base
))
9027 (unsigned long)(TYPE_ALIGN (as_base
) / BITS_PER_UNIT
));
9028 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
9029 fprintf (stream
, "\n");
9032 /* Debug interface to hierarchy dumping. */
9035 debug_class (tree t
)
9037 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
9041 dump_class_hierarchy (tree t
)
9044 if (FILE *stream
= dump_begin (class_dump_id
, &flags
))
9046 dump_class_hierarchy_1 (stream
, flags
, t
);
9047 dump_end (class_dump_id
, stream
);
9052 dump_array (FILE * stream
, tree decl
)
9055 unsigned HOST_WIDE_INT ix
;
9057 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
9059 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
9061 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
9062 fprintf (stream
, " %s entries",
9063 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
9064 TFF_PLAIN_IDENTIFIER
));
9065 fprintf (stream
, "\n");
9067 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
9069 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
9070 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
9074 dump_vtable (tree t
, tree binfo
, tree vtable
)
9077 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9082 if (!(flags
& TDF_SLIM
))
9084 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
9086 fprintf (stream
, "%s for %s",
9087 ctor_vtbl_p
? "Construction vtable" : "Vtable",
9088 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
9091 if (!BINFO_VIRTUAL_P (binfo
))
9092 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
9093 (HOST_WIDE_INT
) (uintptr_t) binfo
);
9094 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9096 fprintf (stream
, "\n");
9097 dump_array (stream
, vtable
);
9098 fprintf (stream
, "\n");
9101 dump_end (class_dump_id
, stream
);
9105 dump_vtt (tree t
, tree vtt
)
9108 FILE *stream
= dump_begin (class_dump_id
, &flags
);
9113 if (!(flags
& TDF_SLIM
))
9115 fprintf (stream
, "VTT for %s\n",
9116 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9117 dump_array (stream
, vtt
);
9118 fprintf (stream
, "\n");
9121 dump_end (class_dump_id
, stream
);
9124 /* Dump a function or thunk and its thunkees. */
9127 dump_thunk (FILE *stream
, int indent
, tree thunk
)
9129 static const char spaces
[] = " ";
9130 tree name
= DECL_NAME (thunk
);
9133 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
9135 !DECL_THUNK_P (thunk
) ? "function"
9136 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
9137 name
? IDENTIFIER_POINTER (name
) : "<unset>");
9138 if (DECL_THUNK_P (thunk
))
9140 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
9141 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
9143 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
9144 if (!virtual_adjust
)
9146 else if (DECL_THIS_THUNK_P (thunk
))
9147 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
9148 tree_to_shwi (virtual_adjust
));
9150 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
9151 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
9152 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
9153 if (THUNK_ALIAS (thunk
))
9154 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
9156 fprintf (stream
, "\n");
9157 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
9158 dump_thunk (stream
, indent
+ 2, thunks
);
9161 /* Dump the thunks for FN. */
9164 debug_thunks (tree fn
)
9166 dump_thunk (stderr
, 0, fn
);
9169 /* Virtual function table initialization. */
9171 /* Create all the necessary vtables for T and its base classes. */
9174 finish_vtbls (tree t
)
9177 vec
<constructor_elt
, va_gc
> *v
= NULL
;
9178 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
9180 /* We lay out the primary and secondary vtables in one contiguous
9181 vtable. The primary vtable is first, followed by the non-virtual
9182 secondary vtables in inheritance graph order. */
9183 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
9186 /* Then come the virtual bases, also in inheritance graph order. */
9187 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
9189 if (!BINFO_VIRTUAL_P (vbase
))
9191 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
9194 if (BINFO_VTABLE (TYPE_BINFO (t
)))
9195 initialize_vtable (TYPE_BINFO (t
), v
);
9198 /* Initialize the vtable for BINFO with the INITS. */
9201 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
9205 layout_vtable_decl (binfo
, vec_safe_length (inits
));
9206 decl
= get_vtbl_decl_for_binfo (binfo
);
9207 initialize_artificial_var (decl
, inits
);
9208 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
9211 /* Build the VTT (virtual table table) for T.
9212 A class requires a VTT if it has virtual bases.
9215 1 - primary virtual pointer for complete object T
9216 2 - secondary VTTs for each direct non-virtual base of T which requires a
9218 3 - secondary virtual pointers for each direct or indirect base of T which
9219 has virtual bases or is reachable via a virtual path from T.
9220 4 - secondary VTTs for each direct or indirect virtual base of T.
9222 Secondary VTTs look like complete object VTTs without part 4. */
9230 vec
<constructor_elt
, va_gc
> *inits
;
9232 /* Build up the initializers for the VTT. */
9234 index
= size_zero_node
;
9235 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
9237 /* If we didn't need a VTT, we're done. */
9241 /* Figure out the type of the VTT. */
9242 type
= build_array_of_n_type (const_ptr_type_node
,
9245 /* Now, build the VTT object itself. */
9246 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
9247 initialize_artificial_var (vtt
, inits
);
9248 /* Add the VTT to the vtables list. */
9249 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
9250 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
9255 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9256 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9257 and CHAIN the vtable pointer for this binfo after construction is
9258 complete. VALUE can also be another BINFO, in which case we recurse. */
9261 binfo_ctor_vtable (tree binfo
)
9267 vt
= BINFO_VTABLE (binfo
);
9268 if (TREE_CODE (vt
) == TREE_LIST
)
9269 vt
= TREE_VALUE (vt
);
9270 if (TREE_CODE (vt
) == TREE_BINFO
)
9279 /* Data for secondary VTT initialization. */
9280 struct secondary_vptr_vtt_init_data
9282 /* Is this the primary VTT? */
9285 /* Current index into the VTT. */
9288 /* Vector of initializers built up. */
9289 vec
<constructor_elt
, va_gc
> *inits
;
9291 /* The type being constructed by this secondary VTT. */
9292 tree type_being_constructed
;
9295 /* Recursively build the VTT-initializer for BINFO (which is in the
9296 hierarchy dominated by T). INITS points to the end of the initializer
9297 list to date. INDEX is the VTT index where the next element will be
9298 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9299 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9300 for virtual bases of T. When it is not so, we build the constructor
9301 vtables for the BINFO-in-T variant. */
9304 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
9310 secondary_vptr_vtt_init_data data
;
9311 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9313 /* We only need VTTs for subobjects with virtual bases. */
9314 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9317 /* We need to use a construction vtable if this is not the primary
9321 build_ctor_vtbl_group (binfo
, t
);
9323 /* Record the offset in the VTT where this sub-VTT can be found. */
9324 BINFO_SUBVTT_INDEX (binfo
) = *index
;
9327 /* Add the address of the primary vtable for the complete object. */
9328 init
= binfo_ctor_vtable (binfo
);
9329 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9332 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9333 BINFO_VPTR_INDEX (binfo
) = *index
;
9335 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
9337 /* Recursively add the secondary VTTs for non-virtual bases. */
9338 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
9339 if (!BINFO_VIRTUAL_P (b
))
9340 build_vtt_inits (b
, t
, inits
, index
);
9342 /* Add secondary virtual pointers for all subobjects of BINFO with
9343 either virtual bases or reachable along a virtual path, except
9344 subobjects that are non-virtual primary bases. */
9345 data
.top_level_p
= top_level_p
;
9346 data
.index
= *index
;
9347 data
.inits
= *inits
;
9348 data
.type_being_constructed
= BINFO_TYPE (binfo
);
9350 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
9352 *index
= data
.index
;
9354 /* data.inits might have grown as we added secondary virtual pointers.
9355 Make sure our caller knows about the new vector. */
9356 *inits
= data
.inits
;
9359 /* Add the secondary VTTs for virtual bases in inheritance graph
9361 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
9363 if (!BINFO_VIRTUAL_P (b
))
9366 build_vtt_inits (b
, t
, inits
, index
);
9369 /* Remove the ctor vtables we created. */
9370 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
9373 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9374 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9377 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
9379 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
9381 /* We don't care about bases that don't have vtables. */
9382 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
9383 return dfs_skip_bases
;
9385 /* We're only interested in proper subobjects of the type being
9387 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
9390 /* We're only interested in bases with virtual bases or reachable
9391 via a virtual path from the type being constructed. */
9392 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9393 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
9394 return dfs_skip_bases
;
9396 /* We're not interested in non-virtual primary bases. */
9397 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
9400 /* Record the index where this secondary vptr can be found. */
9401 if (data
->top_level_p
)
9403 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9404 BINFO_VPTR_INDEX (binfo
) = data
->index
;
9406 if (BINFO_VIRTUAL_P (binfo
))
9408 /* It's a primary virtual base, and this is not a
9409 construction vtable. Find the base this is primary of in
9410 the inheritance graph, and use that base's vtable
9412 while (BINFO_PRIMARY_P (binfo
))
9413 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
9417 /* Add the initializer for the secondary vptr itself. */
9418 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
9420 /* Advance the vtt index. */
9421 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
9422 TYPE_SIZE_UNIT (ptr_type_node
));
9427 /* Called from build_vtt_inits via dfs_walk. After building
9428 constructor vtables and generating the sub-vtt from them, we need
9429 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9430 binfo of the base whose sub vtt was generated. */
9433 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9435 tree vtable
= BINFO_VTABLE (binfo
);
9437 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9438 /* If this class has no vtable, none of its bases do. */
9439 return dfs_skip_bases
;
9442 /* This might be a primary base, so have no vtable in this
9446 /* If we scribbled the construction vtable vptr into BINFO, clear it
9448 if (TREE_CODE (vtable
) == TREE_LIST
9449 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9450 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9455 /* Build the construction vtable group for BINFO which is in the
9456 hierarchy dominated by T. */
9459 build_ctor_vtbl_group (tree binfo
, tree t
)
9465 vec
<constructor_elt
, va_gc
> *v
;
9467 /* See if we've already created this construction vtable group. */
9468 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9469 if (get_global_binding (id
))
9472 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9473 /* Build a version of VTBL (with the wrong type) for use in
9474 constructing the addresses of secondary vtables in the
9475 construction vtable group. */
9476 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9478 /* Don't export construction vtables from shared libraries. Even on
9479 targets that don't support hidden visibility, this tells
9480 can_refer_decl_in_current_unit_p not to assume that it's safe to
9481 access from a different compilation unit (bz 54314). */
9482 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9483 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9486 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9487 binfo
, vtbl
, t
, &v
);
9489 /* Add the vtables for each of our virtual bases using the vbase in T
9491 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9493 vbase
= TREE_CHAIN (vbase
))
9497 if (!BINFO_VIRTUAL_P (vbase
))
9499 b
= copied_binfo (vbase
, binfo
);
9501 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9504 /* Figure out the type of the construction vtable. */
9505 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9507 TREE_TYPE (vtbl
) = type
;
9508 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9509 layout_decl (vtbl
, 0);
9511 /* Initialize the construction vtable. */
9512 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9513 initialize_artificial_var (vtbl
, v
);
9514 dump_vtable (t
, binfo
, vtbl
);
9517 /* Add the vtbl initializers for BINFO (and its bases other than
9518 non-virtual primaries) to the list of INITS. BINFO is in the
9519 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9520 the constructor the vtbl inits should be accumulated for. (If this
9521 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9522 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9523 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9524 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9525 but are not necessarily the same in terms of layout. */
9528 accumulate_vtbl_inits (tree binfo
,
9533 vec
<constructor_elt
, va_gc
> **inits
)
9537 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9539 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9541 /* If it doesn't have a vptr, we don't do anything. */
9542 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9545 /* If we're building a construction vtable, we're not interested in
9546 subobjects that don't require construction vtables. */
9548 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9549 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9552 /* Build the initializers for the BINFO-in-T vtable. */
9553 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9555 /* Walk the BINFO and its bases. We walk in preorder so that as we
9556 initialize each vtable we can figure out at what offset the
9557 secondary vtable lies from the primary vtable. We can't use
9558 dfs_walk here because we need to iterate through bases of BINFO
9559 and RTTI_BINFO simultaneously. */
9560 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9562 /* Skip virtual bases. */
9563 if (BINFO_VIRTUAL_P (base_binfo
))
9565 accumulate_vtbl_inits (base_binfo
,
9566 BINFO_BASE_BINFO (orig_binfo
, i
),
9567 rtti_binfo
, vtbl
, t
,
9572 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9573 BINFO vtable to L. */
9576 dfs_accumulate_vtbl_inits (tree binfo
,
9581 vec
<constructor_elt
, va_gc
> **l
)
9583 tree vtbl
= NULL_TREE
;
9584 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9588 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9590 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9591 primary virtual base. If it is not the same primary in
9592 the hierarchy of T, we'll need to generate a ctor vtable
9593 for it, to place at its location in T. If it is the same
9594 primary, we still need a VTT entry for the vtable, but it
9595 should point to the ctor vtable for the base it is a
9596 primary for within the sub-hierarchy of RTTI_BINFO.
9598 There are three possible cases:
9600 1) We are in the same place.
9601 2) We are a primary base within a lost primary virtual base of
9603 3) We are primary to something not a base of RTTI_BINFO. */
9606 tree last
= NULL_TREE
;
9608 /* First, look through the bases we are primary to for RTTI_BINFO
9609 or a virtual base. */
9611 while (BINFO_PRIMARY_P (b
))
9613 b
= BINFO_INHERITANCE_CHAIN (b
);
9615 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9618 /* If we run out of primary links, keep looking down our
9619 inheritance chain; we might be an indirect primary. */
9620 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9621 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9625 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9626 base B and it is a base of RTTI_BINFO, this is case 2. In
9627 either case, we share our vtable with LAST, i.e. the
9628 derived-most base within B of which we are a primary. */
9630 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9631 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9632 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9633 binfo_ctor_vtable after everything's been set up. */
9636 /* Otherwise, this is case 3 and we get our own. */
9638 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9641 n_inits
= vec_safe_length (*l
);
9648 /* Add the initializer for this vtable. */
9649 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9650 &non_fn_entries
, l
);
9652 /* Figure out the position to which the VPTR should point. */
9653 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9654 index
= size_binop (MULT_EXPR
,
9655 TYPE_SIZE_UNIT (vtable_entry_type
),
9656 size_int (non_fn_entries
+ n_inits
));
9657 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9661 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9662 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9663 straighten this out. */
9664 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9665 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9666 /* Throw away any unneeded intializers. */
9667 (*l
)->truncate (n_inits
);
9669 /* For an ordinary vtable, set BINFO_VTABLE. */
9670 BINFO_VTABLE (binfo
) = vtbl
;
9673 static GTY(()) tree abort_fndecl_addr
;
9674 static GTY(()) tree dvirt_fn
;
9676 /* Construct the initializer for BINFO's virtual function table. BINFO
9677 is part of the hierarchy dominated by T. If we're building a
9678 construction vtable, the ORIG_BINFO is the binfo we should use to
9679 find the actual function pointers to put in the vtable - but they
9680 can be overridden on the path to most-derived in the graph that
9681 ORIG_BINFO belongs. Otherwise,
9682 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9683 BINFO that should be indicated by the RTTI information in the
9684 vtable; it will be a base class of T, rather than T itself, if we
9685 are building a construction vtable.
9687 The value returned is a TREE_LIST suitable for wrapping in a
9688 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9689 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9690 number of non-function entries in the vtable.
9692 It might seem that this function should never be called with a
9693 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9694 base is always subsumed by a derived class vtable. However, when
9695 we are building construction vtables, we do build vtables for
9696 primary bases; we need these while the primary base is being
9700 build_vtbl_initializer (tree binfo
,
9704 int* non_fn_entries_p
,
9705 vec
<constructor_elt
, va_gc
> **inits
)
9711 vec
<tree
, va_gc
> *vbases
;
9714 /* Initialize VID. */
9715 memset (&vid
, 0, sizeof (vid
));
9718 vid
.rtti_binfo
= rtti_binfo
;
9719 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9720 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9721 vid
.generate_vcall_entries
= true;
9722 /* The first vbase or vcall offset is at index -3 in the vtable. */
9723 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
9725 /* Add entries to the vtable for RTTI. */
9726 build_rtti_vtbl_entries (binfo
, &vid
);
9728 /* Create an array for keeping track of the functions we've
9729 processed. When we see multiple functions with the same
9730 signature, we share the vcall offsets. */
9731 vec_alloc (vid
.fns
, 32);
9732 /* Add the vcall and vbase offset entries. */
9733 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
9735 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9736 build_vbase_offset_vtbl_entries. */
9737 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
9738 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
9739 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
9741 /* If the target requires padding between data entries, add that now. */
9742 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
9744 int n_entries
= vec_safe_length (vid
.inits
);
9746 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
);
9748 /* Move data entries into their new positions and add padding
9749 after the new positions. Iterate backwards so we don't
9750 overwrite entries that we would need to process later. */
9751 for (ix
= n_entries
- 1;
9752 vid
.inits
->iterate (ix
, &e
);
9756 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
9757 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
9759 (*vid
.inits
)[new_position
] = *e
;
9761 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
9763 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
9764 f
->index
= NULL_TREE
;
9765 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9771 if (non_fn_entries_p
)
9772 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9774 /* The initializers for virtual functions were built up in reverse
9775 order. Straighten them out and add them to the running list in one
9777 jx
= vec_safe_length (*inits
);
9778 vec_safe_grow (*inits
, jx
+ vid
.inits
->length ());
9780 for (ix
= vid
.inits
->length () - 1;
9781 vid
.inits
->iterate (ix
, &e
);
9785 /* Go through all the ordinary virtual functions, building up
9787 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9791 tree fn
, fn_original
;
9792 tree init
= NULL_TREE
;
9796 if (DECL_THUNK_P (fn
))
9798 if (!DECL_NAME (fn
))
9800 if (THUNK_ALIAS (fn
))
9802 fn
= THUNK_ALIAS (fn
);
9805 fn_original
= THUNK_TARGET (fn
);
9808 /* If the only definition of this function signature along our
9809 primary base chain is from a lost primary, this vtable slot will
9810 never be used, so just zero it out. This is important to avoid
9811 requiring extra thunks which cannot be generated with the function.
9813 We first check this in update_vtable_entry_for_fn, so we handle
9814 restored primary bases properly; we also need to do it here so we
9815 zero out unused slots in ctor vtables, rather than filling them
9816 with erroneous values (though harmless, apart from relocation
9818 if (BV_LOST_PRIMARY (v
))
9819 init
= size_zero_node
;
9823 /* Pull the offset for `this', and the function to call, out of
9825 delta
= BV_DELTA (v
);
9826 vcall_index
= BV_VCALL_INDEX (v
);
9828 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9829 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9831 /* You can't call an abstract virtual function; it's abstract.
9832 So, we replace these functions with __pure_virtual. */
9833 if (DECL_PURE_VIRTUAL_P (fn_original
))
9836 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9838 if (abort_fndecl_addr
== NULL
)
9840 = fold_convert (vfunc_ptr_type_node
,
9841 build_fold_addr_expr (fn
));
9842 init
= abort_fndecl_addr
;
9845 /* Likewise for deleted virtuals. */
9846 else if (DECL_DELETED_FN (fn_original
))
9850 tree name
= get_identifier ("__cxa_deleted_virtual");
9851 dvirt_fn
= get_global_binding (name
);
9853 dvirt_fn
= push_library_fn
9855 build_function_type_list (void_type_node
, NULL_TREE
),
9856 NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
9859 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9860 init
= fold_convert (vfunc_ptr_type_node
,
9861 build_fold_addr_expr (fn
));
9865 if (!integer_zerop (delta
) || vcall_index
)
9867 fn
= make_thunk (fn
, /*this_adjusting=*/1,
9868 delta
, vcall_index
);
9869 if (!DECL_NAME (fn
))
9872 /* Take the address of the function, considering it to be of an
9873 appropriate generic type. */
9874 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9875 init
= fold_convert (vfunc_ptr_type_node
,
9876 build_fold_addr_expr (fn
));
9877 /* Don't refer to a virtual destructor from a constructor
9878 vtable or a vtable for an abstract class, since destroying
9879 an object under construction is undefined behavior and we
9880 don't want it to be considered a candidate for speculative
9881 devirtualization. But do create the thunk for ABI
9883 if (DECL_DESTRUCTOR_P (fn_original
)
9884 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9885 || orig_binfo
!= binfo
))
9886 init
= size_zero_node
;
9890 /* And add it to the chain of initializers. */
9891 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9894 if (init
== size_zero_node
)
9895 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9896 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9898 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9900 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9901 fn
, build_int_cst (NULL_TREE
, i
));
9902 TREE_CONSTANT (fdesc
) = 1;
9904 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), fdesc
);
9908 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9912 /* Adds to vid->inits the initializers for the vbase and vcall
9913 offsets in BINFO, which is in the hierarchy dominated by T. */
9916 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9920 /* If this is a derived class, we must first create entries
9921 corresponding to the primary base class. */
9922 b
= get_primary_binfo (binfo
);
9924 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9926 /* Add the vbase entries for this base. */
9927 build_vbase_offset_vtbl_entries (binfo
, vid
);
9928 /* Add the vcall entries for this base. */
9929 build_vcall_offset_vtbl_entries (binfo
, vid
);
9932 /* Returns the initializers for the vbase offset entries in the vtable
9933 for BINFO (which is part of the class hierarchy dominated by T), in
9934 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9935 where the next vbase offset will go. */
9938 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9942 tree non_primary_binfo
;
9944 /* If there are no virtual baseclasses, then there is nothing to
9946 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9951 /* We might be a primary base class. Go up the inheritance hierarchy
9952 until we find the most derived class of which we are a primary base:
9953 it is the offset of that which we need to use. */
9954 non_primary_binfo
= binfo
;
9955 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9959 /* If we have reached a virtual base, then it must be a primary
9960 base (possibly multi-level) of vid->binfo, or we wouldn't
9961 have called build_vcall_and_vbase_vtbl_entries for it. But it
9962 might be a lost primary, so just skip down to vid->binfo. */
9963 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9965 non_primary_binfo
= vid
->binfo
;
9969 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9970 if (get_primary_binfo (b
) != non_primary_binfo
)
9972 non_primary_binfo
= b
;
9975 /* Go through the virtual bases, adding the offsets. */
9976 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9978 vbase
= TREE_CHAIN (vbase
))
9983 if (!BINFO_VIRTUAL_P (vbase
))
9986 /* Find the instance of this virtual base in the complete
9988 b
= copied_binfo (vbase
, binfo
);
9990 /* If we've already got an offset for this virtual base, we
9991 don't need another one. */
9992 if (BINFO_VTABLE_PATH_MARKED (b
))
9994 BINFO_VTABLE_PATH_MARKED (b
) = 1;
9996 /* Figure out where we can find this vbase offset. */
9997 delta
= size_binop (MULT_EXPR
,
9999 fold_convert (ssizetype
,
10000 TYPE_SIZE_UNIT (vtable_entry_type
)));
10001 if (vid
->primary_vtbl_p
)
10002 BINFO_VPTR_FIELD (b
) = delta
;
10004 if (binfo
!= TYPE_BINFO (t
))
10005 /* The vbase offset had better be the same. */
10006 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
10008 /* The next vbase will come at a more negative offset. */
10009 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10010 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10012 /* The initializer is the delta from BINFO to this virtual base.
10013 The vbase offsets go in reverse inheritance-graph order, and
10014 we are walking in inheritance graph order so these end up in
10015 the right order. */
10016 delta
= size_diffop_loc (input_location
,
10017 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
10019 if (capability_type_p (vtable_entry_type
))
10020 delta
= fold_build_pointer_plus (null_pointer_node
, delta
);
10022 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
10023 fold_build1_loc (input_location
, NOP_EXPR
,
10024 vtable_entry_type
, delta
));
10028 /* Adds the initializers for the vcall offset entries in the vtable
10029 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10033 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10035 /* We only need these entries if this base is a virtual base. We
10036 compute the indices -- but do not add to the vtable -- when
10037 building the main vtable for a class. */
10038 if (binfo
== TYPE_BINFO (vid
->derived
)
10039 || (BINFO_VIRTUAL_P (binfo
)
10040 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10041 correspond to VID->DERIVED), we are building a primary
10042 construction virtual table. Since this is a primary
10043 virtual table, we do not need the vcall offsets for
10045 && binfo
!= vid
->rtti_binfo
))
10047 /* We need a vcall offset for each of the virtual functions in this
10048 vtable. For example:
10050 class A { virtual void f (); };
10051 class B1 : virtual public A { virtual void f (); };
10052 class B2 : virtual public A { virtual void f (); };
10053 class C: public B1, public B2 { virtual void f (); };
10055 A C object has a primary base of B1, which has a primary base of A. A
10056 C also has a secondary base of B2, which no longer has a primary base
10057 of A. So the B2-in-C construction vtable needs a secondary vtable for
10058 A, which will adjust the A* to a B2* to call f. We have no way of
10059 knowing what (or even whether) this offset will be when we define B2,
10060 so we store this "vcall offset" in the A sub-vtable and look it up in
10061 a "virtual thunk" for B2::f.
10063 We need entries for all the functions in our primary vtable and
10064 in our non-virtual bases' secondary vtables. */
10065 vid
->vbase
= binfo
;
10066 /* If we are just computing the vcall indices -- but do not need
10067 the actual entries -- not that. */
10068 if (!BINFO_VIRTUAL_P (binfo
))
10069 vid
->generate_vcall_entries
= false;
10070 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10071 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
10075 /* Build vcall offsets, starting with those for BINFO. */
10078 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
10081 tree primary_binfo
;
10084 /* Don't walk into virtual bases -- except, of course, for the
10085 virtual base for which we are building vcall offsets. Any
10086 primary virtual base will have already had its offsets generated
10087 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10088 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
10091 /* If BINFO has a primary base, process it first. */
10092 primary_binfo
= get_primary_binfo (binfo
);
10094 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
10096 /* Add BINFO itself to the list. */
10097 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
10099 /* Scan the non-primary bases of BINFO. */
10100 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
10101 if (base_binfo
!= primary_binfo
)
10102 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
10105 /* Called from build_vcall_offset_vtbl_entries_r. */
10108 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
10110 /* Make entries for the rest of the virtuals. */
10113 /* The ABI requires that the methods be processed in declaration
10115 for (orig_fn
= TYPE_FIELDS (BINFO_TYPE (binfo
));
10117 orig_fn
= DECL_CHAIN (orig_fn
))
10118 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
10119 add_vcall_offset (orig_fn
, binfo
, vid
);
10122 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10125 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
10129 tree derived_entry
;
10131 /* If there is already an entry for a function with the same
10132 signature as FN, then we do not need a second vcall offset.
10133 Check the list of functions already present in the derived
10135 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
10137 if (same_signature_p (derived_entry
, orig_fn
)
10138 /* We only use one vcall offset for virtual destructors,
10139 even though there are two virtual table entries. */
10140 || (DECL_DESTRUCTOR_P (derived_entry
)
10141 && DECL_DESTRUCTOR_P (orig_fn
)))
10145 /* If we are building these vcall offsets as part of building
10146 the vtable for the most derived class, remember the vcall
10148 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
10150 tree_pair_s elt
= {orig_fn
, vid
->index
};
10151 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
10154 /* The next vcall offset will be found at a more negative
10156 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10157 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10159 /* Keep track of this function. */
10160 vec_safe_push (vid
->fns
, orig_fn
);
10162 if (vid
->generate_vcall_entries
)
10167 /* Find the overriding function. */
10168 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
10169 if (fn
== error_mark_node
)
10170 vcall_offset
= build_zero_cst (vtable_entry_type
);
10173 base
= TREE_VALUE (fn
);
10175 /* The vbase we're working on is a primary base of
10176 vid->binfo. But it might be a lost primary, so its
10177 BINFO_OFFSET might be wrong, so we just use the
10178 BINFO_OFFSET from vid->binfo. */
10179 vcall_offset
= size_diffop_loc (input_location
,
10180 BINFO_OFFSET (base
),
10181 BINFO_OFFSET (vid
->binfo
));
10183 if (capability_type_p (vtable_entry_type
))
10184 vcall_offset
= fold_build_pointer_plus (null_pointer_node
,
10187 vcall_offset
= fold_build1_loc (input_location
,
10188 NOP_EXPR
, vtable_entry_type
,
10191 /* Add the initializer to the vtable. */
10192 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
10196 /* Return vtbl initializers for the RTTI entries corresponding to the
10197 BINFO's vtable. The RTTI entries should indicate the object given
10198 by VID->rtti_binfo. */
10201 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10209 t
= BINFO_TYPE (vid
->rtti_binfo
);
10211 /* To find the complete object, we will first convert to our most
10212 primary base, and then add the offset in the vtbl to that value. */
10213 b
= most_primary_binfo (binfo
);
10214 offset
= size_diffop_loc (input_location
,
10215 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
10217 /* The second entry is the address of the typeinfo object. */
10219 decl
= build_address (get_tinfo_decl (t
));
10221 decl
= capability_type_p (vfunc_ptr_type_node
)
10222 ? null_pointer_node
10223 : integer_zero_node
;
10225 /* Convert the declaration to a type that can be stored in the
10227 init
= build_nop (vfunc_ptr_type_node
, decl
);
10228 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10230 /* Add the offset-to-top entry. It comes earlier in the vtable than
10231 the typeinfo entry. Convert the offset to look like a
10232 function pointer, so that we can put it in the vtable. */
10233 if (capability_type_p (vfunc_ptr_type_node
))
10234 offset
= fold_build_pointer_plus (null_pointer_node
, offset
);
10236 init
= build_nop (vfunc_ptr_type_node
, offset
);
10237 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10240 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10244 uniquely_derived_from_p (tree parent
, tree type
)
10246 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
10247 return base
&& base
!= error_mark_node
;
10250 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10253 publicly_uniquely_derived_p (tree parent
, tree type
)
10255 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
10257 return base
&& base
!= error_mark_node
;
10260 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10261 class between them, if any. */
10264 common_enclosing_class (tree ctx1
, tree ctx2
)
10266 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
10268 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
10269 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
10272 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10273 TYPE_MARKED_P (t
) = true;
10274 tree found
= NULL_TREE
;
10275 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10276 if (TYPE_MARKED_P (t
))
10281 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10282 TYPE_MARKED_P (t
) = false;
10286 #include "gt-cp-class.h"