1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011,
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
29 #include "coretypes.h"
39 #include "splay-tree.h"
40 #include "pointer-set.h"
41 #include "hash-table.h"
43 /* The number of nested classes being processed. If we are not in the
44 scope of any class, this is zero. */
46 int current_class_depth
;
48 /* In order to deal with nested classes, we keep a stack of classes.
49 The topmost entry is the innermost class, and is the entry at index
50 CURRENT_CLASS_DEPTH */
52 typedef struct class_stack_node
{
53 /* The name of the class. */
56 /* The _TYPE node for the class. */
59 /* The access specifier pending for new declarations in the scope of
63 /* If were defining TYPE, the names used in this class. */
64 splay_tree names_used
;
66 /* Nonzero if this class is no longer open, because of a call to
69 }* class_stack_node_t
;
71 typedef struct vtbl_init_data_s
73 /* The base for which we're building initializers. */
75 /* The type of the most-derived type. */
77 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
78 unless ctor_vtbl_p is true. */
80 /* The negative-index vtable initializers built up so far. These
81 are in order from least negative index to most negative index. */
82 vec
<constructor_elt
, va_gc
> *inits
;
83 /* The binfo for the virtual base for which we're building
84 vcall offset initializers. */
86 /* The functions in vbase for which we have already provided vcall
88 vec
<tree
, va_gc
> *fns
;
89 /* The vtable index of the next vcall or vbase offset. */
91 /* Nonzero if we are building the initializer for the primary
94 /* Nonzero if we are building the initializer for a construction
97 /* True when adding vcall offset entries to the vtable. False when
98 merely computing the indices. */
99 bool generate_vcall_entries
;
102 /* The type of a function passed to walk_subobject_offsets. */
103 typedef int (*subobject_offset_fn
) (tree
, tree
, splay_tree
);
105 /* The stack itself. This is a dynamically resized array. The
106 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
107 static int current_class_stack_size
;
108 static class_stack_node_t current_class_stack
;
110 /* The size of the largest empty class seen in this translation unit. */
111 static GTY (()) tree sizeof_biggest_empty_class
;
113 /* An array of all local classes present in this translation unit, in
114 declaration order. */
115 vec
<tree
, va_gc
> *local_classes
;
117 static tree
get_vfield_name (tree
);
118 static void finish_struct_anon (tree
);
119 static tree
get_vtable_name (tree
);
120 static tree
get_basefndecls (tree
, tree
);
121 static int build_primary_vtable (tree
, tree
);
122 static int build_secondary_vtable (tree
);
123 static void finish_vtbls (tree
);
124 static void modify_vtable_entry (tree
, tree
, tree
, tree
, tree
*);
125 static void finish_struct_bits (tree
);
126 static int alter_access (tree
, tree
, tree
);
127 static void handle_using_decl (tree
, tree
);
128 static tree
dfs_modify_vtables (tree
, void *);
129 static tree
modify_all_vtables (tree
, tree
);
130 static void determine_primary_bases (tree
);
131 static void finish_struct_methods (tree
);
132 static void maybe_warn_about_overly_private_class (tree
);
133 static int method_name_cmp (const void *, const void *);
134 static int resort_method_name_cmp (const void *, const void *);
135 static void add_implicitly_declared_members (tree
, tree
*, int, int);
136 static tree
fixed_type_or_null (tree
, int *, int *);
137 static tree
build_simple_base_path (tree expr
, tree binfo
);
138 static tree
build_vtbl_ref_1 (tree
, tree
);
139 static void build_vtbl_initializer (tree
, tree
, tree
, tree
, int *,
140 vec
<constructor_elt
, va_gc
> **);
141 static int count_fields (tree
);
142 static int add_fields_to_record_type (tree
, struct sorted_fields_type
*, int);
143 static void insert_into_classtype_sorted_fields (tree
, tree
, int);
144 static bool check_bitfield_decl (tree
);
145 static void check_field_decl (tree
, tree
, int *, int *, int *);
146 static void check_field_decls (tree
, tree
*, int *, int *);
147 static tree
*build_base_field (record_layout_info
, tree
, splay_tree
, tree
*);
148 static void build_base_fields (record_layout_info
, splay_tree
, tree
*);
149 static void check_methods (tree
);
150 static void remove_zero_width_bit_fields (tree
);
151 static void check_bases (tree
, int *, int *);
152 static void check_bases_and_members (tree
);
153 static tree
create_vtable_ptr (tree
, tree
*);
154 static void include_empty_classes (record_layout_info
);
155 static void layout_class_type (tree
, tree
*);
156 static void propagate_binfo_offsets (tree
, tree
);
157 static void layout_virtual_bases (record_layout_info
, splay_tree
);
158 static void build_vbase_offset_vtbl_entries (tree
, vtbl_init_data
*);
159 static void add_vcall_offset_vtbl_entries_r (tree
, vtbl_init_data
*);
160 static void add_vcall_offset_vtbl_entries_1 (tree
, vtbl_init_data
*);
161 static void build_vcall_offset_vtbl_entries (tree
, vtbl_init_data
*);
162 static void add_vcall_offset (tree
, tree
, vtbl_init_data
*);
163 static void layout_vtable_decl (tree
, int);
164 static tree
dfs_find_final_overrider_pre (tree
, void *);
165 static tree
dfs_find_final_overrider_post (tree
, void *);
166 static tree
find_final_overrider (tree
, tree
, tree
);
167 static int make_new_vtable (tree
, tree
);
168 static tree
get_primary_binfo (tree
);
169 static int maybe_indent_hierarchy (FILE *, int, int);
170 static tree
dump_class_hierarchy_r (FILE *, int, tree
, tree
, int);
171 static void dump_class_hierarchy (tree
);
172 static void dump_class_hierarchy_1 (FILE *, int, tree
);
173 static void dump_array (FILE *, tree
);
174 static void dump_vtable (tree
, tree
, tree
);
175 static void dump_vtt (tree
, tree
);
176 static void dump_thunk (FILE *, int, tree
);
177 static tree
build_vtable (tree
, tree
, tree
);
178 static void initialize_vtable (tree
, vec
<constructor_elt
, va_gc
> *);
179 static void layout_nonempty_base_or_field (record_layout_info
,
180 tree
, tree
, splay_tree
);
181 static tree
end_of_class (tree
, int);
182 static bool layout_empty_base (record_layout_info
, tree
, tree
, splay_tree
);
183 static void accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
184 vec
<constructor_elt
, va_gc
> **);
185 static void dfs_accumulate_vtbl_inits (tree
, tree
, tree
, tree
, tree
,
186 vec
<constructor_elt
, va_gc
> **);
187 static void build_rtti_vtbl_entries (tree
, vtbl_init_data
*);
188 static void build_vcall_and_vbase_vtbl_entries (tree
, vtbl_init_data
*);
189 static void clone_constructors_and_destructors (tree
);
190 static tree
build_clone (tree
, tree
);
191 static void update_vtable_entry_for_fn (tree
, tree
, tree
, tree
*, unsigned);
192 static void build_ctor_vtbl_group (tree
, tree
);
193 static void build_vtt (tree
);
194 static tree
binfo_ctor_vtable (tree
);
195 static void build_vtt_inits (tree
, tree
, vec
<constructor_elt
, va_gc
> **,
197 static tree
dfs_build_secondary_vptr_vtt_inits (tree
, void *);
198 static tree
dfs_fixup_binfo_vtbls (tree
, void *);
199 static int record_subobject_offset (tree
, tree
, splay_tree
);
200 static int check_subobject_offset (tree
, tree
, splay_tree
);
201 static int walk_subobject_offsets (tree
, subobject_offset_fn
,
202 tree
, splay_tree
, tree
, int);
203 static void record_subobject_offsets (tree
, tree
, splay_tree
, bool);
204 static int layout_conflict_p (tree
, tree
, splay_tree
, int);
205 static int splay_tree_compare_integer_csts (splay_tree_key k1
,
207 static void warn_about_ambiguous_bases (tree
);
208 static bool type_requires_array_cookie (tree
);
209 static bool contains_empty_class_p (tree
);
210 static bool base_derived_from (tree
, tree
);
211 static int empty_base_at_nonzero_offset_p (tree
, tree
, splay_tree
);
212 static tree
end_of_base (tree
);
213 static tree
get_vcall_index (tree
, tree
);
215 /* Variables shared between class.c and call.c. */
218 int n_vtable_entries
= 0;
219 int n_vtable_searches
= 0;
220 int n_vtable_elems
= 0;
221 int n_convert_harshness
= 0;
222 int n_compute_conversion_costs
= 0;
223 int n_inner_fields_searched
= 0;
225 /* Convert to or from a base subobject. EXPR is an expression of type
226 `A' or `A*', an expression of type `B' or `B*' is returned. To
227 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
228 the B base instance within A. To convert base A to derived B, CODE
229 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
230 In this latter case, A must not be a morally virtual base of B.
231 NONNULL is true if EXPR is known to be non-NULL (this is only
232 needed when EXPR is of pointer type). CV qualifiers are preserved
236 build_base_path (enum tree_code code
,
240 tsubst_flags_t complain
)
242 tree v_binfo
= NULL_TREE
;
243 tree d_binfo
= NULL_TREE
;
247 tree null_test
= NULL
;
248 tree ptr_target_type
;
250 int want_pointer
= TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
;
251 bool has_empty
= false;
254 if (expr
== error_mark_node
|| binfo
== error_mark_node
|| !binfo
)
255 return error_mark_node
;
257 for (probe
= binfo
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
260 if (is_empty_class (BINFO_TYPE (probe
)))
262 if (!v_binfo
&& BINFO_VIRTUAL_P (probe
))
266 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
268 probe
= TYPE_MAIN_VARIANT (TREE_TYPE (probe
));
270 if (code
== PLUS_EXPR
271 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo
), probe
))
273 /* This can happen when adjust_result_of_qualified_name_lookup can't
274 find a unique base binfo in a call to a member function. We
275 couldn't give the diagnostic then since we might have been calling
276 a static member function, so we do it now. */
277 if (complain
& tf_error
)
279 tree base
= lookup_base (probe
, BINFO_TYPE (d_binfo
),
280 ba_unique
, NULL
, complain
);
281 gcc_assert (base
== error_mark_node
);
283 return error_mark_node
;
286 gcc_assert ((code
== MINUS_EXPR
287 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), probe
))
288 || code
== PLUS_EXPR
);
290 if (binfo
== d_binfo
)
294 if (code
== MINUS_EXPR
&& v_binfo
)
296 if (complain
& tf_error
)
297 error ("cannot convert from base %qT to derived type %qT via "
298 "virtual base %qT", BINFO_TYPE (binfo
), BINFO_TYPE (d_binfo
),
299 BINFO_TYPE (v_binfo
));
300 return error_mark_node
;
304 /* This must happen before the call to save_expr. */
305 expr
= cp_build_addr_expr (expr
, complain
);
307 expr
= mark_rvalue_use (expr
);
309 offset
= BINFO_OFFSET (binfo
);
310 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
311 target_type
= code
== PLUS_EXPR
? BINFO_TYPE (binfo
) : BINFO_TYPE (d_binfo
);
312 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
313 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
314 expression returned matches the input. */
315 target_type
= cp_build_qualified_type
316 (target_type
, cp_type_quals (TREE_TYPE (TREE_TYPE (expr
))));
317 ptr_target_type
= build_pointer_type (target_type
);
319 /* Do we need to look in the vtable for the real offset? */
320 virtual_access
= (v_binfo
&& fixed_type_p
<= 0);
322 /* Don't bother with the calculations inside sizeof; they'll ICE if the
323 source type is incomplete and the pointer value doesn't matter. In a
324 template (even in fold_non_dependent_expr), we don't have vtables set
325 up properly yet, and the value doesn't matter there either; we're just
326 interested in the result of overload resolution. */
327 if (cp_unevaluated_operand
!= 0
328 || in_template_function ())
330 expr
= build_nop (ptr_target_type
, expr
);
332 expr
= build_indirect_ref (EXPR_LOCATION (expr
), expr
, RO_NULL
);
336 /* If we're in an NSDMI, we don't have the full constructor context yet
337 that we need for converting to a virtual base, so just build a stub
338 CONVERT_EXPR and expand it later in bot_replace. */
339 if (virtual_access
&& fixed_type_p
< 0
340 && current_scope () != current_function_decl
)
342 expr
= build1 (CONVERT_EXPR
, ptr_target_type
, expr
);
343 CONVERT_EXPR_VBASE_PATH (expr
) = true;
345 expr
= build_indirect_ref (EXPR_LOCATION (expr
), expr
, RO_NULL
);
349 /* Do we need to check for a null pointer? */
350 if (want_pointer
&& !nonnull
)
352 /* If we know the conversion will not actually change the value
353 of EXPR, then we can avoid testing the expression for NULL.
354 We have to avoid generating a COMPONENT_REF for a base class
355 field, because other parts of the compiler know that such
356 expressions are always non-NULL. */
357 if (!virtual_access
&& integer_zerop (offset
))
358 return build_nop (ptr_target_type
, expr
);
359 null_test
= error_mark_node
;
362 /* Protect against multiple evaluation if necessary. */
363 if (TREE_SIDE_EFFECTS (expr
) && (null_test
|| virtual_access
))
364 expr
= save_expr (expr
);
366 /* Now that we've saved expr, build the real null test. */
369 tree zero
= cp_convert (TREE_TYPE (expr
), nullptr_node
, complain
);
370 null_test
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
374 /* If this is a simple base reference, express it as a COMPONENT_REF. */
375 if (code
== PLUS_EXPR
&& !virtual_access
376 /* We don't build base fields for empty bases, and they aren't very
377 interesting to the optimizers anyway. */
380 expr
= cp_build_indirect_ref (expr
, RO_NULL
, complain
);
381 expr
= build_simple_base_path (expr
, binfo
);
383 expr
= build_address (expr
);
384 target_type
= TREE_TYPE (expr
);
390 /* Going via virtual base V_BINFO. We need the static offset
391 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
392 V_BINFO. That offset is an entry in D_BINFO's vtable. */
395 if (fixed_type_p
< 0 && in_base_initializer
)
397 /* In a base member initializer, we cannot rely on the
398 vtable being set up. We have to indirect via the
402 t
= TREE_TYPE (TYPE_VFIELD (current_class_type
));
403 t
= build_pointer_type (t
);
404 v_offset
= convert (t
, current_vtt_parm
);
405 v_offset
= cp_build_indirect_ref (v_offset
, RO_NULL
, complain
);
408 v_offset
= build_vfield_ref (cp_build_indirect_ref (expr
, RO_NULL
,
410 TREE_TYPE (TREE_TYPE (expr
)));
412 v_offset
= fold_build_pointer_plus (v_offset
, BINFO_VPTR_FIELD (v_binfo
));
413 v_offset
= build1 (NOP_EXPR
,
414 build_pointer_type (ptrdiff_type_node
),
416 v_offset
= cp_build_indirect_ref (v_offset
, RO_NULL
, complain
);
417 TREE_CONSTANT (v_offset
) = 1;
419 offset
= convert_to_integer (ptrdiff_type_node
,
420 size_diffop_loc (input_location
, offset
,
421 BINFO_OFFSET (v_binfo
)));
423 if (!integer_zerop (offset
))
424 v_offset
= build2 (code
, ptrdiff_type_node
, v_offset
, offset
);
426 if (fixed_type_p
< 0)
427 /* Negative fixed_type_p means this is a constructor or destructor;
428 virtual base layout is fixed in in-charge [cd]tors, but not in
430 offset
= build3 (COND_EXPR
, ptrdiff_type_node
,
431 build2 (EQ_EXPR
, boolean_type_node
,
432 current_in_charge_parm
, integer_zero_node
),
434 convert_to_integer (ptrdiff_type_node
,
435 BINFO_OFFSET (binfo
)));
441 target_type
= ptr_target_type
;
443 expr
= build1 (NOP_EXPR
, ptr_target_type
, expr
);
445 if (!integer_zerop (offset
))
447 offset
= fold_convert (sizetype
, offset
);
448 if (code
== MINUS_EXPR
)
449 offset
= fold_build1_loc (input_location
, NEGATE_EXPR
, sizetype
, offset
);
450 expr
= fold_build_pointer_plus (expr
, offset
);
456 expr
= cp_build_indirect_ref (expr
, RO_NULL
, complain
);
460 expr
= fold_build3_loc (input_location
, COND_EXPR
, target_type
, null_test
, expr
,
461 build_zero_cst (target_type
));
466 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
467 Perform a derived-to-base conversion by recursively building up a
468 sequence of COMPONENT_REFs to the appropriate base fields. */
471 build_simple_base_path (tree expr
, tree binfo
)
473 tree type
= BINFO_TYPE (binfo
);
474 tree d_binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
477 if (d_binfo
== NULL_TREE
)
481 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
);
483 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
484 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
485 an lvalue in the front end; only _DECLs and _REFs are lvalues
487 temp
= unary_complex_lvalue (ADDR_EXPR
, expr
);
489 expr
= cp_build_indirect_ref (temp
, RO_NULL
, tf_warning_or_error
);
495 expr
= build_simple_base_path (expr
, d_binfo
);
497 for (field
= TYPE_FIELDS (BINFO_TYPE (d_binfo
));
498 field
; field
= DECL_CHAIN (field
))
499 /* Is this the base field created by build_base_field? */
500 if (TREE_CODE (field
) == FIELD_DECL
501 && DECL_FIELD_IS_BASE (field
)
502 && TREE_TYPE (field
) == type
503 /* If we're looking for a field in the most-derived class,
504 also check the field offset; we can have two base fields
505 of the same type if one is an indirect virtual base and one
506 is a direct non-virtual base. */
507 && (BINFO_INHERITANCE_CHAIN (d_binfo
)
508 || tree_int_cst_equal (byte_position (field
),
509 BINFO_OFFSET (binfo
))))
511 /* We don't use build_class_member_access_expr here, as that
512 has unnecessary checks, and more importantly results in
513 recursive calls to dfs_walk_once. */
514 int type_quals
= cp_type_quals (TREE_TYPE (expr
));
516 expr
= build3 (COMPONENT_REF
,
517 cp_build_qualified_type (type
, type_quals
),
518 expr
, field
, NULL_TREE
);
519 expr
= fold_if_not_in_template (expr
);
521 /* Mark the expression const or volatile, as appropriate.
522 Even though we've dealt with the type above, we still have
523 to mark the expression itself. */
524 if (type_quals
& TYPE_QUAL_CONST
)
525 TREE_READONLY (expr
) = 1;
526 if (type_quals
& TYPE_QUAL_VOLATILE
)
527 TREE_THIS_VOLATILE (expr
) = 1;
532 /* Didn't find the base field?!? */
536 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
537 type is a class type or a pointer to a class type. In the former
538 case, TYPE is also a class type; in the latter it is another
539 pointer type. If CHECK_ACCESS is true, an error message is emitted
540 if TYPE is inaccessible. If OBJECT has pointer type, the value is
541 assumed to be non-NULL. */
544 convert_to_base (tree object
, tree type
, bool check_access
, bool nonnull
,
545 tsubst_flags_t complain
)
550 if (TYPE_PTR_P (TREE_TYPE (object
)))
552 object_type
= TREE_TYPE (TREE_TYPE (object
));
553 type
= TREE_TYPE (type
);
556 object_type
= TREE_TYPE (object
);
558 binfo
= lookup_base (object_type
, type
, check_access
? ba_check
: ba_unique
,
560 if (!binfo
|| binfo
== error_mark_node
)
561 return error_mark_node
;
563 return build_base_path (PLUS_EXPR
, object
, binfo
, nonnull
, complain
);
566 /* EXPR is an expression with unqualified class type. BASE is a base
567 binfo of that class type. Returns EXPR, converted to the BASE
568 type. This function assumes that EXPR is the most derived class;
569 therefore virtual bases can be found at their static offsets. */
572 convert_to_base_statically (tree expr
, tree base
)
576 expr_type
= TREE_TYPE (expr
);
577 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base
), expr_type
))
579 /* If this is a non-empty base, use a COMPONENT_REF. */
580 if (!is_empty_class (BINFO_TYPE (base
)))
581 return build_simple_base_path (expr
, base
);
583 /* We use fold_build2 and fold_convert below to simplify the trees
584 provided to the optimizers. It is not safe to call these functions
585 when processing a template because they do not handle C++-specific
587 gcc_assert (!processing_template_decl
);
588 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
589 if (!integer_zerop (BINFO_OFFSET (base
)))
590 expr
= fold_build_pointer_plus_loc (input_location
,
591 expr
, BINFO_OFFSET (base
));
592 expr
= fold_convert (build_pointer_type (BINFO_TYPE (base
)), expr
);
593 expr
= build_fold_indirect_ref_loc (input_location
, expr
);
601 build_vfield_ref (tree datum
, tree type
)
603 tree vfield
, vcontext
;
605 if (datum
== error_mark_node
)
606 return error_mark_node
;
608 /* First, convert to the requested type. */
609 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
610 datum
= convert_to_base (datum
, type
, /*check_access=*/false,
611 /*nonnull=*/true, tf_warning_or_error
);
613 /* Second, the requested type may not be the owner of its own vptr.
614 If not, convert to the base class that owns it. We cannot use
615 convert_to_base here, because VCONTEXT may appear more than once
616 in the inheritance hierarchy of TYPE, and thus direct conversion
617 between the types may be ambiguous. Following the path back up
618 one step at a time via primary bases avoids the problem. */
619 vfield
= TYPE_VFIELD (type
);
620 vcontext
= DECL_CONTEXT (vfield
);
621 while (!same_type_ignoring_top_level_qualifiers_p (vcontext
, type
))
623 datum
= build_simple_base_path (datum
, CLASSTYPE_PRIMARY_BINFO (type
));
624 type
= TREE_TYPE (datum
);
627 return build3 (COMPONENT_REF
, TREE_TYPE (vfield
), datum
, vfield
, NULL_TREE
);
630 /* Given an object INSTANCE, return an expression which yields the
631 vtable element corresponding to INDEX. There are many special
632 cases for INSTANCE which we take care of here, mainly to avoid
633 creating extra tree nodes when we don't have to. */
636 build_vtbl_ref_1 (tree instance
, tree idx
)
639 tree vtbl
= NULL_TREE
;
641 /* Try to figure out what a reference refers to, and
642 access its virtual function table directly. */
645 tree fixed_type
= fixed_type_or_null (instance
, NULL
, &cdtorp
);
647 tree basetype
= non_reference (TREE_TYPE (instance
));
649 if (fixed_type
&& !cdtorp
)
651 tree binfo
= lookup_base (fixed_type
, basetype
,
652 ba_unique
, NULL
, tf_none
);
653 if (binfo
&& binfo
!= error_mark_node
)
654 vtbl
= unshare_expr (BINFO_VTABLE (binfo
));
658 vtbl
= build_vfield_ref (instance
, basetype
);
660 aref
= build_array_ref (input_location
, vtbl
, idx
);
661 TREE_CONSTANT (aref
) |= TREE_CONSTANT (vtbl
) && TREE_CONSTANT (idx
);
667 build_vtbl_ref (tree instance
, tree idx
)
669 tree aref
= build_vtbl_ref_1 (instance
, idx
);
674 /* Given a stable object pointer INSTANCE_PTR, return an expression which
675 yields a function pointer corresponding to vtable element INDEX. */
678 build_vfn_ref (tree instance_ptr
, tree idx
)
682 aref
= build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr
, RO_NULL
,
683 tf_warning_or_error
),
686 /* When using function descriptors, the address of the
687 vtable entry is treated as a function pointer. */
688 if (TARGET_VTABLE_USES_DESCRIPTORS
)
689 aref
= build1 (NOP_EXPR
, TREE_TYPE (aref
),
690 cp_build_addr_expr (aref
, tf_warning_or_error
));
692 /* Remember this as a method reference, for later devirtualization. */
693 aref
= build3 (OBJ_TYPE_REF
, TREE_TYPE (aref
), aref
, instance_ptr
, idx
);
698 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
699 for the given TYPE. */
702 get_vtable_name (tree type
)
704 return mangle_vtbl_for_type (type
);
707 /* DECL is an entity associated with TYPE, like a virtual table or an
708 implicitly generated constructor. Determine whether or not DECL
709 should have external or internal linkage at the object file
710 level. This routine does not deal with COMDAT linkage and other
711 similar complexities; it simply sets TREE_PUBLIC if it possible for
712 entities in other translation units to contain copies of DECL, in
716 set_linkage_according_to_type (tree
/*type*/, tree decl
)
718 TREE_PUBLIC (decl
) = 1;
719 determine_visibility (decl
);
722 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
723 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
724 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
727 build_vtable (tree class_type
, tree name
, tree vtable_type
)
731 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
732 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
733 now to avoid confusion in mangle_decl. */
734 SET_DECL_ASSEMBLER_NAME (decl
, name
);
735 DECL_CONTEXT (decl
) = class_type
;
736 DECL_ARTIFICIAL (decl
) = 1;
737 TREE_STATIC (decl
) = 1;
738 TREE_READONLY (decl
) = 1;
739 DECL_VIRTUAL_P (decl
) = 1;
740 DECL_ALIGN (decl
) = TARGET_VTABLE_ENTRY_ALIGN
;
741 DECL_VTABLE_OR_VTT_P (decl
) = 1;
742 /* At one time the vtable info was grabbed 2 words at a time. This
743 fails on sparc unless you have 8-byte alignment. (tiemann) */
744 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
746 set_linkage_according_to_type (class_type
, decl
);
747 /* The vtable has not been defined -- yet. */
748 DECL_EXTERNAL (decl
) = 1;
749 DECL_NOT_REALLY_EXTERN (decl
) = 1;
751 /* Mark the VAR_DECL node representing the vtable itself as a
752 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
753 is rather important that such things be ignored because any
754 effort to actually generate DWARF for them will run into
755 trouble when/if we encounter code like:
758 struct S { virtual void member (); };
760 because the artificial declaration of the vtable itself (as
761 manufactured by the g++ front end) will say that the vtable is
762 a static member of `S' but only *after* the debug output for
763 the definition of `S' has already been output. This causes
764 grief because the DWARF entry for the definition of the vtable
765 will try to refer back to an earlier *declaration* of the
766 vtable as a static member of `S' and there won't be one. We
767 might be able to arrange to have the "vtable static member"
768 attached to the member list for `S' before the debug info for
769 `S' get written (which would solve the problem) but that would
770 require more intrusive changes to the g++ front end. */
771 DECL_IGNORED_P (decl
) = 1;
776 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
777 or even complete. If this does not exist, create it. If COMPLETE is
778 nonzero, then complete the definition of it -- that will render it
779 impossible to actually build the vtable, but is useful to get at those
780 which are known to exist in the runtime. */
783 get_vtable_decl (tree type
, int complete
)
787 if (CLASSTYPE_VTABLES (type
))
788 return CLASSTYPE_VTABLES (type
);
790 decl
= build_vtable (type
, get_vtable_name (type
), vtbl_type_node
);
791 CLASSTYPE_VTABLES (type
) = decl
;
795 DECL_EXTERNAL (decl
) = 1;
796 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
802 /* Build the primary virtual function table for TYPE. If BINFO is
803 non-NULL, build the vtable starting with the initial approximation
804 that it is the same as the one which is the head of the association
805 list. Returns a nonzero value if a new vtable is actually
809 build_primary_vtable (tree binfo
, tree type
)
814 decl
= get_vtable_decl (type
, /*complete=*/0);
818 if (BINFO_NEW_VTABLE_MARKED (binfo
))
819 /* We have already created a vtable for this base, so there's
820 no need to do it again. */
823 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
824 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
825 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
826 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
830 gcc_assert (TREE_TYPE (decl
) == vtbl_type_node
);
831 virtuals
= NULL_TREE
;
834 if (GATHER_STATISTICS
)
837 n_vtable_elems
+= list_length (virtuals
);
840 /* Initialize the association list for this type, based
841 on our first approximation. */
842 BINFO_VTABLE (TYPE_BINFO (type
)) = decl
;
843 BINFO_VIRTUALS (TYPE_BINFO (type
)) = virtuals
;
844 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
));
848 /* Give BINFO a new virtual function table which is initialized
849 with a skeleton-copy of its original initialization. The only
850 entry that changes is the `delta' entry, so we can really
851 share a lot of structure.
853 FOR_TYPE is the most derived type which caused this table to
856 Returns nonzero if we haven't met BINFO before.
858 The order in which vtables are built (by calling this function) for
859 an object must remain the same, otherwise a binary incompatibility
863 build_secondary_vtable (tree binfo
)
865 if (BINFO_NEW_VTABLE_MARKED (binfo
))
866 /* We already created a vtable for this base. There's no need to
870 /* Remember that we've created a vtable for this BINFO, so that we
871 don't try to do so again. */
872 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
874 /* Make fresh virtual list, so we can smash it later. */
875 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
877 /* Secondary vtables are laid out as part of the same structure as
878 the primary vtable. */
879 BINFO_VTABLE (binfo
) = NULL_TREE
;
883 /* Create a new vtable for BINFO which is the hierarchy dominated by
884 T. Return nonzero if we actually created a new vtable. */
887 make_new_vtable (tree t
, tree binfo
)
889 if (binfo
== TYPE_BINFO (t
))
890 /* In this case, it is *type*'s vtable we are modifying. We start
891 with the approximation that its vtable is that of the
892 immediate base class. */
893 return build_primary_vtable (binfo
, t
);
895 /* This is our very own copy of `basetype' to play with. Later,
896 we will fill in all the virtual functions that override the
897 virtual functions in these base classes which are not defined
898 by the current type. */
899 return build_secondary_vtable (binfo
);
902 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
903 (which is in the hierarchy dominated by T) list FNDECL as its
904 BV_FN. DELTA is the required constant adjustment from the `this'
905 pointer where the vtable entry appears to the `this' required when
906 the function is actually called. */
909 modify_vtable_entry (tree t
,
919 if (fndecl
!= BV_FN (v
)
920 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
922 /* We need a new vtable for BINFO. */
923 if (make_new_vtable (t
, binfo
))
925 /* If we really did make a new vtable, we also made a copy
926 of the BINFO_VIRTUALS list. Now, we have to find the
927 corresponding entry in that list. */
928 *virtuals
= BINFO_VIRTUALS (binfo
);
929 while (BV_FN (*virtuals
) != BV_FN (v
))
930 *virtuals
= TREE_CHAIN (*virtuals
);
934 BV_DELTA (v
) = delta
;
935 BV_VCALL_INDEX (v
) = NULL_TREE
;
941 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
942 the USING_DECL naming METHOD. Returns true if the method could be
943 added to the method vec. */
946 add_method (tree type
, tree method
, tree using_decl
)
950 bool template_conv_p
= false;
952 vec
<tree
, va_gc
> *method_vec
;
954 bool insert_p
= false;
958 if (method
== error_mark_node
)
961 complete_p
= COMPLETE_TYPE_P (type
);
962 conv_p
= DECL_CONV_FN_P (method
);
964 template_conv_p
= (TREE_CODE (method
) == TEMPLATE_DECL
965 && DECL_TEMPLATE_CONV_FN_P (method
));
967 method_vec
= CLASSTYPE_METHOD_VEC (type
);
970 /* Make a new method vector. We start with 8 entries. We must
971 allocate at least two (for constructors and destructors), and
972 we're going to end up with an assignment operator at some
974 vec_alloc (method_vec
, 8);
975 /* Create slots for constructors and destructors. */
976 method_vec
->quick_push (NULL_TREE
);
977 method_vec
->quick_push (NULL_TREE
);
978 CLASSTYPE_METHOD_VEC (type
) = method_vec
;
981 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
982 grok_special_member_properties (method
);
984 /* Constructors and destructors go in special slots. */
985 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method
))
986 slot
= CLASSTYPE_CONSTRUCTOR_SLOT
;
987 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
989 slot
= CLASSTYPE_DESTRUCTOR_SLOT
;
991 if (TYPE_FOR_JAVA (type
))
993 if (!DECL_ARTIFICIAL (method
))
994 error ("Java class %qT cannot have a destructor", type
);
995 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
996 error ("Java class %qT cannot have an implicit non-trivial "
1006 /* See if we already have an entry with this name. */
1007 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1008 vec_safe_iterate (method_vec
, slot
, &m
);
1011 m
= OVL_CURRENT (m
);
1012 if (template_conv_p
)
1014 if (TREE_CODE (m
) == TEMPLATE_DECL
1015 && DECL_TEMPLATE_CONV_FN_P (m
))
1019 if (conv_p
&& !DECL_CONV_FN_P (m
))
1021 if (DECL_NAME (m
) == DECL_NAME (method
))
1027 && !DECL_CONV_FN_P (m
)
1028 && DECL_NAME (m
) > DECL_NAME (method
))
1032 current_fns
= insert_p
? NULL_TREE
: (*method_vec
)[slot
];
1034 /* Check to see if we've already got this method. */
1035 for (fns
= current_fns
; fns
; fns
= OVL_NEXT (fns
))
1037 tree fn
= OVL_CURRENT (fns
);
1043 if (TREE_CODE (fn
) != TREE_CODE (method
))
1046 /* [over.load] Member function declarations with the
1047 same name and the same parameter types cannot be
1048 overloaded if any of them is a static member
1049 function declaration.
1051 [namespace.udecl] When a using-declaration brings names
1052 from a base class into a derived class scope, member
1053 functions in the derived class override and/or hide member
1054 functions with the same name and parameter types in a base
1055 class (rather than conflicting). */
1056 fn_type
= TREE_TYPE (fn
);
1057 method_type
= TREE_TYPE (method
);
1058 parms1
= TYPE_ARG_TYPES (fn_type
);
1059 parms2
= TYPE_ARG_TYPES (method_type
);
1061 /* Compare the quals on the 'this' parm. Don't compare
1062 the whole types, as used functions are treated as
1063 coming from the using class in overload resolution. */
1064 if (! DECL_STATIC_FUNCTION_P (fn
)
1065 && ! DECL_STATIC_FUNCTION_P (method
)
1066 && TREE_TYPE (TREE_VALUE (parms1
)) != error_mark_node
1067 && TREE_TYPE (TREE_VALUE (parms2
)) != error_mark_node
1068 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1
)))
1069 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2
)))))
1072 /* For templates, the return type and template parameters
1073 must be identical. */
1074 if (TREE_CODE (fn
) == TEMPLATE_DECL
1075 && (!same_type_p (TREE_TYPE (fn_type
),
1076 TREE_TYPE (method_type
))
1077 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
1078 DECL_TEMPLATE_PARMS (method
))))
1081 if (! DECL_STATIC_FUNCTION_P (fn
))
1082 parms1
= TREE_CHAIN (parms1
);
1083 if (! DECL_STATIC_FUNCTION_P (method
))
1084 parms2
= TREE_CHAIN (parms2
);
1086 if (compparms (parms1
, parms2
)
1087 && (!DECL_CONV_FN_P (fn
)
1088 || same_type_p (TREE_TYPE (fn_type
),
1089 TREE_TYPE (method_type
))))
1091 /* For function versions, their parms and types match
1092 but they are not duplicates. Record function versions
1093 as and when they are found. extern "C" functions are
1094 not treated as versions. */
1095 if (TREE_CODE (fn
) == FUNCTION_DECL
1096 && TREE_CODE (method
) == FUNCTION_DECL
1097 && !DECL_EXTERN_C_P (fn
)
1098 && !DECL_EXTERN_C_P (method
)
1099 && (DECL_FUNCTION_SPECIFIC_TARGET (fn
)
1100 || DECL_FUNCTION_SPECIFIC_TARGET (method
))
1101 && targetm
.target_option
.function_versions (fn
, method
))
1103 /* Mark functions as versions if necessary. Modify the mangled
1104 decl name if necessary. */
1105 if (!DECL_FUNCTION_VERSIONED (fn
))
1107 DECL_FUNCTION_VERSIONED (fn
) = 1;
1108 if (DECL_ASSEMBLER_NAME_SET_P (fn
))
1111 if (!DECL_FUNCTION_VERSIONED (method
))
1113 DECL_FUNCTION_VERSIONED (method
) = 1;
1114 if (DECL_ASSEMBLER_NAME_SET_P (method
))
1115 mangle_decl (method
);
1117 record_function_versions (fn
, method
);
1120 if (DECL_INHERITED_CTOR_BASE (method
))
1122 if (DECL_INHERITED_CTOR_BASE (fn
))
1124 error_at (DECL_SOURCE_LOCATION (method
),
1125 "%q#D inherited from %qT", method
,
1126 DECL_INHERITED_CTOR_BASE (method
));
1127 error_at (DECL_SOURCE_LOCATION (fn
),
1128 "conflicts with version inherited from %qT",
1129 DECL_INHERITED_CTOR_BASE (fn
));
1131 /* Otherwise defer to the other function. */
1136 if (DECL_CONTEXT (fn
) == type
)
1137 /* Defer to the local function. */
1142 error ("%q+#D cannot be overloaded", method
);
1143 error ("with %q+#D", fn
);
1146 /* We don't call duplicate_decls here to merge the
1147 declarations because that will confuse things if the
1148 methods have inline definitions. In particular, we
1149 will crash while processing the definitions. */
1154 /* A class should never have more than one destructor. */
1155 if (current_fns
&& DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
1158 /* Add the new binding. */
1161 overload
= ovl_cons (method
, current_fns
);
1162 OVL_USED (overload
) = true;
1165 overload
= build_overload (method
, current_fns
);
1168 TYPE_HAS_CONVERSION (type
) = 1;
1169 else if (slot
>= CLASSTYPE_FIRST_CONVERSION_SLOT
&& !complete_p
)
1170 push_class_level_binding (DECL_NAME (method
), overload
);
1176 /* We only expect to add few methods in the COMPLETE_P case, so
1177 just make room for one more method in that case. */
1179 reallocated
= vec_safe_reserve_exact (method_vec
, 1);
1181 reallocated
= vec_safe_reserve (method_vec
, 1);
1183 CLASSTYPE_METHOD_VEC (type
) = method_vec
;
1184 if (slot
== method_vec
->length ())
1185 method_vec
->quick_push (overload
);
1187 method_vec
->quick_insert (slot
, overload
);
1190 /* Replace the current slot. */
1191 (*method_vec
)[slot
] = overload
;
1195 /* Subroutines of finish_struct. */
1197 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1198 legit, otherwise return 0. */
1201 alter_access (tree t
, tree fdecl
, tree access
)
1205 if (!DECL_LANG_SPECIFIC (fdecl
))
1206 retrofit_lang_decl (fdecl
);
1208 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl
));
1210 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1213 if (TREE_VALUE (elem
) != access
)
1215 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1216 error ("conflicting access specifications for method"
1217 " %q+D, ignored", TREE_TYPE (fdecl
));
1219 error ("conflicting access specifications for field %qE, ignored",
1224 /* They're changing the access to the same thing they changed
1225 it to before. That's OK. */
1231 perform_or_defer_access_check (TYPE_BINFO (t
), fdecl
, fdecl
,
1232 tf_warning_or_error
);
1233 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1239 /* Process the USING_DECL, which is a member of T. */
1242 handle_using_decl (tree using_decl
, tree t
)
1244 tree decl
= USING_DECL_DECLS (using_decl
);
1245 tree name
= DECL_NAME (using_decl
);
1247 = TREE_PRIVATE (using_decl
) ? access_private_node
1248 : TREE_PROTECTED (using_decl
) ? access_protected_node
1249 : access_public_node
;
1250 tree flist
= NULL_TREE
;
1253 gcc_assert (!processing_template_decl
&& decl
);
1255 old_value
= lookup_member (t
, name
, /*protect=*/0, /*want_type=*/false,
1256 tf_warning_or_error
);
1259 if (is_overloaded_fn (old_value
))
1260 old_value
= OVL_CURRENT (old_value
);
1262 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1265 old_value
= NULL_TREE
;
1268 cp_emit_debug_info_for_using (decl
, USING_DECL_SCOPE (using_decl
));
1270 if (is_overloaded_fn (decl
))
1275 else if (is_overloaded_fn (old_value
))
1278 /* It's OK to use functions from a base when there are functions with
1279 the same name already present in the current class. */;
1282 error ("%q+D invalid in %q#T", using_decl
, t
);
1283 error (" because of local method %q+#D with same name",
1284 OVL_CURRENT (old_value
));
1288 else if (!DECL_ARTIFICIAL (old_value
))
1290 error ("%q+D invalid in %q#T", using_decl
, t
);
1291 error (" because of local member %q+#D with same name", old_value
);
1295 /* Make type T see field decl FDECL with access ACCESS. */
1297 for (; flist
; flist
= OVL_NEXT (flist
))
1299 add_method (t
, OVL_CURRENT (flist
), using_decl
);
1300 alter_access (t
, OVL_CURRENT (flist
), access
);
1303 alter_access (t
, decl
, access
);
1306 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1307 types with abi tags, add the corresponding identifiers to the VEC in
1308 *DATA and set IDENTIFIER_MARKED. */
1317 find_abi_tags_r (tree
*tp
, int */
*walk_subtrees*/
, void *data
)
1319 if (!TAGGED_TYPE_P (*tp
))
1322 if (tree attributes
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp
)))
1324 struct abi_tag_data
*p
= static_cast<struct abi_tag_data
*>(data
);
1325 for (tree list
= TREE_VALUE (attributes
); list
;
1326 list
= TREE_CHAIN (list
))
1328 tree tag
= TREE_VALUE (list
);
1329 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1330 if (!IDENTIFIER_MARKED (id
))
1332 if (TYPE_P (p
->subob
))
1334 warning (OPT_Wabi_tag
, "%qT does not have the %E abi tag "
1335 "that base %qT has", p
->t
, tag
, p
->subob
);
1336 inform (location_of (p
->subob
), "%qT declared here",
1341 warning (OPT_Wabi_tag
, "%qT does not have the %E abi tag "
1342 "that %qT (used in the type of %qD) has",
1343 p
->t
, tag
, *tp
, p
->subob
);
1344 inform (location_of (p
->subob
), "%qD declared here",
1346 inform (location_of (*tp
), "%qT declared here", *tp
);
1354 /* Check that class T has all the abi tags that subobject SUBOB has, or
1358 check_abi_tags (tree t
, tree subob
)
1360 tree attributes
= lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t
));
1363 for (tree list
= TREE_VALUE (attributes
); list
;
1364 list
= TREE_CHAIN (list
))
1366 tree tag
= TREE_VALUE (list
);
1367 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1368 IDENTIFIER_MARKED (id
) = true;
1372 tree subtype
= TYPE_P (subob
) ? subob
: TREE_TYPE (subob
);
1373 struct abi_tag_data data
= { t
, subob
};
1375 cp_walk_tree_without_duplicates (&subtype
, find_abi_tags_r
, &data
);
1379 for (tree list
= TREE_VALUE (attributes
); list
;
1380 list
= TREE_CHAIN (list
))
1382 tree tag
= TREE_VALUE (list
);
1383 tree id
= get_identifier (TREE_STRING_POINTER (tag
));
1384 IDENTIFIER_MARKED (id
) = false;
1389 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1390 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1391 properties of the bases. */
1394 check_bases (tree t
,
1395 int* cant_have_const_ctor_p
,
1396 int* no_const_asn_ref_p
)
1399 bool seen_non_virtual_nearly_empty_base_p
= 0;
1400 int seen_tm_mask
= 0;
1403 tree field
= NULL_TREE
;
1405 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1406 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
1407 if (TREE_CODE (field
) == FIELD_DECL
)
1410 for (binfo
= TYPE_BINFO (t
), i
= 0;
1411 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1413 tree basetype
= TREE_TYPE (base_binfo
);
1415 gcc_assert (COMPLETE_TYPE_P (basetype
));
1417 if (CLASSTYPE_FINAL (basetype
))
1418 error ("cannot derive from %<final%> base %qT in derived type %qT",
1421 /* If any base class is non-literal, so is the derived class. */
1422 if (!CLASSTYPE_LITERAL_P (basetype
))
1423 CLASSTYPE_LITERAL_P (t
) = false;
1425 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1426 here because the case of virtual functions but non-virtual
1427 dtor is handled in finish_struct_1. */
1428 if (!TYPE_POLYMORPHIC_P (basetype
))
1429 warning (OPT_Weffc__
,
1430 "base class %q#T has a non-virtual destructor", basetype
);
1432 /* If the base class doesn't have copy constructors or
1433 assignment operators that take const references, then the
1434 derived class cannot have such a member automatically
1436 if (TYPE_HAS_COPY_CTOR (basetype
)
1437 && ! TYPE_HAS_CONST_COPY_CTOR (basetype
))
1438 *cant_have_const_ctor_p
= 1;
1439 if (TYPE_HAS_COPY_ASSIGN (basetype
)
1440 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype
))
1441 *no_const_asn_ref_p
= 1;
1443 if (BINFO_VIRTUAL_P (base_binfo
))
1444 /* A virtual base does not effect nearly emptiness. */
1446 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1448 if (seen_non_virtual_nearly_empty_base_p
)
1449 /* And if there is more than one nearly empty base, then the
1450 derived class is not nearly empty either. */
1451 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1453 /* Remember we've seen one. */
1454 seen_non_virtual_nearly_empty_base_p
= 1;
1456 else if (!is_empty_class (basetype
))
1457 /* If the base class is not empty or nearly empty, then this
1458 class cannot be nearly empty. */
1459 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1461 /* A lot of properties from the bases also apply to the derived
1463 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1464 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1465 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1466 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
1467 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype
)
1468 || !TYPE_HAS_COPY_ASSIGN (basetype
));
1469 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype
)
1470 || !TYPE_HAS_COPY_CTOR (basetype
));
1471 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
)
1472 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype
);
1473 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype
);
1474 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1475 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
)
1476 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype
);
1477 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
1478 || TYPE_HAS_COMPLEX_DFLT (basetype
));
1480 /* A standard-layout class is a class that:
1482 * has no non-standard-layout base classes, */
1483 CLASSTYPE_NON_STD_LAYOUT (t
) |= CLASSTYPE_NON_STD_LAYOUT (basetype
);
1484 if (!CLASSTYPE_NON_STD_LAYOUT (t
))
1487 /* ...has no base classes of the same type as the first non-static
1489 if (field
&& DECL_CONTEXT (field
) == t
1490 && (same_type_ignoring_top_level_qualifiers_p
1491 (TREE_TYPE (field
), basetype
)))
1492 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1494 /* ...either has no non-static data members in the most-derived
1495 class and at most one base class with non-static data
1496 members, or has no base classes with non-static data
1498 for (basefield
= TYPE_FIELDS (basetype
); basefield
;
1499 basefield
= DECL_CHAIN (basefield
))
1500 if (TREE_CODE (basefield
) == FIELD_DECL
)
1503 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
1510 /* Don't bother collecting tm attributes if transactional memory
1511 support is not enabled. */
1514 tree tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (basetype
));
1516 seen_tm_mask
|= tm_attr_to_mask (tm_attr
);
1519 check_abi_tags (t
, basetype
);
1522 /* If one of the base classes had TM attributes, and the current class
1523 doesn't define its own, then the current class inherits one. */
1524 if (seen_tm_mask
&& !find_tm_attribute (TYPE_ATTRIBUTES (t
)))
1526 tree tm_attr
= tm_mask_to_attr (seen_tm_mask
& -seen_tm_mask
);
1527 TYPE_ATTRIBUTES (t
) = tree_cons (tm_attr
, NULL
, TYPE_ATTRIBUTES (t
));
1531 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1532 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1533 that have had a nearly-empty virtual primary base stolen by some
1534 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1538 determine_primary_bases (tree t
)
1541 tree primary
= NULL_TREE
;
1542 tree type_binfo
= TYPE_BINFO (t
);
1545 /* Determine the primary bases of our bases. */
1546 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1547 base_binfo
= TREE_CHAIN (base_binfo
))
1549 tree primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo
));
1551 /* See if we're the non-virtual primary of our inheritance
1553 if (!BINFO_VIRTUAL_P (base_binfo
))
1555 tree parent
= BINFO_INHERITANCE_CHAIN (base_binfo
);
1556 tree parent_primary
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent
));
1559 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo
),
1560 BINFO_TYPE (parent_primary
)))
1561 /* We are the primary binfo. */
1562 BINFO_PRIMARY_P (base_binfo
) = 1;
1564 /* Determine if we have a virtual primary base, and mark it so.
1566 if (primary
&& BINFO_VIRTUAL_P (primary
))
1568 tree this_primary
= copied_binfo (primary
, base_binfo
);
1570 if (BINFO_PRIMARY_P (this_primary
))
1571 /* Someone already claimed this base. */
1572 BINFO_LOST_PRIMARY_P (base_binfo
) = 1;
1577 BINFO_PRIMARY_P (this_primary
) = 1;
1578 BINFO_INHERITANCE_CHAIN (this_primary
) = base_binfo
;
1580 /* A virtual binfo might have been copied from within
1581 another hierarchy. As we're about to use it as a
1582 primary base, make sure the offsets match. */
1583 delta
= size_diffop_loc (input_location
,
1585 BINFO_OFFSET (base_binfo
)),
1587 BINFO_OFFSET (this_primary
)));
1589 propagate_binfo_offsets (this_primary
, delta
);
1594 /* First look for a dynamic direct non-virtual base. */
1595 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, base_binfo
); i
++)
1597 tree basetype
= BINFO_TYPE (base_binfo
);
1599 if (TYPE_CONTAINS_VPTR_P (basetype
) && !BINFO_VIRTUAL_P (base_binfo
))
1601 primary
= base_binfo
;
1606 /* A "nearly-empty" virtual base class can be the primary base
1607 class, if no non-virtual polymorphic base can be found. Look for
1608 a nearly-empty virtual dynamic base that is not already a primary
1609 base of something in the hierarchy. If there is no such base,
1610 just pick the first nearly-empty virtual base. */
1612 for (base_binfo
= TREE_CHAIN (type_binfo
); base_binfo
;
1613 base_binfo
= TREE_CHAIN (base_binfo
))
1614 if (BINFO_VIRTUAL_P (base_binfo
)
1615 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo
)))
1617 if (!BINFO_PRIMARY_P (base_binfo
))
1619 /* Found one that is not primary. */
1620 primary
= base_binfo
;
1624 /* Remember the first candidate. */
1625 primary
= base_binfo
;
1629 /* If we've got a primary base, use it. */
1632 tree basetype
= BINFO_TYPE (primary
);
1634 CLASSTYPE_PRIMARY_BINFO (t
) = primary
;
1635 if (BINFO_PRIMARY_P (primary
))
1636 /* We are stealing a primary base. */
1637 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary
)) = 1;
1638 BINFO_PRIMARY_P (primary
) = 1;
1639 if (BINFO_VIRTUAL_P (primary
))
1643 BINFO_INHERITANCE_CHAIN (primary
) = type_binfo
;
1644 /* A virtual binfo might have been copied from within
1645 another hierarchy. As we're about to use it as a primary
1646 base, make sure the offsets match. */
1647 delta
= size_diffop_loc (input_location
, ssize_int (0),
1648 convert (ssizetype
, BINFO_OFFSET (primary
)));
1650 propagate_binfo_offsets (primary
, delta
);
1653 primary
= TYPE_BINFO (basetype
);
1655 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1656 BINFO_VTABLE (type_binfo
) = BINFO_VTABLE (primary
);
1657 BINFO_VIRTUALS (type_binfo
) = BINFO_VIRTUALS (primary
);
1661 /* Update the variant types of T. */
1664 fixup_type_variants (tree t
)
1671 for (variants
= TYPE_NEXT_VARIANT (t
);
1673 variants
= TYPE_NEXT_VARIANT (variants
))
1675 /* These fields are in the _TYPE part of the node, not in
1676 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1677 TYPE_HAS_USER_CONSTRUCTOR (variants
) = TYPE_HAS_USER_CONSTRUCTOR (t
);
1678 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1679 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1680 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1682 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1684 TYPE_BINFO (variants
) = TYPE_BINFO (t
);
1686 /* Copy whatever these are holding today. */
1687 TYPE_VFIELD (variants
) = TYPE_VFIELD (t
);
1688 TYPE_METHODS (variants
) = TYPE_METHODS (t
);
1689 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1693 /* Early variant fixups: we apply attributes at the beginning of the class
1694 definition, and we need to fix up any variants that have already been
1695 made via elaborated-type-specifier so that check_qualified_type works. */
1698 fixup_attribute_variants (tree t
)
1705 for (variants
= TYPE_NEXT_VARIANT (t
);
1707 variants
= TYPE_NEXT_VARIANT (variants
))
1709 /* These are the two fields that check_qualified_type looks at and
1710 are affected by attributes. */
1711 TYPE_ATTRIBUTES (variants
) = TYPE_ATTRIBUTES (t
);
1712 TYPE_ALIGN (variants
) = TYPE_ALIGN (t
);
1716 /* Set memoizing fields and bits of T (and its variants) for later
1720 finish_struct_bits (tree t
)
1722 /* Fix up variants (if any). */
1723 fixup_type_variants (t
);
1725 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) && TYPE_POLYMORPHIC_P (t
))
1726 /* For a class w/o baseclasses, 'finish_struct' has set
1727 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1728 Similarly for a class whose base classes do not have vtables.
1729 When neither of these is true, we might have removed abstract
1730 virtuals (by providing a definition), added some (by declaring
1731 new ones), or redeclared ones from a base class. We need to
1732 recalculate what's really an abstract virtual at this point (by
1733 looking in the vtables). */
1734 get_pure_virtuals (t
);
1736 /* If this type has a copy constructor or a destructor, force its
1737 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1738 nonzero. This will cause it to be passed by invisible reference
1739 and prevent it from being returned in a register. */
1740 if (type_has_nontrivial_copy_init (t
)
1741 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
1744 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
1745 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
1747 SET_TYPE_MODE (variants
, BLKmode
);
1748 TREE_ADDRESSABLE (variants
) = 1;
1753 /* Issue warnings about T having private constructors, but no friends,
1756 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1757 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1758 non-private static member functions. */
1761 maybe_warn_about_overly_private_class (tree t
)
1763 int has_member_fn
= 0;
1764 int has_nonprivate_method
= 0;
1767 if (!warn_ctor_dtor_privacy
1768 /* If the class has friends, those entities might create and
1769 access instances, so we should not warn. */
1770 || (CLASSTYPE_FRIEND_CLASSES (t
)
1771 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1772 /* We will have warned when the template was declared; there's
1773 no need to warn on every instantiation. */
1774 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
1775 /* There's no reason to even consider warning about this
1779 /* We only issue one warning, if more than one applies, because
1780 otherwise, on code like:
1783 // Oops - forgot `public:'
1789 we warn several times about essentially the same problem. */
1791 /* Check to see if all (non-constructor, non-destructor) member
1792 functions are private. (Since there are no friends or
1793 non-private statics, we can't ever call any of the private member
1795 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
1796 /* We're not interested in compiler-generated methods; they don't
1797 provide any way to call private members. */
1798 if (!DECL_ARTIFICIAL (fn
))
1800 if (!TREE_PRIVATE (fn
))
1802 if (DECL_STATIC_FUNCTION_P (fn
))
1803 /* A non-private static member function is just like a
1804 friend; it can create and invoke private member
1805 functions, and be accessed without a class
1809 has_nonprivate_method
= 1;
1810 /* Keep searching for a static member function. */
1812 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
1816 if (!has_nonprivate_method
&& has_member_fn
)
1818 /* There are no non-private methods, and there's at least one
1819 private member function that isn't a constructor or
1820 destructor. (If all the private members are
1821 constructors/destructors we want to use the code below that
1822 issues error messages specifically referring to
1823 constructors/destructors.) */
1825 tree binfo
= TYPE_BINFO (t
);
1827 for (i
= 0; i
!= BINFO_N_BASE_BINFOS (binfo
); i
++)
1828 if (BINFO_BASE_ACCESS (binfo
, i
) != access_private_node
)
1830 has_nonprivate_method
= 1;
1833 if (!has_nonprivate_method
)
1835 warning (OPT_Wctor_dtor_privacy
,
1836 "all member functions in class %qT are private", t
);
1841 /* Even if some of the member functions are non-private, the class
1842 won't be useful for much if all the constructors or destructors
1843 are private: such an object can never be created or destroyed. */
1844 fn
= CLASSTYPE_DESTRUCTORS (t
);
1845 if (fn
&& TREE_PRIVATE (fn
))
1847 warning (OPT_Wctor_dtor_privacy
,
1848 "%q#T only defines a private destructor and has no friends",
1853 /* Warn about classes that have private constructors and no friends. */
1854 if (TYPE_HAS_USER_CONSTRUCTOR (t
)
1855 /* Implicitly generated constructors are always public. */
1856 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t
)
1857 || !CLASSTYPE_LAZY_COPY_CTOR (t
)))
1859 int nonprivate_ctor
= 0;
1861 /* If a non-template class does not define a copy
1862 constructor, one is defined for it, enabling it to avoid
1863 this warning. For a template class, this does not
1864 happen, and so we would normally get a warning on:
1866 template <class T> class C { private: C(); };
1868 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1869 complete non-template or fully instantiated classes have this
1871 if (!TYPE_HAS_COPY_CTOR (t
))
1872 nonprivate_ctor
= 1;
1874 for (fn
= CLASSTYPE_CONSTRUCTORS (t
); fn
; fn
= OVL_NEXT (fn
))
1876 tree ctor
= OVL_CURRENT (fn
);
1877 /* Ideally, we wouldn't count copy constructors (or, in
1878 fact, any constructor that takes an argument of the
1879 class type as a parameter) because such things cannot
1880 be used to construct an instance of the class unless
1881 you already have one. But, for now at least, we're
1883 if (! TREE_PRIVATE (ctor
))
1885 nonprivate_ctor
= 1;
1890 if (nonprivate_ctor
== 0)
1892 warning (OPT_Wctor_dtor_privacy
,
1893 "%q#T only defines private constructors and has no friends",
1901 gt_pointer_operator new_value
;
1905 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1908 method_name_cmp (const void* m1_p
, const void* m2_p
)
1910 const tree
*const m1
= (const tree
*) m1_p
;
1911 const tree
*const m2
= (const tree
*) m2_p
;
1913 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
1915 if (*m1
== NULL_TREE
)
1917 if (*m2
== NULL_TREE
)
1919 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
1924 /* This routine compares two fields like method_name_cmp but using the
1925 pointer operator in resort_field_decl_data. */
1928 resort_method_name_cmp (const void* m1_p
, const void* m2_p
)
1930 const tree
*const m1
= (const tree
*) m1_p
;
1931 const tree
*const m2
= (const tree
*) m2_p
;
1932 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
1934 if (*m1
== NULL_TREE
)
1936 if (*m2
== NULL_TREE
)
1939 tree d1
= DECL_NAME (OVL_CURRENT (*m1
));
1940 tree d2
= DECL_NAME (OVL_CURRENT (*m2
));
1941 resort_data
.new_value (&d1
, resort_data
.cookie
);
1942 resort_data
.new_value (&d2
, resort_data
.cookie
);
1949 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1952 resort_type_method_vec (void* obj
,
1954 gt_pointer_operator new_value
,
1957 vec
<tree
, va_gc
> *method_vec
= (vec
<tree
, va_gc
> *) obj
;
1958 int len
= vec_safe_length (method_vec
);
1962 /* The type conversion ops have to live at the front of the vec, so we
1964 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1965 vec_safe_iterate (method_vec
, slot
, &fn
);
1967 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
1972 resort_data
.new_value
= new_value
;
1973 resort_data
.cookie
= cookie
;
1974 qsort (method_vec
->address () + slot
, len
- slot
, sizeof (tree
),
1975 resort_method_name_cmp
);
1979 /* Warn about duplicate methods in fn_fields.
1981 Sort methods that are not special (i.e., constructors, destructors,
1982 and type conversion operators) so that we can find them faster in
1986 finish_struct_methods (tree t
)
1989 vec
<tree
, va_gc
> *method_vec
;
1992 method_vec
= CLASSTYPE_METHOD_VEC (t
);
1996 len
= method_vec
->length ();
1998 /* Clear DECL_IN_AGGR_P for all functions. */
1999 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2000 fn_fields
= DECL_CHAIN (fn_fields
))
2001 DECL_IN_AGGR_P (fn_fields
) = 0;
2003 /* Issue warnings about private constructors and such. If there are
2004 no methods, then some public defaults are generated. */
2005 maybe_warn_about_overly_private_class (t
);
2007 /* The type conversion ops have to live at the front of the vec, so we
2009 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2010 method_vec
->iterate (slot
, &fn_fields
);
2012 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields
)))
2015 qsort (method_vec
->address () + slot
,
2016 len
-slot
, sizeof (tree
), method_name_cmp
);
2019 /* Make BINFO's vtable have N entries, including RTTI entries,
2020 vbase and vcall offsets, etc. Set its type and call the back end
2024 layout_vtable_decl (tree binfo
, int n
)
2029 atype
= build_array_of_n_type (vtable_entry_type
, n
);
2030 layout_type (atype
);
2032 /* We may have to grow the vtable. */
2033 vtable
= get_vtbl_decl_for_binfo (binfo
);
2034 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2036 TREE_TYPE (vtable
) = atype
;
2037 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2038 layout_decl (vtable
, 0);
2042 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2043 have the same signature. */
2046 same_signature_p (const_tree fndecl
, const_tree base_fndecl
)
2048 /* One destructor overrides another if they are the same kind of
2050 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2051 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2053 /* But a non-destructor never overrides a destructor, nor vice
2054 versa, nor do different kinds of destructors override
2055 one-another. For example, a complete object destructor does not
2056 override a deleting destructor. */
2057 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2060 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
)
2061 || (DECL_CONV_FN_P (fndecl
)
2062 && DECL_CONV_FN_P (base_fndecl
)
2063 && same_type_p (DECL_CONV_FN_TYPE (fndecl
),
2064 DECL_CONV_FN_TYPE (base_fndecl
))))
2066 tree types
, base_types
;
2067 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2068 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2069 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types
)))
2070 == cp_type_quals (TREE_TYPE (TREE_VALUE (types
))))
2071 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2077 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2081 base_derived_from (tree derived
, tree base
)
2085 for (probe
= base
; probe
; probe
= BINFO_INHERITANCE_CHAIN (probe
))
2087 if (probe
== derived
)
2089 else if (BINFO_VIRTUAL_P (probe
))
2090 /* If we meet a virtual base, we can't follow the inheritance
2091 any more. See if the complete type of DERIVED contains
2092 such a virtual base. */
2093 return (binfo_for_vbase (BINFO_TYPE (probe
), BINFO_TYPE (derived
))
2099 typedef struct find_final_overrider_data_s
{
2100 /* The function for which we are trying to find a final overrider. */
2102 /* The base class in which the function was declared. */
2103 tree declaring_base
;
2104 /* The candidate overriders. */
2106 /* Path to most derived. */
2108 } find_final_overrider_data
;
2110 /* Add the overrider along the current path to FFOD->CANDIDATES.
2111 Returns true if an overrider was found; false otherwise. */
2114 dfs_find_final_overrider_1 (tree binfo
,
2115 find_final_overrider_data
*ffod
,
2120 /* If BINFO is not the most derived type, try a more derived class.
2121 A definition there will overrider a definition here. */
2125 if (dfs_find_final_overrider_1
2126 (ffod
->path
[depth
], ffod
, depth
))
2130 method
= look_for_overrides_here (BINFO_TYPE (binfo
), ffod
->fn
);
2133 tree
*candidate
= &ffod
->candidates
;
2135 /* Remove any candidates overridden by this new function. */
2138 /* If *CANDIDATE overrides METHOD, then METHOD
2139 cannot override anything else on the list. */
2140 if (base_derived_from (TREE_VALUE (*candidate
), binfo
))
2142 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2143 if (base_derived_from (binfo
, TREE_VALUE (*candidate
)))
2144 *candidate
= TREE_CHAIN (*candidate
);
2146 candidate
= &TREE_CHAIN (*candidate
);
2149 /* Add the new function. */
2150 ffod
->candidates
= tree_cons (method
, binfo
, ffod
->candidates
);
2157 /* Called from find_final_overrider via dfs_walk. */
2160 dfs_find_final_overrider_pre (tree binfo
, void *data
)
2162 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2164 if (binfo
== ffod
->declaring_base
)
2165 dfs_find_final_overrider_1 (binfo
, ffod
, ffod
->path
.length ());
2166 ffod
->path
.safe_push (binfo
);
2172 dfs_find_final_overrider_post (tree
/*binfo*/, void *data
)
2174 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2180 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2181 FN and whose TREE_VALUE is the binfo for the base where the
2182 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2183 DERIVED) is the base object in which FN is declared. */
2186 find_final_overrider (tree derived
, tree binfo
, tree fn
)
2188 find_final_overrider_data ffod
;
2190 /* Getting this right is a little tricky. This is valid:
2192 struct S { virtual void f (); };
2193 struct T { virtual void f (); };
2194 struct U : public S, public T { };
2196 even though calling `f' in `U' is ambiguous. But,
2198 struct R { virtual void f(); };
2199 struct S : virtual public R { virtual void f (); };
2200 struct T : virtual public R { virtual void f (); };
2201 struct U : public S, public T { };
2203 is not -- there's no way to decide whether to put `S::f' or
2204 `T::f' in the vtable for `R'.
2206 The solution is to look at all paths to BINFO. If we find
2207 different overriders along any two, then there is a problem. */
2208 if (DECL_THUNK_P (fn
))
2209 fn
= THUNK_TARGET (fn
);
2211 /* Determine the depth of the hierarchy. */
2213 ffod
.declaring_base
= binfo
;
2214 ffod
.candidates
= NULL_TREE
;
2215 ffod
.path
.create (30);
2217 dfs_walk_all (derived
, dfs_find_final_overrider_pre
,
2218 dfs_find_final_overrider_post
, &ffod
);
2220 ffod
.path
.release ();
2222 /* If there was no winner, issue an error message. */
2223 if (!ffod
.candidates
|| TREE_CHAIN (ffod
.candidates
))
2224 return error_mark_node
;
2226 return ffod
.candidates
;
2229 /* Return the index of the vcall offset for FN when TYPE is used as a
2233 get_vcall_index (tree fn
, tree type
)
2235 vec
<tree_pair_s
, va_gc
> *indices
= CLASSTYPE_VCALL_INDICES (type
);
2239 FOR_EACH_VEC_SAFE_ELT (indices
, ix
, p
)
2240 if ((DECL_DESTRUCTOR_P (fn
) && DECL_DESTRUCTOR_P (p
->purpose
))
2241 || same_signature_p (fn
, p
->purpose
))
2244 /* There should always be an appropriate index. */
2248 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2249 dominated by T. FN is the old function; VIRTUALS points to the
2250 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2251 of that entry in the list. */
2254 update_vtable_entry_for_fn (tree t
, tree binfo
, tree fn
, tree
* virtuals
,
2262 tree overrider_fn
, overrider_target
;
2263 tree target_fn
= DECL_THUNK_P (fn
) ? THUNK_TARGET (fn
) : fn
;
2264 tree over_return
, base_return
;
2267 /* Find the nearest primary base (possibly binfo itself) which defines
2268 this function; this is the class the caller will convert to when
2269 calling FN through BINFO. */
2270 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2273 if (look_for_overrides_here (BINFO_TYPE (b
), target_fn
))
2276 /* The nearest definition is from a lost primary. */
2277 if (BINFO_LOST_PRIMARY_P (b
))
2282 /* Find the final overrider. */
2283 overrider
= find_final_overrider (TYPE_BINFO (t
), b
, target_fn
);
2284 if (overrider
== error_mark_node
)
2286 error ("no unique final overrider for %qD in %qT", target_fn
, t
);
2289 overrider_target
= overrider_fn
= TREE_PURPOSE (overrider
);
2291 /* Check for adjusting covariant return types. */
2292 over_return
= TREE_TYPE (TREE_TYPE (overrider_target
));
2293 base_return
= TREE_TYPE (TREE_TYPE (target_fn
));
2295 if (POINTER_TYPE_P (over_return
)
2296 && TREE_CODE (over_return
) == TREE_CODE (base_return
)
2297 && CLASS_TYPE_P (TREE_TYPE (over_return
))
2298 && CLASS_TYPE_P (TREE_TYPE (base_return
))
2299 /* If the overrider is invalid, don't even try. */
2300 && !DECL_INVALID_OVERRIDER_P (overrider_target
))
2302 /* If FN is a covariant thunk, we must figure out the adjustment
2303 to the final base FN was converting to. As OVERRIDER_TARGET might
2304 also be converting to the return type of FN, we have to
2305 combine the two conversions here. */
2306 tree fixed_offset
, virtual_offset
;
2308 over_return
= TREE_TYPE (over_return
);
2309 base_return
= TREE_TYPE (base_return
);
2311 if (DECL_THUNK_P (fn
))
2313 gcc_assert (DECL_RESULT_THUNK_P (fn
));
2314 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn
));
2315 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn
);
2318 fixed_offset
= virtual_offset
= NULL_TREE
;
2321 /* Find the equivalent binfo within the return type of the
2322 overriding function. We will want the vbase offset from
2324 virtual_offset
= binfo_for_vbase (BINFO_TYPE (virtual_offset
),
2326 else if (!same_type_ignoring_top_level_qualifiers_p
2327 (over_return
, base_return
))
2329 /* There was no existing virtual thunk (which takes
2330 precedence). So find the binfo of the base function's
2331 return type within the overriding function's return type.
2332 We cannot call lookup base here, because we're inside a
2333 dfs_walk, and will therefore clobber the BINFO_MARKED
2334 flags. Fortunately we know the covariancy is valid (it
2335 has already been checked), so we can just iterate along
2336 the binfos, which have been chained in inheritance graph
2337 order. Of course it is lame that we have to repeat the
2338 search here anyway -- we should really be caching pieces
2339 of the vtable and avoiding this repeated work. */
2340 tree thunk_binfo
, base_binfo
;
2342 /* Find the base binfo within the overriding function's
2343 return type. We will always find a thunk_binfo, except
2344 when the covariancy is invalid (which we will have
2345 already diagnosed). */
2346 for (base_binfo
= TYPE_BINFO (base_return
),
2347 thunk_binfo
= TYPE_BINFO (over_return
);
2349 thunk_binfo
= TREE_CHAIN (thunk_binfo
))
2350 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo
),
2351 BINFO_TYPE (base_binfo
)))
2354 /* See if virtual inheritance is involved. */
2355 for (virtual_offset
= thunk_binfo
;
2357 virtual_offset
= BINFO_INHERITANCE_CHAIN (virtual_offset
))
2358 if (BINFO_VIRTUAL_P (virtual_offset
))
2362 || (thunk_binfo
&& !BINFO_OFFSET_ZEROP (thunk_binfo
)))
2364 tree offset
= convert (ssizetype
, BINFO_OFFSET (thunk_binfo
));
2368 /* We convert via virtual base. Adjust the fixed
2369 offset to be from there. */
2371 size_diffop (offset
,
2373 BINFO_OFFSET (virtual_offset
)));
2376 /* There was an existing fixed offset, this must be
2377 from the base just converted to, and the base the
2378 FN was thunking to. */
2379 fixed_offset
= size_binop (PLUS_EXPR
, fixed_offset
, offset
);
2381 fixed_offset
= offset
;
2385 if (fixed_offset
|| virtual_offset
)
2386 /* Replace the overriding function with a covariant thunk. We
2387 will emit the overriding function in its own slot as
2389 overrider_fn
= make_thunk (overrider_target
, /*this_adjusting=*/0,
2390 fixed_offset
, virtual_offset
);
2393 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target
) ||
2394 !DECL_THUNK_P (fn
));
2396 /* If we need a covariant thunk, then we may need to adjust first_defn.
2397 The ABI specifies that the thunks emitted with a function are
2398 determined by which bases the function overrides, so we need to be
2399 sure that we're using a thunk for some overridden base; even if we
2400 know that the necessary this adjustment is zero, there may not be an
2401 appropriate zero-this-adjusment thunk for us to use since thunks for
2402 overriding virtual bases always use the vcall offset.
2404 Furthermore, just choosing any base that overrides this function isn't
2405 quite right, as this slot won't be used for calls through a type that
2406 puts a covariant thunk here. Calling the function through such a type
2407 will use a different slot, and that slot is the one that determines
2408 the thunk emitted for that base.
2410 So, keep looking until we find the base that we're really overriding
2411 in this slot: the nearest primary base that doesn't use a covariant
2412 thunk in this slot. */
2413 if (overrider_target
!= overrider_fn
)
2415 if (BINFO_TYPE (b
) == DECL_CONTEXT (overrider_target
))
2416 /* We already know that the overrider needs a covariant thunk. */
2417 b
= get_primary_binfo (b
);
2418 for (; ; b
= get_primary_binfo (b
))
2420 tree main_binfo
= TYPE_BINFO (BINFO_TYPE (b
));
2421 tree bv
= chain_index (ix
, BINFO_VIRTUALS (main_binfo
));
2422 if (!DECL_THUNK_P (TREE_VALUE (bv
)))
2424 if (BINFO_LOST_PRIMARY_P (b
))
2430 /* Assume that we will produce a thunk that convert all the way to
2431 the final overrider, and not to an intermediate virtual base. */
2432 virtual_base
= NULL_TREE
;
2434 /* See if we can convert to an intermediate virtual base first, and then
2435 use the vcall offset located there to finish the conversion. */
2436 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2438 /* If we find the final overrider, then we can stop
2440 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b
),
2441 BINFO_TYPE (TREE_VALUE (overrider
))))
2444 /* If we find a virtual base, and we haven't yet found the
2445 overrider, then there is a virtual base between the
2446 declaring base (first_defn) and the final overrider. */
2447 if (BINFO_VIRTUAL_P (b
))
2454 /* Compute the constant adjustment to the `this' pointer. The
2455 `this' pointer, when this function is called, will point at BINFO
2456 (or one of its primary bases, which are at the same offset). */
2458 /* The `this' pointer needs to be adjusted from the declaration to
2459 the nearest virtual base. */
2460 delta
= size_diffop_loc (input_location
,
2461 convert (ssizetype
, BINFO_OFFSET (virtual_base
)),
2462 convert (ssizetype
, BINFO_OFFSET (first_defn
)));
2464 /* If the nearest definition is in a lost primary, we don't need an
2465 entry in our vtable. Except possibly in a constructor vtable,
2466 if we happen to get our primary back. In that case, the offset
2467 will be zero, as it will be a primary base. */
2468 delta
= size_zero_node
;
2470 /* The `this' pointer needs to be adjusted from pointing to
2471 BINFO to pointing at the base where the final overrider
2473 delta
= size_diffop_loc (input_location
,
2475 BINFO_OFFSET (TREE_VALUE (overrider
))),
2476 convert (ssizetype
, BINFO_OFFSET (binfo
)));
2478 modify_vtable_entry (t
, binfo
, overrider_fn
, delta
, virtuals
);
2481 BV_VCALL_INDEX (*virtuals
)
2482 = get_vcall_index (overrider_target
, BINFO_TYPE (virtual_base
));
2484 BV_VCALL_INDEX (*virtuals
) = NULL_TREE
;
2486 BV_LOST_PRIMARY (*virtuals
) = lost
;
2489 /* Called from modify_all_vtables via dfs_walk. */
2492 dfs_modify_vtables (tree binfo
, void* data
)
2494 tree t
= (tree
) data
;
2499 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2500 /* A base without a vtable needs no modification, and its bases
2501 are uninteresting. */
2502 return dfs_skip_bases
;
2504 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
)
2505 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2506 /* Don't do the primary vtable, if it's new. */
2509 if (BINFO_PRIMARY_P (binfo
) && !BINFO_VIRTUAL_P (binfo
))
2510 /* There's no need to modify the vtable for a non-virtual primary
2511 base; we're not going to use that vtable anyhow. We do still
2512 need to do this for virtual primary bases, as they could become
2513 non-primary in a construction vtable. */
2516 make_new_vtable (t
, binfo
);
2518 /* Now, go through each of the virtual functions in the virtual
2519 function table for BINFO. Find the final overrider, and update
2520 the BINFO_VIRTUALS list appropriately. */
2521 for (ix
= 0, virtuals
= BINFO_VIRTUALS (binfo
),
2522 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2524 ix
++, virtuals
= TREE_CHAIN (virtuals
),
2525 old_virtuals
= TREE_CHAIN (old_virtuals
))
2526 update_vtable_entry_for_fn (t
,
2528 BV_FN (old_virtuals
),
2534 /* Update all of the primary and secondary vtables for T. Create new
2535 vtables as required, and initialize their RTTI information. Each
2536 of the functions in VIRTUALS is declared in T and may override a
2537 virtual function from a base class; find and modify the appropriate
2538 entries to point to the overriding functions. Returns a list, in
2539 declaration order, of the virtual functions that are declared in T,
2540 but do not appear in the primary base class vtable, and which
2541 should therefore be appended to the end of the vtable for T. */
2544 modify_all_vtables (tree t
, tree virtuals
)
2546 tree binfo
= TYPE_BINFO (t
);
2549 /* Update all of the vtables. */
2550 dfs_walk_once (binfo
, dfs_modify_vtables
, NULL
, t
);
2552 /* Add virtual functions not already in our primary vtable. These
2553 will be both those introduced by this class, and those overridden
2554 from secondary bases. It does not include virtuals merely
2555 inherited from secondary bases. */
2556 for (fnsp
= &virtuals
; *fnsp
; )
2558 tree fn
= TREE_VALUE (*fnsp
);
2560 if (!value_member (fn
, BINFO_VIRTUALS (binfo
))
2561 || DECL_VINDEX (fn
) == error_mark_node
)
2563 /* We don't need to adjust the `this' pointer when
2564 calling this function. */
2565 BV_DELTA (*fnsp
) = integer_zero_node
;
2566 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2568 /* This is a function not already in our vtable. Keep it. */
2569 fnsp
= &TREE_CHAIN (*fnsp
);
2572 /* We've already got an entry for this function. Skip it. */
2573 *fnsp
= TREE_CHAIN (*fnsp
);
2579 /* Get the base virtual function declarations in T that have the
2583 get_basefndecls (tree name
, tree t
)
2586 tree base_fndecls
= NULL_TREE
;
2587 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
2590 /* Find virtual functions in T with the indicated NAME. */
2591 i
= lookup_fnfields_1 (t
, name
);
2593 for (methods
= (*CLASSTYPE_METHOD_VEC (t
))[i
];
2595 methods
= OVL_NEXT (methods
))
2597 tree method
= OVL_CURRENT (methods
);
2599 if (TREE_CODE (method
) == FUNCTION_DECL
2600 && DECL_VINDEX (method
))
2601 base_fndecls
= tree_cons (NULL_TREE
, method
, base_fndecls
);
2605 return base_fndecls
;
2607 for (i
= 0; i
< n_baseclasses
; i
++)
2609 tree basetype
= BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t
), i
));
2610 base_fndecls
= chainon (get_basefndecls (name
, basetype
),
2614 return base_fndecls
;
2617 /* If this declaration supersedes the declaration of
2618 a method declared virtual in the base class, then
2619 mark this field as being virtual as well. */
2622 check_for_override (tree decl
, tree ctype
)
2624 bool overrides_found
= false;
2625 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2626 /* In [temp.mem] we have:
2628 A specialization of a member function template does not
2629 override a virtual function from a base class. */
2631 if ((DECL_DESTRUCTOR_P (decl
)
2632 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
))
2633 || DECL_CONV_FN_P (decl
))
2634 && look_for_overrides (ctype
, decl
)
2635 && !DECL_STATIC_FUNCTION_P (decl
))
2636 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2637 the error_mark_node so that we know it is an overriding
2640 DECL_VINDEX (decl
) = decl
;
2641 overrides_found
= true;
2644 if (DECL_VIRTUAL_P (decl
))
2646 if (!DECL_VINDEX (decl
))
2647 DECL_VINDEX (decl
) = error_mark_node
;
2648 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2649 if (DECL_DESTRUCTOR_P (decl
))
2650 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
2652 else if (DECL_FINAL_P (decl
))
2653 error ("%q+#D marked final, but is not virtual", decl
);
2654 if (DECL_OVERRIDE_P (decl
) && !overrides_found
)
2655 error ("%q+#D marked override, but does not override", decl
);
2658 /* Warn about hidden virtual functions that are not overridden in t.
2659 We know that constructors and destructors don't apply. */
2662 warn_hidden (tree t
)
2664 vec
<tree
, va_gc
> *method_vec
= CLASSTYPE_METHOD_VEC (t
);
2668 /* We go through each separately named virtual function. */
2669 for (i
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2670 vec_safe_iterate (method_vec
, i
, &fns
);
2681 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2682 have the same name. Figure out what name that is. */
2683 name
= DECL_NAME (OVL_CURRENT (fns
));
2684 /* There are no possibly hidden functions yet. */
2685 base_fndecls
= NULL_TREE
;
2686 /* Iterate through all of the base classes looking for possibly
2687 hidden functions. */
2688 for (binfo
= TYPE_BINFO (t
), j
= 0;
2689 BINFO_BASE_ITERATE (binfo
, j
, base_binfo
); j
++)
2691 tree basetype
= BINFO_TYPE (base_binfo
);
2692 base_fndecls
= chainon (get_basefndecls (name
, basetype
),
2696 /* If there are no functions to hide, continue. */
2700 /* Remove any overridden functions. */
2701 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
2703 fndecl
= OVL_CURRENT (fn
);
2704 if (DECL_VINDEX (fndecl
))
2706 tree
*prev
= &base_fndecls
;
2709 /* If the method from the base class has the same
2710 signature as the method from the derived class, it
2711 has been overridden. */
2712 if (same_signature_p (fndecl
, TREE_VALUE (*prev
)))
2713 *prev
= TREE_CHAIN (*prev
);
2715 prev
= &TREE_CHAIN (*prev
);
2719 /* Now give a warning for all base functions without overriders,
2720 as they are hidden. */
2721 while (base_fndecls
)
2723 /* Here we know it is a hider, and no overrider exists. */
2724 warning (OPT_Woverloaded_virtual
, "%q+D was hidden", TREE_VALUE (base_fndecls
));
2725 warning (OPT_Woverloaded_virtual
, " by %q+D", fns
);
2726 base_fndecls
= TREE_CHAIN (base_fndecls
);
2731 /* Check for things that are invalid. There are probably plenty of other
2732 things we should check for also. */
2735 finish_struct_anon (tree t
)
2739 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2741 if (TREE_STATIC (field
))
2743 if (TREE_CODE (field
) != FIELD_DECL
)
2746 if (DECL_NAME (field
) == NULL_TREE
2747 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2749 bool is_union
= TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
;
2750 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
2751 for (; elt
; elt
= DECL_CHAIN (elt
))
2753 /* We're generally only interested in entities the user
2754 declared, but we also find nested classes by noticing
2755 the TYPE_DECL that we create implicitly. You're
2756 allowed to put one anonymous union inside another,
2757 though, so we explicitly tolerate that. We use
2758 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2759 we also allow unnamed types used for defining fields. */
2760 if (DECL_ARTIFICIAL (elt
)
2761 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2762 || TYPE_ANONYMOUS_P (TREE_TYPE (elt
))))
2765 if (TREE_CODE (elt
) != FIELD_DECL
)
2768 permerror (input_location
, "%q+#D invalid; an anonymous union can "
2769 "only have non-static data members", elt
);
2771 permerror (input_location
, "%q+#D invalid; an anonymous struct can "
2772 "only have non-static data members", elt
);
2776 if (TREE_PRIVATE (elt
))
2779 permerror (input_location
, "private member %q+#D in anonymous union", elt
);
2781 permerror (input_location
, "private member %q+#D in anonymous struct", elt
);
2783 else if (TREE_PROTECTED (elt
))
2786 permerror (input_location
, "protected member %q+#D in anonymous union", elt
);
2788 permerror (input_location
, "protected member %q+#D in anonymous struct", elt
);
2791 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
2792 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
2798 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2799 will be used later during class template instantiation.
2800 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2801 a non-static member data (FIELD_DECL), a member function
2802 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2803 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2804 When FRIEND_P is nonzero, T is either a friend class
2805 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2806 (FUNCTION_DECL, TEMPLATE_DECL). */
2809 maybe_add_class_template_decl_list (tree type
, tree t
, int friend_p
)
2811 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2812 if (CLASSTYPE_TEMPLATE_INFO (type
))
2813 CLASSTYPE_DECL_LIST (type
)
2814 = tree_cons (friend_p
? NULL_TREE
: type
,
2815 t
, CLASSTYPE_DECL_LIST (type
));
2818 /* This function is called from declare_virt_assop_and_dtor via
2821 DATA is a type that direcly or indirectly inherits the base
2822 represented by BINFO. If BINFO contains a virtual assignment [copy
2823 assignment or move assigment] operator or a virtual constructor,
2824 declare that function in DATA if it hasn't been already declared. */
2827 dfs_declare_virt_assop_and_dtor (tree binfo
, void *data
)
2829 tree bv
, fn
, t
= (tree
)data
;
2830 tree opname
= ansi_assopname (NOP_EXPR
);
2832 gcc_assert (t
&& CLASS_TYPE_P (t
));
2833 gcc_assert (binfo
&& TREE_CODE (binfo
) == TREE_BINFO
);
2835 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
2836 /* A base without a vtable needs no modification, and its bases
2837 are uninteresting. */
2838 return dfs_skip_bases
;
2840 if (BINFO_PRIMARY_P (binfo
))
2841 /* If this is a primary base, then we have already looked at the
2842 virtual functions of its vtable. */
2845 for (bv
= BINFO_VIRTUALS (binfo
); bv
; bv
= TREE_CHAIN (bv
))
2849 if (DECL_NAME (fn
) == opname
)
2851 if (CLASSTYPE_LAZY_COPY_ASSIGN (t
))
2852 lazily_declare_fn (sfk_copy_assignment
, t
);
2853 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
2854 lazily_declare_fn (sfk_move_assignment
, t
);
2856 else if (DECL_DESTRUCTOR_P (fn
)
2857 && CLASSTYPE_LAZY_DESTRUCTOR (t
))
2858 lazily_declare_fn (sfk_destructor
, t
);
2864 /* If the class type T has a direct or indirect base that contains a
2865 virtual assignment operator or a virtual destructor, declare that
2866 function in T if it hasn't been already declared. */
2869 declare_virt_assop_and_dtor (tree t
)
2871 if (!(TYPE_POLYMORPHIC_P (t
)
2872 && (CLASSTYPE_LAZY_COPY_ASSIGN (t
)
2873 || CLASSTYPE_LAZY_MOVE_ASSIGN (t
)
2874 || CLASSTYPE_LAZY_DESTRUCTOR (t
))))
2877 dfs_walk_all (TYPE_BINFO (t
),
2878 dfs_declare_virt_assop_and_dtor
,
2882 /* Declare the inheriting constructor for class T inherited from base
2883 constructor CTOR with the parameter array PARMS of size NPARMS. */
2886 one_inheriting_sig (tree t
, tree ctor
, tree
*parms
, int nparms
)
2888 /* We don't declare an inheriting ctor that would be a default,
2889 copy or move ctor for derived or base. */
2893 && TREE_CODE (parms
[0]) == REFERENCE_TYPE
)
2895 tree parm
= TYPE_MAIN_VARIANT (TREE_TYPE (parms
[0]));
2896 if (parm
== t
|| parm
== DECL_CONTEXT (ctor
))
2900 tree parmlist
= void_list_node
;
2901 for (int i
= nparms
- 1; i
>= 0; i
--)
2902 parmlist
= tree_cons (NULL_TREE
, parms
[i
], parmlist
);
2903 tree fn
= implicitly_declare_fn (sfk_inheriting_constructor
,
2904 t
, false, ctor
, parmlist
);
2905 if (add_method (t
, fn
, NULL_TREE
))
2907 DECL_CHAIN (fn
) = TYPE_METHODS (t
);
2908 TYPE_METHODS (t
) = fn
;
2912 /* Declare all the inheriting constructors for class T inherited from base
2913 constructor CTOR. */
2916 one_inherited_ctor (tree ctor
, tree t
)
2918 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (ctor
);
2920 tree
*new_parms
= XALLOCAVEC (tree
, list_length (parms
));
2922 for (; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
2924 if (TREE_PURPOSE (parms
))
2925 one_inheriting_sig (t
, ctor
, new_parms
, i
);
2926 new_parms
[i
++] = TREE_VALUE (parms
);
2928 one_inheriting_sig (t
, ctor
, new_parms
, i
);
2929 if (parms
== NULL_TREE
)
2931 warning (OPT_Winherited_variadic_ctor
,
2932 "the ellipsis in %qD is not inherited", ctor
);
2933 inform (DECL_SOURCE_LOCATION (ctor
), "%qD declared here", ctor
);
2937 /* Create default constructors, assignment operators, and so forth for
2938 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2939 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2940 the class cannot have a default constructor, copy constructor
2941 taking a const reference argument, or an assignment operator taking
2942 a const reference, respectively. */
2945 add_implicitly_declared_members (tree t
, tree
* access_decls
,
2946 int cant_have_const_cctor
,
2947 int cant_have_const_assignment
)
2949 bool move_ok
= false;
2951 if (cxx_dialect
>= cxx0x
&& !CLASSTYPE_DESTRUCTORS (t
)
2952 && !TYPE_HAS_COPY_CTOR (t
) && !TYPE_HAS_COPY_ASSIGN (t
)
2953 && !type_has_move_constructor (t
) && !type_has_move_assign (t
))
2957 if (!CLASSTYPE_DESTRUCTORS (t
))
2959 /* In general, we create destructors lazily. */
2960 CLASSTYPE_LAZY_DESTRUCTOR (t
) = 1;
2962 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
2963 && TYPE_FOR_JAVA (t
))
2964 /* But if this is a Java class, any non-trivial destructor is
2965 invalid, even if compiler-generated. Therefore, if the
2966 destructor is non-trivial we create it now. */
2967 lazily_declare_fn (sfk_destructor
, t
);
2972 If there is no user-declared constructor for a class, a default
2973 constructor is implicitly declared. */
2974 if (! TYPE_HAS_USER_CONSTRUCTOR (t
))
2976 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 1;
2977 CLASSTYPE_LAZY_DEFAULT_CTOR (t
) = 1;
2978 if (cxx_dialect
>= cxx0x
)
2979 TYPE_HAS_CONSTEXPR_CTOR (t
)
2980 /* This might force the declaration. */
2981 = type_has_constexpr_default_constructor (t
);
2986 If a class definition does not explicitly declare a copy
2987 constructor, one is declared implicitly. */
2988 if (! TYPE_HAS_COPY_CTOR (t
) && ! TYPE_FOR_JAVA (t
))
2990 TYPE_HAS_COPY_CTOR (t
) = 1;
2991 TYPE_HAS_CONST_COPY_CTOR (t
) = !cant_have_const_cctor
;
2992 CLASSTYPE_LAZY_COPY_CTOR (t
) = 1;
2994 CLASSTYPE_LAZY_MOVE_CTOR (t
) = 1;
2997 /* If there is no assignment operator, one will be created if and
2998 when it is needed. For now, just record whether or not the type
2999 of the parameter to the assignment operator will be a const or
3000 non-const reference. */
3001 if (!TYPE_HAS_COPY_ASSIGN (t
) && !TYPE_FOR_JAVA (t
))
3003 TYPE_HAS_COPY_ASSIGN (t
) = 1;
3004 TYPE_HAS_CONST_COPY_ASSIGN (t
) = !cant_have_const_assignment
;
3005 CLASSTYPE_LAZY_COPY_ASSIGN (t
) = 1;
3007 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 1;
3010 /* We can't be lazy about declaring functions that might override
3011 a virtual function from a base class. */
3012 declare_virt_assop_and_dtor (t
);
3014 while (*access_decls
)
3016 tree using_decl
= TREE_VALUE (*access_decls
);
3017 tree decl
= USING_DECL_DECLS (using_decl
);
3018 if (DECL_SELF_REFERENCE_P (decl
))
3020 /* declare, then remove the decl */
3021 tree ctor_list
= lookup_fnfields_slot (TREE_TYPE (decl
),
3023 location_t loc
= input_location
;
3024 input_location
= DECL_SOURCE_LOCATION (using_decl
);
3026 for (; ctor_list
; ctor_list
= OVL_NEXT (ctor_list
))
3027 one_inherited_ctor (OVL_CURRENT (ctor_list
), t
);
3028 *access_decls
= TREE_CHAIN (*access_decls
);
3029 input_location
= loc
;
3032 access_decls
= &TREE_CHAIN (*access_decls
);
3036 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3037 count the number of fields in TYPE, including anonymous union
3041 count_fields (tree fields
)
3045 for (x
= fields
; x
; x
= DECL_CHAIN (x
))
3047 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3048 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3055 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3056 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3057 elts, starting at offset IDX. */
3060 add_fields_to_record_type (tree fields
, struct sorted_fields_type
*field_vec
, int idx
)
3063 for (x
= fields
; x
; x
= DECL_CHAIN (x
))
3065 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3066 idx
= add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3068 field_vec
->elts
[idx
++] = x
;
3073 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3074 starting at offset IDX. */
3077 add_enum_fields_to_record_type (tree enumtype
,
3078 struct sorted_fields_type
*field_vec
,
3082 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
3083 field_vec
->elts
[idx
++] = TREE_VALUE (values
);
3087 /* FIELD is a bit-field. We are finishing the processing for its
3088 enclosing type. Issue any appropriate messages and set appropriate
3089 flags. Returns false if an error has been diagnosed. */
3092 check_bitfield_decl (tree field
)
3094 tree type
= TREE_TYPE (field
);
3097 /* Extract the declared width of the bitfield, which has been
3098 temporarily stashed in DECL_INITIAL. */
3099 w
= DECL_INITIAL (field
);
3100 gcc_assert (w
!= NULL_TREE
);
3101 /* Remove the bit-field width indicator so that the rest of the
3102 compiler does not treat that value as an initializer. */
3103 DECL_INITIAL (field
) = NULL_TREE
;
3105 /* Detect invalid bit-field type. */
3106 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3108 error ("bit-field %q+#D with non-integral type", field
);
3109 w
= error_mark_node
;
3113 location_t loc
= input_location
;
3114 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3117 /* detect invalid field size. */
3118 input_location
= DECL_SOURCE_LOCATION (field
);
3119 w
= cxx_constant_value (w
);
3120 input_location
= loc
;
3122 if (TREE_CODE (w
) != INTEGER_CST
)
3124 error ("bit-field %q+D width not an integer constant", field
);
3125 w
= error_mark_node
;
3127 else if (tree_int_cst_sgn (w
) < 0)
3129 error ("negative width in bit-field %q+D", field
);
3130 w
= error_mark_node
;
3132 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3134 error ("zero width for bit-field %q+D", field
);
3135 w
= error_mark_node
;
3137 else if (compare_tree_int (w
, TYPE_PRECISION (type
)) > 0
3138 && TREE_CODE (type
) != ENUMERAL_TYPE
3139 && TREE_CODE (type
) != BOOLEAN_TYPE
)
3140 warning (0, "width of %q+D exceeds its type", field
);
3141 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3142 && (0 > (compare_tree_int
3143 (w
, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type
))))))
3144 warning (0, "%q+D is too small to hold all values of %q#T", field
, type
);
3147 if (w
!= error_mark_node
)
3149 DECL_SIZE (field
) = convert (bitsizetype
, w
);
3150 DECL_BIT_FIELD (field
) = 1;
3155 /* Non-bit-fields are aligned for their type. */
3156 DECL_BIT_FIELD (field
) = 0;
3157 CLEAR_DECL_C_BIT_FIELD (field
);
3162 /* FIELD is a non bit-field. We are finishing the processing for its
3163 enclosing type T. Issue any appropriate messages and set appropriate
3167 check_field_decl (tree field
,
3169 int* cant_have_const_ctor
,
3170 int* no_const_asn_ref
,
3171 int* any_default_members
)
3173 tree type
= strip_array_types (TREE_TYPE (field
));
3175 /* In C++98 an anonymous union cannot contain any fields which would change
3176 the settings of CANT_HAVE_CONST_CTOR and friends. */
3177 if (ANON_UNION_TYPE_P (type
) && cxx_dialect
< cxx0x
)
3179 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3180 structs. So, we recurse through their fields here. */
3181 else if (ANON_AGGR_TYPE_P (type
))
3185 for (fields
= TYPE_FIELDS (type
); fields
; fields
= DECL_CHAIN (fields
))
3186 if (TREE_CODE (fields
) == FIELD_DECL
&& !DECL_C_BIT_FIELD (field
))
3187 check_field_decl (fields
, t
, cant_have_const_ctor
,
3188 no_const_asn_ref
, any_default_members
);
3190 /* Check members with class type for constructors, destructors,
3192 else if (CLASS_TYPE_P (type
))
3194 /* Never let anything with uninheritable virtuals
3195 make it through without complaint. */
3196 abstract_virtuals_error (field
, type
);
3198 if (TREE_CODE (t
) == UNION_TYPE
&& cxx_dialect
< cxx0x
)
3201 int oldcount
= errorcount
;
3202 if (TYPE_NEEDS_CONSTRUCTING (type
))
3203 error ("member %q+#D with constructor not allowed in union",
3205 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3206 error ("member %q+#D with destructor not allowed in union", field
);
3207 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
3208 error ("member %q+#D with copy assignment operator not allowed in union",
3210 if (!warned
&& errorcount
> oldcount
)
3212 inform (DECL_SOURCE_LOCATION (field
), "unrestricted unions "
3213 "only available with -std=c++11 or -std=gnu++11");
3219 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3220 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3221 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3222 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
)
3223 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
)
3224 || !TYPE_HAS_COPY_ASSIGN (type
));
3225 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type
)
3226 || !TYPE_HAS_COPY_CTOR (type
));
3227 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type
);
3228 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type
);
3229 TYPE_HAS_COMPLEX_DFLT (t
) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)
3230 || TYPE_HAS_COMPLEX_DFLT (type
));
3233 if (TYPE_HAS_COPY_CTOR (type
)
3234 && !TYPE_HAS_CONST_COPY_CTOR (type
))
3235 *cant_have_const_ctor
= 1;
3237 if (TYPE_HAS_COPY_ASSIGN (type
)
3238 && !TYPE_HAS_CONST_COPY_ASSIGN (type
))
3239 *no_const_asn_ref
= 1;
3242 check_abi_tags (t
, field
);
3244 if (DECL_INITIAL (field
) != NULL_TREE
)
3246 /* `build_class_init_list' does not recognize
3248 if (TREE_CODE (t
) == UNION_TYPE
&& *any_default_members
!= 0)
3249 error ("multiple fields in union %qT initialized", t
);
3250 *any_default_members
= 1;
3254 /* Check the data members (both static and non-static), class-scoped
3255 typedefs, etc., appearing in the declaration of T. Issue
3256 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3257 declaration order) of access declarations; each TREE_VALUE in this
3258 list is a USING_DECL.
3260 In addition, set the following flags:
3263 The class is empty, i.e., contains no non-static data members.
3265 CANT_HAVE_CONST_CTOR_P
3266 This class cannot have an implicitly generated copy constructor
3267 taking a const reference.
3269 CANT_HAVE_CONST_ASN_REF
3270 This class cannot have an implicitly generated assignment
3271 operator taking a const reference.
3273 All of these flags should be initialized before calling this
3276 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3277 fields can be added by adding to this chain. */
3280 check_field_decls (tree t
, tree
*access_decls
,
3281 int *cant_have_const_ctor_p
,
3282 int *no_const_asn_ref_p
)
3287 int any_default_members
;
3289 int field_access
= -1;
3291 /* Assume there are no access declarations. */
3292 *access_decls
= NULL_TREE
;
3293 /* Assume this class has no pointer members. */
3294 has_pointers
= false;
3295 /* Assume none of the members of this class have default
3297 any_default_members
= 0;
3299 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3302 tree type
= TREE_TYPE (x
);
3303 int this_field_access
;
3305 next
= &DECL_CHAIN (x
);
3307 if (TREE_CODE (x
) == USING_DECL
)
3309 /* Save the access declarations for our caller. */
3310 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3314 if (TREE_CODE (x
) == TYPE_DECL
3315 || TREE_CODE (x
) == TEMPLATE_DECL
)
3318 /* If we've gotten this far, it's a data member, possibly static,
3319 or an enumerator. */
3320 if (TREE_CODE (x
) != CONST_DECL
)
3321 DECL_CONTEXT (x
) = t
;
3323 /* When this goes into scope, it will be a non-local reference. */
3324 DECL_NONLOCAL (x
) = 1;
3326 if (TREE_CODE (t
) == UNION_TYPE
)
3330 If a union contains a static data member, or a member of
3331 reference type, the program is ill-formed. */
3332 if (TREE_CODE (x
) == VAR_DECL
)
3334 error ("%q+D may not be static because it is a member of a union", x
);
3337 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3339 error ("%q+D may not have reference type %qT because"
3340 " it is a member of a union",
3346 /* Perform error checking that did not get done in
3348 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3350 error ("field %q+D invalidly declared function type", x
);
3351 type
= build_pointer_type (type
);
3352 TREE_TYPE (x
) = type
;
3354 else if (TREE_CODE (type
) == METHOD_TYPE
)
3356 error ("field %q+D invalidly declared method type", x
);
3357 type
= build_pointer_type (type
);
3358 TREE_TYPE (x
) = type
;
3361 if (type
== error_mark_node
)
3364 if (TREE_CODE (x
) == CONST_DECL
|| TREE_CODE (x
) == VAR_DECL
)
3367 /* Now it can only be a FIELD_DECL. */
3369 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3370 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3372 /* If at least one non-static data member is non-literal, the whole
3373 class becomes non-literal. Note: if the type is incomplete we
3374 will complain later on. */
3375 if (COMPLETE_TYPE_P (type
) && !literal_type_p (type
))
3376 CLASSTYPE_LITERAL_P (t
) = false;
3378 /* A standard-layout class is a class that:
3380 has the same access control (Clause 11) for all non-static data members,
3382 this_field_access
= TREE_PROTECTED (x
) ? 1 : TREE_PRIVATE (x
) ? 2 : 0;
3383 if (field_access
== -1)
3384 field_access
= this_field_access
;
3385 else if (this_field_access
!= field_access
)
3386 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3388 /* If this is of reference type, check if it needs an init. */
3389 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3391 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3392 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3393 if (DECL_INITIAL (x
) == NULL_TREE
)
3394 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3396 /* ARM $12.6.2: [A member initializer list] (or, for an
3397 aggregate, initialization by a brace-enclosed list) is the
3398 only way to initialize nonstatic const and reference
3400 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3401 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3404 type
= strip_array_types (type
);
3406 if (TYPE_PACKED (t
))
3408 if (!layout_pod_type_p (type
) && !TYPE_PACKED (type
))
3412 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3416 else if (DECL_C_BIT_FIELD (x
)
3417 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
)
3418 DECL_PACKED (x
) = 1;
3421 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3422 /* We don't treat zero-width bitfields as making a class
3427 /* The class is non-empty. */
3428 CLASSTYPE_EMPTY_P (t
) = 0;
3429 /* The class is not even nearly empty. */
3430 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3431 /* If one of the data members contains an empty class,
3433 if (CLASS_TYPE_P (type
)
3434 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3435 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
3438 /* This is used by -Weffc++ (see below). Warn only for pointers
3439 to members which might hold dynamic memory. So do not warn
3440 for pointers to functions or pointers to members. */
3441 if (TYPE_PTR_P (type
)
3442 && !TYPE_PTRFN_P (type
))
3443 has_pointers
= true;
3445 if (CLASS_TYPE_P (type
))
3447 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type
))
3448 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t
, 1);
3449 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3450 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3453 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3454 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3456 if (! layout_pod_type_p (type
))
3457 /* DR 148 now allows pointers to members (which are POD themselves),
3458 to be allowed in POD structs. */
3459 CLASSTYPE_NON_LAYOUT_POD_P (t
) = 1;
3461 if (!std_layout_type_p (type
))
3462 CLASSTYPE_NON_STD_LAYOUT (t
) = 1;
3464 if (! zero_init_p (type
))
3465 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3467 /* We set DECL_C_BIT_FIELD in grokbitfield.
3468 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3469 if (! DECL_C_BIT_FIELD (x
) || ! check_bitfield_decl (x
))
3470 check_field_decl (x
, t
,
3471 cant_have_const_ctor_p
,
3473 &any_default_members
);
3475 /* Now that we've removed bit-field widths from DECL_INITIAL,
3476 anything left in DECL_INITIAL is an NSDMI that makes the class
3478 if (DECL_INITIAL (x
))
3479 CLASSTYPE_NON_AGGREGATE (t
) = true;
3481 /* If any field is const, the structure type is pseudo-const. */
3482 if (CP_TYPE_CONST_P (type
))
3484 C_TYPE_FIELDS_READONLY (t
) = 1;
3485 if (DECL_INITIAL (x
) == NULL_TREE
)
3486 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
, 1);
3488 /* ARM $12.6.2: [A member initializer list] (or, for an
3489 aggregate, initialization by a brace-enclosed list) is the
3490 only way to initialize nonstatic const and reference
3492 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
3493 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) = 1;
3495 /* A field that is pseudo-const makes the structure likewise. */
3496 else if (CLASS_TYPE_P (type
))
3498 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3499 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
,
3500 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3501 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
));
3504 /* Core issue 80: A nonstatic data member is required to have a
3505 different name from the class iff the class has a
3506 user-declared constructor. */
3507 if (constructor_name_p (DECL_NAME (x
), t
)
3508 && TYPE_HAS_USER_CONSTRUCTOR (t
))
3509 permerror (input_location
, "field %q+#D with same name as class", x
);
3512 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3513 it should also define a copy constructor and an assignment operator to
3514 implement the correct copy semantic (deep vs shallow, etc.). As it is
3515 not feasible to check whether the constructors do allocate dynamic memory
3516 and store it within members, we approximate the warning like this:
3518 -- Warn only if there are members which are pointers
3519 -- Warn only if there is a non-trivial constructor (otherwise,
3520 there cannot be memory allocated).
3521 -- Warn only if there is a non-trivial destructor. We assume that the
3522 user at least implemented the cleanup correctly, and a destructor
3523 is needed to free dynamic memory.
3525 This seems enough for practical purposes. */
3528 && TYPE_HAS_USER_CONSTRUCTOR (t
)
3529 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3530 && !(TYPE_HAS_COPY_CTOR (t
) && TYPE_HAS_COPY_ASSIGN (t
)))
3532 warning (OPT_Weffc__
, "%q#T has pointer data members", t
);
3534 if (! TYPE_HAS_COPY_CTOR (t
))
3536 warning (OPT_Weffc__
,
3537 " but does not override %<%T(const %T&)%>", t
, t
);
3538 if (!TYPE_HAS_COPY_ASSIGN (t
))
3539 warning (OPT_Weffc__
, " or %<operator=(const %T&)%>", t
);
3541 else if (! TYPE_HAS_COPY_ASSIGN (t
))
3542 warning (OPT_Weffc__
,
3543 " but does not override %<operator=(const %T&)%>", t
);
3546 /* Non-static data member initializers make the default constructor
3548 if (any_default_members
)
3550 TYPE_NEEDS_CONSTRUCTING (t
) = true;
3551 TYPE_HAS_COMPLEX_DFLT (t
) = true;
3554 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3556 TYPE_PACKED (t
) = 0;
3558 /* Check anonymous struct/anonymous union fields. */
3559 finish_struct_anon (t
);
3561 /* We've built up the list of access declarations in reverse order.
3563 *access_decls
= nreverse (*access_decls
);
3566 /* If TYPE is an empty class type, records its OFFSET in the table of
3570 record_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3574 if (!is_empty_class (type
))
3577 /* Record the location of this empty object in OFFSETS. */
3578 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3580 n
= splay_tree_insert (offsets
,
3581 (splay_tree_key
) offset
,
3582 (splay_tree_value
) NULL_TREE
);
3583 n
->value
= ((splay_tree_value
)
3584 tree_cons (NULL_TREE
,
3591 /* Returns nonzero if TYPE is an empty class type and there is
3592 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3595 check_subobject_offset (tree type
, tree offset
, splay_tree offsets
)
3600 if (!is_empty_class (type
))
3603 /* Record the location of this empty object in OFFSETS. */
3604 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3608 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3609 if (same_type_p (TREE_VALUE (t
), type
))
3615 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3616 F for every subobject, passing it the type, offset, and table of
3617 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3620 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3621 than MAX_OFFSET will not be walked.
3623 If F returns a nonzero value, the traversal ceases, and that value
3624 is returned. Otherwise, returns zero. */
3627 walk_subobject_offsets (tree type
,
3628 subobject_offset_fn f
,
3635 tree type_binfo
= NULL_TREE
;
3637 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3639 if (max_offset
&& INT_CST_LT (max_offset
, offset
))
3642 if (type
== error_mark_node
)
3647 if (abi_version_at_least (2))
3649 type
= BINFO_TYPE (type
);
3652 if (CLASS_TYPE_P (type
))
3658 /* Avoid recursing into objects that are not interesting. */
3659 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type
))
3662 /* Record the location of TYPE. */
3663 r
= (*f
) (type
, offset
, offsets
);
3667 /* Iterate through the direct base classes of TYPE. */
3669 type_binfo
= TYPE_BINFO (type
);
3670 for (i
= 0; BINFO_BASE_ITERATE (type_binfo
, i
, binfo
); i
++)
3674 if (abi_version_at_least (2)
3675 && BINFO_VIRTUAL_P (binfo
))
3679 && BINFO_VIRTUAL_P (binfo
)
3680 && !BINFO_PRIMARY_P (binfo
))
3683 if (!abi_version_at_least (2))
3684 binfo_offset
= size_binop (PLUS_EXPR
,
3686 BINFO_OFFSET (binfo
));
3690 /* We cannot rely on BINFO_OFFSET being set for the base
3691 class yet, but the offsets for direct non-virtual
3692 bases can be calculated by going back to the TYPE. */
3693 orig_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
3694 binfo_offset
= size_binop (PLUS_EXPR
,
3696 BINFO_OFFSET (orig_binfo
));
3699 r
= walk_subobject_offsets (binfo
,
3704 (abi_version_at_least (2)
3705 ? /*vbases_p=*/0 : vbases_p
));
3710 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type
))
3713 vec
<tree
, va_gc
> *vbases
;
3715 /* Iterate through the virtual base classes of TYPE. In G++
3716 3.2, we included virtual bases in the direct base class
3717 loop above, which results in incorrect results; the
3718 correct offsets for virtual bases are only known when
3719 working with the most derived type. */
3721 for (vbases
= CLASSTYPE_VBASECLASSES (type
), ix
= 0;
3722 vec_safe_iterate (vbases
, ix
, &binfo
); ix
++)
3724 r
= walk_subobject_offsets (binfo
,
3726 size_binop (PLUS_EXPR
,
3728 BINFO_OFFSET (binfo
)),
3737 /* We still have to walk the primary base, if it is
3738 virtual. (If it is non-virtual, then it was walked
3740 tree vbase
= get_primary_binfo (type_binfo
);
3742 if (vbase
&& BINFO_VIRTUAL_P (vbase
)
3743 && BINFO_PRIMARY_P (vbase
)
3744 && BINFO_INHERITANCE_CHAIN (vbase
) == type_binfo
)
3746 r
= (walk_subobject_offsets
3748 offsets
, max_offset
, /*vbases_p=*/0));
3755 /* Iterate through the fields of TYPE. */
3756 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
3757 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_ARTIFICIAL (field
))
3761 if (abi_version_at_least (2))
3762 field_offset
= byte_position (field
);
3764 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3765 field_offset
= DECL_FIELD_OFFSET (field
);
3767 r
= walk_subobject_offsets (TREE_TYPE (field
),
3769 size_binop (PLUS_EXPR
,
3779 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3781 tree element_type
= strip_array_types (type
);
3782 tree domain
= TYPE_DOMAIN (type
);
3785 /* Avoid recursing into objects that are not interesting. */
3786 if (!CLASS_TYPE_P (element_type
)
3787 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type
))
3790 /* Step through each of the elements in the array. */
3791 for (index
= size_zero_node
;
3792 /* G++ 3.2 had an off-by-one error here. */
3793 (abi_version_at_least (2)
3794 ? !INT_CST_LT (TYPE_MAX_VALUE (domain
), index
)
3795 : INT_CST_LT (index
, TYPE_MAX_VALUE (domain
)));
3796 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
3798 r
= walk_subobject_offsets (TREE_TYPE (type
),
3806 offset
= size_binop (PLUS_EXPR
, offset
,
3807 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
3808 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3809 there's no point in iterating through the remaining
3810 elements of the array. */
3811 if (max_offset
&& INT_CST_LT (max_offset
, offset
))
3819 /* Record all of the empty subobjects of TYPE (either a type or a
3820 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3821 is being placed at OFFSET; otherwise, it is a base class that is
3822 being placed at OFFSET. */
3825 record_subobject_offsets (tree type
,
3828 bool is_data_member
)
3831 /* If recording subobjects for a non-static data member or a
3832 non-empty base class , we do not need to record offsets beyond
3833 the size of the biggest empty class. Additional data members
3834 will go at the end of the class. Additional base classes will go
3835 either at offset zero (if empty, in which case they cannot
3836 overlap with offsets past the size of the biggest empty class) or
3837 at the end of the class.
3839 However, if we are placing an empty base class, then we must record
3840 all offsets, as either the empty class is at offset zero (where
3841 other empty classes might later be placed) or at the end of the
3842 class (where other objects might then be placed, so other empty
3843 subobjects might later overlap). */
3845 || !is_empty_class (BINFO_TYPE (type
)))
3846 max_offset
= sizeof_biggest_empty_class
;
3848 max_offset
= NULL_TREE
;
3849 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
3850 offsets
, max_offset
, is_data_member
);
3853 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3854 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3855 virtual bases of TYPE are examined. */
3858 layout_conflict_p (tree type
,
3863 splay_tree_node max_node
;
3865 /* Get the node in OFFSETS that indicates the maximum offset where
3866 an empty subobject is located. */
3867 max_node
= splay_tree_max (offsets
);
3868 /* If there aren't any empty subobjects, then there's no point in
3869 performing this check. */
3873 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
3874 offsets
, (tree
) (max_node
->key
),
3878 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3879 non-static data member of the type indicated by RLI. BINFO is the
3880 binfo corresponding to the base subobject, OFFSETS maps offsets to
3881 types already located at those offsets. This function determines
3882 the position of the DECL. */
3885 layout_nonempty_base_or_field (record_layout_info rli
,
3890 tree offset
= NULL_TREE
;
3896 /* For the purposes of determining layout conflicts, we want to
3897 use the class type of BINFO; TREE_TYPE (DECL) will be the
3898 CLASSTYPE_AS_BASE version, which does not contain entries for
3899 zero-sized bases. */
3900 type
= TREE_TYPE (binfo
);
3905 type
= TREE_TYPE (decl
);
3909 /* Try to place the field. It may take more than one try if we have
3910 a hard time placing the field without putting two objects of the
3911 same type at the same address. */
3914 struct record_layout_info_s old_rli
= *rli
;
3916 /* Place this field. */
3917 place_field (rli
, decl
);
3918 offset
= byte_position (decl
);
3920 /* We have to check to see whether or not there is already
3921 something of the same type at the offset we're about to use.
3922 For example, consider:
3925 struct T : public S { int i; };
3926 struct U : public S, public T {};
3928 Here, we put S at offset zero in U. Then, we can't put T at
3929 offset zero -- its S component would be at the same address
3930 as the S we already allocated. So, we have to skip ahead.
3931 Since all data members, including those whose type is an
3932 empty class, have nonzero size, any overlap can happen only
3933 with a direct or indirect base-class -- it can't happen with
3935 /* In a union, overlap is permitted; all members are placed at
3937 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
3939 /* G++ 3.2 did not check for overlaps when placing a non-empty
3941 if (!abi_version_at_least (2) && binfo
&& BINFO_VIRTUAL_P (binfo
))
3943 if (layout_conflict_p (field_p
? type
: binfo
, offset
,
3946 /* Strip off the size allocated to this field. That puts us
3947 at the first place we could have put the field with
3948 proper alignment. */
3951 /* Bump up by the alignment required for the type. */
3953 = size_binop (PLUS_EXPR
, rli
->bitpos
,
3955 ? CLASSTYPE_ALIGN (type
)
3956 : TYPE_ALIGN (type
)));
3957 normalize_rli (rli
);
3960 /* There was no conflict. We're done laying out this field. */
3964 /* Now that we know where it will be placed, update its
3966 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
3967 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3968 this point because their BINFO_OFFSET is copied from another
3969 hierarchy. Therefore, we may not need to add the entire
3971 propagate_binfo_offsets (binfo
,
3972 size_diffop_loc (input_location
,
3973 convert (ssizetype
, offset
),
3975 BINFO_OFFSET (binfo
))));
3978 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3981 empty_base_at_nonzero_offset_p (tree type
,
3983 splay_tree
/*offsets*/)
3985 return is_empty_class (type
) && !integer_zerop (offset
);
3988 /* Layout the empty base BINFO. EOC indicates the byte currently just
3989 past the end of the class, and should be correctly aligned for a
3990 class of the type indicated by BINFO; OFFSETS gives the offsets of
3991 the empty bases allocated so far. T is the most derived
3992 type. Return nonzero iff we added it at the end. */
3995 layout_empty_base (record_layout_info rli
, tree binfo
,
3996 tree eoc
, splay_tree offsets
)
3999 tree basetype
= BINFO_TYPE (binfo
);
4002 /* This routine should only be used for empty classes. */
4003 gcc_assert (is_empty_class (basetype
));
4004 alignment
= ssize_int (CLASSTYPE_ALIGN_UNIT (basetype
));
4006 if (!integer_zerop (BINFO_OFFSET (binfo
)))
4008 if (abi_version_at_least (2))
4009 propagate_binfo_offsets
4010 (binfo
, size_diffop_loc (input_location
,
4011 size_zero_node
, BINFO_OFFSET (binfo
)));
4014 "offset of empty base %qT may not be ABI-compliant and may"
4015 "change in a future version of GCC",
4016 BINFO_TYPE (binfo
));
4019 /* This is an empty base class. We first try to put it at offset
4021 if (layout_conflict_p (binfo
,
4022 BINFO_OFFSET (binfo
),
4026 /* That didn't work. Now, we move forward from the next
4027 available spot in the class. */
4029 propagate_binfo_offsets (binfo
, convert (ssizetype
, eoc
));
4032 if (!layout_conflict_p (binfo
,
4033 BINFO_OFFSET (binfo
),
4036 /* We finally found a spot where there's no overlap. */
4039 /* There's overlap here, too. Bump along to the next spot. */
4040 propagate_binfo_offsets (binfo
, alignment
);
4044 if (CLASSTYPE_USER_ALIGN (basetype
))
4046 rli
->record_align
= MAX (rli
->record_align
, CLASSTYPE_ALIGN (basetype
));
4048 rli
->unpacked_align
= MAX (rli
->unpacked_align
, CLASSTYPE_ALIGN (basetype
));
4049 TYPE_USER_ALIGN (rli
->t
) = 1;
4055 /* Layout the base given by BINFO in the class indicated by RLI.
4056 *BASE_ALIGN is a running maximum of the alignments of
4057 any base class. OFFSETS gives the location of empty base
4058 subobjects. T is the most derived type. Return nonzero if the new
4059 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4060 *NEXT_FIELD, unless BINFO is for an empty base class.
4062 Returns the location at which the next field should be inserted. */
4065 build_base_field (record_layout_info rli
, tree binfo
,
4066 splay_tree offsets
, tree
*next_field
)
4069 tree basetype
= BINFO_TYPE (binfo
);
4071 if (!COMPLETE_TYPE_P (basetype
))
4072 /* This error is now reported in xref_tag, thus giving better
4073 location information. */
4076 /* Place the base class. */
4077 if (!is_empty_class (basetype
))
4081 /* The containing class is non-empty because it has a non-empty
4083 CLASSTYPE_EMPTY_P (t
) = 0;
4085 /* Create the FIELD_DECL. */
4086 decl
= build_decl (input_location
,
4087 FIELD_DECL
, NULL_TREE
, CLASSTYPE_AS_BASE (basetype
));
4088 DECL_ARTIFICIAL (decl
) = 1;
4089 DECL_IGNORED_P (decl
) = 1;
4090 DECL_FIELD_CONTEXT (decl
) = t
;
4091 if (CLASSTYPE_AS_BASE (basetype
))
4093 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4094 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4095 DECL_ALIGN (decl
) = CLASSTYPE_ALIGN (basetype
);
4096 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4097 DECL_MODE (decl
) = TYPE_MODE (basetype
);
4098 DECL_FIELD_IS_BASE (decl
) = 1;
4100 /* Try to place the field. It may take more than one try if we
4101 have a hard time placing the field without putting two
4102 objects of the same type at the same address. */
4103 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4104 /* Add the new FIELD_DECL to the list of fields for T. */
4105 DECL_CHAIN (decl
) = *next_field
;
4107 next_field
= &DECL_CHAIN (decl
);
4115 /* On some platforms (ARM), even empty classes will not be
4117 eoc
= round_up_loc (input_location
,
4118 rli_size_unit_so_far (rli
),
4119 CLASSTYPE_ALIGN_UNIT (basetype
));
4120 atend
= layout_empty_base (rli
, binfo
, eoc
, offsets
);
4121 /* A nearly-empty class "has no proper base class that is empty,
4122 not morally virtual, and at an offset other than zero." */
4123 if (!BINFO_VIRTUAL_P (binfo
) && CLASSTYPE_NEARLY_EMPTY_P (t
))
4126 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4127 /* The check above (used in G++ 3.2) is insufficient because
4128 an empty class placed at offset zero might itself have an
4129 empty base at a nonzero offset. */
4130 else if (walk_subobject_offsets (basetype
,
4131 empty_base_at_nonzero_offset_p
,
4134 /*max_offset=*/NULL_TREE
,
4137 if (abi_version_at_least (2))
4138 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4141 "class %qT will be considered nearly empty in a "
4142 "future version of GCC", t
);
4146 /* We do not create a FIELD_DECL for empty base classes because
4147 it might overlap some other field. We want to be able to
4148 create CONSTRUCTORs for the class by iterating over the
4149 FIELD_DECLs, and the back end does not handle overlapping
4152 /* An empty virtual base causes a class to be non-empty
4153 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4154 here because that was already done when the virtual table
4155 pointer was created. */
4158 /* Record the offsets of BINFO and its base subobjects. */
4159 record_subobject_offsets (binfo
,
4160 BINFO_OFFSET (binfo
),
4162 /*is_data_member=*/false);
4167 /* Layout all of the non-virtual base classes. Record empty
4168 subobjects in OFFSETS. T is the most derived type. Return nonzero
4169 if the type cannot be nearly empty. The fields created
4170 corresponding to the base classes will be inserted at
4174 build_base_fields (record_layout_info rli
,
4175 splay_tree offsets
, tree
*next_field
)
4177 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4180 int n_baseclasses
= BINFO_N_BASE_BINFOS (TYPE_BINFO (t
));
4183 /* The primary base class is always allocated first. */
4184 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
4185 next_field
= build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (t
),
4186 offsets
, next_field
);
4188 /* Now allocate the rest of the bases. */
4189 for (i
= 0; i
< n_baseclasses
; ++i
)
4193 base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (t
), i
);
4195 /* The primary base was already allocated above, so we don't
4196 need to allocate it again here. */
4197 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (t
))
4200 /* Virtual bases are added at the end (a primary virtual base
4201 will have already been added). */
4202 if (BINFO_VIRTUAL_P (base_binfo
))
4205 next_field
= build_base_field (rli
, base_binfo
,
4206 offsets
, next_field
);
4210 /* Go through the TYPE_METHODS of T issuing any appropriate
4211 diagnostics, figuring out which methods override which other
4212 methods, and so forth. */
4215 check_methods (tree t
)
4219 for (x
= TYPE_METHODS (t
); x
; x
= DECL_CHAIN (x
))
4221 check_for_override (x
, t
);
4222 if (DECL_PURE_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
4223 error ("initializer specified for non-virtual method %q+D", x
);
4224 /* The name of the field is the original field name
4225 Save this in auxiliary field for later overloading. */
4226 if (DECL_VINDEX (x
))
4228 TYPE_POLYMORPHIC_P (t
) = 1;
4229 if (DECL_PURE_VIRTUAL_P (x
))
4230 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
4232 /* All user-provided destructors are non-trivial.
4233 Constructors and assignment ops are handled in
4234 grok_special_member_properties. */
4235 if (DECL_DESTRUCTOR_P (x
) && user_provided_p (x
))
4236 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 1;
4240 /* FN is a constructor or destructor. Clone the declaration to create
4241 a specialized in-charge or not-in-charge version, as indicated by
4245 build_clone (tree fn
, tree name
)
4250 /* Copy the function. */
4251 clone
= copy_decl (fn
);
4252 /* Reset the function name. */
4253 DECL_NAME (clone
) = name
;
4254 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
4255 /* Remember where this function came from. */
4256 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4257 /* Make it easy to find the CLONE given the FN. */
4258 DECL_CHAIN (clone
) = DECL_CHAIN (fn
);
4259 DECL_CHAIN (fn
) = clone
;
4261 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4262 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4264 tree result
= build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4265 DECL_TEMPLATE_RESULT (clone
) = result
;
4266 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4267 DECL_TI_TEMPLATE (result
) = clone
;
4268 TREE_TYPE (clone
) = TREE_TYPE (result
);
4272 DECL_CLONED_FUNCTION (clone
) = fn
;
4273 /* There's no pending inline data for this function. */
4274 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4275 DECL_PENDING_INLINE_P (clone
) = 0;
4277 /* The base-class destructor is not virtual. */
4278 if (name
== base_dtor_identifier
)
4280 DECL_VIRTUAL_P (clone
) = 0;
4281 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4282 DECL_VINDEX (clone
) = NULL_TREE
;
4285 /* If there was an in-charge parameter, drop it from the function
4287 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4293 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4294 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4295 parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4296 /* Skip the `this' parameter. */
4297 parmtypes
= TREE_CHAIN (parmtypes
);
4298 /* Skip the in-charge parameter. */
4299 parmtypes
= TREE_CHAIN (parmtypes
);
4300 /* And the VTT parm, in a complete [cd]tor. */
4301 if (DECL_HAS_VTT_PARM_P (fn
)
4302 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4303 parmtypes
= TREE_CHAIN (parmtypes
);
4304 /* If this is subobject constructor or destructor, add the vtt
4307 = build_method_type_directly (basetype
,
4308 TREE_TYPE (TREE_TYPE (clone
)),
4311 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
),
4314 = cp_build_type_attribute_variant (TREE_TYPE (clone
),
4315 TYPE_ATTRIBUTES (TREE_TYPE (fn
)));
4318 /* Copy the function parameters. */
4319 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4320 /* Remove the in-charge parameter. */
4321 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4323 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4324 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4325 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4327 /* And the VTT parm, in a complete [cd]tor. */
4328 if (DECL_HAS_VTT_PARM_P (fn
))
4330 if (DECL_NEEDS_VTT_PARM_P (clone
))
4331 DECL_HAS_VTT_PARM_P (clone
) = 1;
4334 DECL_CHAIN (DECL_ARGUMENTS (clone
))
4335 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone
)));
4336 DECL_HAS_VTT_PARM_P (clone
) = 0;
4340 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= DECL_CHAIN (parms
))
4342 DECL_CONTEXT (parms
) = clone
;
4343 cxx_dup_lang_specific_decl (parms
);
4346 /* Create the RTL for this function. */
4347 SET_DECL_RTL (clone
, NULL
);
4348 rest_of_decl_compilation (clone
, /*top_level=*/1, at_eof
);
4351 note_decl_for_pch (clone
);
4356 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4357 not invoke this function directly.
4359 For a non-thunk function, returns the address of the slot for storing
4360 the function it is a clone of. Otherwise returns NULL_TREE.
4362 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4363 cloned_function is unset. This is to support the separate
4364 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4365 on a template makes sense, but not the former. */
4368 decl_cloned_function_p (const_tree decl
, bool just_testing
)
4372 decl
= STRIP_TEMPLATE (decl
);
4374 if (TREE_CODE (decl
) != FUNCTION_DECL
4375 || !DECL_LANG_SPECIFIC (decl
)
4376 || DECL_LANG_SPECIFIC (decl
)->u
.fn
.thunk_p
)
4378 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4380 lang_check_failed (__FILE__
, __LINE__
, __FUNCTION__
);
4386 ptr
= &DECL_LANG_SPECIFIC (decl
)->u
.fn
.u5
.cloned_function
;
4387 if (just_testing
&& *ptr
== NULL_TREE
)
4393 /* Produce declarations for all appropriate clones of FN. If
4394 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4395 CLASTYPE_METHOD_VEC as well. */
4398 clone_function_decl (tree fn
, int update_method_vec_p
)
4402 /* Avoid inappropriate cloning. */
4404 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn
)))
4407 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4409 /* For each constructor, we need two variants: an in-charge version
4410 and a not-in-charge version. */
4411 clone
= build_clone (fn
, complete_ctor_identifier
);
4412 if (update_method_vec_p
)
4413 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4414 clone
= build_clone (fn
, base_ctor_identifier
);
4415 if (update_method_vec_p
)
4416 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4420 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
));
4422 /* For each destructor, we need three variants: an in-charge
4423 version, a not-in-charge version, and an in-charge deleting
4424 version. We clone the deleting version first because that
4425 means it will go second on the TYPE_METHODS list -- and that
4426 corresponds to the correct layout order in the virtual
4429 For a non-virtual destructor, we do not build a deleting
4431 if (DECL_VIRTUAL_P (fn
))
4433 clone
= build_clone (fn
, deleting_dtor_identifier
);
4434 if (update_method_vec_p
)
4435 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4437 clone
= build_clone (fn
, complete_dtor_identifier
);
4438 if (update_method_vec_p
)
4439 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4440 clone
= build_clone (fn
, base_dtor_identifier
);
4441 if (update_method_vec_p
)
4442 add_method (DECL_CONTEXT (clone
), clone
, NULL_TREE
);
4445 /* Note that this is an abstract function that is never emitted. */
4446 DECL_ABSTRACT (fn
) = 1;
4449 /* DECL is an in charge constructor, which is being defined. This will
4450 have had an in class declaration, from whence clones were
4451 declared. An out-of-class definition can specify additional default
4452 arguments. As it is the clones that are involved in overload
4453 resolution, we must propagate the information from the DECL to its
4457 adjust_clone_args (tree decl
)
4461 for (clone
= DECL_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION_P (clone
);
4462 clone
= DECL_CHAIN (clone
))
4464 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4465 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4466 tree decl_parms
, clone_parms
;
4468 clone_parms
= orig_clone_parms
;
4470 /* Skip the 'this' parameter. */
4471 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4472 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4474 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4475 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4476 if (DECL_HAS_VTT_PARM_P (decl
))
4477 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4479 clone_parms
= orig_clone_parms
;
4480 if (DECL_HAS_VTT_PARM_P (clone
))
4481 clone_parms
= TREE_CHAIN (clone_parms
);
4483 for (decl_parms
= orig_decl_parms
; decl_parms
;
4484 decl_parms
= TREE_CHAIN (decl_parms
),
4485 clone_parms
= TREE_CHAIN (clone_parms
))
4487 gcc_assert (same_type_p (TREE_TYPE (decl_parms
),
4488 TREE_TYPE (clone_parms
)));
4490 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
4492 /* A default parameter has been added. Adjust the
4493 clone's parameters. */
4494 tree exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4495 tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (clone
));
4496 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4499 clone_parms
= orig_decl_parms
;
4501 if (DECL_HAS_VTT_PARM_P (clone
))
4503 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
4504 TREE_VALUE (orig_clone_parms
),
4506 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
4508 type
= build_method_type_directly (basetype
,
4509 TREE_TYPE (TREE_TYPE (clone
)),
4512 type
= build_exception_variant (type
, exceptions
);
4514 type
= cp_build_type_attribute_variant (type
, attrs
);
4515 TREE_TYPE (clone
) = type
;
4517 clone_parms
= NULL_TREE
;
4521 gcc_assert (!clone_parms
);
4525 /* For each of the constructors and destructors in T, create an
4526 in-charge and not-in-charge variant. */
4529 clone_constructors_and_destructors (tree t
)
4533 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4535 if (!CLASSTYPE_METHOD_VEC (t
))
4538 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4539 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4540 for (fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4541 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4544 /* Deduce noexcept for a destructor DTOR. */
4547 deduce_noexcept_on_destructor (tree dtor
)
4549 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor
)))
4551 tree ctx
= DECL_CONTEXT (dtor
);
4552 tree implicit_fn
= implicitly_declare_fn (sfk_destructor
, ctx
,
4555 tree eh_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn
));
4556 TREE_TYPE (dtor
) = build_exception_variant (TREE_TYPE (dtor
), eh_spec
);
4560 /* For each destructor in T, deduce noexcept:
4562 12.4/3: A declaration of a destructor that does not have an
4563 exception-specification is implicitly considered to have the
4564 same exception-specification as an implicit declaration (15.4). */
4567 deduce_noexcept_on_destructors (tree t
)
4571 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4573 if (!CLASSTYPE_METHOD_VEC (t
))
4576 for (fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4577 deduce_noexcept_on_destructor (OVL_CURRENT (fns
));
4580 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4581 of TYPE for virtual functions which FNDECL overrides. Return a
4582 mask of the tm attributes found therein. */
4585 look_for_tm_attr_overrides (tree type
, tree fndecl
)
4587 tree binfo
= TYPE_BINFO (type
);
4591 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ++ix
)
4593 tree o
, basetype
= BINFO_TYPE (base_binfo
);
4595 if (!TYPE_POLYMORPHIC_P (basetype
))
4598 o
= look_for_overrides_here (basetype
, fndecl
);
4600 found
|= tm_attr_to_mask (find_tm_attribute
4601 (TYPE_ATTRIBUTES (TREE_TYPE (o
))));
4603 found
|= look_for_tm_attr_overrides (basetype
, fndecl
);
4609 /* Subroutine of set_method_tm_attributes. Handle the checks and
4610 inheritance for one virtual method FNDECL. */
4613 set_one_vmethod_tm_attributes (tree type
, tree fndecl
)
4618 found
= look_for_tm_attr_overrides (type
, fndecl
);
4620 /* If FNDECL doesn't actually override anything (i.e. T is the
4621 class that first declares FNDECL virtual), then we're done. */
4625 tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)));
4626 have
= tm_attr_to_mask (tm_attr
);
4628 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4629 tm_pure must match exactly, otherwise no weakening of
4630 tm_safe > tm_callable > nothing. */
4631 /* ??? The tm_pure attribute didn't make the transition to the
4632 multivendor language spec. */
4633 if (have
== TM_ATTR_PURE
)
4635 if (found
!= TM_ATTR_PURE
)
4641 /* If the overridden function is tm_pure, then FNDECL must be. */
4642 else if (found
== TM_ATTR_PURE
&& tm_attr
)
4644 /* Look for base class combinations that cannot be satisfied. */
4645 else if (found
!= TM_ATTR_PURE
&& (found
& TM_ATTR_PURE
))
4647 found
&= ~TM_ATTR_PURE
;
4649 error_at (DECL_SOURCE_LOCATION (fndecl
),
4650 "method overrides both %<transaction_pure%> and %qE methods",
4651 tm_mask_to_attr (found
));
4653 /* If FNDECL did not declare an attribute, then inherit the most
4655 else if (tm_attr
== NULL
)
4657 apply_tm_attr (fndecl
, tm_mask_to_attr (found
& -found
));
4659 /* Otherwise validate that we're not weaker than a function
4660 that is being overridden. */
4664 if (found
<= TM_ATTR_CALLABLE
&& have
> found
)
4670 error_at (DECL_SOURCE_LOCATION (fndecl
),
4671 "method declared %qE overriding %qE method",
4672 tm_attr
, tm_mask_to_attr (found
));
4675 /* For each of the methods in T, propagate a class-level tm attribute. */
4678 set_method_tm_attributes (tree t
)
4680 tree class_tm_attr
, fndecl
;
4682 /* Don't bother collecting tm attributes if transactional memory
4683 support is not enabled. */
4687 /* Process virtual methods first, as they inherit directly from the
4688 base virtual function and also require validation of new attributes. */
4689 if (TYPE_CONTAINS_VPTR_P (t
))
4692 for (vchain
= BINFO_VIRTUALS (TYPE_BINFO (t
)); vchain
;
4693 vchain
= TREE_CHAIN (vchain
))
4695 fndecl
= BV_FN (vchain
);
4696 if (DECL_THUNK_P (fndecl
))
4697 fndecl
= THUNK_TARGET (fndecl
);
4698 set_one_vmethod_tm_attributes (t
, fndecl
);
4702 /* If the class doesn't have an attribute, nothing more to do. */
4703 class_tm_attr
= find_tm_attribute (TYPE_ATTRIBUTES (t
));
4704 if (class_tm_attr
== NULL
)
4707 /* Any method that does not yet have a tm attribute inherits
4708 the one from the class. */
4709 for (fndecl
= TYPE_METHODS (t
); fndecl
; fndecl
= TREE_CHAIN (fndecl
))
4711 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
4712 apply_tm_attr (fndecl
, class_tm_attr
);
4716 /* Returns true iff class T has a user-defined constructor other than
4717 the default constructor. */
4720 type_has_user_nondefault_constructor (tree t
)
4724 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4727 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4729 tree fn
= OVL_CURRENT (fns
);
4730 if (!DECL_ARTIFICIAL (fn
)
4731 && (TREE_CODE (fn
) == TEMPLATE_DECL
4732 || (skip_artificial_parms_for (fn
, DECL_ARGUMENTS (fn
))
4740 /* Returns the defaulted constructor if T has one. Otherwise, returns
4744 in_class_defaulted_default_constructor (tree t
)
4748 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4751 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4753 tree fn
= OVL_CURRENT (fns
);
4755 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
4757 args
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
4758 while (args
&& TREE_PURPOSE (args
))
4759 args
= TREE_CHAIN (args
);
4760 if (!args
|| args
== void_list_node
)
4768 /* Returns true iff FN is a user-provided function, i.e. user-declared
4769 and not defaulted at its first declaration; or explicit, private,
4770 protected, or non-const. */
4773 user_provided_p (tree fn
)
4775 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4778 return (!DECL_ARTIFICIAL (fn
)
4779 && !DECL_DEFAULTED_IN_CLASS_P (fn
));
4782 /* Returns true iff class T has a user-provided constructor. */
4785 type_has_user_provided_constructor (tree t
)
4789 if (!CLASS_TYPE_P (t
))
4792 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4795 /* This can happen in error cases; avoid crashing. */
4796 if (!CLASSTYPE_METHOD_VEC (t
))
4799 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4800 if (user_provided_p (OVL_CURRENT (fns
)))
4806 /* Returns true iff class T has a user-provided default constructor. */
4809 type_has_user_provided_default_constructor (tree t
)
4813 if (!TYPE_HAS_USER_CONSTRUCTOR (t
))
4816 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4818 tree fn
= OVL_CURRENT (fns
);
4819 if (TREE_CODE (fn
) == FUNCTION_DECL
4820 && user_provided_p (fn
)
4821 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)))
4828 /* If default-initialization leaves part of TYPE uninitialized, returns
4829 a DECL for the field or TYPE itself (DR 253). */
4832 default_init_uninitialized_part (tree type
)
4837 type
= strip_array_types (type
);
4838 if (!CLASS_TYPE_P (type
))
4840 if (type_has_user_provided_default_constructor (type
))
4842 for (binfo
= TYPE_BINFO (type
), i
= 0;
4843 BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
4845 r
= default_init_uninitialized_part (BINFO_TYPE (t
));
4849 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
4850 if (TREE_CODE (t
) == FIELD_DECL
4851 && !DECL_ARTIFICIAL (t
)
4852 && !DECL_INITIAL (t
))
4854 r
= default_init_uninitialized_part (TREE_TYPE (t
));
4856 return DECL_P (r
) ? r
: t
;
4862 /* Returns true iff for class T, a trivial synthesized default constructor
4863 would be constexpr. */
4866 trivial_default_constructor_is_constexpr (tree t
)
4868 /* A defaulted trivial default constructor is constexpr
4869 if there is nothing to initialize. */
4870 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t
));
4871 return is_really_empty_class (t
);
4874 /* Returns true iff class T has a constexpr default constructor. */
4877 type_has_constexpr_default_constructor (tree t
)
4881 if (!CLASS_TYPE_P (t
))
4883 /* The caller should have stripped an enclosing array. */
4884 gcc_assert (TREE_CODE (t
) != ARRAY_TYPE
);
4887 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t
))
4889 if (!TYPE_HAS_COMPLEX_DFLT (t
))
4890 return trivial_default_constructor_is_constexpr (t
);
4891 /* Non-trivial, we need to check subobject constructors. */
4892 lazily_declare_fn (sfk_constructor
, t
);
4894 fns
= locate_ctor (t
);
4895 return (fns
&& DECL_DECLARED_CONSTEXPR_P (fns
));
4898 /* Returns true iff class TYPE has a virtual destructor. */
4901 type_has_virtual_destructor (tree type
)
4905 if (!CLASS_TYPE_P (type
))
4908 gcc_assert (COMPLETE_TYPE_P (type
));
4909 dtor
= CLASSTYPE_DESTRUCTORS (type
);
4910 return (dtor
&& DECL_VIRTUAL_P (dtor
));
4913 /* Returns true iff class T has a move constructor. */
4916 type_has_move_constructor (tree t
)
4920 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
4922 gcc_assert (COMPLETE_TYPE_P (t
));
4923 lazily_declare_fn (sfk_move_constructor
, t
);
4926 if (!CLASSTYPE_METHOD_VEC (t
))
4929 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4930 if (move_fn_p (OVL_CURRENT (fns
)))
4936 /* Returns true iff class T has a move assignment operator. */
4939 type_has_move_assign (tree t
)
4943 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
4945 gcc_assert (COMPLETE_TYPE_P (t
));
4946 lazily_declare_fn (sfk_move_assignment
, t
);
4949 for (fns
= lookup_fnfields_slot_nolazy (t
, ansi_assopname (NOP_EXPR
));
4950 fns
; fns
= OVL_NEXT (fns
))
4951 if (move_fn_p (OVL_CURRENT (fns
)))
4957 /* Returns true iff class T has a move constructor that was explicitly
4958 declared in the class body. Note that this is different from
4959 "user-provided", which doesn't include functions that are defaulted in
4963 type_has_user_declared_move_constructor (tree t
)
4967 if (CLASSTYPE_LAZY_MOVE_CTOR (t
))
4970 if (!CLASSTYPE_METHOD_VEC (t
))
4973 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4975 tree fn
= OVL_CURRENT (fns
);
4976 if (move_fn_p (fn
) && !DECL_ARTIFICIAL (fn
))
4983 /* Returns true iff class T has a move assignment operator that was
4984 explicitly declared in the class body. */
4987 type_has_user_declared_move_assign (tree t
)
4991 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t
))
4994 for (fns
= lookup_fnfields_slot_nolazy (t
, ansi_assopname (NOP_EXPR
));
4995 fns
; fns
= OVL_NEXT (fns
))
4997 tree fn
= OVL_CURRENT (fns
);
4998 if (move_fn_p (fn
) && !DECL_ARTIFICIAL (fn
))
5005 /* Nonzero if we need to build up a constructor call when initializing an
5006 object of this class, either because it has a user-provided constructor
5007 or because it doesn't have a default constructor (so we need to give an
5008 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5009 what you care about is whether or not an object can be produced by a
5010 constructor (e.g. so we don't set TREE_READONLY on const variables of
5011 such type); use this function when what you care about is whether or not
5012 to try to call a constructor to create an object. The latter case is
5013 the former plus some cases of constructors that cannot be called. */
5016 type_build_ctor_call (tree t
)
5019 if (TYPE_NEEDS_CONSTRUCTING (t
))
5021 inner
= strip_array_types (t
);
5022 return (CLASS_TYPE_P (inner
) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner
)
5023 && !ANON_AGGR_TYPE_P (inner
));
5026 /* Remove all zero-width bit-fields from T. */
5029 remove_zero_width_bit_fields (tree t
)
5033 fieldsp
= &TYPE_FIELDS (t
);
5036 if (TREE_CODE (*fieldsp
) == FIELD_DECL
5037 && DECL_C_BIT_FIELD (*fieldsp
)
5038 /* We should not be confused by the fact that grokbitfield
5039 temporarily sets the width of the bit field into
5040 DECL_INITIAL (*fieldsp).
5041 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5043 && integer_zerop (DECL_SIZE (*fieldsp
)))
5044 *fieldsp
= DECL_CHAIN (*fieldsp
);
5046 fieldsp
= &DECL_CHAIN (*fieldsp
);
5050 /* Returns TRUE iff we need a cookie when dynamically allocating an
5051 array whose elements have the indicated class TYPE. */
5054 type_requires_array_cookie (tree type
)
5057 bool has_two_argument_delete_p
= false;
5059 gcc_assert (CLASS_TYPE_P (type
));
5061 /* If there's a non-trivial destructor, we need a cookie. In order
5062 to iterate through the array calling the destructor for each
5063 element, we'll have to know how many elements there are. */
5064 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5067 /* If the usual deallocation function is a two-argument whose second
5068 argument is of type `size_t', then we have to pass the size of
5069 the array to the deallocation function, so we will need to store
5071 fns
= lookup_fnfields (TYPE_BINFO (type
),
5072 ansi_opname (VEC_DELETE_EXPR
),
5074 /* If there are no `operator []' members, or the lookup is
5075 ambiguous, then we don't need a cookie. */
5076 if (!fns
|| fns
== error_mark_node
)
5078 /* Loop through all of the functions. */
5079 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
5084 /* Select the current function. */
5085 fn
= OVL_CURRENT (fns
);
5086 /* See if this function is a one-argument delete function. If
5087 it is, then it will be the usual deallocation function. */
5088 second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
5089 if (second_parm
== void_list_node
)
5091 /* Do not consider this function if its second argument is an
5095 /* Otherwise, if we have a two-argument function and the second
5096 argument is `size_t', it will be the usual deallocation
5097 function -- unless there is one-argument function, too. */
5098 if (TREE_CHAIN (second_parm
) == void_list_node
5099 && same_type_p (TREE_VALUE (second_parm
), size_type_node
))
5100 has_two_argument_delete_p
= true;
5103 return has_two_argument_delete_p
;
5106 /* Finish computing the `literal type' property of class type T.
5108 At this point, we have already processed base classes and
5109 non-static data members. We need to check whether the copy
5110 constructor is trivial, the destructor is trivial, and there
5111 is a trivial default constructor or at least one constexpr
5112 constructor other than the copy constructor. */
5115 finalize_literal_type_property (tree t
)
5119 if (cxx_dialect
< cxx0x
5120 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5121 CLASSTYPE_LITERAL_P (t
) = false;
5122 else if (CLASSTYPE_LITERAL_P (t
) && !TYPE_HAS_TRIVIAL_DFLT (t
)
5123 && CLASSTYPE_NON_AGGREGATE (t
)
5124 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5125 CLASSTYPE_LITERAL_P (t
) = false;
5127 if (!CLASSTYPE_LITERAL_P (t
))
5128 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5129 if (DECL_DECLARED_CONSTEXPR_P (fn
)
5130 && TREE_CODE (fn
) != TEMPLATE_DECL
5131 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
5132 && !DECL_CONSTRUCTOR_P (fn
))
5134 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
5135 if (!DECL_GENERATED_P (fn
))
5137 error ("enclosing class of constexpr non-static member "
5138 "function %q+#D is not a literal type", fn
);
5139 explain_non_literal_class (t
);
5144 /* T is a non-literal type used in a context which requires a constant
5145 expression. Explain why it isn't literal. */
5148 explain_non_literal_class (tree t
)
5150 static struct pointer_set_t
*diagnosed
;
5152 if (!CLASS_TYPE_P (t
))
5154 t
= TYPE_MAIN_VARIANT (t
);
5156 if (diagnosed
== NULL
)
5157 diagnosed
= pointer_set_create ();
5158 if (pointer_set_insert (diagnosed
, t
) != 0)
5159 /* Already explained. */
5162 inform (0, "%q+T is not literal because:", t
);
5163 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
5164 inform (0, " %q+T has a non-trivial destructor", t
);
5165 else if (CLASSTYPE_NON_AGGREGATE (t
)
5166 && !TYPE_HAS_TRIVIAL_DFLT (t
)
5167 && !TYPE_HAS_CONSTEXPR_CTOR (t
))
5169 inform (0, " %q+T is not an aggregate, does not have a trivial "
5170 "default constructor, and has no constexpr constructor that "
5171 "is not a copy or move constructor", t
);
5172 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t
)
5173 && !type_has_user_provided_default_constructor (t
))
5175 /* Note that we can't simply call locate_ctor because when the
5176 constructor is deleted it just returns NULL_TREE. */
5178 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
5180 tree fn
= OVL_CURRENT (fns
);
5181 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5183 parms
= skip_artificial_parms_for (fn
, parms
);
5185 if (sufficient_parms_p (parms
))
5187 if (DECL_DELETED_FN (fn
))
5188 maybe_explain_implicit_delete (fn
);
5190 explain_invalid_constexpr_fn (fn
);
5198 tree binfo
, base_binfo
, field
; int i
;
5199 for (binfo
= TYPE_BINFO (t
), i
= 0;
5200 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5202 tree basetype
= TREE_TYPE (base_binfo
);
5203 if (!CLASSTYPE_LITERAL_P (basetype
))
5205 inform (0, " base class %qT of %q+T is non-literal",
5207 explain_non_literal_class (basetype
);
5211 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
5214 if (TREE_CODE (field
) != FIELD_DECL
)
5216 ftype
= TREE_TYPE (field
);
5217 if (!literal_type_p (ftype
))
5219 inform (0, " non-static data member %q+D has "
5220 "non-literal type", field
);
5221 if (CLASS_TYPE_P (ftype
))
5222 explain_non_literal_class (ftype
);
5228 /* Check the validity of the bases and members declared in T. Add any
5229 implicitly-generated functions (like copy-constructors and
5230 assignment operators). Compute various flag bits (like
5231 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5232 level: i.e., independently of the ABI in use. */
5235 check_bases_and_members (tree t
)
5237 /* Nonzero if the implicitly generated copy constructor should take
5238 a non-const reference argument. */
5239 int cant_have_const_ctor
;
5240 /* Nonzero if the implicitly generated assignment operator
5241 should take a non-const reference argument. */
5242 int no_const_asn_ref
;
5244 bool saved_complex_asn_ref
;
5245 bool saved_nontrivial_dtor
;
5248 /* By default, we use const reference arguments and generate default
5250 cant_have_const_ctor
= 0;
5251 no_const_asn_ref
= 0;
5253 /* Deduce noexcept on destructors. */
5254 if (cxx_dialect
>= cxx0x
)
5255 deduce_noexcept_on_destructors (t
);
5257 /* Check all the base-classes. */
5258 check_bases (t
, &cant_have_const_ctor
,
5261 /* Check all the method declarations. */
5264 /* Save the initial values of these flags which only indicate whether
5265 or not the class has user-provided functions. As we analyze the
5266 bases and members we can set these flags for other reasons. */
5267 saved_complex_asn_ref
= TYPE_HAS_COMPLEX_COPY_ASSIGN (t
);
5268 saved_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
5270 /* Check all the data member declarations. We cannot call
5271 check_field_decls until we have called check_bases check_methods,
5272 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5273 being set appropriately. */
5274 check_field_decls (t
, &access_decls
,
5275 &cant_have_const_ctor
,
5278 /* A nearly-empty class has to be vptr-containing; a nearly empty
5279 class contains just a vptr. */
5280 if (!TYPE_CONTAINS_VPTR_P (t
))
5281 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
5283 /* Do some bookkeeping that will guide the generation of implicitly
5284 declared member functions. */
5285 TYPE_HAS_COMPLEX_COPY_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5286 TYPE_HAS_COMPLEX_MOVE_CTOR (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5287 /* We need to call a constructor for this class if it has a
5288 user-provided constructor, or if the default constructor is going
5289 to initialize the vptr. (This is not an if-and-only-if;
5290 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5291 themselves need constructing.) */
5292 TYPE_NEEDS_CONSTRUCTING (t
)
5293 |= (type_has_user_provided_constructor (t
) || TYPE_CONTAINS_VPTR_P (t
));
5296 An aggregate is an array or a class with no user-provided
5297 constructors ... and no virtual functions.
5299 Again, other conditions for being an aggregate are checked
5301 CLASSTYPE_NON_AGGREGATE (t
)
5302 |= (type_has_user_provided_constructor (t
) || TYPE_POLYMORPHIC_P (t
));
5303 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5304 retain the old definition internally for ABI reasons. */
5305 CLASSTYPE_NON_LAYOUT_POD_P (t
)
5306 |= (CLASSTYPE_NON_AGGREGATE (t
)
5307 || saved_nontrivial_dtor
|| saved_complex_asn_ref
);
5308 CLASSTYPE_NON_STD_LAYOUT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5309 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5310 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5311 TYPE_HAS_COMPLEX_DFLT (t
) |= TYPE_CONTAINS_VPTR_P (t
);
5313 /* If the class has no user-declared constructor, but does have
5314 non-static const or reference data members that can never be
5315 initialized, issue a warning. */
5316 if (warn_uninitialized
5317 /* Classes with user-declared constructors are presumed to
5318 initialize these members. */
5319 && !TYPE_HAS_USER_CONSTRUCTOR (t
)
5320 /* Aggregates can be initialized with brace-enclosed
5322 && CLASSTYPE_NON_AGGREGATE (t
))
5326 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5330 if (TREE_CODE (field
) != FIELD_DECL
5331 || DECL_INITIAL (field
) != NULL_TREE
)
5334 type
= TREE_TYPE (field
);
5335 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5336 warning (OPT_Wuninitialized
, "non-static reference %q+#D "
5337 "in class without a constructor", field
);
5338 else if (CP_TYPE_CONST_P (type
)
5339 && (!CLASS_TYPE_P (type
)
5340 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
5341 warning (OPT_Wuninitialized
, "non-static const member %q+#D "
5342 "in class without a constructor", field
);
5346 /* Synthesize any needed methods. */
5347 add_implicitly_declared_members (t
, &access_decls
,
5348 cant_have_const_ctor
,
5351 /* Check defaulted declarations here so we have cant_have_const_ctor
5352 and don't need to worry about clones. */
5353 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5354 if (!DECL_ARTIFICIAL (fn
) && DECL_DEFAULTED_IN_CLASS_P (fn
))
5356 int copy
= copy_fn_p (fn
);
5360 = (DECL_CONSTRUCTOR_P (fn
) ? !cant_have_const_ctor
5361 : !no_const_asn_ref
);
5362 bool fn_const_p
= (copy
== 2);
5364 if (fn_const_p
&& !imp_const_p
)
5365 /* If the function is defaulted outside the class, we just
5366 give the synthesis error. */
5367 error ("%q+D declared to take const reference, but implicit "
5368 "declaration would take non-const", fn
);
5370 defaulted_late_check (fn
);
5373 if (LAMBDA_TYPE_P (t
))
5375 /* "The closure type associated with a lambda-expression has a deleted
5376 default constructor and a deleted copy assignment operator." */
5377 TYPE_NEEDS_CONSTRUCTING (t
) = 1;
5378 TYPE_HAS_COMPLEX_DFLT (t
) = 1;
5379 TYPE_HAS_COMPLEX_COPY_ASSIGN (t
) = 1;
5380 CLASSTYPE_LAZY_MOVE_ASSIGN (t
) = 0;
5382 /* "This class type is not an aggregate." */
5383 CLASSTYPE_NON_AGGREGATE (t
) = 1;
5386 /* Compute the 'literal type' property before we
5387 do anything with non-static member functions. */
5388 finalize_literal_type_property (t
);
5390 /* Create the in-charge and not-in-charge variants of constructors
5392 clone_constructors_and_destructors (t
);
5394 /* Process the using-declarations. */
5395 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
5396 handle_using_decl (TREE_VALUE (access_decls
), t
);
5398 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5399 finish_struct_methods (t
);
5401 /* Figure out whether or not we will need a cookie when dynamically
5402 allocating an array of this type. */
5403 TYPE_LANG_SPECIFIC (t
)->u
.c
.vec_new_uses_cookie
5404 = type_requires_array_cookie (t
);
5407 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5408 accordingly. If a new vfield was created (because T doesn't have a
5409 primary base class), then the newly created field is returned. It
5410 is not added to the TYPE_FIELDS list; it is the caller's
5411 responsibility to do that. Accumulate declared virtual functions
5415 create_vtable_ptr (tree t
, tree
* virtuals_p
)
5419 /* Collect the virtual functions declared in T. */
5420 for (fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
5421 if (DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
)
5422 && TREE_CODE (DECL_VINDEX (fn
)) != INTEGER_CST
)
5424 tree new_virtual
= make_node (TREE_LIST
);
5426 BV_FN (new_virtual
) = fn
;
5427 BV_DELTA (new_virtual
) = integer_zero_node
;
5428 BV_VCALL_INDEX (new_virtual
) = NULL_TREE
;
5430 TREE_CHAIN (new_virtual
) = *virtuals_p
;
5431 *virtuals_p
= new_virtual
;
5434 /* If we couldn't find an appropriate base class, create a new field
5435 here. Even if there weren't any new virtual functions, we might need a
5436 new virtual function table if we're supposed to include vptrs in
5437 all classes that need them. */
5438 if (!TYPE_VFIELD (t
) && (*virtuals_p
|| TYPE_CONTAINS_VPTR_P (t
)))
5440 /* We build this decl with vtbl_ptr_type_node, which is a
5441 `vtable_entry_type*'. It might seem more precise to use
5442 `vtable_entry_type (*)[N]' where N is the number of virtual
5443 functions. However, that would require the vtable pointer in
5444 base classes to have a different type than the vtable pointer
5445 in derived classes. We could make that happen, but that
5446 still wouldn't solve all the problems. In particular, the
5447 type-based alias analysis code would decide that assignments
5448 to the base class vtable pointer can't alias assignments to
5449 the derived class vtable pointer, since they have different
5450 types. Thus, in a derived class destructor, where the base
5451 class constructor was inlined, we could generate bad code for
5452 setting up the vtable pointer.
5454 Therefore, we use one type for all vtable pointers. We still
5455 use a type-correct type; it's just doesn't indicate the array
5456 bounds. That's better than using `void*' or some such; it's
5457 cleaner, and it let's the alias analysis code know that these
5458 stores cannot alias stores to void*! */
5461 field
= build_decl (input_location
,
5462 FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
5463 DECL_VIRTUAL_P (field
) = 1;
5464 DECL_ARTIFICIAL (field
) = 1;
5465 DECL_FIELD_CONTEXT (field
) = t
;
5466 DECL_FCONTEXT (field
) = t
;
5467 if (TYPE_PACKED (t
))
5468 DECL_PACKED (field
) = 1;
5470 TYPE_VFIELD (t
) = field
;
5472 /* This class is non-empty. */
5473 CLASSTYPE_EMPTY_P (t
) = 0;
5481 /* Add OFFSET to all base types of BINFO which is a base in the
5482 hierarchy dominated by T.
5484 OFFSET, which is a type offset, is number of bytes. */
5487 propagate_binfo_offsets (tree binfo
, tree offset
)
5493 /* Update BINFO's offset. */
5494 BINFO_OFFSET (binfo
)
5495 = convert (sizetype
,
5496 size_binop (PLUS_EXPR
,
5497 convert (ssizetype
, BINFO_OFFSET (binfo
)),
5500 /* Find the primary base class. */
5501 primary_binfo
= get_primary_binfo (binfo
);
5503 if (primary_binfo
&& BINFO_INHERITANCE_CHAIN (primary_binfo
) == binfo
)
5504 propagate_binfo_offsets (primary_binfo
, offset
);
5506 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5508 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5510 /* Don't do the primary base twice. */
5511 if (base_binfo
== primary_binfo
)
5514 if (BINFO_VIRTUAL_P (base_binfo
))
5517 propagate_binfo_offsets (base_binfo
, offset
);
5521 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5522 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5523 empty subobjects of T. */
5526 layout_virtual_bases (record_layout_info rli
, splay_tree offsets
)
5530 bool first_vbase
= true;
5533 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)) == 0)
5536 if (!abi_version_at_least(2))
5538 /* In G++ 3.2, we incorrectly rounded the size before laying out
5539 the virtual bases. */
5540 finish_record_layout (rli
, /*free_p=*/false);
5541 #ifdef STRUCTURE_SIZE_BOUNDARY
5542 /* Packed structures don't need to have minimum size. */
5543 if (! TYPE_PACKED (t
))
5544 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), (unsigned) STRUCTURE_SIZE_BOUNDARY
);
5546 rli
->offset
= TYPE_SIZE_UNIT (t
);
5547 rli
->bitpos
= bitsize_zero_node
;
5548 rli
->record_align
= TYPE_ALIGN (t
);
5551 /* Find the last field. The artificial fields created for virtual
5552 bases will go after the last extant field to date. */
5553 next_field
= &TYPE_FIELDS (t
);
5555 next_field
= &DECL_CHAIN (*next_field
);
5557 /* Go through the virtual bases, allocating space for each virtual
5558 base that is not already a primary base class. These are
5559 allocated in inheritance graph order. */
5560 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
5562 if (!BINFO_VIRTUAL_P (vbase
))
5565 if (!BINFO_PRIMARY_P (vbase
))
5567 tree basetype
= TREE_TYPE (vbase
);
5569 /* This virtual base is not a primary base of any class in the
5570 hierarchy, so we have to add space for it. */
5571 next_field
= build_base_field (rli
, vbase
,
5572 offsets
, next_field
);
5574 /* If the first virtual base might have been placed at a
5575 lower address, had we started from CLASSTYPE_SIZE, rather
5576 than TYPE_SIZE, issue a warning. There can be both false
5577 positives and false negatives from this warning in rare
5578 cases; to deal with all the possibilities would probably
5579 require performing both layout algorithms and comparing
5580 the results which is not particularly tractable. */
5584 (size_binop (CEIL_DIV_EXPR
,
5585 round_up_loc (input_location
,
5587 CLASSTYPE_ALIGN (basetype
)),
5589 BINFO_OFFSET (vbase
))))
5591 "offset of virtual base %qT is not ABI-compliant and "
5592 "may change in a future version of GCC",
5595 first_vbase
= false;
5600 /* Returns the offset of the byte just past the end of the base class
5604 end_of_base (tree binfo
)
5608 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo
)))
5609 size
= TYPE_SIZE_UNIT (char_type_node
);
5610 else if (is_empty_class (BINFO_TYPE (binfo
)))
5611 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5612 allocate some space for it. It cannot have virtual bases, so
5613 TYPE_SIZE_UNIT is fine. */
5614 size
= TYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5616 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo
));
5618 return size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), size
);
5621 /* Returns the offset of the byte just past the end of the base class
5622 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5623 only non-virtual bases are included. */
5626 end_of_class (tree t
, int include_virtuals_p
)
5628 tree result
= size_zero_node
;
5629 vec
<tree
, va_gc
> *vbases
;
5635 for (binfo
= TYPE_BINFO (t
), i
= 0;
5636 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5638 if (!include_virtuals_p
5639 && BINFO_VIRTUAL_P (base_binfo
)
5640 && (!BINFO_PRIMARY_P (base_binfo
)
5641 || BINFO_INHERITANCE_CHAIN (base_binfo
) != TYPE_BINFO (t
)))
5644 offset
= end_of_base (base_binfo
);
5645 if (INT_CST_LT_UNSIGNED (result
, offset
))
5649 /* G++ 3.2 did not check indirect virtual bases. */
5650 if (abi_version_at_least (2) && include_virtuals_p
)
5651 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
5652 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
5654 offset
= end_of_base (base_binfo
);
5655 if (INT_CST_LT_UNSIGNED (result
, offset
))
5662 /* Warn about bases of T that are inaccessible because they are
5663 ambiguous. For example:
5666 struct T : public S {};
5667 struct U : public S, public T {};
5669 Here, `(S*) new U' is not allowed because there are two `S'
5673 warn_about_ambiguous_bases (tree t
)
5676 vec
<tree
, va_gc
> *vbases
;
5681 /* If there are no repeated bases, nothing can be ambiguous. */
5682 if (!CLASSTYPE_REPEATED_BASE_P (t
))
5685 /* Check direct bases. */
5686 for (binfo
= TYPE_BINFO (t
), i
= 0;
5687 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
5689 basetype
= BINFO_TYPE (base_binfo
);
5691 if (!uniquely_derived_from_p (basetype
, t
))
5692 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5696 /* Check for ambiguous virtual bases. */
5698 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
5699 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
5701 basetype
= BINFO_TYPE (binfo
);
5703 if (!uniquely_derived_from_p (basetype
, t
))
5704 warning (OPT_Wextra
, "virtual base %qT inaccessible in %qT due "
5705 "to ambiguity", basetype
, t
);
5709 /* Compare two INTEGER_CSTs K1 and K2. */
5712 splay_tree_compare_integer_csts (splay_tree_key k1
, splay_tree_key k2
)
5714 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
5717 /* Increase the size indicated in RLI to account for empty classes
5718 that are "off the end" of the class. */
5721 include_empty_classes (record_layout_info rli
)
5726 /* It might be the case that we grew the class to allocate a
5727 zero-sized base class. That won't be reflected in RLI, yet,
5728 because we are willing to overlay multiple bases at the same
5729 offset. However, now we need to make sure that RLI is big enough
5730 to reflect the entire class. */
5731 eoc
= end_of_class (rli
->t
,
5732 CLASSTYPE_AS_BASE (rli
->t
) != NULL_TREE
);
5733 rli_size
= rli_size_unit_so_far (rli
);
5734 if (TREE_CODE (rli_size
) == INTEGER_CST
5735 && INT_CST_LT_UNSIGNED (rli_size
, eoc
))
5737 if (!abi_version_at_least (2))
5738 /* In version 1 of the ABI, the size of a class that ends with
5739 a bitfield was not rounded up to a whole multiple of a
5740 byte. Because rli_size_unit_so_far returns only the number
5741 of fully allocated bytes, any extra bits were not included
5743 rli
->bitpos
= round_down (rli
->bitpos
, BITS_PER_UNIT
);
5745 /* The size should have been rounded to a whole byte. */
5746 gcc_assert (tree_int_cst_equal
5747 (rli
->bitpos
, round_down (rli
->bitpos
, BITS_PER_UNIT
)));
5749 = size_binop (PLUS_EXPR
,
5751 size_binop (MULT_EXPR
,
5752 convert (bitsizetype
,
5753 size_binop (MINUS_EXPR
,
5755 bitsize_int (BITS_PER_UNIT
)));
5756 normalize_rli (rli
);
5760 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5761 BINFO_OFFSETs for all of the base-classes. Position the vtable
5762 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5765 layout_class_type (tree t
, tree
*virtuals_p
)
5767 tree non_static_data_members
;
5770 record_layout_info rli
;
5771 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5772 types that appear at that offset. */
5773 splay_tree empty_base_offsets
;
5774 /* True if the last field layed out was a bit-field. */
5775 bool last_field_was_bitfield
= false;
5776 /* The location at which the next field should be inserted. */
5778 /* T, as a base class. */
5781 /* Keep track of the first non-static data member. */
5782 non_static_data_members
= TYPE_FIELDS (t
);
5784 /* Start laying out the record. */
5785 rli
= start_record_layout (t
);
5787 /* Mark all the primary bases in the hierarchy. */
5788 determine_primary_bases (t
);
5790 /* Create a pointer to our virtual function table. */
5791 vptr
= create_vtable_ptr (t
, virtuals_p
);
5793 /* The vptr is always the first thing in the class. */
5796 DECL_CHAIN (vptr
) = TYPE_FIELDS (t
);
5797 TYPE_FIELDS (t
) = vptr
;
5798 next_field
= &DECL_CHAIN (vptr
);
5799 place_field (rli
, vptr
);
5802 next_field
= &TYPE_FIELDS (t
);
5804 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5805 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
5807 build_base_fields (rli
, empty_base_offsets
, next_field
);
5809 /* Layout the non-static data members. */
5810 for (field
= non_static_data_members
; field
; field
= DECL_CHAIN (field
))
5815 /* We still pass things that aren't non-static data members to
5816 the back end, in case it wants to do something with them. */
5817 if (TREE_CODE (field
) != FIELD_DECL
)
5819 place_field (rli
, field
);
5820 /* If the static data member has incomplete type, keep track
5821 of it so that it can be completed later. (The handling
5822 of pending statics in finish_record_layout is
5823 insufficient; consider:
5826 struct S2 { static S1 s1; };
5828 At this point, finish_record_layout will be called, but
5829 S1 is still incomplete.) */
5830 if (TREE_CODE (field
) == VAR_DECL
)
5832 maybe_register_incomplete_var (field
);
5833 /* The visibility of static data members is determined
5834 at their point of declaration, not their point of
5836 determine_visibility (field
);
5841 type
= TREE_TYPE (field
);
5842 if (type
== error_mark_node
)
5845 padding
= NULL_TREE
;
5847 /* If this field is a bit-field whose width is greater than its
5848 type, then there are some special rules for allocating
5850 if (DECL_C_BIT_FIELD (field
)
5851 && INT_CST_LT (TYPE_SIZE (type
), DECL_SIZE (field
)))
5855 bool was_unnamed_p
= false;
5856 /* We must allocate the bits as if suitably aligned for the
5857 longest integer type that fits in this many bits. type
5858 of the field. Then, we are supposed to use the left over
5859 bits as additional padding. */
5860 for (itk
= itk_char
; itk
!= itk_none
; ++itk
)
5861 if (integer_types
[itk
] != NULL_TREE
5862 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE
),
5863 TYPE_SIZE (integer_types
[itk
]))
5864 || INT_CST_LT (DECL_SIZE (field
),
5865 TYPE_SIZE (integer_types
[itk
]))))
5868 /* ITK now indicates a type that is too large for the
5869 field. We have to back up by one to find the largest
5874 integer_type
= integer_types
[itk
];
5875 } while (itk
> 0 && integer_type
== NULL_TREE
);
5877 /* Figure out how much additional padding is required. GCC
5878 3.2 always created a padding field, even if it had zero
5880 if (!abi_version_at_least (2)
5881 || INT_CST_LT (TYPE_SIZE (integer_type
), DECL_SIZE (field
)))
5883 if (abi_version_at_least (2) && TREE_CODE (t
) == UNION_TYPE
)
5884 /* In a union, the padding field must have the full width
5885 of the bit-field; all fields start at offset zero. */
5886 padding
= DECL_SIZE (field
);
5889 if (TREE_CODE (t
) == UNION_TYPE
)
5890 warning (OPT_Wabi
, "size assigned to %qT may not be "
5891 "ABI-compliant and may change in a future "
5894 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
5895 TYPE_SIZE (integer_type
));
5898 #ifdef PCC_BITFIELD_TYPE_MATTERS
5899 /* An unnamed bitfield does not normally affect the
5900 alignment of the containing class on a target where
5901 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5902 make any exceptions for unnamed bitfields when the
5903 bitfields are longer than their types. Therefore, we
5904 temporarily give the field a name. */
5905 if (PCC_BITFIELD_TYPE_MATTERS
&& !DECL_NAME (field
))
5907 was_unnamed_p
= true;
5908 DECL_NAME (field
) = make_anon_name ();
5911 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
5912 DECL_ALIGN (field
) = TYPE_ALIGN (integer_type
);
5913 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
5914 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
5915 empty_base_offsets
);
5917 DECL_NAME (field
) = NULL_TREE
;
5918 /* Now that layout has been performed, set the size of the
5919 field to the size of its declared type; the rest of the
5920 field is effectively invisible. */
5921 DECL_SIZE (field
) = TYPE_SIZE (type
);
5922 /* We must also reset the DECL_MODE of the field. */
5923 if (abi_version_at_least (2))
5924 DECL_MODE (field
) = TYPE_MODE (type
);
5926 && DECL_MODE (field
) != TYPE_MODE (type
))
5927 /* Versions of G++ before G++ 3.4 did not reset the
5930 "the offset of %qD may not be ABI-compliant and may "
5931 "change in a future version of GCC", field
);
5934 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
5935 empty_base_offsets
);
5937 /* Remember the location of any empty classes in FIELD. */
5938 if (abi_version_at_least (2))
5939 record_subobject_offsets (TREE_TYPE (field
),
5940 byte_position(field
),
5942 /*is_data_member=*/true);
5944 /* If a bit-field does not immediately follow another bit-field,
5945 and yet it starts in the middle of a byte, we have failed to
5946 comply with the ABI. */
5948 && DECL_C_BIT_FIELD (field
)
5949 /* The TREE_NO_WARNING flag gets set by Objective-C when
5950 laying out an Objective-C class. The ObjC ABI differs
5951 from the C++ ABI, and so we do not want a warning
5953 && !TREE_NO_WARNING (field
)
5954 && !last_field_was_bitfield
5955 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
,
5956 DECL_FIELD_BIT_OFFSET (field
),
5957 bitsize_unit_node
)))
5958 warning (OPT_Wabi
, "offset of %q+D is not ABI-compliant and may "
5959 "change in a future version of GCC", field
);
5961 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5962 offset of the field. */
5964 && !abi_version_at_least (2)
5965 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field
),
5966 byte_position (field
))
5967 && contains_empty_class_p (TREE_TYPE (field
)))
5968 warning (OPT_Wabi
, "%q+D contains empty classes which may cause base "
5969 "classes to be placed at different locations in a "
5970 "future version of GCC", field
);
5972 /* The middle end uses the type of expressions to determine the
5973 possible range of expression values. In order to optimize
5974 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5975 must be made aware of the width of "i", via its type.
5977 Because C++ does not have integer types of arbitrary width,
5978 we must (for the purposes of the front end) convert from the
5979 type assigned here to the declared type of the bitfield
5980 whenever a bitfield expression is used as an rvalue.
5981 Similarly, when assigning a value to a bitfield, the value
5982 must be converted to the type given the bitfield here. */
5983 if (DECL_C_BIT_FIELD (field
))
5985 unsigned HOST_WIDE_INT width
;
5986 tree ftype
= TREE_TYPE (field
);
5987 width
= tree_low_cst (DECL_SIZE (field
), /*unsignedp=*/1);
5988 if (width
!= TYPE_PRECISION (ftype
))
5991 = c_build_bitfield_integer_type (width
,
5992 TYPE_UNSIGNED (ftype
));
5994 = cp_build_qualified_type (TREE_TYPE (field
),
5995 cp_type_quals (ftype
));
5999 /* If we needed additional padding after this field, add it
6005 padding_field
= build_decl (input_location
,
6009 DECL_BIT_FIELD (padding_field
) = 1;
6010 DECL_SIZE (padding_field
) = padding
;
6011 DECL_CONTEXT (padding_field
) = t
;
6012 DECL_ARTIFICIAL (padding_field
) = 1;
6013 DECL_IGNORED_P (padding_field
) = 1;
6014 layout_nonempty_base_or_field (rli
, padding_field
,
6016 empty_base_offsets
);
6019 last_field_was_bitfield
= DECL_C_BIT_FIELD (field
);
6022 if (abi_version_at_least (2) && !integer_zerop (rli
->bitpos
))
6024 /* Make sure that we are on a byte boundary so that the size of
6025 the class without virtual bases will always be a round number
6027 rli
->bitpos
= round_up_loc (input_location
, rli
->bitpos
, BITS_PER_UNIT
);
6028 normalize_rli (rli
);
6031 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
6033 if (!abi_version_at_least (2))
6034 include_empty_classes(rli
);
6036 /* Delete all zero-width bit-fields from the list of fields. Now
6037 that the type is laid out they are no longer important. */
6038 remove_zero_width_bit_fields (t
);
6040 /* Create the version of T used for virtual bases. We do not use
6041 make_class_type for this version; this is an artificial type. For
6042 a POD type, we just reuse T. */
6043 if (CLASSTYPE_NON_LAYOUT_POD_P (t
) || CLASSTYPE_EMPTY_P (t
))
6045 base_t
= make_node (TREE_CODE (t
));
6047 /* Set the size and alignment for the new type. In G++ 3.2, all
6048 empty classes were considered to have size zero when used as
6050 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t
))
6052 TYPE_SIZE (base_t
) = bitsize_zero_node
;
6053 TYPE_SIZE_UNIT (base_t
) = size_zero_node
;
6054 if (warn_abi
&& !integer_zerop (rli_size_unit_so_far (rli
)))
6056 "layout of classes derived from empty class %qT "
6057 "may change in a future version of GCC",
6064 /* If the ABI version is not at least two, and the last
6065 field was a bit-field, RLI may not be on a byte
6066 boundary. In particular, rli_size_unit_so_far might
6067 indicate the last complete byte, while rli_size_so_far
6068 indicates the total number of bits used. Therefore,
6069 rli_size_so_far, rather than rli_size_unit_so_far, is
6070 used to compute TYPE_SIZE_UNIT. */
6071 eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
6072 TYPE_SIZE_UNIT (base_t
)
6073 = size_binop (MAX_EXPR
,
6075 size_binop (CEIL_DIV_EXPR
,
6076 rli_size_so_far (rli
),
6077 bitsize_int (BITS_PER_UNIT
))),
6080 = size_binop (MAX_EXPR
,
6081 rli_size_so_far (rli
),
6082 size_binop (MULT_EXPR
,
6083 convert (bitsizetype
, eoc
),
6084 bitsize_int (BITS_PER_UNIT
)));
6086 TYPE_ALIGN (base_t
) = rli
->record_align
;
6087 TYPE_USER_ALIGN (base_t
) = TYPE_USER_ALIGN (t
);
6089 /* Copy the fields from T. */
6090 next_field
= &TYPE_FIELDS (base_t
);
6091 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6092 if (TREE_CODE (field
) == FIELD_DECL
)
6094 *next_field
= build_decl (input_location
,
6098 DECL_CONTEXT (*next_field
) = base_t
;
6099 DECL_FIELD_OFFSET (*next_field
) = DECL_FIELD_OFFSET (field
);
6100 DECL_FIELD_BIT_OFFSET (*next_field
)
6101 = DECL_FIELD_BIT_OFFSET (field
);
6102 DECL_SIZE (*next_field
) = DECL_SIZE (field
);
6103 DECL_MODE (*next_field
) = DECL_MODE (field
);
6104 next_field
= &DECL_CHAIN (*next_field
);
6107 /* Record the base version of the type. */
6108 CLASSTYPE_AS_BASE (t
) = base_t
;
6109 TYPE_CONTEXT (base_t
) = t
;
6112 CLASSTYPE_AS_BASE (t
) = t
;
6114 /* Every empty class contains an empty class. */
6115 if (CLASSTYPE_EMPTY_P (t
))
6116 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 1;
6118 /* Set the TYPE_DECL for this type to contain the right
6119 value for DECL_OFFSET, so that we can use it as part
6120 of a COMPONENT_REF for multiple inheritance. */
6121 layout_decl (TYPE_MAIN_DECL (t
), 0);
6123 /* Now fix up any virtual base class types that we left lying
6124 around. We must get these done before we try to lay out the
6125 virtual function table. As a side-effect, this will remove the
6126 base subobject fields. */
6127 layout_virtual_bases (rli
, empty_base_offsets
);
6129 /* Make sure that empty classes are reflected in RLI at this
6131 include_empty_classes(rli
);
6133 /* Make sure not to create any structures with zero size. */
6134 if (integer_zerop (rli_size_unit_so_far (rli
)) && CLASSTYPE_EMPTY_P (t
))
6136 build_decl (input_location
,
6137 FIELD_DECL
, NULL_TREE
, char_type_node
));
6139 /* If this is a non-POD, declaring it packed makes a difference to how it
6140 can be used as a field; don't let finalize_record_size undo it. */
6141 if (TYPE_PACKED (t
) && !layout_pod_type_p (t
))
6142 rli
->packed_maybe_necessary
= true;
6144 /* Let the back end lay out the type. */
6145 finish_record_layout (rli
, /*free_p=*/true);
6147 /* Warn about bases that can't be talked about due to ambiguity. */
6148 warn_about_ambiguous_bases (t
);
6150 /* Now that we're done with layout, give the base fields the real types. */
6151 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
6152 if (DECL_ARTIFICIAL (field
) && IS_FAKE_BASE_TYPE (TREE_TYPE (field
)))
6153 TREE_TYPE (field
) = TYPE_CONTEXT (TREE_TYPE (field
));
6156 splay_tree_delete (empty_base_offsets
);
6158 if (CLASSTYPE_EMPTY_P (t
)
6159 && tree_int_cst_lt (sizeof_biggest_empty_class
,
6160 TYPE_SIZE_UNIT (t
)))
6161 sizeof_biggest_empty_class
= TYPE_SIZE_UNIT (t
);
6164 /* Determine the "key method" for the class type indicated by TYPE,
6165 and set CLASSTYPE_KEY_METHOD accordingly. */
6168 determine_key_method (tree type
)
6172 if (TYPE_FOR_JAVA (type
)
6173 || processing_template_decl
6174 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
6175 || CLASSTYPE_INTERFACE_KNOWN (type
))
6178 /* The key method is the first non-pure virtual function that is not
6179 inline at the point of class definition. On some targets the
6180 key function may not be inline; those targets should not call
6181 this function until the end of the translation unit. */
6182 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
6183 method
= DECL_CHAIN (method
))
6184 if (DECL_VINDEX (method
) != NULL_TREE
6185 && ! DECL_DECLARED_INLINE_P (method
)
6186 && ! DECL_PURE_VIRTUAL_P (method
))
6188 CLASSTYPE_KEY_METHOD (type
) = method
;
6196 /* Allocate and return an instance of struct sorted_fields_type with
6199 static struct sorted_fields_type
*
6200 sorted_fields_type_new (int n
)
6202 struct sorted_fields_type
*sft
;
6203 sft
= ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type
)
6204 + n
* sizeof (tree
));
6211 /* Perform processing required when the definition of T (a class type)
6215 finish_struct_1 (tree t
)
6218 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6219 tree virtuals
= NULL_TREE
;
6221 if (COMPLETE_TYPE_P (t
))
6223 gcc_assert (MAYBE_CLASS_TYPE_P (t
));
6224 error ("redefinition of %q#T", t
);
6229 /* If this type was previously laid out as a forward reference,
6230 make sure we lay it out again. */
6231 TYPE_SIZE (t
) = NULL_TREE
;
6232 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
6234 /* Make assumptions about the class; we'll reset the flags if
6236 CLASSTYPE_EMPTY_P (t
) = 1;
6237 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
6238 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t
) = 0;
6239 CLASSTYPE_LITERAL_P (t
) = true;
6241 /* Do end-of-class semantic processing: checking the validity of the
6242 bases and members and add implicitly generated methods. */
6243 check_bases_and_members (t
);
6245 /* Find the key method. */
6246 if (TYPE_CONTAINS_VPTR_P (t
))
6248 /* The Itanium C++ ABI permits the key method to be chosen when
6249 the class is defined -- even though the key method so
6250 selected may later turn out to be an inline function. On
6251 some systems (such as ARM Symbian OS) the key method cannot
6252 be determined until the end of the translation unit. On such
6253 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6254 will cause the class to be added to KEYED_CLASSES. Then, in
6255 finish_file we will determine the key method. */
6256 if (targetm
.cxx
.key_method_may_be_inline ())
6257 determine_key_method (t
);
6259 /* If a polymorphic class has no key method, we may emit the vtable
6260 in every translation unit where the class definition appears. */
6261 if (CLASSTYPE_KEY_METHOD (t
) == NULL_TREE
)
6262 keyed_classes
= tree_cons (NULL_TREE
, t
, keyed_classes
);
6265 /* Layout the class itself. */
6266 layout_class_type (t
, &virtuals
);
6267 if (CLASSTYPE_AS_BASE (t
) != t
)
6268 /* We use the base type for trivial assignments, and hence it
6270 compute_record_mode (CLASSTYPE_AS_BASE (t
));
6272 virtuals
= modify_all_vtables (t
, nreverse (virtuals
));
6274 /* If necessary, create the primary vtable for this class. */
6275 if (virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
6277 /* We must enter these virtuals into the table. */
6278 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6279 build_primary_vtable (NULL_TREE
, t
);
6280 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
6281 /* Here we know enough to change the type of our virtual
6282 function table, but we will wait until later this function. */
6283 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
6285 /* If we're warning about ABI tags, check the types of the new
6286 virtual functions. */
6288 for (tree v
= virtuals
; v
; v
= TREE_CHAIN (v
))
6289 check_abi_tags (t
, TREE_VALUE (v
));
6292 if (TYPE_CONTAINS_VPTR_P (t
))
6297 if (BINFO_VTABLE (TYPE_BINFO (t
)))
6298 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t
))));
6299 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
6300 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t
)) == NULL_TREE
);
6302 /* Add entries for virtual functions introduced by this class. */
6303 BINFO_VIRTUALS (TYPE_BINFO (t
))
6304 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t
)), virtuals
);
6306 /* Set DECL_VINDEX for all functions declared in this class. */
6307 for (vindex
= 0, fn
= BINFO_VIRTUALS (TYPE_BINFO (t
));
6309 fn
= TREE_CHAIN (fn
),
6310 vindex
+= (TARGET_VTABLE_USES_DESCRIPTORS
6311 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1))
6313 tree fndecl
= BV_FN (fn
);
6315 if (DECL_THUNK_P (fndecl
))
6316 /* A thunk. We should never be calling this entry directly
6317 from this vtable -- we'd use the entry for the non
6318 thunk base function. */
6319 DECL_VINDEX (fndecl
) = NULL_TREE
;
6320 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
6321 DECL_VINDEX (fndecl
) = build_int_cst (NULL_TREE
, vindex
);
6325 finish_struct_bits (t
);
6326 set_method_tm_attributes (t
);
6328 /* Complete the rtl for any static member objects of the type we're
6330 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
6331 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
6332 && TREE_TYPE (x
) != error_mark_node
6333 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
6334 DECL_MODE (x
) = TYPE_MODE (t
);
6336 /* Done with FIELDS...now decide whether to sort these for
6337 faster lookups later.
6339 We use a small number because most searches fail (succeeding
6340 ultimately as the search bores through the inheritance
6341 hierarchy), and we want this failure to occur quickly. */
6343 insert_into_classtype_sorted_fields (TYPE_FIELDS (t
), t
, 8);
6345 /* Complain if one of the field types requires lower visibility. */
6346 constrain_class_visibility (t
);
6348 /* Make the rtl for any new vtables we have created, and unmark
6349 the base types we marked. */
6352 /* Build the VTT for T. */
6355 /* This warning does not make sense for Java classes, since they
6356 cannot have destructors. */
6357 if (!TYPE_FOR_JAVA (t
) && warn_nonvdtor
&& TYPE_POLYMORPHIC_P (t
))
6361 dtor
= CLASSTYPE_DESTRUCTORS (t
);
6362 if (/* An implicitly declared destructor is always public. And,
6363 if it were virtual, we would have created it by now. */
6365 || (!DECL_VINDEX (dtor
)
6366 && (/* public non-virtual */
6367 (!TREE_PRIVATE (dtor
) && !TREE_PROTECTED (dtor
))
6368 || (/* non-public non-virtual with friends */
6369 (TREE_PRIVATE (dtor
) || TREE_PROTECTED (dtor
))
6370 && (CLASSTYPE_FRIEND_CLASSES (t
)
6371 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))))))
6372 warning (OPT_Wnon_virtual_dtor
,
6373 "%q#T has virtual functions and accessible"
6374 " non-virtual destructor", t
);
6379 if (warn_overloaded_virtual
)
6382 /* Class layout, assignment of virtual table slots, etc., is now
6383 complete. Give the back end a chance to tweak the visibility of
6384 the class or perform any other required target modifications. */
6385 targetm
.cxx
.adjust_class_at_definition (t
);
6387 maybe_suppress_debug_info (t
);
6389 dump_class_hierarchy (t
);
6391 /* Finish debugging output for this type. */
6392 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
6394 if (TYPE_TRANSPARENT_AGGR (t
))
6396 tree field
= first_field (t
);
6397 if (field
== NULL_TREE
|| error_operand_p (field
))
6399 error ("type transparent %q#T does not have any fields", t
);
6400 TYPE_TRANSPARENT_AGGR (t
) = 0;
6402 else if (DECL_ARTIFICIAL (field
))
6404 if (DECL_FIELD_IS_BASE (field
))
6405 error ("type transparent class %qT has base classes", t
);
6408 gcc_checking_assert (DECL_VIRTUAL_P (field
));
6409 error ("type transparent class %qT has virtual functions", t
);
6411 TYPE_TRANSPARENT_AGGR (t
) = 0;
6413 else if (TYPE_MODE (t
) != DECL_MODE (field
))
6415 error ("type transparent %q#T cannot be made transparent because "
6416 "the type of the first field has a different ABI from the "
6417 "class overall", t
);
6418 TYPE_TRANSPARENT_AGGR (t
) = 0;
6423 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6424 equal to THRESHOLD or greater than THRESHOLD. */
6427 insert_into_classtype_sorted_fields (tree fields
, tree t
, int threshold
)
6429 int n_fields
= count_fields (fields
);
6430 if (n_fields
>= threshold
)
6432 struct sorted_fields_type
*field_vec
= sorted_fields_type_new (n_fields
);
6433 add_fields_to_record_type (fields
, field_vec
, 0);
6434 qsort (field_vec
->elts
, n_fields
, sizeof (tree
), field_decl_cmp
);
6435 CLASSTYPE_SORTED_FIELDS (t
) = field_vec
;
6439 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6442 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype
, tree t
)
6444 struct sorted_fields_type
*sorted_fields
= CLASSTYPE_SORTED_FIELDS (t
);
6449 = list_length (TYPE_VALUES (enumtype
)) + sorted_fields
->len
;
6450 struct sorted_fields_type
*field_vec
= sorted_fields_type_new (n_fields
);
6452 for (i
= 0; i
< sorted_fields
->len
; ++i
)
6453 field_vec
->elts
[i
] = sorted_fields
->elts
[i
];
6455 add_enum_fields_to_record_type (enumtype
, field_vec
,
6456 sorted_fields
->len
);
6457 qsort (field_vec
->elts
, n_fields
, sizeof (tree
), field_decl_cmp
);
6458 CLASSTYPE_SORTED_FIELDS (t
) = field_vec
;
6462 /* When T was built up, the member declarations were added in reverse
6463 order. Rearrange them to declaration order. */
6466 unreverse_member_declarations (tree t
)
6472 /* The following lists are all in reverse order. Put them in
6473 declaration order now. */
6474 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
6475 CLASSTYPE_DECL_LIST (t
) = nreverse (CLASSTYPE_DECL_LIST (t
));
6477 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6478 reverse order, so we can't just use nreverse. */
6480 for (x
= TYPE_FIELDS (t
);
6481 x
&& TREE_CODE (x
) != TYPE_DECL
;
6484 next
= DECL_CHAIN (x
);
6485 DECL_CHAIN (x
) = prev
;
6490 DECL_CHAIN (TYPE_FIELDS (t
)) = x
;
6492 TYPE_FIELDS (t
) = prev
;
6497 finish_struct (tree t
, tree attributes
)
6499 location_t saved_loc
= input_location
;
6501 /* Now that we've got all the field declarations, reverse everything
6503 unreverse_member_declarations (t
);
6505 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
6507 /* Nadger the current location so that diagnostics point to the start of
6508 the struct, not the end. */
6509 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (t
));
6511 if (processing_template_decl
)
6515 finish_struct_methods (t
);
6516 TYPE_SIZE (t
) = bitsize_zero_node
;
6517 TYPE_SIZE_UNIT (t
) = size_zero_node
;
6519 /* We need to emit an error message if this type was used as a parameter
6520 and it is an abstract type, even if it is a template. We construct
6521 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6522 account and we call complete_vars with this type, which will check
6523 the PARM_DECLS. Note that while the type is being defined,
6524 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6525 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6526 CLASSTYPE_PURE_VIRTUALS (t
) = NULL
;
6527 for (x
= TYPE_METHODS (t
); x
; x
= DECL_CHAIN (x
))
6528 if (DECL_PURE_VIRTUAL_P (x
))
6529 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t
), x
);
6531 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6532 an enclosing scope is a template class, so that this function be
6533 found by lookup_fnfields_1 when the using declaration is not
6534 instantiated yet. */
6535 for (x
= TYPE_FIELDS (t
); x
; x
= DECL_CHAIN (x
))
6536 if (TREE_CODE (x
) == USING_DECL
)
6538 tree fn
= strip_using_decl (x
);
6539 if (is_overloaded_fn (fn
))
6540 for (; fn
; fn
= OVL_NEXT (fn
))
6541 add_method (t
, OVL_CURRENT (fn
), x
);
6544 /* Remember current #pragma pack value. */
6545 TYPE_PRECISION (t
) = maximum_field_alignment
;
6547 /* Fix up any variants we've already built. */
6548 for (x
= TYPE_NEXT_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
6550 TYPE_SIZE (x
) = TYPE_SIZE (t
);
6551 TYPE_SIZE_UNIT (x
) = TYPE_SIZE_UNIT (t
);
6552 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
6553 TYPE_METHODS (x
) = TYPE_METHODS (t
);
6557 finish_struct_1 (t
);
6559 input_location
= saved_loc
;
6561 TYPE_BEING_DEFINED (t
) = 0;
6563 if (current_class_type
)
6566 error ("trying to finish struct, but kicked out due to previous parse errors");
6568 if (processing_template_decl
&& at_function_scope_p ()
6569 /* Lambdas are defined by the LAMBDA_EXPR. */
6570 && !LAMBDA_TYPE_P (t
))
6571 add_stmt (build_min (TAG_DEFN
, t
));
6576 /* Hash table to avoid endless recursion when handling references. */
6577 static hash_table
<pointer_hash
<tree_node
> > fixed_type_or_null_ref_ht
;
6579 /* Return the dynamic type of INSTANCE, if known.
6580 Used to determine whether the virtual function table is needed
6583 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6584 of our knowledge of its type. *NONNULL should be initialized
6585 before this function is called. */
6588 fixed_type_or_null (tree instance
, int *nonnull
, int *cdtorp
)
6590 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6592 switch (TREE_CODE (instance
))
6595 if (POINTER_TYPE_P (TREE_TYPE (instance
)))
6598 return RECUR (TREE_OPERAND (instance
, 0));
6601 /* This is a call to a constructor, hence it's never zero. */
6602 if (TREE_HAS_CONSTRUCTOR (instance
))
6606 return TREE_TYPE (instance
);
6611 /* This is a call to a constructor, hence it's never zero. */
6612 if (TREE_HAS_CONSTRUCTOR (instance
))
6616 return TREE_TYPE (instance
);
6618 return RECUR (TREE_OPERAND (instance
, 0));
6620 case POINTER_PLUS_EXPR
:
6623 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
6624 return RECUR (TREE_OPERAND (instance
, 0));
6625 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
6626 /* Propagate nonnull. */
6627 return RECUR (TREE_OPERAND (instance
, 0));
6632 return RECUR (TREE_OPERAND (instance
, 0));
6635 instance
= TREE_OPERAND (instance
, 0);
6638 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6639 with a real object -- given &p->f, p can still be null. */
6640 tree t
= get_base_address (instance
);
6641 /* ??? Probably should check DECL_WEAK here. */
6642 if (t
&& DECL_P (t
))
6645 return RECUR (instance
);
6648 /* If this component is really a base class reference, then the field
6649 itself isn't definitive. */
6650 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance
, 1)))
6651 return RECUR (TREE_OPERAND (instance
, 0));
6652 return RECUR (TREE_OPERAND (instance
, 1));
6656 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
6657 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
6661 return TREE_TYPE (TREE_TYPE (instance
));
6663 /* fall through... */
6667 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance
)))
6671 return TREE_TYPE (instance
);
6673 else if (instance
== current_class_ptr
)
6678 /* if we're in a ctor or dtor, we know our type. If
6679 current_class_ptr is set but we aren't in a function, we're in
6680 an NSDMI (and therefore a constructor). */
6681 if (current_scope () != current_function_decl
6682 || (DECL_LANG_SPECIFIC (current_function_decl
)
6683 && (DECL_CONSTRUCTOR_P (current_function_decl
)
6684 || DECL_DESTRUCTOR_P (current_function_decl
))))
6688 return TREE_TYPE (TREE_TYPE (instance
));
6691 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
6693 /* We only need one hash table because it is always left empty. */
6694 if (!fixed_type_or_null_ref_ht
.is_created ())
6695 fixed_type_or_null_ref_ht
.create (37);
6697 /* Reference variables should be references to objects. */
6701 /* Enter the INSTANCE in a table to prevent recursion; a
6702 variable's initializer may refer to the variable
6704 if (TREE_CODE (instance
) == VAR_DECL
6705 && DECL_INITIAL (instance
)
6706 && !type_dependent_expression_p_push (DECL_INITIAL (instance
))
6707 && !fixed_type_or_null_ref_ht
.find (instance
))
6712 slot
= fixed_type_or_null_ref_ht
.find_slot (instance
, INSERT
);
6714 type
= RECUR (DECL_INITIAL (instance
));
6715 fixed_type_or_null_ref_ht
.remove_elt (instance
);
6728 /* Return nonzero if the dynamic type of INSTANCE is known, and
6729 equivalent to the static type. We also handle the case where
6730 INSTANCE is really a pointer. Return negative if this is a
6731 ctor/dtor. There the dynamic type is known, but this might not be
6732 the most derived base of the original object, and hence virtual
6733 bases may not be layed out according to this type.
6735 Used to determine whether the virtual function table is needed
6738 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6739 of our knowledge of its type. *NONNULL should be initialized
6740 before this function is called. */
6743 resolves_to_fixed_type_p (tree instance
, int* nonnull
)
6745 tree t
= TREE_TYPE (instance
);
6749 /* processing_template_decl can be false in a template if we're in
6750 fold_non_dependent_expr, but we still want to suppress this check. */
6751 if (in_template_function ())
6753 /* In a template we only care about the type of the result. */
6759 fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
6760 if (fixed
== NULL_TREE
)
6762 if (POINTER_TYPE_P (t
))
6764 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
6766 return cdtorp
? -1 : 1;
6771 init_class_processing (void)
6773 current_class_depth
= 0;
6774 current_class_stack_size
= 10;
6776 = XNEWVEC (struct class_stack_node
, current_class_stack_size
);
6777 vec_alloc (local_classes
, 8);
6778 sizeof_biggest_empty_class
= size_zero_node
;
6780 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
6781 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
6782 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
6785 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6788 restore_class_cache (void)
6792 /* We are re-entering the same class we just left, so we don't
6793 have to search the whole inheritance matrix to find all the
6794 decls to bind again. Instead, we install the cached
6795 class_shadowed list and walk through it binding names. */
6796 push_binding_level (previous_class_level
);
6797 class_binding_level
= previous_class_level
;
6798 /* Restore IDENTIFIER_TYPE_VALUE. */
6799 for (type
= class_binding_level
->type_shadowed
;
6801 type
= TREE_CHAIN (type
))
6802 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type
), TREE_TYPE (type
));
6805 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6806 appropriate for TYPE.
6808 So that we may avoid calls to lookup_name, we cache the _TYPE
6809 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6811 For multiple inheritance, we perform a two-pass depth-first search
6812 of the type lattice. */
6815 pushclass (tree type
)
6817 class_stack_node_t csn
;
6819 type
= TYPE_MAIN_VARIANT (type
);
6821 /* Make sure there is enough room for the new entry on the stack. */
6822 if (current_class_depth
+ 1 >= current_class_stack_size
)
6824 current_class_stack_size
*= 2;
6826 = XRESIZEVEC (struct class_stack_node
, current_class_stack
,
6827 current_class_stack_size
);
6830 /* Insert a new entry on the class stack. */
6831 csn
= current_class_stack
+ current_class_depth
;
6832 csn
->name
= current_class_name
;
6833 csn
->type
= current_class_type
;
6834 csn
->access
= current_access_specifier
;
6835 csn
->names_used
= 0;
6837 current_class_depth
++;
6839 /* Now set up the new type. */
6840 current_class_name
= TYPE_NAME (type
);
6841 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
6842 current_class_name
= DECL_NAME (current_class_name
);
6843 current_class_type
= type
;
6845 /* By default, things in classes are private, while things in
6846 structures or unions are public. */
6847 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
6848 ? access_private_node
6849 : access_public_node
);
6851 if (previous_class_level
6852 && type
!= previous_class_level
->this_entity
6853 && current_class_depth
== 1)
6855 /* Forcibly remove any old class remnants. */
6856 invalidate_class_lookup_cache ();
6859 if (!previous_class_level
6860 || type
!= previous_class_level
->this_entity
6861 || current_class_depth
> 1)
6864 restore_class_cache ();
6867 /* When we exit a toplevel class scope, we save its binding level so
6868 that we can restore it quickly. Here, we've entered some other
6869 class, so we must invalidate our cache. */
6872 invalidate_class_lookup_cache (void)
6874 previous_class_level
= NULL
;
6877 /* Get out of the current class scope. If we were in a class scope
6878 previously, that is the one popped to. */
6885 current_class_depth
--;
6886 current_class_name
= current_class_stack
[current_class_depth
].name
;
6887 current_class_type
= current_class_stack
[current_class_depth
].type
;
6888 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
6889 if (current_class_stack
[current_class_depth
].names_used
)
6890 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
6893 /* Mark the top of the class stack as hidden. */
6896 push_class_stack (void)
6898 if (current_class_depth
)
6899 ++current_class_stack
[current_class_depth
- 1].hidden
;
6902 /* Mark the top of the class stack as un-hidden. */
6905 pop_class_stack (void)
6907 if (current_class_depth
)
6908 --current_class_stack
[current_class_depth
- 1].hidden
;
6911 /* Returns 1 if the class type currently being defined is either T or
6912 a nested type of T. */
6915 currently_open_class (tree t
)
6919 if (!CLASS_TYPE_P (t
))
6922 t
= TYPE_MAIN_VARIANT (t
);
6924 /* We start looking from 1 because entry 0 is from global scope,
6926 for (i
= current_class_depth
; i
> 0; --i
)
6929 if (i
== current_class_depth
)
6930 c
= current_class_type
;
6933 if (current_class_stack
[i
].hidden
)
6935 c
= current_class_stack
[i
].type
;
6939 if (same_type_p (c
, t
))
6945 /* If either current_class_type or one of its enclosing classes are derived
6946 from T, return the appropriate type. Used to determine how we found
6947 something via unqualified lookup. */
6950 currently_open_derived_class (tree t
)
6954 /* The bases of a dependent type are unknown. */
6955 if (dependent_type_p (t
))
6958 if (!current_class_type
)
6961 if (DERIVED_FROM_P (t
, current_class_type
))
6962 return current_class_type
;
6964 for (i
= current_class_depth
- 1; i
> 0; --i
)
6966 if (current_class_stack
[i
].hidden
)
6968 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
6969 return current_class_stack
[i
].type
;
6975 /* Returns the innermost class type which is not a lambda closure type. */
6978 current_nonlambda_class_type (void)
6982 /* We start looking from 1 because entry 0 is from global scope,
6984 for (i
= current_class_depth
; i
> 0; --i
)
6987 if (i
== current_class_depth
)
6988 c
= current_class_type
;
6991 if (current_class_stack
[i
].hidden
)
6993 c
= current_class_stack
[i
].type
;
6997 if (!LAMBDA_TYPE_P (c
))
7003 /* When entering a class scope, all enclosing class scopes' names with
7004 static meaning (static variables, static functions, types and
7005 enumerators) have to be visible. This recursive function calls
7006 pushclass for all enclosing class contexts until global or a local
7007 scope is reached. TYPE is the enclosed class. */
7010 push_nested_class (tree type
)
7012 /* A namespace might be passed in error cases, like A::B:C. */
7013 if (type
== NULL_TREE
7014 || !CLASS_TYPE_P (type
))
7017 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
7022 /* Undoes a push_nested_class call. */
7025 pop_nested_class (void)
7027 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
7030 if (context
&& CLASS_TYPE_P (context
))
7031 pop_nested_class ();
7034 /* Returns the number of extern "LANG" blocks we are nested within. */
7037 current_lang_depth (void)
7039 return vec_safe_length (current_lang_base
);
7042 /* Set global variables CURRENT_LANG_NAME to appropriate value
7043 so that behavior of name-mangling machinery is correct. */
7046 push_lang_context (tree name
)
7048 vec_safe_push (current_lang_base
, current_lang_name
);
7050 if (name
== lang_name_cplusplus
)
7052 current_lang_name
= name
;
7054 else if (name
== lang_name_java
)
7056 current_lang_name
= name
;
7057 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7058 (See record_builtin_java_type in decl.c.) However, that causes
7059 incorrect debug entries if these types are actually used.
7060 So we re-enable debug output after extern "Java". */
7061 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node
)) = 0;
7062 DECL_IGNORED_P (TYPE_NAME (java_short_type_node
)) = 0;
7063 DECL_IGNORED_P (TYPE_NAME (java_int_type_node
)) = 0;
7064 DECL_IGNORED_P (TYPE_NAME (java_long_type_node
)) = 0;
7065 DECL_IGNORED_P (TYPE_NAME (java_float_type_node
)) = 0;
7066 DECL_IGNORED_P (TYPE_NAME (java_double_type_node
)) = 0;
7067 DECL_IGNORED_P (TYPE_NAME (java_char_type_node
)) = 0;
7068 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node
)) = 0;
7070 else if (name
== lang_name_c
)
7072 current_lang_name
= name
;
7075 error ("language string %<\"%E\"%> not recognized", name
);
7078 /* Get out of the current language scope. */
7081 pop_lang_context (void)
7083 current_lang_name
= current_lang_base
->pop ();
7086 /* Type instantiation routines. */
7088 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7089 matches the TARGET_TYPE. If there is no satisfactory match, return
7090 error_mark_node, and issue an error & warning messages under
7091 control of FLAGS. Permit pointers to member function if FLAGS
7092 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7093 a template-id, and EXPLICIT_TARGS are the explicitly provided
7096 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7097 is the base path used to reference those member functions. If
7098 the address is resolved to a member function, access checks will be
7099 performed and errors issued if appropriate. */
7102 resolve_address_of_overloaded_function (tree target_type
,
7104 tsubst_flags_t flags
,
7106 tree explicit_targs
,
7109 /* Here's what the standard says:
7113 If the name is a function template, template argument deduction
7114 is done, and if the argument deduction succeeds, the deduced
7115 arguments are used to generate a single template function, which
7116 is added to the set of overloaded functions considered.
7118 Non-member functions and static member functions match targets of
7119 type "pointer-to-function" or "reference-to-function." Nonstatic
7120 member functions match targets of type "pointer-to-member
7121 function;" the function type of the pointer to member is used to
7122 select the member function from the set of overloaded member
7123 functions. If a nonstatic member function is selected, the
7124 reference to the overloaded function name is required to have the
7125 form of a pointer to member as described in 5.3.1.
7127 If more than one function is selected, any template functions in
7128 the set are eliminated if the set also contains a non-template
7129 function, and any given template function is eliminated if the
7130 set contains a second template function that is more specialized
7131 than the first according to the partial ordering rules 14.5.5.2.
7132 After such eliminations, if any, there shall remain exactly one
7133 selected function. */
7136 /* We store the matches in a TREE_LIST rooted here. The functions
7137 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7138 interoperability with most_specialized_instantiation. */
7139 tree matches
= NULL_TREE
;
7141 tree target_fn_type
;
7143 /* By the time we get here, we should be seeing only real
7144 pointer-to-member types, not the internal POINTER_TYPE to
7145 METHOD_TYPE representation. */
7146 gcc_assert (TREE_CODE (target_type
) != POINTER_TYPE
7147 || TREE_CODE (TREE_TYPE (target_type
)) != METHOD_TYPE
);
7149 gcc_assert (is_overloaded_fn (overload
));
7151 /* Check that the TARGET_TYPE is reasonable. */
7152 if (TYPE_PTRFN_P (target_type
))
7154 else if (TYPE_PTRMEMFUNC_P (target_type
))
7155 /* This is OK, too. */
7157 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
7158 /* This is OK, too. This comes from a conversion to reference
7160 target_type
= build_reference_type (target_type
);
7163 if (flags
& tf_error
)
7164 error ("cannot resolve overloaded function %qD based on"
7165 " conversion to type %qT",
7166 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
7167 return error_mark_node
;
7170 /* Non-member functions and static member functions match targets of type
7171 "pointer-to-function" or "reference-to-function." Nonstatic member
7172 functions match targets of type "pointer-to-member-function;" the
7173 function type of the pointer to member is used to select the member
7174 function from the set of overloaded member functions.
7176 So figure out the FUNCTION_TYPE that we want to match against. */
7177 target_fn_type
= static_fn_type (target_type
);
7179 /* If we can find a non-template function that matches, we can just
7180 use it. There's no point in generating template instantiations
7181 if we're just going to throw them out anyhow. But, of course, we
7182 can only do this when we don't *need* a template function. */
7187 for (fns
= overload
; fns
; fns
= OVL_NEXT (fns
))
7189 tree fn
= OVL_CURRENT (fns
);
7191 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
7192 /* We're not looking for templates just yet. */
7195 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
7197 /* We're looking for a non-static member, and this isn't
7198 one, or vice versa. */
7201 /* Ignore functions which haven't been explicitly
7203 if (DECL_ANTICIPATED (fn
))
7206 /* See if there's a match. */
7207 if (same_type_p (target_fn_type
, static_fn_type (fn
)))
7208 matches
= tree_cons (fn
, NULL_TREE
, matches
);
7212 /* Now, if we've already got a match (or matches), there's no need
7213 to proceed to the template functions. But, if we don't have a
7214 match we need to look at them, too. */
7217 tree target_arg_types
;
7218 tree target_ret_type
;
7221 unsigned int nargs
, ia
;
7224 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
7225 target_ret_type
= TREE_TYPE (target_fn_type
);
7227 nargs
= list_length (target_arg_types
);
7228 args
= XALLOCAVEC (tree
, nargs
);
7229 for (arg
= target_arg_types
, ia
= 0;
7230 arg
!= NULL_TREE
&& arg
!= void_list_node
;
7231 arg
= TREE_CHAIN (arg
), ++ia
)
7232 args
[ia
] = TREE_VALUE (arg
);
7235 for (fns
= overload
; fns
; fns
= OVL_NEXT (fns
))
7237 tree fn
= OVL_CURRENT (fns
);
7241 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7242 /* We're only looking for templates. */
7245 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
7247 /* We're not looking for a non-static member, and this is
7248 one, or vice versa. */
7251 /* Try to do argument deduction. */
7252 targs
= make_tree_vec (DECL_NTPARMS (fn
));
7253 instantiation
= fn_type_unification (fn
, explicit_targs
, targs
, args
,
7254 nargs
, target_ret_type
,
7255 DEDUCE_EXACT
, LOOKUP_NORMAL
,
7257 if (instantiation
== error_mark_node
)
7258 /* Instantiation failed. */
7261 /* See if there's a match. */
7262 if (same_type_p (target_fn_type
, static_fn_type (instantiation
)))
7263 matches
= tree_cons (instantiation
, fn
, matches
);
7266 /* Now, remove all but the most specialized of the matches. */
7269 tree match
= most_specialized_instantiation (matches
);
7271 if (match
!= error_mark_node
)
7272 matches
= tree_cons (TREE_PURPOSE (match
),
7278 /* Now we should have exactly one function in MATCHES. */
7279 if (matches
== NULL_TREE
)
7281 /* There were *no* matches. */
7282 if (flags
& tf_error
)
7284 error ("no matches converting function %qD to type %q#T",
7285 DECL_NAME (OVL_CURRENT (overload
)),
7288 print_candidates (overload
);
7290 return error_mark_node
;
7292 else if (TREE_CHAIN (matches
))
7294 /* There were too many matches. First check if they're all
7295 the same function. */
7296 tree match
= NULL_TREE
;
7298 fn
= TREE_PURPOSE (matches
);
7300 /* For multi-versioned functions, more than one match is just fine and
7301 decls_match will return false as they are different. */
7302 for (match
= TREE_CHAIN (matches
); match
; match
= TREE_CHAIN (match
))
7303 if (!decls_match (fn
, TREE_PURPOSE (match
))
7304 && !targetm
.target_option
.function_versions
7305 (fn
, TREE_PURPOSE (match
)))
7310 if (flags
& tf_error
)
7312 error ("converting overloaded function %qD to type %q#T is ambiguous",
7313 DECL_NAME (OVL_FUNCTION (overload
)),
7316 /* Since print_candidates expects the functions in the
7317 TREE_VALUE slot, we flip them here. */
7318 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
7319 TREE_VALUE (match
) = TREE_PURPOSE (match
);
7321 print_candidates (matches
);
7324 return error_mark_node
;
7328 /* Good, exactly one match. Now, convert it to the correct type. */
7329 fn
= TREE_PURPOSE (matches
);
7331 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
7332 && !(flags
& tf_ptrmem_ok
) && !flag_ms_extensions
)
7334 static int explained
;
7336 if (!(flags
& tf_error
))
7337 return error_mark_node
;
7339 permerror (input_location
, "assuming pointer to member %qD", fn
);
7342 inform (input_location
, "(a pointer to member can only be formed with %<&%E%>)", fn
);
7347 /* If a pointer to a function that is multi-versioned is requested, the
7348 pointer to the dispatcher function is returned instead. This works
7349 well because indirectly calling the function will dispatch the right
7350 function version at run-time. */
7351 if (DECL_FUNCTION_VERSIONED (fn
))
7353 fn
= get_function_version_dispatcher (fn
);
7355 return error_mark_node
;
7356 /* Mark all the versions corresponding to the dispatcher as used. */
7357 if (!(flags
& tf_conv
))
7358 mark_versions_used (fn
);
7361 /* If we're doing overload resolution purely for the purpose of
7362 determining conversion sequences, we should not consider the
7363 function used. If this conversion sequence is selected, the
7364 function will be marked as used at this point. */
7365 if (!(flags
& tf_conv
))
7367 /* Make =delete work with SFINAE. */
7368 if (DECL_DELETED_FN (fn
) && !(flags
& tf_error
))
7369 return error_mark_node
;
7374 /* We could not check access to member functions when this
7375 expression was originally created since we did not know at that
7376 time to which function the expression referred. */
7377 if (DECL_FUNCTION_MEMBER_P (fn
))
7379 gcc_assert (access_path
);
7380 perform_or_defer_access_check (access_path
, fn
, fn
, flags
);
7383 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
7384 return cp_build_addr_expr (fn
, flags
);
7387 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7388 will mark the function as addressed, but here we must do it
7390 cxx_mark_addressable (fn
);
7396 /* This function will instantiate the type of the expression given in
7397 RHS to match the type of LHSTYPE. If errors exist, then return
7398 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7399 we complain on errors. If we are not complaining, never modify rhs,
7400 as overload resolution wants to try many possible instantiations, in
7401 the hope that at least one will work.
7403 For non-recursive calls, LHSTYPE should be a function, pointer to
7404 function, or a pointer to member function. */
7407 instantiate_type (tree lhstype
, tree rhs
, tsubst_flags_t flags
)
7409 tsubst_flags_t flags_in
= flags
;
7410 tree access_path
= NULL_TREE
;
7412 flags
&= ~tf_ptrmem_ok
;
7414 if (lhstype
== unknown_type_node
)
7416 if (flags
& tf_error
)
7417 error ("not enough type information");
7418 return error_mark_node
;
7421 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
7423 if (same_type_p (lhstype
, TREE_TYPE (rhs
)))
7425 if (flag_ms_extensions
7426 && TYPE_PTRMEMFUNC_P (lhstype
)
7427 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
7428 /* Microsoft allows `A::f' to be resolved to a
7429 pointer-to-member. */
7433 if (flags
& tf_error
)
7434 error ("cannot convert %qE from type %qT to type %qT",
7435 rhs
, TREE_TYPE (rhs
), lhstype
);
7436 return error_mark_node
;
7440 if (BASELINK_P (rhs
))
7442 access_path
= BASELINK_ACCESS_BINFO (rhs
);
7443 rhs
= BASELINK_FUNCTIONS (rhs
);
7446 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7447 deduce any type information. */
7448 if (TREE_CODE (rhs
) == NON_DEPENDENT_EXPR
)
7450 if (flags
& tf_error
)
7451 error ("not enough type information");
7452 return error_mark_node
;
7455 /* There only a few kinds of expressions that may have a type
7456 dependent on overload resolution. */
7457 gcc_assert (TREE_CODE (rhs
) == ADDR_EXPR
7458 || TREE_CODE (rhs
) == COMPONENT_REF
7459 || really_overloaded_fn (rhs
)
7460 || (flag_ms_extensions
&& TREE_CODE (rhs
) == FUNCTION_DECL
));
7462 /* This should really only be used when attempting to distinguish
7463 what sort of a pointer to function we have. For now, any
7464 arithmetic operation which is not supported on pointers
7465 is rejected as an error. */
7467 switch (TREE_CODE (rhs
))
7471 tree member
= TREE_OPERAND (rhs
, 1);
7473 member
= instantiate_type (lhstype
, member
, flags
);
7474 if (member
!= error_mark_node
7475 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs
, 0)))
7476 /* Do not lose object's side effects. */
7477 return build2 (COMPOUND_EXPR
, TREE_TYPE (member
),
7478 TREE_OPERAND (rhs
, 0), member
);
7483 rhs
= TREE_OPERAND (rhs
, 1);
7484 if (BASELINK_P (rhs
))
7485 return instantiate_type (lhstype
, rhs
, flags_in
);
7487 /* This can happen if we are forming a pointer-to-member for a
7489 gcc_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
);
7493 case TEMPLATE_ID_EXPR
:
7495 tree fns
= TREE_OPERAND (rhs
, 0);
7496 tree args
= TREE_OPERAND (rhs
, 1);
7499 resolve_address_of_overloaded_function (lhstype
, fns
, flags_in
,
7500 /*template_only=*/true,
7507 resolve_address_of_overloaded_function (lhstype
, rhs
, flags_in
,
7508 /*template_only=*/false,
7509 /*explicit_targs=*/NULL_TREE
,
7514 if (PTRMEM_OK_P (rhs
))
7515 flags
|= tf_ptrmem_ok
;
7517 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
7521 return error_mark_node
;
7526 return error_mark_node
;
7529 /* Return the name of the virtual function pointer field
7530 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7531 this may have to look back through base types to find the
7532 ultimate field name. (For single inheritance, these could
7533 all be the same name. Who knows for multiple inheritance). */
7536 get_vfield_name (tree type
)
7538 tree binfo
, base_binfo
;
7541 for (binfo
= TYPE_BINFO (type
);
7542 BINFO_N_BASE_BINFOS (binfo
);
7545 base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
7547 if (BINFO_VIRTUAL_P (base_binfo
)
7548 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo
)))
7552 type
= BINFO_TYPE (binfo
);
7553 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
7554 + TYPE_NAME_LENGTH (type
) + 2);
7555 sprintf (buf
, VFIELD_NAME_FORMAT
,
7556 IDENTIFIER_POINTER (constructor_name (type
)));
7557 return get_identifier (buf
);
7561 print_class_statistics (void)
7563 if (! GATHER_STATISTICS
)
7566 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
7567 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
7570 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
7571 n_vtables
, n_vtable_searches
);
7572 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
7573 n_vtable_entries
, n_vtable_elems
);
7577 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7578 according to [class]:
7579 The class-name is also inserted
7580 into the scope of the class itself. For purposes of access checking,
7581 the inserted class name is treated as if it were a public member name. */
7584 build_self_reference (void)
7586 tree name
= constructor_name (current_class_type
);
7587 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
7590 DECL_NONLOCAL (value
) = 1;
7591 DECL_CONTEXT (value
) = current_class_type
;
7592 DECL_ARTIFICIAL (value
) = 1;
7593 SET_DECL_SELF_REFERENCE_P (value
);
7594 set_underlying_type (value
);
7596 if (processing_template_decl
)
7597 value
= push_template_decl (value
);
7599 saved_cas
= current_access_specifier
;
7600 current_access_specifier
= access_public_node
;
7601 finish_member_declaration (value
);
7602 current_access_specifier
= saved_cas
;
7605 /* Returns 1 if TYPE contains only padding bytes. */
7608 is_empty_class (tree type
)
7610 if (type
== error_mark_node
)
7613 if (! CLASS_TYPE_P (type
))
7616 /* In G++ 3.2, whether or not a class was empty was determined by
7617 looking at its size. */
7618 if (abi_version_at_least (2))
7619 return CLASSTYPE_EMPTY_P (type
);
7621 return integer_zerop (CLASSTYPE_SIZE (type
));
7624 /* Returns true if TYPE contains an empty class. */
7627 contains_empty_class_p (tree type
)
7629 if (is_empty_class (type
))
7631 if (CLASS_TYPE_P (type
))
7638 for (binfo
= TYPE_BINFO (type
), i
= 0;
7639 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
7640 if (contains_empty_class_p (BINFO_TYPE (base_binfo
)))
7642 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
7643 if (TREE_CODE (field
) == FIELD_DECL
7644 && !DECL_ARTIFICIAL (field
)
7645 && is_empty_class (TREE_TYPE (field
)))
7648 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7649 return contains_empty_class_p (TREE_TYPE (type
));
7653 /* Returns true if TYPE contains no actual data, just various
7654 possible combinations of empty classes and possibly a vptr. */
7657 is_really_empty_class (tree type
)
7659 if (CLASS_TYPE_P (type
))
7666 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7667 out, but we'd like to be able to check this before then. */
7668 if (COMPLETE_TYPE_P (type
) && is_empty_class (type
))
7671 for (binfo
= TYPE_BINFO (type
), i
= 0;
7672 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
7673 if (!is_really_empty_class (BINFO_TYPE (base_binfo
)))
7675 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
7676 if (TREE_CODE (field
) == FIELD_DECL
7677 && !DECL_ARTIFICIAL (field
)
7678 && !is_really_empty_class (TREE_TYPE (field
)))
7682 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7683 return is_really_empty_class (TREE_TYPE (type
));
7687 /* Note that NAME was looked up while the current class was being
7688 defined and that the result of that lookup was DECL. */
7691 maybe_note_name_used_in_class (tree name
, tree decl
)
7693 splay_tree names_used
;
7695 /* If we're not defining a class, there's nothing to do. */
7696 if (!(innermost_scope_kind() == sk_class
7697 && TYPE_BEING_DEFINED (current_class_type
)
7698 && !LAMBDA_TYPE_P (current_class_type
)))
7701 /* If there's already a binding for this NAME, then we don't have
7702 anything to worry about. */
7703 if (lookup_member (current_class_type
, name
,
7704 /*protect=*/0, /*want_type=*/false, tf_warning_or_error
))
7707 if (!current_class_stack
[current_class_depth
- 1].names_used
)
7708 current_class_stack
[current_class_depth
- 1].names_used
7709 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
7710 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
7712 splay_tree_insert (names_used
,
7713 (splay_tree_key
) name
,
7714 (splay_tree_value
) decl
);
7717 /* Note that NAME was declared (as DECL) in the current class. Check
7718 to see that the declaration is valid. */
7721 note_name_declared_in_class (tree name
, tree decl
)
7723 splay_tree names_used
;
7726 /* Look to see if we ever used this name. */
7728 = current_class_stack
[current_class_depth
- 1].names_used
;
7731 /* The C language allows members to be declared with a type of the same
7732 name, and the C++ standard says this diagnostic is not required. So
7733 allow it in extern "C" blocks unless predantic is specified.
7734 Allow it in all cases if -ms-extensions is specified. */
7735 if ((!pedantic
&& current_lang_name
== lang_name_c
)
7736 || flag_ms_extensions
)
7738 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
7741 /* [basic.scope.class]
7743 A name N used in a class S shall refer to the same declaration
7744 in its context and when re-evaluated in the completed scope of
7746 permerror (input_location
, "declaration of %q#D", decl
);
7747 permerror (input_location
, "changes meaning of %qD from %q+#D",
7748 DECL_NAME (OVL_CURRENT (decl
)), (tree
) n
->value
);
7752 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7753 Secondary vtables are merged with primary vtables; this function
7754 will return the VAR_DECL for the primary vtable. */
7757 get_vtbl_decl_for_binfo (tree binfo
)
7761 decl
= BINFO_VTABLE (binfo
);
7762 if (decl
&& TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
7764 gcc_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
);
7765 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
7768 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
7773 /* Returns the binfo for the primary base of BINFO. If the resulting
7774 BINFO is a virtual base, and it is inherited elsewhere in the
7775 hierarchy, then the returned binfo might not be the primary base of
7776 BINFO in the complete object. Check BINFO_PRIMARY_P or
7777 BINFO_LOST_PRIMARY_P to be sure. */
7780 get_primary_binfo (tree binfo
)
7784 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
7788 return copied_binfo (primary_base
, binfo
);
7791 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7794 maybe_indent_hierarchy (FILE * stream
, int indent
, int indented_p
)
7797 fprintf (stream
, "%*s", indent
, "");
7801 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7802 INDENT should be zero when called from the top level; it is
7803 incremented recursively. IGO indicates the next expected BINFO in
7804 inheritance graph ordering. */
7807 dump_class_hierarchy_r (FILE *stream
,
7817 indented
= maybe_indent_hierarchy (stream
, indent
, 0);
7818 fprintf (stream
, "%s (0x" HOST_WIDE_INT_PRINT_HEX
") ",
7819 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
),
7820 (HOST_WIDE_INT
) (uintptr_t) binfo
);
7823 fprintf (stream
, "alternative-path\n");
7826 igo
= TREE_CHAIN (binfo
);
7828 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
7829 tree_low_cst (BINFO_OFFSET (binfo
), 0));
7830 if (is_empty_class (BINFO_TYPE (binfo
)))
7831 fprintf (stream
, " empty");
7832 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
7833 fprintf (stream
, " nearly-empty");
7834 if (BINFO_VIRTUAL_P (binfo
))
7835 fprintf (stream
, " virtual");
7836 fprintf (stream
, "\n");
7839 if (BINFO_PRIMARY_P (binfo
))
7841 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
7842 fprintf (stream
, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX
")",
7843 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
7844 TFF_PLAIN_IDENTIFIER
),
7845 (HOST_WIDE_INT
) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo
));
7847 if (BINFO_LOST_PRIMARY_P (binfo
))
7849 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
7850 fprintf (stream
, " lost-primary");
7853 fprintf (stream
, "\n");
7855 if (!(flags
& TDF_SLIM
))
7859 if (BINFO_SUBVTT_INDEX (binfo
))
7861 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
7862 fprintf (stream
, " subvttidx=%s",
7863 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
7864 TFF_PLAIN_IDENTIFIER
));
7866 if (BINFO_VPTR_INDEX (binfo
))
7868 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
7869 fprintf (stream
, " vptridx=%s",
7870 expr_as_string (BINFO_VPTR_INDEX (binfo
),
7871 TFF_PLAIN_IDENTIFIER
));
7873 if (BINFO_VPTR_FIELD (binfo
))
7875 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
7876 fprintf (stream
, " vbaseoffset=%s",
7877 expr_as_string (BINFO_VPTR_FIELD (binfo
),
7878 TFF_PLAIN_IDENTIFIER
));
7880 if (BINFO_VTABLE (binfo
))
7882 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
7883 fprintf (stream
, " vptr=%s",
7884 expr_as_string (BINFO_VTABLE (binfo
),
7885 TFF_PLAIN_IDENTIFIER
));
7889 fprintf (stream
, "\n");
7892 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
7893 igo
= dump_class_hierarchy_r (stream
, flags
, base_binfo
, igo
, indent
+ 2);
7898 /* Dump the BINFO hierarchy for T. */
7901 dump_class_hierarchy_1 (FILE *stream
, int flags
, tree t
)
7903 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
7904 fprintf (stream
, " size=%lu align=%lu\n",
7905 (unsigned long)(tree_low_cst (TYPE_SIZE (t
), 0) / BITS_PER_UNIT
),
7906 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
7907 fprintf (stream
, " base size=%lu base align=%lu\n",
7908 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t
)), 0)
7910 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t
))
7912 dump_class_hierarchy_r (stream
, flags
, TYPE_BINFO (t
), TYPE_BINFO (t
), 0);
7913 fprintf (stream
, "\n");
7916 /* Debug interface to hierarchy dumping. */
7919 debug_class (tree t
)
7921 dump_class_hierarchy_1 (stderr
, TDF_SLIM
, t
);
7925 dump_class_hierarchy (tree t
)
7928 FILE *stream
= dump_begin (TDI_class
, &flags
);
7932 dump_class_hierarchy_1 (stream
, flags
, t
);
7933 dump_end (TDI_class
, stream
);
7938 dump_array (FILE * stream
, tree decl
)
7941 unsigned HOST_WIDE_INT ix
;
7943 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
7945 elt
= (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))), 0)
7947 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
7948 fprintf (stream
, " %s entries",
7949 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
7950 TFF_PLAIN_IDENTIFIER
));
7951 fprintf (stream
, "\n");
7953 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
7955 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
7956 expr_as_string (value
, TFF_PLAIN_IDENTIFIER
));
7960 dump_vtable (tree t
, tree binfo
, tree vtable
)
7963 FILE *stream
= dump_begin (TDI_class
, &flags
);
7968 if (!(flags
& TDF_SLIM
))
7970 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
7972 fprintf (stream
, "%s for %s",
7973 ctor_vtbl_p
? "Construction vtable" : "Vtable",
7974 type_as_string (BINFO_TYPE (binfo
), TFF_PLAIN_IDENTIFIER
));
7977 if (!BINFO_VIRTUAL_P (binfo
))
7978 fprintf (stream
, " (0x" HOST_WIDE_INT_PRINT_HEX
" instance)",
7979 (HOST_WIDE_INT
) (uintptr_t) binfo
);
7980 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
7982 fprintf (stream
, "\n");
7983 dump_array (stream
, vtable
);
7984 fprintf (stream
, "\n");
7987 dump_end (TDI_class
, stream
);
7991 dump_vtt (tree t
, tree vtt
)
7994 FILE *stream
= dump_begin (TDI_class
, &flags
);
7999 if (!(flags
& TDF_SLIM
))
8001 fprintf (stream
, "VTT for %s\n",
8002 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
8003 dump_array (stream
, vtt
);
8004 fprintf (stream
, "\n");
8007 dump_end (TDI_class
, stream
);
8010 /* Dump a function or thunk and its thunkees. */
8013 dump_thunk (FILE *stream
, int indent
, tree thunk
)
8015 static const char spaces
[] = " ";
8016 tree name
= DECL_NAME (thunk
);
8019 fprintf (stream
, "%.*s%p %s %s", indent
, spaces
,
8021 !DECL_THUNK_P (thunk
) ? "function"
8022 : DECL_THIS_THUNK_P (thunk
) ? "this-thunk" : "covariant-thunk",
8023 name
? IDENTIFIER_POINTER (name
) : "<unset>");
8024 if (DECL_THUNK_P (thunk
))
8026 HOST_WIDE_INT fixed_adjust
= THUNK_FIXED_OFFSET (thunk
);
8027 tree virtual_adjust
= THUNK_VIRTUAL_OFFSET (thunk
);
8029 fprintf (stream
, " fixed=" HOST_WIDE_INT_PRINT_DEC
, fixed_adjust
);
8030 if (!virtual_adjust
)
8032 else if (DECL_THIS_THUNK_P (thunk
))
8033 fprintf (stream
, " vcall=" HOST_WIDE_INT_PRINT_DEC
,
8034 tree_low_cst (virtual_adjust
, 0));
8036 fprintf (stream
, " vbase=" HOST_WIDE_INT_PRINT_DEC
"(%s)",
8037 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust
), 0),
8038 type_as_string (BINFO_TYPE (virtual_adjust
), TFF_SCOPE
));
8039 if (THUNK_ALIAS (thunk
))
8040 fprintf (stream
, " alias to %p", (void *)THUNK_ALIAS (thunk
));
8042 fprintf (stream
, "\n");
8043 for (thunks
= DECL_THUNKS (thunk
); thunks
; thunks
= TREE_CHAIN (thunks
))
8044 dump_thunk (stream
, indent
+ 2, thunks
);
8047 /* Dump the thunks for FN. */
8050 debug_thunks (tree fn
)
8052 dump_thunk (stderr
, 0, fn
);
8055 /* Virtual function table initialization. */
8057 /* Create all the necessary vtables for T and its base classes. */
8060 finish_vtbls (tree t
)
8063 vec
<constructor_elt
, va_gc
> *v
= NULL
;
8064 tree vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
8066 /* We lay out the primary and secondary vtables in one contiguous
8067 vtable. The primary vtable is first, followed by the non-virtual
8068 secondary vtables in inheritance graph order. */
8069 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
), TYPE_BINFO (t
),
8072 /* Then come the virtual bases, also in inheritance graph order. */
8073 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
8075 if (!BINFO_VIRTUAL_P (vbase
))
8077 accumulate_vtbl_inits (vbase
, vbase
, TYPE_BINFO (t
), vtable
, t
, &v
);
8080 if (BINFO_VTABLE (TYPE_BINFO (t
)))
8081 initialize_vtable (TYPE_BINFO (t
), v
);
8084 /* Initialize the vtable for BINFO with the INITS. */
8087 initialize_vtable (tree binfo
, vec
<constructor_elt
, va_gc
> *inits
)
8091 layout_vtable_decl (binfo
, vec_safe_length (inits
));
8092 decl
= get_vtbl_decl_for_binfo (binfo
);
8093 initialize_artificial_var (decl
, inits
);
8094 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
8097 /* Build the VTT (virtual table table) for T.
8098 A class requires a VTT if it has virtual bases.
8101 1 - primary virtual pointer for complete object T
8102 2 - secondary VTTs for each direct non-virtual base of T which requires a
8104 3 - secondary virtual pointers for each direct or indirect base of T which
8105 has virtual bases or is reachable via a virtual path from T.
8106 4 - secondary VTTs for each direct or indirect virtual base of T.
8108 Secondary VTTs look like complete object VTTs without part 4. */
8116 vec
<constructor_elt
, va_gc
> *inits
;
8118 /* Build up the initializers for the VTT. */
8120 index
= size_zero_node
;
8121 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
8123 /* If we didn't need a VTT, we're done. */
8127 /* Figure out the type of the VTT. */
8128 type
= build_array_of_n_type (const_ptr_type_node
,
8131 /* Now, build the VTT object itself. */
8132 vtt
= build_vtable (t
, mangle_vtt_for_type (t
), type
);
8133 initialize_artificial_var (vtt
, inits
);
8134 /* Add the VTT to the vtables list. */
8135 DECL_CHAIN (vtt
) = DECL_CHAIN (CLASSTYPE_VTABLES (t
));
8136 DECL_CHAIN (CLASSTYPE_VTABLES (t
)) = vtt
;
8141 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8142 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8143 and CHAIN the vtable pointer for this binfo after construction is
8144 complete. VALUE can also be another BINFO, in which case we recurse. */
8147 binfo_ctor_vtable (tree binfo
)
8153 vt
= BINFO_VTABLE (binfo
);
8154 if (TREE_CODE (vt
) == TREE_LIST
)
8155 vt
= TREE_VALUE (vt
);
8156 if (TREE_CODE (vt
) == TREE_BINFO
)
8165 /* Data for secondary VTT initialization. */
8166 typedef struct secondary_vptr_vtt_init_data_s
8168 /* Is this the primary VTT? */
8171 /* Current index into the VTT. */
8174 /* Vector of initializers built up. */
8175 vec
<constructor_elt
, va_gc
> *inits
;
8177 /* The type being constructed by this secondary VTT. */
8178 tree type_being_constructed
;
8179 } secondary_vptr_vtt_init_data
;
8181 /* Recursively build the VTT-initializer for BINFO (which is in the
8182 hierarchy dominated by T). INITS points to the end of the initializer
8183 list to date. INDEX is the VTT index where the next element will be
8184 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8185 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8186 for virtual bases of T. When it is not so, we build the constructor
8187 vtables for the BINFO-in-T variant. */
8190 build_vtt_inits (tree binfo
, tree t
, vec
<constructor_elt
, va_gc
> **inits
,
8196 secondary_vptr_vtt_init_data data
;
8197 int top_level_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
8199 /* We only need VTTs for subobjects with virtual bases. */
8200 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
8203 /* We need to use a construction vtable if this is not the primary
8207 build_ctor_vtbl_group (binfo
, t
);
8209 /* Record the offset in the VTT where this sub-VTT can be found. */
8210 BINFO_SUBVTT_INDEX (binfo
) = *index
;
8213 /* Add the address of the primary vtable for the complete object. */
8214 init
= binfo_ctor_vtable (binfo
);
8215 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
8218 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8219 BINFO_VPTR_INDEX (binfo
) = *index
;
8221 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
8223 /* Recursively add the secondary VTTs for non-virtual bases. */
8224 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, b
); ++i
)
8225 if (!BINFO_VIRTUAL_P (b
))
8226 build_vtt_inits (b
, t
, inits
, index
);
8228 /* Add secondary virtual pointers for all subobjects of BINFO with
8229 either virtual bases or reachable along a virtual path, except
8230 subobjects that are non-virtual primary bases. */
8231 data
.top_level_p
= top_level_p
;
8232 data
.index
= *index
;
8233 data
.inits
= *inits
;
8234 data
.type_being_constructed
= BINFO_TYPE (binfo
);
8236 dfs_walk_once (binfo
, dfs_build_secondary_vptr_vtt_inits
, NULL
, &data
);
8238 *index
= data
.index
;
8240 /* data.inits might have grown as we added secondary virtual pointers.
8241 Make sure our caller knows about the new vector. */
8242 *inits
= data
.inits
;
8245 /* Add the secondary VTTs for virtual bases in inheritance graph
8247 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
8249 if (!BINFO_VIRTUAL_P (b
))
8252 build_vtt_inits (b
, t
, inits
, index
);
8255 /* Remove the ctor vtables we created. */
8256 dfs_walk_all (binfo
, dfs_fixup_binfo_vtbls
, NULL
, binfo
);
8259 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8260 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8263 dfs_build_secondary_vptr_vtt_inits (tree binfo
, void *data_
)
8265 secondary_vptr_vtt_init_data
*data
= (secondary_vptr_vtt_init_data
*)data_
;
8267 /* We don't care about bases that don't have vtables. */
8268 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
8269 return dfs_skip_bases
;
8271 /* We're only interested in proper subobjects of the type being
8273 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), data
->type_being_constructed
))
8276 /* We're only interested in bases with virtual bases or reachable
8277 via a virtual path from the type being constructed. */
8278 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
8279 || binfo_via_virtual (binfo
, data
->type_being_constructed
)))
8280 return dfs_skip_bases
;
8282 /* We're not interested in non-virtual primary bases. */
8283 if (!BINFO_VIRTUAL_P (binfo
) && BINFO_PRIMARY_P (binfo
))
8286 /* Record the index where this secondary vptr can be found. */
8287 if (data
->top_level_p
)
8289 gcc_assert (!BINFO_VPTR_INDEX (binfo
));
8290 BINFO_VPTR_INDEX (binfo
) = data
->index
;
8292 if (BINFO_VIRTUAL_P (binfo
))
8294 /* It's a primary virtual base, and this is not a
8295 construction vtable. Find the base this is primary of in
8296 the inheritance graph, and use that base's vtable
8298 while (BINFO_PRIMARY_P (binfo
))
8299 binfo
= BINFO_INHERITANCE_CHAIN (binfo
);
8303 /* Add the initializer for the secondary vptr itself. */
8304 CONSTRUCTOR_APPEND_ELT (data
->inits
, NULL_TREE
, binfo_ctor_vtable (binfo
));
8306 /* Advance the vtt index. */
8307 data
->index
= size_binop (PLUS_EXPR
, data
->index
,
8308 TYPE_SIZE_UNIT (ptr_type_node
));
8313 /* Called from build_vtt_inits via dfs_walk. After building
8314 constructor vtables and generating the sub-vtt from them, we need
8315 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8316 binfo of the base whose sub vtt was generated. */
8319 dfs_fixup_binfo_vtbls (tree binfo
, void* data
)
8321 tree vtable
= BINFO_VTABLE (binfo
);
8323 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
8324 /* If this class has no vtable, none of its bases do. */
8325 return dfs_skip_bases
;
8328 /* This might be a primary base, so have no vtable in this
8332 /* If we scribbled the construction vtable vptr into BINFO, clear it
8334 if (TREE_CODE (vtable
) == TREE_LIST
8335 && (TREE_PURPOSE (vtable
) == (tree
) data
))
8336 BINFO_VTABLE (binfo
) = TREE_CHAIN (vtable
);
8341 /* Build the construction vtable group for BINFO which is in the
8342 hierarchy dominated by T. */
8345 build_ctor_vtbl_group (tree binfo
, tree t
)
8351 vec
<constructor_elt
, va_gc
> *v
;
8353 /* See if we've already created this construction vtable group. */
8354 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
8355 if (IDENTIFIER_GLOBAL_VALUE (id
))
8358 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
));
8359 /* Build a version of VTBL (with the wrong type) for use in
8360 constructing the addresses of secondary vtables in the
8361 construction vtable group. */
8362 vtbl
= build_vtable (t
, id
, ptr_type_node
);
8363 DECL_CONSTRUCTION_VTABLE_P (vtbl
) = 1;
8366 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
8367 binfo
, vtbl
, t
, &v
);
8369 /* Add the vtables for each of our virtual bases using the vbase in T
8371 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
8373 vbase
= TREE_CHAIN (vbase
))
8377 if (!BINFO_VIRTUAL_P (vbase
))
8379 b
= copied_binfo (vbase
, binfo
);
8381 accumulate_vtbl_inits (b
, vbase
, binfo
, vtbl
, t
, &v
);
8384 /* Figure out the type of the construction vtable. */
8385 type
= build_array_of_n_type (vtable_entry_type
, v
->length ());
8387 TREE_TYPE (vtbl
) = type
;
8388 DECL_SIZE (vtbl
) = DECL_SIZE_UNIT (vtbl
) = NULL_TREE
;
8389 layout_decl (vtbl
, 0);
8391 /* Initialize the construction vtable. */
8392 CLASSTYPE_VTABLES (t
) = chainon (CLASSTYPE_VTABLES (t
), vtbl
);
8393 initialize_artificial_var (vtbl
, v
);
8394 dump_vtable (t
, binfo
, vtbl
);
8397 /* Add the vtbl initializers for BINFO (and its bases other than
8398 non-virtual primaries) to the list of INITS. BINFO is in the
8399 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8400 the constructor the vtbl inits should be accumulated for. (If this
8401 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8402 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8403 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8404 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8405 but are not necessarily the same in terms of layout. */
8408 accumulate_vtbl_inits (tree binfo
,
8413 vec
<constructor_elt
, va_gc
> **inits
)
8417 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
8419 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), BINFO_TYPE (orig_binfo
)));
8421 /* If it doesn't have a vptr, we don't do anything. */
8422 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
8425 /* If we're building a construction vtable, we're not interested in
8426 subobjects that don't require construction vtables. */
8428 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
))
8429 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
8432 /* Build the initializers for the BINFO-in-T vtable. */
8433 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, vtbl
, t
, inits
);
8435 /* Walk the BINFO and its bases. We walk in preorder so that as we
8436 initialize each vtable we can figure out at what offset the
8437 secondary vtable lies from the primary vtable. We can't use
8438 dfs_walk here because we need to iterate through bases of BINFO
8439 and RTTI_BINFO simultaneously. */
8440 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8442 /* Skip virtual bases. */
8443 if (BINFO_VIRTUAL_P (base_binfo
))
8445 accumulate_vtbl_inits (base_binfo
,
8446 BINFO_BASE_BINFO (orig_binfo
, i
),
8447 rtti_binfo
, vtbl
, t
,
8452 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8453 BINFO vtable to L. */
8456 dfs_accumulate_vtbl_inits (tree binfo
,
8461 vec
<constructor_elt
, va_gc
> **l
)
8463 tree vtbl
= NULL_TREE
;
8464 int ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
8468 && BINFO_VIRTUAL_P (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
8470 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8471 primary virtual base. If it is not the same primary in
8472 the hierarchy of T, we'll need to generate a ctor vtable
8473 for it, to place at its location in T. If it is the same
8474 primary, we still need a VTT entry for the vtable, but it
8475 should point to the ctor vtable for the base it is a
8476 primary for within the sub-hierarchy of RTTI_BINFO.
8478 There are three possible cases:
8480 1) We are in the same place.
8481 2) We are a primary base within a lost primary virtual base of
8483 3) We are primary to something not a base of RTTI_BINFO. */
8486 tree last
= NULL_TREE
;
8488 /* First, look through the bases we are primary to for RTTI_BINFO
8489 or a virtual base. */
8491 while (BINFO_PRIMARY_P (b
))
8493 b
= BINFO_INHERITANCE_CHAIN (b
);
8495 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
8498 /* If we run out of primary links, keep looking down our
8499 inheritance chain; we might be an indirect primary. */
8500 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
8501 if (BINFO_VIRTUAL_P (b
) || b
== rtti_binfo
)
8505 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8506 base B and it is a base of RTTI_BINFO, this is case 2. In
8507 either case, we share our vtable with LAST, i.e. the
8508 derived-most base within B of which we are a primary. */
8510 || (b
&& binfo_for_vbase (BINFO_TYPE (b
), BINFO_TYPE (rtti_binfo
))))
8511 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8512 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8513 binfo_ctor_vtable after everything's been set up. */
8516 /* Otherwise, this is case 3 and we get our own. */
8518 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
))
8521 n_inits
= vec_safe_length (*l
);
8528 /* Add the initializer for this vtable. */
8529 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
8530 &non_fn_entries
, l
);
8532 /* Figure out the position to which the VPTR should point. */
8533 vtbl
= build1 (ADDR_EXPR
, vtbl_ptr_type_node
, orig_vtbl
);
8534 index
= size_binop (MULT_EXPR
,
8535 TYPE_SIZE_UNIT (vtable_entry_type
),
8536 size_int (non_fn_entries
+ n_inits
));
8537 vtbl
= fold_build_pointer_plus (vtbl
, index
);
8541 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8542 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8543 straighten this out. */
8544 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
8545 else if (BINFO_PRIMARY_P (binfo
) && BINFO_VIRTUAL_P (binfo
))
8546 /* Throw away any unneeded intializers. */
8547 (*l
)->truncate (n_inits
);
8549 /* For an ordinary vtable, set BINFO_VTABLE. */
8550 BINFO_VTABLE (binfo
) = vtbl
;
8553 static GTY(()) tree abort_fndecl_addr
;
8555 /* Construct the initializer for BINFO's virtual function table. BINFO
8556 is part of the hierarchy dominated by T. If we're building a
8557 construction vtable, the ORIG_BINFO is the binfo we should use to
8558 find the actual function pointers to put in the vtable - but they
8559 can be overridden on the path to most-derived in the graph that
8560 ORIG_BINFO belongs. Otherwise,
8561 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8562 BINFO that should be indicated by the RTTI information in the
8563 vtable; it will be a base class of T, rather than T itself, if we
8564 are building a construction vtable.
8566 The value returned is a TREE_LIST suitable for wrapping in a
8567 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8568 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8569 number of non-function entries in the vtable.
8571 It might seem that this function should never be called with a
8572 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8573 base is always subsumed by a derived class vtable. However, when
8574 we are building construction vtables, we do build vtables for
8575 primary bases; we need these while the primary base is being
8579 build_vtbl_initializer (tree binfo
,
8583 int* non_fn_entries_p
,
8584 vec
<constructor_elt
, va_gc
> **inits
)
8590 vec
<tree
, va_gc
> *vbases
;
8593 /* Initialize VID. */
8594 memset (&vid
, 0, sizeof (vid
));
8597 vid
.rtti_binfo
= rtti_binfo
;
8598 vid
.primary_vtbl_p
= SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), t
);
8599 vid
.ctor_vtbl_p
= !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo
), t
);
8600 vid
.generate_vcall_entries
= true;
8601 /* The first vbase or vcall offset is at index -3 in the vtable. */
8602 vid
.index
= ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
8604 /* Add entries to the vtable for RTTI. */
8605 build_rtti_vtbl_entries (binfo
, &vid
);
8607 /* Create an array for keeping track of the functions we've
8608 processed. When we see multiple functions with the same
8609 signature, we share the vcall offsets. */
8610 vec_alloc (vid
.fns
, 32);
8611 /* Add the vcall and vbase offset entries. */
8612 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
8614 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8615 build_vbase_offset_vtbl_entries. */
8616 for (vbases
= CLASSTYPE_VBASECLASSES (t
), ix
= 0;
8617 vec_safe_iterate (vbases
, ix
, &vbinfo
); ix
++)
8618 BINFO_VTABLE_PATH_MARKED (vbinfo
) = 0;
8620 /* If the target requires padding between data entries, add that now. */
8621 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE
> 1)
8623 int n_entries
= vec_safe_length (vid
.inits
);
8625 vec_safe_grow (vid
.inits
, TARGET_VTABLE_DATA_ENTRY_DISTANCE
* n_entries
);
8627 /* Move data entries into their new positions and add padding
8628 after the new positions. Iterate backwards so we don't
8629 overwrite entries that we would need to process later. */
8630 for (ix
= n_entries
- 1;
8631 vid
.inits
->iterate (ix
, &e
);
8635 int new_position
= (TARGET_VTABLE_DATA_ENTRY_DISTANCE
* ix
8636 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE
- 1));
8638 (*vid
.inits
)[new_position
] = *e
;
8640 for (j
= 1; j
< TARGET_VTABLE_DATA_ENTRY_DISTANCE
; ++j
)
8642 constructor_elt
*f
= &(*vid
.inits
)[new_position
- j
];
8643 f
->index
= NULL_TREE
;
8644 f
->value
= build1 (NOP_EXPR
, vtable_entry_type
,
8650 if (non_fn_entries_p
)
8651 *non_fn_entries_p
= vec_safe_length (vid
.inits
);
8653 /* The initializers for virtual functions were built up in reverse
8654 order. Straighten them out and add them to the running list in one
8656 jx
= vec_safe_length (*inits
);
8657 vec_safe_grow (*inits
, jx
+ vid
.inits
->length ());
8659 for (ix
= vid
.inits
->length () - 1;
8660 vid
.inits
->iterate (ix
, &e
);
8664 /* Go through all the ordinary virtual functions, building up
8666 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
8670 tree fn
, fn_original
;
8671 tree init
= NULL_TREE
;
8675 if (DECL_THUNK_P (fn
))
8677 if (!DECL_NAME (fn
))
8679 if (THUNK_ALIAS (fn
))
8681 fn
= THUNK_ALIAS (fn
);
8684 fn_original
= THUNK_TARGET (fn
);
8687 /* If the only definition of this function signature along our
8688 primary base chain is from a lost primary, this vtable slot will
8689 never be used, so just zero it out. This is important to avoid
8690 requiring extra thunks which cannot be generated with the function.
8692 We first check this in update_vtable_entry_for_fn, so we handle
8693 restored primary bases properly; we also need to do it here so we
8694 zero out unused slots in ctor vtables, rather than filling them
8695 with erroneous values (though harmless, apart from relocation
8697 if (BV_LOST_PRIMARY (v
))
8698 init
= size_zero_node
;
8702 /* Pull the offset for `this', and the function to call, out of
8704 delta
= BV_DELTA (v
);
8705 vcall_index
= BV_VCALL_INDEX (v
);
8707 gcc_assert (TREE_CODE (delta
) == INTEGER_CST
);
8708 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8710 /* You can't call an abstract virtual function; it's abstract.
8711 So, we replace these functions with __pure_virtual. */
8712 if (DECL_PURE_VIRTUAL_P (fn_original
))
8715 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
8717 if (abort_fndecl_addr
== NULL
)
8719 = fold_convert (vfunc_ptr_type_node
,
8720 build_fold_addr_expr (fn
));
8721 init
= abort_fndecl_addr
;
8724 /* Likewise for deleted virtuals. */
8725 else if (DECL_DELETED_FN (fn_original
))
8727 fn
= get_identifier ("__cxa_deleted_virtual");
8728 if (!get_global_value_if_present (fn
, &fn
))
8729 fn
= push_library_fn (fn
, (build_function_type_list
8730 (void_type_node
, NULL_TREE
)),
8732 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
8733 init
= fold_convert (vfunc_ptr_type_node
,
8734 build_fold_addr_expr (fn
));
8738 if (!integer_zerop (delta
) || vcall_index
)
8740 fn
= make_thunk (fn
, /*this_adjusting=*/1, delta
, vcall_index
);
8741 if (!DECL_NAME (fn
))
8744 /* Take the address of the function, considering it to be of an
8745 appropriate generic type. */
8746 if (!TARGET_VTABLE_USES_DESCRIPTORS
)
8747 init
= fold_convert (vfunc_ptr_type_node
,
8748 build_fold_addr_expr (fn
));
8752 /* And add it to the chain of initializers. */
8753 if (TARGET_VTABLE_USES_DESCRIPTORS
)
8756 if (init
== size_zero_node
)
8757 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
8758 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
8760 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
8762 tree fdesc
= build2 (FDESC_EXPR
, vfunc_ptr_type_node
,
8763 fn
, build_int_cst (NULL_TREE
, i
));
8764 TREE_CONSTANT (fdesc
) = 1;
8766 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, fdesc
);
8770 CONSTRUCTOR_APPEND_ELT (*inits
, NULL_TREE
, init
);
8774 /* Adds to vid->inits the initializers for the vbase and vcall
8775 offsets in BINFO, which is in the hierarchy dominated by T. */
8778 build_vcall_and_vbase_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
8782 /* If this is a derived class, we must first create entries
8783 corresponding to the primary base class. */
8784 b
= get_primary_binfo (binfo
);
8786 build_vcall_and_vbase_vtbl_entries (b
, vid
);
8788 /* Add the vbase entries for this base. */
8789 build_vbase_offset_vtbl_entries (binfo
, vid
);
8790 /* Add the vcall entries for this base. */
8791 build_vcall_offset_vtbl_entries (binfo
, vid
);
8794 /* Returns the initializers for the vbase offset entries in the vtable
8795 for BINFO (which is part of the class hierarchy dominated by T), in
8796 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8797 where the next vbase offset will go. */
8800 build_vbase_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
8804 tree non_primary_binfo
;
8806 /* If there are no virtual baseclasses, then there is nothing to
8808 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo
)))
8813 /* We might be a primary base class. Go up the inheritance hierarchy
8814 until we find the most derived class of which we are a primary base:
8815 it is the offset of that which we need to use. */
8816 non_primary_binfo
= binfo
;
8817 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
8821 /* If we have reached a virtual base, then it must be a primary
8822 base (possibly multi-level) of vid->binfo, or we wouldn't
8823 have called build_vcall_and_vbase_vtbl_entries for it. But it
8824 might be a lost primary, so just skip down to vid->binfo. */
8825 if (BINFO_VIRTUAL_P (non_primary_binfo
))
8827 non_primary_binfo
= vid
->binfo
;
8831 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
8832 if (get_primary_binfo (b
) != non_primary_binfo
)
8834 non_primary_binfo
= b
;
8837 /* Go through the virtual bases, adding the offsets. */
8838 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
8840 vbase
= TREE_CHAIN (vbase
))
8845 if (!BINFO_VIRTUAL_P (vbase
))
8848 /* Find the instance of this virtual base in the complete
8850 b
= copied_binfo (vbase
, binfo
);
8852 /* If we've already got an offset for this virtual base, we
8853 don't need another one. */
8854 if (BINFO_VTABLE_PATH_MARKED (b
))
8856 BINFO_VTABLE_PATH_MARKED (b
) = 1;
8858 /* Figure out where we can find this vbase offset. */
8859 delta
= size_binop (MULT_EXPR
,
8862 TYPE_SIZE_UNIT (vtable_entry_type
)));
8863 if (vid
->primary_vtbl_p
)
8864 BINFO_VPTR_FIELD (b
) = delta
;
8866 if (binfo
!= TYPE_BINFO (t
))
8867 /* The vbase offset had better be the same. */
8868 gcc_assert (tree_int_cst_equal (delta
, BINFO_VPTR_FIELD (vbase
)));
8870 /* The next vbase will come at a more negative offset. */
8871 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
8872 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
8874 /* The initializer is the delta from BINFO to this virtual base.
8875 The vbase offsets go in reverse inheritance-graph order, and
8876 we are walking in inheritance graph order so these end up in
8878 delta
= size_diffop_loc (input_location
,
8879 BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
8881 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
,
8882 fold_build1_loc (input_location
, NOP_EXPR
,
8883 vtable_entry_type
, delta
));
8887 /* Adds the initializers for the vcall offset entries in the vtable
8888 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8892 build_vcall_offset_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
8894 /* We only need these entries if this base is a virtual base. We
8895 compute the indices -- but do not add to the vtable -- when
8896 building the main vtable for a class. */
8897 if (binfo
== TYPE_BINFO (vid
->derived
)
8898 || (BINFO_VIRTUAL_P (binfo
)
8899 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8900 correspond to VID->DERIVED), we are building a primary
8901 construction virtual table. Since this is a primary
8902 virtual table, we do not need the vcall offsets for
8904 && binfo
!= vid
->rtti_binfo
))
8906 /* We need a vcall offset for each of the virtual functions in this
8907 vtable. For example:
8909 class A { virtual void f (); };
8910 class B1 : virtual public A { virtual void f (); };
8911 class B2 : virtual public A { virtual void f (); };
8912 class C: public B1, public B2 { virtual void f (); };
8914 A C object has a primary base of B1, which has a primary base of A. A
8915 C also has a secondary base of B2, which no longer has a primary base
8916 of A. So the B2-in-C construction vtable needs a secondary vtable for
8917 A, which will adjust the A* to a B2* to call f. We have no way of
8918 knowing what (or even whether) this offset will be when we define B2,
8919 so we store this "vcall offset" in the A sub-vtable and look it up in
8920 a "virtual thunk" for B2::f.
8922 We need entries for all the functions in our primary vtable and
8923 in our non-virtual bases' secondary vtables. */
8925 /* If we are just computing the vcall indices -- but do not need
8926 the actual entries -- not that. */
8927 if (!BINFO_VIRTUAL_P (binfo
))
8928 vid
->generate_vcall_entries
= false;
8929 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8930 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
8934 /* Build vcall offsets, starting with those for BINFO. */
8937 add_vcall_offset_vtbl_entries_r (tree binfo
, vtbl_init_data
* vid
)
8943 /* Don't walk into virtual bases -- except, of course, for the
8944 virtual base for which we are building vcall offsets. Any
8945 primary virtual base will have already had its offsets generated
8946 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8947 if (BINFO_VIRTUAL_P (binfo
) && vid
->vbase
!= binfo
)
8950 /* If BINFO has a primary base, process it first. */
8951 primary_binfo
= get_primary_binfo (binfo
);
8953 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
8955 /* Add BINFO itself to the list. */
8956 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
8958 /* Scan the non-primary bases of BINFO. */
8959 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
8960 if (base_binfo
!= primary_binfo
)
8961 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
8964 /* Called from build_vcall_offset_vtbl_entries_r. */
8967 add_vcall_offset_vtbl_entries_1 (tree binfo
, vtbl_init_data
* vid
)
8969 /* Make entries for the rest of the virtuals. */
8970 if (abi_version_at_least (2))
8974 /* The ABI requires that the methods be processed in declaration
8975 order. G++ 3.2 used the order in the vtable. */
8976 for (orig_fn
= TYPE_METHODS (BINFO_TYPE (binfo
));
8978 orig_fn
= DECL_CHAIN (orig_fn
))
8979 if (DECL_VINDEX (orig_fn
))
8980 add_vcall_offset (orig_fn
, binfo
, vid
);
8984 tree derived_virtuals
;
8987 /* If BINFO is a primary base, the most derived class which has
8988 BINFO as a primary base; otherwise, just BINFO. */
8989 tree non_primary_binfo
;
8991 /* We might be a primary base class. Go up the inheritance hierarchy
8992 until we find the most derived class of which we are a primary base:
8993 it is the BINFO_VIRTUALS there that we need to consider. */
8994 non_primary_binfo
= binfo
;
8995 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
8999 /* If we have reached a virtual base, then it must be vid->vbase,
9000 because we ignore other virtual bases in
9001 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
9002 base (possibly multi-level) of vid->binfo, or we wouldn't
9003 have called build_vcall_and_vbase_vtbl_entries for it. But it
9004 might be a lost primary, so just skip down to vid->binfo. */
9005 if (BINFO_VIRTUAL_P (non_primary_binfo
))
9007 gcc_assert (non_primary_binfo
== vid
->vbase
);
9008 non_primary_binfo
= vid
->binfo
;
9012 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
9013 if (get_primary_binfo (b
) != non_primary_binfo
)
9015 non_primary_binfo
= b
;
9018 if (vid
->ctor_vtbl_p
)
9019 /* For a ctor vtable we need the equivalent binfo within the hierarchy
9020 where rtti_binfo is the most derived type. */
9022 = original_binfo (non_primary_binfo
, vid
->rtti_binfo
);
9024 for (base_virtuals
= BINFO_VIRTUALS (binfo
),
9025 derived_virtuals
= BINFO_VIRTUALS (non_primary_binfo
),
9026 orig_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
9028 base_virtuals
= TREE_CHAIN (base_virtuals
),
9029 derived_virtuals
= TREE_CHAIN (derived_virtuals
),
9030 orig_virtuals
= TREE_CHAIN (orig_virtuals
))
9034 /* Find the declaration that originally caused this function to
9035 be present in BINFO_TYPE (binfo). */
9036 orig_fn
= BV_FN (orig_virtuals
);
9038 /* When processing BINFO, we only want to generate vcall slots for
9039 function slots introduced in BINFO. So don't try to generate
9040 one if the function isn't even defined in BINFO. */
9041 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo
), DECL_CONTEXT (orig_fn
)))
9044 add_vcall_offset (orig_fn
, binfo
, vid
);
9049 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9052 add_vcall_offset (tree orig_fn
, tree binfo
, vtbl_init_data
*vid
)
9058 /* If there is already an entry for a function with the same
9059 signature as FN, then we do not need a second vcall offset.
9060 Check the list of functions already present in the derived
9062 FOR_EACH_VEC_SAFE_ELT (vid
->fns
, i
, derived_entry
)
9064 if (same_signature_p (derived_entry
, orig_fn
)
9065 /* We only use one vcall offset for virtual destructors,
9066 even though there are two virtual table entries. */
9067 || (DECL_DESTRUCTOR_P (derived_entry
)
9068 && DECL_DESTRUCTOR_P (orig_fn
)))
9072 /* If we are building these vcall offsets as part of building
9073 the vtable for the most derived class, remember the vcall
9075 if (vid
->binfo
== TYPE_BINFO (vid
->derived
))
9077 tree_pair_s elt
= {orig_fn
, vid
->index
};
9078 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid
->derived
), elt
);
9081 /* The next vcall offset will be found at a more negative
9083 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
,
9084 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE
));
9086 /* Keep track of this function. */
9087 vec_safe_push (vid
->fns
, orig_fn
);
9089 if (vid
->generate_vcall_entries
)
9094 /* Find the overriding function. */
9095 fn
= find_final_overrider (vid
->rtti_binfo
, binfo
, orig_fn
);
9096 if (fn
== error_mark_node
)
9097 vcall_offset
= build_zero_cst (vtable_entry_type
);
9100 base
= TREE_VALUE (fn
);
9102 /* The vbase we're working on is a primary base of
9103 vid->binfo. But it might be a lost primary, so its
9104 BINFO_OFFSET might be wrong, so we just use the
9105 BINFO_OFFSET from vid->binfo. */
9106 vcall_offset
= size_diffop_loc (input_location
,
9107 BINFO_OFFSET (base
),
9108 BINFO_OFFSET (vid
->binfo
));
9109 vcall_offset
= fold_build1_loc (input_location
,
9110 NOP_EXPR
, vtable_entry_type
,
9113 /* Add the initializer to the vtable. */
9114 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, vcall_offset
);
9118 /* Return vtbl initializers for the RTTI entries corresponding to the
9119 BINFO's vtable. The RTTI entries should indicate the object given
9120 by VID->rtti_binfo. */
9123 build_rtti_vtbl_entries (tree binfo
, vtbl_init_data
* vid
)
9131 t
= BINFO_TYPE (vid
->rtti_binfo
);
9133 /* To find the complete object, we will first convert to our most
9134 primary base, and then add the offset in the vtbl to that value. */
9136 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
9137 && !BINFO_LOST_PRIMARY_P (b
))
9141 primary_base
= get_primary_binfo (b
);
9142 gcc_assert (BINFO_PRIMARY_P (primary_base
)
9143 && BINFO_INHERITANCE_CHAIN (primary_base
) == b
);
9146 offset
= size_diffop_loc (input_location
,
9147 BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
9149 /* The second entry is the address of the typeinfo object. */
9151 decl
= build_address (get_tinfo_decl (t
));
9153 decl
= integer_zero_node
;
9155 /* Convert the declaration to a type that can be stored in the
9157 init
= build_nop (vfunc_ptr_type_node
, decl
);
9158 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9160 /* Add the offset-to-top entry. It comes earlier in the vtable than
9161 the typeinfo entry. Convert the offset to look like a
9162 function pointer, so that we can put it in the vtable. */
9163 init
= build_nop (vfunc_ptr_type_node
, offset
);
9164 CONSTRUCTOR_APPEND_ELT (vid
->inits
, NULL_TREE
, init
);
9167 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9171 uniquely_derived_from_p (tree parent
, tree type
)
9173 tree base
= lookup_base (type
, parent
, ba_unique
, NULL
, tf_none
);
9174 return base
&& base
!= error_mark_node
;
9177 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9180 publicly_uniquely_derived_p (tree parent
, tree type
)
9182 tree base
= lookup_base (type
, parent
, ba_ignore_scope
| ba_check
,
9184 return base
&& base
!= error_mark_node
;
9187 #include "gt-cp-class.h"