1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2015 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"
32 #include "stringpool.h"
33 #include "stor-layout.h"
40 #include "plugin-api.h"
41 #include "hard-reg-set.h"
46 #include "splay-tree.h"
49 /* The number of nested classes being processed. If we are not in the
50 scope of any class, this is zero. */
52 int current_class_depth
;
54 /* In order to deal with nested classes, we keep a stack of classes.
55 The topmost entry is the innermost class, and is the entry at index
56 CURRENT_CLASS_DEPTH */
58 typedef struct class_stack_node
{
59 /* The name of the class. */
62 /* The _TYPE node for the class. */
65 /* The access specifier pending for new declarations in the scope of
69 /* If were defining TYPE, the names used in this class. */
70 splay_tree names_used
;
72 /* Nonzero if this class is no longer open, because of a call to
75 }* class_stack_node_t
;
77 typedef struct vtbl_init_data_s
79 /* The base for which we're building initializers. */
81 /* The type of the most-derived type. */
83 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
84 unless ctor_vtbl_p is true. */
86 /* The negative-index vtable initializers built up so far. These
87 are in order from least negative index to most negative index. */
88 vec
<constructor_elt
, va_gc
> *inits
;
89 /* The binfo for the virtual base for which we're building
90 vcall offset initializers. */
92 /* The functions in vbase for which we have already provided vcall
94 vec
<tree
, va_gc
> *fns
;
95 /* The vtable index of the next vcall or vbase offset. */
97 /* Nonzero if we are building the initializer for the primary
100 /* Nonzero if we are building the initializer for a construction
103 /* True when adding vcall offset entries to the vtable. False when
104 merely computing the indices. */
105 bool generate_vcall_entries
;
108 /* The type of a function passed to walk_subobject_offsets. */
109 typedef int (*subobject_offset_fn
) (tree
, tree
, splay_tree
);
111 /* The stack itself. This is a dynamically resized array. The
112 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
113 static int current_class_stack_size
;
114 static class_stack_node_t current_class_stack
;
116 /* The size of the largest empty class seen in this translation unit. */
117 static GTY (()) tree sizeof_biggest_empty_class
;
119 /* An array of all local classes present in this translation unit, in
120 declaration order. */
121 vec
<tree
, va_gc
> *local_classes
;
123 static tree
get_vfield_name (tree
);
124 static void finish_struct_anon (tree
);
125 static tree
get_vtable_name (tree
);
126 static void get_basefndecls (tree
, tree
, vec
<tree
> *);
127 static int build_primary_vtable (tree
, tree
);
128 static int build_secondary_vtable (tree
);
129 static void finish_vtbls (tree
);
130 static void modify_vtable_entry (tree
, tree
, tree
, tree
, tree
*);
131 static void finish_struct_bits (tree
);
132 static int alter_access (tree
, tree
, tree
);
133 static void handle_using_decl (tree
, tree
);
134 static tree
dfs_modify_vtables (tree
, void *);
135 static tree
modify_all_vtables (tree
, tree
);
136 static void determine_primary_bases (tree
);
137 static void finish_struct_methods (tree
);
138 static void maybe_warn_about_overly_private_class (tree
);
139 static int method_name_cmp (const void *, const void *);
140 static int resort_method_name_cmp (const void *, const void *);
141 static void add_implicitly_declared_members (tree
, tree
*, int, int);
142 static tree
fixed_type_or_null (tree
, int *, int *);
143 static tree
build_simple_base_path (tree expr
, tree binfo
);
144 static tree
build_vtbl_ref_1 (tree
, tree
);
145 static void build_vtbl_initializer (tree
, tree
, tree
, tree
, int *,
146 vec
<constructor_elt
, va_gc
> **);
147 static int count_fields (tree
);
148 static int add_fields_to_record_type (tree
, struct sorted_fields_type
*, int);
149 static void insert_into_classtype_sorted_fields (tree
, tree
, int);
150 static bool check_bitfield_decl (tree
);
151 static void check_field_decl (tree
, tree
, int *, int *, int *);
152 static void check_field_decls (tree
, tree
*, int *, int *);
153 static tree
*build_base_field (record_layout_info
, tree
, splay_tree
, tree
*);
154 static void build_base_fields (record_layout_info
, splay_tree
, tree
*);
155 static void check_methods (tree
);
156 static void remove_zero_width_bit_fields (tree
);
157 static bool accessible_nvdtor_p (tree
);
158 static void check_bases (tree
, int *, int *);
159 static void check_bases_and_members (tree
);
160 static tree
create_vtable_ptr (tree
, tree
*);
161 static void include_empty_classes (record_layout_info
);
162 static void layout_class_type (tree
, tree
*);
163 static void propagate_binfo_offsets (tree
, tree
);
164 static void layout_virtual_bases (record_layout_info
, splay_tree
);
165 static void build_vbase_offset_vtbl_entries (tree
, vtbl_init_data
*);
166 static void add_vcall_offset_vtbl_entries_r (tree
, vtbl_init_data
*);
167 static void add_vcall_offset_vtbl_entries_1 (tree
, vtbl_init_data
*);
168 static void build_vcall_offset_vtbl_entries (tree
, vtbl_init_data
*);
169 static void add_vcall_offset (tree
, tree
, vtbl_init_data
*);
170 static void layout_vtable_decl (tree
, int);
171 static tree
dfs_find_final_overrider_pre (tree
, void *);
172 static tree
dfs_find_final_overrider_post (tree
, void *);
173 static tree
find_final_overrider (tree
, tree
, tree
);
174 static int make_new_vtable (tree
, tree
);
175 static tree
get_primary_binfo (tree
);
176 static int maybe_indent_hierarchy (FILE *, int, int);
177 static tree
dump_class_hierarchy_r (FILE *, int, tree
, tree
, int);
178 static void dump_class_hierarchy (tree
);
179 static void dump_class_hierarchy_1 (FILE *, int, tree
);
180 static void dump_array (FILE *, tree
);
181 static void dump_vtable (tree
, tree
, tree
);
182 static void dump_vtt (tree
, tree
);
183 static void dump_thunk (FILE *, int, tree
);
184 static tree
build_vtable (tree
, tree
, tree
);
185 static void initialize_vtable (tree
, vec
<constructor_elt
, va_gc
> *);
186 static void layout_nonempty_base_or_field (record_layout_info
,
187 tree
, tree
, splay_tree
);
188 static tree
end_of_class (tree
, int);
189 static bool layout_empty_base (record_layout_info
, tree
, tree
, splay_tree
);
190 static void accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
191 vec
<constructor_elt
, va_gc
> **);
192 static void dfs_accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
193 vec
<constructor_elt
, va_gc
> **);
194 static void build_rtti_vtbl_entries (tree
, vtbl_init_data
*);
195 static void build_vcall_and_vbase_vtbl_entries (tree
, vtbl_init_data
*);
196 static void clone_constructors_and_destructors (tree
);
197 static tree
build_clone (tree
, tree
);
198 static void update_vtable_entry_for_fn (tree
, tree
, tree
, tree
*, unsigned);
199 static void build_ctor_vtbl_group (tree
, tree
);
200 static void build_vtt (tree
);
201 static tree
binfo_ctor_vtable (tree
);
202 static void build_vtt_inits (tree
, tree
, vec
<constructor_elt
, va_gc
> **,
204 static tree
dfs_build_secondary_vptr_vtt_inits (tree
, void *);
205 static tree
dfs_fixup_binfo_vtbls (tree
, void *);
206 static int record_subobject_offset (tree
, tree
, splay_tree
);
207 static int check_subobject_offset (tree
, tree
, splay_tree
);
208 static int walk_subobject_offsets (tree
, subobject_offset_fn
,
209 tree
, splay_tree
, tree
, int);
210 static void record_subobject_offsets (tree
, tree
, splay_tree
, bool);
211 static int layout_conflict_p (tree
, tree
, splay_tree
, int);
212 static int splay_tree_compare_integer_csts (splay_tree_key k1
,
214 static void warn_about_ambiguous_bases (tree
);
215 static bool type_requires_array_cookie (tree
);
216 static bool base_derived_from (tree
, tree
);
217 static int empty_base_at_nonzero_offset_p (tree
, tree
, splay_tree
);
218 static tree
end_of_base (tree
);
219 static tree
get_vcall_index (tree
, tree
);
221 /* Variables shared between class.c and call.c. */
224 int n_vtable_entries
= 0;
225 int n_vtable_searches
= 0;
226 int n_vtable_elems
= 0;
227 int n_convert_harshness
= 0;
228 int n_compute_conversion_costs
= 0;
229 int n_inner_fields_searched
= 0;
231 /* Convert to or from a base subobject. EXPR is an expression of type
232 `A' or `A*', an expression of type `B' or `B*' is returned. To
233 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
234 the B base instance within A. To convert base A to derived B, CODE
235 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
236 In this latter case, A must not be a morally virtual base of B.
237 NONNULL is true if EXPR is known to be non-NULL (this is only
238 needed when EXPR is of pointer type). CV qualifiers are preserved
242 build_base_path (enum tree_code code
,
246 tsubst_flags_t complain
)
248 tree v_binfo
= NULL_TREE
;
249 tree d_binfo
= NULL_TREE
;
253 tree null_test
= NULL
;
254 tree ptr_target_type
;
256 int want_pointer
= TYPE_PTR_P (TREE_TYPE (expr
));
257 bool has_empty
= false;
261 if (expr
== error_mark_node
|| binfo
== error_mark_node
|| !binfo
)
262 return error_mark_node
;
264 for (probe
= binfo
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
267 if (is_empty_class (BINFO_TYPE (probe
)))
269 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
273 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
275 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
277 if (code
== PLUS_EXPR
278 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo
), probe
))
280 /* This can happen when adjust_result_of_qualified_name_lookup can't
281 find a unique base binfo in a call to a member function. We
282 couldn't give the diagnostic then since we might have been calling
283 a static member function, so we do it now. */
284 if (complain
& tf_error
)
286 tree base
= lookup_base (probe
, BINFO_TYPE (d_binfo
),
287 ba_unique
, NULL
, complain
);
288 gcc_assert (base
== error_mark_node
);
290 return error_mark_node
;
293 gcc_assert ((code
== MINUS_EXPR
294 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), probe
))
295 || code
== PLUS_EXPR
);
297 if (binfo
== d_binfo
)
301 if (code
== MINUS_EXPR
&& v_binfo
)
303 if (complain
& tf_error
)
305 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (v_binfo
)))
308 error ("cannot convert from pointer to base class %qT to "
309 "pointer to derived class %qT because the base is "
310 "virtual", BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
312 error ("cannot convert from base class %qT to derived "
313 "class %qT because the base is virtual",
314 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
));
319 error ("cannot convert from pointer to base class %qT to "
320 "pointer to derived class %qT via virtual base %qT",
321 BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
),
322 BINFO_TYPE (v_binfo
));
324 error ("cannot convert from base class %qT to derived "
325 "class %qT via virtual base %qT", BINFO_TYPE (binfo
),
326 BINFO_TYPE (d_binfo
), BINFO_TYPE (v_binfo
));
329 return error_mark_node
;
334 rvalue
= !real_lvalue_p (expr
);
335 /* This must happen before the call to save_expr. */
336 expr
= cp_build_addr_expr (expr
, complain
);
339 expr
= mark_rvalue_use (expr
);
341 offset
= BINFO_OFFSET (binfo
);
342 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
343 target_type
= code
== PLUS_EXPR
? BINFO_TYPE (binfo
) : BINFO_TYPE (d_binfo
);
344 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
345 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
346 expression returned matches the input. */
347 target_type
= cp_build_qualified_type
348 (target_type
, cp_type_quals (TREE_TYPE (TREE_TYPE (expr
))));
349 ptr_target_type
= build_pointer_type (target_type
);
351 /* Do we need to look in the vtable for the real offset? */
352 virtual_access
= (v_binfo
&& fixed_type_p
<= 0);
354 /* Don't bother with the calculations inside sizeof; they'll ICE if the
355 source type is incomplete and the pointer value doesn't matter. In a
356 template (even in instantiate_non_dependent_expr), we don't have vtables
357 set up properly yet, and the value doesn't matter there either; we're
358 just interested in the result of overload resolution. */
359 if (cp_unevaluated_operand
!= 0
360 || in_template_function ())
362 expr
= build_nop (ptr_target_type
, expr
);
366 /* If we're in an NSDMI, we don't have the full constructor context yet
367 that we need for converting to a virtual base, so just build a stub
368 CONVERT_EXPR and expand it later in bot_replace. */
369 if (virtual_access
&& fixed_type_p
< 0
370 && current_scope () != current_function_decl
)
372 expr
= build1 (CONVERT_EXPR
, ptr_target_type
, expr
);
373 CONVERT_EXPR_VBASE_PATH (expr
) = true;
377 /* Do we need to check for a null pointer? */
378 if (want_pointer
&& !nonnull
)
380 /* If we know the conversion will not actually change the value
381 of EXPR, then we can avoid testing the expression for NULL.
382 We have to avoid generating a COMPONENT_REF for a base class
383 field, because other parts of the compiler know that such
384 expressions are always non-NULL. */
385 if (!virtual_access
&& integer_zerop (offset
))
386 return build_nop (ptr_target_type
, expr
);
387 null_test
= error_mark_node
;
390 /* Protect against multiple evaluation if necessary. */
391 if (TREE_SIDE_EFFECTS (expr
) && (null_test
|| virtual_access
))
392 expr
= save_expr (expr
);
394 /* Now that we've saved expr, build the real null test. */
397 tree zero
= cp_convert (TREE_TYPE (expr
), nullptr_node
, complain
);
398 null_test
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
402 /* If this is a simple base reference, express it as a COMPONENT_REF. */
403 if (code
== PLUS_EXPR
&& !virtual_access
404 /* We don't build base fields for empty bases, and they aren't very
405 interesting to the optimizers anyway. */
408 expr
= cp_build_indirect_ref (expr
, RO_NULL
, complain
);
409 expr
= build_simple_base_path (expr
, binfo
);
413 expr
= build_address (expr
);
414 target_type
= TREE_TYPE (expr
);
420 /* Going via virtual base V_BINFO. We need the static offset
421 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
422 V_BINFO. That offset is an entry in D_BINFO's vtable. */
425 if (fixed_type_p
< 0 && in_base_initializer
)
427 /* In a base member initializer, we cannot rely on the
428 vtable being set up. We have to indirect via the
432 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
433 t
= build_pointer_type (t
);
434 v_offset
= convert (t
, current_vtt_parm
);
435 v_offset
= cp_build_indirect_ref (v_offset
, RO_NULL
, complain
);
440 if ((flag_sanitize
& SANITIZE_VPTR
) && fixed_type_p
== 0)
442 t
= cp_ubsan_maybe_instrument_cast_to_vbase (input_location
,
447 v_offset
= build_vfield_ref (cp_build_indirect_ref (t
, RO_NULL
,
449 TREE_TYPE (TREE_TYPE (expr
)));
452 if (v_offset
== error_mark_node
)
453 return error_mark_node
;
455 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
456 v_offset
= build1 (NOP_EXPR
,
457 build_pointer_type (ptrdiff_type_node
),
459 v_offset
= cp_build_indirect_ref (v_offset
, RO_NULL
, complain
);
460 TREE_CONSTANT (v_offset
) = 1;
462 offset
= convert_to_integer (ptrdiff_type_node
,
463 size_diffop_loc (input_location
, offset
,
464 BINFO_OFFSET (v_binfo
)));
466 if (!integer_zerop (offset
))
467 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
469 if (fixed_type_p
< 0)
470 /* Negative fixed_type_p means this is a constructor or destructor;
471 virtual base layout is fixed in in-charge [cd]tors, but not in
473 offset
= build3 (COND_EXPR
, ptrdiff_type_node
,
474 build2 (EQ_EXPR
, boolean_type_node
,
475 current_in_charge_parm
, integer_zero_node
),
477 convert_to_integer (ptrdiff_type_node
,
478 BINFO_OFFSET (binfo
)));
484 target_type
= ptr_target_type
;
486 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
488 if (!integer_zerop (offset
))
490 offset
= fold_convert (sizetype
, offset
);
491 if (code
== MINUS_EXPR
)
492 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
493 expr
= fold_build_pointer_plus (expr
, offset
);
501 expr
= cp_build_indirect_ref (expr
, RO_NULL
, complain
);
508 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
, expr
,
509 build_zero_cst (target_type
));
514 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
515 Perform a derived-to-base conversion by recursively building up a
516 sequence of COMPONENT_REFs to the appropriate base fields. */
519 build_simple_base_path (tree expr
, tree binfo
)
521 tree type
= BINFO_TYPE (binfo
);
522 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
525 if (d_binfo
== NULL_TREE
)
529 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
531 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
532 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
533 an lvalue in the front end; only _DECLs and _REFs are lvalues
535 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
537 expr
= cp_build_indirect_ref (temp
, RO_NULL
, tf_warning_or_error
);
543 expr
= build_simple_base_path (expr
, d_binfo
);
545 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
546 field
; field
= DECL_CHAIN (field
))
547 /* Is this the base field created by build_base_field? */
548 if (TREE_CODE (field
) == FIELD_DECL
549 && DECL_FIELD_IS_BASE (field
)
550 && TREE_TYPE (field
) == type
551 /* If we're looking for a field in the most-derived class,
552 also check the field offset; we can have two base fields
553 of the same type if one is an indirect virtual base and one
554 is a direct non-virtual base. */
555 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
556 || tree_int_cst_equal (byte_position (field
),
557 BINFO_OFFSET (binfo
))))
559 /* We don't use build_class_member_access_expr here, as that
560 has unnecessary checks, and more importantly results in
561 recursive calls to dfs_walk_once. */
562 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
564 expr
= build3 (COMPONENT_REF
,
565 cp_build_qualified_type (type
, type_quals
),
566 expr
, field
, NULL_TREE
);
567 expr
= fold_if_not_in_template (expr
);
569 /* Mark the expression const or volatile, as appropriate.
570 Even though we've dealt with the type above, we still have
571 to mark the expression itself. */
572 if (type_quals
& TYPE_QUAL_CONST
)
573 TREE_READONLY (expr
) = 1;
574 if (type_quals
& TYPE_QUAL_VOLATILE
)
575 TREE_THIS_VOLATILE (expr
) = 1;
580 /* Didn't find the base field?!? */
584 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
585 type is a class type or a pointer to a class type. In the former
586 case, TYPE is also a class type; in the latter it is another
587 pointer type. If CHECK_ACCESS is true, an error message is emitted
588 if TYPE is inaccessible. If OBJECT has pointer type, the value is
589 assumed to be non-NULL. */
592 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
593 tsubst_flags_t complain
)
598 if (TYPE_PTR_P (TREE_TYPE (object
)))
600 object_type
= TREE_TYPE (TREE_TYPE (object
));
601 type
= TREE_TYPE (type
);
604 object_type
= TREE_TYPE (object
);
606 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
608 if (!binfo
|| binfo
== error_mark_node
)
609 return error_mark_node
;
611 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
614 /* EXPR is an expression with unqualified class type. BASE is a base
615 binfo of that class type. Returns EXPR, converted to the BASE
616 type. This function assumes that EXPR is the most derived class;
617 therefore virtual bases can be found at their static offsets. */
620 convert_to_base_statically (tree expr
, tree base
)
624 expr_type
= TREE_TYPE (expr
);
625 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
627 /* If this is a non-empty base, use a COMPONENT_REF. */
628 if (!is_empty_class (BINFO_TYPE (base
)))
629 return build_simple_base_path (expr
, base
);
631 /* We use fold_build2 and fold_convert below to simplify the trees
632 provided to the optimizers. It is not safe to call these functions
633 when processing a template because they do not handle C++-specific
635 gcc_assert (!processing_template_decl
);
636 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
637 if (!integer_zerop (BINFO_OFFSET (base
)))
638 expr
= fold_build_pointer_plus_loc (input_location
,
639 expr
, BINFO_OFFSET (base
));
640 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
641 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
649 build_vfield_ref (tree datum
, tree type
)
651 tree vfield
, vcontext
;
653 if (datum
== error_mark_node
654 /* Can happen in case of duplicate base types (c++/59082). */
655 || !TYPE_VFIELD (type
))
656 return error_mark_node
;
658 /* First, convert to the requested type. */
659 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
660 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
661 /*nonnull=*/true, tf_warning_or_error
);
663 /* Second, the requested type may not be the owner of its own vptr.
664 If not, convert to the base class that owns it. We cannot use
665 convert_to_base here, because VCONTEXT may appear more than once
666 in the inheritance hierarchy of TYPE, and thus direct conversion
667 between the types may be ambiguous. Following the path back up
668 one step at a time via primary bases avoids the problem. */
669 vfield
= TYPE_VFIELD (type
);
670 vcontext
= DECL_CONTEXT (vfield
);
671 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
673 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
674 type
= TREE_TYPE (datum
);
677 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
680 /* Given an object INSTANCE, return an expression which yields the
681 vtable element corresponding to INDEX. There are many special
682 cases for INSTANCE which we take care of here, mainly to avoid
683 creating extra tree nodes when we don't have to. */
686 build_vtbl_ref_1 (tree instance
, tree idx
)
689 tree vtbl
= NULL_TREE
;
691 /* Try to figure out what a reference refers to, and
692 access its virtual function table directly. */
695 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
697 tree basetype
= non_reference (TREE_TYPE (instance
));
699 if (fixed_type
&& !cdtorp
)
701 tree binfo
= lookup_base (fixed_type
, basetype
,
702 ba_unique
, NULL
, tf_none
);
703 if (binfo
&& binfo
!= error_mark_node
)
704 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
708 vtbl
= build_vfield_ref (instance
, basetype
);
710 aref
= build_array_ref (input_location
, vtbl
, idx
);
711 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
717 build_vtbl_ref (tree instance
, tree idx
)
719 tree aref
= build_vtbl_ref_1 (instance
, idx
);
724 /* Given a stable object pointer INSTANCE_PTR, return an expression which
725 yields a function pointer corresponding to vtable element INDEX. */
728 build_vfn_ref (tree instance_ptr
, tree idx
)
732 aref
= build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr
, RO_NULL
,
733 tf_warning_or_error
),
736 /* When using function descriptors, the address of the
737 vtable entry is treated as a function pointer. */
738 if (TARGET_VTABLE_USES_DESCRIPTORS
)
739 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
740 cp_build_addr_expr (aref
, tf_warning_or_error
));
742 /* Remember this as a method reference, for later devirtualization. */
743 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
748 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
749 for the given TYPE. */
752 get_vtable_name (tree type
)
754 return mangle_vtbl_for_type (type
);
757 /* DECL is an entity associated with TYPE, like a virtual table or an
758 implicitly generated constructor. Determine whether or not DECL
759 should have external or internal linkage at the object file
760 level. This routine does not deal with COMDAT linkage and other
761 similar complexities; it simply sets TREE_PUBLIC if it possible for
762 entities in other translation units to contain copies of DECL, in
766 set_linkage_according_to_type (tree
/*type*/, tree decl
)
768 TREE_PUBLIC (decl
) = 1;
769 determine_visibility (decl
);
772 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
773 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
774 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
777 build_vtable (tree class_type
, tree name
, tree vtable_type
)
781 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
782 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
783 now to avoid confusion in mangle_decl. */
784 SET_DECL_ASSEMBLER_NAME (decl
, name
);
785 DECL_CONTEXT (decl
) = class_type
;
786 DECL_ARTIFICIAL (decl
) = 1;
787 TREE_STATIC (decl
) = 1;
788 TREE_READONLY (decl
) = 1;
789 DECL_VIRTUAL_P (decl
) = 1;
790 DECL_ALIGN (decl
) = TARGET_VTABLE_ENTRY_ALIGN
;
791 DECL_USER_ALIGN (decl
) = true;
792 DECL_VTABLE_OR_VTT_P (decl
) = 1;
793 set_linkage_according_to_type (class_type
, decl
);
794 /* The vtable has not been defined -- yet. */
795 DECL_EXTERNAL (decl
) = 1;
796 DECL_NOT_REALLY_EXTERN (decl
) = 1;
798 /* Mark the VAR_DECL node representing the vtable itself as a
799 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
800 is rather important that such things be ignored because any
801 effort to actually generate DWARF for them will run into
802 trouble when/if we encounter code like:
805 struct S { virtual void member (); };
807 because the artificial declaration of the vtable itself (as
808 manufactured by the g++ front end) will say that the vtable is
809 a static member of `S' but only *after* the debug output for
810 the definition of `S' has already been output. This causes
811 grief because the DWARF entry for the definition of the vtable
812 will try to refer back to an earlier *declaration* of the
813 vtable as a static member of `S' and there won't be one. We
814 might be able to arrange to have the "vtable static member"
815 attached to the member list for `S' before the debug info for
816 `S' get written (which would solve the problem) but that would
817 require more intrusive changes to the g++ front end. */
818 DECL_IGNORED_P (decl
) = 1;
823 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
824 or even complete. If this does not exist, create it. If COMPLETE is
825 nonzero, then complete the definition of it -- that will render it
826 impossible to actually build the vtable, but is useful to get at those
827 which are known to exist in the runtime. */
830 get_vtable_decl (tree type
, int complete
)
834 if (CLASSTYPE_VTABLES (type
))
835 return CLASSTYPE_VTABLES (type
);
837 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
838 CLASSTYPE_VTABLES (type
) = decl
;
842 DECL_EXTERNAL (decl
) = 1;
843 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
849 /* Build the primary virtual function table for TYPE. If BINFO is
850 non-NULL, build the vtable starting with the initial approximation
851 that it is the same as the one which is the head of the association
852 list. Returns a nonzero value if a new vtable is actually
856 build_primary_vtable (tree binfo
, tree type
)
861 decl
= get_vtable_decl (type
, /*complete=*/0);
865 if (BINFO_NEW_VTABLE_MARKED (binfo
))
866 /* We have already created a vtable for this base, so there's
867 no need to do it again. */
870 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
871 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
872 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
873 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
877 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
878 virtuals
= NULL_TREE
;
881 if (GATHER_STATISTICS
)
884 n_vtable_elems
+= list_length (virtuals
);
887 /* Initialize the association list for this type, based
888 on our first approximation. */
889 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
890 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
891 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
895 /* Give BINFO a new virtual function table which is initialized
896 with a skeleton-copy of its original initialization. The only
897 entry that changes is the `delta' entry, so we can really
898 share a lot of structure.
900 FOR_TYPE is the most derived type which caused this table to
903 Returns nonzero if we haven't met BINFO before.
905 The order in which vtables are built (by calling this function) for
906 an object must remain the same, otherwise a binary incompatibility
910 build_secondary_vtable (tree binfo
)
912 if (BINFO_NEW_VTABLE_MARKED (binfo
))
913 /* We already created a vtable for this base. There's no need to
917 /* Remember that we've created a vtable for this BINFO, so that we
918 don't try to do so again. */
919 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
921 /* Make fresh virtual list, so we can smash it later. */
922 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
924 /* Secondary vtables are laid out as part of the same structure as
925 the primary vtable. */
926 BINFO_VTABLE (binfo
) = NULL_TREE
;
930 /* Create a new vtable for BINFO which is the hierarchy dominated by
931 T. Return nonzero if we actually created a new vtable. */
934 make_new_vtable (tree t
, tree binfo
)
936 if (binfo
== TYPE_BINFO (t
))
937 /* In this case, it is *type*'s vtable we are modifying. We start
938 with the approximation that its vtable is that of the
939 immediate base class. */
940 return build_primary_vtable (binfo
, t
);
942 /* This is our very own copy of `basetype' to play with. Later,
943 we will fill in all the virtual functions that override the
944 virtual functions in these base classes which are not defined
945 by the current type. */
946 return build_secondary_vtable (binfo
);
949 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
950 (which is in the hierarchy dominated by T) list FNDECL as its
951 BV_FN. DELTA is the required constant adjustment from the `this'
952 pointer where the vtable entry appears to the `this' required when
953 the function is actually called. */
956 modify_vtable_entry (tree t
,
966 if (fndecl
!= BV_FN (v
)
967 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
969 /* We need a new vtable for BINFO. */
970 if (make_new_vtable (t
, binfo
))
972 /* If we really did make a new vtable, we also made a copy
973 of the BINFO_VIRTUALS list. Now, we have to find the
974 corresponding entry in that list. */
975 *virtuals
= BINFO_VIRTUALS (binfo
);
976 while (BV_FN (*virtuals
) != BV_FN (v
))
977 *virtuals
= TREE_CHAIN (*virtuals
);
981 BV_DELTA (v
) = delta
;
982 BV_VCALL_INDEX (v
) = NULL_TREE
;
988 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
989 the USING_DECL naming METHOD. Returns true if the method could be
990 added to the method vec. */
993 add_method (tree type
, tree method
, tree using_decl
)
997 bool template_conv_p
= false;
999 vec
<tree
, va_gc
> *method_vec
;
1001 bool insert_p
= false;
1005 if (method
== error_mark_node
)
1008 complete_p
= COMPLETE_TYPE_P (type
);
1009 conv_p
= DECL_CONV_FN_P (method
);
1011 template_conv_p
= (TREE_CODE (method
) == TEMPLATE_DECL
1012 && DECL_TEMPLATE_CONV_FN_P (method
));
1014 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1017 /* Make a new method vector. We start with 8 entries. We must
1018 allocate at least two (for constructors and destructors), and
1019 we're going to end up with an assignment operator at some
1021 vec_alloc (method_vec
, 8);
1022 /* Create slots for constructors and destructors. */
1023 method_vec
->quick_push (NULL_TREE
);
1024 method_vec
->quick_push (NULL_TREE
);
1025 CLASSTYPE_METHOD_VEC (type
) = method_vec
;
1028 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1029 grok_special_member_properties (method
);
1031 /* Constructors and destructors go in special slots. */
1032 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method
))
1033 slot
= CLASSTYPE_CONSTRUCTOR_SLOT
;
1034 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
1036 slot
= CLASSTYPE_DESTRUCTOR_SLOT
;
1038 if (TYPE_FOR_JAVA (type
))
1040 if (!DECL_ARTIFICIAL (method
))
1041 error ("Java class %qT cannot have a destructor", type
);
1042 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1043 error ("Java class %qT cannot have an implicit non-trivial "
1053 /* See if we already have an entry with this name. */
1054 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1055 vec_safe_iterate (method_vec
, slot
, &m
);
1058 m
= OVL_CURRENT (m
);
1059 if (template_conv_p
)
1061 if (TREE_CODE (m
) == TEMPLATE_DECL
1062 && DECL_TEMPLATE_CONV_FN_P (m
))
1066 if (conv_p
&& !DECL_CONV_FN_P (m
))
1068 if (DECL_NAME (m
) == DECL_NAME (method
))
1074 && !DECL_CONV_FN_P (m
)
1075 && DECL_NAME (m
) > DECL_NAME (method
))
1079 current_fns
= insert_p
? NULL_TREE
: (*method_vec
)[slot
];
1081 /* Check to see if we've already got this method. */
1082 for (fns
= current_fns
; fns
; fns
= OVL_NEXT (fns
))
1084 tree fn
= OVL_CURRENT (fns
);
1090 if (TREE_CODE (fn
) != TREE_CODE (method
))
1093 /* [over.load] Member function declarations with the
1094 same name and the same parameter types cannot be
1095 overloaded if any of them is a static member
1096 function declaration.
1098 [over.load] Member function declarations with the same name and
1099 the same parameter-type-list as well as member function template
1100 declarations with the same name, the same parameter-type-list, and
1101 the same template parameter lists cannot be overloaded if any of
1102 them, but not all, have a ref-qualifier.
1104 [namespace.udecl] When a using-declaration brings names
1105 from a base class into a derived class scope, member
1106 functions in the derived class override and/or hide member
1107 functions with the same name and parameter types in a base
1108 class (rather than conflicting). */
1109 fn_type
= TREE_TYPE (fn
);
1110 method_type
= TREE_TYPE (method
);
1111 parms1
= TYPE_ARG_TYPES (fn_type
);
1112 parms2
= TYPE_ARG_TYPES (method_type
);
1114 /* Compare the quals on the 'this' parm. Don't compare
1115 the whole types, as used functions are treated as
1116 coming from the using class in overload resolution. */
1117 if (! DECL_STATIC_FUNCTION_P (fn
)
1118 && ! DECL_STATIC_FUNCTION_P (method
)
1119 /* Either both or neither need to be ref-qualified for
1120 differing quals to allow overloading. */
1121 && (FUNCTION_REF_QUALIFIED (fn_type
)
1122 == FUNCTION_REF_QUALIFIED (method_type
))
1123 && (type_memfn_quals (fn_type
) != type_memfn_quals (method_type
)
1124 || type_memfn_rqual (fn_type
) != type_memfn_rqual (method_type
)))
1127 /* For templates, the return type and template parameters
1128 must be identical. */
1129 if (TREE_CODE (fn
) == TEMPLATE_DECL
1130 && (!same_type_p (TREE_TYPE (fn_type
),
1131 TREE_TYPE (method_type
))
1132 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1133 DECL_TEMPLATE_PARMS (method
))))
1136 if (! DECL_STATIC_FUNCTION_P (fn
))
1137 parms1
= TREE_CHAIN (parms1
);
1138 if (! DECL_STATIC_FUNCTION_P (method
))
1139 parms2
= TREE_CHAIN (parms2
);
1141 if (compparms (parms1
, parms2
)
1142 && (!DECL_CONV_FN_P (fn
)
1143 || same_type_p (TREE_TYPE (fn_type
),
1144 TREE_TYPE (method_type
))))
1146 /* For function versions, their parms and types match
1147 but they are not duplicates. Record function versions
1148 as and when they are found. extern "C" functions are
1149 not treated as versions. */
1150 if (TREE_CODE (fn
) == FUNCTION_DECL
1151 && TREE_CODE (method
) == FUNCTION_DECL
1152 && !DECL_EXTERN_C_P (fn
)
1153 && !DECL_EXTERN_C_P (method
)
1154 && targetm
.target_option
.function_versions (fn
, method
))
1156 /* Mark functions as versions if necessary. Modify the mangled
1157 decl name if necessary. */
1158 if (!DECL_FUNCTION_VERSIONED (fn
))
1160 DECL_FUNCTION_VERSIONED (fn
) = 1;
1161 if (DECL_ASSEMBLER_NAME_SET_P (fn
))
1164 if (!DECL_FUNCTION_VERSIONED (method
))
1166 DECL_FUNCTION_VERSIONED (method
) = 1;
1167 if (DECL_ASSEMBLER_NAME_SET_P (method
))
1168 mangle_decl (method
);
1170 cgraph_node::record_function_versions (fn
, method
);
1173 if (DECL_INHERITED_CTOR_BASE (method
))
1175 if (DECL_INHERITED_CTOR_BASE (fn
))
1177 error_at (DECL_SOURCE_LOCATION (method
),
1178 "%q#D inherited from %qT", method
,
1179 DECL_INHERITED_CTOR_BASE (method
));
1180 error_at (DECL_SOURCE_LOCATION (fn
),
1181 "conflicts with version inherited from %qT",
1182 DECL_INHERITED_CTOR_BASE (fn
));
1184 /* Otherwise defer to the other function. */
1189 if (DECL_CONTEXT (fn
) == type
)
1190 /* Defer to the local function. */
1195 error ("%q+#D cannot be overloaded", method
);
1196 error ("with %q+#D", fn
);
1199 /* We don't call duplicate_decls here to merge the
1200 declarations because that will confuse things if the
1201 methods have inline definitions. In particular, we
1202 will crash while processing the definitions. */
1207 /* A class should never have more than one destructor. */
1208 if (current_fns
&& DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
1211 /* Add the new binding. */
1214 overload
= ovl_cons (method
, current_fns
);
1215 OVL_USED (overload
) = true;
1218 overload
= build_overload (method
, current_fns
);
1221 TYPE_HAS_CONVERSION (type
) = 1;
1222 else if (slot
>= CLASSTYPE_FIRST_CONVERSION_SLOT
&& !complete_p
)
1223 push_class_level_binding (DECL_NAME (method
), overload
);
1229 /* We only expect to add few methods in the COMPLETE_P case, so
1230 just make room for one more method in that case. */
1232 reallocated
= vec_safe_reserve_exact (method_vec
, 1);
1234 reallocated
= vec_safe_reserve (method_vec
, 1);
1236 CLASSTYPE_METHOD_VEC (type
) = method_vec
;
1237 if (slot
== method_vec
->length ())
1238 method_vec
->quick_push (overload
);
1240 method_vec
->quick_insert (slot
, overload
);
1243 /* Replace the current slot. */
1244 (*method_vec
)[slot
] = overload
;
1248 /* Subroutines of finish_struct. */
1250 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1251 legit, otherwise return 0. */
1254 alter_access (tree t
, tree fdecl
, tree access
)
1258 if (!DECL_LANG_SPECIFIC (fdecl
))
1259 retrofit_lang_decl (fdecl
);
1261 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1263 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1266 if (TREE_VALUE (elem
) != access
)
1268 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1269 error ("conflicting access specifications for method"
1270 " %q+D, ignored", TREE_TYPE (fdecl
));
1272 error ("conflicting access specifications for field %qE, ignored",
1277 /* They're changing the access to the same thing they changed
1278 it to before. That's OK. */
1284 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1285 tf_warning_or_error
);
1286 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1292 /* Process the USING_DECL, which is a member of T. */
1295 handle_using_decl (tree using_decl
, tree t
)
1297 tree decl
= USING_DECL_DECLS (using_decl
);
1298 tree name
= DECL_NAME (using_decl
);
1300 = TREE_PRIVATE (using_decl
) ? access_private_node
1301 : TREE_PROTECTED (using_decl
) ? access_protected_node
1302 : access_public_node
;
1303 tree flist
= NULL_TREE
;
1306 gcc_assert (!processing_template_decl
&& decl
);
1308 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1309 tf_warning_or_error
);
1312 if (is_overloaded_fn (old_value
))
1313 old_value
= OVL_CURRENT (old_value
);
1315 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1318 old_value
= NULL_TREE
;
1321 cp_emit_debug_info_for_using (decl
, t
);
1323 if (is_overloaded_fn (decl
))
1328 else if (is_overloaded_fn (old_value
))
1331 /* It's OK to use functions from a base when there are functions with
1332 the same name already present in the current class. */;
1335 error ("%q+D invalid in %q#T", using_decl
, t
);
1336 error (" because of local method %q+#D with same name",
1337 OVL_CURRENT (old_value
));
1341 else if (!DECL_ARTIFICIAL (old_value
))
1343 error ("%q+D invalid in %q#T", using_decl
, t
);
1344 error (" because of local member %q+#D with same name", old_value
);
1348 /* Make type T see field decl FDECL with access ACCESS. */
1350 for (; flist
; flist
= OVL_NEXT (flist
))
1352 add_method (t
, OVL_CURRENT (flist
), using_decl
);
1353 alter_access (t
, OVL_CURRENT (flist
), access
);
1356 alter_access (t
, decl
, access
);
1359 /* Data structure for find_abi_tags_r, below. */
1363 tree t
; // The type that we're checking for missing tags.
1364 tree subob
; // The subobject of T that we're getting tags from.
1365 tree tags
; // error_mark_node for diagnostics, or a list of missing tags.
1368 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1369 in the context of P. TAG can be either an identifier (the DECL_NAME of
1370 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1373 check_tag (tree tag
, tree id
, tree
*tp
, abi_tag_data
*p
)
1375 if (!IDENTIFIER_MARKED (id
))
1377 if (p
->tags
!= error_mark_node
)
1379 /* We're collecting tags from template arguments or from
1380 the type of a variable or function return type. */
1381 p
->tags
= tree_cons (NULL_TREE
, tag
, p
->tags
);
1383 /* Don't inherit this tag multiple times. */
1384 IDENTIFIER_MARKED (id
) = true;
1388 /* Tags inherited from type template arguments are only used
1389 to avoid warnings. */
1390 ABI_TAG_IMPLICIT (p
->tags
) = true;
1393 /* For functions and variables we want to warn, too. */
1396 /* Otherwise we're diagnosing missing tags. */
1397 if (TREE_CODE (p
->t
) == FUNCTION_DECL
)
1399 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1400 "that %qT (used in its return type) has",
1402 inform (location_of (*tp
), "%qT declared here", *tp
);
1404 else if (TREE_CODE (p
->t
) == VAR_DECL
)
1406 if (warning (OPT_Wabi_tag
, "%qD inherits the %E ABI tag "
1407 "that %qT (used in its type) has", p
->t
, tag
, *tp
))
1408 inform (location_of (*tp
), "%qT declared here", *tp
);
1410 else if (TYPE_P (p
->subob
))
1412 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1413 "that base %qT has", p
->t
, tag
, p
->subob
))
1414 inform (location_of (p
->subob
), "%qT declared here",
1419 if (warning (OPT_Wabi_tag
, "%qT does not have the %E ABI tag "
1420 "that %qT (used in the type of %qD) has",
1421 p
->t
, tag
, *tp
, p
->subob
))
1423 inform (location_of (p
->subob
), "%qD declared here",
1425 inform (location_of (*tp
), "%qT declared here", *tp
);
1431 /* Find all the ABI tags in the attribute list ATTR and either call
1432 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1435 mark_or_check_attr_tags (tree attr
, tree
*tp
, abi_tag_data
*p
, bool val
)
1439 for (; (attr
= lookup_attribute ("abi_tag", attr
));
1440 attr
= TREE_CHAIN (attr
))
1441 for (tree list
= TREE_VALUE (attr
); list
;
1442 list
= TREE_CHAIN (list
))
1444 tree tag
= TREE_VALUE (list
);
1445 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1447 check_tag (tag
, id
, tp
, p
);
1449 IDENTIFIER_MARKED (id
) = val
;
1453 /* Find all the ABI tags on T and its enclosing scopes and either call
1454 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1457 mark_or_check_tags (tree t
, tree
*tp
, abi_tag_data
*p
, bool val
)
1459 while (t
!= global_namespace
)
1464 attr
= TYPE_ATTRIBUTES (t
);
1465 t
= CP_TYPE_CONTEXT (t
);
1469 attr
= DECL_ATTRIBUTES (t
);
1470 t
= CP_DECL_CONTEXT (t
);
1472 mark_or_check_attr_tags (attr
, tp
, p
, val
);
1476 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1477 types with ABI tags, add the corresponding identifiers to the VEC in
1478 *DATA and set IDENTIFIER_MARKED. */
1481 find_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1483 if (!OVERLOAD_TYPE_P (*tp
))
1486 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1487 anyway, but let's make sure of it. */
1488 *walk_subtrees
= false;
1490 abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1492 mark_or_check_tags (*tp
, tp
, p
, false);
1497 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1498 IDENTIFIER_MARKED on its ABI tags. */
1501 mark_abi_tags_r (tree
*tp
, int *walk_subtrees
, void *data
)
1503 if (!OVERLOAD_TYPE_P (*tp
))
1506 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1507 anyway, but let's make sure of it. */
1508 *walk_subtrees
= false;
1510 bool *valp
= static_cast<bool*>(data
);
1512 mark_or_check_tags (*tp
, NULL
, NULL
, *valp
);
1517 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1521 mark_abi_tags (tree t
, bool val
)
1523 mark_or_check_tags (t
, NULL
, NULL
, val
);
1526 if (DECL_LANG_SPECIFIC (t
) && DECL_USE_TEMPLATE (t
)
1527 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1529 /* Template arguments are part of the signature. */
1530 tree level
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1531 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1533 tree arg
= TREE_VEC_ELT (level
, j
);
1534 cp_walk_tree_without_duplicates (&arg
, mark_abi_tags_r
, &val
);
1537 if (TREE_CODE (t
) == FUNCTION_DECL
)
1538 /* A function's parameter types are part of the signature, so
1539 we don't need to inherit any tags that are also in them. */
1540 for (tree arg
= FUNCTION_FIRST_USER_PARMTYPE (t
); arg
;
1541 arg
= TREE_CHAIN (arg
))
1542 cp_walk_tree_without_duplicates (&TREE_VALUE (arg
),
1543 mark_abi_tags_r
, &val
);
1547 /* Check that T has all the ABI tags that subobject SUBOB has, or
1548 warn if not. If T is a (variable or function) declaration, also
1549 add any missing tags. */
1552 check_abi_tags (tree t
, tree subob
)
1554 bool inherit
= DECL_P (t
);
1556 if (!inherit
&& !warn_abi_tag
)
1559 tree decl
= TYPE_P (t
) ? TYPE_NAME (t
) : t
;
1560 if (!TREE_PUBLIC (decl
))
1561 /* No need to worry about things local to this TU. */
1564 mark_abi_tags (t
, true);
1566 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1567 struct abi_tag_data data
= { t
, subob
, error_mark_node
};
1569 data
.tags
= NULL_TREE
;
1571 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1573 if (inherit
&& data
.tags
)
1575 tree attr
= lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t
));
1577 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1580 = tree_cons (get_identifier ("abi_tag"), data
.tags
,
1581 DECL_ATTRIBUTES (t
));
1584 mark_abi_tags (t
, false);
1587 /* Check that DECL has all the ABI tags that are used in parts of its type
1588 that are not reflected in its mangled name. */
1591 check_abi_tags (tree decl
)
1593 if (TREE_CODE (decl
) == VAR_DECL
)
1594 check_abi_tags (decl
, TREE_TYPE (decl
));
1595 else if (TREE_CODE (decl
) == FUNCTION_DECL
1596 && !mangle_return_type_p (decl
))
1597 check_abi_tags (decl
, TREE_TYPE (TREE_TYPE (decl
)));
1601 inherit_targ_abi_tags (tree t
)
1603 if (!CLASS_TYPE_P (t
)
1604 || CLASSTYPE_TEMPLATE_INFO (t
) == NULL_TREE
)
1607 mark_abi_tags (t
, true);
1609 tree args
= CLASSTYPE_TI_ARGS (t
);
1610 struct abi_tag_data data
= { t
, NULL_TREE
, NULL_TREE
};
1611 for (int i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
1613 tree level
= TMPL_ARGS_LEVEL (args
, i
+1);
1614 for (int j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
1616 tree arg
= TREE_VEC_ELT (level
, j
);
1618 cp_walk_tree_without_duplicates (&arg
, find_abi_tags_r
, &data
);
1622 // If we found some tags on our template arguments, add them to our
1623 // abi_tag attribute.
1626 tree attr
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1628 TREE_VALUE (attr
) = chainon (data
.tags
, TREE_VALUE (attr
));
1631 = tree_cons (get_identifier ("abi_tag"), data
.tags
,
1632 TYPE_ATTRIBUTES (t
));
1635 mark_abi_tags (t
, false);
1638 /* Return true, iff class T has a non-virtual destructor that is
1639 accessible from outside the class heirarchy (i.e. is public, or
1640 there's a suitable friend. */
1643 accessible_nvdtor_p (tree t
)
1645 tree dtor
= CLASSTYPE_DESTRUCTORS (t
);
1647 /* An implicitly declared destructor is always public. And,
1648 if it were virtual, we would have created it by now. */
1652 if (DECL_VINDEX (dtor
))
1653 return false; /* Virtual */
1655 if (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
1656 return true; /* Public */
1658 if (CLASSTYPE_FRIEND_CLASSES (t
)
1659 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1660 return true; /* Has friends */
1665 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1666 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1667 properties of the bases. */
1670 check_bases (tree t
,
1671 int* cant_have_const_ctor_p
,
1672 int* no_const_asn_ref_p
)
1675 bool seen_non_virtual_nearly_empty_base_p
= 0;
1676 int seen_tm_mask
= 0;
1679 tree field
= NULL_TREE
;
1681 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1682 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1683 if (TREE_CODE (field
) == FIELD_DECL
)
1686 for (binfo
= TYPE_BINFO (t
), i
= 0;
1687 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1689 tree basetype
= TREE_TYPE (base_binfo
);
1691 gcc_assert (COMPLETE_TYPE_P (basetype
));
1693 if (CLASSTYPE_FINAL (basetype
))
1694 error ("cannot derive from %<final%> base %qT in derived type %qT",
1697 /* If any base class is non-literal, so is the derived class. */
1698 if (!CLASSTYPE_LITERAL_P (basetype
))
1699 CLASSTYPE_LITERAL_P (t
) = false;
1701 /* If the base class doesn't have copy constructors or
1702 assignment operators that take const references, then the
1703 derived class cannot have such a member automatically
1705 if (TYPE_HAS_COPY_CTOR (basetype
)
1706 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1707 *cant_have_const_ctor_p
= 1;
1708 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1709 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1710 *no_const_asn_ref_p
= 1;
1712 if (BINFO_VIRTUAL_P (base_binfo
))
1713 /* A virtual base does not effect nearly emptiness. */
1715 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1717 if (seen_non_virtual_nearly_empty_base_p
)
1718 /* And if there is more than one nearly empty base, then the
1719 derived class is not nearly empty either. */
1720 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1722 /* Remember we've seen one. */
1723 seen_non_virtual_nearly_empty_base_p
= 1;
1725 else if (!is_empty_class (basetype
))
1726 /* If the base class is not empty or nearly empty, then this
1727 class cannot be nearly empty. */
1728 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1730 /* A lot of properties from the bases also apply to the derived
1732 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1733 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1734 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1735 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1736 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1737 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1738 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1739 || !TYPE_HAS_COPY_CTOR (basetype
));
1740 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1741 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1742 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1743 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1744 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1745 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1746 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1747 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1748 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1749 (t
, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
1750 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype
));
1751 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1752 (t
, CLASSTYPE_REF_FIELDS_NEED_INIT (t
)
1753 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype
));
1755 /* A standard-layout class is a class that:
1757 * has no non-standard-layout base classes, */
1758 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1759 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1762 /* ...has no base classes of the same type as the first non-static
1764 if (field
&& DECL_CONTEXT (field
) == t
1765 && (same_type_ignoring_top_level_qualifiers_p
1766 (TREE_TYPE (field
), basetype
)))
1767 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1769 /* ...either has no non-static data members in the most-derived
1770 class and at most one base class with non-static data
1771 members, or has no base classes with non-static data
1773 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1774 basefield
= DECL_CHAIN (basefield
))
1775 if (TREE_CODE (basefield
) == FIELD_DECL
)
1778 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1785 /* Don't bother collecting tm attributes if transactional memory
1786 support is not enabled. */
1789 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1791 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1794 check_abi_tags (t
, basetype
);
1797 /* If one of the base classes had TM attributes, and the current class
1798 doesn't define its own, then the current class inherits one. */
1799 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1801 tree tm_attr
= tm_mask_to_attr (seen_tm_mask
& -seen_tm_mask
);
1802 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1806 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1807 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1808 that have had a nearly-empty virtual primary base stolen by some
1809 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1813 determine_primary_bases (tree t
)
1816 tree primary
= NULL_TREE
;
1817 tree type_binfo
= TYPE_BINFO (t
);
1820 /* Determine the primary bases of our bases. */
1821 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1822 base_binfo
= TREE_CHAIN (base_binfo
))
1824 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1826 /* See if we're the non-virtual primary of our inheritance
1828 if (!BINFO_VIRTUAL_P (base_binfo
))
1830 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1831 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1834 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1835 BINFO_TYPE (parent_primary
)))
1836 /* We are the primary binfo. */
1837 BINFO_PRIMARY_P (base_binfo
) = 1;
1839 /* Determine if we have a virtual primary base, and mark it so.
1841 if (primary
&& BINFO_VIRTUAL_P (primary
))
1843 tree this_primary
= copied_binfo (primary
, base_binfo
);
1845 if (BINFO_PRIMARY_P (this_primary
))
1846 /* Someone already claimed this base. */
1847 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1852 BINFO_PRIMARY_P (this_primary
) = 1;
1853 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1855 /* A virtual binfo might have been copied from within
1856 another hierarchy. As we're about to use it as a
1857 primary base, make sure the offsets match. */
1858 delta
= size_diffop_loc (input_location
,
1860 BINFO_OFFSET (base_binfo
)),
1862 BINFO_OFFSET (this_primary
)));
1864 propagate_binfo_offsets (this_primary
, delta
);
1869 /* First look for a dynamic direct non-virtual base. */
1870 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1872 tree basetype
= BINFO_TYPE (base_binfo
);
1874 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1876 primary
= base_binfo
;
1881 /* A "nearly-empty" virtual base class can be the primary base
1882 class, if no non-virtual polymorphic base can be found. Look for
1883 a nearly-empty virtual dynamic base that is not already a primary
1884 base of something in the hierarchy. If there is no such base,
1885 just pick the first nearly-empty virtual base. */
1887 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1888 base_binfo
= TREE_CHAIN (base_binfo
))
1889 if (BINFO_VIRTUAL_P (base_binfo
)
1890 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1892 if (!BINFO_PRIMARY_P (base_binfo
))
1894 /* Found one that is not primary. */
1895 primary
= base_binfo
;
1899 /* Remember the first candidate. */
1900 primary
= base_binfo
;
1904 /* If we've got a primary base, use it. */
1907 tree basetype
= BINFO_TYPE (primary
);
1909 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1910 if (BINFO_PRIMARY_P (primary
))
1911 /* We are stealing a primary base. */
1912 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1913 BINFO_PRIMARY_P (primary
) = 1;
1914 if (BINFO_VIRTUAL_P (primary
))
1918 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1919 /* A virtual binfo might have been copied from within
1920 another hierarchy. As we're about to use it as a primary
1921 base, make sure the offsets match. */
1922 delta
= size_diffop_loc (input_location
, ssize_int (0),
1923 convert (ssizetype
, BINFO_OFFSET (primary
)));
1925 propagate_binfo_offsets (primary
, delta
);
1928 primary
= TYPE_BINFO (basetype
);
1930 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1931 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1932 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1936 /* Update the variant types of T. */
1939 fixup_type_variants (tree t
)
1946 for (variants
= TYPE_NEXT_VARIANT (t
);
1948 variants
= TYPE_NEXT_VARIANT (variants
))
1950 /* These fields are in the _TYPE part of the node, not in
1951 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1952 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1953 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1954 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1955 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1957 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1959 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1961 /* Copy whatever these are holding today. */
1962 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1963 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1967 /* Early variant fixups: we apply attributes at the beginning of the class
1968 definition, and we need to fix up any variants that have already been
1969 made via elaborated-type-specifier so that check_qualified_type works. */
1972 fixup_attribute_variants (tree t
)
1979 tree attrs
= TYPE_ATTRIBUTES (t
);
1980 unsigned align
= TYPE_ALIGN (t
);
1981 bool user_align
= TYPE_USER_ALIGN (t
);
1983 for (variants
= TYPE_NEXT_VARIANT (t
);
1985 variants
= TYPE_NEXT_VARIANT (variants
))
1987 /* These are the two fields that check_qualified_type looks at and
1988 are affected by attributes. */
1989 TYPE_ATTRIBUTES (variants
) = attrs
;
1990 unsigned valign
= align
;
1991 if (TYPE_USER_ALIGN (variants
))
1992 valign
= MAX (valign
, TYPE_ALIGN (variants
));
1994 TYPE_USER_ALIGN (variants
) = user_align
;
1995 TYPE_ALIGN (variants
) = valign
;
1999 /* Set memoizing fields and bits of T (and its variants) for later
2003 finish_struct_bits (tree t
)
2005 /* Fix up variants (if any). */
2006 fixup_type_variants (t
);
2008 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
2009 /* For a class w/o baseclasses, 'finish_struct' has set
2010 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2011 Similarly for a class whose base classes do not have vtables.
2012 When neither of these is true, we might have removed abstract
2013 virtuals (by providing a definition), added some (by declaring
2014 new ones), or redeclared ones from a base class. We need to
2015 recalculate what's really an abstract virtual at this point (by
2016 looking in the vtables). */
2017 get_pure_virtuals (t
);
2019 /* If this type has a copy constructor or a destructor, force its
2020 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2021 nonzero. This will cause it to be passed by invisible reference
2022 and prevent it from being returned in a register. */
2023 if (type_has_nontrivial_copy_init (t
)
2024 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
2027 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
2028 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2030 SET_TYPE_MODE (variants
, BLKmode
);
2031 TREE_ADDRESSABLE (variants
) = 1;
2036 /* Issue warnings about T having private constructors, but no friends,
2039 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2040 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2041 non-private static member functions. */
2044 maybe_warn_about_overly_private_class (tree t
)
2046 int has_member_fn
= 0;
2047 int has_nonprivate_method
= 0;
2050 if (!warn_ctor_dtor_privacy
2051 /* If the class has friends, those entities might create and
2052 access instances, so we should not warn. */
2053 || (CLASSTYPE_FRIEND_CLASSES (t
)
2054 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2055 /* We will have warned when the template was declared; there's
2056 no need to warn on every instantiation. */
2057 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2058 /* There's no reason to even consider warning about this
2062 /* We only issue one warning, if more than one applies, because
2063 otherwise, on code like:
2066 // Oops - forgot `public:'
2072 we warn several times about essentially the same problem. */
2074 /* Check to see if all (non-constructor, non-destructor) member
2075 functions are private. (Since there are no friends or
2076 non-private statics, we can't ever call any of the private member
2078 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
2079 /* We're not interested in compiler-generated methods; they don't
2080 provide any way to call private members. */
2081 if (!DECL_ARTIFICIAL (fn
))
2083 if (!TREE_PRIVATE (fn
))
2085 if (DECL_STATIC_FUNCTION_P (fn
))
2086 /* A non-private static member function is just like a
2087 friend; it can create and invoke private member
2088 functions, and be accessed without a class
2092 has_nonprivate_method
= 1;
2093 /* Keep searching for a static member function. */
2095 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2099 if (!has_nonprivate_method
&& has_member_fn
)
2101 /* There are no non-private methods, and there's at least one
2102 private member function that isn't a constructor or
2103 destructor. (If all the private members are
2104 constructors/destructors we want to use the code below that
2105 issues error messages specifically referring to
2106 constructors/destructors.) */
2108 tree binfo
= TYPE_BINFO (t
);
2110 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
2111 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
2113 has_nonprivate_method
= 1;
2116 if (!has_nonprivate_method
)
2118 warning (OPT_Wctor_dtor_privacy
,
2119 "all member functions in class %qT are private", t
);
2124 /* Even if some of the member functions are non-private, the class
2125 won't be useful for much if all the constructors or destructors
2126 are private: such an object can never be created or destroyed. */
2127 fn
= CLASSTYPE_DESTRUCTORS (t
);
2128 if (fn
&& TREE_PRIVATE (fn
))
2130 warning (OPT_Wctor_dtor_privacy
,
2131 "%q#T only defines a private destructor and has no friends",
2136 /* Warn about classes that have private constructors and no friends. */
2137 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
2138 /* Implicitly generated constructors are always public. */
2139 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
2140 || !CLASSTYPE_LAZY_COPY_CTOR (t
)))
2142 int nonprivate_ctor
= 0;
2144 /* If a non-template class does not define a copy
2145 constructor, one is defined for it, enabling it to avoid
2146 this warning. For a template class, this does not
2147 happen, and so we would normally get a warning on:
2149 template <class T> class C { private: C(); };
2151 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2152 complete non-template or fully instantiated classes have this
2154 if (!TYPE_HAS_COPY_CTOR (t
))
2155 nonprivate_ctor
= 1;
2157 for (fn
= CLASSTYPE_CONSTRUCTORS (t
); fn
; fn
= OVL_NEXT (fn
))
2159 tree ctor
= OVL_CURRENT (fn
);
2160 /* Ideally, we wouldn't count copy constructors (or, in
2161 fact, any constructor that takes an argument of the
2162 class type as a parameter) because such things cannot
2163 be used to construct an instance of the class unless
2164 you already have one. But, for now at least, we're
2166 if (! TREE_PRIVATE (ctor
))
2168 nonprivate_ctor
= 1;
2173 if (nonprivate_ctor
== 0)
2175 warning (OPT_Wctor_dtor_privacy
,
2176 "%q#T only defines private constructors and has no friends",
2184 gt_pointer_operator new_value
;
2188 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2191 method_name_cmp (const void* m1_p
, const void* m2_p
)
2193 const tree
*const m1
= (const tree
*) m1_p
;
2194 const tree
*const m2
= (const tree
*) m2_p
;
2196 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2198 if (*m1
== NULL_TREE
)
2200 if (*m2
== NULL_TREE
)
2202 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
2207 /* This routine compares two fields like method_name_cmp but using the
2208 pointer operator in resort_field_decl_data. */
2211 resort_method_name_cmp (const void* m1_p
, const void* m2_p
)
2213 const tree
*const m1
= (const tree
*) m1_p
;
2214 const tree
*const m2
= (const tree
*) m2_p
;
2215 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2217 if (*m1
== NULL_TREE
)
2219 if (*m2
== NULL_TREE
)
2222 tree d1
= DECL_NAME (OVL_CURRENT (*m1
));
2223 tree d2
= DECL_NAME (OVL_CURRENT (*m2
));
2224 resort_data
.new_value (&d1
, resort_data
.cookie
);
2225 resort_data
.new_value (&d2
, resort_data
.cookie
);
2232 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2235 resort_type_method_vec (void* obj
,
2237 gt_pointer_operator new_value
,
2240 vec
<tree
, va_gc
> *method_vec
= (vec
<tree
, va_gc
> *) obj
;
2241 int len
= vec_safe_length (method_vec
);
2245 /* The type conversion ops have to live at the front of the vec, so we
2247 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2248 vec_safe_iterate (method_vec
, slot
, &fn
);
2250 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2255 resort_data
.new_value
= new_value
;
2256 resort_data
.cookie
= cookie
;
2257 qsort (method_vec
->address () + slot
, len
- slot
, sizeof (tree
),
2258 resort_method_name_cmp
);
2262 /* Warn about duplicate methods in fn_fields.
2264 Sort methods that are not special (i.e., constructors, destructors,
2265 and type conversion operators) so that we can find them faster in
2269 finish_struct_methods (tree t
)
2272 vec
<tree
, va_gc
> *method_vec
;
2275 method_vec
= CLASSTYPE_METHOD_VEC (t
);
2279 len
= method_vec
->length ();
2281 /* Clear DECL_IN_AGGR_P for all functions. */
2282 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2283 fn_fields
= DECL_CHAIN (fn_fields
))
2284 DECL_IN_AGGR_P (fn_fields
) = 0;
2286 /* Issue warnings about private constructors and such. If there are
2287 no methods, then some public defaults are generated. */
2288 maybe_warn_about_overly_private_class (t
);
2290 /* The type conversion ops have to live at the front of the vec, so we
2292 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2293 method_vec
->iterate (slot
, &fn_fields
);
2295 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields
)))
2298 qsort (method_vec
->address () + slot
,
2299 len
-slot
, sizeof (tree
), method_name_cmp
);
2302 /* Make BINFO's vtable have N entries, including RTTI entries,
2303 vbase and vcall offsets, etc. Set its type and call the back end
2307 layout_vtable_decl (tree binfo
, int n
)
2312 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2313 layout_type (atype
);
2315 /* We may have to grow the vtable. */
2316 vtable
= get_vtbl_decl_for_binfo (binfo
);
2317 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2319 TREE_TYPE (vtable
) = atype
;
2320 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2321 layout_decl (vtable
, 0);
2325 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2326 have the same signature. */
2329 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2331 /* One destructor overrides another if they are the same kind of
2333 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2334 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2336 /* But a non-destructor never overrides a destructor, nor vice
2337 versa, nor do different kinds of destructors override
2338 one-another. For example, a complete object destructor does not
2339 override a deleting destructor. */
2340 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2343 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2344 || (DECL_CONV_FN_P (fndecl
)
2345 && DECL_CONV_FN_P (base_fndecl
)
2346 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2347 DECL_CONV_FN_TYPE (base_fndecl
))))
2349 tree fntype
= TREE_TYPE (fndecl
);
2350 tree base_fntype
= TREE_TYPE (base_fndecl
);
2351 if (type_memfn_quals (fntype
) == type_memfn_quals (base_fntype
)
2352 && type_memfn_rqual (fntype
) == type_memfn_rqual (base_fntype
)
2353 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl
),
2354 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl
)))
2360 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2364 base_derived_from (tree derived
, tree base
)
2368 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2370 if (probe
== derived
)
2372 else if (BINFO_VIRTUAL_P (probe
))
2373 /* If we meet a virtual base, we can't follow the inheritance
2374 any more. See if the complete type of DERIVED contains
2375 such a virtual base. */
2376 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2382 typedef struct find_final_overrider_data_s
{
2383 /* The function for which we are trying to find a final overrider. */
2385 /* The base class in which the function was declared. */
2386 tree declaring_base
;
2387 /* The candidate overriders. */
2389 /* Path to most derived. */
2391 } find_final_overrider_data
;
2393 /* Add the overrider along the current path to FFOD->CANDIDATES.
2394 Returns true if an overrider was found; false otherwise. */
2397 dfs_find_final_overrider_1 (tree binfo
,
2398 find_final_overrider_data
*ffod
,
2403 /* If BINFO is not the most derived type, try a more derived class.
2404 A definition there will overrider a definition here. */
2408 if (dfs_find_final_overrider_1
2409 (ffod
->path
[depth
], ffod
, depth
))
2413 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2416 tree
*candidate
= &ffod
->candidates
;
2418 /* Remove any candidates overridden by this new function. */
2421 /* If *CANDIDATE overrides METHOD, then METHOD
2422 cannot override anything else on the list. */
2423 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2425 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2426 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2427 *candidate
= TREE_CHAIN (*candidate
);
2429 candidate
= &TREE_CHAIN (*candidate
);
2432 /* Add the new function. */
2433 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2440 /* Called from find_final_overrider via dfs_walk. */
2443 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2445 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2447 if (binfo
== ffod
->declaring_base
)
2448 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2449 ffod
->path
.safe_push (binfo
);
2455 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2457 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2463 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2464 FN and whose TREE_VALUE is the binfo for the base where the
2465 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2466 DERIVED) is the base object in which FN is declared. */
2469 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2471 find_final_overrider_data ffod
;
2473 /* Getting this right is a little tricky. This is valid:
2475 struct S { virtual void f (); };
2476 struct T { virtual void f (); };
2477 struct U : public S, public T { };
2479 even though calling `f' in `U' is ambiguous. But,
2481 struct R { virtual void f(); };
2482 struct S : virtual public R { virtual void f (); };
2483 struct T : virtual public R { virtual void f (); };
2484 struct U : public S, public T { };
2486 is not -- there's no way to decide whether to put `S::f' or
2487 `T::f' in the vtable for `R'.
2489 The solution is to look at all paths to BINFO. If we find
2490 different overriders along any two, then there is a problem. */
2491 if (DECL_THUNK_P (fn
))
2492 fn
= THUNK_TARGET (fn
);
2494 /* Determine the depth of the hierarchy. */
2496 ffod
.declaring_base
= binfo
;
2497 ffod
.candidates
= NULL_TREE
;
2498 ffod
.path
.create (30);
2500 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2501 dfs_find_final_overrider_post
, &ffod
);
2503 ffod
.path
.release ();
2505 /* If there was no winner, issue an error message. */
2506 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2507 return error_mark_node
;
2509 return ffod
.candidates
;
2512 /* Return the index of the vcall offset for FN when TYPE is used as a
2516 get_vcall_index (tree fn
, tree type
)
2518 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2522 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2523 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2524 || same_signature_p (fn
, p
->purpose
))
2527 /* There should always be an appropriate index. */
2531 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2532 dominated by T. FN is the old function; VIRTUALS points to the
2533 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2534 of that entry in the list. */
2537 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2545 tree overrider_fn
, overrider_target
;
2546 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2547 tree over_return
, base_return
;
2550 /* Find the nearest primary base (possibly binfo itself) which defines
2551 this function; this is the class the caller will convert to when
2552 calling FN through BINFO. */
2553 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2556 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2559 /* The nearest definition is from a lost primary. */
2560 if (BINFO_LOST_PRIMARY_P (b
))
2565 /* Find the final overrider. */
2566 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2567 if (overrider
== error_mark_node
)
2569 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2572 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2574 /* Check for adjusting covariant return types. */
2575 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2576 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2578 if (POINTER_TYPE_P (over_return
)
2579 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2580 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2581 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2582 /* If the overrider is invalid, don't even try. */
2583 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2585 /* If FN is a covariant thunk, we must figure out the adjustment
2586 to the final base FN was converting to. As OVERRIDER_TARGET might
2587 also be converting to the return type of FN, we have to
2588 combine the two conversions here. */
2589 tree fixed_offset
, virtual_offset
;
2591 over_return
= TREE_TYPE (over_return
);
2592 base_return
= TREE_TYPE (base_return
);
2594 if (DECL_THUNK_P (fn
))
2596 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2597 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2598 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2601 fixed_offset
= virtual_offset
= NULL_TREE
;
2604 /* Find the equivalent binfo within the return type of the
2605 overriding function. We will want the vbase offset from
2607 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2609 else if (!same_type_ignoring_top_level_qualifiers_p
2610 (over_return
, base_return
))
2612 /* There was no existing virtual thunk (which takes
2613 precedence). So find the binfo of the base function's
2614 return type within the overriding function's return type.
2615 We cannot call lookup base here, because we're inside a
2616 dfs_walk, and will therefore clobber the BINFO_MARKED
2617 flags. Fortunately we know the covariancy is valid (it
2618 has already been checked), so we can just iterate along
2619 the binfos, which have been chained in inheritance graph
2620 order. Of course it is lame that we have to repeat the
2621 search here anyway -- we should really be caching pieces
2622 of the vtable and avoiding this repeated work. */
2623 tree thunk_binfo
, base_binfo
;
2625 /* Find the base binfo within the overriding function's
2626 return type. We will always find a thunk_binfo, except
2627 when the covariancy is invalid (which we will have
2628 already diagnosed). */
2629 for (base_binfo
= TYPE_BINFO (base_return
),
2630 thunk_binfo
= TYPE_BINFO (over_return
);
2632 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2633 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2634 BINFO_TYPE (base_binfo
)))
2637 /* See if virtual inheritance is involved. */
2638 for (virtual_offset
= thunk_binfo
;
2640 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2641 if (BINFO_VIRTUAL_P (virtual_offset
))
2645 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2647 tree offset
= convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2651 /* We convert via virtual base. Adjust the fixed
2652 offset to be from there. */
2654 size_diffop (offset
,
2656 BINFO_OFFSET (virtual_offset
)));
2659 /* There was an existing fixed offset, this must be
2660 from the base just converted to, and the base the
2661 FN was thunking to. */
2662 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2664 fixed_offset
= offset
;
2668 if (fixed_offset
|| virtual_offset
)
2669 /* Replace the overriding function with a covariant thunk. We
2670 will emit the overriding function in its own slot as
2672 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2673 fixed_offset
, virtual_offset
);
2676 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2677 !DECL_THUNK_P (fn
));
2679 /* If we need a covariant thunk, then we may need to adjust first_defn.
2680 The ABI specifies that the thunks emitted with a function are
2681 determined by which bases the function overrides, so we need to be
2682 sure that we're using a thunk for some overridden base; even if we
2683 know that the necessary this adjustment is zero, there may not be an
2684 appropriate zero-this-adjusment thunk for us to use since thunks for
2685 overriding virtual bases always use the vcall offset.
2687 Furthermore, just choosing any base that overrides this function isn't
2688 quite right, as this slot won't be used for calls through a type that
2689 puts a covariant thunk here. Calling the function through such a type
2690 will use a different slot, and that slot is the one that determines
2691 the thunk emitted for that base.
2693 So, keep looking until we find the base that we're really overriding
2694 in this slot: the nearest primary base that doesn't use a covariant
2695 thunk in this slot. */
2696 if (overrider_target
!= overrider_fn
)
2698 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2699 /* We already know that the overrider needs a covariant thunk. */
2700 b
= get_primary_binfo (b
);
2701 for (; ; b
= get_primary_binfo (b
))
2703 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2704 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2705 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2707 if (BINFO_LOST_PRIMARY_P (b
))
2713 /* Assume that we will produce a thunk that convert all the way to
2714 the final overrider, and not to an intermediate virtual base. */
2715 virtual_base
= NULL_TREE
;
2717 /* See if we can convert to an intermediate virtual base first, and then
2718 use the vcall offset located there to finish the conversion. */
2719 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2721 /* If we find the final overrider, then we can stop
2723 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2724 BINFO_TYPE (TREE_VALUE (overrider
))))
2727 /* If we find a virtual base, and we haven't yet found the
2728 overrider, then there is a virtual base between the
2729 declaring base (first_defn) and the final overrider. */
2730 if (BINFO_VIRTUAL_P (b
))
2737 /* Compute the constant adjustment to the `this' pointer. The
2738 `this' pointer, when this function is called, will point at BINFO
2739 (or one of its primary bases, which are at the same offset). */
2741 /* The `this' pointer needs to be adjusted from the declaration to
2742 the nearest virtual base. */
2743 delta
= size_diffop_loc (input_location
,
2744 convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2745 convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2747 /* If the nearest definition is in a lost primary, we don't need an
2748 entry in our vtable. Except possibly in a constructor vtable,
2749 if we happen to get our primary back. In that case, the offset
2750 will be zero, as it will be a primary base. */
2751 delta
= size_zero_node
;
2753 /* The `this' pointer needs to be adjusted from pointing to
2754 BINFO to pointing at the base where the final overrider
2756 delta
= size_diffop_loc (input_location
,
2758 BINFO_OFFSET (TREE_VALUE (overrider
))),
2759 convert (ssizetype
, BINFO_OFFSET (binfo
)));
2761 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2764 BV_VCALL_INDEX (*virtuals
)
2765 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2767 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2769 BV_LOST_PRIMARY (*virtuals
) = lost
;
2772 /* Called from modify_all_vtables via dfs_walk. */
2775 dfs_modify_vtables (tree binfo
, void* data
)
2777 tree t
= (tree
) data
;
2782 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2783 /* A base without a vtable needs no modification, and its bases
2784 are uninteresting. */
2785 return dfs_skip_bases
;
2787 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2788 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2789 /* Don't do the primary vtable, if it's new. */
2792 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2793 /* There's no need to modify the vtable for a non-virtual primary
2794 base; we're not going to use that vtable anyhow. We do still
2795 need to do this for virtual primary bases, as they could become
2796 non-primary in a construction vtable. */
2799 make_new_vtable (t
, binfo
);
2801 /* Now, go through each of the virtual functions in the virtual
2802 function table for BINFO. Find the final overrider, and update
2803 the BINFO_VIRTUALS list appropriately. */
2804 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2805 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2807 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2808 old_virtuals
= TREE_CHAIN (old_virtuals
))
2809 update_vtable_entry_for_fn (t
,
2811 BV_FN (old_virtuals
),
2817 /* Update all of the primary and secondary vtables for T. Create new
2818 vtables as required, and initialize their RTTI information. Each
2819 of the functions in VIRTUALS is declared in T and may override a
2820 virtual function from a base class; find and modify the appropriate
2821 entries to point to the overriding functions. Returns a list, in
2822 declaration order, of the virtual functions that are declared in T,
2823 but do not appear in the primary base class vtable, and which
2824 should therefore be appended to the end of the vtable for T. */
2827 modify_all_vtables (tree t
, tree virtuals
)
2829 tree binfo
= TYPE_BINFO (t
);
2832 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2833 if (TYPE_CONTAINS_VPTR_P (t
))
2834 get_vtable_decl (t
, false);
2836 /* Update all of the vtables. */
2837 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2839 /* Add virtual functions not already in our primary vtable. These
2840 will be both those introduced by this class, and those overridden
2841 from secondary bases. It does not include virtuals merely
2842 inherited from secondary bases. */
2843 for (fnsp
= &virtuals
; *fnsp
; )
2845 tree fn
= TREE_VALUE (*fnsp
);
2847 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2848 || DECL_VINDEX (fn
) == error_mark_node
)
2850 /* We don't need to adjust the `this' pointer when
2851 calling this function. */
2852 BV_DELTA (*fnsp
) = integer_zero_node
;
2853 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2855 /* This is a function not already in our vtable. Keep it. */
2856 fnsp
= &TREE_CHAIN (*fnsp
);
2859 /* We've already got an entry for this function. Skip it. */
2860 *fnsp
= TREE_CHAIN (*fnsp
);
2866 /* Get the base virtual function declarations in T that have the
2870 get_basefndecls (tree name
, tree t
, vec
<tree
> *base_fndecls
)
2873 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2876 /* Find virtual functions in T with the indicated NAME. */
2877 i
= lookup_fnfields_1 (t
, name
);
2878 bool found_decls
= false;
2880 for (methods
= (*CLASSTYPE_METHOD_VEC (t
))[i
];
2882 methods
= OVL_NEXT (methods
))
2884 tree method
= OVL_CURRENT (methods
);
2886 if (TREE_CODE (method
) == FUNCTION_DECL
2887 && DECL_VINDEX (method
))
2889 base_fndecls
->safe_push (method
);
2897 for (i
= 0; i
< n_baseclasses
; i
++)
2899 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2900 get_basefndecls (name
, basetype
, base_fndecls
);
2904 /* If this declaration supersedes the declaration of
2905 a method declared virtual in the base class, then
2906 mark this field as being virtual as well. */
2909 check_for_override (tree decl
, tree ctype
)
2911 bool overrides_found
= false;
2912 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2913 /* In [temp.mem] we have:
2915 A specialization of a member function template does not
2916 override a virtual function from a base class. */
2918 if ((DECL_DESTRUCTOR_P (decl
)
2919 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2920 || DECL_CONV_FN_P (decl
))
2921 && look_for_overrides (ctype
, decl
)
2922 && !DECL_STATIC_FUNCTION_P (decl
))
2923 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2924 the error_mark_node so that we know it is an overriding
2927 DECL_VINDEX (decl
) = decl
;
2928 overrides_found
= true;
2929 if (warn_override
&& !DECL_OVERRIDE_P (decl
)
2930 && !DECL_DESTRUCTOR_P (decl
))
2931 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wsuggest_override
,
2932 "%q+D can be marked override", decl
);
2935 if (DECL_VIRTUAL_P (decl
))
2937 if (!DECL_VINDEX (decl
))
2938 DECL_VINDEX (decl
) = error_mark_node
;
2939 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2940 if (DECL_DESTRUCTOR_P (decl
))
2941 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2943 else if (DECL_FINAL_P (decl
))
2944 error ("%q+#D marked %<final%>, but is not virtual", decl
);
2945 if (DECL_OVERRIDE_P (decl
) && !overrides_found
)
2946 error ("%q+#D marked %<override%>, but does not override", decl
);
2949 /* Warn about hidden virtual functions that are not overridden in t.
2950 We know that constructors and destructors don't apply. */
2953 warn_hidden (tree t
)
2955 vec
<tree
, va_gc
> *method_vec
= CLASSTYPE_METHOD_VEC (t
);
2959 /* We go through each separately named virtual function. */
2960 for (i
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2961 vec_safe_iterate (method_vec
, i
, &fns
);
2971 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2972 have the same name. Figure out what name that is. */
2973 name
= DECL_NAME (OVL_CURRENT (fns
));
2974 /* There are no possibly hidden functions yet. */
2975 auto_vec
<tree
, 20> base_fndecls
;
2976 /* Iterate through all of the base classes looking for possibly
2977 hidden functions. */
2978 for (binfo
= TYPE_BINFO (t
), j
= 0;
2979 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2981 tree basetype
= BINFO_TYPE (base_binfo
);
2982 get_basefndecls (name
, basetype
, &base_fndecls
);
2985 /* If there are no functions to hide, continue. */
2986 if (base_fndecls
.is_empty ())
2989 /* Remove any overridden functions. */
2990 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
2992 fndecl
= OVL_CURRENT (fn
);
2993 if (TREE_CODE (fndecl
) == FUNCTION_DECL
2994 && DECL_VINDEX (fndecl
))
2996 /* If the method from the base class has the same
2997 signature as the method from the derived class, it
2998 has been overridden. */
2999 for (size_t k
= 0; k
< base_fndecls
.length (); k
++)
3001 && same_signature_p (fndecl
, base_fndecls
[k
]))
3002 base_fndecls
[k
] = NULL_TREE
;
3006 /* Now give a warning for all base functions without overriders,
3007 as they are hidden. */
3010 FOR_EACH_VEC_ELT (base_fndecls
, k
, base_fndecl
)
3013 /* Here we know it is a hider, and no overrider exists. */
3014 warning (OPT_Woverloaded_virtual
, "%q+D was hidden", base_fndecl
);
3015 warning (OPT_Woverloaded_virtual
, " by %q+D", fns
);
3020 /* Recursive helper for finish_struct_anon. */
3023 finish_struct_anon_r (tree field
, bool complain
)
3025 bool is_union
= TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
;
3026 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
3027 for (; elt
; elt
= DECL_CHAIN (elt
))
3029 /* We're generally only interested in entities the user
3030 declared, but we also find nested classes by noticing
3031 the TYPE_DECL that we create implicitly. You're
3032 allowed to put one anonymous union inside another,
3033 though, so we explicitly tolerate that. We use
3034 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3035 we also allow unnamed types used for defining fields. */
3036 if (DECL_ARTIFICIAL (elt
)
3037 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
3038 || TYPE_ANONYMOUS_P (TREE_TYPE (elt
))))
3041 if (TREE_CODE (elt
) != FIELD_DECL
)
3043 /* We already complained about static data members in
3044 finish_static_data_member_decl. */
3045 if (complain
&& TREE_CODE (elt
) != VAR_DECL
)
3048 permerror (input_location
,
3049 "%q+#D invalid; an anonymous union can "
3050 "only have non-static data members", elt
);
3052 permerror (input_location
,
3053 "%q+#D invalid; an anonymous struct can "
3054 "only have non-static data members", elt
);
3061 if (TREE_PRIVATE (elt
))
3064 permerror (input_location
,
3065 "private member %q+#D in anonymous union", elt
);
3067 permerror (input_location
,
3068 "private member %q+#D in anonymous struct", elt
);
3070 else if (TREE_PROTECTED (elt
))
3073 permerror (input_location
,
3074 "protected member %q+#D in anonymous union", elt
);
3076 permerror (input_location
,
3077 "protected member %q+#D in anonymous struct", elt
);
3081 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3082 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3084 /* Recurse into the anonymous aggregates to handle correctly
3085 access control (c++/24926):
3096 if (DECL_NAME (elt
) == NULL_TREE
3097 && ANON_AGGR_TYPE_P (TREE_TYPE (elt
)))
3098 finish_struct_anon_r (elt
, /*complain=*/false);
3102 /* Check for things that are invalid. There are probably plenty of other
3103 things we should check for also. */
3106 finish_struct_anon (tree t
)
3108 for (tree field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
3110 if (TREE_STATIC (field
))
3112 if (TREE_CODE (field
) != FIELD_DECL
)
3115 if (DECL_NAME (field
) == NULL_TREE
3116 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3117 finish_struct_anon_r (field
, /*complain=*/true);
3121 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3122 will be used later during class template instantiation.
3123 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3124 a non-static member data (FIELD_DECL), a member function
3125 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3126 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3127 When FRIEND_P is nonzero, T is either a friend class
3128 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3129 (FUNCTION_DECL, TEMPLATE_DECL). */
3132 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
3134 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3135 if (CLASSTYPE_TEMPLATE_INFO (type
))
3136 CLASSTYPE_DECL_LIST (type
)
3137 = tree_cons (friend_p
? NULL_TREE
: type
,
3138 t
, CLASSTYPE_DECL_LIST (type
));
3141 /* This function is called from declare_virt_assop_and_dtor via
3144 DATA is a type that direcly or indirectly inherits the base
3145 represented by BINFO. If BINFO contains a virtual assignment [copy
3146 assignment or move assigment] operator or a virtual constructor,
3147 declare that function in DATA if it hasn't been already declared. */
3150 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
3152 tree bv
, fn
, t
= (tree
)data
;
3153 tree opname
= ansi_assopname (NOP_EXPR
);
3155 gcc_assert (t
&& CLASS_TYPE_P (t
));
3156 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
3158 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
3159 /* A base without a vtable needs no modification, and its bases
3160 are uninteresting. */
3161 return dfs_skip_bases
;
3163 if (BINFO_PRIMARY_P (binfo
))
3164 /* If this is a primary base, then we have already looked at the
3165 virtual functions of its vtable. */
3168 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
3172 if (DECL_NAME (fn
) == opname
)
3174 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
3175 lazily_declare_fn (sfk_copy_assignment
, t
);
3176 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
3177 lazily_declare_fn (sfk_move_assignment
, t
);
3179 else if (DECL_DESTRUCTOR_P (fn
)
3180 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
3181 lazily_declare_fn (sfk_destructor
, t
);
3187 /* If the class type T has a direct or indirect base that contains a
3188 virtual assignment operator or a virtual destructor, declare that
3189 function in T if it hasn't been already declared. */
3192 declare_virt_assop_and_dtor (tree t
)
3194 if (!(TYPE_POLYMORPHIC_P (t
)
3195 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
3196 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
3197 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
3200 dfs_walk_all (TYPE_BINFO (t
),
3201 dfs_declare_virt_assop_and_dtor
,
3205 /* Declare the inheriting constructor for class T inherited from base
3206 constructor CTOR with the parameter array PARMS of size NPARMS. */
3209 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
3211 /* We don't declare an inheriting ctor that would be a default,
3212 copy or move ctor for derived or base. */
3216 && TREE_CODE (parms
[0]) == REFERENCE_TYPE
)
3218 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
3219 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
3223 tree parmlist
= void_list_node
;
3224 for (int i
= nparms
- 1; i
>= 0; i
--)
3225 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
3226 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
3227 t
, false, ctor
, parmlist
);
3228 gcc_assert (TYPE_MAIN_VARIANT (t
) == t
);
3229 if (add_method (t
, fn
, NULL_TREE
))
3231 DECL_CHAIN (fn
) = TYPE_METHODS (t
);
3232 TYPE_METHODS (t
) = fn
;
3236 /* Declare all the inheriting constructors for class T inherited from base
3237 constructor CTOR. */
3240 one_inherited_ctor (tree ctor
, tree t
)
3242 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
3244 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
3246 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3248 if (TREE_PURPOSE (parms
))
3249 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3250 new_parms
[i
++] = TREE_VALUE (parms
);
3252 one_inheriting_sig (t
, ctor
, new_parms
, i
);
3253 if (parms
== NULL_TREE
)
3255 if (warning (OPT_Winherited_variadic_ctor
,
3256 "the ellipsis in %qD is not inherited", ctor
))
3257 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
3261 /* Create default constructors, assignment operators, and so forth for
3262 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3263 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3264 the class cannot have a default constructor, copy constructor
3265 taking a const reference argument, or an assignment operator taking
3266 a const reference, respectively. */
3269 add_implicitly_declared_members (tree t
, tree
* access_decls
,
3270 int cant_have_const_cctor
,
3271 int cant_have_const_assignment
)
3273 bool move_ok
= false;
3275 if (cxx_dialect
>= cxx11
&& !CLASSTYPE_DESTRUCTORS (t
)
3276 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
3277 && !type_has_move_constructor (t
) && !type_has_move_assign (t
))
3281 if (!CLASSTYPE_DESTRUCTORS (t
))
3283 /* In general, we create destructors lazily. */
3284 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
3286 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3287 && TYPE_FOR_JAVA (t
))
3288 /* But if this is a Java class, any non-trivial destructor is
3289 invalid, even if compiler-generated. Therefore, if the
3290 destructor is non-trivial we create it now. */
3291 lazily_declare_fn (sfk_destructor
, t
);
3296 If there is no user-declared constructor for a class, a default
3297 constructor is implicitly declared. */
3298 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
3300 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
3301 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
3302 if (cxx_dialect
>= cxx11
)
3303 TYPE_HAS_CONSTEXPR_CTOR (t
)
3304 /* This might force the declaration. */
3305 = type_has_constexpr_default_constructor (t
);
3310 If a class definition does not explicitly declare a copy
3311 constructor, one is declared implicitly. */
3312 if (! TYPE_HAS_COPY_CTOR (t
) && ! TYPE_FOR_JAVA (t
))
3314 TYPE_HAS_COPY_CTOR (t
) = 1;
3315 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
3316 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
3318 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
3321 /* If there is no assignment operator, one will be created if and
3322 when it is needed. For now, just record whether or not the type
3323 of the parameter to the assignment operator will be a const or
3324 non-const reference. */
3325 if (!TYPE_HAS_COPY_ASSIGN (t
) && !TYPE_FOR_JAVA (t
))
3327 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3328 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3329 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3330 if (move_ok
&& !LAMBDA_TYPE_P (t
))
3331 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3334 /* We can't be lazy about declaring functions that might override
3335 a virtual function from a base class. */
3336 declare_virt_assop_and_dtor (t
);
3338 while (*access_decls
)
3340 tree using_decl
= TREE_VALUE (*access_decls
);
3341 tree decl
= USING_DECL_DECLS (using_decl
);
3342 if (DECL_NAME (using_decl
) == ctor_identifier
)
3344 /* declare, then remove the decl */
3345 tree ctor_list
= decl
;
3346 location_t loc
= input_location
;
3347 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3349 for (; ctor_list
; ctor_list
= OVL_NEXT (ctor_list
))
3350 one_inherited_ctor (OVL_CURRENT (ctor_list
), t
);
3351 *access_decls
= TREE_CHAIN (*access_decls
);
3352 input_location
= loc
;
3355 access_decls
= &TREE_CHAIN (*access_decls
);
3359 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3360 count the number of fields in TYPE, including anonymous union
3364 count_fields (tree fields
)
3368 for (x
= fields
; x
; x
= DECL_CHAIN (x
))
3370 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3371 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3378 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3379 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3380 elts, starting at offset IDX. */
3383 add_fields_to_record_type (tree fields
, struct sorted_fields_type
*field_vec
, int idx
)
3386 for (x
= fields
; x
; x
= DECL_CHAIN (x
))
3388 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3389 idx
= add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3391 field_vec
->elts
[idx
++] = x
;
3396 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3397 starting at offset IDX. */
3400 add_enum_fields_to_record_type (tree enumtype
,
3401 struct sorted_fields_type
*field_vec
,
3405 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
3406 field_vec
->elts
[idx
++] = TREE_VALUE (values
);
3410 /* FIELD is a bit-field. We are finishing the processing for its
3411 enclosing type. Issue any appropriate messages and set appropriate
3412 flags. Returns false if an error has been diagnosed. */
3415 check_bitfield_decl (tree field
)
3417 tree type
= TREE_TYPE (field
);
3420 /* Extract the declared width of the bitfield, which has been
3421 temporarily stashed in DECL_INITIAL. */
3422 w
= DECL_INITIAL (field
);
3423 gcc_assert (w
!= NULL_TREE
);
3424 /* Remove the bit-field width indicator so that the rest of the
3425 compiler does not treat that value as an initializer. */
3426 DECL_INITIAL (field
) = NULL_TREE
;
3428 /* Detect invalid bit-field type. */
3429 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3431 error ("bit-field %q+#D with non-integral type", field
);
3432 w
= error_mark_node
;
3436 location_t loc
= input_location
;
3437 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3440 /* detect invalid field size. */
3441 input_location
= DECL_SOURCE_LOCATION (field
);
3442 w
= cxx_constant_value (w
);
3443 input_location
= loc
;
3445 if (TREE_CODE (w
) != INTEGER_CST
)
3447 error ("bit-field %q+D width not an integer constant", field
);
3448 w
= error_mark_node
;
3450 else if (tree_int_cst_sgn (w
) < 0)
3452 error ("negative width in bit-field %q+D", field
);
3453 w
= error_mark_node
;
3455 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3457 error ("zero width for bit-field %q+D", field
);
3458 w
= error_mark_node
;
3460 else if ((TREE_CODE (type
) != ENUMERAL_TYPE
3461 && TREE_CODE (type
) != BOOLEAN_TYPE
3462 && compare_tree_int (w
, TYPE_PRECISION (type
)) > 0)
3463 || ((TREE_CODE (type
) == ENUMERAL_TYPE
3464 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3465 && tree_int_cst_lt (TYPE_SIZE (type
), w
)))
3466 warning (0, "width of %q+D exceeds its type", field
);
3467 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3468 && (0 > (compare_tree_int
3469 (w
, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
))))))
3470 warning (0, "%q+D is too small to hold all values of %q#T", field
, type
);
3473 if (w
!= error_mark_node
)
3475 DECL_SIZE (field
) = convert (bitsizetype
, w
);
3476 DECL_BIT_FIELD (field
) = 1;
3481 /* Non-bit-fields are aligned for their type. */
3482 DECL_BIT_FIELD (field
) = 0;
3483 CLEAR_DECL_C_BIT_FIELD (field
);
3488 /* FIELD is a non bit-field. We are finishing the processing for its
3489 enclosing type T. Issue any appropriate messages and set appropriate
3493 check_field_decl (tree field
,
3495 int* cant_have_const_ctor
,
3496 int* no_const_asn_ref
,
3497 int* any_default_members
)
3499 tree type
= strip_array_types (TREE_TYPE (field
));
3501 /* In C++98 an anonymous union cannot contain any fields which would change
3502 the settings of CANT_HAVE_CONST_CTOR and friends. */
3503 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx11
)
3505 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3506 structs. So, we recurse through their fields here. */
3507 else if (ANON_AGGR_TYPE_P (type
))
3511 for (fields
= TYPE_FIELDS (type
); fields
; fields
= DECL_CHAIN (fields
))
3512 if (TREE_CODE (fields
) == FIELD_DECL
&& !DECL_C_BIT_FIELD (field
))
3513 check_field_decl (fields
, t
, cant_have_const_ctor
,
3514 no_const_asn_ref
, any_default_members
);
3516 /* Check members with class type for constructors, destructors,
3518 else if (CLASS_TYPE_P (type
))
3520 /* Never let anything with uninheritable virtuals
3521 make it through without complaint. */
3522 abstract_virtuals_error (field
, type
);
3524 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx11
)
3527 int oldcount
= errorcount
;
3528 if (TYPE_NEEDS_CONSTRUCTING (type
))
3529 error ("member %q+#D with constructor not allowed in union",
3531 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3532 error ("member %q+#D with destructor not allowed in union", field
);
3533 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3534 error ("member %q+#D with copy assignment operator not allowed in union",
3536 if (!warned
&& errorcount
> oldcount
)
3538 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3539 "only available with -std=c++11 or -std=gnu++11");
3545 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3546 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3547 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3548 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3549 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3550 || !TYPE_HAS_COPY_ASSIGN (type
));
3551 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3552 || !TYPE_HAS_COPY_CTOR (type
));
3553 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3554 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3555 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3556 || TYPE_HAS_COMPLEX_DFLT (type
));
3559 if (TYPE_HAS_COPY_CTOR (type
)
3560 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3561 *cant_have_const_ctor
= 1;
3563 if (TYPE_HAS_COPY_ASSIGN (type
)
3564 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3565 *no_const_asn_ref
= 1;
3568 check_abi_tags (t
, field
);
3570 if (DECL_INITIAL (field
) != NULL_TREE
)
3572 /* `build_class_init_list' does not recognize
3574 if (TREE_CODE (t
) == UNION_TYPE
&& *any_default_members
!= 0)
3575 error ("multiple fields in union %qT initialized", t
);
3576 *any_default_members
= 1;
3580 /* Check the data members (both static and non-static), class-scoped
3581 typedefs, etc., appearing in the declaration of T. Issue
3582 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3583 declaration order) of access declarations; each TREE_VALUE in this
3584 list is a USING_DECL.
3586 In addition, set the following flags:
3589 The class is empty, i.e., contains no non-static data members.
3591 CANT_HAVE_CONST_CTOR_P
3592 This class cannot have an implicitly generated copy constructor
3593 taking a const reference.
3595 CANT_HAVE_CONST_ASN_REF
3596 This class cannot have an implicitly generated assignment
3597 operator taking a const reference.
3599 All of these flags should be initialized before calling this
3602 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3603 fields can be added by adding to this chain. */
3606 check_field_decls (tree t
, tree
*access_decls
,
3607 int *cant_have_const_ctor_p
,
3608 int *no_const_asn_ref_p
)
3613 int any_default_members
;
3615 int field_access
= -1;
3617 /* Assume there are no access declarations. */
3618 *access_decls
= NULL_TREE
;
3619 /* Assume this class has no pointer members. */
3620 has_pointers
= false;
3621 /* Assume none of the members of this class have default
3623 any_default_members
= 0;
3625 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3628 tree type
= TREE_TYPE (x
);
3629 int this_field_access
;
3631 next
= &DECL_CHAIN (x
);
3633 if (TREE_CODE (x
) == USING_DECL
)
3635 /* Save the access declarations for our caller. */
3636 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3640 if (TREE_CODE (x
) == TYPE_DECL
3641 || TREE_CODE (x
) == TEMPLATE_DECL
)
3644 /* If we've gotten this far, it's a data member, possibly static,
3645 or an enumerator. */
3646 if (TREE_CODE (x
) != CONST_DECL
)
3647 DECL_CONTEXT (x
) = t
;
3649 /* When this goes into scope, it will be a non-local reference. */
3650 DECL_NONLOCAL (x
) = 1;
3652 if (TREE_CODE (t
) == UNION_TYPE
3653 && cxx_dialect
< cxx11
)
3655 /* [class.union] (C++98)
3657 If a union contains a static data member, or a member of
3658 reference type, the program is ill-formed.
3660 In C++11 this limitation doesn't exist anymore. */
3663 error ("in C++98 %q+D may not be static because it is "
3664 "a member of a union", x
);
3667 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3669 error ("in C++98 %q+D may not have reference type %qT "
3670 "because it is a member of a union", x
, type
);
3675 /* Perform error checking that did not get done in
3677 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3679 error ("field %q+D invalidly declared function type", x
);
3680 type
= build_pointer_type (type
);
3681 TREE_TYPE (x
) = type
;
3683 else if (TREE_CODE (type
) == METHOD_TYPE
)
3685 error ("field %q+D invalidly declared method type", x
);
3686 type
= build_pointer_type (type
);
3687 TREE_TYPE (x
) = type
;
3690 if (type
== error_mark_node
)
3693 if (TREE_CODE (x
) == CONST_DECL
|| VAR_P (x
))
3696 /* Now it can only be a FIELD_DECL. */
3698 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3699 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3701 /* If at least one non-static data member is non-literal, the whole
3702 class becomes non-literal. Per Core/1453, volatile non-static
3703 data members and base classes are also not allowed.
3704 Note: if the type is incomplete we will complain later on. */
3705 if (COMPLETE_TYPE_P (type
)
3706 && (!literal_type_p (type
) || CP_TYPE_VOLATILE_P (type
)))
3707 CLASSTYPE_LITERAL_P (t
) = false;
3709 /* A standard-layout class is a class that:
3711 has the same access control (Clause 11) for all non-static data members,
3713 this_field_access
= TREE_PROTECTED (x
) ? 1 : TREE_PRIVATE (x
) ? 2 : 0;
3714 if (field_access
== -1)
3715 field_access
= this_field_access
;
3716 else if (this_field_access
!= field_access
)
3717 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3719 /* If this is of reference type, check if it needs an init. */
3720 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3722 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3723 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3724 if (DECL_INITIAL (x
) == NULL_TREE
)
3725 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3726 if (cxx_dialect
< cxx11
)
3728 /* ARM $12.6.2: [A member initializer list] (or, for an
3729 aggregate, initialization by a brace-enclosed list) is the
3730 only way to initialize nonstatic const and reference
3732 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3733 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3737 type
= strip_array_types (type
);
3739 if (TYPE_PACKED (t
))
3741 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3745 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3749 else if (DECL_C_BIT_FIELD (x
)
3750 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
)
3751 DECL_PACKED (x
) = 1;
3754 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3755 /* We don't treat zero-width bitfields as making a class
3760 /* The class is non-empty. */
3761 CLASSTYPE_EMPTY_P (t
) = 0;
3762 /* The class is not even nearly empty. */
3763 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3764 /* If one of the data members contains an empty class,
3766 if (CLASS_TYPE_P (type
)
3767 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3768 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3771 /* This is used by -Weffc++ (see below). Warn only for pointers
3772 to members which might hold dynamic memory. So do not warn
3773 for pointers to functions or pointers to members. */
3774 if (TYPE_PTR_P (type
)
3775 && !TYPE_PTRFN_P (type
))
3776 has_pointers
= true;
3778 if (CLASS_TYPE_P (type
))
3780 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3781 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3782 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3783 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3786 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3787 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3789 if (DECL_MUTABLE_P (x
))
3791 if (CP_TYPE_CONST_P (type
))
3793 error ("member %q+D cannot be declared both %<const%> "
3794 "and %<mutable%>", x
);
3797 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3799 error ("member %q+D cannot be declared as a %<mutable%> "
3805 if (! layout_pod_type_p (type
))
3806 /* DR 148 now allows pointers to members (which are POD themselves),
3807 to be allowed in POD structs. */
3808 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3810 if (!std_layout_type_p (type
))
3811 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3813 if (! zero_init_p (type
))
3814 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3816 /* We set DECL_C_BIT_FIELD in grokbitfield.
3817 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3818 if (! DECL_C_BIT_FIELD (x
) || ! check_bitfield_decl (x
))
3819 check_field_decl (x
, t
,
3820 cant_have_const_ctor_p
,
3822 &any_default_members
);
3824 /* Now that we've removed bit-field widths from DECL_INITIAL,
3825 anything left in DECL_INITIAL is an NSDMI that makes the class
3826 non-aggregate in C++11. */
3827 if (DECL_INITIAL (x
) && cxx_dialect
< cxx14
)
3828 CLASSTYPE_NON_AGGREGATE (t
) = true;
3830 /* If any field is const, the structure type is pseudo-const. */
3831 if (CP_TYPE_CONST_P (type
))
3833 C_TYPE_FIELDS_READONLY (t
) = 1;
3834 if (DECL_INITIAL (x
) == NULL_TREE
)
3835 SET_CLASSTYPE_READONLY_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;
3846 /* A field that is pseudo-const makes the structure likewise. */
3847 else if (CLASS_TYPE_P (type
))
3849 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3850 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3851 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3852 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3855 /* Core issue 80: A nonstatic data member is required to have a
3856 different name from the class iff the class has a
3857 user-declared constructor. */
3858 if (constructor_name_p (DECL_NAME (x
), t
)
3859 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3860 permerror (input_location
, "field %q+#D with same name as class", x
);
3863 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3864 it should also define a copy constructor and an assignment operator to
3865 implement the correct copy semantic (deep vs shallow, etc.). As it is
3866 not feasible to check whether the constructors do allocate dynamic memory
3867 and store it within members, we approximate the warning like this:
3869 -- Warn only if there are members which are pointers
3870 -- Warn only if there is a non-trivial constructor (otherwise,
3871 there cannot be memory allocated).
3872 -- Warn only if there is a non-trivial destructor. We assume that the
3873 user at least implemented the cleanup correctly, and a destructor
3874 is needed to free dynamic memory.
3876 This seems enough for practical purposes. */
3879 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3880 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3881 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3883 warning (OPT_Weffc__
, "%q#T has pointer data members", t
);
3885 if (! TYPE_HAS_COPY_CTOR (t
))
3887 warning (OPT_Weffc__
,
3888 " but does not override %<%T(const %T&)%>", t
, t
);
3889 if (!TYPE_HAS_COPY_ASSIGN (t
))
3890 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3892 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3893 warning (OPT_Weffc__
,
3894 " but does not override %<operator=(const %T&)%>", t
);
3897 /* Non-static data member initializers make the default constructor
3899 if (any_default_members
)
3901 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3902 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3905 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3907 TYPE_PACKED (t
) = 0;
3909 /* Check anonymous struct/anonymous union fields. */
3910 finish_struct_anon (t
);
3912 /* We've built up the list of access declarations in reverse order.
3914 *access_decls
= nreverse (*access_decls
);
3917 /* If TYPE is an empty class type, records its OFFSET in the table of
3921 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3925 if (!is_empty_class (type
))
3928 /* Record the location of this empty object in OFFSETS. */
3929 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3931 n
= splay_tree_insert (offsets
,
3932 (splay_tree_key
) offset
,
3933 (splay_tree_value
) NULL_TREE
);
3934 n
->value
= ((splay_tree_value
)
3935 tree_cons (NULL_TREE
,
3942 /* Returns nonzero if TYPE is an empty class type and there is
3943 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3946 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3951 if (!is_empty_class (type
))
3954 /* Record the location of this empty object in OFFSETS. */
3955 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3959 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3960 if (same_type_p (TREE_VALUE (t
), type
))
3966 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3967 F for every subobject, passing it the type, offset, and table of
3968 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3971 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3972 than MAX_OFFSET will not be walked.
3974 If F returns a nonzero value, the traversal ceases, and that value
3975 is returned. Otherwise, returns zero. */
3978 walk_subobject_offsets (tree type
,
3979 subobject_offset_fn f
,
3986 tree type_binfo
= NULL_TREE
;
3988 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3990 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
3993 if (type
== error_mark_node
)
3999 type
= BINFO_TYPE (type
);
4002 if (CLASS_TYPE_P (type
))
4008 /* Avoid recursing into objects that are not interesting. */
4009 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
4012 /* Record the location of TYPE. */
4013 r
= (*f
) (type
, offset
, offsets
);
4017 /* Iterate through the direct base classes of TYPE. */
4019 type_binfo
= TYPE_BINFO (type
);
4020 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
4024 if (BINFO_VIRTUAL_P (binfo
))
4028 /* We cannot rely on BINFO_OFFSET being set for the base
4029 class yet, but the offsets for direct non-virtual
4030 bases can be calculated by going back to the TYPE. */
4031 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
4032 binfo_offset
= size_binop (PLUS_EXPR
,
4034 BINFO_OFFSET (orig_binfo
));
4036 r
= walk_subobject_offsets (binfo
,
4046 if (CLASSTYPE_VBASECLASSES (type
))
4049 vec
<tree
, va_gc
> *vbases
;
4051 /* Iterate through the virtual base classes of TYPE. In G++
4052 3.2, we included virtual bases in the direct base class
4053 loop above, which results in incorrect results; the
4054 correct offsets for virtual bases are only known when
4055 working with the most derived type. */
4057 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
4058 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
4060 r
= walk_subobject_offsets (binfo
,
4062 size_binop (PLUS_EXPR
,
4064 BINFO_OFFSET (binfo
)),
4073 /* We still have to walk the primary base, if it is
4074 virtual. (If it is non-virtual, then it was walked
4076 tree vbase
= get_primary_binfo (type_binfo
);
4078 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
4079 && BINFO_PRIMARY_P (vbase
)
4080 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
4082 r
= (walk_subobject_offsets
4084 offsets
, max_offset
, /*vbases_p=*/0));
4091 /* Iterate through the fields of TYPE. */
4092 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4093 if (TREE_CODE (field
) == FIELD_DECL
4094 && TREE_TYPE (field
) != error_mark_node
4095 && !DECL_ARTIFICIAL (field
))
4099 field_offset
= byte_position (field
);
4101 r
= walk_subobject_offsets (TREE_TYPE (field
),
4103 size_binop (PLUS_EXPR
,
4113 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4115 tree element_type
= strip_array_types (type
);
4116 tree domain
= TYPE_DOMAIN (type
);
4119 /* Avoid recursing into objects that are not interesting. */
4120 if (!CLASS_TYPE_P (element_type
)
4121 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
))
4124 /* Step through each of the elements in the array. */
4125 for (index
= size_zero_node
;
4126 !tree_int_cst_lt (TYPE_MAX_VALUE (domain
), index
);
4127 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
4129 r
= walk_subobject_offsets (TREE_TYPE (type
),
4137 offset
= size_binop (PLUS_EXPR
, offset
,
4138 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
4139 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4140 there's no point in iterating through the remaining
4141 elements of the array. */
4142 if (max_offset
&& tree_int_cst_lt (max_offset
, offset
))
4150 /* Record all of the empty subobjects of TYPE (either a type or a
4151 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4152 is being placed at OFFSET; otherwise, it is a base class that is
4153 being placed at OFFSET. */
4156 record_subobject_offsets (tree type
,
4159 bool is_data_member
)
4162 /* If recording subobjects for a non-static data member or a
4163 non-empty base class , we do not need to record offsets beyond
4164 the size of the biggest empty class. Additional data members
4165 will go at the end of the class. Additional base classes will go
4166 either at offset zero (if empty, in which case they cannot
4167 overlap with offsets past the size of the biggest empty class) or
4168 at the end of the class.
4170 However, if we are placing an empty base class, then we must record
4171 all offsets, as either the empty class is at offset zero (where
4172 other empty classes might later be placed) or at the end of the
4173 class (where other objects might then be placed, so other empty
4174 subobjects might later overlap). */
4176 || !is_empty_class (BINFO_TYPE (type
)))
4177 max_offset
= sizeof_biggest_empty_class
;
4179 max_offset
= NULL_TREE
;
4180 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
4181 offsets
, max_offset
, is_data_member
);
4184 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4185 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4186 virtual bases of TYPE are examined. */
4189 layout_conflict_p (tree type
,
4194 splay_tree_node max_node
;
4196 /* Get the node in OFFSETS that indicates the maximum offset where
4197 an empty subobject is located. */
4198 max_node
= splay_tree_max (offsets
);
4199 /* If there aren't any empty subobjects, then there's no point in
4200 performing this check. */
4204 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
4205 offsets
, (tree
) (max_node
->key
),
4209 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4210 non-static data member of the type indicated by RLI. BINFO is the
4211 binfo corresponding to the base subobject, OFFSETS maps offsets to
4212 types already located at those offsets. This function determines
4213 the position of the DECL. */
4216 layout_nonempty_base_or_field (record_layout_info rli
,
4221 tree offset
= NULL_TREE
;
4227 /* For the purposes of determining layout conflicts, we want to
4228 use the class type of BINFO; TREE_TYPE (DECL) will be the
4229 CLASSTYPE_AS_BASE version, which does not contain entries for
4230 zero-sized bases. */
4231 type
= TREE_TYPE (binfo
);
4236 type
= TREE_TYPE (decl
);
4240 /* Try to place the field. It may take more than one try if we have
4241 a hard time placing the field without putting two objects of the
4242 same type at the same address. */
4245 struct record_layout_info_s old_rli
= *rli
;
4247 /* Place this field. */
4248 place_field (rli
, decl
);
4249 offset
= byte_position (decl
);
4251 /* We have to check to see whether or not there is already
4252 something of the same type at the offset we're about to use.
4253 For example, consider:
4256 struct T : public S { int i; };
4257 struct U : public S, public T {};
4259 Here, we put S at offset zero in U. Then, we can't put T at
4260 offset zero -- its S component would be at the same address
4261 as the S we already allocated. So, we have to skip ahead.
4262 Since all data members, including those whose type is an
4263 empty class, have nonzero size, any overlap can happen only
4264 with a direct or indirect base-class -- it can't happen with
4266 /* In a union, overlap is permitted; all members are placed at
4268 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
4270 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
4273 /* Strip off the size allocated to this field. That puts us
4274 at the first place we could have put the field with
4275 proper alignment. */
4278 /* Bump up by the alignment required for the type. */
4280 = size_binop (PLUS_EXPR
, rli
->bitpos
,
4282 ? CLASSTYPE_ALIGN (type
)
4283 : TYPE_ALIGN (type
)));
4284 normalize_rli (rli
);
4287 /* There was no conflict. We're done laying out this field. */
4291 /* Now that we know where it will be placed, update its
4293 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
4294 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4295 this point because their BINFO_OFFSET is copied from another
4296 hierarchy. Therefore, we may not need to add the entire
4298 propagate_binfo_offsets (binfo
,
4299 size_diffop_loc (input_location
,
4300 convert (ssizetype
, offset
),
4302 BINFO_OFFSET (binfo
))));
4305 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4308 empty_base_at_nonzero_offset_p (tree type
,
4310 splay_tree
/*offsets*/)
4312 return is_empty_class (type
) && !integer_zerop (offset
);
4315 /* Layout the empty base BINFO. EOC indicates the byte currently just
4316 past the end of the class, and should be correctly aligned for a
4317 class of the type indicated by BINFO; OFFSETS gives the offsets of
4318 the empty bases allocated so far. T is the most derived
4319 type. Return nonzero iff we added it at the end. */
4322 layout_empty_base (record_layout_info rli
, tree binfo
,
4323 tree eoc
, splay_tree offsets
)
4326 tree basetype
= BINFO_TYPE (binfo
);
4329 /* This routine should only be used for empty classes. */
4330 gcc_assert (is_empty_class (basetype
));
4331 alignment
= ssize_int (CLASSTYPE_ALIGN_UNIT (basetype
));
4333 if (!integer_zerop (BINFO_OFFSET (binfo
)))
4334 propagate_binfo_offsets
4335 (binfo
, size_diffop_loc (input_location
,
4336 size_zero_node
, BINFO_OFFSET (binfo
)));
4338 /* This is an empty base class. We first try to put it at offset
4340 if (layout_conflict_p (binfo
,
4341 BINFO_OFFSET (binfo
),
4345 /* That didn't work. Now, we move forward from the next
4346 available spot in the class. */
4348 propagate_binfo_offsets (binfo
, convert (ssizetype
, eoc
));
4351 if (!layout_conflict_p (binfo
,
4352 BINFO_OFFSET (binfo
),
4355 /* We finally found a spot where there's no overlap. */
4358 /* There's overlap here, too. Bump along to the next spot. */
4359 propagate_binfo_offsets (binfo
, alignment
);
4363 if (CLASSTYPE_USER_ALIGN (basetype
))
4365 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (basetype
));
4367 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (basetype
));
4368 TYPE_USER_ALIGN (rli
->t
) = 1;
4374 /* Layout the base given by BINFO in the class indicated by RLI.
4375 *BASE_ALIGN is a running maximum of the alignments of
4376 any base class. OFFSETS gives the location of empty base
4377 subobjects. T is the most derived type. Return nonzero if the new
4378 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4379 *NEXT_FIELD, unless BINFO is for an empty base class.
4381 Returns the location at which the next field should be inserted. */
4384 build_base_field (record_layout_info rli
, tree binfo
,
4385 splay_tree offsets
, tree
*next_field
)
4388 tree basetype
= BINFO_TYPE (binfo
);
4390 if (!COMPLETE_TYPE_P (basetype
))
4391 /* This error is now reported in xref_tag, thus giving better
4392 location information. */
4395 /* Place the base class. */
4396 if (!is_empty_class (basetype
))
4400 /* The containing class is non-empty because it has a non-empty
4402 CLASSTYPE_EMPTY_P (t
) = 0;
4404 /* Create the FIELD_DECL. */
4405 decl
= build_decl (input_location
,
4406 FIELD_DECL
, NULL_TREE
, CLASSTYPE_AS_BASE (basetype
));
4407 DECL_ARTIFICIAL (decl
) = 1;
4408 DECL_IGNORED_P (decl
) = 1;
4409 DECL_FIELD_CONTEXT (decl
) = t
;
4410 if (CLASSTYPE_AS_BASE (basetype
))
4412 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4413 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4414 DECL_ALIGN (decl
) = CLASSTYPE_ALIGN (basetype
);
4415 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4416 DECL_MODE (decl
) = TYPE_MODE (basetype
);
4417 DECL_FIELD_IS_BASE (decl
) = 1;
4419 /* Try to place the field. It may take more than one try if we
4420 have a hard time placing the field without putting two
4421 objects of the same type at the same address. */
4422 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4423 /* Add the new FIELD_DECL to the list of fields for T. */
4424 DECL_CHAIN (decl
) = *next_field
;
4426 next_field
= &DECL_CHAIN (decl
);
4434 /* On some platforms (ARM), even empty classes will not be
4436 eoc
= round_up_loc (input_location
,
4437 rli_size_unit_so_far (rli
),
4438 CLASSTYPE_ALIGN_UNIT (basetype
));
4439 atend
= layout_empty_base (rli
, binfo
, eoc
, offsets
);
4440 /* A nearly-empty class "has no proper base class that is empty,
4441 not morally virtual, and at an offset other than zero." */
4442 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4445 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4446 /* The check above (used in G++ 3.2) is insufficient because
4447 an empty class placed at offset zero might itself have an
4448 empty base at a nonzero offset. */
4449 else if (walk_subobject_offsets (basetype
,
4450 empty_base_at_nonzero_offset_p
,
4453 /*max_offset=*/NULL_TREE
,
4455 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4458 /* We do not create a FIELD_DECL for empty base classes because
4459 it might overlap some other field. We want to be able to
4460 create CONSTRUCTORs for the class by iterating over the
4461 FIELD_DECLs, and the back end does not handle overlapping
4464 /* An empty virtual base causes a class to be non-empty
4465 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4466 here because that was already done when the virtual table
4467 pointer was created. */
4470 /* Record the offsets of BINFO and its base subobjects. */
4471 record_subobject_offsets (binfo
,
4472 BINFO_OFFSET (binfo
),
4474 /*is_data_member=*/false);
4479 /* Layout all of the non-virtual base classes. Record empty
4480 subobjects in OFFSETS. T is the most derived type. Return nonzero
4481 if the type cannot be nearly empty. The fields created
4482 corresponding to the base classes will be inserted at
4486 build_base_fields (record_layout_info rli
,
4487 splay_tree offsets
, tree
*next_field
)
4489 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4492 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
4495 /* The primary base class is always allocated first. */
4496 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
4497 next_field
= build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (t
),
4498 offsets
, next_field
);
4500 /* Now allocate the rest of the bases. */
4501 for (i
= 0; i
< n_baseclasses
; ++i
)
4505 base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
4507 /* The primary base was already allocated above, so we don't
4508 need to allocate it again here. */
4509 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
4512 /* Virtual bases are added at the end (a primary virtual base
4513 will have already been added). */
4514 if (BINFO_VIRTUAL_P (base_binfo
))
4517 next_field
= build_base_field (rli
, base_binfo
,
4518 offsets
, next_field
);
4522 /* Go through the TYPE_METHODS of T issuing any appropriate
4523 diagnostics, figuring out which methods override which other
4524 methods, and so forth. */
4527 check_methods (tree t
)
4531 for (x
= TYPE_METHODS (t
); x
; x
= DECL_CHAIN (x
))
4533 check_for_override (x
, t
);
4534 if (DECL_PURE_VIRTUAL_P (x
) && (TREE_CODE (x
) != FUNCTION_DECL
|| ! DECL_VINDEX (x
)))
4535 error ("initializer specified for non-virtual method %q+D", x
);
4536 /* The name of the field is the original field name
4537 Save this in auxiliary field for later overloading. */
4538 if (TREE_CODE (x
) == FUNCTION_DECL
&& DECL_VINDEX (x
))
4540 TYPE_POLYMORPHIC_P (t
) = 1;
4541 if (DECL_PURE_VIRTUAL_P (x
))
4542 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4544 /* All user-provided destructors are non-trivial.
4545 Constructors and assignment ops are handled in
4546 grok_special_member_properties. */
4547 if (DECL_DESTRUCTOR_P (x
) && user_provided_p (x
))
4548 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 1;
4552 /* FN is a constructor or destructor. Clone the declaration to create
4553 a specialized in-charge or not-in-charge version, as indicated by
4557 build_clone (tree fn
, tree name
)
4562 /* Copy the function. */
4563 clone
= copy_decl (fn
);
4564 /* Reset the function name. */
4565 DECL_NAME (clone
) = name
;
4566 /* Remember where this function came from. */
4567 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4568 /* Make it easy to find the CLONE given the FN. */
4569 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4570 DECL_CHAIN (fn
) = clone
;
4572 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4573 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4575 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4576 DECL_TEMPLATE_RESULT (clone
) = result
;
4577 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4578 DECL_TI_TEMPLATE (result
) = clone
;
4579 TREE_TYPE (clone
) = TREE_TYPE (result
);
4583 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4584 DECL_CLONED_FUNCTION (clone
) = fn
;
4585 /* There's no pending inline data for this function. */
4586 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4587 DECL_PENDING_INLINE_P (clone
) = 0;
4589 /* The base-class destructor is not virtual. */
4590 if (name
== base_dtor_identifier
)
4592 DECL_VIRTUAL_P (clone
) = 0;
4593 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4594 DECL_VINDEX (clone
) = NULL_TREE
;
4597 /* If there was an in-charge parameter, drop it from the function
4599 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4605 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4606 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4607 parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4608 /* Skip the `this' parameter. */
4609 parmtypes
= TREE_CHAIN (parmtypes
);
4610 /* Skip the in-charge parameter. */
4611 parmtypes
= TREE_CHAIN (parmtypes
);
4612 /* And the VTT parm, in a complete [cd]tor. */
4613 if (DECL_HAS_VTT_PARM_P (fn
)
4614 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4615 parmtypes
= TREE_CHAIN (parmtypes
);
4616 /* If this is subobject constructor or destructor, add the vtt
4619 = build_method_type_directly (basetype
,
4620 TREE_TYPE (TREE_TYPE (clone
)),
4623 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
),
4626 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4627 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4630 /* Copy the function parameters. */
4631 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4632 /* Remove the in-charge parameter. */
4633 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4635 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4636 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4637 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4639 /* And the VTT parm, in a complete [cd]tor. */
4640 if (DECL_HAS_VTT_PARM_P (fn
))
4642 if (DECL_NEEDS_VTT_PARM_P (clone
))
4643 DECL_HAS_VTT_PARM_P (clone
) = 1;
4646 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4647 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4648 DECL_HAS_VTT_PARM_P (clone
) = 0;
4652 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4654 DECL_CONTEXT (parms
) = clone
;
4655 cxx_dup_lang_specific_decl (parms
);
4658 /* Create the RTL for this function. */
4659 SET_DECL_RTL (clone
, NULL
);
4660 rest_of_decl_compilation (clone
, /*top_level=*/1, at_eof
);
4663 note_decl_for_pch (clone
);
4668 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4669 not invoke this function directly.
4671 For a non-thunk function, returns the address of the slot for storing
4672 the function it is a clone of. Otherwise returns NULL_TREE.
4674 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4675 cloned_function is unset. This is to support the separate
4676 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4677 on a template makes sense, but not the former. */
4680 decl_cloned_function_p (const_tree decl
, bool just_testing
)
4684 decl
= STRIP_TEMPLATE (decl
);
4686 if (TREE_CODE (decl
) != FUNCTION_DECL
4687 || !DECL_LANG_SPECIFIC (decl
)
4688 || DECL_LANG_SPECIFIC (decl
)->u
.fn
.thunk_p
)
4690 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4692 lang_check_failed (__FILE__
, __LINE__
, __FUNCTION__
);
4698 ptr
= &DECL_LANG_SPECIFIC (decl
)->u
.fn
.u5
.cloned_function
;
4699 if (just_testing
&& *ptr
== NULL_TREE
)
4705 /* Produce declarations for all appropriate clones of FN. If
4706 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4707 CLASTYPE_METHOD_VEC as well. */
4710 clone_function_decl (tree fn
, int update_method_vec_p
)
4714 /* Avoid inappropriate cloning. */
4716 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4719 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4721 /* For each constructor, we need two variants: an in-charge version
4722 and a not-in-charge version. */
4723 clone
= build_clone (fn
, complete_ctor_identifier
);
4724 if (update_method_vec_p
)
4725 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4726 clone
= build_clone (fn
, base_ctor_identifier
);
4727 if (update_method_vec_p
)
4728 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4732 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4734 /* For each destructor, we need three variants: an in-charge
4735 version, a not-in-charge version, and an in-charge deleting
4736 version. We clone the deleting version first because that
4737 means it will go second on the TYPE_METHODS list -- and that
4738 corresponds to the correct layout order in the virtual
4741 For a non-virtual destructor, we do not build a deleting
4743 if (DECL_VIRTUAL_P (fn
))
4745 clone
= build_clone (fn
, deleting_dtor_identifier
);
4746 if (update_method_vec_p
)
4747 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4749 clone
= build_clone (fn
, complete_dtor_identifier
);
4750 if (update_method_vec_p
)
4751 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4752 clone
= build_clone (fn
, base_dtor_identifier
);
4753 if (update_method_vec_p
)
4754 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4757 /* Note that this is an abstract function that is never emitted. */
4758 DECL_ABSTRACT_P (fn
) = true;
4761 /* DECL is an in charge constructor, which is being defined. This will
4762 have had an in class declaration, from whence clones were
4763 declared. An out-of-class definition can specify additional default
4764 arguments. As it is the clones that are involved in overload
4765 resolution, we must propagate the information from the DECL to its
4769 adjust_clone_args (tree decl
)
4773 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4774 clone
= DECL_CHAIN (clone
))
4776 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4777 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4778 tree decl_parms
, clone_parms
;
4780 clone_parms
= orig_clone_parms
;
4782 /* Skip the 'this' parameter. */
4783 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4784 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4786 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4787 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4788 if (DECL_HAS_VTT_PARM_P (decl
))
4789 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4791 clone_parms
= orig_clone_parms
;
4792 if (DECL_HAS_VTT_PARM_P (clone
))
4793 clone_parms
= TREE_CHAIN (clone_parms
);
4795 for (decl_parms
= orig_decl_parms
; decl_parms
;
4796 decl_parms
= TREE_CHAIN (decl_parms
),
4797 clone_parms
= TREE_CHAIN (clone_parms
))
4799 gcc_assert (same_type_p (TREE_TYPE (decl_parms
),
4800 TREE_TYPE (clone_parms
)));
4802 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
4804 /* A default parameter has been added. Adjust the
4805 clone's parameters. */
4806 tree exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4807 tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
));
4808 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4811 clone_parms
= orig_decl_parms
;
4813 if (DECL_HAS_VTT_PARM_P (clone
))
4815 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
4816 TREE_VALUE (orig_clone_parms
),
4818 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
4820 type
= build_method_type_directly (basetype
,
4821 TREE_TYPE (TREE_TYPE (clone
)),
4824 type
= build_exception_variant (type
, exceptions
);
4826 type
= cp_build_type_attribute_variant (type
, attrs
);
4827 TREE_TYPE (clone
) = type
;
4829 clone_parms
= NULL_TREE
;
4833 gcc_assert (!clone_parms
);
4837 /* For each of the constructors and destructors in T, create an
4838 in-charge and not-in-charge variant. */
4841 clone_constructors_and_destructors (tree t
)
4845 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4847 if (!CLASSTYPE_METHOD_VEC (t
))
4850 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4851 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4852 for (fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4853 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4856 /* Deduce noexcept for a destructor DTOR. */
4859 deduce_noexcept_on_destructor (tree dtor
)
4861 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
4863 tree eh_spec
= unevaluated_noexcept_spec ();
4864 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
), eh_spec
);
4868 /* For each destructor in T, deduce noexcept:
4870 12.4/3: A declaration of a destructor that does not have an
4871 exception-specification is implicitly considered to have the
4872 same exception-specification as an implicit declaration (15.4). */
4875 deduce_noexcept_on_destructors (tree t
)
4877 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4879 if (!CLASSTYPE_METHOD_VEC (t
))
4882 for (tree fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4883 deduce_noexcept_on_destructor (OVL_CURRENT (fns
));
4886 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4887 of TYPE for virtual functions which FNDECL overrides. Return a
4888 mask of the tm attributes found therein. */
4891 look_for_tm_attr_overrides (tree type
, tree fndecl
)
4893 tree binfo
= TYPE_BINFO (type
);
4897 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
4899 tree o
, basetype
= BINFO_TYPE (base_binfo
);
4901 if (!TYPE_POLYMORPHIC_P (basetype
))
4904 o
= look_for_overrides_here (basetype
, fndecl
);
4906 found
|= tm_attr_to_mask (find_tm_attribute
4907 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
4909 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
4915 /* Subroutine of set_method_tm_attributes. Handle the checks and
4916 inheritance for one virtual method FNDECL. */
4919 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
4924 found
= look_for_tm_attr_overrides (type
, fndecl
);
4926 /* If FNDECL doesn't actually override anything (i.e. T is the
4927 class that first declares FNDECL virtual), then we're done. */
4931 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
4932 have
= tm_attr_to_mask (tm_attr
);
4934 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4935 tm_pure must match exactly, otherwise no weakening of
4936 tm_safe > tm_callable > nothing. */
4937 /* ??? The tm_pure attribute didn't make the transition to the
4938 multivendor language spec. */
4939 if (have
== TM_ATTR_PURE
)
4941 if (found
!= TM_ATTR_PURE
)
4947 /* If the overridden function is tm_pure, then FNDECL must be. */
4948 else if (found
== TM_ATTR_PURE
&& tm_attr
)
4950 /* Look for base class combinations that cannot be satisfied. */
4951 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
4953 found
&= ~TM_ATTR_PURE
;
4955 error_at (DECL_SOURCE_LOCATION (fndecl
),
4956 "method overrides both %<transaction_pure%> and %qE methods",
4957 tm_mask_to_attr (found
));
4959 /* If FNDECL did not declare an attribute, then inherit the most
4961 else if (tm_attr
== NULL
)
4963 apply_tm_attr (fndecl
, tm_mask_to_attr (found
& -found
));
4965 /* Otherwise validate that we're not weaker than a function
4966 that is being overridden. */
4970 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
4976 error_at (DECL_SOURCE_LOCATION (fndecl
),
4977 "method declared %qE overriding %qE method",
4978 tm_attr
, tm_mask_to_attr (found
));
4981 /* For each of the methods in T, propagate a class-level tm attribute. */
4984 set_method_tm_attributes (tree t
)
4986 tree class_tm_attr
, fndecl
;
4988 /* Don't bother collecting tm attributes if transactional memory
4989 support is not enabled. */
4993 /* Process virtual methods first, as they inherit directly from the
4994 base virtual function and also require validation of new attributes. */
4995 if (TYPE_CONTAINS_VPTR_P (t
))
4998 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
4999 vchain
= TREE_CHAIN (vchain
))
5001 fndecl
= BV_FN (vchain
);
5002 if (DECL_THUNK_P (fndecl
))
5003 fndecl
= THUNK_TARGET (fndecl
);
5004 set_one_vmethod_tm_attributes (t
, fndecl
);
5008 /* If the class doesn't have an attribute, nothing more to do. */
5009 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
5010 if (class_tm_attr
== NULL
)
5013 /* Any method that does not yet have a tm attribute inherits
5014 the one from the class. */
5015 for (fndecl
= TYPE_METHODS (t
); fndecl
; fndecl
= TREE_CHAIN (fndecl
))
5017 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
5018 apply_tm_attr (fndecl
, class_tm_attr
);
5022 /* Returns true iff class T has a user-defined constructor other than
5023 the default constructor. */
5026 type_has_user_nondefault_constructor (tree t
)
5030 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5033 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5035 tree fn
= OVL_CURRENT (fns
);
5036 if (!DECL_ARTIFICIAL (fn
)
5037 && (TREE_CODE (fn
) == TEMPLATE_DECL
5038 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
5046 /* Returns the defaulted constructor if T has one. Otherwise, returns
5050 in_class_defaulted_default_constructor (tree t
)
5054 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5057 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5059 tree fn
= OVL_CURRENT (fns
);
5061 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
5063 args
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
5064 while (args
&& TREE_PURPOSE (args
))
5065 args
= TREE_CHAIN (args
);
5066 if (!args
|| args
== void_list_node
)
5074 /* Returns true iff FN is a user-provided function, i.e. user-declared
5075 and not defaulted at its first declaration; or explicit, private,
5076 protected, or non-const. */
5079 user_provided_p (tree fn
)
5081 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5084 return (!DECL_ARTIFICIAL (fn
)
5085 && !(DECL_INITIALIZED_IN_CLASS_P (fn
)
5086 && (DECL_DEFAULTED_FN (fn
) || DECL_DELETED_FN (fn
))));
5089 /* Returns true iff class T has a user-provided constructor. */
5092 type_has_user_provided_constructor (tree t
)
5096 if (!CLASS_TYPE_P (t
))
5099 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
5102 /* This can happen in error cases; avoid crashing. */
5103 if (!CLASSTYPE_METHOD_VEC (t
))
5106 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5107 if (user_provided_p (OVL_CURRENT (fns
)))
5113 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5114 declared or explicitly defaulted in the class body) default
5118 type_has_non_user_provided_default_constructor (tree t
)
5122 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t
))
5124 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5127 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5129 tree fn
= OVL_CURRENT (fns
);
5130 if (TREE_CODE (fn
) == FUNCTION_DECL
5131 && !user_provided_p (fn
)
5132 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)))
5139 /* TYPE is being used as a virtual base, and has a non-trivial move
5140 assignment. Return true if this is due to there being a user-provided
5141 move assignment in TYPE or one of its subobjects; if there isn't, then
5142 multiple move assignment can't cause any harm. */
5145 vbase_has_user_provided_move_assign (tree type
)
5147 /* Does the type itself have a user-provided move assignment operator? */
5149 = lookup_fnfields_slot_nolazy (type
, ansi_assopname (NOP_EXPR
));
5150 fns
; fns
= OVL_NEXT (fns
))
5152 tree fn
= OVL_CURRENT (fns
);
5153 if (move_fn_p (fn
) && user_provided_p (fn
))
5157 /* Do any of its bases? */
5158 tree binfo
= TYPE_BINFO (type
);
5160 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5161 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
5164 /* Or non-static data members? */
5165 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5167 if (TREE_CODE (field
) == FIELD_DECL
5168 && CLASS_TYPE_P (TREE_TYPE (field
))
5169 && vbase_has_user_provided_move_assign (TREE_TYPE (field
)))
5177 /* If default-initialization leaves part of TYPE uninitialized, returns
5178 a DECL for the field or TYPE itself (DR 253). */
5181 default_init_uninitialized_part (tree type
)
5186 type
= strip_array_types (type
);
5187 if (!CLASS_TYPE_P (type
))
5189 if (!type_has_non_user_provided_default_constructor (type
))
5191 for (binfo
= TYPE_BINFO (type
), i
= 0;
5192 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
5194 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
5198 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
5199 if (TREE_CODE (t
) == FIELD_DECL
5200 && !DECL_ARTIFICIAL (t
)
5201 && !DECL_INITIAL (t
))
5203 r
= default_init_uninitialized_part (TREE_TYPE (t
));
5205 return DECL_P (r
) ? r
: t
;
5211 /* Returns true iff for class T, a trivial synthesized default constructor
5212 would be constexpr. */
5215 trivial_default_constructor_is_constexpr (tree t
)
5217 /* A defaulted trivial default constructor is constexpr
5218 if there is nothing to initialize. */
5219 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
5220 return is_really_empty_class (t
);
5223 /* Returns true iff class T has a constexpr default constructor. */
5226 type_has_constexpr_default_constructor (tree t
)
5230 if (!CLASS_TYPE_P (t
))
5232 /* The caller should have stripped an enclosing array. */
5233 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
5236 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
5238 if (!TYPE_HAS_COMPLEX_DFLT (t
))
5239 return trivial_default_constructor_is_constexpr (t
);
5240 /* Non-trivial, we need to check subobject constructors. */
5241 lazily_declare_fn (sfk_constructor
, t
);
5243 fns
= locate_ctor (t
);
5244 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
5247 /* Returns true iff class TYPE has a virtual destructor. */
5250 type_has_virtual_destructor (tree type
)
5254 if (!CLASS_TYPE_P (type
))
5257 gcc_assert (COMPLETE_TYPE_P (type
));
5258 dtor
= CLASSTYPE_DESTRUCTORS (type
);
5259 return (dtor
&& DECL_VIRTUAL_P (dtor
));
5262 /* Returns true iff class T has a move constructor. */
5265 type_has_move_constructor (tree t
)
5269 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5271 gcc_assert (COMPLETE_TYPE_P (t
));
5272 lazily_declare_fn (sfk_move_constructor
, t
);
5275 if (!CLASSTYPE_METHOD_VEC (t
))
5278 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5279 if (move_fn_p (OVL_CURRENT (fns
)))
5285 /* Returns true iff class T has a move assignment operator. */
5288 type_has_move_assign (tree t
)
5292 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5294 gcc_assert (COMPLETE_TYPE_P (t
));
5295 lazily_declare_fn (sfk_move_assignment
, t
);
5298 for (fns
= lookup_fnfields_slot_nolazy (t
, ansi_assopname (NOP_EXPR
));
5299 fns
; fns
= OVL_NEXT (fns
))
5300 if (move_fn_p (OVL_CURRENT (fns
)))
5306 /* Returns true iff class T has a move constructor that was explicitly
5307 declared in the class body. Note that this is different from
5308 "user-provided", which doesn't include functions that are defaulted in
5312 type_has_user_declared_move_constructor (tree t
)
5316 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
5319 if (!CLASSTYPE_METHOD_VEC (t
))
5322 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5324 tree fn
= OVL_CURRENT (fns
);
5325 if (move_fn_p (fn
) && !DECL_ARTIFICIAL (fn
))
5332 /* Returns true iff class T has a move assignment operator that was
5333 explicitly declared in the class body. */
5336 type_has_user_declared_move_assign (tree t
)
5340 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
5343 for (fns
= lookup_fnfields_slot_nolazy (t
, ansi_assopname (NOP_EXPR
));
5344 fns
; fns
= OVL_NEXT (fns
))
5346 tree fn
= OVL_CURRENT (fns
);
5347 if (move_fn_p (fn
) && !DECL_ARTIFICIAL (fn
))
5354 /* Nonzero if we need to build up a constructor call when initializing an
5355 object of this class, either because it has a user-declared constructor
5356 or because it doesn't have a default constructor (so we need to give an
5357 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5358 what you care about is whether or not an object can be produced by a
5359 constructor (e.g. so we don't set TREE_READONLY on const variables of
5360 such type); use this function when what you care about is whether or not
5361 to try to call a constructor to create an object. The latter case is
5362 the former plus some cases of constructors that cannot be called. */
5365 type_build_ctor_call (tree t
)
5368 if (TYPE_NEEDS_CONSTRUCTING (t
))
5370 inner
= strip_array_types (t
);
5371 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
))
5373 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
))
5375 if (cxx_dialect
< cxx11
)
5377 /* A user-declared constructor might be private, and a constructor might
5378 be trivial but deleted. */
5379 for (tree fns
= lookup_fnfields_slot (inner
, complete_ctor_identifier
);
5380 fns
; fns
= OVL_NEXT (fns
))
5382 tree fn
= OVL_CURRENT (fns
);
5383 if (!DECL_ARTIFICIAL (fn
)
5384 || DECL_DELETED_FN (fn
))
5390 /* Like type_build_ctor_call, but for destructors. */
5393 type_build_dtor_call (tree t
)
5396 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5398 inner
= strip_array_types (t
);
5399 if (!CLASS_TYPE_P (inner
) || ANON_AGGR_TYPE_P (inner
)
5400 || !COMPLETE_TYPE_P (inner
))
5402 if (cxx_dialect
< cxx11
)
5404 /* A user-declared destructor might be private, and a destructor might
5405 be trivial but deleted. */
5406 for (tree fns
= lookup_fnfields_slot (inner
, complete_dtor_identifier
);
5407 fns
; fns
= OVL_NEXT (fns
))
5409 tree fn
= OVL_CURRENT (fns
);
5410 if (!DECL_ARTIFICIAL (fn
)
5411 || DECL_DELETED_FN (fn
))
5417 /* Remove all zero-width bit-fields from T. */
5420 remove_zero_width_bit_fields (tree t
)
5424 fieldsp
= &TYPE_FIELDS (t
);
5427 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5428 && DECL_C_BIT_FIELD (*fieldsp
)
5429 /* We should not be confused by the fact that grokbitfield
5430 temporarily sets the width of the bit field into
5431 DECL_INITIAL (*fieldsp).
5432 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5434 && (DECL_SIZE (*fieldsp
) == NULL_TREE
5435 || integer_zerop (DECL_SIZE (*fieldsp
))))
5436 *fieldsp
= DECL_CHAIN (*fieldsp
);
5438 fieldsp
= &DECL_CHAIN (*fieldsp
);
5442 /* Returns TRUE iff we need a cookie when dynamically allocating an
5443 array whose elements have the indicated class TYPE. */
5446 type_requires_array_cookie (tree type
)
5449 bool has_two_argument_delete_p
= false;
5451 gcc_assert (CLASS_TYPE_P (type
));
5453 /* If there's a non-trivial destructor, we need a cookie. In order
5454 to iterate through the array calling the destructor for each
5455 element, we'll have to know how many elements there are. */
5456 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5459 /* If the usual deallocation function is a two-argument whose second
5460 argument is of type `size_t', then we have to pass the size of
5461 the array to the deallocation function, so we will need to store
5463 fns
= lookup_fnfields (TYPE_BINFO (type
),
5464 ansi_opname (VEC_DELETE_EXPR
),
5466 /* If there are no `operator []' members, or the lookup is
5467 ambiguous, then we don't need a cookie. */
5468 if (!fns
|| fns
== error_mark_node
)
5470 /* Loop through all of the functions. */
5471 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
5476 /* Select the current function. */
5477 fn
= OVL_CURRENT (fns
);
5478 /* See if this function is a one-argument delete function. If
5479 it is, then it will be the usual deallocation function. */
5480 second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5481 if (second_parm
== void_list_node
)
5483 /* Do not consider this function if its second argument is an
5487 /* Otherwise, if we have a two-argument function and the second
5488 argument is `size_t', it will be the usual deallocation
5489 function -- unless there is one-argument function, too. */
5490 if (TREE_CHAIN (second_parm
) == void_list_node
5491 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5492 has_two_argument_delete_p
= true;
5495 return has_two_argument_delete_p
;
5498 /* Finish computing the `literal type' property of class type T.
5500 At this point, we have already processed base classes and
5501 non-static data members. We need to check whether the copy
5502 constructor is trivial, the destructor is trivial, and there
5503 is a trivial default constructor or at least one constexpr
5504 constructor other than the copy constructor. */
5507 finalize_literal_type_property (tree t
)
5511 if (cxx_dialect
< cxx11
5512 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5513 CLASSTYPE_LITERAL_P (t
) = false;
5514 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5515 && CLASSTYPE_NON_AGGREGATE (t
)
5516 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5517 CLASSTYPE_LITERAL_P (t
) = false;
5519 if (!CLASSTYPE_LITERAL_P (t
))
5520 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5521 if (DECL_DECLARED_CONSTEXPR_P (fn
)
5522 && TREE_CODE (fn
) != TEMPLATE_DECL
5523 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5524 && !DECL_CONSTRUCTOR_P (fn
))
5526 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5527 if (!DECL_GENERATED_P (fn
))
5529 error ("enclosing class of constexpr non-static member "
5530 "function %q+#D is not a literal type", fn
);
5531 explain_non_literal_class (t
);
5536 /* T is a non-literal type used in a context which requires a constant
5537 expression. Explain why it isn't literal. */
5540 explain_non_literal_class (tree t
)
5542 static hash_set
<tree
> *diagnosed
;
5544 if (!CLASS_TYPE_P (t
))
5546 t
= TYPE_MAIN_VARIANT (t
);
5548 if (diagnosed
== NULL
)
5549 diagnosed
= new hash_set
<tree
>;
5550 if (diagnosed
->add (t
))
5551 /* Already explained. */
5554 inform (0, "%q+T is not literal because:", t
);
5555 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5556 inform (0, " %q+T has a non-trivial destructor", t
);
5557 else if (CLASSTYPE_NON_AGGREGATE (t
)
5558 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5559 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5561 inform (0, " %q+T is not an aggregate, does not have a trivial "
5562 "default constructor, and has no constexpr constructor that "
5563 "is not a copy or move constructor", t
);
5564 if (type_has_non_user_provided_default_constructor (t
))
5566 /* Note that we can't simply call locate_ctor because when the
5567 constructor is deleted it just returns NULL_TREE. */
5569 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5571 tree fn
= OVL_CURRENT (fns
);
5572 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5574 parms
= skip_artificial_parms_for (fn
, parms
);
5576 if (sufficient_parms_p (parms
))
5578 if (DECL_DELETED_FN (fn
))
5579 maybe_explain_implicit_delete (fn
);
5581 explain_invalid_constexpr_fn (fn
);
5589 tree binfo
, base_binfo
, field
; int i
;
5590 for (binfo
= TYPE_BINFO (t
), i
= 0;
5591 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5593 tree basetype
= TREE_TYPE (base_binfo
);
5594 if (!CLASSTYPE_LITERAL_P (basetype
))
5596 inform (0, " base class %qT of %q+T is non-literal",
5598 explain_non_literal_class (basetype
);
5602 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5605 if (TREE_CODE (field
) != FIELD_DECL
)
5607 ftype
= TREE_TYPE (field
);
5608 if (!literal_type_p (ftype
))
5610 inform (0, " non-static data member %q+D has "
5611 "non-literal type", field
);
5612 if (CLASS_TYPE_P (ftype
))
5613 explain_non_literal_class (ftype
);
5615 if (CP_TYPE_VOLATILE_P (ftype
))
5616 inform (0, " non-static data member %q+D has "
5617 "volatile type", field
);
5622 /* Check the validity of the bases and members declared in T. Add any
5623 implicitly-generated functions (like copy-constructors and
5624 assignment operators). Compute various flag bits (like
5625 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5626 level: i.e., independently of the ABI in use. */
5629 check_bases_and_members (tree t
)
5631 /* Nonzero if the implicitly generated copy constructor should take
5632 a non-const reference argument. */
5633 int cant_have_const_ctor
;
5634 /* Nonzero if the implicitly generated assignment operator
5635 should take a non-const reference argument. */
5636 int no_const_asn_ref
;
5638 bool saved_complex_asn_ref
;
5639 bool saved_nontrivial_dtor
;
5642 /* By default, we use const reference arguments and generate default
5644 cant_have_const_ctor
= 0;
5645 no_const_asn_ref
= 0;
5647 /* Check all the base-classes. */
5648 check_bases (t
, &cant_have_const_ctor
,
5651 /* Deduce noexcept on destructors. This needs to happen after we've set
5652 triviality flags appropriately for our bases. */
5653 if (cxx_dialect
>= cxx11
)
5654 deduce_noexcept_on_destructors (t
);
5656 /* Check all the method declarations. */
5659 /* Save the initial values of these flags which only indicate whether
5660 or not the class has user-provided functions. As we analyze the
5661 bases and members we can set these flags for other reasons. */
5662 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5663 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5665 /* Check all the data member declarations. We cannot call
5666 check_field_decls until we have called check_bases check_methods,
5667 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5668 being set appropriately. */
5669 check_field_decls (t
, &access_decls
,
5670 &cant_have_const_ctor
,
5673 /* A nearly-empty class has to be vptr-containing; a nearly empty
5674 class contains just a vptr. */
5675 if (!TYPE_CONTAINS_VPTR_P (t
))
5676 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5678 /* Do some bookkeeping that will guide the generation of implicitly
5679 declared member functions. */
5680 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5681 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5682 /* We need to call a constructor for this class if it has a
5683 user-provided constructor, or if the default constructor is going
5684 to initialize the vptr. (This is not an if-and-only-if;
5685 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5686 themselves need constructing.) */
5687 TYPE_NEEDS_CONSTRUCTING (t
)
5688 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5691 An aggregate is an array or a class with no user-provided
5692 constructors ... and no virtual functions.
5694 Again, other conditions for being an aggregate are checked
5696 CLASSTYPE_NON_AGGREGATE (t
)
5697 |= (type_has_user_provided_constructor (t
) || TYPE_POLYMORPHIC_P (t
));
5698 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5699 retain the old definition internally for ABI reasons. */
5700 CLASSTYPE_NON_LAYOUT_POD_P (t
)
5701 |= (CLASSTYPE_NON_AGGREGATE (t
)
5702 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
5703 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5704 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5705 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5706 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5708 /* If the only explicitly declared default constructor is user-provided,
5709 set TYPE_HAS_COMPLEX_DFLT. */
5710 if (!TYPE_HAS_COMPLEX_DFLT (t
)
5711 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
5712 && !type_has_non_user_provided_default_constructor (t
))
5713 TYPE_HAS_COMPLEX_DFLT (t
) = true;
5715 /* Warn if a public base of a polymorphic type has an accessible
5716 non-virtual destructor. It is only now that we know the class is
5717 polymorphic. Although a polymorphic base will have a already
5718 been diagnosed during its definition, we warn on use too. */
5719 if (TYPE_POLYMORPHIC_P (t
) && warn_nonvdtor
)
5721 tree binfo
= TYPE_BINFO (t
);
5722 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (binfo
);
5726 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5728 tree basetype
= TREE_TYPE (base_binfo
);
5730 if ((*accesses
)[i
] == access_public_node
5731 && (TYPE_POLYMORPHIC_P (basetype
) || warn_ecpp
)
5732 && accessible_nvdtor_p (basetype
))
5733 warning (OPT_Wnon_virtual_dtor
,
5734 "base class %q#T has accessible non-virtual destructor",
5739 /* If the class has no user-declared constructor, but does have
5740 non-static const or reference data members that can never be
5741 initialized, issue a warning. */
5742 if (warn_uninitialized
5743 /* Classes with user-declared constructors are presumed to
5744 initialize these members. */
5745 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
5746 /* Aggregates can be initialized with brace-enclosed
5748 && CLASSTYPE_NON_AGGREGATE (t
))
5752 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5756 if (TREE_CODE (field
) != FIELD_DECL
5757 || DECL_INITIAL (field
) != NULL_TREE
)
5760 type
= TREE_TYPE (field
);
5761 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5762 warning (OPT_Wuninitialized
, "non-static reference %q+#D "
5763 "in class without a constructor", field
);
5764 else if (CP_TYPE_CONST_P (type
)
5765 && (!CLASS_TYPE_P (type
)
5766 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
5767 warning (OPT_Wuninitialized
, "non-static const member %q+#D "
5768 "in class without a constructor", field
);
5772 /* Synthesize any needed methods. */
5773 add_implicitly_declared_members (t
, &access_decls
,
5774 cant_have_const_ctor
,
5777 /* Check defaulted declarations here so we have cant_have_const_ctor
5778 and don't need to worry about clones. */
5779 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5780 if (!DECL_ARTIFICIAL (fn
) && DECL_DEFAULTED_IN_CLASS_P (fn
))
5782 int copy
= copy_fn_p (fn
);
5786 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
5787 : !no_const_asn_ref
);
5788 bool fn_const_p
= (copy
== 2);
5790 if (fn_const_p
&& !imp_const_p
)
5791 /* If the function is defaulted outside the class, we just
5792 give the synthesis error. */
5793 error ("%q+D declared to take const reference, but implicit "
5794 "declaration would take non-const", fn
);
5796 defaulted_late_check (fn
);
5799 if (LAMBDA_TYPE_P (t
))
5801 /* "This class type is not an aggregate." */
5802 CLASSTYPE_NON_AGGREGATE (t
) = 1;
5805 /* Compute the 'literal type' property before we
5806 do anything with non-static member functions. */
5807 finalize_literal_type_property (t
);
5809 /* Create the in-charge and not-in-charge variants of constructors
5811 clone_constructors_and_destructors (t
);
5813 /* Process the using-declarations. */
5814 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
5815 handle_using_decl (TREE_VALUE (access_decls
), t
);
5817 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5818 finish_struct_methods (t
);
5820 /* Figure out whether or not we will need a cookie when dynamically
5821 allocating an array of this type. */
5822 TYPE_LANG_SPECIFIC (t
)->u
.c
.vec_new_uses_cookie
5823 = type_requires_array_cookie (t
);
5826 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5827 accordingly. If a new vfield was created (because T doesn't have a
5828 primary base class), then the newly created field is returned. It
5829 is not added to the TYPE_FIELDS list; it is the caller's
5830 responsibility to do that. Accumulate declared virtual functions
5834 create_vtable_ptr (tree t
, tree
* virtuals_p
)
5838 /* Collect the virtual functions declared in T. */
5839 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5840 if (TREE_CODE (fn
) == FUNCTION_DECL
5841 && DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
5842 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
5844 tree new_virtual
= make_node (TREE_LIST
);
5846 BV_FN (new_virtual
) = fn
;
5847 BV_DELTA (new_virtual
) = integer_zero_node
;
5848 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
5850 TREE_CHAIN (new_virtual
) = *virtuals_p
;
5851 *virtuals_p
= new_virtual
;
5854 /* If we couldn't find an appropriate base class, create a new field
5855 here. Even if there weren't any new virtual functions, we might need a
5856 new virtual function table if we're supposed to include vptrs in
5857 all classes that need them. */
5858 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
5860 /* We build this decl with vtbl_ptr_type_node, which is a
5861 `vtable_entry_type*'. It might seem more precise to use
5862 `vtable_entry_type (*)[N]' where N is the number of virtual
5863 functions. However, that would require the vtable pointer in
5864 base classes to have a different type than the vtable pointer
5865 in derived classes. We could make that happen, but that
5866 still wouldn't solve all the problems. In particular, the
5867 type-based alias analysis code would decide that assignments
5868 to the base class vtable pointer can't alias assignments to
5869 the derived class vtable pointer, since they have different
5870 types. Thus, in a derived class destructor, where the base
5871 class constructor was inlined, we could generate bad code for
5872 setting up the vtable pointer.
5874 Therefore, we use one type for all vtable pointers. We still
5875 use a type-correct type; it's just doesn't indicate the array
5876 bounds. That's better than using `void*' or some such; it's
5877 cleaner, and it let's the alias analysis code know that these
5878 stores cannot alias stores to void*! */
5881 field
= build_decl (input_location
,
5882 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
5883 DECL_VIRTUAL_P (field
) = 1;
5884 DECL_ARTIFICIAL (field
) = 1;
5885 DECL_FIELD_CONTEXT (field
) = t
;
5886 DECL_FCONTEXT (field
) = t
;
5887 if (TYPE_PACKED (t
))
5888 DECL_PACKED (field
) = 1;
5890 TYPE_VFIELD (t
) = field
;
5892 /* This class is non-empty. */
5893 CLASSTYPE_EMPTY_P (t
) = 0;
5901 /* Add OFFSET to all base types of BINFO which is a base in the
5902 hierarchy dominated by T.
5904 OFFSET, which is a type offset, is number of bytes. */
5907 propagate_binfo_offsets (tree binfo
, tree offset
)
5913 /* Update BINFO's offset. */
5914 BINFO_OFFSET (binfo
)
5915 = convert (sizetype
,
5916 size_binop (PLUS_EXPR
,
5917 convert (ssizetype
, BINFO_OFFSET (binfo
)),
5920 /* Find the primary base class. */
5921 primary_binfo
= get_primary_binfo (binfo
);
5923 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
5924 propagate_binfo_offsets (primary_binfo
, offset
);
5926 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5928 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5930 /* Don't do the primary base twice. */
5931 if (base_binfo
== primary_binfo
)
5934 if (BINFO_VIRTUAL_P (base_binfo
))
5937 propagate_binfo_offsets (base_binfo
, offset
);
5941 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5942 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5943 empty subobjects of T. */
5946 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
5952 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
5955 /* Find the last field. The artificial fields created for virtual
5956 bases will go after the last extant field to date. */
5957 next_field
= &TYPE_FIELDS (t
);
5959 next_field
= &DECL_CHAIN (*next_field
);
5961 /* Go through the virtual bases, allocating space for each virtual
5962 base that is not already a primary base class. These are
5963 allocated in inheritance graph order. */
5964 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
5966 if (!BINFO_VIRTUAL_P (vbase
))
5969 if (!BINFO_PRIMARY_P (vbase
))
5971 /* This virtual base is not a primary base of any class in the
5972 hierarchy, so we have to add space for it. */
5973 next_field
= build_base_field (rli
, vbase
,
5974 offsets
, next_field
);
5979 /* Returns the offset of the byte just past the end of the base class
5983 end_of_base (tree binfo
)
5987 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
5988 size
= TYPE_SIZE_UNIT (char_type_node
);
5989 else if (is_empty_class (BINFO_TYPE (binfo
)))
5990 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5991 allocate some space for it. It cannot have virtual bases, so
5992 TYPE_SIZE_UNIT is fine. */
5993 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5995 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5997 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
6000 /* Returns the offset of the byte just past the end of the base class
6001 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6002 only non-virtual bases are included. */
6005 end_of_class (tree t
, int include_virtuals_p
)
6007 tree result
= size_zero_node
;
6008 vec
<tree
, va_gc
> *vbases
;
6014 for (binfo
= TYPE_BINFO (t
), i
= 0;
6015 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6017 if (!include_virtuals_p
6018 && BINFO_VIRTUAL_P (base_binfo
)
6019 && (!BINFO_PRIMARY_P (base_binfo
)
6020 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
6023 offset
= end_of_base (base_binfo
);
6024 if (tree_int_cst_lt (result
, offset
))
6028 if (include_virtuals_p
)
6029 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6030 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
6032 offset
= end_of_base (base_binfo
);
6033 if (tree_int_cst_lt (result
, offset
))
6040 /* Warn about bases of T that are inaccessible because they are
6041 ambiguous. For example:
6044 struct T : public S {};
6045 struct U : public S, public T {};
6047 Here, `(S*) new U' is not allowed because there are two `S'
6051 warn_about_ambiguous_bases (tree t
)
6054 vec
<tree
, va_gc
> *vbases
;
6059 /* If there are no repeated bases, nothing can be ambiguous. */
6060 if (!CLASSTYPE_REPEATED_BASE_P (t
))
6063 /* Check direct bases. */
6064 for (binfo
= TYPE_BINFO (t
), i
= 0;
6065 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
6067 basetype
= BINFO_TYPE (base_binfo
);
6069 if (!uniquely_derived_from_p (basetype
, t
))
6070 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6074 /* Check for ambiguous virtual bases. */
6076 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
6077 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
6079 basetype
= BINFO_TYPE (binfo
);
6081 if (!uniquely_derived_from_p (basetype
, t
))
6082 warning (OPT_Wextra
, "virtual base %qT inaccessible in %qT due "
6083 "to ambiguity", basetype
, t
);
6087 /* Compare two INTEGER_CSTs K1 and K2. */
6090 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
6092 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6095 /* Increase the size indicated in RLI to account for empty classes
6096 that are "off the end" of the class. */
6099 include_empty_classes (record_layout_info rli
)
6104 /* It might be the case that we grew the class to allocate a
6105 zero-sized base class. That won't be reflected in RLI, yet,
6106 because we are willing to overlay multiple bases at the same
6107 offset. However, now we need to make sure that RLI is big enough
6108 to reflect the entire class. */
6109 eoc
= end_of_class (rli
->t
,
6110 CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
6111 rli_size
= rli_size_unit_so_far (rli
);
6112 if (TREE_CODE (rli_size
) == INTEGER_CST
6113 && tree_int_cst_lt (rli_size
, eoc
))
6115 /* The size should have been rounded to a whole byte. */
6116 gcc_assert (tree_int_cst_equal
6117 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
6119 = size_binop (PLUS_EXPR
,
6121 size_binop (MULT_EXPR
,
6122 convert (bitsizetype
,
6123 size_binop (MINUS_EXPR
,
6125 bitsize_int (BITS_PER_UNIT
)));
6126 normalize_rli (rli
);
6130 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6131 BINFO_OFFSETs for all of the base-classes. Position the vtable
6132 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6135 layout_class_type (tree t
, tree
*virtuals_p
)
6137 tree non_static_data_members
;
6140 record_layout_info rli
;
6141 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6142 types that appear at that offset. */
6143 splay_tree empty_base_offsets
;
6144 /* True if the last field laid out was a bit-field. */
6145 bool last_field_was_bitfield
= false;
6146 /* The location at which the next field should be inserted. */
6148 /* T, as a base class. */
6151 /* Keep track of the first non-static data member. */
6152 non_static_data_members
= TYPE_FIELDS (t
);
6154 /* Start laying out the record. */
6155 rli
= start_record_layout (t
);
6157 /* Mark all the primary bases in the hierarchy. */
6158 determine_primary_bases (t
);
6160 /* Create a pointer to our virtual function table. */
6161 vptr
= create_vtable_ptr (t
, virtuals_p
);
6163 /* The vptr is always the first thing in the class. */
6166 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
6167 TYPE_FIELDS (t
) = vptr
;
6168 next_field
= &DECL_CHAIN (vptr
);
6169 place_field (rli
, vptr
);
6172 next_field
= &TYPE_FIELDS (t
);
6174 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6175 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
6177 build_base_fields (rli
, empty_base_offsets
, next_field
);
6179 /* Layout the non-static data members. */
6180 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
6185 /* We still pass things that aren't non-static data members to
6186 the back end, in case it wants to do something with them. */
6187 if (TREE_CODE (field
) != FIELD_DECL
)
6189 place_field (rli
, field
);
6190 /* If the static data member has incomplete type, keep track
6191 of it so that it can be completed later. (The handling
6192 of pending statics in finish_record_layout is
6193 insufficient; consider:
6196 struct S2 { static S1 s1; };
6198 At this point, finish_record_layout will be called, but
6199 S1 is still incomplete.) */
6202 maybe_register_incomplete_var (field
);
6203 /* The visibility of static data members is determined
6204 at their point of declaration, not their point of
6206 determine_visibility (field
);
6211 type
= TREE_TYPE (field
);
6212 if (type
== error_mark_node
)
6215 padding
= NULL_TREE
;
6217 /* If this field is a bit-field whose width is greater than its
6218 type, then there are some special rules for allocating
6220 if (DECL_C_BIT_FIELD (field
)
6221 && tree_int_cst_lt (TYPE_SIZE (type
), DECL_SIZE (field
)))
6225 bool was_unnamed_p
= false;
6226 /* We must allocate the bits as if suitably aligned for the
6227 longest integer type that fits in this many bits. type
6228 of the field. Then, we are supposed to use the left over
6229 bits as additional padding. */
6230 for (itk
= itk_char
; itk
!= itk_none
; ++itk
)
6231 if (integer_types
[itk
] != NULL_TREE
6232 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE
),
6233 TYPE_SIZE (integer_types
[itk
]))
6234 || tree_int_cst_lt (DECL_SIZE (field
),
6235 TYPE_SIZE (integer_types
[itk
]))))
6238 /* ITK now indicates a type that is too large for the
6239 field. We have to back up by one to find the largest
6244 integer_type
= integer_types
[itk
];
6245 } while (itk
> 0 && integer_type
== NULL_TREE
);
6247 /* Figure out how much additional padding is required. */
6248 if (tree_int_cst_lt (TYPE_SIZE (integer_type
), DECL_SIZE (field
)))
6250 if (TREE_CODE (t
) == UNION_TYPE
)
6251 /* In a union, the padding field must have the full width
6252 of the bit-field; all fields start at offset zero. */
6253 padding
= DECL_SIZE (field
);
6255 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
6256 TYPE_SIZE (integer_type
));
6259 /* An unnamed bitfield does not normally affect the
6260 alignment of the containing class on a target where
6261 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6262 make any exceptions for unnamed bitfields when the
6263 bitfields are longer than their types. Therefore, we
6264 temporarily give the field a name. */
6265 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
6267 was_unnamed_p
= true;
6268 DECL_NAME (field
) = make_anon_name ();
6271 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
6272 DECL_ALIGN (field
) = TYPE_ALIGN (integer_type
);
6273 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
6274 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6275 empty_base_offsets
);
6277 DECL_NAME (field
) = NULL_TREE
;
6278 /* Now that layout has been performed, set the size of the
6279 field to the size of its declared type; the rest of the
6280 field is effectively invisible. */
6281 DECL_SIZE (field
) = TYPE_SIZE (type
);
6282 /* We must also reset the DECL_MODE of the field. */
6283 DECL_MODE (field
) = TYPE_MODE (type
);
6286 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
6287 empty_base_offsets
);
6289 /* Remember the location of any empty classes in FIELD. */
6290 record_subobject_offsets (TREE_TYPE (field
),
6291 byte_position(field
),
6293 /*is_data_member=*/true);
6295 /* If a bit-field does not immediately follow another bit-field,
6296 and yet it starts in the middle of a byte, we have failed to
6297 comply with the ABI. */
6299 && DECL_C_BIT_FIELD (field
)
6300 /* The TREE_NO_WARNING flag gets set by Objective-C when
6301 laying out an Objective-C class. The ObjC ABI differs
6302 from the C++ ABI, and so we do not want a warning
6304 && !TREE_NO_WARNING (field
)
6305 && !last_field_was_bitfield
6306 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
6307 DECL_FIELD_BIT_OFFSET (field
),
6308 bitsize_unit_node
)))
6309 warning (OPT_Wabi
, "offset of %q+D is not ABI-compliant and may "
6310 "change in a future version of GCC", field
);
6312 /* The middle end uses the type of expressions to determine the
6313 possible range of expression values. In order to optimize
6314 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6315 must be made aware of the width of "i", via its type.
6317 Because C++ does not have integer types of arbitrary width,
6318 we must (for the purposes of the front end) convert from the
6319 type assigned here to the declared type of the bitfield
6320 whenever a bitfield expression is used as an rvalue.
6321 Similarly, when assigning a value to a bitfield, the value
6322 must be converted to the type given the bitfield here. */
6323 if (DECL_C_BIT_FIELD (field
))
6325 unsigned HOST_WIDE_INT width
;
6326 tree ftype
= TREE_TYPE (field
);
6327 width
= tree_to_uhwi (DECL_SIZE (field
));
6328 if (width
!= TYPE_PRECISION (ftype
))
6331 = c_build_bitfield_integer_type (width
,
6332 TYPE_UNSIGNED (ftype
));
6334 = cp_build_qualified_type (TREE_TYPE (field
),
6335 cp_type_quals (ftype
));
6339 /* If we needed additional padding after this field, add it
6345 padding_field
= build_decl (input_location
,
6349 DECL_BIT_FIELD (padding_field
) = 1;
6350 DECL_SIZE (padding_field
) = padding
;
6351 DECL_CONTEXT (padding_field
) = t
;
6352 DECL_ARTIFICIAL (padding_field
) = 1;
6353 DECL_IGNORED_P (padding_field
) = 1;
6354 layout_nonempty_base_or_field (rli
, padding_field
,
6356 empty_base_offsets
);
6359 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6362 if (!integer_zerop (rli
->bitpos
))
6364 /* Make sure that we are on a byte boundary so that the size of
6365 the class without virtual bases will always be a round number
6367 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6368 normalize_rli (rli
);
6371 /* Delete all zero-width bit-fields from the list of fields. Now
6372 that the type is laid out they are no longer important. */
6373 remove_zero_width_bit_fields (t
);
6375 /* Create the version of T used for virtual bases. We do not use
6376 make_class_type for this version; this is an artificial type. For
6377 a POD type, we just reuse T. */
6378 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6380 base_t
= make_node (TREE_CODE (t
));
6382 /* Set the size and alignment for the new type. */
6385 /* If the ABI version is not at least two, and the last
6386 field was a bit-field, RLI may not be on a byte
6387 boundary. In particular, rli_size_unit_so_far might
6388 indicate the last complete byte, while rli_size_so_far
6389 indicates the total number of bits used. Therefore,
6390 rli_size_so_far, rather than rli_size_unit_so_far, is
6391 used to compute TYPE_SIZE_UNIT. */
6392 eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6393 TYPE_SIZE_UNIT (base_t
)
6394 = size_binop (MAX_EXPR
,
6396 size_binop (CEIL_DIV_EXPR
,
6397 rli_size_so_far (rli
),
6398 bitsize_int (BITS_PER_UNIT
))),
6401 = size_binop (MAX_EXPR
,
6402 rli_size_so_far (rli
),
6403 size_binop (MULT_EXPR
,
6404 convert (bitsizetype
, eoc
),
6405 bitsize_int (BITS_PER_UNIT
)));
6406 TYPE_ALIGN (base_t
) = rli
->record_align
;
6407 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6409 /* Copy the fields from T. */
6410 next_field
= &TYPE_FIELDS (base_t
);
6411 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6412 if (TREE_CODE (field
) == FIELD_DECL
)
6414 *next_field
= build_decl (input_location
,
6418 DECL_CONTEXT (*next_field
) = base_t
;
6419 DECL_FIELD_OFFSET (*next_field
) = DECL_FIELD_OFFSET (field
);
6420 DECL_FIELD_BIT_OFFSET (*next_field
)
6421 = DECL_FIELD_BIT_OFFSET (field
);
6422 DECL_SIZE (*next_field
) = DECL_SIZE (field
);
6423 DECL_MODE (*next_field
) = DECL_MODE (field
);
6424 next_field
= &DECL_CHAIN (*next_field
);
6427 /* Record the base version of the type. */
6428 CLASSTYPE_AS_BASE (t
) = base_t
;
6429 TYPE_CONTEXT (base_t
) = t
;
6432 CLASSTYPE_AS_BASE (t
) = t
;
6434 /* Every empty class contains an empty class. */
6435 if (CLASSTYPE_EMPTY_P (t
))
6436 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6438 /* Set the TYPE_DECL for this type to contain the right
6439 value for DECL_OFFSET, so that we can use it as part
6440 of a COMPONENT_REF for multiple inheritance. */
6441 layout_decl (TYPE_MAIN_DECL (t
), 0);
6443 /* Now fix up any virtual base class types that we left lying
6444 around. We must get these done before we try to lay out the
6445 virtual function table. As a side-effect, this will remove the
6446 base subobject fields. */
6447 layout_virtual_bases (rli
, empty_base_offsets
);
6449 /* Make sure that empty classes are reflected in RLI at this
6451 include_empty_classes(rli
);
6453 /* Make sure not to create any structures with zero size. */
6454 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6456 build_decl (input_location
,
6457 FIELD_DECL
, NULL_TREE
, char_type_node
));
6459 /* If this is a non-POD, declaring it packed makes a difference to how it
6460 can be used as a field; don't let finalize_record_size undo it. */
6461 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6462 rli
->packed_maybe_necessary
= true;
6464 /* Let the back end lay out the type. */
6465 finish_record_layout (rli
, /*free_p=*/true);
6467 if (TYPE_SIZE_UNIT (t
)
6468 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
6469 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
6470 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
6471 error ("type %qT is too large", t
);
6473 /* Warn about bases that can't be talked about due to ambiguity. */
6474 warn_about_ambiguous_bases (t
);
6476 /* Now that we're done with layout, give the base fields the real types. */
6477 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6478 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6479 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6482 splay_tree_delete (empty_base_offsets
);
6484 if (CLASSTYPE_EMPTY_P (t
)
6485 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6486 TYPE_SIZE_UNIT (t
)))
6487 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6490 /* Determine the "key method" for the class type indicated by TYPE,
6491 and set CLASSTYPE_KEY_METHOD accordingly. */
6494 determine_key_method (tree type
)
6498 if (TYPE_FOR_JAVA (type
)
6499 || processing_template_decl
6500 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6501 || CLASSTYPE_INTERFACE_KNOWN (type
))
6504 /* The key method is the first non-pure virtual function that is not
6505 inline at the point of class definition. On some targets the
6506 key function may not be inline; those targets should not call
6507 this function until the end of the translation unit. */
6508 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
6509 method
= DECL_CHAIN (method
))
6510 if (TREE_CODE (method
) == FUNCTION_DECL
6511 && DECL_VINDEX (method
) != NULL_TREE
6512 && ! DECL_DECLARED_INLINE_P (method
)
6513 && ! DECL_PURE_VIRTUAL_P (method
))
6515 CLASSTYPE_KEY_METHOD (type
) = method
;
6523 /* Allocate and return an instance of struct sorted_fields_type with
6526 static struct sorted_fields_type
*
6527 sorted_fields_type_new (int n
)
6529 struct sorted_fields_type
*sft
;
6530 sft
= (sorted_fields_type
*) ggc_internal_alloc (sizeof (sorted_fields_type
)
6531 + n
* sizeof (tree
));
6538 /* Perform processing required when the definition of T (a class type)
6542 finish_struct_1 (tree t
)
6545 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6546 tree virtuals
= NULL_TREE
;
6548 if (COMPLETE_TYPE_P (t
))
6550 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
6551 error ("redefinition of %q#T", t
);
6556 /* If this type was previously laid out as a forward reference,
6557 make sure we lay it out again. */
6558 TYPE_SIZE (t
) = NULL_TREE
;
6559 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
6561 /* Make assumptions about the class; we'll reset the flags if
6563 CLASSTYPE_EMPTY_P (t
) = 1;
6564 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
6565 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
6566 CLASSTYPE_LITERAL_P (t
) = true;
6568 /* Do end-of-class semantic processing: checking the validity of the
6569 bases and members and add implicitly generated methods. */
6570 check_bases_and_members (t
);
6572 /* Find the key method. */
6573 if (TYPE_CONTAINS_VPTR_P (t
))
6575 /* The Itanium C++ ABI permits the key method to be chosen when
6576 the class is defined -- even though the key method so
6577 selected may later turn out to be an inline function. On
6578 some systems (such as ARM Symbian OS) the key method cannot
6579 be determined until the end of the translation unit. On such
6580 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6581 will cause the class to be added to KEYED_CLASSES. Then, in
6582 finish_file we will determine the key method. */
6583 if (targetm
.cxx
.key_method_may_be_inline ())
6584 determine_key_method (t
);
6586 /* If a polymorphic class has no key method, we may emit the vtable
6587 in every translation unit where the class definition appears. If
6588 we're devirtualizing, we can look into the vtable even if we
6589 aren't emitting it. */
6590 if (CLASSTYPE_KEY_METHOD (t
) == NULL_TREE
)
6591 keyed_classes
= tree_cons (NULL_TREE
, t
, keyed_classes
);
6594 /* Layout the class itself. */
6595 layout_class_type (t
, &virtuals
);
6596 if (CLASSTYPE_AS_BASE (t
) != t
)
6597 /* We use the base type for trivial assignments, and hence it
6599 compute_record_mode (CLASSTYPE_AS_BASE (t
));
6601 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
6603 /* If necessary, create the primary vtable for this class. */
6604 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
6606 /* We must enter these virtuals into the table. */
6607 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6608 build_primary_vtable (NULL_TREE
, t
);
6609 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
6610 /* Here we know enough to change the type of our virtual
6611 function table, but we will wait until later this function. */
6612 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
6614 /* If we're warning about ABI tags, check the types of the new
6615 virtual functions. */
6617 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
6618 check_abi_tags (t
, TREE_VALUE (v
));
6621 if (TYPE_CONTAINS_VPTR_P (t
))
6626 if (BINFO_VTABLE (TYPE_BINFO (t
)))
6627 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
6628 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6629 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
6631 /* Add entries for virtual functions introduced by this class. */
6632 BINFO_VIRTUALS (TYPE_BINFO (t
))
6633 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
6635 /* Set DECL_VINDEX for all functions declared in this class. */
6636 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
6638 fn
= TREE_CHAIN (fn
),
6639 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
6640 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
6642 tree fndecl
= BV_FN (fn
);
6644 if (DECL_THUNK_P (fndecl
))
6645 /* A thunk. We should never be calling this entry directly
6646 from this vtable -- we'd use the entry for the non
6647 thunk base function. */
6648 DECL_VINDEX (fndecl
) = NULL_TREE
;
6649 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
6650 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
6654 finish_struct_bits (t
);
6655 set_method_tm_attributes (t
);
6657 /* Complete the rtl for any static member objects of the type we're
6659 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
6660 if (VAR_P (x
) && TREE_STATIC (x
)
6661 && TREE_TYPE (x
) != error_mark_node
6662 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
6663 DECL_MODE (x
) = TYPE_MODE (t
);
6665 /* Done with FIELDS...now decide whether to sort these for
6666 faster lookups later.
6668 We use a small number because most searches fail (succeeding
6669 ultimately as the search bores through the inheritance
6670 hierarchy), and we want this failure to occur quickly. */
6672 insert_into_classtype_sorted_fields (TYPE_FIELDS (t
), t
, 8);
6674 /* Complain if one of the field types requires lower visibility. */
6675 constrain_class_visibility (t
);
6677 /* Make the rtl for any new vtables we have created, and unmark
6678 the base types we marked. */
6681 /* Build the VTT for T. */
6684 /* This warning does not make sense for Java classes, since they
6685 cannot have destructors. */
6686 if (!TYPE_FOR_JAVA (t
) && warn_nonvdtor
6687 && TYPE_POLYMORPHIC_P (t
) && accessible_nvdtor_p (t
)
6688 && !CLASSTYPE_FINAL (t
))
6689 warning (OPT_Wnon_virtual_dtor
,
6690 "%q#T has virtual functions and accessible"
6691 " non-virtual destructor", t
);
6695 if (warn_overloaded_virtual
)
6698 /* Class layout, assignment of virtual table slots, etc., is now
6699 complete. Give the back end a chance to tweak the visibility of
6700 the class or perform any other required target modifications. */
6701 targetm
.cxx
.adjust_class_at_definition (t
);
6703 maybe_suppress_debug_info (t
);
6705 if (flag_vtable_verify
)
6706 vtv_save_class_info (t
);
6708 dump_class_hierarchy (t
);
6710 /* Finish debugging output for this type. */
6711 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
6713 if (TYPE_TRANSPARENT_AGGR (t
))
6715 tree field
= first_field (t
);
6716 if (field
== NULL_TREE
|| error_operand_p (field
))
6718 error ("type transparent %q#T does not have any fields", t
);
6719 TYPE_TRANSPARENT_AGGR (t
) = 0;
6721 else if (DECL_ARTIFICIAL (field
))
6723 if (DECL_FIELD_IS_BASE (field
))
6724 error ("type transparent class %qT has base classes", t
);
6727 gcc_checking_assert (DECL_VIRTUAL_P (field
));
6728 error ("type transparent class %qT has virtual functions", t
);
6730 TYPE_TRANSPARENT_AGGR (t
) = 0;
6732 else if (TYPE_MODE (t
) != DECL_MODE (field
))
6734 error ("type transparent %q#T cannot be made transparent because "
6735 "the type of the first field has a different ABI from the "
6736 "class overall", t
);
6737 TYPE_TRANSPARENT_AGGR (t
) = 0;
6742 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6743 equal to THRESHOLD or greater than THRESHOLD. */
6746 insert_into_classtype_sorted_fields (tree fields
, tree t
, int threshold
)
6748 int n_fields
= count_fields (fields
);
6749 if (n_fields
>= threshold
)
6751 struct sorted_fields_type
*field_vec
= sorted_fields_type_new (n_fields
);
6752 add_fields_to_record_type (fields
, field_vec
, 0);
6753 qsort (field_vec
->elts
, n_fields
, sizeof (tree
), field_decl_cmp
);
6754 CLASSTYPE_SORTED_FIELDS (t
) = field_vec
;
6758 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6761 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype
, tree t
)
6763 struct sorted_fields_type
*sorted_fields
= CLASSTYPE_SORTED_FIELDS (t
);
6768 = list_length (TYPE_VALUES (enumtype
)) + sorted_fields
->len
;
6769 struct sorted_fields_type
*field_vec
= sorted_fields_type_new (n_fields
);
6771 for (i
= 0; i
< sorted_fields
->len
; ++i
)
6772 field_vec
->elts
[i
] = sorted_fields
->elts
[i
];
6774 add_enum_fields_to_record_type (enumtype
, field_vec
,
6775 sorted_fields
->len
);
6776 qsort (field_vec
->elts
, n_fields
, sizeof (tree
), field_decl_cmp
);
6777 CLASSTYPE_SORTED_FIELDS (t
) = field_vec
;
6781 /* When T was built up, the member declarations were added in reverse
6782 order. Rearrange them to declaration order. */
6785 unreverse_member_declarations (tree t
)
6791 /* The following lists are all in reverse order. Put them in
6792 declaration order now. */
6793 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
6794 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
6796 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6797 reverse order, so we can't just use nreverse. */
6799 for (x
= TYPE_FIELDS (t
);
6800 x
&& TREE_CODE (x
) != TYPE_DECL
;
6803 next
= DECL_CHAIN (x
);
6804 DECL_CHAIN (x
) = prev
;
6809 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
6811 TYPE_FIELDS (t
) = prev
;
6816 finish_struct (tree t
, tree attributes
)
6818 location_t saved_loc
= input_location
;
6820 /* Now that we've got all the field declarations, reverse everything
6822 unreverse_member_declarations (t
);
6824 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
6826 /* Nadger the current location so that diagnostics point to the start of
6827 the struct, not the end. */
6828 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
6830 if (processing_template_decl
)
6834 finish_struct_methods (t
);
6835 TYPE_SIZE (t
) = bitsize_zero_node
;
6836 TYPE_SIZE_UNIT (t
) = size_zero_node
;
6838 /* We need to emit an error message if this type was used as a parameter
6839 and it is an abstract type, even if it is a template. We construct
6840 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6841 account and we call complete_vars with this type, which will check
6842 the PARM_DECLS. Note that while the type is being defined,
6843 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6844 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6845 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
6846 for (x
= TYPE_METHODS (t
); x
; x
= DECL_CHAIN (x
))
6847 if (DECL_PURE_VIRTUAL_P (x
))
6848 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
6850 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6851 an enclosing scope is a template class, so that this function be
6852 found by lookup_fnfields_1 when the using declaration is not
6853 instantiated yet. */
6854 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
6855 if (TREE_CODE (x
) == USING_DECL
)
6857 tree fn
= strip_using_decl (x
);
6858 if (is_overloaded_fn (fn
))
6859 for (; fn
; fn
= OVL_NEXT (fn
))
6860 add_method (t
, OVL_CURRENT (fn
), x
);
6863 /* Remember current #pragma pack value. */
6864 TYPE_PRECISION (t
) = maximum_field_alignment
;
6866 /* Fix up any variants we've already built. */
6867 for (x
= TYPE_NEXT_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
6869 TYPE_SIZE (x
) = TYPE_SIZE (t
);
6870 TYPE_SIZE_UNIT (x
) = TYPE_SIZE_UNIT (t
);
6871 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
6872 TYPE_METHODS (x
) = TYPE_METHODS (t
);
6876 finish_struct_1 (t
);
6878 if (is_std_init_list (t
))
6880 /* People keep complaining that the compiler crashes on an invalid
6881 definition of initializer_list, so I guess we should explicitly
6882 reject it. What the compiler internals care about is that it's a
6883 template and has a pointer field followed by an integer field. */
6885 if (processing_template_decl
)
6887 tree f
= next_initializable_field (TYPE_FIELDS (t
));
6888 if (f
&& TREE_CODE (TREE_TYPE (f
)) == POINTER_TYPE
)
6890 f
= next_initializable_field (DECL_CHAIN (f
));
6891 if (f
&& same_type_p (TREE_TYPE (f
), size_type_node
))
6896 fatal_error (input_location
,
6897 "definition of std::initializer_list does not match "
6898 "#include <initializer_list>");
6901 input_location
= saved_loc
;
6903 TYPE_BEING_DEFINED (t
) = 0;
6905 if (current_class_type
)
6908 error ("trying to finish struct, but kicked out due to previous parse errors");
6910 if (processing_template_decl
&& at_function_scope_p ()
6911 /* Lambdas are defined by the LAMBDA_EXPR. */
6912 && !LAMBDA_TYPE_P (t
))
6913 add_stmt (build_min (TAG_DEFN
, t
));
6918 /* Hash table to avoid endless recursion when handling references. */
6919 static hash_table
<nofree_ptr_hash
<tree_node
> > *fixed_type_or_null_ref_ht
;
6921 /* Return the dynamic type of INSTANCE, if known.
6922 Used to determine whether the virtual function table is needed
6925 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6926 of our knowledge of its type. *NONNULL should be initialized
6927 before this function is called. */
6930 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
6932 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6934 switch (TREE_CODE (instance
))
6937 if (POINTER_TYPE_P (TREE_TYPE (instance
)))
6940 return RECUR (TREE_OPERAND (instance
, 0));
6943 /* This is a call to a constructor, hence it's never zero. */
6944 if (TREE_HAS_CONSTRUCTOR (instance
))
6948 return TREE_TYPE (instance
);
6953 /* This is a call to a constructor, hence it's never zero. */
6954 if (TREE_HAS_CONSTRUCTOR (instance
))
6958 return TREE_TYPE (instance
);
6960 return RECUR (TREE_OPERAND (instance
, 0));
6962 case POINTER_PLUS_EXPR
:
6965 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
6966 return RECUR (TREE_OPERAND (instance
, 0));
6967 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
6968 /* Propagate nonnull. */
6969 return RECUR (TREE_OPERAND (instance
, 0));
6974 return RECUR (TREE_OPERAND (instance
, 0));
6977 instance
= TREE_OPERAND (instance
, 0);
6980 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6981 with a real object -- given &p->f, p can still be null. */
6982 tree t
= get_base_address (instance
);
6983 /* ??? Probably should check DECL_WEAK here. */
6984 if (t
&& DECL_P (t
))
6987 return RECUR (instance
);
6990 /* If this component is really a base class reference, then the field
6991 itself isn't definitive. */
6992 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
6993 return RECUR (TREE_OPERAND (instance
, 0));
6994 return RECUR (TREE_OPERAND (instance
, 1));
6998 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
6999 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
7003 return TREE_TYPE (TREE_TYPE (instance
));
7005 /* fall through... */
7009 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
7013 return TREE_TYPE (instance
);
7015 else if (instance
== current_class_ptr
)
7020 /* if we're in a ctor or dtor, we know our type. If
7021 current_class_ptr is set but we aren't in a function, we're in
7022 an NSDMI (and therefore a constructor). */
7023 if (current_scope () != current_function_decl
7024 || (DECL_LANG_SPECIFIC (current_function_decl
)
7025 && (DECL_CONSTRUCTOR_P (current_function_decl
)
7026 || DECL_DESTRUCTOR_P (current_function_decl
))))
7030 return TREE_TYPE (TREE_TYPE (instance
));
7033 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
7035 /* We only need one hash table because it is always left empty. */
7036 if (!fixed_type_or_null_ref_ht
)
7037 fixed_type_or_null_ref_ht
7038 = new hash_table
<nofree_ptr_hash
<tree_node
> > (37);
7040 /* Reference variables should be references to objects. */
7044 /* Enter the INSTANCE in a table to prevent recursion; a
7045 variable's initializer may refer to the variable
7047 if (VAR_P (instance
)
7048 && DECL_INITIAL (instance
)
7049 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
7050 && !fixed_type_or_null_ref_ht
->find (instance
))
7055 slot
= fixed_type_or_null_ref_ht
->find_slot (instance
, INSERT
);
7057 type
= RECUR (DECL_INITIAL (instance
));
7058 fixed_type_or_null_ref_ht
->remove_elt (instance
);
7071 /* Return nonzero if the dynamic type of INSTANCE is known, and
7072 equivalent to the static type. We also handle the case where
7073 INSTANCE is really a pointer. Return negative if this is a
7074 ctor/dtor. There the dynamic type is known, but this might not be
7075 the most derived base of the original object, and hence virtual
7076 bases may not be laid out according to this type.
7078 Used to determine whether the virtual function table is needed
7081 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7082 of our knowledge of its type. *NONNULL should be initialized
7083 before this function is called. */
7086 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
7088 tree t
= TREE_TYPE (instance
);
7092 /* processing_template_decl can be false in a template if we're in
7093 instantiate_non_dependent_expr, but we still want to suppress
7095 if (in_template_function ())
7097 /* In a template we only care about the type of the result. */
7103 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
7104 if (fixed
== NULL_TREE
)
7106 if (POINTER_TYPE_P (t
))
7108 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
7110 return cdtorp
? -1 : 1;
7115 init_class_processing (void)
7117 current_class_depth
= 0;
7118 current_class_stack_size
= 10;
7120 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
7121 vec_alloc (local_classes
, 8);
7122 sizeof_biggest_empty_class
= size_zero_node
;
7124 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
7125 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
7126 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
7129 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7132 restore_class_cache (void)
7136 /* We are re-entering the same class we just left, so we don't
7137 have to search the whole inheritance matrix to find all the
7138 decls to bind again. Instead, we install the cached
7139 class_shadowed list and walk through it binding names. */
7140 push_binding_level (previous_class_level
);
7141 class_binding_level
= previous_class_level
;
7142 /* Restore IDENTIFIER_TYPE_VALUE. */
7143 for (type
= class_binding_level
->type_shadowed
;
7145 type
= TREE_CHAIN (type
))
7146 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
7149 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7150 appropriate for TYPE.
7152 So that we may avoid calls to lookup_name, we cache the _TYPE
7153 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7155 For multiple inheritance, we perform a two-pass depth-first search
7156 of the type lattice. */
7159 pushclass (tree type
)
7161 class_stack_node_t csn
;
7163 type
= TYPE_MAIN_VARIANT (type
);
7165 /* Make sure there is enough room for the new entry on the stack. */
7166 if (current_class_depth
+ 1 >= current_class_stack_size
)
7168 current_class_stack_size
*= 2;
7170 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
7171 current_class_stack_size
);
7174 /* Insert a new entry on the class stack. */
7175 csn
= current_class_stack
+ current_class_depth
;
7176 csn
->name
= current_class_name
;
7177 csn
->type
= current_class_type
;
7178 csn
->access
= current_access_specifier
;
7179 csn
->names_used
= 0;
7181 current_class_depth
++;
7183 /* Now set up the new type. */
7184 current_class_name
= TYPE_NAME (type
);
7185 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
7186 current_class_name
= DECL_NAME (current_class_name
);
7187 current_class_type
= type
;
7189 /* By default, things in classes are private, while things in
7190 structures or unions are public. */
7191 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
7192 ? access_private_node
7193 : access_public_node
);
7195 if (previous_class_level
7196 && type
!= previous_class_level
->this_entity
7197 && current_class_depth
== 1)
7199 /* Forcibly remove any old class remnants. */
7200 invalidate_class_lookup_cache ();
7203 if (!previous_class_level
7204 || type
!= previous_class_level
->this_entity
7205 || current_class_depth
> 1)
7208 restore_class_cache ();
7211 /* When we exit a toplevel class scope, we save its binding level so
7212 that we can restore it quickly. Here, we've entered some other
7213 class, so we must invalidate our cache. */
7216 invalidate_class_lookup_cache (void)
7218 previous_class_level
= NULL
;
7221 /* Get out of the current class scope. If we were in a class scope
7222 previously, that is the one popped to. */
7229 current_class_depth
--;
7230 current_class_name
= current_class_stack
[current_class_depth
].name
;
7231 current_class_type
= current_class_stack
[current_class_depth
].type
;
7232 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
7233 if (current_class_stack
[current_class_depth
].names_used
)
7234 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
7237 /* Mark the top of the class stack as hidden. */
7240 push_class_stack (void)
7242 if (current_class_depth
)
7243 ++current_class_stack
[current_class_depth
- 1].hidden
;
7246 /* Mark the top of the class stack as un-hidden. */
7249 pop_class_stack (void)
7251 if (current_class_depth
)
7252 --current_class_stack
[current_class_depth
- 1].hidden
;
7255 /* Returns 1 if the class type currently being defined is either T or
7256 a nested type of T. */
7259 currently_open_class (tree t
)
7263 if (!CLASS_TYPE_P (t
))
7266 t
= TYPE_MAIN_VARIANT (t
);
7268 /* We start looking from 1 because entry 0 is from global scope,
7270 for (i
= current_class_depth
; i
> 0; --i
)
7273 if (i
== current_class_depth
)
7274 c
= current_class_type
;
7277 if (current_class_stack
[i
].hidden
)
7279 c
= current_class_stack
[i
].type
;
7283 if (same_type_p (c
, t
))
7289 /* If either current_class_type or one of its enclosing classes are derived
7290 from T, return the appropriate type. Used to determine how we found
7291 something via unqualified lookup. */
7294 currently_open_derived_class (tree t
)
7298 /* The bases of a dependent type are unknown. */
7299 if (dependent_type_p (t
))
7302 if (!current_class_type
)
7305 if (DERIVED_FROM_P (t
, current_class_type
))
7306 return current_class_type
;
7308 for (i
= current_class_depth
- 1; i
> 0; --i
)
7310 if (current_class_stack
[i
].hidden
)
7312 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
7313 return current_class_stack
[i
].type
;
7319 /* Return the outermost enclosing class type that is still open, or
7323 outermost_open_class (void)
7325 if (!current_class_type
)
7328 if (TYPE_BEING_DEFINED (current_class_type
))
7329 r
= current_class_type
;
7330 for (int i
= current_class_depth
- 1; i
> 0; --i
)
7332 if (current_class_stack
[i
].hidden
)
7334 tree t
= current_class_stack
[i
].type
;
7335 if (!TYPE_BEING_DEFINED (t
))
7342 /* Returns the innermost class type which is not a lambda closure type. */
7345 current_nonlambda_class_type (void)
7349 /* We start looking from 1 because entry 0 is from global scope,
7351 for (i
= current_class_depth
; i
> 0; --i
)
7354 if (i
== current_class_depth
)
7355 c
= current_class_type
;
7358 if (current_class_stack
[i
].hidden
)
7360 c
= current_class_stack
[i
].type
;
7364 if (!LAMBDA_TYPE_P (c
))
7370 /* When entering a class scope, all enclosing class scopes' names with
7371 static meaning (static variables, static functions, types and
7372 enumerators) have to be visible. This recursive function calls
7373 pushclass for all enclosing class contexts until global or a local
7374 scope is reached. TYPE is the enclosed class. */
7377 push_nested_class (tree type
)
7379 /* A namespace might be passed in error cases, like A::B:C. */
7380 if (type
== NULL_TREE
7381 || !CLASS_TYPE_P (type
))
7384 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
7389 /* Undoes a push_nested_class call. */
7392 pop_nested_class (void)
7394 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
7397 if (context
&& CLASS_TYPE_P (context
))
7398 pop_nested_class ();
7401 /* Returns the number of extern "LANG" blocks we are nested within. */
7404 current_lang_depth (void)
7406 return vec_safe_length (current_lang_base
);
7409 /* Set global variables CURRENT_LANG_NAME to appropriate value
7410 so that behavior of name-mangling machinery is correct. */
7413 push_lang_context (tree name
)
7415 vec_safe_push (current_lang_base
, current_lang_name
);
7417 if (name
== lang_name_cplusplus
)
7419 current_lang_name
= name
;
7421 else if (name
== lang_name_java
)
7423 current_lang_name
= name
;
7424 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7425 (See record_builtin_java_type in decl.c.) However, that causes
7426 incorrect debug entries if these types are actually used.
7427 So we re-enable debug output after extern "Java". */
7428 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node
)) = 0;
7429 DECL_IGNORED_P (TYPE_NAME (java_short_type_node
)) = 0;
7430 DECL_IGNORED_P (TYPE_NAME (java_int_type_node
)) = 0;
7431 DECL_IGNORED_P (TYPE_NAME (java_long_type_node
)) = 0;
7432 DECL_IGNORED_P (TYPE_NAME (java_float_type_node
)) = 0;
7433 DECL_IGNORED_P (TYPE_NAME (java_double_type_node
)) = 0;
7434 DECL_IGNORED_P (TYPE_NAME (java_char_type_node
)) = 0;
7435 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node
)) = 0;
7437 else if (name
== lang_name_c
)
7439 current_lang_name
= name
;
7442 error ("language string %<\"%E\"%> not recognized", name
);
7445 /* Get out of the current language scope. */
7448 pop_lang_context (void)
7450 current_lang_name
= current_lang_base
->pop ();
7453 /* Type instantiation routines. */
7455 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7456 matches the TARGET_TYPE. If there is no satisfactory match, return
7457 error_mark_node, and issue an error & warning messages under
7458 control of FLAGS. Permit pointers to member function if FLAGS
7459 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7460 a template-id, and EXPLICIT_TARGS are the explicitly provided
7463 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7464 is the base path used to reference those member functions. If
7465 the address is resolved to a member function, access checks will be
7466 performed and errors issued if appropriate. */
7469 resolve_address_of_overloaded_function (tree target_type
,
7471 tsubst_flags_t complain
,
7473 tree explicit_targs
,
7476 /* Here's what the standard says:
7480 If the name is a function template, template argument deduction
7481 is done, and if the argument deduction succeeds, the deduced
7482 arguments are used to generate a single template function, which
7483 is added to the set of overloaded functions considered.
7485 Non-member functions and static member functions match targets of
7486 type "pointer-to-function" or "reference-to-function." Nonstatic
7487 member functions match targets of type "pointer-to-member
7488 function;" the function type of the pointer to member is used to
7489 select the member function from the set of overloaded member
7490 functions. If a nonstatic member function is selected, the
7491 reference to the overloaded function name is required to have the
7492 form of a pointer to member as described in 5.3.1.
7494 If more than one function is selected, any template functions in
7495 the set are eliminated if the set also contains a non-template
7496 function, and any given template function is eliminated if the
7497 set contains a second template function that is more specialized
7498 than the first according to the partial ordering rules 14.5.5.2.
7499 After such eliminations, if any, there shall remain exactly one
7500 selected function. */
7503 /* We store the matches in a TREE_LIST rooted here. The functions
7504 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7505 interoperability with most_specialized_instantiation. */
7506 tree matches
= NULL_TREE
;
7508 tree target_fn_type
;
7510 /* By the time we get here, we should be seeing only real
7511 pointer-to-member types, not the internal POINTER_TYPE to
7512 METHOD_TYPE representation. */
7513 gcc_assert (!TYPE_PTR_P (target_type
)
7514 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
7516 gcc_assert (is_overloaded_fn (overload
));
7518 /* Check that the TARGET_TYPE is reasonable. */
7519 if (TYPE_PTRFN_P (target_type
)
7520 || TYPE_REFFN_P (target_type
))
7522 else if (TYPE_PTRMEMFUNC_P (target_type
))
7523 /* This is OK, too. */
7525 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
7526 /* This is OK, too. This comes from a conversion to reference
7528 target_type
= build_reference_type (target_type
);
7531 if (complain
& tf_error
)
7532 error ("cannot resolve overloaded function %qD based on"
7533 " conversion to type %qT",
7534 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
7535 return error_mark_node
;
7538 /* Non-member functions and static member functions match targets of type
7539 "pointer-to-function" or "reference-to-function." Nonstatic member
7540 functions match targets of type "pointer-to-member-function;" the
7541 function type of the pointer to member is used to select the member
7542 function from the set of overloaded member functions.
7544 So figure out the FUNCTION_TYPE that we want to match against. */
7545 target_fn_type
= static_fn_type (target_type
);
7547 /* If we can find a non-template function that matches, we can just
7548 use it. There's no point in generating template instantiations
7549 if we're just going to throw them out anyhow. But, of course, we
7550 can only do this when we don't *need* a template function. */
7555 for (fns
= overload
; fns
; fns
= OVL_NEXT (fns
))
7557 tree fn
= OVL_CURRENT (fns
);
7559 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
7560 /* We're not looking for templates just yet. */
7563 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
7565 /* We're looking for a non-static member, and this isn't
7566 one, or vice versa. */
7569 /* Ignore functions which haven't been explicitly
7571 if (DECL_ANTICIPATED (fn
))
7574 /* See if there's a match. */
7575 if (same_type_p (target_fn_type
, static_fn_type (fn
)))
7576 matches
= tree_cons (fn
, NULL_TREE
, matches
);
7580 /* Now, if we've already got a match (or matches), there's no need
7581 to proceed to the template functions. But, if we don't have a
7582 match we need to look at them, too. */
7585 tree target_arg_types
;
7586 tree target_ret_type
;
7589 unsigned int nargs
, ia
;
7592 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
7593 target_ret_type
= TREE_TYPE (target_fn_type
);
7595 nargs
= list_length (target_arg_types
);
7596 args
= XALLOCAVEC (tree
, nargs
);
7597 for (arg
= target_arg_types
, ia
= 0;
7598 arg
!= NULL_TREE
&& arg
!= void_list_node
;
7599 arg
= TREE_CHAIN (arg
), ++ia
)
7600 args
[ia
] = TREE_VALUE (arg
);
7603 for (fns
= overload
; fns
; fns
= OVL_NEXT (fns
))
7605 tree fn
= OVL_CURRENT (fns
);
7609 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7610 /* We're only looking for templates. */
7613 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
7615 /* We're not looking for a non-static member, and this is
7616 one, or vice versa. */
7619 tree ret
= target_ret_type
;
7621 /* If the template has a deduced return type, don't expose it to
7622 template argument deduction. */
7623 if (undeduced_auto_decl (fn
))
7626 /* Try to do argument deduction. */
7627 targs
= make_tree_vec (DECL_NTPARMS (fn
));
7628 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
7630 DEDUCE_EXACT
, LOOKUP_NORMAL
,
7632 if (instantiation
== error_mark_node
)
7633 /* Instantiation failed. */
7636 /* And now force instantiation to do return type deduction. */
7637 if (undeduced_auto_decl (instantiation
))
7640 instantiate_decl (instantiation
, /*defer*/false, /*class*/false);
7643 require_deduced_type (instantiation
);
7646 /* See if there's a match. */
7647 if (same_type_p (target_fn_type
, static_fn_type (instantiation
)))
7648 matches
= tree_cons (instantiation
, fn
, matches
);
7651 /* Now, remove all but the most specialized of the matches. */
7654 tree match
= most_specialized_instantiation (matches
);
7656 if (match
!= error_mark_node
)
7657 matches
= tree_cons (TREE_PURPOSE (match
),
7663 /* Now we should have exactly one function in MATCHES. */
7664 if (matches
== NULL_TREE
)
7666 /* There were *no* matches. */
7667 if (complain
& tf_error
)
7669 error ("no matches converting function %qD to type %q#T",
7670 DECL_NAME (OVL_CURRENT (overload
)),
7673 print_candidates (overload
);
7675 return error_mark_node
;
7677 else if (TREE_CHAIN (matches
))
7679 /* There were too many matches. First check if they're all
7680 the same function. */
7681 tree match
= NULL_TREE
;
7683 fn
= TREE_PURPOSE (matches
);
7685 /* For multi-versioned functions, more than one match is just fine and
7686 decls_match will return false as they are different. */
7687 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
7688 if (!decls_match (fn
, TREE_PURPOSE (match
))
7689 && !targetm
.target_option
.function_versions
7690 (fn
, TREE_PURPOSE (match
)))
7695 if (complain
& tf_error
)
7697 error ("converting overloaded function %qD to type %q#T is ambiguous",
7698 DECL_NAME (OVL_FUNCTION (overload
)),
7701 /* Since print_candidates expects the functions in the
7702 TREE_VALUE slot, we flip them here. */
7703 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
7704 TREE_VALUE (match
) = TREE_PURPOSE (match
);
7706 print_candidates (matches
);
7709 return error_mark_node
;
7713 /* Good, exactly one match. Now, convert it to the correct type. */
7714 fn
= TREE_PURPOSE (matches
);
7716 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
7717 && !(complain
& tf_ptrmem_ok
) && !flag_ms_extensions
)
7719 static int explained
;
7721 if (!(complain
& tf_error
))
7722 return error_mark_node
;
7724 permerror (input_location
, "assuming pointer to member %qD", fn
);
7727 inform (input_location
, "(a pointer to member can only be formed with %<&%E%>)", fn
);
7732 /* If a pointer to a function that is multi-versioned is requested, the
7733 pointer to the dispatcher function is returned instead. This works
7734 well because indirectly calling the function will dispatch the right
7735 function version at run-time. */
7736 if (DECL_FUNCTION_VERSIONED (fn
))
7738 fn
= get_function_version_dispatcher (fn
);
7740 return error_mark_node
;
7741 /* Mark all the versions corresponding to the dispatcher as used. */
7742 if (!(complain
& tf_conv
))
7743 mark_versions_used (fn
);
7746 /* If we're doing overload resolution purely for the purpose of
7747 determining conversion sequences, we should not consider the
7748 function used. If this conversion sequence is selected, the
7749 function will be marked as used at this point. */
7750 if (!(complain
& tf_conv
))
7752 /* Make =delete work with SFINAE. */
7753 if (DECL_DELETED_FN (fn
) && !(complain
& tf_error
))
7754 return error_mark_node
;
7755 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7756 return error_mark_node
;
7759 /* We could not check access to member functions when this
7760 expression was originally created since we did not know at that
7761 time to which function the expression referred. */
7762 if (DECL_FUNCTION_MEMBER_P (fn
))
7764 gcc_assert (access_path
);
7765 perform_or_defer_access_check (access_path
, fn
, fn
, complain
);
7768 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
7769 return cp_build_addr_expr (fn
, complain
);
7772 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7773 will mark the function as addressed, but here we must do it
7775 cxx_mark_addressable (fn
);
7781 /* This function will instantiate the type of the expression given in
7782 RHS to match the type of LHSTYPE. If errors exist, then return
7783 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7784 we complain on errors. If we are not complaining, never modify rhs,
7785 as overload resolution wants to try many possible instantiations, in
7786 the hope that at least one will work.
7788 For non-recursive calls, LHSTYPE should be a function, pointer to
7789 function, or a pointer to member function. */
7792 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t complain
)
7794 tsubst_flags_t complain_in
= complain
;
7795 tree access_path
= NULL_TREE
;
7797 complain
&= ~tf_ptrmem_ok
;
7799 if (lhstype
== unknown_type_node
)
7801 if (complain
& tf_error
)
7802 error ("not enough type information");
7803 return error_mark_node
;
7806 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
7808 tree fntype
= non_reference (lhstype
);
7809 if (same_type_p (fntype
, TREE_TYPE (rhs
)))
7811 if (flag_ms_extensions
7812 && TYPE_PTRMEMFUNC_P (fntype
)
7813 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
7814 /* Microsoft allows `A::f' to be resolved to a
7815 pointer-to-member. */
7819 if (complain
& tf_error
)
7820 error ("cannot convert %qE from type %qT to type %qT",
7821 rhs
, TREE_TYPE (rhs
), fntype
);
7822 return error_mark_node
;
7826 if (BASELINK_P (rhs
))
7828 access_path
= BASELINK_ACCESS_BINFO (rhs
);
7829 rhs
= BASELINK_FUNCTIONS (rhs
);
7832 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7833 deduce any type information. */
7834 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
7836 if (complain
& tf_error
)
7837 error ("not enough type information");
7838 return error_mark_node
;
7841 /* There only a few kinds of expressions that may have a type
7842 dependent on overload resolution. */
7843 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
7844 || TREE_CODE (rhs
) == COMPONENT_REF
7845 || is_overloaded_fn (rhs
)
7846 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
7848 /* This should really only be used when attempting to distinguish
7849 what sort of a pointer to function we have. For now, any
7850 arithmetic operation which is not supported on pointers
7851 is rejected as an error. */
7853 switch (TREE_CODE (rhs
))
7857 tree member
= TREE_OPERAND (rhs
, 1);
7859 member
= instantiate_type (lhstype
, member
, complain
);
7860 if (member
!= error_mark_node
7861 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
7862 /* Do not lose object's side effects. */
7863 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
7864 TREE_OPERAND (rhs
, 0), member
);
7869 rhs
= TREE_OPERAND (rhs
, 1);
7870 if (BASELINK_P (rhs
))
7871 return instantiate_type (lhstype
, rhs
, complain_in
);
7873 /* This can happen if we are forming a pointer-to-member for a
7875 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
7879 case TEMPLATE_ID_EXPR
:
7881 tree fns
= TREE_OPERAND (rhs
, 0);
7882 tree args
= TREE_OPERAND (rhs
, 1);
7885 resolve_address_of_overloaded_function (lhstype
, fns
, complain_in
,
7886 /*template_only=*/true,
7893 resolve_address_of_overloaded_function (lhstype
, rhs
, complain_in
,
7894 /*template_only=*/false,
7895 /*explicit_targs=*/NULL_TREE
,
7900 if (PTRMEM_OK_P (rhs
))
7901 complain
|= tf_ptrmem_ok
;
7903 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
7907 return error_mark_node
;
7912 return error_mark_node
;
7915 /* Return the name of the virtual function pointer field
7916 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7917 this may have to look back through base types to find the
7918 ultimate field name. (For single inheritance, these could
7919 all be the same name. Who knows for multiple inheritance). */
7922 get_vfield_name (tree type
)
7924 tree binfo
, base_binfo
;
7927 for (binfo
= TYPE_BINFO (type
);
7928 BINFO_N_BASE_BINFOS (binfo
);
7931 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
7933 if (BINFO_VIRTUAL_P (base_binfo
)
7934 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
7938 type
= BINFO_TYPE (binfo
);
7939 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
7940 + TYPE_NAME_LENGTH (type
) + 2);
7941 sprintf (buf
, VFIELD_NAME_FORMAT
,
7942 IDENTIFIER_POINTER (constructor_name (type
)));
7943 return get_identifier (buf
);
7947 print_class_statistics (void)
7949 if (! GATHER_STATISTICS
)
7952 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
7953 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
7956 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
7957 n_vtables
, n_vtable_searches
);
7958 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
7959 n_vtable_entries
, n_vtable_elems
);
7963 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7964 according to [class]:
7965 The class-name is also inserted
7966 into the scope of the class itself. For purposes of access checking,
7967 the inserted class name is treated as if it were a public member name. */
7970 build_self_reference (void)
7972 tree name
= constructor_name (current_class_type
);
7973 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
7976 DECL_NONLOCAL (value
) = 1;
7977 DECL_CONTEXT (value
) = current_class_type
;
7978 DECL_ARTIFICIAL (value
) = 1;
7979 SET_DECL_SELF_REFERENCE_P (value
);
7980 set_underlying_type (value
);
7982 if (processing_template_decl
)
7983 value
= push_template_decl (value
);
7985 saved_cas
= current_access_specifier
;
7986 current_access_specifier
= access_public_node
;
7987 finish_member_declaration (value
);
7988 current_access_specifier
= saved_cas
;
7991 /* Returns 1 if TYPE contains only padding bytes. */
7994 is_empty_class (tree type
)
7996 if (type
== error_mark_node
)
7999 if (! CLASS_TYPE_P (type
))
8002 return CLASSTYPE_EMPTY_P (type
);
8005 /* Returns true if TYPE contains no actual data, just various
8006 possible combinations of empty classes and possibly a vptr. */
8009 is_really_empty_class (tree type
)
8011 if (CLASS_TYPE_P (type
))
8018 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8019 out, but we'd like to be able to check this before then. */
8020 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
8023 for (binfo
= TYPE_BINFO (type
), i
= 0;
8024 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8025 if (!is_really_empty_class (BINFO_TYPE (base_binfo
)))
8027 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8028 if (TREE_CODE (field
) == FIELD_DECL
8029 && !DECL_ARTIFICIAL (field
)
8030 && !is_really_empty_class (TREE_TYPE (field
)))
8034 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8035 return is_really_empty_class (TREE_TYPE (type
));
8039 /* Note that NAME was looked up while the current class was being
8040 defined and that the result of that lookup was DECL. */
8043 maybe_note_name_used_in_class (tree name
, tree decl
)
8045 splay_tree names_used
;
8047 /* If we're not defining a class, there's nothing to do. */
8048 if (!(innermost_scope_kind() == sk_class
8049 && TYPE_BEING_DEFINED (current_class_type
)
8050 && !LAMBDA_TYPE_P (current_class_type
)))
8053 /* If there's already a binding for this NAME, then we don't have
8054 anything to worry about. */
8055 if (lookup_member (current_class_type
, name
,
8056 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
8059 if (!current_class_stack
[current_class_depth
- 1].names_used
)
8060 current_class_stack
[current_class_depth
- 1].names_used
8061 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
8062 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
8064 splay_tree_insert (names_used
,
8065 (splay_tree_key
) name
,
8066 (splay_tree_value
) decl
);
8069 /* Note that NAME was declared (as DECL) in the current class. Check
8070 to see that the declaration is valid. */
8073 note_name_declared_in_class (tree name
, tree decl
)
8075 splay_tree names_used
;
8078 /* Look to see if we ever used this name. */
8080 = current_class_stack
[current_class_depth
- 1].names_used
;
8083 /* The C language allows members to be declared with a type of the same
8084 name, and the C++ standard says this diagnostic is not required. So
8085 allow it in extern "C" blocks unless predantic is specified.
8086 Allow it in all cases if -ms-extensions is specified. */
8087 if ((!pedantic
&& current_lang_name
== lang_name_c
)
8088 || flag_ms_extensions
)
8090 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
8093 /* [basic.scope.class]
8095 A name N used in a class S shall refer to the same declaration
8096 in its context and when re-evaluated in the completed scope of
8098 permerror (input_location
, "declaration of %q#D", decl
);
8099 permerror (input_location
, "changes meaning of %qD from %q+#D",
8100 DECL_NAME (OVL_CURRENT (decl
)), (tree
) n
->value
);
8104 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8105 Secondary vtables are merged with primary vtables; this function
8106 will return the VAR_DECL for the primary vtable. */
8109 get_vtbl_decl_for_binfo (tree binfo
)
8113 decl
= BINFO_VTABLE (binfo
);
8114 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
8116 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
8117 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
8120 gcc_assert (VAR_P (decl
));
8125 /* Returns the binfo for the primary base of BINFO. If the resulting
8126 BINFO is a virtual base, and it is inherited elsewhere in the
8127 hierarchy, then the returned binfo might not be the primary base of
8128 BINFO in the complete object. Check BINFO_PRIMARY_P or
8129 BINFO_LOST_PRIMARY_P to be sure. */
8132 get_primary_binfo (tree binfo
)
8136 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
8140 return copied_binfo (primary_base
, binfo
);
8143 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8146 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
8149 fprintf (stream
, "%*s", indent
, "");
8153 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8154 INDENT should be zero when called from the top level; it is
8155 incremented recursively. IGO indicates the next expected BINFO in
8156 inheritance graph ordering. */
8159 dump_class_hierarchy_r (FILE *stream
,
8169 indented
= maybe_indent_hierarchy (stream
, indent
, 0);
8170 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
8171 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
8172 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8175 fprintf (stream
, "alternative-path\n");
8178 igo
= TREE_CHAIN (binfo
);
8180 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
8181 tree_to_shwi (BINFO_OFFSET (binfo
)));
8182 if (is_empty_class (BINFO_TYPE (binfo
)))
8183 fprintf (stream
, " empty");
8184 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
8185 fprintf (stream
, " nearly-empty");
8186 if (BINFO_VIRTUAL_P (binfo
))
8187 fprintf (stream
, " virtual");
8188 fprintf (stream
, "\n");
8191 if (BINFO_PRIMARY_P (binfo
))
8193 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8194 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
8195 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
8196 TFF_PLAIN_IDENTIFIER
),
8197 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
8199 if (BINFO_LOST_PRIMARY_P (binfo
))
8201 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8202 fprintf (stream
, " lost-primary");
8205 fprintf (stream
, "\n");
8207 if (!(flags
& TDF_SLIM
))
8211 if (BINFO_SUBVTT_INDEX (binfo
))
8213 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8214 fprintf (stream
, " subvttidx=%s",
8215 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
8216 TFF_PLAIN_IDENTIFIER
));
8218 if (BINFO_VPTR_INDEX (binfo
))
8220 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8221 fprintf (stream
, " vptridx=%s",
8222 expr_as_string (BINFO_VPTR_INDEX (binfo
),
8223 TFF_PLAIN_IDENTIFIER
));
8225 if (BINFO_VPTR_FIELD (binfo
))
8227 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8228 fprintf (stream
, " vbaseoffset=%s",
8229 expr_as_string (BINFO_VPTR_FIELD (binfo
),
8230 TFF_PLAIN_IDENTIFIER
));
8232 if (BINFO_VTABLE (binfo
))
8234 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
8235 fprintf (stream
, " vptr=%s",
8236 expr_as_string (BINFO_VTABLE (binfo
),
8237 TFF_PLAIN_IDENTIFIER
));
8241 fprintf (stream
, "\n");
8244 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
8245 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
8250 /* Dump the BINFO hierarchy for T. */
8253 dump_class_hierarchy_1 (FILE *stream
, int flags
, tree t
)
8255 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8256 fprintf (stream
, " size=%lu align=%lu\n",
8257 (unsigned long)(tree_to_shwi (TYPE_SIZE (t
)) / BITS_PER_UNIT
),
8258 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
8259 fprintf (stream
, " base size=%lu base align=%lu\n",
8260 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t
)))
8262 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t
))
8264 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
8265 fprintf (stream
, "\n");
8268 /* Debug interface to hierarchy dumping. */
8271 debug_class (tree t
)
8273 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
8277 dump_class_hierarchy (tree t
)
8280 FILE *stream
= get_dump_info (TDI_class
, &flags
);
8284 dump_class_hierarchy_1 (stream
, flags
, t
);
8289 dump_array (FILE * stream
, tree decl
)
8292 unsigned HOST_WIDE_INT ix
;
8294 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
8296 elt
= (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))))
8298 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
8299 fprintf (stream
, " %s entries",
8300 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
8301 TFF_PLAIN_IDENTIFIER
));
8302 fprintf (stream
, "\n");
8304 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
8306 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
8307 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
8311 dump_vtable (tree t
, tree binfo
, tree vtable
)
8314 FILE *stream
= get_dump_info (TDI_class
, &flags
);
8319 if (!(flags
& TDF_SLIM
))
8321 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
8323 fprintf (stream
, "%s for %s",
8324 ctor_vtbl_p
? "Construction vtable" : "Vtable",
8325 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
8328 if (!BINFO_VIRTUAL_P (binfo
))
8329 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
8330 (HOST_WIDE_INT
) (uintptr_t) binfo
);
8331 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8333 fprintf (stream
, "\n");
8334 dump_array (stream
, vtable
);
8335 fprintf (stream
, "\n");
8340 dump_vtt (tree t
, tree vtt
)
8343 FILE *stream
= get_dump_info (TDI_class
, &flags
);
8348 if (!(flags
& TDF_SLIM
))
8350 fprintf (stream
, "VTT for %s\n",
8351 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8352 dump_array (stream
, vtt
);
8353 fprintf (stream
, "\n");
8357 /* Dump a function or thunk and its thunkees. */
8360 dump_thunk (FILE *stream
, int indent
, tree thunk
)
8362 static const char spaces
[] = " ";
8363 tree name
= DECL_NAME (thunk
);
8366 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
8368 !DECL_THUNK_P (thunk
) ? "function"
8369 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
8370 name
? IDENTIFIER_POINTER (name
) : "<unset>");
8371 if (DECL_THUNK_P (thunk
))
8373 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
8374 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
8376 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
8377 if (!virtual_adjust
)
8379 else if (DECL_THIS_THUNK_P (thunk
))
8380 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
8381 tree_to_shwi (virtual_adjust
));
8383 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
8384 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust
)),
8385 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
8386 if (THUNK_ALIAS (thunk
))
8387 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
8389 fprintf (stream
, "\n");
8390 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
8391 dump_thunk (stream
, indent
+ 2, thunks
);
8394 /* Dump the thunks for FN. */
8397 debug_thunks (tree fn
)
8399 dump_thunk (stderr
, 0, fn
);
8402 /* Virtual function table initialization. */
8404 /* Create all the necessary vtables for T and its base classes. */
8407 finish_vtbls (tree t
)
8410 vec
<constructor_elt
, va_gc
> *v
= NULL
;
8411 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
8413 /* We lay out the primary and secondary vtables in one contiguous
8414 vtable. The primary vtable is first, followed by the non-virtual
8415 secondary vtables in inheritance graph order. */
8416 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
8419 /* Then come the virtual bases, also in inheritance graph order. */
8420 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
8422 if (!BINFO_VIRTUAL_P (vbase
))
8424 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
8427 if (BINFO_VTABLE (TYPE_BINFO (t
)))
8428 initialize_vtable (TYPE_BINFO (t
), v
);
8431 /* Initialize the vtable for BINFO with the INITS. */
8434 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
8438 layout_vtable_decl (binfo
, vec_safe_length (inits
));
8439 decl
= get_vtbl_decl_for_binfo (binfo
);
8440 initialize_artificial_var (decl
, inits
);
8441 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
8444 /* Build the VTT (virtual table table) for T.
8445 A class requires a VTT if it has virtual bases.
8448 1 - primary virtual pointer for complete object T
8449 2 - secondary VTTs for each direct non-virtual base of T which requires a
8451 3 - secondary virtual pointers for each direct or indirect base of T which
8452 has virtual bases or is reachable via a virtual path from T.
8453 4 - secondary VTTs for each direct or indirect virtual base of T.
8455 Secondary VTTs look like complete object VTTs without part 4. */
8463 vec
<constructor_elt
, va_gc
> *inits
;
8465 /* Build up the initializers for the VTT. */
8467 index
= size_zero_node
;
8468 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
8470 /* If we didn't need a VTT, we're done. */
8474 /* Figure out the type of the VTT. */
8475 type
= build_array_of_n_type (const_ptr_type_node
,
8478 /* Now, build the VTT object itself. */
8479 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
8480 initialize_artificial_var (vtt
, inits
);
8481 /* Add the VTT to the vtables list. */
8482 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
8483 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
8488 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8489 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8490 and CHAIN the vtable pointer for this binfo after construction is
8491 complete. VALUE can also be another BINFO, in which case we recurse. */
8494 binfo_ctor_vtable (tree binfo
)
8500 vt
= BINFO_VTABLE (binfo
);
8501 if (TREE_CODE (vt
) == TREE_LIST
)
8502 vt
= TREE_VALUE (vt
);
8503 if (TREE_CODE (vt
) == TREE_BINFO
)
8512 /* Data for secondary VTT initialization. */
8513 typedef struct secondary_vptr_vtt_init_data_s
8515 /* Is this the primary VTT? */
8518 /* Current index into the VTT. */
8521 /* Vector of initializers built up. */
8522 vec
<constructor_elt
, va_gc
> *inits
;
8524 /* The type being constructed by this secondary VTT. */
8525 tree type_being_constructed
;
8526 } secondary_vptr_vtt_init_data
;
8528 /* Recursively build the VTT-initializer for BINFO (which is in the
8529 hierarchy dominated by T). INITS points to the end of the initializer
8530 list to date. INDEX is the VTT index where the next element will be
8531 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8532 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8533 for virtual bases of T. When it is not so, we build the constructor
8534 vtables for the BINFO-in-T variant. */
8537 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
8543 secondary_vptr_vtt_init_data data
;
8544 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
8546 /* We only need VTTs for subobjects with virtual bases. */
8547 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
8550 /* We need to use a construction vtable if this is not the primary
8554 build_ctor_vtbl_group (binfo
, t
);
8556 /* Record the offset in the VTT where this sub-VTT can be found. */
8557 BINFO_SUBVTT_INDEX (binfo
) = *index
;
8560 /* Add the address of the primary vtable for the complete object. */
8561 init
= binfo_ctor_vtable (binfo
);
8562 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
8565 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8566 BINFO_VPTR_INDEX (binfo
) = *index
;
8568 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
8570 /* Recursively add the secondary VTTs for non-virtual bases. */
8571 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
8572 if (!BINFO_VIRTUAL_P (b
))
8573 build_vtt_inits (b
, t
, inits
, index
);
8575 /* Add secondary virtual pointers for all subobjects of BINFO with
8576 either virtual bases or reachable along a virtual path, except
8577 subobjects that are non-virtual primary bases. */
8578 data
.top_level_p
= top_level_p
;
8579 data
.index
= *index
;
8580 data
.inits
= *inits
;
8581 data
.type_being_constructed
= BINFO_TYPE (binfo
);
8583 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
8585 *index
= data
.index
;
8587 /* data.inits might have grown as we added secondary virtual pointers.
8588 Make sure our caller knows about the new vector. */
8589 *inits
= data
.inits
;
8592 /* Add the secondary VTTs for virtual bases in inheritance graph
8594 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
8596 if (!BINFO_VIRTUAL_P (b
))
8599 build_vtt_inits (b
, t
, inits
, index
);
8602 /* Remove the ctor vtables we created. */
8603 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
8606 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8607 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8610 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
8612 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
8614 /* We don't care about bases that don't have vtables. */
8615 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
8616 return dfs_skip_bases
;
8618 /* We're only interested in proper subobjects of the type being
8620 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
8623 /* We're only interested in bases with virtual bases or reachable
8624 via a virtual path from the type being constructed. */
8625 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
8626 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
8627 return dfs_skip_bases
;
8629 /* We're not interested in non-virtual primary bases. */
8630 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
8633 /* Record the index where this secondary vptr can be found. */
8634 if (data
->top_level_p
)
8636 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8637 BINFO_VPTR_INDEX (binfo
) = data
->index
;
8639 if (BINFO_VIRTUAL_P (binfo
))
8641 /* It's a primary virtual base, and this is not a
8642 construction vtable. Find the base this is primary of in
8643 the inheritance graph, and use that base's vtable
8645 while (BINFO_PRIMARY_P (binfo
))
8646 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
8650 /* Add the initializer for the secondary vptr itself. */
8651 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
8653 /* Advance the vtt index. */
8654 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
8655 TYPE_SIZE_UNIT (ptr_type_node
));
8660 /* Called from build_vtt_inits via dfs_walk. After building
8661 constructor vtables and generating the sub-vtt from them, we need
8662 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8663 binfo of the base whose sub vtt was generated. */
8666 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
8668 tree vtable
= BINFO_VTABLE (binfo
);
8670 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
8671 /* If this class has no vtable, none of its bases do. */
8672 return dfs_skip_bases
;
8675 /* This might be a primary base, so have no vtable in this
8679 /* If we scribbled the construction vtable vptr into BINFO, clear it
8681 if (TREE_CODE (vtable
) == TREE_LIST
8682 && (TREE_PURPOSE (vtable
) == (tree
) data
))
8683 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
8688 /* Build the construction vtable group for BINFO which is in the
8689 hierarchy dominated by T. */
8692 build_ctor_vtbl_group (tree binfo
, tree t
)
8698 vec
<constructor_elt
, va_gc
> *v
;
8700 /* See if we've already created this construction vtable group. */
8701 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
8702 if (IDENTIFIER_GLOBAL_VALUE (id
))
8705 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
8706 /* Build a version of VTBL (with the wrong type) for use in
8707 constructing the addresses of secondary vtables in the
8708 construction vtable group. */
8709 vtbl
= build_vtable (t
, id
, ptr_type_node
);
8710 DECL_CONSTRUCTION_VTABLE_P (vtbl
) = 1;
8711 /* Don't export construction vtables from shared libraries. Even on
8712 targets that don't support hidden visibility, this tells
8713 can_refer_decl_in_current_unit_p not to assume that it's safe to
8714 access from a different compilation unit (bz 54314). */
8715 DECL_VISIBILITY (vtbl
) = VISIBILITY_HIDDEN
;
8716 DECL_VISIBILITY_SPECIFIED (vtbl
) = true;
8719 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
8720 binfo
, vtbl
, t
, &v
);
8722 /* Add the vtables for each of our virtual bases using the vbase in T
8724 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
8726 vbase
= TREE_CHAIN (vbase
))
8730 if (!BINFO_VIRTUAL_P (vbase
))
8732 b
= copied_binfo (vbase
, binfo
);
8734 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
8737 /* Figure out the type of the construction vtable. */
8738 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
8740 TREE_TYPE (vtbl
) = type
;
8741 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
8742 layout_decl (vtbl
, 0);
8744 /* Initialize the construction vtable. */
8745 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
8746 initialize_artificial_var (vtbl
, v
);
8747 dump_vtable (t
, binfo
, vtbl
);
8750 /* Add the vtbl initializers for BINFO (and its bases other than
8751 non-virtual primaries) to the list of INITS. BINFO is in the
8752 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8753 the constructor the vtbl inits should be accumulated for. (If this
8754 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8755 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8756 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8757 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8758 but are not necessarily the same in terms of layout. */
8761 accumulate_vtbl_inits (tree binfo
,
8766 vec
<constructor_elt
, va_gc
> **inits
)
8770 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
8772 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
8774 /* If it doesn't have a vptr, we don't do anything. */
8775 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
8778 /* If we're building a construction vtable, we're not interested in
8779 subobjects that don't require construction vtables. */
8781 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
8782 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
8785 /* Build the initializers for the BINFO-in-T vtable. */
8786 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
8788 /* Walk the BINFO and its bases. We walk in preorder so that as we
8789 initialize each vtable we can figure out at what offset the
8790 secondary vtable lies from the primary vtable. We can't use
8791 dfs_walk here because we need to iterate through bases of BINFO
8792 and RTTI_BINFO simultaneously. */
8793 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8795 /* Skip virtual bases. */
8796 if (BINFO_VIRTUAL_P (base_binfo
))
8798 accumulate_vtbl_inits (base_binfo
,
8799 BINFO_BASE_BINFO (orig_binfo
, i
),
8800 rtti_binfo
, vtbl
, t
,
8805 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8806 BINFO vtable to L. */
8809 dfs_accumulate_vtbl_inits (tree binfo
,
8814 vec
<constructor_elt
, va_gc
> **l
)
8816 tree vtbl
= NULL_TREE
;
8817 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
8821 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
8823 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8824 primary virtual base. If it is not the same primary in
8825 the hierarchy of T, we'll need to generate a ctor vtable
8826 for it, to place at its location in T. If it is the same
8827 primary, we still need a VTT entry for the vtable, but it
8828 should point to the ctor vtable for the base it is a
8829 primary for within the sub-hierarchy of RTTI_BINFO.
8831 There are three possible cases:
8833 1) We are in the same place.
8834 2) We are a primary base within a lost primary virtual base of
8836 3) We are primary to something not a base of RTTI_BINFO. */
8839 tree last
= NULL_TREE
;
8841 /* First, look through the bases we are primary to for RTTI_BINFO
8842 or a virtual base. */
8844 while (BINFO_PRIMARY_P (b
))
8846 b
= BINFO_INHERITANCE_CHAIN (b
);
8848 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
8851 /* If we run out of primary links, keep looking down our
8852 inheritance chain; we might be an indirect primary. */
8853 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
8854 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
8858 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8859 base B and it is a base of RTTI_BINFO, this is case 2. In
8860 either case, we share our vtable with LAST, i.e. the
8861 derived-most base within B of which we are a primary. */
8863 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
8864 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8865 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8866 binfo_ctor_vtable after everything's been set up. */
8869 /* Otherwise, this is case 3 and we get our own. */
8871 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
8874 n_inits
= vec_safe_length (*l
);
8881 /* Add the initializer for this vtable. */
8882 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
8883 &non_fn_entries
, l
);
8885 /* Figure out the position to which the VPTR should point. */
8886 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
8887 index
= size_binop (MULT_EXPR
,
8888 TYPE_SIZE_UNIT (vtable_entry_type
),
8889 size_int (non_fn_entries
+ n_inits
));
8890 vtbl
= fold_build_pointer_plus (vtbl
, index
);
8894 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8895 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8896 straighten this out. */
8897 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
8898 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
8899 /* Throw away any unneeded intializers. */
8900 (*l
)->truncate (n_inits
);
8902 /* For an ordinary vtable, set BINFO_VTABLE. */
8903 BINFO_VTABLE (binfo
) = vtbl
;
8906 static GTY(()) tree abort_fndecl_addr
;
8908 /* Construct the initializer for BINFO's virtual function table. BINFO
8909 is part of the hierarchy dominated by T. If we're building a
8910 construction vtable, the ORIG_BINFO is the binfo we should use to
8911 find the actual function pointers to put in the vtable - but they
8912 can be overridden on the path to most-derived in the graph that
8913 ORIG_BINFO belongs. Otherwise,
8914 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8915 BINFO that should be indicated by the RTTI information in the
8916 vtable; it will be a base class of T, rather than T itself, if we
8917 are building a construction vtable.
8919 The value returned is a TREE_LIST suitable for wrapping in a
8920 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8921 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8922 number of non-function entries in the vtable.
8924 It might seem that this function should never be called with a
8925 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8926 base is always subsumed by a derived class vtable. However, when
8927 we are building construction vtables, we do build vtables for
8928 primary bases; we need these while the primary base is being
8932 build_vtbl_initializer (tree binfo
,
8936 int* non_fn_entries_p
,
8937 vec
<constructor_elt
, va_gc
> **inits
)
8943 vec
<tree
, va_gc
> *vbases
;
8946 /* Initialize VID. */
8947 memset (&vid
, 0, sizeof (vid
));
8950 vid
.rtti_binfo
= rtti_binfo
;
8951 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
8952 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
8953 vid
.generate_vcall_entries
= true;
8954 /* The first vbase or vcall offset is at index -3 in the vtable. */
8955 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
8957 /* Add entries to the vtable for RTTI. */
8958 build_rtti_vtbl_entries (binfo
, &vid
);
8960 /* Create an array for keeping track of the functions we've
8961 processed. When we see multiple functions with the same
8962 signature, we share the vcall offsets. */
8963 vec_alloc (vid
.fns
, 32);
8964 /* Add the vcall and vbase offset entries. */
8965 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
8967 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8968 build_vbase_offset_vtbl_entries. */
8969 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
8970 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
8971 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
8973 /* If the target requires padding between data entries, add that now. */
8974 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
8976 int n_entries
= vec_safe_length (vid
.inits
);
8978 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
);
8980 /* Move data entries into their new positions and add padding
8981 after the new positions. Iterate backwards so we don't
8982 overwrite entries that we would need to process later. */
8983 for (ix
= n_entries
- 1;
8984 vid
.inits
->iterate (ix
, &e
);
8988 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
8989 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
8991 (*vid
.inits
)[new_position
] = *e
;
8993 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
8995 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
8996 f
->index
= NULL_TREE
;
8997 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
9003 if (non_fn_entries_p
)
9004 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
9006 /* The initializers for virtual functions were built up in reverse
9007 order. Straighten them out and add them to the running list in one
9009 jx
= vec_safe_length (*inits
);
9010 vec_safe_grow (*inits
, jx
+ vid
.inits
->length ());
9012 for (ix
= vid
.inits
->length () - 1;
9013 vid
.inits
->iterate (ix
, &e
);
9017 /* Go through all the ordinary virtual functions, building up
9019 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
9023 tree fn
, fn_original
;
9024 tree init
= NULL_TREE
;
9028 if (DECL_THUNK_P (fn
))
9030 if (!DECL_NAME (fn
))
9032 if (THUNK_ALIAS (fn
))
9034 fn
= THUNK_ALIAS (fn
);
9037 fn_original
= THUNK_TARGET (fn
);
9040 /* If the only definition of this function signature along our
9041 primary base chain is from a lost primary, this vtable slot will
9042 never be used, so just zero it out. This is important to avoid
9043 requiring extra thunks which cannot be generated with the function.
9045 We first check this in update_vtable_entry_for_fn, so we handle
9046 restored primary bases properly; we also need to do it here so we
9047 zero out unused slots in ctor vtables, rather than filling them
9048 with erroneous values (though harmless, apart from relocation
9050 if (BV_LOST_PRIMARY (v
))
9051 init
= size_zero_node
;
9055 /* Pull the offset for `this', and the function to call, out of
9057 delta
= BV_DELTA (v
);
9058 vcall_index
= BV_VCALL_INDEX (v
);
9060 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
9061 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9063 /* You can't call an abstract virtual function; it's abstract.
9064 So, we replace these functions with __pure_virtual. */
9065 if (DECL_PURE_VIRTUAL_P (fn_original
))
9068 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9070 if (abort_fndecl_addr
== NULL
)
9072 = fold_convert (vfunc_ptr_type_node
,
9073 build_fold_addr_expr (fn
));
9074 init
= abort_fndecl_addr
;
9077 /* Likewise for deleted virtuals. */
9078 else if (DECL_DELETED_FN (fn_original
))
9080 fn
= get_identifier ("__cxa_deleted_virtual");
9081 if (!get_global_value_if_present (fn
, &fn
))
9082 fn
= push_library_fn (fn
, (build_function_type_list
9083 (void_type_node
, NULL_TREE
)),
9084 NULL_TREE
, ECF_NORETURN
);
9085 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9086 init
= fold_convert (vfunc_ptr_type_node
,
9087 build_fold_addr_expr (fn
));
9091 if (!integer_zerop (delta
) || vcall_index
)
9093 fn
= make_thunk (fn
, /*this_adjusting=*/1, delta
, vcall_index
);
9094 if (!DECL_NAME (fn
))
9097 /* Take the address of the function, considering it to be of an
9098 appropriate generic type. */
9099 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
9100 init
= fold_convert (vfunc_ptr_type_node
,
9101 build_fold_addr_expr (fn
));
9102 /* Don't refer to a virtual destructor from a constructor
9103 vtable or a vtable for an abstract class, since destroying
9104 an object under construction is undefined behavior and we
9105 don't want it to be considered a candidate for speculative
9106 devirtualization. But do create the thunk for ABI
9108 if (DECL_DESTRUCTOR_P (fn_original
)
9109 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original
))
9110 || orig_binfo
!= binfo
))
9111 init
= size_zero_node
;
9115 /* And add it to the chain of initializers. */
9116 if (TARGET_VTABLE_USES_DESCRIPTORS
)
9119 if (init
== size_zero_node
)
9120 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9121 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9123 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
9125 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
9126 fn
, build_int_cst (NULL_TREE
, i
));
9127 TREE_CONSTANT (fdesc
) = 1;
9129 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, fdesc
);
9133 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
9137 /* Adds to vid->inits the initializers for the vbase and vcall
9138 offsets in BINFO, which is in the hierarchy dominated by T. */
9141 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9145 /* If this is a derived class, we must first create entries
9146 corresponding to the primary base class. */
9147 b
= get_primary_binfo (binfo
);
9149 build_vcall_and_vbase_vtbl_entries (b
, vid
);
9151 /* Add the vbase entries for this base. */
9152 build_vbase_offset_vtbl_entries (binfo
, vid
);
9153 /* Add the vcall entries for this base. */
9154 build_vcall_offset_vtbl_entries (binfo
, vid
);
9157 /* Returns the initializers for the vbase offset entries in the vtable
9158 for BINFO (which is part of the class hierarchy dominated by T), in
9159 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9160 where the next vbase offset will go. */
9163 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9167 tree non_primary_binfo
;
9169 /* If there are no virtual baseclasses, then there is nothing to
9171 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
9176 /* We might be a primary base class. Go up the inheritance hierarchy
9177 until we find the most derived class of which we are a primary base:
9178 it is the offset of that which we need to use. */
9179 non_primary_binfo
= binfo
;
9180 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
9184 /* If we have reached a virtual base, then it must be a primary
9185 base (possibly multi-level) of vid->binfo, or we wouldn't
9186 have called build_vcall_and_vbase_vtbl_entries for it. But it
9187 might be a lost primary, so just skip down to vid->binfo. */
9188 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9190 non_primary_binfo
= vid
->binfo
;
9194 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9195 if (get_primary_binfo (b
) != non_primary_binfo
)
9197 non_primary_binfo
= b
;
9200 /* Go through the virtual bases, adding the offsets. */
9201 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
9203 vbase
= TREE_CHAIN (vbase
))
9208 if (!BINFO_VIRTUAL_P (vbase
))
9211 /* Find the instance of this virtual base in the complete
9213 b
= copied_binfo (vbase
, binfo
);
9215 /* If we've already got an offset for this virtual base, we
9216 don't need another one. */
9217 if (BINFO_VTABLE_PATH_MARKED (b
))
9219 BINFO_VTABLE_PATH_MARKED (b
) = 1;
9221 /* Figure out where we can find this vbase offset. */
9222 delta
= size_binop (MULT_EXPR
,
9225 TYPE_SIZE_UNIT (vtable_entry_type
)));
9226 if (vid
->primary_vtbl_p
)
9227 BINFO_VPTR_FIELD (b
) = delta
;
9229 if (binfo
!= TYPE_BINFO (t
))
9230 /* The vbase offset had better be the same. */
9231 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
9233 /* The next vbase will come at a more negative offset. */
9234 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9235 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9237 /* The initializer is the delta from BINFO to this virtual base.
9238 The vbase offsets go in reverse inheritance-graph order, and
9239 we are walking in inheritance graph order so these end up in
9241 delta
= size_diffop_loc (input_location
,
9242 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
9244 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
9245 fold_build1_loc (input_location
, NOP_EXPR
,
9246 vtable_entry_type
, delta
));
9250 /* Adds the initializers for the vcall offset entries in the vtable
9251 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9255 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9257 /* We only need these entries if this base is a virtual base. We
9258 compute the indices -- but do not add to the vtable -- when
9259 building the main vtable for a class. */
9260 if (binfo
== TYPE_BINFO (vid
->derived
)
9261 || (BINFO_VIRTUAL_P (binfo
)
9262 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9263 correspond to VID->DERIVED), we are building a primary
9264 construction virtual table. Since this is a primary
9265 virtual table, we do not need the vcall offsets for
9267 && binfo
!= vid
->rtti_binfo
))
9269 /* We need a vcall offset for each of the virtual functions in this
9270 vtable. For example:
9272 class A { virtual void f (); };
9273 class B1 : virtual public A { virtual void f (); };
9274 class B2 : virtual public A { virtual void f (); };
9275 class C: public B1, public B2 { virtual void f (); };
9277 A C object has a primary base of B1, which has a primary base of A. A
9278 C also has a secondary base of B2, which no longer has a primary base
9279 of A. So the B2-in-C construction vtable needs a secondary vtable for
9280 A, which will adjust the A* to a B2* to call f. We have no way of
9281 knowing what (or even whether) this offset will be when we define B2,
9282 so we store this "vcall offset" in the A sub-vtable and look it up in
9283 a "virtual thunk" for B2::f.
9285 We need entries for all the functions in our primary vtable and
9286 in our non-virtual bases' secondary vtables. */
9288 /* If we are just computing the vcall indices -- but do not need
9289 the actual entries -- not that. */
9290 if (!BINFO_VIRTUAL_P (binfo
))
9291 vid
->generate_vcall_entries
= false;
9292 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9293 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
9297 /* Build vcall offsets, starting with those for BINFO. */
9300 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
9306 /* Don't walk into virtual bases -- except, of course, for the
9307 virtual base for which we are building vcall offsets. Any
9308 primary virtual base will have already had its offsets generated
9309 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9310 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
9313 /* If BINFO has a primary base, process it first. */
9314 primary_binfo
= get_primary_binfo (binfo
);
9316 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
9318 /* Add BINFO itself to the list. */
9319 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
9321 /* Scan the non-primary bases of BINFO. */
9322 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
9323 if (base_binfo
!= primary_binfo
)
9324 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
9327 /* Called from build_vcall_offset_vtbl_entries_r. */
9330 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
9332 /* Make entries for the rest of the virtuals. */
9335 /* The ABI requires that the methods be processed in declaration
9337 for (orig_fn
= TYPE_METHODS (BINFO_TYPE (binfo
));
9339 orig_fn
= DECL_CHAIN (orig_fn
))
9340 if (TREE_CODE (orig_fn
) == FUNCTION_DECL
&& DECL_VINDEX (orig_fn
))
9341 add_vcall_offset (orig_fn
, binfo
, vid
);
9344 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9347 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
9353 /* If there is already an entry for a function with the same
9354 signature as FN, then we do not need a second vcall offset.
9355 Check the list of functions already present in the derived
9357 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
9359 if (same_signature_p (derived_entry
, orig_fn
)
9360 /* We only use one vcall offset for virtual destructors,
9361 even though there are two virtual table entries. */
9362 || (DECL_DESTRUCTOR_P (derived_entry
)
9363 && DECL_DESTRUCTOR_P (orig_fn
)))
9367 /* If we are building these vcall offsets as part of building
9368 the vtable for the most derived class, remember the vcall
9370 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
9372 tree_pair_s elt
= {orig_fn
, vid
->index
};
9373 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
9376 /* The next vcall offset will be found at a more negative
9378 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9379 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9381 /* Keep track of this function. */
9382 vec_safe_push (vid
->fns
, orig_fn
);
9384 if (vid
->generate_vcall_entries
)
9389 /* Find the overriding function. */
9390 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
9391 if (fn
== error_mark_node
)
9392 vcall_offset
= build_zero_cst (vtable_entry_type
);
9395 base
= TREE_VALUE (fn
);
9397 /* The vbase we're working on is a primary base of
9398 vid->binfo. But it might be a lost primary, so its
9399 BINFO_OFFSET might be wrong, so we just use the
9400 BINFO_OFFSET from vid->binfo. */
9401 vcall_offset
= size_diffop_loc (input_location
,
9402 BINFO_OFFSET (base
),
9403 BINFO_OFFSET (vid
->binfo
));
9404 vcall_offset
= fold_build1_loc (input_location
,
9405 NOP_EXPR
, vtable_entry_type
,
9408 /* Add the initializer to the vtable. */
9409 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
9413 /* Return vtbl initializers for the RTTI entries corresponding to the
9414 BINFO's vtable. The RTTI entries should indicate the object given
9415 by VID->rtti_binfo. */
9418 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9426 t
= BINFO_TYPE (vid
->rtti_binfo
);
9428 /* To find the complete object, we will first convert to our most
9429 primary base, and then add the offset in the vtbl to that value. */
9431 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
9432 && !BINFO_LOST_PRIMARY_P (b
))
9436 primary_base
= get_primary_binfo (b
);
9437 gcc_assert (BINFO_PRIMARY_P (primary_base
)
9438 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
9441 offset
= size_diffop_loc (input_location
,
9442 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
9444 /* The second entry is the address of the typeinfo object. */
9446 decl
= build_address (get_tinfo_decl (t
));
9448 decl
= integer_zero_node
;
9450 /* Convert the declaration to a type that can be stored in the
9452 init
= build_nop (vfunc_ptr_type_node
, decl
);
9453 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9455 /* Add the offset-to-top entry. It comes earlier in the vtable than
9456 the typeinfo entry. Convert the offset to look like a
9457 function pointer, so that we can put it in the vtable. */
9458 init
= build_nop (vfunc_ptr_type_node
, offset
);
9459 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9462 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9466 uniquely_derived_from_p (tree parent
, tree type
)
9468 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
9469 return base
&& base
!= error_mark_node
;
9472 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9475 publicly_uniquely_derived_p (tree parent
, tree type
)
9477 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
9479 return base
&& base
!= error_mark_node
;
9482 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9483 class between them, if any. */
9486 common_enclosing_class (tree ctx1
, tree ctx2
)
9488 if (!TYPE_P (ctx1
) || !TYPE_P (ctx2
))
9490 gcc_assert (ctx1
== TYPE_MAIN_VARIANT (ctx1
)
9491 && ctx2
== TYPE_MAIN_VARIANT (ctx2
));
9494 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9495 TYPE_MARKED_P (t
) = true;
9496 tree found
= NULL_TREE
;
9497 for (tree t
= ctx2
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9498 if (TYPE_MARKED_P (t
))
9503 for (tree t
= ctx1
; TYPE_P (t
); t
= TYPE_CONTEXT (t
))
9504 TYPE_MARKED_P (t
) = false;
9508 #include "gt-cp-class.h"