1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2016 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"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth
;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node
{
50 /* The name of the class. */
53 /* The _TYPE node for the class. */
56 /* The access specifier pending for new declarations in the scope of
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used
;
63 /* Nonzero if this class is no longer open, because of a call to
66 }* class_stack_node_t
;
70 /* The base for which we're building initializers. */
72 /* The type of the most-derived type. */
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec
<constructor_elt
, va_gc
> *inits
;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
83 /* The functions in vbase for which we have already provided vcall
85 vec
<tree
, va_gc
> *fns
;
86 /* The vtable index of the next vcall or vbase offset. */
88 /* Nonzero if we are building the initializer for the primary
91 /* Nonzero if we are building the initializer for a construction
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries
;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn
) (tree
, tree
, splay_tree
);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size
;
105 static class_stack_node_t current_class_stack
;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class
;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec
<tree
, va_gc
> *local_classes
;
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 finish_struct_methods (tree
);
129 static void maybe_warn_about_overly_private_class (tree
);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree
, tree
*, int, int);
133 static tree
fixed_type_or_null (tree
, int *, int *);
134 static tree
build_simple_base_path (tree expr
, tree binfo
);
135 static tree
build_vtbl_ref_1 (tree
, tree
);
136 static void build_vtbl_initializer (tree
, tree
, tree
, tree
, int *,
137 vec
<constructor_elt
, va_gc
> **);
138 static int count_fields (tree
);
139 static int add_fields_to_record_type (tree
, struct sorted_fields_type
*, int);
140 static void insert_into_classtype_sorted_fields (tree
, tree
, int);
141 static bool check_bitfield_decl (tree
);
142 static bool check_field_decl (tree
, tree
, int *, int *);
143 static void check_field_decls (tree
, tree
*, int *, int *);
144 static tree
*build_base_field (record_layout_info
, tree
, splay_tree
, tree
*);
145 static void build_base_fields (record_layout_info
, splay_tree
, tree
*);
146 static void check_methods (tree
);
147 static void remove_zero_width_bit_fields (tree
);
148 static bool accessible_nvdtor_p (tree
);
150 /* Used by find_flexarrays and related functions. */
152 static void diagnose_flexarrays (tree
, const flexmems_t
*);
153 static void find_flexarrays (tree
, flexmems_t
*, bool = false,
154 tree
= NULL_TREE
, tree
= NULL_TREE
);
155 static void check_flexarrays (tree
, flexmems_t
* = NULL
, bool = false);
156 static void check_bases (tree
, int *, int *);
157 static void check_bases_and_members (tree
);
158 static tree
create_vtable_ptr (tree
, tree
*);
159 static void include_empty_classes (record_layout_info
);
160 static void layout_class_type (tree
, tree
*);
161 static void propagate_binfo_offsets (tree
, tree
);
162 static void layout_virtual_bases (record_layout_info
, splay_tree
);
163 static void build_vbase_offset_vtbl_entries (tree
, vtbl_init_data
*);
164 static void add_vcall_offset_vtbl_entries_r (tree
, vtbl_init_data
*);
165 static void add_vcall_offset_vtbl_entries_1 (tree
, vtbl_init_data
*);
166 static void build_vcall_offset_vtbl_entries (tree
, vtbl_init_data
*);
167 static void add_vcall_offset (tree
, tree
, vtbl_init_data
*);
168 static void layout_vtable_decl (tree
, int);
169 static tree
dfs_find_final_overrider_pre (tree
, void *);
170 static tree
dfs_find_final_overrider_post (tree
, void *);
171 static tree
find_final_overrider (tree
, tree
, tree
);
172 static int make_new_vtable (tree
, tree
);
173 static tree
get_primary_binfo (tree
);
174 static int maybe_indent_hierarchy (FILE *, int, int);
175 static tree
dump_class_hierarchy_r (FILE *, int, tree
, tree
, int);
176 static void dump_class_hierarchy (tree
);
177 static void dump_class_hierarchy_1 (FILE *, int, tree
);
178 static void dump_array (FILE *, tree
);
179 static void dump_vtable (tree
, tree
, tree
);
180 static void dump_vtt (tree
, tree
);
181 static void dump_thunk (FILE *, int, tree
);
182 static tree
build_vtable (tree
, tree
, tree
);
183 static void initialize_vtable (tree
, vec
<constructor_elt
, va_gc
> *);
184 static void layout_nonempty_base_or_field (record_layout_info
,
185 tree
, tree
, splay_tree
);
186 static tree
end_of_class (tree
, int);
187 static bool layout_empty_base (record_layout_info
, tree
, tree
, splay_tree
);
188 static void accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
189 vec
<constructor_elt
, va_gc
> **);
190 static void dfs_accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
191 vec
<constructor_elt
, va_gc
> **);
192 static void build_rtti_vtbl_entries (tree
, vtbl_init_data
*);
193 static void build_vcall_and_vbase_vtbl_entries (tree
, vtbl_init_data
*);
194 static void clone_constructors_and_destructors (tree
);
195 static tree
build_clone (tree
, tree
);
196 static void update_vtable_entry_for_fn (tree
, tree
, tree
, tree
*, unsigned);
197 static void build_ctor_vtbl_group (tree
, tree
);
198 static void build_vtt (tree
);
199 static tree
binfo_ctor_vtable (tree
);
200 static void build_vtt_inits (tree
, tree
, vec
<constructor_elt
, va_gc
> **,
202 static tree
dfs_build_secondary_vptr_vtt_inits (tree
, void *);
203 static tree
dfs_fixup_binfo_vtbls (tree
, void *);
204 static int record_subobject_offset (tree
, tree
, splay_tree
);
205 static int check_subobject_offset (tree
, tree
, splay_tree
);
206 static int walk_subobject_offsets (tree
, subobject_offset_fn
,
207 tree
, splay_tree
, tree
, int);
208 static void record_subobject_offsets (tree
, tree
, splay_tree
, bool);
209 static int layout_conflict_p (tree
, tree
, splay_tree
, int);
210 static int splay_tree_compare_integer_csts (splay_tree_key k1
,
212 static void warn_about_ambiguous_bases (tree
);
213 static bool type_requires_array_cookie (tree
);
214 static bool base_derived_from (tree
, tree
);
215 static int empty_base_at_nonzero_offset_p (tree
, tree
, splay_tree
);
216 static tree
end_of_base (tree
);
217 static tree
get_vcall_index (tree
, tree
);
218 static bool type_maybe_constexpr_default_constructor (tree
);
220 /* Variables shared between class.c and call.c. */
223 int n_vtable_entries
= 0;
224 int n_vtable_searches
= 0;
225 int n_vtable_elems
= 0;
226 int n_convert_harshness
= 0;
227 int n_compute_conversion_costs
= 0;
228 int n_inner_fields_searched
= 0;
230 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
231 'structor is in charge of 'structing virtual bases, or FALSE_STMT
235 build_if_in_charge (tree true_stmt
, tree false_stmt
)
237 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl
));
238 tree cmp
= build2 (NE_EXPR
, boolean_type_node
,
239 current_in_charge_parm
, integer_zero_node
);
240 tree type
= unlowered_expr_type (true_stmt
);
241 if (VOID_TYPE_P (type
))
242 type
= unlowered_expr_type (false_stmt
);
243 tree cond
= build3 (COND_EXPR
, type
,
244 cmp
, true_stmt
, false_stmt
);
248 /* Convert to or from a base subobject. EXPR is an expression of type
249 `A' or `A*', an expression of type `B' or `B*' is returned. To
250 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
251 the B base instance within A. To convert base A to derived B, CODE
252 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
253 In this latter case, A must not be a morally virtual base of B.
254 NONNULL is true if EXPR is known to be non-NULL (this is only
255 needed when EXPR is of pointer type). CV qualifiers are preserved
259 build_base_path (enum tree_code code
,
263 tsubst_flags_t complain
)
265 tree v_binfo
= NULL_TREE
;
266 tree d_binfo
= NULL_TREE
;
270 tree null_test
= NULL
;
271 tree ptr_target_type
;
273 int want_pointer
= TYPE_PTR_P (TREE_TYPE (expr
));
274 bool has_empty
= false;
278 if (expr
== error_mark_node
|| binfo
== error_mark_node
|| !binfo
)
279 return error_mark_node
;
281 for (probe
= binfo
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
284 if (is_empty_class (BINFO_TYPE (probe
)))
286 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
290 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
292 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
294 if (code
== PLUS_EXPR
295 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo
), probe
))
297 /* This can happen when adjust_result_of_qualified_name_lookup can't
298 find a unique base binfo in a call to a member function. We
299 couldn't give the diagnostic then since we might have been calling
300 a static member function, so we do it now. In other cases, eg.
301 during error recovery (c++/71979), we may not have a base at all. */
302 if (complain
& tf_error
)
304 tree base
= lookup_base (probe
, BINFO_TYPE (d_binfo
),
305 ba_unique
, NULL
, complain
);
306 gcc_assert (base
== error_mark_node
|| !base
);
308 return error_mark_node
;
311 gcc_assert ((code
== MINUS_EXPR
312 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), probe
))
313 || code
== PLUS_EXPR
);
315 if (binfo
== d_binfo
)
319 if (code
== MINUS_EXPR
&& v_binfo
)
321 if (complain
& tf_error
)
323 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (v_binfo
)))
326 error ("cannot convert from pointer to base class %qT to "
327 "pointer to derived class %qT because the base is "
328 "virtual", BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
330 error ("cannot convert from base class %qT to derived "
331 "class %qT because the base is virtual",
332 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
337 error ("cannot convert from pointer to base class %qT to "
338 "pointer to derived class %qT via virtual base %qT",
339 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
),
340 BINFO_TYPE (v_binfo
));
342 error ("cannot convert from base class %qT to derived "
343 "class %qT via virtual base %qT", BINFO_TYPE (binfo
),
344 BINFO_TYPE (d_binfo
), BINFO_TYPE (v_binfo
));
347 return error_mark_node
;
352 rvalue
= !lvalue_p (expr
);
353 /* This must happen before the call to save_expr. */
354 expr
= cp_build_addr_expr (expr
, complain
);
357 expr
= mark_rvalue_use (expr
);
359 offset
= BINFO_OFFSET (binfo
);
360 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
361 target_type
= code
== PLUS_EXPR
? BINFO_TYPE (binfo
) : BINFO_TYPE (d_binfo
);
362 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
363 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
364 expression returned matches the input. */
365 target_type
= cp_build_qualified_type
366 (target_type
, cp_type_quals (TREE_TYPE (TREE_TYPE (expr
))));
367 ptr_target_type
= build_pointer_type (target_type
);
369 /* Do we need to look in the vtable for the real offset? */
370 virtual_access
= (v_binfo
&& fixed_type_p
<= 0);
372 /* Don't bother with the calculations inside sizeof; they'll ICE if the
373 source type is incomplete and the pointer value doesn't matter. In a
374 template (even in instantiate_non_dependent_expr), we don't have vtables
375 set up properly yet, and the value doesn't matter there either; we're
376 just interested in the result of overload resolution. */
377 if (cp_unevaluated_operand
!= 0
378 || in_template_function ())
380 expr
= build_nop (ptr_target_type
, expr
);
384 /* If we're in an NSDMI, we don't have the full constructor context yet
385 that we need for converting to a virtual base, so just build a stub
386 CONVERT_EXPR and expand it later in bot_replace. */
387 if (virtual_access
&& fixed_type_p
< 0
388 && current_scope () != current_function_decl
)
390 expr
= build1 (CONVERT_EXPR
, ptr_target_type
, expr
);
391 CONVERT_EXPR_VBASE_PATH (expr
) = true;
395 /* Do we need to check for a null pointer? */
396 if (want_pointer
&& !nonnull
)
398 /* If we know the conversion will not actually change the value
399 of EXPR, then we can avoid testing the expression for NULL.
400 We have to avoid generating a COMPONENT_REF for a base class
401 field, because other parts of the compiler know that such
402 expressions are always non-NULL. */
403 if (!virtual_access
&& integer_zerop (offset
))
404 return build_nop (ptr_target_type
, expr
);
405 null_test
= error_mark_node
;
408 /* Protect against multiple evaluation if necessary. */
409 if (TREE_SIDE_EFFECTS (expr
) && (null_test
|| virtual_access
))
410 expr
= save_expr (expr
);
412 /* Now that we've saved expr, build the real null test. */
415 tree zero
= cp_convert (TREE_TYPE (expr
), nullptr_node
, complain
);
416 null_test
= build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
418 /* This is a compiler generated comparison, don't emit
419 e.g. -Wnonnull-compare warning for it. */
420 TREE_NO_WARNING (null_test
) = 1;
423 /* If this is a simple base reference, express it as a COMPONENT_REF. */
424 if (code
== PLUS_EXPR
&& !virtual_access
425 /* We don't build base fields for empty bases, and they aren't very
426 interesting to the optimizers anyway. */
429 expr
= cp_build_indirect_ref (expr
, RO_NULL
, complain
);
430 expr
= build_simple_base_path (expr
, binfo
);
434 expr
= build_address (expr
);
435 target_type
= TREE_TYPE (expr
);
441 /* Going via virtual base V_BINFO. We need the static offset
442 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
443 V_BINFO. That offset is an entry in D_BINFO's vtable. */
446 if (fixed_type_p
< 0 && in_base_initializer
)
448 /* In a base member initializer, we cannot rely on the
449 vtable being set up. We have to indirect via the
453 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
454 t
= build_pointer_type (t
);
455 v_offset
= fold_convert (t
, current_vtt_parm
);
456 v_offset
= cp_build_indirect_ref (v_offset
, RO_NULL
, complain
);
461 if ((flag_sanitize
& SANITIZE_VPTR
) && fixed_type_p
== 0)
463 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
468 v_offset
= build_vfield_ref (cp_build_indirect_ref (t
, RO_NULL
,
470 TREE_TYPE (TREE_TYPE (expr
)));
473 if (v_offset
== error_mark_node
)
474 return error_mark_node
;
476 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
477 v_offset
= build1 (NOP_EXPR
,
478 build_pointer_type (ptrdiff_type_node
),
480 v_offset
= cp_build_indirect_ref (v_offset
, RO_NULL
, complain
);
481 TREE_CONSTANT (v_offset
) = 1;
483 offset
= convert_to_integer (ptrdiff_type_node
,
484 size_diffop_loc (input_location
, offset
,
485 BINFO_OFFSET (v_binfo
)));
487 if (!integer_zerop (offset
))
488 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
490 if (fixed_type_p
< 0)
491 /* Negative fixed_type_p means this is a constructor or destructor;
492 virtual base layout is fixed in in-charge [cd]tors, but not in
494 offset
= build_if_in_charge
495 (convert_to_integer (ptrdiff_type_node
, BINFO_OFFSET (binfo
)),
502 target_type
= ptr_target_type
;
504 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
506 if (!integer_zerop (offset
))
508 offset
= fold_convert (sizetype
, offset
);
509 if (code
== MINUS_EXPR
)
510 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
511 expr
= fold_build_pointer_plus (expr
, offset
);
519 expr
= cp_build_indirect_ref (expr
, RO_NULL
, complain
);
526 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
, expr
,
527 build_zero_cst (target_type
));
532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
533 Perform a derived-to-base conversion by recursively building up a
534 sequence of COMPONENT_REFs to the appropriate base fields. */
537 build_simple_base_path (tree expr
, tree binfo
)
539 tree type
= BINFO_TYPE (binfo
);
540 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
543 if (d_binfo
== NULL_TREE
)
547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
551 an lvalue in the front end; only _DECLs and _REFs are lvalues
553 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
555 expr
= cp_build_indirect_ref (temp
, RO_NULL
, tf_warning_or_error
);
561 expr
= build_simple_base_path (expr
, d_binfo
);
563 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
564 field
; field
= DECL_CHAIN (field
))
565 /* Is this the base field created by build_base_field? */
566 if (TREE_CODE (field
) == FIELD_DECL
567 && DECL_FIELD_IS_BASE (field
)
568 && TREE_TYPE (field
) == type
569 /* If we're looking for a field in the most-derived class,
570 also check the field offset; we can have two base fields
571 of the same type if one is an indirect virtual base and one
572 is a direct non-virtual base. */
573 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
574 || tree_int_cst_equal (byte_position (field
),
575 BINFO_OFFSET (binfo
))))
577 /* We don't use build_class_member_access_expr here, as that
578 has unnecessary checks, and more importantly results in
579 recursive calls to dfs_walk_once. */
580 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
582 expr
= build3 (COMPONENT_REF
,
583 cp_build_qualified_type (type
, type_quals
),
584 expr
, field
, NULL_TREE
);
585 /* Mark the expression const or volatile, as appropriate.
586 Even though we've dealt with the type above, we still have
587 to mark the expression itself. */
588 if (type_quals
& TYPE_QUAL_CONST
)
589 TREE_READONLY (expr
) = 1;
590 if (type_quals
& TYPE_QUAL_VOLATILE
)
591 TREE_THIS_VOLATILE (expr
) = 1;
596 /* Didn't find the base field?!? */
600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
601 type is a class type or a pointer to a class type. In the former
602 case, TYPE is also a class type; in the latter it is another
603 pointer type. If CHECK_ACCESS is true, an error message is emitted
604 if TYPE is inaccessible. If OBJECT has pointer type, the value is
605 assumed to be non-NULL. */
608 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
609 tsubst_flags_t complain
)
614 if (TYPE_PTR_P (TREE_TYPE (object
)))
616 object_type
= TREE_TYPE (TREE_TYPE (object
));
617 type
= TREE_TYPE (type
);
620 object_type
= TREE_TYPE (object
);
622 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
624 if (!binfo
|| binfo
== error_mark_node
)
625 return error_mark_node
;
627 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
630 /* EXPR is an expression with unqualified class type. BASE is a base
631 binfo of that class type. Returns EXPR, converted to the BASE
632 type. This function assumes that EXPR is the most derived class;
633 therefore virtual bases can be found at their static offsets. */
636 convert_to_base_statically (tree expr
, tree base
)
640 expr_type
= TREE_TYPE (expr
);
641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
643 /* If this is a non-empty base, use a COMPONENT_REF. */
644 if (!is_empty_class (BINFO_TYPE (base
)))
645 return build_simple_base_path (expr
, base
);
647 /* We use fold_build2 and fold_convert below to simplify the trees
648 provided to the optimizers. It is not safe to call these functions
649 when processing a template because they do not handle C++-specific
651 gcc_assert (!processing_template_decl
);
652 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
653 if (!integer_zerop (BINFO_OFFSET (base
)))
654 expr
= fold_build_pointer_plus_loc (input_location
,
655 expr
, BINFO_OFFSET (base
));
656 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
657 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
665 build_vfield_ref (tree datum
, tree type
)
667 tree vfield
, vcontext
;
669 if (datum
== error_mark_node
670 /* Can happen in case of duplicate base types (c++/59082). */
671 || !TYPE_VFIELD (type
))
672 return error_mark_node
;
674 /* First, convert to the requested type. */
675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
676 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
677 /*nonnull=*/true, tf_warning_or_error
);
679 /* Second, the requested type may not be the owner of its own vptr.
680 If not, convert to the base class that owns it. We cannot use
681 convert_to_base here, because VCONTEXT may appear more than once
682 in the inheritance hierarchy of TYPE, and thus direct conversion
683 between the types may be ambiguous. Following the path back up
684 one step at a time via primary bases avoids the problem. */
685 vfield
= TYPE_VFIELD (type
);
686 vcontext
= DECL_CONTEXT (vfield
);
687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
689 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
690 type
= TREE_TYPE (datum
);
693 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
696 /* Given an object INSTANCE, return an expression which yields the
697 vtable element corresponding to INDEX. There are many special
698 cases for INSTANCE which we take care of here, mainly to avoid
699 creating extra tree nodes when we don't have to. */
702 build_vtbl_ref_1 (tree instance
, tree idx
)
705 tree vtbl
= NULL_TREE
;
707 /* Try to figure out what a reference refers to, and
708 access its virtual function table directly. */
711 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
713 tree basetype
= non_reference (TREE_TYPE (instance
));
715 if (fixed_type
&& !cdtorp
)
717 tree binfo
= lookup_base (fixed_type
, basetype
,
718 ba_unique
, NULL
, tf_none
);
719 if (binfo
&& binfo
!= error_mark_node
)
720 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
724 vtbl
= build_vfield_ref (instance
, basetype
);
726 aref
= build_array_ref (input_location
, vtbl
, idx
);
727 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
733 build_vtbl_ref (tree instance
, tree idx
)
735 tree aref
= build_vtbl_ref_1 (instance
, idx
);
740 /* Given a stable object pointer INSTANCE_PTR, return an expression which
741 yields a function pointer corresponding to vtable element INDEX. */
744 build_vfn_ref (tree instance_ptr
, tree idx
)
748 aref
= build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr
, RO_NULL
,
749 tf_warning_or_error
),
752 /* When using function descriptors, the address of the
753 vtable entry is treated as a function pointer. */
754 if (TARGET_VTABLE_USES_DESCRIPTORS
)
755 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
756 cp_build_addr_expr (aref
, tf_warning_or_error
));
758 /* Remember this as a method reference, for later devirtualization. */
759 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
764 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
765 for the given TYPE. */
768 get_vtable_name (tree type
)
770 return mangle_vtbl_for_type (type
);
773 /* DECL is an entity associated with TYPE, like a virtual table or an
774 implicitly generated constructor. Determine whether or not DECL
775 should have external or internal linkage at the object file
776 level. This routine does not deal with COMDAT linkage and other
777 similar complexities; it simply sets TREE_PUBLIC if it possible for
778 entities in other translation units to contain copies of DECL, in
782 set_linkage_according_to_type (tree
/*type*/, tree decl
)
784 TREE_PUBLIC (decl
) = 1;
785 determine_visibility (decl
);
788 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
789 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
790 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
793 build_vtable (tree class_type
, tree name
, tree vtable_type
)
797 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
798 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
799 now to avoid confusion in mangle_decl. */
800 SET_DECL_ASSEMBLER_NAME (decl
, name
);
801 DECL_CONTEXT (decl
) = class_type
;
802 DECL_ARTIFICIAL (decl
) = 1;
803 TREE_STATIC (decl
) = 1;
804 TREE_READONLY (decl
) = 1;
805 DECL_VIRTUAL_P (decl
) = 1;
806 SET_DECL_ALIGN (decl
, TARGET_VTABLE_ENTRY_ALIGN
);
807 DECL_USER_ALIGN (decl
) = true;
808 DECL_VTABLE_OR_VTT_P (decl
) = 1;
809 set_linkage_according_to_type (class_type
, decl
);
810 /* The vtable has not been defined -- yet. */
811 DECL_EXTERNAL (decl
) = 1;
812 DECL_NOT_REALLY_EXTERN (decl
) = 1;
814 /* Mark the VAR_DECL node representing the vtable itself as a
815 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
816 is rather important that such things be ignored because any
817 effort to actually generate DWARF for them will run into
818 trouble when/if we encounter code like:
821 struct S { virtual void member (); };
823 because the artificial declaration of the vtable itself (as
824 manufactured by the g++ front end) will say that the vtable is
825 a static member of `S' but only *after* the debug output for
826 the definition of `S' has already been output. This causes
827 grief because the DWARF entry for the definition of the vtable
828 will try to refer back to an earlier *declaration* of the
829 vtable as a static member of `S' and there won't be one. We
830 might be able to arrange to have the "vtable static member"
831 attached to the member list for `S' before the debug info for
832 `S' get written (which would solve the problem) but that would
833 require more intrusive changes to the g++ front end. */
834 DECL_IGNORED_P (decl
) = 1;
839 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
840 or even complete. If this does not exist, create it. If COMPLETE is
841 nonzero, then complete the definition of it -- that will render it
842 impossible to actually build the vtable, but is useful to get at those
843 which are known to exist in the runtime. */
846 get_vtable_decl (tree type
, int complete
)
850 if (CLASSTYPE_VTABLES (type
))
851 return CLASSTYPE_VTABLES (type
);
853 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
854 CLASSTYPE_VTABLES (type
) = decl
;
858 DECL_EXTERNAL (decl
) = 1;
859 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
865 /* Build the primary virtual function table for TYPE. If BINFO is
866 non-NULL, build the vtable starting with the initial approximation
867 that it is the same as the one which is the head of the association
868 list. Returns a nonzero value if a new vtable is actually
872 build_primary_vtable (tree binfo
, tree type
)
877 decl
= get_vtable_decl (type
, /*complete=*/0);
881 if (BINFO_NEW_VTABLE_MARKED (binfo
))
882 /* We have already created a vtable for this base, so there's
883 no need to do it again. */
886 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
887 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
888 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
889 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
893 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
894 virtuals
= NULL_TREE
;
897 if (GATHER_STATISTICS
)
900 n_vtable_elems
+= list_length (virtuals
);
903 /* Initialize the association list for this type, based
904 on our first approximation. */
905 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
906 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
907 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
911 /* Give BINFO a new virtual function table which is initialized
912 with a skeleton-copy of its original initialization. The only
913 entry that changes is the `delta' entry, so we can really
914 share a lot of structure.
916 FOR_TYPE is the most derived type which caused this table to
919 Returns nonzero if we haven't met BINFO before.
921 The order in which vtables are built (by calling this function) for
922 an object must remain the same, otherwise a binary incompatibility
926 build_secondary_vtable (tree binfo
)
928 if (BINFO_NEW_VTABLE_MARKED (binfo
))
929 /* We already created a vtable for this base. There's no need to
933 /* Remember that we've created a vtable for this BINFO, so that we
934 don't try to do so again. */
935 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
937 /* Make fresh virtual list, so we can smash it later. */
938 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
940 /* Secondary vtables are laid out as part of the same structure as
941 the primary vtable. */
942 BINFO_VTABLE (binfo
) = NULL_TREE
;
946 /* Create a new vtable for BINFO which is the hierarchy dominated by
947 T. Return nonzero if we actually created a new vtable. */
950 make_new_vtable (tree t
, tree binfo
)
952 if (binfo
== TYPE_BINFO (t
))
953 /* In this case, it is *type*'s vtable we are modifying. We start
954 with the approximation that its vtable is that of the
955 immediate base class. */
956 return build_primary_vtable (binfo
, t
);
958 /* This is our very own copy of `basetype' to play with. Later,
959 we will fill in all the virtual functions that override the
960 virtual functions in these base classes which are not defined
961 by the current type. */
962 return build_secondary_vtable (binfo
);
965 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
966 (which is in the hierarchy dominated by T) list FNDECL as its
967 BV_FN. DELTA is the required constant adjustment from the `this'
968 pointer where the vtable entry appears to the `this' required when
969 the function is actually called. */
972 modify_vtable_entry (tree t
,
982 if (fndecl
!= BV_FN (v
)
983 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
985 /* We need a new vtable for BINFO. */
986 if (make_new_vtable (t
, binfo
))
988 /* If we really did make a new vtable, we also made a copy
989 of the BINFO_VIRTUALS list. Now, we have to find the
990 corresponding entry in that list. */
991 *virtuals
= BINFO_VIRTUALS (binfo
);
992 while (BV_FN (*virtuals
) != BV_FN (v
))
993 *virtuals
= TREE_CHAIN (*virtuals
);
997 BV_DELTA (v
) = delta
;
998 BV_VCALL_INDEX (v
) = NULL_TREE
;
1004 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1005 the USING_DECL naming METHOD. Returns true if the method could be
1006 added to the method vec. */
1009 add_method (tree type
, tree method
, tree using_decl
)
1013 bool template_conv_p
= false;
1015 vec
<tree
, va_gc
> *method_vec
;
1017 bool insert_p
= false;
1020 if (method
== error_mark_node
)
1023 complete_p
= COMPLETE_TYPE_P (type
);
1024 conv_p
= DECL_CONV_FN_P (method
);
1026 template_conv_p
= (TREE_CODE (method
) == TEMPLATE_DECL
1027 && DECL_TEMPLATE_CONV_FN_P (method
));
1029 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1032 /* Make a new method vector. We start with 8 entries. We must
1033 allocate at least two (for constructors and destructors), and
1034 we're going to end up with an assignment operator at some
1036 vec_alloc (method_vec
, 8);
1037 /* Create slots for constructors and destructors. */
1038 method_vec
->quick_push (NULL_TREE
);
1039 method_vec
->quick_push (NULL_TREE
);
1040 CLASSTYPE_METHOD_VEC (type
) = method_vec
;
1043 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1044 grok_special_member_properties (method
);
1046 /* Constructors and destructors go in special slots. */
1047 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method
))
1048 slot
= CLASSTYPE_CONSTRUCTOR_SLOT
;
1049 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
1050 slot
= CLASSTYPE_DESTRUCTOR_SLOT
;
1056 /* See if we already have an entry with this name. */
1057 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1058 vec_safe_iterate (method_vec
, slot
, &m
);
1061 m
= OVL_CURRENT (m
);
1062 if (template_conv_p
)
1064 if (TREE_CODE (m
) == TEMPLATE_DECL
1065 && DECL_TEMPLATE_CONV_FN_P (m
))
1069 if (conv_p
&& !DECL_CONV_FN_P (m
))
1071 if (DECL_NAME (m
) == DECL_NAME (method
))
1077 && !DECL_CONV_FN_P (m
)
1078 && DECL_NAME (m
) > DECL_NAME (method
))
1082 current_fns
= insert_p
? NULL_TREE
: (*method_vec
)[slot
];
1084 /* Check to see if we've already got this method. */
1085 for (tree
*p
= ¤t_fns
; *p
; )
1088 tree fn
= OVL_CURRENT (fns
);
1094 if (TREE_CODE (fn
) != TREE_CODE (method
))
1097 /* Two using-declarations can coexist, we'll complain about ambiguity in
1098 overload resolution. */
1099 if (using_decl
&& TREE_CODE (fns
) == OVERLOAD
&& OVL_USED (fns
)
1100 /* Except handle inherited constructors specially. */
1101 && ! DECL_CONSTRUCTOR_P (fn
))
1104 /* [over.load] Member function declarations with the
1105 same name and the same parameter types cannot be
1106 overloaded if any of them is a static member
1107 function declaration.
1109 [over.load] Member function declarations with the same name and
1110 the same parameter-type-list as well as member function template
1111 declarations with the same name, the same parameter-type-list, and
1112 the same template parameter lists cannot be overloaded if any of
1113 them, but not all, have a ref-qualifier.
1115 [namespace.udecl] When a using-declaration brings names
1116 from a base class into a derived class scope, member
1117 functions in the derived class override and/or hide member
1118 functions with the same name and parameter types in a base
1119 class (rather than conflicting). */
1120 fn_type
= TREE_TYPE (fn
);
1121 method_type
= TREE_TYPE (method
);
1122 parms1
= TYPE_ARG_TYPES (fn_type
);
1123 parms2
= TYPE_ARG_TYPES (method_type
);
1125 /* Compare the quals on the 'this' parm. Don't compare
1126 the whole types, as used functions are treated as
1127 coming from the using class in overload resolution. */
1128 if (! DECL_STATIC_FUNCTION_P (fn
)
1129 && ! DECL_STATIC_FUNCTION_P (method
)
1130 /* Either both or neither need to be ref-qualified for
1131 differing quals to allow overloading. */
1132 && (FUNCTION_REF_QUALIFIED (fn_type
)
1133 == FUNCTION_REF_QUALIFIED (method_type
))
1134 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1135 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1138 /* For templates, the return type and template parameters
1139 must be identical. */
1140 if (TREE_CODE (fn
) == TEMPLATE_DECL
1141 && (!same_type_p (TREE_TYPE (fn_type
),
1142 TREE_TYPE (method_type
))
1143 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1144 DECL_TEMPLATE_PARMS (method
))))
1147 if (! DECL_STATIC_FUNCTION_P (fn
))
1148 parms1
= TREE_CHAIN (parms1
);
1149 if (! DECL_STATIC_FUNCTION_P (method
))
1150 parms2
= TREE_CHAIN (parms2
);
1152 if (compparms (parms1
, parms2
)
1153 && (!DECL_CONV_FN_P (fn
)
1154 || same_type_p (TREE_TYPE (fn_type
),
1155 TREE_TYPE (method_type
)))
1156 && equivalently_constrained (fn
, method
))
1158 /* For function versions, their parms and types match
1159 but they are not duplicates. Record function versions
1160 as and when they are found. extern "C" functions are
1161 not treated as versions. */
1162 if (TREE_CODE (fn
) == FUNCTION_DECL
1163 && TREE_CODE (method
) == FUNCTION_DECL
1164 && !DECL_EXTERN_C_P (fn
)
1165 && !DECL_EXTERN_C_P (method
)
1166 && targetm
.target_option
.function_versions (fn
, method
))
1168 /* Mark functions as versions if necessary. Modify the mangled
1169 decl name if necessary. */
1170 if (!DECL_FUNCTION_VERSIONED (fn
))
1172 DECL_FUNCTION_VERSIONED (fn
) = 1;
1173 if (DECL_ASSEMBLER_NAME_SET_P (fn
))
1176 if (!DECL_FUNCTION_VERSIONED (method
))
1178 DECL_FUNCTION_VERSIONED (method
) = 1;
1179 if (DECL_ASSEMBLER_NAME_SET_P (method
))
1180 mangle_decl (method
);
1182 cgraph_node::record_function_versions (fn
, method
);
1185 if (DECL_INHERITED_CTOR (method
))
1187 if (DECL_INHERITED_CTOR (fn
))
1189 tree basem
= DECL_INHERITED_CTOR_BASE (method
);
1190 tree basef
= DECL_INHERITED_CTOR_BASE (fn
);
1191 if (flag_new_inheriting_ctors
)
1195 /* Inheriting the same constructor along different
1196 paths, combine them. */
1197 SET_DECL_INHERITED_CTOR
1198 (fn
, ovl_cons (DECL_INHERITED_CTOR (method
),
1199 DECL_INHERITED_CTOR (fn
)));
1200 /* Adjust deletedness and such. */
1201 deduce_inheriting_ctor (fn
);
1202 /* And discard the new one. */
1206 /* Inherited ctors can coexist until overload
1210 error_at (DECL_SOURCE_LOCATION (method
),
1212 error_at (DECL_SOURCE_LOCATION (fn
),
1213 "conflicts with version inherited from %qT",
1216 /* Otherwise defer to the other function. */
1221 if (DECL_CONTEXT (fn
) == type
)
1222 /* Defer to the local function. */
1225 else if (flag_new_inheriting_ctors
1226 && DECL_INHERITED_CTOR (fn
))
1228 /* Hide the inherited constructor. */
1229 *p
= OVL_NEXT (fns
);
1234 error ("%q+#D cannot be overloaded", method
);
1235 error ("with %q+#D", fn
);
1238 /* We don't call duplicate_decls here to merge the
1239 declarations because that will confuse things if the
1240 methods have inline definitions. In particular, we
1241 will crash while processing the definitions. */
1246 if (TREE_CODE (fns
) == OVERLOAD
)
1247 p
= &OVL_CHAIN (fns
);
1252 /* A class should never have more than one destructor. */
1253 if (current_fns
&& DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
1256 /* Add the new binding. */
1259 overload
= ovl_cons (method
, current_fns
);
1260 OVL_USED (overload
) = true;
1263 overload
= build_overload (method
, current_fns
);
1266 TYPE_HAS_CONVERSION (type
) = 1;
1267 else if (slot
>= CLASSTYPE_FIRST_CONVERSION_SLOT
&& !complete_p
)
1268 push_class_level_binding (DECL_NAME (method
), overload
);
1274 /* We only expect to add few methods in the COMPLETE_P case, so
1275 just make room for one more method in that case. */
1277 reallocated
= vec_safe_reserve_exact (method_vec
, 1);
1279 reallocated
= vec_safe_reserve (method_vec
, 1);
1281 CLASSTYPE_METHOD_VEC (type
) = method_vec
;
1282 if (slot
== method_vec
->length ())
1283 method_vec
->quick_push (overload
);
1285 method_vec
->quick_insert (slot
, overload
);
1288 /* Replace the current slot. */
1289 (*method_vec
)[slot
] = overload
;
1293 /* Subroutines of finish_struct. */
1295 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1296 legit, otherwise return 0. */
1299 alter_access (tree t
, tree fdecl
, tree access
)
1303 if (!DECL_LANG_SPECIFIC (fdecl
))
1304 retrofit_lang_decl (fdecl
);
1306 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1308 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1311 if (TREE_VALUE (elem
) != access
)
1313 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1314 error ("conflicting access specifications for method"
1315 " %q+D, ignored", TREE_TYPE (fdecl
));
1317 error ("conflicting access specifications for field %qE, ignored",
1322 /* They're changing the access to the same thing they changed
1323 it to before. That's OK. */
1329 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1330 tf_warning_or_error
);
1331 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1337 /* Return the access node for DECL's access in its enclosing class. */
1340 declared_access (tree decl
)
1342 return (TREE_PRIVATE (decl
) ? access_private_node
1343 : TREE_PROTECTED (decl
) ? access_protected_node
1344 : access_public_node
);
1347 /* Process the USING_DECL, which is a member of T. */
1350 handle_using_decl (tree using_decl
, tree t
)
1352 tree decl
= USING_DECL_DECLS (using_decl
);
1353 tree name
= DECL_NAME (using_decl
);
1354 tree access
= declared_access (using_decl
);
1355 tree flist
= NULL_TREE
;
1358 gcc_assert (!processing_template_decl
&& decl
);
1360 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1361 tf_warning_or_error
);
1364 if (is_overloaded_fn (old_value
))
1365 old_value
= OVL_CURRENT (old_value
);
1367 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1370 old_value
= NULL_TREE
;
1373 cp_emit_debug_info_for_using (decl
, t
);
1375 if (is_overloaded_fn (decl
))
1380 else if (is_overloaded_fn (old_value
))
1383 /* It's OK to use functions from a base when there are functions with
1384 the same name already present in the current class. */;
1387 error ("%q+D invalid in %q#T", using_decl
, t
);
1388 error (" because of local method %q+#D with same name",
1389 OVL_CURRENT (old_value
));
1393 else if (!DECL_ARTIFICIAL (old_value
))
1395 error ("%q+D invalid in %q#T", using_decl
, t
);
1396 error (" because of local member %q+#D with same name", old_value
);
1400 /* Make type T see field decl FDECL with access ACCESS. */
1402 for (; flist
; flist
= OVL_NEXT (flist
))
1404 add_method (t
, OVL_CURRENT (flist
), using_decl
);
1405 alter_access (t
, OVL_CURRENT (flist
), access
);
1408 alter_access (t
, decl
, access
);
1411 /* Data structure for find_abi_tags_r, below. */
1415 tree t
; // The type that we're checking for missing tags.
1416 tree subob
; // The subobject of T that we're getting tags from.
1417 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1420 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1421 in the context of P. TAG can be either an identifier (the DECL_NAME of
1422 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1425 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1427 if (!IDENTIFIER_MARKED (id
))
1429 if (p
->tags
!= error_mark_node
)
1431 /* We're collecting tags from template arguments or from
1432 the type of a variable or function return type. */
1433 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1435 /* Don't inherit this tag multiple times. */
1436 IDENTIFIER_MARKED (id
) = true;
1440 /* Tags inherited from type template arguments are only used
1441 to avoid warnings. */
1442 ABI_TAG_IMPLICIT (p
->tags
) = true;
1445 /* For functions and variables we want to warn, too. */
1448 /* Otherwise we're diagnosing missing tags. */
1449 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1451 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1452 "that %qT (used in its return type) has",
1454 inform (location_of (*tp
), "%qT declared here", *tp
);
1456 else if (VAR_P (p
->t
))
1458 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1459 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1460 inform (location_of (*tp
), "%qT declared here", *tp
);
1462 else if (TYPE_P (p
->subob
))
1464 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1465 "that base %qT has", p
->t
, tag
, p
->subob
))
1466 inform (location_of (p
->subob
), "%qT declared here",
1471 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1472 "that %qT (used in the type of %qD) has",
1473 p
->t
, tag
, *tp
, p
->subob
))
1475 inform (location_of (p
->subob
), "%qD declared here",
1477 inform (location_of (*tp
), "%qT declared here", *tp
);
1483 /* Find all the ABI tags in the attribute list ATTR and either call
1484 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1487 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1491 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1492 attr
= TREE_CHAIN (attr
))
1493 for (tree list
= TREE_VALUE (attr
); list
;
1494 list
= TREE_CHAIN (list
))
1496 tree tag
= TREE_VALUE (list
);
1497 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1499 check_tag (tag
, id
, tp
, p
);
1501 IDENTIFIER_MARKED (id
) = val
;
1505 /* Find all the ABI tags on T and its enclosing scopes and either call
1506 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1509 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1511 while (t
!= global_namespace
)
1516 attr
= TYPE_ATTRIBUTES (t
);
1517 t
= CP_TYPE_CONTEXT (t
);
1521 attr
= DECL_ATTRIBUTES (t
);
1522 t
= CP_DECL_CONTEXT (t
);
1524 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1528 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1529 types with ABI tags, add the corresponding identifiers to the VEC in
1530 *DATA and set IDENTIFIER_MARKED. */
1533 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1535 if (!OVERLOAD_TYPE_P (*tp
))
1538 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1539 anyway, but let's make sure of it. */
1540 *walk_subtrees
= false;
1542 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1544 mark_or_check_tags (*tp
, tp
, p
, false);
1549 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1550 IDENTIFIER_MARKED on its ABI tags. */
1553 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1555 if (!OVERLOAD_TYPE_P (*tp
))
1558 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1559 anyway, but let's make sure of it. */
1560 *walk_subtrees
= false;
1562 bool *valp
= static_cast<bool*>(data
);
1564 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1569 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1573 mark_abi_tags (tree t
, bool val
)
1575 mark_or_check_tags (t
, NULL
, NULL
, val
);
1578 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1579 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1581 /* Template arguments are part of the signature. */
1582 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1583 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1585 tree arg
= TREE_VEC_ELT (level
, j
);
1586 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1589 if (TREE_CODE (t
) == FUNCTION_DECL
)
1590 /* A function's parameter types are part of the signature, so
1591 we don't need to inherit any tags that are also in them. */
1592 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1593 arg
= TREE_CHAIN (arg
))
1594 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1595 mark_abi_tags_r
, &val
);
1599 /* Check that T has all the ABI tags that subobject SUBOB has, or
1600 warn if not. If T is a (variable or function) declaration, also
1601 return any missing tags, and add them to T if JUST_CHECKING is false. */
1604 check_abi_tags (tree t
, tree subob
, bool just_checking
= false)
1606 bool inherit
= DECL_P (t
);
1608 if (!inherit
&& !warn_abi_tag
)
1611 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1612 if (!TREE_PUBLIC (decl
))
1613 /* No need to worry about things local to this TU. */
1616 mark_abi_tags (t
, true);
1618 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1619 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1621 data
.tags
= NULL_TREE
;
1623 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1625 if (!(inherit
&& data
.tags
))
1626 /* We don't need to do anything with data.tags. */;
1627 else if (just_checking
)
1628 for (tree t
= data
.tags
; t
; t
= TREE_CHAIN (t
))
1630 tree id
= get_identifier (TREE_STRING_POINTER (TREE_VALUE (t
)));
1631 IDENTIFIER_MARKED (id
) = false;
1635 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1637 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1640 = tree_cons (get_identifier ("abi_tag"), data
.tags
,
1641 DECL_ATTRIBUTES (t
));
1644 mark_abi_tags (t
, false);
1649 /* Check that DECL has all the ABI tags that are used in parts of its type
1650 that are not reflected in its mangled name. */
1653 check_abi_tags (tree decl
)
1656 check_abi_tags (decl
, TREE_TYPE (decl
));
1657 else if (TREE_CODE (decl
) == FUNCTION_DECL
1658 && !DECL_CONV_FN_P (decl
)
1659 && !mangle_return_type_p (decl
))
1660 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1663 /* Return any ABI tags that are used in parts of the type of DECL
1664 that are not reflected in its mangled name. This function is only
1665 used in backward-compatible mangling for ABI <11. */
1668 missing_abi_tags (tree decl
)
1671 return check_abi_tags (decl
, TREE_TYPE (decl
), true);
1672 else if (TREE_CODE (decl
) == FUNCTION_DECL
1673 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1674 that we can use this function for setting need_abi_warning
1675 regardless of the current flag_abi_version. */
1676 && !mangle_return_type_p (decl
))
1677 return check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)), true);
1683 inherit_targ_abi_tags (tree t
)
1685 if (!CLASS_TYPE_P (t
)
1686 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1689 mark_abi_tags (t
, true);
1691 tree args
= CLASSTYPE_TI_ARGS (t
);
1692 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1693 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1695 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1696 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1698 tree arg
= TREE_VEC_ELT (level
, j
);
1700 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1704 // If we found some tags on our template arguments, add them to our
1705 // abi_tag attribute.
1708 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1710 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1713 = tree_cons (get_identifier ("abi_tag"), data
.tags
,
1714 TYPE_ATTRIBUTES (t
));
1717 mark_abi_tags (t
, false);
1720 /* Return true, iff class T has a non-virtual destructor that is
1721 accessible from outside the class heirarchy (i.e. is public, or
1722 there's a suitable friend. */
1725 accessible_nvdtor_p (tree t
)
1727 tree dtor
= CLASSTYPE_DESTRUCTORS (t
);
1729 /* An implicitly declared destructor is always public. And,
1730 if it were virtual, we would have created it by now. */
1734 if (DECL_VINDEX (dtor
))
1735 return false; /* Virtual */
1737 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1738 return true; /* Public */
1740 if (CLASSTYPE_FRIEND_CLASSES (t
)
1741 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1742 return true; /* Has friends */
1747 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1748 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1749 properties of the bases. */
1752 check_bases (tree t
,
1753 int* cant_have_const_ctor_p
,
1754 int* no_const_asn_ref_p
)
1757 bool seen_non_virtual_nearly_empty_base_p
= 0;
1758 int seen_tm_mask
= 0;
1761 tree field
= NULL_TREE
;
1763 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1764 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1765 if (TREE_CODE (field
) == FIELD_DECL
)
1768 for (binfo
= TYPE_BINFO (t
), i
= 0;
1769 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1771 tree basetype
= TREE_TYPE (base_binfo
);
1773 gcc_assert (COMPLETE_TYPE_P (basetype
));
1775 if (CLASSTYPE_FINAL (basetype
))
1776 error ("cannot derive from %<final%> base %qT in derived type %qT",
1779 /* If any base class is non-literal, so is the derived class. */
1780 if (!CLASSTYPE_LITERAL_P (basetype
))
1781 CLASSTYPE_LITERAL_P (t
) = false;
1783 /* If the base class doesn't have copy constructors or
1784 assignment operators that take const references, then the
1785 derived class cannot have such a member automatically
1787 if (TYPE_HAS_COPY_CTOR (basetype
)
1788 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1789 *cant_have_const_ctor_p
= 1;
1790 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1791 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1792 *no_const_asn_ref_p
= 1;
1794 if (BINFO_VIRTUAL_P (base_binfo
))
1795 /* A virtual base does not effect nearly emptiness. */
1797 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1799 if (seen_non_virtual_nearly_empty_base_p
)
1800 /* And if there is more than one nearly empty base, then the
1801 derived class is not nearly empty either. */
1802 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1804 /* Remember we've seen one. */
1805 seen_non_virtual_nearly_empty_base_p
= 1;
1807 else if (!is_empty_class (basetype
))
1808 /* If the base class is not empty or nearly empty, then this
1809 class cannot be nearly empty. */
1810 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1812 /* A lot of properties from the bases also apply to the derived
1814 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1815 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1816 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1817 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1818 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1819 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1820 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1821 || !TYPE_HAS_COPY_CTOR (basetype
));
1822 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1823 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1824 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1825 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1826 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1827 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1828 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1829 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1830 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1831 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1832 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1833 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1834 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1835 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1836 if (TYPE_HAS_MUTABLE_P (basetype
))
1837 CLASSTYPE_HAS_MUTABLE (t
) = 1;
1839 /* A standard-layout class is a class that:
1841 * has no non-standard-layout base classes, */
1842 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1843 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1846 /* ...has no base classes of the same type as the first non-static
1848 if (field
&& DECL_CONTEXT (field
) == t
1849 && (same_type_ignoring_top_level_qualifiers_p
1850 (TREE_TYPE (field
), basetype
)))
1851 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1853 /* ...either has no non-static data members in the most-derived
1854 class and at most one base class with non-static data
1855 members, or has no base classes with non-static data
1857 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1858 basefield
= DECL_CHAIN (basefield
))
1859 if (TREE_CODE (basefield
) == FIELD_DECL
)
1862 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1869 /* Don't bother collecting tm attributes if transactional memory
1870 support is not enabled. */
1873 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1875 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1878 check_abi_tags (t
, basetype
);
1881 /* If one of the base classes had TM attributes, and the current class
1882 doesn't define its own, then the current class inherits one. */
1883 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1885 tree tm_attr
= tm_mask_to_attr (least_bit_hwi (seen_tm_mask
));
1886 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1890 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1891 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1892 that have had a nearly-empty virtual primary base stolen by some
1893 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1897 determine_primary_bases (tree t
)
1900 tree primary
= NULL_TREE
;
1901 tree type_binfo
= TYPE_BINFO (t
);
1904 /* Determine the primary bases of our bases. */
1905 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1906 base_binfo
= TREE_CHAIN (base_binfo
))
1908 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1910 /* See if we're the non-virtual primary of our inheritance
1912 if (!BINFO_VIRTUAL_P (base_binfo
))
1914 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1915 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1918 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1919 BINFO_TYPE (parent_primary
)))
1920 /* We are the primary binfo. */
1921 BINFO_PRIMARY_P (base_binfo
) = 1;
1923 /* Determine if we have a virtual primary base, and mark it so.
1925 if (primary
&& BINFO_VIRTUAL_P (primary
))
1927 tree this_primary
= copied_binfo (primary
, base_binfo
);
1929 if (BINFO_PRIMARY_P (this_primary
))
1930 /* Someone already claimed this base. */
1931 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1936 BINFO_PRIMARY_P (this_primary
) = 1;
1937 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1939 /* A virtual binfo might have been copied from within
1940 another hierarchy. As we're about to use it as a
1941 primary base, make sure the offsets match. */
1942 delta
= size_diffop_loc (input_location
,
1943 fold_convert (ssizetype
,
1944 BINFO_OFFSET (base_binfo
)),
1945 fold_convert (ssizetype
,
1946 BINFO_OFFSET (this_primary
)));
1948 propagate_binfo_offsets (this_primary
, delta
);
1953 /* First look for a dynamic direct non-virtual base. */
1954 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1956 tree basetype
= BINFO_TYPE (base_binfo
);
1958 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1960 primary
= base_binfo
;
1965 /* A "nearly-empty" virtual base class can be the primary base
1966 class, if no non-virtual polymorphic base can be found. Look for
1967 a nearly-empty virtual dynamic base that is not already a primary
1968 base of something in the hierarchy. If there is no such base,
1969 just pick the first nearly-empty virtual base. */
1971 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1972 base_binfo
= TREE_CHAIN (base_binfo
))
1973 if (BINFO_VIRTUAL_P (base_binfo
)
1974 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1976 if (!BINFO_PRIMARY_P (base_binfo
))
1978 /* Found one that is not primary. */
1979 primary
= base_binfo
;
1983 /* Remember the first candidate. */
1984 primary
= base_binfo
;
1988 /* If we've got a primary base, use it. */
1991 tree basetype
= BINFO_TYPE (primary
);
1993 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1994 if (BINFO_PRIMARY_P (primary
))
1995 /* We are stealing a primary base. */
1996 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1997 BINFO_PRIMARY_P (primary
) = 1;
1998 if (BINFO_VIRTUAL_P (primary
))
2002 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
2003 /* A virtual binfo might have been copied from within
2004 another hierarchy. As we're about to use it as a primary
2005 base, make sure the offsets match. */
2006 delta
= size_diffop_loc (input_location
, ssize_int (0),
2007 fold_convert (ssizetype
, BINFO_OFFSET (primary
)));
2009 propagate_binfo_offsets (primary
, delta
);
2012 primary
= TYPE_BINFO (basetype
);
2014 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
2015 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
2016 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
2020 /* Update the variant types of T. */
2023 fixup_type_variants (tree t
)
2030 for (variants
= TYPE_NEXT_VARIANT (t
);
2032 variants
= TYPE_NEXT_VARIANT (variants
))
2034 /* These fields are in the _TYPE part of the node, not in
2035 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2036 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
2037 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
2038 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
2039 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
2041 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
2043 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
2045 /* Copy whatever these are holding today. */
2046 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
2047 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
2051 /* KLASS is a class that we're applying may_alias to after the body is
2052 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2053 canonical type(s) will be implicitly updated. */
2056 fixup_may_alias (tree klass
)
2060 for (t
= TYPE_POINTER_TO (klass
); t
; t
= TYPE_NEXT_PTR_TO (t
))
2061 TYPE_REF_CAN_ALIAS_ALL (t
) = true;
2062 for (t
= TYPE_REFERENCE_TO (klass
); t
; t
= TYPE_NEXT_REF_TO (t
))
2063 TYPE_REF_CAN_ALIAS_ALL (t
) = true;
2066 /* Early variant fixups: we apply attributes at the beginning of the class
2067 definition, and we need to fix up any variants that have already been
2068 made via elaborated-type-specifier so that check_qualified_type works. */
2071 fixup_attribute_variants (tree t
)
2078 tree attrs
= TYPE_ATTRIBUTES (t
);
2079 unsigned align
= TYPE_ALIGN (t
);
2080 bool user_align
= TYPE_USER_ALIGN (t
);
2081 bool may_alias
= lookup_attribute ("may_alias", attrs
);
2084 fixup_may_alias (t
);
2086 for (variants
= TYPE_NEXT_VARIANT (t
);
2088 variants
= TYPE_NEXT_VARIANT (variants
))
2090 /* These are the two fields that check_qualified_type looks at and
2091 are affected by attributes. */
2092 TYPE_ATTRIBUTES (variants
) = attrs
;
2093 unsigned valign
= align
;
2094 if (TYPE_USER_ALIGN (variants
))
2095 valign
= MAX (valign
, TYPE_ALIGN (variants
));
2097 TYPE_USER_ALIGN (variants
) = user_align
;
2098 SET_TYPE_ALIGN (variants
, valign
);
2100 fixup_may_alias (variants
);
2104 /* Set memoizing fields and bits of T (and its variants) for later
2108 finish_struct_bits (tree t
)
2110 /* Fix up variants (if any). */
2111 fixup_type_variants (t
);
2113 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
2114 /* For a class w/o baseclasses, 'finish_struct' has set
2115 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2116 Similarly for a class whose base classes do not have vtables.
2117 When neither of these is true, we might have removed abstract
2118 virtuals (by providing a definition), added some (by declaring
2119 new ones), or redeclared ones from a base class. We need to
2120 recalculate what's really an abstract virtual at this point (by
2121 looking in the vtables). */
2122 get_pure_virtuals (t
);
2124 /* If this type has a copy constructor or a destructor, force its
2125 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2126 nonzero. This will cause it to be passed by invisible reference
2127 and prevent it from being returned in a register. */
2128 if (type_has_nontrivial_copy_init (t
)
2129 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2132 SET_DECL_MODE (TYPE_MAIN_DECL (t
), BLKmode
);
2133 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2135 SET_TYPE_MODE (variants
, BLKmode
);
2136 TREE_ADDRESSABLE (variants
) = 1;
2141 /* Issue warnings about T having private constructors, but no friends,
2144 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2145 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2146 non-private static member functions. */
2149 maybe_warn_about_overly_private_class (tree t
)
2151 int has_member_fn
= 0;
2152 int has_nonprivate_method
= 0;
2155 if (!warn_ctor_dtor_privacy
2156 /* If the class has friends, those entities might create and
2157 access instances, so we should not warn. */
2158 || (CLASSTYPE_FRIEND_CLASSES (t
)
2159 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2160 /* We will have warned when the template was declared; there's
2161 no need to warn on every instantiation. */
2162 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2163 /* There's no reason to even consider warning about this
2167 /* We only issue one warning, if more than one applies, because
2168 otherwise, on code like:
2171 // Oops - forgot `public:'
2177 we warn several times about essentially the same problem. */
2179 /* Check to see if all (non-constructor, non-destructor) member
2180 functions are private. (Since there are no friends or
2181 non-private statics, we can't ever call any of the private member
2183 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
2184 /* We're not interested in compiler-generated methods; they don't
2185 provide any way to call private members. */
2186 if (!DECL_ARTIFICIAL (fn
))
2188 if (!TREE_PRIVATE (fn
))
2190 if (DECL_STATIC_FUNCTION_P (fn
))
2191 /* A non-private static member function is just like a
2192 friend; it can create and invoke private member
2193 functions, and be accessed without a class
2197 has_nonprivate_method
= 1;
2198 /* Keep searching for a static member function. */
2200 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2204 if (!has_nonprivate_method
&& has_member_fn
)
2206 /* There are no non-private methods, and there's at least one
2207 private member function that isn't a constructor or
2208 destructor. (If all the private members are
2209 constructors/destructors we want to use the code below that
2210 issues error messages specifically referring to
2211 constructors/destructors.) */
2213 tree binfo
= TYPE_BINFO (t
);
2215 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2216 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2218 has_nonprivate_method
= 1;
2221 if (!has_nonprivate_method
)
2223 warning (OPT_Wctor_dtor_privacy
,
2224 "all member functions in class %qT are private", t
);
2229 /* Even if some of the member functions are non-private, the class
2230 won't be useful for much if all the constructors or destructors
2231 are private: such an object can never be created or destroyed. */
2232 fn
= CLASSTYPE_DESTRUCTORS (t
);
2233 if (fn
&& TREE_PRIVATE (fn
))
2235 warning (OPT_Wctor_dtor_privacy
,
2236 "%q#T only defines a private destructor and has no friends",
2241 /* Warn about classes that have private constructors and no friends. */
2242 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2243 /* Implicitly generated constructors are always public. */
2244 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
2245 || !CLASSTYPE_LAZY_COPY_CTOR (t
)))
2247 int nonprivate_ctor
= 0;
2249 /* If a non-template class does not define a copy
2250 constructor, one is defined for it, enabling it to avoid
2251 this warning. For a template class, this does not
2252 happen, and so we would normally get a warning on:
2254 template <class T> class C { private: C(); };
2256 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2257 complete non-template or fully instantiated classes have this
2259 if (!TYPE_HAS_COPY_CTOR (t
))
2260 nonprivate_ctor
= 1;
2262 for (fn
= CLASSTYPE_CONSTRUCTORS (t
); fn
; fn
= OVL_NEXT (fn
))
2264 tree ctor
= OVL_CURRENT (fn
);
2265 /* Ideally, we wouldn't count copy constructors (or, in
2266 fact, any constructor that takes an argument of the
2267 class type as a parameter) because such things cannot
2268 be used to construct an instance of the class unless
2269 you already have one. But, for now at least, we're
2271 if (! TREE_PRIVATE (ctor
))
2273 nonprivate_ctor
= 1;
2278 if (nonprivate_ctor
== 0)
2280 warning (OPT_Wctor_dtor_privacy
,
2281 "%q#T only defines private constructors and has no friends",
2289 gt_pointer_operator new_value
;
2293 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2296 method_name_cmp (const void* m1_p
, const void* m2_p
)
2298 const tree
*const m1
= (const tree
*) m1_p
;
2299 const tree
*const m2
= (const tree
*) m2_p
;
2301 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2303 if (*m1
== NULL_TREE
)
2305 if (*m2
== NULL_TREE
)
2307 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
2312 /* This routine compares two fields like method_name_cmp but using the
2313 pointer operator in resort_field_decl_data. */
2316 resort_method_name_cmp (const void* m1_p
, const void* m2_p
)
2318 const tree
*const m1
= (const tree
*) m1_p
;
2319 const tree
*const m2
= (const tree
*) m2_p
;
2320 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2322 if (*m1
== NULL_TREE
)
2324 if (*m2
== NULL_TREE
)
2327 tree d1
= DECL_NAME (OVL_CURRENT (*m1
));
2328 tree d2
= DECL_NAME (OVL_CURRENT (*m2
));
2329 resort_data
.new_value (&d1
, resort_data
.cookie
);
2330 resort_data
.new_value (&d2
, resort_data
.cookie
);
2337 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2340 resort_type_method_vec (void* obj
,
2342 gt_pointer_operator new_value
,
2345 vec
<tree
, va_gc
> *method_vec
= (vec
<tree
, va_gc
> *) obj
;
2346 int len
= vec_safe_length (method_vec
);
2350 /* The type conversion ops have to live at the front of the vec, so we
2352 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2353 vec_safe_iterate (method_vec
, slot
, &fn
);
2355 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2360 resort_data
.new_value
= new_value
;
2361 resort_data
.cookie
= cookie
;
2362 qsort (method_vec
->address () + slot
, len
- slot
, sizeof (tree
),
2363 resort_method_name_cmp
);
2367 /* Warn about duplicate methods in fn_fields.
2369 Sort methods that are not special (i.e., constructors, destructors,
2370 and type conversion operators) so that we can find them faster in
2374 finish_struct_methods (tree t
)
2377 vec
<tree
, va_gc
> *method_vec
;
2380 method_vec
= CLASSTYPE_METHOD_VEC (t
);
2384 len
= method_vec
->length ();
2386 /* Clear DECL_IN_AGGR_P for all functions. */
2387 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2388 fn_fields
= DECL_CHAIN (fn_fields
))
2389 DECL_IN_AGGR_P (fn_fields
) = 0;
2391 /* Issue warnings about private constructors and such. If there are
2392 no methods, then some public defaults are generated. */
2393 maybe_warn_about_overly_private_class (t
);
2395 /* The type conversion ops have to live at the front of the vec, so we
2397 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2398 method_vec
->iterate (slot
, &fn_fields
);
2400 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields
)))
2403 qsort (method_vec
->address () + slot
,
2404 len
-slot
, sizeof (tree
), method_name_cmp
);
2407 /* Make BINFO's vtable have N entries, including RTTI entries,
2408 vbase and vcall offsets, etc. Set its type and call the back end
2412 layout_vtable_decl (tree binfo
, int n
)
2417 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2418 layout_type (atype
);
2420 /* We may have to grow the vtable. */
2421 vtable
= get_vtbl_decl_for_binfo (binfo
);
2422 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2424 TREE_TYPE (vtable
) = atype
;
2425 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2426 layout_decl (vtable
, 0);
2430 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2431 have the same signature. */
2434 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2436 /* One destructor overrides another if they are the same kind of
2438 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2439 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2441 /* But a non-destructor never overrides a destructor, nor vice
2442 versa, nor do different kinds of destructors override
2443 one-another. For example, a complete object destructor does not
2444 override a deleting destructor. */
2445 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2448 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2449 || (DECL_CONV_FN_P (fndecl
)
2450 && DECL_CONV_FN_P (base_fndecl
)
2451 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2452 DECL_CONV_FN_TYPE (base_fndecl
))))
2454 tree fntype
= TREE_TYPE (fndecl
);
2455 tree base_fntype
= TREE_TYPE (base_fndecl
);
2456 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2457 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2458 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2459 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2465 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2469 base_derived_from (tree derived
, tree base
)
2473 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2475 if (probe
== derived
)
2477 else if (BINFO_VIRTUAL_P (probe
))
2478 /* If we meet a virtual base, we can't follow the inheritance
2479 any more. See if the complete type of DERIVED contains
2480 such a virtual base. */
2481 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2487 struct find_final_overrider_data
{
2488 /* The function for which we are trying to find a final overrider. */
2490 /* The base class in which the function was declared. */
2491 tree declaring_base
;
2492 /* The candidate overriders. */
2494 /* Path to most derived. */
2498 /* Add the overrider along the current path to FFOD->CANDIDATES.
2499 Returns true if an overrider was found; false otherwise. */
2502 dfs_find_final_overrider_1 (tree binfo
,
2503 find_final_overrider_data
*ffod
,
2508 /* If BINFO is not the most derived type, try a more derived class.
2509 A definition there will overrider a definition here. */
2513 if (dfs_find_final_overrider_1
2514 (ffod
->path
[depth
], ffod
, depth
))
2518 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2521 tree
*candidate
= &ffod
->candidates
;
2523 /* Remove any candidates overridden by this new function. */
2526 /* If *CANDIDATE overrides METHOD, then METHOD
2527 cannot override anything else on the list. */
2528 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2530 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2531 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2532 *candidate
= TREE_CHAIN (*candidate
);
2534 candidate
= &TREE_CHAIN (*candidate
);
2537 /* Add the new function. */
2538 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2545 /* Called from find_final_overrider via dfs_walk. */
2548 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2550 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2552 if (binfo
== ffod
->declaring_base
)
2553 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2554 ffod
->path
.safe_push (binfo
);
2560 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2562 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2568 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2569 FN and whose TREE_VALUE is the binfo for the base where the
2570 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2571 DERIVED) is the base object in which FN is declared. */
2574 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2576 find_final_overrider_data ffod
;
2578 /* Getting this right is a little tricky. This is valid:
2580 struct S { virtual void f (); };
2581 struct T { virtual void f (); };
2582 struct U : public S, public T { };
2584 even though calling `f' in `U' is ambiguous. But,
2586 struct R { virtual void f(); };
2587 struct S : virtual public R { virtual void f (); };
2588 struct T : virtual public R { virtual void f (); };
2589 struct U : public S, public T { };
2591 is not -- there's no way to decide whether to put `S::f' or
2592 `T::f' in the vtable for `R'.
2594 The solution is to look at all paths to BINFO. If we find
2595 different overriders along any two, then there is a problem. */
2596 if (DECL_THUNK_P (fn
))
2597 fn
= THUNK_TARGET (fn
);
2599 /* Determine the depth of the hierarchy. */
2601 ffod
.declaring_base
= binfo
;
2602 ffod
.candidates
= NULL_TREE
;
2603 ffod
.path
.create (30);
2605 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2606 dfs_find_final_overrider_post
, &ffod
);
2608 ffod
.path
.release ();
2610 /* If there was no winner, issue an error message. */
2611 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2612 return error_mark_node
;
2614 return ffod
.candidates
;
2617 /* Return the index of the vcall offset for FN when TYPE is used as a
2621 get_vcall_index (tree fn
, tree type
)
2623 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2627 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2628 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2629 || same_signature_p (fn
, p
->purpose
))
2632 /* There should always be an appropriate index. */
2636 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2637 dominated by T. FN is the old function; VIRTUALS points to the
2638 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2639 of that entry in the list. */
2642 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2650 tree overrider_fn
, overrider_target
;
2651 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2652 tree over_return
, base_return
;
2655 /* Find the nearest primary base (possibly binfo itself) which defines
2656 this function; this is the class the caller will convert to when
2657 calling FN through BINFO. */
2658 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2661 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2664 /* The nearest definition is from a lost primary. */
2665 if (BINFO_LOST_PRIMARY_P (b
))
2670 /* Find the final overrider. */
2671 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2672 if (overrider
== error_mark_node
)
2674 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2677 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2679 /* Check for adjusting covariant return types. */
2680 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2681 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2683 if (POINTER_TYPE_P (over_return
)
2684 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2685 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2686 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2687 /* If the overrider is invalid, don't even try. */
2688 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2690 /* If FN is a covariant thunk, we must figure out the adjustment
2691 to the final base FN was converting to. As OVERRIDER_TARGET might
2692 also be converting to the return type of FN, we have to
2693 combine the two conversions here. */
2694 tree fixed_offset
, virtual_offset
;
2696 over_return
= TREE_TYPE (over_return
);
2697 base_return
= TREE_TYPE (base_return
);
2699 if (DECL_THUNK_P (fn
))
2701 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2702 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2703 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2706 fixed_offset
= virtual_offset
= NULL_TREE
;
2709 /* Find the equivalent binfo within the return type of the
2710 overriding function. We will want the vbase offset from
2712 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2714 else if (!same_type_ignoring_top_level_qualifiers_p
2715 (over_return
, base_return
))
2717 /* There was no existing virtual thunk (which takes
2718 precedence). So find the binfo of the base function's
2719 return type within the overriding function's return type.
2720 Fortunately we know the covariancy is valid (it
2721 has already been checked), so we can just iterate along
2722 the binfos, which have been chained in inheritance graph
2723 order. Of course it is lame that we have to repeat the
2724 search here anyway -- we should really be caching pieces
2725 of the vtable and avoiding this repeated work. */
2726 tree thunk_binfo
, base_binfo
;
2728 /* Find the base binfo within the overriding function's
2729 return type. We will always find a thunk_binfo, except
2730 when the covariancy is invalid (which we will have
2731 already diagnosed). */
2732 for (base_binfo
= TYPE_BINFO (base_return
),
2733 thunk_binfo
= TYPE_BINFO (over_return
);
2735 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2736 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2737 BINFO_TYPE (base_binfo
)))
2740 /* See if virtual inheritance is involved. */
2741 for (virtual_offset
= thunk_binfo
;
2743 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2744 if (BINFO_VIRTUAL_P (virtual_offset
))
2748 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2750 tree offset
= fold_convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2754 /* We convert via virtual base. Adjust the fixed
2755 offset to be from there. */
2757 size_diffop (offset
,
2758 fold_convert (ssizetype
,
2759 BINFO_OFFSET (virtual_offset
)));
2762 /* There was an existing fixed offset, this must be
2763 from the base just converted to, and the base the
2764 FN was thunking to. */
2765 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2767 fixed_offset
= offset
;
2771 if (fixed_offset
|| virtual_offset
)
2772 /* Replace the overriding function with a covariant thunk. We
2773 will emit the overriding function in its own slot as
2775 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2776 fixed_offset
, virtual_offset
);
2779 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2780 !DECL_THUNK_P (fn
));
2782 /* If we need a covariant thunk, then we may need to adjust first_defn.
2783 The ABI specifies that the thunks emitted with a function are
2784 determined by which bases the function overrides, so we need to be
2785 sure that we're using a thunk for some overridden base; even if we
2786 know that the necessary this adjustment is zero, there may not be an
2787 appropriate zero-this-adjusment thunk for us to use since thunks for
2788 overriding virtual bases always use the vcall offset.
2790 Furthermore, just choosing any base that overrides this function isn't
2791 quite right, as this slot won't be used for calls through a type that
2792 puts a covariant thunk here. Calling the function through such a type
2793 will use a different slot, and that slot is the one that determines
2794 the thunk emitted for that base.
2796 So, keep looking until we find the base that we're really overriding
2797 in this slot: the nearest primary base that doesn't use a covariant
2798 thunk in this slot. */
2799 if (overrider_target
!= overrider_fn
)
2801 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2802 /* We already know that the overrider needs a covariant thunk. */
2803 b
= get_primary_binfo (b
);
2804 for (; ; b
= get_primary_binfo (b
))
2806 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2807 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2808 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2810 if (BINFO_LOST_PRIMARY_P (b
))
2816 /* Assume that we will produce a thunk that convert all the way to
2817 the final overrider, and not to an intermediate virtual base. */
2818 virtual_base
= NULL_TREE
;
2820 /* See if we can convert to an intermediate virtual base first, and then
2821 use the vcall offset located there to finish the conversion. */
2822 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2824 /* If we find the final overrider, then we can stop
2826 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2827 BINFO_TYPE (TREE_VALUE (overrider
))))
2830 /* If we find a virtual base, and we haven't yet found the
2831 overrider, then there is a virtual base between the
2832 declaring base (first_defn) and the final overrider. */
2833 if (BINFO_VIRTUAL_P (b
))
2840 /* Compute the constant adjustment to the `this' pointer. The
2841 `this' pointer, when this function is called, will point at BINFO
2842 (or one of its primary bases, which are at the same offset). */
2844 /* The `this' pointer needs to be adjusted from the declaration to
2845 the nearest virtual base. */
2846 delta
= size_diffop_loc (input_location
,
2847 fold_convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2848 fold_convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2850 /* If the nearest definition is in a lost primary, we don't need an
2851 entry in our vtable. Except possibly in a constructor vtable,
2852 if we happen to get our primary back. In that case, the offset
2853 will be zero, as it will be a primary base. */
2854 delta
= size_zero_node
;
2856 /* The `this' pointer needs to be adjusted from pointing to
2857 BINFO to pointing at the base where the final overrider
2859 delta
= size_diffop_loc (input_location
,
2860 fold_convert (ssizetype
,
2861 BINFO_OFFSET (TREE_VALUE (overrider
))),
2862 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)));
2864 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2867 BV_VCALL_INDEX (*virtuals
)
2868 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2870 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2872 BV_LOST_PRIMARY (*virtuals
) = lost
;
2875 /* Called from modify_all_vtables via dfs_walk. */
2878 dfs_modify_vtables (tree binfo
, void* data
)
2880 tree t
= (tree
) data
;
2885 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2886 /* A base without a vtable needs no modification, and its bases
2887 are uninteresting. */
2888 return dfs_skip_bases
;
2890 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2891 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2892 /* Don't do the primary vtable, if it's new. */
2895 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2896 /* There's no need to modify the vtable for a non-virtual primary
2897 base; we're not going to use that vtable anyhow. We do still
2898 need to do this for virtual primary bases, as they could become
2899 non-primary in a construction vtable. */
2902 make_new_vtable (t
, binfo
);
2904 /* Now, go through each of the virtual functions in the virtual
2905 function table for BINFO. Find the final overrider, and update
2906 the BINFO_VIRTUALS list appropriately. */
2907 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2908 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2910 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2911 old_virtuals
= TREE_CHAIN (old_virtuals
))
2912 update_vtable_entry_for_fn (t
,
2914 BV_FN (old_virtuals
),
2920 /* Update all of the primary and secondary vtables for T. Create new
2921 vtables as required, and initialize their RTTI information. Each
2922 of the functions in VIRTUALS is declared in T and may override a
2923 virtual function from a base class; find and modify the appropriate
2924 entries to point to the overriding functions. Returns a list, in
2925 declaration order, of the virtual functions that are declared in T,
2926 but do not appear in the primary base class vtable, and which
2927 should therefore be appended to the end of the vtable for T. */
2930 modify_all_vtables (tree t
, tree virtuals
)
2932 tree binfo
= TYPE_BINFO (t
);
2935 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2936 if (TYPE_CONTAINS_VPTR_P (t
))
2937 get_vtable_decl (t
, false);
2939 /* Update all of the vtables. */
2940 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2942 /* Add virtual functions not already in our primary vtable. These
2943 will be both those introduced by this class, and those overridden
2944 from secondary bases. It does not include virtuals merely
2945 inherited from secondary bases. */
2946 for (fnsp
= &virtuals
; *fnsp
; )
2948 tree fn
= TREE_VALUE (*fnsp
);
2950 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2951 || DECL_VINDEX (fn
) == error_mark_node
)
2953 /* We don't need to adjust the `this' pointer when
2954 calling this function. */
2955 BV_DELTA (*fnsp
) = integer_zero_node
;
2956 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2958 /* This is a function not already in our vtable. Keep it. */
2959 fnsp
= &TREE_CHAIN (*fnsp
);
2962 /* We've already got an entry for this function. Skip it. */
2963 *fnsp
= TREE_CHAIN (*fnsp
);
2969 /* Get the base virtual function declarations in T that have the
2973 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2976 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2979 /* Find virtual functions in T with the indicated NAME. */
2980 i
= lookup_fnfields_1 (t
, name
);
2981 bool found_decls
= false;
2983 for (methods
= (*CLASSTYPE_METHOD_VEC (t
))[i
];
2985 methods
= OVL_NEXT (methods
))
2987 tree method
= OVL_CURRENT (methods
);
2989 if (TREE_CODE (method
) == FUNCTION_DECL
2990 && DECL_VINDEX (method
))
2992 base_fndecls
->safe_push (method
);
3000 for (i
= 0; i
< n_baseclasses
; i
++)
3002 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
3003 get_basefndecls (name
, basetype
, base_fndecls
);
3007 /* If this declaration supersedes the declaration of
3008 a method declared virtual in the base class, then
3009 mark this field as being virtual as well. */
3012 check_for_override (tree decl
, tree ctype
)
3014 bool overrides_found
= false;
3015 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3016 /* In [temp.mem] we have:
3018 A specialization of a member function template does not
3019 override a virtual function from a base class. */
3021 if ((DECL_DESTRUCTOR_P (decl
)
3022 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
3023 || DECL_CONV_FN_P (decl
))
3024 && look_for_overrides (ctype
, decl
)
3025 && !DECL_STATIC_FUNCTION_P (decl
))
3026 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3027 the error_mark_node so that we know it is an overriding
3030 DECL_VINDEX (decl
) = decl
;
3031 overrides_found
= true;
3032 if (warn_override
&& !DECL_OVERRIDE_P (decl
)
3033 && !DECL_DESTRUCTOR_P (decl
))
3034 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
3035 "%qD can be marked override", decl
);
3038 if (DECL_VIRTUAL_P (decl
))
3040 if (!DECL_VINDEX (decl
))
3041 DECL_VINDEX (decl
) = error_mark_node
;
3042 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
3043 if (DECL_DESTRUCTOR_P (decl
))
3044 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
3046 else if (DECL_FINAL_P (decl
))
3047 error ("%q+#D marked %<final%>, but is not virtual", decl
);
3048 if (DECL_OVERRIDE_P (decl
) && !overrides_found
)
3049 error ("%q+#D marked %<override%>, but does not override", decl
);
3052 /* Warn about hidden virtual functions that are not overridden in t.
3053 We know that constructors and destructors don't apply. */
3056 warn_hidden (tree t
)
3058 vec
<tree
, va_gc
> *method_vec
= CLASSTYPE_METHOD_VEC (t
);
3062 /* We go through each separately named virtual function. */
3063 for (i
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
3064 vec_safe_iterate (method_vec
, i
, &fns
);
3074 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3075 have the same name. Figure out what name that is. */
3076 name
= DECL_NAME (OVL_CURRENT (fns
));
3077 /* There are no possibly hidden functions yet. */
3078 auto_vec
<tree
, 20> base_fndecls
;
3079 /* Iterate through all of the base classes looking for possibly
3080 hidden functions. */
3081 for (binfo
= TYPE_BINFO (t
), j
= 0;
3082 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
3084 tree basetype
= BINFO_TYPE (base_binfo
);
3085 get_basefndecls (name
, basetype
, &base_fndecls
);
3088 /* If there are no functions to hide, continue. */
3089 if (base_fndecls
.is_empty ())
3092 /* Remove any overridden functions. */
3093 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
3095 fndecl
= OVL_CURRENT (fn
);
3096 if (TREE_CODE (fndecl
) == FUNCTION_DECL
3097 && DECL_VINDEX (fndecl
))
3099 /* If the method from the base class has the same
3100 signature as the method from the derived class, it
3101 has been overridden. */
3102 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
3104 && same_signature_p (fndecl
, base_fndecls
[k
]))
3105 base_fndecls
[k
] = NULL_TREE
;
3109 /* Now give a warning for all base functions without overriders,
3110 as they are hidden. */
3113 FOR_EACH_VEC_ELT (base_fndecls
, k
, base_fndecl
)
3116 /* Here we know it is a hider, and no overrider exists. */
3117 warning_at (location_of (base_fndecl
),
3118 OPT_Woverloaded_virtual
,
3119 "%qD was hidden", base_fndecl
);
3120 warning_at (location_of (fns
),
3121 OPT_Woverloaded_virtual
, " by %qD", fns
);
3126 /* Recursive helper for finish_struct_anon. */
3129 finish_struct_anon_r (tree field
, bool complain
)
3131 bool is_union
= TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
;
3132 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
3133 for (; elt
; elt
= DECL_CHAIN (elt
))
3135 /* We're generally only interested in entities the user
3136 declared, but we also find nested classes by noticing
3137 the TYPE_DECL that we create implicitly. You're
3138 allowed to put one anonymous union inside another,
3139 though, so we explicitly tolerate that. We use
3140 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3141 we also allow unnamed types used for defining fields. */
3142 if (DECL_ARTIFICIAL (elt
)
3143 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
3144 || TYPE_UNNAMED_P (TREE_TYPE (elt
))))
3147 if (TREE_CODE (elt
) != FIELD_DECL
)
3149 /* We already complained about static data members in
3150 finish_static_data_member_decl. */
3151 if (complain
&& !VAR_P (elt
))
3154 permerror (DECL_SOURCE_LOCATION (elt
),
3155 "%q#D invalid; an anonymous union can "
3156 "only have non-static data members", elt
);
3158 permerror (DECL_SOURCE_LOCATION (elt
),
3159 "%q#D invalid; an anonymous struct can "
3160 "only have non-static data members", elt
);
3167 if (TREE_PRIVATE (elt
))
3170 permerror (DECL_SOURCE_LOCATION (elt
),
3171 "private member %q#D in anonymous union", elt
);
3173 permerror (DECL_SOURCE_LOCATION (elt
),
3174 "private member %q#D in anonymous struct", elt
);
3176 else if (TREE_PROTECTED (elt
))
3179 permerror (DECL_SOURCE_LOCATION (elt
),
3180 "protected member %q#D in anonymous union", elt
);
3182 permerror (DECL_SOURCE_LOCATION (elt
),
3183 "protected member %q#D in anonymous struct", elt
);
3187 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3188 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3190 /* Recurse into the anonymous aggregates to handle correctly
3191 access control (c++/24926):
3202 if (DECL_NAME (elt
) == NULL_TREE
3203 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
3204 finish_struct_anon_r (elt
, /*complain=*/false);
3208 /* Check for things that are invalid. There are probably plenty of other
3209 things we should check for also. */
3212 finish_struct_anon (tree t
)
3214 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3216 if (TREE_STATIC (field
))
3218 if (TREE_CODE (field
) != FIELD_DECL
)
3221 if (DECL_NAME (field
) == NULL_TREE
3222 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3223 finish_struct_anon_r (field
, /*complain=*/true);
3227 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3228 will be used later during class template instantiation.
3229 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3230 a non-static member data (FIELD_DECL), a member function
3231 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3232 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3233 When FRIEND_P is nonzero, T is either a friend class
3234 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3235 (FUNCTION_DECL, TEMPLATE_DECL). */
3238 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
3240 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3241 if (CLASSTYPE_TEMPLATE_INFO (type
))
3242 CLASSTYPE_DECL_LIST (type
)
3243 = tree_cons (friend_p
? NULL_TREE
: type
,
3244 t
, CLASSTYPE_DECL_LIST (type
));
3247 /* This function is called from declare_virt_assop_and_dtor via
3250 DATA is a type that direcly or indirectly inherits the base
3251 represented by BINFO. If BINFO contains a virtual assignment [copy
3252 assignment or move assigment] operator or a virtual constructor,
3253 declare that function in DATA if it hasn't been already declared. */
3256 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
3258 tree bv
, fn
, t
= (tree
)data
;
3259 tree opname
= ansi_assopname (NOP_EXPR
);
3261 gcc_assert (t
&& CLASS_TYPE_P (t
));
3262 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
3264 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
3265 /* A base without a vtable needs no modification, and its bases
3266 are uninteresting. */
3267 return dfs_skip_bases
;
3269 if (BINFO_PRIMARY_P (binfo
))
3270 /* If this is a primary base, then we have already looked at the
3271 virtual functions of its vtable. */
3274 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3278 if (DECL_NAME (fn
) == opname
)
3280 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3281 lazily_declare_fn (sfk_copy_assignment
, t
);
3282 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3283 lazily_declare_fn (sfk_move_assignment
, t
);
3285 else if (DECL_DESTRUCTOR_P (fn
)
3286 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3287 lazily_declare_fn (sfk_destructor
, t
);
3293 /* If the class type T has a direct or indirect base that contains a
3294 virtual assignment operator or a virtual destructor, declare that
3295 function in T if it hasn't been already declared. */
3298 declare_virt_assop_and_dtor (tree t
)
3300 if (!(TYPE_POLYMORPHIC_P (t
)
3301 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3302 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3303 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3306 dfs_walk_all (TYPE_BINFO (t
),
3307 dfs_declare_virt_assop_and_dtor
,
3311 /* Declare the inheriting constructor for class T inherited from base
3312 constructor CTOR with the parameter array PARMS of size NPARMS. */
3315 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3317 /* We don't declare an inheriting ctor that would be a default,
3318 copy or move ctor for derived or base. */
3322 && TREE_CODE (parms
[0]) == REFERENCE_TYPE
)
3324 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3325 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3329 tree parmlist
= void_list_node
;
3330 for (int i
= nparms
- 1; i
>= 0; i
--)
3331 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3332 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3333 t
, false, ctor
, parmlist
);
3334 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3335 if (add_method (t
, fn
, NULL_TREE
))
3337 DECL_CHAIN (fn
) = TYPE_METHODS (t
);
3338 TYPE_METHODS (t
) = fn
;
3342 /* Declare all the inheriting constructors for class T inherited from base
3343 constructor CTOR. */
3346 one_inherited_ctor (tree ctor
, tree t
, tree using_decl
)
3348 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3350 if (flag_new_inheriting_ctors
)
3352 ctor
= implicitly_declare_fn (sfk_inheriting_constructor
,
3353 t
, /*const*/false, ctor
, parms
);
3354 add_method (t
, ctor
, using_decl
);
3355 TYPE_HAS_USER_CONSTRUCTOR (t
) = true;
3359 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3361 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3363 if (TREE_PURPOSE (parms
))
3364 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3365 new_parms
[i
++] = TREE_VALUE (parms
);
3367 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3368 if (parms
== NULL_TREE
)
3370 if (warning (OPT_Winherited_variadic_ctor
,
3371 "the ellipsis in %qD is not inherited", ctor
))
3372 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3376 /* Create default constructors, assignment operators, and so forth for
3377 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3378 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3379 the class cannot have a default constructor, copy constructor
3380 taking a const reference argument, or an assignment operator taking
3381 a const reference, respectively. */
3384 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3385 int cant_have_const_cctor
,
3386 int cant_have_const_assignment
)
3388 bool move_ok
= false;
3390 if (cxx_dialect
>= cxx11
&& !CLASSTYPE_DESTRUCTORS (t
)
3391 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3392 && !type_has_move_constructor (t
) && !type_has_move_assign (t
))
3396 if (!CLASSTYPE_DESTRUCTORS (t
))
3397 /* In general, we create destructors lazily. */
3398 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3402 If there is no user-declared constructor for a class, a default
3403 constructor is implicitly declared. */
3404 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3406 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3407 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3408 if (cxx_dialect
>= cxx11
)
3409 TYPE_HAS_CONSTEXPR_CTOR (t
)
3410 /* Don't force the declaration to get a hard answer; if the
3411 definition would have made the class non-literal, it will still be
3412 non-literal because of the base or member in question, and that
3413 gives a better diagnostic. */
3414 = type_maybe_constexpr_default_constructor (t
);
3419 If a class definition does not explicitly declare a copy
3420 constructor, one is declared implicitly. */
3421 if (! TYPE_HAS_COPY_CTOR (t
))
3423 TYPE_HAS_COPY_CTOR (t
) = 1;
3424 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3425 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3427 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3430 /* If there is no assignment operator, one will be created if and
3431 when it is needed. For now, just record whether or not the type
3432 of the parameter to the assignment operator will be a const or
3433 non-const reference. */
3434 if (!TYPE_HAS_COPY_ASSIGN (t
))
3436 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3437 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3438 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3439 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3440 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3443 /* We can't be lazy about declaring functions that might override
3444 a virtual function from a base class. */
3445 declare_virt_assop_and_dtor (t
);
3447 while (*access_decls
)
3449 tree using_decl
= TREE_VALUE (*access_decls
);
3450 tree decl
= USING_DECL_DECLS (using_decl
);
3451 if (DECL_NAME (using_decl
) == ctor_identifier
)
3453 /* declare, then remove the decl */
3454 tree ctor_list
= decl
;
3455 location_t loc
= input_location
;
3456 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3458 for (; ctor_list
; ctor_list
= OVL_NEXT (ctor_list
))
3459 one_inherited_ctor (OVL_CURRENT (ctor_list
), t
, using_decl
);
3460 *access_decls
= TREE_CHAIN (*access_decls
);
3461 input_location
= loc
;
3464 access_decls
= &TREE_CHAIN (*access_decls
);
3468 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3469 count the number of fields in TYPE, including anonymous union
3473 count_fields (tree fields
)
3477 for (x
= fields
; x
; x
= DECL_CHAIN (x
))
3479 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3480 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3487 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3488 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3489 elts, starting at offset IDX. */
3492 add_fields_to_record_type (tree fields
, struct sorted_fields_type
*field_vec
, int idx
)
3495 for (x
= fields
; x
; x
= DECL_CHAIN (x
))
3497 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3498 idx
= add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3500 field_vec
->elts
[idx
++] = x
;
3505 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3506 starting at offset IDX. */
3509 add_enum_fields_to_record_type (tree enumtype
,
3510 struct sorted_fields_type
*field_vec
,
3514 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
3515 field_vec
->elts
[idx
++] = TREE_VALUE (values
);
3519 /* FIELD is a bit-field. We are finishing the processing for its
3520 enclosing type. Issue any appropriate messages and set appropriate
3521 flags. Returns false if an error has been diagnosed. */
3524 check_bitfield_decl (tree field
)
3526 tree type
= TREE_TYPE (field
);
3529 /* Extract the declared width of the bitfield, which has been
3530 temporarily stashed in DECL_INITIAL. */
3531 w
= DECL_INITIAL (field
);
3532 gcc_assert (w
!= NULL_TREE
);
3533 /* Remove the bit-field width indicator so that the rest of the
3534 compiler does not treat that value as an initializer. */
3535 DECL_INITIAL (field
) = NULL_TREE
;
3537 /* Detect invalid bit-field type. */
3538 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3540 error ("bit-field %q+#D with non-integral type", field
);
3541 w
= error_mark_node
;
3545 location_t loc
= input_location
;
3546 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3549 /* detect invalid field size. */
3550 input_location
= DECL_SOURCE_LOCATION (field
);
3551 w
= cxx_constant_value (w
);
3552 input_location
= loc
;
3554 if (TREE_CODE (w
) != INTEGER_CST
)
3556 error ("bit-field %q+D width not an integer constant", field
);
3557 w
= error_mark_node
;
3559 else if (tree_int_cst_sgn (w
) < 0)
3561 error ("negative width in bit-field %q+D", field
);
3562 w
= error_mark_node
;
3564 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3566 error ("zero width for bit-field %q+D", field
);
3567 w
= error_mark_node
;
3569 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3570 && TREE_CODE (type
) != BOOLEAN_TYPE
3571 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3572 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3573 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3574 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3575 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3576 "width of %qD exceeds its type", field
);
3577 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3578 && (0 > (compare_tree_int
3579 (w
, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
))))))
3580 warning_at (DECL_SOURCE_LOCATION (field
), 0,
3581 "%qD is too small to hold all values of %q#T",
3585 if (w
!= error_mark_node
)
3587 DECL_SIZE (field
) = fold_convert (bitsizetype
, w
);
3588 DECL_BIT_FIELD (field
) = 1;
3593 /* Non-bit-fields are aligned for their type. */
3594 DECL_BIT_FIELD (field
) = 0;
3595 CLEAR_DECL_C_BIT_FIELD (field
);
3600 /* FIELD is a non bit-field. We are finishing the processing for its
3601 enclosing type T. Issue any appropriate messages and set appropriate
3605 check_field_decl (tree field
,
3607 int* cant_have_const_ctor
,
3608 int* no_const_asn_ref
)
3610 tree type
= strip_array_types (TREE_TYPE (field
));
3611 bool any_default_members
= false;
3613 /* In C++98 an anonymous union cannot contain any fields which would change
3614 the settings of CANT_HAVE_CONST_CTOR and friends. */
3615 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3617 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3618 structs. So, we recurse through their fields here. */
3619 else if (ANON_AGGR_TYPE_P (type
))
3621 for (tree fields
= TYPE_FIELDS (type
); fields
;
3622 fields
= DECL_CHAIN (fields
))
3623 if (TREE_CODE (fields
) == FIELD_DECL
&& !DECL_C_BIT_FIELD (field
))
3624 any_default_members
|= check_field_decl (fields
, t
,
3625 cant_have_const_ctor
,
3628 /* Check members with class type for constructors, destructors,
3630 else if (CLASS_TYPE_P (type
))
3632 /* Never let anything with uninheritable virtuals
3633 make it through without complaint. */
3634 abstract_virtuals_error (field
, type
);
3636 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3639 int oldcount
= errorcount
;
3640 if (TYPE_NEEDS_CONSTRUCTING (type
))
3641 error ("member %q+#D with constructor not allowed in union",
3643 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3644 error ("member %q+#D with destructor not allowed in union", field
);
3645 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3646 error ("member %q+#D with copy assignment operator not allowed in union",
3648 if (!warned
&& errorcount
> oldcount
)
3650 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3651 "only available with -std=c++11 or -std=gnu++11");
3657 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3658 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3659 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3660 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3661 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3662 || !TYPE_HAS_COPY_ASSIGN (type
));
3663 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3664 || !TYPE_HAS_COPY_CTOR (type
));
3665 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3666 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3667 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3668 || TYPE_HAS_COMPLEX_DFLT (type
));
3671 if (TYPE_HAS_COPY_CTOR (type
)
3672 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3673 *cant_have_const_ctor
= 1;
3675 if (TYPE_HAS_COPY_ASSIGN (type
)
3676 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3677 *no_const_asn_ref
= 1;
3680 check_abi_tags (t
, field
);
3682 if (DECL_INITIAL (field
) != NULL_TREE
)
3683 /* `build_class_init_list' does not recognize
3685 any_default_members
= true;
3687 return any_default_members
;
3690 /* Check the data members (both static and non-static), class-scoped
3691 typedefs, etc., appearing in the declaration of T. Issue
3692 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3693 declaration order) of access declarations; each TREE_VALUE in this
3694 list is a USING_DECL.
3696 In addition, set the following flags:
3699 The class is empty, i.e., contains no non-static data members.
3701 CANT_HAVE_CONST_CTOR_P
3702 This class cannot have an implicitly generated copy constructor
3703 taking a const reference.
3705 CANT_HAVE_CONST_ASN_REF
3706 This class cannot have an implicitly generated assignment
3707 operator taking a const reference.
3709 All of these flags should be initialized before calling this
3712 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3713 fields can be added by adding to this chain. */
3716 check_field_decls (tree t
, tree
*access_decls
,
3717 int *cant_have_const_ctor_p
,
3718 int *no_const_asn_ref_p
)
3723 bool any_default_members
;
3725 int field_access
= -1;
3727 /* Assume there are no access declarations. */
3728 *access_decls
= NULL_TREE
;
3729 /* Assume this class has no pointer members. */
3730 has_pointers
= false;
3731 /* Assume none of the members of this class have default
3733 any_default_members
= false;
3735 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3738 tree type
= TREE_TYPE (x
);
3739 int this_field_access
;
3741 next
= &DECL_CHAIN (x
);
3743 if (TREE_CODE (x
) == USING_DECL
)
3745 /* Save the access declarations for our caller. */
3746 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3750 if (TREE_CODE (x
) == TYPE_DECL
3751 || TREE_CODE (x
) == TEMPLATE_DECL
)
3754 /* If we've gotten this far, it's a data member, possibly static,
3755 or an enumerator. */
3756 if (TREE_CODE (x
) != CONST_DECL
)
3757 DECL_CONTEXT (x
) = t
;
3759 /* When this goes into scope, it will be a non-local reference. */
3760 DECL_NONLOCAL (x
) = 1;
3762 if (TREE_CODE (t
) == UNION_TYPE
3763 && cxx_dialect
< cxx11
)
3765 /* [class.union] (C++98)
3767 If a union contains a static data member, or a member of
3768 reference type, the program is ill-formed.
3770 In C++11 this limitation doesn't exist anymore. */
3773 error ("in C++98 %q+D may not be static because it is "
3774 "a member of a union", x
);
3777 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3779 error ("in C++98 %q+D may not have reference type %qT "
3780 "because it is a member of a union", x
, type
);
3785 /* Perform error checking that did not get done in
3787 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3789 error ("field %q+D invalidly declared function type", x
);
3790 type
= build_pointer_type (type
);
3791 TREE_TYPE (x
) = type
;
3793 else if (TREE_CODE (type
) == METHOD_TYPE
)
3795 error ("field %q+D invalidly declared method type", x
);
3796 type
= build_pointer_type (type
);
3797 TREE_TYPE (x
) = type
;
3800 if (type
== error_mark_node
)
3803 if (TREE_CODE (x
) == CONST_DECL
|| VAR_P (x
))
3806 /* Now it can only be a FIELD_DECL. */
3808 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3809 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3811 /* If at least one non-static data member is non-literal, the whole
3812 class becomes non-literal. Per Core/1453, volatile non-static
3813 data members and base classes are also not allowed.
3814 Note: if the type is incomplete we will complain later on. */
3815 if (COMPLETE_TYPE_P (type
)
3816 && (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
)))
3817 CLASSTYPE_LITERAL_P (t
) = false;
3819 /* A standard-layout class is a class that:
3821 has the same access control (Clause 11) for all non-static data members,
3823 this_field_access
= TREE_PROTECTED (x
) ? 1 : TREE_PRIVATE (x
) ? 2 : 0;
3824 if (field_access
== -1)
3825 field_access
= this_field_access
;
3826 else if (this_field_access
!= field_access
)
3827 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3829 /* If this is of reference type, check if it needs an init. */
3830 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3832 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3833 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3834 if (DECL_INITIAL (x
) == NULL_TREE
)
3835 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3836 if (cxx_dialect
< cxx11
)
3838 /* ARM $12.6.2: [A member initializer list] (or, for an
3839 aggregate, initialization by a brace-enclosed list) is the
3840 only way to initialize nonstatic const and reference
3842 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3843 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3847 type
= strip_array_types (type
);
3849 if (TYPE_PACKED (t
))
3851 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3854 (DECL_SOURCE_LOCATION (x
), 0,
3855 "ignoring packed attribute because of unpacked non-POD field %q#D",
3859 else if (DECL_C_BIT_FIELD (x
)
3860 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
)
3861 DECL_PACKED (x
) = 1;
3864 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3865 /* We don't treat zero-width bitfields as making a class
3870 /* The class is non-empty. */
3871 CLASSTYPE_EMPTY_P (t
) = 0;
3872 /* The class is not even nearly empty. */
3873 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3874 /* If one of the data members contains an empty class,
3876 if (CLASS_TYPE_P (type
)
3877 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3878 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3881 /* This is used by -Weffc++ (see below). Warn only for pointers
3882 to members which might hold dynamic memory. So do not warn
3883 for pointers to functions or pointers to members. */
3884 if (TYPE_PTR_P (type
)
3885 && !TYPE_PTRFN_P (type
))
3886 has_pointers
= true;
3888 if (CLASS_TYPE_P (type
))
3890 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3891 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3892 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3893 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3896 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3897 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3899 if (DECL_MUTABLE_P (x
))
3901 if (CP_TYPE_CONST_P (type
))
3903 error ("member %q+D cannot be declared both %<const%> "
3904 "and %<mutable%>", x
);
3907 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3909 error ("member %q+D cannot be declared as a %<mutable%> "
3915 if (! layout_pod_type_p (type
))
3916 /* DR 148 now allows pointers to members (which are POD themselves),
3917 to be allowed in POD structs. */
3918 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3920 if (!std_layout_type_p (type
))
3921 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3923 if (! zero_init_p (type
))
3924 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3926 /* We set DECL_C_BIT_FIELD in grokbitfield.
3927 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3928 if ((! DECL_C_BIT_FIELD (x
) || ! check_bitfield_decl (x
))
3929 && check_field_decl (x
, t
,
3930 cant_have_const_ctor_p
,
3931 no_const_asn_ref_p
))
3933 if (any_default_members
3934 && TREE_CODE (t
) == UNION_TYPE
)
3935 error ("multiple fields in union %qT initialized", t
);
3936 any_default_members
= true;
3939 /* Now that we've removed bit-field widths from DECL_INITIAL,
3940 anything left in DECL_INITIAL is an NSDMI that makes the class
3941 non-aggregate in C++11. */
3942 if (DECL_INITIAL (x
) && cxx_dialect
< cxx14
)
3943 CLASSTYPE_NON_AGGREGATE (t
) = true;
3945 /* If any field is const, the structure type is pseudo-const. */
3946 if (CP_TYPE_CONST_P (type
))
3948 C_TYPE_FIELDS_READONLY (t
) = 1;
3949 if (DECL_INITIAL (x
) == NULL_TREE
)
3950 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3951 if (cxx_dialect
< cxx11
)
3953 /* ARM $12.6.2: [A member initializer list] (or, for an
3954 aggregate, initialization by a brace-enclosed list) is the
3955 only way to initialize nonstatic const and reference
3957 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3958 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3961 /* A field that is pseudo-const makes the structure likewise. */
3962 else if (CLASS_TYPE_P (type
))
3964 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3965 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3966 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3967 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3970 /* Core issue 80: A nonstatic data member is required to have a
3971 different name from the class iff the class has a
3972 user-declared constructor. */
3973 if (constructor_name_p (DECL_NAME (x
), t
)
3974 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3975 permerror (DECL_SOURCE_LOCATION (x
),
3976 "field %q#D with same name as class", x
);
3979 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3980 it should also define a copy constructor and an assignment operator to
3981 implement the correct copy semantic (deep vs shallow, etc.). As it is
3982 not feasible to check whether the constructors do allocate dynamic memory
3983 and store it within members, we approximate the warning like this:
3985 -- Warn only if there are members which are pointers
3986 -- Warn only if there is a non-trivial constructor (otherwise,
3987 there cannot be memory allocated).
3988 -- Warn only if there is a non-trivial destructor. We assume that the
3989 user at least implemented the cleanup correctly, and a destructor
3990 is needed to free dynamic memory.
3992 This seems enough for practical purposes. */
3995 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3996 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3997 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3999 warning (OPT_Weffc__
, "%q#T has pointer data members", t
);
4001 if (! TYPE_HAS_COPY_CTOR (t
))
4003 warning (OPT_Weffc__
,
4004 " but does not override %<%T(const %T&)%>", t
, t
);
4005 if (!TYPE_HAS_COPY_ASSIGN (t
))
4006 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
4008 else if (! TYPE_HAS_COPY_ASSIGN (t
))
4009 warning (OPT_Weffc__
,
4010 " but does not override %<operator=(const %T&)%>", t
);
4013 /* Non-static data member initializers make the default constructor
4015 if (any_default_members
)
4017 TYPE_NEEDS_CONSTRUCTING (t
) = true;
4018 TYPE_HAS_COMPLEX_DFLT (t
) = true;
4021 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4023 TYPE_PACKED (t
) = 0;
4025 /* Check anonymous struct/anonymous union fields. */
4026 finish_struct_anon (t
);
4028 /* We've built up the list of access declarations in reverse order.
4030 *access_decls
= nreverse (*access_decls
);
4033 /* If TYPE is an empty class type, records its OFFSET in the table of
4037 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
4041 if (!is_empty_class (type
))
4044 /* Record the location of this empty object in OFFSETS. */
4045 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
4047 n
= splay_tree_insert (offsets
,
4048 (splay_tree_key
) offset
,
4049 (splay_tree_value
) NULL_TREE
);
4050 n
->value
= ((splay_tree_value
)
4051 tree_cons (NULL_TREE
,
4058 /* Returns nonzero if TYPE is an empty class type and there is
4059 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4062 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
4067 if (!is_empty_class (type
))
4070 /* Record the location of this empty object in OFFSETS. */
4071 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
4075 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
4076 if (same_type_p (TREE_VALUE (t
), type
))
4082 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4083 F for every subobject, passing it the type, offset, and table of
4084 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4087 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4088 than MAX_OFFSET will not be walked.
4090 If F returns a nonzero value, the traversal ceases, and that value
4091 is returned. Otherwise, returns zero. */
4094 walk_subobject_offsets (tree type
,
4095 subobject_offset_fn f
,
4102 tree type_binfo
= NULL_TREE
;
4104 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4106 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4109 if (type
== error_mark_node
)
4115 type
= BINFO_TYPE (type
);
4118 if (CLASS_TYPE_P (type
))
4124 /* Avoid recursing into objects that are not interesting. */
4125 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
4128 /* Record the location of TYPE. */
4129 r
= (*f
) (type
, offset
, offsets
);
4133 /* Iterate through the direct base classes of TYPE. */
4135 type_binfo
= TYPE_BINFO (type
);
4136 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
4140 if (BINFO_VIRTUAL_P (binfo
))
4144 /* We cannot rely on BINFO_OFFSET being set for the base
4145 class yet, but the offsets for direct non-virtual
4146 bases can be calculated by going back to the TYPE. */
4147 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
4148 binfo_offset
= size_binop (PLUS_EXPR
,
4150 BINFO_OFFSET (orig_binfo
));
4152 r
= walk_subobject_offsets (binfo
,
4162 if (CLASSTYPE_VBASECLASSES (type
))
4165 vec
<tree
, va_gc
> *vbases
;
4167 /* Iterate through the virtual base classes of TYPE. In G++
4168 3.2, we included virtual bases in the direct base class
4169 loop above, which results in incorrect results; the
4170 correct offsets for virtual bases are only known when
4171 working with the most derived type. */
4173 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
4174 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
4176 r
= walk_subobject_offsets (binfo
,
4178 size_binop (PLUS_EXPR
,
4180 BINFO_OFFSET (binfo
)),
4189 /* We still have to walk the primary base, if it is
4190 virtual. (If it is non-virtual, then it was walked
4192 tree vbase
= get_primary_binfo (type_binfo
);
4194 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
4195 && BINFO_PRIMARY_P (vbase
)
4196 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
4198 r
= (walk_subobject_offsets
4200 offsets
, max_offset
, /*vbases_p=*/0));
4207 /* Iterate through the fields of TYPE. */
4208 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4209 if (TREE_CODE (field
) == FIELD_DECL
4210 && TREE_TYPE (field
) != error_mark_node
4211 && !DECL_ARTIFICIAL (field
))
4215 field_offset
= byte_position (field
);
4217 r
= walk_subobject_offsets (TREE_TYPE (field
),
4219 size_binop (PLUS_EXPR
,
4229 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4231 tree element_type
= strip_array_types (type
);
4232 tree domain
= TYPE_DOMAIN (type
);
4235 /* Avoid recursing into objects that are not interesting. */
4236 if (!CLASS_TYPE_P (element_type
)
4237 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
)
4239 || integer_minus_onep (TYPE_MAX_VALUE (domain
)))
4242 /* Step through each of the elements in the array. */
4243 for (index
= size_zero_node
;
4244 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
4245 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
4247 r
= walk_subobject_offsets (TREE_TYPE (type
),
4255 offset
= size_binop (PLUS_EXPR
, offset
,
4256 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
4257 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4258 there's no point in iterating through the remaining
4259 elements of the array. */
4260 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4268 /* Record all of the empty subobjects of TYPE (either a type or a
4269 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4270 is being placed at OFFSET; otherwise, it is a base class that is
4271 being placed at OFFSET. */
4274 record_subobject_offsets (tree type
,
4277 bool is_data_member
)
4280 /* If recording subobjects for a non-static data member or a
4281 non-empty base class , we do not need to record offsets beyond
4282 the size of the biggest empty class. Additional data members
4283 will go at the end of the class. Additional base classes will go
4284 either at offset zero (if empty, in which case they cannot
4285 overlap with offsets past the size of the biggest empty class) or
4286 at the end of the class.
4288 However, if we are placing an empty base class, then we must record
4289 all offsets, as either the empty class is at offset zero (where
4290 other empty classes might later be placed) or at the end of the
4291 class (where other objects might then be placed, so other empty
4292 subobjects might later overlap). */
4294 || !is_empty_class (BINFO_TYPE (type
)))
4295 max_offset
= sizeof_biggest_empty_class
;
4297 max_offset
= NULL_TREE
;
4298 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4299 offsets
, max_offset
, is_data_member
);
4302 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4303 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4304 virtual bases of TYPE are examined. */
4307 layout_conflict_p (tree type
,
4312 splay_tree_node max_node
;
4314 /* Get the node in OFFSETS that indicates the maximum offset where
4315 an empty subobject is located. */
4316 max_node
= splay_tree_max (offsets
);
4317 /* If there aren't any empty subobjects, then there's no point in
4318 performing this check. */
4322 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4323 offsets
, (tree
) (max_node
->key
),
4327 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4328 non-static data member of the type indicated by RLI. BINFO is the
4329 binfo corresponding to the base subobject, OFFSETS maps offsets to
4330 types already located at those offsets. This function determines
4331 the position of the DECL. */
4334 layout_nonempty_base_or_field (record_layout_info rli
,
4339 tree offset
= NULL_TREE
;
4345 /* For the purposes of determining layout conflicts, we want to
4346 use the class type of BINFO; TREE_TYPE (DECL) will be the
4347 CLASSTYPE_AS_BASE version, which does not contain entries for
4348 zero-sized bases. */
4349 type
= TREE_TYPE (binfo
);
4354 type
= TREE_TYPE (decl
);
4358 /* Try to place the field. It may take more than one try if we have
4359 a hard time placing the field without putting two objects of the
4360 same type at the same address. */
4363 struct record_layout_info_s old_rli
= *rli
;
4365 /* Place this field. */
4366 place_field (rli
, decl
);
4367 offset
= byte_position (decl
);
4369 /* We have to check to see whether or not there is already
4370 something of the same type at the offset we're about to use.
4371 For example, consider:
4374 struct T : public S { int i; };
4375 struct U : public S, public T {};
4377 Here, we put S at offset zero in U. Then, we can't put T at
4378 offset zero -- its S component would be at the same address
4379 as the S we already allocated. So, we have to skip ahead.
4380 Since all data members, including those whose type is an
4381 empty class, have nonzero size, any overlap can happen only
4382 with a direct or indirect base-class -- it can't happen with
4384 /* In a union, overlap is permitted; all members are placed at
4386 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4388 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4391 /* Strip off the size allocated to this field. That puts us
4392 at the first place we could have put the field with
4393 proper alignment. */
4396 /* Bump up by the alignment required for the type. */
4398 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4400 ? CLASSTYPE_ALIGN (type
)
4401 : TYPE_ALIGN (type
)));
4402 normalize_rli (rli
);
4404 else if (TREE_CODE (type
) == NULLPTR_TYPE
4405 && warn_abi
&& abi_version_crosses (9))
4407 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4408 the offset wasn't aligned like a pointer when we started to
4409 layout this field, that affects its position. */
4410 tree pos
= rli_size_unit_so_far (&old_rli
);
4411 if (int_cst_value (pos
) % TYPE_ALIGN_UNIT (ptr_type_node
) != 0)
4413 if (abi_version_at_least (9))
4414 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
,
4415 "alignment of %qD increased in -fabi-version=9 "
4418 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wabi
, "alignment "
4419 "of %qD will increase in -fabi-version=9", decl
);
4424 /* There was no conflict. We're done laying out this field. */
4428 /* Now that we know where it will be placed, update its
4430 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4431 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4432 this point because their BINFO_OFFSET is copied from another
4433 hierarchy. Therefore, we may not need to add the entire
4435 propagate_binfo_offsets (binfo
,
4436 size_diffop_loc (input_location
,
4437 fold_convert (ssizetype
, offset
),
4438 fold_convert (ssizetype
,
4439 BINFO_OFFSET (binfo
))));
4442 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4445 empty_base_at_nonzero_offset_p (tree type
,
4447 splay_tree
/*offsets*/)
4449 return is_empty_class (type
) && !integer_zerop (offset
);
4452 /* Layout the empty base BINFO. EOC indicates the byte currently just
4453 past the end of the class, and should be correctly aligned for a
4454 class of the type indicated by BINFO; OFFSETS gives the offsets of
4455 the empty bases allocated so far. T is the most derived
4456 type. Return nonzero iff we added it at the end. */
4459 layout_empty_base (record_layout_info rli
, tree binfo
,
4460 tree eoc
, splay_tree offsets
)
4463 tree basetype
= BINFO_TYPE (binfo
);
4466 /* This routine should only be used for empty classes. */
4467 gcc_assert (is_empty_class (basetype
));
4468 alignment
= ssize_int (CLASSTYPE_ALIGN_UNIT (basetype
));
4470 if (!integer_zerop (BINFO_OFFSET (binfo
)))
4471 propagate_binfo_offsets
4472 (binfo
, size_diffop_loc (input_location
,
4473 size_zero_node
, BINFO_OFFSET (binfo
)));
4475 /* This is an empty base class. We first try to put it at offset
4477 if (layout_conflict_p (binfo
,
4478 BINFO_OFFSET (binfo
),
4482 /* That didn't work. Now, we move forward from the next
4483 available spot in the class. */
4485 propagate_binfo_offsets (binfo
, fold_convert (ssizetype
, eoc
));
4488 if (!layout_conflict_p (binfo
,
4489 BINFO_OFFSET (binfo
),
4492 /* We finally found a spot where there's no overlap. */
4495 /* There's overlap here, too. Bump along to the next spot. */
4496 propagate_binfo_offsets (binfo
, alignment
);
4500 if (CLASSTYPE_USER_ALIGN (basetype
))
4502 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (basetype
));
4504 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (basetype
));
4505 TYPE_USER_ALIGN (rli
->t
) = 1;
4511 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4512 fields at NEXT_FIELD, and return it. */
4515 build_base_field_1 (tree t
, tree basetype
, tree
*&next_field
)
4517 /* Create the FIELD_DECL. */
4518 gcc_assert (CLASSTYPE_AS_BASE (basetype
));
4519 tree decl
= build_decl (input_location
,
4520 FIELD_DECL
, NULL_TREE
, CLASSTYPE_AS_BASE (basetype
));
4521 DECL_ARTIFICIAL (decl
) = 1;
4522 DECL_IGNORED_P (decl
) = 1;
4523 DECL_FIELD_CONTEXT (decl
) = t
;
4524 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4525 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4526 SET_DECL_ALIGN (decl
, CLASSTYPE_ALIGN (basetype
));
4527 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4528 SET_DECL_MODE (decl
, TYPE_MODE (basetype
));
4529 DECL_FIELD_IS_BASE (decl
) = 1;
4531 /* Add the new FIELD_DECL to the list of fields for T. */
4532 DECL_CHAIN (decl
) = *next_field
;
4534 next_field
= &DECL_CHAIN (decl
);
4539 /* Layout the base given by BINFO in the class indicated by RLI.
4540 *BASE_ALIGN is a running maximum of the alignments of
4541 any base class. OFFSETS gives the location of empty base
4542 subobjects. T is the most derived type. Return nonzero if the new
4543 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4544 *NEXT_FIELD, unless BINFO is for an empty base class.
4546 Returns the location at which the next field should be inserted. */
4549 build_base_field (record_layout_info rli
, tree binfo
,
4550 splay_tree offsets
, tree
*next_field
)
4553 tree basetype
= BINFO_TYPE (binfo
);
4555 if (!COMPLETE_TYPE_P (basetype
))
4556 /* This error is now reported in xref_tag, thus giving better
4557 location information. */
4560 /* Place the base class. */
4561 if (!is_empty_class (basetype
))
4565 /* The containing class is non-empty because it has a non-empty
4567 CLASSTYPE_EMPTY_P (t
) = 0;
4569 /* Create the FIELD_DECL. */
4570 decl
= build_base_field_1 (t
, basetype
, next_field
);
4572 /* Try to place the field. It may take more than one try if we
4573 have a hard time placing the field without putting two
4574 objects of the same type at the same address. */
4575 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4582 /* On some platforms (ARM), even empty classes will not be
4584 eoc
= round_up_loc (input_location
,
4585 rli_size_unit_so_far (rli
),
4586 CLASSTYPE_ALIGN_UNIT (basetype
));
4587 atend
= layout_empty_base (rli
, binfo
, eoc
, offsets
);
4588 /* A nearly-empty class "has no proper base class that is empty,
4589 not morally virtual, and at an offset other than zero." */
4590 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4593 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4594 /* The check above (used in G++ 3.2) is insufficient because
4595 an empty class placed at offset zero might itself have an
4596 empty base at a nonzero offset. */
4597 else if (walk_subobject_offsets (basetype
,
4598 empty_base_at_nonzero_offset_p
,
4601 /*max_offset=*/NULL_TREE
,
4603 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4606 /* We used to not create a FIELD_DECL for empty base classes because of
4607 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4608 be a problem anymore. We need them to handle initialization of C++17
4610 if (cxx_dialect
>= cxx1z
&& !BINFO_VIRTUAL_P (binfo
))
4612 tree decl
= build_base_field_1 (t
, basetype
, next_field
);
4613 DECL_FIELD_OFFSET (decl
) = BINFO_OFFSET (binfo
);
4614 DECL_FIELD_BIT_OFFSET (decl
) = bitsize_zero_node
;
4615 SET_DECL_OFFSET_ALIGN (decl
, BITS_PER_UNIT
);
4618 /* An empty virtual base causes a class to be non-empty
4619 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4620 here because that was already done when the virtual table
4621 pointer was created. */
4624 /* Record the offsets of BINFO and its base subobjects. */
4625 record_subobject_offsets (binfo
,
4626 BINFO_OFFSET (binfo
),
4628 /*is_data_member=*/false);
4633 /* Layout all of the non-virtual base classes. Record empty
4634 subobjects in OFFSETS. T is the most derived type. Return nonzero
4635 if the type cannot be nearly empty. The fields created
4636 corresponding to the base classes will be inserted at
4640 build_base_fields (record_layout_info rli
,
4641 splay_tree offsets
, tree
*next_field
)
4643 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4646 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
4649 /* The primary base class is always allocated first. */
4650 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
4651 next_field
= build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (t
),
4652 offsets
, next_field
);
4654 /* Now allocate the rest of the bases. */
4655 for (i
= 0; i
< n_baseclasses
; ++i
)
4659 base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
4661 /* The primary base was already allocated above, so we don't
4662 need to allocate it again here. */
4663 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
4666 /* Virtual bases are added at the end (a primary virtual base
4667 will have already been added). */
4668 if (BINFO_VIRTUAL_P (base_binfo
))
4671 next_field
= build_base_field (rli
, base_binfo
,
4672 offsets
, next_field
);
4676 /* Go through the TYPE_METHODS of T issuing any appropriate
4677 diagnostics, figuring out which methods override which other
4678 methods, and so forth. */
4681 check_methods (tree t
)
4685 for (x
= TYPE_METHODS (t
); x
; x
= DECL_CHAIN (x
))
4687 check_for_override (x
, t
);
4688 if (DECL_PURE_VIRTUAL_P (x
) && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4689 error ("initializer specified for non-virtual method %q+D", x
);
4690 /* The name of the field is the original field name
4691 Save this in auxiliary field for later overloading. */
4692 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4694 TYPE_POLYMORPHIC_P (t
) = 1;
4695 if (DECL_PURE_VIRTUAL_P (x
))
4696 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4698 /* All user-provided destructors are non-trivial.
4699 Constructors and assignment ops are handled in
4700 grok_special_member_properties. */
4701 if (DECL_DESTRUCTOR_P (x
) && user_provided_p (x
))
4702 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 1;
4703 if (!DECL_VIRTUAL_P (x
)
4704 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x
)))
4705 error_at (DECL_SOURCE_LOCATION (x
),
4706 "%<transaction_safe_dynamic%> may only be specified for "
4707 "a virtual function");
4711 /* FN is a constructor or destructor. Clone the declaration to create
4712 a specialized in-charge or not-in-charge version, as indicated by
4716 build_clone (tree fn
, tree name
)
4721 /* Copy the function. */
4722 clone
= copy_decl (fn
);
4723 /* Reset the function name. */
4724 DECL_NAME (clone
) = name
;
4725 /* Remember where this function came from. */
4726 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4727 /* Make it easy to find the CLONE given the FN. */
4728 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4729 DECL_CHAIN (fn
) = clone
;
4731 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4732 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4734 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4735 DECL_TEMPLATE_RESULT (clone
) = result
;
4736 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4737 DECL_TI_TEMPLATE (result
) = clone
;
4738 TREE_TYPE (clone
) = TREE_TYPE (result
);
4743 // Clone constraints.
4745 if (tree ci
= get_constraints (fn
))
4746 set_constraints (clone
, copy_node (ci
));
4750 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4751 DECL_CLONED_FUNCTION (clone
) = fn
;
4752 /* There's no pending inline data for this function. */
4753 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4754 DECL_PENDING_INLINE_P (clone
) = 0;
4756 /* The base-class destructor is not virtual. */
4757 if (name
== base_dtor_identifier
)
4759 DECL_VIRTUAL_P (clone
) = 0;
4760 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4761 DECL_VINDEX (clone
) = NULL_TREE
;
4764 /* If there was an in-charge parameter, drop it from the function
4766 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4772 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4773 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4774 parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4775 /* Skip the `this' parameter. */
4776 parmtypes
= TREE_CHAIN (parmtypes
);
4777 /* Skip the in-charge parameter. */
4778 parmtypes
= TREE_CHAIN (parmtypes
);
4779 /* And the VTT parm, in a complete [cd]tor. */
4780 if (DECL_HAS_VTT_PARM_P (fn
)
4781 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4782 parmtypes
= TREE_CHAIN (parmtypes
);
4783 /* If this is subobject constructor or destructor, add the vtt
4786 = build_method_type_directly (basetype
,
4787 TREE_TYPE (TREE_TYPE (clone
)),
4790 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
),
4793 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4794 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4797 /* Copy the function parameters. */
4798 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4799 /* Remove the in-charge parameter. */
4800 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4802 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4803 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4804 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4806 /* And the VTT parm, in a complete [cd]tor. */
4807 if (DECL_HAS_VTT_PARM_P (fn
))
4809 if (DECL_NEEDS_VTT_PARM_P (clone
))
4810 DECL_HAS_VTT_PARM_P (clone
) = 1;
4813 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4814 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4815 DECL_HAS_VTT_PARM_P (clone
) = 0;
4819 /* A base constructor inheriting from a virtual base doesn't get the
4821 if (ctor_omit_inherited_parms (fn
))
4822 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
))) = NULL_TREE
;
4824 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4826 DECL_CONTEXT (parms
) = clone
;
4827 cxx_dup_lang_specific_decl (parms
);
4830 /* Create the RTL for this function. */
4831 SET_DECL_RTL (clone
, NULL
);
4832 rest_of_decl_compilation (clone
, /*top_level=*/1, at_eof
);
4837 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4838 not invoke this function directly.
4840 For a non-thunk function, returns the address of the slot for storing
4841 the function it is a clone of. Otherwise returns NULL_TREE.
4843 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4844 cloned_function is unset. This is to support the separate
4845 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4846 on a template makes sense, but not the former. */
4849 decl_cloned_function_p (const_tree decl
, bool just_testing
)
4853 decl
= STRIP_TEMPLATE (decl
);
4855 if (TREE_CODE (decl
) != FUNCTION_DECL
4856 || !DECL_LANG_SPECIFIC (decl
)
4857 || DECL_LANG_SPECIFIC (decl
)->u
.fn
.thunk_p
)
4859 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4861 lang_check_failed (__FILE__
, __LINE__
, __FUNCTION__
);
4867 ptr
= &DECL_LANG_SPECIFIC (decl
)->u
.fn
.u5
.cloned_function
;
4868 if (just_testing
&& *ptr
== NULL_TREE
)
4874 /* Produce declarations for all appropriate clones of FN. If
4875 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4876 CLASTYPE_METHOD_VEC as well. */
4879 clone_function_decl (tree fn
, int update_method_vec_p
)
4883 /* Avoid inappropriate cloning. */
4885 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4888 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4890 /* For each constructor, we need two variants: an in-charge version
4891 and a not-in-charge version. */
4892 clone
= build_clone (fn
, complete_ctor_identifier
);
4893 if (update_method_vec_p
)
4894 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4895 clone
= build_clone (fn
, base_ctor_identifier
);
4896 if (update_method_vec_p
)
4897 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4901 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4903 /* For each destructor, we need three variants: an in-charge
4904 version, a not-in-charge version, and an in-charge deleting
4905 version. We clone the deleting version first because that
4906 means it will go second on the TYPE_METHODS list -- and that
4907 corresponds to the correct layout order in the virtual
4910 For a non-virtual destructor, we do not build a deleting
4912 if (DECL_VIRTUAL_P (fn
))
4914 clone
= build_clone (fn
, deleting_dtor_identifier
);
4915 if (update_method_vec_p
)
4916 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4918 clone
= build_clone (fn
, complete_dtor_identifier
);
4919 if (update_method_vec_p
)
4920 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4921 clone
= build_clone (fn
, base_dtor_identifier
);
4922 if (update_method_vec_p
)
4923 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4926 /* Note that this is an abstract function that is never emitted. */
4927 DECL_ABSTRACT_P (fn
) = true;
4930 /* DECL is an in charge constructor, which is being defined. This will
4931 have had an in class declaration, from whence clones were
4932 declared. An out-of-class definition can specify additional default
4933 arguments. As it is the clones that are involved in overload
4934 resolution, we must propagate the information from the DECL to its
4938 adjust_clone_args (tree decl
)
4942 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4943 clone
= DECL_CHAIN (clone
))
4945 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4946 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4947 tree decl_parms
, clone_parms
;
4949 clone_parms
= orig_clone_parms
;
4951 /* Skip the 'this' parameter. */
4952 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4953 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4955 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4956 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4957 if (DECL_HAS_VTT_PARM_P (decl
))
4958 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4960 clone_parms
= orig_clone_parms
;
4961 if (DECL_HAS_VTT_PARM_P (clone
))
4962 clone_parms
= TREE_CHAIN (clone_parms
);
4964 for (decl_parms
= orig_decl_parms
; decl_parms
;
4965 decl_parms
= TREE_CHAIN (decl_parms
),
4966 clone_parms
= TREE_CHAIN (clone_parms
))
4968 gcc_assert (same_type_p (TREE_TYPE (decl_parms
),
4969 TREE_TYPE (clone_parms
)));
4971 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
4973 /* A default parameter has been added. Adjust the
4974 clone's parameters. */
4975 tree exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4976 tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
));
4977 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4980 clone_parms
= orig_decl_parms
;
4982 if (DECL_HAS_VTT_PARM_P (clone
))
4984 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
4985 TREE_VALUE (orig_clone_parms
),
4987 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
4989 type
= build_method_type_directly (basetype
,
4990 TREE_TYPE (TREE_TYPE (clone
)),
4993 type
= build_exception_variant (type
, exceptions
);
4995 type
= cp_build_type_attribute_variant (type
, attrs
);
4996 TREE_TYPE (clone
) = type
;
4998 clone_parms
= NULL_TREE
;
5002 gcc_assert (!clone_parms
);
5006 /* For each of the constructors and destructors in T, create an
5007 in-charge and not-in-charge variant. */
5010 clone_constructors_and_destructors (tree t
)
5014 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5016 if (!CLASSTYPE_METHOD_VEC (t
))
5019 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5020 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
5021 for (fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5022 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
5025 /* Deduce noexcept for a destructor DTOR. */
5028 deduce_noexcept_on_destructor (tree dtor
)
5030 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
5032 tree eh_spec
= unevaluated_noexcept_spec ();
5033 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
), eh_spec
);
5037 /* For each destructor in T, deduce noexcept:
5039 12.4/3: A declaration of a destructor that does not have an
5040 exception-specification is implicitly considered to have the
5041 same exception-specification as an implicit declaration (15.4). */
5044 deduce_noexcept_on_destructors (tree t
)
5046 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5048 if (!CLASSTYPE_METHOD_VEC (t
))
5051 for (tree fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5052 deduce_noexcept_on_destructor (OVL_CURRENT (fns
));
5055 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5056 of TYPE for virtual functions which FNDECL overrides. Return a
5057 mask of the tm attributes found therein. */
5060 look_for_tm_attr_overrides (tree type
, tree fndecl
)
5062 tree binfo
= TYPE_BINFO (type
);
5066 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
5068 tree o
, basetype
= BINFO_TYPE (base_binfo
);
5070 if (!TYPE_POLYMORPHIC_P (basetype
))
5073 o
= look_for_overrides_here (basetype
, fndecl
);
5076 if (lookup_attribute ("transaction_safe_dynamic",
5077 DECL_ATTRIBUTES (o
)))
5078 /* transaction_safe_dynamic is not inherited. */;
5080 found
|= tm_attr_to_mask (find_tm_attribute
5081 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
5084 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
5090 /* Subroutine of set_method_tm_attributes. Handle the checks and
5091 inheritance for one virtual method FNDECL. */
5094 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
5099 found
= look_for_tm_attr_overrides (type
, fndecl
);
5101 /* If FNDECL doesn't actually override anything (i.e. T is the
5102 class that first declares FNDECL virtual), then we're done. */
5106 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
5107 have
= tm_attr_to_mask (tm_attr
);
5109 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5110 tm_pure must match exactly, otherwise no weakening of
5111 tm_safe > tm_callable > nothing. */
5112 /* ??? The tm_pure attribute didn't make the transition to the
5113 multivendor language spec. */
5114 if (have
== TM_ATTR_PURE
)
5116 if (found
!= TM_ATTR_PURE
)
5122 /* If the overridden function is tm_pure, then FNDECL must be. */
5123 else if (found
== TM_ATTR_PURE
&& tm_attr
)
5125 /* Look for base class combinations that cannot be satisfied. */
5126 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
5128 found
&= ~TM_ATTR_PURE
;
5130 error_at (DECL_SOURCE_LOCATION (fndecl
),
5131 "method overrides both %<transaction_pure%> and %qE methods",
5132 tm_mask_to_attr (found
));
5134 /* If FNDECL did not declare an attribute, then inherit the most
5136 else if (tm_attr
== NULL
)
5138 apply_tm_attr (fndecl
, tm_mask_to_attr (least_bit_hwi (found
)));
5140 /* Otherwise validate that we're not weaker than a function
5141 that is being overridden. */
5145 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
5151 error_at (DECL_SOURCE_LOCATION (fndecl
),
5152 "method declared %qE overriding %qE method",
5153 tm_attr
, tm_mask_to_attr (found
));
5156 /* For each of the methods in T, propagate a class-level tm attribute. */
5159 set_method_tm_attributes (tree t
)
5161 tree class_tm_attr
, fndecl
;
5163 /* Don't bother collecting tm attributes if transactional memory
5164 support is not enabled. */
5168 /* Process virtual methods first, as they inherit directly from the
5169 base virtual function and also require validation of new attributes. */
5170 if (TYPE_CONTAINS_VPTR_P (t
))
5173 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
5174 vchain
= TREE_CHAIN (vchain
))
5176 fndecl
= BV_FN (vchain
);
5177 if (DECL_THUNK_P (fndecl
))
5178 fndecl
= THUNK_TARGET (fndecl
);
5179 set_one_vmethod_tm_attributes (t
, fndecl
);
5183 /* If the class doesn't have an attribute, nothing more to do. */
5184 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
5185 if (class_tm_attr
== NULL
)
5188 /* Any method that does not yet have a tm attribute inherits
5189 the one from the class. */
5190 for (fndecl
= TYPE_METHODS (t
); fndecl
; fndecl
= TREE_CHAIN (fndecl
))
5192 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
5193 apply_tm_attr (fndecl
, class_tm_attr
);
5197 /* Returns true if FN is a default constructor. */
5200 default_ctor_p (tree fn
)
5202 return (DECL_CONSTRUCTOR_P (fn
)
5203 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
5206 /* Returns true iff class T has a user-defined constructor that can be called
5207 with more than zero arguments. */
5210 type_has_user_nondefault_constructor (tree t
)
5214 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5217 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5219 tree fn
= OVL_CURRENT (fns
);
5220 if (!DECL_ARTIFICIAL (fn
)
5221 && (TREE_CODE (fn
) == TEMPLATE_DECL
5222 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
5230 /* Returns the defaulted constructor if T has one. Otherwise, returns
5234 in_class_defaulted_default_constructor (tree t
)
5236 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5239 for (tree fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5241 tree fn
= OVL_CURRENT (fns
);
5243 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
5244 && default_ctor_p (fn
))
5251 /* Returns true iff FN is a user-provided function, i.e. user-declared
5252 and not defaulted at its first declaration. */
5255 user_provided_p (tree fn
)
5257 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5260 return (!DECL_ARTIFICIAL (fn
)
5261 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
5262 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
5265 /* Returns true iff class T has a user-provided constructor. */
5268 type_has_user_provided_constructor (tree t
)
5272 if (!CLASS_TYPE_P (t
))
5275 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5278 /* This can happen in error cases; avoid crashing. */
5279 if (!CLASSTYPE_METHOD_VEC (t
))
5282 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5283 if (user_provided_p (OVL_CURRENT (fns
)))
5289 /* Returns true iff class T has a user-provided or explicit constructor. */
5292 type_has_user_provided_or_explicit_constructor (tree t
)
5296 if (!CLASS_TYPE_P (t
))
5299 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5302 /* This can happen in error cases; avoid crashing. */
5303 if (!CLASSTYPE_METHOD_VEC (t
))
5306 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5308 tree fn
= OVL_CURRENT (fns
);
5309 if (user_provided_p (fn
) || DECL_NONCONVERTING_P (fn
))
5316 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5317 declared or explicitly defaulted in the class body) default
5321 type_has_non_user_provided_default_constructor (tree t
)
5325 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5327 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5330 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5332 tree fn
= OVL_CURRENT (fns
);
5333 if (TREE_CODE (fn
) == FUNCTION_DECL
5334 && default_ctor_p (fn
)
5335 && !user_provided_p (fn
))
5342 /* TYPE is being used as a virtual base, and has a non-trivial move
5343 assignment. Return true if this is due to there being a user-provided
5344 move assignment in TYPE or one of its subobjects; if there isn't, then
5345 multiple move assignment can't cause any harm. */
5348 vbase_has_user_provided_move_assign (tree type
)
5350 /* Does the type itself have a user-provided move assignment operator? */
5352 = lookup_fnfields_slot_nolazy (type
, ansi_assopname (NOP_EXPR
));
5353 fns
; fns
= OVL_NEXT (fns
))
5355 tree fn
= OVL_CURRENT (fns
);
5356 if (move_fn_p (fn
) && user_provided_p (fn
))
5360 /* Do any of its bases? */
5361 tree binfo
= TYPE_BINFO (type
);
5363 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5364 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5367 /* Or non-static data members? */
5368 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5370 if (TREE_CODE (field
) == FIELD_DECL
5371 && CLASS_TYPE_P (TREE_TYPE (field
))
5372 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5380 /* If default-initialization leaves part of TYPE uninitialized, returns
5381 a DECL for the field or TYPE itself (DR 253). */
5384 default_init_uninitialized_part (tree type
)
5389 type
= strip_array_types (type
);
5390 if (!CLASS_TYPE_P (type
))
5392 if (!type_has_non_user_provided_default_constructor (type
))
5394 for (binfo
= TYPE_BINFO (type
), i
= 0;
5395 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5397 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5401 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5402 if (TREE_CODE (t
) == FIELD_DECL
5403 && !DECL_ARTIFICIAL (t
)
5404 && !DECL_INITIAL (t
))
5406 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5408 return DECL_P (r
) ? r
: t
;
5414 /* Returns true iff for class T, a trivial synthesized default constructor
5415 would be constexpr. */
5418 trivial_default_constructor_is_constexpr (tree t
)
5420 /* A defaulted trivial default constructor is constexpr
5421 if there is nothing to initialize. */
5422 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5423 return is_really_empty_class (t
);
5426 /* Returns true iff class T has a constexpr default constructor. */
5429 type_has_constexpr_default_constructor (tree t
)
5433 if (!CLASS_TYPE_P (t
))
5435 /* The caller should have stripped an enclosing array. */
5436 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5439 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5441 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5442 return trivial_default_constructor_is_constexpr (t
);
5443 /* Non-trivial, we need to check subobject constructors. */
5444 lazily_declare_fn (sfk_constructor
, t
);
5446 fns
= locate_ctor (t
);
5447 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5450 /* Returns true iff class T has a constexpr default constructor or has an
5451 implicitly declared default constructor that we can't tell if it's constexpr
5452 without forcing a lazy declaration (which might cause undesired
5456 type_maybe_constexpr_default_constructor (tree t
)
5458 if (CLASS_TYPE_P (t
) && CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
5459 && TYPE_HAS_COMPLEX_DFLT (t
))
5460 /* Assume it's constexpr. */
5462 return type_has_constexpr_default_constructor (t
);
5465 /* Returns true iff class TYPE has a virtual destructor. */
5468 type_has_virtual_destructor (tree type
)
5472 if (!CLASS_TYPE_P (type
))
5475 gcc_assert (COMPLETE_TYPE_P (type
));
5476 dtor
= CLASSTYPE_DESTRUCTORS (type
);
5477 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5480 /* Returns true iff class T has a move constructor. */
5483 type_has_move_constructor (tree t
)
5487 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5489 gcc_assert (COMPLETE_TYPE_P (t
));
5490 lazily_declare_fn (sfk_move_constructor
, t
);
5493 if (!CLASSTYPE_METHOD_VEC (t
))
5496 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5497 if (move_fn_p (OVL_CURRENT (fns
)))
5503 /* Returns true iff class T has a move assignment operator. */
5506 type_has_move_assign (tree t
)
5510 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5512 gcc_assert (COMPLETE_TYPE_P (t
));
5513 lazily_declare_fn (sfk_move_assignment
, t
);
5516 for (fns
= lookup_fnfields_slot_nolazy (t
, ansi_assopname (NOP_EXPR
));
5517 fns
; fns
= OVL_NEXT (fns
))
5518 if (move_fn_p (OVL_CURRENT (fns
)))
5524 /* Returns true iff class T has a move constructor that was explicitly
5525 declared in the class body. Note that this is different from
5526 "user-provided", which doesn't include functions that are defaulted in
5530 type_has_user_declared_move_constructor (tree t
)
5534 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5537 if (!CLASSTYPE_METHOD_VEC (t
))
5540 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5542 tree fn
= OVL_CURRENT (fns
);
5543 if (move_fn_p (fn
) && !DECL_ARTIFICIAL (fn
))
5550 /* Returns true iff class T has a move assignment operator that was
5551 explicitly declared in the class body. */
5554 type_has_user_declared_move_assign (tree t
)
5558 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5561 for (fns
= lookup_fnfields_slot_nolazy (t
, ansi_assopname (NOP_EXPR
));
5562 fns
; fns
= OVL_NEXT (fns
))
5564 tree fn
= OVL_CURRENT (fns
);
5565 if (move_fn_p (fn
) && !DECL_ARTIFICIAL (fn
))
5572 /* Nonzero if we need to build up a constructor call when initializing an
5573 object of this class, either because it has a user-declared constructor
5574 or because it doesn't have a default constructor (so we need to give an
5575 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5576 what you care about is whether or not an object can be produced by a
5577 constructor (e.g. so we don't set TREE_READONLY on const variables of
5578 such type); use this function when what you care about is whether or not
5579 to try to call a constructor to create an object. The latter case is
5580 the former plus some cases of constructors that cannot be called. */
5583 type_build_ctor_call (tree t
)
5586 if (TYPE_NEEDS_CONSTRUCTING (t
))
5588 inner
= strip_array_types (t
);
5589 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5591 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5593 if (cxx_dialect
< cxx11
)
5595 /* A user-declared constructor might be private, and a constructor might
5596 be trivial but deleted. */
5597 for (tree fns
= lookup_fnfields_slot (inner
, complete_ctor_identifier
);
5598 fns
; fns
= OVL_NEXT (fns
))
5600 tree fn
= OVL_CURRENT (fns
);
5601 if (!DECL_ARTIFICIAL (fn
)
5602 || DECL_DELETED_FN (fn
))
5608 /* Like type_build_ctor_call, but for destructors. */
5611 type_build_dtor_call (tree t
)
5614 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5616 inner
= strip_array_types (t
);
5617 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5618 || !COMPLETE_TYPE_P (inner
))
5620 if (cxx_dialect
< cxx11
)
5622 /* A user-declared destructor might be private, and a destructor might
5623 be trivial but deleted. */
5624 for (tree fns
= lookup_fnfields_slot (inner
, complete_dtor_identifier
);
5625 fns
; fns
= OVL_NEXT (fns
))
5627 tree fn
= OVL_CURRENT (fns
);
5628 if (!DECL_ARTIFICIAL (fn
)
5629 || DECL_DELETED_FN (fn
))
5635 /* Remove all zero-width bit-fields from T. */
5638 remove_zero_width_bit_fields (tree t
)
5642 fieldsp
= &TYPE_FIELDS (t
);
5645 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5646 && DECL_C_BIT_FIELD (*fieldsp
)
5647 /* We should not be confused by the fact that grokbitfield
5648 temporarily sets the width of the bit field into
5649 DECL_INITIAL (*fieldsp).
5650 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5652 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5653 || integer_zerop (DECL_SIZE (*fieldsp
))))
5654 *fieldsp
= DECL_CHAIN (*fieldsp
);
5656 fieldsp
= &DECL_CHAIN (*fieldsp
);
5660 /* Returns TRUE iff we need a cookie when dynamically allocating an
5661 array whose elements have the indicated class TYPE. */
5664 type_requires_array_cookie (tree type
)
5667 bool has_two_argument_delete_p
= false;
5669 gcc_assert (CLASS_TYPE_P (type
));
5671 /* If there's a non-trivial destructor, we need a cookie. In order
5672 to iterate through the array calling the destructor for each
5673 element, we'll have to know how many elements there are. */
5674 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5677 /* If the usual deallocation function is a two-argument whose second
5678 argument is of type `size_t', then we have to pass the size of
5679 the array to the deallocation function, so we will need to store
5681 fns
= lookup_fnfields (TYPE_BINFO (type
),
5682 ansi_opname (VEC_DELETE_EXPR
),
5684 /* If there are no `operator []' members, or the lookup is
5685 ambiguous, then we don't need a cookie. */
5686 if (!fns
|| fns
== error_mark_node
)
5688 /* Loop through all of the functions. */
5689 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
5694 /* Select the current function. */
5695 fn
= OVL_CURRENT (fns
);
5696 /* See if this function is a one-argument delete function. If
5697 it is, then it will be the usual deallocation function. */
5698 second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5699 if (second_parm
== void_list_node
)
5701 /* Do not consider this function if its second argument is an
5705 /* Otherwise, if we have a two-argument function and the second
5706 argument is `size_t', it will be the usual deallocation
5707 function -- unless there is one-argument function, too. */
5708 if (TREE_CHAIN (second_parm
) == void_list_node
5709 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5710 has_two_argument_delete_p
= true;
5713 return has_two_argument_delete_p
;
5716 /* Finish computing the `literal type' property of class type T.
5718 At this point, we have already processed base classes and
5719 non-static data members. We need to check whether the copy
5720 constructor is trivial, the destructor is trivial, and there
5721 is a trivial default constructor or at least one constexpr
5722 constructor other than the copy constructor. */
5725 finalize_literal_type_property (tree t
)
5729 if (cxx_dialect
< cxx11
5730 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5731 CLASSTYPE_LITERAL_P (t
) = false;
5732 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5733 && CLASSTYPE_NON_AGGREGATE (t
)
5734 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5735 CLASSTYPE_LITERAL_P (t
) = false;
5737 if (!CLASSTYPE_LITERAL_P (t
))
5738 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5739 if (DECL_DECLARED_CONSTEXPR_P (fn
)
5740 && TREE_CODE (fn
) != TEMPLATE_DECL
5741 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5742 && !DECL_CONSTRUCTOR_P (fn
))
5744 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5745 if (!DECL_GENERATED_P (fn
) && !LAMBDA_TYPE_P (t
))
5747 error ("enclosing class of constexpr non-static member "
5748 "function %q+#D is not a literal type", fn
);
5749 explain_non_literal_class (t
);
5754 /* T is a non-literal type used in a context which requires a constant
5755 expression. Explain why it isn't literal. */
5758 explain_non_literal_class (tree t
)
5760 static hash_set
<tree
> *diagnosed
;
5762 if (!CLASS_TYPE_P (t
))
5764 t
= TYPE_MAIN_VARIANT (t
);
5766 if (diagnosed
== NULL
)
5767 diagnosed
= new hash_set
<tree
>;
5768 if (diagnosed
->add (t
))
5769 /* Already explained. */
5772 inform (0, "%q+T is not literal because:", t
);
5773 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5774 inform (0, " %q+T has a non-trivial destructor", t
);
5775 else if (CLASSTYPE_NON_AGGREGATE (t
)
5776 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5777 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5779 inform (0, " %q+T is not an aggregate, does not have a trivial "
5780 "default constructor, and has no constexpr constructor that "
5781 "is not a copy or move constructor", t
);
5782 if (type_has_non_user_provided_default_constructor (t
))
5784 /* Note that we can't simply call locate_ctor because when the
5785 constructor is deleted it just returns NULL_TREE. */
5787 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5789 tree fn
= OVL_CURRENT (fns
);
5790 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5792 parms
= skip_artificial_parms_for (fn
, parms
);
5794 if (sufficient_parms_p (parms
))
5796 if (DECL_DELETED_FN (fn
))
5797 maybe_explain_implicit_delete (fn
);
5799 explain_invalid_constexpr_fn (fn
);
5807 tree binfo
, base_binfo
, field
; int i
;
5808 for (binfo
= TYPE_BINFO (t
), i
= 0;
5809 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5811 tree basetype
= TREE_TYPE (base_binfo
);
5812 if (!CLASSTYPE_LITERAL_P (basetype
))
5814 inform (0, " base class %qT of %q+T is non-literal",
5816 explain_non_literal_class (basetype
);
5820 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5823 if (TREE_CODE (field
) != FIELD_DECL
)
5825 ftype
= TREE_TYPE (field
);
5826 if (!literal_type_p (ftype
))
5828 inform (DECL_SOURCE_LOCATION (field
),
5829 " non-static data member %qD has non-literal type",
5831 if (CLASS_TYPE_P (ftype
))
5832 explain_non_literal_class (ftype
);
5834 if (CP_TYPE_VOLATILE_P (ftype
))
5835 inform (DECL_SOURCE_LOCATION (field
),
5836 " non-static data member %qD has volatile type", field
);
5841 /* Check the validity of the bases and members declared in T. Add any
5842 implicitly-generated functions (like copy-constructors and
5843 assignment operators). Compute various flag bits (like
5844 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5845 level: i.e., independently of the ABI in use. */
5848 check_bases_and_members (tree t
)
5850 /* Nonzero if the implicitly generated copy constructor should take
5851 a non-const reference argument. */
5852 int cant_have_const_ctor
;
5853 /* Nonzero if the implicitly generated assignment operator
5854 should take a non-const reference argument. */
5855 int no_const_asn_ref
;
5857 bool saved_complex_asn_ref
;
5858 bool saved_nontrivial_dtor
;
5861 /* By default, we use const reference arguments and generate default
5863 cant_have_const_ctor
= 0;
5864 no_const_asn_ref
= 0;
5866 /* Check all the base-classes and set FMEM members to point to arrays
5867 of potential interest. */
5868 check_bases (t
, &cant_have_const_ctor
, &no_const_asn_ref
);
5870 /* Deduce noexcept on destructors. This needs to happen after we've set
5871 triviality flags appropriately for our bases. */
5872 if (cxx_dialect
>= cxx11
)
5873 deduce_noexcept_on_destructors (t
);
5875 /* Check all the method declarations. */
5878 /* Save the initial values of these flags which only indicate whether
5879 or not the class has user-provided functions. As we analyze the
5880 bases and members we can set these flags for other reasons. */
5881 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5882 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5884 /* Check all the data member declarations. We cannot call
5885 check_field_decls until we have called check_bases check_methods,
5886 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5887 being set appropriately. */
5888 check_field_decls (t
, &access_decls
,
5889 &cant_have_const_ctor
,
5892 /* A nearly-empty class has to be vptr-containing; a nearly empty
5893 class contains just a vptr. */
5894 if (!TYPE_CONTAINS_VPTR_P (t
))
5895 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5897 /* Do some bookkeeping that will guide the generation of implicitly
5898 declared member functions. */
5899 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5900 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5901 /* We need to call a constructor for this class if it has a
5902 user-provided constructor, or if the default constructor is going
5903 to initialize the vptr. (This is not an if-and-only-if;
5904 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5905 themselves need constructing.) */
5906 TYPE_NEEDS_CONSTRUCTING (t
)
5907 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5910 An aggregate is an array or a class with no user-provided
5911 constructors ... and no virtual functions.
5913 Again, other conditions for being an aggregate are checked
5915 CLASSTYPE_NON_AGGREGATE (t
)
5916 |= (type_has_user_provided_or_explicit_constructor (t
)
5917 || TYPE_POLYMORPHIC_P (t
));
5918 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5919 retain the old definition internally for ABI reasons. */
5920 CLASSTYPE_NON_LAYOUT_POD_P (t
)
5921 |= (CLASSTYPE_NON_AGGREGATE (t
)
5922 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
5923 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5924 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5925 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5926 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5928 /* If the only explicitly declared default constructor is user-provided,
5929 set TYPE_HAS_COMPLEX_DFLT. */
5930 if (!TYPE_HAS_COMPLEX_DFLT (t
)
5931 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
5932 && !type_has_non_user_provided_default_constructor (t
))
5933 TYPE_HAS_COMPLEX_DFLT (t
) = true;
5935 /* Warn if a public base of a polymorphic type has an accessible
5936 non-virtual destructor. It is only now that we know the class is
5937 polymorphic. Although a polymorphic base will have a already
5938 been diagnosed during its definition, we warn on use too. */
5939 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
5941 tree binfo
= TYPE_BINFO (t
);
5942 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
5946 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5948 tree basetype
= TREE_TYPE (base_binfo
);
5950 if ((*accesses
)[i
] == access_public_node
5951 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
5952 && accessible_nvdtor_p (basetype
))
5953 warning (OPT_Wnon_virtual_dtor
,
5954 "base class %q#T has accessible non-virtual destructor",
5959 /* If the class has no user-declared constructor, but does have
5960 non-static const or reference data members that can never be
5961 initialized, issue a warning. */
5962 if (warn_uninitialized
5963 /* Classes with user-declared constructors are presumed to
5964 initialize these members. */
5965 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
5966 /* Aggregates can be initialized with brace-enclosed
5968 && CLASSTYPE_NON_AGGREGATE (t
))
5972 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5976 if (TREE_CODE (field
) != FIELD_DECL
5977 || DECL_INITIAL (field
) != NULL_TREE
)
5980 type
= TREE_TYPE (field
);
5981 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5982 warning_at (DECL_SOURCE_LOCATION (field
),
5983 OPT_Wuninitialized
, "non-static reference %q#D "
5984 "in class without a constructor", field
);
5985 else if (CP_TYPE_CONST_P (type
)
5986 && (!CLASS_TYPE_P (type
)
5987 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
5988 warning_at (DECL_SOURCE_LOCATION (field
),
5989 OPT_Wuninitialized
, "non-static const member %q#D "
5990 "in class without a constructor", field
);
5994 /* Synthesize any needed methods. */
5995 add_implicitly_declared_members (t
, &access_decls
,
5996 cant_have_const_ctor
,
5999 /* Check defaulted declarations here so we have cant_have_const_ctor
6000 and don't need to worry about clones. */
6001 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
6002 if (!DECL_ARTIFICIAL (fn
) && DECL_DEFAULTED_IN_CLASS_P (fn
))
6004 int copy
= copy_fn_p (fn
);
6008 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
6009 : !no_const_asn_ref
);
6010 bool fn_const_p
= (copy
== 2);
6012 if (fn_const_p
&& !imp_const_p
)
6013 /* If the function is defaulted outside the class, we just
6014 give the synthesis error. */
6015 error ("%q+D declared to take const reference, but implicit "
6016 "declaration would take non-const", fn
);
6018 defaulted_late_check (fn
);
6021 if (LAMBDA_TYPE_P (t
))
6023 /* "This class type is not an aggregate." */
6024 CLASSTYPE_NON_AGGREGATE (t
) = 1;
6027 /* Compute the 'literal type' property before we
6028 do anything with non-static member functions. */
6029 finalize_literal_type_property (t
);
6031 /* Create the in-charge and not-in-charge variants of constructors
6033 clone_constructors_and_destructors (t
);
6035 /* Process the using-declarations. */
6036 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
6037 handle_using_decl (TREE_VALUE (access_decls
), t
);
6039 /* Build and sort the CLASSTYPE_METHOD_VEC. */
6040 finish_struct_methods (t
);
6042 /* Figure out whether or not we will need a cookie when dynamically
6043 allocating an array of this type. */
6044 TYPE_LANG_SPECIFIC (t
)->u
.c
.vec_new_uses_cookie
6045 = type_requires_array_cookie (t
);
6048 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6049 accordingly. If a new vfield was created (because T doesn't have a
6050 primary base class), then the newly created field is returned. It
6051 is not added to the TYPE_FIELDS list; it is the caller's
6052 responsibility to do that. Accumulate declared virtual functions
6056 create_vtable_ptr (tree t
, tree
* virtuals_p
)
6060 /* Collect the virtual functions declared in T. */
6061 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
6062 if (TREE_CODE (fn
) == FUNCTION_DECL
6063 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
6064 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
6066 tree new_virtual
= make_node (TREE_LIST
);
6068 BV_FN (new_virtual
) = fn
;
6069 BV_DELTA (new_virtual
) = integer_zero_node
;
6070 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
6072 TREE_CHAIN (new_virtual
) = *virtuals_p
;
6073 *virtuals_p
= new_virtual
;
6076 /* If we couldn't find an appropriate base class, create a new field
6077 here. Even if there weren't any new virtual functions, we might need a
6078 new virtual function table if we're supposed to include vptrs in
6079 all classes that need them. */
6080 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
6082 /* We build this decl with vtbl_ptr_type_node, which is a
6083 `vtable_entry_type*'. It might seem more precise to use
6084 `vtable_entry_type (*)[N]' where N is the number of virtual
6085 functions. However, that would require the vtable pointer in
6086 base classes to have a different type than the vtable pointer
6087 in derived classes. We could make that happen, but that
6088 still wouldn't solve all the problems. In particular, the
6089 type-based alias analysis code would decide that assignments
6090 to the base class vtable pointer can't alias assignments to
6091 the derived class vtable pointer, since they have different
6092 types. Thus, in a derived class destructor, where the base
6093 class constructor was inlined, we could generate bad code for
6094 setting up the vtable pointer.
6096 Therefore, we use one type for all vtable pointers. We still
6097 use a type-correct type; it's just doesn't indicate the array
6098 bounds. That's better than using `void*' or some such; it's
6099 cleaner, and it let's the alias analysis code know that these
6100 stores cannot alias stores to void*! */
6103 field
= build_decl (input_location
,
6104 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
6105 DECL_VIRTUAL_P (field
) = 1;
6106 DECL_ARTIFICIAL (field
) = 1;
6107 DECL_FIELD_CONTEXT (field
) = t
;
6108 DECL_FCONTEXT (field
) = t
;
6109 if (TYPE_PACKED (t
))
6110 DECL_PACKED (field
) = 1;
6112 TYPE_VFIELD (t
) = field
;
6114 /* This class is non-empty. */
6115 CLASSTYPE_EMPTY_P (t
) = 0;
6123 /* Add OFFSET to all base types of BINFO which is a base in the
6124 hierarchy dominated by T.
6126 OFFSET, which is a type offset, is number of bytes. */
6129 propagate_binfo_offsets (tree binfo
, tree offset
)
6135 /* Update BINFO's offset. */
6136 BINFO_OFFSET (binfo
)
6137 = fold_convert (sizetype
,
6138 size_binop (PLUS_EXPR
,
6139 fold_convert (ssizetype
, BINFO_OFFSET (binfo
)),
6142 /* Find the primary base class. */
6143 primary_binfo
= get_primary_binfo (binfo
);
6145 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
6146 propagate_binfo_offsets (primary_binfo
, offset
);
6148 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6150 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6152 /* Don't do the primary base twice. */
6153 if (base_binfo
== primary_binfo
)
6156 if (BINFO_VIRTUAL_P (base_binfo
))
6159 propagate_binfo_offsets (base_binfo
, offset
);
6163 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6164 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6165 empty subobjects of T. */
6168 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
6174 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
6177 /* Find the last field. The artificial fields created for virtual
6178 bases will go after the last extant field to date. */
6179 next_field
= &TYPE_FIELDS (t
);
6181 next_field
= &DECL_CHAIN (*next_field
);
6183 /* Go through the virtual bases, allocating space for each virtual
6184 base that is not already a primary base class. These are
6185 allocated in inheritance graph order. */
6186 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6188 if (!BINFO_VIRTUAL_P (vbase
))
6191 if (!BINFO_PRIMARY_P (vbase
))
6193 /* This virtual base is not a primary base of any class in the
6194 hierarchy, so we have to add space for it. */
6195 next_field
= build_base_field (rli
, vbase
,
6196 offsets
, next_field
);
6201 /* Returns the offset of the byte just past the end of the base class
6205 end_of_base (tree binfo
)
6209 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
6210 size
= TYPE_SIZE_UNIT (char_type_node
);
6211 else if (is_empty_class (BINFO_TYPE (binfo
)))
6212 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6213 allocate some space for it. It cannot have virtual bases, so
6214 TYPE_SIZE_UNIT is fine. */
6215 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6217 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
6219 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
6222 /* Returns the offset of the byte just past the end of the base class
6223 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6224 only non-virtual bases are included. */
6227 end_of_class (tree t
, int include_virtuals_p
)
6229 tree result
= size_zero_node
;
6230 vec
<tree
, va_gc
> *vbases
;
6236 for (binfo
= TYPE_BINFO (t
), i
= 0;
6237 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6239 if (!include_virtuals_p
6240 && BINFO_VIRTUAL_P (base_binfo
)
6241 && (!BINFO_PRIMARY_P (base_binfo
)
6242 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
6245 offset
= end_of_base (base_binfo
);
6246 if (tree_int_cst_lt (result
, offset
))
6250 if (include_virtuals_p
)
6251 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6252 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6254 offset
= end_of_base (base_binfo
);
6255 if (tree_int_cst_lt (result
, offset
))
6262 /* Warn about bases of T that are inaccessible because they are
6263 ambiguous. For example:
6266 struct T : public S {};
6267 struct U : public S, public T {};
6269 Here, `(S*) new U' is not allowed because there are two `S'
6273 warn_about_ambiguous_bases (tree t
)
6276 vec
<tree
, va_gc
> *vbases
;
6281 /* If there are no repeated bases, nothing can be ambiguous. */
6282 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6285 /* Check direct bases. */
6286 for (binfo
= TYPE_BINFO (t
), i
= 0;
6287 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6289 basetype
= BINFO_TYPE (base_binfo
);
6291 if (!uniquely_derived_from_p (basetype
, t
))
6292 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6296 /* Check for ambiguous virtual bases. */
6298 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6299 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6301 basetype
= BINFO_TYPE (binfo
);
6303 if (!uniquely_derived_from_p (basetype
, t
))
6304 warning (OPT_Wextra
, "virtual base %qT inaccessible in %qT due "
6305 "to ambiguity", basetype
, t
);
6309 /* Compare two INTEGER_CSTs K1 and K2. */
6312 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6314 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6317 /* Increase the size indicated in RLI to account for empty classes
6318 that are "off the end" of the class. */
6321 include_empty_classes (record_layout_info rli
)
6326 /* It might be the case that we grew the class to allocate a
6327 zero-sized base class. That won't be reflected in RLI, yet,
6328 because we are willing to overlay multiple bases at the same
6329 offset. However, now we need to make sure that RLI is big enough
6330 to reflect the entire class. */
6331 eoc
= end_of_class (rli
->t
,
6332 CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6333 rli_size
= rli_size_unit_so_far (rli
);
6334 if (TREE_CODE (rli_size
) == INTEGER_CST
6335 && tree_int_cst_lt (rli_size
, eoc
))
6337 /* The size should have been rounded to a whole byte. */
6338 gcc_assert (tree_int_cst_equal
6339 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6341 = size_binop (PLUS_EXPR
,
6343 size_binop (MULT_EXPR
,
6344 fold_convert (bitsizetype
,
6345 size_binop (MINUS_EXPR
,
6347 bitsize_int (BITS_PER_UNIT
)));
6348 normalize_rli (rli
);
6352 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6353 BINFO_OFFSETs for all of the base-classes. Position the vtable
6354 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6357 layout_class_type (tree t
, tree
*virtuals_p
)
6359 tree non_static_data_members
;
6362 record_layout_info rli
;
6363 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6364 types that appear at that offset. */
6365 splay_tree empty_base_offsets
;
6366 /* True if the last field laid out was a bit-field. */
6367 bool last_field_was_bitfield
= false;
6368 /* The location at which the next field should be inserted. */
6370 /* T, as a base class. */
6373 /* Keep track of the first non-static data member. */
6374 non_static_data_members
= TYPE_FIELDS (t
);
6376 /* Start laying out the record. */
6377 rli
= start_record_layout (t
);
6379 /* Mark all the primary bases in the hierarchy. */
6380 determine_primary_bases (t
);
6382 /* Create a pointer to our virtual function table. */
6383 vptr
= create_vtable_ptr (t
, virtuals_p
);
6385 /* The vptr is always the first thing in the class. */
6388 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6389 TYPE_FIELDS (t
) = vptr
;
6390 next_field
= &DECL_CHAIN (vptr
);
6391 place_field (rli
, vptr
);
6394 next_field
= &TYPE_FIELDS (t
);
6396 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6397 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6399 build_base_fields (rli
, empty_base_offsets
, next_field
);
6401 /* Layout the non-static data members. */
6402 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6407 /* We still pass things that aren't non-static data members to
6408 the back end, in case it wants to do something with them. */
6409 if (TREE_CODE (field
) != FIELD_DECL
)
6411 place_field (rli
, field
);
6412 /* If the static data member has incomplete type, keep track
6413 of it so that it can be completed later. (The handling
6414 of pending statics in finish_record_layout is
6415 insufficient; consider:
6418 struct S2 { static S1 s1; };
6420 At this point, finish_record_layout will be called, but
6421 S1 is still incomplete.) */
6424 maybe_register_incomplete_var (field
);
6425 /* The visibility of static data members is determined
6426 at their point of declaration, not their point of
6428 determine_visibility (field
);
6433 type
= TREE_TYPE (field
);
6434 if (type
== error_mark_node
)
6437 padding
= NULL_TREE
;
6439 /* If this field is a bit-field whose width is greater than its
6440 type, then there are some special rules for allocating
6442 if (DECL_C_BIT_FIELD (field
)
6443 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6447 bool was_unnamed_p
= false;
6448 /* We must allocate the bits as if suitably aligned for the
6449 longest integer type that fits in this many bits. type
6450 of the field. Then, we are supposed to use the left over
6451 bits as additional padding. */
6452 for (itk
= itk_char
; itk
!= itk_none
; ++itk
)
6453 if (integer_types
[itk
] != NULL_TREE
6454 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE
),
6455 TYPE_SIZE (integer_types
[itk
]))
6456 || tree_int_cst_lt (DECL_SIZE (field
),
6457 TYPE_SIZE (integer_types
[itk
]))))
6460 /* ITK now indicates a type that is too large for the
6461 field. We have to back up by one to find the largest
6466 integer_type
= integer_types
[itk
];
6467 } while (itk
> 0 && integer_type
== NULL_TREE
);
6469 /* Figure out how much additional padding is required. */
6470 if (tree_int_cst_lt (TYPE_SIZE (integer_type
), DECL_SIZE (field
)))
6472 if (TREE_CODE (t
) == UNION_TYPE
)
6473 /* In a union, the padding field must have the full width
6474 of the bit-field; all fields start at offset zero. */
6475 padding
= DECL_SIZE (field
);
6477 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6478 TYPE_SIZE (integer_type
));
6481 /* An unnamed bitfield does not normally affect the
6482 alignment of the containing class on a target where
6483 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6484 make any exceptions for unnamed bitfields when the
6485 bitfields are longer than their types. Therefore, we
6486 temporarily give the field a name. */
6487 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6489 was_unnamed_p
= true;
6490 DECL_NAME (field
) = make_anon_name ();
6493 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6494 SET_DECL_ALIGN (field
, TYPE_ALIGN (integer_type
));
6495 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6496 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6497 empty_base_offsets
);
6499 DECL_NAME (field
) = NULL_TREE
;
6500 /* Now that layout has been performed, set the size of the
6501 field to the size of its declared type; the rest of the
6502 field is effectively invisible. */
6503 DECL_SIZE (field
) = TYPE_SIZE (type
);
6504 /* We must also reset the DECL_MODE of the field. */
6505 SET_DECL_MODE (field
, TYPE_MODE (type
));
6508 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6509 empty_base_offsets
);
6511 /* Remember the location of any empty classes in FIELD. */
6512 record_subobject_offsets (TREE_TYPE (field
),
6513 byte_position(field
),
6515 /*is_data_member=*/true);
6517 /* If a bit-field does not immediately follow another bit-field,
6518 and yet it starts in the middle of a byte, we have failed to
6519 comply with the ABI. */
6521 && DECL_C_BIT_FIELD (field
)
6522 /* The TREE_NO_WARNING flag gets set by Objective-C when
6523 laying out an Objective-C class. The ObjC ABI differs
6524 from the C++ ABI, and so we do not want a warning
6526 && !TREE_NO_WARNING (field
)
6527 && !last_field_was_bitfield
6528 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6529 DECL_FIELD_BIT_OFFSET (field
),
6530 bitsize_unit_node
)))
6531 warning_at (DECL_SOURCE_LOCATION (field
), OPT_Wabi
,
6532 "offset of %qD is not ABI-compliant and may "
6533 "change in a future version of GCC", field
);
6535 /* The middle end uses the type of expressions to determine the
6536 possible range of expression values. In order to optimize
6537 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6538 must be made aware of the width of "i", via its type.
6540 Because C++ does not have integer types of arbitrary width,
6541 we must (for the purposes of the front end) convert from the
6542 type assigned here to the declared type of the bitfield
6543 whenever a bitfield expression is used as an rvalue.
6544 Similarly, when assigning a value to a bitfield, the value
6545 must be converted to the type given the bitfield here. */
6546 if (DECL_C_BIT_FIELD (field
))
6548 unsigned HOST_WIDE_INT width
;
6549 tree ftype
= TREE_TYPE (field
);
6550 width
= tree_to_uhwi (DECL_SIZE (field
));
6551 if (width
!= TYPE_PRECISION (ftype
))
6554 = c_build_bitfield_integer_type (width
,
6555 TYPE_UNSIGNED (ftype
));
6557 = cp_build_qualified_type (TREE_TYPE (field
),
6558 cp_type_quals (ftype
));
6562 /* If we needed additional padding after this field, add it
6568 padding_field
= build_decl (input_location
,
6572 DECL_BIT_FIELD (padding_field
) = 1;
6573 DECL_SIZE (padding_field
) = padding
;
6574 DECL_CONTEXT (padding_field
) = t
;
6575 DECL_ARTIFICIAL (padding_field
) = 1;
6576 DECL_IGNORED_P (padding_field
) = 1;
6577 layout_nonempty_base_or_field (rli
, padding_field
,
6579 empty_base_offsets
);
6582 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6585 if (!integer_zerop (rli
->bitpos
))
6587 /* Make sure that we are on a byte boundary so that the size of
6588 the class without virtual bases will always be a round number
6590 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6591 normalize_rli (rli
);
6594 /* Delete all zero-width bit-fields from the list of fields. Now
6595 that the type is laid out they are no longer important. */
6596 remove_zero_width_bit_fields (t
);
6598 /* Create the version of T used for virtual bases. We do not use
6599 make_class_type for this version; this is an artificial type. For
6600 a POD type, we just reuse T. */
6601 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6603 base_t
= make_node (TREE_CODE (t
));
6605 /* Set the size and alignment for the new type. */
6608 /* If the ABI version is not at least two, and the last
6609 field was a bit-field, RLI may not be on a byte
6610 boundary. In particular, rli_size_unit_so_far might
6611 indicate the last complete byte, while rli_size_so_far
6612 indicates the total number of bits used. Therefore,
6613 rli_size_so_far, rather than rli_size_unit_so_far, is
6614 used to compute TYPE_SIZE_UNIT. */
6615 eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6616 TYPE_SIZE_UNIT (base_t
)
6617 = size_binop (MAX_EXPR
,
6618 fold_convert (sizetype
,
6619 size_binop (CEIL_DIV_EXPR
,
6620 rli_size_so_far (rli
),
6621 bitsize_int (BITS_PER_UNIT
))),
6624 = size_binop (MAX_EXPR
,
6625 rli_size_so_far (rli
),
6626 size_binop (MULT_EXPR
,
6627 fold_convert (bitsizetype
, eoc
),
6628 bitsize_int (BITS_PER_UNIT
)));
6629 SET_TYPE_ALIGN (base_t
, rli
->record_align
);
6630 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6632 /* Copy the fields from T. */
6633 next_field
= &TYPE_FIELDS (base_t
);
6634 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6635 if (TREE_CODE (field
) == FIELD_DECL
)
6637 *next_field
= copy_node (field
);
6638 DECL_CONTEXT (*next_field
) = base_t
;
6639 next_field
= &DECL_CHAIN (*next_field
);
6641 *next_field
= NULL_TREE
;
6643 /* Record the base version of the type. */
6644 CLASSTYPE_AS_BASE (t
) = base_t
;
6645 TYPE_CONTEXT (base_t
) = t
;
6648 CLASSTYPE_AS_BASE (t
) = t
;
6650 /* Every empty class contains an empty class. */
6651 if (CLASSTYPE_EMPTY_P (t
))
6652 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6654 /* Set the TYPE_DECL for this type to contain the right
6655 value for DECL_OFFSET, so that we can use it as part
6656 of a COMPONENT_REF for multiple inheritance. */
6657 layout_decl (TYPE_MAIN_DECL (t
), 0);
6659 /* Now fix up any virtual base class types that we left lying
6660 around. We must get these done before we try to lay out the
6661 virtual function table. As a side-effect, this will remove the
6662 base subobject fields. */
6663 layout_virtual_bases (rli
, empty_base_offsets
);
6665 /* Make sure that empty classes are reflected in RLI at this
6667 include_empty_classes (rli
);
6669 /* Make sure not to create any structures with zero size. */
6670 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6672 build_decl (input_location
,
6673 FIELD_DECL
, NULL_TREE
, char_type_node
));
6675 /* If this is a non-POD, declaring it packed makes a difference to how it
6676 can be used as a field; don't let finalize_record_size undo it. */
6677 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6678 rli
->packed_maybe_necessary
= true;
6680 /* Let the back end lay out the type. */
6681 finish_record_layout (rli
, /*free_p=*/true);
6683 if (TYPE_SIZE_UNIT (t
)
6684 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6685 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6686 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6687 error ("size of type %qT is too large (%qE bytes)", t
, TYPE_SIZE_UNIT (t
));
6689 /* Warn about bases that can't be talked about due to ambiguity. */
6690 warn_about_ambiguous_bases (t
);
6692 /* Now that we're done with layout, give the base fields the real types. */
6693 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6694 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6695 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6698 splay_tree_delete (empty_base_offsets
);
6700 if (CLASSTYPE_EMPTY_P (t
)
6701 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6702 TYPE_SIZE_UNIT (t
)))
6703 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6706 /* Determine the "key method" for the class type indicated by TYPE,
6707 and set CLASSTYPE_KEY_METHOD accordingly. */
6710 determine_key_method (tree type
)
6714 if (processing_template_decl
6715 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6716 || CLASSTYPE_INTERFACE_KNOWN (type
))
6719 /* The key method is the first non-pure virtual function that is not
6720 inline at the point of class definition. On some targets the
6721 key function may not be inline; those targets should not call
6722 this function until the end of the translation unit. */
6723 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
6724 method
= DECL_CHAIN (method
))
6725 if (TREE_CODE (method
) == FUNCTION_DECL
6726 && DECL_VINDEX (method
) != NULL_TREE
6727 && ! DECL_DECLARED_INLINE_P (method
)
6728 && ! DECL_PURE_VIRTUAL_P (method
))
6730 CLASSTYPE_KEY_METHOD (type
) = method
;
6738 /* Allocate and return an instance of struct sorted_fields_type with
6741 static struct sorted_fields_type
*
6742 sorted_fields_type_new (int n
)
6744 struct sorted_fields_type
*sft
;
6745 sft
= (sorted_fields_type
*) ggc_internal_alloc (sizeof (sorted_fields_type
)
6746 + n
* sizeof (tree
));
6752 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6753 class data member of non-zero size, otherwise false. */
6756 field_nonempty_p (const_tree fld
)
6758 if (TREE_CODE (fld
) == ERROR_MARK
)
6761 tree type
= TREE_TYPE (fld
);
6762 if (TREE_CODE (fld
) == FIELD_DECL
6763 && TREE_CODE (type
) != ERROR_MARK
6764 && (DECL_NAME (fld
) || RECORD_OR_UNION_TYPE_P (type
)))
6766 return TYPE_SIZE (type
)
6767 && (TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
6768 || !tree_int_cst_equal (size_zero_node
, TYPE_SIZE (type
)));
6774 /* Used by find_flexarrays and related functions. */
6778 /* The first flexible array member or non-zero array member found
6779 in the order of layout. */
6781 /* First non-static non-empty data member in the class or its bases. */
6783 /* The first non-static non-empty data member following either
6784 the flexible array member, if found, or the zero-length array member
6785 otherwise. AFTER[1] refers to the first such data member of a union
6786 of which the struct containing the flexible array member or zero-length
6787 array is a member, or NULL when no such union exists. This element is
6788 only used during searching, not for diagnosing problems. AFTER[0]
6789 refers to the first such data member that is not a member of such
6793 /* Refers to a struct (not union) in which the struct of which the flexible
6794 array is member is defined. Used to diagnose strictly (according to C)
6795 invalid uses of the latter structs. */
6799 /* Find either the first flexible array member or the first zero-length
6800 array, in that order of preference, among members of class T (but not
6801 its base classes), and set members of FMEM accordingly.
6802 BASE_P is true if T is a base class of another class.
6803 PUN is set to the outermost union in which the flexible array member
6804 (or zero-length array) is defined if one such union exists, otherwise
6806 Similarly, PSTR is set to a data member of the outermost struct of
6807 which the flexible array is a member if one such struct exists,
6808 otherwise to NULL. */
6811 find_flexarrays (tree t
, flexmems_t
*fmem
, bool base_p
,
6812 tree pun
/* = NULL_TREE */,
6813 tree pstr
/* = NULL_TREE */)
6815 /* Set the "pointer" to the outermost enclosing union if not set
6816 yet and maintain it for the remainder of the recursion. */
6817 if (!pun
&& TREE_CODE (t
) == UNION_TYPE
)
6820 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
6822 if (fld
== error_mark_node
)
6825 /* Is FLD a typedef for an anonymous struct? */
6827 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6828 handled elsewhere so that errors like the following are detected
6830 typedef struct { int i, a[], j; } S; // bug c++/72753
6831 S s [2]; // bug c++/68489
6833 if (TREE_CODE (fld
) == TYPE_DECL
6834 && DECL_IMPLICIT_TYPEDEF_P (fld
)
6835 && CLASS_TYPE_P (TREE_TYPE (fld
))
6836 && anon_aggrname_p (DECL_NAME (fld
)))
6838 /* Check the nested unnamed type referenced via a typedef
6839 independently of FMEM (since it's not a data member of
6840 the enclosing class). */
6841 check_flexarrays (TREE_TYPE (fld
));
6845 /* Skip anything that's GCC-generated or not a (non-static) data
6847 if (DECL_ARTIFICIAL (fld
) || TREE_CODE (fld
) != FIELD_DECL
)
6850 /* Type of the member. */
6851 tree fldtype
= TREE_TYPE (fld
);
6852 if (fldtype
== error_mark_node
)
6855 /* Determine the type of the array element or object referenced
6856 by the member so that it can be checked for flexible array
6857 members if it hasn't been yet. */
6858 tree eltype
= fldtype
;
6859 while (TREE_CODE (eltype
) == ARRAY_TYPE
6860 || TREE_CODE (eltype
) == POINTER_TYPE
6861 || TREE_CODE (eltype
) == REFERENCE_TYPE
)
6862 eltype
= TREE_TYPE (eltype
);
6864 if (RECORD_OR_UNION_TYPE_P (eltype
))
6866 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6868 /* Once the member after the flexible array has been found
6870 fmem
->after
[bool (pun
)] = fld
;
6874 if (eltype
== fldtype
|| TYPE_UNNAMED_P (eltype
))
6876 /* Descend into the non-static member struct or union and try
6877 to find a flexible array member or zero-length array among
6878 its members. This is only necessary for anonymous types
6879 and types in whose context the current type T has not been
6880 defined (the latter must not be checked again because they
6881 are already in the process of being checked by one of the
6882 recursive calls). */
6884 tree first
= fmem
->first
;
6885 tree array
= fmem
->array
;
6887 /* If this member isn't anonymous and a prior non-flexible array
6888 member has been seen in one of the enclosing structs, clear
6889 the FIRST member since it doesn't contribute to the flexible
6890 array struct's members. */
6891 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
6892 fmem
->first
= NULL_TREE
;
6894 find_flexarrays (eltype
, fmem
, false, pun
,
6895 !pstr
&& TREE_CODE (t
) == RECORD_TYPE
? fld
: pstr
);
6897 if (fmem
->array
!= array
)
6900 if (first
&& !array
&& !ANON_AGGR_TYPE_P (eltype
))
6902 /* Restore the FIRST member reset above if no flexible
6903 array member has been found in this member's struct. */
6904 fmem
->first
= first
;
6907 /* If the member struct contains the first flexible array
6908 member, or if this member is a base class, continue to
6909 the next member and avoid setting the FMEM->NEXT pointer
6916 if (field_nonempty_p (fld
))
6918 /* Remember the first non-static data member. */
6922 /* Remember the first non-static data member after the flexible
6923 array member, if one has been found, or the zero-length array
6924 if it has been found. */
6925 if (fmem
->array
&& !fmem
->after
[bool (pun
)])
6926 fmem
->after
[bool (pun
)] = fld
;
6929 /* Skip non-arrays. */
6930 if (TREE_CODE (fldtype
) != ARRAY_TYPE
)
6933 /* Determine the upper bound of the array if it has one. */
6934 if (TYPE_DOMAIN (fldtype
))
6938 /* Make a record of the zero-length array if either one
6939 such field or a flexible array member has been seen to
6940 handle the pathological and unlikely case of multiple
6942 if (!fmem
->after
[bool (pun
)])
6943 fmem
->after
[bool (pun
)] = fld
;
6945 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype
))))
6947 /* Remember the first zero-length array unless a flexible array
6948 member has already been seen. */
6950 fmem
->enclosing
= pstr
;
6955 /* Flexible array members have no upper bound. */
6958 /* Replace the zero-length array if it's been stored and
6959 reset the after pointer. */
6960 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
6962 fmem
->after
[bool (pun
)] = NULL_TREE
;
6964 fmem
->enclosing
= pstr
;
6970 fmem
->enclosing
= pstr
;
6976 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6977 a flexible array member (or the zero-length array extension). */
6980 diagnose_invalid_flexarray (const flexmems_t
*fmem
)
6982 if (fmem
->array
&& fmem
->enclosing
6983 && pedwarn (location_of (fmem
->enclosing
), OPT_Wpedantic
,
6984 TYPE_DOMAIN (TREE_TYPE (fmem
->array
))
6985 ? G_("invalid use of %q#T with a zero-size array "
6987 : G_("invalid use of %q#T with a flexible array member "
6989 DECL_CONTEXT (fmem
->array
),
6990 DECL_CONTEXT (fmem
->enclosing
)))
6991 inform (DECL_SOURCE_LOCATION (fmem
->array
),
6992 "array member %q#D declared here", fmem
->array
);
6995 /* Issue diagnostics for invalid flexible array members or zero-length
6996 arrays that are not the last elements of the containing class or its
6997 base classes or that are its sole members. */
7000 diagnose_flexarrays (tree t
, const flexmems_t
*fmem
)
7005 if (fmem
->first
&& !fmem
->after
[0])
7007 diagnose_invalid_flexarray (fmem
);
7011 /* Has a diagnostic been issued? */
7014 const char *msg
= 0;
7016 if (TYPE_DOMAIN (TREE_TYPE (fmem
->array
)))
7019 msg
= G_("zero-size array member %qD not at end of %q#T");
7020 else if (!fmem
->first
)
7021 msg
= G_("zero-size array member %qD in an otherwise empty %q#T");
7025 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7027 if (pedwarn (loc
, OPT_Wpedantic
, msg
, fmem
->array
, t
))
7029 inform (location_of (t
), "in the definition of %q#T", t
);
7037 msg
= G_("flexible array member %qD not at end of %q#T");
7038 else if (!fmem
->first
)
7039 msg
= G_("flexible array member %qD in an otherwise empty %q#T");
7043 location_t loc
= DECL_SOURCE_LOCATION (fmem
->array
);
7046 error_at (loc
, msg
, fmem
->array
, t
);
7048 /* In the unlikely event that the member following the flexible
7049 array member is declared in a different class, or the member
7050 overlaps another member of a common union, point to it.
7051 Otherwise it should be obvious. */
7053 && ((DECL_CONTEXT (fmem
->after
[0])
7054 != DECL_CONTEXT (fmem
->array
))))
7056 inform (DECL_SOURCE_LOCATION (fmem
->after
[0]),
7057 "next member %q#D declared here",
7059 inform (location_of (t
), "in the definition of %q#T", t
);
7064 if (!diagd
&& fmem
->array
&& fmem
->enclosing
)
7065 diagnose_invalid_flexarray (fmem
);
7069 /* Recursively check to make sure that any flexible array or zero-length
7070 array members of class T or its bases are valid (i.e., not the sole
7071 non-static data member of T and, if one exists, that it is the last
7072 non-static data member of T and its base classes. FMEM is expected
7073 to be initially null and is used internally by recursive calls to
7074 the function. Issue the appropriate diagnostics for the array member
7075 that fails the checks. */
7078 check_flexarrays (tree t
, flexmems_t
*fmem
/* = NULL */,
7079 bool base_p
/* = false */)
7081 /* Initialize the result of a search for flexible array and zero-length
7082 array members. Avoid doing any work if the most interesting FMEM data
7083 have already been populated. */
7084 flexmems_t flexmems
= flexmems_t ();
7087 else if (fmem
->array
&& fmem
->first
&& fmem
->after
[0])
7090 tree fam
= fmem
->array
;
7092 /* Recursively check the primary base class first. */
7093 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7095 tree basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
7096 check_flexarrays (basetype
, fmem
, true);
7099 /* Recursively check the base classes. */
7100 int nbases
= TYPE_BINFO (t
) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) : 0;
7101 for (int i
= 0; i
< nbases
; ++i
)
7103 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
7105 /* The primary base class was already checked above. */
7106 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
7109 /* Virtual base classes are at the end. */
7110 if (BINFO_VIRTUAL_P (base_binfo
))
7113 /* Check the base class. */
7114 check_flexarrays (BINFO_TYPE (base_binfo
), fmem
, /*base_p=*/true);
7117 if (fmem
== &flexmems
)
7119 /* Check virtual base classes only once per derived class.
7120 I.e., this check is not performed recursively for base
7124 vec
<tree
, va_gc
> *vbases
;
7125 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
7126 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
7128 /* Check the virtual base class. */
7129 tree basetype
= TREE_TYPE (base_binfo
);
7131 check_flexarrays (basetype
, fmem
, /*base_p=*/true);
7135 /* Is the type unnamed (and therefore a member of it potentially
7136 an anonymous struct or union)? */
7137 bool maybe_anon_p
= TYPE_UNNAMED_P (t
);
7139 /* Search the members of the current (possibly derived) class, skipping
7140 unnamed structs and unions since those could be anonymous. */
7141 if (fmem
!= &flexmems
|| !maybe_anon_p
)
7142 find_flexarrays (t
, fmem
, base_p
|| fam
!= fmem
->array
);
7144 if (fmem
== &flexmems
&& !maybe_anon_p
)
7146 /* Issue diagnostics for invalid flexible and zero-length array
7147 members found in base classes or among the members of the current
7148 class. Ignore anonymous structs and unions whose members are
7149 considered to be members of the enclosing class and thus will
7150 be diagnosed when checking it. */
7151 diagnose_flexarrays (t
, fmem
);
7155 /* Perform processing required when the definition of T (a class type)
7156 is complete. Diagnose invalid definitions of flexible array members
7157 and zero-size arrays. */
7160 finish_struct_1 (tree t
)
7163 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7164 tree virtuals
= NULL_TREE
;
7166 if (COMPLETE_TYPE_P (t
))
7168 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
7169 error ("redefinition of %q#T", t
);
7174 /* If this type was previously laid out as a forward reference,
7175 make sure we lay it out again. */
7176 TYPE_SIZE (t
) = NULL_TREE
;
7177 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
7179 /* Make assumptions about the class; we'll reset the flags if
7181 CLASSTYPE_EMPTY_P (t
) = 1;
7182 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
7183 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
7184 CLASSTYPE_LITERAL_P (t
) = true;
7186 /* Do end-of-class semantic processing: checking the validity of the
7187 bases and members and add implicitly generated methods. */
7188 check_bases_and_members (t
);
7190 /* Find the key method. */
7191 if (TYPE_CONTAINS_VPTR_P (t
))
7193 /* The Itanium C++ ABI permits the key method to be chosen when
7194 the class is defined -- even though the key method so
7195 selected may later turn out to be an inline function. On
7196 some systems (such as ARM Symbian OS) the key method cannot
7197 be determined until the end of the translation unit. On such
7198 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7199 will cause the class to be added to KEYED_CLASSES. Then, in
7200 finish_file we will determine the key method. */
7201 if (targetm
.cxx
.key_method_may_be_inline ())
7202 determine_key_method (t
);
7204 /* If a polymorphic class has no key method, we may emit the vtable
7205 in every translation unit where the class definition appears. If
7206 we're devirtualizing, we can look into the vtable even if we
7207 aren't emitting it. */
7208 if (CLASSTYPE_KEY_METHOD (t
) == NULL_TREE
)
7209 keyed_classes
= tree_cons (NULL_TREE
, t
, keyed_classes
);
7212 /* Layout the class itself. */
7213 layout_class_type (t
, &virtuals
);
7214 if (CLASSTYPE_AS_BASE (t
) != t
)
7215 /* We use the base type for trivial assignments, and hence it
7217 compute_record_mode (CLASSTYPE_AS_BASE (t
));
7219 /* With the layout complete, check for flexible array members and
7220 zero-length arrays that might overlap other members in the final
7222 check_flexarrays (t
);
7224 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
7226 /* If necessary, create the primary vtable for this class. */
7227 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
7229 /* We must enter these virtuals into the table. */
7230 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7231 build_primary_vtable (NULL_TREE
, t
);
7232 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
7233 /* Here we know enough to change the type of our virtual
7234 function table, but we will wait until later this function. */
7235 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
7237 /* If we're warning about ABI tags, check the types of the new
7238 virtual functions. */
7240 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
7241 check_abi_tags (t
, TREE_VALUE (v
));
7244 if (TYPE_CONTAINS_VPTR_P (t
))
7249 if (BINFO_VTABLE (TYPE_BINFO (t
)))
7250 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
7251 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
7252 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
7254 /* Add entries for virtual functions introduced by this class. */
7255 BINFO_VIRTUALS (TYPE_BINFO (t
))
7256 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
7258 /* Set DECL_VINDEX for all functions declared in this class. */
7259 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
7261 fn
= TREE_CHAIN (fn
),
7262 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
7263 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
7265 tree fndecl
= BV_FN (fn
);
7267 if (DECL_THUNK_P (fndecl
))
7268 /* A thunk. We should never be calling this entry directly
7269 from this vtable -- we'd use the entry for the non
7270 thunk base function. */
7271 DECL_VINDEX (fndecl
) = NULL_TREE
;
7272 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
7273 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
7277 finish_struct_bits (t
);
7278 set_method_tm_attributes (t
);
7279 if (flag_openmp
|| flag_openmp_simd
)
7280 finish_omp_declare_simd_methods (t
);
7282 /* Complete the rtl for any static member objects of the type we're
7284 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7285 if (VAR_P (x
) && TREE_STATIC (x
)
7286 && TREE_TYPE (x
) != error_mark_node
7287 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
7288 SET_DECL_MODE (x
, TYPE_MODE (t
));
7290 /* Done with FIELDS...now decide whether to sort these for
7291 faster lookups later.
7293 We use a small number because most searches fail (succeeding
7294 ultimately as the search bores through the inheritance
7295 hierarchy), and we want this failure to occur quickly. */
7297 insert_into_classtype_sorted_fields (TYPE_FIELDS (t
), t
, 8);
7299 /* Complain if one of the field types requires lower visibility. */
7300 constrain_class_visibility (t
);
7302 /* Make the rtl for any new vtables we have created, and unmark
7303 the base types we marked. */
7306 /* Build the VTT for T. */
7310 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
7311 && !CLASSTYPE_FINAL (t
))
7312 warning (OPT_Wnon_virtual_dtor
,
7313 "%q#T has virtual functions and accessible"
7314 " non-virtual destructor", t
);
7318 if (warn_overloaded_virtual
)
7321 /* Class layout, assignment of virtual table slots, etc., is now
7322 complete. Give the back end a chance to tweak the visibility of
7323 the class or perform any other required target modifications. */
7324 targetm
.cxx
.adjust_class_at_definition (t
);
7326 maybe_suppress_debug_info (t
);
7328 if (flag_vtable_verify
)
7329 vtv_save_class_info (t
);
7331 dump_class_hierarchy (t
);
7333 /* Finish debugging output for this type. */
7334 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
7336 if (TYPE_TRANSPARENT_AGGR (t
))
7338 tree field
= first_field (t
);
7339 if (field
== NULL_TREE
|| error_operand_p (field
))
7341 error ("type transparent %q#T does not have any fields", t
);
7342 TYPE_TRANSPARENT_AGGR (t
) = 0;
7344 else if (DECL_ARTIFICIAL (field
))
7346 if (DECL_FIELD_IS_BASE (field
))
7347 error ("type transparent class %qT has base classes", t
);
7350 gcc_checking_assert (DECL_VIRTUAL_P (field
));
7351 error ("type transparent class %qT has virtual functions", t
);
7353 TYPE_TRANSPARENT_AGGR (t
) = 0;
7355 else if (TYPE_MODE (t
) != DECL_MODE (field
))
7357 error ("type transparent %q#T cannot be made transparent because "
7358 "the type of the first field has a different ABI from the "
7359 "class overall", t
);
7360 TYPE_TRANSPARENT_AGGR (t
) = 0;
7365 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7366 equal to THRESHOLD or greater than THRESHOLD. */
7369 insert_into_classtype_sorted_fields (tree fields
, tree t
, int threshold
)
7371 int n_fields
= count_fields (fields
);
7372 if (n_fields
>= threshold
)
7374 struct sorted_fields_type
*field_vec
= sorted_fields_type_new (n_fields
);
7375 add_fields_to_record_type (fields
, field_vec
, 0);
7376 qsort (field_vec
->elts
, n_fields
, sizeof (tree
), field_decl_cmp
);
7377 CLASSTYPE_SORTED_FIELDS (t
) = field_vec
;
7381 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7384 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype
, tree t
)
7386 struct sorted_fields_type
*sorted_fields
= CLASSTYPE_SORTED_FIELDS (t
);
7391 = list_length (TYPE_VALUES (enumtype
)) + sorted_fields
->len
;
7392 struct sorted_fields_type
*field_vec
= sorted_fields_type_new (n_fields
);
7394 for (i
= 0; i
< sorted_fields
->len
; ++i
)
7395 field_vec
->elts
[i
] = sorted_fields
->elts
[i
];
7397 add_enum_fields_to_record_type (enumtype
, field_vec
,
7398 sorted_fields
->len
);
7399 qsort (field_vec
->elts
, n_fields
, sizeof (tree
), field_decl_cmp
);
7400 CLASSTYPE_SORTED_FIELDS (t
) = field_vec
;
7404 /* When T was built up, the member declarations were added in reverse
7405 order. Rearrange them to declaration order. */
7408 unreverse_member_declarations (tree t
)
7414 /* The following lists are all in reverse order. Put them in
7415 declaration order now. */
7416 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
7417 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
7419 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7420 reverse order, so we can't just use nreverse. */
7422 for (x
= TYPE_FIELDS (t
);
7423 x
&& TREE_CODE (x
) != TYPE_DECL
;
7426 next
= DECL_CHAIN (x
);
7427 DECL_CHAIN (x
) = prev
;
7432 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
7434 TYPE_FIELDS (t
) = prev
;
7439 finish_struct (tree t
, tree attributes
)
7441 location_t saved_loc
= input_location
;
7443 /* Now that we've got all the field declarations, reverse everything
7445 unreverse_member_declarations (t
);
7447 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7448 fixup_attribute_variants (t
);
7450 /* Nadger the current location so that diagnostics point to the start of
7451 the struct, not the end. */
7452 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
7454 if (processing_template_decl
)
7458 finish_struct_methods (t
);
7459 TYPE_SIZE (t
) = bitsize_zero_node
;
7460 TYPE_SIZE_UNIT (t
) = size_zero_node
;
7462 /* We need to emit an error message if this type was used as a parameter
7463 and it is an abstract type, even if it is a template. We construct
7464 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7465 account and we call complete_vars with this type, which will check
7466 the PARM_DECLS. Note that while the type is being defined,
7467 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7468 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7469 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
7470 for (x
= TYPE_METHODS (t
); x
; x
= DECL_CHAIN (x
))
7471 if (DECL_PURE_VIRTUAL_P (x
))
7472 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
7474 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7475 an enclosing scope is a template class, so that this function be
7476 found by lookup_fnfields_1 when the using declaration is not
7477 instantiated yet. */
7478 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
7479 if (TREE_CODE (x
) == USING_DECL
)
7481 tree fn
= strip_using_decl (x
);
7482 if (is_overloaded_fn (fn
))
7483 for (; fn
; fn
= OVL_NEXT (fn
))
7484 add_method (t
, OVL_CURRENT (fn
), x
);
7487 /* Remember current #pragma pack value. */
7488 TYPE_PRECISION (t
) = maximum_field_alignment
;
7490 /* Fix up any variants we've already built. */
7491 for (x
= TYPE_NEXT_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
7493 TYPE_SIZE (x
) = TYPE_SIZE (t
);
7494 TYPE_SIZE_UNIT (x
) = TYPE_SIZE_UNIT (t
);
7495 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
7496 TYPE_METHODS (x
) = TYPE_METHODS (t
);
7500 finish_struct_1 (t
);
7502 if (is_std_init_list (t
))
7504 /* People keep complaining that the compiler crashes on an invalid
7505 definition of initializer_list, so I guess we should explicitly
7506 reject it. What the compiler internals care about is that it's a
7507 template and has a pointer field followed by an integer field. */
7509 if (processing_template_decl
)
7511 tree f
= next_initializable_field (TYPE_FIELDS (t
));
7512 if (f
&& TREE_CODE (TREE_TYPE (f
)) == POINTER_TYPE
)
7514 f
= next_initializable_field (DECL_CHAIN (f
));
7515 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
7520 fatal_error (input_location
,
7521 "definition of std::initializer_list does not match "
7522 "#include <initializer_list>");
7525 input_location
= saved_loc
;
7527 TYPE_BEING_DEFINED (t
) = 0;
7529 if (current_class_type
)
7532 error ("trying to finish struct, but kicked out due to previous parse errors");
7534 if (processing_template_decl
&& at_function_scope_p ()
7535 /* Lambdas are defined by the LAMBDA_EXPR. */
7536 && !LAMBDA_TYPE_P (t
))
7537 add_stmt (build_min (TAG_DEFN
, t
));
7542 /* Hash table to avoid endless recursion when handling references. */
7543 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
7545 /* Return the dynamic type of INSTANCE, if known.
7546 Used to determine whether the virtual function table is needed
7549 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7550 of our knowledge of its type. *NONNULL should be initialized
7551 before this function is called. */
7554 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
7556 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7558 switch (TREE_CODE (instance
))
7561 if (POINTER_TYPE_P (TREE_TYPE (instance
)))
7564 return RECUR (TREE_OPERAND (instance
, 0));
7567 /* This is a call to a constructor, hence it's never zero. */
7568 if (TREE_HAS_CONSTRUCTOR (instance
))
7572 return TREE_TYPE (instance
);
7577 /* This is a call to a constructor, hence it's never zero. */
7578 if (TREE_HAS_CONSTRUCTOR (instance
))
7582 return TREE_TYPE (instance
);
7584 return RECUR (TREE_OPERAND (instance
, 0));
7586 case POINTER_PLUS_EXPR
:
7589 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
7590 return RECUR (TREE_OPERAND (instance
, 0));
7591 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
7592 /* Propagate nonnull. */
7593 return RECUR (TREE_OPERAND (instance
, 0));
7598 return RECUR (TREE_OPERAND (instance
, 0));
7601 instance
= TREE_OPERAND (instance
, 0);
7604 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7605 with a real object -- given &p->f, p can still be null. */
7606 tree t
= get_base_address (instance
);
7607 /* ??? Probably should check DECL_WEAK here. */
7608 if (t
&& DECL_P (t
))
7611 return RECUR (instance
);
7614 /* If this component is really a base class reference, then the field
7615 itself isn't definitive. */
7616 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
7617 return RECUR (TREE_OPERAND (instance
, 0));
7618 return RECUR (TREE_OPERAND (instance
, 1));
7622 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
7623 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7627 return TREE_TYPE (TREE_TYPE (instance
));
7633 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7637 return TREE_TYPE (instance
);
7639 else if (instance
== current_class_ptr
)
7644 /* if we're in a ctor or dtor, we know our type. If
7645 current_class_ptr is set but we aren't in a function, we're in
7646 an NSDMI (and therefore a constructor). */
7647 if (current_scope () != current_function_decl
7648 || (DECL_LANG_SPECIFIC (current_function_decl
)
7649 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7650 || DECL_DESTRUCTOR_P (current_function_decl
))))
7654 return TREE_TYPE (TREE_TYPE (instance
));
7657 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
7659 /* We only need one hash table because it is always left empty. */
7660 if (!fixed_type_or_null_ref_ht
)
7661 fixed_type_or_null_ref_ht
7662 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7664 /* Reference variables should be references to objects. */
7668 /* Enter the INSTANCE in a table to prevent recursion; a
7669 variable's initializer may refer to the variable
7671 if (VAR_P (instance
)
7672 && DECL_INITIAL (instance
)
7673 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7674 && !fixed_type_or_null_ref_ht
->find (instance
))
7679 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7681 type
= RECUR (DECL_INITIAL (instance
));
7682 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7695 /* Return nonzero if the dynamic type of INSTANCE is known, and
7696 equivalent to the static type. We also handle the case where
7697 INSTANCE is really a pointer. Return negative if this is a
7698 ctor/dtor. There the dynamic type is known, but this might not be
7699 the most derived base of the original object, and hence virtual
7700 bases may not be laid out according to this type.
7702 Used to determine whether the virtual function table is needed
7705 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7706 of our knowledge of its type. *NONNULL should be initialized
7707 before this function is called. */
7710 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7712 tree t
= TREE_TYPE (instance
);
7716 /* processing_template_decl can be false in a template if we're in
7717 instantiate_non_dependent_expr, but we still want to suppress
7719 if (in_template_function ())
7721 /* In a template we only care about the type of the result. */
7727 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7728 if (fixed
== NULL_TREE
)
7730 if (POINTER_TYPE_P (t
))
7732 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7734 return cdtorp
? -1 : 1;
7739 init_class_processing (void)
7741 current_class_depth
= 0;
7742 current_class_stack_size
= 10;
7744 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7745 vec_alloc (local_classes
, 8);
7746 sizeof_biggest_empty_class
= size_zero_node
;
7748 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7749 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7750 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7753 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7756 restore_class_cache (void)
7760 /* We are re-entering the same class we just left, so we don't
7761 have to search the whole inheritance matrix to find all the
7762 decls to bind again. Instead, we install the cached
7763 class_shadowed list and walk through it binding names. */
7764 push_binding_level (previous_class_level
);
7765 class_binding_level
= previous_class_level
;
7766 /* Restore IDENTIFIER_TYPE_VALUE. */
7767 for (type
= class_binding_level
->type_shadowed
;
7769 type
= TREE_CHAIN (type
))
7770 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7773 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7774 appropriate for TYPE.
7776 So that we may avoid calls to lookup_name, we cache the _TYPE
7777 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7779 For multiple inheritance, we perform a two-pass depth-first search
7780 of the type lattice. */
7783 pushclass (tree type
)
7785 class_stack_node_t csn
;
7787 type
= TYPE_MAIN_VARIANT (type
);
7789 /* Make sure there is enough room for the new entry on the stack. */
7790 if (current_class_depth
+ 1 >= current_class_stack_size
)
7792 current_class_stack_size
*= 2;
7794 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7795 current_class_stack_size
);
7798 /* Insert a new entry on the class stack. */
7799 csn
= current_class_stack
+ current_class_depth
;
7800 csn
->name
= current_class_name
;
7801 csn
->type
= current_class_type
;
7802 csn
->access
= current_access_specifier
;
7803 csn
->names_used
= 0;
7805 current_class_depth
++;
7807 /* Now set up the new type. */
7808 current_class_name
= TYPE_NAME (type
);
7809 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7810 current_class_name
= DECL_NAME (current_class_name
);
7811 current_class_type
= type
;
7813 /* By default, things in classes are private, while things in
7814 structures or unions are public. */
7815 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7816 ? access_private_node
7817 : access_public_node
);
7819 if (previous_class_level
7820 && type
!= previous_class_level
->this_entity
7821 && current_class_depth
== 1)
7823 /* Forcibly remove any old class remnants. */
7824 invalidate_class_lookup_cache ();
7827 if (!previous_class_level
7828 || type
!= previous_class_level
->this_entity
7829 || current_class_depth
> 1)
7832 restore_class_cache ();
7835 /* When we exit a toplevel class scope, we save its binding level so
7836 that we can restore it quickly. Here, we've entered some other
7837 class, so we must invalidate our cache. */
7840 invalidate_class_lookup_cache (void)
7842 previous_class_level
= NULL
;
7845 /* Get out of the current class scope. If we were in a class scope
7846 previously, that is the one popped to. */
7853 current_class_depth
--;
7854 current_class_name
= current_class_stack
[current_class_depth
].name
;
7855 current_class_type
= current_class_stack
[current_class_depth
].type
;
7856 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7857 if (current_class_stack
[current_class_depth
].names_used
)
7858 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
7861 /* Mark the top of the class stack as hidden. */
7864 push_class_stack (void)
7866 if (current_class_depth
)
7867 ++current_class_stack
[current_class_depth
- 1].hidden
;
7870 /* Mark the top of the class stack as un-hidden. */
7873 pop_class_stack (void)
7875 if (current_class_depth
)
7876 --current_class_stack
[current_class_depth
- 1].hidden
;
7879 /* Returns 1 if the class type currently being defined is either T or
7880 a nested type of T. Returns the type from the current_class_stack,
7881 which might be equivalent to but not equal to T in case of
7882 constrained partial specializations. */
7885 currently_open_class (tree t
)
7889 if (!CLASS_TYPE_P (t
))
7892 t
= TYPE_MAIN_VARIANT (t
);
7894 /* We start looking from 1 because entry 0 is from global scope,
7896 for (i
= current_class_depth
; i
> 0; --i
)
7899 if (i
== current_class_depth
)
7900 c
= current_class_type
;
7903 if (current_class_stack
[i
].hidden
)
7905 c
= current_class_stack
[i
].type
;
7909 if (same_type_p (c
, t
))
7915 /* If either current_class_type or one of its enclosing classes are derived
7916 from T, return the appropriate type. Used to determine how we found
7917 something via unqualified lookup. */
7920 currently_open_derived_class (tree t
)
7924 /* The bases of a dependent type are unknown. */
7925 if (dependent_type_p (t
))
7928 if (!current_class_type
)
7931 if (DERIVED_FROM_P (t
, current_class_type
))
7932 return current_class_type
;
7934 for (i
= current_class_depth
- 1; i
> 0; --i
)
7936 if (current_class_stack
[i
].hidden
)
7938 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
7939 return current_class_stack
[i
].type
;
7945 /* Return the outermost enclosing class type that is still open, or
7949 outermost_open_class (void)
7951 if (!current_class_type
)
7954 if (TYPE_BEING_DEFINED (current_class_type
))
7955 r
= current_class_type
;
7956 for (int i
= current_class_depth
- 1; i
> 0; --i
)
7958 if (current_class_stack
[i
].hidden
)
7960 tree t
= current_class_stack
[i
].type
;
7961 if (!TYPE_BEING_DEFINED (t
))
7968 /* Returns the innermost class type which is not a lambda closure type. */
7971 current_nonlambda_class_type (void)
7975 /* We start looking from 1 because entry 0 is from global scope,
7977 for (i
= current_class_depth
; i
> 0; --i
)
7980 if (i
== current_class_depth
)
7981 c
= current_class_type
;
7984 if (current_class_stack
[i
].hidden
)
7986 c
= current_class_stack
[i
].type
;
7990 if (!LAMBDA_TYPE_P (c
))
7996 /* When entering a class scope, all enclosing class scopes' names with
7997 static meaning (static variables, static functions, types and
7998 enumerators) have to be visible. This recursive function calls
7999 pushclass for all enclosing class contexts until global or a local
8000 scope is reached. TYPE is the enclosed class. */
8003 push_nested_class (tree type
)
8005 /* A namespace might be passed in error cases, like A::B:C. */
8006 if (type
== NULL_TREE
8007 || !CLASS_TYPE_P (type
))
8010 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
8015 /* Undoes a push_nested_class call. */
8018 pop_nested_class (void)
8020 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
8023 if (context
&& CLASS_TYPE_P (context
))
8024 pop_nested_class ();
8027 /* Returns the number of extern "LANG" blocks we are nested within. */
8030 current_lang_depth (void)
8032 return vec_safe_length (current_lang_base
);
8035 /* Set global variables CURRENT_LANG_NAME to appropriate value
8036 so that behavior of name-mangling machinery is correct. */
8039 push_lang_context (tree name
)
8041 vec_safe_push (current_lang_base
, current_lang_name
);
8043 if (name
== lang_name_cplusplus
)
8044 current_lang_name
= name
;
8045 else if (name
== lang_name_c
)
8046 current_lang_name
= name
;
8048 error ("language string %<\"%E\"%> not recognized", name
);
8051 /* Get out of the current language scope. */
8054 pop_lang_context (void)
8056 current_lang_name
= current_lang_base
->pop ();
8059 /* Type instantiation routines. */
8061 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8062 matches the TARGET_TYPE. If there is no satisfactory match, return
8063 error_mark_node, and issue an error & warning messages under
8064 control of FLAGS. Permit pointers to member function if FLAGS
8065 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8066 a template-id, and EXPLICIT_TARGS are the explicitly provided
8069 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8070 is the base path used to reference those member functions. If
8071 the address is resolved to a member function, access checks will be
8072 performed and errors issued if appropriate. */
8075 resolve_address_of_overloaded_function (tree target_type
,
8077 tsubst_flags_t complain
,
8079 tree explicit_targs
,
8082 /* Here's what the standard says:
8086 If the name is a function template, template argument deduction
8087 is done, and if the argument deduction succeeds, the deduced
8088 arguments are used to generate a single template function, which
8089 is added to the set of overloaded functions considered.
8091 Non-member functions and static member functions match targets of
8092 type "pointer-to-function" or "reference-to-function." Nonstatic
8093 member functions match targets of type "pointer-to-member
8094 function;" the function type of the pointer to member is used to
8095 select the member function from the set of overloaded member
8096 functions. If a nonstatic member function is selected, the
8097 reference to the overloaded function name is required to have the
8098 form of a pointer to member as described in 5.3.1.
8100 If more than one function is selected, any template functions in
8101 the set are eliminated if the set also contains a non-template
8102 function, and any given template function is eliminated if the
8103 set contains a second template function that is more specialized
8104 than the first according to the partial ordering rules 14.5.5.2.
8105 After such eliminations, if any, there shall remain exactly one
8106 selected function. */
8109 /* We store the matches in a TREE_LIST rooted here. The functions
8110 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8111 interoperability with most_specialized_instantiation. */
8112 tree matches
= NULL_TREE
;
8114 tree target_fn_type
;
8116 /* By the time we get here, we should be seeing only real
8117 pointer-to-member types, not the internal POINTER_TYPE to
8118 METHOD_TYPE representation. */
8119 gcc_assert (!TYPE_PTR_P (target_type
)
8120 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
8122 gcc_assert (is_overloaded_fn (overload
));
8124 /* Check that the TARGET_TYPE is reasonable. */
8125 if (TYPE_PTRFN_P (target_type
)
8126 || TYPE_REFFN_P (target_type
))
8128 else if (TYPE_PTRMEMFUNC_P (target_type
))
8129 /* This is OK, too. */
8131 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
8132 /* This is OK, too. This comes from a conversion to reference
8134 target_type
= build_reference_type (target_type
);
8137 if (complain
& tf_error
)
8138 error ("cannot resolve overloaded function %qD based on"
8139 " conversion to type %qT",
8140 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
8141 return error_mark_node
;
8144 /* Non-member functions and static member functions match targets of type
8145 "pointer-to-function" or "reference-to-function." Nonstatic member
8146 functions match targets of type "pointer-to-member-function;" the
8147 function type of the pointer to member is used to select the member
8148 function from the set of overloaded member functions.
8150 So figure out the FUNCTION_TYPE that we want to match against. */
8151 target_fn_type
= static_fn_type (target_type
);
8153 /* If we can find a non-template function that matches, we can just
8154 use it. There's no point in generating template instantiations
8155 if we're just going to throw them out anyhow. But, of course, we
8156 can only do this when we don't *need* a template function. */
8161 for (fns
= overload
; fns
; fns
= OVL_NEXT (fns
))
8163 tree fn
= OVL_CURRENT (fns
);
8165 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8166 /* We're not looking for templates just yet. */
8169 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8171 /* We're looking for a non-static member, and this isn't
8172 one, or vice versa. */
8175 /* Ignore functions which haven't been explicitly
8177 if (DECL_ANTICIPATED (fn
))
8180 /* In C++17 we need the noexcept-qualifier to compare types. */
8181 if (flag_noexcept_type
)
8182 maybe_instantiate_noexcept (fn
);
8184 /* See if there's a match. */
8185 tree fntype
= static_fn_type (fn
);
8186 if (same_type_p (target_fn_type
, fntype
)
8187 || fnptr_conv_p (target_fn_type
, fntype
))
8188 matches
= tree_cons (fn
, NULL_TREE
, matches
);
8192 /* Now, if we've already got a match (or matches), there's no need
8193 to proceed to the template functions. But, if we don't have a
8194 match we need to look at them, too. */
8197 tree target_arg_types
;
8198 tree target_ret_type
;
8201 unsigned int nargs
, ia
;
8204 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
8205 target_ret_type
= TREE_TYPE (target_fn_type
);
8207 nargs
= list_length (target_arg_types
);
8208 args
= XALLOCAVEC (tree
, nargs
);
8209 for (arg
= target_arg_types
, ia
= 0;
8210 arg
!= NULL_TREE
&& arg
!= void_list_node
;
8211 arg
= TREE_CHAIN (arg
), ++ia
)
8212 args
[ia
] = TREE_VALUE (arg
);
8215 for (fns
= overload
; fns
; fns
= OVL_NEXT (fns
))
8217 tree fn
= OVL_CURRENT (fns
);
8221 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8222 /* We're only looking for templates. */
8225 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8227 /* We're not looking for a non-static member, and this is
8228 one, or vice versa. */
8231 tree ret
= target_ret_type
;
8233 /* If the template has a deduced return type, don't expose it to
8234 template argument deduction. */
8235 if (undeduced_auto_decl (fn
))
8238 /* Try to do argument deduction. */
8239 targs
= make_tree_vec (DECL_NTPARMS (fn
));
8240 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
8242 DEDUCE_EXACT
, LOOKUP_NORMAL
,
8244 if (instantiation
== error_mark_node
)
8245 /* Instantiation failed. */
8248 /* Constraints must be satisfied. This is done before
8249 return type deduction since that instantiates the
8251 if (flag_concepts
&& !constraints_satisfied_p (instantiation
))
8254 /* And now force instantiation to do return type deduction. */
8255 if (undeduced_auto_decl (instantiation
))
8258 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
8261 require_deduced_type (instantiation
);
8264 /* In C++17 we need the noexcept-qualifier to compare types. */
8265 if (flag_noexcept_type
)
8266 maybe_instantiate_noexcept (instantiation
);
8268 /* See if there's a match. */
8269 tree fntype
= static_fn_type (instantiation
);
8270 if (same_type_p (target_fn_type
, fntype
)
8271 || fnptr_conv_p (target_fn_type
, fntype
))
8272 matches
= tree_cons (instantiation
, fn
, matches
);
8275 /* Now, remove all but the most specialized of the matches. */
8278 tree match
= most_specialized_instantiation (matches
);
8280 if (match
!= error_mark_node
)
8281 matches
= tree_cons (TREE_PURPOSE (match
),
8287 /* Now we should have exactly one function in MATCHES. */
8288 if (matches
== NULL_TREE
)
8290 /* There were *no* matches. */
8291 if (complain
& tf_error
)
8293 error ("no matches converting function %qD to type %q#T",
8294 DECL_NAME (OVL_CURRENT (overload
)),
8297 print_candidates (overload
);
8299 return error_mark_node
;
8301 else if (TREE_CHAIN (matches
))
8303 /* There were too many matches. First check if they're all
8304 the same function. */
8305 tree match
= NULL_TREE
;
8307 fn
= TREE_PURPOSE (matches
);
8309 /* For multi-versioned functions, more than one match is just fine and
8310 decls_match will return false as they are different. */
8311 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
8312 if (!decls_match (fn
, TREE_PURPOSE (match
))
8313 && !targetm
.target_option
.function_versions
8314 (fn
, TREE_PURPOSE (match
)))
8319 if (complain
& tf_error
)
8321 error ("converting overloaded function %qD to type %q#T is ambiguous",
8322 DECL_NAME (OVL_FUNCTION (overload
)),
8325 /* Since print_candidates expects the functions in the
8326 TREE_VALUE slot, we flip them here. */
8327 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
8328 TREE_VALUE (match
) = TREE_PURPOSE (match
);
8330 print_candidates (matches
);
8333 return error_mark_node
;
8337 /* Good, exactly one match. Now, convert it to the correct type. */
8338 fn
= TREE_PURPOSE (matches
);
8340 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
8341 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
8343 static int explained
;
8345 if (!(complain
& tf_error
))
8346 return error_mark_node
;
8348 permerror (input_location
, "assuming pointer to member %qD", fn
);
8351 inform (input_location
, "(a pointer to member can only be formed with %<&%E%>)", fn
);
8356 /* If a pointer to a function that is multi-versioned is requested, the
8357 pointer to the dispatcher function is returned instead. This works
8358 well because indirectly calling the function will dispatch the right
8359 function version at run-time. */
8360 if (DECL_FUNCTION_VERSIONED (fn
))
8362 fn
= get_function_version_dispatcher (fn
);
8364 return error_mark_node
;
8365 /* Mark all the versions corresponding to the dispatcher as used. */
8366 if (!(complain
& tf_conv
))
8367 mark_versions_used (fn
);
8370 /* If we're doing overload resolution purely for the purpose of
8371 determining conversion sequences, we should not consider the
8372 function used. If this conversion sequence is selected, the
8373 function will be marked as used at this point. */
8374 if (!(complain
& tf_conv
))
8376 /* Make =delete work with SFINAE. */
8377 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
8378 return error_mark_node
;
8379 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
8380 return error_mark_node
;
8383 /* We could not check access to member functions when this
8384 expression was originally created since we did not know at that
8385 time to which function the expression referred. */
8386 if (DECL_FUNCTION_MEMBER_P (fn
))
8388 gcc_assert (access_path
);
8389 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
8392 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
8393 return cp_build_addr_expr (fn
, complain
);
8396 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8397 will mark the function as addressed, but here we must do it
8399 cxx_mark_addressable (fn
);
8405 /* This function will instantiate the type of the expression given in
8406 RHS to match the type of LHSTYPE. If errors exist, then return
8407 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8408 we complain on errors. If we are not complaining, never modify rhs,
8409 as overload resolution wants to try many possible instantiations, in
8410 the hope that at least one will work.
8412 For non-recursive calls, LHSTYPE should be a function, pointer to
8413 function, or a pointer to member function. */
8416 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
8418 tsubst_flags_t complain_in
= complain
;
8419 tree access_path
= NULL_TREE
;
8421 complain
&= ~tf_ptrmem_ok
;
8423 if (lhstype
== unknown_type_node
)
8425 if (complain
& tf_error
)
8426 error ("not enough type information");
8427 return error_mark_node
;
8430 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
8432 tree fntype
= non_reference (lhstype
);
8433 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
8435 if (fnptr_conv_p (fntype
, TREE_TYPE (rhs
)))
8437 if (flag_ms_extensions
8438 && TYPE_PTRMEMFUNC_P (fntype
)
8439 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
8440 /* Microsoft allows `A::f' to be resolved to a
8441 pointer-to-member. */
8445 if (complain
& tf_error
)
8446 error ("cannot convert %qE from type %qT to type %qT",
8447 rhs
, TREE_TYPE (rhs
), fntype
);
8448 return error_mark_node
;
8452 if (BASELINK_P (rhs
))
8454 access_path
= BASELINK_ACCESS_BINFO (rhs
);
8455 rhs
= BASELINK_FUNCTIONS (rhs
);
8458 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8459 deduce any type information. */
8460 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
8462 if (complain
& tf_error
)
8463 error ("not enough type information");
8464 return error_mark_node
;
8467 /* If we instantiate a template, and it is a A ?: C expression
8468 with omitted B, look through the SAVE_EXPR. */
8469 if (TREE_CODE (rhs
) == SAVE_EXPR
)
8470 rhs
= TREE_OPERAND (rhs
, 0);
8472 /* There are only a few kinds of expressions that may have a type
8473 dependent on overload resolution. */
8474 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
8475 || TREE_CODE (rhs
) == COMPONENT_REF
8476 || is_overloaded_fn (rhs
)
8477 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
8479 /* This should really only be used when attempting to distinguish
8480 what sort of a pointer to function we have. For now, any
8481 arithmetic operation which is not supported on pointers
8482 is rejected as an error. */
8484 switch (TREE_CODE (rhs
))
8488 tree member
= TREE_OPERAND (rhs
, 1);
8490 member
= instantiate_type (lhstype
, member
, complain
);
8491 if (member
!= error_mark_node
8492 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
8493 /* Do not lose object's side effects. */
8494 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
8495 TREE_OPERAND (rhs
, 0), member
);
8500 rhs
= TREE_OPERAND (rhs
, 1);
8501 if (BASELINK_P (rhs
))
8502 return instantiate_type (lhstype
, rhs
, complain_in
);
8504 /* This can happen if we are forming a pointer-to-member for a
8506 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
8510 case TEMPLATE_ID_EXPR
:
8512 tree fns
= TREE_OPERAND (rhs
, 0);
8513 tree args
= TREE_OPERAND (rhs
, 1);
8516 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
8517 /*template_only=*/true,
8524 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
8525 /*template_only=*/false,
8526 /*explicit_targs=*/NULL_TREE
,
8531 if (PTRMEM_OK_P (rhs
))
8532 complain
|= tf_ptrmem_ok
;
8534 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
8538 return error_mark_node
;
8543 return error_mark_node
;
8546 /* Return the name of the virtual function pointer field
8547 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8548 this may have to look back through base types to find the
8549 ultimate field name. (For single inheritance, these could
8550 all be the same name. Who knows for multiple inheritance). */
8553 get_vfield_name (tree type
)
8555 tree binfo
, base_binfo
;
8558 for (binfo
= TYPE_BINFO (type
);
8559 BINFO_N_BASE_BINFOS (binfo
);
8562 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
8564 if (BINFO_VIRTUAL_P (base_binfo
)
8565 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
8569 type
= BINFO_TYPE (binfo
);
8570 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
8571 + TYPE_NAME_LENGTH (type
) + 2);
8572 sprintf (buf
, VFIELD_NAME_FORMAT
,
8573 IDENTIFIER_POINTER (constructor_name (type
)));
8574 return get_identifier (buf
);
8578 print_class_statistics (void)
8580 if (! GATHER_STATISTICS
)
8583 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
8584 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
8587 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
8588 n_vtables
, n_vtable_searches
);
8589 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
8590 n_vtable_entries
, n_vtable_elems
);
8594 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8595 according to [class]:
8596 The class-name is also inserted
8597 into the scope of the class itself. For purposes of access checking,
8598 the inserted class name is treated as if it were a public member name. */
8601 build_self_reference (void)
8603 tree name
= constructor_name (current_class_type
);
8604 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
8607 DECL_NONLOCAL (value
) = 1;
8608 DECL_CONTEXT (value
) = current_class_type
;
8609 DECL_ARTIFICIAL (value
) = 1;
8610 SET_DECL_SELF_REFERENCE_P (value
);
8611 set_underlying_type (value
);
8613 if (processing_template_decl
)
8614 value
= push_template_decl (value
);
8616 saved_cas
= current_access_specifier
;
8617 current_access_specifier
= access_public_node
;
8618 finish_member_declaration (value
);
8619 current_access_specifier
= saved_cas
;
8622 /* Returns 1 if TYPE contains only padding bytes. */
8625 is_empty_class (tree type
)
8627 if (type
== error_mark_node
)
8630 if (! CLASS_TYPE_P (type
))
8633 return CLASSTYPE_EMPTY_P (type
);
8636 /* Returns true if TYPE contains no actual data, just various
8637 possible combinations of empty classes and possibly a vptr. */
8640 is_really_empty_class (tree type
)
8642 if (CLASS_TYPE_P (type
))
8649 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8650 out, but we'd like to be able to check this before then. */
8651 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8654 for (binfo
= TYPE_BINFO (type
), i
= 0;
8655 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8656 if (!is_really_empty_class (BINFO_TYPE (base_binfo
)))
8658 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8659 if (TREE_CODE (field
) == FIELD_DECL
8660 && !DECL_ARTIFICIAL (field
)
8661 /* An unnamed bit-field is not a data member. */
8662 && (DECL_NAME (field
) || !DECL_C_BIT_FIELD (field
))
8663 && !is_really_empty_class (TREE_TYPE (field
)))
8667 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8668 return (integer_zerop (array_type_nelts_top (type
))
8669 || is_really_empty_class (TREE_TYPE (type
)));
8673 /* Note that NAME was looked up while the current class was being
8674 defined and that the result of that lookup was DECL. */
8677 maybe_note_name_used_in_class (tree name
, tree decl
)
8679 splay_tree names_used
;
8681 /* If we're not defining a class, there's nothing to do. */
8682 if (!(innermost_scope_kind() == sk_class
8683 && TYPE_BEING_DEFINED (current_class_type
)
8684 && !LAMBDA_TYPE_P (current_class_type
)))
8687 /* If there's already a binding for this NAME, then we don't have
8688 anything to worry about. */
8689 if (lookup_member (current_class_type
, name
,
8690 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8693 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8694 current_class_stack
[current_class_depth
- 1].names_used
8695 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8696 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8698 splay_tree_insert (names_used
,
8699 (splay_tree_key
) name
,
8700 (splay_tree_value
) decl
);
8703 /* Note that NAME was declared (as DECL) in the current class. Check
8704 to see that the declaration is valid. */
8707 note_name_declared_in_class (tree name
, tree decl
)
8709 splay_tree names_used
;
8712 /* Look to see if we ever used this name. */
8714 = current_class_stack
[current_class_depth
- 1].names_used
;
8717 /* The C language allows members to be declared with a type of the same
8718 name, and the C++ standard says this diagnostic is not required. So
8719 allow it in extern "C" blocks unless predantic is specified.
8720 Allow it in all cases if -ms-extensions is specified. */
8721 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8722 || flag_ms_extensions
)
8724 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8727 /* [basic.scope.class]
8729 A name N used in a class S shall refer to the same declaration
8730 in its context and when re-evaluated in the completed scope of
8732 permerror (input_location
, "declaration of %q#D", decl
);
8733 permerror (location_of ((tree
) n
->value
),
8734 "changes meaning of %qD from %q#D",
8735 DECL_NAME (OVL_CURRENT (decl
)), (tree
) n
->value
);
8739 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8740 Secondary vtables are merged with primary vtables; this function
8741 will return the VAR_DECL for the primary vtable. */
8744 get_vtbl_decl_for_binfo (tree binfo
)
8748 decl
= BINFO_VTABLE (binfo
);
8749 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8751 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
8752 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8755 gcc_assert (VAR_P (decl
));
8760 /* Returns the binfo for the primary base of BINFO. If the resulting
8761 BINFO is a virtual base, and it is inherited elsewhere in the
8762 hierarchy, then the returned binfo might not be the primary base of
8763 BINFO in the complete object. Check BINFO_PRIMARY_P or
8764 BINFO_LOST_PRIMARY_P to be sure. */
8767 get_primary_binfo (tree binfo
)
8771 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8775 return copied_binfo (primary_base
, binfo
);
8778 /* As above, but iterate until we reach the binfo that actually provides the
8782 most_primary_binfo (tree binfo
)
8785 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
8786 && !BINFO_LOST_PRIMARY_P (b
))
8788 tree primary_base
= get_primary_binfo (b
);
8789 gcc_assert (BINFO_PRIMARY_P (primary_base
)
8790 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
8796 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8797 type. Note that the virtual inheritance might be above or below BINFO in
8801 vptr_via_virtual_p (tree binfo
)
8804 binfo
= TYPE_BINFO (binfo
);
8805 tree primary
= most_primary_binfo (binfo
);
8806 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8807 a morally virtual base. */
8808 tree virt
= binfo_via_virtual (primary
, NULL_TREE
);
8809 return virt
!= NULL_TREE
;
8812 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8815 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8818 fprintf (stream
, "%*s", indent
, "");
8822 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8823 INDENT should be zero when called from the top level; it is
8824 incremented recursively. IGO indicates the next expected BINFO in
8825 inheritance graph ordering. */
8828 dump_class_hierarchy_r (FILE *stream
,
8838 indented
= maybe_indent_hierarchy (stream
, indent
, 0);
8839 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8840 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8841 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8844 fprintf (stream
, "alternative-path\n");
8847 igo
= TREE_CHAIN (binfo
);
8849 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8850 tree_to_shwi (BINFO_OFFSET (binfo
)));
8851 if (is_empty_class (BINFO_TYPE (binfo
)))
8852 fprintf (stream
, " empty");
8853 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8854 fprintf (stream
, " nearly-empty");
8855 if (BINFO_VIRTUAL_P (binfo
))
8856 fprintf (stream
, " virtual");
8857 fprintf (stream
, "\n");
8860 if (BINFO_PRIMARY_P (binfo
))
8862 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8863 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8864 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8865 TFF_PLAIN_IDENTIFIER
),
8866 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8868 if (BINFO_LOST_PRIMARY_P (binfo
))
8870 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8871 fprintf (stream
, " lost-primary");
8874 fprintf (stream
, "\n");
8876 if (!(flags
& TDF_SLIM
))
8880 if (BINFO_SUBVTT_INDEX (binfo
))
8882 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8883 fprintf (stream
, " subvttidx=%s",
8884 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8885 TFF_PLAIN_IDENTIFIER
));
8887 if (BINFO_VPTR_INDEX (binfo
))
8889 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8890 fprintf (stream
, " vptridx=%s",
8891 expr_as_string (BINFO_VPTR_INDEX (binfo
),
8892 TFF_PLAIN_IDENTIFIER
));
8894 if (BINFO_VPTR_FIELD (binfo
))
8896 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8897 fprintf (stream
, " vbaseoffset=%s",
8898 expr_as_string (BINFO_VPTR_FIELD (binfo
),
8899 TFF_PLAIN_IDENTIFIER
));
8901 if (BINFO_VTABLE (binfo
))
8903 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8904 fprintf (stream
, " vptr=%s",
8905 expr_as_string (BINFO_VTABLE (binfo
),
8906 TFF_PLAIN_IDENTIFIER
));
8910 fprintf (stream
, "\n");
8913 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
8914 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
8919 /* Dump the BINFO hierarchy for T. */
8922 dump_class_hierarchy_1 (FILE *stream
, int flags
, tree t
)
8924 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8925 fprintf (stream
, " size=%lu align=%lu\n",
8926 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
8927 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
8928 fprintf (stream
, " base size=%lu base align=%lu\n",
8929 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t
)))
8931 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t
))
8933 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
8934 fprintf (stream
, "\n");
8937 /* Debug interface to hierarchy dumping. */
8940 debug_class (tree t
)
8942 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
8946 dump_class_hierarchy (tree t
)
8949 FILE *stream
= get_dump_info (TDI_class
, &flags
);
8953 dump_class_hierarchy_1 (stream
, flags
, t
);
8958 dump_array (FILE * stream
, tree decl
)
8961 unsigned HOST_WIDE_INT ix
;
8963 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
8965 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
8967 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
8968 fprintf (stream
, " %s entries",
8969 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
8970 TFF_PLAIN_IDENTIFIER
));
8971 fprintf (stream
, "\n");
8973 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
8975 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
8976 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
8980 dump_vtable (tree t
, tree binfo
, tree vtable
)
8983 FILE *stream
= get_dump_info (TDI_class
, &flags
);
8988 if (!(flags
& TDF_SLIM
))
8990 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
8992 fprintf (stream
, "%s for %s",
8993 ctor_vtbl_p
? "Construction vtable" : "Vtable",
8994 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
8997 if (!BINFO_VIRTUAL_P (binfo
))
8998 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
8999 (HOST_WIDE_INT
) (uintptr_t) binfo
);
9000 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9002 fprintf (stream
, "\n");
9003 dump_array (stream
, vtable
);
9004 fprintf (stream
, "\n");
9009 dump_vtt (tree t
, tree vtt
)
9012 FILE *stream
= get_dump_info (TDI_class
, &flags
);
9017 if (!(flags
& TDF_SLIM
))
9019 fprintf (stream
, "VTT for %s\n",
9020 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
9021 dump_array (stream
, vtt
);
9022 fprintf (stream
, "\n");
9026 /* Dump a function or thunk and its thunkees. */
9029 dump_thunk (FILE *stream
, int indent
, tree thunk
)
9031 static const char spaces
[] = " ";
9032 tree name
= DECL_NAME (thunk
);
9035 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
9037 !DECL_THUNK_P (thunk
) ? "function"
9038 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
9039 name
? IDENTIFIER_POINTER (name
) : "<unset>");
9040 if (DECL_THUNK_P (thunk
))
9042 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
9043 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
9045 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
9046 if (!virtual_adjust
)
9048 else if (DECL_THIS_THUNK_P (thunk
))
9049 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
9050 tree_to_shwi (virtual_adjust
));
9052 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
9053 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
9054 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
9055 if (THUNK_ALIAS (thunk
))
9056 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
9058 fprintf (stream
, "\n");
9059 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
9060 dump_thunk (stream
, indent
+ 2, thunks
);
9063 /* Dump the thunks for FN. */
9066 debug_thunks (tree fn
)
9068 dump_thunk (stderr
, 0, fn
);
9071 /* Virtual function table initialization. */
9073 /* Create all the necessary vtables for T and its base classes. */
9076 finish_vtbls (tree t
)
9079 vec
<constructor_elt
, va_gc
> *v
= NULL
;
9080 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
9082 /* We lay out the primary and secondary vtables in one contiguous
9083 vtable. The primary vtable is first, followed by the non-virtual
9084 secondary vtables in inheritance graph order. */
9085 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
9088 /* Then come the virtual bases, also in inheritance graph order. */
9089 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
9091 if (!BINFO_VIRTUAL_P (vbase
))
9093 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
9096 if (BINFO_VTABLE (TYPE_BINFO (t
)))
9097 initialize_vtable (TYPE_BINFO (t
), v
);
9100 /* Initialize the vtable for BINFO with the INITS. */
9103 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
9107 layout_vtable_decl (binfo
, vec_safe_length (inits
));
9108 decl
= get_vtbl_decl_for_binfo (binfo
);
9109 initialize_artificial_var (decl
, inits
);
9110 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
9113 /* Build the VTT (virtual table table) for T.
9114 A class requires a VTT if it has virtual bases.
9117 1 - primary virtual pointer for complete object T
9118 2 - secondary VTTs for each direct non-virtual base of T which requires a
9120 3 - secondary virtual pointers for each direct or indirect base of T which
9121 has virtual bases or is reachable via a virtual path from T.
9122 4 - secondary VTTs for each direct or indirect virtual base of T.
9124 Secondary VTTs look like complete object VTTs without part 4. */
9132 vec
<constructor_elt
, va_gc
> *inits
;
9134 /* Build up the initializers for the VTT. */
9136 index
= size_zero_node
;
9137 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
9139 /* If we didn't need a VTT, we're done. */
9143 /* Figure out the type of the VTT. */
9144 type
= build_array_of_n_type (const_ptr_type_node
,
9147 /* Now, build the VTT object itself. */
9148 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
9149 initialize_artificial_var (vtt
, inits
);
9150 /* Add the VTT to the vtables list. */
9151 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
9152 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
9157 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9158 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9159 and CHAIN the vtable pointer for this binfo after construction is
9160 complete. VALUE can also be another BINFO, in which case we recurse. */
9163 binfo_ctor_vtable (tree binfo
)
9169 vt
= BINFO_VTABLE (binfo
);
9170 if (TREE_CODE (vt
) == TREE_LIST
)
9171 vt
= TREE_VALUE (vt
);
9172 if (TREE_CODE (vt
) == TREE_BINFO
)
9181 /* Data for secondary VTT initialization. */
9182 struct secondary_vptr_vtt_init_data
9184 /* Is this the primary VTT? */
9187 /* Current index into the VTT. */
9190 /* Vector of initializers built up. */
9191 vec
<constructor_elt
, va_gc
> *inits
;
9193 /* The type being constructed by this secondary VTT. */
9194 tree type_being_constructed
;
9197 /* Recursively build the VTT-initializer for BINFO (which is in the
9198 hierarchy dominated by T). INITS points to the end of the initializer
9199 list to date. INDEX is the VTT index where the next element will be
9200 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9201 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9202 for virtual bases of T. When it is not so, we build the constructor
9203 vtables for the BINFO-in-T variant. */
9206 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
9212 secondary_vptr_vtt_init_data data
;
9213 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9215 /* We only need VTTs for subobjects with virtual bases. */
9216 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9219 /* We need to use a construction vtable if this is not the primary
9223 build_ctor_vtbl_group (binfo
, t
);
9225 /* Record the offset in the VTT where this sub-VTT can be found. */
9226 BINFO_SUBVTT_INDEX (binfo
) = *index
;
9229 /* Add the address of the primary vtable for the complete object. */
9230 init
= binfo_ctor_vtable (binfo
);
9231 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9234 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9235 BINFO_VPTR_INDEX (binfo
) = *index
;
9237 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
9239 /* Recursively add the secondary VTTs for non-virtual bases. */
9240 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
9241 if (!BINFO_VIRTUAL_P (b
))
9242 build_vtt_inits (b
, t
, inits
, index
);
9244 /* Add secondary virtual pointers for all subobjects of BINFO with
9245 either virtual bases or reachable along a virtual path, except
9246 subobjects that are non-virtual primary bases. */
9247 data
.top_level_p
= top_level_p
;
9248 data
.index
= *index
;
9249 data
.inits
= *inits
;
9250 data
.type_being_constructed
= BINFO_TYPE (binfo
);
9252 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
9254 *index
= data
.index
;
9256 /* data.inits might have grown as we added secondary virtual pointers.
9257 Make sure our caller knows about the new vector. */
9258 *inits
= data
.inits
;
9261 /* Add the secondary VTTs for virtual bases in inheritance graph
9263 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
9265 if (!BINFO_VIRTUAL_P (b
))
9268 build_vtt_inits (b
, t
, inits
, index
);
9271 /* Remove the ctor vtables we created. */
9272 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
9275 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9276 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9279 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
9281 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
9283 /* We don't care about bases that don't have vtables. */
9284 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
9285 return dfs_skip_bases
;
9287 /* We're only interested in proper subobjects of the type being
9289 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
9292 /* We're only interested in bases with virtual bases or reachable
9293 via a virtual path from the type being constructed. */
9294 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9295 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
9296 return dfs_skip_bases
;
9298 /* We're not interested in non-virtual primary bases. */
9299 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
9302 /* Record the index where this secondary vptr can be found. */
9303 if (data
->top_level_p
)
9305 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
9306 BINFO_VPTR_INDEX (binfo
) = data
->index
;
9308 if (BINFO_VIRTUAL_P (binfo
))
9310 /* It's a primary virtual base, and this is not a
9311 construction vtable. Find the base this is primary of in
9312 the inheritance graph, and use that base's vtable
9314 while (BINFO_PRIMARY_P (binfo
))
9315 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
9319 /* Add the initializer for the secondary vptr itself. */
9320 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
9322 /* Advance the vtt index. */
9323 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
9324 TYPE_SIZE_UNIT (ptr_type_node
));
9329 /* Called from build_vtt_inits via dfs_walk. After building
9330 constructor vtables and generating the sub-vtt from them, we need
9331 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9332 binfo of the base whose sub vtt was generated. */
9335 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
9337 tree vtable
= BINFO_VTABLE (binfo
);
9339 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9340 /* If this class has no vtable, none of its bases do. */
9341 return dfs_skip_bases
;
9344 /* This might be a primary base, so have no vtable in this
9348 /* If we scribbled the construction vtable vptr into BINFO, clear it
9350 if (TREE_CODE (vtable
) == TREE_LIST
9351 && (TREE_PURPOSE (vtable
) == (tree
) data
))
9352 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
9357 /* Build the construction vtable group for BINFO which is in the
9358 hierarchy dominated by T. */
9361 build_ctor_vtbl_group (tree binfo
, tree t
)
9367 vec
<constructor_elt
, va_gc
> *v
;
9369 /* See if we've already created this construction vtable group. */
9370 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
9371 if (IDENTIFIER_GLOBAL_VALUE (id
))
9374 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
9375 /* Build a version of VTBL (with the wrong type) for use in
9376 constructing the addresses of secondary vtables in the
9377 construction vtable group. */
9378 vtbl
= build_vtable (t
, id
, ptr_type_node
);
9379 DECL_CONSTRUCTION_VTABLE_P (vtbl
) = 1;
9380 /* Don't export construction vtables from shared libraries. Even on
9381 targets that don't support hidden visibility, this tells
9382 can_refer_decl_in_current_unit_p not to assume that it's safe to
9383 access from a different compilation unit (bz 54314). */
9384 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
9385 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
9388 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
9389 binfo
, vtbl
, t
, &v
);
9391 /* Add the vtables for each of our virtual bases using the vbase in T
9393 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9395 vbase
= TREE_CHAIN (vbase
))
9399 if (!BINFO_VIRTUAL_P (vbase
))
9401 b
= copied_binfo (vbase
, binfo
);
9403 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
9406 /* Figure out the type of the construction vtable. */
9407 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
9409 TREE_TYPE (vtbl
) = type
;
9410 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
9411 layout_decl (vtbl
, 0);
9413 /* Initialize the construction vtable. */
9414 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
9415 initialize_artificial_var (vtbl
, v
);
9416 dump_vtable (t
, binfo
, vtbl
);
9419 /* Add the vtbl initializers for BINFO (and its bases other than
9420 non-virtual primaries) to the list of INITS. BINFO is in the
9421 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9422 the constructor the vtbl inits should be accumulated for. (If this
9423 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9424 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9425 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9426 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9427 but are not necessarily the same in terms of layout. */
9430 accumulate_vtbl_inits (tree binfo
,
9435 vec
<constructor_elt
, va_gc
> **inits
)
9439 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9441 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
9443 /* If it doesn't have a vptr, we don't do anything. */
9444 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
9447 /* If we're building a construction vtable, we're not interested in
9448 subobjects that don't require construction vtables. */
9450 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
9451 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
9454 /* Build the initializers for the BINFO-in-T vtable. */
9455 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
9457 /* Walk the BINFO and its bases. We walk in preorder so that as we
9458 initialize each vtable we can figure out at what offset the
9459 secondary vtable lies from the primary vtable. We can't use
9460 dfs_walk here because we need to iterate through bases of BINFO
9461 and RTTI_BINFO simultaneously. */
9462 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9464 /* Skip virtual bases. */
9465 if (BINFO_VIRTUAL_P (base_binfo
))
9467 accumulate_vtbl_inits (base_binfo
,
9468 BINFO_BASE_BINFO (orig_binfo
, i
),
9469 rtti_binfo
, vtbl
, t
,
9474 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9475 BINFO vtable to L. */
9478 dfs_accumulate_vtbl_inits (tree binfo
,
9483 vec
<constructor_elt
, va_gc
> **l
)
9485 tree vtbl
= NULL_TREE
;
9486 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9490 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
9492 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9493 primary virtual base. If it is not the same primary in
9494 the hierarchy of T, we'll need to generate a ctor vtable
9495 for it, to place at its location in T. If it is the same
9496 primary, we still need a VTT entry for the vtable, but it
9497 should point to the ctor vtable for the base it is a
9498 primary for within the sub-hierarchy of RTTI_BINFO.
9500 There are three possible cases:
9502 1) We are in the same place.
9503 2) We are a primary base within a lost primary virtual base of
9505 3) We are primary to something not a base of RTTI_BINFO. */
9508 tree last
= NULL_TREE
;
9510 /* First, look through the bases we are primary to for RTTI_BINFO
9511 or a virtual base. */
9513 while (BINFO_PRIMARY_P (b
))
9515 b
= BINFO_INHERITANCE_CHAIN (b
);
9517 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9520 /* If we run out of primary links, keep looking down our
9521 inheritance chain; we might be an indirect primary. */
9522 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
9523 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
9527 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9528 base B and it is a base of RTTI_BINFO, this is case 2. In
9529 either case, we share our vtable with LAST, i.e. the
9530 derived-most base within B of which we are a primary. */
9532 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
9533 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9534 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9535 binfo_ctor_vtable after everything's been set up. */
9538 /* Otherwise, this is case 3 and we get our own. */
9540 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
9543 n_inits
= vec_safe_length (*l
);
9550 /* Add the initializer for this vtable. */
9551 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
9552 &non_fn_entries
, l
);
9554 /* Figure out the position to which the VPTR should point. */
9555 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
9556 index
= size_binop (MULT_EXPR
,
9557 TYPE_SIZE_UNIT (vtable_entry_type
),
9558 size_int (non_fn_entries
+ n_inits
));
9559 vtbl
= fold_build_pointer_plus (vtbl
, index
);
9563 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9564 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9565 straighten this out. */
9566 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
9567 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
9568 /* Throw away any unneeded intializers. */
9569 (*l
)->truncate (n_inits
);
9571 /* For an ordinary vtable, set BINFO_VTABLE. */
9572 BINFO_VTABLE (binfo
) = vtbl
;
9575 static GTY(()) tree abort_fndecl_addr
;
9577 /* Construct the initializer for BINFO's virtual function table. BINFO
9578 is part of the hierarchy dominated by T. If we're building a
9579 construction vtable, the ORIG_BINFO is the binfo we should use to
9580 find the actual function pointers to put in the vtable - but they
9581 can be overridden on the path to most-derived in the graph that
9582 ORIG_BINFO belongs. Otherwise,
9583 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9584 BINFO that should be indicated by the RTTI information in the
9585 vtable; it will be a base class of T, rather than T itself, if we
9586 are building a construction vtable.
9588 The value returned is a TREE_LIST suitable for wrapping in a
9589 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9590 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9591 number of non-function entries in the vtable.
9593 It might seem that this function should never be called with a
9594 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9595 base is always subsumed by a derived class vtable. However, when
9596 we are building construction vtables, we do build vtables for
9597 primary bases; we need these while the primary base is being
9601 build_vtbl_initializer (tree binfo
,
9605 int* non_fn_entries_p
,
9606 vec
<constructor_elt
, va_gc
> **inits
)
9612 vec
<tree
, va_gc
> *vbases
;
9615 /* Initialize VID. */
9616 memset (&vid
, 0, sizeof (vid
));
9619 vid
.rtti_binfo
= rtti_binfo
;
9620 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
9621 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
9622 vid
.generate_vcall_entries
= true;
9623 /* The first vbase or vcall offset is at index -3 in the vtable. */
9624 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
9626 /* Add entries to the vtable for RTTI. */
9627 build_rtti_vtbl_entries (binfo
, &vid
);
9629 /* Create an array for keeping track of the functions we've
9630 processed. When we see multiple functions with the same
9631 signature, we share the vcall offsets. */
9632 vec_alloc (vid
.fns
, 32);
9633 /* Add the vcall and vbase offset entries. */
9634 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
9636 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9637 build_vbase_offset_vtbl_entries. */
9638 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
9639 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
9640 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
9642 /* If the target requires padding between data entries, add that now. */
9643 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
9645 int n_entries
= vec_safe_length (vid
.inits
);
9647 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
);
9649 /* Move data entries into their new positions and add padding
9650 after the new positions. Iterate backwards so we don't
9651 overwrite entries that we would need to process later. */
9652 for (ix
= n_entries
- 1;
9653 vid
.inits
->iterate (ix
, &e
);
9657 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
9658 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
9660 (*vid
.inits
)[new_position
] = *e
;
9662 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
9664 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
9665 f
->index
= NULL_TREE
;
9666 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9672 if (non_fn_entries_p
)
9673 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9675 /* The initializers for virtual functions were built up in reverse
9676 order. Straighten them out and add them to the running list in one
9678 jx
= vec_safe_length (*inits
);
9679 vec_safe_grow (*inits
, jx
+ vid
.inits
->length ());
9681 for (ix
= vid
.inits
->length () - 1;
9682 vid
.inits
->iterate (ix
, &e
);
9686 /* Go through all the ordinary virtual functions, building up
9688 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9692 tree fn
, fn_original
;
9693 tree init
= NULL_TREE
;
9697 if (DECL_THUNK_P (fn
))
9699 if (!DECL_NAME (fn
))
9701 if (THUNK_ALIAS (fn
))
9703 fn
= THUNK_ALIAS (fn
);
9706 fn_original
= THUNK_TARGET (fn
);
9709 /* If the only definition of this function signature along our
9710 primary base chain is from a lost primary, this vtable slot will
9711 never be used, so just zero it out. This is important to avoid
9712 requiring extra thunks which cannot be generated with the function.
9714 We first check this in update_vtable_entry_for_fn, so we handle
9715 restored primary bases properly; we also need to do it here so we
9716 zero out unused slots in ctor vtables, rather than filling them
9717 with erroneous values (though harmless, apart from relocation
9719 if (BV_LOST_PRIMARY (v
))
9720 init
= size_zero_node
;
9724 /* Pull the offset for `this', and the function to call, out of
9726 delta
= BV_DELTA (v
);
9727 vcall_index
= BV_VCALL_INDEX (v
);
9729 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9730 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9732 /* You can't call an abstract virtual function; it's abstract.
9733 So, we replace these functions with __pure_virtual. */
9734 if (DECL_PURE_VIRTUAL_P (fn_original
))
9737 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9739 if (abort_fndecl_addr
== NULL
)
9741 = fold_convert (vfunc_ptr_type_node
,
9742 build_fold_addr_expr (fn
));
9743 init
= abort_fndecl_addr
;
9746 /* Likewise for deleted virtuals. */
9747 else if (DECL_DELETED_FN (fn_original
))
9749 fn
= get_identifier ("__cxa_deleted_virtual");
9750 if (!get_global_value_if_present (fn
, &fn
))
9751 fn
= push_library_fn (fn
, (build_function_type_list
9752 (void_type_node
, NULL_TREE
)),
9753 NULL_TREE
, ECF_NORETURN
);
9754 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9755 init
= fold_convert (vfunc_ptr_type_node
,
9756 build_fold_addr_expr (fn
));
9760 if (!integer_zerop (delta
) || vcall_index
)
9762 fn
= make_thunk (fn
, /*this_adjusting=*/1, delta
, vcall_index
);
9763 if (!DECL_NAME (fn
))
9766 /* Take the address of the function, considering it to be of an
9767 appropriate generic type. */
9768 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9769 init
= fold_convert (vfunc_ptr_type_node
,
9770 build_fold_addr_expr (fn
));
9771 /* Don't refer to a virtual destructor from a constructor
9772 vtable or a vtable for an abstract class, since destroying
9773 an object under construction is undefined behavior and we
9774 don't want it to be considered a candidate for speculative
9775 devirtualization. But do create the thunk for ABI
9777 if (DECL_DESTRUCTOR_P (fn_original
)
9778 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9779 || orig_binfo
!= binfo
))
9780 init
= size_zero_node
;
9784 /* And add it to the chain of initializers. */
9785 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9788 if (init
== size_zero_node
)
9789 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9790 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9792 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9794 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9795 fn
, build_int_cst (NULL_TREE
, i
));
9796 TREE_CONSTANT (fdesc
) = 1;
9798 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, fdesc
);
9802 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9806 /* Adds to vid->inits the initializers for the vbase and vcall
9807 offsets in BINFO, which is in the hierarchy dominated by T. */
9810 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9814 /* If this is a derived class, we must first create entries
9815 corresponding to the primary base class. */
9816 b
= get_primary_binfo (binfo
);
9818 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9820 /* Add the vbase entries for this base. */
9821 build_vbase_offset_vtbl_entries (binfo
, vid
);
9822 /* Add the vcall entries for this base. */
9823 build_vcall_offset_vtbl_entries (binfo
, vid
);
9826 /* Returns the initializers for the vbase offset entries in the vtable
9827 for BINFO (which is part of the class hierarchy dominated by T), in
9828 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9829 where the next vbase offset will go. */
9832 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9836 tree non_primary_binfo
;
9838 /* If there are no virtual baseclasses, then there is nothing to
9840 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9845 /* We might be a primary base class. Go up the inheritance hierarchy
9846 until we find the most derived class of which we are a primary base:
9847 it is the offset of that which we need to use. */
9848 non_primary_binfo
= binfo
;
9849 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9853 /* If we have reached a virtual base, then it must be a primary
9854 base (possibly multi-level) of vid->binfo, or we wouldn't
9855 have called build_vcall_and_vbase_vtbl_entries for it. But it
9856 might be a lost primary, so just skip down to vid->binfo. */
9857 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9859 non_primary_binfo
= vid
->binfo
;
9863 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9864 if (get_primary_binfo (b
) != non_primary_binfo
)
9866 non_primary_binfo
= b
;
9869 /* Go through the virtual bases, adding the offsets. */
9870 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9872 vbase
= TREE_CHAIN (vbase
))
9877 if (!BINFO_VIRTUAL_P (vbase
))
9880 /* Find the instance of this virtual base in the complete
9882 b
= copied_binfo (vbase
, binfo
);
9884 /* If we've already got an offset for this virtual base, we
9885 don't need another one. */
9886 if (BINFO_VTABLE_PATH_MARKED (b
))
9888 BINFO_VTABLE_PATH_MARKED (b
) = 1;
9890 /* Figure out where we can find this vbase offset. */
9891 delta
= size_binop (MULT_EXPR
,
9893 fold_convert (ssizetype
,
9894 TYPE_SIZE_UNIT (vtable_entry_type
)));
9895 if (vid
->primary_vtbl_p
)
9896 BINFO_VPTR_FIELD (b
) = delta
;
9898 if (binfo
!= TYPE_BINFO (t
))
9899 /* The vbase offset had better be the same. */
9900 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
9902 /* The next vbase will come at a more negative offset. */
9903 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9904 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9906 /* The initializer is the delta from BINFO to this virtual base.
9907 The vbase offsets go in reverse inheritance-graph order, and
9908 we are walking in inheritance graph order so these end up in
9910 delta
= size_diffop_loc (input_location
,
9911 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
9913 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
9914 fold_build1_loc (input_location
, NOP_EXPR
,
9915 vtable_entry_type
, delta
));
9919 /* Adds the initializers for the vcall offset entries in the vtable
9920 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9924 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9926 /* We only need these entries if this base is a virtual base. We
9927 compute the indices -- but do not add to the vtable -- when
9928 building the main vtable for a class. */
9929 if (binfo
== TYPE_BINFO (vid
->derived
)
9930 || (BINFO_VIRTUAL_P (binfo
)
9931 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9932 correspond to VID->DERIVED), we are building a primary
9933 construction virtual table. Since this is a primary
9934 virtual table, we do not need the vcall offsets for
9936 && binfo
!= vid
->rtti_binfo
))
9938 /* We need a vcall offset for each of the virtual functions in this
9939 vtable. For example:
9941 class A { virtual void f (); };
9942 class B1 : virtual public A { virtual void f (); };
9943 class B2 : virtual public A { virtual void f (); };
9944 class C: public B1, public B2 { virtual void f (); };
9946 A C object has a primary base of B1, which has a primary base of A. A
9947 C also has a secondary base of B2, which no longer has a primary base
9948 of A. So the B2-in-C construction vtable needs a secondary vtable for
9949 A, which will adjust the A* to a B2* to call f. We have no way of
9950 knowing what (or even whether) this offset will be when we define B2,
9951 so we store this "vcall offset" in the A sub-vtable and look it up in
9952 a "virtual thunk" for B2::f.
9954 We need entries for all the functions in our primary vtable and
9955 in our non-virtual bases' secondary vtables. */
9957 /* If we are just computing the vcall indices -- but do not need
9958 the actual entries -- not that. */
9959 if (!BINFO_VIRTUAL_P (binfo
))
9960 vid
->generate_vcall_entries
= false;
9961 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9962 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
9966 /* Build vcall offsets, starting with those for BINFO. */
9969 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
9975 /* Don't walk into virtual bases -- except, of course, for the
9976 virtual base for which we are building vcall offsets. Any
9977 primary virtual base will have already had its offsets generated
9978 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9979 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
9982 /* If BINFO has a primary base, process it first. */
9983 primary_binfo
= get_primary_binfo (binfo
);
9985 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
9987 /* Add BINFO itself to the list. */
9988 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
9990 /* Scan the non-primary bases of BINFO. */
9991 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9992 if (base_binfo
!= primary_binfo
)
9993 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
9996 /* Called from build_vcall_offset_vtbl_entries_r. */
9999 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
10001 /* Make entries for the rest of the virtuals. */
10004 /* The ABI requires that the methods be processed in declaration
10006 for (orig_fn
= TYPE_METHODS (BINFO_TYPE (binfo
));
10008 orig_fn
= DECL_CHAIN (orig_fn
))
10009 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
10010 add_vcall_offset (orig_fn
, binfo
, vid
);
10013 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10016 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
10020 tree derived_entry
;
10022 /* If there is already an entry for a function with the same
10023 signature as FN, then we do not need a second vcall offset.
10024 Check the list of functions already present in the derived
10026 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
10028 if (same_signature_p (derived_entry
, orig_fn
)
10029 /* We only use one vcall offset for virtual destructors,
10030 even though there are two virtual table entries. */
10031 || (DECL_DESTRUCTOR_P (derived_entry
)
10032 && DECL_DESTRUCTOR_P (orig_fn
)))
10036 /* If we are building these vcall offsets as part of building
10037 the vtable for the most derived class, remember the vcall
10039 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
10041 tree_pair_s elt
= {orig_fn
, vid
->index
};
10042 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
10045 /* The next vcall offset will be found at a more negative
10047 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
10048 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
10050 /* Keep track of this function. */
10051 vec_safe_push (vid
->fns
, orig_fn
);
10053 if (vid
->generate_vcall_entries
)
10058 /* Find the overriding function. */
10059 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
10060 if (fn
== error_mark_node
)
10061 vcall_offset
= build_zero_cst (vtable_entry_type
);
10064 base
= TREE_VALUE (fn
);
10066 /* The vbase we're working on is a primary base of
10067 vid->binfo. But it might be a lost primary, so its
10068 BINFO_OFFSET might be wrong, so we just use the
10069 BINFO_OFFSET from vid->binfo. */
10070 vcall_offset
= size_diffop_loc (input_location
,
10071 BINFO_OFFSET (base
),
10072 BINFO_OFFSET (vid
->binfo
));
10073 vcall_offset
= fold_build1_loc (input_location
,
10074 NOP_EXPR
, vtable_entry_type
,
10077 /* Add the initializer to the vtable. */
10078 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
10082 /* Return vtbl initializers for the RTTI entries corresponding to the
10083 BINFO's vtable. The RTTI entries should indicate the object given
10084 by VID->rtti_binfo. */
10087 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
10095 t
= BINFO_TYPE (vid
->rtti_binfo
);
10097 /* To find the complete object, we will first convert to our most
10098 primary base, and then add the offset in the vtbl to that value. */
10099 b
= most_primary_binfo (binfo
);
10100 offset
= size_diffop_loc (input_location
,
10101 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
10103 /* The second entry is the address of the typeinfo object. */
10105 decl
= build_address (get_tinfo_decl (t
));
10107 decl
= integer_zero_node
;
10109 /* Convert the declaration to a type that can be stored in the
10111 init
= build_nop (vfunc_ptr_type_node
, decl
);
10112 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10114 /* Add the offset-to-top entry. It comes earlier in the vtable than
10115 the typeinfo entry. Convert the offset to look like a
10116 function pointer, so that we can put it in the vtable. */
10117 init
= build_nop (vfunc_ptr_type_node
, offset
);
10118 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
10121 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10125 uniquely_derived_from_p (tree parent
, tree type
)
10127 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
10128 return base
&& base
!= error_mark_node
;
10131 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10134 publicly_uniquely_derived_p (tree parent
, tree type
)
10136 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
10138 return base
&& base
!= error_mark_node
;
10141 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10142 class between them, if any. */
10145 common_enclosing_class (tree ctx1
, tree ctx2
)
10147 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
10149 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
10150 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
10153 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10154 TYPE_MARKED_P (t
) = true;
10155 tree found
= NULL_TREE
;
10156 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10157 if (TYPE_MARKED_P (t
))
10162 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
10163 TYPE_MARKED_P (t
) = false;
10167 #include "gt-cp-class.h"