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
);
209 static bool field_poverlapping_p (tree
);
211 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
212 'structor is in charge of 'structing virtual bases, or FALSE_STMT
216 build_if_in_charge (tree true_stmt
, tree false_stmt
)
218 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl
));
219 tree cmp
= build2 (NE_EXPR
, boolean_type_node
,
220 current_in_charge_parm
, integer_zero_node
);
221 tree type
= unlowered_expr_type (true_stmt
);
222 if (VOID_TYPE_P (type
))
223 type
= unlowered_expr_type (false_stmt
);
224 tree cond
= build3 (COND_EXPR
, type
,
225 cmp
, true_stmt
, false_stmt
);
229 /* Convert to or from a base subobject. EXPR is an expression of type
230 `A' or `A*', an expression of type `B' or `B*' is returned. To
231 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
232 the B base instance within A. To convert base A to derived B, CODE
233 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
234 In this latter case, A must not be a morally virtual base of B.
235 NONNULL is true if EXPR is known to be non-NULL (this is only
236 needed when EXPR is of pointer type). CV qualifiers are preserved
240 build_base_path (enum tree_code code
,
244 tsubst_flags_t complain
)
246 tree v_binfo
= NULL_TREE
;
247 tree d_binfo
= NULL_TREE
;
251 tree null_test
= NULL
;
252 tree ptr_target_type
;
254 int want_pointer
= TYPE_PTR_P (TREE_TYPE (expr
));
255 bool has_empty
= false;
259 if (expr
== error_mark_node
|| binfo
== error_mark_node
|| !binfo
)
260 return error_mark_node
;
262 for (probe
= binfo
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
265 if (is_empty_class (BINFO_TYPE (probe
)))
267 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
271 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
273 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
274 if (dependent_type_p (probe
))
275 if (tree open
= currently_open_class (probe
))
278 if (code
== PLUS_EXPR
279 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo
), probe
))
281 /* This can happen when adjust_result_of_qualified_name_lookup can't
282 find a unique base binfo in a call to a member function. We
283 couldn't give the diagnostic then since we might have been calling
284 a static member function, so we do it now. In other cases, eg.
285 during error recovery (c++/71979), we may not have a base at all. */
286 if (complain
& tf_error
)
288 tree base
= lookup_base (probe
, BINFO_TYPE (d_binfo
),
289 ba_unique
, NULL
, complain
);
290 gcc_assert (base
== error_mark_node
|| !base
);
292 return error_mark_node
;
295 gcc_assert ((code
== MINUS_EXPR
296 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), probe
))
297 || code
== PLUS_EXPR
);
299 if (binfo
== d_binfo
)
303 if (code
== MINUS_EXPR
&& v_binfo
)
305 if (complain
& tf_error
)
307 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (v_binfo
)))
310 error ("cannot convert from pointer to base class %qT to "
311 "pointer to derived class %qT because the base is "
312 "virtual", BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
314 error ("cannot convert from base class %qT to derived "
315 "class %qT because the base is virtual",
316 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
321 error ("cannot convert from pointer to base class %qT to "
322 "pointer to derived class %qT via virtual base %qT",
323 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
),
324 BINFO_TYPE (v_binfo
));
326 error ("cannot convert from base class %qT to derived "
327 "class %qT via virtual base %qT", BINFO_TYPE (binfo
),
328 BINFO_TYPE (d_binfo
), BINFO_TYPE (v_binfo
));
331 return error_mark_node
;
336 rvalue
= !lvalue_p (expr
);
337 /* This must happen before the call to save_expr. */
338 expr
= cp_build_addr_expr (expr
, complain
);
341 expr
= mark_rvalue_use (expr
);
343 offset
= BINFO_OFFSET (binfo
);
344 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
345 target_type
= code
== PLUS_EXPR
? BINFO_TYPE (binfo
) : BINFO_TYPE (d_binfo
);
346 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
347 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
348 expression returned matches the input. */
349 target_type
= cp_build_qualified_type
350 (target_type
, cp_type_quals (TREE_TYPE (TREE_TYPE (expr
))));
351 ptr_target_type
= build_pointer_type (target_type
);
353 /* Do we need to look in the vtable for the real offset? */
354 virtual_access
= (v_binfo
&& fixed_type_p
<= 0);
356 /* Don't bother with the calculations inside sizeof; they'll ICE if the
357 source type is incomplete and the pointer value doesn't matter. In a
358 template (even in instantiate_non_dependent_expr), we don't have vtables
359 set up properly yet, and the value doesn't matter there either; we're
360 just interested in the result of overload resolution. */
361 if (cp_unevaluated_operand
!= 0
362 || processing_template_decl
363 || in_template_function ())
365 expr
= build_nop (ptr_target_type
, expr
);
369 if (!COMPLETE_TYPE_P (probe
))
371 if (complain
& tf_error
)
372 error ("cannot convert from %qT to base class %qT because %qT is "
373 "incomplete", BINFO_TYPE (d_binfo
), BINFO_TYPE (binfo
),
374 BINFO_TYPE (d_binfo
));
375 return error_mark_node
;
378 /* If we're in an NSDMI, we don't have the full constructor context yet
379 that we need for converting to a virtual base, so just build a stub
380 CONVERT_EXPR and expand it later in bot_replace. */
381 if (virtual_access
&& fixed_type_p
< 0
382 && current_scope () != current_function_decl
)
384 expr
= build1 (CONVERT_EXPR
, ptr_target_type
, expr
);
385 CONVERT_EXPR_VBASE_PATH (expr
) = true;
389 /* Do we need to check for a null pointer? */
390 if (want_pointer
&& !nonnull
)
392 /* If we know the conversion will not actually change the value
393 of EXPR, then we can avoid testing the expression for NULL.
394 We have to avoid generating a COMPONENT_REF for a base class
395 field, because other parts of the compiler know that such
396 expressions are always non-NULL. */
397 if (!virtual_access
&& integer_zerop (offset
))
398 return build_nop (ptr_target_type
, expr
);
399 null_test
= error_mark_node
;
402 /* Protect against multiple evaluation if necessary. */
403 if (TREE_SIDE_EFFECTS (expr
) && (null_test
|| virtual_access
))
404 expr
= save_expr (expr
);
406 /* Now that we've saved expr, build the real null test. */
409 tree zero
= cp_convert (TREE_TYPE (expr
), nullptr_node
, complain
);
410 null_test
= build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
412 /* This is a compiler generated comparison, don't emit
413 e.g. -Wnonnull-compare warning for it. */
414 TREE_NO_WARNING (null_test
) = 1;
417 /* If this is a simple base reference, express it as a COMPONENT_REF. */
418 if (code
== PLUS_EXPR
&& !virtual_access
419 /* We don't build base fields for empty bases, and they aren't very
420 interesting to the optimizers anyway. */
423 expr
= cp_build_fold_indirect_ref (expr
);
424 expr
= build_simple_base_path (expr
, binfo
);
425 if (rvalue
&& lvalue_p (expr
))
428 expr
= build_address (expr
);
429 target_type
= TREE_TYPE (expr
);
435 /* Going via virtual base V_BINFO. We need the static offset
436 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
437 V_BINFO. That offset is an entry in D_BINFO's vtable. */
440 if (fixed_type_p
< 0 && in_base_initializer
)
442 /* In a base member initializer, we cannot rely on the
443 vtable being set up. We have to indirect via the
447 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
448 t
= build_pointer_type (t
);
449 v_offset
= fold_convert (t
, current_vtt_parm
);
450 v_offset
= cp_build_fold_indirect_ref (v_offset
);
455 if (sanitize_flags_p (SANITIZE_VPTR
)
456 && fixed_type_p
== 0)
458 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
463 v_offset
= build_vfield_ref (cp_build_fold_indirect_ref (t
),
464 TREE_TYPE (TREE_TYPE (expr
)));
467 if (v_offset
== error_mark_node
)
468 return error_mark_node
;
470 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
471 v_offset
= build1 (NOP_EXPR
,
472 build_pointer_type (ptrdiff_type_node
),
474 v_offset
= cp_build_fold_indirect_ref (v_offset
);
475 TREE_CONSTANT (v_offset
) = 1;
477 offset
= convert_to_integer (ptrdiff_type_node
,
478 size_diffop_loc (input_location
, offset
,
479 BINFO_OFFSET (v_binfo
)));
481 if (!integer_zerop (offset
))
482 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
484 if (fixed_type_p
< 0)
485 /* Negative fixed_type_p means this is a constructor or destructor;
486 virtual base layout is fixed in in-charge [cd]tors, but not in
488 offset
= build_if_in_charge
489 (convert_to_integer (ptrdiff_type_node
, BINFO_OFFSET (binfo
)),
496 target_type
= ptr_target_type
;
498 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
500 if (!integer_zerop (offset
))
502 offset
= fold_convert (sizetype
, offset
);
503 if (code
== MINUS_EXPR
)
504 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
505 expr
= fold_build_pointer_plus (expr
, offset
);
513 expr
= cp_build_fold_indirect_ref (expr
);
520 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
, expr
,
521 build_zero_cst (target_type
));
526 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
527 Perform a derived-to-base conversion by recursively building up a
528 sequence of COMPONENT_REFs to the appropriate base fields. */
531 build_simple_base_path (tree expr
, tree binfo
)
533 tree type
= BINFO_TYPE (binfo
);
534 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
537 if (d_binfo
== NULL_TREE
)
541 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
543 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
544 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
545 an lvalue in the front end; only _DECLs and _REFs are lvalues
547 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
549 expr
= cp_build_fold_indirect_ref (temp
);
555 expr
= build_simple_base_path (expr
, d_binfo
);
557 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
558 field
; field
= DECL_CHAIN (field
))
559 /* Is this the base field created by build_base_field? */
560 if (TREE_CODE (field
) == FIELD_DECL
561 && DECL_FIELD_IS_BASE (field
)
562 && TREE_TYPE (field
) == type
563 /* If we're looking for a field in the most-derived class,
564 also check the field offset; we can have two base fields
565 of the same type if one is an indirect virtual base and one
566 is a direct non-virtual base. */
567 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
568 || tree_int_cst_equal (byte_position (field
),
569 BINFO_OFFSET (binfo
))))
571 /* We don't use build_class_member_access_expr here, as that
572 has unnecessary checks, and more importantly results in
573 recursive calls to dfs_walk_once. */
574 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
576 expr
= build3 (COMPONENT_REF
,
577 cp_build_qualified_type (type
, type_quals
),
578 expr
, field
, NULL_TREE
);
579 /* Mark the expression const or volatile, as appropriate.
580 Even though we've dealt with the type above, we still have
581 to mark the expression itself. */
582 if (type_quals
& TYPE_QUAL_CONST
)
583 TREE_READONLY (expr
) = 1;
584 if (type_quals
& TYPE_QUAL_VOLATILE
)
585 TREE_THIS_VOLATILE (expr
) = 1;
590 /* Didn't find the base field?!? */
594 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
595 type is a class type or a pointer to a class type. In the former
596 case, TYPE is also a class type; in the latter it is another
597 pointer type. If CHECK_ACCESS is true, an error message is emitted
598 if TYPE is inaccessible. If OBJECT has pointer type, the value is
599 assumed to be non-NULL. */
602 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
603 tsubst_flags_t complain
)
608 if (TYPE_PTR_P (TREE_TYPE (object
)))
610 object_type
= TREE_TYPE (TREE_TYPE (object
));
611 type
= TREE_TYPE (type
);
614 object_type
= TREE_TYPE (object
);
616 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
618 if (!binfo
|| binfo
== error_mark_node
)
619 return error_mark_node
;
621 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
624 /* EXPR is an expression with unqualified class type. BASE is a base
625 binfo of that class type. Returns EXPR, converted to the BASE
626 type. This function assumes that EXPR is the most derived class;
627 therefore virtual bases can be found at their static offsets. */
630 convert_to_base_statically (tree expr
, tree base
)
634 expr_type
= TREE_TYPE (expr
);
635 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
637 /* If this is a non-empty base, use a COMPONENT_REF. */
638 if (!is_empty_class (BINFO_TYPE (base
)))
639 return build_simple_base_path (expr
, base
);
641 /* We use fold_build2 and fold_convert below to simplify the trees
642 provided to the optimizers. It is not safe to call these functions
643 when processing a template because they do not handle C++-specific
645 gcc_assert (!processing_template_decl
);
646 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
647 if (!integer_zerop (BINFO_OFFSET (base
)))
648 expr
= fold_build_pointer_plus_loc (input_location
,
649 expr
, BINFO_OFFSET (base
));
650 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
651 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
659 build_vfield_ref (tree datum
, tree type
)
661 tree vfield
, vcontext
;
663 if (datum
== error_mark_node
664 /* Can happen in case of duplicate base types (c++/59082). */
665 || !TYPE_VFIELD (type
))
666 return error_mark_node
;
668 /* First, convert to the requested type. */
669 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
670 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
671 /*nonnull=*/true, tf_warning_or_error
);
673 /* Second, the requested type may not be the owner of its own vptr.
674 If not, convert to the base class that owns it. We cannot use
675 convert_to_base here, because VCONTEXT may appear more than once
676 in the inheritance hierarchy of TYPE, and thus direct conversion
677 between the types may be ambiguous. Following the path back up
678 one step at a time via primary bases avoids the problem. */
679 vfield
= TYPE_VFIELD (type
);
680 vcontext
= DECL_CONTEXT (vfield
);
681 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
683 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
684 type
= TREE_TYPE (datum
);
687 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
690 /* Given an object INSTANCE, return an expression which yields the
691 vtable element corresponding to INDEX. There are many special
692 cases for INSTANCE which we take care of here, mainly to avoid
693 creating extra tree nodes when we don't have to. */
696 build_vtbl_ref (tree instance
, tree idx
)
699 tree vtbl
= NULL_TREE
;
701 /* Try to figure out what a reference refers to, and
702 access its virtual function table directly. */
705 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
707 tree basetype
= non_reference (TREE_TYPE (instance
));
709 if (fixed_type
&& !cdtorp
)
711 tree binfo
= lookup_base (fixed_type
, basetype
,
712 ba_unique
, NULL
, tf_none
);
713 if (binfo
&& binfo
!= error_mark_node
)
714 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
718 vtbl
= build_vfield_ref (instance
, basetype
);
720 aref
= build_array_ref (input_location
, vtbl
, idx
);
721 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
726 /* Given a stable object pointer INSTANCE_PTR, return an expression which
727 yields a function pointer corresponding to vtable element INDEX. */
730 build_vfn_ref (tree instance_ptr
, tree idx
)
734 aref
= build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr
), idx
);
736 /* When using function descriptors, the address of the
737 vtable entry is treated as a function pointer. */
738 if (TARGET_VTABLE_USES_DESCRIPTORS
)
739 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
740 cp_build_addr_expr (aref
, tf_warning_or_error
));
742 /* Remember this as a method reference, for later devirtualization. */
743 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
748 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
749 for the given TYPE. */
752 get_vtable_name (tree type
)
754 return mangle_vtbl_for_type (type
);
757 /* DECL is an entity associated with TYPE, like a virtual table or an
758 implicitly generated constructor. Determine whether or not DECL
759 should have external or internal linkage at the object file
760 level. This routine does not deal with COMDAT linkage and other
761 similar complexities; it simply sets TREE_PUBLIC if it possible for
762 entities in other translation units to contain copies of DECL, in
766 set_linkage_according_to_type (tree
/*type*/, tree decl
)
768 TREE_PUBLIC (decl
) = 1;
769 determine_visibility (decl
);
772 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
773 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
774 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
777 build_vtable (tree class_type
, tree name
, tree vtable_type
)
781 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
782 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
783 now to avoid confusion in mangle_decl. */
784 SET_DECL_ASSEMBLER_NAME (decl
, name
);
785 DECL_CONTEXT (decl
) = class_type
;
786 DECL_ARTIFICIAL (decl
) = 1;
787 TREE_STATIC (decl
) = 1;
788 TREE_READONLY (decl
) = 1;
789 DECL_VIRTUAL_P (decl
) = 1;
790 SET_DECL_ALIGN (decl
, TARGET_VTABLE_ENTRY_ALIGN
);
791 DECL_USER_ALIGN (decl
) = true;
792 DECL_VTABLE_OR_VTT_P (decl
) = 1;
793 set_linkage_according_to_type (class_type
, decl
);
794 /* The vtable has not been defined -- yet. */
795 DECL_EXTERNAL (decl
) = 1;
796 DECL_NOT_REALLY_EXTERN (decl
) = 1;
798 /* Mark the VAR_DECL node representing the vtable itself as a
799 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
800 is rather important that such things be ignored because any
801 effort to actually generate DWARF for them will run into
802 trouble when/if we encounter code like:
805 struct S { virtual void member (); };
807 because the artificial declaration of the vtable itself (as
808 manufactured by the g++ front end) will say that the vtable is
809 a static member of `S' but only *after* the debug output for
810 the definition of `S' has already been output. This causes
811 grief because the DWARF entry for the definition of the vtable
812 will try to refer back to an earlier *declaration* of the
813 vtable as a static member of `S' and there won't be one. We
814 might be able to arrange to have the "vtable static member"
815 attached to the member list for `S' before the debug info for
816 `S' get written (which would solve the problem) but that would
817 require more intrusive changes to the g++ front end. */
818 DECL_IGNORED_P (decl
) = 1;
823 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
824 or even complete. If this does not exist, create it. If COMPLETE is
825 nonzero, then complete the definition of it -- that will render it
826 impossible to actually build the vtable, but is useful to get at those
827 which are known to exist in the runtime. */
830 get_vtable_decl (tree type
, int complete
)
834 if (CLASSTYPE_VTABLES (type
))
835 return CLASSTYPE_VTABLES (type
);
837 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
838 CLASSTYPE_VTABLES (type
) = decl
;
842 DECL_EXTERNAL (decl
) = 1;
843 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
849 /* Build the primary virtual function table for TYPE. If BINFO is
850 non-NULL, build the vtable starting with the initial approximation
851 that it is the same as the one which is the head of the association
852 list. Returns a nonzero value if a new vtable is actually
856 build_primary_vtable (tree binfo
, tree type
)
861 decl
= get_vtable_decl (type
, /*complete=*/0);
865 if (BINFO_NEW_VTABLE_MARKED (binfo
))
866 /* We have already created a vtable for this base, so there's
867 no need to do it again. */
870 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
871 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
872 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
873 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
877 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
878 virtuals
= NULL_TREE
;
881 /* Initialize the association list for this type, based
882 on our first approximation. */
883 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
884 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
885 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
889 /* Give BINFO a new virtual function table which is initialized
890 with a skeleton-copy of its original initialization. The only
891 entry that changes is the `delta' entry, so we can really
892 share a lot of structure.
894 FOR_TYPE is the most derived type which caused this table to
897 Returns nonzero if we haven't met BINFO before.
899 The order in which vtables are built (by calling this function) for
900 an object must remain the same, otherwise a binary incompatibility
904 build_secondary_vtable (tree binfo
)
906 if (BINFO_NEW_VTABLE_MARKED (binfo
))
907 /* We already created a vtable for this base. There's no need to
911 /* Remember that we've created a vtable for this BINFO, so that we
912 don't try to do so again. */
913 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
915 /* Make fresh virtual list, so we can smash it later. */
916 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
918 /* Secondary vtables are laid out as part of the same structure as
919 the primary vtable. */
920 BINFO_VTABLE (binfo
) = NULL_TREE
;
924 /* Create a new vtable for BINFO which is the hierarchy dominated by
925 T. Return nonzero if we actually created a new vtable. */
928 make_new_vtable (tree t
, tree binfo
)
930 if (binfo
== TYPE_BINFO (t
))
931 /* In this case, it is *type*'s vtable we are modifying. We start
932 with the approximation that its vtable is that of the
933 immediate base class. */
934 return build_primary_vtable (binfo
, t
);
936 /* This is our very own copy of `basetype' to play with. Later,
937 we will fill in all the virtual functions that override the
938 virtual functions in these base classes which are not defined
939 by the current type. */
940 return build_secondary_vtable (binfo
);
943 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
944 (which is in the hierarchy dominated by T) list FNDECL as its
945 BV_FN. DELTA is the required constant adjustment from the `this'
946 pointer where the vtable entry appears to the `this' required when
947 the function is actually called. */
950 modify_vtable_entry (tree t
,
960 if (fndecl
!= BV_FN (v
)
961 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
963 /* We need a new vtable for BINFO. */
964 if (make_new_vtable (t
, binfo
))
966 /* If we really did make a new vtable, we also made a copy
967 of the BINFO_VIRTUALS list. Now, we have to find the
968 corresponding entry in that list. */
969 *virtuals
= BINFO_VIRTUALS (binfo
);
970 while (BV_FN (*virtuals
) != BV_FN (v
))
971 *virtuals
= TREE_CHAIN (*virtuals
);
975 BV_DELTA (v
) = delta
;
976 BV_VCALL_INDEX (v
) = NULL_TREE
;
982 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
983 METHOD is being injected via a using_decl. Returns true if the
984 method could be added to the method vec. */
987 add_method (tree type
, tree method
, bool via_using
)
989 if (method
== error_mark_node
)
992 gcc_assert (!DECL_EXTERN_C_P (method
));
994 tree
*slot
= find_member_slot (type
, DECL_NAME (method
));
995 tree current_fns
= slot
? *slot
: NULL_TREE
;
997 /* Check to see if we've already got this method. */
998 for (ovl_iterator
iter (current_fns
); iter
; ++iter
)
1006 if (TREE_CODE (fn
) != TREE_CODE (method
))
1009 /* Two using-declarations can coexist, we'll complain about ambiguity in
1010 overload resolution. */
1011 if (via_using
&& iter
.using_p ()
1012 /* Except handle inherited constructors specially. */
1013 && ! DECL_CONSTRUCTOR_P (fn
))
1016 /* [over.load] Member function declarations with the
1017 same name and the same parameter types cannot be
1018 overloaded if any of them is a static member
1019 function declaration.
1021 [over.load] Member function declarations with the same name and
1022 the same parameter-type-list as well as member function template
1023 declarations with the same name, the same parameter-type-list, and
1024 the same template parameter lists cannot be overloaded if any of
1025 them, but not all, have a ref-qualifier.
1027 [namespace.udecl] When a using-declaration brings names
1028 from a base class into a derived class scope, member
1029 functions in the derived class override and/or hide member
1030 functions with the same name and parameter types in a base
1031 class (rather than conflicting). */
1032 fn_type
= TREE_TYPE (fn
);
1033 method_type
= TREE_TYPE (method
);
1034 parms1
= TYPE_ARG_TYPES (fn_type
);
1035 parms2
= TYPE_ARG_TYPES (method_type
);
1037 /* Compare the quals on the 'this' parm. Don't compare
1038 the whole types, as used functions are treated as
1039 coming from the using class in overload resolution. */
1040 if (! DECL_STATIC_FUNCTION_P (fn
)
1041 && ! DECL_STATIC_FUNCTION_P (method
)
1042 /* Either both or neither need to be ref-qualified for
1043 differing quals to allow overloading. */
1044 && (FUNCTION_REF_QUALIFIED (fn_type
)
1045 == FUNCTION_REF_QUALIFIED (method_type
))
1046 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1047 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1050 /* For templates, the return type and template parameters
1051 must be identical. */
1052 if (TREE_CODE (fn
) == TEMPLATE_DECL
1053 && (!same_type_p (TREE_TYPE (fn_type
),
1054 TREE_TYPE (method_type
))
1055 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1056 DECL_TEMPLATE_PARMS (method
))))
1059 if (! DECL_STATIC_FUNCTION_P (fn
))
1060 parms1
= TREE_CHAIN (parms1
);
1061 if (! DECL_STATIC_FUNCTION_P (method
))
1062 parms2
= TREE_CHAIN (parms2
);
1064 /* Bring back parameters omitted from an inherited ctor. */
1065 if (ctor_omit_inherited_parms (fn
))
1066 parms1
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
1067 if (ctor_omit_inherited_parms (method
))
1068 parms2
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method
));
1070 if (compparms (parms1
, parms2
)
1071 && (!DECL_CONV_FN_P (fn
)
1072 || same_type_p (TREE_TYPE (fn_type
),
1073 TREE_TYPE (method_type
)))
1074 && equivalently_constrained (fn
, method
))
1076 /* If these are versions of the same function, process and
1078 if (TREE_CODE (fn
) == FUNCTION_DECL
1079 && maybe_version_functions (method
, fn
, true))
1082 if (DECL_INHERITED_CTOR (method
))
1084 if (DECL_INHERITED_CTOR (fn
))
1086 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1087 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1088 if (flag_new_inheriting_ctors
)
1092 /* Inheriting the same constructor along different
1093 paths, combine them. */
1094 SET_DECL_INHERITED_CTOR
1095 (fn
, ovl_make (DECL_INHERITED_CTOR (method
),
1096 DECL_INHERITED_CTOR (fn
)));
1097 /* And discard the new one. */
1101 /* Inherited ctors can coexist until overload
1105 error_at (DECL_SOURCE_LOCATION (method
),
1106 "%q#D conflicts with version inherited from %qT",
1108 inform (DECL_SOURCE_LOCATION (fn
),
1109 "version inherited from %qT declared here",
1112 /* Otherwise defer to the other function. */
1117 /* Defer to the local function. */
1119 else if (flag_new_inheriting_ctors
1120 && DECL_INHERITED_CTOR (fn
))
1122 /* Remove the inherited constructor. */
1123 current_fns
= iter
.remove_node (current_fns
);
1128 error_at (DECL_SOURCE_LOCATION (method
),
1129 "%q#D cannot be overloaded with %q#D", method
, fn
);
1130 inform (DECL_SOURCE_LOCATION (fn
),
1131 "previous declaration %q#D", fn
);
1137 current_fns
= ovl_insert (method
, current_fns
, via_using
);
1139 if (!COMPLETE_TYPE_P (type
) && !DECL_CONV_FN_P (method
)
1140 && !push_class_level_binding (DECL_NAME (method
), current_fns
))
1144 slot
= add_member_slot (type
, DECL_NAME (method
));
1146 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1147 grok_special_member_properties (method
);
1149 *slot
= current_fns
;
1154 /* Subroutines of finish_struct. */
1156 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1157 legit, otherwise return 0. */
1160 alter_access (tree t
, tree fdecl
, tree access
)
1164 retrofit_lang_decl (fdecl
);
1166 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1168 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1171 if (TREE_VALUE (elem
) != access
)
1173 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1174 error ("conflicting access specifications for method"
1175 " %q+D, ignored", TREE_TYPE (fdecl
));
1177 error ("conflicting access specifications for field %qE, ignored",
1182 /* They're changing the access to the same thing they changed
1183 it to before. That's OK. */
1189 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1190 tf_warning_or_error
);
1191 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1197 /* Return the access node for DECL's access in its enclosing class. */
1200 declared_access (tree decl
)
1202 return (TREE_PRIVATE (decl
) ? access_private_node
1203 : TREE_PROTECTED (decl
) ? access_protected_node
1204 : access_public_node
);
1207 /* Process the USING_DECL, which is a member of T. */
1210 handle_using_decl (tree using_decl
, tree t
)
1212 tree decl
= USING_DECL_DECLS (using_decl
);
1213 tree name
= DECL_NAME (using_decl
);
1214 tree access
= declared_access (using_decl
);
1215 tree flist
= NULL_TREE
;
1218 gcc_assert (!processing_template_decl
&& decl
);
1220 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1221 tf_warning_or_error
);
1224 old_value
= OVL_FIRST (old_value
);
1226 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1229 old_value
= NULL_TREE
;
1232 cp_emit_debug_info_for_using (decl
, t
);
1234 if (is_overloaded_fn (decl
))
1239 else if (is_overloaded_fn (old_value
))
1242 /* It's OK to use functions from a base when there are functions with
1243 the same name already present in the current class. */;
1246 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1247 "because of local method %q#D with same name",
1248 using_decl
, t
, old_value
);
1249 inform (DECL_SOURCE_LOCATION (old_value
),
1250 "local method %q#D declared here", old_value
);
1254 else if (!DECL_ARTIFICIAL (old_value
))
1256 error_at (DECL_SOURCE_LOCATION (using_decl
), "%qD invalid in %q#T "
1257 "because of local member %q#D with same name",
1258 using_decl
, t
, old_value
);
1259 inform (DECL_SOURCE_LOCATION (old_value
),
1260 "local member %q#D declared here", old_value
);
1264 /* Make type T see field decl FDECL with access ACCESS. */
1266 for (ovl_iterator
iter (flist
); iter
; ++iter
)
1268 add_method (t
, *iter
, true);
1269 alter_access (t
, *iter
, access
);
1272 alter_access (t
, decl
, access
);
1275 /* Data structure for find_abi_tags_r, below. */
1279 tree t
; // The type that we're checking for missing tags.
1280 tree subob
; // The subobject of T that we're getting tags from.
1281 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1284 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1285 in the context of P. TAG can be either an identifier (the DECL_NAME of
1286 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1289 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1291 if (!IDENTIFIER_MARKED (id
))
1293 if (p
->tags
!= error_mark_node
)
1295 /* We're collecting tags from template arguments or from
1296 the type of a variable or function return type. */
1297 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1299 /* Don't inherit this tag multiple times. */
1300 IDENTIFIER_MARKED (id
) = true;
1304 /* Tags inherited from type template arguments are only used
1305 to avoid warnings. */
1306 ABI_TAG_IMPLICIT (p
->tags
) = true;
1309 /* For functions and variables we want to warn, too. */
1312 /* Otherwise we're diagnosing missing tags. */
1313 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1315 auto_diagnostic_group d
;
1316 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1317 "that %qT (used in its return type) has",
1319 inform (location_of (*tp
), "%qT declared here", *tp
);
1321 else if (VAR_P (p
->t
))
1323 auto_diagnostic_group d
;
1324 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1325 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1326 inform (location_of (*tp
), "%qT declared here", *tp
);
1328 else if (TYPE_P (p
->subob
))
1330 auto_diagnostic_group d
;
1331 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1332 "that base %qT has", p
->t
, tag
, p
->subob
))
1333 inform (location_of (p
->subob
), "%qT declared here",
1338 auto_diagnostic_group d
;
1339 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1340 "that %qT (used in the type of %qD) has",
1341 p
->t
, tag
, *tp
, p
->subob
))
1343 inform (location_of (p
->subob
), "%qD declared here",
1345 inform (location_of (*tp
), "%qT declared here", *tp
);
1351 /* Find all the ABI tags in the attribute list ATTR and either call
1352 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1355 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1359 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1360 attr
= TREE_CHAIN (attr
))
1361 for (tree list
= TREE_VALUE (attr
); list
;
1362 list
= TREE_CHAIN (list
))
1364 tree tag
= TREE_VALUE (list
);
1365 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1367 check_tag (tag
, id
, tp
, p
);
1369 IDENTIFIER_MARKED (id
) = val
;
1373 /* Find all the ABI tags on T and its enclosing scopes and either call
1374 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1377 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1379 while (t
!= global_namespace
)
1384 attr
= TYPE_ATTRIBUTES (t
);
1385 t
= CP_TYPE_CONTEXT (t
);
1389 attr
= DECL_ATTRIBUTES (t
);
1390 t
= CP_DECL_CONTEXT (t
);
1392 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1396 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1397 types with ABI tags, add the corresponding identifiers to the VEC in
1398 *DATA and set IDENTIFIER_MARKED. */
1401 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1403 if (!OVERLOAD_TYPE_P (*tp
))
1406 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1407 anyway, but let's make sure of it. */
1408 *walk_subtrees
= false;
1410 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1412 mark_or_check_tags (*tp
, tp
, p
, false);
1417 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1418 IDENTIFIER_MARKED on its ABI tags. */
1421 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1423 if (!OVERLOAD_TYPE_P (*tp
))
1426 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1427 anyway, but let's make sure of it. */
1428 *walk_subtrees
= false;
1430 bool *valp
= static_cast<bool*>(data
);
1432 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1437 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1441 mark_abi_tags (tree t
, bool val
)
1443 mark_or_check_tags (t
, NULL
, NULL
, val
);
1446 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1447 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1449 /* Template arguments are part of the signature. */
1450 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1451 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1453 tree arg
= TREE_VEC_ELT (level
, j
);
1454 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1457 if (TREE_CODE (t
) == FUNCTION_DECL
)
1458 /* A function's parameter types are part of the signature, so
1459 we don't need to inherit any tags that are also in them. */
1460 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1461 arg
= TREE_CHAIN (arg
))
1462 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1463 mark_abi_tags_r
, &val
);
1467 /* Check that T has all the ABI tags that subobject SUBOB has, or
1468 warn if not. If T is a (variable or function) declaration, also
1469 return any missing tags, and add them to T if JUST_CHECKING is false. */
1472 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1474 bool inherit
= DECL_P (t
);
1476 if (!inherit
&& !warn_abi_tag
)
1479 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1480 if (!TREE_PUBLIC (decl
))
1481 /* No need to worry about things local to this TU. */
1484 mark_abi_tags (t
, true);
1486 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1487 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1489 data
.tags
= NULL_TREE
;
1491 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1493 if (!(inherit
&& data
.tags
))
1494 /* We don't need to do anything with data.tags. */;
1495 else if (just_checking
)
1496 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1498 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1499 IDENTIFIER_MARKED (id
) = false;
1503 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1505 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1508 = tree_cons (abi_tag_identifier
, data
.tags
, DECL_ATTRIBUTES (t
));
1511 mark_abi_tags (t
, false);
1516 /* Check that DECL has all the ABI tags that are used in parts of its type
1517 that are not reflected in its mangled name. */
1520 check_abi_tags (tree decl
)
1523 check_abi_tags (decl
, TREE_TYPE (decl
));
1524 else if (TREE_CODE (decl
) == FUNCTION_DECL
1525 && !DECL_CONV_FN_P (decl
)
1526 && !mangle_return_type_p (decl
))
1527 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1530 /* Return any ABI tags that are used in parts of the type of DECL
1531 that are not reflected in its mangled name. This function is only
1532 used in backward-compatible mangling for ABI <11. */
1535 missing_abi_tags (tree decl
)
1538 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1539 else if (TREE_CODE (decl
) == FUNCTION_DECL
1540 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1541 that we can use this function for setting need_abi_warning
1542 regardless of the current flag_abi_version. */
1543 && !mangle_return_type_p (decl
))
1544 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1550 inherit_targ_abi_tags (tree t
)
1552 if (!CLASS_TYPE_P (t
)
1553 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1556 mark_abi_tags (t
, true);
1558 tree args
= CLASSTYPE_TI_ARGS (t
);
1559 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1560 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1562 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1563 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1565 tree arg
= TREE_VEC_ELT (level
, j
);
1567 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1571 // If we found some tags on our template arguments, add them to our
1572 // abi_tag attribute.
1575 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1577 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1580 = tree_cons (abi_tag_identifier
, data
.tags
, TYPE_ATTRIBUTES (t
));
1583 mark_abi_tags (t
, false);
1586 /* Return true, iff class T has a non-virtual destructor that is
1587 accessible from outside the class heirarchy (i.e. is public, or
1588 there's a suitable friend. */
1591 accessible_nvdtor_p (tree t
)
1593 tree dtor
= CLASSTYPE_DESTRUCTOR (t
);
1595 /* An implicitly declared destructor is always public. And,
1596 if it were virtual, we would have created it by now. */
1600 if (DECL_VINDEX (dtor
))
1601 return false; /* Virtual */
1603 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1604 return true; /* Public */
1606 if (CLASSTYPE_FRIEND_CLASSES (t
)
1607 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1608 return true; /* Has friends */
1613 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1614 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1615 properties of the bases. */
1618 check_bases (tree t
,
1619 int* cant_have_const_ctor_p
,
1620 int* no_const_asn_ref_p
)
1623 bool seen_non_virtual_nearly_empty_base_p
= 0;
1624 int seen_tm_mask
= 0;
1627 tree field
= NULL_TREE
;
1629 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1630 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1631 if (TREE_CODE (field
) == FIELD_DECL
)
1634 for (binfo
= TYPE_BINFO (t
), i
= 0;
1635 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1637 tree basetype
= TREE_TYPE (base_binfo
);
1639 gcc_assert (COMPLETE_TYPE_P (basetype
));
1641 if (CLASSTYPE_FINAL (basetype
))
1642 error ("cannot derive from %<final%> base %qT in derived type %qT",
1645 /* If any base class is non-literal, so is the derived class. */
1646 if (!CLASSTYPE_LITERAL_P (basetype
))
1647 CLASSTYPE_LITERAL_P (t
) = false;
1649 /* If the base class doesn't have copy constructors or
1650 assignment operators that take const references, then the
1651 derived class cannot have such a member automatically
1653 if (TYPE_HAS_COPY_CTOR (basetype
)
1654 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1655 *cant_have_const_ctor_p
= 1;
1656 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1657 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1658 *no_const_asn_ref_p
= 1;
1660 if (BINFO_VIRTUAL_P (base_binfo
))
1661 /* A virtual base does not effect nearly emptiness. */
1663 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1665 if (seen_non_virtual_nearly_empty_base_p
)
1666 /* And if there is more than one nearly empty base, then the
1667 derived class is not nearly empty either. */
1668 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1670 /* Remember we've seen one. */
1671 seen_non_virtual_nearly_empty_base_p
= 1;
1673 else if (!is_empty_class (basetype
))
1674 /* If the base class is not empty or nearly empty, then this
1675 class cannot be nearly empty. */
1676 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1678 /* A lot of properties from the bases also apply to the derived
1680 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1681 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1682 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1683 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1684 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1685 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1686 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1687 || !TYPE_HAS_COPY_CTOR (basetype
));
1688 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1689 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1690 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1691 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1692 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1693 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1694 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1695 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1696 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1697 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1698 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1699 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1700 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1701 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1702 if (TYPE_HAS_MUTABLE_P (basetype
))
1703 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1705 /* A standard-layout class is a class that:
1707 * has no non-standard-layout base classes, */
1708 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1709 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1712 /* ...has no base classes of the same type as the first non-static
1714 if (field
&& DECL_CONTEXT (field
) == t
1715 && (same_type_ignoring_top_level_qualifiers_p
1716 (TREE_TYPE (field
), basetype
)))
1717 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1719 /* ...either has no non-static data members in the most-derived
1720 class and at most one base class with non-static data
1721 members, or has no base classes with non-static data
1723 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1724 basefield
= DECL_CHAIN (basefield
))
1725 if (TREE_CODE (basefield
) == FIELD_DECL
1726 && !(DECL_FIELD_IS_BASE (basefield
)
1727 && integer_zerop (DECL_SIZE (basefield
))))
1730 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1737 /* Don't bother collecting tm attributes if transactional memory
1738 support is not enabled. */
1741 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1743 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1746 check_abi_tags (t
, basetype
);
1749 /* If one of the base classes had TM attributes, and the current class
1750 doesn't define its own, then the current class inherits one. */
1751 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1753 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1754 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1758 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1759 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1760 that have had a nearly-empty virtual primary base stolen by some
1761 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1765 determine_primary_bases (tree t
)
1768 tree primary
= NULL_TREE
;
1769 tree type_binfo
= TYPE_BINFO (t
);
1772 /* Determine the primary bases of our bases. */
1773 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1774 base_binfo
= TREE_CHAIN (base_binfo
))
1776 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1778 /* See if we're the non-virtual primary of our inheritance
1780 if (!BINFO_VIRTUAL_P (base_binfo
))
1782 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1783 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1786 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1787 BINFO_TYPE (parent_primary
)))
1788 /* We are the primary binfo. */
1789 BINFO_PRIMARY_P (base_binfo
) = 1;
1791 /* Determine if we have a virtual primary base, and mark it so.
1793 if (primary
&& BINFO_VIRTUAL_P (primary
))
1795 tree this_primary
= copied_binfo (primary
, base_binfo
);
1797 if (BINFO_PRIMARY_P (this_primary
))
1798 /* Someone already claimed this base. */
1799 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1804 BINFO_PRIMARY_P (this_primary
) = 1;
1805 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1807 /* A virtual binfo might have been copied from within
1808 another hierarchy. As we're about to use it as a
1809 primary base, make sure the offsets match. */
1810 delta
= size_diffop_loc (input_location
,
1811 fold_convert (ssizetype
,
1812 BINFO_OFFSET (base_binfo
)),
1813 fold_convert (ssizetype
,
1814 BINFO_OFFSET (this_primary
)));
1816 propagate_binfo_offsets (this_primary
, delta
);
1821 /* First look for a dynamic direct non-virtual base. */
1822 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1824 tree basetype
= BINFO_TYPE (base_binfo
);
1826 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1828 primary
= base_binfo
;
1833 /* A "nearly-empty" virtual base class can be the primary base
1834 class, if no non-virtual polymorphic base can be found. Look for
1835 a nearly-empty virtual dynamic base that is not already a primary
1836 base of something in the hierarchy. If there is no such base,
1837 just pick the first nearly-empty virtual base. */
1839 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1840 base_binfo
= TREE_CHAIN (base_binfo
))
1841 if (BINFO_VIRTUAL_P (base_binfo
)
1842 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1844 if (!BINFO_PRIMARY_P (base_binfo
))
1846 /* Found one that is not primary. */
1847 primary
= base_binfo
;
1851 /* Remember the first candidate. */
1852 primary
= base_binfo
;
1856 /* If we've got a primary base, use it. */
1859 tree basetype
= BINFO_TYPE (primary
);
1861 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1862 if (BINFO_PRIMARY_P (primary
))
1863 /* We are stealing a primary base. */
1864 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1865 BINFO_PRIMARY_P (primary
) = 1;
1866 if (BINFO_VIRTUAL_P (primary
))
1870 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1871 /* A virtual binfo might have been copied from within
1872 another hierarchy. As we're about to use it as a primary
1873 base, make sure the offsets match. */
1874 delta
= size_diffop_loc (input_location
, ssize_int (0),
1875 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
1877 propagate_binfo_offsets (primary
, delta
);
1880 primary
= TYPE_BINFO (basetype
);
1882 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1883 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1884 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1888 /* Update the variant types of T. */
1891 fixup_type_variants (tree t
)
1898 for (variants
= TYPE_NEXT_VARIANT (t
);
1900 variants
= TYPE_NEXT_VARIANT (variants
))
1902 /* These fields are in the _TYPE part of the node, not in
1903 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1904 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1905 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1906 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1907 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1909 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1911 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1913 /* Copy whatever these are holding today. */
1914 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1915 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1919 /* KLASS is a class that we're applying may_alias to after the body is
1920 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1921 canonical type(s) will be implicitly updated. */
1924 fixup_may_alias (tree klass
)
1928 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
1929 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1930 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1931 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
1932 for (v
= TYPE_MAIN_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
1933 TYPE_REF_CAN_ALIAS_ALL (v
) = true;
1936 /* Early variant fixups: we apply attributes at the beginning of the class
1937 definition, and we need to fix up any variants that have already been
1938 made via elaborated-type-specifier so that check_qualified_type works. */
1941 fixup_attribute_variants (tree t
)
1948 tree attrs
= TYPE_ATTRIBUTES (t
);
1949 unsigned align
= TYPE_ALIGN (t
);
1950 bool user_align
= TYPE_USER_ALIGN (t
);
1951 bool may_alias
= lookup_attribute ("may_alias", attrs
);
1952 bool packed
= TYPE_PACKED (t
);
1955 fixup_may_alias (t
);
1957 for (variants
= TYPE_NEXT_VARIANT (t
);
1959 variants
= TYPE_NEXT_VARIANT (variants
))
1961 /* These are the two fields that check_qualified_type looks at and
1962 are affected by attributes. */
1963 TYPE_ATTRIBUTES (variants
) = attrs
;
1964 unsigned valign
= align
;
1965 if (TYPE_USER_ALIGN (variants
))
1966 valign
= MAX (valign
, TYPE_ALIGN (variants
));
1968 TYPE_USER_ALIGN (variants
) = user_align
;
1969 SET_TYPE_ALIGN (variants
, valign
);
1970 TYPE_PACKED (variants
) = packed
;
1972 fixup_may_alias (variants
);
1976 /* Set memoizing fields and bits of T (and its variants) for later
1980 finish_struct_bits (tree t
)
1982 /* Fix up variants (if any). */
1983 fixup_type_variants (t
);
1985 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
1986 /* For a class w/o baseclasses, 'finish_struct' has set
1987 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1988 Similarly for a class whose base classes do not have vtables.
1989 When neither of these is true, we might have removed abstract
1990 virtuals (by providing a definition), added some (by declaring
1991 new ones), or redeclared ones from a base class. We need to
1992 recalculate what's really an abstract virtual at this point (by
1993 looking in the vtables). */
1994 get_pure_virtuals (t
);
1996 /* If this type has a copy constructor or a destructor, force its
1997 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1998 nonzero. This will cause it to be passed by invisible reference
1999 and prevent it from being returned in a register. */
2000 if (type_has_nontrivial_copy_init (t
)
2001 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2004 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2005 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2007 SET_TYPE_MODE (variants
, BLKmode
);
2008 TREE_ADDRESSABLE (variants
) = 1;
2013 /* Issue warnings about T having private constructors, but no friends,
2016 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2017 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2018 non-private static member functions. */
2021 maybe_warn_about_overly_private_class (tree t
)
2023 int has_member_fn
= 0;
2024 int has_nonprivate_method
= 0;
2025 bool nonprivate_ctor
= false;
2027 if (!warn_ctor_dtor_privacy
2028 /* If the class has friends, those entities might create and
2029 access instances, so we should not warn. */
2030 || (CLASSTYPE_FRIEND_CLASSES (t
)
2031 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2032 /* We will have warned when the template was declared; there's
2033 no need to warn on every instantiation. */
2034 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2035 /* There's no reason to even consider warning about this
2039 /* We only issue one warning, if more than one applies, because
2040 otherwise, on code like:
2043 // Oops - forgot `public:'
2049 we warn several times about essentially the same problem. */
2051 /* Check to see if all (non-constructor, non-destructor) member
2052 functions are private. (Since there are no friends or
2053 non-private statics, we can't ever call any of the private member
2055 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2056 if (TREE_CODE (fn
) == USING_DECL
2057 && DECL_NAME (fn
) == ctor_identifier
2058 && !TREE_PRIVATE (fn
))
2059 nonprivate_ctor
= true;
2060 else if (!DECL_DECLARES_FUNCTION_P (fn
))
2061 /* Not a function. */;
2062 else if (DECL_ARTIFICIAL (fn
))
2063 /* We're not interested in compiler-generated methods; they don't
2064 provide any way to call private members. */;
2065 else if (!TREE_PRIVATE (fn
))
2067 if (DECL_STATIC_FUNCTION_P (fn
))
2068 /* A non-private static member function is just like a
2069 friend; it can create and invoke private member
2070 functions, and be accessed without a class
2074 has_nonprivate_method
= 1;
2075 /* Keep searching for a static member function. */
2077 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2080 if (!has_nonprivate_method
&& has_member_fn
)
2082 /* There are no non-private methods, and there's at least one
2083 private member function that isn't a constructor or
2084 destructor. (If all the private members are
2085 constructors/destructors we want to use the code below that
2086 issues error messages specifically referring to
2087 constructors/destructors.) */
2089 tree binfo
= TYPE_BINFO (t
);
2091 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2092 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2094 has_nonprivate_method
= 1;
2097 if (!has_nonprivate_method
)
2099 warning (OPT_Wctor_dtor_privacy
,
2100 "all member functions in class %qT are private", t
);
2105 /* Even if some of the member functions are non-private, the class
2106 won't be useful for much if all the constructors or destructors
2107 are private: such an object can never be created or destroyed. */
2108 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
2109 if (TREE_PRIVATE (dtor
))
2111 warning (OPT_Wctor_dtor_privacy
,
2112 "%q#T only defines a private destructor and has no friends",
2117 /* Warn about classes that have private constructors and no friends. */
2118 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2119 /* Implicitly generated constructors are always public. */
2120 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
2122 tree copy_or_move
= NULL_TREE
;
2124 /* If a non-template class does not define a copy
2125 constructor, one is defined for it, enabling it to avoid
2126 this warning. For a template class, this does not
2127 happen, and so we would normally get a warning on:
2129 template <class T> class C { private: C(); };
2131 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2132 complete non-template or fully instantiated classes have this
2134 if (!TYPE_HAS_COPY_CTOR (t
))
2135 nonprivate_ctor
= true;
2137 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
));
2138 !nonprivate_ctor
&& iter
; ++iter
)
2139 if (TREE_PRIVATE (*iter
))
2141 else if (copy_fn_p (*iter
) || move_fn_p (*iter
))
2142 /* Ideally, we wouldn't count any constructor that takes
2143 an argument of the class type as a parameter, because
2144 such things cannot be used to construct an instance of
2145 the class unless you already have one. */
2146 copy_or_move
= *iter
;
2148 nonprivate_ctor
= true;
2150 if (!nonprivate_ctor
)
2152 warning (OPT_Wctor_dtor_privacy
,
2153 "%q#T only defines private constructors and has no friends",
2156 inform (DECL_SOURCE_LOCATION (copy_or_move
),
2157 "%q#D is public, but requires an existing %q#T object",
2164 /* Make BINFO's vtable have N entries, including RTTI entries,
2165 vbase and vcall offsets, etc. Set its type and call the back end
2169 layout_vtable_decl (tree binfo
, int n
)
2174 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2175 layout_type (atype
);
2177 /* We may have to grow the vtable. */
2178 vtable
= get_vtbl_decl_for_binfo (binfo
);
2179 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2181 TREE_TYPE (vtable
) = atype
;
2182 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2183 layout_decl (vtable
, 0);
2187 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2188 have the same signature. */
2191 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2193 /* One destructor overrides another if they are the same kind of
2195 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2196 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2198 /* But a non-destructor never overrides a destructor, nor vice
2199 versa, nor do different kinds of destructors override
2200 one-another. For example, a complete object destructor does not
2201 override a deleting destructor. */
2202 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2205 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2206 || (DECL_CONV_FN_P (fndecl
)
2207 && DECL_CONV_FN_P (base_fndecl
)
2208 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2209 DECL_CONV_FN_TYPE (base_fndecl
))))
2211 tree fntype
= TREE_TYPE (fndecl
);
2212 tree base_fntype
= TREE_TYPE (base_fndecl
);
2213 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2214 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2215 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2216 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2222 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2226 base_derived_from (tree derived
, tree base
)
2230 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2232 if (probe
== derived
)
2234 else if (BINFO_VIRTUAL_P (probe
))
2235 /* If we meet a virtual base, we can't follow the inheritance
2236 any more. See if the complete type of DERIVED contains
2237 such a virtual base. */
2238 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2244 struct find_final_overrider_data
{
2245 /* The function for which we are trying to find a final overrider. */
2247 /* The base class in which the function was declared. */
2248 tree declaring_base
;
2249 /* The candidate overriders. */
2251 /* Path to most derived. */
2255 /* Add the overrider along the current path to FFOD->CANDIDATES.
2256 Returns true if an overrider was found; false otherwise. */
2259 dfs_find_final_overrider_1 (tree binfo
,
2260 find_final_overrider_data
*ffod
,
2265 /* If BINFO is not the most derived type, try a more derived class.
2266 A definition there will overrider a definition here. */
2270 if (dfs_find_final_overrider_1
2271 (ffod
->path
[depth
], ffod
, depth
))
2275 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2278 tree
*candidate
= &ffod
->candidates
;
2280 /* Remove any candidates overridden by this new function. */
2283 /* If *CANDIDATE overrides METHOD, then METHOD
2284 cannot override anything else on the list. */
2285 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2287 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2288 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2289 *candidate
= TREE_CHAIN (*candidate
);
2291 candidate
= &TREE_CHAIN (*candidate
);
2294 /* Add the new function. */
2295 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2302 /* Called from find_final_overrider via dfs_walk. */
2305 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2307 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2309 if (binfo
== ffod
->declaring_base
)
2310 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2311 ffod
->path
.safe_push (binfo
);
2317 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2319 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2325 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2326 FN and whose TREE_VALUE is the binfo for the base where the
2327 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2328 DERIVED) is the base object in which FN is declared. */
2331 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2333 find_final_overrider_data ffod
;
2335 /* Getting this right is a little tricky. This is valid:
2337 struct S { virtual void f (); };
2338 struct T { virtual void f (); };
2339 struct U : public S, public T { };
2341 even though calling `f' in `U' is ambiguous. But,
2343 struct R { virtual void f(); };
2344 struct S : virtual public R { virtual void f (); };
2345 struct T : virtual public R { virtual void f (); };
2346 struct U : public S, public T { };
2348 is not -- there's no way to decide whether to put `S::f' or
2349 `T::f' in the vtable for `R'.
2351 The solution is to look at all paths to BINFO. If we find
2352 different overriders along any two, then there is a problem. */
2353 if (DECL_THUNK_P (fn
))
2354 fn
= THUNK_TARGET (fn
);
2356 /* Determine the depth of the hierarchy. */
2358 ffod
.declaring_base
= binfo
;
2359 ffod
.candidates
= NULL_TREE
;
2360 ffod
.path
.create (30);
2362 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2363 dfs_find_final_overrider_post
, &ffod
);
2365 ffod
.path
.release ();
2367 /* If there was no winner, issue an error message. */
2368 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2369 return error_mark_node
;
2371 return ffod
.candidates
;
2374 /* Return the index of the vcall offset for FN when TYPE is used as a
2378 get_vcall_index (tree fn
, tree type
)
2380 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2384 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2385 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2386 || same_signature_p (fn
, p
->purpose
))
2389 /* There should always be an appropriate index. */
2393 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2394 dominated by T. FN is the old function; VIRTUALS points to the
2395 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2396 of that entry in the list. */
2399 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2407 tree overrider_fn
, overrider_target
;
2408 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2409 tree over_return
, base_return
;
2412 /* Find the nearest primary base (possibly binfo itself) which defines
2413 this function; this is the class the caller will convert to when
2414 calling FN through BINFO. */
2415 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2418 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2421 /* The nearest definition is from a lost primary. */
2422 if (BINFO_LOST_PRIMARY_P (b
))
2427 /* Find the final overrider. */
2428 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2429 if (overrider
== error_mark_node
)
2431 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2434 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2436 /* Check for adjusting covariant return types. */
2437 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2438 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2440 if (INDIRECT_TYPE_P (over_return
)
2441 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2442 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2443 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2444 /* If the overrider is invalid, don't even try. */
2445 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2447 /* If FN is a covariant thunk, we must figure out the adjustment
2448 to the final base FN was converting to. As OVERRIDER_TARGET might
2449 also be converting to the return type of FN, we have to
2450 combine the two conversions here. */
2451 tree fixed_offset
, virtual_offset
;
2453 over_return
= TREE_TYPE (over_return
);
2454 base_return
= TREE_TYPE (base_return
);
2456 if (DECL_THUNK_P (fn
))
2458 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2459 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2460 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2463 fixed_offset
= virtual_offset
= NULL_TREE
;
2466 /* Find the equivalent binfo within the return type of the
2467 overriding function. We will want the vbase offset from
2469 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2471 else if (!same_type_ignoring_top_level_qualifiers_p
2472 (over_return
, base_return
))
2474 /* There was no existing virtual thunk (which takes
2475 precedence). So find the binfo of the base function's
2476 return type within the overriding function's return type.
2477 Fortunately we know the covariancy is valid (it
2478 has already been checked), so we can just iterate along
2479 the binfos, which have been chained in inheritance graph
2480 order. Of course it is lame that we have to repeat the
2481 search here anyway -- we should really be caching pieces
2482 of the vtable and avoiding this repeated work. */
2483 tree thunk_binfo
= NULL_TREE
;
2484 tree base_binfo
= TYPE_BINFO (base_return
);
2486 /* Find the base binfo within the overriding function's
2487 return type. We will always find a thunk_binfo, except
2488 when the covariancy is invalid (which we will have
2489 already diagnosed). */
2491 for (thunk_binfo
= TYPE_BINFO (over_return
); thunk_binfo
;
2492 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2493 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2494 BINFO_TYPE (base_binfo
)))
2496 gcc_assert (thunk_binfo
|| errorcount
);
2498 /* See if virtual inheritance is involved. */
2499 for (virtual_offset
= thunk_binfo
;
2501 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2502 if (BINFO_VIRTUAL_P (virtual_offset
))
2506 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2508 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2512 /* We convert via virtual base. Adjust the fixed
2513 offset to be from there. */
2515 size_diffop (offset
,
2516 fold_convert (ssizetype
,
2517 BINFO_OFFSET (virtual_offset
)));
2520 /* There was an existing fixed offset, this must be
2521 from the base just converted to, and the base the
2522 FN was thunking to. */
2523 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2525 fixed_offset
= offset
;
2529 if (fixed_offset
|| virtual_offset
)
2530 /* Replace the overriding function with a covariant thunk. We
2531 will emit the overriding function in its own slot as
2533 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2534 fixed_offset
, virtual_offset
);
2537 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2538 !DECL_THUNK_P (fn
));
2540 /* If we need a covariant thunk, then we may need to adjust first_defn.
2541 The ABI specifies that the thunks emitted with a function are
2542 determined by which bases the function overrides, so we need to be
2543 sure that we're using a thunk for some overridden base; even if we
2544 know that the necessary this adjustment is zero, there may not be an
2545 appropriate zero-this-adjustment thunk for us to use since thunks for
2546 overriding virtual bases always use the vcall offset.
2548 Furthermore, just choosing any base that overrides this function isn't
2549 quite right, as this slot won't be used for calls through a type that
2550 puts a covariant thunk here. Calling the function through such a type
2551 will use a different slot, and that slot is the one that determines
2552 the thunk emitted for that base.
2554 So, keep looking until we find the base that we're really overriding
2555 in this slot: the nearest primary base that doesn't use a covariant
2556 thunk in this slot. */
2557 if (overrider_target
!= overrider_fn
)
2559 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2560 /* We already know that the overrider needs a covariant thunk. */
2561 b
= get_primary_binfo (b
);
2562 for (; ; b
= get_primary_binfo (b
))
2564 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2565 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2566 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2568 if (BINFO_LOST_PRIMARY_P (b
))
2574 /* Assume that we will produce a thunk that convert all the way to
2575 the final overrider, and not to an intermediate virtual base. */
2576 virtual_base
= NULL_TREE
;
2578 /* See if we can convert to an intermediate virtual base first, and then
2579 use the vcall offset located there to finish the conversion. */
2580 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2582 /* If we find the final overrider, then we can stop
2584 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2585 BINFO_TYPE (TREE_VALUE (overrider
))))
2588 /* If we find a virtual base, and we haven't yet found the
2589 overrider, then there is a virtual base between the
2590 declaring base (first_defn) and the final overrider. */
2591 if (BINFO_VIRTUAL_P (b
))
2598 /* Compute the constant adjustment to the `this' pointer. The
2599 `this' pointer, when this function is called, will point at BINFO
2600 (or one of its primary bases, which are at the same offset). */
2602 /* The `this' pointer needs to be adjusted from the declaration to
2603 the nearest virtual base. */
2604 delta
= size_diffop_loc (input_location
,
2605 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2606 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2608 /* If the nearest definition is in a lost primary, we don't need an
2609 entry in our vtable. Except possibly in a constructor vtable,
2610 if we happen to get our primary back. In that case, the offset
2611 will be zero, as it will be a primary base. */
2612 delta
= size_zero_node
;
2614 /* The `this' pointer needs to be adjusted from pointing to
2615 BINFO to pointing at the base where the final overrider
2617 delta
= size_diffop_loc (input_location
,
2618 fold_convert (ssizetype
,
2619 BINFO_OFFSET (TREE_VALUE (overrider
))),
2620 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2622 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2625 BV_VCALL_INDEX (*virtuals
)
2626 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2628 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2630 BV_LOST_PRIMARY (*virtuals
) = lost
;
2633 /* Called from modify_all_vtables via dfs_walk. */
2636 dfs_modify_vtables (tree binfo
, void* data
)
2638 tree t
= (tree
) data
;
2643 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2644 /* A base without a vtable needs no modification, and its bases
2645 are uninteresting. */
2646 return dfs_skip_bases
;
2648 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2649 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2650 /* Don't do the primary vtable, if it's new. */
2653 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2654 /* There's no need to modify the vtable for a non-virtual primary
2655 base; we're not going to use that vtable anyhow. We do still
2656 need to do this for virtual primary bases, as they could become
2657 non-primary in a construction vtable. */
2660 make_new_vtable (t
, binfo
);
2662 /* Now, go through each of the virtual functions in the virtual
2663 function table for BINFO. Find the final overrider, and update
2664 the BINFO_VIRTUALS list appropriately. */
2665 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2666 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2668 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2669 old_virtuals
= TREE_CHAIN (old_virtuals
))
2670 update_vtable_entry_for_fn (t
,
2672 BV_FN (old_virtuals
),
2678 /* Update all of the primary and secondary vtables for T. Create new
2679 vtables as required, and initialize their RTTI information. Each
2680 of the functions in VIRTUALS is declared in T and may override a
2681 virtual function from a base class; find and modify the appropriate
2682 entries to point to the overriding functions. Returns a list, in
2683 declaration order, of the virtual functions that are declared in T,
2684 but do not appear in the primary base class vtable, and which
2685 should therefore be appended to the end of the vtable for T. */
2688 modify_all_vtables (tree t
, tree virtuals
)
2690 tree binfo
= TYPE_BINFO (t
);
2693 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2694 if (TYPE_CONTAINS_VPTR_P (t
))
2695 get_vtable_decl (t
, false);
2697 /* Update all of the vtables. */
2698 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2700 /* Add virtual functions not already in our primary vtable. These
2701 will be both those introduced by this class, and those overridden
2702 from secondary bases. It does not include virtuals merely
2703 inherited from secondary bases. */
2704 for (fnsp
= &virtuals
; *fnsp
; )
2706 tree fn
= TREE_VALUE (*fnsp
);
2708 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2709 || DECL_VINDEX (fn
) == error_mark_node
)
2711 /* We don't need to adjust the `this' pointer when
2712 calling this function. */
2713 BV_DELTA (*fnsp
) = integer_zero_node
;
2714 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2716 /* This is a function not already in our vtable. Keep it. */
2717 fnsp
= &TREE_CHAIN (*fnsp
);
2720 /* We've already got an entry for this function. Skip it. */
2721 *fnsp
= TREE_CHAIN (*fnsp
);
2727 /* Get the base virtual function declarations in T that have the
2731 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2733 bool found_decls
= false;
2735 /* Find virtual functions in T with the indicated NAME. */
2736 for (ovl_iterator
iter (get_class_binding (t
, name
)); iter
; ++iter
)
2738 tree method
= *iter
;
2740 if (TREE_CODE (method
) == FUNCTION_DECL
&& DECL_VINDEX (method
))
2742 base_fndecls
->safe_push (method
);
2750 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2751 for (int i
= 0; i
< n_baseclasses
; i
++)
2753 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2754 get_basefndecls (name
, basetype
, base_fndecls
);
2758 /* If this declaration supersedes the declaration of
2759 a method declared virtual in the base class, then
2760 mark this field as being virtual as well. */
2763 check_for_override (tree decl
, tree ctype
)
2765 bool overrides_found
= false;
2766 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2767 /* In [temp.mem] we have:
2769 A specialization of a member function template does not
2770 override a virtual function from a base class. */
2772 if ((DECL_DESTRUCTOR_P (decl
)
2773 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2774 || DECL_CONV_FN_P (decl
))
2775 && look_for_overrides (ctype
, decl
)
2776 && !DECL_STATIC_FUNCTION_P (decl
))
2777 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2778 the error_mark_node so that we know it is an overriding
2781 DECL_VINDEX (decl
) = decl
;
2782 overrides_found
= true;
2783 if (warn_override
&& !DECL_OVERRIDE_P (decl
)
2784 && !DECL_DESTRUCTOR_P (decl
))
2785 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2786 "%qD can be marked override", decl
);
2789 if (DECL_VIRTUAL_P (decl
))
2791 if (!DECL_VINDEX (decl
))
2792 DECL_VINDEX (decl
) = error_mark_node
;
2793 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2794 if (DECL_DESTRUCTOR_P (decl
))
2795 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2797 else if (DECL_FINAL_P (decl
))
2798 error ("%q+#D marked %<final%>, but is not virtual", decl
);
2799 if (DECL_OVERRIDE_P (decl
) && !overrides_found
)
2800 error ("%q+#D marked %<override%>, but does not override", decl
);
2803 /* Warn about hidden virtual functions that are not overridden in t.
2804 We know that constructors and destructors don't apply. */
2807 warn_hidden (tree t
)
2809 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (t
))
2810 for (unsigned ix
= member_vec
->length (); ix
--;)
2812 tree fns
= (*member_vec
)[ix
];
2817 tree name
= OVL_NAME (fns
);
2818 auto_vec
<tree
, 20> base_fndecls
;
2823 /* Iterate through all of the base classes looking for possibly
2824 hidden functions. */
2825 for (binfo
= TYPE_BINFO (t
), j
= 0;
2826 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2828 tree basetype
= BINFO_TYPE (base_binfo
);
2829 get_basefndecls (name
, basetype
, &base_fndecls
);
2832 /* If there are no functions to hide, continue. */
2833 if (base_fndecls
.is_empty ())
2836 /* Remove any overridden functions. */
2837 for (ovl_iterator
iter (fns
); iter
; ++iter
)
2839 tree fndecl
= *iter
;
2840 if (TREE_CODE (fndecl
) == FUNCTION_DECL
2841 && DECL_VINDEX (fndecl
))
2843 /* If the method from the base class has the same
2844 signature as the method from the derived class, it
2845 has been overridden. */
2846 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
2848 && same_signature_p (fndecl
, base_fndecls
[k
]))
2849 base_fndecls
[k
] = NULL_TREE
;
2853 /* Now give a warning for all base functions without overriders,
2854 as they are hidden. */
2856 FOR_EACH_VEC_ELT (base_fndecls
, j
, base_fndecl
)
2859 /* Here we know it is a hider, and no overrider exists. */
2860 warning_at (location_of (base_fndecl
),
2861 OPT_Woverloaded_virtual
,
2862 "%qD was hidden", base_fndecl
);
2863 warning_at (location_of (fns
),
2864 OPT_Woverloaded_virtual
, " by %qD", fns
);
2869 /* Recursive helper for finish_struct_anon. */
2872 finish_struct_anon_r (tree field
, bool complain
)
2874 for (tree elt
= TYPE_FIELDS (TREE_TYPE (field
)); elt
; elt
= DECL_CHAIN (elt
))
2876 /* We're generally only interested in entities the user
2877 declared, but we also find nested classes by noticing
2878 the TYPE_DECL that we create implicitly. You're
2879 allowed to put one anonymous union inside another,
2880 though, so we explicitly tolerate that. We use
2881 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2882 we also allow unnamed types used for defining fields. */
2883 if (DECL_ARTIFICIAL (elt
)
2884 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2885 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
2889 && (TREE_CODE (elt
) != FIELD_DECL
2890 || (TREE_PRIVATE (elt
) || TREE_PROTECTED (elt
))))
2892 /* We already complained about static data members in
2893 finish_static_data_member_decl. */
2896 auto_diagnostic_group d
;
2897 if (permerror (DECL_SOURCE_LOCATION (elt
),
2898 TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
2899 ? "%q#D invalid; an anonymous union may "
2900 "only have public non-static data members"
2901 : "%q#D invalid; an anonymous struct may "
2902 "only have public non-static data members", elt
))
2905 if (flag_permissive
&& !hint
)
2908 inform (DECL_SOURCE_LOCATION (elt
),
2909 "this flexibility is deprecated and will be "
2916 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
2917 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
2919 /* Recurse into the anonymous aggregates to correctly handle
2920 access control (c++/24926):
2931 if (DECL_NAME (elt
) == NULL_TREE
2932 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
2933 finish_struct_anon_r (elt
, /*complain=*/false);
2937 /* Check for things that are invalid. There are probably plenty of other
2938 things we should check for also. */
2941 finish_struct_anon (tree t
)
2943 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2945 if (TREE_STATIC (field
))
2947 if (TREE_CODE (field
) != FIELD_DECL
)
2950 if (DECL_NAME (field
) == NULL_TREE
2951 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2952 finish_struct_anon_r (field
, /*complain=*/true);
2956 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2957 will be used later during class template instantiation.
2958 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2959 a non-static member data (FIELD_DECL), a member function
2960 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2961 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2962 When FRIEND_P is nonzero, T is either a friend class
2963 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2964 (FUNCTION_DECL, TEMPLATE_DECL). */
2967 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
2969 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2970 if (CLASSTYPE_TEMPLATE_INFO (type
))
2971 CLASSTYPE_DECL_LIST (type
)
2972 = tree_cons (friend_p
? NULL_TREE
: type
,
2973 t
, CLASSTYPE_DECL_LIST (type
));
2976 /* This function is called from declare_virt_assop_and_dtor via
2979 DATA is a type that direcly or indirectly inherits the base
2980 represented by BINFO. If BINFO contains a virtual assignment [copy
2981 assignment or move assigment] operator or a virtual constructor,
2982 declare that function in DATA if it hasn't been already declared. */
2985 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
2987 tree bv
, fn
, t
= (tree
)data
;
2988 tree opname
= assign_op_identifier
;
2990 gcc_assert (t
&& CLASS_TYPE_P (t
));
2991 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
2993 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2994 /* A base without a vtable needs no modification, and its bases
2995 are uninteresting. */
2996 return dfs_skip_bases
;
2998 if (BINFO_PRIMARY_P (binfo
))
2999 /* If this is a primary base, then we have already looked at the
3000 virtual functions of its vtable. */
3003 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3007 if (DECL_NAME (fn
) == opname
)
3009 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3010 lazily_declare_fn (sfk_copy_assignment
, t
);
3011 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3012 lazily_declare_fn (sfk_move_assignment
, t
);
3014 else if (DECL_DESTRUCTOR_P (fn
)
3015 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3016 lazily_declare_fn (sfk_destructor
, t
);
3022 /* If the class type T has a direct or indirect base that contains a
3023 virtual assignment operator or a virtual destructor, declare that
3024 function in T if it hasn't been already declared. */
3027 declare_virt_assop_and_dtor (tree t
)
3029 if (!(TYPE_POLYMORPHIC_P (t
)
3030 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3031 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3032 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3035 dfs_walk_all (TYPE_BINFO (t
),
3036 dfs_declare_virt_assop_and_dtor
,
3040 /* Declare the inheriting constructor for class T inherited from base
3041 constructor CTOR with the parameter array PARMS of size NPARMS. */
3044 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3046 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3048 /* We don't declare an inheriting ctor that would be a default,
3049 copy or move ctor for derived or base. */
3053 && TYPE_REF_P (parms
[0]))
3055 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3056 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3060 tree parmlist
= void_list_node
;
3061 for (int i
= nparms
- 1; i
>= 0; i
--)
3062 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3063 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3064 t
, false, ctor
, parmlist
);
3066 if (add_method (t
, fn
, false))
3068 DECL_CHAIN (fn
) = TYPE_FIELDS (t
);
3069 TYPE_FIELDS (t
) = fn
;
3073 /* Declare all the inheriting constructors for class T inherited from base
3074 constructor CTOR. */
3077 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3079 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3081 if (flag_new_inheriting_ctors
)
3083 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3084 t
, /*const*/false, ctor
, parms
);
3085 add_method (t
, ctor
, using_decl
!= NULL_TREE
);
3086 TYPE_HAS_USER_CONSTRUCTOR (t
) = true;
3090 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3092 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3094 if (TREE_PURPOSE (parms
))
3095 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3096 new_parms
[i
++] = TREE_VALUE (parms
);
3098 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3099 if (parms
== NULL_TREE
)
3101 auto_diagnostic_group d
;
3102 if (warning (OPT_Winherited_variadic_ctor
,
3103 "the ellipsis in %qD is not inherited", ctor
))
3104 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3108 /* Create default constructors, assignment operators, and so forth for
3109 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3110 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3111 the class cannot have a default constructor, copy constructor
3112 taking a const reference argument, or an assignment operator taking
3113 a const reference, respectively. */
3116 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3117 int cant_have_const_cctor
,
3118 int cant_have_const_assignment
)
3121 if (!CLASSTYPE_DESTRUCTOR (t
))
3122 /* In general, we create destructors lazily. */
3123 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3125 bool move_ok
= false;
3126 if (cxx_dialect
>= cxx11
&& CLASSTYPE_LAZY_DESTRUCTOR (t
)
3127 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3128 && !classtype_has_move_assign_or_move_ctor_p (t
, false))
3133 If there is no user-declared constructor for a class, a default
3134 constructor is implicitly declared. */
3135 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3137 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3138 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3139 if (cxx_dialect
>= cxx11
)
3140 TYPE_HAS_CONSTEXPR_CTOR (t
)
3141 /* Don't force the declaration to get a hard answer; if the
3142 definition would have made the class non-literal, it will still be
3143 non-literal because of the base or member in question, and that
3144 gives a better diagnostic. */
3145 = type_maybe_constexpr_default_constructor (t
);
3150 If a class definition does not explicitly declare a copy
3151 constructor, one is declared implicitly. */
3152 if (! TYPE_HAS_COPY_CTOR (t
))
3154 TYPE_HAS_COPY_CTOR (t
) = 1;
3155 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3156 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3158 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3161 /* If there is no assignment operator, one will be created if and
3162 when it is needed. For now, just record whether or not the type
3163 of the parameter to the assignment operator will be a const or
3164 non-const reference. */
3165 if (!TYPE_HAS_COPY_ASSIGN (t
))
3167 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3168 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3169 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3170 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3171 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3174 /* We can't be lazy about declaring functions that might override
3175 a virtual function from a base class. */
3176 declare_virt_assop_and_dtor (t
);
3178 while (*access_decls
)
3180 tree using_decl
= TREE_VALUE (*access_decls
);
3181 tree decl
= USING_DECL_DECLS (using_decl
);
3182 if (DECL_NAME (using_decl
) == ctor_identifier
)
3184 /* declare, then remove the decl */
3185 tree ctor_list
= decl
;
3186 location_t loc
= input_location
;
3187 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3188 for (ovl_iterator
iter (ctor_list
); iter
; ++iter
)
3189 one_inherited_ctor (*iter
, t
, using_decl
);
3190 *access_decls
= TREE_CHAIN (*access_decls
);
3191 input_location
= loc
;
3194 access_decls
= &TREE_CHAIN (*access_decls
);
3198 /* FIELD is a bit-field. We are finishing the processing for its
3199 enclosing type. Issue any appropriate messages and set appropriate
3200 flags. Returns false if an error has been diagnosed. */
3203 check_bitfield_decl (tree field
)
3205 tree type
= TREE_TYPE (field
);
3208 /* Extract the declared width of the bitfield, which has been
3209 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3210 w
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
3211 gcc_assert (w
!= NULL_TREE
);
3212 /* Remove the bit-field width indicator so that the rest of the
3213 compiler does not treat that value as a qualifier. */
3214 DECL_BIT_FIELD_REPRESENTATIVE (field
) = NULL_TREE
;
3216 /* Detect invalid bit-field type. */
3217 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3219 error_at (DECL_SOURCE_LOCATION (field
),
3220 "bit-field %q#D with non-integral type %qT", field
, type
);
3221 w
= error_mark_node
;
3225 location_t loc
= input_location
;
3226 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3229 /* detect invalid field size. */
3230 input_location
= DECL_SOURCE_LOCATION (field
);
3231 w
= cxx_constant_value (w
);
3232 input_location
= loc
;
3234 if (TREE_CODE (w
) != INTEGER_CST
)
3236 error ("bit-field %q+D width not an integer constant", field
);
3237 w
= error_mark_node
;
3239 else if (tree_int_cst_sgn (w
) < 0)
3241 error ("negative width in bit-field %q+D", field
);
3242 w
= error_mark_node
;
3244 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3246 error ("zero width for bit-field %q+D", field
);
3247 w
= error_mark_node
;
3249 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3250 && TREE_CODE (type
) != BOOLEAN_TYPE
3251 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3252 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3253 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3254 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3255 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3256 "width of %qD exceeds its type", field
);
3257 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3259 int prec
= TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
));
3260 if (compare_tree_int (w
, prec
) < 0)
3261 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3262 "%qD is too small to hold all values of %q#T",
3267 if (w
!= error_mark_node
)
3269 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3270 DECL_BIT_FIELD (field
) = 1;
3275 /* Non-bit-fields are aligned for their type. */
3276 DECL_BIT_FIELD (field
) = 0;
3277 CLEAR_DECL_C_BIT_FIELD (field
);
3282 /* FIELD is a non bit-field. We are finishing the processing for its
3283 enclosing type T. Issue any appropriate messages and set appropriate
3287 check_field_decl (tree field
,
3289 int* cant_have_const_ctor
,
3290 int* no_const_asn_ref
)
3292 tree type
= strip_array_types (TREE_TYPE (field
));
3293 bool any_default_members
= false;
3295 /* In C++98 an anonymous union cannot contain any fields which would change
3296 the settings of CANT_HAVE_CONST_CTOR and friends. */
3297 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3299 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3300 structs. So, we recurse through their fields here. */
3301 else if (ANON_AGGR_TYPE_P (type
))
3303 for (tree fields
= TYPE_FIELDS (type
); fields
;
3304 fields
= DECL_CHAIN (fields
))
3305 if (TREE_CODE (fields
) == FIELD_DECL
)
3306 any_default_members
|= check_field_decl (fields
, t
,
3307 cant_have_const_ctor
,
3310 /* Check members with class type for constructors, destructors,
3312 else if (CLASS_TYPE_P (type
))
3314 /* Never let anything with uninheritable virtuals
3315 make it through without complaint. */
3316 abstract_virtuals_error (field
, type
);
3318 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3321 int oldcount
= errorcount
;
3322 if (TYPE_NEEDS_CONSTRUCTING (type
))
3323 error ("member %q+#D with constructor not allowed in union",
3325 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3326 error ("member %q+#D with destructor not allowed in union", field
);
3327 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3328 error ("member %q+#D with copy assignment operator not allowed in union",
3330 if (!warned
&& errorcount
> oldcount
)
3332 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3333 "only available with -std=c++11 or -std=gnu++11");
3339 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3340 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3341 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3342 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3343 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3344 || !TYPE_HAS_COPY_ASSIGN (type
));
3345 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3346 || !TYPE_HAS_COPY_CTOR (type
));
3347 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3348 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3349 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3350 || TYPE_HAS_COMPLEX_DFLT (type
));
3353 if (TYPE_HAS_COPY_CTOR (type
)
3354 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3355 *cant_have_const_ctor
= 1;
3357 if (TYPE_HAS_COPY_ASSIGN (type
)
3358 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3359 *no_const_asn_ref
= 1;
3362 check_abi_tags (t
, field
);
3364 if (DECL_INITIAL (field
) != NULL_TREE
)
3365 /* `build_class_init_list' does not recognize
3367 any_default_members
= true;
3369 return any_default_members
;
3372 /* Check the data members (both static and non-static), class-scoped
3373 typedefs, etc., appearing in the declaration of T. Issue
3374 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3375 declaration order) of access declarations; each TREE_VALUE in this
3376 list is a USING_DECL.
3378 In addition, set the following flags:
3381 The class is empty, i.e., contains no non-static data members.
3383 CANT_HAVE_CONST_CTOR_P
3384 This class cannot have an implicitly generated copy constructor
3385 taking a const reference.
3387 CANT_HAVE_CONST_ASN_REF
3388 This class cannot have an implicitly generated assignment
3389 operator taking a const reference.
3391 All of these flags should be initialized before calling this
3394 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3395 fields can be added by adding to this chain. */
3398 check_field_decls (tree t
, tree
*access_decls
,
3399 int *cant_have_const_ctor_p
,
3400 int *no_const_asn_ref_p
)
3405 bool any_default_members
;
3407 int field_access
= -1;
3409 /* Assume there are no access declarations. */
3410 *access_decls
= NULL_TREE
;
3411 /* Assume this class has no pointer members. */
3412 has_pointers
= false;
3413 /* Assume none of the members of this class have default
3415 any_default_members
= false;
3417 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3420 tree type
= TREE_TYPE (x
);
3421 int this_field_access
;
3423 next
= &DECL_CHAIN (x
);
3425 if (TREE_CODE (x
) == USING_DECL
)
3427 /* Save the access declarations for our caller. */
3428 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3432 if (TREE_CODE (x
) == TYPE_DECL
3433 || TREE_CODE (x
) == TEMPLATE_DECL
)
3436 if (TREE_CODE (x
) == FUNCTION_DECL
)
3437 /* FIXME: We should fold in the checking from check_methods. */
3440 /* If we've gotten this far, it's a data member, possibly static,
3441 or an enumerator. */
3442 if (TREE_CODE (x
) != CONST_DECL
)
3443 DECL_CONTEXT (x
) = t
;
3445 /* When this goes into scope, it will be a non-local reference. */
3446 DECL_NONLOCAL (x
) = 1;
3448 if (TREE_CODE (t
) == UNION_TYPE
)
3450 /* [class.union] (C++98)
3452 If a union contains a static data member, or a member of
3453 reference type, the program is ill-formed.
3455 In C++11 [class.union] says:
3456 If a union contains a non-static data member of reference type
3457 the program is ill-formed. */
3458 if (VAR_P (x
) && cxx_dialect
< cxx11
)
3460 error ("in C++98 %q+D may not be static because it is "
3461 "a member of a union", x
);
3464 if (TYPE_REF_P (type
)
3465 && TREE_CODE (x
) == FIELD_DECL
)
3467 error ("non-static data member %q+D in a union may not "
3468 "have reference type %qT", x
, type
);
3473 /* Perform error checking that did not get done in
3475 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3477 error ("field %q+D invalidly declared function type", x
);
3478 type
= build_pointer_type (type
);
3479 TREE_TYPE (x
) = type
;
3481 else if (TREE_CODE (type
) == METHOD_TYPE
)
3483 error ("field %q+D invalidly declared method type", x
);
3484 type
= build_pointer_type (type
);
3485 TREE_TYPE (x
) = type
;
3488 if (type
== error_mark_node
)
3491 if (TREE_CODE (x
) == CONST_DECL
|| VAR_P (x
))
3494 /* Now it can only be a FIELD_DECL. */
3496 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3497 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3499 /* If at least one non-static data member is non-literal, the whole
3500 class becomes non-literal. Per Core/1453, volatile non-static
3501 data members and base classes are also not allowed.
3502 Note: if the type is incomplete we will complain later on. */
3503 if (COMPLETE_TYPE_P (type
)
3504 && (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
)))
3505 CLASSTYPE_LITERAL_P (t
) = false;
3507 /* A standard-layout class is a class that:
3509 has the same access control (Clause 11) for all non-static data members,
3511 this_field_access
= TREE_PROTECTED (x
) ? 1 : TREE_PRIVATE (x
) ? 2 : 0;
3512 if (field_access
== -1)
3513 field_access
= this_field_access
;
3514 else if (this_field_access
!= field_access
)
3515 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3517 /* If this is of reference type, check if it needs an init. */
3518 if (TYPE_REF_P (type
))
3520 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3521 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3522 if (DECL_INITIAL (x
) == NULL_TREE
)
3523 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3524 if (cxx_dialect
< cxx11
)
3526 /* ARM $12.6.2: [A member initializer list] (or, for an
3527 aggregate, initialization by a brace-enclosed list) is the
3528 only way to initialize nonstatic const and reference
3530 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3531 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3535 type
= strip_array_types (type
);
3537 if (TYPE_PACKED (t
))
3539 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3542 (DECL_SOURCE_LOCATION (x
), 0,
3543 "ignoring packed attribute because of unpacked non-POD field %q#D",
3547 else if (DECL_C_BIT_FIELD (x
)
3548 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
)
3549 DECL_PACKED (x
) = 1;
3552 if (DECL_C_BIT_FIELD (x
)
3553 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x
)))
3554 /* We don't treat zero-width bitfields as making a class
3557 else if (field_poverlapping_p (x
) && is_empty_class (type
))
3559 /* Empty data members also don't make a class non-empty. */
3560 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3564 /* The class is non-empty. */
3565 CLASSTYPE_EMPTY_P (t
) = 0;
3566 /* The class is not even nearly empty. */
3567 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3568 /* If one of the data members contains an empty class,
3570 if (CLASS_TYPE_P (type
)
3571 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3572 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3575 /* This is used by -Weffc++ (see below). Warn only for pointers
3576 to members which might hold dynamic memory. So do not warn
3577 for pointers to functions or pointers to members. */
3578 if (TYPE_PTR_P (type
)
3579 && !TYPE_PTRFN_P (type
))
3580 has_pointers
= true;
3582 if (CLASS_TYPE_P (type
))
3584 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3585 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3586 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3587 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3590 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3591 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3593 if (DECL_MUTABLE_P (x
))
3595 if (CP_TYPE_CONST_P (type
))
3597 error ("member %q+D cannot be declared both %<const%> "
3598 "and %<mutable%>", x
);
3601 if (TYPE_REF_P (type
))
3603 error ("member %q+D cannot be declared as a %<mutable%> "
3609 if (! layout_pod_type_p (type
))
3610 /* DR 148 now allows pointers to members (which are POD themselves),
3611 to be allowed in POD structs. */
3612 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3614 if (field_poverlapping_p (x
))
3615 /* A potentially-overlapping non-static data member makes the class
3617 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3619 if (!std_layout_type_p (type
))
3620 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3622 if (! zero_init_p (type
))
3623 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3625 /* We set DECL_C_BIT_FIELD in grokbitfield.
3626 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3627 if (DECL_C_BIT_FIELD (x
))
3628 check_bitfield_decl (x
);
3630 if (check_field_decl (x
, t
, cant_have_const_ctor_p
, no_const_asn_ref_p
))
3632 if (any_default_members
3633 && TREE_CODE (t
) == UNION_TYPE
)
3634 error ("multiple fields in union %qT initialized", t
);
3635 any_default_members
= true;
3638 /* Now that we've removed bit-field widths from DECL_INITIAL,
3639 anything left in DECL_INITIAL is an NSDMI that makes the class
3640 non-aggregate in C++11. */
3641 if (DECL_INITIAL (x
) && cxx_dialect
< cxx14
)
3642 CLASSTYPE_NON_AGGREGATE (t
) = true;
3644 /* If any field is const, the structure type is pseudo-const. */
3645 if (CP_TYPE_CONST_P (type
))
3647 C_TYPE_FIELDS_READONLY (t
) = 1;
3648 if (DECL_INITIAL (x
) == NULL_TREE
)
3649 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3650 if (cxx_dialect
< cxx11
)
3652 /* ARM $12.6.2: [A member initializer list] (or, for an
3653 aggregate, initialization by a brace-enclosed list) is the
3654 only way to initialize nonstatic const and reference
3656 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3657 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3660 /* A field that is pseudo-const makes the structure likewise. */
3661 else if (CLASS_TYPE_P (type
))
3663 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3664 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3665 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3666 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3669 /* Core issue 80: A nonstatic data member is required to have a
3670 different name from the class iff the class has a
3671 user-declared constructor. */
3672 if (constructor_name_p (DECL_NAME (x
), t
)
3673 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3674 permerror (DECL_SOURCE_LOCATION (x
),
3675 "field %q#D with same name as class", x
);
3678 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3679 it should also define a copy constructor and an assignment operator to
3680 implement the correct copy semantic (deep vs shallow, etc.). As it is
3681 not feasible to check whether the constructors do allocate dynamic memory
3682 and store it within members, we approximate the warning like this:
3684 -- Warn only if there are members which are pointers
3685 -- Warn only if there is a non-trivial constructor (otherwise,
3686 there cannot be memory allocated).
3687 -- Warn only if there is a non-trivial destructor. We assume that the
3688 user at least implemented the cleanup correctly, and a destructor
3689 is needed to free dynamic memory.
3691 This seems enough for practical purposes. */
3694 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3695 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3696 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3698 warning (OPT_Weffc__
, "%q#T has pointer data members", t
);
3700 if (! TYPE_HAS_COPY_CTOR (t
))
3702 warning (OPT_Weffc__
,
3703 " but does not override %<%T(const %T&)%>", t
, t
);
3704 if (!TYPE_HAS_COPY_ASSIGN (t
))
3705 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3707 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3708 warning (OPT_Weffc__
,
3709 " but does not override %<operator=(const %T&)%>", t
);
3712 /* Non-static data member initializers make the default constructor
3714 if (any_default_members
)
3716 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3717 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3720 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3722 TYPE_PACKED (t
) = 0;
3724 /* Check anonymous struct/anonymous union fields. */
3725 finish_struct_anon (t
);
3727 /* We've built up the list of access declarations in reverse order.
3729 *access_decls
= nreverse (*access_decls
);
3732 /* If TYPE is an empty class type, records its OFFSET in the table of
3736 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3740 if (!is_empty_class (type
))
3743 /* Record the location of this empty object in OFFSETS. */
3744 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3746 n
= splay_tree_insert (offsets
,
3747 (splay_tree_key
) offset
,
3748 (splay_tree_value
) NULL_TREE
);
3749 n
->value
= ((splay_tree_value
)
3750 tree_cons (NULL_TREE
,
3757 /* Returns nonzero if TYPE is an empty class type and there is
3758 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3761 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3766 if (!is_empty_class (type
))
3769 /* Record the location of this empty object in OFFSETS. */
3770 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3774 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3775 if (same_type_p (TREE_VALUE (t
), type
))
3781 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3782 F for every subobject, passing it the type, offset, and table of
3783 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3786 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3787 than MAX_OFFSET will not be walked.
3789 If F returns a nonzero value, the traversal ceases, and that value
3790 is returned. Otherwise, returns zero. */
3793 walk_subobject_offsets (tree type
,
3794 subobject_offset_fn f
,
3801 tree type_binfo
= NULL_TREE
;
3803 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3805 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3808 if (type
== error_mark_node
)
3814 type
= BINFO_TYPE (type
);
3817 if (CLASS_TYPE_P (type
))
3823 /* Avoid recursing into objects that are not interesting. */
3824 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3827 /* Record the location of TYPE. */
3828 r
= (*f
) (type
, offset
, offsets
);
3832 /* Iterate through the direct base classes of TYPE. */
3834 type_binfo
= TYPE_BINFO (type
);
3835 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
3839 if (BINFO_VIRTUAL_P (binfo
))
3843 /* We cannot rely on BINFO_OFFSET being set for the base
3844 class yet, but the offsets for direct non-virtual
3845 bases can be calculated by going back to the TYPE. */
3846 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
3847 binfo_offset
= size_binop (PLUS_EXPR
,
3849 BINFO_OFFSET (orig_binfo
));
3851 r
= walk_subobject_offsets (binfo
,
3861 if (CLASSTYPE_VBASECLASSES (type
))
3864 vec
<tree
, va_gc
> *vbases
;
3866 /* Iterate through the virtual base classes of TYPE. In G++
3867 3.2, we included virtual bases in the direct base class
3868 loop above, which results in incorrect results; the
3869 correct offsets for virtual bases are only known when
3870 working with the most derived type. */
3872 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
3873 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
3875 r
= walk_subobject_offsets (binfo
,
3877 size_binop (PLUS_EXPR
,
3879 BINFO_OFFSET (binfo
)),
3888 /* We still have to walk the primary base, if it is
3889 virtual. (If it is non-virtual, then it was walked
3891 tree vbase
= get_primary_binfo (type_binfo
);
3893 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
3894 && BINFO_PRIMARY_P (vbase
)
3895 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
3897 r
= (walk_subobject_offsets
3899 offsets
, max_offset
, /*vbases_p=*/0));
3906 /* Iterate through the fields of TYPE. */
3907 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
3908 if (TREE_CODE (field
) == FIELD_DECL
3909 && TREE_TYPE (field
) != error_mark_node
3910 && !DECL_ARTIFICIAL (field
))
3914 field_offset
= byte_position (field
);
3916 r
= walk_subobject_offsets (TREE_TYPE (field
),
3918 size_binop (PLUS_EXPR
,
3928 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3930 tree element_type
= strip_array_types (type
);
3931 tree domain
= TYPE_DOMAIN (type
);
3934 /* Avoid recursing into objects that are not interesting. */
3935 if (!CLASS_TYPE_P (element_type
)
3936 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
3938 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
3941 /* Step through each of the elements in the array. */
3942 for (index
= size_zero_node
;
3943 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
3944 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
3946 r
= walk_subobject_offsets (TREE_TYPE (type
),
3954 offset
= size_binop (PLUS_EXPR
, offset
,
3955 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
3956 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3957 there's no point in iterating through the remaining
3958 elements of the array. */
3959 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3967 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
3970 field_poverlapping_p (tree decl
)
3972 /* Base fields are actually potentially overlapping, but C++ bases go through
3973 a different code path based on binfos, and ObjC++ base fields are laid out
3974 in objc-act, so we don't want layout_class_type to mess with them. */
3975 if (DECL_FIELD_IS_BASE (decl
))
3977 gcc_checking_assert (c_dialect_objc ());
3981 return lookup_attribute ("no_unique_address",
3982 DECL_ATTRIBUTES (decl
));
3985 /* Record all of the empty subobjects of DECL_OR_BINFO. */
3988 record_subobject_offsets (tree decl_or_binfo
,
3992 bool overlapping
, vbases_p
;
3994 if (DECL_P (decl_or_binfo
))
3996 tree decl
= decl_or_binfo
;
3997 type
= TREE_TYPE (decl
);
3998 offset
= byte_position (decl
);
3999 overlapping
= field_poverlapping_p (decl
);
4004 type
= BINFO_TYPE (decl_or_binfo
);
4005 offset
= BINFO_OFFSET (decl_or_binfo
);
4011 /* If recording subobjects for a non-static data member or a
4012 non-empty base class, we do not need to record offsets beyond
4013 the size of the biggest empty class. Additional data members
4014 will go at the end of the class. Additional base classes will go
4015 either at offset zero (if empty, in which case they cannot
4016 overlap with offsets past the size of the biggest empty class) or
4017 at the end of the class.
4019 However, if we are placing an empty base class, then we must record
4020 all offsets, as either the empty class is at offset zero (where
4021 other empty classes might later be placed) or at the end of the
4022 class (where other objects might then be placed, so other empty
4023 subobjects might later overlap). */
4025 || !is_empty_class (type
))
4026 max_offset
= sizeof_biggest_empty_class
;
4028 max_offset
= NULL_TREE
;
4029 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4030 offsets
, max_offset
, vbases_p
);
4033 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4034 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4035 virtual bases of TYPE are examined. */
4038 layout_conflict_p (tree type
,
4043 splay_tree_node max_node
;
4045 /* Get the node in OFFSETS that indicates the maximum offset where
4046 an empty subobject is located. */
4047 max_node
= splay_tree_max (offsets
);
4048 /* If there aren't any empty subobjects, then there's no point in
4049 performing this check. */
4053 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4054 offsets
, (tree
) (max_node
->key
),
4058 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4059 non-static data member of the type indicated by RLI. BINFO is the
4060 binfo corresponding to the base subobject, OFFSETS maps offsets to
4061 types already located at those offsets. This function determines
4062 the position of the DECL. */
4065 layout_nonempty_base_or_field (record_layout_info rli
,
4070 tree offset
= NULL_TREE
;
4076 /* For the purposes of determining layout conflicts, we want to
4077 use the class type of BINFO; TREE_TYPE (DECL) will be the
4078 CLASSTYPE_AS_BASE version, which does not contain entries for
4079 zero-sized bases. */
4080 type
= TREE_TYPE (binfo
);
4085 type
= TREE_TYPE (decl
);
4089 /* Try to place the field. It may take more than one try if we have
4090 a hard time placing the field without putting two objects of the
4091 same type at the same address. */
4094 struct record_layout_info_s old_rli
= *rli
;
4096 /* Place this field. */
4097 place_field (rli
, decl
);
4098 offset
= byte_position (decl
);
4100 /* We have to check to see whether or not there is already
4101 something of the same type at the offset we're about to use.
4102 For example, consider:
4105 struct T : public S { int i; };
4106 struct U : public S, public T {};
4108 Here, we put S at offset zero in U. Then, we can't put T at
4109 offset zero -- its S component would be at the same address
4110 as the S we already allocated. So, we have to skip ahead.
4111 Since all data members, including those whose type is an
4112 empty class, have nonzero size, any overlap can happen only
4113 with a direct or indirect base-class -- it can't happen with
4115 /* In a union, overlap is permitted; all members are placed at
4117 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4119 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4122 /* Strip off the size allocated to this field. That puts us
4123 at the first place we could have put the field with
4124 proper alignment. */
4127 /* Bump up by the alignment required for the type. */
4129 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4131 ? CLASSTYPE_ALIGN (type
)
4132 : TYPE_ALIGN (type
)));
4133 normalize_rli (rli
);
4135 else if (TREE_CODE (type
) == NULLPTR_TYPE
4136 && warn_abi
&& abi_version_crosses (9))
4138 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4139 the offset wasn't aligned like a pointer when we started to
4140 layout this field, that affects its position. */
4141 tree pos
= rli_size_unit_so_far (&old_rli
);
4142 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4144 if (abi_version_at_least (9))
4145 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4146 "alignment of %qD increased in -fabi-version=9 "
4149 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4150 "of %qD will increase in -fabi-version=9", decl
);
4155 /* There was no conflict. We're done laying out this field. */
4159 /* Now that we know where it will be placed, update its
4161 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4162 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4163 this point because their BINFO_OFFSET is copied from another
4164 hierarchy. Therefore, we may not need to add the entire
4166 propagate_binfo_offsets (binfo
,
4167 size_diffop_loc (input_location
,
4168 fold_convert (ssizetype
, offset
),
4169 fold_convert (ssizetype
,
4170 BINFO_OFFSET (binfo
))));
4173 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4176 empty_base_at_nonzero_offset_p (tree type
,
4178 splay_tree
/*offsets*/)
4180 return is_empty_class (type
) && !integer_zerop (offset
);
4183 /* Layout the empty base BINFO. EOC indicates the byte currently just
4184 past the end of the class, and should be correctly aligned for a
4185 class of the type indicated by BINFO; OFFSETS gives the offsets of
4186 the empty bases allocated so far. T is the most derived
4187 type. Return nonzero iff we added it at the end. */
4190 layout_empty_base_or_field (record_layout_info rli
, tree binfo_or_decl
,
4195 tree binfo
= NULL_TREE
;
4196 tree decl
= NULL_TREE
;
4198 if (TREE_CODE (binfo_or_decl
) == TREE_BINFO
)
4200 binfo
= binfo_or_decl
;
4201 type
= BINFO_TYPE (binfo
);
4205 decl
= binfo_or_decl
;
4206 type
= TREE_TYPE (decl
);
4209 /* On some platforms (ARM), even empty classes will not be
4211 tree eoc
= round_up_loc (input_location
,
4212 rli_size_unit_so_far (rli
),
4213 CLASSTYPE_ALIGN_UNIT (type
));
4215 /* This routine should only be used for empty classes. */
4216 gcc_assert (is_empty_class (type
));
4217 alignment
= size_int (CLASSTYPE_ALIGN_UNIT (type
));
4219 /* This is an empty base class. We first try to put it at offset
4221 tree offset
= size_zero_node
;
4222 if (layout_conflict_p (type
,
4227 /* That didn't work. Now, we move forward from the next
4228 available spot in the class. */
4233 if (!layout_conflict_p (type
,
4237 /* We finally found a spot where there's no overlap. */
4240 /* There's overlap here, too. Bump along to the next spot. */
4241 offset
= size_binop (PLUS_EXPR
, offset
, alignment
);
4245 if (CLASSTYPE_USER_ALIGN (type
))
4247 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (type
));
4249 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (type
));
4250 TYPE_USER_ALIGN (rli
->t
) = 1;
4254 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4255 propagate_binfo_offsets (binfo
,
4256 size_diffop (offset
, BINFO_OFFSET (binfo
)));
4259 DECL_FIELD_OFFSET (decl
) = offset
;
4260 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4261 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4267 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4268 fields at NEXT_FIELD, and return it. */
4271 build_base_field_1 (tree t
, tree basetype
, tree
*&next_field
)
4273 /* Create the FIELD_DECL. */
4274 gcc_assert (CLASSTYPE_AS_BASE (basetype
));
4275 tree decl
= build_decl (input_location
,
4276 FIELD_DECL
, NULL_TREE
, CLASSTYPE_AS_BASE (basetype
));
4277 DECL_ARTIFICIAL (decl
) = 1;
4278 DECL_IGNORED_P (decl
) = 1;
4279 DECL_FIELD_CONTEXT (decl
) = t
;
4280 if (is_empty_class (basetype
))
4281 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4282 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = size_zero_node
;
4285 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4286 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4288 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4289 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4290 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4291 DECL_FIELD_IS_BASE (decl
) = 1;
4293 /* Add the new FIELD_DECL to the list of fields for T. */
4294 DECL_CHAIN (decl
) = *next_field
;
4296 next_field
= &DECL_CHAIN (decl
);
4301 /* Layout the base given by BINFO in the class indicated by RLI.
4302 *BASE_ALIGN is a running maximum of the alignments of
4303 any base class. OFFSETS gives the location of empty base
4304 subobjects. T is the most derived type. Return nonzero if the new
4305 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4306 *NEXT_FIELD, unless BINFO is for an empty base class.
4308 Returns the location at which the next field should be inserted. */
4311 build_base_field (record_layout_info rli
, tree binfo
,
4312 splay_tree offsets
, tree
*next_field
)
4315 tree basetype
= BINFO_TYPE (binfo
);
4317 if (!COMPLETE_TYPE_P (basetype
))
4318 /* This error is now reported in xref_tag, thus giving better
4319 location information. */
4322 /* Place the base class. */
4323 if (!is_empty_class (basetype
))
4327 /* The containing class is non-empty because it has a non-empty
4329 CLASSTYPE_EMPTY_P (t
) = 0;
4331 /* Create the FIELD_DECL. */
4332 decl
= build_base_field_1 (t
, basetype
, next_field
);
4334 /* Try to place the field. It may take more than one try if we
4335 have a hard time placing the field without putting two
4336 objects of the same type at the same address. */
4337 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4341 bool atend
= layout_empty_base_or_field (rli
, binfo
, offsets
);
4342 /* A nearly-empty class "has no proper base class that is empty,
4343 not morally virtual, and at an offset other than zero." */
4344 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4347 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4348 /* The check above (used in G++ 3.2) is insufficient because
4349 an empty class placed at offset zero might itself have an
4350 empty base at a nonzero offset. */
4351 else if (walk_subobject_offsets (basetype
,
4352 empty_base_at_nonzero_offset_p
,
4355 /*max_offset=*/NULL_TREE
,
4357 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4360 /* We used to not create a FIELD_DECL for empty base classes because of
4361 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4362 be a problem anymore. We need them to handle initialization of C++17
4364 if (cxx_dialect
>= cxx17
&& !BINFO_VIRTUAL_P (binfo
))
4366 tree decl
= build_base_field_1 (t
, basetype
, next_field
);
4367 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4368 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4369 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4372 /* An empty virtual base causes a class to be non-empty
4373 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4374 here because that was already done when the virtual table
4375 pointer was created. */
4378 /* Record the offsets of BINFO and its base subobjects. */
4379 record_subobject_offsets (binfo
, offsets
);
4384 /* Layout all of the non-virtual base classes. Record empty
4385 subobjects in OFFSETS. T is the most derived type. Return nonzero
4386 if the type cannot be nearly empty. The fields created
4387 corresponding to the base classes will be inserted at
4391 build_base_fields (record_layout_info rli
,
4392 splay_tree offsets
, tree
*next_field
)
4394 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4397 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
4400 /* The primary base class is always allocated first. */
4401 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
4402 next_field
= build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (t
),
4403 offsets
, next_field
);
4405 /* Now allocate the rest of the bases. */
4406 for (i
= 0; i
< n_baseclasses
; ++i
)
4410 base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
4412 /* The primary base was already allocated above, so we don't
4413 need to allocate it again here. */
4414 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
4417 /* Virtual bases are added at the end (a primary virtual base
4418 will have already been added). */
4419 if (BINFO_VIRTUAL_P (base_binfo
))
4422 next_field
= build_base_field (rli
, base_binfo
,
4423 offsets
, next_field
);
4427 /* Go through the TYPE_FIELDS of T issuing any appropriate
4428 diagnostics, figuring out which methods override which other
4429 methods, and so forth. */
4432 check_methods (tree t
)
4434 for (tree x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
4435 if (DECL_DECLARES_FUNCTION_P (x
))
4437 check_for_override (x
, t
);
4439 if (DECL_PURE_VIRTUAL_P (x
)
4440 && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4441 error ("initializer specified for non-virtual method %q+D", x
);
4442 /* The name of the field is the original field name
4443 Save this in auxiliary field for later overloading. */
4444 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4446 TYPE_POLYMORPHIC_P (t
) = 1;
4447 if (DECL_PURE_VIRTUAL_P (x
))
4448 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4451 /* All user-provided destructors are non-trivial.
4452 Constructors and assignment ops are handled in
4453 grok_special_member_properties. */
4454 if (DECL_DESTRUCTOR_P (x
) && user_provided_p (x
))
4455 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 1;
4456 if (!DECL_VIRTUAL_P (x
)
4457 && lookup_attribute ("transaction_safe_dynamic",
4458 DECL_ATTRIBUTES (x
)))
4459 error_at (DECL_SOURCE_LOCATION (x
),
4460 "%<transaction_safe_dynamic%> may only be specified for "
4461 "a virtual function");
4465 /* FN is a constructor or destructor. Clone the declaration to create
4466 a specialized in-charge or not-in-charge version, as indicated by
4470 build_clone (tree fn
, tree name
)
4475 /* Copy the function. */
4476 clone
= copy_decl (fn
);
4477 /* Reset the function name. */
4478 DECL_NAME (clone
) = name
;
4479 /* Remember where this function came from. */
4480 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4481 /* Make it easy to find the CLONE given the FN. */
4482 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4483 DECL_CHAIN (fn
) = clone
;
4485 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4486 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4488 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4489 DECL_TEMPLATE_RESULT (clone
) = result
;
4490 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4491 DECL_TI_TEMPLATE (result
) = clone
;
4492 TREE_TYPE (clone
) = TREE_TYPE (result
);
4497 // Clone constraints.
4499 if (tree ci
= get_constraints (fn
))
4500 set_constraints (clone
, copy_node (ci
));
4504 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4505 DECL_CLONED_FUNCTION (clone
) = fn
;
4506 /* There's no pending inline data for this function. */
4507 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4508 DECL_PENDING_INLINE_P (clone
) = 0;
4510 /* The base-class destructor is not virtual. */
4511 if (name
== base_dtor_identifier
)
4513 DECL_VIRTUAL_P (clone
) = 0;
4514 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4515 DECL_VINDEX (clone
) = NULL_TREE
;
4518 bool ctor_omit_inherited_parms_p
= ctor_omit_inherited_parms (clone
);
4519 if (ctor_omit_inherited_parms_p
)
4520 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone
));
4522 /* If there was an in-charge parameter, drop it from the function
4524 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4526 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4527 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4528 /* Skip the `this' parameter. */
4529 parmtypes
= TREE_CHAIN (parmtypes
);
4530 /* Skip the in-charge parameter. */
4531 parmtypes
= TREE_CHAIN (parmtypes
);
4532 /* And the VTT parm, in a complete [cd]tor. */
4533 if (DECL_HAS_VTT_PARM_P (fn
)
4534 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4535 parmtypes
= TREE_CHAIN (parmtypes
);
4536 if (ctor_omit_inherited_parms_p
)
4538 /* If we're omitting inherited parms, that just leaves the VTT. */
4539 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone
));
4540 parmtypes
= tree_cons (NULL_TREE
, vtt_parm_type
, void_list_node
);
4543 = build_method_type_directly (basetype
,
4544 TREE_TYPE (TREE_TYPE (clone
)),
4547 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4548 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4550 = cxx_copy_lang_qualifiers (TREE_TYPE (clone
), TREE_TYPE (fn
));
4553 /* Copy the function parameters. */
4554 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4555 /* Remove the in-charge parameter. */
4556 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4558 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4559 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4560 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4562 /* And the VTT parm, in a complete [cd]tor. */
4563 if (DECL_HAS_VTT_PARM_P (fn
))
4565 if (DECL_NEEDS_VTT_PARM_P (clone
))
4566 DECL_HAS_VTT_PARM_P (clone
) = 1;
4569 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4570 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4571 DECL_HAS_VTT_PARM_P (clone
) = 0;
4575 /* A base constructor inheriting from a virtual base doesn't get the
4577 if (ctor_omit_inherited_parms_p
)
4578 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
4580 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4582 DECL_CONTEXT (parms
) = clone
;
4583 cxx_dup_lang_specific_decl (parms
);
4586 /* Create the RTL for this function. */
4587 SET_DECL_RTL (clone
, NULL
);
4588 rest_of_decl_compilation (clone
, /*top_level=*/1, at_eof
);
4593 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4594 not invoke this function directly.
4596 For a non-thunk function, returns the address of the slot for storing
4597 the function it is a clone of. Otherwise returns NULL_TREE.
4599 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4600 cloned_function is unset. This is to support the separate
4601 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4602 on a template makes sense, but not the former. */
4605 decl_cloned_function_p (const_tree decl
, bool just_testing
)
4609 decl
= STRIP_TEMPLATE (decl
);
4611 if (TREE_CODE (decl
) != FUNCTION_DECL
4612 || !DECL_LANG_SPECIFIC (decl
)
4613 || DECL_LANG_SPECIFIC (decl
)->u
.fn
.thunk_p
)
4615 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4617 lang_check_failed (__FILE__
, __LINE__
, __FUNCTION__
);
4623 ptr
= &DECL_LANG_SPECIFIC (decl
)->u
.fn
.u5
.cloned_function
;
4624 if (just_testing
&& *ptr
== NULL_TREE
)
4630 /* Produce declarations for all appropriate clones of FN. If
4631 UPDATE_METHODS is true, the clones are added to the
4632 CLASSTYPE_MEMBER_VEC. */
4635 clone_function_decl (tree fn
, bool update_methods
)
4639 /* Avoid inappropriate cloning. */
4641 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4644 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4646 /* For each constructor, we need two variants: an in-charge version
4647 and a not-in-charge version. */
4648 clone
= build_clone (fn
, complete_ctor_identifier
);
4650 add_method (DECL_CONTEXT (clone
), clone
, false);
4651 clone
= build_clone (fn
, base_ctor_identifier
);
4653 add_method (DECL_CONTEXT (clone
), clone
, false);
4657 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4659 /* For each destructor, we need three variants: an in-charge
4660 version, a not-in-charge version, and an in-charge deleting
4661 version. We clone the deleting version first because that
4662 means it will go second on the TYPE_FIELDS list -- and that
4663 corresponds to the correct layout order in the virtual
4666 For a non-virtual destructor, we do not build a deleting
4668 if (DECL_VIRTUAL_P (fn
))
4670 clone
= build_clone (fn
, deleting_dtor_identifier
);
4672 add_method (DECL_CONTEXT (clone
), clone
, false);
4674 clone
= build_clone (fn
, complete_dtor_identifier
);
4676 add_method (DECL_CONTEXT (clone
), clone
, false);
4677 clone
= build_clone (fn
, base_dtor_identifier
);
4679 add_method (DECL_CONTEXT (clone
), clone
, false);
4682 /* Note that this is an abstract function that is never emitted. */
4683 DECL_ABSTRACT_P (fn
) = true;
4686 /* DECL is an in charge constructor, which is being defined. This will
4687 have had an in class declaration, from whence clones were
4688 declared. An out-of-class definition can specify additional default
4689 arguments. As it is the clones that are involved in overload
4690 resolution, we must propagate the information from the DECL to its
4694 adjust_clone_args (tree decl
)
4698 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4699 clone
= DECL_CHAIN (clone
))
4701 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4702 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4703 tree decl_parms
, clone_parms
;
4705 clone_parms
= orig_clone_parms
;
4707 /* Skip the 'this' parameter. */
4708 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4709 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4711 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4712 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4713 if (DECL_HAS_VTT_PARM_P (decl
))
4714 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4716 clone_parms
= orig_clone_parms
;
4717 if (DECL_HAS_VTT_PARM_P (clone
))
4718 clone_parms
= TREE_CHAIN (clone_parms
);
4720 for (decl_parms
= orig_decl_parms
; decl_parms
;
4721 decl_parms
= TREE_CHAIN (decl_parms
),
4722 clone_parms
= TREE_CHAIN (clone_parms
))
4724 if (clone_parms
== void_list_node
)
4726 gcc_assert (decl_parms
== clone_parms
4727 || ctor_omit_inherited_parms (clone
));
4731 gcc_assert (same_type_p (TREE_TYPE (decl_parms
),
4732 TREE_TYPE (clone_parms
)));
4734 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
4736 /* A default parameter has been added. Adjust the
4737 clone's parameters. */
4738 clone_parms
= orig_decl_parms
;
4740 if (DECL_HAS_VTT_PARM_P (clone
))
4742 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
4743 TREE_VALUE (orig_clone_parms
),
4745 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
4748 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4750 = build_method_type_directly (basetype
,
4751 TREE_TYPE (TREE_TYPE (clone
)),
4753 if (tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
)))
4754 type
= cp_build_type_attribute_variant (type
, attrs
);
4755 type
= cxx_copy_lang_qualifiers (type
, TREE_TYPE (clone
));
4756 TREE_TYPE (clone
) = type
;
4758 clone_parms
= NULL_TREE
;
4762 gcc_assert (!clone_parms
|| clone_parms
== void_list_node
);
4766 /* For each of the constructors and destructors in T, create an
4767 in-charge and not-in-charge variant. */
4770 clone_constructors_and_destructors (tree t
)
4772 /* While constructors can be via a using declaration, at this point
4773 we no longer need to know that. */
4774 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
4775 clone_function_decl (*iter
, /*update_methods=*/true);
4777 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
4778 clone_function_decl (dtor
, /*update_methods=*/true);
4781 /* Deduce noexcept for a destructor DTOR. */
4784 deduce_noexcept_on_destructor (tree dtor
)
4786 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
4787 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
),
4788 noexcept_deferred_spec
);
4791 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4792 of TYPE for virtual functions which FNDECL overrides. Return a
4793 mask of the tm attributes found therein. */
4796 look_for_tm_attr_overrides (tree type
, tree fndecl
)
4798 tree binfo
= TYPE_BINFO (type
);
4802 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
4804 tree o
, basetype
= BINFO_TYPE (base_binfo
);
4806 if (!TYPE_POLYMORPHIC_P (basetype
))
4809 o
= look_for_overrides_here (basetype
, fndecl
);
4812 if (lookup_attribute ("transaction_safe_dynamic",
4813 DECL_ATTRIBUTES (o
)))
4814 /* transaction_safe_dynamic is not inherited. */;
4816 found
|= tm_attr_to_mask (find_tm_attribute
4817 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
4820 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
4826 /* Subroutine of set_method_tm_attributes. Handle the checks and
4827 inheritance for one virtual method FNDECL. */
4830 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
4835 found
= look_for_tm_attr_overrides (type
, fndecl
);
4837 /* If FNDECL doesn't actually override anything (i.e. T is the
4838 class that first declares FNDECL virtual), then we're done. */
4842 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
4843 have
= tm_attr_to_mask (tm_attr
);
4845 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4846 tm_pure must match exactly, otherwise no weakening of
4847 tm_safe > tm_callable > nothing. */
4848 /* ??? The tm_pure attribute didn't make the transition to the
4849 multivendor language spec. */
4850 if (have
== TM_ATTR_PURE
)
4852 if (found
!= TM_ATTR_PURE
)
4858 /* If the overridden function is tm_pure, then FNDECL must be. */
4859 else if (found
== TM_ATTR_PURE
&& tm_attr
)
4861 /* Look for base class combinations that cannot be satisfied. */
4862 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
4864 found
&= ~TM_ATTR_PURE
;
4866 error_at (DECL_SOURCE_LOCATION (fndecl
),
4867 "method overrides both %<transaction_pure%> and %qE methods",
4868 tm_mask_to_attr (found
));
4870 /* If FNDECL did not declare an attribute, then inherit the most
4872 else if (tm_attr
== NULL
)
4874 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
4876 /* Otherwise validate that we're not weaker than a function
4877 that is being overridden. */
4881 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
4887 error_at (DECL_SOURCE_LOCATION (fndecl
),
4888 "method declared %qE overriding %qE method",
4889 tm_attr
, tm_mask_to_attr (found
));
4892 /* For each of the methods in T, propagate a class-level tm attribute. */
4895 set_method_tm_attributes (tree t
)
4897 tree class_tm_attr
, fndecl
;
4899 /* Don't bother collecting tm attributes if transactional memory
4900 support is not enabled. */
4904 /* Process virtual methods first, as they inherit directly from the
4905 base virtual function and also require validation of new attributes. */
4906 if (TYPE_CONTAINS_VPTR_P (t
))
4909 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
4910 vchain
= TREE_CHAIN (vchain
))
4912 fndecl
= BV_FN (vchain
);
4913 if (DECL_THUNK_P (fndecl
))
4914 fndecl
= THUNK_TARGET (fndecl
);
4915 set_one_vmethod_tm_attributes (t
, fndecl
);
4919 /* If the class doesn't have an attribute, nothing more to do. */
4920 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
4921 if (class_tm_attr
== NULL
)
4924 /* Any method that does not yet have a tm attribute inherits
4925 the one from the class. */
4926 for (fndecl
= TYPE_FIELDS (t
); fndecl
; fndecl
= DECL_CHAIN (fndecl
))
4927 if (DECL_DECLARES_FUNCTION_P (fndecl
)
4928 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
4929 apply_tm_attr (fndecl
, class_tm_attr
);
4932 /* Returns true if FN is a default constructor. */
4935 default_ctor_p (tree fn
)
4937 return (DECL_CONSTRUCTOR_P (fn
)
4938 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
4941 /* Returns true iff class T has a user-provided constructor that can be called
4942 with more than zero arguments. */
4945 type_has_user_nondefault_constructor (tree t
)
4947 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4950 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
4953 if (user_provided_p (fn
)
4954 && (TREE_CODE (fn
) == TEMPLATE_DECL
4955 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
4963 /* Returns the defaulted constructor if T has one. Otherwise, returns
4967 in_class_defaulted_default_constructor (tree t
)
4969 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4972 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
4976 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
4977 && default_ctor_p (fn
))
4984 /* Returns true iff FN is a user-provided function, i.e. user-declared
4985 and not defaulted at its first declaration. */
4988 user_provided_p (tree fn
)
4990 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4993 return (!DECL_ARTIFICIAL (fn
)
4994 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
4995 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
4998 /* Returns true iff class T has a user-provided constructor. */
5001 type_has_user_provided_constructor (tree t
)
5003 if (!CLASS_TYPE_P (t
))
5006 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5009 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5010 if (user_provided_p (*iter
))
5016 /* Returns true iff class T has a user-provided or explicit constructor. */
5019 type_has_user_provided_or_explicit_constructor (tree t
)
5021 if (!CLASS_TYPE_P (t
))
5024 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5027 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5030 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5037 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5038 declared or explicitly defaulted in the class body) default
5042 type_has_non_user_provided_default_constructor (tree t
)
5044 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5046 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5049 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5052 if (TREE_CODE (fn
) == FUNCTION_DECL
5053 && default_ctor_p (fn
)
5054 && !user_provided_p (fn
))
5061 /* TYPE is being used as a virtual base, and has a non-trivial move
5062 assignment. Return true if this is due to there being a user-provided
5063 move assignment in TYPE or one of its subobjects; if there isn't, then
5064 multiple move assignment can't cause any harm. */
5067 vbase_has_user_provided_move_assign (tree type
)
5069 /* Does the type itself have a user-provided move assignment operator? */
5070 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type
))
5071 for (ovl_iterator
iter (get_class_binding_direct
5072 (type
, assign_op_identifier
));
5074 if (user_provided_p (*iter
) && move_fn_p (*iter
))
5077 /* Do any of its bases? */
5078 tree binfo
= TYPE_BINFO (type
);
5080 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5081 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5084 /* Or non-static data members? */
5085 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5087 if (TREE_CODE (field
) == FIELD_DECL
5088 && CLASS_TYPE_P (TREE_TYPE (field
))
5089 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5097 /* If default-initialization leaves part of TYPE uninitialized, returns
5098 a DECL for the field or TYPE itself (DR 253). */
5101 default_init_uninitialized_part (tree type
)
5106 type
= strip_array_types (type
);
5107 if (!CLASS_TYPE_P (type
))
5109 if (!type_has_non_user_provided_default_constructor (type
))
5111 for (binfo
= TYPE_BINFO (type
), i
= 0;
5112 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5114 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5118 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5119 if (TREE_CODE (t
) == FIELD_DECL
5120 && !DECL_ARTIFICIAL (t
)
5121 && !DECL_INITIAL (t
))
5123 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5125 return DECL_P (r
) ? r
: t
;
5131 /* Returns true iff for class T, a trivial synthesized default constructor
5132 would be constexpr. */
5135 trivial_default_constructor_is_constexpr (tree t
)
5137 /* A defaulted trivial default constructor is constexpr
5138 if there is nothing to initialize. */
5139 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5140 return is_really_empty_class (t
);
5143 /* Returns true iff class T has a constexpr default constructor. */
5146 type_has_constexpr_default_constructor (tree t
)
5150 if (!CLASS_TYPE_P (t
))
5152 /* The caller should have stripped an enclosing array. */
5153 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5156 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5158 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5159 return trivial_default_constructor_is_constexpr (t
);
5160 /* Non-trivial, we need to check subobject constructors. */
5161 lazily_declare_fn (sfk_constructor
, t
);
5163 fns
= locate_ctor (t
);
5164 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5167 /* Returns true iff class T has a constexpr default constructor or has an
5168 implicitly declared default constructor that we can't tell if it's constexpr
5169 without forcing a lazy declaration (which might cause undesired
5173 type_maybe_constexpr_default_constructor (tree t
)
5175 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5176 && TYPE_HAS_COMPLEX_DFLT (t
))
5177 /* Assume it's constexpr. */
5179 return type_has_constexpr_default_constructor (t
);
5182 /* Returns true iff class TYPE has a virtual destructor. */
5185 type_has_virtual_destructor (tree type
)
5189 if (!CLASS_TYPE_P (type
))
5192 gcc_assert (COMPLETE_TYPE_P (type
));
5193 dtor
= CLASSTYPE_DESTRUCTOR (type
);
5194 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5197 /* Returns true iff T, a class, has a move-assignment or
5198 move-constructor. Does not lazily declare either.
5199 If USER_P is false, any move function will do. If it is true, the
5200 move function must be user-declared.
5202 Note that user-declared here is different from "user-provided",
5203 which doesn't include functions that are defaulted in the
5207 classtype_has_move_assign_or_move_ctor_p (tree t
, bool user_p
)
5210 || (!CLASSTYPE_LAZY_MOVE_CTOR (t
)
5211 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t
)));
5213 if (!CLASSTYPE_LAZY_MOVE_CTOR (t
))
5214 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5215 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5218 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5219 for (ovl_iterator
iter (get_class_binding_direct
5220 (t
, assign_op_identifier
));
5222 if ((!user_p
|| !DECL_ARTIFICIAL (*iter
)) && move_fn_p (*iter
))
5228 /* True iff T has a move constructor that is not deleted. */
5231 classtype_has_non_deleted_move_ctor (tree t
)
5233 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5234 lazily_declare_fn (sfk_move_constructor
, t
);
5235 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5236 if (move_fn_p (*iter
) && !DECL_DELETED_FN (*iter
))
5241 /* If T, a class, has a user-provided copy constructor, copy assignment
5242 operator, or destructor, returns that function. Otherwise, null. */
5245 classtype_has_depr_implicit_copy (tree t
)
5247 if (!CLASSTYPE_LAZY_COPY_CTOR (t
))
5248 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5251 if (user_provided_p (fn
) && copy_fn_p (fn
))
5255 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t
))
5256 for (ovl_iterator
iter (get_class_binding_direct
5257 (t
, assign_op_identifier
));
5261 if (user_provided_p (fn
) && copy_fn_p (fn
))
5265 if (!CLASSTYPE_LAZY_DESTRUCTOR (t
))
5267 tree fn
= CLASSTYPE_DESTRUCTOR (t
);
5268 if (user_provided_p (fn
))
5275 /* Nonzero if we need to build up a constructor call when initializing an
5276 object of this class, either because it has a user-declared constructor
5277 or because it doesn't have a default constructor (so we need to give an
5278 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5279 what you care about is whether or not an object can be produced by a
5280 constructor (e.g. so we don't set TREE_READONLY on const variables of
5281 such type); use this function when what you care about is whether or not
5282 to try to call a constructor to create an object. The latter case is
5283 the former plus some cases of constructors that cannot be called. */
5286 type_build_ctor_call (tree t
)
5289 if (TYPE_NEEDS_CONSTRUCTING (t
))
5291 inner
= strip_array_types (t
);
5292 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5294 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5296 if (cxx_dialect
< cxx11
)
5298 /* A user-declared constructor might be private, and a constructor might
5299 be trivial but deleted. */
5300 for (ovl_iterator
iter (get_class_binding (inner
, complete_ctor_identifier
));
5304 if (!DECL_ARTIFICIAL (fn
)
5305 || TREE_DEPRECATED (fn
)
5306 || DECL_DELETED_FN (fn
))
5312 /* Like type_build_ctor_call, but for destructors. */
5315 type_build_dtor_call (tree t
)
5318 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5320 inner
= strip_array_types (t
);
5321 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5322 || !COMPLETE_TYPE_P (inner
))
5324 if (cxx_dialect
< cxx11
)
5326 /* A user-declared destructor might be private, and a destructor might
5327 be trivial but deleted. */
5328 for (ovl_iterator
iter (get_class_binding (inner
, complete_dtor_identifier
));
5332 if (!DECL_ARTIFICIAL (fn
)
5333 || TREE_DEPRECATED (fn
)
5334 || DECL_DELETED_FN (fn
))
5340 /* Remove all zero-width bit-fields from T. */
5343 remove_zero_width_bit_fields (tree t
)
5347 fieldsp
= &TYPE_FIELDS (t
);
5350 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5351 && DECL_C_BIT_FIELD (*fieldsp
)
5352 /* We should not be confused by the fact that grokbitfield
5353 temporarily sets the width of the bit field into
5354 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5355 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5357 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5358 || integer_zerop (DECL_SIZE (*fieldsp
))))
5359 *fieldsp
= DECL_CHAIN (*fieldsp
);
5361 fieldsp
= &DECL_CHAIN (*fieldsp
);
5365 /* Returns TRUE iff we need a cookie when dynamically allocating an
5366 array whose elements have the indicated class TYPE. */
5369 type_requires_array_cookie (tree type
)
5372 bool has_two_argument_delete_p
= false;
5374 gcc_assert (CLASS_TYPE_P (type
));
5376 /* If there's a non-trivial destructor, we need a cookie. In order
5377 to iterate through the array calling the destructor for each
5378 element, we'll have to know how many elements there are. */
5379 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5382 /* If the usual deallocation function is a two-argument whose second
5383 argument is of type `size_t', then we have to pass the size of
5384 the array to the deallocation function, so we will need to store
5386 fns
= lookup_fnfields (TYPE_BINFO (type
),
5387 ovl_op_identifier (false, VEC_DELETE_EXPR
),
5389 /* If there are no `operator []' members, or the lookup is
5390 ambiguous, then we don't need a cookie. */
5391 if (!fns
|| fns
== error_mark_node
)
5393 /* Loop through all of the functions. */
5394 for (lkp_iterator
iter (BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
5398 /* See if this function is a one-argument delete function. If
5399 it is, then it will be the usual deallocation function. */
5400 tree second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5401 if (second_parm
== void_list_node
)
5403 /* Do not consider this function if its second argument is an
5407 /* Otherwise, if we have a two-argument function and the second
5408 argument is `size_t', it will be the usual deallocation
5409 function -- unless there is one-argument function, too. */
5410 if (TREE_CHAIN (second_parm
) == void_list_node
5411 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5412 has_two_argument_delete_p
= true;
5415 return has_two_argument_delete_p
;
5418 /* Finish computing the `literal type' property of class type T.
5420 At this point, we have already processed base classes and
5421 non-static data members. We need to check whether the copy
5422 constructor is trivial, the destructor is trivial, and there
5423 is a trivial default constructor or at least one constexpr
5424 constructor other than the copy constructor. */
5427 finalize_literal_type_property (tree t
)
5431 if (cxx_dialect
< cxx11
5432 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5433 CLASSTYPE_LITERAL_P (t
) = false;
5434 else if (CLASSTYPE_LITERAL_P (t
) && LAMBDA_TYPE_P (t
))
5435 CLASSTYPE_LITERAL_P (t
) = (cxx_dialect
>= cxx17
);
5436 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5437 && CLASSTYPE_NON_AGGREGATE (t
)
5438 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5439 CLASSTYPE_LITERAL_P (t
) = false;
5441 /* C++14 DR 1684 removed this restriction. */
5442 if (cxx_dialect
< cxx14
5443 && !CLASSTYPE_LITERAL_P (t
) && !LAMBDA_TYPE_P (t
))
5444 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5445 if (TREE_CODE (fn
) == FUNCTION_DECL
5446 && DECL_DECLARED_CONSTEXPR_P (fn
)
5447 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5448 && !DECL_CONSTRUCTOR_P (fn
))
5450 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5451 if (!DECL_GENERATED_P (fn
))
5453 auto_diagnostic_group d
;
5454 if (pedwarn (DECL_SOURCE_LOCATION (fn
), OPT_Wpedantic
,
5455 "enclosing class of %<constexpr%> non-static "
5456 "member function %q+#D is not a literal type", fn
))
5457 explain_non_literal_class (t
);
5462 /* T is a non-literal type used in a context which requires a constant
5463 expression. Explain why it isn't literal. */
5466 explain_non_literal_class (tree t
)
5468 static hash_set
<tree
> *diagnosed
;
5470 if (!CLASS_TYPE_P (t
))
5472 t
= TYPE_MAIN_VARIANT (t
);
5474 if (diagnosed
== NULL
)
5475 diagnosed
= new hash_set
<tree
>;
5476 if (diagnosed
->add (t
))
5477 /* Already explained. */
5480 auto_diagnostic_group d
;
5481 inform (UNKNOWN_LOCATION
, "%q+T is not literal because:", t
);
5482 if (cxx_dialect
< cxx17
&& LAMBDA_TYPE_P (t
))
5483 inform (UNKNOWN_LOCATION
,
5484 " %qT is a closure type, which is only literal in "
5485 "C++17 and later", t
);
5486 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5487 inform (UNKNOWN_LOCATION
, " %q+T has a non-trivial destructor", t
);
5488 else if (CLASSTYPE_NON_AGGREGATE (t
)
5489 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5490 && !LAMBDA_TYPE_P (t
)
5491 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5493 inform (UNKNOWN_LOCATION
,
5494 " %q+T is not an aggregate, does not have a trivial "
5495 "default constructor, and has no %<constexpr%> constructor that "
5496 "is not a copy or move constructor", t
);
5497 if (type_has_non_user_provided_default_constructor (t
))
5498 /* Note that we can't simply call locate_ctor because when the
5499 constructor is deleted it just returns NULL_TREE. */
5500 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (t
)); iter
; ++iter
)
5503 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5505 parms
= skip_artificial_parms_for (fn
, parms
);
5507 if (sufficient_parms_p (parms
))
5509 if (DECL_DELETED_FN (fn
))
5510 maybe_explain_implicit_delete (fn
);
5512 explain_invalid_constexpr_fn (fn
);
5519 tree binfo
, base_binfo
, field
; int i
;
5520 for (binfo
= TYPE_BINFO (t
), i
= 0;
5521 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5523 tree basetype
= TREE_TYPE (base_binfo
);
5524 if (!CLASSTYPE_LITERAL_P (basetype
))
5526 inform (UNKNOWN_LOCATION
,
5527 " base class %qT of %q+T is non-literal",
5529 explain_non_literal_class (basetype
);
5533 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5536 if (TREE_CODE (field
) != FIELD_DECL
)
5538 ftype
= TREE_TYPE (field
);
5539 if (!literal_type_p (ftype
))
5541 inform (DECL_SOURCE_LOCATION (field
),
5542 " non-static data member %qD has non-literal type",
5544 if (CLASS_TYPE_P (ftype
))
5545 explain_non_literal_class (ftype
);
5547 if (CP_TYPE_VOLATILE_P (ftype
))
5548 inform (DECL_SOURCE_LOCATION (field
),
5549 " non-static data member %qD has volatile type", field
);
5554 /* Check the validity of the bases and members declared in T. Add any
5555 implicitly-generated functions (like copy-constructors and
5556 assignment operators). Compute various flag bits (like
5557 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5558 level: i.e., independently of the ABI in use. */
5561 check_bases_and_members (tree t
)
5563 /* Nonzero if the implicitly generated copy constructor should take
5564 a non-const reference argument. */
5565 int cant_have_const_ctor
;
5566 /* Nonzero if the implicitly generated assignment operator
5567 should take a non-const reference argument. */
5568 int no_const_asn_ref
;
5570 bool saved_complex_asn_ref
;
5571 bool saved_nontrivial_dtor
;
5574 /* By default, we use const reference arguments and generate default
5576 cant_have_const_ctor
= 0;
5577 no_const_asn_ref
= 0;
5579 /* Check all the base-classes and set FMEM members to point to arrays
5580 of potential interest. */
5581 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
5583 /* Deduce noexcept on destructor. This needs to happen after we've set
5584 triviality flags appropriately for our bases. */
5585 if (cxx_dialect
>= cxx11
)
5586 if (tree dtor
= CLASSTYPE_DESTRUCTOR (t
))
5587 deduce_noexcept_on_destructor (dtor
);
5589 /* Check all the method declarations. */
5592 /* Save the initial values of these flags which only indicate whether
5593 or not the class has user-provided functions. As we analyze the
5594 bases and members we can set these flags for other reasons. */
5595 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5596 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5598 /* Check all the data member declarations. We cannot call
5599 check_field_decls until we have called check_bases check_methods,
5600 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5601 being set appropriately. */
5602 check_field_decls (t
, &access_decls
,
5603 &cant_have_const_ctor
,
5606 /* A nearly-empty class has to be vptr-containing; a nearly empty
5607 class contains just a vptr. */
5608 if (!TYPE_CONTAINS_VPTR_P (t
))
5609 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5611 /* Do some bookkeeping that will guide the generation of implicitly
5612 declared member functions. */
5613 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5614 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5615 /* We need to call a constructor for this class if it has a
5616 user-provided constructor, or if the default constructor is going
5617 to initialize the vptr. (This is not an if-and-only-if;
5618 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5619 themselves need constructing.) */
5620 TYPE_NEEDS_CONSTRUCTING (t
)
5621 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5624 An aggregate is an array or a class with no user-provided
5625 constructors ... and no virtual functions.
5627 Again, other conditions for being an aggregate are checked
5629 CLASSTYPE_NON_AGGREGATE (t
)
5630 |= ((cxx_dialect
< cxx2a
5631 ? type_has_user_provided_or_explicit_constructor (t
)
5632 : TYPE_HAS_USER_CONSTRUCTOR (t
))
5633 || TYPE_POLYMORPHIC_P (t
));
5634 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5635 retain the old definition internally for ABI reasons. */
5636 CLASSTYPE_NON_LAYOUT_POD_P (t
)
5637 |= (CLASSTYPE_NON_AGGREGATE (t
)
5638 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
5639 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5640 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5641 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5642 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5644 /* If the only explicitly declared default constructor is user-provided,
5645 set TYPE_HAS_COMPLEX_DFLT. */
5646 if (!TYPE_HAS_COMPLEX_DFLT (t
)
5647 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
5648 && !type_has_non_user_provided_default_constructor (t
))
5649 TYPE_HAS_COMPLEX_DFLT (t
) = true;
5651 /* Warn if a public base of a polymorphic type has an accessible
5652 non-virtual destructor. It is only now that we know the class is
5653 polymorphic. Although a polymorphic base will have a already
5654 been diagnosed during its definition, we warn on use too. */
5655 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
5657 tree binfo
= TYPE_BINFO (t
);
5658 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
5662 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5664 tree basetype
= TREE_TYPE (base_binfo
);
5666 if ((*accesses
)[i
] == access_public_node
5667 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
5668 && accessible_nvdtor_p (basetype
))
5669 warning (OPT_Wnon_virtual_dtor
,
5670 "base class %q#T has accessible non-virtual destructor",
5675 /* If the class has no user-declared constructor, but does have
5676 non-static const or reference data members that can never be
5677 initialized, issue a warning. */
5678 if (warn_uninitialized
5679 /* Classes with user-declared constructors are presumed to
5680 initialize these members. */
5681 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
5682 /* Aggregates can be initialized with brace-enclosed
5684 && CLASSTYPE_NON_AGGREGATE (t
))
5688 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5692 if (TREE_CODE (field
) != FIELD_DECL
5693 || DECL_INITIAL (field
) != NULL_TREE
)
5696 type
= TREE_TYPE (field
);
5697 if (TYPE_REF_P (type
))
5698 warning_at (DECL_SOURCE_LOCATION (field
),
5699 OPT_Wuninitialized
, "non-static reference %q#D "
5700 "in class without a constructor", field
);
5701 else if (CP_TYPE_CONST_P (type
)
5702 && (!CLASS_TYPE_P (type
)
5703 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
5704 warning_at (DECL_SOURCE_LOCATION (field
),
5705 OPT_Wuninitialized
, "non-static const member %q#D "
5706 "in class without a constructor", field
);
5710 /* Synthesize any needed methods. */
5711 add_implicitly_declared_members (t
, &access_decls
,
5712 cant_have_const_ctor
,
5715 /* Check defaulted declarations here so we have cant_have_const_ctor
5716 and don't need to worry about clones. */
5717 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5718 if (DECL_DECLARES_FUNCTION_P (fn
)
5719 && !DECL_ARTIFICIAL (fn
)
5720 && DECL_DEFAULTED_IN_CLASS_P (fn
))
5722 int copy
= copy_fn_p (fn
);
5726 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
5727 : !no_const_asn_ref
);
5728 bool fn_const_p
= (copy
== 2);
5730 if (fn_const_p
&& !imp_const_p
)
5731 /* If the function is defaulted outside the class, we just
5732 give the synthesis error. Core Issue #1331 says this is
5733 no longer ill-formed, it is defined as deleted instead. */
5734 DECL_DELETED_FN (fn
) = true;
5736 defaulted_late_check (fn
);
5739 if (LAMBDA_TYPE_P (t
))
5741 /* "This class type is not an aggregate." */
5742 CLASSTYPE_NON_AGGREGATE (t
) = 1;
5745 /* Compute the 'literal type' property before we
5746 do anything with non-static member functions. */
5747 finalize_literal_type_property (t
);
5749 /* Create the in-charge and not-in-charge variants of constructors
5751 clone_constructors_and_destructors (t
);
5753 /* Process the using-declarations. */
5754 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
5755 handle_using_decl (TREE_VALUE (access_decls
), t
);
5757 /* Figure out whether or not we will need a cookie when dynamically
5758 allocating an array of this type. */
5759 LANG_TYPE_CLASS_CHECK (t
)->vec_new_uses_cookie
5760 = type_requires_array_cookie (t
);
5763 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5764 accordingly. If a new vfield was created (because T doesn't have a
5765 primary base class), then the newly created field is returned. It
5766 is not added to the TYPE_FIELDS list; it is the caller's
5767 responsibility to do that. Accumulate declared virtual functions
5771 create_vtable_ptr (tree t
, tree
* virtuals_p
)
5775 /* Collect the virtual functions declared in T. */
5776 for (fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
5777 if (TREE_CODE (fn
) == FUNCTION_DECL
5778 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
5779 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
5781 tree new_virtual
= make_node (TREE_LIST
);
5783 BV_FN (new_virtual
) = fn
;
5784 BV_DELTA (new_virtual
) = integer_zero_node
;
5785 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
5787 TREE_CHAIN (new_virtual
) = *virtuals_p
;
5788 *virtuals_p
= new_virtual
;
5791 /* If we couldn't find an appropriate base class, create a new field
5792 here. Even if there weren't any new virtual functions, we might need a
5793 new virtual function table if we're supposed to include vptrs in
5794 all classes that need them. */
5795 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
5797 /* We build this decl with vtbl_ptr_type_node, which is a
5798 `vtable_entry_type*'. It might seem more precise to use
5799 `vtable_entry_type (*)[N]' where N is the number of virtual
5800 functions. However, that would require the vtable pointer in
5801 base classes to have a different type than the vtable pointer
5802 in derived classes. We could make that happen, but that
5803 still wouldn't solve all the problems. In particular, the
5804 type-based alias analysis code would decide that assignments
5805 to the base class vtable pointer can't alias assignments to
5806 the derived class vtable pointer, since they have different
5807 types. Thus, in a derived class destructor, where the base
5808 class constructor was inlined, we could generate bad code for
5809 setting up the vtable pointer.
5811 Therefore, we use one type for all vtable pointers. We still
5812 use a type-correct type; it's just doesn't indicate the array
5813 bounds. That's better than using `void*' or some such; it's
5814 cleaner, and it let's the alias analysis code know that these
5815 stores cannot alias stores to void*! */
5818 field
= build_decl (input_location
,
5819 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
5820 DECL_VIRTUAL_P (field
) = 1;
5821 DECL_ARTIFICIAL (field
) = 1;
5822 DECL_FIELD_CONTEXT (field
) = t
;
5823 DECL_FCONTEXT (field
) = t
;
5824 if (TYPE_PACKED (t
))
5825 DECL_PACKED (field
) = 1;
5827 TYPE_VFIELD (t
) = field
;
5829 /* This class is non-empty. */
5830 CLASSTYPE_EMPTY_P (t
) = 0;
5838 /* Add OFFSET to all base types of BINFO which is a base in the
5839 hierarchy dominated by T.
5841 OFFSET, which is a type offset, is number of bytes. */
5844 propagate_binfo_offsets (tree binfo
, tree offset
)
5850 /* Update BINFO's offset. */
5851 BINFO_OFFSET (binfo
)
5852 = fold_convert (sizetype
,
5853 size_binop (PLUS_EXPR
,
5854 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
5857 /* Find the primary base class. */
5858 primary_binfo
= get_primary_binfo (binfo
);
5860 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
5861 propagate_binfo_offsets (primary_binfo
, offset
);
5863 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5865 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5867 /* Don't do the primary base twice. */
5868 if (base_binfo
== primary_binfo
)
5871 if (BINFO_VIRTUAL_P (base_binfo
))
5874 propagate_binfo_offsets (base_binfo
, offset
);
5878 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5879 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5880 empty subobjects of T. */
5883 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
5889 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
5892 /* Find the last field. The artificial fields created for virtual
5893 bases will go after the last extant field to date. */
5894 next_field
= &TYPE_FIELDS (t
);
5896 next_field
= &DECL_CHAIN (*next_field
);
5898 /* Go through the virtual bases, allocating space for each virtual
5899 base that is not already a primary base class. These are
5900 allocated in inheritance graph order. */
5901 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
5903 if (!BINFO_VIRTUAL_P (vbase
))
5906 if (!BINFO_PRIMARY_P (vbase
))
5908 /* This virtual base is not a primary base of any class in the
5909 hierarchy, so we have to add space for it. */
5910 next_field
= build_base_field (rli
, vbase
,
5911 offsets
, next_field
);
5916 /* Returns the offset of the byte just past the end of the base class
5920 end_of_base (tree binfo
)
5924 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
5925 size
= TYPE_SIZE_UNIT (char_type_node
);
5926 else if (is_empty_class (BINFO_TYPE (binfo
)))
5927 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5928 allocate some space for it. It cannot have virtual bases, so
5929 TYPE_SIZE_UNIT is fine. */
5930 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5932 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5934 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
5937 /* Returns the offset of the byte just past the end of the base class or empty
5938 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
5939 then only non-virtual bases are included. */
5942 end_of_class (tree t
, bool include_virtuals_p
)
5944 tree result
= size_zero_node
;
5945 vec
<tree
, va_gc
> *vbases
;
5951 for (binfo
= TYPE_BINFO (t
), i
= 0;
5952 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5954 if (!include_virtuals_p
5955 && BINFO_VIRTUAL_P (base_binfo
)
5956 && (!BINFO_PRIMARY_P (base_binfo
)
5957 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
5960 offset
= end_of_base (base_binfo
);
5961 if (tree_int_cst_lt (result
, offset
))
5965 /* Also consider empty data members. */
5966 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5967 if (TREE_CODE (field
) == FIELD_DECL
5968 && !DECL_ARTIFICIAL (field
)
5969 && field_poverlapping_p (field
)
5970 && is_empty_class (TREE_TYPE (field
)))
5972 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
5973 offset
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (field
),
5974 TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5975 if (tree_int_cst_lt (result
, offset
))
5979 if (include_virtuals_p
)
5980 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
5981 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
5983 offset
= end_of_base (base_binfo
);
5984 if (tree_int_cst_lt (result
, offset
))
5991 /* Warn about bases of T that are inaccessible because they are
5992 ambiguous. For example:
5995 struct T : public S {};
5996 struct U : public S, public T {};
5998 Here, `(S*) new U' is not allowed because there are two `S'
6002 warn_about_ambiguous_bases (tree t
)
6005 vec
<tree
, va_gc
> *vbases
;
6010 /* If there are no repeated bases, nothing can be ambiguous. */
6011 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6014 /* Check direct bases. */
6015 for (binfo
= TYPE_BINFO (t
), i
= 0;
6016 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6018 basetype
= BINFO_TYPE (base_binfo
);
6020 if (!uniquely_derived_from_p (basetype
, t
))
6021 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6025 /* Check for ambiguous virtual bases. */
6027 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6028 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6030 basetype
= BINFO_TYPE (binfo
);
6032 if (!uniquely_derived_from_p (basetype
, t
))
6033 warning (OPT_Wextra
, "virtual base %qT inaccessible in %qT due "
6034 "to ambiguity", basetype
, t
);
6038 /* Compare two INTEGER_CSTs K1 and K2. */
6041 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6043 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6046 /* Increase the size indicated in RLI to account for empty classes
6047 that are "off the end" of the class. */
6050 include_empty_classes (record_layout_info rli
)
6055 /* It might be the case that we grew the class to allocate a
6056 zero-sized base class. That won't be reflected in RLI, yet,
6057 because we are willing to overlay multiple bases at the same
6058 offset. However, now we need to make sure that RLI is big enough
6059 to reflect the entire class. */
6060 eoc
= end_of_class (rli
->t
, CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6061 rli_size
= rli_size_unit_so_far (rli
);
6062 if (TREE_CODE (rli_size
) == INTEGER_CST
6063 && tree_int_cst_lt (rli_size
, eoc
))
6065 /* The size should have been rounded to a whole byte. */
6066 gcc_assert (tree_int_cst_equal
6067 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6069 = size_binop (PLUS_EXPR
,
6071 size_binop (MULT_EXPR
,
6072 fold_convert (bitsizetype
,
6073 size_binop (MINUS_EXPR
,
6075 bitsize_int (BITS_PER_UNIT
)));
6076 normalize_rli (rli
);
6080 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6081 BINFO_OFFSETs for all of the base-classes. Position the vtable
6082 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6085 layout_class_type (tree t
, tree
*virtuals_p
)
6087 tree non_static_data_members
;
6090 record_layout_info rli
;
6091 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6092 types that appear at that offset. */
6093 splay_tree empty_base_offsets
;
6094 /* True if the last field laid out was a bit-field. */
6095 bool last_field_was_bitfield
= false;
6096 /* The location at which the next field should be inserted. */
6099 /* Keep track of the first non-static data member. */
6100 non_static_data_members
= TYPE_FIELDS (t
);
6102 /* Start laying out the record. */
6103 rli
= start_record_layout (t
);
6105 /* Mark all the primary bases in the hierarchy. */
6106 determine_primary_bases (t
);
6108 /* Create a pointer to our virtual function table. */
6109 vptr
= create_vtable_ptr (t
, virtuals_p
);
6111 /* The vptr is always the first thing in the class. */
6114 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6115 TYPE_FIELDS (t
) = vptr
;
6116 next_field
= &DECL_CHAIN (vptr
);
6117 place_field (rli
, vptr
);
6120 next_field
= &TYPE_FIELDS (t
);
6122 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6123 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6125 build_base_fields (rli
, empty_base_offsets
, next_field
);
6127 /* Layout the non-static data members. */
6128 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6133 /* We still pass things that aren't non-static data members to
6134 the back end, in case it wants to do something with them. */
6135 if (TREE_CODE (field
) != FIELD_DECL
)
6137 place_field (rli
, field
);
6138 /* If the static data member has incomplete type, keep track
6139 of it so that it can be completed later. (The handling
6140 of pending statics in finish_record_layout is
6141 insufficient; consider:
6144 struct S2 { static S1 s1; };
6146 At this point, finish_record_layout will be called, but
6147 S1 is still incomplete.) */
6150 maybe_register_incomplete_var (field
);
6151 /* The visibility of static data members is determined
6152 at their point of declaration, not their point of
6154 determine_visibility (field
);
6159 type
= TREE_TYPE (field
);
6160 if (type
== error_mark_node
)
6163 padding
= NULL_TREE
;
6165 bool might_overlap
= field_poverlapping_p (field
);
6167 if (might_overlap
&& CLASS_TYPE_P (type
)
6168 && (CLASSTYPE_NON_LAYOUT_POD_P (type
) || CLASSTYPE_EMPTY_P (type
)))
6170 /* if D is a potentially-overlapping data member, update sizeof(C) to
6171 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6172 tree nvsize
= CLASSTYPE_SIZE_UNIT (type
);
6173 /* end_of_class doesn't always give dsize, but it does in the case of
6174 a class with virtual bases, which is when dsize > nvsize. */
6175 tree dsize
= end_of_class (type
, /*vbases*/true);
6176 if (tree_int_cst_le (dsize
, nvsize
))
6178 DECL_SIZE_UNIT (field
) = nvsize
;
6179 DECL_SIZE (field
) = CLASSTYPE_SIZE (type
);
6183 DECL_SIZE_UNIT (field
) = dsize
;
6184 DECL_SIZE (field
) = bit_from_pos (dsize
, bitsize_zero_node
);
6188 /* If this field is a bit-field whose width is greater than its
6189 type, then there are some special rules for allocating
6191 if (DECL_C_BIT_FIELD (field
)
6192 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6194 bool was_unnamed_p
= false;
6195 /* We must allocate the bits as if suitably aligned for the
6196 longest integer type that fits in this many bits. Then,
6197 we are supposed to use the left over bits as additional
6200 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6201 tree limit
= size_int (MAX_FIXED_MODE_SIZE
);
6202 if (tree_int_cst_lt (DECL_SIZE (field
), limit
))
6203 limit
= DECL_SIZE (field
);
6205 tree integer_type
= integer_types
[itk_char
];
6206 for (unsigned itk
= itk_char
; itk
!= itk_none
; itk
++)
6207 if (tree next
= integer_types
[itk
])
6209 if (tree_int_cst_lt (limit
, TYPE_SIZE (next
)))
6210 /* Too big, so our current guess is what we want. */
6212 /* Not bigger than limit, ok */
6213 integer_type
= next
;
6216 /* Figure out how much additional padding is required. */
6217 if (TREE_CODE (t
) == UNION_TYPE
)
6218 /* In a union, the padding field must have the full width
6219 of the bit-field; all fields start at offset zero. */
6220 padding
= DECL_SIZE (field
);
6222 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6223 TYPE_SIZE (integer_type
));
6225 if (integer_zerop (padding
))
6226 padding
= NULL_TREE
;
6228 /* An unnamed bitfield does not normally affect the
6229 alignment of the containing class on a target where
6230 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6231 make any exceptions for unnamed bitfields when the
6232 bitfields are longer than their types. Therefore, we
6233 temporarily give the field a name. */
6234 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6236 was_unnamed_p
= true;
6237 DECL_NAME (field
) = make_anon_name ();
6240 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6241 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6242 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6243 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6244 empty_base_offsets
);
6246 DECL_NAME (field
) = NULL_TREE
;
6247 /* Now that layout has been performed, set the size of the
6248 field to the size of its declared type; the rest of the
6249 field is effectively invisible. */
6250 DECL_SIZE (field
) = TYPE_SIZE (type
);
6251 /* We must also reset the DECL_MODE of the field. */
6252 SET_DECL_MODE (field
, TYPE_MODE (type
));
6254 else if (might_overlap
&& is_empty_class (type
))
6255 layout_empty_base_or_field (rli
, field
, empty_base_offsets
);
6257 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6258 empty_base_offsets
);
6260 /* Remember the location of any empty classes in FIELD. */
6261 record_subobject_offsets (field
, empty_base_offsets
);
6263 /* If a bit-field does not immediately follow another bit-field,
6264 and yet it starts in the middle of a byte, we have failed to
6265 comply with the ABI. */
6267 && DECL_C_BIT_FIELD (field
)
6268 /* The TREE_NO_WARNING flag gets set by Objective-C when
6269 laying out an Objective-C class. The ObjC ABI differs
6270 from the C++ ABI, and so we do not want a warning
6272 && !TREE_NO_WARNING (field
)
6273 && !last_field_was_bitfield
6274 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6275 DECL_FIELD_BIT_OFFSET (field
),
6276 bitsize_unit_node
)))
6277 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6278 "offset of %qD is not ABI-compliant and may "
6279 "change in a future version of GCC", field
);
6281 /* The middle end uses the type of expressions to determine the
6282 possible range of expression values. In order to optimize
6283 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6284 must be made aware of the width of "i", via its type.
6286 Because C++ does not have integer types of arbitrary width,
6287 we must (for the purposes of the front end) convert from the
6288 type assigned here to the declared type of the bitfield
6289 whenever a bitfield expression is used as an rvalue.
6290 Similarly, when assigning a value to a bitfield, the value
6291 must be converted to the type given the bitfield here. */
6292 if (DECL_C_BIT_FIELD (field
))
6294 unsigned HOST_WIDE_INT width
;
6295 tree ftype
= TREE_TYPE (field
);
6296 width
= tree_to_uhwi (DECL_SIZE (field
));
6297 if (width
!= TYPE_PRECISION (ftype
))
6300 = c_build_bitfield_integer_type (width
,
6301 TYPE_UNSIGNED (ftype
));
6303 = cp_build_qualified_type (TREE_TYPE (field
),
6304 cp_type_quals (ftype
));
6308 /* If we needed additional padding after this field, add it
6314 padding_field
= build_decl (input_location
,
6318 DECL_BIT_FIELD (padding_field
) = 1;
6319 DECL_SIZE (padding_field
) = padding
;
6320 DECL_CONTEXT (padding_field
) = t
;
6321 DECL_ARTIFICIAL (padding_field
) = 1;
6322 DECL_IGNORED_P (padding_field
) = 1;
6323 DECL_PADDING_P (padding_field
) = 1;
6324 layout_nonempty_base_or_field (rli
, padding_field
,
6326 empty_base_offsets
);
6329 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6332 if (!integer_zerop (rli
->bitpos
))
6334 /* Make sure that we are on a byte boundary so that the size of
6335 the class without virtual bases will always be a round number
6337 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6338 normalize_rli (rli
);
6341 /* Delete all zero-width bit-fields from the list of fields. Now
6342 that the type is laid out they are no longer important. */
6343 remove_zero_width_bit_fields (t
);
6345 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6347 /* T needs a different layout as a base (eliding virtual bases
6348 or whatever). Create that version. */
6349 tree base_t
= make_node (TREE_CODE (t
));
6351 /* If the ABI version is not at least two, and the last
6352 field was a bit-field, RLI may not be on a byte
6353 boundary. In particular, rli_size_unit_so_far might
6354 indicate the last complete byte, while rli_size_so_far
6355 indicates the total number of bits used. Therefore,
6356 rli_size_so_far, rather than rli_size_unit_so_far, is
6357 used to compute TYPE_SIZE_UNIT. */
6358 tree eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6359 TYPE_SIZE_UNIT (base_t
)
6360 = size_binop (MAX_EXPR
,
6361 fold_convert (sizetype
,
6362 size_binop (CEIL_DIV_EXPR
,
6363 rli_size_so_far (rli
),
6364 bitsize_int (BITS_PER_UNIT
))),
6367 = size_binop (MAX_EXPR
,
6368 rli_size_so_far (rli
),
6369 size_binop (MULT_EXPR
,
6370 fold_convert (bitsizetype
, eoc
),
6371 bitsize_int (BITS_PER_UNIT
)));
6372 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
6373 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6374 TYPE_TYPELESS_STORAGE (base_t
) = TYPE_TYPELESS_STORAGE (t
);
6376 /* Copy the non-static data members of T. This will include its
6377 direct non-virtual bases & vtable. */
6378 next_field
= &TYPE_FIELDS (base_t
);
6379 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6380 if (TREE_CODE (field
) == FIELD_DECL
)
6382 *next_field
= copy_node (field
);
6383 DECL_CONTEXT (*next_field
) = base_t
;
6384 next_field
= &DECL_CHAIN (*next_field
);
6386 *next_field
= NULL_TREE
;
6388 /* We use the base type for trivial assignments, and hence it
6390 compute_record_mode (base_t
);
6392 TYPE_CONTEXT (base_t
) = t
;
6394 /* Record the base version of the type. */
6395 CLASSTYPE_AS_BASE (t
) = base_t
;
6398 CLASSTYPE_AS_BASE (t
) = t
;
6400 /* Every empty class contains an empty class. */
6401 if (CLASSTYPE_EMPTY_P (t
))
6402 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6404 /* Set the TYPE_DECL for this type to contain the right
6405 value for DECL_OFFSET, so that we can use it as part
6406 of a COMPONENT_REF for multiple inheritance. */
6407 layout_decl (TYPE_MAIN_DECL (t
), 0);
6409 /* Now fix up any virtual base class types that we left lying
6410 around. We must get these done before we try to lay out the
6411 virtual function table. As a side-effect, this will remove the
6412 base subobject fields. */
6413 layout_virtual_bases (rli
, empty_base_offsets
);
6415 /* Make sure that empty classes are reflected in RLI at this
6417 include_empty_classes (rli
);
6419 /* Make sure not to create any structures with zero size. */
6420 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6422 build_decl (input_location
,
6423 FIELD_DECL
, NULL_TREE
, char_type_node
));
6425 /* If this is a non-POD, declaring it packed makes a difference to how it
6426 can be used as a field; don't let finalize_record_size undo it. */
6427 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6428 rli
->packed_maybe_necessary
= true;
6430 /* Let the back end lay out the type. */
6431 finish_record_layout (rli
, /*free_p=*/true);
6433 if (TYPE_SIZE_UNIT (t
)
6434 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6435 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6436 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6437 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
6439 /* Warn about bases that can't be talked about due to ambiguity. */
6440 warn_about_ambiguous_bases (t
);
6442 /* Now that we're done with layout, give the base fields the real types. */
6443 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6444 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6445 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6448 splay_tree_delete (empty_base_offsets
);
6450 if (CLASSTYPE_EMPTY_P (t
)
6451 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6452 TYPE_SIZE_UNIT (t
)))
6453 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6456 /* Determine the "key method" for the class type indicated by TYPE,
6457 and set CLASSTYPE_KEY_METHOD accordingly. */
6460 determine_key_method (tree type
)
6464 if (processing_template_decl
6465 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6466 || CLASSTYPE_INTERFACE_KNOWN (type
))
6469 /* The key method is the first non-pure virtual function that is not
6470 inline at the point of class definition. On some targets the
6471 key function may not be inline; those targets should not call
6472 this function until the end of the translation unit. */
6473 for (method
= TYPE_FIELDS (type
); method
; method
= DECL_CHAIN (method
))
6474 if (TREE_CODE (method
) == FUNCTION_DECL
6475 && DECL_VINDEX (method
) != NULL_TREE
6476 && ! DECL_DECLARED_INLINE_P (method
)
6477 && ! DECL_PURE_VIRTUAL_P (method
))
6479 CLASSTYPE_KEY_METHOD (type
) = method
;
6486 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6487 class data member of non-zero size, otherwise false. */
6490 field_nonempty_p (const_tree fld
)
6492 if (TREE_CODE (fld
) == ERROR_MARK
)
6495 tree type
= TREE_TYPE (fld
);
6496 if (TREE_CODE (fld
) == FIELD_DECL
6497 && TREE_CODE (type
) != ERROR_MARK
6498 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
6500 return TYPE_SIZE (type
)
6501 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
6502 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
6508 /* Used by find_flexarrays and related functions. */
6512 /* The first flexible array member or non-zero array member found
6513 in the order of layout. */
6515 /* First non-static non-empty data member in the class or its bases. */
6517 /* The first non-static non-empty data member following either
6518 the flexible array member, if found, or the zero-length array member
6519 otherwise. AFTER[1] refers to the first such data member of a union
6520 of which the struct containing the flexible array member or zero-length
6521 array is a member, or NULL when no such union exists. This element is
6522 only used during searching, not for diagnosing problems. AFTER[0]
6523 refers to the first such data member that is not a member of such
6527 /* Refers to a struct (not union) in which the struct of which the flexible
6528 array is member is defined. Used to diagnose strictly (according to C)
6529 invalid uses of the latter structs. */
6533 /* Find either the first flexible array member or the first zero-length
6534 array, in that order of preference, among members of class T (but not
6535 its base classes), and set members of FMEM accordingly.
6536 BASE_P is true if T is a base class of another class.
6537 PUN is set to the outermost union in which the flexible array member
6538 (or zero-length array) is defined if one such union exists, otherwise
6540 Similarly, PSTR is set to a data member of the outermost struct of
6541 which the flexible array is a member if one such struct exists,
6542 otherwise to NULL. */
6545 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
6546 tree pun
/* = NULL_TREE */,
6547 tree pstr
/* = NULL_TREE */)
6549 /* Set the "pointer" to the outermost enclosing union if not set
6550 yet and maintain it for the remainder of the recursion. */
6551 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
6554 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
6556 if (fld
== error_mark_node
)
6559 /* Is FLD a typedef for an anonymous struct? */
6561 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6562 handled elsewhere so that errors like the following are detected
6564 typedef struct { int i, a[], j; } S; // bug c++/72753
6565 S s [2]; // bug c++/68489
6567 if (TREE_CODE (fld
) == TYPE_DECL
6568 && DECL_IMPLICIT_TYPEDEF_P (fld
)
6569 && CLASS_TYPE_P (TREE_TYPE (fld
))
6570 && anon_aggrname_p (DECL_NAME (fld
)))
6572 /* Check the nested unnamed type referenced via a typedef
6573 independently of FMEM (since it's not a data member of
6574 the enclosing class). */
6575 check_flexarrays (TREE_TYPE (fld
));
6579 /* Skip anything that's GCC-generated or not a (non-static) data
6581 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
6584 /* Type of the member. */
6585 tree fldtype
= TREE_TYPE (fld
);
6586 if (fldtype
== error_mark_node
)
6589 /* Determine the type of the array element or object referenced
6590 by the member so that it can be checked for flexible array
6591 members if it hasn't been yet. */
6592 tree eltype
= fldtype
;
6593 while (TREE_CODE (eltype
) == ARRAY_TYPE
6594 || INDIRECT_TYPE_P (eltype
))
6595 eltype
= TREE_TYPE (eltype
);
6597 if (RECORD_OR_UNION_TYPE_P (eltype
))
6599 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6601 /* Once the member after the flexible array has been found
6603 fmem
->after
[bool (pun
)] = fld
;
6607 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
6609 /* Descend into the non-static member struct or union and try
6610 to find a flexible array member or zero-length array among
6611 its members. This is only necessary for anonymous types
6612 and types in whose context the current type T has not been
6613 defined (the latter must not be checked again because they
6614 are already in the process of being checked by one of the
6615 recursive calls). */
6617 tree first
= fmem
->first
;
6618 tree array
= fmem
->array
;
6620 /* If this member isn't anonymous and a prior non-flexible array
6621 member has been seen in one of the enclosing structs, clear
6622 the FIRST member since it doesn't contribute to the flexible
6623 array struct's members. */
6624 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
6625 fmem
->first
= NULL_TREE
;
6627 find_flexarrays (eltype
, fmem
, false, pun
,
6628 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
6630 if (fmem
->array
!= array
)
6633 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
6635 /* Restore the FIRST member reset above if no flexible
6636 array member has been found in this member's struct. */
6637 fmem
->first
= first
;
6640 /* If the member struct contains the first flexible array
6641 member, or if this member is a base class, continue to
6642 the next member and avoid setting the FMEM->NEXT pointer
6649 if (field_nonempty_p (fld
))
6651 /* Remember the first non-static data member. */
6655 /* Remember the first non-static data member after the flexible
6656 array member, if one has been found, or the zero-length array
6657 if it has been found. */
6658 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6659 fmem
->after
[bool (pun
)] = fld
;
6662 /* Skip non-arrays. */
6663 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
6666 /* Determine the upper bound of the array if it has one. */
6667 if (TYPE_DOMAIN (fldtype
))
6671 /* Make a record of the zero-length array if either one
6672 such field or a flexible array member has been seen to
6673 handle the pathological and unlikely case of multiple
6675 if (!fmem
->after
[bool (pun
)])
6676 fmem
->after
[bool (pun
)] = fld
;
6678 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
6680 /* Remember the first zero-length array unless a flexible array
6681 member has already been seen. */
6683 fmem
->enclosing
= pstr
;
6688 /* Flexible array members have no upper bound. */
6691 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
6693 /* Replace the zero-length array if it's been stored and
6694 reset the after pointer. */
6695 fmem
->after
[bool (pun
)] = NULL_TREE
;
6697 fmem
->enclosing
= pstr
;
6699 else if (!fmem
->after
[bool (pun
)])
6700 /* Make a record of another flexible array member. */
6701 fmem
->after
[bool (pun
)] = fld
;
6706 fmem
->enclosing
= pstr
;
6712 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6713 a flexible array member (or the zero-length array extension). */
6716 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
6718 if (fmem
->array
&& fmem
->enclosing
)
6720 auto_diagnostic_group d
;
6721 if (pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
6722 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
6723 ? G_("invalid use of %q#T with a zero-size array "
6725 : G_("invalid use of %q#T with a flexible array member "
6727 DECL_CONTEXT (fmem
->array
),
6728 DECL_CONTEXT (fmem
->enclosing
)))
6729 inform (DECL_SOURCE_LOCATION (fmem
->array
),
6730 "array member %q#D declared here", fmem
->array
);
6734 /* Issue diagnostics for invalid flexible array members or zero-length
6735 arrays that are not the last elements of the containing class or its
6736 base classes or that are its sole members. */
6739 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
6744 if (fmem
->first
&& !fmem
->after
[0])
6746 diagnose_invalid_flexarray (fmem
);
6750 /* Has a diagnostic been issued? */
6753 const char *msg
= 0;
6755 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
6758 msg
= G_("zero-size array member %qD not at end of %q#T");
6759 else if (!fmem
->first
)
6760 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
6764 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
6766 auto_diagnostic_group d
;
6767 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
6769 inform (location_of (t
), "in the definition of %q#T", t
);
6777 msg
= G_("flexible array member %qD not at end of %q#T");
6778 else if (!fmem
->first
)
6779 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
6783 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
6786 auto_diagnostic_group d
;
6787 error_at (loc
, msg
, fmem
->array
, t
);
6789 /* In the unlikely event that the member following the flexible
6790 array member is declared in a different class, or the member
6791 overlaps another member of a common union, point to it.
6792 Otherwise it should be obvious. */
6794 && ((DECL_CONTEXT (fmem
->after
[0])
6795 != DECL_CONTEXT (fmem
->array
))))
6797 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
6798 "next member %q#D declared here",
6800 inform (location_of (t
), "in the definition of %q#T", t
);
6805 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
6806 diagnose_invalid_flexarray (fmem
);
6810 /* Recursively check to make sure that any flexible array or zero-length
6811 array members of class T or its bases are valid (i.e., not the sole
6812 non-static data member of T and, if one exists, that it is the last
6813 non-static data member of T and its base classes. FMEM is expected
6814 to be initially null and is used internally by recursive calls to
6815 the function. Issue the appropriate diagnostics for the array member
6816 that fails the checks. */
6819 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
6820 bool base_p
/* = false */)
6822 /* Initialize the result of a search for flexible array and zero-length
6823 array members. Avoid doing any work if the most interesting FMEM data
6824 have already been populated. */
6825 flexmems_t flexmems
= flexmems_t ();
6828 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
6831 tree fam
= fmem
->array
;
6833 /* Recursively check the primary base class first. */
6834 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6836 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
6837 check_flexarrays (basetype
, fmem
, true);
6840 /* Recursively check the base classes. */
6841 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
6842 for (int i
= 0; i
< nbases
; ++i
)
6844 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
6846 /* The primary base class was already checked above. */
6847 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
6850 /* Virtual base classes are at the end. */
6851 if (BINFO_VIRTUAL_P (base_binfo
))
6854 /* Check the base class. */
6855 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
6858 if (fmem
== &flexmems
)
6860 /* Check virtual base classes only once per derived class.
6861 I.e., this check is not performed recursively for base
6865 vec
<tree
, va_gc
> *vbases
;
6866 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6867 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6869 /* Check the virtual base class. */
6870 tree basetype
= TREE_TYPE (base_binfo
);
6872 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
6876 /* Is the type unnamed (and therefore a member of it potentially
6877 an anonymous struct or union)? */
6878 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
6880 /* Search the members of the current (possibly derived) class, skipping
6881 unnamed structs and unions since those could be anonymous. */
6882 if (fmem
!= &flexmems
|| !maybe_anon_p
)
6883 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
6885 if (fmem
== &flexmems
&& !maybe_anon_p
)
6887 /* Issue diagnostics for invalid flexible and zero-length array
6888 members found in base classes or among the members of the current
6889 class. Ignore anonymous structs and unions whose members are
6890 considered to be members of the enclosing class and thus will
6891 be diagnosed when checking it. */
6892 diagnose_flexarrays (t
, fmem
);
6896 /* Perform processing required when the definition of T (a class type)
6897 is complete. Diagnose invalid definitions of flexible array members
6898 and zero-size arrays. */
6901 finish_struct_1 (tree t
)
6904 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6905 tree virtuals
= NULL_TREE
;
6907 if (COMPLETE_TYPE_P (t
))
6909 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
6910 error ("redefinition of %q#T", t
);
6915 /* If this type was previously laid out as a forward reference,
6916 make sure we lay it out again. */
6917 TYPE_SIZE (t
) = NULL_TREE
;
6918 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
6920 /* Make assumptions about the class; we'll reset the flags if
6922 CLASSTYPE_EMPTY_P (t
) = 1;
6923 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
6924 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
6925 CLASSTYPE_LITERAL_P (t
) = true;
6927 /* Do end-of-class semantic processing: checking the validity of the
6928 bases and members and add implicitly generated methods. */
6929 check_bases_and_members (t
);
6931 /* Find the key method. */
6932 if (TYPE_CONTAINS_VPTR_P (t
))
6934 /* The Itanium C++ ABI permits the key method to be chosen when
6935 the class is defined -- even though the key method so
6936 selected may later turn out to be an inline function. On
6937 some systems (such as ARM Symbian OS) the key method cannot
6938 be determined until the end of the translation unit. On such
6939 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6940 will cause the class to be added to KEYED_CLASSES. Then, in
6941 finish_file we will determine the key method. */
6942 if (targetm
.cxx
.key_method_may_be_inline ())
6943 determine_key_method (t
);
6945 /* If a polymorphic class has no key method, we may emit the vtable
6946 in every translation unit where the class definition appears. If
6947 we're devirtualizing, we can look into the vtable even if we
6948 aren't emitting it. */
6949 if (!CLASSTYPE_KEY_METHOD (t
))
6950 vec_safe_push (keyed_classes
, t
);
6953 /* Layout the class itself. */
6954 layout_class_type (t
, &virtuals
);
6955 /* COMPLETE_TYPE_P is now true. */
6957 set_class_bindings (t
);
6959 /* With the layout complete, check for flexible array members and
6960 zero-length arrays that might overlap other members in the final
6962 check_flexarrays (t
);
6964 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
6966 /* If necessary, create the primary vtable for this class. */
6967 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
6969 /* We must enter these virtuals into the table. */
6970 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6971 build_primary_vtable (NULL_TREE
, t
);
6972 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
6973 /* Here we know enough to change the type of our virtual
6974 function table, but we will wait until later this function. */
6975 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
6977 /* If we're warning about ABI tags, check the types of the new
6978 virtual functions. */
6980 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
6981 check_abi_tags (t
, TREE_VALUE (v
));
6984 if (TYPE_CONTAINS_VPTR_P (t
))
6989 if (BINFO_VTABLE (TYPE_BINFO (t
)))
6990 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
6991 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6992 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
6994 /* Add entries for virtual functions introduced by this class. */
6995 BINFO_VIRTUALS (TYPE_BINFO (t
))
6996 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
6998 /* Set DECL_VINDEX for all functions declared in this class. */
6999 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
7001 fn
= TREE_CHAIN (fn
),
7002 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
7003 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
7005 tree fndecl
= BV_FN (fn
);
7007 if (DECL_THUNK_P (fndecl
))
7008 /* A thunk. We should never be calling this entry directly
7009 from this vtable -- we'd use the entry for the non
7010 thunk base function. */
7011 DECL_VINDEX (fndecl
) = NULL_TREE
;
7012 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7013 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7017 finish_struct_bits (t
);
7019 set_method_tm_attributes (t
);
7020 if (flag_openmp
|| flag_openmp_simd
)
7021 finish_omp_declare_simd_methods (t
);
7023 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7024 for any static member objects of the type we're working on. */
7025 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7026 if (DECL_DECLARES_FUNCTION_P (x
))
7027 DECL_IN_AGGR_P (x
) = false;
7028 else if (VAR_P (x
) && TREE_STATIC (x
)
7029 && TREE_TYPE (x
) != error_mark_node
7030 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7031 SET_DECL_MODE (x
, TYPE_MODE (t
));
7033 /* Complain if one of the field types requires lower visibility. */
7034 constrain_class_visibility (t
);
7036 /* Make the rtl for any new vtables we have created, and unmark
7037 the base types we marked. */
7040 /* Build the VTT for T. */
7044 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7045 && !CLASSTYPE_FINAL (t
))
7046 warning (OPT_Wnon_virtual_dtor
,
7047 "%q#T has virtual functions and accessible"
7048 " non-virtual destructor", t
);
7052 if (warn_overloaded_virtual
)
7055 /* Class layout, assignment of virtual table slots, etc., is now
7056 complete. Give the back end a chance to tweak the visibility of
7057 the class or perform any other required target modifications. */
7058 targetm
.cxx
.adjust_class_at_definition (t
);
7060 maybe_suppress_debug_info (t
);
7062 if (flag_vtable_verify
)
7063 vtv_save_class_info (t
);
7065 dump_class_hierarchy (t
);
7067 /* Finish debugging output for this type. */
7068 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7070 if (TYPE_TRANSPARENT_AGGR (t
))
7072 tree field
= first_field (t
);
7073 if (field
== NULL_TREE
|| error_operand_p (field
))
7075 error ("type transparent %q#T does not have any fields", t
);
7076 TYPE_TRANSPARENT_AGGR (t
) = 0;
7078 else if (DECL_ARTIFICIAL (field
))
7080 if (DECL_FIELD_IS_BASE (field
))
7081 error ("type transparent class %qT has base classes", t
);
7084 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7085 error ("type transparent class %qT has virtual functions", t
);
7087 TYPE_TRANSPARENT_AGGR (t
) = 0;
7089 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7091 error ("type transparent %q#T cannot be made transparent because "
7092 "the type of the first field has a different ABI from the "
7093 "class overall", t
);
7094 TYPE_TRANSPARENT_AGGR (t
) = 0;
7099 /* When T was built up, the member declarations were added in reverse
7100 order. Rearrange them to declaration order. */
7103 unreverse_member_declarations (tree t
)
7109 /* The following lists are all in reverse order. Put them in
7110 declaration order now. */
7111 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7113 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7114 order, so we can't just use nreverse. Due to stat_hack
7115 chicanery in finish_member_declaration. */
7117 for (x
= TYPE_FIELDS (t
);
7118 x
&& TREE_CODE (x
) != TYPE_DECL
;
7121 next
= DECL_CHAIN (x
);
7122 DECL_CHAIN (x
) = prev
;
7128 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7129 TYPE_FIELDS (t
) = prev
;
7134 finish_struct (tree t
, tree attributes
)
7136 location_t saved_loc
= input_location
;
7138 /* Now that we've got all the field declarations, reverse everything
7140 unreverse_member_declarations (t
);
7142 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7143 fixup_attribute_variants (t
);
7145 /* Nadger the current location so that diagnostics point to the start of
7146 the struct, not the end. */
7147 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7149 if (processing_template_decl
)
7153 /* We need to add the target functions of USING_DECLS, so that
7154 they can be found when the using declaration is not
7155 instantiated yet. */
7156 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7157 if (TREE_CODE (x
) == USING_DECL
)
7159 tree fn
= strip_using_decl (x
);
7161 for (lkp_iterator
iter (fn
); iter
; ++iter
)
7162 add_method (t
, *iter
, true);
7164 else if (DECL_DECLARES_FUNCTION_P (x
))
7165 DECL_IN_AGGR_P (x
) = false;
7167 /* Also add a USING_DECL for operator=. We know there'll be (at
7168 least) one, but we don't know the signature(s). We want name
7169 lookup not to fail or recurse into bases. This isn't added
7170 to the template decl list so we drop this at instantiation
7172 tree ass_op
= build_lang_decl (USING_DECL
, assign_op_identifier
,
7174 DECL_CONTEXT (ass_op
) = t
;
7175 USING_DECL_SCOPE (ass_op
) = t
;
7176 DECL_DEPENDENT_P (ass_op
) = true;
7177 DECL_ARTIFICIAL (ass_op
) = true;
7178 DECL_CHAIN (ass_op
) = TYPE_FIELDS (t
);
7179 TYPE_FIELDS (t
) = ass_op
;
7181 TYPE_SIZE (t
) = bitsize_zero_node
;
7182 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7183 /* COMPLETE_TYPE_P is now true. */
7185 set_class_bindings (t
);
7187 /* We need to emit an error message if this type was used as a parameter
7188 and it is an abstract type, even if it is a template. We construct
7189 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7190 account and we call complete_vars with this type, which will check
7191 the PARM_DECLS. Note that while the type is being defined,
7192 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7193 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7194 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7195 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7196 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_PURE_VIRTUAL_P (x
))
7197 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7200 /* Remember current #pragma pack value. */
7201 TYPE_PRECISION (t
) = maximum_field_alignment
;
7203 /* Fix up any variants we've already built. */
7204 for (x
= TYPE_NEXT_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
7206 TYPE_SIZE (x
) = TYPE_SIZE (t
);
7207 TYPE_SIZE_UNIT (x
) = TYPE_SIZE_UNIT (t
);
7208 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
7212 finish_struct_1 (t
);
7213 /* COMPLETE_TYPE_P is now true. */
7215 maybe_warn_about_overly_private_class (t
);
7217 if (is_std_init_list (t
))
7219 /* People keep complaining that the compiler crashes on an invalid
7220 definition of initializer_list, so I guess we should explicitly
7221 reject it. What the compiler internals care about is that it's a
7222 template and has a pointer field followed by size_type field. */
7224 if (processing_template_decl
)
7226 tree f
= next_initializable_field (TYPE_FIELDS (t
));
7227 if (f
&& TYPE_PTR_P (TREE_TYPE (f
)))
7229 f
= next_initializable_field (DECL_CHAIN (f
));
7230 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7235 fatal_error (input_location
, "definition of %qD does not match "
7236 "%<#include <initializer_list>%>", TYPE_NAME (t
));
7239 input_location
= saved_loc
;
7241 TYPE_BEING_DEFINED (t
) = 0;
7243 if (current_class_type
)
7246 error ("trying to finish struct, but kicked out due to previous parse errors");
7248 if (processing_template_decl
&& at_function_scope_p ()
7249 /* Lambdas are defined by the LAMBDA_EXPR. */
7250 && !LAMBDA_TYPE_P (t
))
7251 add_stmt (build_min (TAG_DEFN
, t
));
7256 /* Hash table to avoid endless recursion when handling references. */
7257 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7259 /* Return the dynamic type of INSTANCE, if known.
7260 Used to determine whether the virtual function table is needed
7263 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7264 of our knowledge of its type. *NONNULL should be initialized
7265 before this function is called. */
7268 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7270 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7272 switch (TREE_CODE (instance
))
7275 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
7278 return RECUR (TREE_OPERAND (instance
, 0));
7281 /* This is a call to a constructor, hence it's never zero. */
7282 if (CALL_EXPR_FN (instance
)
7283 && TREE_HAS_CONSTRUCTOR (instance
))
7287 return TREE_TYPE (instance
);
7292 /* This is a call to a constructor, hence it's never zero. */
7293 if (TREE_HAS_CONSTRUCTOR (instance
))
7297 return TREE_TYPE (instance
);
7299 return RECUR (TREE_OPERAND (instance
, 0));
7301 case POINTER_PLUS_EXPR
:
7304 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
7305 return RECUR (TREE_OPERAND (instance
, 0));
7306 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7307 /* Propagate nonnull. */
7308 return RECUR (TREE_OPERAND (instance
, 0));
7313 return RECUR (TREE_OPERAND (instance
, 0));
7316 instance
= TREE_OPERAND (instance
, 0);
7319 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7320 with a real object -- given &p->f, p can still be null. */
7321 tree t
= get_base_address (instance
);
7322 /* ??? Probably should check DECL_WEAK here. */
7323 if (t
&& DECL_P (t
))
7326 return RECUR (instance
);
7329 /* If this component is really a base class reference, then the field
7330 itself isn't definitive. */
7331 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
7332 return RECUR (TREE_OPERAND (instance
, 0));
7333 return RECUR (TREE_OPERAND (instance
, 1));
7337 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
7338 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7342 return TREE_TYPE (TREE_TYPE (instance
));
7348 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7352 return TREE_TYPE (instance
);
7354 else if (instance
== current_class_ptr
)
7359 /* if we're in a ctor or dtor, we know our type. If
7360 current_class_ptr is set but we aren't in a function, we're in
7361 an NSDMI (and therefore a constructor). */
7362 if (current_scope () != current_function_decl
7363 || (DECL_LANG_SPECIFIC (current_function_decl
)
7364 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7365 || DECL_DESTRUCTOR_P (current_function_decl
))))
7369 return TREE_TYPE (TREE_TYPE (instance
));
7372 else if (TYPE_REF_P (TREE_TYPE (instance
)))
7374 /* We only need one hash table because it is always left empty. */
7375 if (!fixed_type_or_null_ref_ht
)
7376 fixed_type_or_null_ref_ht
7377 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7379 /* Reference variables should be references to objects. */
7383 /* Enter the INSTANCE in a table to prevent recursion; a
7384 variable's initializer may refer to the variable
7386 if (VAR_P (instance
)
7387 && DECL_INITIAL (instance
)
7388 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7389 && !fixed_type_or_null_ref_ht
->find (instance
))
7394 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7396 type
= RECUR (DECL_INITIAL (instance
));
7397 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7404 case VIEW_CONVERT_EXPR
:
7405 if (location_wrapper_p (instance
))
7406 return RECUR (TREE_OPERAND (instance
, 0));
7408 /* TODO: Recursion may be correct for some non-location-wrapper
7409 uses of VIEW_CONVERT_EXPR. */
7418 /* Return nonzero if the dynamic type of INSTANCE is known, and
7419 equivalent to the static type. We also handle the case where
7420 INSTANCE is really a pointer. Return negative if this is a
7421 ctor/dtor. There the dynamic type is known, but this might not be
7422 the most derived base of the original object, and hence virtual
7423 bases may not be laid out according to this type.
7425 Used to determine whether the virtual function table is needed
7428 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7429 of our knowledge of its type. *NONNULL should be initialized
7430 before this function is called. */
7433 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7435 tree t
= TREE_TYPE (instance
);
7439 /* processing_template_decl can be false in a template if we're in
7440 instantiate_non_dependent_expr, but we still want to suppress
7442 if (in_template_function ())
7444 /* In a template we only care about the type of the result. */
7450 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7451 if (fixed
== NULL_TREE
)
7453 if (INDIRECT_TYPE_P (t
))
7455 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7457 return cdtorp
? -1 : 1;
7462 init_class_processing (void)
7464 current_class_depth
= 0;
7465 current_class_stack_size
= 10;
7467 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7468 sizeof_biggest_empty_class
= size_zero_node
;
7470 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7471 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7472 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7475 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7478 restore_class_cache (void)
7482 /* We are re-entering the same class we just left, so we don't
7483 have to search the whole inheritance matrix to find all the
7484 decls to bind again. Instead, we install the cached
7485 class_shadowed list and walk through it binding names. */
7486 push_binding_level (previous_class_level
);
7487 class_binding_level
= previous_class_level
;
7488 /* Restore IDENTIFIER_TYPE_VALUE. */
7489 for (type
= class_binding_level
->type_shadowed
;
7491 type
= TREE_CHAIN (type
))
7492 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7495 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7496 appropriate for TYPE.
7498 So that we may avoid calls to lookup_name, we cache the _TYPE
7499 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7501 For multiple inheritance, we perform a two-pass depth-first search
7502 of the type lattice. */
7505 pushclass (tree type
)
7507 class_stack_node_t csn
;
7509 type
= TYPE_MAIN_VARIANT (type
);
7511 /* Make sure there is enough room for the new entry on the stack. */
7512 if (current_class_depth
+ 1 >= current_class_stack_size
)
7514 current_class_stack_size
*= 2;
7516 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7517 current_class_stack_size
);
7520 /* Insert a new entry on the class stack. */
7521 csn
= current_class_stack
+ current_class_depth
;
7522 csn
->name
= current_class_name
;
7523 csn
->type
= current_class_type
;
7524 csn
->access
= current_access_specifier
;
7525 csn
->names_used
= 0;
7527 current_class_depth
++;
7529 /* Now set up the new type. */
7530 current_class_name
= TYPE_NAME (type
);
7531 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7532 current_class_name
= DECL_NAME (current_class_name
);
7533 current_class_type
= type
;
7535 /* By default, things in classes are private, while things in
7536 structures or unions are public. */
7537 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7538 ? access_private_node
7539 : access_public_node
);
7541 if (previous_class_level
7542 && type
!= previous_class_level
->this_entity
7543 && current_class_depth
== 1)
7545 /* Forcibly remove any old class remnants. */
7546 invalidate_class_lookup_cache ();
7549 if (!previous_class_level
7550 || type
!= previous_class_level
->this_entity
7551 || current_class_depth
> 1)
7554 restore_class_cache ();
7557 /* When we exit a toplevel class scope, we save its binding level so
7558 that we can restore it quickly. Here, we've entered some other
7559 class, so we must invalidate our cache. */
7562 invalidate_class_lookup_cache (void)
7564 previous_class_level
= NULL
;
7567 /* Get out of the current class scope. If we were in a class scope
7568 previously, that is the one popped to. */
7575 current_class_depth
--;
7576 current_class_name
= current_class_stack
[current_class_depth
].name
;
7577 current_class_type
= current_class_stack
[current_class_depth
].type
;
7578 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7579 if (current_class_stack
[current_class_depth
].names_used
)
7580 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
7583 /* Mark the top of the class stack as hidden. */
7586 push_class_stack (void)
7588 if (current_class_depth
)
7589 ++current_class_stack
[current_class_depth
- 1].hidden
;
7592 /* Mark the top of the class stack as un-hidden. */
7595 pop_class_stack (void)
7597 if (current_class_depth
)
7598 --current_class_stack
[current_class_depth
- 1].hidden
;
7601 /* If the class type currently being defined is either T or
7602 a nested type of T, returns the type from the current_class_stack,
7603 which might be equivalent to but not equal to T in case of
7604 constrained partial specializations. */
7607 currently_open_class (tree t
)
7611 if (!CLASS_TYPE_P (t
))
7614 t
= TYPE_MAIN_VARIANT (t
);
7616 /* We start looking from 1 because entry 0 is from global scope,
7618 for (i
= current_class_depth
; i
> 0; --i
)
7621 if (i
== current_class_depth
)
7622 c
= current_class_type
;
7625 if (current_class_stack
[i
].hidden
)
7627 c
= current_class_stack
[i
].type
;
7631 if (same_type_p (c
, t
))
7637 /* If either current_class_type or one of its enclosing classes are derived
7638 from T, return the appropriate type. Used to determine how we found
7639 something via unqualified lookup. */
7642 currently_open_derived_class (tree t
)
7646 /* The bases of a dependent type are unknown. */
7647 if (dependent_type_p (t
))
7650 if (!current_class_type
)
7653 if (DERIVED_FROM_P (t
, current_class_type
))
7654 return current_class_type
;
7656 for (i
= current_class_depth
- 1; i
> 0; --i
)
7658 if (current_class_stack
[i
].hidden
)
7660 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
7661 return current_class_stack
[i
].type
;
7667 /* Return the outermost enclosing class type that is still open, or
7671 outermost_open_class (void)
7673 if (!current_class_type
)
7676 if (TYPE_BEING_DEFINED (current_class_type
))
7677 r
= current_class_type
;
7678 for (int i
= current_class_depth
- 1; i
> 0; --i
)
7680 if (current_class_stack
[i
].hidden
)
7682 tree t
= current_class_stack
[i
].type
;
7683 if (!TYPE_BEING_DEFINED (t
))
7690 /* Returns the innermost class type which is not a lambda closure type. */
7693 current_nonlambda_class_type (void)
7695 tree type
= current_class_type
;
7696 while (type
&& LAMBDA_TYPE_P (type
))
7697 type
= decl_type_context (TYPE_NAME (type
));
7701 /* When entering a class scope, all enclosing class scopes' names with
7702 static meaning (static variables, static functions, types and
7703 enumerators) have to be visible. This recursive function calls
7704 pushclass for all enclosing class contexts until global or a local
7705 scope is reached. TYPE is the enclosed class. */
7708 push_nested_class (tree type
)
7710 /* A namespace might be passed in error cases, like A::B:C. */
7711 if (type
== NULL_TREE
7712 || !CLASS_TYPE_P (type
))
7715 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
7720 /* Undoes a push_nested_class call. */
7723 pop_nested_class (void)
7725 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
7728 if (context
&& CLASS_TYPE_P (context
))
7729 pop_nested_class ();
7732 /* Returns the number of extern "LANG" blocks we are nested within. */
7735 current_lang_depth (void)
7737 return vec_safe_length (current_lang_base
);
7740 /* Set global variables CURRENT_LANG_NAME to appropriate value
7741 so that behavior of name-mangling machinery is correct. */
7744 push_lang_context (tree name
)
7746 vec_safe_push (current_lang_base
, current_lang_name
);
7748 if (name
== lang_name_cplusplus
)
7749 current_lang_name
= name
;
7750 else if (name
== lang_name_c
)
7751 current_lang_name
= name
;
7753 error ("language string %<\"%E\"%> not recognized", name
);
7756 /* Get out of the current language scope. */
7759 pop_lang_context (void)
7761 current_lang_name
= current_lang_base
->pop ();
7764 /* Type instantiation routines. */
7766 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7767 matches the TARGET_TYPE. If there is no satisfactory match, return
7768 error_mark_node, and issue an error & warning messages under
7769 control of FLAGS. Permit pointers to member function if FLAGS
7770 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7771 a template-id, and EXPLICIT_TARGS are the explicitly provided
7774 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7775 is the base path used to reference those member functions. If
7776 the address is resolved to a member function, access checks will be
7777 performed and errors issued if appropriate. */
7780 resolve_address_of_overloaded_function (tree target_type
,
7782 tsubst_flags_t complain
,
7784 tree explicit_targs
,
7787 /* Here's what the standard says:
7791 If the name is a function template, template argument deduction
7792 is done, and if the argument deduction succeeds, the deduced
7793 arguments are used to generate a single template function, which
7794 is added to the set of overloaded functions considered.
7796 Non-member functions and static member functions match targets of
7797 type "pointer-to-function" or "reference-to-function." Nonstatic
7798 member functions match targets of type "pointer-to-member
7799 function;" the function type of the pointer to member is used to
7800 select the member function from the set of overloaded member
7801 functions. If a nonstatic member function is selected, the
7802 reference to the overloaded function name is required to have the
7803 form of a pointer to member as described in 5.3.1.
7805 If more than one function is selected, any template functions in
7806 the set are eliminated if the set also contains a non-template
7807 function, and any given template function is eliminated if the
7808 set contains a second template function that is more specialized
7809 than the first according to the partial ordering rules 14.5.5.2.
7810 After such eliminations, if any, there shall remain exactly one
7811 selected function. */
7814 /* We store the matches in a TREE_LIST rooted here. The functions
7815 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7816 interoperability with most_specialized_instantiation. */
7817 tree matches
= NULL_TREE
;
7819 tree target_fn_type
;
7821 /* By the time we get here, we should be seeing only real
7822 pointer-to-member types, not the internal POINTER_TYPE to
7823 METHOD_TYPE representation. */
7824 gcc_assert (!TYPE_PTR_P (target_type
)
7825 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
7827 gcc_assert (is_overloaded_fn (overload
));
7829 /* Check that the TARGET_TYPE is reasonable. */
7830 if (TYPE_PTRFN_P (target_type
)
7831 || TYPE_REFFN_P (target_type
))
7833 else if (TYPE_PTRMEMFUNC_P (target_type
))
7834 /* This is OK, too. */
7836 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
7837 /* This is OK, too. This comes from a conversion to reference
7839 target_type
= build_reference_type (target_type
);
7842 if (complain
& tf_error
)
7843 error ("cannot resolve overloaded function %qD based on"
7844 " conversion to type %qT",
7845 OVL_NAME (overload
), target_type
);
7846 return error_mark_node
;
7849 /* Non-member functions and static member functions match targets of type
7850 "pointer-to-function" or "reference-to-function." Nonstatic member
7851 functions match targets of type "pointer-to-member-function;" the
7852 function type of the pointer to member is used to select the member
7853 function from the set of overloaded member functions.
7855 So figure out the FUNCTION_TYPE that we want to match against. */
7856 target_fn_type
= static_fn_type (target_type
);
7858 /* If we can find a non-template function that matches, we can just
7859 use it. There's no point in generating template instantiations
7860 if we're just going to throw them out anyhow. But, of course, we
7861 can only do this when we don't *need* a template function. */
7863 for (lkp_iterator
iter (overload
); iter
; ++iter
)
7867 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
7868 /* We're not looking for templates just yet. */
7871 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
) != is_ptrmem
)
7872 /* We're looking for a non-static member, and this isn't
7873 one, or vice versa. */
7876 /* In C++17 we need the noexcept-qualifier to compare types. */
7877 if (flag_noexcept_type
7878 && !maybe_instantiate_noexcept (fn
, complain
))
7881 /* See if there's a match. */
7882 tree fntype
= static_fn_type (fn
);
7883 if (same_type_p (target_fn_type
, fntype
)
7884 || fnptr_conv_p (target_fn_type
, fntype
))
7885 matches
= tree_cons (fn
, NULL_TREE
, matches
);
7888 /* Now, if we've already got a match (or matches), there's no need
7889 to proceed to the template functions. But, if we don't have a
7890 match we need to look at them, too. */
7893 tree target_arg_types
;
7894 tree target_ret_type
;
7896 unsigned int nargs
, ia
;
7899 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
7900 target_ret_type
= TREE_TYPE (target_fn_type
);
7902 nargs
= list_length (target_arg_types
);
7903 args
= XALLOCAVEC (tree
, nargs
);
7904 for (arg
= target_arg_types
, ia
= 0;
7905 arg
!= NULL_TREE
&& arg
!= void_list_node
;
7906 arg
= TREE_CHAIN (arg
), ++ia
)
7907 args
[ia
] = TREE_VALUE (arg
);
7910 for (lkp_iterator
iter (overload
); iter
; ++iter
)
7916 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7917 /* We're only looking for templates. */
7920 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
7922 /* We're not looking for a non-static member, and this is
7923 one, or vice versa. */
7926 tree ret
= target_ret_type
;
7928 /* If the template has a deduced return type, don't expose it to
7929 template argument deduction. */
7930 if (undeduced_auto_decl (fn
))
7933 /* Try to do argument deduction. */
7934 targs
= make_tree_vec (DECL_NTPARMS (fn
));
7935 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
7937 DEDUCE_EXACT
, LOOKUP_NORMAL
,
7938 NULL
, false, false);
7939 if (instantiation
== error_mark_node
)
7940 /* Instantiation failed. */
7943 /* Constraints must be satisfied. This is done before
7944 return type deduction since that instantiates the
7946 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
7949 /* And now force instantiation to do return type deduction. */
7950 if (undeduced_auto_decl (instantiation
))
7953 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
7956 require_deduced_type (instantiation
);
7959 /* In C++17 we need the noexcept-qualifier to compare types. */
7960 if (flag_noexcept_type
)
7961 maybe_instantiate_noexcept (instantiation
, complain
);
7963 /* See if there's a match. */
7964 tree fntype
= static_fn_type (instantiation
);
7965 if (same_type_p (target_fn_type
, fntype
)
7966 || fnptr_conv_p (target_fn_type
, fntype
))
7967 matches
= tree_cons (instantiation
, fn
, matches
);
7970 /* Now, remove all but the most specialized of the matches. */
7973 tree match
= most_specialized_instantiation (matches
);
7975 if (match
!= error_mark_node
)
7976 matches
= tree_cons (TREE_PURPOSE (match
),
7982 /* Now we should have exactly one function in MATCHES. */
7983 if (matches
== NULL_TREE
)
7985 /* There were *no* matches. */
7986 if (complain
& tf_error
)
7988 error ("no matches converting function %qD to type %q#T",
7989 OVL_NAME (overload
), target_type
);
7991 print_candidates (overload
);
7993 return error_mark_node
;
7995 else if (TREE_CHAIN (matches
))
7997 /* There were too many matches. First check if they're all
7998 the same function. */
7999 tree match
= NULL_TREE
;
8001 fn
= TREE_PURPOSE (matches
);
8003 /* For multi-versioned functions, more than one match is just fine and
8004 decls_match will return false as they are different. */
8005 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
8006 if (!decls_match (fn
, TREE_PURPOSE (match
))
8007 && !targetm
.target_option
.function_versions
8008 (fn
, TREE_PURPOSE (match
)))
8013 if (complain
& tf_error
)
8015 error ("converting overloaded function %qD to type %q#T is ambiguous",
8016 OVL_NAME (overload
), target_type
);
8018 /* Since print_candidates expects the functions in the
8019 TREE_VALUE slot, we flip them here. */
8020 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8021 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8023 print_candidates (matches
);
8026 return error_mark_node
;
8030 /* Good, exactly one match. Now, convert it to the correct type. */
8031 fn
= TREE_PURPOSE (matches
);
8033 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8034 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8036 static int explained
;
8038 if (!(complain
& tf_error
))
8039 return error_mark_node
;
8041 auto_diagnostic_group d
;
8042 if (permerror (input_location
, "assuming pointer to member %qD", fn
)
8045 inform (input_location
, "(a pointer to member can only be "
8046 "formed with %<&%E%>)", fn
);
8051 /* If a pointer to a function that is multi-versioned is requested, the
8052 pointer to the dispatcher function is returned instead. This works
8053 well because indirectly calling the function will dispatch the right
8054 function version at run-time. */
8055 if (DECL_FUNCTION_VERSIONED (fn
))
8057 fn
= get_function_version_dispatcher (fn
);
8059 return error_mark_node
;
8060 /* Mark all the versions corresponding to the dispatcher as used. */
8061 if (!(complain
& tf_conv
))
8062 mark_versions_used (fn
);
8065 /* If we're doing overload resolution purely for the purpose of
8066 determining conversion sequences, we should not consider the
8067 function used. If this conversion sequence is selected, the
8068 function will be marked as used at this point. */
8069 if (!(complain
& tf_conv
))
8071 /* Make =delete work with SFINAE. */
8072 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8073 return error_mark_node
;
8074 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8075 return error_mark_node
;
8078 /* We could not check access to member functions when this
8079 expression was originally created since we did not know at that
8080 time to which function the expression referred. */
8081 if (DECL_FUNCTION_MEMBER_P (fn
))
8083 gcc_assert (access_path
);
8084 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8087 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8088 return cp_build_addr_expr (fn
, complain
);
8091 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8092 will mark the function as addressed, but here we must do it
8094 cxx_mark_addressable (fn
);
8100 /* This function will instantiate the type of the expression given in
8101 RHS to match the type of LHSTYPE. If errors exist, then return
8102 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8103 we complain on errors. If we are not complaining, never modify rhs,
8104 as overload resolution wants to try many possible instantiations, in
8105 the hope that at least one will work.
8107 For non-recursive calls, LHSTYPE should be a function, pointer to
8108 function, or a pointer to member function. */
8111 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8113 tsubst_flags_t complain_in
= complain
;
8114 tree access_path
= NULL_TREE
;
8116 complain
&= ~tf_ptrmem_ok
;
8118 if (lhstype
== unknown_type_node
)
8120 if (complain
& tf_error
)
8121 error ("not enough type information");
8122 return error_mark_node
;
8125 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8127 tree fntype
= non_reference (lhstype
);
8128 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8130 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8132 if (flag_ms_extensions
8133 && TYPE_PTRMEMFUNC_P (fntype
)
8134 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8135 /* Microsoft allows `A::f' to be resolved to a
8136 pointer-to-member. */
8140 if (complain
& tf_error
)
8141 error ("cannot convert %qE from type %qT to type %qT",
8142 rhs
, TREE_TYPE (rhs
), fntype
);
8143 return error_mark_node
;
8147 /* If we instantiate a template, and it is a A ?: C expression
8148 with omitted B, look through the SAVE_EXPR. */
8149 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8150 rhs
= TREE_OPERAND (rhs
, 0);
8152 if (BASELINK_P (rhs
))
8154 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8155 rhs
= BASELINK_FUNCTIONS (rhs
);
8158 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8159 deduce any type information. */
8160 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
8162 if (complain
& tf_error
)
8163 error ("not enough type information");
8164 return error_mark_node
;
8167 /* There are only a few kinds of expressions that may have a type
8168 dependent on overload resolution. */
8169 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
8170 || TREE_CODE (rhs
) == COMPONENT_REF
8171 || is_overloaded_fn (rhs
)
8172 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8174 /* This should really only be used when attempting to distinguish
8175 what sort of a pointer to function we have. For now, any
8176 arithmetic operation which is not supported on pointers
8177 is rejected as an error. */
8179 switch (TREE_CODE (rhs
))
8183 tree member
= TREE_OPERAND (rhs
, 1);
8185 member
= instantiate_type (lhstype
, member
, complain
);
8186 if (member
!= error_mark_node
8187 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8188 /* Do not lose object's side effects. */
8189 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8190 TREE_OPERAND (rhs
, 0), member
);
8195 rhs
= TREE_OPERAND (rhs
, 1);
8196 if (BASELINK_P (rhs
))
8197 return instantiate_type (lhstype
, rhs
, complain_in
);
8199 /* This can happen if we are forming a pointer-to-member for a
8201 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8205 case TEMPLATE_ID_EXPR
:
8207 tree fns
= TREE_OPERAND (rhs
, 0);
8208 tree args
= TREE_OPERAND (rhs
, 1);
8211 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8212 /*template_only=*/true,
8219 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8220 /*template_only=*/false,
8221 /*explicit_targs=*/NULL_TREE
,
8226 if (PTRMEM_OK_P (rhs
))
8227 complain
|= tf_ptrmem_ok
;
8229 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8233 return error_mark_node
;
8238 return error_mark_node
;
8241 /* Return the name of the virtual function pointer field
8242 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8243 this may have to look back through base types to find the
8244 ultimate field name. (For single inheritance, these could
8245 all be the same name. Who knows for multiple inheritance). */
8248 get_vfield_name (tree type
)
8250 tree binfo
, base_binfo
;
8252 for (binfo
= TYPE_BINFO (type
);
8253 BINFO_N_BASE_BINFOS (binfo
);
8256 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8258 if (BINFO_VIRTUAL_P (base_binfo
)
8259 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8263 type
= BINFO_TYPE (binfo
);
8264 tree ctor_name
= constructor_name (type
);
8265 char *buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8266 + IDENTIFIER_LENGTH (ctor_name
) + 2);
8267 sprintf (buf
, VFIELD_NAME_FORMAT
, IDENTIFIER_POINTER (ctor_name
));
8268 return get_identifier (buf
);
8271 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8272 according to [class]:
8273 The class-name is also inserted
8274 into the scope of the class itself. For purposes of access checking,
8275 the inserted class name is treated as if it were a public member name. */
8278 build_self_reference (void)
8280 tree name
= DECL_NAME (TYPE_NAME (current_class_type
));
8281 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8283 DECL_NONLOCAL (value
) = 1;
8284 DECL_CONTEXT (value
) = current_class_type
;
8285 DECL_ARTIFICIAL (value
) = 1;
8286 SET_DECL_SELF_REFERENCE_P (value
);
8287 set_underlying_type (value
);
8289 if (processing_template_decl
)
8290 value
= push_template_decl (value
);
8292 tree saved_cas
= current_access_specifier
;
8293 current_access_specifier
= access_public_node
;
8294 finish_member_declaration (value
);
8295 current_access_specifier
= saved_cas
;
8298 /* Returns 1 if TYPE contains only padding bytes. */
8301 is_empty_class (tree type
)
8303 if (type
== error_mark_node
)
8306 if (! CLASS_TYPE_P (type
))
8309 return CLASSTYPE_EMPTY_P (type
);
8312 /* Returns true if TYPE contains no actual data, just various
8313 possible combinations of empty classes and possibly a vptr. */
8316 is_really_empty_class (tree type
)
8318 if (CLASS_TYPE_P (type
))
8325 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8326 out, but we'd like to be able to check this before then. */
8327 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8330 for (binfo
= TYPE_BINFO (type
), i
= 0;
8331 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8332 if (!is_really_empty_class (BINFO_TYPE (base_binfo
)))
8334 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8335 if (TREE_CODE (field
) == FIELD_DECL
8336 && !DECL_ARTIFICIAL (field
)
8337 /* An unnamed bit-field is not a data member. */
8338 && !DECL_UNNAMED_BIT_FIELD (field
)
8339 && !is_really_empty_class (TREE_TYPE (field
)))
8343 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8344 return (integer_zerop (array_type_nelts_top (type
))
8345 || is_really_empty_class (TREE_TYPE (type
)));
8349 /* Note that NAME was looked up while the current class was being
8350 defined and that the result of that lookup was DECL. */
8353 maybe_note_name_used_in_class (tree name
, tree decl
)
8355 splay_tree names_used
;
8357 /* If we're not defining a class, there's nothing to do. */
8358 if (!(innermost_scope_kind() == sk_class
8359 && TYPE_BEING_DEFINED (current_class_type
)
8360 && !LAMBDA_TYPE_P (current_class_type
)))
8363 /* If there's already a binding for this NAME, then we don't have
8364 anything to worry about. */
8365 if (lookup_member (current_class_type
, name
,
8366 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8369 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8370 current_class_stack
[current_class_depth
- 1].names_used
8371 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8372 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8374 splay_tree_insert (names_used
,
8375 (splay_tree_key
) name
,
8376 (splay_tree_value
) decl
);
8379 /* Note that NAME was declared (as DECL) in the current class. Check
8380 to see that the declaration is valid. */
8383 note_name_declared_in_class (tree name
, tree decl
)
8385 splay_tree names_used
;
8388 /* Look to see if we ever used this name. */
8390 = current_class_stack
[current_class_depth
- 1].names_used
;
8393 /* The C language allows members to be declared with a type of the same
8394 name, and the C++ standard says this diagnostic is not required. So
8395 allow it in extern "C" blocks unless predantic is specified.
8396 Allow it in all cases if -ms-extensions is specified. */
8397 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8398 || flag_ms_extensions
)
8400 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8403 /* [basic.scope.class]
8405 A name N used in a class S shall refer to the same declaration
8406 in its context and when re-evaluated in the completed scope of
8408 if (permerror (location_of (decl
),
8409 "declaration of %q#D changes meaning of %qD",
8410 decl
, OVL_NAME (decl
)))
8411 inform (location_of ((tree
) n
->value
),
8412 "%qD declared here as %q#D",
8413 OVL_NAME (decl
), (tree
) n
->value
);
8417 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8418 Secondary vtables are merged with primary vtables; this function
8419 will return the VAR_DECL for the primary vtable. */
8422 get_vtbl_decl_for_binfo (tree binfo
)
8426 decl
= BINFO_VTABLE (binfo
);
8427 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8429 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
8430 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8433 gcc_assert (VAR_P (decl
));
8438 /* Returns the binfo for the primary base of BINFO. If the resulting
8439 BINFO is a virtual base, and it is inherited elsewhere in the
8440 hierarchy, then the returned binfo might not be the primary base of
8441 BINFO in the complete object. Check BINFO_PRIMARY_P or
8442 BINFO_LOST_PRIMARY_P to be sure. */
8445 get_primary_binfo (tree binfo
)
8449 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8453 return copied_binfo (primary_base
, binfo
);
8456 /* As above, but iterate until we reach the binfo that actually provides the
8460 most_primary_binfo (tree binfo
)
8463 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
8464 && !BINFO_LOST_PRIMARY_P (b
))
8466 tree primary_base
= get_primary_binfo (b
);
8467 gcc_assert (BINFO_PRIMARY_P (primary_base
)
8468 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
8474 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8475 type. Note that the virtual inheritance might be above or below BINFO in
8479 vptr_via_virtual_p (tree binfo
)
8482 binfo
= TYPE_BINFO (binfo
);
8483 tree primary
= most_primary_binfo (binfo
);
8484 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8485 a morally virtual base. */
8486 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
8487 return virt
!= NULL_TREE
;
8490 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8493 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8496 fprintf (stream
, "%*s", indent
, "");
8500 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8501 INDENT should be zero when called from the top level; it is
8502 incremented recursively. IGO indicates the next expected BINFO in
8503 inheritance graph ordering. */
8506 dump_class_hierarchy_r (FILE *stream
,
8516 indented
= maybe_indent_hierarchy (stream
, indent
, 0);
8517 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8518 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8519 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8522 fprintf (stream
, "alternative-path\n");
8525 igo
= TREE_CHAIN (binfo
);
8527 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8528 tree_to_shwi (BINFO_OFFSET (binfo
)));
8529 if (is_empty_class (BINFO_TYPE (binfo
)))
8530 fprintf (stream
, " empty");
8531 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8532 fprintf (stream
, " nearly-empty");
8533 if (BINFO_VIRTUAL_P (binfo
))
8534 fprintf (stream
, " virtual");
8535 fprintf (stream
, "\n");
8538 if (BINFO_PRIMARY_P (binfo
))
8540 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8541 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8542 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8543 TFF_PLAIN_IDENTIFIER
),
8544 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8546 if (BINFO_LOST_PRIMARY_P (binfo
))
8548 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8549 fprintf (stream
, " lost-primary");
8552 fprintf (stream
, "\n");
8554 if (!(flags
& TDF_SLIM
))
8558 if (BINFO_SUBVTT_INDEX (binfo
))
8560 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8561 fprintf (stream
, " subvttidx=%s",
8562 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8563 TFF_PLAIN_IDENTIFIER
));
8565 if (BINFO_VPTR_INDEX (binfo
))
8567 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8568 fprintf (stream
, " vptridx=%s",
8569 expr_as_string (BINFO_VPTR_INDEX (binfo
),
8570 TFF_PLAIN_IDENTIFIER
));
8572 if (BINFO_VPTR_FIELD (binfo
))
8574 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8575 fprintf (stream
, " vbaseoffset=%s",
8576 expr_as_string (BINFO_VPTR_FIELD (binfo
),
8577 TFF_PLAIN_IDENTIFIER
));
8579 if (BINFO_VTABLE (binfo
))
8581 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8582 fprintf (stream
, " vptr=%s",
8583 expr_as_string (BINFO_VTABLE (binfo
),
8584 TFF_PLAIN_IDENTIFIER
));
8588 fprintf (stream
, "\n");
8591 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
8592 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
8597 /* Dump the BINFO hierarchy for T. */
8600 dump_class_hierarchy_1 (FILE *stream
, dump_flags_t flags
, tree t
)
8602 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8603 fprintf (stream
, " size=%lu align=%lu\n",
8604 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
8605 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
8606 fprintf (stream
, " base size=%lu base align=%lu\n",
8607 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t
)))
8609 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t
))
8611 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
8612 fprintf (stream
, "\n");
8615 /* Debug interface to hierarchy dumping. */
8618 debug_class (tree t
)
8620 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
8624 dump_class_hierarchy (tree t
)
8627 if (FILE *stream
= dump_begin (class_dump_id
, &flags
))
8629 dump_class_hierarchy_1 (stream
, flags
, t
);
8630 dump_end (class_dump_id
, stream
);
8635 dump_array (FILE * stream
, tree decl
)
8638 unsigned HOST_WIDE_INT ix
;
8640 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
8642 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
8644 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
8645 fprintf (stream
, " %s entries",
8646 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
8647 TFF_PLAIN_IDENTIFIER
));
8648 fprintf (stream
, "\n");
8650 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
8652 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
8653 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
8657 dump_vtable (tree t
, tree binfo
, tree vtable
)
8660 FILE *stream
= dump_begin (class_dump_id
, &flags
);
8665 if (!(flags
& TDF_SLIM
))
8667 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
8669 fprintf (stream
, "%s for %s",
8670 ctor_vtbl_p
? "Construction vtable" : "Vtable",
8671 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
8674 if (!BINFO_VIRTUAL_P (binfo
))
8675 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
8676 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8677 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8679 fprintf (stream
, "\n");
8680 dump_array (stream
, vtable
);
8681 fprintf (stream
, "\n");
8684 dump_end (class_dump_id
, stream
);
8688 dump_vtt (tree t
, tree vtt
)
8691 FILE *stream
= dump_begin (class_dump_id
, &flags
);
8696 if (!(flags
& TDF_SLIM
))
8698 fprintf (stream
, "VTT for %s\n",
8699 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8700 dump_array (stream
, vtt
);
8701 fprintf (stream
, "\n");
8704 dump_end (class_dump_id
, stream
);
8707 /* Dump a function or thunk and its thunkees. */
8710 dump_thunk (FILE *stream
, int indent
, tree thunk
)
8712 static const char spaces
[] = " ";
8713 tree name
= DECL_NAME (thunk
);
8716 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
8718 !DECL_THUNK_P (thunk
) ? "function"
8719 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
8720 name
? IDENTIFIER_POINTER (name
) : "<unset>");
8721 if (DECL_THUNK_P (thunk
))
8723 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
8724 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
8726 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
8727 if (!virtual_adjust
)
8729 else if (DECL_THIS_THUNK_P (thunk
))
8730 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
8731 tree_to_shwi (virtual_adjust
));
8733 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
8734 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
8735 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
8736 if (THUNK_ALIAS (thunk
))
8737 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
8739 fprintf (stream
, "\n");
8740 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
8741 dump_thunk (stream
, indent
+ 2, thunks
);
8744 /* Dump the thunks for FN. */
8747 debug_thunks (tree fn
)
8749 dump_thunk (stderr
, 0, fn
);
8752 /* Virtual function table initialization. */
8754 /* Create all the necessary vtables for T and its base classes. */
8757 finish_vtbls (tree t
)
8760 vec
<constructor_elt
, va_gc
> *v
= NULL
;
8761 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
8763 /* We lay out the primary and secondary vtables in one contiguous
8764 vtable. The primary vtable is first, followed by the non-virtual
8765 secondary vtables in inheritance graph order. */
8766 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
8769 /* Then come the virtual bases, also in inheritance graph order. */
8770 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
8772 if (!BINFO_VIRTUAL_P (vbase
))
8774 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
8777 if (BINFO_VTABLE (TYPE_BINFO (t
)))
8778 initialize_vtable (TYPE_BINFO (t
), v
);
8781 /* Initialize the vtable for BINFO with the INITS. */
8784 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
8788 layout_vtable_decl (binfo
, vec_safe_length (inits
));
8789 decl
= get_vtbl_decl_for_binfo (binfo
);
8790 initialize_artificial_var (decl
, inits
);
8791 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
8794 /* Build the VTT (virtual table table) for T.
8795 A class requires a VTT if it has virtual bases.
8798 1 - primary virtual pointer for complete object T
8799 2 - secondary VTTs for each direct non-virtual base of T which requires a
8801 3 - secondary virtual pointers for each direct or indirect base of T which
8802 has virtual bases or is reachable via a virtual path from T.
8803 4 - secondary VTTs for each direct or indirect virtual base of T.
8805 Secondary VTTs look like complete object VTTs without part 4. */
8813 vec
<constructor_elt
, va_gc
> *inits
;
8815 /* Build up the initializers for the VTT. */
8817 index
= size_zero_node
;
8818 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
8820 /* If we didn't need a VTT, we're done. */
8824 /* Figure out the type of the VTT. */
8825 type
= build_array_of_n_type (const_ptr_type_node
,
8828 /* Now, build the VTT object itself. */
8829 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
8830 initialize_artificial_var (vtt
, inits
);
8831 /* Add the VTT to the vtables list. */
8832 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
8833 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
8838 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8839 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8840 and CHAIN the vtable pointer for this binfo after construction is
8841 complete. VALUE can also be another BINFO, in which case we recurse. */
8844 binfo_ctor_vtable (tree binfo
)
8850 vt
= BINFO_VTABLE (binfo
);
8851 if (TREE_CODE (vt
) == TREE_LIST
)
8852 vt
= TREE_VALUE (vt
);
8853 if (TREE_CODE (vt
) == TREE_BINFO
)
8862 /* Data for secondary VTT initialization. */
8863 struct secondary_vptr_vtt_init_data
8865 /* Is this the primary VTT? */
8868 /* Current index into the VTT. */
8871 /* Vector of initializers built up. */
8872 vec
<constructor_elt
, va_gc
> *inits
;
8874 /* The type being constructed by this secondary VTT. */
8875 tree type_being_constructed
;
8878 /* Recursively build the VTT-initializer for BINFO (which is in the
8879 hierarchy dominated by T). INITS points to the end of the initializer
8880 list to date. INDEX is the VTT index where the next element will be
8881 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8882 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8883 for virtual bases of T. When it is not so, we build the constructor
8884 vtables for the BINFO-in-T variant. */
8887 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
8893 secondary_vptr_vtt_init_data data
;
8894 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
8896 /* We only need VTTs for subobjects with virtual bases. */
8897 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
8900 /* We need to use a construction vtable if this is not the primary
8904 build_ctor_vtbl_group (binfo
, t
);
8906 /* Record the offset in the VTT where this sub-VTT can be found. */
8907 BINFO_SUBVTT_INDEX (binfo
) = *index
;
8910 /* Add the address of the primary vtable for the complete object. */
8911 init
= binfo_ctor_vtable (binfo
);
8912 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
8915 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8916 BINFO_VPTR_INDEX (binfo
) = *index
;
8918 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
8920 /* Recursively add the secondary VTTs for non-virtual bases. */
8921 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
8922 if (!BINFO_VIRTUAL_P (b
))
8923 build_vtt_inits (b
, t
, inits
, index
);
8925 /* Add secondary virtual pointers for all subobjects of BINFO with
8926 either virtual bases or reachable along a virtual path, except
8927 subobjects that are non-virtual primary bases. */
8928 data
.top_level_p
= top_level_p
;
8929 data
.index
= *index
;
8930 data
.inits
= *inits
;
8931 data
.type_being_constructed
= BINFO_TYPE (binfo
);
8933 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
8935 *index
= data
.index
;
8937 /* data.inits might have grown as we added secondary virtual pointers.
8938 Make sure our caller knows about the new vector. */
8939 *inits
= data
.inits
;
8942 /* Add the secondary VTTs for virtual bases in inheritance graph
8944 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
8946 if (!BINFO_VIRTUAL_P (b
))
8949 build_vtt_inits (b
, t
, inits
, index
);
8952 /* Remove the ctor vtables we created. */
8953 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
8956 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8957 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8960 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
8962 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
8964 /* We don't care about bases that don't have vtables. */
8965 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
8966 return dfs_skip_bases
;
8968 /* We're only interested in proper subobjects of the type being
8970 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
8973 /* We're only interested in bases with virtual bases or reachable
8974 via a virtual path from the type being constructed. */
8975 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
8976 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
8977 return dfs_skip_bases
;
8979 /* We're not interested in non-virtual primary bases. */
8980 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
8983 /* Record the index where this secondary vptr can be found. */
8984 if (data
->top_level_p
)
8986 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8987 BINFO_VPTR_INDEX (binfo
) = data
->index
;
8989 if (BINFO_VIRTUAL_P (binfo
))
8991 /* It's a primary virtual base, and this is not a
8992 construction vtable. Find the base this is primary of in
8993 the inheritance graph, and use that base's vtable
8995 while (BINFO_PRIMARY_P (binfo
))
8996 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
9000 /* Add the initializer for the secondary vptr itself. */
9001 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
9003 /* Advance the vtt index. */
9004 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
9005 TYPE_SIZE_UNIT (ptr_type_node
));
9010 /* Called from build_vtt_inits via dfs_walk. After building
9011 constructor vtables and generating the sub-vtt from them, we need
9012 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9013 binfo of the base whose sub vtt was generated. */
9016 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9018 tree vtable
= BINFO_VTABLE (binfo
);
9020 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9021 /* If this class has no vtable, none of its bases do. */
9022 return dfs_skip_bases
;
9025 /* This might be a primary base, so have no vtable in this
9029 /* If we scribbled the construction vtable vptr into BINFO, clear it
9031 if (TREE_CODE (vtable
) == TREE_LIST
9032 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9033 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9038 /* Build the construction vtable group for BINFO which is in the
9039 hierarchy dominated by T. */
9042 build_ctor_vtbl_group (tree binfo
, tree t
)
9048 vec
<constructor_elt
, va_gc
> *v
;
9050 /* See if we've already created this construction vtable group. */
9051 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9052 if (get_global_binding (id
))
9055 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9056 /* Build a version of VTBL (with the wrong type) for use in
9057 constructing the addresses of secondary vtables in the
9058 construction vtable group. */
9059 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9060 DECL_CONSTRUCTION_VTABLE_P (vtbl
) = 1;
9061 /* Don't export construction vtables from shared libraries. Even on
9062 targets that don't support hidden visibility, this tells
9063 can_refer_decl_in_current_unit_p not to assume that it's safe to
9064 access from a different compilation unit (bz 54314). */
9065 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9066 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9069 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9070 binfo
, vtbl
, t
, &v
);
9072 /* Add the vtables for each of our virtual bases using the vbase in T
9074 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9076 vbase
= TREE_CHAIN (vbase
))
9080 if (!BINFO_VIRTUAL_P (vbase
))
9082 b
= copied_binfo (vbase
, binfo
);
9084 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9087 /* Figure out the type of the construction vtable. */
9088 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9090 TREE_TYPE (vtbl
) = type
;
9091 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9092 layout_decl (vtbl
, 0);
9094 /* Initialize the construction vtable. */
9095 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9096 initialize_artificial_var (vtbl
, v
);
9097 dump_vtable (t
, binfo
, vtbl
);
9100 /* Add the vtbl initializers for BINFO (and its bases other than
9101 non-virtual primaries) to the list of INITS. BINFO is in the
9102 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9103 the constructor the vtbl inits should be accumulated for. (If this
9104 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9105 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9106 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9107 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9108 but are not necessarily the same in terms of layout. */
9111 accumulate_vtbl_inits (tree binfo
,
9116 vec
<constructor_elt
, va_gc
> **inits
)
9120 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9122 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9124 /* If it doesn't have a vptr, we don't do anything. */
9125 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9128 /* If we're building a construction vtable, we're not interested in
9129 subobjects that don't require construction vtables. */
9131 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9132 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9135 /* Build the initializers for the BINFO-in-T vtable. */
9136 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9138 /* Walk the BINFO and its bases. We walk in preorder so that as we
9139 initialize each vtable we can figure out at what offset the
9140 secondary vtable lies from the primary vtable. We can't use
9141 dfs_walk here because we need to iterate through bases of BINFO
9142 and RTTI_BINFO simultaneously. */
9143 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9145 /* Skip virtual bases. */
9146 if (BINFO_VIRTUAL_P (base_binfo
))
9148 accumulate_vtbl_inits (base_binfo
,
9149 BINFO_BASE_BINFO (orig_binfo
, i
),
9150 rtti_binfo
, vtbl
, t
,
9155 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9156 BINFO vtable to L. */
9159 dfs_accumulate_vtbl_inits (tree binfo
,
9164 vec
<constructor_elt
, va_gc
> **l
)
9166 tree vtbl
= NULL_TREE
;
9167 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9171 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9173 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9174 primary virtual base. If it is not the same primary in
9175 the hierarchy of T, we'll need to generate a ctor vtable
9176 for it, to place at its location in T. If it is the same
9177 primary, we still need a VTT entry for the vtable, but it
9178 should point to the ctor vtable for the base it is a
9179 primary for within the sub-hierarchy of RTTI_BINFO.
9181 There are three possible cases:
9183 1) We are in the same place.
9184 2) We are a primary base within a lost primary virtual base of
9186 3) We are primary to something not a base of RTTI_BINFO. */
9189 tree last
= NULL_TREE
;
9191 /* First, look through the bases we are primary to for RTTI_BINFO
9192 or a virtual base. */
9194 while (BINFO_PRIMARY_P (b
))
9196 b
= BINFO_INHERITANCE_CHAIN (b
);
9198 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9201 /* If we run out of primary links, keep looking down our
9202 inheritance chain; we might be an indirect primary. */
9203 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9204 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9208 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9209 base B and it is a base of RTTI_BINFO, this is case 2. In
9210 either case, we share our vtable with LAST, i.e. the
9211 derived-most base within B of which we are a primary. */
9213 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9214 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9215 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9216 binfo_ctor_vtable after everything's been set up. */
9219 /* Otherwise, this is case 3 and we get our own. */
9221 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9224 n_inits
= vec_safe_length (*l
);
9231 /* Add the initializer for this vtable. */
9232 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9233 &non_fn_entries
, l
);
9235 /* Figure out the position to which the VPTR should point. */
9236 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9237 index
= size_binop (MULT_EXPR
,
9238 TYPE_SIZE_UNIT (vtable_entry_type
),
9239 size_int (non_fn_entries
+ n_inits
));
9240 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9244 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9245 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9246 straighten this out. */
9247 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9248 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9249 /* Throw away any unneeded intializers. */
9250 (*l
)->truncate (n_inits
);
9252 /* For an ordinary vtable, set BINFO_VTABLE. */
9253 BINFO_VTABLE (binfo
) = vtbl
;
9256 static GTY(()) tree abort_fndecl_addr
;
9257 static GTY(()) tree dvirt_fn
;
9259 /* Construct the initializer for BINFO's virtual function table. BINFO
9260 is part of the hierarchy dominated by T. If we're building a
9261 construction vtable, the ORIG_BINFO is the binfo we should use to
9262 find the actual function pointers to put in the vtable - but they
9263 can be overridden on the path to most-derived in the graph that
9264 ORIG_BINFO belongs. Otherwise,
9265 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9266 BINFO that should be indicated by the RTTI information in the
9267 vtable; it will be a base class of T, rather than T itself, if we
9268 are building a construction vtable.
9270 The value returned is a TREE_LIST suitable for wrapping in a
9271 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9272 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9273 number of non-function entries in the vtable.
9275 It might seem that this function should never be called with a
9276 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9277 base is always subsumed by a derived class vtable. However, when
9278 we are building construction vtables, we do build vtables for
9279 primary bases; we need these while the primary base is being
9283 build_vtbl_initializer (tree binfo
,
9287 int* non_fn_entries_p
,
9288 vec
<constructor_elt
, va_gc
> **inits
)
9294 vec
<tree
, va_gc
> *vbases
;
9297 /* Initialize VID. */
9298 memset (&vid
, 0, sizeof (vid
));
9301 vid
.rtti_binfo
= rtti_binfo
;
9302 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9303 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9304 vid
.generate_vcall_entries
= true;
9305 /* The first vbase or vcall offset is at index -3 in the vtable. */
9306 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
9308 /* Add entries to the vtable for RTTI. */
9309 build_rtti_vtbl_entries (binfo
, &vid
);
9311 /* Create an array for keeping track of the functions we've
9312 processed. When we see multiple functions with the same
9313 signature, we share the vcall offsets. */
9314 vec_alloc (vid
.fns
, 32);
9315 /* Add the vcall and vbase offset entries. */
9316 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
9318 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9319 build_vbase_offset_vtbl_entries. */
9320 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
9321 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
9322 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
9324 /* If the target requires padding between data entries, add that now. */
9325 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
9327 int n_entries
= vec_safe_length (vid
.inits
);
9329 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
);
9331 /* Move data entries into their new positions and add padding
9332 after the new positions. Iterate backwards so we don't
9333 overwrite entries that we would need to process later. */
9334 for (ix
= n_entries
- 1;
9335 vid
.inits
->iterate (ix
, &e
);
9339 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
9340 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
9342 (*vid
.inits
)[new_position
] = *e
;
9344 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
9346 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
9347 f
->index
= NULL_TREE
;
9348 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9354 if (non_fn_entries_p
)
9355 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9357 /* The initializers for virtual functions were built up in reverse
9358 order. Straighten them out and add them to the running list in one
9360 jx
= vec_safe_length (*inits
);
9361 vec_safe_grow (*inits
, jx
+ vid
.inits
->length ());
9363 for (ix
= vid
.inits
->length () - 1;
9364 vid
.inits
->iterate (ix
, &e
);
9368 /* Go through all the ordinary virtual functions, building up
9370 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9374 tree fn
, fn_original
;
9375 tree init
= NULL_TREE
;
9379 if (DECL_THUNK_P (fn
))
9381 if (!DECL_NAME (fn
))
9383 if (THUNK_ALIAS (fn
))
9385 fn
= THUNK_ALIAS (fn
);
9388 fn_original
= THUNK_TARGET (fn
);
9391 /* If the only definition of this function signature along our
9392 primary base chain is from a lost primary, this vtable slot will
9393 never be used, so just zero it out. This is important to avoid
9394 requiring extra thunks which cannot be generated with the function.
9396 We first check this in update_vtable_entry_for_fn, so we handle
9397 restored primary bases properly; we also need to do it here so we
9398 zero out unused slots in ctor vtables, rather than filling them
9399 with erroneous values (though harmless, apart from relocation
9401 if (BV_LOST_PRIMARY (v
))
9402 init
= size_zero_node
;
9406 /* Pull the offset for `this', and the function to call, out of
9408 delta
= BV_DELTA (v
);
9409 vcall_index
= BV_VCALL_INDEX (v
);
9411 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9412 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9414 /* You can't call an abstract virtual function; it's abstract.
9415 So, we replace these functions with __pure_virtual. */
9416 if (DECL_PURE_VIRTUAL_P (fn_original
))
9419 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9421 if (abort_fndecl_addr
== NULL
)
9423 = fold_convert (vfunc_ptr_type_node
,
9424 build_fold_addr_expr (fn
));
9425 init
= abort_fndecl_addr
;
9428 /* Likewise for deleted virtuals. */
9429 else if (DECL_DELETED_FN (fn_original
))
9433 tree name
= get_identifier ("__cxa_deleted_virtual");
9434 dvirt_fn
= get_global_binding (name
);
9436 dvirt_fn
= push_library_fn
9438 build_function_type_list (void_type_node
, NULL_TREE
),
9439 NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
9442 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9443 init
= fold_convert (vfunc_ptr_type_node
,
9444 build_fold_addr_expr (fn
));
9448 if (!integer_zerop (delta
) || vcall_index
)
9450 fn
= make_thunk (fn
, /*this_adjusting=*/1,
9451 delta
, vcall_index
);
9452 if (!DECL_NAME (fn
))
9455 /* Take the address of the function, considering it to be of an
9456 appropriate generic type. */
9457 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9458 init
= fold_convert (vfunc_ptr_type_node
,
9459 build_fold_addr_expr (fn
));
9460 /* Don't refer to a virtual destructor from a constructor
9461 vtable or a vtable for an abstract class, since destroying
9462 an object under construction is undefined behavior and we
9463 don't want it to be considered a candidate for speculative
9464 devirtualization. But do create the thunk for ABI
9466 if (DECL_DESTRUCTOR_P (fn_original
)
9467 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9468 || orig_binfo
!= binfo
))
9469 init
= size_zero_node
;
9473 /* And add it to the chain of initializers. */
9474 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9477 if (init
== size_zero_node
)
9478 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9479 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9481 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9483 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9484 fn
, build_int_cst (NULL_TREE
, i
));
9485 TREE_CONSTANT (fdesc
) = 1;
9487 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), fdesc
);
9491 CONSTRUCTOR_APPEND_ELT (*inits
, size_int (jx
++), init
);
9495 /* Adds to vid->inits the initializers for the vbase and vcall
9496 offsets in BINFO, which is in the hierarchy dominated by T. */
9499 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9503 /* If this is a derived class, we must first create entries
9504 corresponding to the primary base class. */
9505 b
= get_primary_binfo (binfo
);
9507 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9509 /* Add the vbase entries for this base. */
9510 build_vbase_offset_vtbl_entries (binfo
, vid
);
9511 /* Add the vcall entries for this base. */
9512 build_vcall_offset_vtbl_entries (binfo
, vid
);
9515 /* Returns the initializers for the vbase offset entries in the vtable
9516 for BINFO (which is part of the class hierarchy dominated by T), in
9517 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9518 where the next vbase offset will go. */
9521 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9525 tree non_primary_binfo
;
9527 /* If there are no virtual baseclasses, then there is nothing to
9529 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9534 /* We might be a primary base class. Go up the inheritance hierarchy
9535 until we find the most derived class of which we are a primary base:
9536 it is the offset of that which we need to use. */
9537 non_primary_binfo
= binfo
;
9538 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9542 /* If we have reached a virtual base, then it must be a primary
9543 base (possibly multi-level) of vid->binfo, or we wouldn't
9544 have called build_vcall_and_vbase_vtbl_entries for it. But it
9545 might be a lost primary, so just skip down to vid->binfo. */
9546 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9548 non_primary_binfo
= vid
->binfo
;
9552 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9553 if (get_primary_binfo (b
) != non_primary_binfo
)
9555 non_primary_binfo
= b
;
9558 /* Go through the virtual bases, adding the offsets. */
9559 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9561 vbase
= TREE_CHAIN (vbase
))
9566 if (!BINFO_VIRTUAL_P (vbase
))
9569 /* Find the instance of this virtual base in the complete
9571 b
= copied_binfo (vbase
, binfo
);
9573 /* If we've already got an offset for this virtual base, we
9574 don't need another one. */
9575 if (BINFO_VTABLE_PATH_MARKED (b
))
9577 BINFO_VTABLE_PATH_MARKED (b
) = 1;
9579 /* Figure out where we can find this vbase offset. */
9580 delta
= size_binop (MULT_EXPR
,
9582 fold_convert (ssizetype
,
9583 TYPE_SIZE_UNIT (vtable_entry_type
)));
9584 if (vid
->primary_vtbl_p
)
9585 BINFO_VPTR_FIELD (b
) = delta
;
9587 if (binfo
!= TYPE_BINFO (t
))
9588 /* The vbase offset had better be the same. */
9589 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
9591 /* The next vbase will come at a more negative offset. */
9592 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9593 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9595 /* The initializer is the delta from BINFO to this virtual base.
9596 The vbase offsets go in reverse inheritance-graph order, and
9597 we are walking in inheritance graph order so these end up in
9599 delta
= size_diffop_loc (input_location
,
9600 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
9602 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
9603 fold_build1_loc (input_location
, NOP_EXPR
,
9604 vtable_entry_type
, delta
));
9608 /* Adds the initializers for the vcall offset entries in the vtable
9609 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9613 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9615 /* We only need these entries if this base is a virtual base. We
9616 compute the indices -- but do not add to the vtable -- when
9617 building the main vtable for a class. */
9618 if (binfo
== TYPE_BINFO (vid
->derived
)
9619 || (BINFO_VIRTUAL_P (binfo
)
9620 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9621 correspond to VID->DERIVED), we are building a primary
9622 construction virtual table. Since this is a primary
9623 virtual table, we do not need the vcall offsets for
9625 && binfo
!= vid
->rtti_binfo
))
9627 /* We need a vcall offset for each of the virtual functions in this
9628 vtable. For example:
9630 class A { virtual void f (); };
9631 class B1 : virtual public A { virtual void f (); };
9632 class B2 : virtual public A { virtual void f (); };
9633 class C: public B1, public B2 { virtual void f (); };
9635 A C object has a primary base of B1, which has a primary base of A. A
9636 C also has a secondary base of B2, which no longer has a primary base
9637 of A. So the B2-in-C construction vtable needs a secondary vtable for
9638 A, which will adjust the A* to a B2* to call f. We have no way of
9639 knowing what (or even whether) this offset will be when we define B2,
9640 so we store this "vcall offset" in the A sub-vtable and look it up in
9641 a "virtual thunk" for B2::f.
9643 We need entries for all the functions in our primary vtable and
9644 in our non-virtual bases' secondary vtables. */
9646 /* If we are just computing the vcall indices -- but do not need
9647 the actual entries -- not that. */
9648 if (!BINFO_VIRTUAL_P (binfo
))
9649 vid
->generate_vcall_entries
= false;
9650 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9651 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
9655 /* Build vcall offsets, starting with those for BINFO. */
9658 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
9664 /* Don't walk into virtual bases -- except, of course, for the
9665 virtual base for which we are building vcall offsets. Any
9666 primary virtual base will have already had its offsets generated
9667 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9668 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
9671 /* If BINFO has a primary base, process it first. */
9672 primary_binfo
= get_primary_binfo (binfo
);
9674 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
9676 /* Add BINFO itself to the list. */
9677 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
9679 /* Scan the non-primary bases of BINFO. */
9680 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9681 if (base_binfo
!= primary_binfo
)
9682 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
9685 /* Called from build_vcall_offset_vtbl_entries_r. */
9688 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
9690 /* Make entries for the rest of the virtuals. */
9693 /* The ABI requires that the methods be processed in declaration
9695 for (orig_fn
= TYPE_FIELDS (BINFO_TYPE (binfo
));
9697 orig_fn
= DECL_CHAIN (orig_fn
))
9698 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
9699 add_vcall_offset (orig_fn
, binfo
, vid
);
9702 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9705 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
9711 /* If there is already an entry for a function with the same
9712 signature as FN, then we do not need a second vcall offset.
9713 Check the list of functions already present in the derived
9715 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
9717 if (same_signature_p (derived_entry
, orig_fn
)
9718 /* We only use one vcall offset for virtual destructors,
9719 even though there are two virtual table entries. */
9720 || (DECL_DESTRUCTOR_P (derived_entry
)
9721 && DECL_DESTRUCTOR_P (orig_fn
)))
9725 /* If we are building these vcall offsets as part of building
9726 the vtable for the most derived class, remember the vcall
9728 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
9730 tree_pair_s elt
= {orig_fn
, vid
->index
};
9731 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
9734 /* The next vcall offset will be found at a more negative
9736 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9737 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9739 /* Keep track of this function. */
9740 vec_safe_push (vid
->fns
, orig_fn
);
9742 if (vid
->generate_vcall_entries
)
9747 /* Find the overriding function. */
9748 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
9749 if (fn
== error_mark_node
)
9750 vcall_offset
= build_zero_cst (vtable_entry_type
);
9753 base
= TREE_VALUE (fn
);
9755 /* The vbase we're working on is a primary base of
9756 vid->binfo. But it might be a lost primary, so its
9757 BINFO_OFFSET might be wrong, so we just use the
9758 BINFO_OFFSET from vid->binfo. */
9759 vcall_offset
= size_diffop_loc (input_location
,
9760 BINFO_OFFSET (base
),
9761 BINFO_OFFSET (vid
->binfo
));
9762 vcall_offset
= fold_build1_loc (input_location
,
9763 NOP_EXPR
, vtable_entry_type
,
9766 /* Add the initializer to the vtable. */
9767 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
9771 /* Return vtbl initializers for the RTTI entries corresponding to the
9772 BINFO's vtable. The RTTI entries should indicate the object given
9773 by VID->rtti_binfo. */
9776 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9784 t
= BINFO_TYPE (vid
->rtti_binfo
);
9786 /* To find the complete object, we will first convert to our most
9787 primary base, and then add the offset in the vtbl to that value. */
9788 b
= most_primary_binfo (binfo
);
9789 offset
= size_diffop_loc (input_location
,
9790 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
9792 /* The second entry is the address of the typeinfo object. */
9794 decl
= build_address (get_tinfo_decl (t
));
9796 decl
= integer_zero_node
;
9798 /* Convert the declaration to a type that can be stored in the
9800 init
= build_nop (vfunc_ptr_type_node
, decl
);
9801 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9803 /* Add the offset-to-top entry. It comes earlier in the vtable than
9804 the typeinfo entry. Convert the offset to look like a
9805 function pointer, so that we can put it in the vtable. */
9806 init
= build_nop (vfunc_ptr_type_node
, offset
);
9807 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9810 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9814 uniquely_derived_from_p (tree parent
, tree type
)
9816 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
9817 return base
&& base
!= error_mark_node
;
9820 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9823 publicly_uniquely_derived_p (tree parent
, tree type
)
9825 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
9827 return base
&& base
!= error_mark_node
;
9830 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9831 class between them, if any. */
9834 common_enclosing_class (tree ctx1
, tree ctx2
)
9836 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
9838 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
9839 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
9842 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9843 TYPE_MARKED_P (t
) = true;
9844 tree found
= NULL_TREE
;
9845 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9846 if (TYPE_MARKED_P (t
))
9851 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9852 TYPE_MARKED_P (t
) = false;
9856 #include "gt-cp-class.h"