1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2019 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 tree
*build_base_field (record_layout_info
, tree
, splay_tree
, tree
*);
138 static void build_base_fields (record_layout_info
, splay_tree
, tree
*);
139 static void check_methods (tree
);
140 static void remove_zero_width_bit_fields (tree
);
141 static bool accessible_nvdtor_p (tree
);
143 /* Used by find_flexarrays and related functions. */
145 static void diagnose_flexarrays (tree
, const flexmems_t
*);
146 static void find_flexarrays (tree
, flexmems_t
*, bool = false,
147 tree
= NULL_TREE
, tree
= NULL_TREE
);
148 static void check_flexarrays (tree
, flexmems_t
* = NULL
, bool = false);
149 static void check_bases (tree
, int *, int *);
150 static void check_bases_and_members (tree
);
151 static tree
create_vtable_ptr (tree
, tree
*);
152 static void include_empty_classes (record_layout_info
);
153 static void layout_class_type (tree
, tree
*);
154 static void propagate_binfo_offsets (tree
, tree
);
155 static void layout_virtual_bases (record_layout_info
, splay_tree
);
156 static void build_vbase_offset_vtbl_entries (tree
, vtbl_init_data
*);
157 static void add_vcall_offset_vtbl_entries_r (tree
, vtbl_init_data
*);
158 static void add_vcall_offset_vtbl_entries_1 (tree
, vtbl_init_data
*);
159 static void build_vcall_offset_vtbl_entries (tree
, vtbl_init_data
*);
160 static void add_vcall_offset (tree
, tree
, vtbl_init_data
*);
161 static void layout_vtable_decl (tree
, int);
162 static tree
dfs_find_final_overrider_pre (tree
, void *);
163 static tree
dfs_find_final_overrider_post (tree
, void *);
164 static tree
find_final_overrider (tree
, tree
, tree
);
165 static int make_new_vtable (tree
, tree
);
166 static tree
get_primary_binfo (tree
);
167 static int maybe_indent_hierarchy (FILE *, int, int);
168 static tree
dump_class_hierarchy_r (FILE *, dump_flags_t
, tree
, tree
, int);
169 static void dump_class_hierarchy (tree
);
170 static void dump_class_hierarchy_1 (FILE *, dump_flags_t
, tree
);
171 static void dump_array (FILE *, tree
);
172 static void dump_vtable (tree
, tree
, tree
);
173 static void dump_vtt (tree
, tree
);
174 static void dump_thunk (FILE *, int, tree
);
175 static tree
build_vtable (tree
, tree
, tree
);
176 static void initialize_vtable (tree
, vec
<constructor_elt
, va_gc
> *);
177 static void layout_nonempty_base_or_field (record_layout_info
,
178 tree
, tree
, splay_tree
);
179 static void accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
180 vec
<constructor_elt
, va_gc
> **);
181 static void dfs_accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
182 vec
<constructor_elt
, va_gc
> **);
183 static void build_rtti_vtbl_entries (tree
, vtbl_init_data
*);
184 static void build_vcall_and_vbase_vtbl_entries (tree
, vtbl_init_data
*);
185 static void clone_constructors_and_destructors (tree
);
186 static tree
build_clone (tree
, tree
);
187 static void update_vtable_entry_for_fn (tree
, tree
, tree
, tree
*, unsigned);
188 static void build_ctor_vtbl_group (tree
, tree
);
189 static void build_vtt (tree
);
190 static tree
binfo_ctor_vtable (tree
);
191 static void build_vtt_inits (tree
, tree
, vec
<constructor_elt
, va_gc
> **,
193 static tree
dfs_build_secondary_vptr_vtt_inits (tree
, void *);
194 static tree
dfs_fixup_binfo_vtbls (tree
, void *);
195 static int record_subobject_offset (tree
, tree
, splay_tree
);
196 static int check_subobject_offset (tree
, tree
, splay_tree
);
197 static int walk_subobject_offsets (tree
, subobject_offset_fn
,
198 tree
, splay_tree
, tree
, int);
199 static int layout_conflict_p (tree
, tree
, splay_tree
, int);
200 static int splay_tree_compare_integer_csts (splay_tree_key k1
,
202 static void warn_about_ambiguous_bases (tree
);
203 static bool type_requires_array_cookie (tree
);
204 static bool base_derived_from (tree
, tree
);
205 static int empty_base_at_nonzero_offset_p (tree
, tree
, splay_tree
);
206 static tree
end_of_base (tree
);
207 static tree
get_vcall_index (tree
, tree
);
208 static bool type_maybe_constexpr_default_constructor (tree
);
210 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
211 'structor is in charge of 'structing virtual bases, or FALSE_STMT
215 build_if_in_charge (tree true_stmt
, tree false_stmt
)
217 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl
));
218 tree cmp
= build2 (NE_EXPR
, boolean_type_node
,
219 current_in_charge_parm
, integer_zero_node
);
220 tree type
= unlowered_expr_type (true_stmt
);
221 if (VOID_TYPE_P (type
))
222 type
= unlowered_expr_type (false_stmt
);
223 tree cond
= build3 (COND_EXPR
, type
,
224 cmp
, true_stmt
, false_stmt
);
228 /* Convert to or from a base subobject. EXPR is an expression of type
229 `A' or `A*', an expression of type `B' or `B*' is returned. To
230 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
231 the B base instance within A. To convert base A to derived B, CODE
232 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
233 In this latter case, A must not be a morally virtual base of B.
234 NONNULL is true if EXPR is known to be non-NULL (this is only
235 needed when EXPR is of pointer type). CV qualifiers are preserved
239 build_base_path (enum tree_code code
,
243 tsubst_flags_t complain
)
245 tree v_binfo
= NULL_TREE
;
246 tree d_binfo
= NULL_TREE
;
250 tree null_test
= NULL
;
251 tree ptr_target_type
;
253 int want_pointer
= TYPE_PTR_P (TREE_TYPE (expr
));
254 bool has_empty
= false;
258 if (expr
== error_mark_node
|| binfo
== error_mark_node
|| !binfo
)
259 return error_mark_node
;
261 for (probe
= binfo
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
264 if (is_empty_class (BINFO_TYPE (probe
)))
266 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
270 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
272 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
273 if (dependent_type_p (probe
))
274 if (tree open
= currently_open_class (probe
))
277 if (code
== PLUS_EXPR
278 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo
), probe
))
280 /* This can happen when adjust_result_of_qualified_name_lookup can't
281 find a unique base binfo in a call to a member function. We
282 couldn't give the diagnostic then since we might have been calling
283 a static member function, so we do it now. In other cases, eg.
284 during error recovery (c++/71979), we may not have a base at all. */
285 if (complain
& tf_error
)
287 tree base
= lookup_base (probe
, BINFO_TYPE (d_binfo
),
288 ba_unique
, NULL
, complain
);
289 gcc_assert (base
== error_mark_node
|| !base
);
291 return error_mark_node
;
294 gcc_assert ((code
== MINUS_EXPR
295 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), probe
))
296 || code
== PLUS_EXPR
);
298 if (binfo
== d_binfo
)
302 if (code
== MINUS_EXPR
&& v_binfo
)
304 if (complain
& tf_error
)
306 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (v_binfo
)))
309 error ("cannot convert from pointer to base class %qT to "
310 "pointer to derived class %qT because the base is "
311 "virtual", BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
313 error ("cannot convert from base class %qT to derived "
314 "class %qT because the base is virtual",
315 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
320 error ("cannot convert from pointer to base class %qT to "
321 "pointer to derived class %qT via virtual base %qT",
322 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
),
323 BINFO_TYPE (v_binfo
));
325 error ("cannot convert from base class %qT to derived "
326 "class %qT via virtual base %qT", BINFO_TYPE (binfo
),
327 BINFO_TYPE (d_binfo
), BINFO_TYPE (v_binfo
));
330 return error_mark_node
;
335 rvalue
= !lvalue_p (expr
);
336 /* This must happen before the call to save_expr. */
337 expr
= cp_build_addr_expr (expr
, complain
);
340 expr
= mark_rvalue_use (expr
);
342 offset
= BINFO_OFFSET (binfo
);
343 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
344 target_type
= code
== PLUS_EXPR
? BINFO_TYPE (binfo
) : BINFO_TYPE (d_binfo
);
345 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
346 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
347 expression returned matches the input. */
348 target_type
= cp_build_qualified_type
349 (target_type
, cp_type_quals (TREE_TYPE (TREE_TYPE (expr
))));
350 ptr_target_type
= build_pointer_type (target_type
);
352 /* Do we need to look in the vtable for the real offset? */
353 virtual_access
= (v_binfo
&& fixed_type_p
<= 0);
355 /* Don't bother with the calculations inside sizeof; they'll ICE if the
356 source type is incomplete and the pointer value doesn't matter. In a
357 template (even in instantiate_non_dependent_expr), we don't have vtables
358 set up properly yet, and the value doesn't matter there either; we're
359 just interested in the result of overload resolution. */
360 if (cp_unevaluated_operand
!= 0
361 || processing_template_decl
362 || in_template_function ())
364 expr
= build_nop (ptr_target_type
, expr
);
368 if (!COMPLETE_TYPE_P (probe
))
370 if (complain
& tf_error
)
371 error ("cannot convert from %qT to base class %qT because %qT is "
372 "incomplete", BINFO_TYPE (d_binfo
), BINFO_TYPE (binfo
),
373 BINFO_TYPE (d_binfo
));
374 return error_mark_node
;
377 /* If we're in an NSDMI, we don't have the full constructor context yet
378 that we need for converting to a virtual base, so just build a stub
379 CONVERT_EXPR and expand it later in bot_replace. */
380 if (virtual_access
&& fixed_type_p
< 0
381 && current_scope () != current_function_decl
)
383 expr
= build1 (CONVERT_EXPR
, ptr_target_type
, expr
);
384 CONVERT_EXPR_VBASE_PATH (expr
) = true;
388 /* Do we need to check for a null pointer? */
389 if (want_pointer
&& !nonnull
)
391 /* If we know the conversion will not actually change the value
392 of EXPR, then we can avoid testing the expression for NULL.
393 We have to avoid generating a COMPONENT_REF for a base class
394 field, because other parts of the compiler know that such
395 expressions are always non-NULL. */
396 if (!virtual_access
&& integer_zerop (offset
))
397 return build_nop (ptr_target_type
, expr
);
398 null_test
= error_mark_node
;
401 /* Protect against multiple evaluation if necessary. */
402 if (TREE_SIDE_EFFECTS (expr
) && (null_test
|| virtual_access
))
403 expr
= save_expr (expr
);
405 /* Now that we've saved expr, build the real null test. */
408 tree zero
= cp_convert (TREE_TYPE (expr
), nullptr_node
, complain
);
409 null_test
= build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
411 /* This is a compiler generated comparison, don't emit
412 e.g. -Wnonnull-compare warning for it. */
413 TREE_NO_WARNING (null_test
) = 1;
416 /* If this is a simple base reference, express it as a COMPONENT_REF. */
417 if (code
== PLUS_EXPR
&& !virtual_access
418 /* We don't build base fields for empty bases, and they aren't very
419 interesting to the optimizers anyway. */
422 expr
= cp_build_fold_indirect_ref (expr
);
423 expr
= build_simple_base_path (expr
, binfo
);
424 if (rvalue
&& lvalue_p (expr
))
427 expr
= build_address (expr
);
428 target_type
= TREE_TYPE (expr
);
434 /* Going via virtual base V_BINFO. We need the static offset
435 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
436 V_BINFO. That offset is an entry in D_BINFO's vtable. */
439 if (fixed_type_p
< 0 && in_base_initializer
)
441 /* In a base member initializer, we cannot rely on the
442 vtable being set up. We have to indirect via the
446 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
447 t
= build_pointer_type (t
);
448 v_offset
= fold_convert (t
, current_vtt_parm
);
449 v_offset
= cp_build_fold_indirect_ref (v_offset
);
454 if (sanitize_flags_p (SANITIZE_VPTR
)
455 && fixed_type_p
== 0)
457 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
462 v_offset
= build_vfield_ref (cp_build_fold_indirect_ref (t
),
463 TREE_TYPE (TREE_TYPE (expr
)));
466 if (v_offset
== error_mark_node
)
467 return error_mark_node
;
469 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
470 v_offset
= build1 (NOP_EXPR
,
471 build_pointer_type (ptrdiff_type_node
),
473 v_offset
= cp_build_fold_indirect_ref (v_offset
);
474 TREE_CONSTANT (v_offset
) = 1;
476 offset
= convert_to_integer (ptrdiff_type_node
,
477 size_diffop_loc (input_location
, offset
,
478 BINFO_OFFSET (v_binfo
)));
480 if (!integer_zerop (offset
))
481 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
483 if (fixed_type_p
< 0)
484 /* Negative fixed_type_p means this is a constructor or destructor;
485 virtual base layout is fixed in in-charge [cd]tors, but not in
487 offset
= build_if_in_charge
488 (convert_to_integer (ptrdiff_type_node
, BINFO_OFFSET (binfo
)),
495 target_type
= ptr_target_type
;
497 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
499 if (!integer_zerop (offset
))
501 offset
= fold_convert (sizetype
, offset
);
502 if (code
== MINUS_EXPR
)
503 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
504 expr
= fold_build_pointer_plus (expr
, offset
);
512 expr
= cp_build_fold_indirect_ref (expr
);
519 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
, expr
,
520 build_zero_cst (target_type
));
525 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
526 Perform a derived-to-base conversion by recursively building up a
527 sequence of COMPONENT_REFs to the appropriate base fields. */
530 build_simple_base_path (tree expr
, tree binfo
)
532 tree type
= BINFO_TYPE (binfo
);
533 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
536 if (d_binfo
== NULL_TREE
)
540 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
542 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
543 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
544 an lvalue in the front end; only _DECLs and _REFs are lvalues
546 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
548 expr
= cp_build_fold_indirect_ref (temp
);
554 expr
= build_simple_base_path (expr
, d_binfo
);
556 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
557 field
; field
= DECL_CHAIN (field
))
558 /* Is this the base field created by build_base_field? */
559 if (TREE_CODE (field
) == FIELD_DECL
560 && DECL_FIELD_IS_BASE (field
)
561 && TREE_TYPE (field
) == type
562 /* If we're looking for a field in the most-derived class,
563 also check the field offset; we can have two base fields
564 of the same type if one is an indirect virtual base and one
565 is a direct non-virtual base. */
566 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
567 || tree_int_cst_equal (byte_position (field
),
568 BINFO_OFFSET (binfo
))))
570 /* We don't use build_class_member_access_expr here, as that
571 has unnecessary checks, and more importantly results in
572 recursive calls to dfs_walk_once. */
573 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
575 expr
= build3 (COMPONENT_REF
,
576 cp_build_qualified_type (type
, type_quals
),
577 expr
, field
, NULL_TREE
);
578 /* Mark the expression const or volatile, as appropriate.
579 Even though we've dealt with the type above, we still have
580 to mark the expression itself. */
581 if (type_quals
& TYPE_QUAL_CONST
)
582 TREE_READONLY (expr
) = 1;
583 if (type_quals
& TYPE_QUAL_VOLATILE
)
584 TREE_THIS_VOLATILE (expr
) = 1;
589 /* Didn't find the base field?!? */
593 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
594 type is a class type or a pointer to a class type. In the former
595 case, TYPE is also a class type; in the latter it is another
596 pointer type. If CHECK_ACCESS is true, an error message is emitted
597 if TYPE is inaccessible. If OBJECT has pointer type, the value is
598 assumed to be non-NULL. */
601 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
602 tsubst_flags_t complain
)
607 if (TYPE_PTR_P (TREE_TYPE (object
)))
609 object_type
= TREE_TYPE (TREE_TYPE (object
));
610 type
= TREE_TYPE (type
);
613 object_type
= TREE_TYPE (object
);
615 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
617 if (!binfo
|| binfo
== error_mark_node
)
618 return error_mark_node
;
620 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
623 /* EXPR is an expression with unqualified class type. BASE is a base
624 binfo of that class type. Returns EXPR, converted to the BASE
625 type. This function assumes that EXPR is the most derived class;
626 therefore virtual bases can be found at their static offsets. */
629 convert_to_base_statically (tree expr
, tree base
)
633 expr_type
= TREE_TYPE (expr
);
634 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
636 /* If this is a non-empty base, use a COMPONENT_REF. */
637 if (!is_empty_class (BINFO_TYPE (base
)))
638 return build_simple_base_path (expr
, base
);
640 /* We use fold_build2 and fold_convert below to simplify the trees
641 provided to the optimizers. It is not safe to call these functions
642 when processing a template because they do not handle C++-specific
644 gcc_assert (!processing_template_decl
);
645 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
646 if (!integer_zerop (BINFO_OFFSET (base
)))
647 expr
= fold_build_pointer_plus_loc (input_location
,
648 expr
, BINFO_OFFSET (base
));
649 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
650 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
658 build_vfield_ref (tree datum
, tree type
)
660 tree vfield
, vcontext
;
662 if (datum
== error_mark_node
663 /* Can happen in case of duplicate base types (c++/59082). */
664 || !TYPE_VFIELD (type
))
665 return error_mark_node
;
667 /* First, convert to the requested type. */
668 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
669 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
670 /*nonnull=*/true, tf_warning_or_error
);
672 /* Second, the requested type may not be the owner of its own vptr.
673 If not, convert to the base class that owns it. We cannot use
674 convert_to_base here, because VCONTEXT may appear more than once
675 in the inheritance hierarchy of TYPE, and thus direct conversion
676 between the types may be ambiguous. Following the path back up
677 one step at a time via primary bases avoids the problem. */
678 vfield
= TYPE_VFIELD (type
);
679 vcontext
= DECL_CONTEXT (vfield
);
680 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
682 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
683 type
= TREE_TYPE (datum
);
686 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
689 /* Given an object INSTANCE, return an expression which yields the
690 vtable element corresponding to INDEX. There are many special
691 cases for INSTANCE which we take care of here, mainly to avoid
692 creating extra tree nodes when we don't have to. */
695 build_vtbl_ref (tree instance
, tree idx
)
698 tree vtbl
= NULL_TREE
;
700 /* Try to figure out what a reference refers to, and
701 access its virtual function table directly. */
704 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
706 tree basetype
= non_reference (TREE_TYPE (instance
));
708 if (fixed_type
&& !cdtorp
)
710 tree binfo
= lookup_base (fixed_type
, basetype
,
711 ba_unique
, NULL
, tf_none
);
712 if (binfo
&& binfo
!= error_mark_node
)
713 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
717 vtbl
= build_vfield_ref (instance
, basetype
);
719 aref
= build_array_ref (input_location
, vtbl
, idx
);
720 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
725 /* Given a stable object pointer INSTANCE_PTR, return an expression which
726 yields a function pointer corresponding to vtable element INDEX. */
729 build_vfn_ref (tree instance_ptr
, tree idx
)
733 aref
= build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr
), idx
);
735 /* When using function descriptors, the address of the
736 vtable entry is treated as a function pointer. */
737 if (TARGET_VTABLE_USES_DESCRIPTORS
)
738 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
739 cp_build_addr_expr (aref
, tf_warning_or_error
));
741 /* Remember this as a method reference, for later devirtualization. */
742 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
747 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
748 for the given TYPE. */
751 get_vtable_name (tree type
)
753 return mangle_vtbl_for_type (type
);
756 /* DECL is an entity associated with TYPE, like a virtual table or an
757 implicitly generated constructor. Determine whether or not DECL
758 should have external or internal linkage at the object file
759 level. This routine does not deal with COMDAT linkage and other
760 similar complexities; it simply sets TREE_PUBLIC if it possible for
761 entities in other translation units to contain copies of DECL, in
765 set_linkage_according_to_type (tree
/*type*/, tree decl
)
767 TREE_PUBLIC (decl
) = 1;
768 determine_visibility (decl
);
771 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
772 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
773 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
776 build_vtable (tree class_type
, tree name
, tree vtable_type
)
780 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
781 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
782 now to avoid confusion in mangle_decl. */
783 SET_DECL_ASSEMBLER_NAME (decl
, name
);
784 DECL_CONTEXT (decl
) = class_type
;
785 DECL_ARTIFICIAL (decl
) = 1;
786 TREE_STATIC (decl
) = 1;
787 TREE_READONLY (decl
) = 1;
788 DECL_VIRTUAL_P (decl
) = 1;
789 SET_DECL_ALIGN (decl
, TARGET_VTABLE_ENTRY_ALIGN
);
790 DECL_USER_ALIGN (decl
) = true;
791 DECL_VTABLE_OR_VTT_P (decl
) = 1;
792 set_linkage_according_to_type (class_type
, decl
);
793 /* The vtable has not been defined -- yet. */
794 DECL_EXTERNAL (decl
) = 1;
795 DECL_NOT_REALLY_EXTERN (decl
) = 1;
797 /* Mark the VAR_DECL node representing the vtable itself as a
798 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
799 is rather important that such things be ignored because any
800 effort to actually generate DWARF for them will run into
801 trouble when/if we encounter code like:
804 struct S { virtual void member (); };
806 because the artificial declaration of the vtable itself (as
807 manufactured by the g++ front end) will say that the vtable is
808 a static member of `S' but only *after* the debug output for
809 the definition of `S' has already been output. This causes
810 grief because the DWARF entry for the definition of the vtable
811 will try to refer back to an earlier *declaration* of the
812 vtable as a static member of `S' and there won't be one. We
813 might be able to arrange to have the "vtable static member"
814 attached to the member list for `S' before the debug info for
815 `S' get written (which would solve the problem) but that would
816 require more intrusive changes to the g++ front end. */
817 DECL_IGNORED_P (decl
) = 1;
822 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
823 or even complete. If this does not exist, create it. If COMPLETE is
824 nonzero, then complete the definition of it -- that will render it
825 impossible to actually build the vtable, but is useful to get at those
826 which are known to exist in the runtime. */
829 get_vtable_decl (tree type
, int complete
)
833 if (CLASSTYPE_VTABLES (type
))
834 return CLASSTYPE_VTABLES (type
);
836 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
837 CLASSTYPE_VTABLES (type
) = decl
;
841 DECL_EXTERNAL (decl
) = 1;
842 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
848 /* Build the primary virtual function table for TYPE. If BINFO is
849 non-NULL, build the vtable starting with the initial approximation
850 that it is the same as the one which is the head of the association
851 list. Returns a nonzero value if a new vtable is actually
855 build_primary_vtable (tree binfo
, tree type
)
860 decl
= get_vtable_decl (type
, /*complete=*/0);
864 if (BINFO_NEW_VTABLE_MARKED (binfo
))
865 /* We have already created a vtable for this base, so there's
866 no need to do it again. */
869 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
870 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
871 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
872 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
876 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
877 virtuals
= NULL_TREE
;
880 /* Initialize the association list for this type, based
881 on our first approximation. */
882 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
883 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
884 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
888 /* Give BINFO a new virtual function table which is initialized
889 with a skeleton-copy of its original initialization. The only
890 entry that changes is the `delta' entry, so we can really
891 share a lot of structure.
893 FOR_TYPE is the most derived type which caused this table to
896 Returns nonzero if we haven't met BINFO before.
898 The order in which vtables are built (by calling this function) for
899 an object must remain the same, otherwise a binary incompatibility
903 build_secondary_vtable (tree binfo
)
905 if (BINFO_NEW_VTABLE_MARKED (binfo
))
906 /* We already created a vtable for this base. There's no need to
910 /* Remember that we've created a vtable for this BINFO, so that we
911 don't try to do so again. */
912 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
914 /* Make fresh virtual list, so we can smash it later. */
915 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
917 /* Secondary vtables are laid out as part of the same structure as
918 the primary vtable. */
919 BINFO_VTABLE (binfo
) = NULL_TREE
;
923 /* Create a new vtable for BINFO which is the hierarchy dominated by
924 T. Return nonzero if we actually created a new vtable. */
927 make_new_vtable (tree t
, tree binfo
)
929 if (binfo
== TYPE_BINFO (t
))
930 /* In this case, it is *type*'s vtable we are modifying. We start
931 with the approximation that its vtable is that of the
932 immediate base class. */
933 return build_primary_vtable (binfo
, t
);
935 /* This is our very own copy of `basetype' to play with. Later,
936 we will fill in all the virtual functions that override the
937 virtual functions in these base classes which are not defined
938 by the current type. */
939 return build_secondary_vtable (binfo
);
942 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
943 (which is in the hierarchy dominated by T) list FNDECL as its
944 BV_FN. DELTA is the required constant adjustment from the `this'
945 pointer where the vtable entry appears to the `this' required when
946 the function is actually called. */
949 modify_vtable_entry (tree t
,
959 if (fndecl
!= BV_FN (v
)
960 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
962 /* We need a new vtable for BINFO. */
963 if (make_new_vtable (t
, binfo
))
965 /* If we really did make a new vtable, we also made a copy
966 of the BINFO_VIRTUALS list. Now, we have to find the
967 corresponding entry in that list. */
968 *virtuals
= BINFO_VIRTUALS (binfo
);
969 while (BV_FN (*virtuals
) != BV_FN (v
))
970 *virtuals
= TREE_CHAIN (*virtuals
);
974 BV_DELTA (v
) = delta
;
975 BV_VCALL_INDEX (v
) = NULL_TREE
;
981 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
982 METHOD is being injected via a using_decl. Returns true if the
983 method could be added to the method vec. */
986 add_method (tree type
, tree method
, bool via_using
)
988 if (method
== error_mark_node
)
991 gcc_assert (!DECL_EXTERN_C_P (method
));
993 tree
*slot
= find_member_slot (type
, DECL_NAME (method
));
994 tree current_fns
= slot
? *slot
: NULL_TREE
;
996 /* Check to see if we've already got this method. */
997 for (ovl_iterator
iter (current_fns
); iter
; ++iter
)
1005 if (TREE_CODE (fn
) != TREE_CODE (method
))
1008 /* Two using-declarations can coexist, we'll complain about ambiguity in
1009 overload resolution. */
1010 if (via_using
&& iter
.using_p ()
1011 /* Except handle inherited constructors specially. */
1012 && ! DECL_CONSTRUCTOR_P (fn
))
1015 /* [over.load] Member function declarations with the
1016 same name and the same parameter types cannot be
1017 overloaded if any of them is a static member
1018 function declaration.
1020 [over.load] Member function declarations with the same name and
1021 the same parameter-type-list as well as member function template
1022 declarations with the same name, the same parameter-type-list, and
1023 the same template parameter lists cannot be overloaded if any of
1024 them, but not all, have a ref-qualifier.
1026 [namespace.udecl] When a using-declaration brings names
1027 from a base class into a derived class scope, member
1028 functions in the derived class override and/or hide member
1029 functions with the same name and parameter types in a base
1030 class (rather than conflicting). */
1031 fn_type
= TREE_TYPE (fn
);
1032 method_type
= TREE_TYPE (method
);
1033 parms1
= TYPE_ARG_TYPES (fn_type
);
1034 parms2
= TYPE_ARG_TYPES (method_type
);
1036 /* Compare the quals on the 'this' parm. Don't compare
1037 the whole types, as used functions are treated as
1038 coming from the using class in overload resolution. */
1039 if (! DECL_STATIC_FUNCTION_P (fn
)
1040 && ! DECL_STATIC_FUNCTION_P (method
)
1041 /* Either both or neither need to be ref-qualified for
1042 differing quals to allow overloading. */
1043 && (FUNCTION_REF_QUALIFIED (fn_type
)
1044 == FUNCTION_REF_QUALIFIED (method_type
))
1045 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1046 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1049 /* For templates, the return type and template parameters
1050 must be identical. */
1051 if (TREE_CODE (fn
) == TEMPLATE_DECL
1052 && (!same_type_p (TREE_TYPE (fn_type
),
1053 TREE_TYPE (method_type
))
1054 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1055 DECL_TEMPLATE_PARMS (method
))))
1058 if (! DECL_STATIC_FUNCTION_P (fn
))
1059 parms1
= TREE_CHAIN (parms1
);
1060 if (! DECL_STATIC_FUNCTION_P (method
))
1061 parms2
= TREE_CHAIN (parms2
);
1063 /* Bring back parameters omitted from an inherited ctor. */
1064 if (ctor_omit_inherited_parms (fn
))
1065 parms1
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
1066 if (ctor_omit_inherited_parms (method
))
1067 parms2
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method
));
1069 if (compparms (parms1
, parms2
)
1070 && (!DECL_CONV_FN_P (fn
)
1071 || same_type_p (TREE_TYPE (fn_type
),
1072 TREE_TYPE (method_type
)))
1073 && equivalently_constrained (fn
, method
))
1075 /* If these are versions of the same function, process and
1077 if (TREE_CODE (fn
) == FUNCTION_DECL
1078 && maybe_version_functions (method
, fn
, true))
1081 if (DECL_INHERITED_CTOR (method
))
1083 if (DECL_INHERITED_CTOR (fn
))
1085 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1086 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1087 if (flag_new_inheriting_ctors
)
1091 /* Inheriting the same constructor along different
1092 paths, combine them. */
1093 SET_DECL_INHERITED_CTOR
1094 (fn
, ovl_make (DECL_INHERITED_CTOR (method
),
1095 DECL_INHERITED_CTOR (fn
)));
1096 /* And discard the new one. */
1100 /* Inherited ctors can coexist until overload
1104 error_at (DECL_SOURCE_LOCATION (method
),
1105 "%q#D conflicts with version inherited from %qT",
1107 inform (DECL_SOURCE_LOCATION (fn
),
1108 "version inherited from %qT declared here",
1111 /* Otherwise defer to the other function. */
1116 /* Defer to the local function. */
1118 else if (flag_new_inheriting_ctors
1119 && DECL_INHERITED_CTOR (fn
))
1121 /* Remove the inherited constructor. */
1122 current_fns
= iter
.remove_node (current_fns
);
1127 error_at (DECL_SOURCE_LOCATION (method
),
1128 "%q#D cannot be overloaded with %q#D", method
, fn
);
1129 inform (DECL_SOURCE_LOCATION (fn
),
1130 "previous declaration %q#D", fn
);
1136 /* A class should never have more than one destructor. */
1137 gcc_assert (!current_fns
|| !DECL_DESTRUCTOR_P (method
));
1139 current_fns
= ovl_insert (method
, current_fns
, via_using
);
1141 if (!COMPLETE_TYPE_P (type
) && !DECL_CONV_FN_P (method
)
1142 && !push_class_level_binding (DECL_NAME (method
), current_fns
))
1146 slot
= add_member_slot (type
, DECL_NAME (method
));
1148 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1149 grok_special_member_properties (method
);
1151 *slot
= current_fns
;
1156 /* Subroutines of finish_struct. */
1158 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1159 legit, otherwise return 0. */
1162 alter_access (tree t
, tree fdecl
, tree access
)
1166 retrofit_lang_decl (fdecl
);
1168 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1170 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1173 if (TREE_VALUE (elem
) != access
)
1175 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1176 error ("conflicting access specifications for method"
1177 " %q+D, ignored", TREE_TYPE (fdecl
));
1179 error ("conflicting access specifications for field %qE, ignored",
1184 /* They're changing the access to the same thing they changed
1185 it to before. That's OK. */
1191 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1192 tf_warning_or_error
);
1193 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1199 /* Return the access node for DECL's access in its enclosing class. */
1202 declared_access (tree decl
)
1204 return (TREE_PRIVATE (decl
) ? access_private_node
1205 : TREE_PROTECTED (decl
) ? access_protected_node
1206 : access_public_node
);
1209 /* Process the USING_DECL, which is a member of T. */
1212 handle_using_decl (tree using_decl
, tree t
)
1214 tree decl
= USING_DECL_DECLS (using_decl
);
1215 tree name
= DECL_NAME (using_decl
);
1216 tree access
= declared_access (using_decl
);
1217 tree flist
= NULL_TREE
;
1220 gcc_assert (!processing_template_decl
&& decl
);
1222 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1223 tf_warning_or_error
);
1226 old_value
= OVL_FIRST (old_value
);
1228 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1231 old_value
= NULL_TREE
;
1234 cp_emit_debug_info_for_using (decl
, t
);
1236 if (is_overloaded_fn (decl
))
1241 else if (is_overloaded_fn (old_value
))
1244 /* It's OK to use functions from a base when there are functions with
1245 the same name already present in the current class. */;
1248 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1249 "because of local method %q#D with same name",
1250 using_decl
, t
, old_value
);
1251 inform (DECL_SOURCE_LOCATION (old_value
),
1252 "local method %q#D declared here", old_value
);
1256 else if (!DECL_ARTIFICIAL (old_value
))
1258 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1259 "because of local member %q#D with same name",
1260 using_decl
, t
, old_value
);
1261 inform (DECL_SOURCE_LOCATION (old_value
),
1262 "local member %q#D declared here", old_value
);
1266 /* Make type T see field decl FDECL with access ACCESS. */
1268 for (ovl_iterator
iter (flist
); iter
; ++iter
)
1270 add_method (t
, *iter
, true);
1271 alter_access (t
, *iter
, access
);
1274 alter_access (t
, decl
, access
);
1277 /* Data structure for find_abi_tags_r, below. */
1281 tree t
; // The type that we're checking for missing tags.
1282 tree subob
; // The subobject of T that we're getting tags from.
1283 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1286 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1287 in the context of P. TAG can be either an identifier (the DECL_NAME of
1288 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1291 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1293 if (!IDENTIFIER_MARKED (id
))
1295 if (p
->tags
!= error_mark_node
)
1297 /* We're collecting tags from template arguments or from
1298 the type of a variable or function return type. */
1299 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1301 /* Don't inherit this tag multiple times. */
1302 IDENTIFIER_MARKED (id
) = true;
1306 /* Tags inherited from type template arguments are only used
1307 to avoid warnings. */
1308 ABI_TAG_IMPLICIT (p
->tags
) = true;
1311 /* For functions and variables we want to warn, too. */
1314 /* Otherwise we're diagnosing missing tags. */
1315 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1317 auto_diagnostic_group d
;
1318 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1319 "that %qT (used in its return type) has",
1321 inform (location_of (*tp
), "%qT declared here", *tp
);
1323 else if (VAR_P (p
->t
))
1325 auto_diagnostic_group d
;
1326 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1327 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1328 inform (location_of (*tp
), "%qT declared here", *tp
);
1330 else if (TYPE_P (p
->subob
))
1332 auto_diagnostic_group d
;
1333 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1334 "that base %qT has", p
->t
, tag
, p
->subob
))
1335 inform (location_of (p
->subob
), "%qT declared here",
1340 auto_diagnostic_group d
;
1341 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1342 "that %qT (used in the type of %qD) has",
1343 p
->t
, tag
, *tp
, p
->subob
))
1345 inform (location_of (p
->subob
), "%qD declared here",
1347 inform (location_of (*tp
), "%qT declared here", *tp
);
1353 /* Find all the ABI tags in the attribute list ATTR and either call
1354 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1357 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1361 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1362 attr
= TREE_CHAIN (attr
))
1363 for (tree list
= TREE_VALUE (attr
); list
;
1364 list
= TREE_CHAIN (list
))
1366 tree tag
= TREE_VALUE (list
);
1367 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1369 check_tag (tag
, id
, tp
, p
);
1371 IDENTIFIER_MARKED (id
) = val
;
1375 /* Find all the ABI tags on T and its enclosing scopes and either call
1376 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1379 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1381 while (t
!= global_namespace
)
1386 attr
= TYPE_ATTRIBUTES (t
);
1387 t
= CP_TYPE_CONTEXT (t
);
1391 attr
= DECL_ATTRIBUTES (t
);
1392 t
= CP_DECL_CONTEXT (t
);
1394 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1398 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1399 types with ABI tags, add the corresponding identifiers to the VEC in
1400 *DATA and set IDENTIFIER_MARKED. */
1403 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1405 if (!OVERLOAD_TYPE_P (*tp
))
1408 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1409 anyway, but let's make sure of it. */
1410 *walk_subtrees
= false;
1412 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1414 mark_or_check_tags (*tp
, tp
, p
, false);
1419 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1420 IDENTIFIER_MARKED on its ABI tags. */
1423 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1425 if (!OVERLOAD_TYPE_P (*tp
))
1428 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1429 anyway, but let's make sure of it. */
1430 *walk_subtrees
= false;
1432 bool *valp
= static_cast<bool*>(data
);
1434 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1439 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1443 mark_abi_tags (tree t
, bool val
)
1445 mark_or_check_tags (t
, NULL
, NULL
, val
);
1448 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1449 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1451 /* Template arguments are part of the signature. */
1452 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1453 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1455 tree arg
= TREE_VEC_ELT (level
, j
);
1456 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1459 if (TREE_CODE (t
) == FUNCTION_DECL
)
1460 /* A function's parameter types are part of the signature, so
1461 we don't need to inherit any tags that are also in them. */
1462 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1463 arg
= TREE_CHAIN (arg
))
1464 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1465 mark_abi_tags_r
, &val
);
1469 /* Check that T has all the ABI tags that subobject SUBOB has, or
1470 warn if not. If T is a (variable or function) declaration, also
1471 return any missing tags, and add them to T if JUST_CHECKING is false. */
1474 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1476 bool inherit
= DECL_P (t
);
1478 if (!inherit
&& !warn_abi_tag
)
1481 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1482 if (!TREE_PUBLIC (decl
))
1483 /* No need to worry about things local to this TU. */
1486 mark_abi_tags (t
, true);
1488 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1489 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1491 data
.tags
= NULL_TREE
;
1493 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1495 if (!(inherit
&& data
.tags
))
1496 /* We don't need to do anything with data.tags. */;
1497 else if (just_checking
)
1498 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1500 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1501 IDENTIFIER_MARKED (id
) = false;
1505 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1507 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1510 = tree_cons (abi_tag_identifier
, data
.tags
, DECL_ATTRIBUTES (t
));
1513 mark_abi_tags (t
, false);
1518 /* Check that DECL has all the ABI tags that are used in parts of its type
1519 that are not reflected in its mangled name. */
1522 check_abi_tags (tree decl
)
1525 check_abi_tags (decl
, TREE_TYPE (decl
));
1526 else if (TREE_CODE (decl
) == FUNCTION_DECL
1527 && !DECL_CONV_FN_P (decl
)
1528 && !mangle_return_type_p (decl
))
1529 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1532 /* Return any ABI tags that are used in parts of the type of DECL
1533 that are not reflected in its mangled name. This function is only
1534 used in backward-compatible mangling for ABI <11. */
1537 missing_abi_tags (tree decl
)
1540 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1541 else if (TREE_CODE (decl
) == FUNCTION_DECL
1542 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1543 that we can use this function for setting need_abi_warning
1544 regardless of the current flag_abi_version. */
1545 && !mangle_return_type_p (decl
))
1546 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1552 inherit_targ_abi_tags (tree t
)
1554 if (!CLASS_TYPE_P (t
)
1555 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1558 mark_abi_tags (t
, true);
1560 tree args
= CLASSTYPE_TI_ARGS (t
);
1561 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1562 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1564 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1565 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1567 tree arg
= TREE_VEC_ELT (level
, j
);
1569 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1573 // If we found some tags on our template arguments, add them to our
1574 // abi_tag attribute.
1577 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1579 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1582 = tree_cons (abi_tag_identifier
, data
.tags
, TYPE_ATTRIBUTES (t
));
1585 mark_abi_tags (t
, false);
1588 /* Return true, iff class T has a non-virtual destructor that is
1589 accessible from outside the class heirarchy (i.e. is public, or
1590 there's a suitable friend. */
1593 accessible_nvdtor_p (tree t
)
1595 tree dtor
= CLASSTYPE_DESTRUCTOR (t
);
1597 /* An implicitly declared destructor is always public. And,
1598 if it were virtual, we would have created it by now. */
1602 if (DECL_VINDEX (dtor
))
1603 return false; /* Virtual */
1605 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1606 return true; /* Public */
1608 if (CLASSTYPE_FRIEND_CLASSES (t
)
1609 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1610 return true; /* Has friends */
1615 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1616 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1617 properties of the bases. */
1620 check_bases (tree t
,
1621 int* cant_have_const_ctor_p
,
1622 int* no_const_asn_ref_p
)
1625 bool seen_non_virtual_nearly_empty_base_p
= 0;
1626 int seen_tm_mask
= 0;
1629 tree field
= NULL_TREE
;
1631 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1632 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1633 if (TREE_CODE (field
) == FIELD_DECL
)
1636 for (binfo
= TYPE_BINFO (t
), i
= 0;
1637 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1639 tree basetype
= TREE_TYPE (base_binfo
);
1641 gcc_assert (COMPLETE_TYPE_P (basetype
));
1643 if (CLASSTYPE_FINAL (basetype
))
1644 error ("cannot derive from %<final%> base %qT in derived type %qT",
1647 /* If any base class is non-literal, so is the derived class. */
1648 if (!CLASSTYPE_LITERAL_P (basetype
))
1649 CLASSTYPE_LITERAL_P (t
) = false;
1651 /* If the base class doesn't have copy constructors or
1652 assignment operators that take const references, then the
1653 derived class cannot have such a member automatically
1655 if (TYPE_HAS_COPY_CTOR (basetype
)
1656 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1657 *cant_have_const_ctor_p
= 1;
1658 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1659 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1660 *no_const_asn_ref_p
= 1;
1662 if (BINFO_VIRTUAL_P (base_binfo
))
1663 /* A virtual base does not effect nearly emptiness. */
1665 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1667 if (seen_non_virtual_nearly_empty_base_p
)
1668 /* And if there is more than one nearly empty base, then the
1669 derived class is not nearly empty either. */
1670 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1672 /* Remember we've seen one. */
1673 seen_non_virtual_nearly_empty_base_p
= 1;
1675 else if (!is_empty_class (basetype
))
1676 /* If the base class is not empty or nearly empty, then this
1677 class cannot be nearly empty. */
1678 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1680 /* A lot of properties from the bases also apply to the derived
1682 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1683 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1684 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1685 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1686 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1687 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1688 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1689 || !TYPE_HAS_COPY_CTOR (basetype
));
1690 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1691 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1692 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1693 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1694 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1695 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1696 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1697 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1698 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1699 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1700 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1701 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1702 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1703 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1704 if (TYPE_HAS_MUTABLE_P (basetype
))
1705 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1707 /* A standard-layout class is a class that:
1709 * has no non-standard-layout base classes, */
1710 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1711 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1714 /* ...has no base classes of the same type as the first non-static
1716 if (field
&& DECL_CONTEXT (field
) == t
1717 && (same_type_ignoring_top_level_qualifiers_p
1718 (TREE_TYPE (field
), basetype
)))
1719 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1721 /* ...either has no non-static data members in the most-derived
1722 class and at most one base class with non-static data
1723 members, or has no base classes with non-static data
1725 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1726 basefield
= DECL_CHAIN (basefield
))
1727 if (TREE_CODE (basefield
) == FIELD_DECL
1728 && !(DECL_FIELD_IS_BASE (basefield
)
1729 && integer_zerop (DECL_SIZE (basefield
))))
1732 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1739 /* Don't bother collecting tm attributes if transactional memory
1740 support is not enabled. */
1743 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1745 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1748 check_abi_tags (t
, basetype
);
1751 /* If one of the base classes had TM attributes, and the current class
1752 doesn't define its own, then the current class inherits one. */
1753 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1755 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1756 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1760 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1761 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1762 that have had a nearly-empty virtual primary base stolen by some
1763 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1767 determine_primary_bases (tree t
)
1770 tree primary
= NULL_TREE
;
1771 tree type_binfo
= TYPE_BINFO (t
);
1774 /* Determine the primary bases of our bases. */
1775 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1776 base_binfo
= TREE_CHAIN (base_binfo
))
1778 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1780 /* See if we're the non-virtual primary of our inheritance
1782 if (!BINFO_VIRTUAL_P (base_binfo
))
1784 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1785 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1788 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1789 BINFO_TYPE (parent_primary
)))
1790 /* We are the primary binfo. */
1791 BINFO_PRIMARY_P (base_binfo
) = 1;
1793 /* Determine if we have a virtual primary base, and mark it so.
1795 if (primary
&& BINFO_VIRTUAL_P (primary
))
1797 tree this_primary
= copied_binfo (primary
, base_binfo
);
1799 if (BINFO_PRIMARY_P (this_primary
))
1800 /* Someone already claimed this base. */
1801 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1806 BINFO_PRIMARY_P (this_primary
) = 1;
1807 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1809 /* A virtual binfo might have been copied from within
1810 another hierarchy. As we're about to use it as a
1811 primary base, make sure the offsets match. */
1812 delta
= size_diffop_loc (input_location
,
1813 fold_convert (ssizetype
,
1814 BINFO_OFFSET (base_binfo
)),
1815 fold_convert (ssizetype
,
1816 BINFO_OFFSET (this_primary
)));
1818 propagate_binfo_offsets (this_primary
, delta
);
1823 /* First look for a dynamic direct non-virtual base. */
1824 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1826 tree basetype
= BINFO_TYPE (base_binfo
);
1828 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1830 primary
= base_binfo
;
1835 /* A "nearly-empty" virtual base class can be the primary base
1836 class, if no non-virtual polymorphic base can be found. Look for
1837 a nearly-empty virtual dynamic base that is not already a primary
1838 base of something in the hierarchy. If there is no such base,
1839 just pick the first nearly-empty virtual base. */
1841 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1842 base_binfo
= TREE_CHAIN (base_binfo
))
1843 if (BINFO_VIRTUAL_P (base_binfo
)
1844 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1846 if (!BINFO_PRIMARY_P (base_binfo
))
1848 /* Found one that is not primary. */
1849 primary
= base_binfo
;
1853 /* Remember the first candidate. */
1854 primary
= base_binfo
;
1858 /* If we've got a primary base, use it. */
1861 tree basetype
= BINFO_TYPE (primary
);
1863 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1864 if (BINFO_PRIMARY_P (primary
))
1865 /* We are stealing a primary base. */
1866 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1867 BINFO_PRIMARY_P (primary
) = 1;
1868 if (BINFO_VIRTUAL_P (primary
))
1872 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1873 /* A virtual binfo might have been copied from within
1874 another hierarchy. As we're about to use it as a primary
1875 base, make sure the offsets match. */
1876 delta
= size_diffop_loc (input_location
, ssize_int (0),
1877 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
1879 propagate_binfo_offsets (primary
, delta
);
1882 primary
= TYPE_BINFO (basetype
);
1884 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1885 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1886 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1890 /* Update the variant types of T. */
1893 fixup_type_variants (tree t
)
1900 for (variants
= TYPE_NEXT_VARIANT (t
);
1902 variants
= TYPE_NEXT_VARIANT (variants
))
1904 /* These fields are in the _TYPE part of the node, not in
1905 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1906 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1907 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1908 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1909 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1911 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1913 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1915 /* Copy whatever these are holding today. */
1916 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1917 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1921 /* KLASS is a class that we're applying may_alias to after the body is
1922 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1923 canonical type(s) will be implicitly updated. */
1926 fixup_may_alias (tree klass
)
1930 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
1931 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1932 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1933 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
1934 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1935 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1938 /* Early variant fixups: we apply attributes at the beginning of the class
1939 definition, and we need to fix up any variants that have already been
1940 made via elaborated-type-specifier so that check_qualified_type works. */
1943 fixup_attribute_variants (tree t
)
1950 tree attrs
= TYPE_ATTRIBUTES (t
);
1951 unsigned align
= TYPE_ALIGN (t
);
1952 bool user_align
= TYPE_USER_ALIGN (t
);
1953 bool may_alias
= lookup_attribute ("may_alias", attrs
);
1954 bool packed
= TYPE_PACKED (t
);
1957 fixup_may_alias (t
);
1959 for (variants
= TYPE_NEXT_VARIANT (t
);
1961 variants
= TYPE_NEXT_VARIANT (variants
))
1963 /* These are the two fields that check_qualified_type looks at and
1964 are affected by attributes. */
1965 TYPE_ATTRIBUTES (variants
) = attrs
;
1966 unsigned valign
= align
;
1967 if (TYPE_USER_ALIGN (variants
))
1968 valign
= MAX (valign
, TYPE_ALIGN (variants
));
1970 TYPE_USER_ALIGN (variants
) = user_align
;
1971 SET_TYPE_ALIGN (variants
, valign
);
1972 TYPE_PACKED (variants
) = packed
;
1974 fixup_may_alias (variants
);
1978 /* Set memoizing fields and bits of T (and its variants) for later
1982 finish_struct_bits (tree t
)
1984 /* Fix up variants (if any). */
1985 fixup_type_variants (t
);
1987 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
1988 /* For a class w/o baseclasses, 'finish_struct' has set
1989 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1990 Similarly for a class whose base classes do not have vtables.
1991 When neither of these is true, we might have removed abstract
1992 virtuals (by providing a definition), added some (by declaring
1993 new ones), or redeclared ones from a base class. We need to
1994 recalculate what's really an abstract virtual at this point (by
1995 looking in the vtables). */
1996 get_pure_virtuals (t
);
1998 /* If this type has a copy constructor or a destructor, force its
1999 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2000 nonzero. This will cause it to be passed by invisible reference
2001 and prevent it from being returned in a register. */
2002 if (type_has_nontrivial_copy_init (t
)
2003 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2006 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2007 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2009 SET_TYPE_MODE (variants
, BLKmode
);
2010 TREE_ADDRESSABLE (variants
) = 1;
2015 /* Issue warnings about T having private constructors, but no friends,
2018 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2019 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2020 non-private static member functions. */
2023 maybe_warn_about_overly_private_class (tree t
)
2025 int has_member_fn
= 0;
2026 int has_nonprivate_method
= 0;
2027 bool nonprivate_ctor
= false;
2029 if (!warn_ctor_dtor_privacy
2030 /* If the class has friends, those entities might create and
2031 access instances, so we should not warn. */
2032 || (CLASSTYPE_FRIEND_CLASSES (t
)
2033 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2034 /* We will have warned when the template was declared; there's
2035 no need to warn on every instantiation. */
2036 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2037 /* There's no reason to even consider warning about this
2041 /* We only issue one warning, if more than one applies, because
2042 otherwise, on code like:
2045 // Oops - forgot `public:'
2051 we warn several times about essentially the same problem. */
2053 /* Check to see if all (non-constructor, non-destructor) member
2054 functions are private. (Since there are no friends or
2055 non-private statics, we can't ever call any of the private member
2057 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2058 if (TREE_CODE (fn
) == USING_DECL
2059 && DECL_NAME (fn
) == ctor_identifier
2060 && !TREE_PRIVATE (fn
))
2061 nonprivate_ctor
= true;
2062 else if (!DECL_DECLARES_FUNCTION_P (fn
))
2063 /* Not a function. */;
2064 else if (DECL_ARTIFICIAL (fn
))
2065 /* We're not interested in compiler-generated methods; they don't
2066 provide any way to call private members. */;
2067 else if (!TREE_PRIVATE (fn
))
2069 if (DECL_STATIC_FUNCTION_P (fn
))
2070 /* A non-private static member function is just like a
2071 friend; it can create and invoke private member
2072 functions, and be accessed without a class
2076 has_nonprivate_method
= 1;
2077 /* Keep searching for a static member function. */
2079 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2082 if (!has_nonprivate_method
&& has_member_fn
)
2084 /* There are no non-private methods, and there's at least one
2085 private member function that isn't a constructor or
2086 destructor. (If all the private members are
2087 constructors/destructors we want to use the code below that
2088 issues error messages specifically referring to
2089 constructors/destructors.) */
2091 tree binfo
= TYPE_BINFO (t
);
2093 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2094 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2096 has_nonprivate_method
= 1;
2099 if (!has_nonprivate_method
)
2101 warning (OPT_Wctor_dtor_privacy
,
2102 "all member functions in class %qT are private", t
);
2107 /* Even if some of the member functions are non-private, the class
2108 won't be useful for much if all the constructors or destructors
2109 are private: such an object can never be created or destroyed. */
2110 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
2111 if (TREE_PRIVATE (dtor
))
2113 warning (OPT_Wctor_dtor_privacy
,
2114 "%q#T only defines a private destructor and has no friends",
2119 /* Warn about classes that have private constructors and no friends. */
2120 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2121 /* Implicitly generated constructors are always public. */
2122 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
2124 tree copy_or_move
= NULL_TREE
;
2126 /* If a non-template class does not define a copy
2127 constructor, one is defined for it, enabling it to avoid
2128 this warning. For a template class, this does not
2129 happen, and so we would normally get a warning on:
2131 template <class T> class C { private: C(); };
2133 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2134 complete non-template or fully instantiated classes have this
2136 if (!TYPE_HAS_COPY_CTOR (t
))
2137 nonprivate_ctor
= true;
2139 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
));
2140 !nonprivate_ctor
&& iter
; ++iter
)
2141 if (TREE_PRIVATE (*iter
))
2143 else if (copy_fn_p (*iter
) || move_fn_p (*iter
))
2144 /* Ideally, we wouldn't count any constructor that takes
2145 an argument of the class type as a parameter, because
2146 such things cannot be used to construct an instance of
2147 the class unless you already have one. */
2148 copy_or_move
= *iter
;
2150 nonprivate_ctor
= true;
2152 if (!nonprivate_ctor
)
2154 warning (OPT_Wctor_dtor_privacy
,
2155 "%q#T only defines private constructors and has no friends",
2158 inform (DECL_SOURCE_LOCATION (copy_or_move
),
2159 "%q#D is public, but requires an existing %q#T object",
2166 /* Make BINFO's vtable have N entries, including RTTI entries,
2167 vbase and vcall offsets, etc. Set its type and call the back end
2171 layout_vtable_decl (tree binfo
, int n
)
2176 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2177 layout_type (atype
);
2179 /* We may have to grow the vtable. */
2180 vtable
= get_vtbl_decl_for_binfo (binfo
);
2181 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2183 TREE_TYPE (vtable
) = atype
;
2184 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2185 layout_decl (vtable
, 0);
2189 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2190 have the same signature. */
2193 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2195 /* One destructor overrides another if they are the same kind of
2197 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2198 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2200 /* But a non-destructor never overrides a destructor, nor vice
2201 versa, nor do different kinds of destructors override
2202 one-another. For example, a complete object destructor does not
2203 override a deleting destructor. */
2204 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2207 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2208 || (DECL_CONV_FN_P (fndecl
)
2209 && DECL_CONV_FN_P (base_fndecl
)
2210 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2211 DECL_CONV_FN_TYPE (base_fndecl
))))
2213 tree fntype
= TREE_TYPE (fndecl
);
2214 tree base_fntype
= TREE_TYPE (base_fndecl
);
2215 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2216 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2217 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2218 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2224 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2228 base_derived_from (tree derived
, tree base
)
2232 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2234 if (probe
== derived
)
2236 else if (BINFO_VIRTUAL_P (probe
))
2237 /* If we meet a virtual base, we can't follow the inheritance
2238 any more. See if the complete type of DERIVED contains
2239 such a virtual base. */
2240 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2246 struct find_final_overrider_data
{
2247 /* The function for which we are trying to find a final overrider. */
2249 /* The base class in which the function was declared. */
2250 tree declaring_base
;
2251 /* The candidate overriders. */
2253 /* Path to most derived. */
2257 /* Add the overrider along the current path to FFOD->CANDIDATES.
2258 Returns true if an overrider was found; false otherwise. */
2261 dfs_find_final_overrider_1 (tree binfo
,
2262 find_final_overrider_data
*ffod
,
2267 /* If BINFO is not the most derived type, try a more derived class.
2268 A definition there will overrider a definition here. */
2272 if (dfs_find_final_overrider_1
2273 (ffod
->path
[depth
], ffod
, depth
))
2277 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2280 tree
*candidate
= &ffod
->candidates
;
2282 /* Remove any candidates overridden by this new function. */
2285 /* If *CANDIDATE overrides METHOD, then METHOD
2286 cannot override anything else on the list. */
2287 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2289 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2290 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2291 *candidate
= TREE_CHAIN (*candidate
);
2293 candidate
= &TREE_CHAIN (*candidate
);
2296 /* Add the new function. */
2297 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2304 /* Called from find_final_overrider via dfs_walk. */
2307 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2309 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2311 if (binfo
== ffod
->declaring_base
)
2312 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2313 ffod
->path
.safe_push (binfo
);
2319 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2321 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2327 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2328 FN and whose TREE_VALUE is the binfo for the base where the
2329 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2330 DERIVED) is the base object in which FN is declared. */
2333 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2335 find_final_overrider_data ffod
;
2337 /* Getting this right is a little tricky. This is valid:
2339 struct S { virtual void f (); };
2340 struct T { virtual void f (); };
2341 struct U : public S, public T { };
2343 even though calling `f' in `U' is ambiguous. But,
2345 struct R { virtual void f(); };
2346 struct S : virtual public R { virtual void f (); };
2347 struct T : virtual public R { virtual void f (); };
2348 struct U : public S, public T { };
2350 is not -- there's no way to decide whether to put `S::f' or
2351 `T::f' in the vtable for `R'.
2353 The solution is to look at all paths to BINFO. If we find
2354 different overriders along any two, then there is a problem. */
2355 if (DECL_THUNK_P (fn
))
2356 fn
= THUNK_TARGET (fn
);
2358 /* Determine the depth of the hierarchy. */
2360 ffod
.declaring_base
= binfo
;
2361 ffod
.candidates
= NULL_TREE
;
2362 ffod
.path
.create (30);
2364 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2365 dfs_find_final_overrider_post
, &ffod
);
2367 ffod
.path
.release ();
2369 /* If there was no winner, issue an error message. */
2370 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2371 return error_mark_node
;
2373 return ffod
.candidates
;
2376 /* Return the index of the vcall offset for FN when TYPE is used as a
2380 get_vcall_index (tree fn
, tree type
)
2382 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2386 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2387 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2388 || same_signature_p (fn
, p
->purpose
))
2391 /* There should always be an appropriate index. */
2395 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2396 dominated by T. FN is the old function; VIRTUALS points to the
2397 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2398 of that entry in the list. */
2401 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2409 tree overrider_fn
, overrider_target
;
2410 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2411 tree over_return
, base_return
;
2414 /* Find the nearest primary base (possibly binfo itself) which defines
2415 this function; this is the class the caller will convert to when
2416 calling FN through BINFO. */
2417 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2420 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2423 /* The nearest definition is from a lost primary. */
2424 if (BINFO_LOST_PRIMARY_P (b
))
2429 /* Find the final overrider. */
2430 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2431 if (overrider
== error_mark_node
)
2433 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2436 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2438 /* Check for adjusting covariant return types. */
2439 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2440 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2442 if (INDIRECT_TYPE_P (over_return
)
2443 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2444 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2445 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2446 /* If the overrider is invalid, don't even try. */
2447 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2449 /* If FN is a covariant thunk, we must figure out the adjustment
2450 to the final base FN was converting to. As OVERRIDER_TARGET might
2451 also be converting to the return type of FN, we have to
2452 combine the two conversions here. */
2453 tree fixed_offset
, virtual_offset
;
2455 over_return
= TREE_TYPE (over_return
);
2456 base_return
= TREE_TYPE (base_return
);
2458 if (DECL_THUNK_P (fn
))
2460 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2461 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2462 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2465 fixed_offset
= virtual_offset
= NULL_TREE
;
2468 /* Find the equivalent binfo within the return type of the
2469 overriding function. We will want the vbase offset from
2471 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2473 else if (!same_type_ignoring_top_level_qualifiers_p
2474 (over_return
, base_return
))
2476 /* There was no existing virtual thunk (which takes
2477 precedence). So find the binfo of the base function's
2478 return type within the overriding function's return type.
2479 Fortunately we know the covariancy is valid (it
2480 has already been checked), so we can just iterate along
2481 the binfos, which have been chained in inheritance graph
2482 order. Of course it is lame that we have to repeat the
2483 search here anyway -- we should really be caching pieces
2484 of the vtable and avoiding this repeated work. */
2485 tree thunk_binfo
= NULL_TREE
;
2486 tree base_binfo
= TYPE_BINFO (base_return
);
2488 /* Find the base binfo within the overriding function's
2489 return type. We will always find a thunk_binfo, except
2490 when the covariancy is invalid (which we will have
2491 already diagnosed). */
2493 for (thunk_binfo
= TYPE_BINFO (over_return
); thunk_binfo
;
2494 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2495 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2496 BINFO_TYPE (base_binfo
)))
2498 gcc_assert (thunk_binfo
|| errorcount
);
2500 /* See if virtual inheritance is involved. */
2501 for (virtual_offset
= thunk_binfo
;
2503 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2504 if (BINFO_VIRTUAL_P (virtual_offset
))
2508 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2510 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2514 /* We convert via virtual base. Adjust the fixed
2515 offset to be from there. */
2517 size_diffop (offset
,
2518 fold_convert (ssizetype
,
2519 BINFO_OFFSET (virtual_offset
)));
2522 /* There was an existing fixed offset, this must be
2523 from the base just converted to, and the base the
2524 FN was thunking to. */
2525 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2527 fixed_offset
= offset
;
2531 if (fixed_offset
|| virtual_offset
)
2532 /* Replace the overriding function with a covariant thunk. We
2533 will emit the overriding function in its own slot as
2535 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2536 fixed_offset
, virtual_offset
);
2539 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2540 !DECL_THUNK_P (fn
));
2542 /* If we need a covariant thunk, then we may need to adjust first_defn.
2543 The ABI specifies that the thunks emitted with a function are
2544 determined by which bases the function overrides, so we need to be
2545 sure that we're using a thunk for some overridden base; even if we
2546 know that the necessary this adjustment is zero, there may not be an
2547 appropriate zero-this-adjustment thunk for us to use since thunks for
2548 overriding virtual bases always use the vcall offset.
2550 Furthermore, just choosing any base that overrides this function isn't
2551 quite right, as this slot won't be used for calls through a type that
2552 puts a covariant thunk here. Calling the function through such a type
2553 will use a different slot, and that slot is the one that determines
2554 the thunk emitted for that base.
2556 So, keep looking until we find the base that we're really overriding
2557 in this slot: the nearest primary base that doesn't use a covariant
2558 thunk in this slot. */
2559 if (overrider_target
!= overrider_fn
)
2561 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2562 /* We already know that the overrider needs a covariant thunk. */
2563 b
= get_primary_binfo (b
);
2564 for (; ; b
= get_primary_binfo (b
))
2566 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2567 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2568 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2570 if (BINFO_LOST_PRIMARY_P (b
))
2576 /* Assume that we will produce a thunk that convert all the way to
2577 the final overrider, and not to an intermediate virtual base. */
2578 virtual_base
= NULL_TREE
;
2580 /* See if we can convert to an intermediate virtual base first, and then
2581 use the vcall offset located there to finish the conversion. */
2582 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2584 /* If we find the final overrider, then we can stop
2586 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2587 BINFO_TYPE (TREE_VALUE (overrider
))))
2590 /* If we find a virtual base, and we haven't yet found the
2591 overrider, then there is a virtual base between the
2592 declaring base (first_defn) and the final overrider. */
2593 if (BINFO_VIRTUAL_P (b
))
2600 /* Compute the constant adjustment to the `this' pointer. The
2601 `this' pointer, when this function is called, will point at BINFO
2602 (or one of its primary bases, which are at the same offset). */
2604 /* The `this' pointer needs to be adjusted from the declaration to
2605 the nearest virtual base. */
2606 delta
= size_diffop_loc (input_location
,
2607 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2608 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2610 /* If the nearest definition is in a lost primary, we don't need an
2611 entry in our vtable. Except possibly in a constructor vtable,
2612 if we happen to get our primary back. In that case, the offset
2613 will be zero, as it will be a primary base. */
2614 delta
= size_zero_node
;
2616 /* The `this' pointer needs to be adjusted from pointing to
2617 BINFO to pointing at the base where the final overrider
2619 delta
= size_diffop_loc (input_location
,
2620 fold_convert (ssizetype
,
2621 BINFO_OFFSET (TREE_VALUE (overrider
))),
2622 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2624 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2627 BV_VCALL_INDEX (*virtuals
)
2628 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2630 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2632 BV_LOST_PRIMARY (*virtuals
) = lost
;
2635 /* Called from modify_all_vtables via dfs_walk. */
2638 dfs_modify_vtables (tree binfo
, void* data
)
2640 tree t
= (tree
) data
;
2645 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2646 /* A base without a vtable needs no modification, and its bases
2647 are uninteresting. */
2648 return dfs_skip_bases
;
2650 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2651 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2652 /* Don't do the primary vtable, if it's new. */
2655 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2656 /* There's no need to modify the vtable for a non-virtual primary
2657 base; we're not going to use that vtable anyhow. We do still
2658 need to do this for virtual primary bases, as they could become
2659 non-primary in a construction vtable. */
2662 make_new_vtable (t
, binfo
);
2664 /* Now, go through each of the virtual functions in the virtual
2665 function table for BINFO. Find the final overrider, and update
2666 the BINFO_VIRTUALS list appropriately. */
2667 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2668 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2670 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2671 old_virtuals
= TREE_CHAIN (old_virtuals
))
2672 update_vtable_entry_for_fn (t
,
2674 BV_FN (old_virtuals
),
2680 /* Update all of the primary and secondary vtables for T. Create new
2681 vtables as required, and initialize their RTTI information. Each
2682 of the functions in VIRTUALS is declared in T and may override a
2683 virtual function from a base class; find and modify the appropriate
2684 entries to point to the overriding functions. Returns a list, in
2685 declaration order, of the virtual functions that are declared in T,
2686 but do not appear in the primary base class vtable, and which
2687 should therefore be appended to the end of the vtable for T. */
2690 modify_all_vtables (tree t
, tree virtuals
)
2692 tree binfo
= TYPE_BINFO (t
);
2695 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2696 if (TYPE_CONTAINS_VPTR_P (t
))
2697 get_vtable_decl (t
, false);
2699 /* Update all of the vtables. */
2700 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2702 /* Add virtual functions not already in our primary vtable. These
2703 will be both those introduced by this class, and those overridden
2704 from secondary bases. It does not include virtuals merely
2705 inherited from secondary bases. */
2706 for (fnsp
= &virtuals
; *fnsp
; )
2708 tree fn
= TREE_VALUE (*fnsp
);
2710 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2711 || DECL_VINDEX (fn
) == error_mark_node
)
2713 /* We don't need to adjust the `this' pointer when
2714 calling this function. */
2715 BV_DELTA (*fnsp
) = integer_zero_node
;
2716 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2718 /* This is a function not already in our vtable. Keep it. */
2719 fnsp
= &TREE_CHAIN (*fnsp
);
2722 /* We've already got an entry for this function. Skip it. */
2723 *fnsp
= TREE_CHAIN (*fnsp
);
2729 /* Get the base virtual function declarations in T that have the
2733 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2735 bool found_decls
= false;
2737 /* Find virtual functions in T with the indicated NAME. */
2738 for (ovl_iterator
iter (get_class_binding (t
, name
)); iter
; ++iter
)
2740 tree method
= *iter
;
2742 if (TREE_CODE (method
) == FUNCTION_DECL
&& DECL_VINDEX (method
))
2744 base_fndecls
->safe_push (method
);
2752 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2753 for (int i
= 0; i
< n_baseclasses
; i
++)
2755 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2756 get_basefndecls (name
, basetype
, base_fndecls
);
2760 /* If this declaration supersedes the declaration of
2761 a method declared virtual in the base class, then
2762 mark this field as being virtual as well. */
2765 check_for_override (tree decl
, tree ctype
)
2767 bool overrides_found
= false;
2768 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2769 /* In [temp.mem] we have:
2771 A specialization of a member function template does not
2772 override a virtual function from a base class. */
2774 if ((DECL_DESTRUCTOR_P (decl
)
2775 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2776 || DECL_CONV_FN_P (decl
))
2777 && look_for_overrides (ctype
, decl
)
2778 && !DECL_STATIC_FUNCTION_P (decl
))
2779 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2780 the error_mark_node so that we know it is an overriding
2783 DECL_VINDEX (decl
) = decl
;
2784 overrides_found
= true;
2785 if (warn_override
&& !DECL_OVERRIDE_P (decl
)
2786 && !DECL_DESTRUCTOR_P (decl
))
2787 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2788 "%qD can be marked override", decl
);
2791 if (DECL_VIRTUAL_P (decl
))
2793 if (!DECL_VINDEX (decl
))
2794 DECL_VINDEX (decl
) = error_mark_node
;
2795 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2796 if (DECL_DESTRUCTOR_P (decl
))
2797 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2799 else if (DECL_FINAL_P (decl
))
2800 error ("%q+#D marked %<final%>, but is not virtual", decl
);
2801 if (DECL_OVERRIDE_P (decl
) && !overrides_found
)
2802 error ("%q+#D marked %<override%>, but does not override", decl
);
2805 /* Warn about hidden virtual functions that are not overridden in t.
2806 We know that constructors and destructors don't apply. */
2809 warn_hidden (tree t
)
2811 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (t
))
2812 for (unsigned ix
= member_vec
->length (); ix
--;)
2814 tree fns
= (*member_vec
)[ix
];
2819 tree name
= OVL_NAME (fns
);
2820 auto_vec
<tree
, 20> base_fndecls
;
2825 /* Iterate through all of the base classes looking for possibly
2826 hidden functions. */
2827 for (binfo
= TYPE_BINFO (t
), j
= 0;
2828 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2830 tree basetype
= BINFO_TYPE (base_binfo
);
2831 get_basefndecls (name
, basetype
, &base_fndecls
);
2834 /* If there are no functions to hide, continue. */
2835 if (base_fndecls
.is_empty ())
2838 /* Remove any overridden functions. */
2839 for (ovl_iterator
iter (fns
); iter
; ++iter
)
2841 tree fndecl
= *iter
;
2842 if (TREE_CODE (fndecl
) == FUNCTION_DECL
2843 && DECL_VINDEX (fndecl
))
2845 /* If the method from the base class has the same
2846 signature as the method from the derived class, it
2847 has been overridden. */
2848 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
2850 && same_signature_p (fndecl
, base_fndecls
[k
]))
2851 base_fndecls
[k
] = NULL_TREE
;
2855 /* Now give a warning for all base functions without overriders,
2856 as they are hidden. */
2858 FOR_EACH_VEC_ELT (base_fndecls
, j
, base_fndecl
)
2861 /* Here we know it is a hider, and no overrider exists. */
2862 warning_at (location_of (base_fndecl
),
2863 OPT_Woverloaded_virtual
,
2864 "%qD was hidden", base_fndecl
);
2865 warning_at (location_of (fns
),
2866 OPT_Woverloaded_virtual
, " by %qD", fns
);
2871 /* Recursive helper for finish_struct_anon. */
2874 finish_struct_anon_r (tree field
, bool complain
)
2876 for (tree elt
= TYPE_FIELDS (TREE_TYPE (field
)); elt
; elt
= DECL_CHAIN (elt
))
2878 /* We're generally only interested in entities the user
2879 declared, but we also find nested classes by noticing
2880 the TYPE_DECL that we create implicitly. You're
2881 allowed to put one anonymous union inside another,
2882 though, so we explicitly tolerate that. We use
2883 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2884 we also allow unnamed types used for defining fields. */
2885 if (DECL_ARTIFICIAL (elt
)
2886 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2887 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
2891 && (TREE_CODE (elt
) != FIELD_DECL
2892 || (TREE_PRIVATE (elt
) || TREE_PROTECTED (elt
))))
2894 /* We already complained about static data members in
2895 finish_static_data_member_decl. */
2898 auto_diagnostic_group d
;
2899 if (permerror (DECL_SOURCE_LOCATION (elt
),
2900 TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
2901 ? "%q#D invalid; an anonymous union may "
2902 "only have public non-static data members"
2903 : "%q#D invalid; an anonymous struct may "
2904 "only have public non-static data members", elt
))
2907 if (flag_permissive
&& !hint
)
2910 inform (DECL_SOURCE_LOCATION (elt
),
2911 "this flexibility is deprecated and will be "
2918 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
2919 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
2921 /* Recurse into the anonymous aggregates to correctly handle
2922 access control (c++/24926):
2933 if (DECL_NAME (elt
) == NULL_TREE
2934 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
2935 finish_struct_anon_r (elt
, /*complain=*/false);
2939 /* Check for things that are invalid. There are probably plenty of other
2940 things we should check for also. */
2943 finish_struct_anon (tree t
)
2945 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2947 if (TREE_STATIC (field
))
2949 if (TREE_CODE (field
) != FIELD_DECL
)
2952 if (DECL_NAME (field
) == NULL_TREE
2953 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2954 finish_struct_anon_r (field
, /*complain=*/true);
2958 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2959 will be used later during class template instantiation.
2960 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2961 a non-static member data (FIELD_DECL), a member function
2962 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2963 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2964 When FRIEND_P is nonzero, T is either a friend class
2965 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2966 (FUNCTION_DECL, TEMPLATE_DECL). */
2969 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
2971 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2972 if (CLASSTYPE_TEMPLATE_INFO (type
))
2973 CLASSTYPE_DECL_LIST (type
)
2974 = tree_cons (friend_p
? NULL_TREE
: type
,
2975 t
, CLASSTYPE_DECL_LIST (type
));
2978 /* This function is called from declare_virt_assop_and_dtor via
2981 DATA is a type that direcly or indirectly inherits the base
2982 represented by BINFO. If BINFO contains a virtual assignment [copy
2983 assignment or move assigment] operator or a virtual constructor,
2984 declare that function in DATA if it hasn't been already declared. */
2987 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
2989 tree bv
, fn
, t
= (tree
)data
;
2990 tree opname
= assign_op_identifier
;
2992 gcc_assert (t
&& CLASS_TYPE_P (t
));
2993 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
2995 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2996 /* A base without a vtable needs no modification, and its bases
2997 are uninteresting. */
2998 return dfs_skip_bases
;
3000 if (BINFO_PRIMARY_P (binfo
))
3001 /* If this is a primary base, then we have already looked at the
3002 virtual functions of its vtable. */
3005 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3009 if (DECL_NAME (fn
) == opname
)
3011 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3012 lazily_declare_fn (sfk_copy_assignment
, t
);
3013 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3014 lazily_declare_fn (sfk_move_assignment
, t
);
3016 else if (DECL_DESTRUCTOR_P (fn
)
3017 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3018 lazily_declare_fn (sfk_destructor
, t
);
3024 /* If the class type T has a direct or indirect base that contains a
3025 virtual assignment operator or a virtual destructor, declare that
3026 function in T if it hasn't been already declared. */
3029 declare_virt_assop_and_dtor (tree t
)
3031 if (!(TYPE_POLYMORPHIC_P (t
)
3032 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3033 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3034 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3037 dfs_walk_all (TYPE_BINFO (t
),
3038 dfs_declare_virt_assop_and_dtor
,
3042 /* Declare the inheriting constructor for class T inherited from base
3043 constructor CTOR with the parameter array PARMS of size NPARMS. */
3046 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3048 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3050 /* We don't declare an inheriting ctor that would be a default,
3051 copy or move ctor for derived or base. */
3055 && TYPE_REF_P (parms
[0]))
3057 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3058 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3062 tree parmlist
= void_list_node
;
3063 for (int i
= nparms
- 1; i
>= 0; i
--)
3064 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3065 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3066 t
, false, ctor
, parmlist
);
3068 if (add_method (t
, fn
, false))
3070 DECL_CHAIN (fn
) = TYPE_FIELDS (t
);
3071 TYPE_FIELDS (t
) = fn
;
3075 /* Declare all the inheriting constructors for class T inherited from base
3076 constructor CTOR. */
3079 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3081 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3083 if (flag_new_inheriting_ctors
)
3085 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3086 t
, /*const*/false, ctor
, parms
);
3087 add_method (t
, ctor
, using_decl
!= NULL_TREE
);
3088 TYPE_HAS_USER_CONSTRUCTOR (t
) = true;
3092 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3094 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3096 if (TREE_PURPOSE (parms
))
3097 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3098 new_parms
[i
++] = TREE_VALUE (parms
);
3100 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3101 if (parms
== NULL_TREE
)
3103 auto_diagnostic_group d
;
3104 if (warning (OPT_Winherited_variadic_ctor
,
3105 "the ellipsis in %qD is not inherited", ctor
))
3106 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3110 /* Create default constructors, assignment operators, and so forth for
3111 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3112 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3113 the class cannot have a default constructor, copy constructor
3114 taking a const reference argument, or an assignment operator taking
3115 a const reference, respectively. */
3118 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3119 int cant_have_const_cctor
,
3120 int cant_have_const_assignment
)
3123 if (!CLASSTYPE_DESTRUCTOR (t
))
3124 /* In general, we create destructors lazily. */
3125 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3127 bool move_ok
= false;
3128 if (cxx_dialect
>= cxx11
&& CLASSTYPE_LAZY_DESTRUCTOR (t
)
3129 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3130 && !classtype_has_move_assign_or_move_ctor_p (t
, false))
3135 If there is no user-declared constructor for a class, a default
3136 constructor is implicitly declared. */
3137 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3139 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3140 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3141 if (cxx_dialect
>= cxx11
)
3142 TYPE_HAS_CONSTEXPR_CTOR (t
)
3143 /* Don't force the declaration to get a hard answer; if the
3144 definition would have made the class non-literal, it will still be
3145 non-literal because of the base or member in question, and that
3146 gives a better diagnostic. */
3147 = type_maybe_constexpr_default_constructor (t
);
3152 If a class definition does not explicitly declare a copy
3153 constructor, one is declared implicitly. */
3154 if (! TYPE_HAS_COPY_CTOR (t
))
3156 TYPE_HAS_COPY_CTOR (t
) = 1;
3157 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3158 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3160 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3163 /* If there is no assignment operator, one will be created if and
3164 when it is needed. For now, just record whether or not the type
3165 of the parameter to the assignment operator will be a const or
3166 non-const reference. */
3167 if (!TYPE_HAS_COPY_ASSIGN (t
))
3169 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3170 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3171 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3172 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3173 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3176 /* We can't be lazy about declaring functions that might override
3177 a virtual function from a base class. */
3178 declare_virt_assop_and_dtor (t
);
3180 while (*access_decls
)
3182 tree using_decl
= TREE_VALUE (*access_decls
);
3183 tree decl
= USING_DECL_DECLS (using_decl
);
3184 if (DECL_NAME (using_decl
) == ctor_identifier
)
3186 /* declare, then remove the decl */
3187 tree ctor_list
= decl
;
3188 location_t loc
= input_location
;
3189 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3190 for (ovl_iterator
iter (ctor_list
); iter
; ++iter
)
3191 one_inherited_ctor (*iter
, t
, using_decl
);
3192 *access_decls
= TREE_CHAIN (*access_decls
);
3193 input_location
= loc
;
3196 access_decls
= &TREE_CHAIN (*access_decls
);
3200 /* FIELD is a bit-field. We are finishing the processing for its
3201 enclosing type. Issue any appropriate messages and set appropriate
3202 flags. Returns false if an error has been diagnosed. */
3205 check_bitfield_decl (tree field
)
3207 tree type
= TREE_TYPE (field
);
3210 /* Extract the declared width of the bitfield, which has been
3211 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3212 w
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
3213 gcc_assert (w
!= NULL_TREE
);
3214 /* Remove the bit-field width indicator so that the rest of the
3215 compiler does not treat that value as a qualifier. */
3216 DECL_BIT_FIELD_REPRESENTATIVE (field
) = NULL_TREE
;
3218 /* Detect invalid bit-field type. */
3219 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3221 error_at (DECL_SOURCE_LOCATION (field
),
3222 "bit-field %q#D with non-integral type %qT", field
, type
);
3223 w
= error_mark_node
;
3227 location_t loc
= input_location
;
3228 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3231 /* detect invalid field size. */
3232 input_location
= DECL_SOURCE_LOCATION (field
);
3233 w
= cxx_constant_value (w
);
3234 input_location
= loc
;
3236 if (TREE_CODE (w
) != INTEGER_CST
)
3238 error ("bit-field %q+D width not an integer constant", field
);
3239 w
= error_mark_node
;
3241 else if (tree_int_cst_sgn (w
) < 0)
3243 error ("negative width in bit-field %q+D", field
);
3244 w
= error_mark_node
;
3246 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3248 error ("zero width for bit-field %q+D", field
);
3249 w
= error_mark_node
;
3251 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3252 && TREE_CODE (type
) != BOOLEAN_TYPE
3253 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3254 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3255 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3256 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3257 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3258 "width of %qD exceeds its type", field
);
3259 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3261 int prec
= TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
));
3262 if (compare_tree_int (w
, prec
) < 0)
3263 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3264 "%qD is too small to hold all values of %q#T",
3269 if (w
!= error_mark_node
)
3271 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3272 DECL_BIT_FIELD (field
) = 1;
3277 /* Non-bit-fields are aligned for their type. */
3278 DECL_BIT_FIELD (field
) = 0;
3279 CLEAR_DECL_C_BIT_FIELD (field
);
3284 /* FIELD is a non bit-field. We are finishing the processing for its
3285 enclosing type T. Issue any appropriate messages and set appropriate
3289 check_field_decl (tree field
,
3291 int* cant_have_const_ctor
,
3292 int* no_const_asn_ref
)
3294 tree type
= strip_array_types (TREE_TYPE (field
));
3295 bool any_default_members
= false;
3297 /* In C++98 an anonymous union cannot contain any fields which would change
3298 the settings of CANT_HAVE_CONST_CTOR and friends. */
3299 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3301 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3302 structs. So, we recurse through their fields here. */
3303 else if (ANON_AGGR_TYPE_P (type
))
3305 for (tree fields
= TYPE_FIELDS (type
); fields
;
3306 fields
= DECL_CHAIN (fields
))
3307 if (TREE_CODE (fields
) == FIELD_DECL
)
3308 any_default_members
|= check_field_decl (fields
, t
,
3309 cant_have_const_ctor
,
3312 /* Check members with class type for constructors, destructors,
3314 else if (CLASS_TYPE_P (type
))
3316 /* Never let anything with uninheritable virtuals
3317 make it through without complaint. */
3318 abstract_virtuals_error (field
, type
);
3320 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3323 int oldcount
= errorcount
;
3324 if (TYPE_NEEDS_CONSTRUCTING (type
))
3325 error ("member %q+#D with constructor not allowed in union",
3327 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3328 error ("member %q+#D with destructor not allowed in union", field
);
3329 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3330 error ("member %q+#D with copy assignment operator not allowed in union",
3332 if (!warned
&& errorcount
> oldcount
)
3334 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3335 "only available with -std=c++11 or -std=gnu++11");
3341 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3342 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3343 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3344 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3345 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3346 || !TYPE_HAS_COPY_ASSIGN (type
));
3347 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3348 || !TYPE_HAS_COPY_CTOR (type
));
3349 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3350 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3351 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3352 || TYPE_HAS_COMPLEX_DFLT (type
));
3355 if (TYPE_HAS_COPY_CTOR (type
)
3356 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3357 *cant_have_const_ctor
= 1;
3359 if (TYPE_HAS_COPY_ASSIGN (type
)
3360 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3361 *no_const_asn_ref
= 1;
3364 check_abi_tags (t
, field
);
3366 if (DECL_INITIAL (field
) != NULL_TREE
)
3367 /* `build_class_init_list' does not recognize
3369 any_default_members
= true;
3371 return any_default_members
;
3374 /* Check the data members (both static and non-static), class-scoped
3375 typedefs, etc., appearing in the declaration of T. Issue
3376 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3377 declaration order) of access declarations; each TREE_VALUE in this
3378 list is a USING_DECL.
3380 In addition, set the following flags:
3383 The class is empty, i.e., contains no non-static data members.
3385 CANT_HAVE_CONST_CTOR_P
3386 This class cannot have an implicitly generated copy constructor
3387 taking a const reference.
3389 CANT_HAVE_CONST_ASN_REF
3390 This class cannot have an implicitly generated assignment
3391 operator taking a const reference.
3393 All of these flags should be initialized before calling this
3396 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3397 fields can be added by adding to this chain. */
3400 check_field_decls (tree t
, tree
*access_decls
,
3401 int *cant_have_const_ctor_p
,
3402 int *no_const_asn_ref_p
)
3407 bool any_default_members
;
3409 int field_access
= -1;
3411 /* Assume there are no access declarations. */
3412 *access_decls
= NULL_TREE
;
3413 /* Assume this class has no pointer members. */
3414 has_pointers
= false;
3415 /* Assume none of the members of this class have default
3417 any_default_members
= false;
3419 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3422 tree type
= TREE_TYPE (x
);
3423 int this_field_access
;
3425 next
= &DECL_CHAIN (x
);
3427 if (TREE_CODE (x
) == USING_DECL
)
3429 /* Save the access declarations for our caller. */
3430 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3434 if (TREE_CODE (x
) == TYPE_DECL
3435 || TREE_CODE (x
) == TEMPLATE_DECL
)
3438 if (TREE_CODE (x
) == FUNCTION_DECL
)
3439 /* FIXME: We should fold in the checking from check_methods. */
3442 /* If we've gotten this far, it's a data member, possibly static,
3443 or an enumerator. */
3444 if (TREE_CODE (x
) != CONST_DECL
)
3445 DECL_CONTEXT (x
) = t
;
3447 /* When this goes into scope, it will be a non-local reference. */
3448 DECL_NONLOCAL (x
) = 1;
3450 if (TREE_CODE (t
) == UNION_TYPE
)
3452 /* [class.union] (C++98)
3454 If a union contains a static data member, or a member of
3455 reference type, the program is ill-formed.
3457 In C++11 [class.union] says:
3458 If a union contains a non-static data member of reference type
3459 the program is ill-formed. */
3460 if (VAR_P (x
) && cxx_dialect
< cxx11
)
3462 error ("in C++98 %q+D may not be static because it is "
3463 "a member of a union", x
);
3466 if (TYPE_REF_P (type
)
3467 && TREE_CODE (x
) == FIELD_DECL
)
3469 error ("non-static data member %q+D in a union may not "
3470 "have reference type %qT", x
, type
);
3475 /* Perform error checking that did not get done in
3477 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3479 error ("field %q+D invalidly declared function type", x
);
3480 type
= build_pointer_type (type
);
3481 TREE_TYPE (x
) = type
;
3483 else if (TREE_CODE (type
) == METHOD_TYPE
)
3485 error ("field %q+D invalidly declared method type", x
);
3486 type
= build_pointer_type (type
);
3487 TREE_TYPE (x
) = type
;
3490 if (type
== error_mark_node
)
3493 if (TREE_CODE (x
) == CONST_DECL
|| VAR_P (x
))
3496 /* Now it can only be a FIELD_DECL. */
3498 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3499 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3501 /* If at least one non-static data member is non-literal, the whole
3502 class becomes non-literal. Per Core/1453, volatile non-static
3503 data members and base classes are also not allowed.
3504 Note: if the type is incomplete we will complain later on. */
3505 if (COMPLETE_TYPE_P (type
)
3506 && (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
)))
3507 CLASSTYPE_LITERAL_P (t
) = false;
3509 /* A standard-layout class is a class that:
3511 has the same access control (Clause 11) for all non-static data members,
3513 this_field_access
= TREE_PROTECTED (x
) ? 1 : TREE_PRIVATE (x
) ? 2 : 0;
3514 if (field_access
== -1)
3515 field_access
= this_field_access
;
3516 else if (this_field_access
!= field_access
)
3517 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3519 /* If this is of reference type, check if it needs an init. */
3520 if (TYPE_REF_P (type
))
3522 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3523 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3524 if (DECL_INITIAL (x
) == NULL_TREE
)
3525 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3526 if (cxx_dialect
< cxx11
)
3528 /* ARM $12.6.2: [A member initializer list] (or, for an
3529 aggregate, initialization by a brace-enclosed list) is the
3530 only way to initialize nonstatic const and reference
3532 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3533 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3537 type
= strip_array_types (type
);
3539 if (TYPE_PACKED (t
))
3541 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3544 (DECL_SOURCE_LOCATION (x
), 0,
3545 "ignoring packed attribute because of unpacked non-POD field %q#D",
3549 else if (DECL_C_BIT_FIELD (x
)
3550 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
)
3551 DECL_PACKED (x
) = 1;
3554 if (DECL_C_BIT_FIELD (x
)
3555 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x
)))
3556 /* We don't treat zero-width bitfields as making a class
3561 /* The class is non-empty. */
3562 CLASSTYPE_EMPTY_P (t
) = 0;
3563 /* The class is not even nearly empty. */
3564 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3565 /* If one of the data members contains an empty class,
3567 if (CLASS_TYPE_P (type
)
3568 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3569 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3572 /* This is used by -Weffc++ (see below). Warn only for pointers
3573 to members which might hold dynamic memory. So do not warn
3574 for pointers to functions or pointers to members. */
3575 if (TYPE_PTR_P (type
)
3576 && !TYPE_PTRFN_P (type
))
3577 has_pointers
= true;
3579 if (CLASS_TYPE_P (type
))
3581 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3582 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3583 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3584 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3587 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3588 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3590 if (DECL_MUTABLE_P (x
))
3592 if (CP_TYPE_CONST_P (type
))
3594 error ("member %q+D cannot be declared both %<const%> "
3595 "and %<mutable%>", x
);
3598 if (TYPE_REF_P (type
))
3600 error ("member %q+D cannot be declared as a %<mutable%> "
3606 if (! layout_pod_type_p (type
))
3607 /* DR 148 now allows pointers to members (which are POD themselves),
3608 to be allowed in POD structs. */
3609 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3611 if (!std_layout_type_p (type
))
3612 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3614 if (! zero_init_p (type
))
3615 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3617 /* We set DECL_C_BIT_FIELD in grokbitfield.
3618 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3619 if (DECL_C_BIT_FIELD (x
))
3620 check_bitfield_decl (x
);
3622 if (check_field_decl (x
, t
, cant_have_const_ctor_p
, no_const_asn_ref_p
))
3624 if (any_default_members
3625 && TREE_CODE (t
) == UNION_TYPE
)
3626 error ("multiple fields in union %qT initialized", t
);
3627 any_default_members
= true;
3630 /* Now that we've removed bit-field widths from DECL_INITIAL,
3631 anything left in DECL_INITIAL is an NSDMI that makes the class
3632 non-aggregate in C++11. */
3633 if (DECL_INITIAL (x
) && cxx_dialect
< cxx14
)
3634 CLASSTYPE_NON_AGGREGATE (t
) = true;
3636 /* If any field is const, the structure type is pseudo-const. */
3637 if (CP_TYPE_CONST_P (type
))
3639 C_TYPE_FIELDS_READONLY (t
) = 1;
3640 if (DECL_INITIAL (x
) == NULL_TREE
)
3641 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3642 if (cxx_dialect
< cxx11
)
3644 /* ARM $12.6.2: [A member initializer list] (or, for an
3645 aggregate, initialization by a brace-enclosed list) is the
3646 only way to initialize nonstatic const and reference
3648 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3649 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3652 /* A field that is pseudo-const makes the structure likewise. */
3653 else if (CLASS_TYPE_P (type
))
3655 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3656 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3657 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3658 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3661 /* Core issue 80: A nonstatic data member is required to have a
3662 different name from the class iff the class has a
3663 user-declared constructor. */
3664 if (constructor_name_p (DECL_NAME (x
), t
)
3665 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3666 permerror (DECL_SOURCE_LOCATION (x
),
3667 "field %q#D with same name as class", x
);
3670 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3671 it should also define a copy constructor and an assignment operator to
3672 implement the correct copy semantic (deep vs shallow, etc.). As it is
3673 not feasible to check whether the constructors do allocate dynamic memory
3674 and store it within members, we approximate the warning like this:
3676 -- Warn only if there are members which are pointers
3677 -- Warn only if there is a non-trivial constructor (otherwise,
3678 there cannot be memory allocated).
3679 -- Warn only if there is a non-trivial destructor. We assume that the
3680 user at least implemented the cleanup correctly, and a destructor
3681 is needed to free dynamic memory.
3683 This seems enough for practical purposes. */
3686 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3687 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3688 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3690 warning (OPT_Weffc__
, "%q#T has pointer data members", t
);
3692 if (! TYPE_HAS_COPY_CTOR (t
))
3694 warning (OPT_Weffc__
,
3695 " but does not override %<%T(const %T&)%>", t
, t
);
3696 if (!TYPE_HAS_COPY_ASSIGN (t
))
3697 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3699 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3700 warning (OPT_Weffc__
,
3701 " but does not override %<operator=(const %T&)%>", t
);
3704 /* Non-static data member initializers make the default constructor
3706 if (any_default_members
)
3708 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3709 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3712 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3714 TYPE_PACKED (t
) = 0;
3716 /* Check anonymous struct/anonymous union fields. */
3717 finish_struct_anon (t
);
3719 /* We've built up the list of access declarations in reverse order.
3721 *access_decls
= nreverse (*access_decls
);
3724 /* If TYPE is an empty class type, records its OFFSET in the table of
3728 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3732 if (!is_empty_class (type
))
3735 /* Record the location of this empty object in OFFSETS. */
3736 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3738 n
= splay_tree_insert (offsets
,
3739 (splay_tree_key
) offset
,
3740 (splay_tree_value
) NULL_TREE
);
3741 n
->value
= ((splay_tree_value
)
3742 tree_cons (NULL_TREE
,
3749 /* Returns nonzero if TYPE is an empty class type and there is
3750 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3753 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3758 if (!is_empty_class (type
))
3761 /* Record the location of this empty object in OFFSETS. */
3762 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3766 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3767 if (same_type_p (TREE_VALUE (t
), type
))
3773 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3774 F for every subobject, passing it the type, offset, and table of
3775 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3778 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3779 than MAX_OFFSET will not be walked.
3781 If F returns a nonzero value, the traversal ceases, and that value
3782 is returned. Otherwise, returns zero. */
3785 walk_subobject_offsets (tree type
,
3786 subobject_offset_fn f
,
3793 tree type_binfo
= NULL_TREE
;
3795 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3797 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3800 if (type
== error_mark_node
)
3806 type
= BINFO_TYPE (type
);
3809 if (CLASS_TYPE_P (type
))
3815 /* Avoid recursing into objects that are not interesting. */
3816 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3819 /* Record the location of TYPE. */
3820 r
= (*f
) (type
, offset
, offsets
);
3824 /* Iterate through the direct base classes of TYPE. */
3826 type_binfo
= TYPE_BINFO (type
);
3827 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
3831 if (BINFO_VIRTUAL_P (binfo
))
3835 /* We cannot rely on BINFO_OFFSET being set for the base
3836 class yet, but the offsets for direct non-virtual
3837 bases can be calculated by going back to the TYPE. */
3838 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
3839 binfo_offset
= size_binop (PLUS_EXPR
,
3841 BINFO_OFFSET (orig_binfo
));
3843 r
= walk_subobject_offsets (binfo
,
3853 if (CLASSTYPE_VBASECLASSES (type
))
3856 vec
<tree
, va_gc
> *vbases
;
3858 /* Iterate through the virtual base classes of TYPE. In G++
3859 3.2, we included virtual bases in the direct base class
3860 loop above, which results in incorrect results; the
3861 correct offsets for virtual bases are only known when
3862 working with the most derived type. */
3864 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
3865 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
3867 r
= walk_subobject_offsets (binfo
,
3869 size_binop (PLUS_EXPR
,
3871 BINFO_OFFSET (binfo
)),
3880 /* We still have to walk the primary base, if it is
3881 virtual. (If it is non-virtual, then it was walked
3883 tree vbase
= get_primary_binfo (type_binfo
);
3885 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
3886 && BINFO_PRIMARY_P (vbase
)
3887 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
3889 r
= (walk_subobject_offsets
3891 offsets
, max_offset
, /*vbases_p=*/0));
3898 /* Iterate through the fields of TYPE. */
3899 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
3900 if (TREE_CODE (field
) == FIELD_DECL
3901 && TREE_TYPE (field
) != error_mark_node
3902 && !DECL_ARTIFICIAL (field
))
3906 field_offset
= byte_position (field
);
3908 r
= walk_subobject_offsets (TREE_TYPE (field
),
3910 size_binop (PLUS_EXPR
,
3920 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3922 tree element_type
= strip_array_types (type
);
3923 tree domain
= TYPE_DOMAIN (type
);
3926 /* Avoid recursing into objects that are not interesting. */
3927 if (!CLASS_TYPE_P (element_type
)
3928 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
3930 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
3933 /* Step through each of the elements in the array. */
3934 for (index
= size_zero_node
;
3935 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
3936 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
3938 r
= walk_subobject_offsets (TREE_TYPE (type
),
3946 offset
= size_binop (PLUS_EXPR
, offset
,
3947 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
3948 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3949 there's no point in iterating through the remaining
3950 elements of the array. */
3951 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3959 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
3962 field_poverlapping_p (tree decl
)
3964 /* Base fields are actually potentially overlapping, but C++ bases go through
3965 a different code path based on binfos, and ObjC++ base fields are laid out
3966 in objc-act, so we don't want layout_class_type to mess with them. */
3967 if (DECL_FIELD_IS_BASE (decl
))
3969 gcc_checking_assert (c_dialect_objc ());
3973 return lookup_attribute ("no_unique_address",
3974 DECL_ATTRIBUTES (decl
));
3977 /* Record all of the empty subobjects of DECL_OR_BINFO. */
3980 record_subobject_offsets (tree decl_or_binfo
,
3984 bool overlapping
, vbases_p
;
3986 if (DECL_P (decl_or_binfo
))
3988 tree decl
= decl_or_binfo
;
3989 type
= TREE_TYPE (decl
);
3990 offset
= byte_position (decl
);
3991 overlapping
= field_poverlapping_p (decl
);
3996 type
= BINFO_TYPE (decl_or_binfo
);
3997 offset
= BINFO_OFFSET (decl_or_binfo
);
4003 /* If recording subobjects for a non-static data member or a
4004 non-empty base class, we do not need to record offsets beyond
4005 the size of the biggest empty class. Additional data members
4006 will go at the end of the class. Additional base classes will go
4007 either at offset zero (if empty, in which case they cannot
4008 overlap with offsets past the size of the biggest empty class) or
4009 at the end of the class.
4011 However, if we are placing an empty base class, then we must record
4012 all offsets, as either the empty class is at offset zero (where
4013 other empty classes might later be placed) or at the end of the
4014 class (where other objects might then be placed, so other empty
4015 subobjects might later overlap). */
4017 || !is_empty_class (type
))
4018 max_offset
= sizeof_biggest_empty_class
;
4020 max_offset
= NULL_TREE
;
4021 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4022 offsets
, max_offset
, vbases_p
);
4025 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4026 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4027 virtual bases of TYPE are examined. */
4030 layout_conflict_p (tree type
,
4035 splay_tree_node max_node
;
4037 /* Get the node in OFFSETS that indicates the maximum offset where
4038 an empty subobject is located. */
4039 max_node
= splay_tree_max (offsets
);
4040 /* If there aren't any empty subobjects, then there's no point in
4041 performing this check. */
4045 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4046 offsets
, (tree
) (max_node
->key
),
4050 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4051 non-static data member of the type indicated by RLI. BINFO is the
4052 binfo corresponding to the base subobject, OFFSETS maps offsets to
4053 types already located at those offsets. This function determines
4054 the position of the DECL. */
4057 layout_nonempty_base_or_field (record_layout_info rli
,
4062 tree offset
= NULL_TREE
;
4068 /* For the purposes of determining layout conflicts, we want to
4069 use the class type of BINFO; TREE_TYPE (DECL) will be the
4070 CLASSTYPE_AS_BASE version, which does not contain entries for
4071 zero-sized bases. */
4072 type
= TREE_TYPE (binfo
);
4077 type
= TREE_TYPE (decl
);
4081 /* Try to place the field. It may take more than one try if we have
4082 a hard time placing the field without putting two objects of the
4083 same type at the same address. */
4086 struct record_layout_info_s old_rli
= *rli
;
4088 /* Place this field. */
4089 place_field (rli
, decl
);
4090 offset
= byte_position (decl
);
4092 /* We have to check to see whether or not there is already
4093 something of the same type at the offset we're about to use.
4094 For example, consider:
4097 struct T : public S { int i; };
4098 struct U : public S, public T {};
4100 Here, we put S at offset zero in U. Then, we can't put T at
4101 offset zero -- its S component would be at the same address
4102 as the S we already allocated. So, we have to skip ahead.
4103 Since all data members, including those whose type is an
4104 empty class, have nonzero size, any overlap can happen only
4105 with a direct or indirect base-class -- it can't happen with
4107 /* In a union, overlap is permitted; all members are placed at
4109 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4111 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4114 /* Strip off the size allocated to this field. That puts us
4115 at the first place we could have put the field with
4116 proper alignment. */
4119 /* Bump up by the alignment required for the type. */
4121 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4123 ? CLASSTYPE_ALIGN (type
)
4124 : TYPE_ALIGN (type
)));
4125 normalize_rli (rli
);
4127 else if (TREE_CODE (type
) == NULLPTR_TYPE
4128 && warn_abi
&& abi_version_crosses (9))
4130 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4131 the offset wasn't aligned like a pointer when we started to
4132 layout this field, that affects its position. */
4133 tree pos
= rli_size_unit_so_far (&old_rli
);
4134 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4136 if (abi_version_at_least (9))
4137 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4138 "alignment of %qD increased in -fabi-version=9 "
4141 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4142 "of %qD will increase in -fabi-version=9", decl
);
4147 /* There was no conflict. We're done laying out this field. */
4151 /* Now that we know where it will be placed, update its
4153 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4154 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4155 this point because their BINFO_OFFSET is copied from another
4156 hierarchy. Therefore, we may not need to add the entire
4158 propagate_binfo_offsets (binfo
,
4159 size_diffop_loc (input_location
,
4160 fold_convert (ssizetype
, offset
),
4161 fold_convert (ssizetype
,
4162 BINFO_OFFSET (binfo
))));
4165 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4168 empty_base_at_nonzero_offset_p (tree type
,
4170 splay_tree
/*offsets*/)
4172 return is_empty_class (type
) && !integer_zerop (offset
);
4175 /* Layout the empty base BINFO. EOC indicates the byte currently just
4176 past the end of the class, and should be correctly aligned for a
4177 class of the type indicated by BINFO; OFFSETS gives the offsets of
4178 the empty bases allocated so far. T is the most derived
4179 type. Return nonzero iff we added it at the end. */
4182 layout_empty_base_or_field (record_layout_info rli
, tree binfo_or_decl
,
4187 tree binfo
= NULL_TREE
;
4188 tree decl
= NULL_TREE
;
4190 if (TREE_CODE (binfo_or_decl
) == TREE_BINFO
)
4192 binfo
= binfo_or_decl
;
4193 type
= BINFO_TYPE (binfo
);
4197 decl
= binfo_or_decl
;
4198 type
= TREE_TYPE (decl
);
4201 /* On some platforms (ARM), even empty classes will not be
4203 tree eoc
= round_up_loc (input_location
,
4204 rli_size_unit_so_far (rli
),
4205 CLASSTYPE_ALIGN_UNIT (type
));
4207 /* This routine should only be used for empty classes. */
4208 gcc_assert (is_empty_class (type
));
4209 alignment
= size_int (CLASSTYPE_ALIGN_UNIT (type
));
4211 /* This is an empty base class. We first try to put it at offset
4213 tree offset
= size_zero_node
;
4214 if (layout_conflict_p (type
,
4219 /* That didn't work. Now, we move forward from the next
4220 available spot in the class. */
4225 if (!layout_conflict_p (type
,
4229 /* We finally found a spot where there's no overlap. */
4232 /* There's overlap here, too. Bump along to the next spot. */
4233 offset
= size_binop (PLUS_EXPR
, offset
, alignment
);
4237 if (CLASSTYPE_USER_ALIGN (type
))
4239 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (type
));
4241 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (type
));
4242 TYPE_USER_ALIGN (rli
->t
) = 1;
4246 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4247 propagate_binfo_offsets (binfo
,
4248 size_diffop (offset
, BINFO_OFFSET (binfo
)));
4251 DECL_FIELD_OFFSET (decl
) = offset
;
4252 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4253 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4259 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4260 fields at NEXT_FIELD, and return it. */
4263 build_base_field_1 (tree t
, tree basetype
, tree
*&next_field
)
4265 /* Create the FIELD_DECL. */
4266 gcc_assert (CLASSTYPE_AS_BASE (basetype
));
4267 tree decl
= build_decl (input_location
,
4268 FIELD_DECL
, NULL_TREE
, CLASSTYPE_AS_BASE (basetype
));
4269 DECL_ARTIFICIAL (decl
) = 1;
4270 DECL_IGNORED_P (decl
) = 1;
4271 DECL_FIELD_CONTEXT (decl
) = t
;
4272 if (is_empty_class (basetype
))
4273 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4274 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = size_zero_node
;
4277 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4278 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4280 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4281 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4282 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4283 DECL_FIELD_IS_BASE (decl
) = 1;
4285 /* Add the new FIELD_DECL to the list of fields for T. */
4286 DECL_CHAIN (decl
) = *next_field
;
4288 next_field
= &DECL_CHAIN (decl
);
4293 /* Layout the base given by BINFO in the class indicated by RLI.
4294 *BASE_ALIGN is a running maximum of the alignments of
4295 any base class. OFFSETS gives the location of empty base
4296 subobjects. T is the most derived type. Return nonzero if the new
4297 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4298 *NEXT_FIELD, unless BINFO is for an empty base class.
4300 Returns the location at which the next field should be inserted. */
4303 build_base_field (record_layout_info rli
, tree binfo
,
4304 splay_tree offsets
, tree
*next_field
)
4307 tree basetype
= BINFO_TYPE (binfo
);
4309 if (!COMPLETE_TYPE_P (basetype
))
4310 /* This error is now reported in xref_tag, thus giving better
4311 location information. */
4314 /* Place the base class. */
4315 if (!is_empty_class (basetype
))
4319 /* The containing class is non-empty because it has a non-empty
4321 CLASSTYPE_EMPTY_P (t
) = 0;
4323 /* Create the FIELD_DECL. */
4324 decl
= build_base_field_1 (t
, basetype
, next_field
);
4326 /* Try to place the field. It may take more than one try if we
4327 have a hard time placing the field without putting two
4328 objects of the same type at the same address. */
4329 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4333 bool atend
= layout_empty_base_or_field (rli
, binfo
, offsets
);
4334 /* A nearly-empty class "has no proper base class that is empty,
4335 not morally virtual, and at an offset other than zero." */
4336 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4339 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4340 /* The check above (used in G++ 3.2) is insufficient because
4341 an empty class placed at offset zero might itself have an
4342 empty base at a nonzero offset. */
4343 else if (walk_subobject_offsets (basetype
,
4344 empty_base_at_nonzero_offset_p
,
4347 /*max_offset=*/NULL_TREE
,
4349 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4352 /* We used to not create a FIELD_DECL for empty base classes because of
4353 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4354 be a problem anymore. We need them to handle initialization of C++17
4356 if (cxx_dialect
>= cxx17
&& !BINFO_VIRTUAL_P (binfo
))
4358 tree decl
= build_base_field_1 (t
, basetype
, next_field
);
4359 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4360 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4361 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4364 /* An empty virtual base causes a class to be non-empty
4365 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4366 here because that was already done when the virtual table
4367 pointer was created. */
4370 /* Record the offsets of BINFO and its base subobjects. */
4371 record_subobject_offsets (binfo
, offsets
);
4376 /* Layout all of the non-virtual base classes. Record empty
4377 subobjects in OFFSETS. T is the most derived type. Return nonzero
4378 if the type cannot be nearly empty. The fields created
4379 corresponding to the base classes will be inserted at
4383 build_base_fields (record_layout_info rli
,
4384 splay_tree offsets
, tree
*next_field
)
4386 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4389 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
4392 /* The primary base class is always allocated first. */
4393 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
4394 next_field
= build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (t
),
4395 offsets
, next_field
);
4397 /* Now allocate the rest of the bases. */
4398 for (i
= 0; i
< n_baseclasses
; ++i
)
4402 base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
4404 /* The primary base was already allocated above, so we don't
4405 need to allocate it again here. */
4406 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
4409 /* Virtual bases are added at the end (a primary virtual base
4410 will have already been added). */
4411 if (BINFO_VIRTUAL_P (base_binfo
))
4414 next_field
= build_base_field (rli
, base_binfo
,
4415 offsets
, next_field
);
4419 /* Go through the TYPE_FIELDS of T issuing any appropriate
4420 diagnostics, figuring out which methods override which other
4421 methods, and so forth. */
4424 check_methods (tree t
)
4426 for (tree x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
4427 if (DECL_DECLARES_FUNCTION_P (x
))
4429 check_for_override (x
, t
);
4431 if (DECL_PURE_VIRTUAL_P (x
)
4432 && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4433 error ("initializer specified for non-virtual method %q+D", x
);
4434 /* The name of the field is the original field name
4435 Save this in auxiliary field for later overloading. */
4436 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4438 TYPE_POLYMORPHIC_P (t
) = 1;
4439 if (DECL_PURE_VIRTUAL_P (x
))
4440 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4443 /* All user-provided destructors are non-trivial.
4444 Constructors and assignment ops are handled in
4445 grok_special_member_properties. */
4446 if (DECL_DESTRUCTOR_P (x
) && user_provided_p (x
))
4447 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 1;
4448 if (!DECL_VIRTUAL_P (x
)
4449 && lookup_attribute ("transaction_safe_dynamic",
4450 DECL_ATTRIBUTES (x
)))
4451 error_at (DECL_SOURCE_LOCATION (x
),
4452 "%<transaction_safe_dynamic%> may only be specified for "
4453 "a virtual function");
4457 /* FN is a constructor or destructor. Clone the declaration to create
4458 a specialized in-charge or not-in-charge version, as indicated by
4462 build_clone (tree fn
, tree name
)
4467 /* Copy the function. */
4468 clone
= copy_decl (fn
);
4469 /* Reset the function name. */
4470 DECL_NAME (clone
) = name
;
4471 /* Remember where this function came from. */
4472 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4473 /* Make it easy to find the CLONE given the FN. */
4474 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4475 DECL_CHAIN (fn
) = clone
;
4477 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4478 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4480 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4481 DECL_TEMPLATE_RESULT (clone
) = result
;
4482 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4483 DECL_TI_TEMPLATE (result
) = clone
;
4484 TREE_TYPE (clone
) = TREE_TYPE (result
);
4489 // Clone constraints.
4491 if (tree ci
= get_constraints (fn
))
4492 set_constraints (clone
, copy_node (ci
));
4496 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4497 DECL_CLONED_FUNCTION (clone
) = fn
;
4498 /* There's no pending inline data for this function. */
4499 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4500 DECL_PENDING_INLINE_P (clone
) = 0;
4502 /* The base-class destructor is not virtual. */
4503 if (name
== base_dtor_identifier
)
4505 DECL_VIRTUAL_P (clone
) = 0;
4506 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4507 DECL_VINDEX (clone
) = NULL_TREE
;
4510 bool ctor_omit_inherited_parms_p
= ctor_omit_inherited_parms (clone
);
4511 if (ctor_omit_inherited_parms_p
)
4512 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone
));
4514 /* If there was an in-charge parameter, drop it from the function
4516 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4518 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4519 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4520 /* Skip the `this' parameter. */
4521 parmtypes
= TREE_CHAIN (parmtypes
);
4522 /* Skip the in-charge parameter. */
4523 parmtypes
= TREE_CHAIN (parmtypes
);
4524 /* And the VTT parm, in a complete [cd]tor. */
4525 if (DECL_HAS_VTT_PARM_P (fn
)
4526 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4527 parmtypes
= TREE_CHAIN (parmtypes
);
4528 if (ctor_omit_inherited_parms_p
)
4530 /* If we're omitting inherited parms, that just leaves the VTT. */
4531 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone
));
4532 parmtypes
= tree_cons (NULL_TREE
, vtt_parm_type
, void_list_node
);
4535 = build_method_type_directly (basetype
,
4536 TREE_TYPE (TREE_TYPE (clone
)),
4539 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4540 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4542 = cxx_copy_lang_qualifiers (TREE_TYPE (clone
), TREE_TYPE (fn
));
4545 /* Copy the function parameters. */
4546 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4547 /* Remove the in-charge parameter. */
4548 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4550 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4551 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4552 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4554 /* And the VTT parm, in a complete [cd]tor. */
4555 if (DECL_HAS_VTT_PARM_P (fn
))
4557 if (DECL_NEEDS_VTT_PARM_P (clone
))
4558 DECL_HAS_VTT_PARM_P (clone
) = 1;
4561 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4562 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4563 DECL_HAS_VTT_PARM_P (clone
) = 0;
4567 /* A base constructor inheriting from a virtual base doesn't get the
4569 if (ctor_omit_inherited_parms_p
)
4570 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
4572 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4574 DECL_CONTEXT (parms
) = clone
;
4575 cxx_dup_lang_specific_decl (parms
);
4578 /* Create the RTL for this function. */
4579 SET_DECL_RTL (clone
, NULL
);
4580 rest_of_decl_compilation (clone
, /*top_level=*/1, at_eof
);
4585 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4586 not invoke this function directly.
4588 For a non-thunk function, returns the address of the slot for storing
4589 the function it is a clone of. Otherwise returns NULL_TREE.
4591 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4592 cloned_function is unset. This is to support the separate
4593 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4594 on a template makes sense, but not the former. */
4597 decl_cloned_function_p (const_tree decl
, bool just_testing
)
4601 decl
= STRIP_TEMPLATE (decl
);
4603 if (TREE_CODE (decl
) != FUNCTION_DECL
4604 || !DECL_LANG_SPECIFIC (decl
)
4605 || DECL_LANG_SPECIFIC (decl
)->u
.fn
.thunk_p
)
4607 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4609 lang_check_failed (__FILE__
, __LINE__
, __FUNCTION__
);
4615 ptr
= &DECL_LANG_SPECIFIC (decl
)->u
.fn
.u5
.cloned_function
;
4616 if (just_testing
&& *ptr
== NULL_TREE
)
4622 /* Produce declarations for all appropriate clones of FN. If
4623 UPDATE_METHODS is true, the clones are added to the
4624 CLASSTYPE_MEMBER_VEC. */
4627 clone_function_decl (tree fn
, bool update_methods
)
4631 /* Avoid inappropriate cloning. */
4633 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4636 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4638 /* For each constructor, we need two variants: an in-charge version
4639 and a not-in-charge version. */
4640 clone
= build_clone (fn
, complete_ctor_identifier
);
4642 add_method (DECL_CONTEXT (clone
), clone
, false);
4643 clone
= build_clone (fn
, base_ctor_identifier
);
4645 add_method (DECL_CONTEXT (clone
), clone
, false);
4649 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4651 /* For each destructor, we need three variants: an in-charge
4652 version, a not-in-charge version, and an in-charge deleting
4653 version. We clone the deleting version first because that
4654 means it will go second on the TYPE_FIELDS list -- and that
4655 corresponds to the correct layout order in the virtual
4658 For a non-virtual destructor, we do not build a deleting
4660 if (DECL_VIRTUAL_P (fn
))
4662 clone
= build_clone (fn
, deleting_dtor_identifier
);
4664 add_method (DECL_CONTEXT (clone
), clone
, false);
4666 clone
= build_clone (fn
, complete_dtor_identifier
);
4668 add_method (DECL_CONTEXT (clone
), clone
, false);
4669 clone
= build_clone (fn
, base_dtor_identifier
);
4671 add_method (DECL_CONTEXT (clone
), clone
, false);
4674 /* Note that this is an abstract function that is never emitted. */
4675 DECL_ABSTRACT_P (fn
) = true;
4678 /* DECL is an in charge constructor, which is being defined. This will
4679 have had an in class declaration, from whence clones were
4680 declared. An out-of-class definition can specify additional default
4681 arguments. As it is the clones that are involved in overload
4682 resolution, we must propagate the information from the DECL to its
4686 adjust_clone_args (tree decl
)
4690 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4691 clone
= DECL_CHAIN (clone
))
4693 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4694 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4695 tree decl_parms
, clone_parms
;
4697 clone_parms
= orig_clone_parms
;
4699 /* Skip the 'this' parameter. */
4700 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4701 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4703 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4704 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4705 if (DECL_HAS_VTT_PARM_P (decl
))
4706 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4708 clone_parms
= orig_clone_parms
;
4709 if (DECL_HAS_VTT_PARM_P (clone
))
4710 clone_parms
= TREE_CHAIN (clone_parms
);
4712 for (decl_parms
= orig_decl_parms
; decl_parms
;
4713 decl_parms
= TREE_CHAIN (decl_parms
),
4714 clone_parms
= TREE_CHAIN (clone_parms
))
4716 if (clone_parms
== void_list_node
)
4718 gcc_assert (decl_parms
== clone_parms
4719 || ctor_omit_inherited_parms (clone
));
4723 gcc_assert (same_type_p (TREE_TYPE (decl_parms
),
4724 TREE_TYPE (clone_parms
)));
4726 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
4728 /* A default parameter has been added. Adjust the
4729 clone's parameters. */
4730 clone_parms
= orig_decl_parms
;
4732 if (DECL_HAS_VTT_PARM_P (clone
))
4734 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
4735 TREE_VALUE (orig_clone_parms
),
4737 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
4740 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4742 = build_method_type_directly (basetype
,
4743 TREE_TYPE (TREE_TYPE (clone
)),
4745 if (tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
)))
4746 type
= cp_build_type_attribute_variant (type
, attrs
);
4747 type
= cxx_copy_lang_qualifiers (type
, TREE_TYPE (clone
));
4748 TREE_TYPE (clone
) = type
;
4750 clone_parms
= NULL_TREE
;
4754 gcc_assert (!clone_parms
|| clone_parms
== void_list_node
);
4758 /* For each of the constructors and destructors in T, create an
4759 in-charge and not-in-charge variant. */
4762 clone_constructors_and_destructors (tree t
)
4764 /* While constructors can be via a using declaration, at this point
4765 we no longer need to know that. */
4766 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
4767 clone_function_decl (*iter
, /*update_methods=*/true);
4769 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
4770 clone_function_decl (dtor
, /*update_methods=*/true);
4773 /* Deduce noexcept for a destructor DTOR. */
4776 deduce_noexcept_on_destructor (tree dtor
)
4778 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
4779 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
),
4780 noexcept_deferred_spec
);
4783 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4784 of TYPE for virtual functions which FNDECL overrides. Return a
4785 mask of the tm attributes found therein. */
4788 look_for_tm_attr_overrides (tree type
, tree fndecl
)
4790 tree binfo
= TYPE_BINFO (type
);
4794 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
4796 tree o
, basetype
= BINFO_TYPE (base_binfo
);
4798 if (!TYPE_POLYMORPHIC_P (basetype
))
4801 o
= look_for_overrides_here (basetype
, fndecl
);
4804 if (lookup_attribute ("transaction_safe_dynamic",
4805 DECL_ATTRIBUTES (o
)))
4806 /* transaction_safe_dynamic is not inherited. */;
4808 found
|= tm_attr_to_mask (find_tm_attribute
4809 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
4812 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
4818 /* Subroutine of set_method_tm_attributes. Handle the checks and
4819 inheritance for one virtual method FNDECL. */
4822 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
4827 found
= look_for_tm_attr_overrides (type
, fndecl
);
4829 /* If FNDECL doesn't actually override anything (i.e. T is the
4830 class that first declares FNDECL virtual), then we're done. */
4834 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
4835 have
= tm_attr_to_mask (tm_attr
);
4837 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4838 tm_pure must match exactly, otherwise no weakening of
4839 tm_safe > tm_callable > nothing. */
4840 /* ??? The tm_pure attribute didn't make the transition to the
4841 multivendor language spec. */
4842 if (have
== TM_ATTR_PURE
)
4844 if (found
!= TM_ATTR_PURE
)
4850 /* If the overridden function is tm_pure, then FNDECL must be. */
4851 else if (found
== TM_ATTR_PURE
&& tm_attr
)
4853 /* Look for base class combinations that cannot be satisfied. */
4854 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
4856 found
&= ~TM_ATTR_PURE
;
4858 error_at (DECL_SOURCE_LOCATION (fndecl
),
4859 "method overrides both %<transaction_pure%> and %qE methods",
4860 tm_mask_to_attr (found
));
4862 /* If FNDECL did not declare an attribute, then inherit the most
4864 else if (tm_attr
== NULL
)
4866 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
4868 /* Otherwise validate that we're not weaker than a function
4869 that is being overridden. */
4873 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
4879 error_at (DECL_SOURCE_LOCATION (fndecl
),
4880 "method declared %qE overriding %qE method",
4881 tm_attr
, tm_mask_to_attr (found
));
4884 /* For each of the methods in T, propagate a class-level tm attribute. */
4887 set_method_tm_attributes (tree t
)
4889 tree class_tm_attr
, fndecl
;
4891 /* Don't bother collecting tm attributes if transactional memory
4892 support is not enabled. */
4896 /* Process virtual methods first, as they inherit directly from the
4897 base virtual function and also require validation of new attributes. */
4898 if (TYPE_CONTAINS_VPTR_P (t
))
4901 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
4902 vchain
= TREE_CHAIN (vchain
))
4904 fndecl
= BV_FN (vchain
);
4905 if (DECL_THUNK_P (fndecl
))
4906 fndecl
= THUNK_TARGET (fndecl
);
4907 set_one_vmethod_tm_attributes (t
, fndecl
);
4911 /* If the class doesn't have an attribute, nothing more to do. */
4912 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
4913 if (class_tm_attr
== NULL
)
4916 /* Any method that does not yet have a tm attribute inherits
4917 the one from the class. */
4918 for (fndecl
= TYPE_FIELDS (t
); fndecl
; fndecl
= DECL_CHAIN (fndecl
))
4919 if (DECL_DECLARES_FUNCTION_P (fndecl
)
4920 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
4921 apply_tm_attr (fndecl
, class_tm_attr
);
4924 /* Returns true if FN is a default constructor. */
4927 default_ctor_p (tree fn
)
4929 return (DECL_CONSTRUCTOR_P (fn
)
4930 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
4933 /* Returns true iff class T has a user-provided constructor that can be called
4934 with more than zero arguments. */
4937 type_has_user_nondefault_constructor (tree t
)
4939 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4942 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
4945 if (user_provided_p (fn
)
4946 && (TREE_CODE (fn
) == TEMPLATE_DECL
4947 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
4955 /* Returns the defaulted constructor if T has one. Otherwise, returns
4959 in_class_defaulted_default_constructor (tree t
)
4961 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4964 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
4968 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
4969 && default_ctor_p (fn
))
4976 /* Returns true iff FN is a user-provided function, i.e. user-declared
4977 and not defaulted at its first declaration. */
4980 user_provided_p (tree fn
)
4982 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4985 return (!DECL_ARTIFICIAL (fn
)
4986 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
4987 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
4990 /* Returns true iff class T has a user-provided constructor. */
4993 type_has_user_provided_constructor (tree t
)
4995 if (!CLASS_TYPE_P (t
))
4998 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5001 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5002 if (user_provided_p (*iter
))
5008 /* Returns true iff class T has a user-provided or explicit constructor. */
5011 type_has_user_provided_or_explicit_constructor (tree t
)
5013 if (!CLASS_TYPE_P (t
))
5016 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5019 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5022 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5029 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5030 declared or explicitly defaulted in the class body) default
5034 type_has_non_user_provided_default_constructor (tree t
)
5036 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5038 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5041 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5044 if (TREE_CODE (fn
) == FUNCTION_DECL
5045 && default_ctor_p (fn
)
5046 && !user_provided_p (fn
))
5053 /* TYPE is being used as a virtual base, and has a non-trivial move
5054 assignment. Return true if this is due to there being a user-provided
5055 move assignment in TYPE or one of its subobjects; if there isn't, then
5056 multiple move assignment can't cause any harm. */
5059 vbase_has_user_provided_move_assign (tree type
)
5061 /* Does the type itself have a user-provided move assignment operator? */
5062 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type
))
5063 for (ovl_iterator
iter (get_class_binding_direct
5064 (type
, assign_op_identifier
));
5066 if (user_provided_p (*iter
) && move_fn_p (*iter
))
5069 /* Do any of its bases? */
5070 tree binfo
= TYPE_BINFO (type
);
5072 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5073 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5076 /* Or non-static data members? */
5077 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5079 if (TREE_CODE (field
) == FIELD_DECL
5080 && CLASS_TYPE_P (TREE_TYPE (field
))
5081 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5089 /* If default-initialization leaves part of TYPE uninitialized, returns
5090 a DECL for the field or TYPE itself (DR 253). */
5093 default_init_uninitialized_part (tree type
)
5098 type
= strip_array_types (type
);
5099 if (!CLASS_TYPE_P (type
))
5101 if (!type_has_non_user_provided_default_constructor (type
))
5103 for (binfo
= TYPE_BINFO (type
), i
= 0;
5104 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5106 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5110 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5111 if (TREE_CODE (t
) == FIELD_DECL
5112 && !DECL_ARTIFICIAL (t
)
5113 && !DECL_INITIAL (t
))
5115 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5117 return DECL_P (r
) ? r
: t
;
5123 /* Returns true iff for class T, a trivial synthesized default constructor
5124 would be constexpr. */
5127 trivial_default_constructor_is_constexpr (tree t
)
5129 /* A defaulted trivial default constructor is constexpr
5130 if there is nothing to initialize. */
5131 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5132 return is_really_empty_class (t
);
5135 /* Returns true iff class T has a constexpr default constructor. */
5138 type_has_constexpr_default_constructor (tree t
)
5142 if (!CLASS_TYPE_P (t
))
5144 /* The caller should have stripped an enclosing array. */
5145 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5148 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5150 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5151 return trivial_default_constructor_is_constexpr (t
);
5152 /* Non-trivial, we need to check subobject constructors. */
5153 lazily_declare_fn (sfk_constructor
, t
);
5155 fns
= locate_ctor (t
);
5156 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5159 /* Returns true iff class T has a constexpr default constructor or has an
5160 implicitly declared default constructor that we can't tell if it's constexpr
5161 without forcing a lazy declaration (which might cause undesired
5165 type_maybe_constexpr_default_constructor (tree t
)
5167 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5168 && TYPE_HAS_COMPLEX_DFLT (t
))
5169 /* Assume it's constexpr. */
5171 return type_has_constexpr_default_constructor (t
);
5174 /* Returns true iff class TYPE has a virtual destructor. */
5177 type_has_virtual_destructor (tree type
)
5181 if (!CLASS_TYPE_P (type
))
5184 gcc_assert (COMPLETE_TYPE_P (type
));
5185 dtor
= CLASSTYPE_DESTRUCTOR (type
);
5186 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5189 /* Returns true iff T, a class, has a move-assignment or
5190 move-constructor. Does not lazily declare either.
5191 If USER_P is false, any move function will do. If it is true, the
5192 move function must be user-declared.
5194 Note that user-declared here is different from "user-provided",
5195 which doesn't include functions that are defaulted in the
5199 classtype_has_move_assign_or_move_ctor_p (tree t
, bool user_p
)
5202 || (!CLASSTYPE_LAZY_MOVE_CTOR (t
)
5203 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t
)));
5205 if (!CLASSTYPE_LAZY_MOVE_CTOR (t
))
5206 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5207 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5210 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5211 for (ovl_iterator
iter (get_class_binding_direct
5212 (t
, assign_op_identifier
));
5214 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5220 /* True iff T has a move constructor that is not deleted. */
5223 classtype_has_non_deleted_move_ctor (tree t
)
5225 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5226 lazily_declare_fn (sfk_move_constructor
, t
);
5227 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5228 if (move_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5233 /* If T, a class, has a user-provided copy constructor, copy assignment
5234 operator, or destructor, returns that function. Otherwise, null. */
5237 classtype_has_depr_implicit_copy (tree t
)
5239 if (!CLASSTYPE_LAZY_COPY_CTOR (t
))
5240 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5243 if (user_provided_p (fn
) && copy_fn_p (fn
))
5247 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t
))
5248 for (ovl_iterator
iter (get_class_binding_direct
5249 (t
, assign_op_identifier
));
5253 if (user_provided_p (fn
) && copy_fn_p (fn
))
5257 if (!CLASSTYPE_LAZY_DESTRUCTOR (t
))
5259 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5260 if (user_provided_p (fn
))
5267 /* Nonzero if we need to build up a constructor call when initializing an
5268 object of this class, either because it has a user-declared constructor
5269 or because it doesn't have a default constructor (so we need to give an
5270 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5271 what you care about is whether or not an object can be produced by a
5272 constructor (e.g. so we don't set TREE_READONLY on const variables of
5273 such type); use this function when what you care about is whether or not
5274 to try to call a constructor to create an object. The latter case is
5275 the former plus some cases of constructors that cannot be called. */
5278 type_build_ctor_call (tree t
)
5281 if (TYPE_NEEDS_CONSTRUCTING (t
))
5283 inner
= strip_array_types (t
);
5284 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5286 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5288 if (cxx_dialect
< cxx11
)
5290 /* A user-declared constructor might be private, and a constructor might
5291 be trivial but deleted. */
5292 for (ovl_iterator
iter (get_class_binding (inner
, complete_ctor_identifier
));
5296 if (!DECL_ARTIFICIAL (fn
)
5297 || TREE_DEPRECATED (fn
)
5298 || DECL_DELETED_FN (fn
))
5304 /* Like type_build_ctor_call, but for destructors. */
5307 type_build_dtor_call (tree t
)
5310 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5312 inner
= strip_array_types (t
);
5313 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5314 || !COMPLETE_TYPE_P (inner
))
5316 if (cxx_dialect
< cxx11
)
5318 /* A user-declared destructor might be private, and a destructor might
5319 be trivial but deleted. */
5320 for (ovl_iterator
iter (get_class_binding (inner
, complete_dtor_identifier
));
5324 if (!DECL_ARTIFICIAL (fn
)
5325 || TREE_DEPRECATED (fn
)
5326 || DECL_DELETED_FN (fn
))
5332 /* Remove all zero-width bit-fields from T. */
5335 remove_zero_width_bit_fields (tree t
)
5339 fieldsp
= &TYPE_FIELDS (t
);
5342 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5343 && DECL_C_BIT_FIELD (*fieldsp
)
5344 /* We should not be confused by the fact that grokbitfield
5345 temporarily sets the width of the bit field into
5346 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5347 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5349 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5350 || integer_zerop (DECL_SIZE (*fieldsp
))))
5351 *fieldsp
= DECL_CHAIN (*fieldsp
);
5353 fieldsp
= &DECL_CHAIN (*fieldsp
);
5357 /* Returns TRUE iff we need a cookie when dynamically allocating an
5358 array whose elements have the indicated class TYPE. */
5361 type_requires_array_cookie (tree type
)
5364 bool has_two_argument_delete_p
= false;
5366 gcc_assert (CLASS_TYPE_P (type
));
5368 /* If there's a non-trivial destructor, we need a cookie. In order
5369 to iterate through the array calling the destructor for each
5370 element, we'll have to know how many elements there are. */
5371 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5374 /* If the usual deallocation function is a two-argument whose second
5375 argument is of type `size_t', then we have to pass the size of
5376 the array to the deallocation function, so we will need to store
5378 fns
= lookup_fnfields (TYPE_BINFO (type
),
5379 ovl_op_identifier (false, VEC_DELETE_EXPR
),
5381 /* If there are no `operator []' members, or the lookup is
5382 ambiguous, then we don't need a cookie. */
5383 if (!fns
|| fns
== error_mark_node
)
5385 /* Loop through all of the functions. */
5386 for (lkp_iterator
iter (BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
5390 /* See if this function is a one-argument delete function. If
5391 it is, then it will be the usual deallocation function. */
5392 tree second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5393 if (second_parm
== void_list_node
)
5395 /* Do not consider this function if its second argument is an
5399 /* Otherwise, if we have a two-argument function and the second
5400 argument is `size_t', it will be the usual deallocation
5401 function -- unless there is one-argument function, too. */
5402 if (TREE_CHAIN (second_parm
) == void_list_node
5403 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5404 has_two_argument_delete_p
= true;
5407 return has_two_argument_delete_p
;
5410 /* Finish computing the `literal type' property of class type T.
5412 At this point, we have already processed base classes and
5413 non-static data members. We need to check whether the copy
5414 constructor is trivial, the destructor is trivial, and there
5415 is a trivial default constructor or at least one constexpr
5416 constructor other than the copy constructor. */
5419 finalize_literal_type_property (tree t
)
5423 if (cxx_dialect
< cxx11
5424 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5425 CLASSTYPE_LITERAL_P (t
) = false;
5426 else if (CLASSTYPE_LITERAL_P (t
) && LAMBDA_TYPE_P (t
))
5427 CLASSTYPE_LITERAL_P (t
) = (cxx_dialect
>= cxx17
);
5428 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5429 && CLASSTYPE_NON_AGGREGATE (t
)
5430 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5431 CLASSTYPE_LITERAL_P (t
) = false;
5433 /* C++14 DR 1684 removed this restriction. */
5434 if (cxx_dialect
< cxx14
5435 && !CLASSTYPE_LITERAL_P (t
) && !LAMBDA_TYPE_P (t
))
5436 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5437 if (TREE_CODE (fn
) == FUNCTION_DECL
5438 && DECL_DECLARED_CONSTEXPR_P (fn
)
5439 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5440 && !DECL_CONSTRUCTOR_P (fn
))
5442 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5443 if (!DECL_GENERATED_P (fn
))
5445 auto_diagnostic_group d
;
5446 if (pedwarn (DECL_SOURCE_LOCATION (fn
), OPT_Wpedantic
,
5447 "enclosing class of %<constexpr%> non-static "
5448 "member function %q+#D is not a literal type", fn
))
5449 explain_non_literal_class (t
);
5454 /* T is a non-literal type used in a context which requires a constant
5455 expression. Explain why it isn't literal. */
5458 explain_non_literal_class (tree t
)
5460 static hash_set
<tree
> *diagnosed
;
5462 if (!CLASS_TYPE_P (t
))
5464 t
= TYPE_MAIN_VARIANT (t
);
5466 if (diagnosed
== NULL
)
5467 diagnosed
= new hash_set
<tree
>;
5468 if (diagnosed
->add (t
))
5469 /* Already explained. */
5472 auto_diagnostic_group d
;
5473 inform (UNKNOWN_LOCATION
, "%q+T is not literal because:", t
);
5474 if (cxx_dialect
< cxx17
&& LAMBDA_TYPE_P (t
))
5475 inform (UNKNOWN_LOCATION
,
5476 " %qT is a closure type, which is only literal in "
5477 "C++17 and later", t
);
5478 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5479 inform (UNKNOWN_LOCATION
, " %q+T has a non-trivial destructor", t
);
5480 else if (CLASSTYPE_NON_AGGREGATE (t
)
5481 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5482 && !LAMBDA_TYPE_P (t
)
5483 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5485 inform (UNKNOWN_LOCATION
,
5486 " %q+T is not an aggregate, does not have a trivial "
5487 "default constructor, and has no %<constexpr%> constructor that "
5488 "is not a copy or move constructor", t
);
5489 if (type_has_non_user_provided_default_constructor (t
))
5490 /* Note that we can't simply call locate_ctor because when the
5491 constructor is deleted it just returns NULL_TREE. */
5492 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5495 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5497 parms
= skip_artificial_parms_for (fn
, parms
);
5499 if (sufficient_parms_p (parms
))
5501 if (DECL_DELETED_FN (fn
))
5502 maybe_explain_implicit_delete (fn
);
5504 explain_invalid_constexpr_fn (fn
);
5511 tree binfo
, base_binfo
, field
; int i
;
5512 for (binfo
= TYPE_BINFO (t
), i
= 0;
5513 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5515 tree basetype
= TREE_TYPE (base_binfo
);
5516 if (!CLASSTYPE_LITERAL_P (basetype
))
5518 inform (UNKNOWN_LOCATION
,
5519 " base class %qT of %q+T is non-literal",
5521 explain_non_literal_class (basetype
);
5525 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5528 if (TREE_CODE (field
) != FIELD_DECL
)
5530 ftype
= TREE_TYPE (field
);
5531 if (!literal_type_p (ftype
))
5533 inform (DECL_SOURCE_LOCATION (field
),
5534 " non-static data member %qD has non-literal type",
5536 if (CLASS_TYPE_P (ftype
))
5537 explain_non_literal_class (ftype
);
5539 if (CP_TYPE_VOLATILE_P (ftype
))
5540 inform (DECL_SOURCE_LOCATION (field
),
5541 " non-static data member %qD has volatile type", field
);
5546 /* Check the validity of the bases and members declared in T. Add any
5547 implicitly-generated functions (like copy-constructors and
5548 assignment operators). Compute various flag bits (like
5549 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5550 level: i.e., independently of the ABI in use. */
5553 check_bases_and_members (tree t
)
5555 /* Nonzero if the implicitly generated copy constructor should take
5556 a non-const reference argument. */
5557 int cant_have_const_ctor
;
5558 /* Nonzero if the implicitly generated assignment operator
5559 should take a non-const reference argument. */
5560 int no_const_asn_ref
;
5562 bool saved_complex_asn_ref
;
5563 bool saved_nontrivial_dtor
;
5566 /* By default, we use const reference arguments and generate default
5568 cant_have_const_ctor
= 0;
5569 no_const_asn_ref
= 0;
5571 /* Check all the base-classes and set FMEM members to point to arrays
5572 of potential interest. */
5573 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
5575 /* Deduce noexcept on destructor. This needs to happen after we've set
5576 triviality flags appropriately for our bases. */
5577 if (cxx_dialect
>= cxx11
)
5578 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5579 deduce_noexcept_on_destructor (dtor
);
5581 /* Check all the method declarations. */
5584 /* Save the initial values of these flags which only indicate whether
5585 or not the class has user-provided functions. As we analyze the
5586 bases and members we can set these flags for other reasons. */
5587 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5588 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5590 /* Check all the data member declarations. We cannot call
5591 check_field_decls until we have called check_bases check_methods,
5592 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5593 being set appropriately. */
5594 check_field_decls (t
, &access_decls
,
5595 &cant_have_const_ctor
,
5598 /* A nearly-empty class has to be vptr-containing; a nearly empty
5599 class contains just a vptr. */
5600 if (!TYPE_CONTAINS_VPTR_P (t
))
5601 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5603 /* Do some bookkeeping that will guide the generation of implicitly
5604 declared member functions. */
5605 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5606 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5607 /* We need to call a constructor for this class if it has a
5608 user-provided constructor, or if the default constructor is going
5609 to initialize the vptr. (This is not an if-and-only-if;
5610 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5611 themselves need constructing.) */
5612 TYPE_NEEDS_CONSTRUCTING (t
)
5613 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5616 An aggregate is an array or a class with no user-provided
5617 constructors ... and no virtual functions.
5619 Again, other conditions for being an aggregate are checked
5621 CLASSTYPE_NON_AGGREGATE (t
)
5622 |= ((cxx_dialect
< cxx2a
5623 ? type_has_user_provided_or_explicit_constructor (t
)
5624 : TYPE_HAS_USER_CONSTRUCTOR (t
))
5625 || TYPE_POLYMORPHIC_P (t
));
5626 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5627 retain the old definition internally for ABI reasons. */
5628 CLASSTYPE_NON_LAYOUT_POD_P (t
)
5629 |= (CLASSTYPE_NON_AGGREGATE (t
)
5630 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
5631 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5632 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5633 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5634 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5636 /* If the only explicitly declared default constructor is user-provided,
5637 set TYPE_HAS_COMPLEX_DFLT. */
5638 if (!TYPE_HAS_COMPLEX_DFLT (t
)
5639 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
5640 && !type_has_non_user_provided_default_constructor (t
))
5641 TYPE_HAS_COMPLEX_DFLT (t
) = true;
5643 /* Warn if a public base of a polymorphic type has an accessible
5644 non-virtual destructor. It is only now that we know the class is
5645 polymorphic. Although a polymorphic base will have a already
5646 been diagnosed during its definition, we warn on use too. */
5647 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
5649 tree binfo
= TYPE_BINFO (t
);
5650 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
5654 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5656 tree basetype
= TREE_TYPE (base_binfo
);
5658 if ((*accesses
)[i
] == access_public_node
5659 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
5660 && accessible_nvdtor_p (basetype
))
5661 warning (OPT_Wnon_virtual_dtor
,
5662 "base class %q#T has accessible non-virtual destructor",
5667 /* If the class has no user-declared constructor, but does have
5668 non-static const or reference data members that can never be
5669 initialized, issue a warning. */
5670 if (warn_uninitialized
5671 /* Classes with user-declared constructors are presumed to
5672 initialize these members. */
5673 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
5674 /* Aggregates can be initialized with brace-enclosed
5676 && CLASSTYPE_NON_AGGREGATE (t
))
5680 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5684 if (TREE_CODE (field
) != FIELD_DECL
5685 || DECL_INITIAL (field
) != NULL_TREE
)
5688 type
= TREE_TYPE (field
);
5689 if (TYPE_REF_P (type
))
5690 warning_at (DECL_SOURCE_LOCATION (field
),
5691 OPT_Wuninitialized
, "non-static reference %q#D "
5692 "in class without a constructor", field
);
5693 else if (CP_TYPE_CONST_P (type
)
5694 && (!CLASS_TYPE_P (type
)
5695 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
5696 warning_at (DECL_SOURCE_LOCATION (field
),
5697 OPT_Wuninitialized
, "non-static const member %q#D "
5698 "in class without a constructor", field
);
5702 /* Synthesize any needed methods. */
5703 add_implicitly_declared_members (t
, &access_decls
,
5704 cant_have_const_ctor
,
5707 /* Check defaulted declarations here so we have cant_have_const_ctor
5708 and don't need to worry about clones. */
5709 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5710 if (DECL_DECLARES_FUNCTION_P (fn
)
5711 && !DECL_ARTIFICIAL (fn
)
5712 && DECL_DEFAULTED_IN_CLASS_P (fn
))
5714 int copy
= copy_fn_p (fn
);
5718 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
5719 : !no_const_asn_ref
);
5720 bool fn_const_p
= (copy
== 2);
5722 if (fn_const_p
&& !imp_const_p
)
5723 /* If the function is defaulted outside the class, we just
5724 give the synthesis error. Core Issue #1331 says this is
5725 no longer ill-formed, it is defined as deleted instead. */
5726 DECL_DELETED_FN (fn
) = true;
5728 defaulted_late_check (fn
);
5731 if (LAMBDA_TYPE_P (t
))
5733 /* "This class type is not an aggregate." */
5734 CLASSTYPE_NON_AGGREGATE (t
) = 1;
5737 /* Compute the 'literal type' property before we
5738 do anything with non-static member functions. */
5739 finalize_literal_type_property (t
);
5741 /* Create the in-charge and not-in-charge variants of constructors
5743 clone_constructors_and_destructors (t
);
5745 /* Process the using-declarations. */
5746 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
5747 handle_using_decl (TREE_VALUE (access_decls
), t
);
5749 /* Figure out whether or not we will need a cookie when dynamically
5750 allocating an array of this type. */
5751 LANG_TYPE_CLASS_CHECK (t
)->vec_new_uses_cookie
5752 = type_requires_array_cookie (t
);
5755 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5756 accordingly. If a new vfield was created (because T doesn't have a
5757 primary base class), then the newly created field is returned. It
5758 is not added to the TYPE_FIELDS list; it is the caller's
5759 responsibility to do that. Accumulate declared virtual functions
5763 create_vtable_ptr (tree t
, tree
* virtuals_p
)
5767 /* Collect the virtual functions declared in T. */
5768 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5769 if (TREE_CODE (fn
) == FUNCTION_DECL
5770 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
5771 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
5773 tree new_virtual
= make_node (TREE_LIST
);
5775 BV_FN (new_virtual
) = fn
;
5776 BV_DELTA (new_virtual
) = integer_zero_node
;
5777 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
5779 TREE_CHAIN (new_virtual
) = *virtuals_p
;
5780 *virtuals_p
= new_virtual
;
5783 /* If we couldn't find an appropriate base class, create a new field
5784 here. Even if there weren't any new virtual functions, we might need a
5785 new virtual function table if we're supposed to include vptrs in
5786 all classes that need them. */
5787 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
5789 /* We build this decl with vtbl_ptr_type_node, which is a
5790 `vtable_entry_type*'. It might seem more precise to use
5791 `vtable_entry_type (*)[N]' where N is the number of virtual
5792 functions. However, that would require the vtable pointer in
5793 base classes to have a different type than the vtable pointer
5794 in derived classes. We could make that happen, but that
5795 still wouldn't solve all the problems. In particular, the
5796 type-based alias analysis code would decide that assignments
5797 to the base class vtable pointer can't alias assignments to
5798 the derived class vtable pointer, since they have different
5799 types. Thus, in a derived class destructor, where the base
5800 class constructor was inlined, we could generate bad code for
5801 setting up the vtable pointer.
5803 Therefore, we use one type for all vtable pointers. We still
5804 use a type-correct type; it's just doesn't indicate the array
5805 bounds. That's better than using `void*' or some such; it's
5806 cleaner, and it let's the alias analysis code know that these
5807 stores cannot alias stores to void*! */
5810 field
= build_decl (input_location
,
5811 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
5812 DECL_VIRTUAL_P (field
) = 1;
5813 DECL_ARTIFICIAL (field
) = 1;
5814 DECL_FIELD_CONTEXT (field
) = t
;
5815 DECL_FCONTEXT (field
) = t
;
5816 if (TYPE_PACKED (t
))
5817 DECL_PACKED (field
) = 1;
5819 TYPE_VFIELD (t
) = field
;
5821 /* This class is non-empty. */
5822 CLASSTYPE_EMPTY_P (t
) = 0;
5830 /* Add OFFSET to all base types of BINFO which is a base in the
5831 hierarchy dominated by T.
5833 OFFSET, which is a type offset, is number of bytes. */
5836 propagate_binfo_offsets (tree binfo
, tree offset
)
5842 /* Update BINFO's offset. */
5843 BINFO_OFFSET (binfo
)
5844 = fold_convert (sizetype
,
5845 size_binop (PLUS_EXPR
,
5846 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
5849 /* Find the primary base class. */
5850 primary_binfo
= get_primary_binfo (binfo
);
5852 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
5853 propagate_binfo_offsets (primary_binfo
, offset
);
5855 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5857 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5859 /* Don't do the primary base twice. */
5860 if (base_binfo
== primary_binfo
)
5863 if (BINFO_VIRTUAL_P (base_binfo
))
5866 propagate_binfo_offsets (base_binfo
, offset
);
5870 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5871 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5872 empty subobjects of T. */
5875 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
5881 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
5884 /* Find the last field. The artificial fields created for virtual
5885 bases will go after the last extant field to date. */
5886 next_field
= &TYPE_FIELDS (t
);
5888 next_field
= &DECL_CHAIN (*next_field
);
5890 /* Go through the virtual bases, allocating space for each virtual
5891 base that is not already a primary base class. These are
5892 allocated in inheritance graph order. */
5893 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
5895 if (!BINFO_VIRTUAL_P (vbase
))
5898 if (!BINFO_PRIMARY_P (vbase
))
5900 /* This virtual base is not a primary base of any class in the
5901 hierarchy, so we have to add space for it. */
5902 next_field
= build_base_field (rli
, vbase
,
5903 offsets
, next_field
);
5908 /* Returns the offset of the byte just past the end of the base class
5912 end_of_base (tree binfo
)
5916 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
5917 size
= TYPE_SIZE_UNIT (char_type_node
);
5918 else if (is_empty_class (BINFO_TYPE (binfo
)))
5919 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5920 allocate some space for it. It cannot have virtual bases, so
5921 TYPE_SIZE_UNIT is fine. */
5922 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5924 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5926 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
5929 /* Returns the offset of the byte just past the end of the base class
5930 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5931 only non-virtual bases are included. If INCLUDE_FIELDS_P is true,
5932 then also consider non-static data members. */
5935 end_of_class (tree t
, bool include_virtuals_p
, bool include_fields_p
= false)
5937 tree result
= size_zero_node
;
5938 vec
<tree
, va_gc
> *vbases
;
5944 for (binfo
= TYPE_BINFO (t
), i
= 0;
5945 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5947 if (!include_virtuals_p
5948 && BINFO_VIRTUAL_P (base_binfo
)
5949 && (!BINFO_PRIMARY_P (base_binfo
)
5950 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
5953 offset
= end_of_base (base_binfo
);
5954 if (tree_int_cst_lt (result
, offset
))
5958 if (include_fields_p
)
5959 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5960 if (TREE_CODE (field
) == FIELD_DECL
)
5962 offset
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (field
),
5963 DECL_SIZE_UNIT (field
));
5964 if (tree_int_cst_lt (result
, offset
))
5968 if (include_virtuals_p
)
5969 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
5970 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
5972 offset
= end_of_base (base_binfo
);
5973 if (tree_int_cst_lt (result
, offset
))
5980 /* Warn about bases of T that are inaccessible because they are
5981 ambiguous. For example:
5984 struct T : public S {};
5985 struct U : public S, public T {};
5987 Here, `(S*) new U' is not allowed because there are two `S'
5991 warn_about_ambiguous_bases (tree t
)
5994 vec
<tree
, va_gc
> *vbases
;
5999 /* If there are no repeated bases, nothing can be ambiguous. */
6000 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6003 /* Check direct bases. */
6004 for (binfo
= TYPE_BINFO (t
), i
= 0;
6005 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6007 basetype
= BINFO_TYPE (base_binfo
);
6009 if (!uniquely_derived_from_p (basetype
, t
))
6010 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6014 /* Check for ambiguous virtual bases. */
6016 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6017 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6019 basetype
= BINFO_TYPE (binfo
);
6021 if (!uniquely_derived_from_p (basetype
, t
))
6022 warning (OPT_Wextra
, "virtual base %qT inaccessible in %qT due "
6023 "to ambiguity", basetype
, t
);
6027 /* Compare two INTEGER_CSTs K1 and K2. */
6030 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6032 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6035 /* Increase the size indicated in RLI to account for empty classes
6036 that are "off the end" of the class. */
6039 include_empty_classes (record_layout_info rli
)
6044 /* It might be the case that we grew the class to allocate a
6045 zero-sized base class. That won't be reflected in RLI, yet,
6046 because we are willing to overlay multiple bases at the same
6047 offset. However, now we need to make sure that RLI is big enough
6048 to reflect the entire class. */
6049 eoc
= end_of_class (rli
->t
, CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6050 rli_size
= rli_size_unit_so_far (rli
);
6051 if (TREE_CODE (rli_size
) == INTEGER_CST
6052 && tree_int_cst_lt (rli_size
, eoc
))
6054 /* The size should have been rounded to a whole byte. */
6055 gcc_assert (tree_int_cst_equal
6056 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6058 = size_binop (PLUS_EXPR
,
6060 size_binop (MULT_EXPR
,
6061 fold_convert (bitsizetype
,
6062 size_binop (MINUS_EXPR
,
6064 bitsize_int (BITS_PER_UNIT
)));
6065 normalize_rli (rli
);
6069 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6070 BINFO_OFFSETs for all of the base-classes. Position the vtable
6071 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6074 layout_class_type (tree t
, tree
*virtuals_p
)
6076 tree non_static_data_members
;
6079 record_layout_info rli
;
6080 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6081 types that appear at that offset. */
6082 splay_tree empty_base_offsets
;
6083 /* True if the last field laid out was a bit-field. */
6084 bool last_field_was_bitfield
= false;
6085 /* The location at which the next field should be inserted. */
6088 /* Keep track of the first non-static data member. */
6089 non_static_data_members
= TYPE_FIELDS (t
);
6091 /* Start laying out the record. */
6092 rli
= start_record_layout (t
);
6094 /* Mark all the primary bases in the hierarchy. */
6095 determine_primary_bases (t
);
6097 /* Create a pointer to our virtual function table. */
6098 vptr
= create_vtable_ptr (t
, virtuals_p
);
6100 /* The vptr is always the first thing in the class. */
6103 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6104 TYPE_FIELDS (t
) = vptr
;
6105 next_field
= &DECL_CHAIN (vptr
);
6106 place_field (rli
, vptr
);
6109 next_field
= &TYPE_FIELDS (t
);
6111 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6112 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6114 build_base_fields (rli
, empty_base_offsets
, next_field
);
6116 /* Layout the non-static data members. */
6117 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6122 /* We still pass things that aren't non-static data members to
6123 the back end, in case it wants to do something with them. */
6124 if (TREE_CODE (field
) != FIELD_DECL
)
6126 place_field (rli
, field
);
6127 /* If the static data member has incomplete type, keep track
6128 of it so that it can be completed later. (The handling
6129 of pending statics in finish_record_layout is
6130 insufficient; consider:
6133 struct S2 { static S1 s1; };
6135 At this point, finish_record_layout will be called, but
6136 S1 is still incomplete.) */
6139 maybe_register_incomplete_var (field
);
6140 /* The visibility of static data members is determined
6141 at their point of declaration, not their point of
6143 determine_visibility (field
);
6148 type
= TREE_TYPE (field
);
6149 if (type
== error_mark_node
)
6152 padding
= NULL_TREE
;
6154 bool might_overlap
= field_poverlapping_p (field
);
6156 if (might_overlap
&& CLASS_TYPE_P (type
)
6157 && CLASSTYPE_NON_LAYOUT_POD_P (type
))
6159 /* if D is a potentially-overlapping data member, update sizeof(C) to
6160 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6161 tree nvsize
= CLASSTYPE_SIZE_UNIT (type
);
6162 tree dsize
= end_of_class (type
, /*vbases*/true, /*fields*/true);
6163 if (tree_int_cst_le (dsize
, nvsize
))
6165 DECL_SIZE_UNIT (field
) = nvsize
;
6166 DECL_SIZE (field
) = CLASSTYPE_SIZE (type
);
6170 DECL_SIZE_UNIT (field
) = dsize
;
6171 DECL_SIZE (field
) = bit_from_pos (dsize
, bitsize_zero_node
);
6175 /* If this field is a bit-field whose width is greater than its
6176 type, then there are some special rules for allocating
6178 if (DECL_C_BIT_FIELD (field
)
6179 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6181 bool was_unnamed_p
= false;
6182 /* We must allocate the bits as if suitably aligned for the
6183 longest integer type that fits in this many bits. Then,
6184 we are supposed to use the left over bits as additional
6187 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6188 tree limit
= size_int (MAX_FIXED_MODE_SIZE
);
6189 if (tree_int_cst_lt (DECL_SIZE (field
), limit
))
6190 limit
= DECL_SIZE (field
);
6192 tree integer_type
= integer_types
[itk_char
];
6193 for (unsigned itk
= itk_char
; itk
!= itk_none
; itk
++)
6194 if (tree next
= integer_types
[itk
])
6196 if (tree_int_cst_lt (limit
, TYPE_SIZE (next
)))
6197 /* Too big, so our current guess is what we want. */
6199 /* Not bigger than limit, ok */
6200 integer_type
= next
;
6203 /* Figure out how much additional padding is required. */
6204 if (TREE_CODE (t
) == UNION_TYPE
)
6205 /* In a union, the padding field must have the full width
6206 of the bit-field; all fields start at offset zero. */
6207 padding
= DECL_SIZE (field
);
6209 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6210 TYPE_SIZE (integer_type
));
6212 if (integer_zerop (padding
))
6213 padding
= NULL_TREE
;
6215 /* An unnamed bitfield does not normally affect the
6216 alignment of the containing class on a target where
6217 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6218 make any exceptions for unnamed bitfields when the
6219 bitfields are longer than their types. Therefore, we
6220 temporarily give the field a name. */
6221 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6223 was_unnamed_p
= true;
6224 DECL_NAME (field
) = make_anon_name ();
6227 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6228 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6229 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6230 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6231 empty_base_offsets
);
6233 DECL_NAME (field
) = NULL_TREE
;
6234 /* Now that layout has been performed, set the size of the
6235 field to the size of its declared type; the rest of the
6236 field is effectively invisible. */
6237 DECL_SIZE (field
) = TYPE_SIZE (type
);
6238 /* We must also reset the DECL_MODE of the field. */
6239 SET_DECL_MODE (field
, TYPE_MODE (type
));
6241 else if (might_overlap
&& is_empty_class (type
))
6242 layout_empty_base_or_field (rli
, field
, empty_base_offsets
);
6244 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6245 empty_base_offsets
);
6247 /* Remember the location of any empty classes in FIELD. */
6248 record_subobject_offsets (field
, empty_base_offsets
);
6250 /* If a bit-field does not immediately follow another bit-field,
6251 and yet it starts in the middle of a byte, we have failed to
6252 comply with the ABI. */
6254 && DECL_C_BIT_FIELD (field
)
6255 /* The TREE_NO_WARNING flag gets set by Objective-C when
6256 laying out an Objective-C class. The ObjC ABI differs
6257 from the C++ ABI, and so we do not want a warning
6259 && !TREE_NO_WARNING (field
)
6260 && !last_field_was_bitfield
6261 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6262 DECL_FIELD_BIT_OFFSET (field
),
6263 bitsize_unit_node
)))
6264 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6265 "offset of %qD is not ABI-compliant and may "
6266 "change in a future version of GCC", field
);
6268 /* The middle end uses the type of expressions to determine the
6269 possible range of expression values. In order to optimize
6270 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6271 must be made aware of the width of "i", via its type.
6273 Because C++ does not have integer types of arbitrary width,
6274 we must (for the purposes of the front end) convert from the
6275 type assigned here to the declared type of the bitfield
6276 whenever a bitfield expression is used as an rvalue.
6277 Similarly, when assigning a value to a bitfield, the value
6278 must be converted to the type given the bitfield here. */
6279 if (DECL_C_BIT_FIELD (field
))
6281 unsigned HOST_WIDE_INT width
;
6282 tree ftype
= TREE_TYPE (field
);
6283 width
= tree_to_uhwi (DECL_SIZE (field
));
6284 if (width
!= TYPE_PRECISION (ftype
))
6287 = c_build_bitfield_integer_type (width
,
6288 TYPE_UNSIGNED (ftype
));
6290 = cp_build_qualified_type (TREE_TYPE (field
),
6291 cp_type_quals (ftype
));
6295 /* If we needed additional padding after this field, add it
6301 padding_field
= build_decl (input_location
,
6305 DECL_BIT_FIELD (padding_field
) = 1;
6306 DECL_SIZE (padding_field
) = padding
;
6307 DECL_CONTEXT (padding_field
) = t
;
6308 DECL_ARTIFICIAL (padding_field
) = 1;
6309 DECL_IGNORED_P (padding_field
) = 1;
6310 DECL_PADDING_P (padding_field
) = 1;
6311 layout_nonempty_base_or_field (rli
, padding_field
,
6313 empty_base_offsets
);
6316 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6319 if (!integer_zerop (rli
->bitpos
))
6321 /* Make sure that we are on a byte boundary so that the size of
6322 the class without virtual bases will always be a round number
6324 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6325 normalize_rli (rli
);
6328 /* Delete all zero-width bit-fields from the list of fields. Now
6329 that the type is laid out they are no longer important. */
6330 remove_zero_width_bit_fields (t
);
6332 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6334 /* T needs a different layout as a base (eliding virtual bases
6335 or whatever). Create that version. */
6336 tree base_t
= make_node (TREE_CODE (t
));
6338 /* If the ABI version is not at least two, and the last
6339 field was a bit-field, RLI may not be on a byte
6340 boundary. In particular, rli_size_unit_so_far might
6341 indicate the last complete byte, while rli_size_so_far
6342 indicates the total number of bits used. Therefore,
6343 rli_size_so_far, rather than rli_size_unit_so_far, is
6344 used to compute TYPE_SIZE_UNIT. */
6345 tree eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6346 TYPE_SIZE_UNIT (base_t
)
6347 = size_binop (MAX_EXPR
,
6348 fold_convert (sizetype
,
6349 size_binop (CEIL_DIV_EXPR
,
6350 rli_size_so_far (rli
),
6351 bitsize_int (BITS_PER_UNIT
))),
6354 = size_binop (MAX_EXPR
,
6355 rli_size_so_far (rli
),
6356 size_binop (MULT_EXPR
,
6357 fold_convert (bitsizetype
, eoc
),
6358 bitsize_int (BITS_PER_UNIT
)));
6359 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
6360 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6361 TYPE_TYPELESS_STORAGE (base_t
) = TYPE_TYPELESS_STORAGE (t
);
6363 /* Copy the non-static data members of T. This will include its
6364 direct non-virtual bases & vtable. */
6365 next_field
= &TYPE_FIELDS (base_t
);
6366 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6367 if (TREE_CODE (field
) == FIELD_DECL
)
6369 *next_field
= copy_node (field
);
6370 DECL_CONTEXT (*next_field
) = base_t
;
6371 next_field
= &DECL_CHAIN (*next_field
);
6373 *next_field
= NULL_TREE
;
6375 /* We use the base type for trivial assignments, and hence it
6377 compute_record_mode (base_t
);
6379 TYPE_CONTEXT (base_t
) = t
;
6381 /* Record the base version of the type. */
6382 CLASSTYPE_AS_BASE (t
) = base_t
;
6385 CLASSTYPE_AS_BASE (t
) = t
;
6387 /* Every empty class contains an empty class. */
6388 if (CLASSTYPE_EMPTY_P (t
))
6389 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6391 /* Set the TYPE_DECL for this type to contain the right
6392 value for DECL_OFFSET, so that we can use it as part
6393 of a COMPONENT_REF for multiple inheritance. */
6394 layout_decl (TYPE_MAIN_DECL (t
), 0);
6396 /* Now fix up any virtual base class types that we left lying
6397 around. We must get these done before we try to lay out the
6398 virtual function table. As a side-effect, this will remove the
6399 base subobject fields. */
6400 layout_virtual_bases (rli
, empty_base_offsets
);
6402 /* Make sure that empty classes are reflected in RLI at this
6404 include_empty_classes (rli
);
6406 /* Make sure not to create any structures with zero size. */
6407 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6409 build_decl (input_location
,
6410 FIELD_DECL
, NULL_TREE
, char_type_node
));
6412 /* If this is a non-POD, declaring it packed makes a difference to how it
6413 can be used as a field; don't let finalize_record_size undo it. */
6414 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6415 rli
->packed_maybe_necessary
= true;
6417 /* Let the back end lay out the type. */
6418 finish_record_layout (rli
, /*free_p=*/true);
6420 if (TYPE_SIZE_UNIT (t
)
6421 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6422 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6423 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6424 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
6426 /* Warn about bases that can't be talked about due to ambiguity. */
6427 warn_about_ambiguous_bases (t
);
6429 /* Now that we're done with layout, give the base fields the real types. */
6430 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6431 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6432 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6435 splay_tree_delete (empty_base_offsets
);
6437 if (CLASSTYPE_EMPTY_P (t
)
6438 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6439 TYPE_SIZE_UNIT (t
)))
6440 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6443 /* Determine the "key method" for the class type indicated by TYPE,
6444 and set CLASSTYPE_KEY_METHOD accordingly. */
6447 determine_key_method (tree type
)
6451 if (processing_template_decl
6452 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6453 || CLASSTYPE_INTERFACE_KNOWN (type
))
6456 /* The key method is the first non-pure virtual function that is not
6457 inline at the point of class definition. On some targets the
6458 key function may not be inline; those targets should not call
6459 this function until the end of the translation unit. */
6460 for (method
= TYPE_FIELDS (type
); method
; method
= DECL_CHAIN (method
))
6461 if (TREE_CODE (method
) == FUNCTION_DECL
6462 && DECL_VINDEX (method
) != NULL_TREE
6463 && ! DECL_DECLARED_INLINE_P (method
)
6464 && ! DECL_PURE_VIRTUAL_P (method
))
6466 CLASSTYPE_KEY_METHOD (type
) = method
;
6473 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6474 class data member of non-zero size, otherwise false. */
6477 field_nonempty_p (const_tree fld
)
6479 if (TREE_CODE (fld
) == ERROR_MARK
)
6482 tree type
= TREE_TYPE (fld
);
6483 if (TREE_CODE (fld
) == FIELD_DECL
6484 && TREE_CODE (type
) != ERROR_MARK
6485 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
6487 return TYPE_SIZE (type
)
6488 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
6489 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
6495 /* Used by find_flexarrays and related functions. */
6499 /* The first flexible array member or non-zero array member found
6500 in the order of layout. */
6502 /* First non-static non-empty data member in the class or its bases. */
6504 /* The first non-static non-empty data member following either
6505 the flexible array member, if found, or the zero-length array member
6506 otherwise. AFTER[1] refers to the first such data member of a union
6507 of which the struct containing the flexible array member or zero-length
6508 array is a member, or NULL when no such union exists. This element is
6509 only used during searching, not for diagnosing problems. AFTER[0]
6510 refers to the first such data member that is not a member of such
6514 /* Refers to a struct (not union) in which the struct of which the flexible
6515 array is member is defined. Used to diagnose strictly (according to C)
6516 invalid uses of the latter structs. */
6520 /* Find either the first flexible array member or the first zero-length
6521 array, in that order of preference, among members of class T (but not
6522 its base classes), and set members of FMEM accordingly.
6523 BASE_P is true if T is a base class of another class.
6524 PUN is set to the outermost union in which the flexible array member
6525 (or zero-length array) is defined if one such union exists, otherwise
6527 Similarly, PSTR is set to a data member of the outermost struct of
6528 which the flexible array is a member if one such struct exists,
6529 otherwise to NULL. */
6532 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
6533 tree pun
/* = NULL_TREE */,
6534 tree pstr
/* = NULL_TREE */)
6536 /* Set the "pointer" to the outermost enclosing union if not set
6537 yet and maintain it for the remainder of the recursion. */
6538 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
6541 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
6543 if (fld
== error_mark_node
)
6546 /* Is FLD a typedef for an anonymous struct? */
6548 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6549 handled elsewhere so that errors like the following are detected
6551 typedef struct { int i, a[], j; } S; // bug c++/72753
6552 S s [2]; // bug c++/68489
6554 if (TREE_CODE (fld
) == TYPE_DECL
6555 && DECL_IMPLICIT_TYPEDEF_P (fld
)
6556 && CLASS_TYPE_P (TREE_TYPE (fld
))
6557 && anon_aggrname_p (DECL_NAME (fld
)))
6559 /* Check the nested unnamed type referenced via a typedef
6560 independently of FMEM (since it's not a data member of
6561 the enclosing class). */
6562 check_flexarrays (TREE_TYPE (fld
));
6566 /* Skip anything that's GCC-generated or not a (non-static) data
6568 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
6571 /* Type of the member. */
6572 tree fldtype
= TREE_TYPE (fld
);
6573 if (fldtype
== error_mark_node
)
6576 /* Determine the type of the array element or object referenced
6577 by the member so that it can be checked for flexible array
6578 members if it hasn't been yet. */
6579 tree eltype
= fldtype
;
6580 while (TREE_CODE (eltype
) == ARRAY_TYPE
6581 || INDIRECT_TYPE_P (eltype
))
6582 eltype
= TREE_TYPE (eltype
);
6584 if (RECORD_OR_UNION_TYPE_P (eltype
))
6586 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6588 /* Once the member after the flexible array has been found
6590 fmem
->after
[bool (pun
)] = fld
;
6594 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
6596 /* Descend into the non-static member struct or union and try
6597 to find a flexible array member or zero-length array among
6598 its members. This is only necessary for anonymous types
6599 and types in whose context the current type T has not been
6600 defined (the latter must not be checked again because they
6601 are already in the process of being checked by one of the
6602 recursive calls). */
6604 tree first
= fmem
->first
;
6605 tree array
= fmem
->array
;
6607 /* If this member isn't anonymous and a prior non-flexible array
6608 member has been seen in one of the enclosing structs, clear
6609 the FIRST member since it doesn't contribute to the flexible
6610 array struct's members. */
6611 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
6612 fmem
->first
= NULL_TREE
;
6614 find_flexarrays (eltype
, fmem
, false, pun
,
6615 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
6617 if (fmem
->array
!= array
)
6620 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
6622 /* Restore the FIRST member reset above if no flexible
6623 array member has been found in this member's struct. */
6624 fmem
->first
= first
;
6627 /* If the member struct contains the first flexible array
6628 member, or if this member is a base class, continue to
6629 the next member and avoid setting the FMEM->NEXT pointer
6636 if (field_nonempty_p (fld
))
6638 /* Remember the first non-static data member. */
6642 /* Remember the first non-static data member after the flexible
6643 array member, if one has been found, or the zero-length array
6644 if it has been found. */
6645 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6646 fmem
->after
[bool (pun
)] = fld
;
6649 /* Skip non-arrays. */
6650 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
6653 /* Determine the upper bound of the array if it has one. */
6654 if (TYPE_DOMAIN (fldtype
))
6658 /* Make a record of the zero-length array if either one
6659 such field or a flexible array member has been seen to
6660 handle the pathological and unlikely case of multiple
6662 if (!fmem
->after
[bool (pun
)])
6663 fmem
->after
[bool (pun
)] = fld
;
6665 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
6667 /* Remember the first zero-length array unless a flexible array
6668 member has already been seen. */
6670 fmem
->enclosing
= pstr
;
6675 /* Flexible array members have no upper bound. */
6678 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
6680 /* Replace the zero-length array if it's been stored and
6681 reset the after pointer. */
6682 fmem
->after
[bool (pun
)] = NULL_TREE
;
6684 fmem
->enclosing
= pstr
;
6686 else if (!fmem
->after
[bool (pun
)])
6687 /* Make a record of another flexible array member. */
6688 fmem
->after
[bool (pun
)] = fld
;
6693 fmem
->enclosing
= pstr
;
6699 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6700 a flexible array member (or the zero-length array extension). */
6703 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
6705 if (fmem
->array
&& fmem
->enclosing
)
6707 auto_diagnostic_group d
;
6708 if (pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
6709 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
6710 ? G_("invalid use of %q#T with a zero-size array "
6712 : G_("invalid use of %q#T with a flexible array member "
6714 DECL_CONTEXT (fmem
->array
),
6715 DECL_CONTEXT (fmem
->enclosing
)))
6716 inform (DECL_SOURCE_LOCATION (fmem
->array
),
6717 "array member %q#D declared here", fmem
->array
);
6721 /* Issue diagnostics for invalid flexible array members or zero-length
6722 arrays that are not the last elements of the containing class or its
6723 base classes or that are its sole members. */
6726 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
6731 if (fmem
->first
&& !fmem
->after
[0])
6733 diagnose_invalid_flexarray (fmem
);
6737 /* Has a diagnostic been issued? */
6740 const char *msg
= 0;
6742 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
6745 msg
= G_("zero-size array member %qD not at end of %q#T");
6746 else if (!fmem
->first
)
6747 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
6751 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
6753 auto_diagnostic_group d
;
6754 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
6756 inform (location_of (t
), "in the definition of %q#T", t
);
6764 msg
= G_("flexible array member %qD not at end of %q#T");
6765 else if (!fmem
->first
)
6766 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
6770 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
6773 auto_diagnostic_group d
;
6774 error_at (loc
, msg
, fmem
->array
, t
);
6776 /* In the unlikely event that the member following the flexible
6777 array member is declared in a different class, or the member
6778 overlaps another member of a common union, point to it.
6779 Otherwise it should be obvious. */
6781 && ((DECL_CONTEXT (fmem
->after
[0])
6782 != DECL_CONTEXT (fmem
->array
))))
6784 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
6785 "next member %q#D declared here",
6787 inform (location_of (t
), "in the definition of %q#T", t
);
6792 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
6793 diagnose_invalid_flexarray (fmem
);
6797 /* Recursively check to make sure that any flexible array or zero-length
6798 array members of class T or its bases are valid (i.e., not the sole
6799 non-static data member of T and, if one exists, that it is the last
6800 non-static data member of T and its base classes. FMEM is expected
6801 to be initially null and is used internally by recursive calls to
6802 the function. Issue the appropriate diagnostics for the array member
6803 that fails the checks. */
6806 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
6807 bool base_p
/* = false */)
6809 /* Initialize the result of a search for flexible array and zero-length
6810 array members. Avoid doing any work if the most interesting FMEM data
6811 have already been populated. */
6812 flexmems_t flexmems
= flexmems_t ();
6815 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
6818 tree fam
= fmem
->array
;
6820 /* Recursively check the primary base class first. */
6821 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6823 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
6824 check_flexarrays (basetype
, fmem
, true);
6827 /* Recursively check the base classes. */
6828 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
6829 for (int i
= 0; i
< nbases
; ++i
)
6831 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
6833 /* The primary base class was already checked above. */
6834 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
6837 /* Virtual base classes are at the end. */
6838 if (BINFO_VIRTUAL_P (base_binfo
))
6841 /* Check the base class. */
6842 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
6845 if (fmem
== &flexmems
)
6847 /* Check virtual base classes only once per derived class.
6848 I.e., this check is not performed recursively for base
6852 vec
<tree
, va_gc
> *vbases
;
6853 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6854 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6856 /* Check the virtual base class. */
6857 tree basetype
= TREE_TYPE (base_binfo
);
6859 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
6863 /* Is the type unnamed (and therefore a member of it potentially
6864 an anonymous struct or union)? */
6865 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
6867 /* Search the members of the current (possibly derived) class, skipping
6868 unnamed structs and unions since those could be anonymous. */
6869 if (fmem
!= &flexmems
|| !maybe_anon_p
)
6870 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
6872 if (fmem
== &flexmems
&& !maybe_anon_p
)
6874 /* Issue diagnostics for invalid flexible and zero-length array
6875 members found in base classes or among the members of the current
6876 class. Ignore anonymous structs and unions whose members are
6877 considered to be members of the enclosing class and thus will
6878 be diagnosed when checking it. */
6879 diagnose_flexarrays (t
, fmem
);
6883 /* Perform processing required when the definition of T (a class type)
6884 is complete. Diagnose invalid definitions of flexible array members
6885 and zero-size arrays. */
6888 finish_struct_1 (tree t
)
6891 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6892 tree virtuals
= NULL_TREE
;
6894 if (COMPLETE_TYPE_P (t
))
6896 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
6897 error ("redefinition of %q#T", t
);
6902 /* If this type was previously laid out as a forward reference,
6903 make sure we lay it out again. */
6904 TYPE_SIZE (t
) = NULL_TREE
;
6905 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
6907 /* Make assumptions about the class; we'll reset the flags if
6909 CLASSTYPE_EMPTY_P (t
) = 1;
6910 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
6911 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
6912 CLASSTYPE_LITERAL_P (t
) = true;
6914 /* Do end-of-class semantic processing: checking the validity of the
6915 bases and members and add implicitly generated methods. */
6916 check_bases_and_members (t
);
6918 /* Find the key method. */
6919 if (TYPE_CONTAINS_VPTR_P (t
))
6921 /* The Itanium C++ ABI permits the key method to be chosen when
6922 the class is defined -- even though the key method so
6923 selected may later turn out to be an inline function. On
6924 some systems (such as ARM Symbian OS) the key method cannot
6925 be determined until the end of the translation unit. On such
6926 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6927 will cause the class to be added to KEYED_CLASSES. Then, in
6928 finish_file we will determine the key method. */
6929 if (targetm
.cxx
.key_method_may_be_inline ())
6930 determine_key_method (t
);
6932 /* If a polymorphic class has no key method, we may emit the vtable
6933 in every translation unit where the class definition appears. If
6934 we're devirtualizing, we can look into the vtable even if we
6935 aren't emitting it. */
6936 if (!CLASSTYPE_KEY_METHOD (t
))
6937 vec_safe_push (keyed_classes
, t
);
6940 /* Layout the class itself. */
6941 layout_class_type (t
, &virtuals
);
6942 /* COMPLETE_TYPE_P is now true. */
6944 set_class_bindings (t
);
6946 /* With the layout complete, check for flexible array members and
6947 zero-length arrays that might overlap other members in the final
6949 check_flexarrays (t
);
6951 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
6953 /* If necessary, create the primary vtable for this class. */
6954 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
6956 /* We must enter these virtuals into the table. */
6957 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6958 build_primary_vtable (NULL_TREE
, t
);
6959 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
6960 /* Here we know enough to change the type of our virtual
6961 function table, but we will wait until later this function. */
6962 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
6964 /* If we're warning about ABI tags, check the types of the new
6965 virtual functions. */
6967 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
6968 check_abi_tags (t
, TREE_VALUE (v
));
6971 if (TYPE_CONTAINS_VPTR_P (t
))
6976 if (BINFO_VTABLE (TYPE_BINFO (t
)))
6977 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
6978 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6979 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
6981 /* Add entries for virtual functions introduced by this class. */
6982 BINFO_VIRTUALS (TYPE_BINFO (t
))
6983 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
6985 /* Set DECL_VINDEX for all functions declared in this class. */
6986 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
6988 fn
= TREE_CHAIN (fn
),
6989 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
6990 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
6992 tree fndecl
= BV_FN (fn
);
6994 if (DECL_THUNK_P (fndecl
))
6995 /* A thunk. We should never be calling this entry directly
6996 from this vtable -- we'd use the entry for the non
6997 thunk base function. */
6998 DECL_VINDEX (fndecl
) = NULL_TREE
;
6999 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7000 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7004 finish_struct_bits (t
);
7006 set_method_tm_attributes (t
);
7007 if (flag_openmp
|| flag_openmp_simd
)
7008 finish_omp_declare_simd_methods (t
);
7010 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7011 for any static member objects of the type we're working on. */
7012 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7013 if (DECL_DECLARES_FUNCTION_P (x
))
7014 DECL_IN_AGGR_P (x
) = false;
7015 else if (VAR_P (x
) && TREE_STATIC (x
)
7016 && TREE_TYPE (x
) != error_mark_node
7017 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7018 SET_DECL_MODE (x
, TYPE_MODE (t
));
7020 /* Complain if one of the field types requires lower visibility. */
7021 constrain_class_visibility (t
);
7023 /* Make the rtl for any new vtables we have created, and unmark
7024 the base types we marked. */
7027 /* Build the VTT for T. */
7031 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7032 && !CLASSTYPE_FINAL (t
))
7033 warning (OPT_Wnon_virtual_dtor
,
7034 "%q#T has virtual functions and accessible"
7035 " non-virtual destructor", t
);
7039 if (warn_overloaded_virtual
)
7042 /* Class layout, assignment of virtual table slots, etc., is now
7043 complete. Give the back end a chance to tweak the visibility of
7044 the class or perform any other required target modifications. */
7045 targetm
.cxx
.adjust_class_at_definition (t
);
7047 maybe_suppress_debug_info (t
);
7049 if (flag_vtable_verify
)
7050 vtv_save_class_info (t
);
7052 dump_class_hierarchy (t
);
7054 /* Finish debugging output for this type. */
7055 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7057 if (TYPE_TRANSPARENT_AGGR (t
))
7059 tree field
= first_field (t
);
7060 if (field
== NULL_TREE
|| error_operand_p (field
))
7062 error ("type transparent %q#T does not have any fields", t
);
7063 TYPE_TRANSPARENT_AGGR (t
) = 0;
7065 else if (DECL_ARTIFICIAL (field
))
7067 if (DECL_FIELD_IS_BASE (field
))
7068 error ("type transparent class %qT has base classes", t
);
7071 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7072 error ("type transparent class %qT has virtual functions", t
);
7074 TYPE_TRANSPARENT_AGGR (t
) = 0;
7076 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7078 error ("type transparent %q#T cannot be made transparent because "
7079 "the type of the first field has a different ABI from the "
7080 "class overall", t
);
7081 TYPE_TRANSPARENT_AGGR (t
) = 0;
7086 /* When T was built up, the member declarations were added in reverse
7087 order. Rearrange them to declaration order. */
7090 unreverse_member_declarations (tree t
)
7096 /* The following lists are all in reverse order. Put them in
7097 declaration order now. */
7098 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7100 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7101 order, so we can't just use nreverse. Due to stat_hack
7102 chicanery in finish_member_declaration. */
7104 for (x
= TYPE_FIELDS (t
);
7105 x
&& TREE_CODE (x
) != TYPE_DECL
;
7108 next
= DECL_CHAIN (x
);
7109 DECL_CHAIN (x
) = prev
;
7115 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7116 TYPE_FIELDS (t
) = prev
;
7121 finish_struct (tree t
, tree attributes
)
7123 location_t saved_loc
= input_location
;
7125 /* Now that we've got all the field declarations, reverse everything
7127 unreverse_member_declarations (t
);
7129 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7130 fixup_attribute_variants (t
);
7132 /* Nadger the current location so that diagnostics point to the start of
7133 the struct, not the end. */
7134 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7136 if (processing_template_decl
)
7140 /* We need to add the target functions of USING_DECLS, so that
7141 they can be found when the using declaration is not
7142 instantiated yet. */
7143 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7144 if (TREE_CODE (x
) == USING_DECL
)
7146 tree fn
= strip_using_decl (x
);
7148 for (lkp_iterator
iter (fn
); iter
; ++iter
)
7149 add_method (t
, *iter
, true);
7151 else if (DECL_DECLARES_FUNCTION_P (x
))
7152 DECL_IN_AGGR_P (x
) = false;
7154 /* Also add a USING_DECL for operator=. We know there'll be (at
7155 least) one, but we don't know the signature(s). We want name
7156 lookup not to fail or recurse into bases. This isn't added
7157 to the template decl list so we drop this at instantiation
7159 tree ass_op
= build_lang_decl (USING_DECL
, assign_op_identifier
,
7161 DECL_CONTEXT (ass_op
) = t
;
7162 USING_DECL_SCOPE (ass_op
) = t
;
7163 DECL_DEPENDENT_P (ass_op
) = true;
7164 DECL_ARTIFICIAL (ass_op
) = true;
7165 DECL_CHAIN (ass_op
) = TYPE_FIELDS (t
);
7166 TYPE_FIELDS (t
) = ass_op
;
7168 TYPE_SIZE (t
) = bitsize_zero_node
;
7169 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7170 /* COMPLETE_TYPE_P is now true. */
7172 set_class_bindings (t
);
7174 /* We need to emit an error message if this type was used as a parameter
7175 and it is an abstract type, even if it is a template. We construct
7176 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7177 account and we call complete_vars with this type, which will check
7178 the PARM_DECLS. Note that while the type is being defined,
7179 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7180 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7181 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7182 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7183 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_PURE_VIRTUAL_P (x
))
7184 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7187 /* Remember current #pragma pack value. */
7188 TYPE_PRECISION (t
) = maximum_field_alignment
;
7190 /* Fix up any variants we've already built. */
7191 for (x
= TYPE_NEXT_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
7193 TYPE_SIZE (x
) = TYPE_SIZE (t
);
7194 TYPE_SIZE_UNIT (x
) = TYPE_SIZE_UNIT (t
);
7195 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
7199 finish_struct_1 (t
);
7200 /* COMPLETE_TYPE_P is now true. */
7202 maybe_warn_about_overly_private_class (t
);
7204 if (is_std_init_list (t
))
7206 /* People keep complaining that the compiler crashes on an invalid
7207 definition of initializer_list, so I guess we should explicitly
7208 reject it. What the compiler internals care about is that it's a
7209 template and has a pointer field followed by size_type field. */
7211 if (processing_template_decl
)
7213 tree f
= next_initializable_field (TYPE_FIELDS (t
));
7214 if (f
&& TYPE_PTR_P (TREE_TYPE (f
)))
7216 f
= next_initializable_field (DECL_CHAIN (f
));
7217 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7222 fatal_error (input_location
, "definition of %qD does not match "
7223 "%<#include <initializer_list>%>", TYPE_NAME (t
));
7226 input_location
= saved_loc
;
7228 TYPE_BEING_DEFINED (t
) = 0;
7230 if (current_class_type
)
7233 error ("trying to finish struct, but kicked out due to previous parse errors");
7235 if (processing_template_decl
&& at_function_scope_p ()
7236 /* Lambdas are defined by the LAMBDA_EXPR. */
7237 && !LAMBDA_TYPE_P (t
))
7238 add_stmt (build_min (TAG_DEFN
, t
));
7243 /* Hash table to avoid endless recursion when handling references. */
7244 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7246 /* Return the dynamic type of INSTANCE, if known.
7247 Used to determine whether the virtual function table is needed
7250 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7251 of our knowledge of its type. *NONNULL should be initialized
7252 before this function is called. */
7255 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7257 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7259 switch (TREE_CODE (instance
))
7262 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
7265 return RECUR (TREE_OPERAND (instance
, 0));
7268 /* This is a call to a constructor, hence it's never zero. */
7269 if (CALL_EXPR_FN (instance
)
7270 && TREE_HAS_CONSTRUCTOR (instance
))
7274 return TREE_TYPE (instance
);
7279 /* This is a call to a constructor, hence it's never zero. */
7280 if (TREE_HAS_CONSTRUCTOR (instance
))
7284 return TREE_TYPE (instance
);
7286 return RECUR (TREE_OPERAND (instance
, 0));
7288 case POINTER_PLUS_EXPR
:
7291 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
7292 return RECUR (TREE_OPERAND (instance
, 0));
7293 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7294 /* Propagate nonnull. */
7295 return RECUR (TREE_OPERAND (instance
, 0));
7300 return RECUR (TREE_OPERAND (instance
, 0));
7303 instance
= TREE_OPERAND (instance
, 0);
7306 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7307 with a real object -- given &p->f, p can still be null. */
7308 tree t
= get_base_address (instance
);
7309 /* ??? Probably should check DECL_WEAK here. */
7310 if (t
&& DECL_P (t
))
7313 return RECUR (instance
);
7316 /* If this component is really a base class reference, then the field
7317 itself isn't definitive. */
7318 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
7319 return RECUR (TREE_OPERAND (instance
, 0));
7320 return RECUR (TREE_OPERAND (instance
, 1));
7324 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
7325 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7329 return TREE_TYPE (TREE_TYPE (instance
));
7335 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7339 return TREE_TYPE (instance
);
7341 else if (instance
== current_class_ptr
)
7346 /* if we're in a ctor or dtor, we know our type. If
7347 current_class_ptr is set but we aren't in a function, we're in
7348 an NSDMI (and therefore a constructor). */
7349 if (current_scope () != current_function_decl
7350 || (DECL_LANG_SPECIFIC (current_function_decl
)
7351 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7352 || DECL_DESTRUCTOR_P (current_function_decl
))))
7356 return TREE_TYPE (TREE_TYPE (instance
));
7359 else if (TYPE_REF_P (TREE_TYPE (instance
)))
7361 /* We only need one hash table because it is always left empty. */
7362 if (!fixed_type_or_null_ref_ht
)
7363 fixed_type_or_null_ref_ht
7364 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7366 /* Reference variables should be references to objects. */
7370 /* Enter the INSTANCE in a table to prevent recursion; a
7371 variable's initializer may refer to the variable
7373 if (VAR_P (instance
)
7374 && DECL_INITIAL (instance
)
7375 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7376 && !fixed_type_or_null_ref_ht
->find (instance
))
7381 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7383 type
= RECUR (DECL_INITIAL (instance
));
7384 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7391 case VIEW_CONVERT_EXPR
:
7392 if (location_wrapper_p (instance
))
7393 return RECUR (TREE_OPERAND (instance
, 0));
7395 /* TODO: Recursion may be correct for some non-location-wrapper
7396 uses of VIEW_CONVERT_EXPR. */
7405 /* Return nonzero if the dynamic type of INSTANCE is known, and
7406 equivalent to the static type. We also handle the case where
7407 INSTANCE is really a pointer. Return negative if this is a
7408 ctor/dtor. There the dynamic type is known, but this might not be
7409 the most derived base of the original object, and hence virtual
7410 bases may not be laid out according to this type.
7412 Used to determine whether the virtual function table is needed
7415 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7416 of our knowledge of its type. *NONNULL should be initialized
7417 before this function is called. */
7420 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7422 tree t
= TREE_TYPE (instance
);
7426 /* processing_template_decl can be false in a template if we're in
7427 instantiate_non_dependent_expr, but we still want to suppress
7429 if (in_template_function ())
7431 /* In a template we only care about the type of the result. */
7437 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7438 if (fixed
== NULL_TREE
)
7440 if (INDIRECT_TYPE_P (t
))
7442 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7444 return cdtorp
? -1 : 1;
7449 init_class_processing (void)
7451 current_class_depth
= 0;
7452 current_class_stack_size
= 10;
7454 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7455 sizeof_biggest_empty_class
= size_zero_node
;
7457 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7458 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7459 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7462 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7465 restore_class_cache (void)
7469 /* We are re-entering the same class we just left, so we don't
7470 have to search the whole inheritance matrix to find all the
7471 decls to bind again. Instead, we install the cached
7472 class_shadowed list and walk through it binding names. */
7473 push_binding_level (previous_class_level
);
7474 class_binding_level
= previous_class_level
;
7475 /* Restore IDENTIFIER_TYPE_VALUE. */
7476 for (type
= class_binding_level
->type_shadowed
;
7478 type
= TREE_CHAIN (type
))
7479 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7482 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7483 appropriate for TYPE.
7485 So that we may avoid calls to lookup_name, we cache the _TYPE
7486 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7488 For multiple inheritance, we perform a two-pass depth-first search
7489 of the type lattice. */
7492 pushclass (tree type
)
7494 class_stack_node_t csn
;
7496 type
= TYPE_MAIN_VARIANT (type
);
7498 /* Make sure there is enough room for the new entry on the stack. */
7499 if (current_class_depth
+ 1 >= current_class_stack_size
)
7501 current_class_stack_size
*= 2;
7503 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7504 current_class_stack_size
);
7507 /* Insert a new entry on the class stack. */
7508 csn
= current_class_stack
+ current_class_depth
;
7509 csn
->name
= current_class_name
;
7510 csn
->type
= current_class_type
;
7511 csn
->access
= current_access_specifier
;
7512 csn
->names_used
= 0;
7514 current_class_depth
++;
7516 /* Now set up the new type. */
7517 current_class_name
= TYPE_NAME (type
);
7518 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7519 current_class_name
= DECL_NAME (current_class_name
);
7520 current_class_type
= type
;
7522 /* By default, things in classes are private, while things in
7523 structures or unions are public. */
7524 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7525 ? access_private_node
7526 : access_public_node
);
7528 if (previous_class_level
7529 && type
!= previous_class_level
->this_entity
7530 && current_class_depth
== 1)
7532 /* Forcibly remove any old class remnants. */
7533 invalidate_class_lookup_cache ();
7536 if (!previous_class_level
7537 || type
!= previous_class_level
->this_entity
7538 || current_class_depth
> 1)
7541 restore_class_cache ();
7544 /* When we exit a toplevel class scope, we save its binding level so
7545 that we can restore it quickly. Here, we've entered some other
7546 class, so we must invalidate our cache. */
7549 invalidate_class_lookup_cache (void)
7551 previous_class_level
= NULL
;
7554 /* Get out of the current class scope. If we were in a class scope
7555 previously, that is the one popped to. */
7562 current_class_depth
--;
7563 current_class_name
= current_class_stack
[current_class_depth
].name
;
7564 current_class_type
= current_class_stack
[current_class_depth
].type
;
7565 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7566 if (current_class_stack
[current_class_depth
].names_used
)
7567 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
7570 /* Mark the top of the class stack as hidden. */
7573 push_class_stack (void)
7575 if (current_class_depth
)
7576 ++current_class_stack
[current_class_depth
- 1].hidden
;
7579 /* Mark the top of the class stack as un-hidden. */
7582 pop_class_stack (void)
7584 if (current_class_depth
)
7585 --current_class_stack
[current_class_depth
- 1].hidden
;
7588 /* If the class type currently being defined is either T or
7589 a nested type of T, returns the type from the current_class_stack,
7590 which might be equivalent to but not equal to T in case of
7591 constrained partial specializations. */
7594 currently_open_class (tree t
)
7598 if (!CLASS_TYPE_P (t
))
7601 t
= TYPE_MAIN_VARIANT (t
);
7603 /* We start looking from 1 because entry 0 is from global scope,
7605 for (i
= current_class_depth
; i
> 0; --i
)
7608 if (i
== current_class_depth
)
7609 c
= current_class_type
;
7612 if (current_class_stack
[i
].hidden
)
7614 c
= current_class_stack
[i
].type
;
7618 if (same_type_p (c
, t
))
7624 /* If either current_class_type or one of its enclosing classes are derived
7625 from T, return the appropriate type. Used to determine how we found
7626 something via unqualified lookup. */
7629 currently_open_derived_class (tree t
)
7633 /* The bases of a dependent type are unknown. */
7634 if (dependent_type_p (t
))
7637 if (!current_class_type
)
7640 if (DERIVED_FROM_P (t
, current_class_type
))
7641 return current_class_type
;
7643 for (i
= current_class_depth
- 1; i
> 0; --i
)
7645 if (current_class_stack
[i
].hidden
)
7647 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
7648 return current_class_stack
[i
].type
;
7654 /* Return the outermost enclosing class type that is still open, or
7658 outermost_open_class (void)
7660 if (!current_class_type
)
7663 if (TYPE_BEING_DEFINED (current_class_type
))
7664 r
= current_class_type
;
7665 for (int i
= current_class_depth
- 1; i
> 0; --i
)
7667 if (current_class_stack
[i
].hidden
)
7669 tree t
= current_class_stack
[i
].type
;
7670 if (!TYPE_BEING_DEFINED (t
))
7677 /* Returns the innermost class type which is not a lambda closure type. */
7680 current_nonlambda_class_type (void)
7682 tree type
= current_class_type
;
7683 while (type
&& LAMBDA_TYPE_P (type
))
7684 type
= decl_type_context (TYPE_NAME (type
));
7688 /* When entering a class scope, all enclosing class scopes' names with
7689 static meaning (static variables, static functions, types and
7690 enumerators) have to be visible. This recursive function calls
7691 pushclass for all enclosing class contexts until global or a local
7692 scope is reached. TYPE is the enclosed class. */
7695 push_nested_class (tree type
)
7697 /* A namespace might be passed in error cases, like A::B:C. */
7698 if (type
== NULL_TREE
7699 || !CLASS_TYPE_P (type
))
7702 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
7707 /* Undoes a push_nested_class call. */
7710 pop_nested_class (void)
7712 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
7715 if (context
&& CLASS_TYPE_P (context
))
7716 pop_nested_class ();
7719 /* Returns the number of extern "LANG" blocks we are nested within. */
7722 current_lang_depth (void)
7724 return vec_safe_length (current_lang_base
);
7727 /* Set global variables CURRENT_LANG_NAME to appropriate value
7728 so that behavior of name-mangling machinery is correct. */
7731 push_lang_context (tree name
)
7733 vec_safe_push (current_lang_base
, current_lang_name
);
7735 if (name
== lang_name_cplusplus
)
7736 current_lang_name
= name
;
7737 else if (name
== lang_name_c
)
7738 current_lang_name
= name
;
7740 error ("language string %<\"%E\"%> not recognized", name
);
7743 /* Get out of the current language scope. */
7746 pop_lang_context (void)
7748 current_lang_name
= current_lang_base
->pop ();
7751 /* Type instantiation routines. */
7753 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7754 matches the TARGET_TYPE. If there is no satisfactory match, return
7755 error_mark_node, and issue an error & warning messages under
7756 control of FLAGS. Permit pointers to member function if FLAGS
7757 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7758 a template-id, and EXPLICIT_TARGS are the explicitly provided
7761 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7762 is the base path used to reference those member functions. If
7763 the address is resolved to a member function, access checks will be
7764 performed and errors issued if appropriate. */
7767 resolve_address_of_overloaded_function (tree target_type
,
7769 tsubst_flags_t complain
,
7771 tree explicit_targs
,
7774 /* Here's what the standard says:
7778 If the name is a function template, template argument deduction
7779 is done, and if the argument deduction succeeds, the deduced
7780 arguments are used to generate a single template function, which
7781 is added to the set of overloaded functions considered.
7783 Non-member functions and static member functions match targets of
7784 type "pointer-to-function" or "reference-to-function." Nonstatic
7785 member functions match targets of type "pointer-to-member
7786 function;" the function type of the pointer to member is used to
7787 select the member function from the set of overloaded member
7788 functions. If a nonstatic member function is selected, the
7789 reference to the overloaded function name is required to have the
7790 form of a pointer to member as described in 5.3.1.
7792 If more than one function is selected, any template functions in
7793 the set are eliminated if the set also contains a non-template
7794 function, and any given template function is eliminated if the
7795 set contains a second template function that is more specialized
7796 than the first according to the partial ordering rules 14.5.5.2.
7797 After such eliminations, if any, there shall remain exactly one
7798 selected function. */
7801 /* We store the matches in a TREE_LIST rooted here. The functions
7802 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7803 interoperability with most_specialized_instantiation. */
7804 tree matches
= NULL_TREE
;
7806 tree target_fn_type
;
7808 /* By the time we get here, we should be seeing only real
7809 pointer-to-member types, not the internal POINTER_TYPE to
7810 METHOD_TYPE representation. */
7811 gcc_assert (!TYPE_PTR_P (target_type
)
7812 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
7814 gcc_assert (is_overloaded_fn (overload
));
7816 /* Check that the TARGET_TYPE is reasonable. */
7817 if (TYPE_PTRFN_P (target_type
)
7818 || TYPE_REFFN_P (target_type
))
7820 else if (TYPE_PTRMEMFUNC_P (target_type
))
7821 /* This is OK, too. */
7823 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
7824 /* This is OK, too. This comes from a conversion to reference
7826 target_type
= build_reference_type (target_type
);
7829 if (complain
& tf_error
)
7830 error ("cannot resolve overloaded function %qD based on"
7831 " conversion to type %qT",
7832 OVL_NAME (overload
), target_type
);
7833 return error_mark_node
;
7836 /* Non-member functions and static member functions match targets of type
7837 "pointer-to-function" or "reference-to-function." Nonstatic member
7838 functions match targets of type "pointer-to-member-function;" the
7839 function type of the pointer to member is used to select the member
7840 function from the set of overloaded member functions.
7842 So figure out the FUNCTION_TYPE that we want to match against. */
7843 target_fn_type
= static_fn_type (target_type
);
7845 /* If we can find a non-template function that matches, we can just
7846 use it. There's no point in generating template instantiations
7847 if we're just going to throw them out anyhow. But, of course, we
7848 can only do this when we don't *need* a template function. */
7850 for (lkp_iterator
iter (overload
); iter
; ++iter
)
7854 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
7855 /* We're not looking for templates just yet. */
7858 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
) != is_ptrmem
)
7859 /* We're looking for a non-static member, and this isn't
7860 one, or vice versa. */
7863 /* In C++17 we need the noexcept-qualifier to compare types. */
7864 if (flag_noexcept_type
7865 && !maybe_instantiate_noexcept (fn
, complain
))
7868 /* See if there's a match. */
7869 tree fntype
= static_fn_type (fn
);
7870 if (same_type_p (target_fn_type
, fntype
)
7871 || fnptr_conv_p (target_fn_type
, fntype
))
7872 matches
= tree_cons (fn
, NULL_TREE
, matches
);
7875 /* Now, if we've already got a match (or matches), there's no need
7876 to proceed to the template functions. But, if we don't have a
7877 match we need to look at them, too. */
7880 tree target_arg_types
;
7881 tree target_ret_type
;
7883 unsigned int nargs
, ia
;
7886 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
7887 target_ret_type
= TREE_TYPE (target_fn_type
);
7889 nargs
= list_length (target_arg_types
);
7890 args
= XALLOCAVEC (tree
, nargs
);
7891 for (arg
= target_arg_types
, ia
= 0;
7892 arg
!= NULL_TREE
&& arg
!= void_list_node
;
7893 arg
= TREE_CHAIN (arg
), ++ia
)
7894 args
[ia
] = TREE_VALUE (arg
);
7897 for (lkp_iterator
iter (overload
); iter
; ++iter
)
7903 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7904 /* We're only looking for templates. */
7907 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
7909 /* We're not looking for a non-static member, and this is
7910 one, or vice versa. */
7913 tree ret
= target_ret_type
;
7915 /* If the template has a deduced return type, don't expose it to
7916 template argument deduction. */
7917 if (undeduced_auto_decl (fn
))
7920 /* Try to do argument deduction. */
7921 targs
= make_tree_vec (DECL_NTPARMS (fn
));
7922 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
7924 DEDUCE_EXACT
, LOOKUP_NORMAL
,
7925 NULL
, false, false);
7926 if (instantiation
== error_mark_node
)
7927 /* Instantiation failed. */
7930 /* Constraints must be satisfied. This is done before
7931 return type deduction since that instantiates the
7933 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
7936 /* And now force instantiation to do return type deduction. */
7937 if (undeduced_auto_decl (instantiation
))
7940 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
7943 require_deduced_type (instantiation
);
7946 /* In C++17 we need the noexcept-qualifier to compare types. */
7947 if (flag_noexcept_type
)
7948 maybe_instantiate_noexcept (instantiation
, complain
);
7950 /* See if there's a match. */
7951 tree fntype
= static_fn_type (instantiation
);
7952 if (same_type_p (target_fn_type
, fntype
)
7953 || fnptr_conv_p (target_fn_type
, fntype
))
7954 matches
= tree_cons (instantiation
, fn
, matches
);
7957 /* Now, remove all but the most specialized of the matches. */
7960 tree match
= most_specialized_instantiation (matches
);
7962 if (match
!= error_mark_node
)
7963 matches
= tree_cons (TREE_PURPOSE (match
),
7969 /* Now we should have exactly one function in MATCHES. */
7970 if (matches
== NULL_TREE
)
7972 /* There were *no* matches. */
7973 if (complain
& tf_error
)
7975 error ("no matches converting function %qD to type %q#T",
7976 OVL_NAME (overload
), target_type
);
7978 print_candidates (overload
);
7980 return error_mark_node
;
7982 else if (TREE_CHAIN (matches
))
7984 /* There were too many matches. First check if they're all
7985 the same function. */
7986 tree match
= NULL_TREE
;
7988 fn
= TREE_PURPOSE (matches
);
7990 /* For multi-versioned functions, more than one match is just fine and
7991 decls_match will return false as they are different. */
7992 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
7993 if (!decls_match (fn
, TREE_PURPOSE (match
))
7994 && !targetm
.target_option
.function_versions
7995 (fn
, TREE_PURPOSE (match
)))
8000 if (complain
& tf_error
)
8002 error ("converting overloaded function %qD to type %q#T is ambiguous",
8003 OVL_NAME (overload
), target_type
);
8005 /* Since print_candidates expects the functions in the
8006 TREE_VALUE slot, we flip them here. */
8007 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8008 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8010 print_candidates (matches
);
8013 return error_mark_node
;
8017 /* Good, exactly one match. Now, convert it to the correct type. */
8018 fn
= TREE_PURPOSE (matches
);
8020 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8021 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8023 static int explained
;
8025 if (!(complain
& tf_error
))
8026 return error_mark_node
;
8028 auto_diagnostic_group d
;
8029 if (permerror (input_location
, "assuming pointer to member %qD", fn
)
8032 inform (input_location
, "(a pointer to member can only be "
8033 "formed with %<&%E%>)", fn
);
8038 /* If a pointer to a function that is multi-versioned is requested, the
8039 pointer to the dispatcher function is returned instead. This works
8040 well because indirectly calling the function will dispatch the right
8041 function version at run-time. */
8042 if (DECL_FUNCTION_VERSIONED (fn
))
8044 fn
= get_function_version_dispatcher (fn
);
8046 return error_mark_node
;
8047 /* Mark all the versions corresponding to the dispatcher as used. */
8048 if (!(complain
& tf_conv
))
8049 mark_versions_used (fn
);
8052 /* If we're doing overload resolution purely for the purpose of
8053 determining conversion sequences, we should not consider the
8054 function used. If this conversion sequence is selected, the
8055 function will be marked as used at this point. */
8056 if (!(complain
& tf_conv
))
8058 /* Make =delete work with SFINAE. */
8059 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8060 return error_mark_node
;
8061 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8062 return error_mark_node
;
8065 /* We could not check access to member functions when this
8066 expression was originally created since we did not know at that
8067 time to which function the expression referred. */
8068 if (DECL_FUNCTION_MEMBER_P (fn
))
8070 gcc_assert (access_path
);
8071 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8074 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8075 return cp_build_addr_expr (fn
, complain
);
8078 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8079 will mark the function as addressed, but here we must do it
8081 cxx_mark_addressable (fn
);
8087 /* This function will instantiate the type of the expression given in
8088 RHS to match the type of LHSTYPE. If errors exist, then return
8089 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8090 we complain on errors. If we are not complaining, never modify rhs,
8091 as overload resolution wants to try many possible instantiations, in
8092 the hope that at least one will work.
8094 For non-recursive calls, LHSTYPE should be a function, pointer to
8095 function, or a pointer to member function. */
8098 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8100 tsubst_flags_t complain_in
= complain
;
8101 tree access_path
= NULL_TREE
;
8103 complain
&= ~tf_ptrmem_ok
;
8105 if (lhstype
== unknown_type_node
)
8107 if (complain
& tf_error
)
8108 error ("not enough type information");
8109 return error_mark_node
;
8112 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8114 tree fntype
= non_reference (lhstype
);
8115 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8117 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8119 if (flag_ms_extensions
8120 && TYPE_PTRMEMFUNC_P (fntype
)
8121 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8122 /* Microsoft allows `A::f' to be resolved to a
8123 pointer-to-member. */
8127 if (complain
& tf_error
)
8128 error ("cannot convert %qE from type %qT to type %qT",
8129 rhs
, TREE_TYPE (rhs
), fntype
);
8130 return error_mark_node
;
8134 /* If we instantiate a template, and it is a A ?: C expression
8135 with omitted B, look through the SAVE_EXPR. */
8136 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8137 rhs
= TREE_OPERAND (rhs
, 0);
8139 if (BASELINK_P (rhs
))
8141 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8142 rhs
= BASELINK_FUNCTIONS (rhs
);
8145 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8146 deduce any type information. */
8147 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
8149 if (complain
& tf_error
)
8150 error ("not enough type information");
8151 return error_mark_node
;
8154 /* There are only a few kinds of expressions that may have a type
8155 dependent on overload resolution. */
8156 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
8157 || TREE_CODE (rhs
) == COMPONENT_REF
8158 || is_overloaded_fn (rhs
)
8159 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8161 /* This should really only be used when attempting to distinguish
8162 what sort of a pointer to function we have. For now, any
8163 arithmetic operation which is not supported on pointers
8164 is rejected as an error. */
8166 switch (TREE_CODE (rhs
))
8170 tree member
= TREE_OPERAND (rhs
, 1);
8172 member
= instantiate_type (lhstype
, member
, complain
);
8173 if (member
!= error_mark_node
8174 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8175 /* Do not lose object's side effects. */
8176 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8177 TREE_OPERAND (rhs
, 0), member
);
8182 rhs
= TREE_OPERAND (rhs
, 1);
8183 if (BASELINK_P (rhs
))
8184 return instantiate_type (lhstype
, rhs
, complain_in
);
8186 /* This can happen if we are forming a pointer-to-member for a
8188 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8192 case TEMPLATE_ID_EXPR
:
8194 tree fns
= TREE_OPERAND (rhs
, 0);
8195 tree args
= TREE_OPERAND (rhs
, 1);
8198 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8199 /*template_only=*/true,
8206 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8207 /*template_only=*/false,
8208 /*explicit_targs=*/NULL_TREE
,
8213 if (PTRMEM_OK_P (rhs
))
8214 complain
|= tf_ptrmem_ok
;
8216 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8220 return error_mark_node
;
8225 return error_mark_node
;
8228 /* Return the name of the virtual function pointer field
8229 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8230 this may have to look back through base types to find the
8231 ultimate field name. (For single inheritance, these could
8232 all be the same name. Who knows for multiple inheritance). */
8235 get_vfield_name (tree type
)
8237 tree binfo
, base_binfo
;
8239 for (binfo
= TYPE_BINFO (type
);
8240 BINFO_N_BASE_BINFOS (binfo
);
8243 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8245 if (BINFO_VIRTUAL_P (base_binfo
)
8246 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8250 type
= BINFO_TYPE (binfo
);
8251 tree ctor_name
= constructor_name (type
);
8252 char *buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8253 + IDENTIFIER_LENGTH (ctor_name
) + 2);
8254 sprintf (buf
, VFIELD_NAME_FORMAT
, IDENTIFIER_POINTER (ctor_name
));
8255 return get_identifier (buf
);
8258 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8259 according to [class]:
8260 The class-name is also inserted
8261 into the scope of the class itself. For purposes of access checking,
8262 the inserted class name is treated as if it were a public member name. */
8265 build_self_reference (void)
8267 tree name
= DECL_NAME (TYPE_NAME (current_class_type
));
8268 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8270 DECL_NONLOCAL (value
) = 1;
8271 DECL_CONTEXT (value
) = current_class_type
;
8272 DECL_ARTIFICIAL (value
) = 1;
8273 SET_DECL_SELF_REFERENCE_P (value
);
8274 set_underlying_type (value
);
8276 if (processing_template_decl
)
8277 value
= push_template_decl (value
);
8279 tree saved_cas
= current_access_specifier
;
8280 current_access_specifier
= access_public_node
;
8281 finish_member_declaration (value
);
8282 current_access_specifier
= saved_cas
;
8285 /* Returns 1 if TYPE contains only padding bytes. */
8288 is_empty_class (tree type
)
8290 if (type
== error_mark_node
)
8293 if (! CLASS_TYPE_P (type
))
8296 return CLASSTYPE_EMPTY_P (type
);
8299 /* Returns true if TYPE contains no actual data, just various
8300 possible combinations of empty classes and possibly a vptr. */
8303 is_really_empty_class (tree type
)
8305 if (CLASS_TYPE_P (type
))
8312 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8313 out, but we'd like to be able to check this before then. */
8314 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8317 for (binfo
= TYPE_BINFO (type
), i
= 0;
8318 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8319 if (!is_really_empty_class (BINFO_TYPE (base_binfo
)))
8321 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8322 if (TREE_CODE (field
) == FIELD_DECL
8323 && !DECL_ARTIFICIAL (field
)
8324 /* An unnamed bit-field is not a data member. */
8325 && !DECL_UNNAMED_BIT_FIELD (field
)
8326 && !is_really_empty_class (TREE_TYPE (field
)))
8330 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8331 return (integer_zerop (array_type_nelts_top (type
))
8332 || is_really_empty_class (TREE_TYPE (type
)));
8336 /* Note that NAME was looked up while the current class was being
8337 defined and that the result of that lookup was DECL. */
8340 maybe_note_name_used_in_class (tree name
, tree decl
)
8342 splay_tree names_used
;
8344 /* If we're not defining a class, there's nothing to do. */
8345 if (!(innermost_scope_kind() == sk_class
8346 && TYPE_BEING_DEFINED (current_class_type
)
8347 && !LAMBDA_TYPE_P (current_class_type
)))
8350 /* If there's already a binding for this NAME, then we don't have
8351 anything to worry about. */
8352 if (lookup_member (current_class_type
, name
,
8353 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8356 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8357 current_class_stack
[current_class_depth
- 1].names_used
8358 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8359 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8361 splay_tree_insert (names_used
,
8362 (splay_tree_key
) name
,
8363 (splay_tree_value
) decl
);
8366 /* Note that NAME was declared (as DECL) in the current class. Check
8367 to see that the declaration is valid. */
8370 note_name_declared_in_class (tree name
, tree decl
)
8372 splay_tree names_used
;
8375 /* Look to see if we ever used this name. */
8377 = current_class_stack
[current_class_depth
- 1].names_used
;
8380 /* The C language allows members to be declared with a type of the same
8381 name, and the C++ standard says this diagnostic is not required. So
8382 allow it in extern "C" blocks unless predantic is specified.
8383 Allow it in all cases if -ms-extensions is specified. */
8384 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8385 || flag_ms_extensions
)
8387 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8390 /* [basic.scope.class]
8392 A name N used in a class S shall refer to the same declaration
8393 in its context and when re-evaluated in the completed scope of
8395 if (permerror (location_of (decl
),
8396 "declaration of %q#D changes meaning of %qD",
8397 decl
, OVL_NAME (decl
)))
8398 inform (location_of ((tree
) n
->value
),
8399 "%qD declared here as %q#D",
8400 OVL_NAME (decl
), (tree
) n
->value
);
8404 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8405 Secondary vtables are merged with primary vtables; this function
8406 will return the VAR_DECL for the primary vtable. */
8409 get_vtbl_decl_for_binfo (tree binfo
)
8413 decl
= BINFO_VTABLE (binfo
);
8414 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8416 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
8417 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8420 gcc_assert (VAR_P (decl
));
8425 /* Returns the binfo for the primary base of BINFO. If the resulting
8426 BINFO is a virtual base, and it is inherited elsewhere in the
8427 hierarchy, then the returned binfo might not be the primary base of
8428 BINFO in the complete object. Check BINFO_PRIMARY_P or
8429 BINFO_LOST_PRIMARY_P to be sure. */
8432 get_primary_binfo (tree binfo
)
8436 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8440 return copied_binfo (primary_base
, binfo
);
8443 /* As above, but iterate until we reach the binfo that actually provides the
8447 most_primary_binfo (tree binfo
)
8450 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
8451 && !BINFO_LOST_PRIMARY_P (b
))
8453 tree primary_base
= get_primary_binfo (b
);
8454 gcc_assert (BINFO_PRIMARY_P (primary_base
)
8455 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
8461 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8462 type. Note that the virtual inheritance might be above or below BINFO in
8466 vptr_via_virtual_p (tree binfo
)
8469 binfo
= TYPE_BINFO (binfo
);
8470 tree primary
= most_primary_binfo (binfo
);
8471 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8472 a morally virtual base. */
8473 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
8474 return virt
!= NULL_TREE
;
8477 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8480 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8483 fprintf (stream
, "%*s", indent
, "");
8487 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8488 INDENT should be zero when called from the top level; it is
8489 incremented recursively. IGO indicates the next expected BINFO in
8490 inheritance graph ordering. */
8493 dump_class_hierarchy_r (FILE *stream
,
8503 indented
= maybe_indent_hierarchy (stream
, indent
, 0);
8504 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8505 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8506 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8509 fprintf (stream
, "alternative-path\n");
8512 igo
= TREE_CHAIN (binfo
);
8514 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8515 tree_to_shwi (BINFO_OFFSET (binfo
)));
8516 if (is_empty_class (BINFO_TYPE (binfo
)))
8517 fprintf (stream
, " empty");
8518 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8519 fprintf (stream
, " nearly-empty");
8520 if (BINFO_VIRTUAL_P (binfo
))
8521 fprintf (stream
, " virtual");
8522 fprintf (stream
, "\n");
8525 if (BINFO_PRIMARY_P (binfo
))
8527 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8528 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8529 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8530 TFF_PLAIN_IDENTIFIER
),
8531 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8533 if (BINFO_LOST_PRIMARY_P (binfo
))
8535 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8536 fprintf (stream
, " lost-primary");
8539 fprintf (stream
, "\n");
8541 if (!(flags
& TDF_SLIM
))
8545 if (BINFO_SUBVTT_INDEX (binfo
))
8547 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8548 fprintf (stream
, " subvttidx=%s",
8549 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8550 TFF_PLAIN_IDENTIFIER
));
8552 if (BINFO_VPTR_INDEX (binfo
))
8554 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8555 fprintf (stream
, " vptridx=%s",
8556 expr_as_string (BINFO_VPTR_INDEX (binfo
),
8557 TFF_PLAIN_IDENTIFIER
));
8559 if (BINFO_VPTR_FIELD (binfo
))
8561 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8562 fprintf (stream
, " vbaseoffset=%s",
8563 expr_as_string (BINFO_VPTR_FIELD (binfo
),
8564 TFF_PLAIN_IDENTIFIER
));
8566 if (BINFO_VTABLE (binfo
))
8568 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8569 fprintf (stream
, " vptr=%s",
8570 expr_as_string (BINFO_VTABLE (binfo
),
8571 TFF_PLAIN_IDENTIFIER
));
8575 fprintf (stream
, "\n");
8578 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
8579 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
8584 /* Dump the BINFO hierarchy for T. */
8587 dump_class_hierarchy_1 (FILE *stream
, dump_flags_t flags
, tree t
)
8589 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8590 fprintf (stream
, " size=%lu align=%lu\n",
8591 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
8592 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
8593 fprintf (stream
, " base size=%lu base align=%lu\n",
8594 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t
)))
8596 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t
))
8598 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
8599 fprintf (stream
, "\n");
8602 /* Debug interface to hierarchy dumping. */
8605 debug_class (tree t
)
8607 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
8611 dump_class_hierarchy (tree t
)
8614 if (FILE *stream
= dump_begin (class_dump_id
, &flags
))
8616 dump_class_hierarchy_1 (stream
, flags
, t
);
8617 dump_end (class_dump_id
, stream
);
8622 dump_array (FILE * stream
, tree decl
)
8625 unsigned HOST_WIDE_INT ix
;
8627 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
8629 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
8631 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
8632 fprintf (stream
, " %s entries",
8633 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
8634 TFF_PLAIN_IDENTIFIER
));
8635 fprintf (stream
, "\n");
8637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
8639 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
8640 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
8644 dump_vtable (tree t
, tree binfo
, tree vtable
)
8647 FILE *stream
= dump_begin (class_dump_id
, &flags
);
8652 if (!(flags
& TDF_SLIM
))
8654 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
8656 fprintf (stream
, "%s for %s",
8657 ctor_vtbl_p
? "Construction vtable" : "Vtable",
8658 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
8661 if (!BINFO_VIRTUAL_P (binfo
))
8662 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
8663 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8664 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8666 fprintf (stream
, "\n");
8667 dump_array (stream
, vtable
);
8668 fprintf (stream
, "\n");
8671 dump_end (class_dump_id
, stream
);
8675 dump_vtt (tree t
, tree vtt
)
8678 FILE *stream
= dump_begin (class_dump_id
, &flags
);
8683 if (!(flags
& TDF_SLIM
))
8685 fprintf (stream
, "VTT for %s\n",
8686 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8687 dump_array (stream
, vtt
);
8688 fprintf (stream
, "\n");
8691 dump_end (class_dump_id
, stream
);
8694 /* Dump a function or thunk and its thunkees. */
8697 dump_thunk (FILE *stream
, int indent
, tree thunk
)
8699 static const char spaces
[] = " ";
8700 tree name
= DECL_NAME (thunk
);
8703 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
8705 !DECL_THUNK_P (thunk
) ? "function"
8706 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
8707 name
? IDENTIFIER_POINTER (name
) : "<unset>");
8708 if (DECL_THUNK_P (thunk
))
8710 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
8711 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
8713 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
8714 if (!virtual_adjust
)
8716 else if (DECL_THIS_THUNK_P (thunk
))
8717 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
8718 tree_to_shwi (virtual_adjust
));
8720 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
8721 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
8722 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
8723 if (THUNK_ALIAS (thunk
))
8724 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
8726 fprintf (stream
, "\n");
8727 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
8728 dump_thunk (stream
, indent
+ 2, thunks
);
8731 /* Dump the thunks for FN. */
8734 debug_thunks (tree fn
)
8736 dump_thunk (stderr
, 0, fn
);
8739 /* Virtual function table initialization. */
8741 /* Create all the necessary vtables for T and its base classes. */
8744 finish_vtbls (tree t
)
8747 vec
<constructor_elt
, va_gc
> *v
= NULL
;
8748 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
8750 /* We lay out the primary and secondary vtables in one contiguous
8751 vtable. The primary vtable is first, followed by the non-virtual
8752 secondary vtables in inheritance graph order. */
8753 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
8756 /* Then come the virtual bases, also in inheritance graph order. */
8757 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
8759 if (!BINFO_VIRTUAL_P (vbase
))
8761 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
8764 if (BINFO_VTABLE (TYPE_BINFO (t
)))
8765 initialize_vtable (TYPE_BINFO (t
), v
);
8768 /* Initialize the vtable for BINFO with the INITS. */
8771 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
8775 layout_vtable_decl (binfo
, vec_safe_length (inits
));
8776 decl
= get_vtbl_decl_for_binfo (binfo
);
8777 initialize_artificial_var (decl
, inits
);
8778 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
8781 /* Build the VTT (virtual table table) for T.
8782 A class requires a VTT if it has virtual bases.
8785 1 - primary virtual pointer for complete object T
8786 2 - secondary VTTs for each direct non-virtual base of T which requires a
8788 3 - secondary virtual pointers for each direct or indirect base of T which
8789 has virtual bases or is reachable via a virtual path from T.
8790 4 - secondary VTTs for each direct or indirect virtual base of T.
8792 Secondary VTTs look like complete object VTTs without part 4. */
8800 vec
<constructor_elt
, va_gc
> *inits
;
8802 /* Build up the initializers for the VTT. */
8804 index
= size_zero_node
;
8805 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
8807 /* If we didn't need a VTT, we're done. */
8811 /* Figure out the type of the VTT. */
8812 type
= build_array_of_n_type (const_ptr_type_node
,
8815 /* Now, build the VTT object itself. */
8816 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
8817 initialize_artificial_var (vtt
, inits
);
8818 /* Add the VTT to the vtables list. */
8819 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
8820 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
8825 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8826 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8827 and CHAIN the vtable pointer for this binfo after construction is
8828 complete. VALUE can also be another BINFO, in which case we recurse. */
8831 binfo_ctor_vtable (tree binfo
)
8837 vt
= BINFO_VTABLE (binfo
);
8838 if (TREE_CODE (vt
) == TREE_LIST
)
8839 vt
= TREE_VALUE (vt
);
8840 if (TREE_CODE (vt
) == TREE_BINFO
)
8849 /* Data for secondary VTT initialization. */
8850 struct secondary_vptr_vtt_init_data
8852 /* Is this the primary VTT? */
8855 /* Current index into the VTT. */
8858 /* Vector of initializers built up. */
8859 vec
<constructor_elt
, va_gc
> *inits
;
8861 /* The type being constructed by this secondary VTT. */
8862 tree type_being_constructed
;
8865 /* Recursively build the VTT-initializer for BINFO (which is in the
8866 hierarchy dominated by T). INITS points to the end of the initializer
8867 list to date. INDEX is the VTT index where the next element will be
8868 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8869 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8870 for virtual bases of T. When it is not so, we build the constructor
8871 vtables for the BINFO-in-T variant. */
8874 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
8880 secondary_vptr_vtt_init_data data
;
8881 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
8883 /* We only need VTTs for subobjects with virtual bases. */
8884 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
8887 /* We need to use a construction vtable if this is not the primary
8891 build_ctor_vtbl_group (binfo
, t
);
8893 /* Record the offset in the VTT where this sub-VTT can be found. */
8894 BINFO_SUBVTT_INDEX (binfo
) = *index
;
8897 /* Add the address of the primary vtable for the complete object. */
8898 init
= binfo_ctor_vtable (binfo
);
8899 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
8902 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8903 BINFO_VPTR_INDEX (binfo
) = *index
;
8905 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
8907 /* Recursively add the secondary VTTs for non-virtual bases. */
8908 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
8909 if (!BINFO_VIRTUAL_P (b
))
8910 build_vtt_inits (b
, t
, inits
, index
);
8912 /* Add secondary virtual pointers for all subobjects of BINFO with
8913 either virtual bases or reachable along a virtual path, except
8914 subobjects that are non-virtual primary bases. */
8915 data
.top_level_p
= top_level_p
;
8916 data
.index
= *index
;
8917 data
.inits
= *inits
;
8918 data
.type_being_constructed
= BINFO_TYPE (binfo
);
8920 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
8922 *index
= data
.index
;
8924 /* data.inits might have grown as we added secondary virtual pointers.
8925 Make sure our caller knows about the new vector. */
8926 *inits
= data
.inits
;
8929 /* Add the secondary VTTs for virtual bases in inheritance graph
8931 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
8933 if (!BINFO_VIRTUAL_P (b
))
8936 build_vtt_inits (b
, t
, inits
, index
);
8939 /* Remove the ctor vtables we created. */
8940 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
8943 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8944 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8947 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
8949 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
8951 /* We don't care about bases that don't have vtables. */
8952 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
8953 return dfs_skip_bases
;
8955 /* We're only interested in proper subobjects of the type being
8957 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
8960 /* We're only interested in bases with virtual bases or reachable
8961 via a virtual path from the type being constructed. */
8962 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
8963 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
8964 return dfs_skip_bases
;
8966 /* We're not interested in non-virtual primary bases. */
8967 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
8970 /* Record the index where this secondary vptr can be found. */
8971 if (data
->top_level_p
)
8973 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8974 BINFO_VPTR_INDEX (binfo
) = data
->index
;
8976 if (BINFO_VIRTUAL_P (binfo
))
8978 /* It's a primary virtual base, and this is not a
8979 construction vtable. Find the base this is primary of in
8980 the inheritance graph, and use that base's vtable
8982 while (BINFO_PRIMARY_P (binfo
))
8983 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
8987 /* Add the initializer for the secondary vptr itself. */
8988 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
8990 /* Advance the vtt index. */
8991 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
8992 TYPE_SIZE_UNIT (ptr_type_node
));
8997 /* Called from build_vtt_inits via dfs_walk. After building
8998 constructor vtables and generating the sub-vtt from them, we need
8999 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9000 binfo of the base whose sub vtt was generated. */
9003 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9005 tree vtable
= BINFO_VTABLE (binfo
);
9007 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9008 /* If this class has no vtable, none of its bases do. */
9009 return dfs_skip_bases
;
9012 /* This might be a primary base, so have no vtable in this
9016 /* If we scribbled the construction vtable vptr into BINFO, clear it
9018 if (TREE_CODE (vtable
) == TREE_LIST
9019 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9020 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9025 /* Build the construction vtable group for BINFO which is in the
9026 hierarchy dominated by T. */
9029 build_ctor_vtbl_group (tree binfo
, tree t
)
9035 vec
<constructor_elt
, va_gc
> *v
;
9037 /* See if we've already created this construction vtable group. */
9038 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9039 if (get_global_binding (id
))
9042 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9043 /* Build a version of VTBL (with the wrong type) for use in
9044 constructing the addresses of secondary vtables in the
9045 construction vtable group. */
9046 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9047 DECL_CONSTRUCTION_VTABLE_P (vtbl
) = 1;
9048 /* Don't export construction vtables from shared libraries. Even on
9049 targets that don't support hidden visibility, this tells
9050 can_refer_decl_in_current_unit_p not to assume that it's safe to
9051 access from a different compilation unit (bz 54314). */
9052 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9053 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9056 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9057 binfo
, vtbl
, t
, &v
);
9059 /* Add the vtables for each of our virtual bases using the vbase in T
9061 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9063 vbase
= TREE_CHAIN (vbase
))
9067 if (!BINFO_VIRTUAL_P (vbase
))
9069 b
= copied_binfo (vbase
, binfo
);
9071 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9074 /* Figure out the type of the construction vtable. */
9075 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9077 TREE_TYPE (vtbl
) = type
;
9078 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9079 layout_decl (vtbl
, 0);
9081 /* Initialize the construction vtable. */
9082 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9083 initialize_artificial_var (vtbl
, v
);
9084 dump_vtable (t
, binfo
, vtbl
);
9087 /* Add the vtbl initializers for BINFO (and its bases other than
9088 non-virtual primaries) to the list of INITS. BINFO is in the
9089 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9090 the constructor the vtbl inits should be accumulated for. (If this
9091 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9092 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9093 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9094 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9095 but are not necessarily the same in terms of layout. */
9098 accumulate_vtbl_inits (tree binfo
,
9103 vec
<constructor_elt
, va_gc
> **inits
)
9107 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9109 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9111 /* If it doesn't have a vptr, we don't do anything. */
9112 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9115 /* If we're building a construction vtable, we're not interested in
9116 subobjects that don't require construction vtables. */
9118 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9119 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9122 /* Build the initializers for the BINFO-in-T vtable. */
9123 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9125 /* Walk the BINFO and its bases. We walk in preorder so that as we
9126 initialize each vtable we can figure out at what offset the
9127 secondary vtable lies from the primary vtable. We can't use
9128 dfs_walk here because we need to iterate through bases of BINFO
9129 and RTTI_BINFO simultaneously. */
9130 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9132 /* Skip virtual bases. */
9133 if (BINFO_VIRTUAL_P (base_binfo
))
9135 accumulate_vtbl_inits (base_binfo
,
9136 BINFO_BASE_BINFO (orig_binfo
, i
),
9137 rtti_binfo
, vtbl
, t
,
9142 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9143 BINFO vtable to L. */
9146 dfs_accumulate_vtbl_inits (tree binfo
,
9151 vec
<constructor_elt
, va_gc
> **l
)
9153 tree vtbl
= NULL_TREE
;
9154 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9158 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9160 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9161 primary virtual base. If it is not the same primary in
9162 the hierarchy of T, we'll need to generate a ctor vtable
9163 for it, to place at its location in T. If it is the same
9164 primary, we still need a VTT entry for the vtable, but it
9165 should point to the ctor vtable for the base it is a
9166 primary for within the sub-hierarchy of RTTI_BINFO.
9168 There are three possible cases:
9170 1) We are in the same place.
9171 2) We are a primary base within a lost primary virtual base of
9173 3) We are primary to something not a base of RTTI_BINFO. */
9176 tree last
= NULL_TREE
;
9178 /* First, look through the bases we are primary to for RTTI_BINFO
9179 or a virtual base. */
9181 while (BINFO_PRIMARY_P (b
))
9183 b
= BINFO_INHERITANCE_CHAIN (b
);
9185 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9188 /* If we run out of primary links, keep looking down our
9189 inheritance chain; we might be an indirect primary. */
9190 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9191 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9195 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9196 base B and it is a base of RTTI_BINFO, this is case 2. In
9197 either case, we share our vtable with LAST, i.e. the
9198 derived-most base within B of which we are a primary. */
9200 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9201 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9202 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9203 binfo_ctor_vtable after everything's been set up. */
9206 /* Otherwise, this is case 3 and we get our own. */
9208 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9211 n_inits
= vec_safe_length (*l
);
9218 /* Add the initializer for this vtable. */
9219 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9220 &non_fn_entries
, l
);
9222 /* Figure out the position to which the VPTR should point. */
9223 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9224 index
= size_binop (MULT_EXPR
,
9225 TYPE_SIZE_UNIT (vtable_entry_type
),
9226 size_int (non_fn_entries
+ n_inits
));
9227 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9231 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9232 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9233 straighten this out. */
9234 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9235 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9236 /* Throw away any unneeded intializers. */
9237 (*l
)->truncate (n_inits
);
9239 /* For an ordinary vtable, set BINFO_VTABLE. */
9240 BINFO_VTABLE (binfo
) = vtbl
;
9243 static GTY(()) tree abort_fndecl_addr
;
9244 static GTY(()) tree dvirt_fn
;
9246 /* Construct the initializer for BINFO's virtual function table. BINFO
9247 is part of the hierarchy dominated by T. If we're building a
9248 construction vtable, the ORIG_BINFO is the binfo we should use to
9249 find the actual function pointers to put in the vtable - but they
9250 can be overridden on the path to most-derived in the graph that
9251 ORIG_BINFO belongs. Otherwise,
9252 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9253 BINFO that should be indicated by the RTTI information in the
9254 vtable; it will be a base class of T, rather than T itself, if we
9255 are building a construction vtable.
9257 The value returned is a TREE_LIST suitable for wrapping in a
9258 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9259 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9260 number of non-function entries in the vtable.
9262 It might seem that this function should never be called with a
9263 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9264 base is always subsumed by a derived class vtable. However, when
9265 we are building construction vtables, we do build vtables for
9266 primary bases; we need these while the primary base is being
9270 build_vtbl_initializer (tree binfo
,
9274 int* non_fn_entries_p
,
9275 vec
<constructor_elt
, va_gc
> **inits
)
9281 vec
<tree
, va_gc
> *vbases
;
9284 /* Initialize VID. */
9285 memset (&vid
, 0, sizeof (vid
));
9288 vid
.rtti_binfo
= rtti_binfo
;
9289 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9290 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9291 vid
.generate_vcall_entries
= true;
9292 /* The first vbase or vcall offset is at index -3 in the vtable. */
9293 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
9295 /* Add entries to the vtable for RTTI. */
9296 build_rtti_vtbl_entries (binfo
, &vid
);
9298 /* Create an array for keeping track of the functions we've
9299 processed. When we see multiple functions with the same
9300 signature, we share the vcall offsets. */
9301 vec_alloc (vid
.fns
, 32);
9302 /* Add the vcall and vbase offset entries. */
9303 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
9305 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9306 build_vbase_offset_vtbl_entries. */
9307 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
9308 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
9309 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
9311 /* If the target requires padding between data entries, add that now. */
9312 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
9314 int n_entries
= vec_safe_length (vid
.inits
);
9316 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
);
9318 /* Move data entries into their new positions and add padding
9319 after the new positions. Iterate backwards so we don't
9320 overwrite entries that we would need to process later. */
9321 for (ix
= n_entries
- 1;
9322 vid
.inits
->iterate (ix
, &e
);
9326 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
9327 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
9329 (*vid
.inits
)[new_position
] = *e
;
9331 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
9333 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
9334 f
->index
= NULL_TREE
;
9335 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9341 if (non_fn_entries_p
)
9342 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9344 /* The initializers for virtual functions were built up in reverse
9345 order. Straighten them out and add them to the running list in one
9347 jx
= vec_safe_length (*inits
);
9348 vec_safe_grow (*inits
, jx
+ vid
.inits
->length ());
9350 for (ix
= vid
.inits
->length () - 1;
9351 vid
.inits
->iterate (ix
, &e
);
9355 /* Go through all the ordinary virtual functions, building up
9357 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9361 tree fn
, fn_original
;
9362 tree init
= NULL_TREE
;
9366 if (DECL_THUNK_P (fn
))
9368 if (!DECL_NAME (fn
))
9370 if (THUNK_ALIAS (fn
))
9372 fn
= THUNK_ALIAS (fn
);
9375 fn_original
= THUNK_TARGET (fn
);
9378 /* If the only definition of this function signature along our
9379 primary base chain is from a lost primary, this vtable slot will
9380 never be used, so just zero it out. This is important to avoid
9381 requiring extra thunks which cannot be generated with the function.
9383 We first check this in update_vtable_entry_for_fn, so we handle
9384 restored primary bases properly; we also need to do it here so we
9385 zero out unused slots in ctor vtables, rather than filling them
9386 with erroneous values (though harmless, apart from relocation
9388 if (BV_LOST_PRIMARY (v
))
9389 init
= size_zero_node
;
9393 /* Pull the offset for `this', and the function to call, out of
9395 delta
= BV_DELTA (v
);
9396 vcall_index
= BV_VCALL_INDEX (v
);
9398 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9399 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9401 /* You can't call an abstract virtual function; it's abstract.
9402 So, we replace these functions with __pure_virtual. */
9403 if (DECL_PURE_VIRTUAL_P (fn_original
))
9406 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9408 if (abort_fndecl_addr
== NULL
)
9410 = fold_convert (vfunc_ptr_type_node
,
9411 build_fold_addr_expr (fn
));
9412 init
= abort_fndecl_addr
;
9415 /* Likewise for deleted virtuals. */
9416 else if (DECL_DELETED_FN (fn_original
))
9420 tree name
= get_identifier ("__cxa_deleted_virtual");
9421 dvirt_fn
= get_global_binding (name
);
9423 dvirt_fn
= push_library_fn
9425 build_function_type_list (void_type_node
, NULL_TREE
),
9426 NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
9429 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9430 init
= fold_convert (vfunc_ptr_type_node
,
9431 build_fold_addr_expr (fn
));
9435 if (!integer_zerop (delta
) || vcall_index
)
9437 fn
= make_thunk (fn
, /*this_adjusting=*/1,
9438 delta
, vcall_index
);
9439 if (!DECL_NAME (fn
))
9442 /* Take the address of the function, considering it to be of an
9443 appropriate generic type. */
9444 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9445 init
= fold_convert (vfunc_ptr_type_node
,
9446 build_fold_addr_expr (fn
));
9447 /* Don't refer to a virtual destructor from a constructor
9448 vtable or a vtable for an abstract class, since destroying
9449 an object under construction is undefined behavior and we
9450 don't want it to be considered a candidate for speculative
9451 devirtualization. But do create the thunk for ABI
9453 if (DECL_DESTRUCTOR_P (fn_original
)
9454 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9455 || orig_binfo
!= binfo
))
9456 init
= size_zero_node
;
9460 /* And add it to the chain of initializers. */
9461 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9464 if (init
== size_zero_node
)
9465 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9466 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9468 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9470 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9471 fn
, build_int_cst (NULL_TREE
, i
));
9472 TREE_CONSTANT (fdesc
) = 1;
9474 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), fdesc
);
9478 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9482 /* Adds to vid->inits the initializers for the vbase and vcall
9483 offsets in BINFO, which is in the hierarchy dominated by T. */
9486 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9490 /* If this is a derived class, we must first create entries
9491 corresponding to the primary base class. */
9492 b
= get_primary_binfo (binfo
);
9494 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9496 /* Add the vbase entries for this base. */
9497 build_vbase_offset_vtbl_entries (binfo
, vid
);
9498 /* Add the vcall entries for this base. */
9499 build_vcall_offset_vtbl_entries (binfo
, vid
);
9502 /* Returns the initializers for the vbase offset entries in the vtable
9503 for BINFO (which is part of the class hierarchy dominated by T), in
9504 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9505 where the next vbase offset will go. */
9508 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9512 tree non_primary_binfo
;
9514 /* If there are no virtual baseclasses, then there is nothing to
9516 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9521 /* We might be a primary base class. Go up the inheritance hierarchy
9522 until we find the most derived class of which we are a primary base:
9523 it is the offset of that which we need to use. */
9524 non_primary_binfo
= binfo
;
9525 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9529 /* If we have reached a virtual base, then it must be a primary
9530 base (possibly multi-level) of vid->binfo, or we wouldn't
9531 have called build_vcall_and_vbase_vtbl_entries for it. But it
9532 might be a lost primary, so just skip down to vid->binfo. */
9533 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9535 non_primary_binfo
= vid
->binfo
;
9539 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9540 if (get_primary_binfo (b
) != non_primary_binfo
)
9542 non_primary_binfo
= b
;
9545 /* Go through the virtual bases, adding the offsets. */
9546 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9548 vbase
= TREE_CHAIN (vbase
))
9553 if (!BINFO_VIRTUAL_P (vbase
))
9556 /* Find the instance of this virtual base in the complete
9558 b
= copied_binfo (vbase
, binfo
);
9560 /* If we've already got an offset for this virtual base, we
9561 don't need another one. */
9562 if (BINFO_VTABLE_PATH_MARKED (b
))
9564 BINFO_VTABLE_PATH_MARKED (b
) = 1;
9566 /* Figure out where we can find this vbase offset. */
9567 delta
= size_binop (MULT_EXPR
,
9569 fold_convert (ssizetype
,
9570 TYPE_SIZE_UNIT (vtable_entry_type
)));
9571 if (vid
->primary_vtbl_p
)
9572 BINFO_VPTR_FIELD (b
) = delta
;
9574 if (binfo
!= TYPE_BINFO (t
))
9575 /* The vbase offset had better be the same. */
9576 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
9578 /* The next vbase will come at a more negative offset. */
9579 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9580 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9582 /* The initializer is the delta from BINFO to this virtual base.
9583 The vbase offsets go in reverse inheritance-graph order, and
9584 we are walking in inheritance graph order so these end up in
9586 delta
= size_diffop_loc (input_location
,
9587 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
9589 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
9590 fold_build1_loc (input_location
, NOP_EXPR
,
9591 vtable_entry_type
, delta
));
9595 /* Adds the initializers for the vcall offset entries in the vtable
9596 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9600 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9602 /* We only need these entries if this base is a virtual base. We
9603 compute the indices -- but do not add to the vtable -- when
9604 building the main vtable for a class. */
9605 if (binfo
== TYPE_BINFO (vid
->derived
)
9606 || (BINFO_VIRTUAL_P (binfo
)
9607 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9608 correspond to VID->DERIVED), we are building a primary
9609 construction virtual table. Since this is a primary
9610 virtual table, we do not need the vcall offsets for
9612 && binfo
!= vid
->rtti_binfo
))
9614 /* We need a vcall offset for each of the virtual functions in this
9615 vtable. For example:
9617 class A { virtual void f (); };
9618 class B1 : virtual public A { virtual void f (); };
9619 class B2 : virtual public A { virtual void f (); };
9620 class C: public B1, public B2 { virtual void f (); };
9622 A C object has a primary base of B1, which has a primary base of A. A
9623 C also has a secondary base of B2, which no longer has a primary base
9624 of A. So the B2-in-C construction vtable needs a secondary vtable for
9625 A, which will adjust the A* to a B2* to call f. We have no way of
9626 knowing what (or even whether) this offset will be when we define B2,
9627 so we store this "vcall offset" in the A sub-vtable and look it up in
9628 a "virtual thunk" for B2::f.
9630 We need entries for all the functions in our primary vtable and
9631 in our non-virtual bases' secondary vtables. */
9633 /* If we are just computing the vcall indices -- but do not need
9634 the actual entries -- not that. */
9635 if (!BINFO_VIRTUAL_P (binfo
))
9636 vid
->generate_vcall_entries
= false;
9637 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9638 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
9642 /* Build vcall offsets, starting with those for BINFO. */
9645 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
9651 /* Don't walk into virtual bases -- except, of course, for the
9652 virtual base for which we are building vcall offsets. Any
9653 primary virtual base will have already had its offsets generated
9654 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9655 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
9658 /* If BINFO has a primary base, process it first. */
9659 primary_binfo
= get_primary_binfo (binfo
);
9661 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
9663 /* Add BINFO itself to the list. */
9664 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
9666 /* Scan the non-primary bases of BINFO. */
9667 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9668 if (base_binfo
!= primary_binfo
)
9669 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
9672 /* Called from build_vcall_offset_vtbl_entries_r. */
9675 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
9677 /* Make entries for the rest of the virtuals. */
9680 /* The ABI requires that the methods be processed in declaration
9682 for (orig_fn
= TYPE_FIELDS (BINFO_TYPE (binfo
));
9684 orig_fn
= DECL_CHAIN (orig_fn
))
9685 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
9686 add_vcall_offset (orig_fn
, binfo
, vid
);
9689 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9692 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
9698 /* If there is already an entry for a function with the same
9699 signature as FN, then we do not need a second vcall offset.
9700 Check the list of functions already present in the derived
9702 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
9704 if (same_signature_p (derived_entry
, orig_fn
)
9705 /* We only use one vcall offset for virtual destructors,
9706 even though there are two virtual table entries. */
9707 || (DECL_DESTRUCTOR_P (derived_entry
)
9708 && DECL_DESTRUCTOR_P (orig_fn
)))
9712 /* If we are building these vcall offsets as part of building
9713 the vtable for the most derived class, remember the vcall
9715 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
9717 tree_pair_s elt
= {orig_fn
, vid
->index
};
9718 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
9721 /* The next vcall offset will be found at a more negative
9723 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9724 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9726 /* Keep track of this function. */
9727 vec_safe_push (vid
->fns
, orig_fn
);
9729 if (vid
->generate_vcall_entries
)
9734 /* Find the overriding function. */
9735 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
9736 if (fn
== error_mark_node
)
9737 vcall_offset
= build_zero_cst (vtable_entry_type
);
9740 base
= TREE_VALUE (fn
);
9742 /* The vbase we're working on is a primary base of
9743 vid->binfo. But it might be a lost primary, so its
9744 BINFO_OFFSET might be wrong, so we just use the
9745 BINFO_OFFSET from vid->binfo. */
9746 vcall_offset
= size_diffop_loc (input_location
,
9747 BINFO_OFFSET (base
),
9748 BINFO_OFFSET (vid
->binfo
));
9749 vcall_offset
= fold_build1_loc (input_location
,
9750 NOP_EXPR
, vtable_entry_type
,
9753 /* Add the initializer to the vtable. */
9754 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
9758 /* Return vtbl initializers for the RTTI entries corresponding to the
9759 BINFO's vtable. The RTTI entries should indicate the object given
9760 by VID->rtti_binfo. */
9763 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9771 t
= BINFO_TYPE (vid
->rtti_binfo
);
9773 /* To find the complete object, we will first convert to our most
9774 primary base, and then add the offset in the vtbl to that value. */
9775 b
= most_primary_binfo (binfo
);
9776 offset
= size_diffop_loc (input_location
,
9777 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
9779 /* The second entry is the address of the typeinfo object. */
9781 decl
= build_address (get_tinfo_decl (t
));
9783 decl
= integer_zero_node
;
9785 /* Convert the declaration to a type that can be stored in the
9787 init
= build_nop (vfunc_ptr_type_node
, decl
);
9788 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9790 /* Add the offset-to-top entry. It comes earlier in the vtable than
9791 the typeinfo entry. Convert the offset to look like a
9792 function pointer, so that we can put it in the vtable. */
9793 init
= build_nop (vfunc_ptr_type_node
, offset
);
9794 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9797 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9801 uniquely_derived_from_p (tree parent
, tree type
)
9803 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
9804 return base
&& base
!= error_mark_node
;
9807 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9810 publicly_uniquely_derived_p (tree parent
, tree type
)
9812 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
9814 return base
&& base
!= error_mark_node
;
9817 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9818 class between them, if any. */
9821 common_enclosing_class (tree ctx1
, tree ctx2
)
9823 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
9825 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
9826 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
9829 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9830 TYPE_MARKED_P (t
) = true;
9831 tree found
= NULL_TREE
;
9832 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9833 if (TYPE_MARKED_P (t
))
9838 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9839 TYPE_MARKED_P (t
) = false;
9843 #include "gt-cp-class.h"